Skip to contentSkip to conversation

KVMPPC driver to manage secure guest pages

PATCH Superseded Bharata B Rao 4 participants 14 messages Aug 9, 2019 – Aug 22, 2019

Subsystems (5)

v6 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#1 lore raw

PATCH v6 1/7 kvmppc: Driver to manage pages of secure guest

In reply to Bharata B Rao

KVMPPC driver 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 page
H_SVM_PAGE_OUT: Move the content of a secure page to normal page

Private ZONE_DEVICE memory equal to the amount of secure memory available in the platform for running secure guests is created via a char device. Whenever a page belonging to the guest becomes secure, a page from this private device memory is used to represent and track that secure page on the HV side. The movement of pages between normal and secure memory is done via migrate_vma_pages() using UV_PAGE_IN and UV_PAGE_OUT ucalls.

---
 arch/powerpc/include/asm/hvcall.h          |   4 +
 arch/powerpc/include/asm/kvm_book3s_devm.h |  29 ++
 arch/powerpc/include/asm/kvm_host.h        |  12 +
 arch/powerpc/include/asm/ultravisor-api.h  |   2 +
 arch/powerpc/include/asm/ultravisor.h      |  14 +
 arch/powerpc/kvm/Makefile                  |   3 +
 arch/powerpc/kvm/book3s_hv.c               |  19 +
 arch/powerpc/kvm/book3s_hv_devm.c          | 492 +++++++++++++++++++++
 8 files changed, 575 insertions(+)
 create mode 100644 arch/powerpc/include/asm/kvm_book3s_devm.h
 create mode 100644 arch/powerpc/kvm/book3s_hv_devm.c
arch/powerpc/include/asm/hvcall.h +4 -0
index 463c63a9fcf1..2f6b952deb0f 100644
@@ -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_devm.h new file +29 -0
new file mode 100644
index 000000000000..21f3de5f2acb
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef __POWERPC_KVM_PPC_HMM_H__
+#define __POWERPC_KVM_PPC_HMM_H__
+
+#ifdef CONFIG_PPC_UV
+extern unsigned long kvmppc_h_svm_page_in(struct kvm *kvm,
+					  unsigned long gra,
+					  unsigned long flags,
+					  unsigned long page_shift);
+extern unsigned long kvmppc_h_svm_page_out(struct kvm *kvm,
+					  unsigned long gra,
+					  unsigned long flags,
+					  unsigned long page_shift);
+#else
+static inline unsigned long
+kvmppc_h_svm_page_in(struct kvm *kvm, 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 long gra,
+		      unsigned long flags, unsigned long page_shift)
+{
+	return H_UNSUPPORTED;
+}
+#endif /* CONFIG_PPC_UV */
+#endif /* __POWERPC_KVM_PPC_HMM_H__ */
arch/powerpc/include/asm/kvm_host.h +12 -0
index 4bb552d639b8..86bbe607ad7e 100644
@@ -849,4 +849,16 @@ 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_UV
+extern int kvmppc_devm_init(void);
+extern void kvmppc_devm_free(void);
+#else
+static inline int kvmppc_devm_init(void)
+{
+	return 0;
+}
+
+static inline void kvmppc_devm_free(void) {}
+#endif /* CONFIG_PPC_UV */
+
 #endif /* __POWERPC_KVM_HOST_H__ */
arch/powerpc/include/asm/ultravisor-api.h +2 -0
index 6a0f9c74f959..1cd1f595fd81 100644
@@ -25,5 +25,7 @@
 /* opcodes */
 #define UV_WRITE_PATE			0xF104
 #define UV_RETURN			0xF11C
+#define UV_PAGE_IN			0xF128
+#define UV_PAGE_OUT			0xF12C
 
 #endif /* _ASM_POWERPC_ULTRAVISOR_API_H */
arch/powerpc/include/asm/ultravisor.h +14 -0
index 6fe1f365dec8..d668a59e099b 100644
@@ -19,4 +19,18 @@ static inline int uv_register_pate(u64 lpid, u64 dw0, u64 dw1)
 	return ucall_norets(UV_WRITE_PATE, lpid, dw0, dw1);
 }
 
+static inline int uv_page_in(u64 lpid, u64 src_ra, u64 dst_gpa, u64 flags,
+			     u64 page_shift)
+{
+	return ucall_norets(UV_PAGE_IN, 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)
+{
+	return ucall_norets(UV_PAGE_OUT, lpid, dst_ra, src_gpa, flags,
+			    page_shift);
+}
+
 #endif	/* _ASM_POWERPC_ULTRAVISOR_H */
arch/powerpc/kvm/Makefile +3 -0
index 4c67cc79de7c..16b40590e67c 100644
@@ -71,6 +71,9 @@ kvm-hv-y += \
 	book3s_64_mmu_radix.o \
 	book3s_hv_nested.o
 
+kvm-hv-$(CONFIG_PPC_UV) += \
+	book3s_hv_devm.o
+
 kvm-hv-$(CONFIG_PPC_TRANSACTIONAL_MEM) += \
 	book3s_hv_tm.o
 
arch/powerpc/kvm/book3s_hv.c +19 -0
index ec1804f822af..00b43ee8b693 100644
@@ -72,6 +72,8 @@
 #include <asm/xics.h>
 #include <asm/xive.h>
 #include <asm/hw_breakpoint.h>
+#include <asm/kvm_host.h>
+#include <asm/kvm_book3s_devm.h>
 
 #include "book3s.h"
 
@@ -1075,6 +1077,18 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
 					 kvmppc_get_gpr(vcpu, 5),
 					 kvmppc_get_gpr(vcpu, 6));
 		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));
+		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));
+		break;
 	default:
 		return RESUME_HOST;
 	}
@@ -5510,11 +5524,16 @@ static int kvmppc_book3s_init_hv(void)
 			no_mixing_hpt_and_radix = true;
 	}
 
