blob: 5404a6261db91a4fa204a9e2ad839b97530604c7 [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 Schwidefsky3610cce2007-10-22 12:52:47 +020021
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020022#include <asm/pgtable.h>
23#include <asm/pgalloc.h>
24#include <asm/tlb.h>
25#include <asm/tlbflush.h>
Martin Schwidefsky6252d702008-02-09 18:24:37 +010026#include <asm/mmu_context.h>
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020027
28#ifndef CONFIG_64BIT
29#define ALLOC_ORDER 1
Martin Schwidefsky36409f62011-06-06 14:14:41 +020030#define FRAG_MASK 0x0f
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020031#else
32#define ALLOC_ORDER 2
Martin Schwidefsky36409f62011-06-06 14:14:41 +020033#define FRAG_MASK 0x03
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020034#endif
35
Heiko Carstens239a64252009-06-12 10:26:33 +020036
Martin Schwidefsky043d0702011-05-23 10:24:23 +020037unsigned long *crst_table_alloc(struct mm_struct *mm)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020038{
39 struct page *page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
40
41 if (!page)
42 return NULL;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020043 return (unsigned long *) page_to_phys(page);
44}
45
Martin Schwidefsky146e4b32008-02-09 18:24:35 +010046void crst_table_free(struct mm_struct *mm, unsigned long *table)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +020047{
Martin Schwidefsky043d0702011-05-23 10:24:23 +020048 free_pages((unsigned long) table, ALLOC_ORDER);
Martin Schwidefsky80217142010-10-25 16:10:11 +020049}
50
Martin Schwidefsky6252d702008-02-09 18:24:37 +010051#ifdef CONFIG_64BIT
Martin Schwidefsky10607862013-10-28 14:48:30 +010052static void __crst_table_upgrade(void *arg)
53{
54 struct mm_struct *mm = arg;
55
Martin Schwidefskybeef5602014-04-14 15:11:26 +020056 if (current->active_mm == mm) {
57 clear_user_asce();
58 set_user_asce(mm);
59 }
Martin Schwidefsky10607862013-10-28 14:48:30 +010060 __tlb_flush_local();
61}
62
Martin Schwidefsky6252d702008-02-09 18:24:37 +010063int crst_table_upgrade(struct mm_struct *mm, unsigned long limit)
64{
65 unsigned long *table, *pgd;
66 unsigned long entry;
Martin Schwidefsky10607862013-10-28 14:48:30 +010067 int flush;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010068
69 BUG_ON(limit > (1UL << 53));
Martin Schwidefsky10607862013-10-28 14:48:30 +010070 flush = 0;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010071repeat:
Martin Schwidefsky043d0702011-05-23 10:24:23 +020072 table = crst_table_alloc(mm);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010073 if (!table)
74 return -ENOMEM;
Martin Schwidefsky80217142010-10-25 16:10:11 +020075 spin_lock_bh(&mm->page_table_lock);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010076 if (mm->context.asce_limit < limit) {
77 pgd = (unsigned long *) mm->pgd;
78 if (mm->context.asce_limit <= (1UL << 31)) {
79 entry = _REGION3_ENTRY_EMPTY;
80 mm->context.asce_limit = 1UL << 42;
81 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
82 _ASCE_USER_BITS |
83 _ASCE_TYPE_REGION3;
84 } else {
85 entry = _REGION2_ENTRY_EMPTY;
86 mm->context.asce_limit = 1UL << 53;
87 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
88 _ASCE_USER_BITS |
89 _ASCE_TYPE_REGION2;
90 }
91 crst_table_init(table, entry);
92 pgd_populate(mm, (pgd_t *) table, (pud_t *) pgd);
93 mm->pgd = (pgd_t *) table;
Martin Schwidefskyf481bfa2009-03-18 13:27:36 +010094 mm->task_size = mm->context.asce_limit;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010095 table = NULL;
Martin Schwidefsky10607862013-10-28 14:48:30 +010096 flush = 1;
Martin Schwidefsky6252d702008-02-09 18:24:37 +010097 }
Martin Schwidefsky80217142010-10-25 16:10:11 +020098 spin_unlock_bh(&mm->page_table_lock);
Martin Schwidefsky6252d702008-02-09 18:24:37 +010099 if (table)
100 crst_table_free(mm, table);
101 if (mm->context.asce_limit < limit)
102 goto repeat;
Martin Schwidefsky10607862013-10-28 14:48:30 +0100103 if (flush)
104 on_each_cpu(__crst_table_upgrade, mm, 0);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100105 return 0;
106}
107
108void crst_table_downgrade(struct mm_struct *mm, unsigned long limit)
109{
110 pgd_t *pgd;
111
Martin Schwidefsky02a8f3a2014-04-03 13:54:59 +0200112 if (current->active_mm == mm) {
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200113 clear_user_asce();
Martin Schwidefsky10607862013-10-28 14:48:30 +0100114 __tlb_flush_mm(mm);
Martin Schwidefsky02a8f3a2014-04-03 13:54:59 +0200115 }
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100116 while (mm->context.asce_limit > limit) {
117 pgd = mm->pgd;
118 switch (pgd_val(*pgd) & _REGION_ENTRY_TYPE_MASK) {
119 case _REGION_ENTRY_TYPE_R2:
120 mm->context.asce_limit = 1UL << 42;
121 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
122 _ASCE_USER_BITS |
123 _ASCE_TYPE_REGION3;
124 break;
125 case _REGION_ENTRY_TYPE_R3:
126 mm->context.asce_limit = 1UL << 31;
127 mm->context.asce_bits = _ASCE_TABLE_LENGTH |
128 _ASCE_USER_BITS |
129 _ASCE_TYPE_SEGMENT;
130 break;
131 default:
132 BUG();
133 }
134 mm->pgd = (pgd_t *) (pgd_val(*pgd) & _REGION_ENTRY_ORIGIN);
Martin Schwidefskyf481bfa2009-03-18 13:27:36 +0100135 mm->task_size = mm->context.asce_limit;
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100136 crst_table_free(mm, (unsigned long *) pgd);
137 }
Martin Schwidefsky10607862013-10-28 14:48:30 +0100138 if (current->active_mm == mm)
Martin Schwidefskybeef5602014-04-14 15:11:26 +0200139 set_user_asce(mm);
Martin Schwidefsky6252d702008-02-09 18:24:37 +0100140}
141#endif
142
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200143#ifdef CONFIG_PGSTE
144
145/**
146 * gmap_alloc - allocate a guest address space
147 * @mm: pointer to the parent mm_struct
148 *
149 * Returns a guest address space structure.
150 */
151struct gmap *gmap_alloc(struct mm_struct *mm)
152{
153 struct gmap *gmap;
154 struct page *page;
155 unsigned long *table;
156
157 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL);
158 if (!gmap)
159 goto out;
160 INIT_LIST_HEAD(&gmap->crst_list);
161 gmap->mm = mm;
162 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
163 if (!page)
164 goto out_free;
165 list_add(&page->lru, &gmap->crst_list);
166 table = (unsigned long *) page_to_phys(page);
167 crst_table_init(table, _REGION1_ENTRY_EMPTY);
168 gmap->table = table;
Christian Borntraeger480e5922011-09-20 17:07:28 +0200169 gmap->asce = _ASCE_TYPE_REGION1 | _ASCE_TABLE_LENGTH |
170 _ASCE_USER_BITS | __pa(table);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200171 list_add(&gmap->list, &mm->context.gmap_list);
172 return gmap;
173
174out_free:
175 kfree(gmap);
176out:
177 return NULL;
178}
179EXPORT_SYMBOL_GPL(gmap_alloc);
180
181static int gmap_unlink_segment(struct gmap *gmap, unsigned long *table)
182{
183 struct gmap_pgtable *mp;
184 struct gmap_rmap *rmap;
185 struct page *page;
186
Martin Schwidefskye5098612013-07-23 20:57:57 +0200187 if (*table & _SEGMENT_ENTRY_INVALID)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200188 return 0;
189 page = pfn_to_page(*table >> PAGE_SHIFT);
190 mp = (struct gmap_pgtable *) page->index;
191 list_for_each_entry(rmap, &mp->mapper, list) {
192 if (rmap->entry != table)
193 continue;
194 list_del(&rmap->list);
195 kfree(rmap);
196 break;
197 }
Martin Schwidefskye5098612013-07-23 20:57:57 +0200198 *table = mp->vmaddr | _SEGMENT_ENTRY_INVALID | _SEGMENT_ENTRY_PROTECT;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200199 return 1;
200}
201
202static void gmap_flush_tlb(struct gmap *gmap)
203{
204 if (MACHINE_HAS_IDTE)
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200205 __tlb_flush_asce(gmap->mm, (unsigned long) gmap->table |
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200206 _ASCE_TYPE_REGION1);
207 else
208 __tlb_flush_global();
209}
210
211/**
212 * gmap_free - free a guest address space
213 * @gmap: pointer to the guest address space structure
214 */
215void gmap_free(struct gmap *gmap)
216{
217 struct page *page, *next;
218 unsigned long *table;
219 int i;
220
221
222 /* Flush tlb. */
223 if (MACHINE_HAS_IDTE)
Martin Schwidefsky1b948d62014-04-03 13:55:01 +0200224 __tlb_flush_asce(gmap->mm, (unsigned long) gmap->table |
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200225 _ASCE_TYPE_REGION1);
226 else
227 __tlb_flush_global();
228
229 /* Free all segment & region tables. */
230 down_read(&gmap->mm->mmap_sem);
Carsten Ottecc772452011-10-30 15:17:01 +0100231 spin_lock(&gmap->mm->page_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200232 list_for_each_entry_safe(page, next, &gmap->crst_list, lru) {
233 table = (unsigned long *) page_to_phys(page);
234 if ((*table & _REGION_ENTRY_TYPE_MASK) == 0)
235 /* Remove gmap rmap structures for segment table. */
236 for (i = 0; i < PTRS_PER_PMD; i++, table++)
237 gmap_unlink_segment(gmap, table);
238 __free_pages(page, ALLOC_ORDER);
239 }
Carsten Ottecc772452011-10-30 15:17:01 +0100240 spin_unlock(&gmap->mm->page_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200241 up_read(&gmap->mm->mmap_sem);
242 list_del(&gmap->list);
243 kfree(gmap);
244}
245EXPORT_SYMBOL_GPL(gmap_free);
246
247/**
248 * gmap_enable - switch primary space to the guest address space
249 * @gmap: pointer to the guest address space structure
250 */
251void gmap_enable(struct gmap *gmap)
252{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200253 S390_lowcore.gmap = (unsigned long) gmap;
254}
255EXPORT_SYMBOL_GPL(gmap_enable);
256
257/**
258 * gmap_disable - switch back to the standard primary address space
259 * @gmap: pointer to the guest address space structure
260 */
261void gmap_disable(struct gmap *gmap)
262{
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200263 S390_lowcore.gmap = 0UL;
264}
265EXPORT_SYMBOL_GPL(gmap_disable);
266
Carsten Ottea9162f22011-10-30 15:17:00 +0100267/*
268 * gmap_alloc_table is assumed to be called with mmap_sem held
269 */
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200270static int gmap_alloc_table(struct gmap *gmap,
Heiko Carstens984e2a52013-09-06 18:48:58 +0200271 unsigned long *table, unsigned long init)
272 __releases(&gmap->mm->page_table_lock)
273 __acquires(&gmap->mm->page_table_lock)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200274{
275 struct page *page;
276 unsigned long *new;
277
Christian Borntraegerc86cce22011-12-27 11:25:47 +0100278 /* since we dont free the gmap table until gmap_free we can unlock */
279 spin_unlock(&gmap->mm->page_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200280 page = alloc_pages(GFP_KERNEL, ALLOC_ORDER);
Christian Borntraegerc86cce22011-12-27 11:25:47 +0100281 spin_lock(&gmap->mm->page_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200282 if (!page)
283 return -ENOMEM;
284 new = (unsigned long *) page_to_phys(page);
285 crst_table_init(new, init);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200286 if (*table & _REGION_ENTRY_INVALID) {
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200287 list_add(&page->lru, &gmap->crst_list);
288 *table = (unsigned long) new | _REGION_ENTRY_LENGTH |
289 (*table & _REGION_ENTRY_TYPE_MASK);
290 } else
291 __free_pages(page, ALLOC_ORDER);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200292 return 0;
293}
294
295/**
296 * gmap_unmap_segment - unmap segment from the guest address space
297 * @gmap: pointer to the guest address space structure
298 * @addr: address in the guest address space
299 * @len: length of the memory area to unmap
300 *
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100301 * Returns 0 if the unmap succeeded, -EINVAL if not.
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200302 */
303int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
304{
305 unsigned long *table;
306 unsigned long off;
307 int flush;
308
309 if ((to | len) & (PMD_SIZE - 1))
310 return -EINVAL;
311 if (len == 0 || to + len < to)
312 return -EINVAL;
313
314 flush = 0;
315 down_read(&gmap->mm->mmap_sem);
Carsten Ottecc772452011-10-30 15:17:01 +0100316 spin_lock(&gmap->mm->page_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200317 for (off = 0; off < len; off += PMD_SIZE) {
318 /* Walk the guest addr space page table */
319 table = gmap->table + (((to + off) >> 53) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200320 if (*table & _REGION_ENTRY_INVALID)
Carsten Otte05873df2011-09-26 16:40:34 +0200321 goto out;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200322 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
323 table = table + (((to + off) >> 42) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200324 if (*table & _REGION_ENTRY_INVALID)
Carsten Otte05873df2011-09-26 16:40:34 +0200325 goto out;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200326 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
327 table = table + (((to + off) >> 31) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200328 if (*table & _REGION_ENTRY_INVALID)
Carsten Otte05873df2011-09-26 16:40:34 +0200329 goto out;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200330 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
331 table = table + (((to + off) >> 20) & 0x7ff);
332
333 /* Clear segment table entry in guest address space. */
334 flush |= gmap_unlink_segment(gmap, table);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200335 *table = _SEGMENT_ENTRY_INVALID;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200336 }
Carsten Otte05873df2011-09-26 16:40:34 +0200337out:
Carsten Ottecc772452011-10-30 15:17:01 +0100338 spin_unlock(&gmap->mm->page_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200339 up_read(&gmap->mm->mmap_sem);
340 if (flush)
341 gmap_flush_tlb(gmap);
342 return 0;
343}
344EXPORT_SYMBOL_GPL(gmap_unmap_segment);
345
346/**
347 * gmap_mmap_segment - map a segment to the guest address space
348 * @gmap: pointer to the guest address space structure
349 * @from: source address in the parent address space
350 * @to: target address in the guest address space
351 *
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +0100352 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200353 */
354int gmap_map_segment(struct gmap *gmap, unsigned long from,
355 unsigned long to, unsigned long len)
356{
357 unsigned long *table;
358 unsigned long off;
359 int flush;
360
361 if ((from | to | len) & (PMD_SIZE - 1))
362 return -EINVAL;
Martin Schwidefskyee6ee552013-07-26 15:04:03 +0200363 if (len == 0 || from + len > TASK_MAX_SIZE ||
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200364 from + len < from || to + len < to)
365 return -EINVAL;
366
367 flush = 0;
368 down_read(&gmap->mm->mmap_sem);
Carsten Ottecc772452011-10-30 15:17:01 +0100369 spin_lock(&gmap->mm->page_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200370 for (off = 0; off < len; off += PMD_SIZE) {
371 /* Walk the gmap address space page table */
372 table = gmap->table + (((to + off) >> 53) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200373 if ((*table & _REGION_ENTRY_INVALID) &&
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200374 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY))
375 goto out_unmap;
376 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
377 table = table + (((to + off) >> 42) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200378 if ((*table & _REGION_ENTRY_INVALID) &&
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200379 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY))
380 goto out_unmap;
381 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
382 table = table + (((to + off) >> 31) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200383 if ((*table & _REGION_ENTRY_INVALID) &&
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200384 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY))
385 goto out_unmap;
386 table = (unsigned long *) (*table & _REGION_ENTRY_ORIGIN);
387 table = table + (((to + off) >> 20) & 0x7ff);
388
389 /* Store 'from' address in an invalid segment table entry. */
390 flush |= gmap_unlink_segment(gmap, table);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200391 *table = (from + off) | (_SEGMENT_ENTRY_INVALID |
392 _SEGMENT_ENTRY_PROTECT);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200393 }
Carsten Ottecc772452011-10-30 15:17:01 +0100394 spin_unlock(&gmap->mm->page_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200395 up_read(&gmap->mm->mmap_sem);
396 if (flush)
397 gmap_flush_tlb(gmap);
398 return 0;
399
400out_unmap:
Carsten Ottecc772452011-10-30 15:17:01 +0100401 spin_unlock(&gmap->mm->page_table_lock);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200402 up_read(&gmap->mm->mmap_sem);
403 gmap_unmap_segment(gmap, to, len);
404 return -ENOMEM;
405}
406EXPORT_SYMBOL_GPL(gmap_map_segment);
407
Heiko Carstensc5034942012-09-10 16:14:33 +0200408static unsigned long *gmap_table_walk(unsigned long address, struct gmap *gmap)
409{
410 unsigned long *table;
411
412 table = gmap->table + ((address >> 53) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200413 if (unlikely(*table & _REGION_ENTRY_INVALID))
Heiko Carstensc5034942012-09-10 16:14:33 +0200414 return ERR_PTR(-EFAULT);
415 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
416 table = table + ((address >> 42) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200417 if (unlikely(*table & _REGION_ENTRY_INVALID))
Heiko Carstensc5034942012-09-10 16:14:33 +0200418 return ERR_PTR(-EFAULT);
419 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
420 table = table + ((address >> 31) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200421 if (unlikely(*table & _REGION_ENTRY_INVALID))
Heiko Carstensc5034942012-09-10 16:14:33 +0200422 return ERR_PTR(-EFAULT);
423 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
424 table = table + ((address >> 20) & 0x7ff);
425 return table;
426}
427
428/**
429 * __gmap_translate - translate a guest address to a user space address
430 * @address: guest address
431 * @gmap: pointer to guest mapping meta data structure
432 *
433 * Returns user space address which corresponds to the guest address or
434 * -EFAULT if no such mapping exists.
435 * This function does not establish potentially missing page table entries.
436 * The mmap_sem of the mm that belongs to the address space must be held
437 * when this function gets called.
438 */
439unsigned long __gmap_translate(unsigned long address, struct gmap *gmap)
440{
441 unsigned long *segment_ptr, vmaddr, segment;
442 struct gmap_pgtable *mp;
443 struct page *page;
444
445 current->thread.gmap_addr = address;
446 segment_ptr = gmap_table_walk(address, gmap);
447 if (IS_ERR(segment_ptr))
448 return PTR_ERR(segment_ptr);
449 /* Convert the gmap address to an mm address. */
450 segment = *segment_ptr;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200451 if (!(segment & _SEGMENT_ENTRY_INVALID)) {
Heiko Carstensc5034942012-09-10 16:14:33 +0200452 page = pfn_to_page(segment >> PAGE_SHIFT);
453 mp = (struct gmap_pgtable *) page->index;
454 return mp->vmaddr | (address & ~PMD_MASK);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200455 } else if (segment & _SEGMENT_ENTRY_PROTECT) {
Heiko Carstensc5034942012-09-10 16:14:33 +0200456 vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
457 return vmaddr | (address & ~PMD_MASK);
458 }
459 return -EFAULT;
460}
461EXPORT_SYMBOL_GPL(__gmap_translate);
462
463/**
464 * gmap_translate - translate a guest address to a user space address
465 * @address: guest address
466 * @gmap: pointer to guest mapping meta data structure
467 *
468 * Returns user space address which corresponds to the guest address or
469 * -EFAULT if no such mapping exists.
470 * This function does not establish potentially missing page table entries.
471 */
472unsigned long gmap_translate(unsigned long address, struct gmap *gmap)
473{
474 unsigned long rc;
475
476 down_read(&gmap->mm->mmap_sem);
477 rc = __gmap_translate(address, gmap);
478 up_read(&gmap->mm->mmap_sem);
479 return rc;
480}
481EXPORT_SYMBOL_GPL(gmap_translate);
482
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200483static int gmap_connect_pgtable(unsigned long address, unsigned long segment,
484 unsigned long *segment_ptr, struct gmap *gmap)
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200485{
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200486 unsigned long vmaddr;
Heiko Carstensc5034942012-09-10 16:14:33 +0200487 struct vm_area_struct *vma;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200488 struct gmap_pgtable *mp;
489 struct gmap_rmap *rmap;
Heiko Carstensc5034942012-09-10 16:14:33 +0200490 struct mm_struct *mm;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200491 struct page *page;
492 pgd_t *pgd;
493 pud_t *pud;
494 pmd_t *pmd;
495
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200496 mm = gmap->mm;
497 vmaddr = segment & _SEGMENT_ENTRY_ORIGIN;
498 vma = find_vma(mm, vmaddr);
499 if (!vma || vma->vm_start > vmaddr)
500 return -EFAULT;
501 /* Walk the parent mm page table */
502 pgd = pgd_offset(mm, vmaddr);
503 pud = pud_alloc(mm, pgd, vmaddr);
504 if (!pud)
505 return -ENOMEM;
506 pmd = pmd_alloc(mm, pud, vmaddr);
507 if (!pmd)
508 return -ENOMEM;
509 if (!pmd_present(*pmd) &&
510 __pte_alloc(mm, vma, pmd, vmaddr))
511 return -ENOMEM;
Alex Thorlton1e1836e2014-04-07 15:37:09 -0700512 /* large pmds cannot yet be handled */
513 if (pmd_large(*pmd))
514 return -EFAULT;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200515 /* pmd now points to a valid segment table entry. */
516 rmap = kmalloc(sizeof(*rmap), GFP_KERNEL|__GFP_REPEAT);
517 if (!rmap)
518 return -ENOMEM;
519 /* Link gmap segment table entry location to page table. */
520 page = pmd_page(*pmd);
521 mp = (struct gmap_pgtable *) page->index;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200522 rmap->gmap = gmap;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200523 rmap->entry = segment_ptr;
Christian Borntraegere86cbd82013-05-29 13:08:39 +0200524 rmap->vmaddr = address & PMD_MASK;
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200525 spin_lock(&mm->page_table_lock);
526 if (*segment_ptr == segment) {
527 list_add(&rmap->list, &mp->mapper);
528 /* Set gmap segment table entry to page table. */
529 *segment_ptr = pmd_val(*pmd) & PAGE_MASK;
530 rmap = NULL;
531 }
532 spin_unlock(&mm->page_table_lock);
533 kfree(rmap);
534 return 0;
535}
536
537static void gmap_disconnect_pgtable(struct mm_struct *mm, unsigned long *table)
538{
539 struct gmap_rmap *rmap, *next;
540 struct gmap_pgtable *mp;
541 struct page *page;
542 int flush;
543
544 flush = 0;
545 spin_lock(&mm->page_table_lock);
546 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
547 mp = (struct gmap_pgtable *) page->index;
548 list_for_each_entry_safe(rmap, next, &mp->mapper, list) {
Martin Schwidefskye5098612013-07-23 20:57:57 +0200549 *rmap->entry = mp->vmaddr | (_SEGMENT_ENTRY_INVALID |
550 _SEGMENT_ENTRY_PROTECT);
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200551 list_del(&rmap->list);
552 kfree(rmap);
553 flush = 1;
554 }
555 spin_unlock(&mm->page_table_lock);
556 if (flush)
557 __tlb_flush_global();
558}
559
560/*
561 * this function is assumed to be called with mmap_sem held
562 */
563unsigned long __gmap_fault(unsigned long address, struct gmap *gmap)
564{
565 unsigned long *segment_ptr, segment;
566 struct gmap_pgtable *mp;
567 struct page *page;
568 int rc;
569
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200570 current->thread.gmap_addr = address;
Heiko Carstensc5034942012-09-10 16:14:33 +0200571 segment_ptr = gmap_table_walk(address, gmap);
572 if (IS_ERR(segment_ptr))
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200573 return -EFAULT;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200574 /* Convert the gmap address to an mm address. */
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200575 while (1) {
576 segment = *segment_ptr;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200577 if (!(segment & _SEGMENT_ENTRY_INVALID)) {
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200578 /* Page table is present */
579 page = pfn_to_page(segment >> PAGE_SHIFT);
580 mp = (struct gmap_pgtable *) page->index;
581 return mp->vmaddr | (address & ~PMD_MASK);
582 }
Martin Schwidefskye5098612013-07-23 20:57:57 +0200583 if (!(segment & _SEGMENT_ENTRY_PROTECT))
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200584 /* Nothing mapped in the gmap address space. */
585 break;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200586 rc = gmap_connect_pgtable(address, segment, segment_ptr, gmap);
Martin Schwidefskyab8e5232013-04-16 13:37:46 +0200587 if (rc)
588 return rc;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200589 }
590 return -EFAULT;
Carsten Otte499069e2011-10-30 15:17:02 +0100591}
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200592
Carsten Otte499069e2011-10-30 15:17:02 +0100593unsigned long gmap_fault(unsigned long address, struct gmap *gmap)
594{
595 unsigned long rc;
596
597 down_read(&gmap->mm->mmap_sem);
598 rc = __gmap_fault(address, gmap);
599 up_read(&gmap->mm->mmap_sem);
600
601 return rc;
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200602}
603EXPORT_SYMBOL_GPL(gmap_fault);
604
Konstantin Weitzb31288f2013-04-17 17:36:29 +0200605static void gmap_zap_swap_entry(swp_entry_t entry, struct mm_struct *mm)
606{
607 if (!non_swap_entry(entry))
608 dec_mm_counter(mm, MM_SWAPENTS);
609 else if (is_migration_entry(entry)) {
610 struct page *page = migration_entry_to_page(entry);
611
612 if (PageAnon(page))
613 dec_mm_counter(mm, MM_ANONPAGES);
614 else
615 dec_mm_counter(mm, MM_FILEPAGES);
616 }
617 free_swap_and_cache(entry);
618}
619
620/**
621 * The mm->mmap_sem lock must be held
622 */
623static void gmap_zap_unused(struct mm_struct *mm, unsigned long address)
624{
625 unsigned long ptev, pgstev;
626 spinlock_t *ptl;
627 pgste_t pgste;
628 pte_t *ptep, pte;
629
630 ptep = get_locked_pte(mm, address, &ptl);
631 if (unlikely(!ptep))
632 return;
633 pte = *ptep;
634 if (!pte_swap(pte))
635 goto out_pte;
636 /* Zap unused and logically-zero pages */
637 pgste = pgste_get_lock(ptep);
638 pgstev = pgste_val(pgste);
639 ptev = pte_val(pte);
640 if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
641 ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID))) {
642 gmap_zap_swap_entry(pte_to_swp_entry(pte), mm);
643 pte_clear(mm, address, ptep);
644 }
645 pgste_set_unlock(ptep, pgste);
646out_pte:
647 pte_unmap_unlock(*ptep, ptl);
648}
649
650/*
651 * this function is assumed to be called with mmap_sem held
652 */
653void __gmap_zap(unsigned long address, struct gmap *gmap)
654{
655 unsigned long *table, *segment_ptr;
656 unsigned long segment, pgstev, ptev;
657 struct gmap_pgtable *mp;
658 struct page *page;
659
660 segment_ptr = gmap_table_walk(address, gmap);
661 if (IS_ERR(segment_ptr))
662 return;
663 segment = *segment_ptr;
664 if (segment & _SEGMENT_ENTRY_INVALID)
665 return;
666 page = pfn_to_page(segment >> PAGE_SHIFT);
667 mp = (struct gmap_pgtable *) page->index;
668 address = mp->vmaddr | (address & ~PMD_MASK);
669 /* Page table is present */
670 table = (unsigned long *)(segment & _SEGMENT_ENTRY_ORIGIN);
671 table = table + ((address >> 12) & 0xff);
672 pgstev = table[PTRS_PER_PTE];
673 ptev = table[0];
674 /* quick check, checked again with locks held */
675 if (((pgstev & _PGSTE_GPS_USAGE_MASK) == _PGSTE_GPS_USAGE_UNUSED) ||
676 ((pgstev & _PGSTE_GPS_ZERO) && (ptev & _PAGE_INVALID)))
677 gmap_zap_unused(gmap->mm, address);
678}
679EXPORT_SYMBOL_GPL(__gmap_zap);
680
Christian Borntraeger388186b2011-10-30 15:17:03 +0100681void gmap_discard(unsigned long from, unsigned long to, struct gmap *gmap)
682{
683
684 unsigned long *table, address, size;
685 struct vm_area_struct *vma;
686 struct gmap_pgtable *mp;
687 struct page *page;
688
689 down_read(&gmap->mm->mmap_sem);
690 address = from;
691 while (address < to) {
692 /* Walk the gmap address space page table */
693 table = gmap->table + ((address >> 53) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200694 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
Christian Borntraeger388186b2011-10-30 15:17:03 +0100695 address = (address + PMD_SIZE) & PMD_MASK;
696 continue;
697 }
698 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
699 table = table + ((address >> 42) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200700 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
Christian Borntraeger388186b2011-10-30 15:17:03 +0100701 address = (address + PMD_SIZE) & PMD_MASK;
702 continue;
703 }
704 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
705 table = table + ((address >> 31) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200706 if (unlikely(*table & _REGION_ENTRY_INVALID)) {
Christian Borntraeger388186b2011-10-30 15:17:03 +0100707 address = (address + PMD_SIZE) & PMD_MASK;
708 continue;
709 }
710 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
711 table = table + ((address >> 20) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200712 if (unlikely(*table & _SEGMENT_ENTRY_INVALID)) {
Christian Borntraeger388186b2011-10-30 15:17:03 +0100713 address = (address + PMD_SIZE) & PMD_MASK;
714 continue;
715 }
716 page = pfn_to_page(*table >> PAGE_SHIFT);
717 mp = (struct gmap_pgtable *) page->index;
718 vma = find_vma(gmap->mm, mp->vmaddr);
719 size = min(to - address, PMD_SIZE - (address & ~PMD_MASK));
720 zap_page_range(vma, mp->vmaddr | (address & ~PMD_MASK),
721 size, NULL);
722 address = (address + PMD_SIZE) & PMD_MASK;
723 }
724 up_read(&gmap->mm->mmap_sem);
725}
726EXPORT_SYMBOL_GPL(gmap_discard);
727
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200728static LIST_HEAD(gmap_notifier_list);
729static DEFINE_SPINLOCK(gmap_notifier_lock);
730
731/**
732 * gmap_register_ipte_notifier - register a pte invalidation callback
733 * @nb: pointer to the gmap notifier block
734 */
735void gmap_register_ipte_notifier(struct gmap_notifier *nb)
736{
737 spin_lock(&gmap_notifier_lock);
738 list_add(&nb->list, &gmap_notifier_list);
739 spin_unlock(&gmap_notifier_lock);
740}
741EXPORT_SYMBOL_GPL(gmap_register_ipte_notifier);
742
743/**
744 * gmap_unregister_ipte_notifier - remove a pte invalidation callback
745 * @nb: pointer to the gmap notifier block
746 */
747void gmap_unregister_ipte_notifier(struct gmap_notifier *nb)
748{
749 spin_lock(&gmap_notifier_lock);
750 list_del_init(&nb->list);
751 spin_unlock(&gmap_notifier_lock);
752}
753EXPORT_SYMBOL_GPL(gmap_unregister_ipte_notifier);
754
755/**
756 * gmap_ipte_notify - mark a range of ptes for invalidation notification
757 * @gmap: pointer to guest mapping meta data structure
Dominik Dingelc7c5be72014-03-19 10:13:22 +0100758 * @start: virtual address in the guest address space
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200759 * @len: size of area
760 *
761 * Returns 0 if for each page in the given range a gmap mapping exists and
762 * the invalidation notification could be set. If the gmap mapping is missing
763 * for one or more pages -EFAULT is returned. If no memory could be allocated
764 * -ENOMEM is returned. This function establishes missing page table entries.
765 */
766int gmap_ipte_notify(struct gmap *gmap, unsigned long start, unsigned long len)
767{
768 unsigned long addr;
769 spinlock_t *ptl;
770 pte_t *ptep, entry;
771 pgste_t pgste;
772 int rc = 0;
773
774 if ((start & ~PAGE_MASK) || (len & ~PAGE_MASK))
775 return -EINVAL;
776 down_read(&gmap->mm->mmap_sem);
777 while (len) {
778 /* Convert gmap address and connect the page tables */
779 addr = __gmap_fault(start, gmap);
780 if (IS_ERR_VALUE(addr)) {
781 rc = addr;
782 break;
783 }
784 /* Get the page mapped */
Christian Borntraegerbb4b42c2013-05-08 15:25:38 +0200785 if (fixup_user_fault(current, gmap->mm, addr, FAULT_FLAG_WRITE)) {
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200786 rc = -EFAULT;
787 break;
788 }
789 /* Walk the process page table, lock and get pte pointer */
790 ptep = get_locked_pte(gmap->mm, addr, &ptl);
791 if (unlikely(!ptep))
792 continue;
793 /* Set notification bit in the pgste of the pte */
794 entry = *ptep;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200795 if ((pte_val(entry) & (_PAGE_INVALID | _PAGE_PROTECT)) == 0) {
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200796 pgste = pgste_get_lock(ptep);
Martin Schwidefsky0d0dafc2013-05-17 14:41:33 +0200797 pgste_val(pgste) |= PGSTE_IN_BIT;
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200798 pgste_set_unlock(ptep, pgste);
799 start += PAGE_SIZE;
800 len -= PAGE_SIZE;
801 }
802 spin_unlock(ptl);
803 }
804 up_read(&gmap->mm->mmap_sem);
805 return rc;
806}
807EXPORT_SYMBOL_GPL(gmap_ipte_notify);
808
809/**
810 * gmap_do_ipte_notify - call all invalidation callbacks for a specific pte.
811 * @mm: pointer to the process mm_struct
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200812 * @pte: pointer to the page table entry
813 *
814 * This function is assumed to be called with the page table lock held
815 * for the pte to notify.
816 */
Dominik Dingelaaeff842014-03-19 10:18:49 +0100817void gmap_do_ipte_notify(struct mm_struct *mm, pte_t *pte)
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200818{
819 unsigned long segment_offset;
820 struct gmap_notifier *nb;
821 struct gmap_pgtable *mp;
822 struct gmap_rmap *rmap;
823 struct page *page;
824
825 segment_offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
826 segment_offset = segment_offset * (4096 / sizeof(pte_t));
827 page = pfn_to_page(__pa(pte) >> PAGE_SHIFT);
828 mp = (struct gmap_pgtable *) page->index;
829 spin_lock(&gmap_notifier_lock);
830 list_for_each_entry(rmap, &mp->mapper, list) {
831 list_for_each_entry(nb, &gmap_notifier_list, list)
832 nb->notifier_call(rmap->gmap,
833 rmap->vmaddr + segment_offset);
834 }
835 spin_unlock(&gmap_notifier_lock);
836}
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200837EXPORT_SYMBOL_GPL(gmap_do_ipte_notify);
Martin Schwidefskyd3383632013-04-17 10:53:39 +0200838
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +0200839static inline int page_table_with_pgste(struct page *page)
840{
841 return atomic_read(&page->_mapcount) == 0;
842}
843
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200844static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
845 unsigned long vmaddr)
846{
847 struct page *page;
848 unsigned long *table;
849 struct gmap_pgtable *mp;
850
851 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
852 if (!page)
853 return NULL;
854 mp = kmalloc(sizeof(*mp), GFP_KERNEL|__GFP_REPEAT);
855 if (!mp) {
856 __free_page(page);
857 return NULL;
858 }
Kirill A. Shutemove89cfa52013-11-14 14:31:39 -0800859 if (!pgtable_page_ctor(page)) {
860 kfree(mp);
861 __free_page(page);
862 return NULL;
863 }
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200864 mp->vmaddr = vmaddr & PMD_MASK;
865 INIT_LIST_HEAD(&mp->mapper);
866 page->index = (unsigned long) mp;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +0200867 atomic_set(&page->_mapcount, 0);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200868 table = (unsigned long *) page_to_phys(page);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200869 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
Martin Schwidefsky0a61b222013-10-18 12:03:41 +0200870 clear_table(table + PTRS_PER_PTE, 0, PAGE_SIZE/2);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200871 return table;
872}
873
874static inline void page_table_free_pgste(unsigned long *table)
875{
876 struct page *page;
877 struct gmap_pgtable *mp;
878
879 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
880 mp = (struct gmap_pgtable *) page->index;
881 BUG_ON(!list_empty(&mp->mapper));
Martin Schwidefsky2320c572012-02-17 10:29:21 +0100882 pgtable_page_dtor(page);
Martin Schwidefskye5992f22011-07-24 10:48:20 +0200883 atomic_set(&page->_mapcount, -1);
884 kfree(mp);
885 __free_page(page);
886}
887
Dominik Dingeld4cb1132014-01-29 16:02:32 +0100888static inline unsigned long page_table_reset_pte(struct mm_struct *mm, pmd_t *pmd,
889 unsigned long addr, unsigned long end, bool init_skey)
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200890{
891 pte_t *start_pte, *pte;
892 spinlock_t *ptl;
893 pgste_t pgste;
894
895 start_pte = pte_offset_map_lock(mm, pmd, addr, &ptl);
896 pte = start_pte;
897 do {
898 pgste = pgste_get_lock(pte);
899 pgste_val(pgste) &= ~_PGSTE_GPS_USAGE_MASK;
Dominik Dingeld4cb1132014-01-29 16:02:32 +0100900 if (init_skey) {
901 unsigned long address;
902
903 pgste_val(pgste) &= ~(PGSTE_ACC_BITS | PGSTE_FP_BIT |
904 PGSTE_GR_BIT | PGSTE_GC_BIT);
905
906 /* skip invalid and not writable pages */
907 if (pte_val(*pte) & _PAGE_INVALID ||
908 !(pte_val(*pte) & _PAGE_WRITE)) {
909 pgste_set_unlock(pte, pgste);
910 continue;
911 }
912
913 address = pte_val(*pte) & PAGE_MASK;
914 page_set_storage_key(address, PAGE_DEFAULT_KEY, 1);
915 }
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200916 pgste_set_unlock(pte, pgste);
917 } while (pte++, addr += PAGE_SIZE, addr != end);
918 pte_unmap_unlock(start_pte, ptl);
919
920 return addr;
921}
922
Dominik Dingeld4cb1132014-01-29 16:02:32 +0100923static inline unsigned long page_table_reset_pmd(struct mm_struct *mm, pud_t *pud,
924 unsigned long addr, unsigned long end, bool init_skey)
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200925{
926 unsigned long next;
927 pmd_t *pmd;
928
929 pmd = pmd_offset(pud, addr);
930 do {
931 next = pmd_addr_end(addr, end);
932 if (pmd_none_or_clear_bad(pmd))
933 continue;
Dominik Dingeld4cb1132014-01-29 16:02:32 +0100934 next = page_table_reset_pte(mm, pmd, addr, next, init_skey);
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200935 } while (pmd++, addr = next, addr != end);
936
937 return addr;
938}
939
Dominik Dingeld4cb1132014-01-29 16:02:32 +0100940static inline unsigned long page_table_reset_pud(struct mm_struct *mm, pgd_t *pgd,
941 unsigned long addr, unsigned long end, bool init_skey)
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200942{
943 unsigned long next;
944 pud_t *pud;
945
946 pud = pud_offset(pgd, addr);
947 do {
948 next = pud_addr_end(addr, end);
949 if (pud_none_or_clear_bad(pud))
950 continue;
Dominik Dingeld4cb1132014-01-29 16:02:32 +0100951 next = page_table_reset_pmd(mm, pud, addr, next, init_skey);
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200952 } while (pud++, addr = next, addr != end);
953
954 return addr;
955}
956
Dominik Dingeld4cb1132014-01-29 16:02:32 +0100957void page_table_reset_pgste(struct mm_struct *mm, unsigned long start,
958 unsigned long end, bool init_skey)
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200959{
960 unsigned long addr, next;
961 pgd_t *pgd;
962
Martin Schwidefsky3a801512014-05-16 10:34:11 +0200963 down_write(&mm->mmap_sem);
964 if (init_skey && mm_use_skey(mm))
965 goto out_up;
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200966 addr = start;
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200967 pgd = pgd_offset(mm, addr);
968 do {
969 next = pgd_addr_end(addr, end);
970 if (pgd_none_or_clear_bad(pgd))
971 continue;
Dominik Dingeld4cb1132014-01-29 16:02:32 +0100972 next = page_table_reset_pud(mm, pgd, addr, next, init_skey);
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200973 } while (pgd++, addr = next, addr != end);
Martin Schwidefsky3a801512014-05-16 10:34:11 +0200974 if (init_skey)
975 current->mm->context.use_skey = 1;
976out_up:
977 up_write(&mm->mmap_sem);
Martin Schwidefskydeedabb2013-05-21 17:29:52 +0200978}
979EXPORT_SYMBOL(page_table_reset_pgste);
980
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200981int set_guest_storage_key(struct mm_struct *mm, unsigned long addr,
982 unsigned long key, bool nq)
983{
984 spinlock_t *ptl;
985 pgste_t old, new;
986 pte_t *ptep;
987
988 down_read(&mm->mmap_sem);
Christian Borntraegerab3f2852014-08-19 16:19:35 +0200989retry:
Christian Borntraeger24d5dd02013-05-27 10:42:04 +0200990 ptep = get_locked_pte(current->mm, addr, &ptl);
991 if (unlikely(!ptep)) {
992 up_read(&mm->mmap_sem);
993 return -EFAULT;
994 }
Christian Borntraegerab3f2852014-08-19 16:19:35 +0200995 if (!(pte_val(*ptep) & _PAGE_INVALID) &&
996 (pte_val(*ptep) & _PAGE_PROTECT)) {
997 pte_unmap_unlock(*ptep, ptl);
998 if (fixup_user_fault(current, mm, addr, FAULT_FLAG_WRITE)) {
999 up_read(&mm->mmap_sem);
1000 return -EFAULT;
1001 }
1002 goto retry;
1003 }
Christian Borntraeger24d5dd02013-05-27 10:42:04 +02001004
1005 new = old = pgste_get_lock(ptep);
1006 pgste_val(new) &= ~(PGSTE_GR_BIT | PGSTE_GC_BIT |
1007 PGSTE_ACC_BITS | PGSTE_FP_BIT);
1008 pgste_val(new) |= (key & (_PAGE_CHANGED | _PAGE_REFERENCED)) << 48;
1009 pgste_val(new) |= (key & (_PAGE_ACC_BITS | _PAGE_FP_BIT)) << 56;
1010 if (!(pte_val(*ptep) & _PAGE_INVALID)) {
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001011 unsigned long address, bits, skey;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +02001012
1013 address = pte_val(*ptep) & PAGE_MASK;
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001014 skey = (unsigned long) page_get_storage_key(address);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +02001015 bits = skey & (_PAGE_CHANGED | _PAGE_REFERENCED);
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001016 skey = key & (_PAGE_ACC_BITS | _PAGE_FP_BIT);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +02001017 /* Set storage key ACC and FP */
Martin Schwidefsky0944fe32013-07-23 22:11:42 +02001018 page_set_storage_key(address, skey, !nq);
Christian Borntraeger24d5dd02013-05-27 10:42:04 +02001019 /* Merge host changed & referenced into pgste */
1020 pgste_val(new) |= bits << 52;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +02001021 }
1022 /* changing the guest storage key is considered a change of the page */
1023 if ((pgste_val(new) ^ pgste_val(old)) &
1024 (PGSTE_ACC_BITS | PGSTE_FP_BIT | PGSTE_GR_BIT | PGSTE_GC_BIT))
Martin Schwidefsky0a61b222013-10-18 12:03:41 +02001025 pgste_val(new) |= PGSTE_UC_BIT;
Christian Borntraeger24d5dd02013-05-27 10:42:04 +02001026
1027 pgste_set_unlock(ptep, new);
1028 pte_unmap_unlock(*ptep, ptl);
1029 up_read(&mm->mmap_sem);
1030 return 0;
1031}
1032EXPORT_SYMBOL(set_guest_storage_key);
1033
Martin Schwidefskye5992f22011-07-24 10:48:20 +02001034#else /* CONFIG_PGSTE */
1035
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001036static inline int page_table_with_pgste(struct page *page)
1037{
1038 return 0;
1039}
1040
Martin Schwidefskye5992f22011-07-24 10:48:20 +02001041static inline unsigned long *page_table_alloc_pgste(struct mm_struct *mm,
1042 unsigned long vmaddr)
1043{
Jan Glauber944291d2011-08-03 16:44:18 +02001044 return NULL;
Martin Schwidefskye5992f22011-07-24 10:48:20 +02001045}
1046
Dominik Dingeld4cb1132014-01-29 16:02:32 +01001047void page_table_reset_pgste(struct mm_struct *mm, unsigned long start,
1048 unsigned long end, bool init_skey)
1049{
1050}
1051
Martin Schwidefskye5992f22011-07-24 10:48:20 +02001052static inline void page_table_free_pgste(unsigned long *table)
1053{
1054}
1055
Martin Schwidefskyab8e5232013-04-16 13:37:46 +02001056static inline void gmap_disconnect_pgtable(struct mm_struct *mm,
1057 unsigned long *table)
Martin Schwidefskye5992f22011-07-24 10:48:20 +02001058{
1059}
1060
1061#endif /* CONFIG_PGSTE */
1062
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001063static inline unsigned int atomic_xor_bits(atomic_t *v, unsigned int bits)
1064{
1065 unsigned int old, new;
1066
1067 do {
1068 old = atomic_read(v);
1069 new = old ^ bits;
1070 } while (atomic_cmpxchg(v, old, new) != old);
1071 return new;
1072}
1073
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001074/*
1075 * page table entry allocation/free routines.
1076 */
Martin Schwidefskye5992f22011-07-24 10:48:20 +02001077unsigned long *page_table_alloc(struct mm_struct *mm, unsigned long vmaddr)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001078{
Heiko Carstens41459d32012-09-14 11:09:52 +02001079 unsigned long *uninitialized_var(table);
1080 struct page *uninitialized_var(page);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001081 unsigned int mask, bit;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001082
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001083 if (mm_has_pgste(mm))
Martin Schwidefskye5992f22011-07-24 10:48:20 +02001084 return page_table_alloc_pgste(mm, vmaddr);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001085 /* Allocate fragments of a 4K page as 1K/2K page table */
Martin Schwidefsky80217142010-10-25 16:10:11 +02001086 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001087 mask = FRAG_MASK;
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001088 if (!list_empty(&mm->context.pgtable_list)) {
1089 page = list_first_entry(&mm->context.pgtable_list,
1090 struct page, lru);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001091 table = (unsigned long *) page_to_phys(page);
1092 mask = atomic_read(&page->_mapcount);
1093 mask = mask | (mask >> 4);
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001094 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001095 if ((mask & FRAG_MASK) == FRAG_MASK) {
Martin Schwidefsky80217142010-10-25 16:10:11 +02001096 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001097 page = alloc_page(GFP_KERNEL|__GFP_REPEAT);
1098 if (!page)
1099 return NULL;
Kirill A. Shutemove89cfa52013-11-14 14:31:39 -08001100 if (!pgtable_page_ctor(page)) {
1101 __free_page(page);
1102 return NULL;
1103 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001104 atomic_set(&page->_mapcount, 1);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001105 table = (unsigned long *) page_to_phys(page);
Martin Schwidefskye5098612013-07-23 20:57:57 +02001106 clear_table(table, _PAGE_INVALID, PAGE_SIZE);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001107 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001108 list_add(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001109 } else {
1110 for (bit = 1; mask & bit; bit <<= 1)
1111 table += PTRS_PER_PTE;
1112 mask = atomic_xor_bits(&page->_mapcount, bit);
1113 if ((mask & FRAG_MASK) == FRAG_MASK)
1114 list_del(&page->lru);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001115 }
Martin Schwidefsky80217142010-10-25 16:10:11 +02001116 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001117 return table;
1118}
1119
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001120void page_table_free(struct mm_struct *mm, unsigned long *table)
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001121{
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001122 struct page *page;
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001123 unsigned int bit, mask;
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001124
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001125 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1126 if (page_table_with_pgste(page)) {
Martin Schwidefskyab8e5232013-04-16 13:37:46 +02001127 gmap_disconnect_pgtable(mm, table);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001128 return page_table_free_pgste(table);
Martin Schwidefskye5992f22011-07-24 10:48:20 +02001129 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001130 /* Free 1K/2K page table fragment of a 4K page */
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001131 bit = 1 << ((__pa(table) & ~PAGE_MASK)/(PTRS_PER_PTE*sizeof(pte_t)));
Martin Schwidefsky80217142010-10-25 16:10:11 +02001132 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001133 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001134 list_del(&page->lru);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001135 mask = atomic_xor_bits(&page->_mapcount, bit);
1136 if (mask & FRAG_MASK)
1137 list_add(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001138 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001139 if (mask == 0) {
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001140 pgtable_page_dtor(page);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001141 atomic_set(&page->_mapcount, -1);
Martin Schwidefsky146e4b32008-02-09 18:24:35 +01001142 __free_page(page);
1143 }
1144}
Martin Schwidefsky3610cce2007-10-22 12:52:47 +02001145
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001146static void __page_table_free_rcu(void *table, unsigned bit)
1147{
1148 struct page *page;
1149
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001150 if (bit == FRAG_MASK)
1151 return page_table_free_pgste(table);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001152 /* Free 1K/2K page table fragment of a 4K page */
1153 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1154 if (atomic_xor_bits(&page->_mapcount, bit) == 0) {
1155 pgtable_page_dtor(page);
1156 atomic_set(&page->_mapcount, -1);
1157 __free_page(page);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001158 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001159}
1160
1161void page_table_free_rcu(struct mmu_gather *tlb, unsigned long *table)
1162{
1163 struct mm_struct *mm;
1164 struct page *page;
1165 unsigned int bit, mask;
1166
1167 mm = tlb->mm;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001168 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1169 if (page_table_with_pgste(page)) {
Martin Schwidefskyab8e5232013-04-16 13:37:46 +02001170 gmap_disconnect_pgtable(mm, table);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001171 table = (unsigned long *) (__pa(table) | FRAG_MASK);
1172 tlb_remove_table(tlb, table);
1173 return;
Martin Schwidefsky80217142010-10-25 16:10:11 +02001174 }
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001175 bit = 1 << ((__pa(table) & ~PAGE_MASK) / (PTRS_PER_PTE*sizeof(pte_t)));
Martin Schwidefsky80217142010-10-25 16:10:11 +02001176 spin_lock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001177 if ((atomic_read(&page->_mapcount) & FRAG_MASK) != FRAG_MASK)
1178 list_del(&page->lru);
1179 mask = atomic_xor_bits(&page->_mapcount, bit | (bit << 4));
1180 if (mask & FRAG_MASK)
1181 list_add_tail(&page->lru, &mm->context.pgtable_list);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001182 spin_unlock_bh(&mm->context.list_lock);
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001183 table = (unsigned long *) (__pa(table) | (bit << 4));
1184 tlb_remove_table(tlb, table);
Martin Schwidefsky80217142010-10-25 16:10:11 +02001185}
1186
Heiko Carstens63df41d62013-09-06 19:10:48 +02001187static void __tlb_remove_table(void *_table)
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001188{
Martin Schwidefskye73b7ff2011-10-30 15:16:08 +01001189 const unsigned long mask = (FRAG_MASK << 4) | FRAG_MASK;
1190 void *table = (void *)((unsigned long) _table & ~mask);
1191 unsigned type = (unsigned long) _table & mask;
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001192
1193 if (type)
1194 __page_table_free_rcu(table, type);
1195 else
1196 free_pages((unsigned long) table, ALLOC_ORDER);
1197}
1198
Martin Schwidefskycd941542012-04-11 14:28:07 +02001199static void tlb_remove_table_smp_sync(void *arg)
1200{
1201 /* Simply deliver the interrupt */
1202}
1203
1204static void tlb_remove_table_one(void *table)
1205{
1206 /*
1207 * This isn't an RCU grace period and hence the page-tables cannot be
1208 * assumed to be actually RCU-freed.
1209 *
1210 * It is however sufficient for software page-table walkers that rely
1211 * on IRQ disabling. See the comment near struct mmu_table_batch.
1212 */
1213 smp_call_function(tlb_remove_table_smp_sync, NULL, 1);
1214 __tlb_remove_table(table);
1215}
1216
1217static void tlb_remove_table_rcu(struct rcu_head *head)
1218{
1219 struct mmu_table_batch *batch;
1220 int i;
1221
1222 batch = container_of(head, struct mmu_table_batch, rcu);
1223
1224 for (i = 0; i < batch->nr; i++)
1225 __tlb_remove_table(batch->tables[i]);
1226
1227 free_page((unsigned long)batch);
1228}
1229
1230void tlb_table_flush(struct mmu_gather *tlb)
1231{
1232 struct mmu_table_batch **batch = &tlb->batch;
1233
1234 if (*batch) {
Martin Schwidefskycd941542012-04-11 14:28:07 +02001235 call_rcu_sched(&(*batch)->rcu, tlb_remove_table_rcu);
1236 *batch = NULL;
1237 }
1238}
1239
1240void tlb_remove_table(struct mmu_gather *tlb, void *table)
1241{
1242 struct mmu_table_batch **batch = &tlb->batch;
1243
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001244 tlb->mm->context.flush_mm = 1;
Martin Schwidefskycd941542012-04-11 14:28:07 +02001245 if (*batch == NULL) {
1246 *batch = (struct mmu_table_batch *)
1247 __get_free_page(GFP_NOWAIT | __GFP_NOWARN);
1248 if (*batch == NULL) {
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001249 __tlb_flush_mm_lazy(tlb->mm);
Martin Schwidefskycd941542012-04-11 14:28:07 +02001250 tlb_remove_table_one(table);
1251 return;
1252 }
1253 (*batch)->nr = 0;
1254 }
1255 (*batch)->tables[(*batch)->nr++] = table;
1256 if ((*batch)->nr == MAX_TABLE_BATCH)
Martin Schwidefsky5c474a12013-08-16 13:31:40 +02001257 tlb_flush_mmu(tlb);
Martin Schwidefskycd941542012-04-11 14:28:07 +02001258}
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001259
Gerald Schaefer274023d2012-10-08 16:30:21 -07001260#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001261static inline void thp_split_vma(struct vm_area_struct *vma)
Gerald Schaefer274023d2012-10-08 16:30:21 -07001262{
1263 unsigned long addr;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001264
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001265 for (addr = vma->vm_start; addr < vma->vm_end; addr += PAGE_SIZE)
1266 follow_page(vma, addr, FOLL_SPLIT);
Gerald Schaefer274023d2012-10-08 16:30:21 -07001267}
1268
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001269static inline void thp_split_mm(struct mm_struct *mm)
Gerald Schaefer274023d2012-10-08 16:30:21 -07001270{
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001271 struct vm_area_struct *vma;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001272
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001273 for (vma = mm->mmap; vma != NULL; vma = vma->vm_next) {
Gerald Schaefer274023d2012-10-08 16:30:21 -07001274 thp_split_vma(vma);
1275 vma->vm_flags &= ~VM_HUGEPAGE;
1276 vma->vm_flags |= VM_NOHUGEPAGE;
Gerald Schaefer274023d2012-10-08 16:30:21 -07001277 }
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001278 mm->def_flags |= VM_NOHUGEPAGE;
1279}
1280#else
1281static inline void thp_split_mm(struct mm_struct *mm)
1282{
Gerald Schaefer274023d2012-10-08 16:30:21 -07001283}
1284#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
1285
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001286static unsigned long page_table_realloc_pmd(struct mmu_gather *tlb,
1287 struct mm_struct *mm, pud_t *pud,
1288 unsigned long addr, unsigned long end)
1289{
1290 unsigned long next, *table, *new;
1291 struct page *page;
Christian Borntraeger55e42832014-07-25 14:23:29 +02001292 spinlock_t *ptl;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001293 pmd_t *pmd;
1294
1295 pmd = pmd_offset(pud, addr);
1296 do {
1297 next = pmd_addr_end(addr, end);
1298again:
1299 if (pmd_none_or_clear_bad(pmd))
1300 continue;
1301 table = (unsigned long *) pmd_deref(*pmd);
1302 page = pfn_to_page(__pa(table) >> PAGE_SHIFT);
1303 if (page_table_with_pgste(page))
1304 continue;
1305 /* Allocate new page table with pgstes */
1306 new = page_table_alloc_pgste(mm, addr);
Dominik Dingelbe39f192013-10-31 10:01:16 +01001307 if (!new)
1308 return -ENOMEM;
1309
Christian Borntraeger55e42832014-07-25 14:23:29 +02001310 ptl = pmd_lock(mm, pmd);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001311 if (likely((unsigned long *) pmd_deref(*pmd) == table)) {
1312 /* Nuke pmd entry pointing to the "short" page table */
1313 pmdp_flush_lazy(mm, addr, pmd);
1314 pmd_clear(pmd);
1315 /* Copy ptes from old table to new table */
1316 memcpy(new, table, PAGE_SIZE/2);
1317 clear_table(table, _PAGE_INVALID, PAGE_SIZE/2);
1318 /* Establish new table */
1319 pmd_populate(mm, pmd, (pte_t *) new);
1320 /* Free old table with rcu, there might be a walker! */
1321 page_table_free_rcu(tlb, table);
1322 new = NULL;
1323 }
Christian Borntraeger55e42832014-07-25 14:23:29 +02001324 spin_unlock(ptl);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001325 if (new) {
1326 page_table_free_pgste(new);
1327 goto again;
1328 }
1329 } while (pmd++, addr = next, addr != end);
1330
1331 return addr;
1332}
1333
1334static unsigned long page_table_realloc_pud(struct mmu_gather *tlb,
1335 struct mm_struct *mm, pgd_t *pgd,
1336 unsigned long addr, unsigned long end)
1337{
1338 unsigned long next;
1339 pud_t *pud;
1340
1341 pud = pud_offset(pgd, addr);
1342 do {
1343 next = pud_addr_end(addr, end);
1344 if (pud_none_or_clear_bad(pud))
1345 continue;
1346 next = page_table_realloc_pmd(tlb, mm, pud, addr, next);
Dominik Dingelbe39f192013-10-31 10:01:16 +01001347 if (unlikely(IS_ERR_VALUE(next)))
1348 return next;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001349 } while (pud++, addr = next, addr != end);
1350
1351 return addr;
1352}
1353
Dominik Dingelbe39f192013-10-31 10:01:16 +01001354static unsigned long page_table_realloc(struct mmu_gather *tlb, struct mm_struct *mm,
1355 unsigned long addr, unsigned long end)
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001356{
1357 unsigned long next;
1358 pgd_t *pgd;
1359
1360 pgd = pgd_offset(mm, addr);
1361 do {
1362 next = pgd_addr_end(addr, end);
1363 if (pgd_none_or_clear_bad(pgd))
1364 continue;
1365 next = page_table_realloc_pud(tlb, mm, pgd, addr, next);
Dominik Dingelbe39f192013-10-31 10:01:16 +01001366 if (unlikely(IS_ERR_VALUE(next)))
1367 return next;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001368 } while (pgd++, addr = next, addr != end);
Dominik Dingelbe39f192013-10-31 10:01:16 +01001369
1370 return 0;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001371}
1372
Carsten Otte402b0862008-03-25 18:47:10 +01001373/*
1374 * switch on pgstes for its userspace process (for kvm)
1375 */
1376int s390_enable_sie(void)
1377{
1378 struct task_struct *tsk = current;
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001379 struct mm_struct *mm = tsk->mm;
1380 struct mmu_gather tlb;
Carsten Otte402b0862008-03-25 18:47:10 +01001381
Christian Borntraeger74b6b522008-05-21 13:37:29 +02001382 /* Do we have pgstes? if yes, we are done */
Martin Schwidefsky36409f62011-06-06 14:14:41 +02001383 if (mm_has_pgste(tsk->mm))
Christian Borntraeger74b6b522008-05-21 13:37:29 +02001384 return 0;
Carsten Otte402b0862008-03-25 18:47:10 +01001385
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001386 down_write(&mm->mmap_sem);
Gerald Schaefer274023d2012-10-08 16:30:21 -07001387 /* split thp mappings and disable thp for future mappings */
1388 thp_split_mm(mm);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001389 /* Reallocate the page tables with pgstes */
Linus Torvaldsae7a8352013-09-04 18:15:06 -07001390 tlb_gather_mmu(&tlb, mm, 0, TASK_SIZE);
Dominik Dingelbe39f192013-10-31 10:01:16 +01001391 if (!page_table_realloc(&tlb, mm, 0, TASK_SIZE))
1392 mm->context.has_pgste = 1;
Linus Torvaldsae7a8352013-09-04 18:15:06 -07001393 tlb_finish_mmu(&tlb, 0, TASK_SIZE);
Martin Schwidefsky3eabaee2013-07-26 15:04:02 +02001394 up_write(&mm->mmap_sem);
1395 return mm->context.has_pgste ? 0 : -ENOMEM;
Carsten Otte402b0862008-03-25 18:47:10 +01001396}
1397EXPORT_SYMBOL_GPL(s390_enable_sie);
Hans-Joachim Picht7db11a32009-06-16 10:30:26 +02001398
Dominik Dingel934bc132014-01-14 18:10:17 +01001399/*
1400 * Enable storage key handling from now on and initialize the storage
1401 * keys with the default key.
1402 */
1403void s390_enable_skey(void)
1404{
Dominik Dingel934bc132014-01-14 18:10:17 +01001405 page_table_reset_pgste(current->mm, 0, TASK_SIZE, true);
1406}
1407EXPORT_SYMBOL_GPL(s390_enable_skey);
1408
Dominik Dingela0bf4f12014-03-24 14:27:58 +01001409/*
1410 * Test and reset if a guest page is dirty
1411 */
1412bool gmap_test_and_clear_dirty(unsigned long address, struct gmap *gmap)
1413{
1414 pte_t *pte;
1415 spinlock_t *ptl;
1416 bool dirty = false;
1417
1418 pte = get_locked_pte(gmap->mm, address, &ptl);
1419 if (unlikely(!pte))
1420 return false;
1421
1422 if (ptep_test_and_clear_user_dirty(gmap->mm, address, pte))
1423 dirty = true;
1424
1425 spin_unlock(ptl);
1426 return dirty;
1427}
1428EXPORT_SYMBOL_GPL(gmap_test_and_clear_dirty);
1429
Gerald Schaefer75077af2012-10-08 16:30:15 -07001430#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001431int pmdp_clear_flush_young(struct vm_area_struct *vma, unsigned long address,
1432 pmd_t *pmdp)
1433{
1434 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1435 /* No need to flush TLB
1436 * On s390 reference bits are in storage key and never in TLB */
1437 return pmdp_test_and_clear_young(vma, address, pmdp);
1438}
1439
1440int pmdp_set_access_flags(struct vm_area_struct *vma,
1441 unsigned long address, pmd_t *pmdp,
1442 pmd_t entry, int dirty)
1443{
1444 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1445
Martin Schwidefsky152125b2014-07-24 11:03:41 +02001446 entry = pmd_mkyoung(entry);
1447 if (dirty)
1448 entry = pmd_mkdirty(entry);
Gerald Schaefer1ae1c1d2012-10-08 16:30:24 -07001449 if (pmd_same(*pmdp, entry))
1450 return 0;
1451 pmdp_invalidate(vma, address, pmdp);
1452 set_pmd_at(vma->vm_mm, address, pmdp, entry);
1453 return 1;
1454}
1455
Gerald Schaefer75077af2012-10-08 16:30:15 -07001456static void pmdp_splitting_flush_sync(void *arg)
1457{
1458 /* Simply deliver the interrupt */
1459}
1460
1461void pmdp_splitting_flush(struct vm_area_struct *vma, unsigned long address,
1462 pmd_t *pmdp)
1463{
1464 VM_BUG_ON(address & ~HPAGE_PMD_MASK);
1465 if (!test_and_set_bit(_SEGMENT_ENTRY_SPLIT_BIT,
1466 (unsigned long *) pmdp)) {
1467 /* need to serialize against gup-fast (IRQ disabled) */
1468 smp_call_function(pmdp_splitting_flush_sync, NULL, 1);
1469 }
1470}
Gerald Schaefer9501d092012-10-08 16:30:18 -07001471
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001472void pgtable_trans_huge_deposit(struct mm_struct *mm, pmd_t *pmdp,
1473 pgtable_t pgtable)
Gerald Schaefer9501d092012-10-08 16:30:18 -07001474{
1475 struct list_head *lh = (struct list_head *) pgtable;
1476
Martin Schwidefskyec66ad62014-02-12 14:16:18 +01001477 assert_spin_locked(pmd_lockptr(mm, pmdp));
Gerald Schaefer9501d092012-10-08 16:30:18 -07001478
1479 /* FIFO */
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001480 if (!pmd_huge_pte(mm, pmdp))
Gerald Schaefer9501d092012-10-08 16:30:18 -07001481 INIT_LIST_HEAD(lh);
1482 else
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001483 list_add(lh, (struct list_head *) pmd_huge_pte(mm, pmdp));
1484 pmd_huge_pte(mm, pmdp) = pgtable;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001485}
1486
Aneesh Kumar K.V6b0b50b2013-06-05 17:14:02 -07001487pgtable_t pgtable_trans_huge_withdraw(struct mm_struct *mm, pmd_t *pmdp)
Gerald Schaefer9501d092012-10-08 16:30:18 -07001488{
1489 struct list_head *lh;
1490 pgtable_t pgtable;
1491 pte_t *ptep;
1492
Martin Schwidefskyec66ad62014-02-12 14:16:18 +01001493 assert_spin_locked(pmd_lockptr(mm, pmdp));
Gerald Schaefer9501d092012-10-08 16:30:18 -07001494
1495 /* FIFO */
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001496 pgtable = pmd_huge_pte(mm, pmdp);
Gerald Schaefer9501d092012-10-08 16:30:18 -07001497 lh = (struct list_head *) pgtable;
1498 if (list_empty(lh))
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001499 pmd_huge_pte(mm, pmdp) = NULL;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001500 else {
Kirill A. Shutemovc389a252013-11-14 14:30:59 -08001501 pmd_huge_pte(mm, pmdp) = (pgtable_t) lh->next;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001502 list_del(lh);
1503 }
1504 ptep = (pte_t *) pgtable;
Martin Schwidefskye5098612013-07-23 20:57:57 +02001505 pte_val(*ptep) = _PAGE_INVALID;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001506 ptep++;
Martin Schwidefskye5098612013-07-23 20:57:57 +02001507 pte_val(*ptep) = _PAGE_INVALID;
Gerald Schaefer9501d092012-10-08 16:30:18 -07001508 return pgtable;
1509}
Gerald Schaefer75077af2012-10-08 16:30:15 -07001510#endif /* CONFIG_TRANSPARENT_HUGEPAGE */