1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * KVM guest address space mapping code
4 *
5 * Copyright IBM Corp. 2007, 2020
6 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
7 * David Hildenbrand <david@redhat.com>
8 * Janosch Frank <frankja@linux.vnet.ibm.com>
9 */
10
11 #include <linux/kernel.h>
12 #include <linux/pagewalk.h>
13 #include <linux/swap.h>
14 #include <linux/smp.h>
15 #include <linux/spinlock.h>
16 #include <linux/slab.h>
17 #include <linux/swapops.h>
18 #include <linux/ksm.h>
19 #include <linux/mman.h>
20 #include <linux/pgtable.h>
21
22 #include <asm/pgalloc.h>
23 #include <asm/gmap.h>
24 #include <asm/page.h>
25 #include <asm/tlb.h>
26
27 #define GMAP_SHADOW_FAKE_TABLE 1ULL
28
gmap_alloc_crst(void)29 static struct page *gmap_alloc_crst(void)
30 {
31 struct page *page;
32
33 page = alloc_pages(GFP_KERNEL_ACCOUNT, CRST_ALLOC_ORDER);
34 if (!page)
35 return NULL;
36 arch_set_page_dat(page, CRST_ALLOC_ORDER);
37 return page;
38 }
39
40 /**
41 * gmap_alloc - allocate and initialize a guest address space
42 * @limit: maximum address of the gmap address space
43 *
44 * Returns a guest address space structure.
45 */
gmap_alloc(unsigned long limit)46 static struct gmap *gmap_alloc(unsigned long limit)
47 {
48 struct gmap *gmap;
49 struct page *page;
50 unsigned long *table;
51 unsigned long etype, atype;
52
53 if (limit < _REGION3_SIZE) {
54 limit = _REGION3_SIZE - 1;
55 atype = _ASCE_TYPE_SEGMENT;
56 etype = _SEGMENT_ENTRY_EMPTY;
57 } else if (limit < _REGION2_SIZE) {
58 limit = _REGION2_SIZE - 1;
59 atype = _ASCE_TYPE_REGION3;
60 etype = _REGION3_ENTRY_EMPTY;
61 } else if (limit < _REGION1_SIZE) {
62 limit = _REGION1_SIZE - 1;
63 atype = _ASCE_TYPE_REGION2;
64 etype = _REGION2_ENTRY_EMPTY;
65 } else {
66 limit = -1UL;
67 atype = _ASCE_TYPE_REGION1;
68 etype = _REGION1_ENTRY_EMPTY;
69 }
70 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL_ACCOUNT);
71 if (!gmap)
72 goto out;
73 INIT_LIST_HEAD(&gmap->crst_list);
74 INIT_LIST_HEAD(&gmap->children);
75 INIT_LIST_HEAD(&gmap->pt_list);
76 INIT_RADIX_TREE(&gmap->guest_to_host, GFP_KERNEL_ACCOUNT);
77 INIT_RADIX_TREE(&gmap->host_to_guest, GFP_ATOMIC | __GFP_ACCOUNT);
78 INIT_RADIX_TREE(&gmap->host_to_rmap, GFP_ATOMIC | __GFP_ACCOUNT);
79 spin_lock_init(&gmap->guest_table_lock);
80 spin_lock_init(&gmap->shadow_lock);
81 refcount_set(&gmap->ref_count, 1);
82 page = gmap_alloc_crst();
83 if (!page)
84 goto out_free;
85 page->index = 0;
86 list_add(&page->lru, &gmap->crst_list);
87 table = page_to_virt(page);
88 crst_table_init(table, etype);
89 gmap->table = table;
90 gmap->asce = atype | _ASCE_TABLE_LENGTH |
91 _ASCE_USER_BITS | __pa(table);
92 gmap->asce_end = limit;
93 return gmap;
94
95 out_free:
96 kfree(gmap);
97 out:
98 return NULL;
99 }
100
101 /**
102 * gmap_create - create a guest address space
103 * @mm: pointer to the parent mm_struct
104 * @limit: maximum size of the gmap address space
105 *
106 * Returns a guest address space structure.
107 */
gmap_create(struct mm_struct * mm,unsigned long limit)108 struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
109 {
110 struct gmap *gmap;
111 unsigned long gmap_asce;
112
113 gmap = gmap_alloc(limit);
114 if (!gmap)
115 return NULL;
116 gmap->mm = mm;
117 spin_lock(&mm->context.lock);
118 list_add_rcu(&gmap->list, &mm->context.gmap_list);
119 if (list_is_singular(&mm->context.gmap_list))
120 gmap_asce = gmap->asce;
121 else
122 gmap_asce = -1UL;
123 WRITE_ONCE(mm->context.gmap_asce, gmap_asce);
124 spin_unlock(&mm->context.lock);
125 return gmap;
126 }
127 EXPORT_SYMBOL_GPL(gmap_create);
128
gmap_flush_tlb(struct gmap * gmap)129 static void gmap_flush_tlb(struct gmap *gmap)
130 {
131 if (MACHINE_HAS_IDTE)
132 __tlb_flush_idte(gmap->asce);
133 else
134 __tlb_flush_global();
135 }
136
gmap_radix_tree_free(struct radix_tree_root * root)137 static void gmap_radix_tree_free(struct radix_tree_root *root)
138 {
139 struct radix_tree_iter iter;
140 unsigned long indices[16];
141 unsigned long index;
142 void __rcu **slot;
143 int i, nr;
144
145 /* A radix tree is freed by deleting all of its entries */
146 index = 0;
147 do {
148 nr = 0;
149 radix_tree_for_each_slot(slot, root, &iter, index) {
150 indices[nr] = iter.index;
151 if (++nr == 16)
152 break;
153 }
154 for (i = 0; i < nr; i++) {
155 index = indices[i];
156 radix_tree_delete(root, index);
157 }
158 } while (nr > 0);
159 }
160
gmap_rmap_radix_tree_free(struct radix_tree_root * root)161 static void gmap_rmap_radix_tree_free(struct radix_tree_root *root)
162 {
163 struct gmap_rmap *rmap, *rnext, *head;
164 struct radix_tree_iter iter;
165 unsigned long indices[16];
166 unsigned long index;
167 void __rcu **slot;
168 int i, nr;
169
170 /* A radix tree is freed by deleting all of its entries */
171 index = 0;
172 do {
173 nr = 0;
174 radix_tree_for_each_slot(slot, root, &iter, index) {
175 indices[nr] = iter.index;
176 if (++nr == 16)
177 break;
178 }
179 for (i = 0; i < nr; i++) {
180 index = indices[i];
181 head = radix_tree_delete(root, index);
182 gmap_for_each_rmap_safe(rmap, rnext, head)
183 kfree(rmap);
184 }
185 } while (nr > 0);
186 }
187
188 /**
189 * gmap_free - free a guest address space
190 * @gmap: pointer to the guest address space structure
191 *
192 * No locks required. There are no references to this gmap anymore.
193 */
gmap_free(struct gmap * gmap)194 static void gmap_free(struct gmap *gmap)
195 {
196 struct page *page, *next;
197
198 /* Flush tlb of all gmaps (if not already done for shadows) */
199 if (!(gmap_is_shadow(gmap) && gmap->removed))
200 gmap_flush_tlb(gmap);
201 /* Free all segment & region tables. */
202 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
203 __free_pages(page, CRST_ALLOC_ORDER);
204 gmap_radix_tree_free(&gmap->guest_to_host);
205 gmap_radix_tree_free(&gmap->host_to_guest);
206
207 /* Free additional data for a shadow gmap */
208 if (gmap_is_shadow(gmap)) {
209 /* Free all page tables. */
210 list_for_each_entry_safe(page, next, &gmap->pt_list, lru)
211 page_table_free_pgste(page);
212 gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
213 /* Release reference to the parent */
214 gmap_put(gmap->parent);
215 }
216
217 kfree(gmap);
218 }
219
220 /**
221 * gmap_get - increase reference counter for guest address space
222 * @gmap: pointer to the guest address space structure
223 *
224 * Returns the gmap pointer
225 */
gmap_get(struct gmap * gmap)226 struct gmap *gmap_get(struct gmap *gmap)
227 {
228 refcount_inc(&gmap->ref_count);
229 return gmap;
230 }
231 EXPORT_SYMBOL_GPL(gmap_get);
232
233 /**
234 * gmap_put - decrease reference counter for guest address space
235 * @gmap: pointer to the guest address space structure
236 *
237 * If the reference counter reaches zero the guest address space is freed.
238 */
gmap_put(struct gmap * gmap)239 void gmap_put(struct gmap *gmap)
240 {
241 if (refcount_dec_and_test(&gmap->ref_count))
242 gmap_free(gmap);
243 }
244 EXPORT_SYMBOL_GPL(gmap_put);
245
246 /**
247 * gmap_remove - remove a guest address space but do not free it yet
248 * @gmap: pointer to the guest address space structure
249 */
gmap_remove(struct gmap * gmap)250 void gmap_remove(struct gmap *gmap)
251 {
252 struct gmap *sg, *next;
253 unsigned long gmap_asce;
254
255 /* Remove all shadow gmaps linked to this gmap */
256 if (!list_empty(&gmap->children)) {
257 spin_lock(&gmap->shadow_lock);
258 list_for_each_entry_safe(sg, next, &gmap->children, list) {
259 list_del(&sg->list);
260 gmap_put(sg);
261 }
262 spin_unlock(&gmap->shadow_lock);
263 }
264 /* Remove gmap from the pre-mm list */
265 spin_lock(&gmap->mm->context.lock);
266 list_del_rcu(&gmap->list);
267 if (list_empty(&gmap->mm->context.gmap_list))
268 gmap_asce = 0;
269 else if (list_is_singular(&gmap->mm->context.gmap_list))
270 gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
271 struct gmap, list)->asce;
272 else
273 gmap_asce = -1UL;
274 WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
275 spin_unlock(&gmap->mm->context.lock);
276 synchronize_rcu();
277 /* Put reference */
278 gmap_put(gmap);
279 }
280 EXPORT_SYMBOL_GPL(gmap_remove);
281
282 /**
283 * gmap_enable - switch primary space to the guest address space
284 * @gmap: pointer to the guest address space structure
285 */
gmap_enable(struct gmap * gmap)286 void gmap_enable(struct gmap *gmap)
287 {
288 S390_lowcore.gmap = (unsigned long) gmap;
289 }
290 EXPORT_SYMBOL_GPL(gmap_enable);
291
292 /**
293 * gmap_disable - switch back to the standard primary address space
294 * @gmap: pointer to the guest address space structure
295 */
gmap_disable(struct gmap * gmap)296 void gmap_disable(struct gmap *gmap)
297 {
298 S390_lowcore.gmap = 0UL;
299 }
300 EXPORT_SYMBOL_GPL(gmap_disable);
301
302 /**
303 * gmap_get_enabled - get a pointer to the currently enabled gmap
304 *
305 * Returns a pointer to the currently enabled gmap. 0 if none is enabled.
306 */
gmap_get_enabled(void)307 struct gmap *gmap_get_enabled(void)
308 {
309 return (struct gmap *) S390_lowcore.gmap;
310 }
311 EXPORT_SYMBOL_GPL(gmap_get_enabled);
312
313 /*
314 * gmap_alloc_table is assumed to be called with mmap_lock held
315 */
gmap_alloc_table(struct gmap * gmap,unsigned long * table,unsigned long init,unsigned long gaddr)316 static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
317 unsigned long init, unsigned long gaddr)
318 {
319 struct page *page;
320 unsigned long *new;
321
322 /* since we dont free the gmap table until gmap_free we can unlock */
323 page = gmap_alloc_crst();
324 if (!page)
325 return -ENOMEM;
326 new = page_to_virt(page);
327 crst_table_init(new, init);
328 spin_lock(&gmap->guest_table_lock);
329 if (*table & _REGION_ENTRY_INVALID) {
330 list_add(&page->lru, &gmap->crst_list);
331 *table = __pa(new) | _REGION_ENTRY_LENGTH |
332 (*table & _REGION_ENTRY_TYPE_MASK);
333 page->index = gaddr;
334 page = NULL;
335 }
336 spin_unlock(&gmap->guest_table_lock);
337 if (page)
338 __free_pages(page, CRST_ALLOC_ORDER);
339 return 0;
340 }
341
342 /**
343 * __gmap_segment_gaddr - find virtual address from segment pointer
344 * @entry: pointer to a segment table entry in the guest address space
345 *
346 * Returns the virtual address in the guest address space for the segment
347 */
__gmap_segment_gaddr(unsigned long * entry)348 static unsigned long __gmap_segment_gaddr(unsigned long *entry)
349 {
350 struct page *page;
351 unsigned long offset;
352
353 offset = (unsigned long) entry / sizeof(unsigned long);
354 offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
355 page = pmd_pgtable_page((pmd_t *) entry);
356 return page->index + offset;
357 }
358
359 /**
360 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
361 * @gmap: pointer to the guest address space structure
362 * @vmaddr: address in the host process address space
363 *
364 * Returns 1 if a TLB flush is required
365 */
__gmap_unlink_by_vmaddr(struct gmap * gmap,unsigned long vmaddr)366 static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
367 {
368 unsigned long *entry;
369 int flush = 0;
370
371 BUG_ON(gmap_is_shadow(gmap));
372 spin_lock(&gmap->guest_table_lock);
373 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
374 if (entry) {
375 flush = (*entry != _SEGMENT_ENTRY_EMPTY);
376 *entry = _SEGMENT_ENTRY_EMPTY;
377 }
378 spin_unlock(&gmap->guest_table_lock);
379 return flush;
380 }
381
382 /**
383 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
384 * @gmap: pointer to the guest address space structure
385 * @gaddr: address in the guest address space
386 *
387 * Returns 1 if a TLB flush is required
388 */
__gmap_unmap_by_gaddr(struct gmap * gmap,unsigned long gaddr)389 static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
390 {
391 unsigned long vmaddr;
392
393 vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
394 gaddr >> PMD_SHIFT);
395 return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
396 }
397
398 /**
399 * gmap_unmap_segment - unmap segment from the guest address space
400 * @gmap: pointer to the guest address space structure
401 * @to: address in the guest address space
402 * @len: length of the memory area to unmap
403 *
404 * Returns 0 if the unmap succeeded, -EINVAL if not.
405 */
gmap_unmap_segment(struct gmap * gmap,unsigned long to,unsigned long len)406 int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
407 {
408 unsigned long off;
409 int flush;
410
411 BUG_ON(gmap_is_shadow(gmap));
412 if ((to | len) & (PMD_SIZE - 1))
413 return -EINVAL;
414 if (len == 0 || to + len < to)
415 return -EINVAL;
416
417 flush = 0;
418 mmap_write_lock(gmap->mm);
419 for (off = 0; off < len; off += PMD_SIZE)
420 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
421 mmap_write_unlock(gmap->mm);
422 if (flush)
423 gmap_flush_tlb(gmap);
424 return 0;
425 }
426 EXPORT_SYMBOL_GPL(gmap_unmap_segment);
427
428 /**
429 * gmap_map_segment - map a segment to the guest address space
430 * @gmap: pointer to the guest address space structure
431 * @from: source address in the parent address space
432 * @to: target address in the guest address space
433 * @len: length of the memory area to map
434 *
435 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
436 */
gmap_map_segment(struct gmap * gmap,unsigned long from,unsigned long to,unsigned long len)437 int gmap_map_segment(struct gmap *gmap, unsigned long from,
438 unsigned long to, unsigned long len)
439 {
440 unsigned long off;
441 int flush;
442
443 BUG_ON(gmap_is_shadow(gmap));
444 if ((from | to | len) & (PMD_SIZE - 1))
445 return -EINVAL;
446 if (len == 0 || from + len < from || to + len < to ||
447 from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
448 return -EINVAL;
449
450 flush = 0;
451 mmap_write_lock(gmap->mm);
452 for (off = 0; off < len; off += PMD_SIZE) {
453 /* Remove old translation */
454 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
455 /* Store new translation */
456 if (radix_tree_insert(&gmap->guest_to_host,
457 (to + off) >> PMD_SHIFT,
458 (void *) from + off))
459 break;
460 }
461 mmap_write_unlock(gmap->mm);
462 if (flush)
463 gmap_flush_tlb(gmap);
464 if (off >= len)
465 return 0;
466 gmap_unmap_segment(gmap, to, len);
467 return -ENOMEM;
468 }
469 EXPORT_SYMBOL_GPL(gmap_map_segment);
470
471 /**
472 * __gmap_translate - translate a guest address to a user space address
473 * @gmap: pointer to guest mapping meta data structure
474 * @gaddr: guest address
475 *
476 * Returns user space address which corresponds to the guest address or
477 * -EFAULT if no such mapping exists.
478 * This function does not establish potentially missing page table entries.
479 * The mmap_lock of the mm that belongs to the address space must be held
480 * when this function gets called.
481 *
482 * Note: Can also be called for shadow gmaps.
483 */
__gmap_translate(struct gmap * gmap,unsigned long gaddr)484 unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
485 {
486 unsigned long vmaddr;
487
488 vmaddr = (unsigned long)
489 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
490 /* Note: guest_to_host is empty for a shadow gmap */
491 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
492 }
493 EXPORT_SYMBOL_GPL(__gmap_translate);
494
495 /**
496 * gmap_translate - translate a guest address to a user space address
497 * @gmap: pointer to guest mapping meta data structure
498 * @gaddr: guest address
499 *
500 * Returns user space address which corresponds to the guest address or
501 * -EFAULT if no such mapping exists.
502 * This function does not establish potentially missing page table entries.
503 */
gmap_translate(struct gmap * gmap,unsigned long gaddr)504 unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
505 {
506 unsigned long rc;
507
508 mmap_read_lock(gmap->mm);
509 rc = __gmap_translate(gmap, gaddr);
510 mmap_read_unlock(gmap->mm);
511 return rc;
512 }
513 EXPORT_SYMBOL_GPL(gmap_translate);
514
515 /**
516 * gmap_unlink - disconnect a page table from the gmap shadow tables
517 * @mm: pointer to the parent mm_struct
518 * @table: pointer to the host page table
519 * @vmaddr: vm address associated with the host page table
520 */
gmap_unlink(struct mm_struct * mm,unsigned long * table,unsigned long vmaddr)521 void gmap_unlink(struct mm_struct *mm, unsigned long *table,
522 unsigned long vmaddr)
523 {
524 struct gmap *gmap;
525 int flush;
526
527 rcu_read_lock();
528 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
529 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
530 if (flush)
531 gmap_flush_tlb(gmap);
532 }
533 rcu_read_unlock();
534 }
535
536 static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
537 unsigned long gaddr);
538
539 /**
540 * __gmap_link - set up shadow page tables to connect a host to a guest address
541 * @gmap: pointer to guest mapping meta data structure
542 * @gaddr: guest address
543 * @vmaddr: vm address
544 *
545 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
546 * if the vm address is already mapped to a different guest segment.
547 * The mmap_lock of the mm that belongs to the address space must be held
548 * when this function gets called.
549 */
__gmap_link(struct gmap * gmap,unsigned long gaddr,unsigned long vmaddr)550 int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
551 {
552 struct mm_struct *mm;
553 unsigned long *table;
554 spinlock_t *ptl;
555 pgd_t *pgd;
556 p4d_t *p4d;
557 pud_t *pud;
558 pmd_t *pmd;
559 u64 unprot;
560 int rc;
561
562 BUG_ON(gmap_is_shadow(gmap));
563 /* Create higher level tables in the gmap page table */
564 table = gmap->table;
565 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
566 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
567 if ((*table & _REGION_ENTRY_INVALID) &&
568 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
569 gaddr & _REGION1_MASK))
570 return -ENOMEM;
571 table = __va(*table & _REGION_ENTRY_ORIGIN);
572 }
573 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
574 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
575 if ((*table & _REGION_ENTRY_INVALID) &&
576 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
577 gaddr & _REGION2_MASK))
578 return -ENOMEM;
579 table = __va(*table & _REGION_ENTRY_ORIGIN);
580 }
581 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
582 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
583 if ((*table & _REGION_ENTRY_INVALID) &&
584 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
585 gaddr & _REGION3_MASK))
586 return -ENOMEM;
587 table = __va(*table & _REGION_ENTRY_ORIGIN);
588 }
589 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
590 /* Walk the parent mm page table */
591 mm = gmap->mm;
592 pgd = pgd_offset(mm, vmaddr);
593 VM_BUG_ON(pgd_none(*pgd));
594 p4d = p4d_offset(pgd, vmaddr);
595 VM_BUG_ON(p4d_none(*p4d));
596 pud = pud_offset(p4d, vmaddr);
597 VM_BUG_ON(pud_none(*pud));
598 /* large puds cannot yet be handled */
599 if (pud_leaf(*pud))
600 return -EFAULT;
601 pmd = pmd_offset(pud, vmaddr);
602 VM_BUG_ON(pmd_none(*pmd));
603 /* Are we allowed to use huge pages? */
604 if (pmd_large(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
605 return -EFAULT;
606 /* Link gmap segment table entry location to page table. */
607 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
608 if (rc)
609 return rc;
610 ptl = pmd_lock(mm, pmd);
611 spin_lock(&gmap->guest_table_lock);
612 if (*table == _SEGMENT_ENTRY_EMPTY) {
613 rc = radix_tree_insert(&gmap->host_to_guest,
614 vmaddr >> PMD_SHIFT, table);
615 if (!rc) {
616 if (pmd_large(*pmd)) {
617 *table = (pmd_val(*pmd) &
618 _SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
619 | _SEGMENT_ENTRY_GMAP_UC;
620 } else
621 *table = pmd_val(*pmd) &
622 _SEGMENT_ENTRY_HARDWARE_BITS;
623 }
624 } else if (*table & _SEGMENT_ENTRY_PROTECT &&
625 !(pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT)) {
626 unprot = (u64)*table;
627 unprot &= ~_SEGMENT_ENTRY_PROTECT;
628 unprot |= _SEGMENT_ENTRY_GMAP_UC;
629 gmap_pmdp_xchg(gmap, (pmd_t *)table, __pmd(unprot), gaddr);
630 }
631 spin_unlock(&gmap->guest_table_lock);
632 spin_unlock(ptl);
633 radix_tree_preload_end();
634 return rc;
635 }
636
637 /**
638 * gmap_fault - resolve a fault on a guest address
639 * @gmap: pointer to guest mapping meta data structure
640 * @gaddr: guest address
641 * @fault_flags: flags to pass down to handle_mm_fault()
642 *
643 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
644 * if the vm address is already mapped to a different guest segment.
645 */
gmap_fault(struct gmap * gmap,unsigned long gaddr,unsigned int fault_flags)646 int gmap_fault(struct gmap *gmap, unsigned long gaddr,
647 unsigned int fault_flags)
648 {
649 unsigned long vmaddr;
650 int rc;
651 bool unlocked;
652
653 mmap_read_lock(gmap->mm);
654
655 retry:
656 unlocked = false;
657 vmaddr = __gmap_translate(gmap, gaddr);
658 if (IS_ERR_VALUE(vmaddr)) {
659 rc = vmaddr;
660 goto out_up;
661 }
662 if (fixup_user_fault(gmap->mm, vmaddr, fault_flags,
663 &unlocked)) {
664 rc = -EFAULT;
665 goto out_up;
666 }
667 /*
668 * In the case that fixup_user_fault unlocked the mmap_lock during
669 * faultin redo __gmap_translate to not race with a map/unmap_segment.
670 */
671 if (unlocked)
672 goto retry;
673
674 rc = __gmap_link(gmap, gaddr, vmaddr);
675 out_up:
676 mmap_read_unlock(gmap->mm);
677 return rc;
678 }
679 EXPORT_SYMBOL_GPL(gmap_fault);
680
681 /*
682 * this function is assumed to be called with mmap_lock held
683 */
__gmap_zap(struct gmap * gmap,unsigned long gaddr)684 void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
685 {
686 struct vm_area_struct *vma;
687 unsigned long vmaddr;
688 spinlock_t *ptl;
689 pte_t *ptep;
690
691 /* Find the vm address for the guest address */
692 vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
693 gaddr >> PMD_SHIFT);
694 if (vmaddr) {
695 vmaddr |= gaddr & ~PMD_MASK;
696
697 vma = vma_lookup(gmap->mm, vmaddr);
698 if (!vma || is_vm_hugetlb_page(vma))
699 return;
700
701 /* Get pointer to the page table entry */
702 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
703 if (likely(ptep)) {
704 ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
705 pte_unmap_unlock(ptep, ptl);
706 }
707 }
708 }
709 EXPORT_SYMBOL_GPL(__gmap_zap);
710
gmap_discard(struct gmap * gmap,unsigned long from,unsigned long to)711 void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
712 {
713 unsigned long gaddr, vmaddr, size;
714 struct vm_area_struct *vma;
715
716 mmap_read_lock(gmap->mm);
717 for (gaddr = from; gaddr < to;
718 gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
719 /* Find the vm address for the guest address */
720 vmaddr = (unsigned long)
721 radix_tree_lookup(&gmap->guest_to_host,
722 gaddr >> PMD_SHIFT);
723 if (!vmaddr)
724 continue;
725 vmaddr |= gaddr & ~PMD_MASK;
726 /* Find vma in the parent mm */
727 vma = find_vma(gmap->mm, vmaddr);
728 if (!vma)
729 continue;
730 /*
731 * We do not discard pages that are backed by
732 * hugetlbfs, so we don't have to refault them.
733 */
734 if (is_vm_hugetlb_page(vma))
735 continue;
736 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
737 zap_page_range_single(vma, vmaddr, size, NULL);
738 }
739 mmap_read_unlock(gmap->mm);
740 }
741 EXPORT_SYMBOL_GPL(gmap_discard);
742
743 static LIST_HEAD(gmap_notifier_list);
744 static DEFINE_SPINLOCK(gmap_notifier_lock);
745
746 /**
747 * gmap_register_pte_notifier - register a pte invalidation callback
748 * @nb: pointer to the gmap notifier block
749 */
gmap_register_pte_notifier(struct gmap_notifier * nb)750 void gmap_register_pte_notifier(struct gmap_notifier *nb)
751 {
752 spin_lock(&gmap_notifier_lock);
753 list_add_rcu(&nb->list, &gmap_notifier_list);
754 spin_unlock(&gmap_notifier_lock);
755 }
756 EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
757
758 /**
759 * gmap_unregister_pte_notifier - remove a pte invalidation callback
760 * @nb: pointer to the gmap notifier block
761 */
gmap_unregister_pte_notifier(struct gmap_notifier * nb)762 void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
763 {
764 spin_lock(&gmap_notifier_lock);
765 list_del_rcu(&nb->list);
766 spin_unlock(&gmap_notifier_lock);
767 synchronize_rcu();
768 }
769 EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
770
771 /**
772 * gmap_call_notifier - call all registered invalidation callbacks
773 * @gmap: pointer to guest mapping meta data structure
774 * @start: start virtual address in the guest address space
775 * @end: end virtual address in the guest address space
776 */
gmap_call_notifier(struct gmap * gmap,unsigned long start,unsigned long end)777 static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
778 unsigned long end)
779 {
780 struct gmap_notifier *nb;
781
782 list_for_each_entry(nb, &gmap_notifier_list, list)
783 nb->notifier_call(gmap, start, end);
784 }
785
786 /**
787 * gmap_table_walk - walk the gmap page tables
788 * @gmap: pointer to guest mapping meta data structure
789 * @gaddr: virtual address in the guest address space
790 * @level: page table level to stop at
791 *
792 * Returns a table entry pointer for the given guest address and @level
793 * @level=0 : returns a pointer to a page table table entry (or NULL)
794 * @level=1 : returns a pointer to a segment table entry (or NULL)
795 * @level=2 : returns a pointer to a region-3 table entry (or NULL)
796 * @level=3 : returns a pointer to a region-2 table entry (or NULL)
797 * @level=4 : returns a pointer to a region-1 table entry (or NULL)
798 *
799 * Returns NULL if the gmap page tables could not be walked to the
800 * requested level.
801 *
802 * Note: Can also be called for shadow gmaps.
803 */
gmap_table_walk(struct gmap * gmap,unsigned long gaddr,int level)804 static inline unsigned long *gmap_table_walk(struct gmap *gmap,
805 unsigned long gaddr, int level)
806 {
807 const int asce_type = gmap->asce & _ASCE_TYPE_MASK;
808 unsigned long *table = gmap->table;
809
810 if (gmap_is_shadow(gmap) && gmap->removed)
811 return NULL;
812
813 if (WARN_ON_ONCE(level > (asce_type >> 2) + 1))
814 return NULL;
815
816 if (asce_type != _ASCE_TYPE_REGION1 &&
817 gaddr & (-1UL << (31 + (asce_type >> 2) * 11)))
818 return NULL;
819
820 switch (asce_type) {
821 case _ASCE_TYPE_REGION1:
822 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
823 if (level == 4)
824 break;
825 if (*table & _REGION_ENTRY_INVALID)
826 return NULL;
827 table = __va(*table & _REGION_ENTRY_ORIGIN);
828 fallthrough;
829 case _ASCE_TYPE_REGION2:
830 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
831 if (level == 3)
832 break;
833 if (*table & _REGION_ENTRY_INVALID)
834 return NULL;
835 table = __va(*table & _REGION_ENTRY_ORIGIN);
836 fallthrough;
837 case _ASCE_TYPE_REGION3:
838 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
839 if (level == 2)
840 break;
841 if (*table & _REGION_ENTRY_INVALID)
842 return NULL;
843 table = __va(*table & _REGION_ENTRY_ORIGIN);
844 fallthrough;
845 case _ASCE_TYPE_SEGMENT:
846 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
847 if (level == 1)
848 break;
849 if (*table & _REGION_ENTRY_INVALID)
850 return NULL;
851 table = __va(*table & _SEGMENT_ENTRY_ORIGIN);
852 table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
853 }
854 return table;
855 }
856
857 /**
858 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
859 * and return the pte pointer
860 * @gmap: pointer to guest mapping meta data structure
861 * @gaddr: virtual address in the guest address space
862 * @ptl: pointer to the spinlock pointer
863 *
864 * Returns a pointer to the locked pte for a guest address, or NULL
865 */
gmap_pte_op_walk(struct gmap * gmap,unsigned long gaddr,spinlock_t ** ptl)866 static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
867 spinlock_t **ptl)
868 {
869 unsigned long *table;
870
871 BUG_ON(gmap_is_shadow(gmap));
872 /* Walk the gmap page table, lock and get pte pointer */
873 table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
874 if (!table || *table & _SEGMENT_ENTRY_INVALID)
875 return NULL;
876 return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
877 }
878
879 /**
880 * gmap_pte_op_fixup - force a page in and connect the gmap page table
881 * @gmap: pointer to guest mapping meta data structure
882 * @gaddr: virtual address in the guest address space
883 * @vmaddr: address in the host process address space
884 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
885 *
886 * Returns 0 if the caller can retry __gmap_translate (might fail again),
887 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
888 * up or connecting the gmap page table.
889 */
gmap_pte_op_fixup(struct gmap * gmap,unsigned long gaddr,unsigned long vmaddr,int prot)890 static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
891 unsigned long vmaddr, int prot)
892 {
893 struct mm_struct *mm = gmap->mm;
894 unsigned int fault_flags;
895 bool unlocked = false;
896
897 BUG_ON(gmap_is_shadow(gmap));
898 fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
899 if (fixup_user_fault(mm, vmaddr, fault_flags, &unlocked))
900 return -EFAULT;
901 if (unlocked)
902 /* lost mmap_lock, caller has to retry __gmap_translate */
903 return 0;
904 /* Connect the page tables */
905 return __gmap_link(gmap, gaddr, vmaddr);
906 }
907
908 /**
909 * gmap_pte_op_end - release the page table lock
910 * @ptep: pointer to the locked pte
911 * @ptl: pointer to the page table spinlock
912 */
gmap_pte_op_end(pte_t * ptep,spinlock_t * ptl)913 static void gmap_pte_op_end(pte_t *ptep, spinlock_t *ptl)
914 {
915 pte_unmap_unlock(ptep, ptl);
916 }
917
918 /**
919 * gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
920 * and return the pmd pointer
921 * @gmap: pointer to guest mapping meta data structure
922 * @gaddr: virtual address in the guest address space
923 *
924 * Returns a pointer to the pmd for a guest address, or NULL
925 */
gmap_pmd_op_walk(struct gmap * gmap,unsigned long gaddr)926 static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
927 {
928 pmd_t *pmdp;
929
930 BUG_ON(gmap_is_shadow(gmap));
931 pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
932 if (!pmdp)
933 return NULL;
934
935 /* without huge pages, there is no need to take the table lock */
936 if (!gmap->mm->context.allow_gmap_hpage_1m)
937 return pmd_none(*pmdp) ? NULL : pmdp;
938
939 spin_lock(&gmap->guest_table_lock);
940 if (pmd_none(*pmdp)) {
941 spin_unlock(&gmap->guest_table_lock);
942 return NULL;
943 }
944
945 /* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
946 if (!pmd_large(*pmdp))
947 spin_unlock(&gmap->guest_table_lock);
948 return pmdp;
949 }
950
951 /**
952 * gmap_pmd_op_end - release the guest_table_lock if needed
953 * @gmap: pointer to the guest mapping meta data structure
954 * @pmdp: pointer to the pmd
955 */
gmap_pmd_op_end(struct gmap * gmap,pmd_t * pmdp)956 static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
957 {
958 if (pmd_large(*pmdp))
959 spin_unlock(&gmap->guest_table_lock);
960 }
961
962 /*
963 * gmap_protect_pmd - remove access rights to memory and set pmd notification bits
964 * @pmdp: pointer to the pmd to be protected
965 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
966 * @bits: notification bits to set
967 *
968 * Returns:
969 * 0 if successfully protected
970 * -EAGAIN if a fixup is needed
971 * -EINVAL if unsupported notifier bits have been specified
972 *
973 * Expected to be called with sg->mm->mmap_lock in read and
974 * guest_table_lock held.
975 */
gmap_protect_pmd(struct gmap * gmap,unsigned long gaddr,pmd_t * pmdp,int prot,unsigned long bits)976 static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
977 pmd_t *pmdp, int prot, unsigned long bits)
978 {
979 int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
980 int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
981 pmd_t new = *pmdp;
982
983 /* Fixup needed */
984 if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
985 return -EAGAIN;
986
987 if (prot == PROT_NONE && !pmd_i) {
988 new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
989 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
990 }
991
992 if (prot == PROT_READ && !pmd_p) {
993 new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
994 new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_PROTECT));
995 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
996 }
997
998 if (bits & GMAP_NOTIFY_MPROT)
999 set_pmd(pmdp, set_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
1000
1001 /* Shadow GMAP protection needs split PMDs */
1002 if (bits & GMAP_NOTIFY_SHADOW)
1003 return -EINVAL;
1004
1005 return 0;
1006 }
1007
1008 /*
1009 * gmap_protect_pte - remove access rights to memory and set pgste bits
1010 * @gmap: pointer to guest mapping meta data structure
1011 * @gaddr: virtual address in the guest address space
1012 * @pmdp: pointer to the pmd associated with the pte
1013 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1014 * @bits: notification bits to set
1015 *
1016 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1017 * -EAGAIN if a fixup is needed.
1018 *
1019 * Expected to be called with sg->mm->mmap_lock in read
1020 */
gmap_protect_pte(struct gmap * gmap,unsigned long gaddr,pmd_t * pmdp,int prot,unsigned long bits)1021 static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
1022 pmd_t *pmdp, int prot, unsigned long bits)
1023 {
1024 int rc;
1025 pte_t *ptep;
1026 spinlock_t *ptl;
1027 unsigned long pbits = 0;
1028
1029 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
1030 return -EAGAIN;
1031
1032 ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
1033 if (!ptep)
1034 return -ENOMEM;
1035
1036 pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
1037 pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
1038 /* Protect and unlock. */
1039 rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
1040 gmap_pte_op_end(ptep, ptl);
1041 return rc;
1042 }
1043
1044 /*
1045 * gmap_protect_range - remove access rights to memory and set pgste bits
1046 * @gmap: pointer to guest mapping meta data structure
1047 * @gaddr: virtual address in the guest address space
1048 * @len: size of area
1049 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1050 * @bits: pgste notification bits to set
1051 *
1052 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1053 * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
1054 *
1055 * Called with sg->mm->mmap_lock in read.
1056 */
gmap_protect_range(struct gmap * gmap,unsigned long gaddr,unsigned long len,int prot,unsigned long bits)1057 static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
1058 unsigned long len, int prot, unsigned long bits)
1059 {
1060 unsigned long vmaddr, dist;
1061 pmd_t *pmdp;
1062 int rc;
1063
1064 BUG_ON(gmap_is_shadow(gmap));
1065 while (len) {
1066 rc = -EAGAIN;
1067 pmdp = gmap_pmd_op_walk(gmap, gaddr);
1068 if (pmdp) {
1069 if (!pmd_large(*pmdp)) {
1070 rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
1071 bits);
1072 if (!rc) {
1073 len -= PAGE_SIZE;
1074 gaddr += PAGE_SIZE;
1075 }
1076 } else {
1077 rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
1078 bits);
1079 if (!rc) {
1080 dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
1081 len = len < dist ? 0 : len - dist;
1082 gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
1083 }
1084 }
1085 gmap_pmd_op_end(gmap, pmdp);
1086 }
1087 if (rc) {
1088 if (rc == -EINVAL)
1089 return rc;
1090
1091 /* -EAGAIN, fixup of userspace mm and gmap */
1092 vmaddr = __gmap_translate(gmap, gaddr);
1093 if (IS_ERR_VALUE(vmaddr))
1094 return vmaddr;
1095 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
1096 if (rc)
1097 return rc;
1098 }
1099 }
1100 return 0;
1101 }
1102
1103 /**
1104 * gmap_mprotect_notify - change access rights for a range of ptes and
1105 * call the notifier if any pte changes again
1106 * @gmap: pointer to guest mapping meta data structure
1107 * @gaddr: virtual address in the guest address space
1108 * @len: size of area
1109 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1110 *
1111 * Returns 0 if for each page in the given range a gmap mapping exists,
1112 * the new access rights could be set and the notifier could be armed.
1113 * If the gmap mapping is missing for one or more pages -EFAULT is
1114 * returned. If no memory could be allocated -ENOMEM is returned.
1115 * This function establishes missing page table entries.
1116 */
gmap_mprotect_notify(struct gmap * gmap,unsigned long gaddr,unsigned long len,int prot)1117 int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
1118 unsigned long len, int prot)
1119 {
1120 int rc;
1121
1122 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
1123 return -EINVAL;
1124 if (!MACHINE_HAS_ESOP && prot == PROT_READ)
1125 return -EINVAL;
1126 mmap_read_lock(gmap->mm);
1127 rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
1128 mmap_read_unlock(gmap->mm);
1129 return rc;
1130 }
1131 EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
1132
1133 /**
1134 * gmap_read_table - get an unsigned long value from a guest page table using
1135 * absolute addressing, without marking the page referenced.
1136 * @gmap: pointer to guest mapping meta data structure
1137 * @gaddr: virtual address in the guest address space
1138 * @val: pointer to the unsigned long value to return
1139 *
1140 * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
1141 * if reading using the virtual address failed. -EINVAL if called on a gmap
1142 * shadow.
1143 *
1144 * Called with gmap->mm->mmap_lock in read.
1145 */
gmap_read_table(struct gmap * gmap,unsigned long gaddr,unsigned long * val)1146 int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
1147 {
1148 unsigned long address, vmaddr;
1149 spinlock_t *ptl;
1150 pte_t *ptep, pte;
1151 int rc;
1152
1153 if (gmap_is_shadow(gmap))
1154 return -EINVAL;
1155
1156 while (1) {
1157 rc = -EAGAIN;
1158 ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
1159 if (ptep) {
1160 pte = *ptep;
1161 if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
1162 address = pte_val(pte) & PAGE_MASK;
1163 address += gaddr & ~PAGE_MASK;
1164 *val = *(unsigned long *)__va(address);
1165 set_pte(ptep, set_pte_bit(*ptep, __pgprot(_PAGE_YOUNG)));
1166 /* Do *NOT* clear the _PAGE_INVALID bit! */
1167 rc = 0;
1168 }
1169 gmap_pte_op_end(ptep, ptl);
1170 }
1171 if (!rc)
1172 break;
1173 vmaddr = __gmap_translate(gmap, gaddr);
1174 if (IS_ERR_VALUE(vmaddr)) {
1175 rc = vmaddr;
1176 break;
1177 }
1178 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
1179 if (rc)
1180 break;
1181 }
1182 return rc;
1183 }
1184 EXPORT_SYMBOL_GPL(gmap_read_table);
1185
1186 /**
1187 * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
1188 * @sg: pointer to the shadow guest address space structure
1189 * @vmaddr: vm address associated with the rmap
1190 * @rmap: pointer to the rmap structure
1191 *
1192 * Called with the sg->guest_table_lock
1193 */
gmap_insert_rmap(struct gmap * sg,unsigned long vmaddr,struct gmap_rmap * rmap)1194 static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
1195 struct gmap_rmap *rmap)
1196 {
1197 struct gmap_rmap *temp;
1198 void __rcu **slot;
1199
1200 BUG_ON(!gmap_is_shadow(sg));
1201 slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
1202 if (slot) {
1203 rmap->next = radix_tree_deref_slot_protected(slot,
1204 &sg->guest_table_lock);
1205 for (temp = rmap->next; temp; temp = temp->next) {
1206 if (temp->raddr == rmap->raddr) {
1207 kfree(rmap);
1208 return;
1209 }
1210 }
1211 radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
1212 } else {
1213 rmap->next = NULL;
1214 radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
1215 rmap);
1216 }
1217 }
1218
1219 /**
1220 * gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
1221 * @sg: pointer to the shadow guest address space structure
1222 * @raddr: rmap address in the shadow gmap
1223 * @paddr: address in the parent guest address space
1224 * @len: length of the memory area to protect
1225 *
1226 * Returns 0 if successfully protected and the rmap was created, -ENOMEM
1227 * if out of memory and -EFAULT if paddr is invalid.
1228 */
gmap_protect_rmap(struct gmap * sg,unsigned long raddr,unsigned long paddr,unsigned long len)1229 static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
1230 unsigned long paddr, unsigned long len)
1231 {
1232 struct gmap *parent;
1233 struct gmap_rmap *rmap;
1234 unsigned long vmaddr;
1235 spinlock_t *ptl;
1236 pte_t *ptep;
1237 int rc;
1238
1239 BUG_ON(!gmap_is_shadow(sg));
1240 parent = sg->parent;
1241 while (len) {
1242 vmaddr = __gmap_translate(parent, paddr);
1243 if (IS_ERR_VALUE(vmaddr))
1244 return vmaddr;
1245 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
1246 if (!rmap)
1247 return -ENOMEM;
1248 rmap->raddr = raddr;
1249 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
1250 if (rc) {
1251 kfree(rmap);
1252 return rc;
1253 }
1254 rc = -EAGAIN;
1255 ptep = gmap_pte_op_walk(parent, paddr, &ptl);
1256 if (ptep) {
1257 spin_lock(&sg->guest_table_lock);
1258 rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
1259 PGSTE_VSIE_BIT);
1260 if (!rc)
1261 gmap_insert_rmap(sg, vmaddr, rmap);
1262 spin_unlock(&sg->guest_table_lock);
1263 gmap_pte_op_end(ptep, ptl);
1264 }
1265 radix_tree_preload_end();
1266 if (rc) {
1267 kfree(rmap);
1268 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
1269 if (rc)
1270 return rc;
1271 continue;
1272 }
1273 paddr += PAGE_SIZE;
1274 len -= PAGE_SIZE;
1275 }
1276 return 0;
1277 }
1278
1279 #define _SHADOW_RMAP_MASK 0x7
1280 #define _SHADOW_RMAP_REGION1 0x5
1281 #define _SHADOW_RMAP_REGION2 0x4
1282 #define _SHADOW_RMAP_REGION3 0x3
1283 #define _SHADOW_RMAP_SEGMENT 0x2
1284 #define _SHADOW_RMAP_PGTABLE 0x1
1285
1286 /**
1287 * gmap_idte_one - invalidate a single region or segment table entry
1288 * @asce: region or segment table *origin* + table-type bits
1289 * @vaddr: virtual address to identify the table entry to flush
1290 *
1291 * The invalid bit of a single region or segment table entry is set
1292 * and the associated TLB entries depending on the entry are flushed.
1293 * The table-type of the @asce identifies the portion of the @vaddr
1294 * that is used as the invalidation index.
1295 */
gmap_idte_one(unsigned long asce,unsigned long vaddr)1296 static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
1297 {
1298 asm volatile(
1299 " idte %0,0,%1"
1300 : : "a" (asce), "a" (vaddr) : "cc", "memory");
1301 }
1302
1303 /**
1304 * gmap_unshadow_page - remove a page from a shadow page table
1305 * @sg: pointer to the shadow guest address space structure
1306 * @raddr: rmap address in the shadow guest address space
1307 *
1308 * Called with the sg->guest_table_lock
1309 */
gmap_unshadow_page(struct gmap * sg,unsigned long raddr)1310 static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
1311 {
1312 unsigned long *table;
1313
1314 BUG_ON(!gmap_is_shadow(sg));
1315 table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
1316 if (!table || *table & _PAGE_INVALID)
1317 return;
1318 gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
1319 ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
1320 }
1321
1322 /**
1323 * __gmap_unshadow_pgt - remove all entries from a shadow page table
1324 * @sg: pointer to the shadow guest address space structure
1325 * @raddr: rmap address in the shadow guest address space
1326 * @pgt: pointer to the start of a shadow page table
1327 *
1328 * Called with the sg->guest_table_lock
1329 */
__gmap_unshadow_pgt(struct gmap * sg,unsigned long raddr,unsigned long * pgt)1330 static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
1331 unsigned long *pgt)
1332 {
1333 int i;
1334
1335 BUG_ON(!gmap_is_shadow(sg));
1336 for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
1337 pgt[i] = _PAGE_INVALID;
1338 }
1339
1340 /**
1341 * gmap_unshadow_pgt - remove a shadow page table from a segment entry
1342 * @sg: pointer to the shadow guest address space structure
1343 * @raddr: address in the shadow guest address space
1344 *
1345 * Called with the sg->guest_table_lock
1346 */
gmap_unshadow_pgt(struct gmap * sg,unsigned long raddr)1347 static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
1348 {
1349 unsigned long *ste;
1350 phys_addr_t sto, pgt;
1351 struct page *page;
1352
1353 BUG_ON(!gmap_is_shadow(sg));
1354 ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
1355 if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
1356 return;
1357 gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
1358 sto = __pa(ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
1359 gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
1360 pgt = *ste & _SEGMENT_ENTRY_ORIGIN;
1361 *ste = _SEGMENT_ENTRY_EMPTY;
1362 __gmap_unshadow_pgt(sg, raddr, __va(pgt));
1363 /* Free page table */
1364 page = phys_to_page(pgt);
1365 list_del(&page->lru);
1366 page_table_free_pgste(page);
1367 }
1368
1369 /**
1370 * __gmap_unshadow_sgt - remove all entries from a shadow segment table
1371 * @sg: pointer to the shadow guest address space structure
1372 * @raddr: rmap address in the shadow guest address space
1373 * @sgt: pointer to the start of a shadow segment table
1374 *
1375 * Called with the sg->guest_table_lock
1376 */
__gmap_unshadow_sgt(struct gmap * sg,unsigned long raddr,unsigned long * sgt)1377 static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
1378 unsigned long *sgt)
1379 {
1380 struct page *page;
1381 phys_addr_t pgt;
1382 int i;
1383
1384 BUG_ON(!gmap_is_shadow(sg));
1385 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
1386 if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
1387 continue;
1388 pgt = sgt[i] & _REGION_ENTRY_ORIGIN;
1389 sgt[i] = _SEGMENT_ENTRY_EMPTY;
1390 __gmap_unshadow_pgt(sg, raddr, __va(pgt));
1391 /* Free page table */
1392 page = phys_to_page(pgt);
1393 list_del(&page->lru);
1394 page_table_free_pgste(page);
1395 }
1396 }
1397
1398 /**
1399 * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
1400 * @sg: pointer to the shadow guest address space structure
1401 * @raddr: rmap address in the shadow guest address space
1402 *
1403 * Called with the shadow->guest_table_lock
1404 */
gmap_unshadow_sgt(struct gmap * sg,unsigned long raddr)1405 static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
1406 {
1407 unsigned long r3o, *r3e;
1408 phys_addr_t sgt;
1409 struct page *page;
1410
1411 BUG_ON(!gmap_is_shadow(sg));
1412 r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
1413 if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
1414 return;
1415 gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
1416 r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
1417 gmap_idte_one(__pa(r3o) | _ASCE_TYPE_REGION3, raddr);
1418 sgt = *r3e & _REGION_ENTRY_ORIGIN;
1419 *r3e = _REGION3_ENTRY_EMPTY;
1420 __gmap_unshadow_sgt(sg, raddr, __va(sgt));
1421 /* Free segment table */
1422 page = phys_to_page(sgt);
1423 list_del(&page->lru);
1424 __free_pages(page, CRST_ALLOC_ORDER);
1425 }
1426
1427 /**
1428 * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
1429 * @sg: pointer to the shadow guest address space structure
1430 * @raddr: address in the shadow guest address space
1431 * @r3t: pointer to the start of a shadow region-3 table
1432 *
1433 * Called with the sg->guest_table_lock
1434 */
__gmap_unshadow_r3t(struct gmap * sg,unsigned long raddr,unsigned long * r3t)1435 static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
1436 unsigned long *r3t)
1437 {
1438 struct page *page;
1439 phys_addr_t sgt;
1440 int i;
1441
1442 BUG_ON(!gmap_is_shadow(sg));
1443 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
1444 if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
1445 continue;
1446 sgt = r3t[i] & _REGION_ENTRY_ORIGIN;
1447 r3t[i] = _REGION3_ENTRY_EMPTY;
1448 __gmap_unshadow_sgt(sg, raddr, __va(sgt));
1449 /* Free segment table */
1450 page = phys_to_page(sgt);
1451 list_del(&page->lru);
1452 __free_pages(page, CRST_ALLOC_ORDER);
1453 }
1454 }
1455
1456 /**
1457 * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
1458 * @sg: pointer to the shadow guest address space structure
1459 * @raddr: rmap address in the shadow guest address space
1460 *
1461 * Called with the sg->guest_table_lock
1462 */
gmap_unshadow_r3t(struct gmap * sg,unsigned long raddr)1463 static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
1464 {
1465 unsigned long r2o, *r2e;
1466 phys_addr_t r3t;
1467 struct page *page;
1468
1469 BUG_ON(!gmap_is_shadow(sg));
1470 r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
1471 if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
1472 return;
1473 gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
1474 r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
1475 gmap_idte_one(__pa(r2o) | _ASCE_TYPE_REGION2, raddr);
1476 r3t = *r2e & _REGION_ENTRY_ORIGIN;
1477 *r2e = _REGION2_ENTRY_EMPTY;
1478 __gmap_unshadow_r3t(sg, raddr, __va(r3t));
1479 /* Free region 3 table */
1480 page = phys_to_page(r3t);
1481 list_del(&page->lru);
1482 __free_pages(page, CRST_ALLOC_ORDER);
1483 }
1484
1485 /**
1486 * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
1487 * @sg: pointer to the shadow guest address space structure
1488 * @raddr: rmap address in the shadow guest address space
1489 * @r2t: pointer to the start of a shadow region-2 table
1490 *
1491 * Called with the sg->guest_table_lock
1492 */
__gmap_unshadow_r2t(struct gmap * sg,unsigned long raddr,unsigned long * r2t)1493 static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
1494 unsigned long *r2t)
1495 {
1496 phys_addr_t r3t;
1497 struct page *page;
1498 int i;
1499
1500 BUG_ON(!gmap_is_shadow(sg));
1501 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
1502 if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
1503 continue;
1504 r3t = r2t[i] & _REGION_ENTRY_ORIGIN;
1505 r2t[i] = _REGION2_ENTRY_EMPTY;
1506 __gmap_unshadow_r3t(sg, raddr, __va(r3t));
1507 /* Free region 3 table */
1508 page = phys_to_page(r3t);
1509 list_del(&page->lru);
1510 __free_pages(page, CRST_ALLOC_ORDER);
1511 }
1512 }
1513
1514 /**
1515 * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
1516 * @sg: pointer to the shadow guest address space structure
1517 * @raddr: rmap address in the shadow guest address space
1518 *
1519 * Called with the sg->guest_table_lock
1520 */
gmap_unshadow_r2t(struct gmap * sg,unsigned long raddr)1521 static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
1522 {
1523 unsigned long r1o, *r1e;
1524 struct page *page;
1525 phys_addr_t r2t;
1526
1527 BUG_ON(!gmap_is_shadow(sg));
1528 r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
1529 if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
1530 return;
1531 gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
1532 r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
1533 gmap_idte_one(__pa(r1o) | _ASCE_TYPE_REGION1, raddr);
1534 r2t = *r1e & _REGION_ENTRY_ORIGIN;
1535 *r1e = _REGION1_ENTRY_EMPTY;
1536 __gmap_unshadow_r2t(sg, raddr, __va(r2t));
1537 /* Free region 2 table */
1538 page = phys_to_page(r2t);
1539 list_del(&page->lru);
1540 __free_pages(page, CRST_ALLOC_ORDER);
1541 }
1542
1543 /**
1544 * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
1545 * @sg: pointer to the shadow guest address space structure
1546 * @raddr: rmap address in the shadow guest address space
1547 * @r1t: pointer to the start of a shadow region-1 table
1548 *
1549 * Called with the shadow->guest_table_lock
1550 */
__gmap_unshadow_r1t(struct gmap * sg,unsigned long raddr,unsigned long * r1t)1551 static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
1552 unsigned long *r1t)
1553 {
1554 unsigned long asce;
1555 struct page *page;
1556 phys_addr_t r2t;
1557 int i;
1558
1559 BUG_ON(!gmap_is_shadow(sg));
1560 asce = __pa(r1t) | _ASCE_TYPE_REGION1;
1561 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
1562 if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
1563 continue;
1564 r2t = r1t[i] & _REGION_ENTRY_ORIGIN;
1565 __gmap_unshadow_r2t(sg, raddr, __va(r2t));
1566 /* Clear entry and flush translation r1t -> r2t */
1567 gmap_idte_one(asce, raddr);
1568 r1t[i] = _REGION1_ENTRY_EMPTY;
1569 /* Free region 2 table */
1570 page = phys_to_page(r2t);
1571 list_del(&page->lru);
1572 __free_pages(page, CRST_ALLOC_ORDER);
1573 }
1574 }
1575
1576 /**
1577 * gmap_unshadow - remove a shadow page table completely
1578 * @sg: pointer to the shadow guest address space structure
1579 *
1580 * Called with sg->guest_table_lock
1581 */
gmap_unshadow(struct gmap * sg)1582 static void gmap_unshadow(struct gmap *sg)
1583 {
1584 unsigned long *table;
1585
1586 BUG_ON(!gmap_is_shadow(sg));
1587 if (sg->removed)
1588 return;
1589 sg->removed = 1;
1590 gmap_call_notifier(sg, 0, -1UL);
1591 gmap_flush_tlb(sg);
1592 table = __va(sg->asce & _ASCE_ORIGIN);
1593 switch (sg->asce & _ASCE_TYPE_MASK) {
1594 case _ASCE_TYPE_REGION1:
1595 __gmap_unshadow_r1t(sg, 0, table);
1596 break;
1597 case _ASCE_TYPE_REGION2:
1598 __gmap_unshadow_r2t(sg, 0, table);
1599 break;
1600 case _ASCE_TYPE_REGION3:
1601 __gmap_unshadow_r3t(sg, 0, table);
1602 break;
1603 case _ASCE_TYPE_SEGMENT:
1604 __gmap_unshadow_sgt(sg, 0, table);
1605 break;
1606 }
1607 }
1608
1609 /**
1610 * gmap_find_shadow - find a specific asce in the list of shadow tables
1611 * @parent: pointer to the parent gmap
1612 * @asce: ASCE for which the shadow table is created
1613 * @edat_level: edat level to be used for the shadow translation
1614 *
1615 * Returns the pointer to a gmap if a shadow table with the given asce is
1616 * already available, ERR_PTR(-EAGAIN) if another one is just being created,
1617 * otherwise NULL
1618 */
gmap_find_shadow(struct gmap * parent,unsigned long asce,int edat_level)1619 static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
1620 int edat_level)
1621 {
1622 struct gmap *sg;
1623
1624 list_for_each_entry(sg, &parent->children, list) {
1625 if (sg->orig_asce != asce || sg->edat_level != edat_level ||
1626 sg->removed)
1627 continue;
1628 if (!sg->initialized)
1629 return ERR_PTR(-EAGAIN);
1630 refcount_inc(&sg->ref_count);
1631 return sg;
1632 }
1633 return NULL;
1634 }
1635
1636 /**
1637 * gmap_shadow_valid - check if a shadow guest address space matches the
1638 * given properties and is still valid
1639 * @sg: pointer to the shadow guest address space structure
1640 * @asce: ASCE for which the shadow table is requested
1641 * @edat_level: edat level to be used for the shadow translation
1642 *
1643 * Returns 1 if the gmap shadow is still valid and matches the given
1644 * properties, the caller can continue using it. Returns 0 otherwise, the
1645 * caller has to request a new shadow gmap in this case.
1646 *
1647 */
gmap_shadow_valid(struct gmap * sg,unsigned long asce,int edat_level)1648 int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
1649 {
1650 if (sg->removed)
1651 return 0;
1652 return sg->orig_asce == asce && sg->edat_level == edat_level;
1653 }
1654 EXPORT_SYMBOL_GPL(gmap_shadow_valid);
1655
1656 /**
1657 * gmap_shadow - create/find a shadow guest address space
1658 * @parent: pointer to the parent gmap
1659 * @asce: ASCE for which the shadow table is created
1660 * @edat_level: edat level to be used for the shadow translation
1661 *
1662 * The pages of the top level page table referred by the asce parameter
1663 * will be set to read-only and marked in the PGSTEs of the kvm process.
1664 * The shadow table will be removed automatically on any change to the
1665 * PTE mapping for the source table.
1666 *
1667 * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
1668 * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
1669 * parent gmap table could not be protected.
1670 */
gmap_shadow(struct gmap * parent,unsigned long asce,int edat_level)1671 struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1672 int edat_level)
1673 {
1674 struct gmap *sg, *new;
1675 unsigned long limit;
1676 int rc;
1677
1678 BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
1679 BUG_ON(gmap_is_shadow(parent));
1680 spin_lock(&parent->shadow_lock);
1681 sg = gmap_find_shadow(parent, asce, edat_level);
1682 spin_unlock(&parent->shadow_lock);
1683 if (sg)
1684 return sg;
1685 /* Create a new shadow gmap */
1686 limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
1687 if (asce & _ASCE_REAL_SPACE)
1688 limit = -1UL;
1689 new = gmap_alloc(limit);
1690 if (!new)
1691 return ERR_PTR(-ENOMEM);
1692 new->mm = parent->mm;
1693 new->parent = gmap_get(parent);
1694 new->private = parent->private;
1695 new->orig_asce = asce;
1696 new->edat_level = edat_level;
1697 new->initialized = false;
1698 spin_lock(&parent->shadow_lock);
1699 /* Recheck if another CPU created the same shadow */
1700 sg = gmap_find_shadow(parent, asce, edat_level);
1701 if (sg) {
1702 spin_unlock(&parent->shadow_lock);
1703 gmap_free(new);
1704 return sg;
1705 }
1706 if (asce & _ASCE_REAL_SPACE) {
1707 /* only allow one real-space gmap shadow */
1708 list_for_each_entry(sg, &parent->children, list) {
1709 if (sg->orig_asce & _ASCE_REAL_SPACE) {
1710 spin_lock(&sg->guest_table_lock);
1711 gmap_unshadow(sg);
1712 spin_unlock(&sg->guest_table_lock);
1713 list_del(&sg->list);
1714 gmap_put(sg);
1715 break;
1716 }
1717 }
1718 }
1719 refcount_set(&new->ref_count, 2);
1720 list_add(&new->list, &parent->children);
1721 if (asce & _ASCE_REAL_SPACE) {
1722 /* nothing to protect, return right away */
1723 new->initialized = true;
1724 spin_unlock(&parent->shadow_lock);
1725 return new;
1726 }
1727 spin_unlock(&parent->shadow_lock);
1728 /* protect after insertion, so it will get properly invalidated */
1729 mmap_read_lock(parent->mm);
1730 rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
1731 ((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
1732 PROT_READ, GMAP_NOTIFY_SHADOW);
1733 mmap_read_unlock(parent->mm);
1734 spin_lock(&parent->shadow_lock);
1735 new->initialized = true;
1736 if (rc) {
1737 list_del(&new->list);
1738 gmap_free(new);
1739 new = ERR_PTR(rc);
1740 }
1741 spin_unlock(&parent->shadow_lock);
1742 return new;
1743 }
1744 EXPORT_SYMBOL_GPL(gmap_shadow);
1745
1746 /**
1747 * gmap_shadow_r2t - create an empty shadow region 2 table
1748 * @sg: pointer to the shadow guest address space structure
1749 * @saddr: faulting address in the shadow gmap
1750 * @r2t: parent gmap address of the region 2 table to get shadowed
1751 * @fake: r2t references contiguous guest memory block, not a r2t
1752 *
1753 * The r2t parameter specifies the address of the source table. The
1754 * four pages of the source table are made read-only in the parent gmap
1755 * address space. A write to the source table area @r2t will automatically
1756 * remove the shadow r2 table and all of its descendants.
1757 *
1758 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1759 * shadow table structure is incomplete, -ENOMEM if out of memory and
1760 * -EFAULT if an address in the parent gmap could not be resolved.
1761 *
1762 * Called with sg->mm->mmap_lock in read.
1763 */
gmap_shadow_r2t(struct gmap * sg,unsigned long saddr,unsigned long r2t,int fake)1764 int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1765 int fake)
1766 {
1767 unsigned long raddr, origin, offset, len;
1768 unsigned long *table;
1769 phys_addr_t s_r2t;
1770 struct page *page;
1771 int rc;
1772
1773 BUG_ON(!gmap_is_shadow(sg));
1774 /* Allocate a shadow region second table */
1775 page = gmap_alloc_crst();
1776 if (!page)
1777 return -ENOMEM;
1778 page->index = r2t & _REGION_ENTRY_ORIGIN;
1779 if (fake)
1780 page->index |= GMAP_SHADOW_FAKE_TABLE;
1781 s_r2t = page_to_phys(page);
1782 /* Install shadow region second table */
1783 spin_lock(&sg->guest_table_lock);
1784 table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
1785 if (!table) {
1786 rc = -EAGAIN; /* Race with unshadow */
1787 goto out_free;
1788 }
1789 if (!(*table & _REGION_ENTRY_INVALID)) {
1790 rc = 0; /* Already established */
1791 goto out_free;
1792 } else if (*table & _REGION_ENTRY_ORIGIN) {
1793 rc = -EAGAIN; /* Race with shadow */
1794 goto out_free;
1795 }
1796 crst_table_init(__va(s_r2t), _REGION2_ENTRY_EMPTY);
1797 /* mark as invalid as long as the parent table is not protected */
1798 *table = s_r2t | _REGION_ENTRY_LENGTH |
1799 _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
1800 if (sg->edat_level >= 1)
1801 *table |= (r2t & _REGION_ENTRY_PROTECT);
1802 list_add(&page->lru, &sg->crst_list);
1803 if (fake) {
1804 /* nothing to protect for fake tables */
1805 *table &= ~_REGION_ENTRY_INVALID;
1806 spin_unlock(&sg->guest_table_lock);
1807 return 0;
1808 }
1809 spin_unlock(&sg->guest_table_lock);
1810 /* Make r2t read-only in parent gmap page table */
1811 raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
1812 origin = r2t & _REGION_ENTRY_ORIGIN;
1813 offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1814 len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1815 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1816 spin_lock(&sg->guest_table_lock);
1817 if (!rc) {
1818 table = gmap_table_walk(sg, saddr, 4);
1819 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r2t)
1820 rc = -EAGAIN; /* Race with unshadow */
1821 else
1822 *table &= ~_REGION_ENTRY_INVALID;
1823 } else {
1824 gmap_unshadow_r2t(sg, raddr);
1825 }
1826 spin_unlock(&sg->guest_table_lock);
1827 return rc;
1828 out_free:
1829 spin_unlock(&sg->guest_table_lock);
1830 __free_pages(page, CRST_ALLOC_ORDER);
1831 return rc;
1832 }
1833 EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
1834
1835 /**
1836 * gmap_shadow_r3t - create a shadow region 3 table
1837 * @sg: pointer to the shadow guest address space structure
1838 * @saddr: faulting address in the shadow gmap
1839 * @r3t: parent gmap address of the region 3 table to get shadowed
1840 * @fake: r3t references contiguous guest memory block, not a r3t
1841 *
1842 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1843 * shadow table structure is incomplete, -ENOMEM if out of memory and
1844 * -EFAULT if an address in the parent gmap could not be resolved.
1845 *
1846 * Called with sg->mm->mmap_lock in read.
1847 */
gmap_shadow_r3t(struct gmap * sg,unsigned long saddr,unsigned long r3t,int fake)1848 int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1849 int fake)
1850 {
1851 unsigned long raddr, origin, offset, len;
1852 unsigned long *table;
1853 phys_addr_t s_r3t;
1854 struct page *page;
1855 int rc;
1856
1857 BUG_ON(!gmap_is_shadow(sg));
1858 /* Allocate a shadow region second table */
1859 page = gmap_alloc_crst();
1860 if (!page)
1861 return -ENOMEM;
1862 page->index = r3t & _REGION_ENTRY_ORIGIN;
1863 if (fake)
1864 page->index |= GMAP_SHADOW_FAKE_TABLE;
1865 s_r3t = page_to_phys(page);
1866 /* Install shadow region second table */
1867 spin_lock(&sg->guest_table_lock);
1868 table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
1869 if (!table) {
1870 rc = -EAGAIN; /* Race with unshadow */
1871 goto out_free;
1872 }
1873 if (!(*table & _REGION_ENTRY_INVALID)) {
1874 rc = 0; /* Already established */
1875 goto out_free;
1876 } else if (*table & _REGION_ENTRY_ORIGIN) {
1877 rc = -EAGAIN; /* Race with shadow */
1878 goto out_free;
1879 }
1880 crst_table_init(__va(s_r3t), _REGION3_ENTRY_EMPTY);
1881 /* mark as invalid as long as the parent table is not protected */
1882 *table = s_r3t | _REGION_ENTRY_LENGTH |
1883 _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
1884 if (sg->edat_level >= 1)
1885 *table |= (r3t & _REGION_ENTRY_PROTECT);
1886 list_add(&page->lru, &sg->crst_list);
1887 if (fake) {
1888 /* nothing to protect for fake tables */
1889 *table &= ~_REGION_ENTRY_INVALID;
1890 spin_unlock(&sg->guest_table_lock);
1891 return 0;
1892 }
1893 spin_unlock(&sg->guest_table_lock);
1894 /* Make r3t read-only in parent gmap page table */
1895 raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
1896 origin = r3t & _REGION_ENTRY_ORIGIN;
1897 offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1898 len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1899 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1900 spin_lock(&sg->guest_table_lock);
1901 if (!rc) {
1902 table = gmap_table_walk(sg, saddr, 3);
1903 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r3t)
1904 rc = -EAGAIN; /* Race with unshadow */
1905 else
1906 *table &= ~_REGION_ENTRY_INVALID;
1907 } else {
1908 gmap_unshadow_r3t(sg, raddr);
1909 }
1910 spin_unlock(&sg->guest_table_lock);
1911 return rc;
1912 out_free:
1913 spin_unlock(&sg->guest_table_lock);
1914 __free_pages(page, CRST_ALLOC_ORDER);
1915 return rc;
1916 }
1917 EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
1918
1919 /**
1920 * gmap_shadow_sgt - create a shadow segment table
1921 * @sg: pointer to the shadow guest address space structure
1922 * @saddr: faulting address in the shadow gmap
1923 * @sgt: parent gmap address of the segment table to get shadowed
1924 * @fake: sgt references contiguous guest memory block, not a sgt
1925 *
1926 * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
1927 * shadow table structure is incomplete, -ENOMEM if out of memory and
1928 * -EFAULT if an address in the parent gmap could not be resolved.
1929 *
1930 * Called with sg->mm->mmap_lock in read.
1931 */
gmap_shadow_sgt(struct gmap * sg,unsigned long saddr,unsigned long sgt,int fake)1932 int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1933 int fake)
1934 {
1935 unsigned long raddr, origin, offset, len;
1936 unsigned long *table;
1937 phys_addr_t s_sgt;
1938 struct page *page;
1939 int rc;
1940
1941 BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
1942 /* Allocate a shadow segment table */
1943 page = gmap_alloc_crst();
1944 if (!page)
1945 return -ENOMEM;
1946 page->index = sgt & _REGION_ENTRY_ORIGIN;
1947 if (fake)
1948 page->index |= GMAP_SHADOW_FAKE_TABLE;
1949 s_sgt = page_to_phys(page);
1950 /* Install shadow region second table */
1951 spin_lock(&sg->guest_table_lock);
1952 table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
1953 if (!table) {
1954 rc = -EAGAIN; /* Race with unshadow */
1955 goto out_free;
1956 }
1957 if (!(*table & _REGION_ENTRY_INVALID)) {
1958 rc = 0; /* Already established */
1959 goto out_free;
1960 } else if (*table & _REGION_ENTRY_ORIGIN) {
1961 rc = -EAGAIN; /* Race with shadow */
1962 goto out_free;
1963 }
1964 crst_table_init(__va(s_sgt), _SEGMENT_ENTRY_EMPTY);
1965 /* mark as invalid as long as the parent table is not protected */
1966 *table = s_sgt | _REGION_ENTRY_LENGTH |
1967 _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
1968 if (sg->edat_level >= 1)
1969 *table |= sgt & _REGION_ENTRY_PROTECT;
1970 list_add(&page->lru, &sg->crst_list);
1971 if (fake) {
1972 /* nothing to protect for fake tables */
1973 *table &= ~_REGION_ENTRY_INVALID;
1974 spin_unlock(&sg->guest_table_lock);
1975 return 0;
1976 }
1977 spin_unlock(&sg->guest_table_lock);
1978 /* Make sgt read-only in parent gmap page table */
1979 raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
1980 origin = sgt & _REGION_ENTRY_ORIGIN;
1981 offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1982 len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
1983 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
1984 spin_lock(&sg->guest_table_lock);
1985 if (!rc) {
1986 table = gmap_table_walk(sg, saddr, 2);
1987 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_sgt)
1988 rc = -EAGAIN; /* Race with unshadow */
1989 else
1990 *table &= ~_REGION_ENTRY_INVALID;
1991 } else {
1992 gmap_unshadow_sgt(sg, raddr);
1993 }
1994 spin_unlock(&sg->guest_table_lock);
1995 return rc;
1996 out_free:
1997 spin_unlock(&sg->guest_table_lock);
1998 __free_pages(page, CRST_ALLOC_ORDER);
1999 return rc;
2000 }
2001 EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
2002
2003 /**
2004 * gmap_shadow_pgt_lookup - find a shadow page table
2005 * @sg: pointer to the shadow guest address space structure
2006 * @saddr: the address in the shadow aguest address space
2007 * @pgt: parent gmap address of the page table to get shadowed
2008 * @dat_protection: if the pgtable is marked as protected by dat
2009 * @fake: pgt references contiguous guest memory block, not a pgtable
2010 *
2011 * Returns 0 if the shadow page table was found and -EAGAIN if the page
2012 * table was not found.
2013 *
2014 * Called with sg->mm->mmap_lock in read.
2015 */
gmap_shadow_pgt_lookup(struct gmap * sg,unsigned long saddr,unsigned long * pgt,int * dat_protection,int * fake)2016 int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
2017 unsigned long *pgt, int *dat_protection,
2018 int *fake)
2019 {
2020 unsigned long *table;
2021 struct page *page;
2022 int rc;
2023
2024 BUG_ON(!gmap_is_shadow(sg));
2025 spin_lock(&sg->guest_table_lock);
2026 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2027 if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
2028 /* Shadow page tables are full pages (pte+pgste) */
2029 page = pfn_to_page(*table >> PAGE_SHIFT);
2030 *pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
2031 *dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
2032 *fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
2033 rc = 0;
2034 } else {
2035 rc = -EAGAIN;
2036 }
2037 spin_unlock(&sg->guest_table_lock);
2038 return rc;
2039
2040 }
2041 EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
2042
2043 /**
2044 * gmap_shadow_pgt - instantiate a shadow page table
2045 * @sg: pointer to the shadow guest address space structure
2046 * @saddr: faulting address in the shadow gmap
2047 * @pgt: parent gmap address of the page table to get shadowed
2048 * @fake: pgt references contiguous guest memory block, not a pgtable
2049 *
2050 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2051 * shadow table structure is incomplete, -ENOMEM if out of memory,
2052 * -EFAULT if an address in the parent gmap could not be resolved and
2053 *
2054 * Called with gmap->mm->mmap_lock in read
2055 */
gmap_shadow_pgt(struct gmap * sg,unsigned long saddr,unsigned long pgt,int fake)2056 int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
2057 int fake)
2058 {
2059 unsigned long raddr, origin;
2060 unsigned long *table;
2061 struct page *page;
2062 phys_addr_t s_pgt;
2063 int rc;
2064
2065 BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
2066 /* Allocate a shadow page table */
2067 page = page_table_alloc_pgste(sg->mm);
2068 if (!page)
2069 return -ENOMEM;
2070 page->index = pgt & _SEGMENT_ENTRY_ORIGIN;
2071 if (fake)
2072 page->index |= GMAP_SHADOW_FAKE_TABLE;
2073 s_pgt = page_to_phys(page);
2074 /* Install shadow page table */
2075 spin_lock(&sg->guest_table_lock);
2076 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2077 if (!table) {
2078 rc = -EAGAIN; /* Race with unshadow */
2079 goto out_free;
2080 }
2081 if (!(*table & _SEGMENT_ENTRY_INVALID)) {
2082 rc = 0; /* Already established */
2083 goto out_free;
2084 } else if (*table & _SEGMENT_ENTRY_ORIGIN) {
2085 rc = -EAGAIN; /* Race with shadow */
2086 goto out_free;
2087 }
2088 /* mark as invalid as long as the parent table is not protected */
2089 *table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
2090 (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
2091 list_add(&page->lru, &sg->pt_list);
2092 if (fake) {
2093 /* nothing to protect for fake tables */
2094 *table &= ~_SEGMENT_ENTRY_INVALID;
2095 spin_unlock(&sg->guest_table_lock);
2096 return 0;
2097 }
2098 spin_unlock(&sg->guest_table_lock);
2099 /* Make pgt read-only in parent gmap page table (not the pgste) */
2100 raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
2101 origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
2102 rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
2103 spin_lock(&sg->guest_table_lock);
2104 if (!rc) {
2105 table = gmap_table_walk(sg, saddr, 1);
2106 if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) != s_pgt)
2107 rc = -EAGAIN; /* Race with unshadow */
2108 else
2109 *table &= ~_SEGMENT_ENTRY_INVALID;
2110 } else {
2111 gmap_unshadow_pgt(sg, raddr);
2112 }
2113 spin_unlock(&sg->guest_table_lock);
2114 return rc;
2115 out_free:
2116 spin_unlock(&sg->guest_table_lock);
2117 page_table_free_pgste(page);
2118 return rc;
2119
2120 }
2121 EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
2122
2123 /**
2124 * gmap_shadow_page - create a shadow page mapping
2125 * @sg: pointer to the shadow guest address space structure
2126 * @saddr: faulting address in the shadow gmap
2127 * @pte: pte in parent gmap address space to get shadowed
2128 *
2129 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2130 * shadow table structure is incomplete, -ENOMEM if out of memory and
2131 * -EFAULT if an address in the parent gmap could not be resolved.
2132 *
2133 * Called with sg->mm->mmap_lock in read.
2134 */
gmap_shadow_page(struct gmap * sg,unsigned long saddr,pte_t pte)2135 int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
2136 {
2137 struct gmap *parent;
2138 struct gmap_rmap *rmap;
2139 unsigned long vmaddr, paddr;
2140 spinlock_t *ptl;
2141 pte_t *sptep, *tptep;
2142 int prot;
2143 int rc;
2144
2145 BUG_ON(!gmap_is_shadow(sg));
2146 parent = sg->parent;
2147 prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
2148
2149 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
2150 if (!rmap)
2151 return -ENOMEM;
2152 rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
2153
2154 while (1) {
2155 paddr = pte_val(pte) & PAGE_MASK;
2156 vmaddr = __gmap_translate(parent, paddr);
2157 if (IS_ERR_VALUE(vmaddr)) {
2158 rc = vmaddr;
2159 break;
2160 }
2161 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
2162 if (rc)
2163 break;
2164 rc = -EAGAIN;
2165 sptep = gmap_pte_op_walk(parent, paddr, &ptl);
2166 if (sptep) {
2167 spin_lock(&sg->guest_table_lock);
2168 /* Get page table pointer */
2169 tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
2170 if (!tptep) {
2171 spin_unlock(&sg->guest_table_lock);
2172 gmap_pte_op_end(sptep, ptl);
2173 radix_tree_preload_end();
2174 break;
2175 }
2176 rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
2177 if (rc > 0) {
2178 /* Success and a new mapping */
2179 gmap_insert_rmap(sg, vmaddr, rmap);
2180 rmap = NULL;
2181 rc = 0;
2182 }
2183 gmap_pte_op_end(sptep, ptl);
2184 spin_unlock(&sg->guest_table_lock);
2185 }
2186 radix_tree_preload_end();
2187 if (!rc)
2188 break;
2189 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
2190 if (rc)
2191 break;
2192 }
2193 kfree(rmap);
2194 return rc;
2195 }
2196 EXPORT_SYMBOL_GPL(gmap_shadow_page);
2197
2198 /*
2199 * gmap_shadow_notify - handle notifications for shadow gmap
2200 *
2201 * Called with sg->parent->shadow_lock.
2202 */
gmap_shadow_notify(struct gmap * sg,unsigned long vmaddr,unsigned long gaddr)2203 static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
2204 unsigned long gaddr)
2205 {
2206 struct gmap_rmap *rmap, *rnext, *head;
2207 unsigned long start, end, bits, raddr;
2208
2209 BUG_ON(!gmap_is_shadow(sg));
2210
2211 spin_lock(&sg->guest_table_lock);
2212 if (sg->removed) {
2213 spin_unlock(&sg->guest_table_lock);
2214 return;
2215 }
2216 /* Check for top level table */
2217 start = sg->orig_asce & _ASCE_ORIGIN;
2218 end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
2219 if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
2220 gaddr < end) {
2221 /* The complete shadow table has to go */
2222 gmap_unshadow(sg);
2223 spin_unlock(&sg->guest_table_lock);
2224 list_del(&sg->list);
2225 gmap_put(sg);
2226 return;
2227 }
2228 /* Remove the page table tree from on specific entry */
2229 head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
2230 gmap_for_each_rmap_safe(rmap, rnext, head) {
2231 bits = rmap->raddr & _SHADOW_RMAP_MASK;
2232 raddr = rmap->raddr ^ bits;
2233 switch (bits) {
2234 case _SHADOW_RMAP_REGION1:
2235 gmap_unshadow_r2t(sg, raddr);
2236 break;
2237 case _SHADOW_RMAP_REGION2:
2238 gmap_unshadow_r3t(sg, raddr);
2239 break;
2240 case _SHADOW_RMAP_REGION3:
2241 gmap_unshadow_sgt(sg, raddr);
2242 break;
2243 case _SHADOW_RMAP_SEGMENT:
2244 gmap_unshadow_pgt(sg, raddr);
2245 break;
2246 case _SHADOW_RMAP_PGTABLE:
2247 gmap_unshadow_page(sg, raddr);
2248 break;
2249 }
2250 kfree(rmap);
2251 }
2252 spin_unlock(&sg->guest_table_lock);
2253 }
2254
2255 /**
2256 * ptep_notify - call all invalidation callbacks for a specific pte.
2257 * @mm: pointer to the process mm_struct
2258 * @vmaddr: virtual address in the process address space
2259 * @pte: pointer to the page table entry
2260 * @bits: bits from the pgste that caused the notify call
2261 *
2262 * This function is assumed to be called with the page table lock held
2263 * for the pte to notify.
2264 */
ptep_notify(struct mm_struct * mm,unsigned long vmaddr,pte_t * pte,unsigned long bits)2265 void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
2266 pte_t *pte, unsigned long bits)
2267 {
2268 unsigned long offset, gaddr = 0;
2269 unsigned long *table;
2270 struct gmap *gmap, *sg, *next;
2271
2272 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
2273 offset = offset * (PAGE_SIZE / sizeof(pte_t));
2274 rcu_read_lock();
2275 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2276 spin_lock(&gmap->guest_table_lock);
2277 table = radix_tree_lookup(&gmap->host_to_guest,
2278 vmaddr >> PMD_SHIFT);
2279 if (table)
2280 gaddr = __gmap_segment_gaddr(table) + offset;
2281 spin_unlock(&gmap->guest_table_lock);
2282 if (!table)
2283 continue;
2284
2285 if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
2286 spin_lock(&gmap->shadow_lock);
2287 list_for_each_entry_safe(sg, next,
2288 &gmap->children, list)
2289 gmap_shadow_notify(sg, vmaddr, gaddr);
2290 spin_unlock(&gmap->shadow_lock);
2291 }
2292 if (bits & PGSTE_IN_BIT)
2293 gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
2294 }
2295 rcu_read_unlock();
2296 }
2297 EXPORT_SYMBOL_GPL(ptep_notify);
2298
pmdp_notify_gmap(struct gmap * gmap,pmd_t * pmdp,unsigned long gaddr)2299 static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
2300 unsigned long gaddr)
2301 {
2302 set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
2303 gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
2304 }
2305
2306 /**
2307 * gmap_pmdp_xchg - exchange a gmap pmd with another
2308 * @gmap: pointer to the guest address space structure
2309 * @pmdp: pointer to the pmd entry
2310 * @new: replacement entry
2311 * @gaddr: the affected guest address
2312 *
2313 * This function is assumed to be called with the guest_table_lock
2314 * held.
2315 */
gmap_pmdp_xchg(struct gmap * gmap,pmd_t * pmdp,pmd_t new,unsigned long gaddr)2316 static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
2317 unsigned long gaddr)
2318 {
2319 gaddr &= HPAGE_MASK;
2320 pmdp_notify_gmap(gmap, pmdp, gaddr);
2321 new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_GMAP_IN));
2322 if (MACHINE_HAS_TLB_GUEST)
2323 __pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
2324 IDTE_GLOBAL);
2325 else if (MACHINE_HAS_IDTE)
2326 __pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
2327 else
2328 __pmdp_csp(pmdp);
2329 set_pmd(pmdp, new);
2330 }
2331
gmap_pmdp_clear(struct mm_struct * mm,unsigned long vmaddr,int purge)2332 static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
2333 int purge)
2334 {
2335 pmd_t *pmdp;
2336 struct gmap *gmap;
2337 unsigned long gaddr;
2338
2339 rcu_read_lock();
2340 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2341 spin_lock(&gmap->guest_table_lock);
2342 pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
2343 vmaddr >> PMD_SHIFT);
2344 if (pmdp) {
2345 gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
2346 pmdp_notify_gmap(gmap, pmdp, gaddr);
2347 WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2348 _SEGMENT_ENTRY_GMAP_UC));
2349 if (purge)
2350 __pmdp_csp(pmdp);
2351 set_pmd(pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
2352 }
2353 spin_unlock(&gmap->guest_table_lock);
2354 }
2355 rcu_read_unlock();
2356 }
2357
2358 /**
2359 * gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
2360 * flushing
2361 * @mm: pointer to the process mm_struct
2362 * @vmaddr: virtual address in the process address space
2363 */
gmap_pmdp_invalidate(struct mm_struct * mm,unsigned long vmaddr)2364 void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
2365 {
2366 gmap_pmdp_clear(mm, vmaddr, 0);
2367 }
2368 EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);
2369
2370 /**
2371 * gmap_pmdp_csp - csp all affected guest pmd entries
2372 * @mm: pointer to the process mm_struct
2373 * @vmaddr: virtual address in the process address space
2374 */
gmap_pmdp_csp(struct mm_struct * mm,unsigned long vmaddr)2375 void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
2376 {
2377 gmap_pmdp_clear(mm, vmaddr, 1);
2378 }
2379 EXPORT_SYMBOL_GPL(gmap_pmdp_csp);
2380
2381 /**
2382 * gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
2383 * @mm: pointer to the process mm_struct
2384 * @vmaddr: virtual address in the process address space
2385 */
gmap_pmdp_idte_local(struct mm_struct * mm,unsigned long vmaddr)2386 void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
2387 {
2388 unsigned long *entry, gaddr;
2389 struct gmap *gmap;
2390 pmd_t *pmdp;
2391
2392 rcu_read_lock();
2393 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2394 spin_lock(&gmap->guest_table_lock);
2395 entry = radix_tree_delete(&gmap->host_to_guest,
2396 vmaddr >> PMD_SHIFT);
2397 if (entry) {
2398 pmdp = (pmd_t *)entry;
2399 gaddr = __gmap_segment_gaddr(entry);
2400 pmdp_notify_gmap(gmap, pmdp, gaddr);
2401 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2402 _SEGMENT_ENTRY_GMAP_UC));
2403 if (MACHINE_HAS_TLB_GUEST)
2404 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2405 gmap->asce, IDTE_LOCAL);
2406 else if (MACHINE_HAS_IDTE)
2407 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
2408 *entry = _SEGMENT_ENTRY_EMPTY;
2409 }
2410 spin_unlock(&gmap->guest_table_lock);
2411 }
2412 rcu_read_unlock();
2413 }
2414 EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);
2415
2416 /**
2417 * gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
2418 * @mm: pointer to the process mm_struct
2419 * @vmaddr: virtual address in the process address space
2420 */
gmap_pmdp_idte_global(struct mm_struct * mm,unsigned long vmaddr)2421 void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
2422 {
2423 unsigned long *entry, gaddr;
2424 struct gmap *gmap;
2425 pmd_t *pmdp;
2426
2427 rcu_read_lock();
2428 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2429 spin_lock(&gmap->guest_table_lock);
2430 entry = radix_tree_delete(&gmap->host_to_guest,
2431 vmaddr >> PMD_SHIFT);
2432 if (entry) {
2433 pmdp = (pmd_t *)entry;
2434 gaddr = __gmap_segment_gaddr(entry);
2435 pmdp_notify_gmap(gmap, pmdp, gaddr);
2436 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2437 _SEGMENT_ENTRY_GMAP_UC));
2438 if (MACHINE_HAS_TLB_GUEST)
2439 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2440 gmap->asce, IDTE_GLOBAL);
2441 else if (MACHINE_HAS_IDTE)
2442 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
2443 else
2444 __pmdp_csp(pmdp);
2445 *entry = _SEGMENT_ENTRY_EMPTY;
2446 }
2447 spin_unlock(&gmap->guest_table_lock);
2448 }
2449 rcu_read_unlock();
2450 }
2451 EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);
2452
2453 /**
2454 * gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
2455 * @gmap: pointer to guest address space
2456 * @pmdp: pointer to the pmd to be tested
2457 * @gaddr: virtual address in the guest address space
2458 *
2459 * This function is assumed to be called with the guest_table_lock
2460 * held.
2461 */
gmap_test_and_clear_dirty_pmd(struct gmap * gmap,pmd_t * pmdp,unsigned long gaddr)2462 static bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
2463 unsigned long gaddr)
2464 {
2465 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
2466 return false;
2467
2468 /* Already protected memory, which did not change is clean */
2469 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
2470 !(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
2471 return false;
2472
2473 /* Clear UC indication and reset protection */
2474 set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_UC)));
2475 gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
2476 return true;
2477 }
2478
2479 /**
2480 * gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
2481 * @gmap: pointer to guest address space
2482 * @bitmap: dirty bitmap for this pmd
2483 * @gaddr: virtual address in the guest address space
2484 * @vmaddr: virtual address in the host address space
2485 *
2486 * This function is assumed to be called with the guest_table_lock
2487 * held.
2488 */
gmap_sync_dirty_log_pmd(struct gmap * gmap,unsigned long bitmap[4],unsigned long gaddr,unsigned long vmaddr)2489 void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
2490 unsigned long gaddr, unsigned long vmaddr)
2491 {
2492 int i;
2493 pmd_t *pmdp;
2494 pte_t *ptep;
2495 spinlock_t *ptl;
2496
2497 pmdp = gmap_pmd_op_walk(gmap, gaddr);
2498 if (!pmdp)
2499 return;
2500
2501 if (pmd_large(*pmdp)) {
2502 if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
2503 bitmap_fill(bitmap, _PAGE_ENTRIES);
2504 } else {
2505 for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
2506 ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
2507 if (!ptep)
2508 continue;
2509 if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
2510 set_bit(i, bitmap);
2511 pte_unmap_unlock(ptep, ptl);
2512 }
2513 }
2514 gmap_pmd_op_end(gmap, pmdp);
2515 }
2516 EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);
2517
2518 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
thp_split_walk_pmd_entry(pmd_t * pmd,unsigned long addr,unsigned long end,struct mm_walk * walk)2519 static int thp_split_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
2520 unsigned long end, struct mm_walk *walk)
2521 {
2522 struct vm_area_struct *vma = walk->vma;
2523
2524 split_huge_pmd(vma, pmd, addr);
2525 return 0;
2526 }
2527
2528 static const struct mm_walk_ops thp_split_walk_ops = {
2529 .pmd_entry = thp_split_walk_pmd_entry,
2530 .walk_lock = PGWALK_WRLOCK_VERIFY,
2531 };
2532
thp_split_mm(struct mm_struct * mm)2533 static inline void thp_split_mm(struct mm_struct *mm)
2534 {
2535 struct vm_area_struct *vma;
2536 VMA_ITERATOR(vmi, mm, 0);
2537
2538 for_each_vma(vmi, vma) {
2539 vm_flags_mod(vma, VM_NOHUGEPAGE, VM_HUGEPAGE);
2540 walk_page_vma(vma, &thp_split_walk_ops, NULL);
2541 }
2542 mm->def_flags |= VM_NOHUGEPAGE;
2543 }
2544 #else
thp_split_mm(struct mm_struct * mm)2545 static inline void thp_split_mm(struct mm_struct *mm)
2546 {
2547 }
2548 #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
2549
2550 /*
2551 * switch on pgstes for its userspace process (for kvm)
2552 */
s390_enable_sie(void)2553 int s390_enable_sie(void)
2554 {
2555 struct mm_struct *mm = current->mm;
2556
2557 /* Do we have pgstes? if yes, we are done */
2558 if (mm_has_pgste(mm))
2559 return 0;
2560 /* Fail if the page tables are 2K */
2561 if (!mm_alloc_pgste(mm))
2562 return -EINVAL;
2563 mmap_write_lock(mm);
2564 mm->context.has_pgste = 1;
2565 /* split thp mappings and disable thp for future mappings */
2566 thp_split_mm(mm);
2567 mmap_write_unlock(mm);
2568 return 0;
2569 }
2570 EXPORT_SYMBOL_GPL(s390_enable_sie);
2571
find_zeropage_pte_entry(pte_t * pte,unsigned long addr,unsigned long end,struct mm_walk * walk)2572 static int find_zeropage_pte_entry(pte_t *pte, unsigned long addr,
2573 unsigned long end, struct mm_walk *walk)
2574 {
2575 unsigned long *found_addr = walk->private;
2576
2577 /* Return 1 of the page is a zeropage. */
2578 if (is_zero_pfn(pte_pfn(*pte))) {
2579 /*
2580 * Shared zeropage in e.g., a FS DAX mapping? We cannot do the
2581 * right thing and likely don't care: FAULT_FLAG_UNSHARE
2582 * currently only works in COW mappings, which is also where
2583 * mm_forbids_zeropage() is checked.
2584 */
2585 if (!is_cow_mapping(walk->vma->vm_flags))
2586 return -EFAULT;
2587
2588 *found_addr = addr;
2589 return 1;
2590 }
2591 return 0;
2592 }
2593
2594 static const struct mm_walk_ops find_zeropage_ops = {
2595 .pte_entry = find_zeropage_pte_entry,
2596 .walk_lock = PGWALK_WRLOCK,
2597 };
2598
2599 /*
2600 * Unshare all shared zeropages, replacing them by anonymous pages. Note that
2601 * we cannot simply zap all shared zeropages, because this could later
2602 * trigger unexpected userfaultfd missing events.
2603 *
2604 * This must be called after mm->context.allow_cow_sharing was
2605 * set to 0, to avoid future mappings of shared zeropages.
2606 *
2607 * mm contracts with s390, that even if mm were to remove a page table,
2608 * and racing with walk_page_range_vma() calling pte_offset_map_lock()
2609 * would fail, it will never insert a page table containing empty zero
2610 * pages once mm_forbids_zeropage(mm) i.e.
2611 * mm->context.allow_cow_sharing is set to 0.
2612 */
__s390_unshare_zeropages(struct mm_struct * mm)2613 static int __s390_unshare_zeropages(struct mm_struct *mm)
2614 {
2615 struct vm_area_struct *vma;
2616 VMA_ITERATOR(vmi, mm, 0);
2617 unsigned long addr;
2618 vm_fault_t fault;
2619 int rc;
2620
2621 for_each_vma(vmi, vma) {
2622 /*
2623 * We could only look at COW mappings, but it's more future
2624 * proof to catch unexpected zeropages in other mappings and
2625 * fail.
2626 */
2627 if ((vma->vm_flags & VM_PFNMAP) || is_vm_hugetlb_page(vma))
2628 continue;
2629 addr = vma->vm_start;
2630
2631 retry:
2632 rc = walk_page_range_vma(vma, addr, vma->vm_end,
2633 &find_zeropage_ops, &addr);
2634 if (rc < 0)
2635 return rc;
2636 else if (!rc)
2637 continue;
2638
2639 /* addr was updated by find_zeropage_pte_entry() */
2640 fault = handle_mm_fault(vma, addr,
2641 FAULT_FLAG_UNSHARE | FAULT_FLAG_REMOTE,
2642 NULL);
2643 if (fault & VM_FAULT_OOM)
2644 return -ENOMEM;
2645 /*
2646 * See break_ksm(): even after handle_mm_fault() returned 0, we
2647 * must start the lookup from the current address, because
2648 * handle_mm_fault() may back out if there's any difficulty.
2649 *
2650 * VM_FAULT_SIGBUS and VM_FAULT_SIGSEGV are unexpected but
2651 * maybe they could trigger in the future on concurrent
2652 * truncation. In that case, the shared zeropage would be gone
2653 * and we can simply retry and make progress.
2654 */
2655 cond_resched();
2656 goto retry;
2657 }
2658
2659 return 0;
2660 }
2661
__s390_disable_cow_sharing(struct mm_struct * mm)2662 static int __s390_disable_cow_sharing(struct mm_struct *mm)
2663 {
2664 int rc;
2665
2666 if (!mm->context.allow_cow_sharing)
2667 return 0;
2668
2669 mm->context.allow_cow_sharing = 0;
2670
2671 /* Replace all shared zeropages by anonymous pages. */
2672 rc = __s390_unshare_zeropages(mm);
2673 /*
2674 * Make sure to disable KSM (if enabled for the whole process or
2675 * individual VMAs). Note that nothing currently hinders user space
2676 * from re-enabling it.
2677 */
2678 if (!rc)
2679 rc = ksm_disable(mm);
2680 if (rc)
2681 mm->context.allow_cow_sharing = 1;
2682 return rc;
2683 }
2684
2685 /*
2686 * Disable most COW-sharing of memory pages for the whole process:
2687 * (1) Disable KSM and unmerge/unshare any KSM pages.
2688 * (2) Disallow shared zeropages and unshare any zerpages that are mapped.
2689 *
2690 * Not that we currently don't bother with COW-shared pages that are shared
2691 * with parent/child processes due to fork().
2692 */
s390_disable_cow_sharing(void)2693 int s390_disable_cow_sharing(void)
2694 {
2695 int rc;
2696
2697 mmap_write_lock(current->mm);
2698 rc = __s390_disable_cow_sharing(current->mm);
2699 mmap_write_unlock(current->mm);
2700 return rc;
2701 }
2702 EXPORT_SYMBOL_GPL(s390_disable_cow_sharing);
2703
2704 /*
2705 * Enable storage key handling from now on and initialize the storage
2706 * keys with the default key.
2707 */
__s390_enable_skey_pte(pte_t * pte,unsigned long addr,unsigned long next,struct mm_walk * walk)2708 static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
2709 unsigned long next, struct mm_walk *walk)
2710 {
2711 /* Clear storage key */
2712 ptep_zap_key(walk->mm, addr, pte);
2713 return 0;
2714 }
2715
2716 /*
2717 * Give a chance to schedule after setting a key to 256 pages.
2718 * We only hold the mm lock, which is a rwsem and the kvm srcu.
2719 * Both can sleep.
2720 */
__s390_enable_skey_pmd(pmd_t * pmd,unsigned long addr,unsigned long next,struct mm_walk * walk)2721 static int __s390_enable_skey_pmd(pmd_t *pmd, unsigned long addr,
2722 unsigned long next, struct mm_walk *walk)
2723 {
2724 cond_resched();
2725 return 0;
2726 }
2727
__s390_enable_skey_hugetlb(pte_t * pte,unsigned long addr,unsigned long hmask,unsigned long next,struct mm_walk * walk)2728 static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
2729 unsigned long hmask, unsigned long next,
2730 struct mm_walk *walk)
2731 {
2732 pmd_t *pmd = (pmd_t *)pte;
2733 unsigned long start, end;
2734 struct page *page = pmd_page(*pmd);
2735
2736 /*
2737 * The write check makes sure we do not set a key on shared
2738 * memory. This is needed as the walker does not differentiate
2739 * between actual guest memory and the process executable or
2740 * shared libraries.
2741 */
2742 if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
2743 !(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
2744 return 0;
2745
2746 start = pmd_val(*pmd) & HPAGE_MASK;
2747 end = start + HPAGE_SIZE;
2748 __storage_key_init_range(start, end);
2749 set_bit(PG_arch_1, &page->flags);
2750 cond_resched();
2751 return 0;
2752 }
2753
2754 static const struct mm_walk_ops enable_skey_walk_ops = {
2755 .hugetlb_entry = __s390_enable_skey_hugetlb,
2756 .pte_entry = __s390_enable_skey_pte,
2757 .pmd_entry = __s390_enable_skey_pmd,
2758 .walk_lock = PGWALK_WRLOCK,
2759 };
2760
s390_enable_skey(void)2761 int s390_enable_skey(void)
2762 {
2763 struct mm_struct *mm = current->mm;
2764 int rc = 0;
2765
2766 mmap_write_lock(mm);
2767 if (mm_uses_skeys(mm))
2768 goto out_up;
2769
2770 mm->context.uses_skeys = 1;
2771 rc = __s390_disable_cow_sharing(mm);
2772 if (rc) {
2773 mm->context.uses_skeys = 0;
2774 goto out_up;
2775 }
2776 walk_page_range(mm, 0, TASK_SIZE, &enable_skey_walk_ops, NULL);
2777
2778 out_up:
2779 mmap_write_unlock(mm);
2780 return rc;
2781 }
2782 EXPORT_SYMBOL_GPL(s390_enable_skey);
2783
2784 /*
2785 * Reset CMMA state, make all pages stable again.
2786 */
__s390_reset_cmma(pte_t * pte,unsigned long addr,unsigned long next,struct mm_walk * walk)2787 static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
2788 unsigned long next, struct mm_walk *walk)
2789 {
2790 ptep_zap_unused(walk->mm, addr, pte, 1);
2791 return 0;
2792 }
2793
2794 static const struct mm_walk_ops reset_cmma_walk_ops = {
2795 .pte_entry = __s390_reset_cmma,
2796 .walk_lock = PGWALK_WRLOCK,
2797 };
2798
s390_reset_cmma(struct mm_struct * mm)2799 void s390_reset_cmma(struct mm_struct *mm)
2800 {
2801 mmap_write_lock(mm);
2802 walk_page_range(mm, 0, TASK_SIZE, &reset_cmma_walk_ops, NULL);
2803 mmap_write_unlock(mm);
2804 }
2805 EXPORT_SYMBOL_GPL(s390_reset_cmma);
2806
2807 #define GATHER_GET_PAGES 32
2808
2809 struct reset_walk_state {
2810 unsigned long next;
2811 unsigned long count;
2812 unsigned long pfns[GATHER_GET_PAGES];
2813 };
2814
s390_gather_pages(pte_t * ptep,unsigned long addr,unsigned long next,struct mm_walk * walk)2815 static int s390_gather_pages(pte_t *ptep, unsigned long addr,
2816 unsigned long next, struct mm_walk *walk)
2817 {
2818 struct reset_walk_state *p = walk->private;
2819 pte_t pte = READ_ONCE(*ptep);
2820
2821 if (pte_present(pte)) {
2822 /* we have a reference from the mapping, take an extra one */
2823 get_page(phys_to_page(pte_val(pte)));
2824 p->pfns[p->count] = phys_to_pfn(pte_val(pte));
2825 p->next = next;
2826 p->count++;
2827 }
2828 return p->count >= GATHER_GET_PAGES;
2829 }
2830
2831 static const struct mm_walk_ops gather_pages_ops = {
2832 .pte_entry = s390_gather_pages,
2833 .walk_lock = PGWALK_RDLOCK,
2834 };
2835
2836 /*
2837 * Call the Destroy secure page UVC on each page in the given array of PFNs.
2838 * Each page needs to have an extra reference, which will be released here.
2839 */
s390_uv_destroy_pfns(unsigned long count,unsigned long * pfns)2840 void s390_uv_destroy_pfns(unsigned long count, unsigned long *pfns)
2841 {
2842 unsigned long i;
2843
2844 for (i = 0; i < count; i++) {
2845 /* we always have an extra reference */
2846 uv_destroy_owned_page(pfn_to_phys(pfns[i]));
2847 /* get rid of the extra reference */
2848 put_page(pfn_to_page(pfns[i]));
2849 cond_resched();
2850 }
2851 }
2852 EXPORT_SYMBOL_GPL(s390_uv_destroy_pfns);
2853
2854 /**
2855 * __s390_uv_destroy_range - Call the destroy secure page UVC on each page
2856 * in the given range of the given address space.
2857 * @mm: the mm to operate on
2858 * @start: the start of the range
2859 * @end: the end of the range
2860 * @interruptible: if not 0, stop when a fatal signal is received
2861 *
2862 * Walk the given range of the given address space and call the destroy
2863 * secure page UVC on each page. Optionally exit early if a fatal signal is
2864 * pending.
2865 *
2866 * Return: 0 on success, -EINTR if the function stopped before completing
2867 */
__s390_uv_destroy_range(struct mm_struct * mm,unsigned long start,unsigned long end,bool interruptible)2868 int __s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
2869 unsigned long end, bool interruptible)
2870 {
2871 struct reset_walk_state state = { .next = start };
2872 int r = 1;
2873
2874 while (r > 0) {
2875 state.count = 0;
2876 mmap_read_lock(mm);
2877 r = walk_page_range(mm, state.next, end, &gather_pages_ops, &state);
2878 mmap_read_unlock(mm);
2879 cond_resched();
2880 s390_uv_destroy_pfns(state.count, state.pfns);
2881 if (interruptible && fatal_signal_pending(current))
2882 return -EINTR;
2883 }
2884 return 0;
2885 }
2886 EXPORT_SYMBOL_GPL(__s390_uv_destroy_range);
2887
2888 /**
2889 * s390_unlist_old_asce - Remove the topmost level of page tables from the
2890 * list of page tables of the gmap.
2891 * @gmap: the gmap whose table is to be removed
2892 *
2893 * On s390x, KVM keeps a list of all pages containing the page tables of the
2894 * gmap (the CRST list). This list is used at tear down time to free all
2895 * pages that are now not needed anymore.
2896 *
2897 * This function removes the topmost page of the tree (the one pointed to by
2898 * the ASCE) from the CRST list.
2899 *
2900 * This means that it will not be freed when the VM is torn down, and needs
2901 * to be handled separately by the caller, unless a leak is actually
2902 * intended. Notice that this function will only remove the page from the
2903 * list, the page will still be used as a top level page table (and ASCE).
2904 */
s390_unlist_old_asce(struct gmap * gmap)2905 void s390_unlist_old_asce(struct gmap *gmap)
2906 {
2907 struct page *old;
2908
2909 old = virt_to_page(gmap->table);
2910 spin_lock(&gmap->guest_table_lock);
2911 list_del(&old->lru);
2912 /*
2913 * Sometimes the topmost page might need to be "removed" multiple
2914 * times, for example if the VM is rebooted into secure mode several
2915 * times concurrently, or if s390_replace_asce fails after calling
2916 * s390_remove_old_asce and is attempted again later. In that case
2917 * the old asce has been removed from the list, and therefore it
2918 * will not be freed when the VM terminates, but the ASCE is still
2919 * in use and still pointed to.
2920 * A subsequent call to replace_asce will follow the pointer and try
2921 * to remove the same page from the list again.
2922 * Therefore it's necessary that the page of the ASCE has valid
2923 * pointers, so list_del can work (and do nothing) without
2924 * dereferencing stale or invalid pointers.
2925 */
2926 INIT_LIST_HEAD(&old->lru);
2927 spin_unlock(&gmap->guest_table_lock);
2928 }
2929 EXPORT_SYMBOL_GPL(s390_unlist_old_asce);
2930
2931 /**
2932 * s390_replace_asce - Try to replace the current ASCE of a gmap with a copy
2933 * @gmap: the gmap whose ASCE needs to be replaced
2934 *
2935 * If the ASCE is a SEGMENT type then this function will return -EINVAL,
2936 * otherwise the pointers in the host_to_guest radix tree will keep pointing
2937 * to the wrong pages, causing use-after-free and memory corruption.
2938 * If the allocation of the new top level page table fails, the ASCE is not
2939 * replaced.
2940 * In any case, the old ASCE is always removed from the gmap CRST list.
2941 * Therefore the caller has to make sure to save a pointer to it
2942 * beforehand, unless a leak is actually intended.
2943 */
s390_replace_asce(struct gmap * gmap)2944 int s390_replace_asce(struct gmap *gmap)
2945 {
2946 unsigned long asce;
2947 struct page *page;
2948 void *table;
2949
2950 s390_unlist_old_asce(gmap);
2951
2952 /* Replacing segment type ASCEs would cause serious issues */
2953 if ((gmap->asce & _ASCE_TYPE_MASK) == _ASCE_TYPE_SEGMENT)
2954 return -EINVAL;
2955
2956 page = gmap_alloc_crst();
2957 if (!page)
2958 return -ENOMEM;
2959 page->index = 0;
2960 table = page_to_virt(page);
2961 memcpy(table, gmap->table, 1UL << (CRST_ALLOC_ORDER + PAGE_SHIFT));
2962
2963 /*
2964 * The caller has to deal with the old ASCE, but here we make sure
2965 * the new one is properly added to the CRST list, so that
2966 * it will be freed when the VM is torn down.
2967 */
2968 spin_lock(&gmap->guest_table_lock);
2969 list_add(&page->lru, &gmap->crst_list);
2970 spin_unlock(&gmap->guest_table_lock);
2971
2972 /* Set new table origin while preserving existing ASCE control bits */
2973 asce = (gmap->asce & ~_ASCE_ORIGIN) | __pa(table);
2974 WRITE_ONCE(gmap->asce, asce);
2975 WRITE_ONCE(gmap->mm->context.gmap_asce, asce);
2976 WRITE_ONCE(gmap->table, table);
2977
2978 return 0;
2979 }
2980 EXPORT_SYMBOL_GPL(s390_replace_asce);
2981