blob: eb0b51a36be013a728c80315254c9e898e1bb56f [file] [log] [blame]
Greg Kroah-Hartmanac41aae2017-11-24 15:00:35 +01001// SPDX-License-Identifier: GPL-2.0
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002/*
3 * KVM guest address space mapping code
4 *
Christian Borntraeger0cd2a782020-11-09 13:14:35 +01005 * Copyright IBM Corp. 2007, 2020
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01006 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
Janosch Franka9e00d82018-07-13 11:28:37 +01007 * David Hildenbrand <david@redhat.com>
8 * Janosch Frank <frankja@linux.vnet.ibm.com>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01009 */
10
11#include <linux/kernel.h>
Christoph Hellwiga5201102019-08-28 16:19:53 +020012#include <linux/pagewalk.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010013#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>
Mike Rapoportca5999f2020-06-08 21:32:38 -070020#include <linux/pgtable.h>
Heiko Carstens65d37f12023-10-27 14:12:38 +020021#include <asm/page-states.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010022#include <asm/pgalloc.h>
23#include <asm/gmap.h>
Heiko Carstens1954da42023-10-20 17:26:50 +020024#include <asm/page.h>
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010025#include <asm/tlb.h>
26
David Hildenbrandfd8d4e32016-04-18 13:24:52 +020027#define GMAP_SHADOW_FAKE_TABLE 1ULL
28
Heiko Carstens1954da42023-10-20 17:26:50 +020029static 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;
Heiko Carstens65d37f12023-10-27 14:12:38 +020036 __arch_set_page_dat(page_to_virt(page), 1UL << CRST_ALLOC_ORDER);
Heiko Carstens1954da42023-10-20 17:26:50 +020037 return page;
38}
39
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010040/**
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +010041 * gmap_alloc - allocate and initialize a guest address space
Christian Borntraeger9c650d02016-04-04 09:41:32 +020042 * @limit: maximum address of the gmap address space
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010043 *
44 * Returns a guest address space structure.
45 */
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +010046static struct gmap *gmap_alloc(unsigned long limit)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010047{
48 struct gmap *gmap;
49 struct page *page;
50 unsigned long *table;
51 unsigned long etype, atype;
52
Heiko Carstensf1c11742017-07-05 07:37:27 +020053 if (limit < _REGION3_SIZE) {
54 limit = _REGION3_SIZE - 1;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010055 atype = _ASCE_TYPE_SEGMENT;
56 etype = _SEGMENT_ENTRY_EMPTY;
Heiko Carstensf1c11742017-07-05 07:37:27 +020057 } else if (limit < _REGION2_SIZE) {
58 limit = _REGION2_SIZE - 1;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010059 atype = _ASCE_TYPE_REGION3;
60 etype = _REGION3_ENTRY_EMPTY;
Heiko Carstensf1c11742017-07-05 07:37:27 +020061 } else if (limit < _REGION1_SIZE) {
62 limit = _REGION1_SIZE - 1;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010063 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 }
Christian Borntraeger0cd2a782020-11-09 13:14:35 +010070 gmap = kzalloc(sizeof(struct gmap), GFP_KERNEL_ACCOUNT);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010071 if (!gmap)
72 goto out;
73 INIT_LIST_HEAD(&gmap->crst_list);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +010074 INIT_LIST_HEAD(&gmap->children);
75 INIT_LIST_HEAD(&gmap->pt_list);
Christian Borntraeger0cd2a782020-11-09 13:14:35 +010076 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);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010079 spin_lock_init(&gmap->guest_table_lock);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +010080 spin_lock_init(&gmap->shadow_lock);
Chuhong Yuan40e90652019-08-08 15:18:26 +080081 refcount_set(&gmap->ref_count, 1);
Heiko Carstens1954da42023-10-20 17:26:50 +020082 page = gmap_alloc_crst();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010083 if (!page)
84 goto out_free;
85 page->index = 0;
86 list_add(&page->lru, &gmap->crst_list);
Nico Boehr079f0c22022-10-20 16:31:55 +020087 table = page_to_virt(page);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010088 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;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +010093 return gmap;
94
95out_free:
96 kfree(gmap);
97out:
98 return NULL;
99}
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100100
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 */
108struct gmap *gmap_create(struct mm_struct *mm, unsigned long limit)
109{
110 struct gmap *gmap;
Martin Schwidefsky44b6cc82016-06-13 10:36:00 +0200111 unsigned long gmap_asce;
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100112
113 gmap = gmap_alloc(limit);
114 if (!gmap)
115 return NULL;
116 gmap->mm = mm;
Martin Schwidefskyf28a4b42017-08-17 18:17:49 +0200117 spin_lock(&mm->context.lock);
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100118 list_add_rcu(&gmap->list, &mm->context.gmap_list);
Martin Schwidefsky44b6cc82016-06-13 10:36:00 +0200119 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);
Martin Schwidefskyf28a4b42017-08-17 18:17:49 +0200124 spin_unlock(&mm->context.lock);
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100125 return gmap;
126}
127EXPORT_SYMBOL_GPL(gmap_create);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100128
129static void gmap_flush_tlb(struct gmap *gmap)
130{
131 if (MACHINE_HAS_IDTE)
David Hildenbrandf0454022016-07-07 10:44:10 +0200132 __tlb_flush_idte(gmap->asce);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100133 else
134 __tlb_flush_global();
135}
136
137static 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;
Heiko Carstensd12a3d62017-05-09 13:44:43 +0200142 void __rcu **slot;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100143 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
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100161static 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;
Heiko Carstensd12a3d62017-05-09 13:44:43 +0200167 void __rcu **slot;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100168 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
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100188/**
189 * gmap_free - free a guest address space
190 * @gmap: pointer to the guest address space structure
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100191 *
192 * No locks required. There are no references to this gmap anymore.
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100193 */
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100194static void gmap_free(struct gmap *gmap)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100195{
196 struct page *page, *next;
197
David Hildenbrandeea36782016-04-15 12:45:45 +0200198 /* Flush tlb of all gmaps (if not already done for shadows) */
199 if (!(gmap_is_shadow(gmap) && gmap->removed))
200 gmap_flush_tlb(gmap);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100201 /* Free all segment & region tables. */
202 list_for_each_entry_safe(page, next, &gmap->crst_list, lru)
Heiko Carstensf1c11742017-07-05 07:37:27 +0200203 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100204 gmap_radix_tree_free(&gmap->guest_to_host);
205 gmap_radix_tree_free(&gmap->host_to_guest);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100206
207 /* Free additional data for a shadow gmap */
208 if (gmap_is_shadow(gmap)) {
Qi Zheng57b77b72024-03-04 19:07:20 +0800209 struct ptdesc *ptdesc, *n;
210
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100211 /* Free all page tables. */
Qi Zheng57b77b72024-03-04 19:07:20 +0800212 list_for_each_entry_safe(ptdesc, n, &gmap->pt_list, pt_list)
213 page_table_free_pgste(ptdesc);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100214 gmap_rmap_radix_tree_free(&gmap->host_to_rmap);
215 /* Release reference to the parent */
216 gmap_put(gmap->parent);
217 }
218
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100219 kfree(gmap);
220}
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100221
222/**
223 * gmap_get - increase reference counter for guest address space
224 * @gmap: pointer to the guest address space structure
225 *
226 * Returns the gmap pointer
227 */
228struct gmap *gmap_get(struct gmap *gmap)
229{
Chuhong Yuan40e90652019-08-08 15:18:26 +0800230 refcount_inc(&gmap->ref_count);
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100231 return gmap;
232}
233EXPORT_SYMBOL_GPL(gmap_get);
234
235/**
236 * gmap_put - decrease reference counter for guest address space
237 * @gmap: pointer to the guest address space structure
238 *
239 * If the reference counter reaches zero the guest address space is freed.
240 */
241void gmap_put(struct gmap *gmap)
242{
Chuhong Yuan40e90652019-08-08 15:18:26 +0800243 if (refcount_dec_and_test(&gmap->ref_count))
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100244 gmap_free(gmap);
245}
246EXPORT_SYMBOL_GPL(gmap_put);
247
248/**
249 * gmap_remove - remove a guest address space but do not free it yet
250 * @gmap: pointer to the guest address space structure
251 */
252void gmap_remove(struct gmap *gmap)
253{
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100254 struct gmap *sg, *next;
Martin Schwidefsky44b6cc82016-06-13 10:36:00 +0200255 unsigned long gmap_asce;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100256
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100257 /* Remove all shadow gmaps linked to this gmap */
258 if (!list_empty(&gmap->children)) {
259 spin_lock(&gmap->shadow_lock);
260 list_for_each_entry_safe(sg, next, &gmap->children, list) {
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100261 list_del(&sg->list);
262 gmap_put(sg);
263 }
264 spin_unlock(&gmap->shadow_lock);
265 }
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100266 /* Remove gmap from the pre-mm list */
Martin Schwidefskyf28a4b42017-08-17 18:17:49 +0200267 spin_lock(&gmap->mm->context.lock);
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100268 list_del_rcu(&gmap->list);
Martin Schwidefsky44b6cc82016-06-13 10:36:00 +0200269 if (list_empty(&gmap->mm->context.gmap_list))
270 gmap_asce = 0;
271 else if (list_is_singular(&gmap->mm->context.gmap_list))
272 gmap_asce = list_first_entry(&gmap->mm->context.gmap_list,
273 struct gmap, list)->asce;
274 else
275 gmap_asce = -1UL;
276 WRITE_ONCE(gmap->mm->context.gmap_asce, gmap_asce);
Martin Schwidefskyf28a4b42017-08-17 18:17:49 +0200277 spin_unlock(&gmap->mm->context.lock);
Martin Schwidefsky6ea427b2016-03-08 11:55:04 +0100278 synchronize_rcu();
279 /* Put reference */
280 gmap_put(gmap);
281}
282EXPORT_SYMBOL_GPL(gmap_remove);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100283
284/**
285 * gmap_enable - switch primary space to the guest address space
286 * @gmap: pointer to the guest address space structure
287 */
288void gmap_enable(struct gmap *gmap)
289{
Sven Schnelle208da1d2024-06-10 13:45:25 +0200290 get_lowcore()->gmap = (unsigned long)gmap;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100291}
292EXPORT_SYMBOL_GPL(gmap_enable);
293
294/**
295 * gmap_disable - switch back to the standard primary address space
296 * @gmap: pointer to the guest address space structure
297 */
298void gmap_disable(struct gmap *gmap)
299{
Sven Schnelle208da1d2024-06-10 13:45:25 +0200300 get_lowcore()->gmap = 0UL;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100301}
302EXPORT_SYMBOL_GPL(gmap_disable);
303
David Hildenbrand37d9df92015-03-11 16:47:33 +0100304/**
305 * gmap_get_enabled - get a pointer to the currently enabled gmap
306 *
307 * Returns a pointer to the currently enabled gmap. 0 if none is enabled.
308 */
309struct gmap *gmap_get_enabled(void)
310{
Sven Schnelle208da1d2024-06-10 13:45:25 +0200311 return (struct gmap *)get_lowcore()->gmap;
David Hildenbrand37d9df92015-03-11 16:47:33 +0100312}
313EXPORT_SYMBOL_GPL(gmap_get_enabled);
314
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100315/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700316 * gmap_alloc_table is assumed to be called with mmap_lock held
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100317 */
318static int gmap_alloc_table(struct gmap *gmap, unsigned long *table,
319 unsigned long init, unsigned long gaddr)
320{
321 struct page *page;
322 unsigned long *new;
323
324 /* since we dont free the gmap table until gmap_free we can unlock */
Heiko Carstens1954da42023-10-20 17:26:50 +0200325 page = gmap_alloc_crst();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100326 if (!page)
327 return -ENOMEM;
Nico Boehr079f0c22022-10-20 16:31:55 +0200328 new = page_to_virt(page);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100329 crst_table_init(new, init);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100330 spin_lock(&gmap->guest_table_lock);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100331 if (*table & _REGION_ENTRY_INVALID) {
332 list_add(&page->lru, &gmap->crst_list);
Nico Boehr079f0c22022-10-20 16:31:55 +0200333 *table = __pa(new) | _REGION_ENTRY_LENGTH |
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100334 (*table & _REGION_ENTRY_TYPE_MASK);
335 page->index = gaddr;
336 page = NULL;
337 }
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100338 spin_unlock(&gmap->guest_table_lock);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100339 if (page)
Heiko Carstensf1c11742017-07-05 07:37:27 +0200340 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100341 return 0;
342}
343
344/**
345 * __gmap_segment_gaddr - find virtual address from segment pointer
346 * @entry: pointer to a segment table entry in the guest address space
347 *
348 * Returns the virtual address in the guest address space for the segment
349 */
350static unsigned long __gmap_segment_gaddr(unsigned long *entry)
351{
352 struct page *page;
Anshuman Khandual7e25de72022-11-25 09:15:02 +0530353 unsigned long offset;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100354
355 offset = (unsigned long) entry / sizeof(unsigned long);
356 offset = (offset & (PTRS_PER_PMD - 1)) * PMD_SIZE;
Anshuman Khandual7e25de72022-11-25 09:15:02 +0530357 page = pmd_pgtable_page((pmd_t *) entry);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100358 return page->index + offset;
359}
360
361/**
362 * __gmap_unlink_by_vmaddr - unlink a single segment via a host address
363 * @gmap: pointer to the guest address space structure
364 * @vmaddr: address in the host process address space
365 *
366 * Returns 1 if a TLB flush is required
367 */
368static int __gmap_unlink_by_vmaddr(struct gmap *gmap, unsigned long vmaddr)
369{
370 unsigned long *entry;
371 int flush = 0;
372
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100373 BUG_ON(gmap_is_shadow(gmap));
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100374 spin_lock(&gmap->guest_table_lock);
375 entry = radix_tree_delete(&gmap->host_to_guest, vmaddr >> PMD_SHIFT);
376 if (entry) {
Dominik Dingel54397bb2016-04-27 11:43:07 +0200377 flush = (*entry != _SEGMENT_ENTRY_EMPTY);
378 *entry = _SEGMENT_ENTRY_EMPTY;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100379 }
380 spin_unlock(&gmap->guest_table_lock);
381 return flush;
382}
383
384/**
385 * __gmap_unmap_by_gaddr - unmap a single segment via a guest address
386 * @gmap: pointer to the guest address space structure
387 * @gaddr: address in the guest address space
388 *
389 * Returns 1 if a TLB flush is required
390 */
391static int __gmap_unmap_by_gaddr(struct gmap *gmap, unsigned long gaddr)
392{
393 unsigned long vmaddr;
394
395 vmaddr = (unsigned long) radix_tree_delete(&gmap->guest_to_host,
396 gaddr >> PMD_SHIFT);
397 return vmaddr ? __gmap_unlink_by_vmaddr(gmap, vmaddr) : 0;
398}
399
400/**
401 * gmap_unmap_segment - unmap segment from the guest address space
402 * @gmap: pointer to the guest address space structure
403 * @to: address in the guest address space
404 * @len: length of the memory area to unmap
405 *
406 * Returns 0 if the unmap succeeded, -EINVAL if not.
407 */
408int gmap_unmap_segment(struct gmap *gmap, unsigned long to, unsigned long len)
409{
410 unsigned long off;
411 int flush;
412
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100413 BUG_ON(gmap_is_shadow(gmap));
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100414 if ((to | len) & (PMD_SIZE - 1))
415 return -EINVAL;
416 if (len == 0 || to + len < to)
417 return -EINVAL;
418
419 flush = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700420 mmap_write_lock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100421 for (off = 0; off < len; off += PMD_SIZE)
422 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700423 mmap_write_unlock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100424 if (flush)
425 gmap_flush_tlb(gmap);
426 return 0;
427}
428EXPORT_SYMBOL_GPL(gmap_unmap_segment);
429
430/**
431 * gmap_map_segment - map a segment to the guest address space
432 * @gmap: pointer to the guest address space structure
433 * @from: source address in the parent address space
434 * @to: target address in the guest address space
435 * @len: length of the memory area to map
436 *
437 * Returns 0 if the mmap succeeded, -EINVAL or -ENOMEM if not.
438 */
439int gmap_map_segment(struct gmap *gmap, unsigned long from,
440 unsigned long to, unsigned long len)
441{
442 unsigned long off;
443 int flush;
444
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100445 BUG_ON(gmap_is_shadow(gmap));
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100446 if ((from | to | len) & (PMD_SIZE - 1))
447 return -EINVAL;
448 if (len == 0 || from + len < from || to + len < to ||
Martin Schwidefskyee71d162017-04-20 14:43:51 +0200449 from + len - 1 > TASK_SIZE_MAX || to + len - 1 > gmap->asce_end)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100450 return -EINVAL;
451
452 flush = 0;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700453 mmap_write_lock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100454 for (off = 0; off < len; off += PMD_SIZE) {
455 /* Remove old translation */
456 flush |= __gmap_unmap_by_gaddr(gmap, to + off);
457 /* Store new translation */
458 if (radix_tree_insert(&gmap->guest_to_host,
459 (to + off) >> PMD_SHIFT,
460 (void *) from + off))
461 break;
462 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700463 mmap_write_unlock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100464 if (flush)
465 gmap_flush_tlb(gmap);
466 if (off >= len)
467 return 0;
468 gmap_unmap_segment(gmap, to, len);
469 return -ENOMEM;
470}
471EXPORT_SYMBOL_GPL(gmap_map_segment);
472
473/**
474 * __gmap_translate - translate a guest address to a user space address
475 * @gmap: pointer to guest mapping meta data structure
476 * @gaddr: guest address
477 *
478 * Returns user space address which corresponds to the guest address or
479 * -EFAULT if no such mapping exists.
480 * This function does not establish potentially missing page table entries.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700481 * The mmap_lock of the mm that belongs to the address space must be held
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100482 * when this function gets called.
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100483 *
484 * Note: Can also be called for shadow gmaps.
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100485 */
486unsigned long __gmap_translate(struct gmap *gmap, unsigned long gaddr)
487{
488 unsigned long vmaddr;
489
490 vmaddr = (unsigned long)
491 radix_tree_lookup(&gmap->guest_to_host, gaddr >> PMD_SHIFT);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100492 /* Note: guest_to_host is empty for a shadow gmap */
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100493 return vmaddr ? (vmaddr | (gaddr & ~PMD_MASK)) : -EFAULT;
494}
495EXPORT_SYMBOL_GPL(__gmap_translate);
496
497/**
498 * gmap_translate - translate a guest address to a user space address
499 * @gmap: pointer to guest mapping meta data structure
500 * @gaddr: guest address
501 *
502 * Returns user space address which corresponds to the guest address or
503 * -EFAULT if no such mapping exists.
504 * This function does not establish potentially missing page table entries.
505 */
506unsigned long gmap_translate(struct gmap *gmap, unsigned long gaddr)
507{
508 unsigned long rc;
509
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700510 mmap_read_lock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100511 rc = __gmap_translate(gmap, gaddr);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700512 mmap_read_unlock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100513 return rc;
514}
515EXPORT_SYMBOL_GPL(gmap_translate);
516
517/**
518 * gmap_unlink - disconnect a page table from the gmap shadow tables
Heiko Carstens2e827522021-09-02 19:47:37 +0200519 * @mm: pointer to the parent mm_struct
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100520 * @table: pointer to the host page table
521 * @vmaddr: vm address associated with the host page table
522 */
523void gmap_unlink(struct mm_struct *mm, unsigned long *table,
524 unsigned long vmaddr)
525{
526 struct gmap *gmap;
527 int flush;
528
Martin Schwidefsky8ecb1a52016-03-08 11:54:14 +0100529 rcu_read_lock();
530 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100531 flush = __gmap_unlink_by_vmaddr(gmap, vmaddr);
532 if (flush)
533 gmap_flush_tlb(gmap);
534 }
Martin Schwidefsky8ecb1a52016-03-08 11:54:14 +0100535 rcu_read_unlock();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100536}
537
Janosch Frank0959e162018-07-17 13:21:22 +0100538static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *old, pmd_t new,
539 unsigned long gaddr);
540
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100541/**
Heiko Carstens2e827522021-09-02 19:47:37 +0200542 * __gmap_link - set up shadow page tables to connect a host to a guest address
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100543 * @gmap: pointer to guest mapping meta data structure
544 * @gaddr: guest address
545 * @vmaddr: vm address
546 *
547 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
548 * if the vm address is already mapped to a different guest segment.
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700549 * The mmap_lock of the mm that belongs to the address space must be held
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100550 * when this function gets called.
551 */
552int __gmap_link(struct gmap *gmap, unsigned long gaddr, unsigned long vmaddr)
553{
554 struct mm_struct *mm;
555 unsigned long *table;
556 spinlock_t *ptl;
557 pgd_t *pgd;
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200558 p4d_t *p4d;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100559 pud_t *pud;
560 pmd_t *pmd;
Janosch Frank0959e162018-07-17 13:21:22 +0100561 u64 unprot;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100562 int rc;
563
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100564 BUG_ON(gmap_is_shadow(gmap));
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100565 /* Create higher level tables in the gmap page table */
566 table = gmap->table;
567 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION1) {
Heiko Carstensf1c11742017-07-05 07:37:27 +0200568 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100569 if ((*table & _REGION_ENTRY_INVALID) &&
570 gmap_alloc_table(gmap, table, _REGION2_ENTRY_EMPTY,
Heiko Carstensf1c11742017-07-05 07:37:27 +0200571 gaddr & _REGION1_MASK))
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100572 return -ENOMEM;
Nico Boehr079f0c22022-10-20 16:31:55 +0200573 table = __va(*table & _REGION_ENTRY_ORIGIN);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100574 }
575 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION2) {
Heiko Carstensf1c11742017-07-05 07:37:27 +0200576 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100577 if ((*table & _REGION_ENTRY_INVALID) &&
578 gmap_alloc_table(gmap, table, _REGION3_ENTRY_EMPTY,
Heiko Carstensf1c11742017-07-05 07:37:27 +0200579 gaddr & _REGION2_MASK))
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100580 return -ENOMEM;
Nico Boehr079f0c22022-10-20 16:31:55 +0200581 table = __va(*table & _REGION_ENTRY_ORIGIN);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100582 }
583 if ((gmap->asce & _ASCE_TYPE_MASK) >= _ASCE_TYPE_REGION3) {
Heiko Carstensf1c11742017-07-05 07:37:27 +0200584 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100585 if ((*table & _REGION_ENTRY_INVALID) &&
586 gmap_alloc_table(gmap, table, _SEGMENT_ENTRY_EMPTY,
Heiko Carstensf1c11742017-07-05 07:37:27 +0200587 gaddr & _REGION3_MASK))
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100588 return -ENOMEM;
Nico Boehr079f0c22022-10-20 16:31:55 +0200589 table = __va(*table & _REGION_ENTRY_ORIGIN);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100590 }
Heiko Carstensf1c11742017-07-05 07:37:27 +0200591 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100592 /* Walk the parent mm page table */
593 mm = gmap->mm;
594 pgd = pgd_offset(mm, vmaddr);
595 VM_BUG_ON(pgd_none(*pgd));
Martin Schwidefsky1aea9b32017-04-24 18:19:10 +0200596 p4d = p4d_offset(pgd, vmaddr);
597 VM_BUG_ON(p4d_none(*p4d));
598 pud = pud_offset(p4d, vmaddr);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100599 VM_BUG_ON(pud_none(*pud));
Gerald Schaeferd08de8e2016-07-04 14:47:01 +0200600 /* large puds cannot yet be handled */
Peter Xu0a845e02024-03-05 12:37:48 +0800601 if (pud_leaf(*pud))
Gerald Schaeferd08de8e2016-07-04 14:47:01 +0200602 return -EFAULT;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100603 pmd = pmd_offset(pud, vmaddr);
604 VM_BUG_ON(pmd_none(*pmd));
Janosch Franka9e00d82018-07-13 11:28:37 +0100605 /* Are we allowed to use huge pages? */
Peter Xu2f709f72024-03-05 12:37:47 +0800606 if (pmd_leaf(*pmd) && !gmap->mm->context.allow_gmap_hpage_1m)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100607 return -EFAULT;
608 /* Link gmap segment table entry location to page table. */
Christian Borntraeger0cd2a782020-11-09 13:14:35 +0100609 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100610 if (rc)
611 return rc;
612 ptl = pmd_lock(mm, pmd);
613 spin_lock(&gmap->guest_table_lock);
Dominik Dingel54397bb2016-04-27 11:43:07 +0200614 if (*table == _SEGMENT_ENTRY_EMPTY) {
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100615 rc = radix_tree_insert(&gmap->host_to_guest,
616 vmaddr >> PMD_SHIFT, table);
Janosch Frank58b7e202018-07-13 11:28:20 +0100617 if (!rc) {
Peter Xu2f709f72024-03-05 12:37:47 +0800618 if (pmd_leaf(*pmd)) {
Janosch Frank0959e162018-07-17 13:21:22 +0100619 *table = (pmd_val(*pmd) &
620 _SEGMENT_ENTRY_HARDWARE_BITS_LARGE)
621 | _SEGMENT_ENTRY_GMAP_UC;
Janosch Frank58b7e202018-07-13 11:28:20 +0100622 } else
623 *table = pmd_val(*pmd) &
624 _SEGMENT_ENTRY_HARDWARE_BITS;
625 }
Janosch Frank0959e162018-07-17 13:21:22 +0100626 } else if (*table & _SEGMENT_ENTRY_PROTECT &&
627 !(pmd_val(*pmd) & _SEGMENT_ENTRY_PROTECT)) {
628 unprot = (u64)*table;
629 unprot &= ~_SEGMENT_ENTRY_PROTECT;
630 unprot |= _SEGMENT_ENTRY_GMAP_UC;
631 gmap_pmdp_xchg(gmap, (pmd_t *)table, __pmd(unprot), gaddr);
Janosch Frank58b7e202018-07-13 11:28:20 +0100632 }
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100633 spin_unlock(&gmap->guest_table_lock);
634 spin_unlock(ptl);
635 radix_tree_preload_end();
636 return rc;
637}
638
639/**
640 * gmap_fault - resolve a fault on a guest address
641 * @gmap: pointer to guest mapping meta data structure
642 * @gaddr: guest address
643 * @fault_flags: flags to pass down to handle_mm_fault()
644 *
645 * Returns 0 on success, -ENOMEM for out of memory conditions, and -EFAULT
646 * if the vm address is already mapped to a different guest segment.
647 */
648int gmap_fault(struct gmap *gmap, unsigned long gaddr,
649 unsigned int fault_flags)
650{
651 unsigned long vmaddr;
652 int rc;
653 bool unlocked;
654
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700655 mmap_read_lock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100656
657retry:
658 unlocked = false;
659 vmaddr = __gmap_translate(gmap, gaddr);
660 if (IS_ERR_VALUE(vmaddr)) {
661 rc = vmaddr;
662 goto out_up;
663 }
Peter Xu64019a22020-08-11 18:39:01 -0700664 if (fixup_user_fault(gmap->mm, vmaddr, fault_flags,
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100665 &unlocked)) {
666 rc = -EFAULT;
667 goto out_up;
668 }
669 /*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700670 * In the case that fixup_user_fault unlocked the mmap_lock during
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100671 * faultin redo __gmap_translate to not race with a map/unmap_segment.
672 */
673 if (unlocked)
674 goto retry;
675
676 rc = __gmap_link(gmap, gaddr, vmaddr);
677out_up:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700678 mmap_read_unlock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100679 return rc;
680}
681EXPORT_SYMBOL_GPL(gmap_fault);
682
683/*
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700684 * this function is assumed to be called with mmap_lock held
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100685 */
686void __gmap_zap(struct gmap *gmap, unsigned long gaddr)
687{
David Hildenbrand2d8fb8f2021-09-09 18:22:40 +0200688 struct vm_area_struct *vma;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100689 unsigned long vmaddr;
690 spinlock_t *ptl;
691 pte_t *ptep;
692
693 /* Find the vm address for the guest address */
694 vmaddr = (unsigned long) radix_tree_lookup(&gmap->guest_to_host,
695 gaddr >> PMD_SHIFT);
696 if (vmaddr) {
697 vmaddr |= gaddr & ~PMD_MASK;
David Hildenbrand2d8fb8f2021-09-09 18:22:40 +0200698
699 vma = vma_lookup(gmap->mm, vmaddr);
700 if (!vma || is_vm_hugetlb_page(vma))
701 return;
702
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100703 /* Get pointer to the page table entry */
704 ptep = get_locked_pte(gmap->mm, vmaddr, &ptl);
David Hildenbrandb159f942021-09-09 18:22:41 +0200705 if (likely(ptep)) {
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100706 ptep_zap_unused(gmap->mm, vmaddr, ptep, 0);
David Hildenbrandb159f942021-09-09 18:22:41 +0200707 pte_unmap_unlock(ptep, ptl);
708 }
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100709 }
710}
711EXPORT_SYMBOL_GPL(__gmap_zap);
712
713void gmap_discard(struct gmap *gmap, unsigned long from, unsigned long to)
714{
715 unsigned long gaddr, vmaddr, size;
716 struct vm_area_struct *vma;
717
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700718 mmap_read_lock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100719 for (gaddr = from; gaddr < to;
720 gaddr = (gaddr + PMD_SIZE) & PMD_MASK) {
721 /* Find the vm address for the guest address */
722 vmaddr = (unsigned long)
723 radix_tree_lookup(&gmap->guest_to_host,
724 gaddr >> PMD_SHIFT);
725 if (!vmaddr)
726 continue;
727 vmaddr |= gaddr & ~PMD_MASK;
728 /* Find vma in the parent mm */
729 vma = find_vma(gmap->mm, vmaddr);
Janosch Frank1843abd2018-08-16 09:02:31 +0100730 if (!vma)
731 continue;
Dominik Dingel7d735b92018-07-13 11:28:29 +0100732 /*
733 * We do not discard pages that are backed by
734 * hugetlbfs, so we don't have to refault them.
735 */
Janosch Frank1843abd2018-08-16 09:02:31 +0100736 if (is_vm_hugetlb_page(vma))
Dominik Dingel7d735b92018-07-13 11:28:29 +0100737 continue;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100738 size = min(to - gaddr, PMD_SIZE - (gaddr & ~PMD_MASK));
Mike Kravetze9adcfe2023-01-03 16:27:32 -0800739 zap_page_range_single(vma, vmaddr, size, NULL);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100740 }
Michel Lespinassed8ed45c2020-06-08 21:33:25 -0700741 mmap_read_unlock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100742}
743EXPORT_SYMBOL_GPL(gmap_discard);
744
745static LIST_HEAD(gmap_notifier_list);
746static DEFINE_SPINLOCK(gmap_notifier_lock);
747
748/**
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100749 * gmap_register_pte_notifier - register a pte invalidation callback
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100750 * @nb: pointer to the gmap notifier block
751 */
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100752void gmap_register_pte_notifier(struct gmap_notifier *nb)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100753{
754 spin_lock(&gmap_notifier_lock);
Martin Schwidefsky8ecb1a52016-03-08 11:54:14 +0100755 list_add_rcu(&nb->list, &gmap_notifier_list);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100756 spin_unlock(&gmap_notifier_lock);
757}
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100758EXPORT_SYMBOL_GPL(gmap_register_pte_notifier);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100759
760/**
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100761 * gmap_unregister_pte_notifier - remove a pte invalidation callback
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100762 * @nb: pointer to the gmap notifier block
763 */
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100764void gmap_unregister_pte_notifier(struct gmap_notifier *nb)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100765{
766 spin_lock(&gmap_notifier_lock);
Martin Schwidefsky8ecb1a52016-03-08 11:54:14 +0100767 list_del_rcu(&nb->list);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100768 spin_unlock(&gmap_notifier_lock);
Martin Schwidefsky8ecb1a52016-03-08 11:54:14 +0100769 synchronize_rcu();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100770}
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100771EXPORT_SYMBOL_GPL(gmap_unregister_pte_notifier);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +0100772
773/**
Martin Schwidefsky414d3b02016-03-08 11:52:54 +0100774 * gmap_call_notifier - call all registered invalidation callbacks
775 * @gmap: pointer to guest mapping meta data structure
776 * @start: start virtual address in the guest address space
777 * @end: end virtual address in the guest address space
778 */
779static void gmap_call_notifier(struct gmap *gmap, unsigned long start,
780 unsigned long end)
781{
782 struct gmap_notifier *nb;
783
784 list_for_each_entry(nb, &gmap_notifier_list, list)
785 nb->notifier_call(gmap, start, end);
786}
787
788/**
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100789 * gmap_table_walk - walk the gmap page tables
790 * @gmap: pointer to guest mapping meta data structure
791 * @gaddr: virtual address in the guest address space
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100792 * @level: page table level to stop at
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100793 *
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100794 * Returns a table entry pointer for the given guest address and @level
795 * @level=0 : returns a pointer to a page table table entry (or NULL)
796 * @level=1 : returns a pointer to a segment table entry (or NULL)
797 * @level=2 : returns a pointer to a region-3 table entry (or NULL)
798 * @level=3 : returns a pointer to a region-2 table entry (or NULL)
799 * @level=4 : returns a pointer to a region-1 table entry (or NULL)
800 *
801 * Returns NULL if the gmap page tables could not be walked to the
802 * requested level.
803 *
804 * Note: Can also be called for shadow gmaps.
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100805 */
806static inline unsigned long *gmap_table_walk(struct gmap *gmap,
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100807 unsigned long gaddr, int level)
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100808{
David Hildenbranda1d032a2020-04-03 17:30:46 +0200809 const int asce_type = gmap->asce & _ASCE_TYPE_MASK;
David Hildenbrand62cf6662020-04-03 17:30:50 +0200810 unsigned long *table = gmap->table;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100811
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100812 if (gmap_is_shadow(gmap) && gmap->removed)
813 return NULL;
David Hildenbranda1d032a2020-04-03 17:30:46 +0200814
David Hildenbrand62cf6662020-04-03 17:30:50 +0200815 if (WARN_ON_ONCE(level > (asce_type >> 2) + 1))
816 return NULL;
817
David Hildenbranda1d032a2020-04-03 17:30:46 +0200818 if (asce_type != _ASCE_TYPE_REGION1 &&
819 gaddr & (-1UL << (31 + (asce_type >> 2) * 11)))
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100820 return NULL;
David Hildenbranda1d032a2020-04-03 17:30:46 +0200821
David Hildenbrand62cf6662020-04-03 17:30:50 +0200822 switch (asce_type) {
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100823 case _ASCE_TYPE_REGION1:
Heiko Carstensf1c11742017-07-05 07:37:27 +0200824 table += (gaddr & _REGION1_INDEX) >> _REGION1_SHIFT;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100825 if (level == 4)
826 break;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100827 if (*table & _REGION_ENTRY_INVALID)
828 return NULL;
Nico Boehr079f0c22022-10-20 16:31:55 +0200829 table = __va(*table & _REGION_ENTRY_ORIGIN);
Joe Perches3b684a42020-03-10 21:51:32 -0700830 fallthrough;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100831 case _ASCE_TYPE_REGION2:
Heiko Carstensf1c11742017-07-05 07:37:27 +0200832 table += (gaddr & _REGION2_INDEX) >> _REGION2_SHIFT;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100833 if (level == 3)
834 break;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100835 if (*table & _REGION_ENTRY_INVALID)
836 return NULL;
Nico Boehr079f0c22022-10-20 16:31:55 +0200837 table = __va(*table & _REGION_ENTRY_ORIGIN);
Joe Perches3b684a42020-03-10 21:51:32 -0700838 fallthrough;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100839 case _ASCE_TYPE_REGION3:
Heiko Carstensf1c11742017-07-05 07:37:27 +0200840 table += (gaddr & _REGION3_INDEX) >> _REGION3_SHIFT;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100841 if (level == 2)
842 break;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100843 if (*table & _REGION_ENTRY_INVALID)
844 return NULL;
Nico Boehr079f0c22022-10-20 16:31:55 +0200845 table = __va(*table & _REGION_ENTRY_ORIGIN);
Joe Perches3b684a42020-03-10 21:51:32 -0700846 fallthrough;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100847 case _ASCE_TYPE_SEGMENT:
Heiko Carstensf1c11742017-07-05 07:37:27 +0200848 table += (gaddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100849 if (level == 1)
850 break;
851 if (*table & _REGION_ENTRY_INVALID)
852 return NULL;
Nico Boehr079f0c22022-10-20 16:31:55 +0200853 table = __va(*table & _SEGMENT_ENTRY_ORIGIN);
Heiko Carstensf1c11742017-07-05 07:37:27 +0200854 table += (gaddr & _PAGE_INDEX) >> _PAGE_SHIFT;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100855 }
856 return table;
857}
858
859/**
860 * gmap_pte_op_walk - walk the gmap page table, get the page table lock
861 * and return the pte pointer
862 * @gmap: pointer to guest mapping meta data structure
863 * @gaddr: virtual address in the guest address space
864 * @ptl: pointer to the spinlock pointer
865 *
866 * Returns a pointer to the locked pte for a guest address, or NULL
867 */
868static pte_t *gmap_pte_op_walk(struct gmap *gmap, unsigned long gaddr,
869 spinlock_t **ptl)
870{
871 unsigned long *table;
872
David Hildenbrand96965942017-11-10 16:18:05 +0100873 BUG_ON(gmap_is_shadow(gmap));
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100874 /* Walk the gmap page table, lock and get pte pointer */
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100875 table = gmap_table_walk(gmap, gaddr, 1); /* get segment pointer */
David Hildenbrand96965942017-11-10 16:18:05 +0100876 if (!table || *table & _SEGMENT_ENTRY_INVALID)
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100877 return NULL;
878 return pte_alloc_map_lock(gmap->mm, (pmd_t *) table, gaddr, ptl);
879}
880
881/**
882 * gmap_pte_op_fixup - force a page in and connect the gmap page table
883 * @gmap: pointer to guest mapping meta data structure
884 * @gaddr: virtual address in the guest address space
885 * @vmaddr: address in the host process address space
David Hildenbrand01f719172016-06-13 10:49:04 +0200886 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100887 *
888 * Returns 0 if the caller can retry __gmap_translate (might fail again),
889 * -ENOMEM if out of memory and -EFAULT if anything goes wrong while fixing
890 * up or connecting the gmap page table.
891 */
892static int gmap_pte_op_fixup(struct gmap *gmap, unsigned long gaddr,
David Hildenbrand01f719172016-06-13 10:49:04 +0200893 unsigned long vmaddr, int prot)
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100894{
895 struct mm_struct *mm = gmap->mm;
David Hildenbrand01f719172016-06-13 10:49:04 +0200896 unsigned int fault_flags;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100897 bool unlocked = false;
898
Martin Schwidefsky4be130a2016-03-08 12:12:18 +0100899 BUG_ON(gmap_is_shadow(gmap));
David Hildenbrand01f719172016-06-13 10:49:04 +0200900 fault_flags = (prot == PROT_WRITE) ? FAULT_FLAG_WRITE : 0;
Peter Xu64019a22020-08-11 18:39:01 -0700901 if (fixup_user_fault(mm, vmaddr, fault_flags, &unlocked))
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100902 return -EFAULT;
903 if (unlocked)
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700904 /* lost mmap_lock, caller has to retry __gmap_translate */
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100905 return 0;
906 /* Connect the page tables */
907 return __gmap_link(gmap, gaddr, vmaddr);
908}
909
910/**
911 * gmap_pte_op_end - release the page table lock
Hugh Dickinsb2f58942023-06-08 12:29:13 -0700912 * @ptep: pointer to the locked pte
913 * @ptl: pointer to the page table spinlock
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100914 */
Hugh Dickinsb2f58942023-06-08 12:29:13 -0700915static void gmap_pte_op_end(pte_t *ptep, spinlock_t *ptl)
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +0100916{
Hugh Dickinsb2f58942023-06-08 12:29:13 -0700917 pte_unmap_unlock(ptep, ptl);
Janosch Frank5a045bb2018-07-13 11:28:16 +0100918}
919
920/**
921 * gmap_pmd_op_walk - walk the gmap tables, get the guest table lock
922 * and return the pmd pointer
923 * @gmap: pointer to guest mapping meta data structure
924 * @gaddr: virtual address in the guest address space
925 *
926 * Returns a pointer to the pmd for a guest address, or NULL
927 */
928static inline pmd_t *gmap_pmd_op_walk(struct gmap *gmap, unsigned long gaddr)
929{
930 pmd_t *pmdp;
931
932 BUG_ON(gmap_is_shadow(gmap));
Janosch Frank5a045bb2018-07-13 11:28:16 +0100933 pmdp = (pmd_t *) gmap_table_walk(gmap, gaddr, 1);
David Hildenbrandaf4bf6c2018-08-06 17:54:07 +0200934 if (!pmdp)
935 return NULL;
Janosch Frank5a045bb2018-07-13 11:28:16 +0100936
David Hildenbrandaf4bf6c2018-08-06 17:54:07 +0200937 /* without huge pages, there is no need to take the table lock */
938 if (!gmap->mm->context.allow_gmap_hpage_1m)
939 return pmd_none(*pmdp) ? NULL : pmdp;
940
941 spin_lock(&gmap->guest_table_lock);
942 if (pmd_none(*pmdp)) {
Janosch Frank5a045bb2018-07-13 11:28:16 +0100943 spin_unlock(&gmap->guest_table_lock);
944 return NULL;
945 }
946
947 /* 4k page table entries are locked via the pte (pte_alloc_map_lock). */
Peter Xu2f709f72024-03-05 12:37:47 +0800948 if (!pmd_leaf(*pmdp))
Janosch Frank5a045bb2018-07-13 11:28:16 +0100949 spin_unlock(&gmap->guest_table_lock);
950 return pmdp;
951}
952
953/**
954 * gmap_pmd_op_end - release the guest_table_lock if needed
955 * @gmap: pointer to the guest mapping meta data structure
956 * @pmdp: pointer to the pmd
957 */
958static inline void gmap_pmd_op_end(struct gmap *gmap, pmd_t *pmdp)
959{
Peter Xu2f709f72024-03-05 12:37:47 +0800960 if (pmd_leaf(*pmdp))
Janosch Frank5a045bb2018-07-13 11:28:16 +0100961 spin_unlock(&gmap->guest_table_lock);
962}
963
964/*
Janosch Frank7c4b13a2018-07-13 11:28:21 +0100965 * gmap_protect_pmd - remove access rights to memory and set pmd notification bits
966 * @pmdp: pointer to the pmd to be protected
967 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
968 * @bits: notification bits to set
969 *
970 * Returns:
971 * 0 if successfully protected
972 * -EAGAIN if a fixup is needed
973 * -EINVAL if unsupported notifier bits have been specified
974 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700975 * Expected to be called with sg->mm->mmap_lock in read and
Janosch Frank7c4b13a2018-07-13 11:28:21 +0100976 * guest_table_lock held.
977 */
978static int gmap_protect_pmd(struct gmap *gmap, unsigned long gaddr,
979 pmd_t *pmdp, int prot, unsigned long bits)
980{
981 int pmd_i = pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID;
982 int pmd_p = pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT;
Janosch Frank0959e162018-07-17 13:21:22 +0100983 pmd_t new = *pmdp;
Janosch Frank7c4b13a2018-07-13 11:28:21 +0100984
985 /* Fixup needed */
986 if ((pmd_i && (prot != PROT_NONE)) || (pmd_p && (prot == PROT_WRITE)))
987 return -EAGAIN;
988
Janosch Frank0959e162018-07-17 13:21:22 +0100989 if (prot == PROT_NONE && !pmd_i) {
Heiko Carstense1fc74f2022-02-21 21:25:09 +0100990 new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
Janosch Frank0959e162018-07-17 13:21:22 +0100991 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
992 }
993
994 if (prot == PROT_READ && !pmd_p) {
Heiko Carstense1fc74f2022-02-21 21:25:09 +0100995 new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_INVALID));
996 new = set_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_PROTECT));
Janosch Frank0959e162018-07-17 13:21:22 +0100997 gmap_pmdp_xchg(gmap, pmdp, new, gaddr);
998 }
999
Janosch Frank7c4b13a2018-07-13 11:28:21 +01001000 if (bits & GMAP_NOTIFY_MPROT)
Heiko Carstensb8e3b372022-02-21 20:50:07 +01001001 set_pmd(pmdp, set_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
Janosch Frank7c4b13a2018-07-13 11:28:21 +01001002
1003 /* Shadow GMAP protection needs split PMDs */
1004 if (bits & GMAP_NOTIFY_SHADOW)
1005 return -EINVAL;
1006
1007 return 0;
1008}
1009
1010/*
Janosch Frank5a045bb2018-07-13 11:28:16 +01001011 * gmap_protect_pte - remove access rights to memory and set pgste bits
1012 * @gmap: pointer to guest mapping meta data structure
1013 * @gaddr: virtual address in the guest address space
1014 * @pmdp: pointer to the pmd associated with the pte
1015 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
Janosch Frank2c46e972018-07-13 11:28:18 +01001016 * @bits: notification bits to set
Janosch Frank5a045bb2018-07-13 11:28:16 +01001017 *
1018 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1019 * -EAGAIN if a fixup is needed.
1020 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001021 * Expected to be called with sg->mm->mmap_lock in read
Janosch Frank5a045bb2018-07-13 11:28:16 +01001022 */
1023static int gmap_protect_pte(struct gmap *gmap, unsigned long gaddr,
1024 pmd_t *pmdp, int prot, unsigned long bits)
1025{
1026 int rc;
1027 pte_t *ptep;
Hugh Dickinsb2f58942023-06-08 12:29:13 -07001028 spinlock_t *ptl;
Janosch Frank2c46e972018-07-13 11:28:18 +01001029 unsigned long pbits = 0;
Janosch Frank5a045bb2018-07-13 11:28:16 +01001030
1031 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
1032 return -EAGAIN;
1033
1034 ptep = pte_alloc_map_lock(gmap->mm, pmdp, gaddr, &ptl);
1035 if (!ptep)
1036 return -ENOMEM;
1037
Janosch Frank2c46e972018-07-13 11:28:18 +01001038 pbits |= (bits & GMAP_NOTIFY_MPROT) ? PGSTE_IN_BIT : 0;
1039 pbits |= (bits & GMAP_NOTIFY_SHADOW) ? PGSTE_VSIE_BIT : 0;
Janosch Frank5a045bb2018-07-13 11:28:16 +01001040 /* Protect and unlock. */
Janosch Frank2c46e972018-07-13 11:28:18 +01001041 rc = ptep_force_prot(gmap->mm, gaddr, ptep, prot, pbits);
Hugh Dickinsb2f58942023-06-08 12:29:13 -07001042 gmap_pte_op_end(ptep, ptl);
Janosch Frank5a045bb2018-07-13 11:28:16 +01001043 return rc;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +01001044}
1045
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001046/*
1047 * gmap_protect_range - remove access rights to memory and set pgste bits
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001048 * @gmap: pointer to guest mapping meta data structure
1049 * @gaddr: virtual address in the guest address space
1050 * @len: size of area
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001051 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
1052 * @bits: pgste notification bits to set
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001053 *
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001054 * Returns 0 if successfully protected, -ENOMEM if out of memory and
1055 * -EFAULT if gaddr is invalid (or mapping for shadows is missing).
1056 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001057 * Called with sg->mm->mmap_lock in read.
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001058 */
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001059static int gmap_protect_range(struct gmap *gmap, unsigned long gaddr,
1060 unsigned long len, int prot, unsigned long bits)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001061{
Janosch Frank7c4b13a2018-07-13 11:28:21 +01001062 unsigned long vmaddr, dist;
Janosch Frank5a045bb2018-07-13 11:28:16 +01001063 pmd_t *pmdp;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001064 int rc;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001065
David Hildenbrand96965942017-11-10 16:18:05 +01001066 BUG_ON(gmap_is_shadow(gmap));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001067 while (len) {
1068 rc = -EAGAIN;
Janosch Frank5a045bb2018-07-13 11:28:16 +01001069 pmdp = gmap_pmd_op_walk(gmap, gaddr);
1070 if (pmdp) {
Peter Xu2f709f72024-03-05 12:37:47 +08001071 if (!pmd_leaf(*pmdp)) {
Janosch Frank7c4b13a2018-07-13 11:28:21 +01001072 rc = gmap_protect_pte(gmap, gaddr, pmdp, prot,
1073 bits);
1074 if (!rc) {
1075 len -= PAGE_SIZE;
1076 gaddr += PAGE_SIZE;
1077 }
1078 } else {
1079 rc = gmap_protect_pmd(gmap, gaddr, pmdp, prot,
1080 bits);
1081 if (!rc) {
1082 dist = HPAGE_SIZE - (gaddr & ~HPAGE_MASK);
1083 len = len < dist ? 0 : len - dist;
1084 gaddr = (gaddr & HPAGE_MASK) + HPAGE_SIZE;
1085 }
Janosch Frank5a045bb2018-07-13 11:28:16 +01001086 }
1087 gmap_pmd_op_end(gmap, pmdp);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001088 }
1089 if (rc) {
Janosch Frank7c4b13a2018-07-13 11:28:21 +01001090 if (rc == -EINVAL)
1091 return rc;
1092
1093 /* -EAGAIN, fixup of userspace mm and gmap */
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001094 vmaddr = __gmap_translate(gmap, gaddr);
1095 if (IS_ERR_VALUE(vmaddr))
1096 return vmaddr;
David Hildenbrand01f719172016-06-13 10:49:04 +02001097 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, prot);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001098 if (rc)
1099 return rc;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001100 }
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001101 }
1102 return 0;
1103}
1104
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +01001105/**
1106 * gmap_mprotect_notify - change access rights for a range of ptes and
1107 * call the notifier if any pte changes again
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001108 * @gmap: pointer to guest mapping meta data structure
1109 * @gaddr: virtual address in the guest address space
1110 * @len: size of area
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +01001111 * @prot: indicates access rights: PROT_NONE, PROT_READ or PROT_WRITE
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001112 *
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +01001113 * Returns 0 if for each page in the given range a gmap mapping exists,
1114 * the new access rights could be set and the notifier could be armed.
1115 * If the gmap mapping is missing for one or more pages -EFAULT is
1116 * returned. If no memory could be allocated -ENOMEM is returned.
1117 * This function establishes missing page table entries.
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001118 */
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +01001119int gmap_mprotect_notify(struct gmap *gmap, unsigned long gaddr,
1120 unsigned long len, int prot)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001121{
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001122 int rc;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001123
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001124 if ((gaddr & ~PAGE_MASK) || (len & ~PAGE_MASK) || gmap_is_shadow(gmap))
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001125 return -EINVAL;
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +01001126 if (!MACHINE_HAS_ESOP && prot == PROT_READ)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001127 return -EINVAL;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001128 mmap_read_lock(gmap->mm);
Janosch Frank2c46e972018-07-13 11:28:18 +01001129 rc = gmap_protect_range(gmap, gaddr, len, prot, GMAP_NOTIFY_MPROT);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001130 mmap_read_unlock(gmap->mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001131 return rc;
1132}
Martin Schwidefskyb2d73b22016-03-08 11:54:42 +01001133EXPORT_SYMBOL_GPL(gmap_mprotect_notify);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01001134
1135/**
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001136 * gmap_read_table - get an unsigned long value from a guest page table using
1137 * absolute addressing, without marking the page referenced.
1138 * @gmap: pointer to guest mapping meta data structure
1139 * @gaddr: virtual address in the guest address space
1140 * @val: pointer to the unsigned long value to return
1141 *
1142 * Returns 0 if the value was read, -ENOMEM if out of memory and -EFAULT
David Hildenbrand96965942017-11-10 16:18:05 +01001143 * if reading using the virtual address failed. -EINVAL if called on a gmap
1144 * shadow.
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001145 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001146 * Called with gmap->mm->mmap_lock in read.
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001147 */
1148int gmap_read_table(struct gmap *gmap, unsigned long gaddr, unsigned long *val)
1149{
1150 unsigned long address, vmaddr;
1151 spinlock_t *ptl;
1152 pte_t *ptep, pte;
1153 int rc;
1154
David Hildenbrand96965942017-11-10 16:18:05 +01001155 if (gmap_is_shadow(gmap))
1156 return -EINVAL;
1157
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001158 while (1) {
1159 rc = -EAGAIN;
1160 ptep = gmap_pte_op_walk(gmap, gaddr, &ptl);
1161 if (ptep) {
1162 pte = *ptep;
1163 if (pte_present(pte) && (pte_val(pte) & _PAGE_READ)) {
1164 address = pte_val(pte) & PAGE_MASK;
1165 address += gaddr & ~PAGE_MASK;
Nico Boehr079f0c22022-10-20 16:31:55 +02001166 *val = *(unsigned long *)__va(address);
Heiko Carstensb8e3b372022-02-21 20:50:07 +01001167 set_pte(ptep, set_pte_bit(*ptep, __pgprot(_PAGE_YOUNG)));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001168 /* Do *NOT* clear the _PAGE_INVALID bit! */
1169 rc = 0;
1170 }
Hugh Dickinsb2f58942023-06-08 12:29:13 -07001171 gmap_pte_op_end(ptep, ptl);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001172 }
1173 if (!rc)
1174 break;
1175 vmaddr = __gmap_translate(gmap, gaddr);
1176 if (IS_ERR_VALUE(vmaddr)) {
1177 rc = vmaddr;
1178 break;
1179 }
David Hildenbrand01f719172016-06-13 10:49:04 +02001180 rc = gmap_pte_op_fixup(gmap, gaddr, vmaddr, PROT_READ);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001181 if (rc)
1182 break;
1183 }
1184 return rc;
1185}
1186EXPORT_SYMBOL_GPL(gmap_read_table);
1187
1188/**
1189 * gmap_insert_rmap - add a rmap to the host_to_rmap radix tree
1190 * @sg: pointer to the shadow guest address space structure
1191 * @vmaddr: vm address associated with the rmap
1192 * @rmap: pointer to the rmap structure
1193 *
1194 * Called with the sg->guest_table_lock
1195 */
1196static inline void gmap_insert_rmap(struct gmap *sg, unsigned long vmaddr,
1197 struct gmap_rmap *rmap)
1198{
Christian Borntraegera06afe82022-04-29 17:15:26 +02001199 struct gmap_rmap *temp;
Heiko Carstensd12a3d62017-05-09 13:44:43 +02001200 void __rcu **slot;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001201
1202 BUG_ON(!gmap_is_shadow(sg));
1203 slot = radix_tree_lookup_slot(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
1204 if (slot) {
1205 rmap->next = radix_tree_deref_slot_protected(slot,
1206 &sg->guest_table_lock);
Christian Borntraegera06afe82022-04-29 17:15:26 +02001207 for (temp = rmap->next; temp; temp = temp->next) {
1208 if (temp->raddr == rmap->raddr) {
1209 kfree(rmap);
1210 return;
1211 }
1212 }
Johannes Weiner6d75f362016-12-12 16:43:43 -08001213 radix_tree_replace_slot(&sg->host_to_rmap, slot, rmap);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001214 } else {
1215 rmap->next = NULL;
1216 radix_tree_insert(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT,
1217 rmap);
1218 }
1219}
1220
1221/**
David Hildenbrand5c528db2018-01-23 22:26:18 +01001222 * gmap_protect_rmap - restrict access rights to memory (RO) and create an rmap
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001223 * @sg: pointer to the shadow guest address space structure
1224 * @raddr: rmap address in the shadow gmap
1225 * @paddr: address in the parent guest address space
1226 * @len: length of the memory area to protect
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001227 *
1228 * Returns 0 if successfully protected and the rmap was created, -ENOMEM
1229 * if out of memory and -EFAULT if paddr is invalid.
1230 */
1231static int gmap_protect_rmap(struct gmap *sg, unsigned long raddr,
David Hildenbrand5c528db2018-01-23 22:26:18 +01001232 unsigned long paddr, unsigned long len)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001233{
1234 struct gmap *parent;
1235 struct gmap_rmap *rmap;
1236 unsigned long vmaddr;
1237 spinlock_t *ptl;
1238 pte_t *ptep;
1239 int rc;
1240
1241 BUG_ON(!gmap_is_shadow(sg));
1242 parent = sg->parent;
1243 while (len) {
1244 vmaddr = __gmap_translate(parent, paddr);
1245 if (IS_ERR_VALUE(vmaddr))
1246 return vmaddr;
Christian Borntraeger0cd2a782020-11-09 13:14:35 +01001247 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001248 if (!rmap)
1249 return -ENOMEM;
1250 rmap->raddr = raddr;
Christian Borntraeger0cd2a782020-11-09 13:14:35 +01001251 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001252 if (rc) {
1253 kfree(rmap);
1254 return rc;
1255 }
1256 rc = -EAGAIN;
1257 ptep = gmap_pte_op_walk(parent, paddr, &ptl);
1258 if (ptep) {
1259 spin_lock(&sg->guest_table_lock);
David Hildenbrand5c528db2018-01-23 22:26:18 +01001260 rc = ptep_force_prot(parent->mm, paddr, ptep, PROT_READ,
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001261 PGSTE_VSIE_BIT);
1262 if (!rc)
1263 gmap_insert_rmap(sg, vmaddr, rmap);
1264 spin_unlock(&sg->guest_table_lock);
Hugh Dickinsb2f58942023-06-08 12:29:13 -07001265 gmap_pte_op_end(ptep, ptl);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001266 }
1267 radix_tree_preload_end();
1268 if (rc) {
1269 kfree(rmap);
David Hildenbrand5c528db2018-01-23 22:26:18 +01001270 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, PROT_READ);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001271 if (rc)
1272 return rc;
1273 continue;
1274 }
1275 paddr += PAGE_SIZE;
1276 len -= PAGE_SIZE;
1277 }
1278 return 0;
1279}
1280
1281#define _SHADOW_RMAP_MASK 0x7
1282#define _SHADOW_RMAP_REGION1 0x5
1283#define _SHADOW_RMAP_REGION2 0x4
1284#define _SHADOW_RMAP_REGION3 0x3
1285#define _SHADOW_RMAP_SEGMENT 0x2
1286#define _SHADOW_RMAP_PGTABLE 0x1
1287
1288/**
1289 * gmap_idte_one - invalidate a single region or segment table entry
1290 * @asce: region or segment table *origin* + table-type bits
1291 * @vaddr: virtual address to identify the table entry to flush
1292 *
1293 * The invalid bit of a single region or segment table entry is set
1294 * and the associated TLB entries depending on the entry are flushed.
1295 * The table-type of the @asce identifies the portion of the @vaddr
1296 * that is used as the invalidation index.
1297 */
1298static inline void gmap_idte_one(unsigned long asce, unsigned long vaddr)
1299{
1300 asm volatile(
Vasily Gorbik731efc92022-02-25 10:39:02 +01001301 " idte %0,0,%1"
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001302 : : "a" (asce), "a" (vaddr) : "cc", "memory");
1303}
1304
1305/**
1306 * gmap_unshadow_page - remove a page from a shadow page table
1307 * @sg: pointer to the shadow guest address space structure
1308 * @raddr: rmap address in the shadow guest address space
1309 *
1310 * Called with the sg->guest_table_lock
1311 */
1312static void gmap_unshadow_page(struct gmap *sg, unsigned long raddr)
1313{
1314 unsigned long *table;
1315
1316 BUG_ON(!gmap_is_shadow(sg));
1317 table = gmap_table_walk(sg, raddr, 0); /* get page table pointer */
1318 if (!table || *table & _PAGE_INVALID)
1319 return;
Heiko Carstensf1c11742017-07-05 07:37:27 +02001320 gmap_call_notifier(sg, raddr, raddr + _PAGE_SIZE - 1);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001321 ptep_unshadow_pte(sg->mm, raddr, (pte_t *) table);
1322}
1323
1324/**
1325 * __gmap_unshadow_pgt - remove all entries from a shadow page table
1326 * @sg: pointer to the shadow guest address space structure
1327 * @raddr: rmap address in the shadow guest address space
1328 * @pgt: pointer to the start of a shadow page table
1329 *
1330 * Called with the sg->guest_table_lock
1331 */
1332static void __gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr,
1333 unsigned long *pgt)
1334{
1335 int i;
1336
1337 BUG_ON(!gmap_is_shadow(sg));
Heiko Carstensf1c11742017-07-05 07:37:27 +02001338 for (i = 0; i < _PAGE_ENTRIES; i++, raddr += _PAGE_SIZE)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001339 pgt[i] = _PAGE_INVALID;
1340}
1341
1342/**
1343 * gmap_unshadow_pgt - remove a shadow page table from a segment entry
1344 * @sg: pointer to the shadow guest address space structure
1345 * @raddr: address in the shadow guest address space
1346 *
1347 * Called with the sg->guest_table_lock
1348 */
1349static void gmap_unshadow_pgt(struct gmap *sg, unsigned long raddr)
1350{
Nico Boehr079f0c22022-10-20 16:31:55 +02001351 unsigned long *ste;
1352 phys_addr_t sto, pgt;
Qi Zheng57b77b72024-03-04 19:07:20 +08001353 struct ptdesc *ptdesc;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001354
1355 BUG_ON(!gmap_is_shadow(sg));
1356 ste = gmap_table_walk(sg, raddr, 1); /* get segment pointer */
David Hildenbrand998f6372016-03-08 12:23:38 +01001357 if (!ste || !(*ste & _SEGMENT_ENTRY_ORIGIN))
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001358 return;
Heiko Carstensf1c11742017-07-05 07:37:27 +02001359 gmap_call_notifier(sg, raddr, raddr + _SEGMENT_SIZE - 1);
Nico Boehr079f0c22022-10-20 16:31:55 +02001360 sto = __pa(ste - ((raddr & _SEGMENT_INDEX) >> _SEGMENT_SHIFT));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001361 gmap_idte_one(sto | _ASCE_TYPE_SEGMENT, raddr);
Nico Boehr079f0c22022-10-20 16:31:55 +02001362 pgt = *ste & _SEGMENT_ENTRY_ORIGIN;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001363 *ste = _SEGMENT_ENTRY_EMPTY;
Nico Boehr079f0c22022-10-20 16:31:55 +02001364 __gmap_unshadow_pgt(sg, raddr, __va(pgt));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001365 /* Free page table */
Qi Zheng57b77b72024-03-04 19:07:20 +08001366 ptdesc = page_ptdesc(phys_to_page(pgt));
1367 list_del(&ptdesc->pt_list);
1368 page_table_free_pgste(ptdesc);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001369}
1370
1371/**
1372 * __gmap_unshadow_sgt - remove all entries from a shadow segment table
1373 * @sg: pointer to the shadow guest address space structure
1374 * @raddr: rmap address in the shadow guest address space
1375 * @sgt: pointer to the start of a shadow segment table
1376 *
1377 * Called with the sg->guest_table_lock
1378 */
1379static void __gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr,
1380 unsigned long *sgt)
1381{
Qi Zheng57b77b72024-03-04 19:07:20 +08001382 struct ptdesc *ptdesc;
Nico Boehr079f0c22022-10-20 16:31:55 +02001383 phys_addr_t pgt;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001384 int i;
1385
1386 BUG_ON(!gmap_is_shadow(sg));
Heiko Carstensf1c11742017-07-05 07:37:27 +02001387 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _SEGMENT_SIZE) {
David Hildenbrand998f6372016-03-08 12:23:38 +01001388 if (!(sgt[i] & _SEGMENT_ENTRY_ORIGIN))
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001389 continue;
Nico Boehr079f0c22022-10-20 16:31:55 +02001390 pgt = sgt[i] & _REGION_ENTRY_ORIGIN;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001391 sgt[i] = _SEGMENT_ENTRY_EMPTY;
Nico Boehr079f0c22022-10-20 16:31:55 +02001392 __gmap_unshadow_pgt(sg, raddr, __va(pgt));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001393 /* Free page table */
Qi Zheng57b77b72024-03-04 19:07:20 +08001394 ptdesc = page_ptdesc(phys_to_page(pgt));
1395 list_del(&ptdesc->pt_list);
1396 page_table_free_pgste(ptdesc);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001397 }
1398}
1399
1400/**
1401 * gmap_unshadow_sgt - remove a shadow segment table from a region-3 entry
1402 * @sg: pointer to the shadow guest address space structure
1403 * @raddr: rmap address in the shadow guest address space
1404 *
1405 * Called with the shadow->guest_table_lock
1406 */
1407static void gmap_unshadow_sgt(struct gmap *sg, unsigned long raddr)
1408{
Nico Boehr079f0c22022-10-20 16:31:55 +02001409 unsigned long r3o, *r3e;
1410 phys_addr_t sgt;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001411 struct page *page;
1412
1413 BUG_ON(!gmap_is_shadow(sg));
1414 r3e = gmap_table_walk(sg, raddr, 2); /* get region-3 pointer */
David Hildenbrand998f6372016-03-08 12:23:38 +01001415 if (!r3e || !(*r3e & _REGION_ENTRY_ORIGIN))
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001416 return;
Heiko Carstensf1c11742017-07-05 07:37:27 +02001417 gmap_call_notifier(sg, raddr, raddr + _REGION3_SIZE - 1);
1418 r3o = (unsigned long) (r3e - ((raddr & _REGION3_INDEX) >> _REGION3_SHIFT));
Nico Boehr079f0c22022-10-20 16:31:55 +02001419 gmap_idte_one(__pa(r3o) | _ASCE_TYPE_REGION3, raddr);
1420 sgt = *r3e & _REGION_ENTRY_ORIGIN;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001421 *r3e = _REGION3_ENTRY_EMPTY;
Nico Boehr079f0c22022-10-20 16:31:55 +02001422 __gmap_unshadow_sgt(sg, raddr, __va(sgt));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001423 /* Free segment table */
Nico Boehr079f0c22022-10-20 16:31:55 +02001424 page = phys_to_page(sgt);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001425 list_del(&page->lru);
Heiko Carstensf1c11742017-07-05 07:37:27 +02001426 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001427}
1428
1429/**
1430 * __gmap_unshadow_r3t - remove all entries from a shadow region-3 table
1431 * @sg: pointer to the shadow guest address space structure
1432 * @raddr: address in the shadow guest address space
1433 * @r3t: pointer to the start of a shadow region-3 table
1434 *
1435 * Called with the sg->guest_table_lock
1436 */
1437static void __gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr,
1438 unsigned long *r3t)
1439{
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001440 struct page *page;
Nico Boehr079f0c22022-10-20 16:31:55 +02001441 phys_addr_t sgt;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001442 int i;
1443
1444 BUG_ON(!gmap_is_shadow(sg));
Heiko Carstensf1c11742017-07-05 07:37:27 +02001445 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION3_SIZE) {
David Hildenbrand998f6372016-03-08 12:23:38 +01001446 if (!(r3t[i] & _REGION_ENTRY_ORIGIN))
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001447 continue;
Nico Boehr079f0c22022-10-20 16:31:55 +02001448 sgt = r3t[i] & _REGION_ENTRY_ORIGIN;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001449 r3t[i] = _REGION3_ENTRY_EMPTY;
Nico Boehr079f0c22022-10-20 16:31:55 +02001450 __gmap_unshadow_sgt(sg, raddr, __va(sgt));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001451 /* Free segment table */
Nico Boehr079f0c22022-10-20 16:31:55 +02001452 page = phys_to_page(sgt);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001453 list_del(&page->lru);
Heiko Carstensf1c11742017-07-05 07:37:27 +02001454 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001455 }
1456}
1457
1458/**
1459 * gmap_unshadow_r3t - remove a shadow region-3 table from a region-2 entry
1460 * @sg: pointer to the shadow guest address space structure
1461 * @raddr: rmap address in the shadow guest address space
1462 *
1463 * Called with the sg->guest_table_lock
1464 */
1465static void gmap_unshadow_r3t(struct gmap *sg, unsigned long raddr)
1466{
Nico Boehr079f0c22022-10-20 16:31:55 +02001467 unsigned long r2o, *r2e;
1468 phys_addr_t r3t;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001469 struct page *page;
1470
1471 BUG_ON(!gmap_is_shadow(sg));
1472 r2e = gmap_table_walk(sg, raddr, 3); /* get region-2 pointer */
David Hildenbrand998f6372016-03-08 12:23:38 +01001473 if (!r2e || !(*r2e & _REGION_ENTRY_ORIGIN))
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001474 return;
Heiko Carstensf1c11742017-07-05 07:37:27 +02001475 gmap_call_notifier(sg, raddr, raddr + _REGION2_SIZE - 1);
1476 r2o = (unsigned long) (r2e - ((raddr & _REGION2_INDEX) >> _REGION2_SHIFT));
Nico Boehr079f0c22022-10-20 16:31:55 +02001477 gmap_idte_one(__pa(r2o) | _ASCE_TYPE_REGION2, raddr);
1478 r3t = *r2e & _REGION_ENTRY_ORIGIN;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001479 *r2e = _REGION2_ENTRY_EMPTY;
Nico Boehr079f0c22022-10-20 16:31:55 +02001480 __gmap_unshadow_r3t(sg, raddr, __va(r3t));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001481 /* Free region 3 table */
Nico Boehr079f0c22022-10-20 16:31:55 +02001482 page = phys_to_page(r3t);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001483 list_del(&page->lru);
Heiko Carstensf1c11742017-07-05 07:37:27 +02001484 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001485}
1486
1487/**
1488 * __gmap_unshadow_r2t - remove all entries from a shadow region-2 table
1489 * @sg: pointer to the shadow guest address space structure
1490 * @raddr: rmap address in the shadow guest address space
1491 * @r2t: pointer to the start of a shadow region-2 table
1492 *
1493 * Called with the sg->guest_table_lock
1494 */
1495static void __gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr,
1496 unsigned long *r2t)
1497{
Nico Boehr079f0c22022-10-20 16:31:55 +02001498 phys_addr_t r3t;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001499 struct page *page;
1500 int i;
1501
1502 BUG_ON(!gmap_is_shadow(sg));
Heiko Carstensf1c11742017-07-05 07:37:27 +02001503 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION2_SIZE) {
David Hildenbrand998f6372016-03-08 12:23:38 +01001504 if (!(r2t[i] & _REGION_ENTRY_ORIGIN))
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001505 continue;
Nico Boehr079f0c22022-10-20 16:31:55 +02001506 r3t = r2t[i] & _REGION_ENTRY_ORIGIN;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001507 r2t[i] = _REGION2_ENTRY_EMPTY;
Nico Boehr079f0c22022-10-20 16:31:55 +02001508 __gmap_unshadow_r3t(sg, raddr, __va(r3t));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001509 /* Free region 3 table */
Nico Boehr079f0c22022-10-20 16:31:55 +02001510 page = phys_to_page(r3t);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001511 list_del(&page->lru);
Heiko Carstensf1c11742017-07-05 07:37:27 +02001512 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001513 }
1514}
1515
1516/**
1517 * gmap_unshadow_r2t - remove a shadow region-2 table from a region-1 entry
1518 * @sg: pointer to the shadow guest address space structure
1519 * @raddr: rmap address in the shadow guest address space
1520 *
1521 * Called with the sg->guest_table_lock
1522 */
1523static void gmap_unshadow_r2t(struct gmap *sg, unsigned long raddr)
1524{
Nico Boehr079f0c22022-10-20 16:31:55 +02001525 unsigned long r1o, *r1e;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001526 struct page *page;
Nico Boehr079f0c22022-10-20 16:31:55 +02001527 phys_addr_t r2t;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001528
1529 BUG_ON(!gmap_is_shadow(sg));
1530 r1e = gmap_table_walk(sg, raddr, 4); /* get region-1 pointer */
David Hildenbrand998f6372016-03-08 12:23:38 +01001531 if (!r1e || !(*r1e & _REGION_ENTRY_ORIGIN))
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001532 return;
Heiko Carstensf1c11742017-07-05 07:37:27 +02001533 gmap_call_notifier(sg, raddr, raddr + _REGION1_SIZE - 1);
1534 r1o = (unsigned long) (r1e - ((raddr & _REGION1_INDEX) >> _REGION1_SHIFT));
Nico Boehr079f0c22022-10-20 16:31:55 +02001535 gmap_idte_one(__pa(r1o) | _ASCE_TYPE_REGION1, raddr);
1536 r2t = *r1e & _REGION_ENTRY_ORIGIN;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001537 *r1e = _REGION1_ENTRY_EMPTY;
Nico Boehr079f0c22022-10-20 16:31:55 +02001538 __gmap_unshadow_r2t(sg, raddr, __va(r2t));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001539 /* Free region 2 table */
Nico Boehr079f0c22022-10-20 16:31:55 +02001540 page = phys_to_page(r2t);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001541 list_del(&page->lru);
Heiko Carstensf1c11742017-07-05 07:37:27 +02001542 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001543}
1544
1545/**
1546 * __gmap_unshadow_r1t - remove all entries from a shadow region-1 table
1547 * @sg: pointer to the shadow guest address space structure
1548 * @raddr: rmap address in the shadow guest address space
1549 * @r1t: pointer to the start of a shadow region-1 table
1550 *
1551 * Called with the shadow->guest_table_lock
1552 */
1553static void __gmap_unshadow_r1t(struct gmap *sg, unsigned long raddr,
1554 unsigned long *r1t)
1555{
Nico Boehr079f0c22022-10-20 16:31:55 +02001556 unsigned long asce;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001557 struct page *page;
Nico Boehr079f0c22022-10-20 16:31:55 +02001558 phys_addr_t r2t;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001559 int i;
1560
1561 BUG_ON(!gmap_is_shadow(sg));
Nico Boehr079f0c22022-10-20 16:31:55 +02001562 asce = __pa(r1t) | _ASCE_TYPE_REGION1;
Heiko Carstensf1c11742017-07-05 07:37:27 +02001563 for (i = 0; i < _CRST_ENTRIES; i++, raddr += _REGION1_SIZE) {
David Hildenbrand998f6372016-03-08 12:23:38 +01001564 if (!(r1t[i] & _REGION_ENTRY_ORIGIN))
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001565 continue;
Nico Boehr079f0c22022-10-20 16:31:55 +02001566 r2t = r1t[i] & _REGION_ENTRY_ORIGIN;
1567 __gmap_unshadow_r2t(sg, raddr, __va(r2t));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001568 /* Clear entry and flush translation r1t -> r2t */
1569 gmap_idte_one(asce, raddr);
1570 r1t[i] = _REGION1_ENTRY_EMPTY;
1571 /* Free region 2 table */
Nico Boehr079f0c22022-10-20 16:31:55 +02001572 page = phys_to_page(r2t);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001573 list_del(&page->lru);
Heiko Carstensf1c11742017-07-05 07:37:27 +02001574 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001575 }
1576}
1577
1578/**
1579 * gmap_unshadow - remove a shadow page table completely
1580 * @sg: pointer to the shadow guest address space structure
1581 *
1582 * Called with sg->guest_table_lock
1583 */
1584static void gmap_unshadow(struct gmap *sg)
1585{
1586 unsigned long *table;
1587
1588 BUG_ON(!gmap_is_shadow(sg));
1589 if (sg->removed)
1590 return;
1591 sg->removed = 1;
1592 gmap_call_notifier(sg, 0, -1UL);
David Hildenbrandeea36782016-04-15 12:45:45 +02001593 gmap_flush_tlb(sg);
Nico Boehr079f0c22022-10-20 16:31:55 +02001594 table = __va(sg->asce & _ASCE_ORIGIN);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001595 switch (sg->asce & _ASCE_TYPE_MASK) {
1596 case _ASCE_TYPE_REGION1:
1597 __gmap_unshadow_r1t(sg, 0, table);
1598 break;
1599 case _ASCE_TYPE_REGION2:
1600 __gmap_unshadow_r2t(sg, 0, table);
1601 break;
1602 case _ASCE_TYPE_REGION3:
1603 __gmap_unshadow_r3t(sg, 0, table);
1604 break;
1605 case _ASCE_TYPE_SEGMENT:
1606 __gmap_unshadow_sgt(sg, 0, table);
1607 break;
1608 }
1609}
1610
1611/**
1612 * gmap_find_shadow - find a specific asce in the list of shadow tables
1613 * @parent: pointer to the parent gmap
1614 * @asce: ASCE for which the shadow table is created
David Hildenbrand5b062bd2016-03-08 12:17:40 +01001615 * @edat_level: edat level to be used for the shadow translation
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001616 *
1617 * Returns the pointer to a gmap if a shadow table with the given asce is
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001618 * already available, ERR_PTR(-EAGAIN) if another one is just being created,
1619 * otherwise NULL
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001620 */
David Hildenbrand5b062bd2016-03-08 12:17:40 +01001621static struct gmap *gmap_find_shadow(struct gmap *parent, unsigned long asce,
1622 int edat_level)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001623{
1624 struct gmap *sg;
1625
1626 list_for_each_entry(sg, &parent->children, list) {
David Hildenbrand5b062bd2016-03-08 12:17:40 +01001627 if (sg->orig_asce != asce || sg->edat_level != edat_level ||
1628 sg->removed)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001629 continue;
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001630 if (!sg->initialized)
1631 return ERR_PTR(-EAGAIN);
Chuhong Yuan40e90652019-08-08 15:18:26 +08001632 refcount_inc(&sg->ref_count);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001633 return sg;
1634 }
1635 return NULL;
1636}
1637
1638/**
David Hildenbrand5b6c9632016-05-27 18:57:33 +02001639 * gmap_shadow_valid - check if a shadow guest address space matches the
1640 * given properties and is still valid
1641 * @sg: pointer to the shadow guest address space structure
1642 * @asce: ASCE for which the shadow table is requested
1643 * @edat_level: edat level to be used for the shadow translation
1644 *
1645 * Returns 1 if the gmap shadow is still valid and matches the given
1646 * properties, the caller can continue using it. Returns 0 otherwise, the
1647 * caller has to request a new shadow gmap in this case.
1648 *
1649 */
1650int gmap_shadow_valid(struct gmap *sg, unsigned long asce, int edat_level)
1651{
1652 if (sg->removed)
1653 return 0;
1654 return sg->orig_asce == asce && sg->edat_level == edat_level;
1655}
1656EXPORT_SYMBOL_GPL(gmap_shadow_valid);
1657
1658/**
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001659 * gmap_shadow - create/find a shadow guest address space
1660 * @parent: pointer to the parent gmap
1661 * @asce: ASCE for which the shadow table is created
David Hildenbrand5b062bd2016-03-08 12:17:40 +01001662 * @edat_level: edat level to be used for the shadow translation
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001663 *
1664 * The pages of the top level page table referred by the asce parameter
1665 * will be set to read-only and marked in the PGSTEs of the kvm process.
1666 * The shadow table will be removed automatically on any change to the
1667 * PTE mapping for the source table.
1668 *
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001669 * Returns a guest address space structure, ERR_PTR(-ENOMEM) if out of memory,
1670 * ERR_PTR(-EAGAIN) if the caller has to retry and ERR_PTR(-EFAULT) if the
1671 * parent gmap table could not be protected.
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001672 */
David Hildenbrand5b062bd2016-03-08 12:17:40 +01001673struct gmap *gmap_shadow(struct gmap *parent, unsigned long asce,
1674 int edat_level)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001675{
1676 struct gmap *sg, *new;
1677 unsigned long limit;
1678 int rc;
1679
Janosch Franka9e00d82018-07-13 11:28:37 +01001680 BUG_ON(parent->mm->context.allow_gmap_hpage_1m);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001681 BUG_ON(gmap_is_shadow(parent));
1682 spin_lock(&parent->shadow_lock);
David Hildenbrand5b062bd2016-03-08 12:17:40 +01001683 sg = gmap_find_shadow(parent, asce, edat_level);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001684 spin_unlock(&parent->shadow_lock);
1685 if (sg)
1686 return sg;
1687 /* Create a new shadow gmap */
1688 limit = -1UL >> (33 - (((asce & _ASCE_TYPE_MASK) >> 2) * 11));
David Hildenbrand3218f702016-04-18 16:22:24 +02001689 if (asce & _ASCE_REAL_SPACE)
1690 limit = -1UL;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001691 new = gmap_alloc(limit);
1692 if (!new)
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001693 return ERR_PTR(-ENOMEM);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001694 new->mm = parent->mm;
1695 new->parent = gmap_get(parent);
Christian Borntraegerfe752332023-12-20 13:53:17 +01001696 new->private = parent->private;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001697 new->orig_asce = asce;
David Hildenbrand5b062bd2016-03-08 12:17:40 +01001698 new->edat_level = edat_level;
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001699 new->initialized = false;
1700 spin_lock(&parent->shadow_lock);
1701 /* Recheck if another CPU created the same shadow */
David Hildenbrand5b062bd2016-03-08 12:17:40 +01001702 sg = gmap_find_shadow(parent, asce, edat_level);
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001703 if (sg) {
1704 spin_unlock(&parent->shadow_lock);
1705 gmap_free(new);
1706 return sg;
1707 }
David Hildenbrand717c0552016-05-02 12:10:17 +02001708 if (asce & _ASCE_REAL_SPACE) {
1709 /* only allow one real-space gmap shadow */
1710 list_for_each_entry(sg, &parent->children, list) {
1711 if (sg->orig_asce & _ASCE_REAL_SPACE) {
1712 spin_lock(&sg->guest_table_lock);
1713 gmap_unshadow(sg);
1714 spin_unlock(&sg->guest_table_lock);
1715 list_del(&sg->list);
1716 gmap_put(sg);
1717 break;
1718 }
1719 }
1720 }
Chuhong Yuan40e90652019-08-08 15:18:26 +08001721 refcount_set(&new->ref_count, 2);
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001722 list_add(&new->list, &parent->children);
David Hildenbrand3218f702016-04-18 16:22:24 +02001723 if (asce & _ASCE_REAL_SPACE) {
1724 /* nothing to protect, return right away */
1725 new->initialized = true;
1726 spin_unlock(&parent->shadow_lock);
1727 return new;
1728 }
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001729 spin_unlock(&parent->shadow_lock);
1730 /* protect after insertion, so it will get properly invalidated */
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001731 mmap_read_lock(parent->mm);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001732 rc = gmap_protect_range(parent, asce & _ASCE_ORIGIN,
Heiko Carstensf1c11742017-07-05 07:37:27 +02001733 ((asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE,
Janosch Frank2c46e972018-07-13 11:28:18 +01001734 PROT_READ, GMAP_NOTIFY_SHADOW);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07001735 mmap_read_unlock(parent->mm);
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001736 spin_lock(&parent->shadow_lock);
1737 new->initialized = true;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001738 if (rc) {
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001739 list_del(&new->list);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001740 gmap_free(new);
David Hildenbrand0f7f8482016-03-08 12:30:46 +01001741 new = ERR_PTR(rc);
1742 }
1743 spin_unlock(&parent->shadow_lock);
1744 return new;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001745}
1746EXPORT_SYMBOL_GPL(gmap_shadow);
1747
1748/**
1749 * gmap_shadow_r2t - create an empty shadow region 2 table
1750 * @sg: pointer to the shadow guest address space structure
1751 * @saddr: faulting address in the shadow gmap
1752 * @r2t: parent gmap address of the region 2 table to get shadowed
David Hildenbrand3218f702016-04-18 16:22:24 +02001753 * @fake: r2t references contiguous guest memory block, not a r2t
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001754 *
1755 * The r2t parameter specifies the address of the source table. The
1756 * four pages of the source table are made read-only in the parent gmap
1757 * address space. A write to the source table area @r2t will automatically
Heiko Carstenscada9382023-06-28 16:23:20 +02001758 * remove the shadow r2 table and all of its descendants.
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001759 *
1760 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1761 * shadow table structure is incomplete, -ENOMEM if out of memory and
1762 * -EFAULT if an address in the parent gmap could not be resolved.
1763 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001764 * Called with sg->mm->mmap_lock in read.
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001765 */
David Hildenbrand3218f702016-04-18 16:22:24 +02001766int gmap_shadow_r2t(struct gmap *sg, unsigned long saddr, unsigned long r2t,
1767 int fake)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001768{
1769 unsigned long raddr, origin, offset, len;
Nico Boehr079f0c22022-10-20 16:31:55 +02001770 unsigned long *table;
1771 phys_addr_t s_r2t;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001772 struct page *page;
1773 int rc;
1774
1775 BUG_ON(!gmap_is_shadow(sg));
1776 /* Allocate a shadow region second table */
Heiko Carstens1954da42023-10-20 17:26:50 +02001777 page = gmap_alloc_crst();
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001778 if (!page)
1779 return -ENOMEM;
1780 page->index = r2t & _REGION_ENTRY_ORIGIN;
David Hildenbrand3218f702016-04-18 16:22:24 +02001781 if (fake)
1782 page->index |= GMAP_SHADOW_FAKE_TABLE;
Nico Boehr079f0c22022-10-20 16:31:55 +02001783 s_r2t = page_to_phys(page);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001784 /* Install shadow region second table */
1785 spin_lock(&sg->guest_table_lock);
1786 table = gmap_table_walk(sg, saddr, 4); /* get region-1 pointer */
1787 if (!table) {
1788 rc = -EAGAIN; /* Race with unshadow */
1789 goto out_free;
1790 }
1791 if (!(*table & _REGION_ENTRY_INVALID)) {
1792 rc = 0; /* Already established */
1793 goto out_free;
David Hildenbrand998f6372016-03-08 12:23:38 +01001794 } else if (*table & _REGION_ENTRY_ORIGIN) {
1795 rc = -EAGAIN; /* Race with shadow */
1796 goto out_free;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001797 }
Nico Boehr079f0c22022-10-20 16:31:55 +02001798 crst_table_init(__va(s_r2t), _REGION2_ENTRY_EMPTY);
David Hildenbrand998f6372016-03-08 12:23:38 +01001799 /* mark as invalid as long as the parent table is not protected */
Nico Boehr079f0c22022-10-20 16:31:55 +02001800 *table = s_r2t | _REGION_ENTRY_LENGTH |
David Hildenbrand998f6372016-03-08 12:23:38 +01001801 _REGION_ENTRY_TYPE_R1 | _REGION_ENTRY_INVALID;
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02001802 if (sg->edat_level >= 1)
1803 *table |= (r2t & _REGION_ENTRY_PROTECT);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001804 list_add(&page->lru, &sg->crst_list);
David Hildenbrand3218f702016-04-18 16:22:24 +02001805 if (fake) {
1806 /* nothing to protect for fake tables */
1807 *table &= ~_REGION_ENTRY_INVALID;
1808 spin_unlock(&sg->guest_table_lock);
1809 return 0;
1810 }
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001811 spin_unlock(&sg->guest_table_lock);
1812 /* Make r2t read-only in parent gmap page table */
Heiko Carstensf1c11742017-07-05 07:37:27 +02001813 raddr = (saddr & _REGION1_MASK) | _SHADOW_RMAP_REGION1;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001814 origin = r2t & _REGION_ENTRY_ORIGIN;
Heiko Carstensf1c11742017-07-05 07:37:27 +02001815 offset = ((r2t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1816 len = ((r2t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
David Hildenbrand5c528db2018-01-23 22:26:18 +01001817 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
David Hildenbrand998f6372016-03-08 12:23:38 +01001818 spin_lock(&sg->guest_table_lock);
1819 if (!rc) {
1820 table = gmap_table_walk(sg, saddr, 4);
Nico Boehr079f0c22022-10-20 16:31:55 +02001821 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r2t)
David Hildenbrand998f6372016-03-08 12:23:38 +01001822 rc = -EAGAIN; /* Race with unshadow */
1823 else
1824 *table &= ~_REGION_ENTRY_INVALID;
1825 } else {
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001826 gmap_unshadow_r2t(sg, raddr);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001827 }
David Hildenbrand998f6372016-03-08 12:23:38 +01001828 spin_unlock(&sg->guest_table_lock);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001829 return rc;
1830out_free:
1831 spin_unlock(&sg->guest_table_lock);
Heiko Carstensf1c11742017-07-05 07:37:27 +02001832 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001833 return rc;
1834}
1835EXPORT_SYMBOL_GPL(gmap_shadow_r2t);
1836
1837/**
1838 * gmap_shadow_r3t - create a shadow region 3 table
1839 * @sg: pointer to the shadow guest address space structure
1840 * @saddr: faulting address in the shadow gmap
1841 * @r3t: parent gmap address of the region 3 table to get shadowed
David Hildenbrand3218f702016-04-18 16:22:24 +02001842 * @fake: r3t references contiguous guest memory block, not a r3t
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001843 *
1844 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
1845 * shadow table structure is incomplete, -ENOMEM if out of memory and
1846 * -EFAULT if an address in the parent gmap could not be resolved.
1847 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001848 * Called with sg->mm->mmap_lock in read.
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001849 */
David Hildenbrand3218f702016-04-18 16:22:24 +02001850int gmap_shadow_r3t(struct gmap *sg, unsigned long saddr, unsigned long r3t,
1851 int fake)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001852{
1853 unsigned long raddr, origin, offset, len;
Nico Boehr079f0c22022-10-20 16:31:55 +02001854 unsigned long *table;
1855 phys_addr_t s_r3t;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001856 struct page *page;
1857 int rc;
1858
1859 BUG_ON(!gmap_is_shadow(sg));
1860 /* Allocate a shadow region second table */
Heiko Carstens1954da42023-10-20 17:26:50 +02001861 page = gmap_alloc_crst();
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001862 if (!page)
1863 return -ENOMEM;
1864 page->index = r3t & _REGION_ENTRY_ORIGIN;
David Hildenbrand3218f702016-04-18 16:22:24 +02001865 if (fake)
1866 page->index |= GMAP_SHADOW_FAKE_TABLE;
Nico Boehr079f0c22022-10-20 16:31:55 +02001867 s_r3t = page_to_phys(page);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001868 /* Install shadow region second table */
1869 spin_lock(&sg->guest_table_lock);
1870 table = gmap_table_walk(sg, saddr, 3); /* get region-2 pointer */
1871 if (!table) {
1872 rc = -EAGAIN; /* Race with unshadow */
1873 goto out_free;
1874 }
1875 if (!(*table & _REGION_ENTRY_INVALID)) {
1876 rc = 0; /* Already established */
1877 goto out_free;
David Hildenbrand998f6372016-03-08 12:23:38 +01001878 } else if (*table & _REGION_ENTRY_ORIGIN) {
1879 rc = -EAGAIN; /* Race with shadow */
David Hildenbrand1493e0f2020-04-03 17:30:48 +02001880 goto out_free;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001881 }
Nico Boehr079f0c22022-10-20 16:31:55 +02001882 crst_table_init(__va(s_r3t), _REGION3_ENTRY_EMPTY);
David Hildenbrand998f6372016-03-08 12:23:38 +01001883 /* mark as invalid as long as the parent table is not protected */
Nico Boehr079f0c22022-10-20 16:31:55 +02001884 *table = s_r3t | _REGION_ENTRY_LENGTH |
David Hildenbrand998f6372016-03-08 12:23:38 +01001885 _REGION_ENTRY_TYPE_R2 | _REGION_ENTRY_INVALID;
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02001886 if (sg->edat_level >= 1)
1887 *table |= (r3t & _REGION_ENTRY_PROTECT);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001888 list_add(&page->lru, &sg->crst_list);
David Hildenbrand3218f702016-04-18 16:22:24 +02001889 if (fake) {
1890 /* nothing to protect for fake tables */
1891 *table &= ~_REGION_ENTRY_INVALID;
1892 spin_unlock(&sg->guest_table_lock);
1893 return 0;
1894 }
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001895 spin_unlock(&sg->guest_table_lock);
1896 /* Make r3t read-only in parent gmap page table */
Heiko Carstensf1c11742017-07-05 07:37:27 +02001897 raddr = (saddr & _REGION2_MASK) | _SHADOW_RMAP_REGION2;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001898 origin = r3t & _REGION_ENTRY_ORIGIN;
Heiko Carstensf1c11742017-07-05 07:37:27 +02001899 offset = ((r3t & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1900 len = ((r3t & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
David Hildenbrand5c528db2018-01-23 22:26:18 +01001901 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
David Hildenbrand998f6372016-03-08 12:23:38 +01001902 spin_lock(&sg->guest_table_lock);
1903 if (!rc) {
1904 table = gmap_table_walk(sg, saddr, 3);
Nico Boehr079f0c22022-10-20 16:31:55 +02001905 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_r3t)
David Hildenbrand998f6372016-03-08 12:23:38 +01001906 rc = -EAGAIN; /* Race with unshadow */
1907 else
1908 *table &= ~_REGION_ENTRY_INVALID;
1909 } else {
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001910 gmap_unshadow_r3t(sg, raddr);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001911 }
David Hildenbrand998f6372016-03-08 12:23:38 +01001912 spin_unlock(&sg->guest_table_lock);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001913 return rc;
1914out_free:
1915 spin_unlock(&sg->guest_table_lock);
Heiko Carstensf1c11742017-07-05 07:37:27 +02001916 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001917 return rc;
1918}
1919EXPORT_SYMBOL_GPL(gmap_shadow_r3t);
1920
1921/**
1922 * gmap_shadow_sgt - create a shadow segment table
1923 * @sg: pointer to the shadow guest address space structure
1924 * @saddr: faulting address in the shadow gmap
1925 * @sgt: parent gmap address of the segment table to get shadowed
David Hildenbrand18b898092016-04-18 13:42:05 +02001926 * @fake: sgt references contiguous guest memory block, not a sgt
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001927 *
1928 * Returns: 0 if successfully shadowed or already shadowed, -EAGAIN if the
1929 * shadow table structure is incomplete, -ENOMEM if out of memory and
1930 * -EFAULT if an address in the parent gmap could not be resolved.
1931 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07001932 * Called with sg->mm->mmap_lock in read.
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001933 */
David Hildenbrand18b898092016-04-18 13:42:05 +02001934int gmap_shadow_sgt(struct gmap *sg, unsigned long saddr, unsigned long sgt,
1935 int fake)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001936{
1937 unsigned long raddr, origin, offset, len;
Nico Boehr079f0c22022-10-20 16:31:55 +02001938 unsigned long *table;
1939 phys_addr_t s_sgt;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001940 struct page *page;
1941 int rc;
1942
David Hildenbrand18b898092016-04-18 13:42:05 +02001943 BUG_ON(!gmap_is_shadow(sg) || (sgt & _REGION3_ENTRY_LARGE));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001944 /* Allocate a shadow segment table */
Heiko Carstens1954da42023-10-20 17:26:50 +02001945 page = gmap_alloc_crst();
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001946 if (!page)
1947 return -ENOMEM;
1948 page->index = sgt & _REGION_ENTRY_ORIGIN;
David Hildenbrand18b898092016-04-18 13:42:05 +02001949 if (fake)
1950 page->index |= GMAP_SHADOW_FAKE_TABLE;
Nico Boehr079f0c22022-10-20 16:31:55 +02001951 s_sgt = page_to_phys(page);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001952 /* Install shadow region second table */
1953 spin_lock(&sg->guest_table_lock);
1954 table = gmap_table_walk(sg, saddr, 2); /* get region-3 pointer */
1955 if (!table) {
1956 rc = -EAGAIN; /* Race with unshadow */
1957 goto out_free;
1958 }
1959 if (!(*table & _REGION_ENTRY_INVALID)) {
1960 rc = 0; /* Already established */
1961 goto out_free;
David Hildenbrand998f6372016-03-08 12:23:38 +01001962 } else if (*table & _REGION_ENTRY_ORIGIN) {
1963 rc = -EAGAIN; /* Race with shadow */
1964 goto out_free;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001965 }
Nico Boehr079f0c22022-10-20 16:31:55 +02001966 crst_table_init(__va(s_sgt), _SEGMENT_ENTRY_EMPTY);
David Hildenbrand998f6372016-03-08 12:23:38 +01001967 /* mark as invalid as long as the parent table is not protected */
Nico Boehr079f0c22022-10-20 16:31:55 +02001968 *table = s_sgt | _REGION_ENTRY_LENGTH |
David Hildenbrand998f6372016-03-08 12:23:38 +01001969 _REGION_ENTRY_TYPE_R3 | _REGION_ENTRY_INVALID;
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02001970 if (sg->edat_level >= 1)
1971 *table |= sgt & _REGION_ENTRY_PROTECT;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001972 list_add(&page->lru, &sg->crst_list);
David Hildenbrand18b898092016-04-18 13:42:05 +02001973 if (fake) {
1974 /* nothing to protect for fake tables */
1975 *table &= ~_REGION_ENTRY_INVALID;
1976 spin_unlock(&sg->guest_table_lock);
1977 return 0;
1978 }
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001979 spin_unlock(&sg->guest_table_lock);
1980 /* Make sgt read-only in parent gmap page table */
Heiko Carstensf1c11742017-07-05 07:37:27 +02001981 raddr = (saddr & _REGION3_MASK) | _SHADOW_RMAP_REGION3;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001982 origin = sgt & _REGION_ENTRY_ORIGIN;
Heiko Carstensf1c11742017-07-05 07:37:27 +02001983 offset = ((sgt & _REGION_ENTRY_OFFSET) >> 6) * PAGE_SIZE;
1984 len = ((sgt & _REGION_ENTRY_LENGTH) + 1) * PAGE_SIZE - offset;
David Hildenbrand5c528db2018-01-23 22:26:18 +01001985 rc = gmap_protect_rmap(sg, raddr, origin + offset, len);
David Hildenbrand998f6372016-03-08 12:23:38 +01001986 spin_lock(&sg->guest_table_lock);
1987 if (!rc) {
1988 table = gmap_table_walk(sg, saddr, 2);
Nico Boehr079f0c22022-10-20 16:31:55 +02001989 if (!table || (*table & _REGION_ENTRY_ORIGIN) != s_sgt)
David Hildenbrand998f6372016-03-08 12:23:38 +01001990 rc = -EAGAIN; /* Race with unshadow */
1991 else
1992 *table &= ~_REGION_ENTRY_INVALID;
1993 } else {
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001994 gmap_unshadow_sgt(sg, raddr);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001995 }
David Hildenbrand998f6372016-03-08 12:23:38 +01001996 spin_unlock(&sg->guest_table_lock);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01001997 return rc;
1998out_free:
1999 spin_unlock(&sg->guest_table_lock);
Heiko Carstensf1c11742017-07-05 07:37:27 +02002000 __free_pages(page, CRST_ALLOC_ORDER);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002001 return rc;
2002}
2003EXPORT_SYMBOL_GPL(gmap_shadow_sgt);
2004
2005/**
Heiko Carstens2e827522021-09-02 19:47:37 +02002006 * gmap_shadow_pgt_lookup - find a shadow page table
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002007 * @sg: pointer to the shadow guest address space structure
2008 * @saddr: the address in the shadow aguest address space
2009 * @pgt: parent gmap address of the page table to get shadowed
2010 * @dat_protection: if the pgtable is marked as protected by dat
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02002011 * @fake: pgt references contiguous guest memory block, not a pgtable
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002012 *
2013 * Returns 0 if the shadow page table was found and -EAGAIN if the page
2014 * table was not found.
2015 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002016 * Called with sg->mm->mmap_lock in read.
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002017 */
2018int gmap_shadow_pgt_lookup(struct gmap *sg, unsigned long saddr,
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02002019 unsigned long *pgt, int *dat_protection,
2020 int *fake)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002021{
2022 unsigned long *table;
2023 struct page *page;
2024 int rc;
2025
2026 BUG_ON(!gmap_is_shadow(sg));
2027 spin_lock(&sg->guest_table_lock);
2028 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2029 if (table && !(*table & _SEGMENT_ENTRY_INVALID)) {
2030 /* Shadow page tables are full pages (pte+pgste) */
2031 page = pfn_to_page(*table >> PAGE_SHIFT);
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02002032 *pgt = page->index & ~GMAP_SHADOW_FAKE_TABLE;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002033 *dat_protection = !!(*table & _SEGMENT_ENTRY_PROTECT);
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02002034 *fake = !!(page->index & GMAP_SHADOW_FAKE_TABLE);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002035 rc = 0;
2036 } else {
2037 rc = -EAGAIN;
2038 }
2039 spin_unlock(&sg->guest_table_lock);
2040 return rc;
2041
2042}
2043EXPORT_SYMBOL_GPL(gmap_shadow_pgt_lookup);
2044
2045/**
2046 * gmap_shadow_pgt - instantiate a shadow page table
2047 * @sg: pointer to the shadow guest address space structure
2048 * @saddr: faulting address in the shadow gmap
2049 * @pgt: parent gmap address of the page table to get shadowed
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02002050 * @fake: pgt references contiguous guest memory block, not a pgtable
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002051 *
2052 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2053 * shadow table structure is incomplete, -ENOMEM if out of memory,
2054 * -EFAULT if an address in the parent gmap could not be resolved and
2055 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002056 * Called with gmap->mm->mmap_lock in read
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002057 */
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02002058int gmap_shadow_pgt(struct gmap *sg, unsigned long saddr, unsigned long pgt,
2059 int fake)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002060{
2061 unsigned long raddr, origin;
Nico Boehr079f0c22022-10-20 16:31:55 +02002062 unsigned long *table;
Qi Zheng57b77b72024-03-04 19:07:20 +08002063 struct ptdesc *ptdesc;
Nico Boehr079f0c22022-10-20 16:31:55 +02002064 phys_addr_t s_pgt;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002065 int rc;
2066
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02002067 BUG_ON(!gmap_is_shadow(sg) || (pgt & _SEGMENT_ENTRY_LARGE));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002068 /* Allocate a shadow page table */
Qi Zheng57b77b72024-03-04 19:07:20 +08002069 ptdesc = page_table_alloc_pgste(sg->mm);
2070 if (!ptdesc)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002071 return -ENOMEM;
Qi Zheng57b77b72024-03-04 19:07:20 +08002072 ptdesc->pt_index = pgt & _SEGMENT_ENTRY_ORIGIN;
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02002073 if (fake)
Qi Zheng57b77b72024-03-04 19:07:20 +08002074 ptdesc->pt_index |= GMAP_SHADOW_FAKE_TABLE;
2075 s_pgt = page_to_phys(ptdesc_page(ptdesc));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002076 /* Install shadow page table */
2077 spin_lock(&sg->guest_table_lock);
2078 table = gmap_table_walk(sg, saddr, 1); /* get segment pointer */
2079 if (!table) {
2080 rc = -EAGAIN; /* Race with unshadow */
2081 goto out_free;
2082 }
2083 if (!(*table & _SEGMENT_ENTRY_INVALID)) {
2084 rc = 0; /* Already established */
2085 goto out_free;
David Hildenbrand998f6372016-03-08 12:23:38 +01002086 } else if (*table & _SEGMENT_ENTRY_ORIGIN) {
2087 rc = -EAGAIN; /* Race with shadow */
2088 goto out_free;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002089 }
David Hildenbrand998f6372016-03-08 12:23:38 +01002090 /* mark as invalid as long as the parent table is not protected */
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002091 *table = (unsigned long) s_pgt | _SEGMENT_ENTRY |
David Hildenbrand998f6372016-03-08 12:23:38 +01002092 (pgt & _SEGMENT_ENTRY_PROTECT) | _SEGMENT_ENTRY_INVALID;
Qi Zheng57b77b72024-03-04 19:07:20 +08002093 list_add(&ptdesc->pt_list, &sg->pt_list);
David Hildenbrandfd8d4e32016-04-18 13:24:52 +02002094 if (fake) {
2095 /* nothing to protect for fake tables */
2096 *table &= ~_SEGMENT_ENTRY_INVALID;
2097 spin_unlock(&sg->guest_table_lock);
2098 return 0;
2099 }
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002100 spin_unlock(&sg->guest_table_lock);
2101 /* Make pgt read-only in parent gmap page table (not the pgste) */
Heiko Carstensf1c11742017-07-05 07:37:27 +02002102 raddr = (saddr & _SEGMENT_MASK) | _SHADOW_RMAP_SEGMENT;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002103 origin = pgt & _SEGMENT_ENTRY_ORIGIN & PAGE_MASK;
David Hildenbrand5c528db2018-01-23 22:26:18 +01002104 rc = gmap_protect_rmap(sg, raddr, origin, PAGE_SIZE);
David Hildenbrand998f6372016-03-08 12:23:38 +01002105 spin_lock(&sg->guest_table_lock);
2106 if (!rc) {
2107 table = gmap_table_walk(sg, saddr, 1);
Nico Boehr079f0c22022-10-20 16:31:55 +02002108 if (!table || (*table & _SEGMENT_ENTRY_ORIGIN) != s_pgt)
David Hildenbrand998f6372016-03-08 12:23:38 +01002109 rc = -EAGAIN; /* Race with unshadow */
2110 else
2111 *table &= ~_SEGMENT_ENTRY_INVALID;
2112 } else {
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002113 gmap_unshadow_pgt(sg, raddr);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002114 }
David Hildenbrand998f6372016-03-08 12:23:38 +01002115 spin_unlock(&sg->guest_table_lock);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002116 return rc;
2117out_free:
2118 spin_unlock(&sg->guest_table_lock);
Qi Zheng57b77b72024-03-04 19:07:20 +08002119 page_table_free_pgste(ptdesc);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002120 return rc;
2121
2122}
2123EXPORT_SYMBOL_GPL(gmap_shadow_pgt);
2124
2125/**
2126 * gmap_shadow_page - create a shadow page mapping
2127 * @sg: pointer to the shadow guest address space structure
2128 * @saddr: faulting address in the shadow gmap
David Hildenbranda9d23e72016-03-08 12:21:41 +01002129 * @pte: pte in parent gmap address space to get shadowed
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002130 *
2131 * Returns 0 if successfully shadowed or already shadowed, -EAGAIN if the
2132 * shadow table structure is incomplete, -ENOMEM if out of memory and
2133 * -EFAULT if an address in the parent gmap could not be resolved.
2134 *
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -07002135 * Called with sg->mm->mmap_lock in read.
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002136 */
David Hildenbranda9d23e72016-03-08 12:21:41 +01002137int gmap_shadow_page(struct gmap *sg, unsigned long saddr, pte_t pte)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002138{
2139 struct gmap *parent;
2140 struct gmap_rmap *rmap;
David Hildenbranda9d23e72016-03-08 12:21:41 +01002141 unsigned long vmaddr, paddr;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002142 spinlock_t *ptl;
2143 pte_t *sptep, *tptep;
David Hildenbrand01f719172016-06-13 10:49:04 +02002144 int prot;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002145 int rc;
2146
2147 BUG_ON(!gmap_is_shadow(sg));
2148 parent = sg->parent;
David Hildenbrand01f719172016-06-13 10:49:04 +02002149 prot = (pte_val(pte) & _PAGE_PROTECT) ? PROT_READ : PROT_WRITE;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002150
Christian Borntraeger0cd2a782020-11-09 13:14:35 +01002151 rmap = kzalloc(sizeof(*rmap), GFP_KERNEL_ACCOUNT);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002152 if (!rmap)
2153 return -ENOMEM;
2154 rmap->raddr = (saddr & PAGE_MASK) | _SHADOW_RMAP_PGTABLE;
2155
2156 while (1) {
David Hildenbranda9d23e72016-03-08 12:21:41 +01002157 paddr = pte_val(pte) & PAGE_MASK;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002158 vmaddr = __gmap_translate(parent, paddr);
2159 if (IS_ERR_VALUE(vmaddr)) {
2160 rc = vmaddr;
2161 break;
2162 }
Christian Borntraeger0cd2a782020-11-09 13:14:35 +01002163 rc = radix_tree_preload(GFP_KERNEL_ACCOUNT);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002164 if (rc)
2165 break;
2166 rc = -EAGAIN;
2167 sptep = gmap_pte_op_walk(parent, paddr, &ptl);
2168 if (sptep) {
2169 spin_lock(&sg->guest_table_lock);
2170 /* Get page table pointer */
2171 tptep = (pte_t *) gmap_table_walk(sg, saddr, 0);
2172 if (!tptep) {
2173 spin_unlock(&sg->guest_table_lock);
Hugh Dickinsb2f58942023-06-08 12:29:13 -07002174 gmap_pte_op_end(sptep, ptl);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002175 radix_tree_preload_end();
2176 break;
2177 }
David Hildenbranda9d23e72016-03-08 12:21:41 +01002178 rc = ptep_shadow_pte(sg->mm, saddr, sptep, tptep, pte);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002179 if (rc > 0) {
2180 /* Success and a new mapping */
2181 gmap_insert_rmap(sg, vmaddr, rmap);
2182 rmap = NULL;
2183 rc = 0;
2184 }
Hugh Dickinsb2f58942023-06-08 12:29:13 -07002185 gmap_pte_op_end(sptep, ptl);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002186 spin_unlock(&sg->guest_table_lock);
2187 }
2188 radix_tree_preload_end();
2189 if (!rc)
2190 break;
David Hildenbrand01f719172016-06-13 10:49:04 +02002191 rc = gmap_pte_op_fixup(parent, paddr, vmaddr, prot);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002192 if (rc)
2193 break;
2194 }
2195 kfree(rmap);
2196 return rc;
2197}
2198EXPORT_SYMBOL_GPL(gmap_shadow_page);
2199
Heiko Carstens2e827522021-09-02 19:47:37 +02002200/*
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002201 * gmap_shadow_notify - handle notifications for shadow gmap
2202 *
2203 * Called with sg->parent->shadow_lock.
2204 */
2205static void gmap_shadow_notify(struct gmap *sg, unsigned long vmaddr,
Janosch Frankc0b4bd22017-12-13 13:53:22 +01002206 unsigned long gaddr)
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002207{
2208 struct gmap_rmap *rmap, *rnext, *head;
Janosch Frank2fa5ed72017-02-08 08:59:56 +01002209 unsigned long start, end, bits, raddr;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002210
2211 BUG_ON(!gmap_is_shadow(sg));
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002212
2213 spin_lock(&sg->guest_table_lock);
2214 if (sg->removed) {
2215 spin_unlock(&sg->guest_table_lock);
2216 return;
2217 }
2218 /* Check for top level table */
2219 start = sg->orig_asce & _ASCE_ORIGIN;
Heiko Carstensf1c11742017-07-05 07:37:27 +02002220 end = start + ((sg->orig_asce & _ASCE_TABLE_LENGTH) + 1) * PAGE_SIZE;
David Hildenbrand3218f702016-04-18 16:22:24 +02002221 if (!(sg->orig_asce & _ASCE_REAL_SPACE) && gaddr >= start &&
2222 gaddr < end) {
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002223 /* The complete shadow table has to go */
2224 gmap_unshadow(sg);
2225 spin_unlock(&sg->guest_table_lock);
2226 list_del(&sg->list);
2227 gmap_put(sg);
2228 return;
2229 }
2230 /* Remove the page table tree from on specific entry */
Heiko Carstensf1c11742017-07-05 07:37:27 +02002231 head = radix_tree_delete(&sg->host_to_rmap, vmaddr >> PAGE_SHIFT);
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002232 gmap_for_each_rmap_safe(rmap, rnext, head) {
2233 bits = rmap->raddr & _SHADOW_RMAP_MASK;
2234 raddr = rmap->raddr ^ bits;
2235 switch (bits) {
2236 case _SHADOW_RMAP_REGION1:
2237 gmap_unshadow_r2t(sg, raddr);
2238 break;
2239 case _SHADOW_RMAP_REGION2:
2240 gmap_unshadow_r3t(sg, raddr);
2241 break;
2242 case _SHADOW_RMAP_REGION3:
2243 gmap_unshadow_sgt(sg, raddr);
2244 break;
2245 case _SHADOW_RMAP_SEGMENT:
2246 gmap_unshadow_pgt(sg, raddr);
2247 break;
2248 case _SHADOW_RMAP_PGTABLE:
2249 gmap_unshadow_page(sg, raddr);
2250 break;
2251 }
2252 kfree(rmap);
2253 }
2254 spin_unlock(&sg->guest_table_lock);
2255}
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002256
2257/**
2258 * ptep_notify - call all invalidation callbacks for a specific pte.
2259 * @mm: pointer to the process mm_struct
Heiko Carstens2e827522021-09-02 19:47:37 +02002260 * @vmaddr: virtual address in the process address space
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002261 * @pte: pointer to the page table entry
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002262 * @bits: bits from the pgste that caused the notify call
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002263 *
2264 * This function is assumed to be called with the page table lock held
2265 * for the pte to notify.
2266 */
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002267void ptep_notify(struct mm_struct *mm, unsigned long vmaddr,
2268 pte_t *pte, unsigned long bits)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002269{
Janosch Frank2fa5ed72017-02-08 08:59:56 +01002270 unsigned long offset, gaddr = 0;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002271 unsigned long *table;
Martin Schwidefsky4be130a2016-03-08 12:12:18 +01002272 struct gmap *gmap, *sg, *next;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002273
2274 offset = ((unsigned long) pte) & (255 * sizeof(pte_t));
Heiko Carstensf1c11742017-07-05 07:37:27 +02002275 offset = offset * (PAGE_SIZE / sizeof(pte_t));
Martin Schwidefsky8ecb1a52016-03-08 11:54:14 +01002276 rcu_read_lock();
2277 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2278 spin_lock(&gmap->guest_table_lock);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002279 table = radix_tree_lookup(&gmap->host_to_guest,
2280 vmaddr >> PMD_SHIFT);
Martin Schwidefsky8ecb1a52016-03-08 11:54:14 +01002281 if (table)
2282 gaddr = __gmap_segment_gaddr(table) + offset;
2283 spin_unlock(&gmap->guest_table_lock);
Janosch Frank2fa5ed72017-02-08 08:59:56 +01002284 if (!table)
2285 continue;
2286
2287 if (!list_empty(&gmap->children) && (bits & PGSTE_VSIE_BIT)) {
2288 spin_lock(&gmap->shadow_lock);
2289 list_for_each_entry_safe(sg, next,
2290 &gmap->children, list)
Janosch Frankc0b4bd22017-12-13 13:53:22 +01002291 gmap_shadow_notify(sg, vmaddr, gaddr);
Janosch Frank2fa5ed72017-02-08 08:59:56 +01002292 spin_unlock(&gmap->shadow_lock);
2293 }
2294 if (bits & PGSTE_IN_BIT)
Martin Schwidefsky8ecb1a52016-03-08 11:54:14 +01002295 gmap_call_notifier(gmap, gaddr, gaddr + PAGE_SIZE - 1);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002296 }
Martin Schwidefsky8ecb1a52016-03-08 11:54:14 +01002297 rcu_read_unlock();
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002298}
2299EXPORT_SYMBOL_GPL(ptep_notify);
2300
Janosch Frank6a376272018-07-13 11:28:22 +01002301static void pmdp_notify_gmap(struct gmap *gmap, pmd_t *pmdp,
2302 unsigned long gaddr)
2303{
Heiko Carstensb8e3b372022-02-21 20:50:07 +01002304 set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_IN)));
Janosch Frank6a376272018-07-13 11:28:22 +01002305 gmap_call_notifier(gmap, gaddr, gaddr + HPAGE_SIZE - 1);
2306}
2307
Janosch Frank0959e162018-07-17 13:21:22 +01002308/**
2309 * gmap_pmdp_xchg - exchange a gmap pmd with another
2310 * @gmap: pointer to the guest address space structure
2311 * @pmdp: pointer to the pmd entry
2312 * @new: replacement entry
2313 * @gaddr: the affected guest address
2314 *
2315 * This function is assumed to be called with the guest_table_lock
2316 * held.
2317 */
2318static void gmap_pmdp_xchg(struct gmap *gmap, pmd_t *pmdp, pmd_t new,
2319 unsigned long gaddr)
2320{
2321 gaddr &= HPAGE_MASK;
2322 pmdp_notify_gmap(gmap, pmdp, gaddr);
Heiko Carstense1fc74f2022-02-21 21:25:09 +01002323 new = clear_pmd_bit(new, __pgprot(_SEGMENT_ENTRY_GMAP_IN));
Janosch Frank0959e162018-07-17 13:21:22 +01002324 if (MACHINE_HAS_TLB_GUEST)
2325 __pmdp_idte(gaddr, (pmd_t *)pmdp, IDTE_GUEST_ASCE, gmap->asce,
2326 IDTE_GLOBAL);
2327 else if (MACHINE_HAS_IDTE)
2328 __pmdp_idte(gaddr, (pmd_t *)pmdp, 0, 0, IDTE_GLOBAL);
2329 else
2330 __pmdp_csp(pmdp);
Heiko Carstensb8e3b372022-02-21 20:50:07 +01002331 set_pmd(pmdp, new);
Janosch Frank0959e162018-07-17 13:21:22 +01002332}
2333
Janosch Frank6a376272018-07-13 11:28:22 +01002334static void gmap_pmdp_clear(struct mm_struct *mm, unsigned long vmaddr,
2335 int purge)
2336{
2337 pmd_t *pmdp;
2338 struct gmap *gmap;
2339 unsigned long gaddr;
2340
2341 rcu_read_lock();
2342 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2343 spin_lock(&gmap->guest_table_lock);
2344 pmdp = (pmd_t *)radix_tree_delete(&gmap->host_to_guest,
2345 vmaddr >> PMD_SHIFT);
2346 if (pmdp) {
2347 gaddr = __gmap_segment_gaddr((unsigned long *)pmdp);
2348 pmdp_notify_gmap(gmap, pmdp, gaddr);
Janosch Frank0959e162018-07-17 13:21:22 +01002349 WARN_ON(pmd_val(*pmdp) & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2350 _SEGMENT_ENTRY_GMAP_UC));
Janosch Frank6a376272018-07-13 11:28:22 +01002351 if (purge)
2352 __pmdp_csp(pmdp);
Heiko Carstensb8e3b372022-02-21 20:50:07 +01002353 set_pmd(pmdp, __pmd(_SEGMENT_ENTRY_EMPTY));
Janosch Frank6a376272018-07-13 11:28:22 +01002354 }
2355 spin_unlock(&gmap->guest_table_lock);
2356 }
2357 rcu_read_unlock();
2358}
2359
2360/**
2361 * gmap_pmdp_invalidate - invalidate all affected guest pmd entries without
2362 * flushing
2363 * @mm: pointer to the process mm_struct
2364 * @vmaddr: virtual address in the process address space
2365 */
2366void gmap_pmdp_invalidate(struct mm_struct *mm, unsigned long vmaddr)
2367{
2368 gmap_pmdp_clear(mm, vmaddr, 0);
2369}
2370EXPORT_SYMBOL_GPL(gmap_pmdp_invalidate);
2371
2372/**
2373 * gmap_pmdp_csp - csp all affected guest pmd entries
2374 * @mm: pointer to the process mm_struct
2375 * @vmaddr: virtual address in the process address space
2376 */
2377void gmap_pmdp_csp(struct mm_struct *mm, unsigned long vmaddr)
2378{
2379 gmap_pmdp_clear(mm, vmaddr, 1);
2380}
2381EXPORT_SYMBOL_GPL(gmap_pmdp_csp);
2382
2383/**
2384 * gmap_pmdp_idte_local - invalidate and clear a guest pmd entry
2385 * @mm: pointer to the process mm_struct
2386 * @vmaddr: virtual address in the process address space
2387 */
2388void gmap_pmdp_idte_local(struct mm_struct *mm, unsigned long vmaddr)
2389{
2390 unsigned long *entry, gaddr;
2391 struct gmap *gmap;
2392 pmd_t *pmdp;
2393
2394 rcu_read_lock();
2395 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2396 spin_lock(&gmap->guest_table_lock);
2397 entry = radix_tree_delete(&gmap->host_to_guest,
2398 vmaddr >> PMD_SHIFT);
2399 if (entry) {
2400 pmdp = (pmd_t *)entry;
2401 gaddr = __gmap_segment_gaddr(entry);
2402 pmdp_notify_gmap(gmap, pmdp, gaddr);
Janosch Frank0959e162018-07-17 13:21:22 +01002403 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2404 _SEGMENT_ENTRY_GMAP_UC));
Janosch Frank6a376272018-07-13 11:28:22 +01002405 if (MACHINE_HAS_TLB_GUEST)
2406 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2407 gmap->asce, IDTE_LOCAL);
2408 else if (MACHINE_HAS_IDTE)
2409 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_LOCAL);
2410 *entry = _SEGMENT_ENTRY_EMPTY;
2411 }
2412 spin_unlock(&gmap->guest_table_lock);
2413 }
2414 rcu_read_unlock();
2415}
2416EXPORT_SYMBOL_GPL(gmap_pmdp_idte_local);
2417
2418/**
2419 * gmap_pmdp_idte_global - invalidate and clear a guest pmd entry
2420 * @mm: pointer to the process mm_struct
2421 * @vmaddr: virtual address in the process address space
2422 */
2423void gmap_pmdp_idte_global(struct mm_struct *mm, unsigned long vmaddr)
2424{
2425 unsigned long *entry, gaddr;
2426 struct gmap *gmap;
2427 pmd_t *pmdp;
2428
2429 rcu_read_lock();
2430 list_for_each_entry_rcu(gmap, &mm->context.gmap_list, list) {
2431 spin_lock(&gmap->guest_table_lock);
2432 entry = radix_tree_delete(&gmap->host_to_guest,
2433 vmaddr >> PMD_SHIFT);
2434 if (entry) {
2435 pmdp = (pmd_t *)entry;
2436 gaddr = __gmap_segment_gaddr(entry);
2437 pmdp_notify_gmap(gmap, pmdp, gaddr);
Janosch Frank0959e162018-07-17 13:21:22 +01002438 WARN_ON(*entry & ~(_SEGMENT_ENTRY_HARDWARE_BITS_LARGE |
2439 _SEGMENT_ENTRY_GMAP_UC));
Janosch Frank6a376272018-07-13 11:28:22 +01002440 if (MACHINE_HAS_TLB_GUEST)
2441 __pmdp_idte(gaddr, pmdp, IDTE_GUEST_ASCE,
2442 gmap->asce, IDTE_GLOBAL);
2443 else if (MACHINE_HAS_IDTE)
2444 __pmdp_idte(gaddr, pmdp, 0, 0, IDTE_GLOBAL);
2445 else
2446 __pmdp_csp(pmdp);
2447 *entry = _SEGMENT_ENTRY_EMPTY;
2448 }
2449 spin_unlock(&gmap->guest_table_lock);
2450 }
2451 rcu_read_unlock();
2452}
2453EXPORT_SYMBOL_GPL(gmap_pmdp_idte_global);
2454
Janosch Frank0959e162018-07-17 13:21:22 +01002455/**
2456 * gmap_test_and_clear_dirty_pmd - test and reset segment dirty status
2457 * @gmap: pointer to guest address space
2458 * @pmdp: pointer to the pmd to be tested
2459 * @gaddr: virtual address in the guest address space
2460 *
2461 * This function is assumed to be called with the guest_table_lock
2462 * held.
2463 */
Vasily Gorbikffbd2682019-07-17 19:41:09 +02002464static bool gmap_test_and_clear_dirty_pmd(struct gmap *gmap, pmd_t *pmdp,
2465 unsigned long gaddr)
Janosch Frank0959e162018-07-17 13:21:22 +01002466{
2467 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_INVALID)
2468 return false;
2469
2470 /* Already protected memory, which did not change is clean */
2471 if (pmd_val(*pmdp) & _SEGMENT_ENTRY_PROTECT &&
2472 !(pmd_val(*pmdp) & _SEGMENT_ENTRY_GMAP_UC))
2473 return false;
2474
2475 /* Clear UC indication and reset protection */
Heiko Carstensb8e3b372022-02-21 20:50:07 +01002476 set_pmd(pmdp, clear_pmd_bit(*pmdp, __pgprot(_SEGMENT_ENTRY_GMAP_UC)));
Janosch Frank0959e162018-07-17 13:21:22 +01002477 gmap_protect_pmd(gmap, gaddr, pmdp, PROT_READ, 0);
2478 return true;
2479}
2480
2481/**
2482 * gmap_sync_dirty_log_pmd - set bitmap based on dirty status of segment
2483 * @gmap: pointer to guest address space
2484 * @bitmap: dirty bitmap for this pmd
2485 * @gaddr: virtual address in the guest address space
2486 * @vmaddr: virtual address in the host address space
2487 *
2488 * This function is assumed to be called with the guest_table_lock
2489 * held.
2490 */
2491void gmap_sync_dirty_log_pmd(struct gmap *gmap, unsigned long bitmap[4],
2492 unsigned long gaddr, unsigned long vmaddr)
2493{
2494 int i;
2495 pmd_t *pmdp;
2496 pte_t *ptep;
2497 spinlock_t *ptl;
2498
2499 pmdp = gmap_pmd_op_walk(gmap, gaddr);
2500 if (!pmdp)
2501 return;
2502
Peter Xu2f709f72024-03-05 12:37:47 +08002503 if (pmd_leaf(*pmdp)) {
Janosch Frank0959e162018-07-17 13:21:22 +01002504 if (gmap_test_and_clear_dirty_pmd(gmap, pmdp, gaddr))
2505 bitmap_fill(bitmap, _PAGE_ENTRIES);
2506 } else {
2507 for (i = 0; i < _PAGE_ENTRIES; i++, vmaddr += PAGE_SIZE) {
2508 ptep = pte_alloc_map_lock(gmap->mm, pmdp, vmaddr, &ptl);
2509 if (!ptep)
2510 continue;
2511 if (ptep_test_and_clear_uc(gmap->mm, vmaddr, ptep))
2512 set_bit(i, bitmap);
Hugh Dickinsb2f58942023-06-08 12:29:13 -07002513 pte_unmap_unlock(ptep, ptl);
Janosch Frank0959e162018-07-17 13:21:22 +01002514 }
2515 }
2516 gmap_pmd_op_end(gmap, pmdp);
2517}
2518EXPORT_SYMBOL_GPL(gmap_sync_dirty_log_pmd);
2519
Gerald Schaeferba925fa2020-07-29 22:22:34 +02002520#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2521static int thp_split_walk_pmd_entry(pmd_t *pmd, unsigned long addr,
2522 unsigned long end, struct mm_walk *walk)
2523{
2524 struct vm_area_struct *vma = walk->vma;
2525
2526 split_huge_pmd(vma, pmd, addr);
2527 return 0;
2528}
2529
2530static const struct mm_walk_ops thp_split_walk_ops = {
2531 .pmd_entry = thp_split_walk_pmd_entry,
Suren Baghdasaryan49b06382023-08-04 08:27:19 -07002532 .walk_lock = PGWALK_WRLOCK_VERIFY,
Gerald Schaeferba925fa2020-07-29 22:22:34 +02002533};
2534
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002535static inline void thp_split_mm(struct mm_struct *mm)
2536{
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002537 struct vm_area_struct *vma;
Matthew Wilcox (Oracle)e7b6b992022-09-06 19:48:54 +00002538 VMA_ITERATOR(vmi, mm, 0);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002539
Matthew Wilcox (Oracle)e7b6b992022-09-06 19:48:54 +00002540 for_each_vma(vmi, vma) {
Suren Baghdasaryan1c712222023-01-26 11:37:49 -08002541 vm_flags_mod(vma, VM_NOHUGEPAGE, VM_HUGEPAGE);
Gerald Schaeferba925fa2020-07-29 22:22:34 +02002542 walk_page_vma(vma, &thp_split_walk_ops, NULL);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002543 }
2544 mm->def_flags |= VM_NOHUGEPAGE;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002545}
Gerald Schaeferba925fa2020-07-29 22:22:34 +02002546#else
2547static inline void thp_split_mm(struct mm_struct *mm)
2548{
2549}
2550#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002551
2552/*
2553 * switch on pgstes for its userspace process (for kvm)
2554 */
2555int s390_enable_sie(void)
2556{
2557 struct mm_struct *mm = current->mm;
2558
2559 /* Do we have pgstes? if yes, we are done */
2560 if (mm_has_pgste(mm))
2561 return 0;
2562 /* Fail if the page tables are 2K */
2563 if (!mm_alloc_pgste(mm))
2564 return -EINVAL;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002565 mmap_write_lock(mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002566 mm->context.has_pgste = 1;
2567 /* split thp mappings and disable thp for future mappings */
2568 thp_split_mm(mm);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002569 mmap_write_unlock(mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002570 return 0;
2571}
2572EXPORT_SYMBOL_GPL(s390_enable_sie);
2573
David Hildenbrand06201e02024-04-11 18:14:41 +02002574static int find_zeropage_pte_entry(pte_t *pte, unsigned long addr,
2575 unsigned long end, struct mm_walk *walk)
Janosch Frankfa0c5ea2019-07-16 13:08:37 +02002576{
David Hildenbrand06201e02024-04-11 18:14:41 +02002577 unsigned long *found_addr = walk->private;
2578
2579 /* Return 1 of the page is a zeropage. */
2580 if (is_zero_pfn(pte_pfn(*pte))) {
2581 /*
2582 * Shared zeropage in e.g., a FS DAX mapping? We cannot do the
2583 * right thing and likely don't care: FAULT_FLAG_UNSHARE
2584 * currently only works in COW mappings, which is also where
2585 * mm_forbids_zeropage() is checked.
2586 */
2587 if (!is_cow_mapping(walk->vma->vm_flags))
2588 return -EFAULT;
2589
2590 *found_addr = addr;
2591 return 1;
2592 }
2593 return 0;
2594}
2595
2596static const struct mm_walk_ops find_zeropage_ops = {
2597 .pte_entry = find_zeropage_pte_entry,
2598 .walk_lock = PGWALK_WRLOCK,
2599};
2600
2601/*
2602 * Unshare all shared zeropages, replacing them by anonymous pages. Note that
2603 * we cannot simply zap all shared zeropages, because this could later
2604 * trigger unexpected userfaultfd missing events.
2605 *
2606 * This must be called after mm->context.allow_cow_sharing was
2607 * set to 0, to avoid future mappings of shared zeropages.
2608 *
2609 * mm contracts with s390, that even if mm were to remove a page table,
2610 * and racing with walk_page_range_vma() calling pte_offset_map_lock()
2611 * would fail, it will never insert a page table containing empty zero
2612 * pages once mm_forbids_zeropage(mm) i.e.
2613 * mm->context.allow_cow_sharing is set to 0.
2614 */
2615static int __s390_unshare_zeropages(struct mm_struct *mm)
2616{
2617 struct vm_area_struct *vma;
2618 VMA_ITERATOR(vmi, mm, 0);
2619 unsigned long addr;
2620 vm_fault_t fault;
2621 int rc;
2622
2623 for_each_vma(vmi, vma) {
2624 /*
2625 * We could only look at COW mappings, but it's more future
2626 * proof to catch unexpected zeropages in other mappings and
2627 * fail.
2628 */
2629 if ((vma->vm_flags & VM_PFNMAP) || is_vm_hugetlb_page(vma))
2630 continue;
2631 addr = vma->vm_start;
2632
2633retry:
2634 rc = walk_page_range_vma(vma, addr, vma->vm_end,
2635 &find_zeropage_ops, &addr);
2636 if (rc < 0)
2637 return rc;
2638 else if (!rc)
2639 continue;
2640
2641 /* addr was updated by find_zeropage_pte_entry() */
2642 fault = handle_mm_fault(vma, addr,
2643 FAULT_FLAG_UNSHARE | FAULT_FLAG_REMOTE,
2644 NULL);
2645 if (fault & VM_FAULT_OOM)
2646 return -ENOMEM;
2647 /*
2648 * See break_ksm(): even after handle_mm_fault() returned 0, we
2649 * must start the lookup from the current address, because
2650 * handle_mm_fault() may back out if there's any difficulty.
2651 *
2652 * VM_FAULT_SIGBUS and VM_FAULT_SIGSEGV are unexpected but
2653 * maybe they could trigger in the future on concurrent
2654 * truncation. In that case, the shared zeropage would be gone
2655 * and we can simply retry and make progress.
2656 */
2657 cond_resched();
2658 goto retry;
2659 }
2660
2661 return 0;
2662}
2663
2664static int __s390_disable_cow_sharing(struct mm_struct *mm)
2665{
2666 int rc;
2667
2668 if (!mm->context.allow_cow_sharing)
2669 return 0;
2670
2671 mm->context.allow_cow_sharing = 0;
2672
2673 /* Replace all shared zeropages by anonymous pages. */
2674 rc = __s390_unshare_zeropages(mm);
Stefan Roeschd7597f52023-04-17 22:13:40 -07002675 /*
2676 * Make sure to disable KSM (if enabled for the whole process or
2677 * individual VMAs). Note that nothing currently hinders user space
2678 * from re-enabling it.
2679 */
David Hildenbrand06201e02024-04-11 18:14:41 +02002680 if (!rc)
2681 rc = ksm_disable(mm);
2682 if (rc)
2683 mm->context.allow_cow_sharing = 1;
2684 return rc;
Janosch Frankfa0c5ea2019-07-16 13:08:37 +02002685}
David Hildenbrand06201e02024-04-11 18:14:41 +02002686
2687/*
2688 * Disable most COW-sharing of memory pages for the whole process:
2689 * (1) Disable KSM and unmerge/unshare any KSM pages.
2690 * (2) Disallow shared zeropages and unshare any zerpages that are mapped.
2691 *
2692 * Not that we currently don't bother with COW-shared pages that are shared
2693 * with parent/child processes due to fork().
2694 */
2695int s390_disable_cow_sharing(void)
2696{
2697 int rc;
2698
2699 mmap_write_lock(current->mm);
2700 rc = __s390_disable_cow_sharing(current->mm);
2701 mmap_write_unlock(current->mm);
2702 return rc;
2703}
2704EXPORT_SYMBOL_GPL(s390_disable_cow_sharing);
Janosch Frankfa0c5ea2019-07-16 13:08:37 +02002705
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002706/*
2707 * Enable storage key handling from now on and initialize the storage
2708 * keys with the default key.
2709 */
Dominik Dingel964c2c02018-07-13 11:28:25 +01002710static int __s390_enable_skey_pte(pte_t *pte, unsigned long addr,
2711 unsigned long next, struct mm_walk *walk)
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002712{
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002713 /* Clear storage key */
2714 ptep_zap_key(walk->mm, addr, pte);
2715 return 0;
2716}
2717
Christian Borntraeger6d5946272022-05-30 11:27:05 +02002718/*
2719 * Give a chance to schedule after setting a key to 256 pages.
2720 * We only hold the mm lock, which is a rwsem and the kvm srcu.
2721 * Both can sleep.
2722 */
2723static int __s390_enable_skey_pmd(pmd_t *pmd, unsigned long addr,
2724 unsigned long next, struct mm_walk *walk)
2725{
2726 cond_resched();
2727 return 0;
2728}
2729
Dominik Dingel964c2c02018-07-13 11:28:25 +01002730static int __s390_enable_skey_hugetlb(pte_t *pte, unsigned long addr,
2731 unsigned long hmask, unsigned long next,
2732 struct mm_walk *walk)
2733{
2734 pmd_t *pmd = (pmd_t *)pte;
2735 unsigned long start, end;
David Hildenbrand1433b362024-05-08 20:29:55 +02002736 struct folio *folio = page_folio(pmd_page(*pmd));
Dominik Dingel964c2c02018-07-13 11:28:25 +01002737
2738 /*
2739 * The write check makes sure we do not set a key on shared
2740 * memory. This is needed as the walker does not differentiate
2741 * between actual guest memory and the process executable or
2742 * shared libraries.
2743 */
2744 if (pmd_val(*pmd) & _SEGMENT_ENTRY_INVALID ||
2745 !(pmd_val(*pmd) & _SEGMENT_ENTRY_WRITE))
2746 return 0;
2747
2748 start = pmd_val(*pmd) & HPAGE_MASK;
Claudio Imbrenda843c3282024-04-16 13:42:19 +02002749 end = start + HPAGE_SIZE;
Dominik Dingel964c2c02018-07-13 11:28:25 +01002750 __storage_key_init_range(start, end);
David Hildenbrand1433b362024-05-08 20:29:55 +02002751 set_bit(PG_arch_1, &folio->flags);
Christian Borntraeger6d5946272022-05-30 11:27:05 +02002752 cond_resched();
Dominik Dingel964c2c02018-07-13 11:28:25 +01002753 return 0;
2754}
2755
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002756static const struct mm_walk_ops enable_skey_walk_ops = {
2757 .hugetlb_entry = __s390_enable_skey_hugetlb,
2758 .pte_entry = __s390_enable_skey_pte,
Christian Borntraeger6d5946272022-05-30 11:27:05 +02002759 .pmd_entry = __s390_enable_skey_pmd,
Suren Baghdasaryan49b06382023-08-04 08:27:19 -07002760 .walk_lock = PGWALK_WRLOCK,
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002761};
2762
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002763int s390_enable_skey(void)
2764{
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002765 struct mm_struct *mm = current->mm;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002766 int rc = 0;
2767
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002768 mmap_write_lock(mm);
Janosch Frank55531b72018-02-15 16:33:47 +01002769 if (mm_uses_skeys(mm))
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002770 goto out_up;
2771
Janosch Frank55531b72018-02-15 16:33:47 +01002772 mm->context.uses_skeys = 1;
David Hildenbrand06201e02024-04-11 18:14:41 +02002773 rc = __s390_disable_cow_sharing(mm);
Janosch Frankfa0c5ea2019-07-16 13:08:37 +02002774 if (rc) {
2775 mm->context.uses_skeys = 0;
2776 goto out_up;
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002777 }
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002778 walk_page_range(mm, 0, TASK_SIZE, &enable_skey_walk_ops, NULL);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002779
2780out_up:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002781 mmap_write_unlock(mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002782 return rc;
2783}
2784EXPORT_SYMBOL_GPL(s390_enable_skey);
2785
2786/*
2787 * Reset CMMA state, make all pages stable again.
2788 */
2789static int __s390_reset_cmma(pte_t *pte, unsigned long addr,
2790 unsigned long next, struct mm_walk *walk)
2791{
2792 ptep_zap_unused(walk->mm, addr, pte, 1);
2793 return 0;
2794}
2795
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002796static const struct mm_walk_ops reset_cmma_walk_ops = {
2797 .pte_entry = __s390_reset_cmma,
Suren Baghdasaryan49b06382023-08-04 08:27:19 -07002798 .walk_lock = PGWALK_WRLOCK,
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002799};
2800
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002801void s390_reset_cmma(struct mm_struct *mm)
2802{
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002803 mmap_write_lock(mm);
Christoph Hellwig7b86ac32019-08-28 16:19:54 +02002804 walk_page_range(mm, 0, TASK_SIZE, &reset_cmma_walk_ops, NULL);
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07002805 mmap_write_unlock(mm);
Martin Schwidefsky1e133ab2016-03-08 11:49:57 +01002806}
2807EXPORT_SYMBOL_GPL(s390_reset_cmma);
Christian Borntraeger12748002019-12-16 10:48:11 -05002808
Claudio Imbrenda6f735172022-06-28 15:56:05 +02002809#define GATHER_GET_PAGES 32
Christian Borntraeger12748002019-12-16 10:48:11 -05002810
Claudio Imbrenda6f735172022-06-28 15:56:05 +02002811struct reset_walk_state {
2812 unsigned long next;
2813 unsigned long count;
2814 unsigned long pfns[GATHER_GET_PAGES];
Christian Borntraeger12748002019-12-16 10:48:11 -05002815};
2816
Claudio Imbrenda6f735172022-06-28 15:56:05 +02002817static int s390_gather_pages(pte_t *ptep, unsigned long addr,
2818 unsigned long next, struct mm_walk *walk)
Christian Borntraeger12748002019-12-16 10:48:11 -05002819{
Claudio Imbrenda6f735172022-06-28 15:56:05 +02002820 struct reset_walk_state *p = walk->private;
2821 pte_t pte = READ_ONCE(*ptep);
2822
2823 if (pte_present(pte)) {
2824 /* we have a reference from the mapping, take an extra one */
2825 get_page(phys_to_page(pte_val(pte)));
2826 p->pfns[p->count] = phys_to_pfn(pte_val(pte));
2827 p->next = next;
2828 p->count++;
2829 }
2830 return p->count >= GATHER_GET_PAGES;
Christian Borntraeger12748002019-12-16 10:48:11 -05002831}
Claudio Imbrenda6f735172022-06-28 15:56:05 +02002832
2833static const struct mm_walk_ops gather_pages_ops = {
2834 .pte_entry = s390_gather_pages,
Suren Baghdasaryan49b06382023-08-04 08:27:19 -07002835 .walk_lock = PGWALK_RDLOCK,
Claudio Imbrenda6f735172022-06-28 15:56:05 +02002836};
2837
2838/*
2839 * Call the Destroy secure page UVC on each page in the given array of PFNs.
2840 * Each page needs to have an extra reference, which will be released here.
2841 */
2842void s390_uv_destroy_pfns(unsigned long count, unsigned long *pfns)
2843{
David Hildenbrand70631502024-05-08 20:29:52 +02002844 struct folio *folio;
Claudio Imbrenda6f735172022-06-28 15:56:05 +02002845 unsigned long i;
2846
2847 for (i = 0; i < count; i++) {
David Hildenbrand70631502024-05-08 20:29:52 +02002848 folio = pfn_folio(pfns[i]);
Claudio Imbrenda6f735172022-06-28 15:56:05 +02002849 /* we always have an extra reference */
David Hildenbrand70631502024-05-08 20:29:52 +02002850 uv_destroy_folio(folio);
Claudio Imbrenda6f735172022-06-28 15:56:05 +02002851 /* get rid of the extra reference */
David Hildenbrand70631502024-05-08 20:29:52 +02002852 folio_put(folio);
Claudio Imbrenda6f735172022-06-28 15:56:05 +02002853 cond_resched();
2854 }
2855}
2856EXPORT_SYMBOL_GPL(s390_uv_destroy_pfns);
2857
2858/**
2859 * __s390_uv_destroy_range - Call the destroy secure page UVC on each page
2860 * in the given range of the given address space.
2861 * @mm: the mm to operate on
2862 * @start: the start of the range
2863 * @end: the end of the range
2864 * @interruptible: if not 0, stop when a fatal signal is received
2865 *
2866 * Walk the given range of the given address space and call the destroy
2867 * secure page UVC on each page. Optionally exit early if a fatal signal is
2868 * pending.
2869 *
2870 * Return: 0 on success, -EINTR if the function stopped before completing
2871 */
2872int __s390_uv_destroy_range(struct mm_struct *mm, unsigned long start,
2873 unsigned long end, bool interruptible)
2874{
2875 struct reset_walk_state state = { .next = start };
2876 int r = 1;
2877
2878 while (r > 0) {
2879 state.count = 0;
2880 mmap_read_lock(mm);
2881 r = walk_page_range(mm, state.next, end, &gather_pages_ops, &state);
2882 mmap_read_unlock(mm);
2883 cond_resched();
2884 s390_uv_destroy_pfns(state.count, state.pfns);
2885 if (interruptible && fatal_signal_pending(current))
2886 return -EINTR;
2887 }
2888 return 0;
2889}
2890EXPORT_SYMBOL_GPL(__s390_uv_destroy_range);
Claudio Imbrendafaa2f722022-06-28 15:56:02 +02002891
2892/**
2893 * s390_unlist_old_asce - Remove the topmost level of page tables from the
2894 * list of page tables of the gmap.
2895 * @gmap: the gmap whose table is to be removed
2896 *
2897 * On s390x, KVM keeps a list of all pages containing the page tables of the
2898 * gmap (the CRST list). This list is used at tear down time to free all
2899 * pages that are now not needed anymore.
2900 *
2901 * This function removes the topmost page of the tree (the one pointed to by
2902 * the ASCE) from the CRST list.
2903 *
2904 * This means that it will not be freed when the VM is torn down, and needs
2905 * to be handled separately by the caller, unless a leak is actually
2906 * intended. Notice that this function will only remove the page from the
2907 * list, the page will still be used as a top level page table (and ASCE).
2908 */
2909void s390_unlist_old_asce(struct gmap *gmap)
2910{
2911 struct page *old;
2912
2913 old = virt_to_page(gmap->table);
2914 spin_lock(&gmap->guest_table_lock);
2915 list_del(&old->lru);
2916 /*
2917 * Sometimes the topmost page might need to be "removed" multiple
2918 * times, for example if the VM is rebooted into secure mode several
2919 * times concurrently, or if s390_replace_asce fails after calling
2920 * s390_remove_old_asce and is attempted again later. In that case
2921 * the old asce has been removed from the list, and therefore it
2922 * will not be freed when the VM terminates, but the ASCE is still
2923 * in use and still pointed to.
2924 * A subsequent call to replace_asce will follow the pointer and try
2925 * to remove the same page from the list again.
2926 * Therefore it's necessary that the page of the ASCE has valid
2927 * pointers, so list_del can work (and do nothing) without
2928 * dereferencing stale or invalid pointers.
2929 */
2930 INIT_LIST_HEAD(&old->lru);
2931 spin_unlock(&gmap->guest_table_lock);
2932}
2933EXPORT_SYMBOL_GPL(s390_unlist_old_asce);
2934
2935/**
2936 * s390_replace_asce - Try to replace the current ASCE of a gmap with a copy
2937 * @gmap: the gmap whose ASCE needs to be replaced
2938 *
Claudio Imbrenda292a7d62023-04-21 10:50:36 +02002939 * If the ASCE is a SEGMENT type then this function will return -EINVAL,
2940 * otherwise the pointers in the host_to_guest radix tree will keep pointing
2941 * to the wrong pages, causing use-after-free and memory corruption.
Claudio Imbrendafaa2f722022-06-28 15:56:02 +02002942 * If the allocation of the new top level page table fails, the ASCE is not
2943 * replaced.
2944 * In any case, the old ASCE is always removed from the gmap CRST list.
2945 * Therefore the caller has to make sure to save a pointer to it
2946 * beforehand, unless a leak is actually intended.
2947 */
2948int s390_replace_asce(struct gmap *gmap)
2949{
2950 unsigned long asce;
2951 struct page *page;
2952 void *table;
2953
2954 s390_unlist_old_asce(gmap);
2955
Claudio Imbrenda292a7d62023-04-21 10:50:36 +02002956 /* Replacing segment type ASCEs would cause serious issues */
2957 if ((gmap->asce & _ASCE_TYPE_MASK) == _ASCE_TYPE_SEGMENT)
2958 return -EINVAL;
2959
Heiko Carstens1954da42023-10-20 17:26:50 +02002960 page = gmap_alloc_crst();
Claudio Imbrendafaa2f722022-06-28 15:56:02 +02002961 if (!page)
2962 return -ENOMEM;
Claudio Imbrendac2fceb52023-07-05 13:19:37 +02002963 page->index = 0;
Claudio Imbrendafaa2f722022-06-28 15:56:02 +02002964 table = page_to_virt(page);
2965 memcpy(table, gmap->table, 1UL << (CRST_ALLOC_ORDER + PAGE_SHIFT));
2966
2967 /*
2968 * The caller has to deal with the old ASCE, but here we make sure
2969 * the new one is properly added to the CRST list, so that
2970 * it will be freed when the VM is torn down.
2971 */
2972 spin_lock(&gmap->guest_table_lock);
2973 list_add(&page->lru, &gmap->crst_list);
2974 spin_unlock(&gmap->guest_table_lock);
2975
2976 /* Set new table origin while preserving existing ASCE control bits */
2977 asce = (gmap->asce & ~_ASCE_ORIGIN) | __pa(table);
2978 WRITE_ONCE(gmap->asce, asce);
2979 WRITE_ONCE(gmap->mm->context.gmap_asce, asce);
2980 WRITE_ONCE(gmap->table, table);
2981
2982 return 0;
2983}
2984EXPORT_SYMBOL_GPL(s390_replace_asce);