+	r = kvmppc_devm_init();
+	if (r < 0)
+		pr_err("KVM-HV: kvmppc_devm_init failed %d\n", r);
+
 	return r;
 }
 
 static void kvmppc_book3s_exit_hv(void)
 {
+	kvmppc_devm_free();
 	kvmppc_free_host_rm_ops();
 	if (kvmppc_radix_possible())
 		kvmppc_radix_exit();
arch/powerpc/kvm/book3s_hv_devm.c new file +492 -0

492 lines not on this page · raw message

Signature · 1 line
2.21.0
#9 lore raw

PATCH v6 3/7 kvmppc: H_SVM_INIT_START and H_SVM_INIT_DONE hcalls

In reply to Bharata B Rao

H_SVM_INIT_START: Initiate securing a VM
H_SVM_INIT_DONE: Conclude securing a VM

As part of H_SVM_INIT_START, register all existing memslots with the UV. H_SVM_INIT_DONE call by UV informs HV that transition of the guest to secure mode is complete.

These two states (transition to secure mode STARTED and transition to secure mode COMPLETED) are recorded in kvm->arch.secure_guest. Setting these states will cause the assembly code that enters the guest to call the UV_RETURN ucall instead of trying to enter the guest directly.

---
 arch/powerpc/include/asm/hvcall.h          |  2 ++
 arch/powerpc/include/asm/kvm_book3s_devm.h | 12 ++++++++
 arch/powerpc/include/asm/kvm_host.h        |  4 +++
 arch/powerpc/include/asm/ultravisor-api.h  |  1 +
 arch/powerpc/include/asm/ultravisor.h      |  7 +++++
 arch/powerpc/kvm/book3s_hv.c               |  7 +++++
 arch/powerpc/kvm/book3s_hv_devm.c          | 34 ++++++++++++++++++++++
 7 files changed, 67 insertions(+)
arch/powerpc/include/asm/hvcall.h +2 -0
index 05b8536f6653..fa7695928e30 100644
@@ -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_devm.h +12 -0
index 21f3de5f2acb..8c7aacabb2e0 100644
@@ -11,6 +11,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 long gra,
@@ -25,5 +27,15 @@ kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gra,
 {
 	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_UV */
 #endif /* __POWERPC_KVM_PPC_HMM_H__ */
arch/powerpc/include/asm/kvm_host.h +4 -0
index 86bbe607ad7e..1827c22909cd 100644
@@ -261,6 +261,10 @@ struct kvm_hpt_info {
 
 struct kvm_resize_hpt;
 
+/* Flag values for kvm_arch.secure_guest */
+#define KVMPPC_SECURE_INIT_START	0x1 /* H_SVM_INIT_START has been called */
+#define KVMPPC_SECURE_INIT_DONE		0x2 /* H_SVM_INIT_DONE completed */
+
 struct kvm_arch {
 	unsigned int lpid;
 	unsigned int smt_mode;		/* # vcpus per virtual core */
arch/powerpc/include/asm/ultravisor-api.h +1 -0
index 1cd1f595fd81..c578d9b13a56 100644
@@ -25,6 +25,7 @@
 /* opcodes */
 #define UV_WRITE_PATE			0xF104
 #define UV_RETURN			0xF11C
+#define UV_REGISTER_MEM_SLOT		0xF120
 #define UV_PAGE_IN			0xF128
 #define UV_PAGE_OUT			0xF12C
 
arch/powerpc/include/asm/ultravisor.h +7 -0
index d668a59e099b..8a722c575c56 100644
@@ -33,4 +33,11 @@ static inline int uv_page_out(u64 lpid, u64 dst_ra, u64 src_gpa, u64 flags,
 			    page_shift);
 }
 
+static inline int uv_register_mem_slot(u64 lpid, u64 start_gpa, u64 size,
+				       u64 flags, u64 slotid)
+{
+	return ucall_norets(UV_REGISTER_MEM_SLOT, lpid, start_gpa,
+			    size, flags, slotid);
+}
+
 #endif	/* _ASM_POWERPC_ULTRAVISOR_H */
arch/powerpc/kvm/book3s_hv.c +7 -0
index 00b43ee8b693..33b8ebffbef0 100644
@@ -1089,6 +1089,13 @@ int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
 					    kvmppc_get_gpr(vcpu, 5),
 					    kvmppc_get_gpr(vcpu, 6));
 		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_devm.c +34 -0
index c9189e58401d..c55bb5f57928 100644
@@ -65,6 +65,40 @@ struct kvmppc_devm_copy_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;
+			goto out;
+		}
+	}
+	kvm->arch.secure_guest |= KVMPPC_SECURE_INIT_START;
+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 & KVMPPC_SECURE_INIT_START))
+		return H_UNSUPPORTED;
+
+	kvm->arch.secure_guest |= KVMPPC_SECURE_INIT_DONE;
+	return H_SUCCESS;
+}
+
 /*
  * Bits 60:56 in the rmap entry will be used to identify the
  * different uses/functions of rmap. This definition with move
Signature · 1 line
2.21.0
#0 lore raw

v6 cover KVMPPC driver to manage secure guest pages

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 char device. Whenever a page belonging to the guest becomes secure, a page from this private device memory is used to represent and track that secure page on the HV side. The movement of pages between normal and secure memory is done via migrate_vma_pages().

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 are against Christoph Hellwig's migrate_vma-cleanup.2 branch
(http://git.infradead.org/users/hch/misc.git/shortlog/refs/heads/migrate_vma-cleanup.2)

plus

Claudio Carvalho's base ultravisor enablement patchset
(https://lore.kernel.org/linuxppc-dev/[email protected]/T/#t)

These patches along with Claudio's above patches are required to run a secure pseries guest on KVM.

Changes in v6
=============
Updated the driver to account for the changes in HMM and migrate_vma() by Christoph Hellwig.
- Not using any HMM routines any more.
- Switched to using migrate_vma_pages()

v5: https://www.mail-archive.com/[email protected]/msg153294.html

Anshuman Khandual (1):
KVM: PPC: Ultravisor: Add PPC_UV config option

Bharata B Rao (6):
kvmppc: Driver to manage pages of secure guest
kvmppc: Shared pages support for secure guests
kvmppc: H_SVM_INIT_START and H_SVM_INIT_DONE hcalls
kvmppc: Handle memory plug/unplug to secure VM
kvmppc: Radix changes for secure guest
kvmppc: Support reset of secure guest

 Documentation/virtual/kvm/api.txt          |  18 +
 arch/powerpc/Kconfig                       |  18 +
 arch/powerpc/include/asm/hvcall.h          |   9 +
 arch/powerpc/include/asm/kvm_book3s_devm.h |  48 ++
 arch/powerpc/include/asm/kvm_host.h        |  28 +
 arch/powerpc/include/asm/kvm_ppc.h         |   2 +
 arch/powerpc/include/asm/ultravisor-api.h  |   6 +
 arch/powerpc/include/asm/ultravisor.h      |  36 ++
 arch/powerpc/kvm/Makefile                  |   3 +
 arch/powerpc/kvm/book3s_64_mmu_radix.c     |  22 +
 arch/powerpc/kvm/book3s_hv.c               | 115 ++++
 arch/powerpc/kvm/book3s_hv_devm.c          | 668 +++++++++++++++++++++
 arch/powerpc/kvm/powerpc.c                 |  12 +
 include/uapi/linux/kvm.h                   |   1 +
 14 files changed, 986 insertions(+)
 create mode 100644 arch/powerpc/include/asm/kvm_book3s_devm.h
 create mode 100644 arch/powerpc/kvm/book3s_hv_devm.c
Signature · 1 line
2.21.0
#11 lore raw

PATCH v6 5/7 kvmppc: Radix changes for secure guest

- After the guest becomes secure, when we handle a page fault of a page belonging to SVM in HV, send that page to UV via UV_PAGE_IN.
- Whenever a page is unmapped on the HV side, inform UV via UV_PAGE_INVAL.
- Ensure all those routines that walk the secondary page tables of the guest don't do so in case of secure VM. For secure guest, the active secondary page tables are in secure memory and the secondary page tables in HV are freed when guest becomes secure.

---
 arch/powerpc/include/asm/kvm_host.h       | 12 ++++++++++++
 arch/powerpc/include/asm/ultravisor-api.h |  1 +
 arch/powerpc/include/asm/ultravisor.h     |  5 +++++
 arch/powerpc/kvm/book3s_64_mmu_radix.c    | 22 ++++++++++++++++++++++
 arch/powerpc/kvm/book3s_hv_devm.c         | 20 ++++++++++++++++++++
 5 files changed, 60 insertions(+)
arch/powerpc/include/asm/kvm_host.h +12 -0
index 1827c22909cd..db680d7f5779 100644
@@ -856,6 +856,8 @@ static inline void kvm_arch_vcpu_block_finish(struct kvm_vcpu *vcpu) {}
 #ifdef CONFIG_PPC_UV
 extern int kvmppc_devm_init(void);
 extern void kvmppc_devm_free(void);
+extern bool kvmppc_is_guest_secure(struct kvm *kvm);
+extern int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gpa);
 #else
 static inline int kvmppc_devm_init(void)
 {
@@ -863,6 +865,16 @@ static inline int kvmppc_devm_init(void)
 }
 
 static inline void kvmppc_devm_free(void) {}
+
+static inline bool kvmppc_is_guest_secure(struct kvm *kvm)
+{
+	return false;
+}
+
+static inline int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gpa)
+{
+	return -EFAULT;
+}
 #endif /* CONFIG_PPC_UV */
 
 #endif /* __POWERPC_KVM_HOST_H__ */
