1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Secure pages management: Migration of pages between normal and secure
4 * memory of KVM guests.
5 *
6 * Copyright 2018 Bharata B Rao, IBM Corp. <bharata@linux.ibm.com>
7 */
8
9 /*
10 * A pseries guest can be run as secure guest on Ultravisor-enabled
11 * POWER platforms. On such platforms, this driver will be used to manage
12 * the movement of guest pages between the normal memory managed by
13 * hypervisor (HV) and secure memory managed by Ultravisor (UV).
14 *
15 * The page-in or page-out requests from UV will come to HV as hcalls and
16 * HV will call back into UV via ultracalls to satisfy these page requests.
17 *
18 * Private ZONE_DEVICE memory equal to the amount of secure memory
19 * available in the platform for running secure guests is hotplugged.
20 * Whenever a page belonging to the guest becomes secure, a page from this
21 * private device memory is used to represent and track that secure page
22 * on the HV side. Some pages (like virtio buffers, VPA pages etc) are
23 * shared between UV and HV. However such pages aren't represented by
24 * device private memory and mappings to shared memory exist in both
25 * UV and HV page tables.
26 */
27
28 /*
29 * Notes on locking
30 *
31 * kvm->arch.uvmem_lock is a per-guest lock that prevents concurrent
32 * page-in and page-out requests for the same GPA. Concurrent accesses
33 * can either come via UV (guest vCPUs requesting for same page)
34 * or when HV and guest simultaneously access the same page.
35 * This mutex serializes the migration of page from HV(normal) to
36 * UV(secure) and vice versa. So the serialization points are around
37 * migrate_vma routines and page-in/out routines.
38 *
39 * Per-guest mutex comes with a cost though. Mainly it serializes the
40 * fault path as page-out can occur when HV faults on accessing secure
41 * guest pages. Currently UV issues page-in requests for all the guest
42 * PFNs one at a time during early boot (UV_ESM uvcall), so this is
43 * not a cause for concern. Also currently the number of page-outs caused
44 * by HV touching secure pages is very very low. If an when UV supports
45 * overcommitting, then we might see concurrent guest driven page-outs.
46 *
47 * Locking order
48 *
49 * 1. kvm->srcu - Protects KVM memslots
50 * 2. kvm->mm->mmap_lock - find_vma, migrate_vma_pages and helpers, ksm_madvise
51 * 3. kvm->arch.uvmem_lock - protects read/writes to uvmem slots thus acting
52 * as sync-points for page-in/out
53 */
54
55 /*
56 * Notes on page size
57 *
58 * Currently UV uses 2MB mappings internally, but will issue H_SVM_PAGE_IN
59 * and H_SVM_PAGE_OUT hcalls in PAGE_SIZE(64K) granularity. HV tracks
60 * secure GPAs at 64K page size and maintains one device PFN for each
61 * 64K secure GPA. UV_PAGE_IN and UV_PAGE_OUT calls by HV are also issued
62 * for 64K page at a time.
63 *
64 * HV faulting on secure pages: When HV touches any secure page, it
65 * faults and issues a UV_PAGE_OUT request with 64K page size. Currently
66 * UV splits and remaps the 2MB page if necessary and copies out the
67 * required 64K page contents.
68 *
69 * Shared pages: Whenever guest shares a secure page, UV will split and
70 * remap the 2MB page if required and issue H_SVM_PAGE_IN with 64K page size.
71 *
72 * HV invalidating a page: When a regular page belonging to secure
73 * guest gets unmapped, HV informs UV with UV_PAGE_INVAL of 64K
74 * page size. Using 64K page size is correct here because any non-secure
75 * page will essentially be of 64K page size. Splitting by UV during sharing
76 * and page-out ensures this.
77 *
78 * Page fault handling: When HV handles page fault of a page belonging
79 * to secure guest, it sends that to UV with a 64K UV_PAGE_IN request.
80 * Using 64K size is correct here too as UV would have split the 2MB page
81 * into 64k mappings and would have done page-outs earlier.
82 *
83 * In summary, the current secure pages handling code in HV assumes
84 * 64K page size and in fact fails any page-in/page-out requests of
85 * non-64K size upfront. If and when UV starts supporting multiple
86 * page-sizes, we need to break this assumption.
87 */
88
89 #include <linux/pagemap.h>
90 #include <linux/migrate.h>
91 #include <linux/kvm_host.h>
92 #include <linux/ksm.h>
93 #include <asm/ultravisor.h>
94 #include <asm/mman.h>
95 #include <asm/kvm_ppc.h>
96 #include <asm/kvm_book3s_uvmem.h>
97
98 static struct dev_pagemap kvmppc_uvmem_pgmap;
99 static unsigned long *kvmppc_uvmem_bitmap;
100 static DEFINE_SPINLOCK(kvmppc_uvmem_bitmap_lock);
101
102 /*
103 * States of a GFN
104 * ---------------
105 * The GFN can be in one of the following states.
106 *
107 * (a) Secure - The GFN is secure. The GFN is associated with
108 * a Secure VM, the contents of the GFN is not accessible
109 * to the Hypervisor. This GFN can be backed by a secure-PFN,
110 * or can be backed by a normal-PFN with contents encrypted.
111 * The former is true when the GFN is paged-in into the
112 * ultravisor. The latter is true when the GFN is paged-out
113 * of the ultravisor.
114 *
115 * (b) Shared - The GFN is shared. The GFN is associated with a
116 * a secure VM. The contents of the GFN is accessible to
117 * Hypervisor. This GFN is backed by a normal-PFN and its
118 * content is un-encrypted.
119 *
120 * (c) Normal - The GFN is a normal. The GFN is associated with
121 * a normal VM. The contents of the GFN is accesible to
122 * the Hypervisor. Its content is never encrypted.
123 *
124 * States of a VM.
125 * ---------------
126 *
127 * Normal VM: A VM whose contents are always accessible to
128 * the hypervisor. All its GFNs are normal-GFNs.
129 *
130 * Secure VM: A VM whose contents are not accessible to the
131 * hypervisor without the VM's consent. Its GFNs are
132 * either Shared-GFN or Secure-GFNs.
133 *
134 * Transient VM: A Normal VM that is transitioning to secure VM.
135 * The transition starts on successful return of
136 * H_SVM_INIT_START, and ends on successful return
137 * of H_SVM_INIT_DONE. This transient VM, can have GFNs
138 * in any of the three states; i.e Secure-GFN, Shared-GFN,
139 * and Normal-GFN. The VM never executes in this state
140 * in supervisor-mode.
141 *
142 * Memory slot State.
143 * -----------------------------
144 * The state of a memory slot mirrors the state of the
145 * VM the memory slot is associated with.
146 *
147 * VM State transition.
148 * --------------------
149 *
150 * A VM always starts in Normal Mode.
151 *
152 * H_SVM_INIT_START moves the VM into transient state. During this
153 * time the Ultravisor may request some of its GFNs to be shared or
154 * secured. So its GFNs can be in one of the three GFN states.
155 *
156 * H_SVM_INIT_DONE moves the VM entirely from transient state to
157 * secure-state. At this point any left-over normal-GFNs are
158 * transitioned to Secure-GFN.
159 *
160 * H_SVM_INIT_ABORT moves the transient VM back to normal VM.
161 * All its GFNs are moved to Normal-GFNs.
162 *
163 * UV_TERMINATE transitions the secure-VM back to normal-VM. All
164 * the secure-GFN and shared-GFNs are tranistioned to normal-GFN
165 * Note: The contents of the normal-GFN is undefined at this point.
166 *
167 * GFN state implementation:
168 * -------------------------
169 *
170 * Secure GFN is associated with a secure-PFN; also called uvmem_pfn,
171 * when the GFN is paged-in. Its pfn[] has KVMPPC_GFN_UVMEM_PFN flag
172 * set, and contains the value of the secure-PFN.
173 * It is associated with a normal-PFN; also called mem_pfn, when
174 * the GFN is pagedout. Its pfn[] has KVMPPC_GFN_MEM_PFN flag set.
175 * The value of the normal-PFN is not tracked.
176 *
177 * Shared GFN is associated with a normal-PFN. Its pfn[] has
178 * KVMPPC_UVMEM_SHARED_PFN flag set. The value of the normal-PFN
179 * is not tracked.
180 *
181 * Normal GFN is associated with normal-PFN. Its pfn[] has
182 * no flag set. The value of the normal-PFN is not tracked.
183 *
184 * Life cycle of a GFN
185 * --------------------
186 *
187 * --------------------------------------------------------------
188 * | | Share | Unshare | SVM |H_SVM_INIT_DONE|
189 * | |operation |operation | abort/ | |
190 * | | | | terminate | |
191 * -------------------------------------------------------------
192 * | | | | | |
193 * | Secure | Shared | Secure |Normal |Secure |
194 * | | | | | |
195 * | Shared | Shared | Secure |Normal |Shared |
196 * | | | | | |
197 * | Normal | Shared | Secure |Normal |Secure |
198 * --------------------------------------------------------------
199 *
200 * Life cycle of a VM
201 * --------------------
202 *
203 * --------------------------------------------------------------------
204 * | | start | H_SVM_ |H_SVM_ |H_SVM_ |UV_SVM_ |
205 * | | VM |INIT_START|INIT_DONE|INIT_ABORT |TERMINATE |
206 * | | | | | | |
207 * --------- ----------------------------------------------------------
208 * | | | | | | |
209 * | Normal | Normal | Transient|Error |Error |Normal |
210 * | | | | | | |
211 * | Secure | Error | Error |Error |Error |Normal |
212 * | | | | | | |
213 * |Transient| N/A | Error |Secure |Normal |Normal |
214 * --------------------------------------------------------------------
215 */
216
217 #define KVMPPC_GFN_UVMEM_PFN (1UL << 63)
218 #define KVMPPC_GFN_MEM_PFN (1UL << 62)
219 #define KVMPPC_GFN_SHARED (1UL << 61)
220 #define KVMPPC_GFN_SECURE (KVMPPC_GFN_UVMEM_PFN | KVMPPC_GFN_MEM_PFN)
221 #define KVMPPC_GFN_FLAG_MASK (KVMPPC_GFN_SECURE | KVMPPC_GFN_SHARED)
222 #define KVMPPC_GFN_PFN_MASK (~KVMPPC_GFN_FLAG_MASK)
223
224 struct kvmppc_uvmem_slot {
225 struct list_head list;
226 unsigned long nr_pfns;
227 unsigned long base_pfn;
228 unsigned long *pfns;
229 };
230 struct kvmppc_uvmem_page_pvt {
231 struct kvm *kvm;
232 unsigned long gpa;
233 bool skip_page_out;
234 bool remove_gfn;
235 };
236
kvmppc_uvmem_available(void)237 bool kvmppc_uvmem_available(void)
238 {
239 /*
240 * If kvmppc_uvmem_bitmap != NULL, then there is an ultravisor
241 * and our data structures have been initialized successfully.
242 */
243 return !!kvmppc_uvmem_bitmap;
244 }
245
kvmppc_uvmem_slot_init(struct kvm * kvm,const struct kvm_memory_slot * slot)246 int kvmppc_uvmem_slot_init(struct kvm *kvm, const struct kvm_memory_slot *slot)
247 {
248 struct kvmppc_uvmem_slot *p;
249
250 p = kzalloc(sizeof(*p), GFP_KERNEL);
251 if (!p)
252 return -ENOMEM;
253 p->pfns = vzalloc(array_size(slot->npages, sizeof(*p->pfns)));
254 if (!p->pfns) {
255 kfree(p);
256 return -ENOMEM;
257 }
258 p->nr_pfns = slot->npages;
259 p->base_pfn = slot->base_gfn;
260
261 mutex_lock(&kvm->arch.uvmem_lock);
262 list_add(&p->list, &kvm->arch.uvmem_pfns);
263 mutex_unlock(&kvm->arch.uvmem_lock);
264
265 return 0;
266 }
267
268 /*
269 * All device PFNs are already released by the time we come here.
270 */
kvmppc_uvmem_slot_free(struct kvm * kvm,const struct kvm_memory_slot * slot)271 void kvmppc_uvmem_slot_free(struct kvm *kvm, const struct kvm_memory_slot *slot)
272 {
273 struct kvmppc_uvmem_slot *p, *next;
274
275 mutex_lock(&kvm->arch.uvmem_lock);
276 list_for_each_entry_safe(p, next, &kvm->arch.uvmem_pfns, list) {
277 if (p->base_pfn == slot->base_gfn) {
278 vfree(p->pfns);
279 list_del(&p->list);
280 kfree(p);
281 break;
282 }
283 }
284 mutex_unlock(&kvm->arch.uvmem_lock);
285 }
286
kvmppc_mark_gfn(unsigned long gfn,struct kvm * kvm,unsigned long flag,unsigned long uvmem_pfn)287 static void kvmppc_mark_gfn(unsigned long gfn, struct kvm *kvm,
288 unsigned long flag, unsigned long uvmem_pfn)
289 {
290 struct kvmppc_uvmem_slot *p;
291
292 list_for_each_entry(p, &kvm->arch.uvmem_pfns, list) {
293 if (gfn >= p->base_pfn && gfn < p->base_pfn + p->nr_pfns) {
294 unsigned long index = gfn - p->base_pfn;
295
296 if (flag == KVMPPC_GFN_UVMEM_PFN)
297 p->pfns[index] = uvmem_pfn | flag;
298 else
299 p->pfns[index] = flag;
300 return;
301 }
302 }
303 }
304
305 /* mark the GFN as secure-GFN associated with @uvmem pfn device-PFN. */
kvmppc_gfn_secure_uvmem_pfn(unsigned long gfn,unsigned long uvmem_pfn,struct kvm * kvm)306 static void kvmppc_gfn_secure_uvmem_pfn(unsigned long gfn,
307 unsigned long uvmem_pfn, struct kvm *kvm)
308 {
309 kvmppc_mark_gfn(gfn, kvm, KVMPPC_GFN_UVMEM_PFN, uvmem_pfn);
310 }
311
312 /* mark the GFN as secure-GFN associated with a memory-PFN. */
kvmppc_gfn_secure_mem_pfn(unsigned long gfn,struct kvm * kvm)313 static void kvmppc_gfn_secure_mem_pfn(unsigned long gfn, struct kvm *kvm)
314 {
315 kvmppc_mark_gfn(gfn, kvm, KVMPPC_GFN_MEM_PFN, 0);
316 }
317
318 /* mark the GFN as a shared GFN. */
kvmppc_gfn_shared(unsigned long gfn,struct kvm * kvm)319 static void kvmppc_gfn_shared(unsigned long gfn, struct kvm *kvm)
320 {
321 kvmppc_mark_gfn(gfn, kvm, KVMPPC_GFN_SHARED, 0);
322 }
323
324 /* mark the GFN as a non-existent GFN. */
kvmppc_gfn_remove(unsigned long gfn,struct kvm * kvm)325 static void kvmppc_gfn_remove(unsigned long gfn, struct kvm *kvm)
326 {
327 kvmppc_mark_gfn(gfn, kvm, 0, 0);
328 }
329
330 /* return true, if the GFN is a secure-GFN backed by a secure-PFN */
kvmppc_gfn_is_uvmem_pfn(unsigned long gfn,struct kvm * kvm,unsigned long * uvmem_pfn)331 static bool kvmppc_gfn_is_uvmem_pfn(unsigned long gfn, struct kvm *kvm,
332 unsigned long *uvmem_pfn)
333 {
334 struct kvmppc_uvmem_slot *p;
335
336 list_for_each_entry(p, &kvm->arch.uvmem_pfns, list) {
337 if (gfn >= p->base_pfn && gfn < p->base_pfn + p->nr_pfns) {
338 unsigned long index = gfn - p->base_pfn;
339
340 if (p->pfns[index] & KVMPPC_GFN_UVMEM_PFN) {
341 if (uvmem_pfn)
342 *uvmem_pfn = p->pfns[index] &
343 KVMPPC_GFN_PFN_MASK;
344 return true;
345 } else
346 return false;
347 }
348 }
349 return false;
350 }
351
352 /*
353 * starting from *gfn search for the next available GFN that is not yet
354 * transitioned to a secure GFN. return the value of that GFN in *gfn. If a
355 * GFN is found, return true, else return false
356 *
357 * Must be called with kvm->arch.uvmem_lock held.
358 */
kvmppc_next_nontransitioned_gfn(const struct kvm_memory_slot * memslot,struct kvm * kvm,unsigned long * gfn)359 static bool kvmppc_next_nontransitioned_gfn(const struct kvm_memory_slot *memslot,
360 struct kvm *kvm, unsigned long *gfn)
361 {
362 struct kvmppc_uvmem_slot *p = NULL, *iter;
363 bool ret = false;
364 unsigned long i;
365
366 list_for_each_entry(iter, &kvm->arch.uvmem_pfns, list)
367 if (*gfn >= iter->base_pfn && *gfn < iter->base_pfn + iter->nr_pfns) {
368 p = iter;
369 break;
370 }
371 if (!p)
372 return ret;
373 /*
374 * The code below assumes, one to one correspondence between
375 * kvmppc_uvmem_slot and memslot.
376 */
377 for (i = *gfn; i < p->base_pfn + p->nr_pfns; i++) {
378 unsigned long index = i - p->base_pfn;
379
380 if (!(p->pfns[index] & KVMPPC_GFN_FLAG_MASK)) {
381 *gfn = i;
382 ret = true;
383 break;
384 }
385 }
386 return ret;
387 }
388
kvmppc_memslot_page_merge(struct kvm * kvm,const struct kvm_memory_slot * memslot,bool merge)389 static int kvmppc_memslot_page_merge(struct kvm *kvm,
390 const struct kvm_memory_slot *memslot, bool merge)
391 {
392 unsigned long gfn = memslot->base_gfn;
393 unsigned long end, start = gfn_to_hva(kvm, gfn);
394 int ret = 0;
395 struct vm_area_struct *vma;
396 int merge_flag = (merge) ? MADV_MERGEABLE : MADV_UNMERGEABLE;
397
398 if (kvm_is_error_hva(start))
399 return H_STATE;
400
401 end = start + (memslot->npages << PAGE_SHIFT);
402
403 mmap_write_lock(kvm->mm);
404 do {
405 vma = find_vma_intersection(kvm->mm, start, end);
406 if (!vma) {
407 ret = H_STATE;
408 break;
409 }
410 ret = ksm_madvise(vma, vma->vm_start, vma->vm_end,
411 merge_flag, &vma->vm_flags);
412 if (ret) {
413 ret = H_STATE;
414 break;
415 }
416 start = vma->vm_end;
417 } while (end > vma->vm_end);
418
419 mmap_write_unlock(kvm->mm);
420 return ret;
421 }
422
__kvmppc_uvmem_memslot_delete(struct kvm * kvm,const struct kvm_memory_slot * memslot)423 static void __kvmppc_uvmem_memslot_delete(struct kvm *kvm,
424 const struct kvm_memory_slot *memslot)
425 {
426 uv_unregister_mem_slot(kvm->arch.lpid, memslot->id);
427 kvmppc_uvmem_slot_free(kvm, memslot);
428 kvmppc_memslot_page_merge(kvm, memslot, true);
429 }
430
__kvmppc_uvmem_memslot_create(struct kvm * kvm,const struct kvm_memory_slot * memslot)431 static int __kvmppc_uvmem_memslot_create(struct kvm *kvm,
432 const struct kvm_memory_slot *memslot)
433 {
434 int ret = H_PARAMETER;
435
436 if (kvmppc_memslot_page_merge(kvm, memslot, false))
437 return ret;
438
439 if (kvmppc_uvmem_slot_init(kvm, memslot))
440 goto out1;
441
442 ret = uv_register_mem_slot(kvm->arch.lpid,
443 memslot->base_gfn << PAGE_SHIFT,
444 memslot->npages * PAGE_SIZE,
445 0, memslot->id);
446 if (ret < 0) {
447 ret = H_PARAMETER;
448 goto out;
449 }
450 return 0;
451 out:
452 kvmppc_uvmem_slot_free(kvm, memslot);
453 out1:
454 kvmppc_memslot_page_merge(kvm, memslot, true);
455 return ret;
456 }
457
kvmppc_h_svm_init_start(struct kvm * kvm)458 unsigned long kvmppc_h_svm_init_start(struct kvm *kvm)
459 {
460 struct kvm_memslots *slots;
461 struct kvm_memory_slot *memslot, *m;
462 int ret = H_SUCCESS;
463 int srcu_idx;
464
465 kvm->arch.secure_guest = KVMPPC_SECURE_INIT_START;
466
467 if (!kvmppc_uvmem_bitmap)
468 return H_UNSUPPORTED;
469
470 /* Only radix guests can be secure guests */
471 if (!kvm_is_radix(kvm))
472 return H_UNSUPPORTED;
473
474 /* NAK the transition to secure if not enabled */
475 if (!kvm->arch.svm_enabled)
476 return H_AUTHORITY;
477
478 srcu_idx = srcu_read_lock(&kvm->srcu);
479
480 /* register the memslot */
481 slots = kvm_memslots(kvm);
482 kvm_for_each_memslot(memslot, slots) {
483 ret = __kvmppc_uvmem_memslot_create(kvm, memslot);
484 if (ret)
485 break;
486 }
487
488 if (ret) {
489 slots = kvm_memslots(kvm);
490 kvm_for_each_memslot(m, slots) {
491 if (m == memslot)
492 break;
493 __kvmppc_uvmem_memslot_delete(kvm, memslot);
494 }
495 }
496
497 srcu_read_unlock(&kvm->srcu, srcu_idx);
498 return ret;
499 }
500
501 /*
502 * Provision a new page on HV side and copy over the contents
503 * from secure memory using UV_PAGE_OUT uvcall.
504 * Caller must held kvm->arch.uvmem_lock.
505 */
__kvmppc_svm_page_out(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long page_shift,struct kvm * kvm,unsigned long gpa,struct page * fault_page)506 static int __kvmppc_svm_page_out(struct vm_area_struct *vma,
507 unsigned long start,
508 unsigned long end, unsigned long page_shift,
509 struct kvm *kvm, unsigned long gpa, struct page *fault_page)
510 {
511 unsigned long src_pfn, dst_pfn = 0;
512 struct migrate_vma mig = { 0 };
513 struct page *dpage, *spage;
514 struct kvmppc_uvmem_page_pvt *pvt;
515 unsigned long pfn;
516 int ret = U_SUCCESS;
517
518 memset(&mig, 0, sizeof(mig));
519 mig.vma = vma;
520 mig.start = start;
521 mig.end = end;
522 mig.src = &src_pfn;
523 mig.dst = &dst_pfn;
524 mig.pgmap_owner = &kvmppc_uvmem_pgmap;
525 mig.flags = MIGRATE_VMA_SELECT_DEVICE_PRIVATE;
526 mig.fault_page = fault_page;
527
528 /* The requested page is already paged-out, nothing to do */
529 if (!kvmppc_gfn_is_uvmem_pfn(gpa >> page_shift, kvm, NULL))
530 return ret;
531
532 ret = migrate_vma_setup(&mig);
533 if (ret)
534 return -1;
535
536 spage = migrate_pfn_to_page(*mig.src);
537 if (!spage || !(*mig.src & MIGRATE_PFN_MIGRATE))
538 goto out_finalize;
539
540 if (!is_zone_device_page(spage))
541 goto out_finalize;
542
543 dpage = alloc_page_vma(GFP_HIGHUSER, vma, start);
544 if (!dpage) {
545 ret = -1;
546 goto out_finalize;
547 }
548
549 lock_page(dpage);
550 pvt = spage->zone_device_data;
551 pfn = page_to_pfn(dpage);
552
553 /*
554 * This function is used in two cases:
555 * - When HV touches a secure page, for which we do UV_PAGE_OUT
556 * - When a secure page is converted to shared page, we *get*
557 * the page to essentially unmap the device page. In this
558 * case we skip page-out.
559 */
560 if (!pvt->skip_page_out)
561 ret = uv_page_out(kvm->arch.lpid, pfn << page_shift,
562 gpa, 0, page_shift);
563
564 if (ret == U_SUCCESS)
565 *mig.dst = migrate_pfn(pfn) | MIGRATE_PFN_LOCKED;
566 else {
567 unlock_page(dpage);
568 __free_page(dpage);
569 goto out_finalize;
570 }
571
572 migrate_vma_pages(&mig);
573
574 out_finalize:
575 migrate_vma_finalize(&mig);
576 return ret;
577 }
578
kvmppc_svm_page_out(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long page_shift,struct kvm * kvm,unsigned long gpa,struct page * fault_page)579 static inline int kvmppc_svm_page_out(struct vm_area_struct *vma,
580 unsigned long start, unsigned long end,
581 unsigned long page_shift,
582 struct kvm *kvm, unsigned long gpa,
583 struct page *fault_page)
584 {
585 int ret;
586
587 mutex_lock(&kvm->arch.uvmem_lock);
588 ret = __kvmppc_svm_page_out(vma, start, end, page_shift, kvm, gpa,
589 fault_page);
590 mutex_unlock(&kvm->arch.uvmem_lock);
591
592 return ret;
593 }
594
595 /*
596 * Drop device pages that we maintain for the secure guest
597 *
598 * We first mark the pages to be skipped from UV_PAGE_OUT when there
599 * is HV side fault on these pages. Next we *get* these pages, forcing
600 * fault on them, do fault time migration to replace the device PTEs in
601 * QEMU page table with normal PTEs from newly allocated pages.
602 */
kvmppc_uvmem_drop_pages(const struct kvm_memory_slot * slot,struct kvm * kvm,bool skip_page_out)603 void kvmppc_uvmem_drop_pages(const struct kvm_memory_slot *slot,
604 struct kvm *kvm, bool skip_page_out)
605 {
606 int i;
607 struct kvmppc_uvmem_page_pvt *pvt;
608 struct page *uvmem_page;
609 struct vm_area_struct *vma = NULL;
610 unsigned long uvmem_pfn, gfn;
611 unsigned long addr;
612
613 mmap_read_lock(kvm->mm);
614
615 addr = slot->userspace_addr;
616
617 gfn = slot->base_gfn;
618 for (i = slot->npages; i; --i, ++gfn, addr += PAGE_SIZE) {
619
620 /* Fetch the VMA if addr is not in the latest fetched one */
621 if (!vma || addr >= vma->vm_end) {
622 vma = find_vma_intersection(kvm->mm, addr, addr+1);
623 if (!vma) {
624 pr_err("Can't find VMA for gfn:0x%lx\n", gfn);
625 break;
626 }
627 }
628
629 mutex_lock(&kvm->arch.uvmem_lock);
630
631 if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, &uvmem_pfn)) {
632 uvmem_page = pfn_to_page(uvmem_pfn);
633 pvt = uvmem_page->zone_device_data;
634 pvt->skip_page_out = skip_page_out;
635 pvt->remove_gfn = true;
636
637 if (__kvmppc_svm_page_out(vma, addr, addr + PAGE_SIZE,
638 PAGE_SHIFT, kvm, pvt->gpa, NULL))
639 pr_err("Can't page out gpa:0x%lx addr:0x%lx\n",
640 pvt->gpa, addr);
641 } else {
642 /* Remove the shared flag if any */
643 kvmppc_gfn_remove(gfn, kvm);
644 }
645
646 mutex_unlock(&kvm->arch.uvmem_lock);
647 }
648
649 mmap_read_unlock(kvm->mm);
650 }
651
kvmppc_h_svm_init_abort(struct kvm * kvm)652 unsigned long kvmppc_h_svm_init_abort(struct kvm *kvm)
653 {
654 int srcu_idx;
655 struct kvm_memory_slot *memslot;
656
657 /*
658 * Expect to be called only after INIT_START and before INIT_DONE.
659 * If INIT_DONE was completed, use normal VM termination sequence.
660 */
661 if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
662 return H_UNSUPPORTED;
663
664 if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
665 return H_STATE;
666
667 srcu_idx = srcu_read_lock(&kvm->srcu);
668
669 kvm_for_each_memslot(memslot, kvm_memslots(kvm))
670 kvmppc_uvmem_drop_pages(memslot, kvm, false);
671
672 srcu_read_unlock(&kvm->srcu, srcu_idx);
673
674 kvm->arch.secure_guest = 0;
675 uv_svm_terminate(kvm->arch.lpid);
676
677 return H_PARAMETER;
678 }
679
680 /*
681 * Get a free device PFN from the pool
682 *
683 * Called when a normal page is moved to secure memory (UV_PAGE_IN). Device
684 * PFN will be used to keep track of the secure page on HV side.
685 *
686 * Called with kvm->arch.uvmem_lock held
687 */
kvmppc_uvmem_get_page(unsigned long gpa,struct kvm * kvm)688 static struct page *kvmppc_uvmem_get_page(unsigned long gpa, struct kvm *kvm)
689 {
690 struct page *dpage = NULL;
691 unsigned long bit, uvmem_pfn;
692 struct kvmppc_uvmem_page_pvt *pvt;
693 unsigned long pfn_last, pfn_first;
694
695 pfn_first = kvmppc_uvmem_pgmap.range.start >> PAGE_SHIFT;
696 pfn_last = pfn_first +
697 (range_len(&kvmppc_uvmem_pgmap.range) >> PAGE_SHIFT);
698
699 spin_lock(&kvmppc_uvmem_bitmap_lock);
700 bit = find_first_zero_bit(kvmppc_uvmem_bitmap,
701 pfn_last - pfn_first);
702 if (bit >= (pfn_last - pfn_first))
703 goto out;
704 bitmap_set(kvmppc_uvmem_bitmap, bit, 1);
705 spin_unlock(&kvmppc_uvmem_bitmap_lock);
706
707 pvt = kzalloc(sizeof(*pvt), GFP_KERNEL);
708 if (!pvt)
709 goto out_clear;
710
711 uvmem_pfn = bit + pfn_first;
712 kvmppc_gfn_secure_uvmem_pfn(gpa >> PAGE_SHIFT, uvmem_pfn, kvm);
713
714 pvt->gpa = gpa;
715 pvt->kvm = kvm;
716
717 dpage = pfn_to_page(uvmem_pfn);
718 dpage->zone_device_data = pvt;
719 get_page(dpage);
720 lock_page(dpage);
721 return dpage;
722 out_clear:
723 spin_lock(&kvmppc_uvmem_bitmap_lock);
724 bitmap_clear(kvmppc_uvmem_bitmap, bit, 1);
725 out:
726 spin_unlock(&kvmppc_uvmem_bitmap_lock);
727 return NULL;
728 }
729
730 /*
731 * Alloc a PFN from private device memory pool. If @pagein is true,
732 * copy page from normal memory to secure memory using UV_PAGE_IN uvcall.
733 */
kvmppc_svm_page_in(struct vm_area_struct * vma,unsigned long start,unsigned long end,unsigned long gpa,struct kvm * kvm,unsigned long page_shift,bool pagein)734 static int kvmppc_svm_page_in(struct vm_area_struct *vma,
735 unsigned long start,
736 unsigned long end, unsigned long gpa, struct kvm *kvm,
737 unsigned long page_shift,
738 bool pagein)
739 {
740 unsigned long src_pfn, dst_pfn = 0;
741 struct migrate_vma mig = { 0 };
742 struct page *spage;
743 unsigned long pfn;
744 struct page *dpage;
745 int ret = 0;
746
747 memset(&mig, 0, sizeof(mig));
748 mig.vma = vma;
749 mig.start = start;
750 mig.end = end;
751 mig.src = &src_pfn;
752 mig.dst = &dst_pfn;
753 mig.flags = MIGRATE_VMA_SELECT_SYSTEM;
754
755 ret = migrate_vma_setup(&mig);
756 if (ret)
757 return ret;
758
759 if (!(*mig.src & MIGRATE_PFN_MIGRATE)) {
760 ret = -1;
761 goto out_finalize;
762 }
763
764 dpage = kvmppc_uvmem_get_page(gpa, kvm);
765 if (!dpage) {
766 ret = -1;
767 goto out_finalize;
768 }
769
770 if (pagein) {
771 pfn = *mig.src >> MIGRATE_PFN_SHIFT;
772 spage = migrate_pfn_to_page(*mig.src);
773 if (spage) {
774 ret = uv_page_in(kvm->arch.lpid, pfn << page_shift,
775 gpa, 0, page_shift);
776 if (ret)
777 goto out_finalize;
778 }
779 }
780
781 *mig.dst = migrate_pfn(page_to_pfn(dpage)) | MIGRATE_PFN_LOCKED;
782 migrate_vma_pages(&mig);
783 out_finalize:
784 migrate_vma_finalize(&mig);
785 return ret;
786 }
787
kvmppc_uv_migrate_mem_slot(struct kvm * kvm,const struct kvm_memory_slot * memslot)788 static int kvmppc_uv_migrate_mem_slot(struct kvm *kvm,
789 const struct kvm_memory_slot *memslot)
790 {
791 unsigned long gfn = memslot->base_gfn;
792 struct vm_area_struct *vma;
793 unsigned long start, end;
794 int ret = 0;
795
796 mmap_read_lock(kvm->mm);
797 mutex_lock(&kvm->arch.uvmem_lock);
798 while (kvmppc_next_nontransitioned_gfn(memslot, kvm, &gfn)) {
799 ret = H_STATE;
800 start = gfn_to_hva(kvm, gfn);
801 if (kvm_is_error_hva(start))
802 break;
803
804 end = start + (1UL << PAGE_SHIFT);
805 vma = find_vma_intersection(kvm->mm, start, end);
806 if (!vma || vma->vm_start > start || vma->vm_end < end)
807 break;
808
809 ret = kvmppc_svm_page_in(vma, start, end,
810 (gfn << PAGE_SHIFT), kvm, PAGE_SHIFT, false);
811 if (ret) {
812 ret = H_STATE;
813 break;
814 }
815
816 /* relinquish the cpu if needed */
817 cond_resched();
818 }
819 mutex_unlock(&kvm->arch.uvmem_lock);
820 mmap_read_unlock(kvm->mm);
821 return ret;
822 }
823
kvmppc_h_svm_init_done(struct kvm * kvm)824 unsigned long kvmppc_h_svm_init_done(struct kvm *kvm)
825 {
826 struct kvm_memslots *slots;
827 struct kvm_memory_slot *memslot;
828 int srcu_idx;
829 long ret = H_SUCCESS;
830
831 if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
832 return H_UNSUPPORTED;
833
834 /* migrate any unmoved normal pfn to device pfns*/
835 srcu_idx = srcu_read_lock(&kvm->srcu);
836 slots = kvm_memslots(kvm);
837 kvm_for_each_memslot(memslot, slots) {
838 ret = kvmppc_uv_migrate_mem_slot(kvm, memslot);
839 if (ret) {
840 /*
841 * The pages will remain transitioned.
842 * Its the callers responsibility to
843 * terminate the VM, which will undo
844 * all state of the VM. Till then
845 * this VM is in a erroneous state.
846 * Its KVMPPC_SECURE_INIT_DONE will
847 * remain unset.
848 */
849 ret = H_STATE;
850 goto out;
851 }
852 }
853
854 kvm->arch.secure_guest |= KVMPPC_SECURE_INIT_DONE;
855 pr_info("LPID %d went secure\n", kvm->arch.lpid);
856
857 out:
858 srcu_read_unlock(&kvm->srcu, srcu_idx);
859 return ret;
860 }
861
862 /*
863 * Shares the page with HV, thus making it a normal page.
864 *
865 * - If the page is already secure, then provision a new page and share
866 * - If the page is a normal page, share the existing page
867 *
868 * In the former case, uses dev_pagemap_ops.migrate_to_ram handler
869 * to unmap the device page from QEMU's page tables.
870 */
kvmppc_share_page(struct kvm * kvm,unsigned long gpa,unsigned long page_shift)871 static unsigned long kvmppc_share_page(struct kvm *kvm, unsigned long gpa,
872 unsigned long page_shift)
873 {
874
875 int ret = H_PARAMETER;
876 struct page *uvmem_page;
877 struct kvmppc_uvmem_page_pvt *pvt;
878 unsigned long pfn;
879 unsigned long gfn = gpa >> page_shift;
880 int srcu_idx;
881 unsigned long uvmem_pfn;
882
883 srcu_idx = srcu_read_lock(&kvm->srcu);
884 mutex_lock(&kvm->arch.uvmem_lock);
885 if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, &uvmem_pfn)) {
886 uvmem_page = pfn_to_page(uvmem_pfn);
887 pvt = uvmem_page->zone_device_data;
888 pvt->skip_page_out = true;
889 /*
890 * do not drop the GFN. It is a valid GFN
891 * that is transitioned to a shared GFN.
892 */
893 pvt->remove_gfn = false;
894 }
895
896 retry:
897 mutex_unlock(&kvm->arch.uvmem_lock);
898 pfn = gfn_to_pfn(kvm, gfn);
899 if (is_error_noslot_pfn(pfn))
900 goto out;
901
902 mutex_lock(&kvm->arch.uvmem_lock);
903 if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, &uvmem_pfn)) {
904 uvmem_page = pfn_to_page(uvmem_pfn);
905 pvt = uvmem_page->zone_device_data;
906 pvt->skip_page_out = true;
907 pvt->remove_gfn = false; /* it continues to be a valid GFN */
908 kvm_release_pfn_clean(pfn);
909 goto retry;
910 }
911
912 if (!uv_page_in(kvm->arch.lpid, pfn << page_shift, gpa, 0,
913 page_shift)) {
914 kvmppc_gfn_shared(gfn, kvm);
915 ret = H_SUCCESS;
916 }
917 kvm_release_pfn_clean(pfn);
918 mutex_unlock(&kvm->arch.uvmem_lock);
919 out:
920 srcu_read_unlock(&kvm->srcu, srcu_idx);
921 return ret;
922 }
923
924 /*
925 * H_SVM_PAGE_IN: Move page from normal memory to secure memory.
926 *
927 * H_PAGE_IN_SHARED flag makes the page shared which means that the same
928 * memory in is visible from both UV and HV.
929 */
kvmppc_h_svm_page_in(struct kvm * kvm,unsigned long gpa,unsigned long flags,unsigned long page_shift)930 unsigned long kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gpa,
931 unsigned long flags,
932 unsigned long page_shift)
933 {
934 unsigned long start, end;
935 struct vm_area_struct *vma;
936 int srcu_idx;
937 unsigned long gfn = gpa >> page_shift;
938 int ret;
939
940 if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
941 return H_UNSUPPORTED;
942
943 if (page_shift != PAGE_SHIFT)
944 return H_P3;
945
946 if (flags & ~H_PAGE_IN_SHARED)
947 return H_P2;
948
949 if (flags & H_PAGE_IN_SHARED)
950 return kvmppc_share_page(kvm, gpa, page_shift);
951
952 ret = H_PARAMETER;
953 srcu_idx = srcu_read_lock(&kvm->srcu);
954 mmap_read_lock(kvm->mm);
955
956 start = gfn_to_hva(kvm, gfn);
957 if (kvm_is_error_hva(start))
958 goto out;
959
960 mutex_lock(&kvm->arch.uvmem_lock);
961 /* Fail the page-in request of an already paged-in page */
962 if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, NULL))
963 goto out_unlock;
964
965 end = start + (1UL << page_shift);
966 vma = find_vma_intersection(kvm->mm, start, end);
967 if (!vma || vma->vm_start > start || vma->vm_end < end)
968 goto out_unlock;
969
970 if (kvmppc_svm_page_in(vma, start, end, gpa, kvm, page_shift,
971 true))
972 goto out_unlock;
973
974 ret = H_SUCCESS;
975
976 out_unlock:
977 mutex_unlock(&kvm->arch.uvmem_lock);
978 out:
979 mmap_read_unlock(kvm->mm);
980 srcu_read_unlock(&kvm->srcu, srcu_idx);
981 return ret;
982 }
983
984
985 /*
986 * Fault handler callback that gets called when HV touches any page that
987 * has been moved to secure memory, we ask UV to give back the page by
988 * issuing UV_PAGE_OUT uvcall.
989 *
990 * This eventually results in dropping of device PFN and the newly
991 * provisioned page/PFN gets populated in QEMU page tables.
992 */
kvmppc_uvmem_migrate_to_ram(struct vm_fault * vmf)993 static vm_fault_t kvmppc_uvmem_migrate_to_ram(struct vm_fault *vmf)
994 {
995 struct kvmppc_uvmem_page_pvt *pvt = vmf->page->zone_device_data;
996
997 if (kvmppc_svm_page_out(vmf->vma, vmf->address,
998 vmf->address + PAGE_SIZE, PAGE_SHIFT,
999 pvt->kvm, pvt->gpa, vmf->page))
1000 return VM_FAULT_SIGBUS;
1001 else
1002 return 0;
1003 }
1004
1005 /*
1006 * Release the device PFN back to the pool
1007 *
1008 * Gets called when secure GFN tranistions from a secure-PFN
1009 * to a normal PFN during H_SVM_PAGE_OUT.
1010 * Gets called with kvm->arch.uvmem_lock held.
1011 */
kvmppc_uvmem_page_free(struct page * page)1012 static void kvmppc_uvmem_page_free(struct page *page)
1013 {
1014 unsigned long pfn = page_to_pfn(page) -
1015 (kvmppc_uvmem_pgmap.range.start >> PAGE_SHIFT);
1016 struct kvmppc_uvmem_page_pvt *pvt;
1017
1018 spin_lock(&kvmppc_uvmem_bitmap_lock);
1019 bitmap_clear(kvmppc_uvmem_bitmap, pfn, 1);
1020 spin_unlock(&kvmppc_uvmem_bitmap_lock);
1021
1022 pvt = page->zone_device_data;
1023 page->zone_device_data = NULL;
1024 if (pvt->remove_gfn)
1025 kvmppc_gfn_remove(pvt->gpa >> PAGE_SHIFT, pvt->kvm);
1026 else
1027 kvmppc_gfn_secure_mem_pfn(pvt->gpa >> PAGE_SHIFT, pvt->kvm);
1028 kfree(pvt);
1029 }
1030
1031 static const struct dev_pagemap_ops kvmppc_uvmem_ops = {
1032 .page_free = kvmppc_uvmem_page_free,
1033 .migrate_to_ram = kvmppc_uvmem_migrate_to_ram,
1034 };
1035
1036 /*
1037 * H_SVM_PAGE_OUT: Move page from secure memory to normal memory.
1038 */
1039 unsigned long
kvmppc_h_svm_page_out(struct kvm * kvm,unsigned long gpa,unsigned long flags,unsigned long page_shift)1040 kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gpa,
1041 unsigned long flags, unsigned long page_shift)
1042 {
1043 unsigned long gfn = gpa >> page_shift;
1044 unsigned long start, end;
1045 struct vm_area_struct *vma;
1046 int srcu_idx;
1047 int ret;
1048
1049 if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
1050 return H_UNSUPPORTED;
1051
1052 if (page_shift != PAGE_SHIFT)
1053 return H_P3;
1054
1055 if (flags)
1056 return H_P2;
1057
1058 ret = H_PARAMETER;
1059 srcu_idx = srcu_read_lock(&kvm->srcu);
1060 mmap_read_lock(kvm->mm);
1061 start = gfn_to_hva(kvm, gfn);
1062 if (kvm_is_error_hva(start))
1063 goto out;
1064
1065 end = start + (1UL << page_shift);
1066 vma = find_vma_intersection(kvm->mm, start, end);
1067 if (!vma || vma->vm_start > start || vma->vm_end < end)
1068 goto out;
1069
1070 if (!kvmppc_svm_page_out(vma, start, end, page_shift, kvm, gpa, NULL))
1071 ret = H_SUCCESS;
1072 out:
1073 mmap_read_unlock(kvm->mm);
1074 srcu_read_unlock(&kvm->srcu, srcu_idx);
1075 return ret;
1076 }
1077
kvmppc_send_page_to_uv(struct kvm * kvm,unsigned long gfn)1078 int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gfn)
1079 {
1080 unsigned long pfn;
1081 int ret = U_SUCCESS;
1082
1083 pfn = gfn_to_pfn(kvm, gfn);
1084 if (is_error_noslot_pfn(pfn))
1085 return -EFAULT;
1086
1087 mutex_lock(&kvm->arch.uvmem_lock);
1088 if (kvmppc_gfn_is_uvmem_pfn(gfn, kvm, NULL))
1089 goto out;
1090
1091 ret = uv_page_in(kvm->arch.lpid, pfn << PAGE_SHIFT, gfn << PAGE_SHIFT,
1092 0, PAGE_SHIFT);
1093 out:
1094 kvm_release_pfn_clean(pfn);
1095 mutex_unlock(&kvm->arch.uvmem_lock);
1096 return (ret == U_SUCCESS) ? RESUME_GUEST : -EFAULT;
1097 }
1098
kvmppc_uvmem_memslot_create(struct kvm * kvm,const struct kvm_memory_slot * new)1099 int kvmppc_uvmem_memslot_create(struct kvm *kvm, const struct kvm_memory_slot *new)
1100 {
1101 int ret = __kvmppc_uvmem_memslot_create(kvm, new);
1102
1103 if (!ret)
1104 ret = kvmppc_uv_migrate_mem_slot(kvm, new);
1105
1106 return ret;
1107 }
1108
kvmppc_uvmem_memslot_delete(struct kvm * kvm,const struct kvm_memory_slot * old)1109 void kvmppc_uvmem_memslot_delete(struct kvm *kvm, const struct kvm_memory_slot *old)
1110 {
1111 __kvmppc_uvmem_memslot_delete(kvm, old);
1112 }
1113
kvmppc_get_secmem_size(void)1114 static u64 kvmppc_get_secmem_size(void)
1115 {
1116 struct device_node *np;
1117 int i, len;
1118 const __be32 *prop;
1119 u64 size = 0;
1120
1121 /*
1122 * First try the new ibm,secure-memory nodes which supersede the
1123 * secure-memory-ranges property.
1124 * If we found some, no need to read the deprecated ones.
1125 */
1126 for_each_compatible_node(np, NULL, "ibm,secure-memory") {
1127 prop = of_get_property(np, "reg", &len);
1128 if (!prop)
1129 continue;
1130 size += of_read_number(prop + 2, 2);
1131 }
1132 if (size)
1133 return size;
1134
1135 np = of_find_compatible_node(NULL, NULL, "ibm,uv-firmware");
1136 if (!np)
1137 goto out;
1138
1139 prop = of_get_property(np, "secure-memory-ranges", &len);
1140 if (!prop)
1141 goto out_put;
1142
1143 for (i = 0; i < len / (sizeof(*prop) * 4); i++)
1144 size += of_read_number(prop + (i * 4) + 2, 2);
1145
1146 out_put:
1147 of_node_put(np);
1148 out:
1149 return size;
1150 }
1151
kvmppc_uvmem_init(void)1152 int kvmppc_uvmem_init(void)
1153 {
1154 int ret = 0;
1155 unsigned long size;
1156 struct resource *res;
1157 void *addr;
1158 unsigned long pfn_last, pfn_first;
1159
1160 size = kvmppc_get_secmem_size();
1161 if (!size) {
1162 /*
1163 * Don't fail the initialization of kvm-hv module if
1164 * the platform doesn't export ibm,uv-firmware node.
1165 * Let normal guests run on such PEF-disabled platform.
1166 */
1167 pr_info("KVMPPC-UVMEM: No support for secure guests\n");
1168 goto out;
1169 }
1170
1171 res = request_free_mem_region(&iomem_resource, size, "kvmppc_uvmem");
1172 if (IS_ERR(res)) {
1173 ret = PTR_ERR(res);
1174 goto out;
1175 }
1176
1177 kvmppc_uvmem_pgmap.type = MEMORY_DEVICE_PRIVATE;
1178 kvmppc_uvmem_pgmap.range.start = res->start;
1179 kvmppc_uvmem_pgmap.range.end = res->end;
1180 kvmppc_uvmem_pgmap.nr_range = 1;
1181 kvmppc_uvmem_pgmap.ops = &kvmppc_uvmem_ops;
1182 /* just one global instance: */
1183 kvmppc_uvmem_pgmap.owner = &kvmppc_uvmem_pgmap;
1184 addr = memremap_pages(&kvmppc_uvmem_pgmap, NUMA_NO_NODE);
1185 if (IS_ERR(addr)) {
1186 ret = PTR_ERR(addr);
1187 goto out_free_region;
1188 }
1189
1190 pfn_first = res->start >> PAGE_SHIFT;
1191 pfn_last = pfn_first + (resource_size(res) >> PAGE_SHIFT);
1192 kvmppc_uvmem_bitmap = kcalloc(BITS_TO_LONGS(pfn_last - pfn_first),
1193 sizeof(unsigned long), GFP_KERNEL);
1194 if (!kvmppc_uvmem_bitmap) {
1195 ret = -ENOMEM;
1196 goto out_unmap;
1197 }
1198
1199 pr_info("KVMPPC-UVMEM: Secure Memory size 0x%lx\n", size);
1200 return ret;
1201 out_unmap:
1202 memunmap_pages(&kvmppc_uvmem_pgmap);
1203 out_free_region:
1204 release_mem_region(res->start, size);
1205 out:
1206 return ret;
1207 }
1208
kvmppc_uvmem_free(void)1209 void kvmppc_uvmem_free(void)
1210 {
1211 if (!kvmppc_uvmem_bitmap)
1212 return;
1213
1214 memunmap_pages(&kvmppc_uvmem_pgmap);
1215 release_mem_region(kvmppc_uvmem_pgmap.range.start,
1216 range_len(&kvmppc_uvmem_pgmap.range));
1217 kfree(kvmppc_uvmem_bitmap);
1218 }
1219