kvmppc: HMM backend driver to manage pages of secure guest
Subsystems (3)
v3 cover kvmppc: HMM backend driver to manage pages of secure guest
Hi,
A pseries guest can be run as a secure guest on Ultravisor-enabled POWER platforms. On such platforms, this driver will be used to manage the movement of guest pages between the normal memory managed by hypervisor (HV) and secure memory managed by Ultravisor (UV).
Private ZONE_DEVICE memory equal to the amount of secure memory available in the platform for running secure guests is created via a HMM device. The movement of pages between normal and secure memory is done by ->alloc_and_copy() callback routine of migrate_vma().
The page-in or page-out requests from UV will come to HV as hcalls and HV will call back into UV via uvcalls to satisfy these page requests.
These patches apply and work on the base Ultravisor patches posted by Ram Pai at https://www.spinics.net/lists/kvm-ppc/msg14981.html
Changes in v3
=============
- Rebased to latest kernel
- Rebased on top of Ram's base Ultravisor patches, so that all the dependencies are met.
- Get secure memory size from device tree.
- Fix a mm struct leak in page-in and page-out hcalls, thereby allowing LPID recycling (Thanks to Sukadev Bhattiprolu for pointing this out)
v2: https://lists.ozlabs.org/pipermail/linuxppc-dev/2018-November/181669.html
Bharata B Rao (4):
kvmppc: HMM backend driver to manage pages of secure guest
kvmppc: Add support for shared pages in HMM driver
kvmppc: H_SVM_INIT_START and H_SVM_INIT_DONE hcalls
kvmppc: Handle memory plug/unplug to secure VM
arch/powerpc/include/asm/hvcall.h | 9 + arch/powerpc/include/asm/kvm_book3s_hmm.h | 45 ++ arch/powerpc/include/asm/kvm_host.h | 14 + arch/powerpc/include/asm/ucall-api.h | 35 ++ arch/powerpc/include/uapi/asm/uapi_uvcall.h | 5 + arch/powerpc/kvm/Makefile | 3 + arch/powerpc/kvm/book3s_hv.c | 48 ++ arch/powerpc/kvm/book3s_hv_hmm.c | 559 ++++++++++++++++++++ 8 files changed, 718 insertions(+) create mode 100644 arch/powerpc/include/asm/kvm_book3s_hmm.h create mode 100644 arch/powerpc/kvm/book3s_hv_hmm.c
Signature · 1 line
2.17.1
RFC PATCH v3 1/4 kvmppc: HMM backend driver to manage pages of secure guest
HMM driver for KVM PPC to manage page transitions of secure guest via H_SVM_PAGE_IN and H_SVM_PAGE_OUT hcalls.
H_SVM_PAGE_IN: Move the content of a normal page to secure pageH_SVM_PAGE_OUT: Move the content of a secure page to normal page
--- arch/powerpc/include/asm/hvcall.h | 4 + arch/powerpc/include/asm/kvm_book3s_hmm.h | 33 ++ arch/powerpc/include/asm/kvm_host.h | 14 + arch/powerpc/include/asm/ucall-api.h | 19 + arch/powerpc/include/uapi/asm/uapi_uvcall.h | 3 + arch/powerpc/kvm/Makefile | 3 + arch/powerpc/kvm/book3s_hv.c | 22 + arch/powerpc/kvm/book3s_hv_hmm.c | 474 ++++++++++++++++++++ 8 files changed, 572 insertions(+) create mode 100644 arch/powerpc/include/asm/kvm_book3s_hmm.h create mode 100644 arch/powerpc/kvm/book3s_hv_hmm.c
arch/powerpc/include/asm/hvcall.h +4 -0
@@ -337,6 +337,10 @@#define H_TLB_INVALIDATE 0xF808#define H_COPY_TOFROM_GUEST 0xF80C +/* Platform-specific hcalls used by the Ultravisor */ +#define H_SVM_PAGE_IN 0xEF00 +#define H_SVM_PAGE_OUT 0xEF04 +/* Values for 2nd argument to H_SET_MODE */ #define H_SET_MODE_RESOURCE_SET_CIABR 1#define H_SET_MODE_RESOURCE_SET_DAWR 2
arch/powerpc/include/asm/kvm_book3s_hmm.h new file +33 -0
@@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef __POWERPC_KVM_PPC_HMM_H__ +#define __POWERPC_KVM_PPC_HMM_H__ + +#ifdef CONFIG_PPC_KVM_UV +extern unsigned long kvmppc_h_svm_page_in (struct kvm *kvm, +unsigned int lpid, +unsigned long gra, +unsigned long flags, +unsigned long page_shift); +extern unsigned long kvmppc_h_svm_page_out (struct kvm *kvm, +unsigned int lpid, +unsigned long gra, +unsigned long flags, +unsigned long page_shift); +#else +static inline unsigned long +kvmppc_h_svm_page_in (struct kvm *kvm,unsigned int lpid, +unsigned long gra,unsigned long flags, +unsigned long page_shift) +{ +return H_UNSUPPORTED ; +} + +static inline unsigned long +kvmppc_h_svm_page_out (struct kvm *kvm,unsigned int lpid, +unsigned long gra,unsigned long flags, +unsigned long page_shift) +{ +return H_UNSUPPORTED ; +} +#endif /* CONFIG_PPC_KVM_UV */ +#endif /* __POWERPC_KVM_PPC_HMM_H__ */
arch/powerpc/include/asm/kvm_host.h +14 -0
@@ -846,4 +846,18 @@ static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {}static inline void kvm_arch_vcpu_unblocking (struct kvm_vcpu *vcpu) {}static inline void kvm_arch_vcpu_block_finish (struct kvm_vcpu *vcpu) {} +#ifdef CONFIG_PPC_KVM_UV +extern int kvmppc_hmm_init (void ); +extern void kvmppc_hmm_free (void ); +extern void kvmppc_hmm_release_pfns (struct kvm_memory_slot *free); +#else +static inline int kvmppc_hmm_init (void ) +{ +return 0 ; +} + +static inline void kvmppc_hmm_free (void ) {} +static inline void kvmppc_hmm_release_pfns (struct kvm_memory_slot *free) {} +#endif /* CONFIG_PPC_KVM_UV */ +#endif /* __POWERPC_KVM_HOST_H__ */
arch/powerpc/include/asm/ucall-api.h +19 -0
@@ -9,6 +9,8 @@#include <linux/of_fdt.h> #include <linux/libfdt.h> +#define U_SUCCESS 0 +extern unsigned int smf_state;static inline bool smf_enabled (void ) {
@@ -54,5 +56,22 @@ static inline int uv_restricted_spr_read(u64 reg, u64 *val)return rc; } +static inline int uv_page_in (u64 lpid,u64 src_ra,u64 dst_gpa,u64 flags, +u64 page_shift) +{ +unsigned long retbuf[PLPAR_UCALL_BUFSIZE ]; + +return plpar_ucall (UV_PAGE_IN , retbuf, lpid, src_ra, dst_gpa, flags, + page_shift); +} + +static inline int uv_page_out (u64 lpid,u64 dst_ra,u64 src_gpa,u64 flags, +u64 page_shift) +{ +unsigned long retbuf[PLPAR_UCALL_BUFSIZE ]; + +return plpar_ucall (UV_PAGE_OUT , retbuf, lpid, dst_ra, src_gpa, flags, + page_shift); +}#endif /* __ASSEMBLY__ */ #endif /* _ASM_POWERPC_UCALL_API_H */
arch/powerpc/include/uapi/asm/uapi_uvcall.h +3 -0
@@ -12,4 +12,7 @@#define UV_RESTRICTED_SPR_WRITE 0xf108#define UV_RESTRICTED_SPR_READ 0xf10C#define UV_RETURN 0xf11C +#define UV_PAGE_IN 0xF128 +#define UV_PAGE_OUT 0xF12C +#endif /* #ifndef UAPI_UC_H */
arch/powerpc/kvm/Makefile +3 -0
@@ -76,6 +76,9 @@ kvm-hv-y += \ book3s_64_mmu_radix.o \ book3s_hv_nested.o +kvm-hv-$(CONFIG_PPC_KVM_UV) += \ + book3s_hv_hmm.o + kvm-hv-$(CONFIG_PPC_TRANSACTIONAL_MEM) += \ book3s_hv_tm.o
arch/powerpc/kvm/book3s_hv.c +22 -0
@@ -74,6 +74,8 @@#include <asm/opal.h> #include <asm/xics.h> #include <asm/xive.h> +#include <asm/kvm_host.h> +#include <asm/kvm_book3s_hmm.h> #include "book3s.h"
@@ -1001,6 +1003,20 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)if (nesting_enabled (vcpu->kvm)) ret =kvmhv_copy_tofrom_guest_nested (vcpu);break ; +case H_SVM_PAGE_IN : + ret =kvmppc_h_svm_page_in (vcpu->kvm, +kvmppc_get_gpr (vcpu,4 ), +kvmppc_get_gpr (vcpu,5 ), +kvmppc_get_gpr (vcpu,6 ), +kvmppc_get_gpr (vcpu,7 )); +break ; +case H_SVM_PAGE_OUT : + ret =kvmppc_h_svm_page_out (vcpu->kvm, +kvmppc_get_gpr (vcpu,4 ), +kvmppc_get_gpr (vcpu,5 ), +kvmppc_get_gpr (vcpu,6 ), +kvmppc_get_gpr (vcpu,7 )); +break ;default :return RESUME_HOST ; }
@@ -4354,6 +4370,7 @@ static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free, struct kvm_memory_slot *dont) {if (!dont || free->arch.rmap != dont->arch.rmap) { +kvmppc_hmm_release_pfns (free);vfree (free->arch.rmap); free->arch.rmap =NULL ; }
@@ -5429,11 +5446,16 @@ static int kvmppc_book3s_init_hv(void) no_mixing_hpt_and_radix = true; } + r =kvmppc_hmm_init (); +if (r <0 ) +pr_err ("KVM-HV: kvmppc_hmm_init failed %d\n" , r); +return r; }static void kvmppc_book3s_exit_hv (void ) { +kvmppc_hmm_free ();kvmppc_free_host_rm_ops ();if (kvmppc_radix_possible ())kvmppc_radix_exit ();
arch/powerpc/kvm/book3s_hv_hmm.c new file +474 -0
474 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH v3 2/4 kvmppc: Add support for shared pages in HMM driver
In reply to Bharata B RaoA secure guest will share some of its pages with hypervisor (Eg. virtio bounce buffers etc). Support shared pages in HMM driver.
--- arch/powerpc/include/asm/hvcall.h | 3 ++ arch/powerpc/kvm/book3s_hv_hmm.c | 58 +++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/hvcall.h +3 -0
@@ -337,6 +337,9 @@#define H_TLB_INVALIDATE 0xF808#define H_COPY_TOFROM_GUEST 0xF80C +/* Flags for H_SVM_PAGE_IN */ +#define H_PAGE_IN_SHARED 0x1 +/* Platform-specific hcalls used by the Ultravisor */ #define H_SVM_PAGE_IN 0xEF00#define H_SVM_PAGE_OUT 0xEF04
arch/powerpc/kvm/book3s_hv_hmm.c +55 -3
84 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH v3 3/4 kvmppc: H_SVM_INIT_START and H_SVM_INIT_DONE hcalls
In reply to Bharata B RaoH_SVM_INIT_START: Initiate securing a VMH_SVM_INIT_DONE: Conclude securing a VM
During early guest init, these hcalls will be issued by UV. As part of these hcalls, [un]register memslots with UV.
--- arch/powerpc/include/asm/hvcall.h | 2 ++ arch/powerpc/include/asm/kvm_book3s_hmm.h | 12 ++++++++ arch/powerpc/include/asm/ucall-api.h | 9 ++++++ arch/powerpc/include/uapi/asm/uapi_uvcall.h | 1 + arch/powerpc/kvm/book3s_hv.c | 7 +++++ arch/powerpc/kvm/book3s_hv_hmm.c | 33 +++++++++++++++++++++ 6 files changed, 64 insertions(+)
arch/powerpc/include/asm/hvcall.h +2 -0
@@ -343,6 +343,8 @@/* Platform-specific hcalls used by the Ultravisor */ #define H_SVM_PAGE_IN 0xEF00#define H_SVM_PAGE_OUT 0xEF04 +#define H_SVM_INIT_START 0xEF08 +#define H_SVM_INIT_DONE 0xEF0C/* Values for 2nd argument to H_SET_MODE */ #define H_SET_MODE_RESOURCE_SET_CIABR 1
arch/powerpc/include/asm/kvm_book3s_hmm.h +12 -0
@@ -13,6 +13,8 @@ extern unsigned long kvmppc_h_svm_page_out(struct kvm *kvm,unsigned long gra, unsigned long flags, unsigned long page_shift); +extern unsigned long kvmppc_h_svm_init_start (struct kvm *kvm); +extern unsigned long kvmppc_h_svm_init_done (struct kvm *kvm);#else static inline unsigned long kvmppc_h_svm_page_in(struct kvm *kvm, unsigned int lpid,
@@ -29,5 +31,15 @@ kvmppc_h_svm_page_out(struct kvm *kvm, unsigned int lpid, {return H_UNSUPPORTED ; } + +static inline unsigned long kvmppc_h_svm_init_start (struct kvm *kvm) +{ +return H_UNSUPPORTED ; +} + +static inline unsigned long kvmppc_h_svm_init_done (struct kvm *kvm) +{ +return H_UNSUPPORTED ; +}#endif /* CONFIG_PPC_KVM_UV */ #endif /* __POWERPC_KVM_PPC_HMM_H__ */
arch/powerpc/include/asm/ucall-api.h +9 -0
@@ -73,5 +73,14 @@ static inline int uv_page_out(u64 lpid, u64 dst_ra, u64 src_gpa, u64 flags,return plpar_ucall (UV_PAGE_OUT , retbuf, lpid, dst_ra, src_gpa, flags, page_shift); } + +static inline int uv_register_mem_slot (u64 lpid,u64 start_gpa,u64 size, +u64 flags,u64 slotid) +{ +unsigned long retbuf[PLPAR_UCALL_BUFSIZE ]; + +return plpar_ucall (UV_REGISTER_MEM_SLOT , retbuf, lpid, start_gpa, + size, flags, slotid); +}#endif /* __ASSEMBLY__ */ #endif /* _ASM_POWERPC_UCALL_API_H */
arch/powerpc/include/uapi/asm/uapi_uvcall.h +1 -0
@@ -12,6 +12,7 @@#define UV_RESTRICTED_SPR_WRITE 0xf108#define UV_RESTRICTED_SPR_READ 0xf10C#define UV_RETURN 0xf11C +#define UV_REGISTER_MEM_SLOT 0xF120#define UV_PAGE_IN 0xF128#define UV_PAGE_OUT 0xF12C
arch/powerpc/kvm/book3s_hv.c +7 -0
@@ -1017,6 +1017,13 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)kvmppc_get_gpr (vcpu,6 ), kvmppc_get_gpr(vcpu, 7));break ; +case H_SVM_INIT_START : + ret =kvmppc_h_svm_init_start (vcpu->kvm); +break ; +case H_SVM_INIT_DONE : + ret =kvmppc_h_svm_init_done (vcpu->kvm); +break ; +default :return RESUME_HOST ; }
arch/powerpc/kvm/book3s_hv_hmm.c +33 -0
@@ -55,6 +55,39 @@ struct kvmppc_hmm_migrate_args {unsigned long page_shift; }; +unsigned long kvmppc_h_svm_init_start (struct kvm *kvm) +{ +struct kvm_memslots *slots; +struct kvm_memory_slot *memslot; +int ret =H_SUCCESS ; +int srcu_idx; + + srcu_idx =srcu_read_lock (&kvm->srcu); + slots =kvm_memslots (kvm); +kvm_for_each_memslot (memslot, slots) { + ret =uv_register_mem_slot (kvm->arch.lpid, + memslot->base_gfn <<PAGE_SHIFT , + memslot->npages *PAGE_SIZE , +0 , memslot->id); +if (ret <0 ) { + ret =H_PARAMETER ;/* TODO: proper retval */ + goto out; + } + } + kvm->arch.secure_guest = true; +out: +srcu_read_unlock (&kvm->srcu, srcu_idx); +return ret; +} + +unsigned long kvmppc_h_svm_init_done (struct kvm *kvm) +{ +if (!kvm->arch.secure_guest) +return H_UNSUPPORTED ; + +return H_SUCCESS ; +} +#define KVMPPC_PFN_HMM (0x1ULL << 61)static inline bool kvmppc_is_hmm_pfn (unsigned long pfn)
Signature · 1 line
2.17.1
RFC PATCH v3 4/4 kvmppc: Handle memory plug/unplug to secure VM
In reply to Bharata B RaoRegister the new memslot with UV during plug and unregister the memslot during unplug.
--- arch/powerpc/include/asm/ucall-api.h | 7 +++++++ arch/powerpc/include/uapi/asm/uapi_uvcall.h | 1 + arch/powerpc/kvm/book3s_hv.c | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+)
arch/powerpc/include/asm/ucall-api.h +7 -0
@@ -82,5 +82,12 @@ static inline int uv_register_mem_slot(u64 lpid, u64 start_gpa, u64 size,return plpar_ucall (UV_REGISTER_MEM_SLOT , retbuf, lpid, start_gpa, size, flags, slotid); } + +static inline int uv_unregister_mem_slot (u64 lpid,u64 slotid) +{ +unsigned long retbuf[PLPAR_UCALL_BUFSIZE ]; + +return plpar_ucall (UV_UNREGISTER_MEM_SLOT , retbuf, lpid, slotid); +}#endif /* __ASSEMBLY__ */ #endif /* _ASM_POWERPC_UCALL_API_H */
arch/powerpc/include/uapi/asm/uapi_uvcall.h +1 -0
@@ -13,6 +13,7 @@#define UV_RESTRICTED_SPR_READ 0xf10C#define UV_RETURN 0xf11C#define UV_REGISTER_MEM_SLOT 0xF120 +#define UV_UNREGISTER_MEM_SLOT 0xF124#define UV_PAGE_IN 0xF128#define UV_PAGE_OUT 0xF12C
arch/powerpc/kvm/book3s_hv.c +19 -0
@@ -76,6 +76,7 @@#include <asm/xive.h> #include <asm/kvm_host.h> #include <asm/kvm_book3s_hmm.h> +#include <asm/ucall-api.h> #include "book3s.h"
@@ -4433,6 +4434,24 @@ static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,if (change ==KVM_MR_FLAGS_ONLY &&kvm_is_radix (kvm) && ((new->flags ^ old->flags) &KVM_MEM_LOG_DIRTY_PAGES ))kvmppc_radix_flush_memslot (kvm, old); +/* +* If UV hasn't yet called H_SVM_INIT_START, don't register memslots. +*/ +if (!kvm->arch.secure_guest) +return ; + +/* +* TODO: Handle KVM_MR_MOVE +*/ +if (change ==KVM_MR_CREATE ) { +uv_register_mem_slot (kvm->arch.lpid, + new->base_gfn <<PAGE_SHIFT , + new->npages *PAGE_SIZE , +0 , + new->id); + }else if (change ==KVM_MR_DELETE ) { +uv_unregister_mem_slot (kvm->arch.lpid, old->id); + } }/*
Signature · 1 line
2.17.1
Re: [RFC PATCH v3 3/4] kvmppc: H_SVM_INIT_START and H_SVM_INIT_DONE hcalls
In reply to Bharata B RaoQuoted 5 lines · Bharata B Rao
H_SVM_INIT_START: Initiate securing a VMH_SVM_INIT_DONE: Conclude securing a VMDuring early guest init, these hcalls will be issued by UV. As part of these hcalls, [un]register memslots with UV.
That last sentence is a bit misleading as it implies that H_SVM_INIT_DONE causes us to unregister the memslots with the UV, which is not the case. Shouldn't it be "As part of H_SVM_INIT_START, register all existing memslots with the UV"?
Also, do we subsequently communicate changes in the memslots to the UV?
Paul.
Re: [RFC PATCH v3 3/4] kvmppc: H_SVM_INIT_START and H_SVM_INIT_DONE hcalls
Quoted 11 lines · Paul Mackerras
Quoted 5 lines · Bharata B Rao
H_SVM_INIT_START: Initiate securing a VMH_SVM_INIT_DONE: Conclude securing a VMDuring early guest init, these hcalls will be issued by UV. As part of these hcalls, [un]register memslots with UV.
That last sentence is a bit misleading as it implies that
H_SVM_INIT_DONEcauses us to unregister the memslots with the UV, which is not the case. Shouldn't it be "As part ofH_SVM_INIT_START, register all existing memslots with the UV"?
Ok, makes sense to rephrase.
Quoted 3 lines
Also, do we subsequently communicate changes in the memslots to the UV?
Yes, currently handing KVM_MR_DELETE, yet to handle KVM_MR_MOVE
Regards,
Bharata.