arch/powerpc/include/asm/ultravisor-api.h +1 -0
index 46b1ee381695..cf200d4ce703 100644
@@ -29,5 +29,6 @@
 #define UV_UNREGISTER_MEM_SLOT		0xF124
 #define UV_PAGE_IN			0xF128
 #define UV_PAGE_OUT			0xF12C
+#define UV_PAGE_INVAL			0xF138
 
 #endif /* _ASM_POWERPC_ULTRAVISOR_API_H */
arch/powerpc/include/asm/ultravisor.h +5 -0
index 79c415bf5ee8..640db659c8c8 100644
@@ -45,4 +45,9 @@ static inline int uv_unregister_mem_slot(u64 lpid, u64 slotid)
 	return ucall_norets(UV_UNREGISTER_MEM_SLOT, lpid, slotid);
 }
 
+static inline int uv_page_inval(u64 lpid, u64 gpa, u64 page_shift)
+{
+	return ucall_norets(UV_PAGE_INVAL, lpid, gpa, page_shift);
+}
+
 #endif	/* _ASM_POWERPC_ULTRAVISOR_H */
arch/powerpc/kvm/book3s_64_mmu_radix.c +22 -0
index 2d415c36a61d..93ad34e63045 100644
@@ -19,6 +19,8 @@
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
 #include <asm/pte-walk.h>
+#include <asm/ultravisor.h>
+#include <asm/kvm_host.h>
 
 /*
  * Supported radix tree geometry.
@@ -915,6 +917,9 @@ int kvmppc_book3s_radix_page_fault(struct kvm_run *run, struct kvm_vcpu *vcpu,
 	if (!(dsisr & DSISR_PRTABLE_FAULT))
 		gpa |= ea & 0xfff;
 
+	if (kvmppc_is_guest_secure(kvm))
+		return kvmppc_send_page_to_uv(kvm, gpa & PAGE_MASK);
+
 	/* Get the corresponding memslot */
 	memslot = gfn_to_memslot(kvm, gfn);
 
@@ -972,6 +977,11 @@ int kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
 	unsigned long gpa = gfn << PAGE_SHIFT;
 	unsigned int shift;
 
