Skip to content

Commit c326225

Browse files
Bharata B Raopaulusmack
authored andcommitted
KVM: PPC: Book3S HV: Handle memory plug/unplug to secure VM
Register the new memslot with UV during plug and unregister the memslot during unplug. In addition, release all the device pages during unplug. Signed-off-by: Bharata B Rao <[email protected]> Signed-off-by: Paul Mackerras <[email protected]>
1 parent 008e359 commit c326225

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

arch/powerpc/include/asm/kvm_book3s_uvmem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ unsigned long kvmppc_h_svm_page_out(struct kvm *kvm,
1919
unsigned long kvmppc_h_svm_init_start(struct kvm *kvm);
2020
unsigned long kvmppc_h_svm_init_done(struct kvm *kvm);
2121
int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gfn);
22+
void kvmppc_uvmem_drop_pages(const struct kvm_memory_slot *free,
23+
struct kvm *kvm);
2224
#else
2325
static inline int kvmppc_uvmem_init(void)
2426
{
@@ -64,5 +66,9 @@ static inline int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gfn)
6466
{
6567
return -EFAULT;
6668
}
69+
70+
static inline void
71+
kvmppc_uvmem_drop_pages(const struct kvm_memory_slot *free,
72+
struct kvm *kvm) { }
6773
#endif /* CONFIG_PPC_UV */
6874
#endif /* __ASM_KVM_BOOK3S_UVMEM_H__ */

arch/powerpc/include/asm/ultravisor-api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#define UV_RETURN 0xF11C
2828
#define UV_ESM 0xF110
2929
#define UV_REGISTER_MEM_SLOT 0xF120
30+
#define UV_UNREGISTER_MEM_SLOT 0xF124
3031
#define UV_PAGE_IN 0xF128
3132
#define UV_PAGE_OUT 0xF12C
3233
#define UV_SHARE_PAGE 0xF130

arch/powerpc/include/asm/ultravisor.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ static inline int uv_register_mem_slot(u64 lpid, u64 start_gpa, u64 size,
6767
size, flags, slotid);
6868
}
6969

70+
static inline int uv_unregister_mem_slot(u64 lpid, u64 slotid)
71+
{
72+
return ucall_norets(UV_UNREGISTER_MEM_SLOT, lpid, slotid);
73+
}
74+
7075
static inline int uv_page_inval(u64 lpid, u64 gpa, u64 page_shift)
7176
{
7277
return ucall_norets(UV_PAGE_INVAL, lpid, gpa, page_shift);

arch/powerpc/kvm/book3s_64_mmu_radix.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,9 @@ void kvmppc_radix_flush_memslot(struct kvm *kvm,
11011101
unsigned long gpa;
11021102
unsigned int shift;
11031103

1104+
if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START)
1105+
kvmppc_uvmem_drop_pages(memslot, kvm);
1106+
11041107
if (kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE)
11051108
return;
11061109

arch/powerpc/kvm/book3s_hv.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
#include <asm/hw_breakpoint.h>
7575
#include <asm/kvm_host.h>
7676
#include <asm/kvm_book3s_uvmem.h>
77+
#include <asm/ultravisor.h>
7778

7879
#include "book3s.h"
7980

@@ -4515,6 +4516,29 @@ static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
45154516
if (change == KVM_MR_FLAGS_ONLY && kvm_is_radix(kvm) &&
45164517
((new->flags ^ old->flags) & KVM_MEM_LOG_DIRTY_PAGES))
45174518
kvmppc_radix_flush_memslot(kvm, old);
4519+
/*
4520+
* If UV hasn't yet called H_SVM_INIT_START, don't register memslots.
4521+
*/
4522+
if (!kvm->arch.secure_guest)
4523+
return;
4524+
4525+
switch (change) {
4526+
case KVM_MR_CREATE:
4527+
if (kvmppc_uvmem_slot_init(kvm, new))
4528+
return;
4529+
uv_register_mem_slot(kvm->arch.lpid,
4530+
new->base_gfn << PAGE_SHIFT,
4531+
new->npages * PAGE_SIZE,
4532+
0, new->id);
4533+
break;
4534+
case KVM_MR_DELETE:
4535+
uv_unregister_mem_slot(kvm->arch.lpid, old->id);
4536+
kvmppc_uvmem_slot_free(kvm, old);
4537+
break;
4538+
default:
4539+
/* TODO: Handle KVM_MR_MOVE */
4540+
break;
4541+
}
45184542
}
45194543

45204544
/*

arch/powerpc/kvm/book3s_hv_uvmem.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,43 @@ unsigned long kvmppc_h_svm_init_done(struct kvm *kvm)
249249
return H_SUCCESS;
250250
}
251251

252+
/*
253+
* Drop device pages that we maintain for the secure guest
254+
*
255+
* We first mark the pages to be skipped from UV_PAGE_OUT when there
256+
* is HV side fault on these pages. Next we *get* these pages, forcing
257+
* fault on them, do fault time migration to replace the device PTEs in
258+
* QEMU page table with normal PTEs from newly allocated pages.
259+
*/
260+
void kvmppc_uvmem_drop_pages(const struct kvm_memory_slot *free,
261+
struct kvm *kvm)
262+
{
263+
int i;
264+
struct kvmppc_uvmem_page_pvt *pvt;
265+
unsigned long pfn, uvmem_pfn;
266+
unsigned long gfn = free->base_gfn;
267+
268+
for (i = free->npages; i; --i, ++gfn) {
269+
struct page *uvmem_page;
270+
271+
mutex_lock(&kvm->arch.uvmem_lock);
272+
if (!kvmppc_gfn_is_uvmem_pfn(gfn, kvm, &uvmem_pfn)) {
273+
mutex_unlock(&kvm->arch.uvmem_lock);
274+
continue;
275+
}
276+
277+
uvmem_page = pfn_to_page(uvmem_pfn);
278+
pvt = uvmem_page->zone_device_data;
279+
pvt->skip_page_out = true;
280+
mutex_unlock(&kvm->arch.uvmem_lock);
281+
282+
pfn = gfn_to_pfn(kvm, gfn);
283+
if (is_error_noslot_pfn(pfn))
284+
continue;
285+
kvm_release_pfn_clean(pfn);
286+
}
287+
}
288+
252289
/*
253290
* Get a free device PFN from the pool
254291
*

0 commit comments

Comments
 (0)