+	if (kvmppc_is_guest_secure(kvm)) {
+		uv_page_inval(kvm->arch.lpid, gpa, PAGE_SIZE);
+		return 0;
+	}
+
 	ptep = __find_linux_pte(kvm->arch.pgtable, gpa, NULL, &shift);
 	if (ptep && pte_present(*ptep))
 		kvmppc_unmap_pte(kvm, ptep, gpa, shift, memslot,
@@ -989,6 +999,9 @@ int kvm_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
 	int ref = 0;
 	unsigned long old, *rmapp;
 
+	if (kvmppc_is_guest_secure(kvm))
+		return ref;
+
 	ptep = __find_linux_pte(kvm->arch.pgtable, gpa, NULL, &shift);
 	if (ptep && pte_present(*ptep) && pte_young(*ptep)) {
 		old = kvmppc_radix_update_pte(kvm, ptep, _PAGE_ACCESSED, 0,
@@ -1013,6 +1026,9 @@ int kvm_test_age_radix(struct kvm *kvm, struct kvm_memory_slot *memslot,
 	unsigned int shift;
 	int ref = 0;
 
+	if (kvmppc_is_guest_secure(kvm))
+		return ref;
+
 	ptep = __find_linux_pte(kvm->arch.pgtable, gpa, NULL, &shift);
 	if (ptep && pte_present(*ptep) && pte_young(*ptep))
 		ref = 1;
@@ -1030,6 +1046,9 @@ static int kvm_radix_test_clear_dirty(struct kvm *kvm,
 	int ret = 0;
 	unsigned long old, *rmapp;
 
+	if (kvmppc_is_guest_secure(kvm))
+		return ret;
+
 	ptep = __find_linux_pte(kvm->arch.pgtable, gpa, NULL, &shift);
 	if (ptep && pte_present(*ptep) && pte_dirty(*ptep)) {
 		ret = 1;
@@ -1082,6 +1101,9 @@ void kvmppc_radix_flush_memslot(struct kvm *kvm,
 	unsigned long gpa;
 	unsigned int shift;
 
+	if (kvmppc_is_guest_secure(kvm))
+		return;
+
 	gpa = memslot->base_gfn << PAGE_SHIFT;
 	spin_lock(&kvm->mmu_lock);
 	for (n = memslot->npages; n; --n) {
arch/powerpc/kvm/book3s_hv_devm.c +20 -0
index c55bb5f57928..9c4b05cd5b0a 100644
@@ -65,6 +65,11 @@ struct kvmppc_devm_copy_args {
 	unsigned long page_shift;
 };
 
+bool kvmppc_is_guest_secure(struct kvm *kvm)
+{
+	return !!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_DONE);
+}
+
 unsigned long kvmppc_h_svm_init_start(struct kvm *kvm)
 {
 	struct kvm_memslots *slots;
@@ -483,6 +488,21 @@ kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long gpa,
 	return ret;
 }
 
+int kvmppc_send_page_to_uv(struct kvm *kvm, unsigned long gpa)
+{
+	unsigned long pfn;
+	int ret;
+
+	pfn = gfn_to_pfn(kvm, gpa >> PAGE_SHIFT);
+	if (is_error_noslot_pfn(pfn))
+		return -EFAULT;
+
+	ret = uv_page_in(kvm->arch.lpid, pfn << PAGE_SHIFT, gpa, 0, PAGE_SHIFT);
+	kvm_release_pfn_clean(pfn);
+
+	return (ret == U_SUCCESS) ? RESUME_GUEST : -EFAULT;
+}
+
 static u64 kvmppc_get_secmem_size(void)
 {
 	struct device_node *np;
Signature · 1 line
2.21.0
#10 lore raw

PATCH v6 4/7 kvmppc: Handle memory plug/unplug to secure VM

In reply to Bharata B Rao

Register the new memslot with UV during plug and unregister the memslot during unplug.

---
 arch/powerpc/include/asm/ultravisor-api.h |  1 +
 arch/powerpc/include/asm/ultravisor.h     |  5 +++++
 arch/powerpc/kvm/book3s_hv.c              | 19 +++++++++++++++++++
 3 files changed, 25 insertions(+)
arch/powerpc/include/asm/ultravisor-api.h +1 -0
index c578d9b13a56..46b1ee381695 100644
@@ -26,6 +26,7 @@
 #define UV_WRITE_PATE			0xF104
 #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/include/asm/ultravisor.h +5 -0
index 8a722c575c56..79c415bf5ee8 100644
@@ -40,4 +40,9 @@ static inline int uv_register_mem_slot(u64 lpid, u64 start_gpa, u64 size,
 			    size, flags, slotid);
 }
 
+static inline int uv_unregister_mem_slot(u64 lpid, u64 slotid)
+{
+	return ucall_norets(UV_UNREGISTER_MEM_SLOT, lpid, slotid);
+}
+
 #endif	/* _ASM_POWERPC_ULTRAVISOR_H */
arch/powerpc/kvm/book3s_hv.c +19 -0
index 33b8ebffbef0..13e31ef3583e 100644
@@ -74,6 +74,7 @@
 #include <asm/hw_breakpoint.h>
 #include <asm/kvm_host.h>
 #include <asm/kvm_book3s_devm.h>
+#include <asm/ultravisor.h>
 
 #include "book3s.h"
 
@@ -4504,6 +4505,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.21.0
#13 lore raw

PATCH v6 7/7 KVM: PPC: Ultravisor: Add PPC_UV config option

In reply to Bharata B Rao

From: Anshuman Khandual <[email protected]>

CONFIG_PPC_UV adds support for ultravisor.

[ Update config help and commit message ]
---
 arch/powerpc/Kconfig | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)
arch/powerpc/Kconfig +18 -0
index d8dcd8820369..8b36ca5ed3b0 100644
@@ -448,6 +448,24 @@ config PPC_TRANSACTIONAL_MEM
 	help
 	  Support user-mode Transactional Memory on POWERPC.
 
+config PPC_UV
+	bool "Ultravisor support"
+	depends on KVM_BOOK3S_HV_POSSIBLE
+	select ZONE_DEVICE
+	select MIGRATE_VMA_HELPER
+	select DEV_PAGEMAP_OPS
+	select DEVICE_PRIVATE
+	select MEMORY_HOTPLUG
+	select MEMORY_HOTREMOVE
+	default n
+	help
+	  This option paravirtualizes the kernel to run in POWER platforms that
+	  supports the Protected Execution Facility (PEF). In such platforms,
+	  the ultravisor firmware runs at a privilege level above the
+	  hypervisor.
+
+	  If unsure, say "N".
+
 config LD_HEAD_STUB_CATCH
 	bool "Reserve 256 bytes to cope with linker stubs in HEAD text" if EXPERT
 	depends on PPC64
Signature · 1 line
2.21.0
#12 lore raw

PATCH v6 6/7 kvmppc: Support reset of secure guest

In reply to Bharata B Rao

Add support for reset of secure guest via a new ioctl KVM_PPC_SVM_OFF. This ioctl will be issued by QEMU during reset and includes the the following steps:

- Ask UV to terminate the guest via UV_SVM_TERMINATE ucall
- Unpin the VPA pages so that they can be migrated back to secure side when guest becomes secure again. This is required because pinned pages can't be migrated.
- Reinitialize guest's partitioned scoped page tables. These are freed when guest become secure (H_SVM_INIT_DONE)
- Release all device pages of the secure guest.

After these steps, guest is ready to issue UV_ESM call once again to switch to secure mode.

[Implementation of uv_svm_terminate() and its call from guest shutdown path]

[Unpinning of VPA pages]
---
 Documentation/virtual/kvm/api.txt          | 18 ++++++
 arch/powerpc/include/asm/kvm_book3s_devm.h |  7 +++
 arch/powerpc/include/asm/kvm_ppc.h         |  2 +
 arch/powerpc/include/asm/ultravisor-api.h  |  1 +
 arch/powerpc/include/asm/ultravisor.h      |  5 ++
 arch/powerpc/kvm/book3s_hv.c               | 70 ++++++++++++++++++++++
 arch/powerpc/kvm/book3s_hv_devm.c          | 61 +++++++++++++++++++
 arch/powerpc/kvm/powerpc.c                 | 12 ++++
 include/uapi/linux/kvm.h                   |  1 +
 9 files changed, 177 insertions(+)
Documentation/virtual/kvm/api.txt +18 -0
index e54a3f51ddc5..6fdd140dd9af 100644
@@ -4111,6 +4111,24 @@ Valid values for 'action':
 #define KVM_PMU_EVENT_ALLOW 0
 #define KVM_PMU_EVENT_DENY 1
 
+4.121 KVM_PPC_SVM_OFF
+
+Capability: basic
+Architectures: powerpc
+Type: vm ioctl
+Parameters: none
+Returns: 0 on successful completion,
+Errors:
+  EINVAL:    if ultravisor failed to terminate the secure guest
+  ENOMEM:    if hypervisor failed to allocate new radix page tables for guest
+
+This ioctl is used to turn off the secure mode of the guest or transition
+the guest from secure mode to normal mode. This is invoked when the guest
+is reset. This has no effect if called for a normal guest.
+
+This ioctl issues an ultravisor call to terminate the secure guest,
+unpin the VPA pages, reinitialize guest's partition scoped page
+tables and releases all the HMM pages that is associated with this guest.
 
 5. The kvm_run structure
 ------------------------
arch/powerpc/include/asm/kvm_book3s_devm.h +7 -0
index 8c7aacabb2e0..6d97da320400 100644
@@ -13,6 +13,8 @@ extern unsigned long kvmppc_h_svm_page_out(struct kvm *kvm,
 					  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);
+extern void kvmppc_devm_free_memslot_pfns(struct kvm *kvm,
+		struct kvm_memslots *slots);
 #else
 static inline unsigned long
 kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gra,
@@ -37,5 +39,10 @@ static inline unsigned long kvmppc_h_svm_init_done(struct kvm *kvm)
 {
 	return H_UNSUPPORTED;
 }
+
+static inline void kvmppc_devm_free_memslot_pfns(struct kvm *kvm,
+		struct kvm_memslots *slots)
+{
+}
 #endif /* CONFIG_PPC_UV */
 #endif /* __POWERPC_KVM_PPC_HMM_H__ */
arch/powerpc/include/asm/kvm_ppc.h +2 -0
index 2484e6a8f5ca..e4093d067354 100644
@@ -177,6 +177,7 @@ extern void kvm_spapr_tce_release_iommu_group(struct kvm *kvm,
 extern int kvmppc_switch_mmu_to_hpt(struct kvm *kvm);
 extern int kvmppc_switch_mmu_to_radix(struct kvm *kvm);
 extern void kvmppc_setup_partition_table(struct kvm *kvm);
+extern int kvmppc_reinit_partition_table(struct kvm *kvm);
 
 extern long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm,
 				struct kvm_create_spapr_tce_64 *args);
@@ -321,6 +322,7 @@ struct kvmppc_ops {
 			       int size);
 	int (*store_to_eaddr)(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
 			      int size);
+	int (*svm_off)(struct kvm *kvm);
 };
 
 extern struct kvmppc_ops *kvmppc_hv_ops;
arch/powerpc/include/asm/ultravisor-api.h +1 -0
index cf200d4ce703..3a27a0c0be05 100644
@@ -30,5 +30,6 @@
 #define UV_PAGE_IN			0xF128
 #define UV_PAGE_OUT			0xF12C
 #define UV_PAGE_INVAL			0xF138
+#define UV_SVM_TERMINATE		0xF13C
 
 #endif /* _ASM_POWERPC_ULTRAVISOR_API_H */
arch/powerpc/include/asm/ultravisor.h +5 -0
index 640db659c8c8..03d4fd49fa0d 100644
@@ -50,4 +50,9 @@ static inline int uv_page_inval(u64 lpid, u64 gpa, u64 page_shift)
 	return ucall_norets(UV_PAGE_INVAL, lpid, gpa, page_shift);
 }
 
+static inline int uv_svm_terminate(u64 lpid)
+{
+	return ucall_norets(UV_SVM_TERMINATE, lpid);
+}
+
 #endif	/* _ASM_POWERPC_ULTRAVISOR_H */
arch/powerpc/kvm/book3s_hv.c +70 -0

100 lines not on this page · raw message

arch/powerpc/kvm/book3s_hv_devm.c +61 -0

79 lines not on this page · raw message

arch/powerpc/kvm/powerpc.c +12 -0
index 0dba7eb24f92..77dceaa8fb55 100644
@@ -31,6 +31,8 @@
 #include <asm/hvcall.h>
 #include <asm/plpar_wrappers.h>
 #endif
+#include <asm/ultravisor.h>
+#include <asm/kvm_host.h>
 
 #include "timing.h"
 #include "irq.h"
@@ -2415,6 +2417,16 @@ long kvm_arch_vm_ioctl(struct file *filp,
 			r = -EFAULT;
 		break;
 	}
+	case KVM_PPC_SVM_OFF: {
+		struct kvm *kvm = filp->private_data;
+
+		r = 0;
+		if (!kvm->arch.kvm_ops->svm_off)
+			goto out;
+
+		r = kvm->arch.kvm_ops->svm_off(kvm);
+		break;
+	}
 	default: {
 		struct kvm *kvm = filp->private_data;
 		r = kvm->arch.kvm_ops->arch_vm_ioctl(filp, ioctl, arg);
include/uapi/linux/kvm.h +1 -0
index a7c19540ce21..07041a64e21f 100644
@@ -1332,6 +1332,7 @@ struct kvm_s390_ucas_mapping {
 #define KVM_PPC_GET_CPU_CHAR	  _IOR(KVMIO,  0xb1, struct kvm_ppc_cpu_char)
 /* Available with KVM_CAP_PMU_EVENT_FILTER */
 #define KVM_SET_PMU_EVENT_FILTER  _IOW(KVMIO,  0xb2, struct kvm_pmu_event_filter)
+#define KVM_PPC_SVM_OFF		  _IO(KVMIO,  0xb3)
 
 /* ioctl for vm fd */
 #define KVM_CREATE_DEVICE	  _IOWR(KVMIO,  0xe0, struct kvm_create_device)
Signature · 1 line
2.21.0
#8 lore raw

PATCH v6 2/7 kvmppc: Shared pages support for secure guests

In reply to Bharata B Rao

A secure guest will share some of its pages with hypervisor (Eg. virtio bounce buffers etc). Support sharing of pages between hypervisor and ultravisor.

Once a secure page is converted to shared page, stop tracking that page as a device page.

---
 arch/powerpc/include/asm/hvcall.h |  3 ++
 arch/powerpc/kvm/book3s_hv_devm.c | 67 +++++++++++++++++++++++++++++--
 2 files changed, 67 insertions(+), 3 deletions(-)
arch/powerpc/include/asm/hvcall.h +3 -0
index 2f6b952deb0f..05b8536f6653 100644
@@ -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_devm.c +64 -3

93 lines not on this page · raw message

Signature · 1 line
2.21.0
#2 lore raw

Re: [PATCH v6 1/7] kvmppc: Driver to manage pages of secure guest

In reply to Bharata B Rao

arch/powerpc/include/asm/kvm_book3s_devm.h line 13

Quoted 9 lines
arch/powerpc/include/asm/kvm_book3s_devm.h new file +9 -0
+#ifdef CONFIG_PPC_UV
+extern unsigned long kvmppc_h_svm_page_in(struct kvm *kvm,
+					  unsigned long gra,
+					  unsigned long flags,
+					  unsigned long page_shift);
+extern unsigned long kvmppc_h_svm_page_out(struct kvm *kvm,
+					  unsigned long gra,
+					  unsigned long flags,
+					  unsigned long page_shift);

No need for externs on function declarations.

arch/powerpc/kvm/book3s_hv_devm.c line 48

Quoted 7 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +7 -0
+struct kvmppc_devm_device {
+	struct device dev;
+	dev_t devt;
+	struct dev_pagemap pagemap;
+	unsigned long pfn_first, pfn_last;
+	unsigned long *pfn_bitmap;
+};

We shouldn't really need this conaining structucture given that there is only a single global instance of it anyway.

arch/powerpc/kvm/book3s_hv_devm.c line 65

Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6 -0
+struct kvmppc_devm_copy_args {
+	unsigned long *rmap;
+	unsigned int lpid;
+	unsigned long gpa;
+	unsigned long page_shift;
+};

Do we really need this args structure? It is just used in a single function call where passing the arguments might be cleaner.

arch/powerpc/kvm/book3s_hv_devm.c line 153

Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +8 -0
+static void kvmppc_devm_put_page(struct page *page)
+{
+	unsigned long pfn = page_to_pfn(page);
+	unsigned long flags;
+	struct kvmppc_devm_page_pvt *pvt;
+
+	spin_lock_irqsave(&kvmppc_devm_lock, flags);
+	pvt = (struct kvmppc_devm_page_pvt *)page->zone_device_data;

No need for the cast.

arch/powerpc/kvm/book3s_hv_devm.c line 154

Quoted 1 line
arch/powerpc/kvm/book3s_hv_devm.c new file +1 -0
+	page->zone_device_data = 0;

This should be NULL.

arch/powerpc/kvm/book3s_hv_devm.c line 158

Quoted 4 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +4 -0
+
+	bitmap_clear(kvmppc_devm.pfn_bitmap,
+		     pfn - kvmppc_devm.pfn_first, 1);
+	*(pvt->rmap) = 0;

No need for the braces.

arch/powerpc/kvm/book3s_hv_devm.c line 279

Quoted 5 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +5 -0
+	dpage = alloc_page_vma(GFP_HIGHUSER, mig->vma, mig->start);
+	if (!dpage)
+		return -EINVAL;
+	lock_page(dpage);
+	pvt = (struct kvmppc_devm_page_pvt *)spage->zone_device_data;

No need for the cast here.

arch/powerpc/kvm/book3s_hv_devm.c line 330

Quoted 4 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +4 -0
+static void kvmppc_devm_page_free(struct page *page)
+{
+	kvmppc_devm_put_page(page);
+}

This seems to be the only caller of kvmppc_devm_put_page, any reason not to just merge the two functions?

arch/powerpc/kvm/book3s_hv_devm.c line 425

Quoted 11 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +11 -0
+static int kvmppc_devm_pages_init(void)
+{
+	unsigned long nr_pfns = kvmppc_devm.pfn_last -
+				kvmppc_devm.pfn_first;
+
+	kvmppc_devm.pfn_bitmap = kcalloc(BITS_TO_LONGS(nr_pfns),
+				 sizeof(unsigned long), GFP_KERNEL);
+	if (!kvmppc_devm.pfn_bitmap)
+		return -ENOMEM;
+
+	spin_lock_init(&kvmppc_devm_lock);

Just initialize the spinlock using DEFINE_SPINLOCK() at compile time. The rest of the function is so trivial that it can be inlined into the caller.

Also is kvmppc_devm_lock such a good name? This mostly just protects the allocation bitmap, so reflecting that in the name might be a good idea.

Quoted 26 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +26 -0
+int kvmppc_devm_init(void)
+{
+	int ret = 0;
+	unsigned long size;
+	struct resource *res;
+	void *addr;
+
+	size = kvmppc_get_secmem_size();
+	if (!size) {
+		ret = -ENODEV;
+		goto out;
+	}
+
+	ret = alloc_chrdev_region(&kvmppc_devm.devt, 0, 1,
+				"kvmppc-devm");
+	if (ret)
+		goto out;
+
+	dev_set_name(&kvmppc_devm.dev, "kvmppc_devm_device%d", 0);
+	kvmppc_devm.dev.release = kvmppc_devm_release;
+	device_initialize(&kvmppc_devm.dev);
+	res = devm_request_free_mem_region(&kvmppc_devm.dev,
+		&iomem_resource, size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		goto out_unregister;

arch/powerpc/kvm/book3s_hv_devm.c line 470

Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10 -0
+	}
+
+	kvmppc_devm.pagemap.type = MEMORY_DEVICE_PRIVATE;
+	kvmppc_devm.pagemap.res = *res;
+	kvmppc_devm.pagemap.ops = &kvmppc_devm_ops;
+	addr = devm_memremap_pages(&kvmppc_devm.dev, &kvmppc_devm.pagemap);
+	if (IS_ERR(addr)) {
+		ret = PTR_ERR(addr);
+		goto out_unregister;
+	}

It seems a little silly to allocate a struct device just so that we can pass it to devm_request_free_mem_region and devm_memremap_pages. I think we should just create non-dev_ versions of those as well.

arch/powerpc/kvm/book3s_hv_devm.c line 474

Quoted 4 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +4 -0
+
+	kvmppc_devm.pfn_first = res->start >> PAGE_SHIFT;
+	kvmppc_devm.pfn_last = kvmppc_devm.pfn_first +
+			(resource_size(res) >> PAGE_SHIFT);

pfn_last is only used to calculat a size. Also I think we could just use kvmppc_devm.pagemap.res directly instead of copying these values out. the ">> PAGE_SHIFT" is cheap enough.

#3 lore raw

Re: [PATCH v6 1/7] kvmppc: Driver to manage pages of secure guest

Quoted 33 lines · Christoph Hellwig
Quoted 33 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +32 -0
+int kvmppc_devm_init(void)
+{
+	int ret = 0;
+	unsigned long size;
+	struct resource *res;
+	void *addr;
+
+	size = kvmppc_get_secmem_size();
+	if (!size) {
+		ret = -ENODEV;
+		goto out;
+	}
+
+	ret = alloc_chrdev_region(&kvmppc_devm.devt, 0, 1,
+				"kvmppc-devm");
+	if (ret)
+		goto out;
+
+	dev_set_name(&kvmppc_devm.dev, "kvmppc_devm_device%d", 0);
+	kvmppc_devm.dev.release = kvmppc_devm_release;
+	device_initialize(&kvmppc_devm.dev);
+	res = devm_request_free_mem_region(&kvmppc_devm.dev,
+		&iomem_resource, size);
+	if (IS_ERR(res)) {
+		ret = PTR_ERR(res);
+		goto out_unregister;
+	}
+
+	kvmppc_devm.pagemap.type = MEMORY_DEVICE_PRIVATE;
+	kvmppc_devm.pagemap.res = *res;
+	kvmppc_devm.pagemap.ops = &kvmppc_devm_ops;
+	addr = devm_memremap_pages(&kvmppc_devm.dev, &kvmppc_devm.pagemap);
Quoted 8 lines

arch/powerpc/kvm/book3s_hv_devm.c line 470

Quoted 4 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +4 -0
+	if (IS_ERR(addr)) {
+		ret = PTR_ERR(addr);
+		goto out_unregister;
+	}

It seems a little silly to allocate a struct device just so that we can pass it to devm_request_free_mem_region and devm_memremap_pages. I think we should just create non-dev_ versions of those as well.

There is no reason for us to create a device really. If non-dev versions of the above two routines are present, I can switch.

I will take care of the rest of your comments. Thanks for the review.

Regards,
Bharata.

#4 lore raw

Re: [PATCH v6 1/7] kvmppc: Driver to manage pages of secure guest

In reply to Bharata B Rao

Hello Bharata,

I have just a couple of small comments.

Quoted 8 lines · Bharata B Rao

+/*
+ * Get a free device PFN from the pool
+ *
+ * Called when a normal page is moved to secure memory (UV_PAGE_IN). Device
+ * PFN will be used to keep track of the secure page on HV side.
+ *
+ * @rmap here is the slot in the rmap array that corresponds to @gpa.
+ * Thus a non-zero rmap entry indicates that the corresonding guest

Typo: corresponding

arch/powerpc/kvm/book3s_hv_devm.c line 400

Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10 -0
+static u64 kvmppc_get_secmem_size(void)
+{
+	struct device_node *np;
+	int i, len;
+	const __be32 *prop;
+	u64 size = 0;
+
+	np = of_find_node_by_path("/ibm,ultravisor/ibm,uv-firmware");
+	if (!np)
+		goto out;

I believe that in general we try to avoid hard-coding the path when a node is accessed and searched instead via its compatible property.

Signature · 2 lines
Thiago Jung Bauermann
IBM Linux Technology Center
#6 lore raw

Re: [PATCH v6 1/7] kvmppc: Driver to manage pages of secure guest

In reply to Bharata B Rao
Quoted 6 lines · Bharata B Rao

KVMPPC driver 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 page
H_SVM_PAGE_OUT: Move the content of a secure page to normal page

Quoted 7 lines

Private ZONE_DEVICE memory equal to the amount of secure memory available in the platform for running secure guests is created via a char device. Whenever a page belonging to the guest becomes secure, a page from this private device memory is used to represent and track that secure page on the HV side. The movement of pages between normal and secure memory is done via migrate_vma_pages() using UV_PAGE_IN and UV_PAGE_OUT ucalls.

Hi Bharata,

please see my patch where I define the bits which define the type of the rmap entry:
https://patchwork.ozlabs.org/patch/1149791/

Please add an entry for the devm pfn type like:
#define KVMPPC_RMAP_PFN_DEVM 0x0200000000000000 /* secure guest devm pfn */

And the following in the appropriate header file

static inline bool kvmppc_rmap_is_pfn_demv(unsigned long *rmapp) {
return !!((*rmapp & KVMPPC_RMAP_TYPE_MASK) =
KVMPPC_RMAP_PFN_DEVM));
}

Also see comment below.

Thanks,
Suraj

Quoted 16 lines
---
 arch/powerpc/include/asm/hvcall.h          |   4 +
 arch/powerpc/include/asm/kvm_book3s_devm.h |  29 ++
 arch/powerpc/include/asm/kvm_host.h        |  12 +
 arch/powerpc/include/asm/ultravisor-api.h  |   2 +
 arch/powerpc/include/asm/ultravisor.h      |  14 +
 arch/powerpc/kvm/Makefile                  |   3 +
 arch/powerpc/kvm/book3s_hv.c               |  19 +
 arch/powerpc/kvm/book3s_hv_devm.c          | 492
+++++++++++++++++++++
 8 files changed, 575 insertions(+)
 create mode 100644 arch/powerpc/include/asm/kvm_book3s_devm.h
 create mode 100644 arch/powerpc/kvm/book3s_hv_devm.c

[snip]

Quoted 63 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +30 -0
+
+struct kvmppc_devm_page_pvt {
+	unsigned long *rmap;
+	unsigned int lpid;
+	unsigned long gpa;
+};
+
+struct kvmppc_devm_copy_args {
+	unsigned long *rmap;
+	unsigned int lpid;
+	unsigned long gpa;
+	unsigned long page_shift;
+};
+
+/*
+ * Bits 60:56 in the rmap entry will be used to identify the
+ * different uses/functions of rmap. This definition with move
+ * to a proper header when all other functions are defined.
+ */
+#define KVMPPC_PFN_DEVM		(0x2ULL << 56)
+
+static inline bool kvmppc_is_devm_pfn(unsigned long pfn)
+{
+	return !!(pfn & KVMPPC_PFN_DEVM);
+}
+
+/*
+ * Get a free device PFN from the pool
+ *
+ * Called when a normal page is moved to secure memory (UV_PAGE_IN).

Device
+ * PFN will be used to keep track of the secure page on HV side.
+ *
+ * @rmap here is the slot in the rmap array that corresponds to @gpa.
+ * Thus a non-zero rmap entry indicates that the corresonding guest
+ * page has become secure, and is not mapped on the HV side.
+ *
+ * NOTE: In this and subsequent functions, we pass around and access
+ * individual elements of kvm_memory_slot->arch.rmap[] without any
+ * protection. Should we use lock_rmap() here?
+ */

(quoted diff) +2 -0
+static struct page *kvmppc_devm_get_page(unsigned long *rmap,
+					unsigned long gpa, unsigned

int lpid)

arch/powerpc/kvm/book3s_hv_devm.c new file +18 -0
+{
+	struct page *dpage = NULL;
+	unsigned long bit, devm_pfn;
+	unsigned long nr_pfns = kvmppc_devm.pfn_last -
+				kvmppc_devm.pfn_first;
+	unsigned long flags;
+	struct kvmppc_devm_page_pvt *pvt;
+
+	if (kvmppc_is_devm_pfn(*rmap))
+		return NULL;
+
+	spin_lock_irqsave(&kvmppc_devm_lock, flags);
+	bit = find_first_zero_bit(kvmppc_devm.pfn_bitmap, nr_pfns);
+	if (bit >= nr_pfns)
+		goto out;
+
+	bitmap_set(kvmppc_devm.pfn_bitmap, bit, 1);
+	devm_pfn = bit + kvmppc_devm.pfn_first;

arch/powerpc/kvm/book3s_hv_devm.c line 122

Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10 -0
+	dpage = pfn_to_page(devm_pfn);
+
+	if (!trylock_page(dpage))
+		goto out_clear;
+
+	*rmap = devm_pfn | KVMPPC_PFN_DEVM;
+	pvt = kzalloc(sizeof(*pvt), GFP_ATOMIC);
+	if (!pvt)
+		goto out_unlock;
+	pvt->rmap = rmap;

Am I missing something, why does the rmap need to be stored in pvt? Given the gpa is already stored and this is enough to get back to the rmap entry, right?

Quoted 19 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +18 -0
+	pvt->gpa = gpa;
+	pvt->lpid = lpid;
+	dpage->zone_device_data = pvt;
+	spin_unlock_irqrestore(&kvmppc_devm_lock, flags);
+
+	get_page(dpage);
+	return dpage;
+
+out_unlock:
+	unlock_page(dpage);
+out_clear:
+	bitmap_clear(kvmppc_devm.pfn_bitmap,
+		     devm_pfn - kvmppc_devm.pfn_first, 1);
+out:
+	spin_unlock_irqrestore(&kvmppc_devm_lock, flags);
+	return NULL;
+}
+

[snip]

#7 lore raw

Re: [PATCH v6 1/7] kvmppc: Driver to manage pages of secure guest

Quoted 25 lines · Suraj Jitindar Singh
Quoted 6 lines · Bharata B Rao

KVMPPC driver 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 page
H_SVM_PAGE_OUT: Move the content of a secure page to normal page

Quoted 7 lines

Private ZONE_DEVICE memory equal to the amount of secure memory available in the platform for running secure guests is created via a char device. Whenever a page belonging to the guest becomes secure, a page from this private device memory is used to represent and track that secure page on the HV side. The movement of pages between normal and secure memory is done via migrate_vma_pages() using UV_PAGE_IN and UV_PAGE_OUT ucalls.

Hi Bharata,

please see my patch where I define the bits which define the type of the rmap entry:
https://patchwork.ozlabs.org/patch/1149791/

Please add an entry for the devm pfn type like:
#define KVMPPC_RMAP_PFN_DEVM 0x0200000000000000 /* secure guest devm pfn */

Quoted 8 lines

And the following in the appropriate header file

static inline bool kvmppc_rmap_is_pfn_demv(unsigned long *rmapp)

{
	return !!((*rmapp & KVMPPC_RMAP_TYPE_MASK) ==

KVMPPC_RMAP_PFN_DEVM));
}

Sure, I have the equivalents defined locally, will move to appropriate headers.

Quoted 29 lines

Also see comment below.

Quoted 27 lines
(quoted diff) +2 -0
+static struct page *kvmppc_devm_get_page(unsigned long *rmap,
+					unsigned long gpa, unsigned

int lpid)

arch/powerpc/kvm/book3s_hv_devm.c new file +24 -0
+{
+	struct page *dpage = NULL;
+	unsigned long bit, devm_pfn;
+	unsigned long nr_pfns = kvmppc_devm.pfn_last -
+				kvmppc_devm.pfn_first;
+	unsigned long flags;
+	struct kvmppc_devm_page_pvt *pvt;
+
+	if (kvmppc_is_devm_pfn(*rmap))
+		return NULL;
+
+	spin_lock_irqsave(&kvmppc_devm_lock, flags);
+	bit = find_first_zero_bit(kvmppc_devm.pfn_bitmap, nr_pfns);
+	if (bit >= nr_pfns)
+		goto out;
+
+	bitmap_set(kvmppc_devm.pfn_bitmap, bit, 1);
+	devm_pfn = bit + kvmppc_devm.pfn_first;
+	dpage = pfn_to_page(devm_pfn);
+
+	if (!trylock_page(dpage))
+		goto out_clear;
+
+	*rmap = devm_pfn | KVMPPC_PFN_DEVM;
Quoted 8 lines

arch/powerpc/kvm/book3s_hv_devm.c line 122

Quoted 4 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +4 -0
+	pvt = kzalloc(sizeof(*pvt), GFP_ATOMIC);
+	if (!pvt)
+		goto out_unlock;
+	pvt->rmap = rmap;

Am I missing something, why does the rmap need to be stored in pvt? Given the gpa is already stored and this is enough to get back to the rmap entry, right?

I use rmap entry to note that this guest page is secure and is being represented by device memory page on the HV side. When the page becomes normal again, I need to undo that from dev_pagemap_ops.page_free() where I don't have gpa.

Regards,
Bharata.

#5 lore raw

Re: [PATCH v6 1/7] kvmppc: Driver to manage pages of secure guest

In reply to Thiago Jung Bauermann
Quoted 18 lines · Thiago Jung Bauermann

Hello Bharata,

I have just a couple of small comments.

Quoted 8 lines · Bharata B Rao

+/*
+ * Get a free device PFN from the pool
+ *
+ * Called when a normal page is moved to secure memory (UV_PAGE_IN). Device
+ * PFN will be used to keep track of the secure page on HV side.
+ *
+ * @rmap here is the slot in the rmap array that corresponds to @gpa.
+ * Thus a non-zero rmap entry indicates that the corresonding guest

Typo: corresponding

Quoted 13 lines

arch/powerpc/kvm/book3s_hv_devm.c line 400

Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10 -0
+static u64 kvmppc_get_secmem_size(void)
+{
+	struct device_node *np;
+	int i, len;
+	const __be32 *prop;
+	u64 size = 0;
+
+	np = of_find_node_by_path("/ibm,ultravisor/ibm,uv-firmware");
+	if (!np)
+		goto out;

I believe that in general we try to avoid hard-coding the path when a node is accessed and searched instead via its compatible property.

Thanks, will switch to of_find_compatible_node().

Regards,
Bharata.