KVMPPC driver to manage secure guest pages
Subsystems (5)
v7 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. 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 reverse movement is driven via pagemap_ops.migrate_to_ram().
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 hmm.git
(https://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git/log/?h=hmm)
plus
Claudio Carvalho's base ultravisor enablement patchset v6
(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. This patchset is based on hmm.git because hmm.git has migrate_vma cleanup and not-device memremap_pages patchsets that are required by this patchset.
Changes in v7
=============
- The major change in this version is to not create a char device but instead use the not device versions of memremap_pages and request_free_mem_region (Christoph Hellwig)
- Other changes
* Addressed all the changes suggested by Christoph Hellwig for v6.
* Removed MIGRATE_VMA_HELPER dependency
* Switched to using of_find_compatible_node() and not doing
find by path (Thiago Jung Bauermann)
* Moved kvmppc_rmap_is_devm_pfn to kvm_host.h
* Updated comments
* use @page_shift argument in H_SVM_PAGE_OUT instead of PAGE_SHIFT
* Proper handling of return val from kvmppc_devm_fault_migrate_alloc_and_copy
v6: https://lore.kernel.org/linuxppc-dev/[email protected]/T/#t
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 | 19 + arch/powerpc/Kconfig | 17 + arch/powerpc/include/asm/hvcall.h | 9 + arch/powerpc/include/asm/kvm_book3s_devm.h | 47 ++ arch/powerpc/include/asm/kvm_host.h | 39 ++ 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 | 113 ++++ arch/powerpc/kvm/book3s_hv_devm.c | 614 +++++++++++++++++++++ arch/powerpc/kvm/powerpc.c | 12 + include/uapi/linux/kvm.h | 1 + 14 files changed, 940 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
PATCH v7 1/7 kvmppc: Driver to manage pages of secure guest
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 pageH_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. 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 | 23 ++ 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 | 438 +++++++++++++++++++++ 8 files changed, 532 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
@@ -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
@@ -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 +unsigned long kvmppc_h_svm_page_in (struct kvm *kvm, +unsigned long gra, +unsigned long flags, +unsigned long page_shift); +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 +23 -0
@@ -242,6 +242,17 @@ struct revmap_entry {#define KVMPPC_RMAP_PRESENT 0x100000000ul#define KVMPPC_RMAP_INDEX 0xfffffffful +/* +* Bits 60:56 in the rmap entry will be used to identify the +* different uses/functions of rmap. +*/ +#define KVMPPC_RMAP_DEVM_PFN (0x2ULL << 56) + +static inline bool kvmppc_rmap_is_devm_pfn (unsigned long pfn) +{ +return !!(pfn &KVMPPC_RMAP_DEVM_PFN ); +} +struct kvm_arch_memory_slot {#ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE unsigned long *rmap;
@@ -849,4 +860,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
@@ -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
@@ -31,4 +31,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
@@ -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
@@ -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 +438 -0
438 lines not on this page · raw message
Signature · 1 line
2.21.0
Re: [PATCH v7 1/7] kvmppc: Driver to manage pages of secure guest
Some minor comments/questions below. Overall, the patches look fine to me.
arch/powerpc/kvm/book3s_hv_devm.c line 43
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +8
-0+#include <linux/pagemap.h> +#include <linux/migrate.h> +#include <linux/kvm_host.h> +#include <asm/ultravisor.h> + +static struct dev_pagemap kvmppc_devm_pgmap; +static unsigned long *kvmppc_devm_pfn_bitmap; +static DEFINE_SPINLOCK (kvmppc_devm_pfn_lock);
Is this lock protecting just the pfn_bitmap?
Quoted 27 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +27
-0+ +struct kvmppc_devm_page_pvt { +unsigned long *rmap; +unsigned int lpid; +unsigned long gpa; +}; + +/* +* 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 corresponding 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? +*/ +static struct page *kvmppc_devm_get_page (unsigned long *rmap,unsigned long gpa, +unsigned int lpid) +{ +struct page *dpage =NULL ; +unsigned long bit, devm_pfn; +unsigned long flags;
arch/powerpc/kvm/book3s_hv_devm.c line 80
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+struct kvmppc_devm_page_pvt *pvt; +unsigned long pfn_last, pfn_first; + +if (kvmppc_rmap_is_devm_pfn (*rmap)) +return NULL ; + + pfn_first = kvmppc_devm_pgmap.res.start >>PAGE_SHIFT ; + pfn_last = pfn_first + + (resource_size (&kvmppc_devm_pgmap.res) >>PAGE_SHIFT ); +spin_lock_irqsave (&kvmppc_devm_pfn_lock, flags);
Blank lines around spin_lock() would help.
arch/powerpc/kvm/book3s_hv_devm.c line 86
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+ bit =find_first_zero_bit (kvmppc_devm_pfn_bitmap, pfn_last - pfn_first); +if (bit >= (pfn_last - pfn_first)) + goto out; + +bitmap_set (kvmppc_devm_pfn_bitmap, bit,1 ); + devm_pfn = bit + pfn_first;
Can we drop the &kvmppc_devm_pfn_lock here or after the trylock_page()? Or does it also protect the ->zone_device_data' assignment below as well? If so, maybe drop the 'pfn_' from the name of the lock?
Besides, we don't seem to hold this lock when accessing ->zone_device_data in kvmppc_share_page(). Maybe &kvmppc_devm_pfn_lock just protects the bitmap?
arch/powerpc/kvm/book3s_hv_devm.c line 87
Quoted 1 line
arch/powerpc/kvm/book3s_hv_devm.c new file +1
-0+ dpage =pfn_to_page (devm_pfn);
Does this code and hence CONFIG_PPC_UV depend on a specific model like CONFIG_SPARSEMEM_VMEMMAP?
arch/powerpc/kvm/book3s_hv_devm.c line 99
Quoted 12 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +12
-0+ +if (!trylock_page (dpage)) + goto out_clear; + + *rmap = devm_pfn |KVMPPC_RMAP_DEVM_PFN ; + pvt =kzalloc (sizeof (*pvt),GFP_ATOMIC ); +if (!pvt) + goto out_unlock; + pvt->rmap = rmap; + pvt->gpa = gpa; + pvt->lpid = lpid; + dpage->zone_device_data = pvt;
->zone_device_data is set after locking the dpage here, but in kvmppc_share_page() and kvmppc_devm_fault_migrate_alloc_and_copy() it is accessed without locking the page?
Quoted 41 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +41
-0+spin_unlock_irqrestore (&kvmppc_devm_pfn_lock, flags); + +get_page (dpage); +return dpage; + +out_unlock: +unlock_page (dpage); +out_clear: +bitmap_clear (kvmppc_devm_pfn_bitmap, devm_pfn - pfn_first,1 ); +out: +spin_unlock_irqrestore (&kvmppc_devm_pfn_lock, flags); +return NULL ; +} + +/* +* Alloc a PFN from private device memory pool and copy page from normal +* memory to secure memory. +*/ +static int +kvmppc_devm_migrate_alloc_and_copy (struct migrate_vma *mig, +unsigned long *rmap,unsigned long gpa, +unsigned int lpid,unsigned long page_shift) +{ +struct page *spage =migrate_pfn_to_page (*mig->src); +unsigned long pfn = *mig->src >>MIGRATE_PFN_SHIFT ; +struct page *dpage; + + *mig->dst =0 ; +if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE )) +return 0 ; + + dpage =kvmppc_devm_get_page (rmap, gpa, lpid); +if (!dpage) +return -EINVAL ; + +if (spage) +uv_page_in (lpid, pfn << page_shift, gpa,0 , page_shift); + + *mig->dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; +return 0 ; +}
arch/powerpc/kvm/book3s_hv_devm.c line 150
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+ +/* +* Move page from normal memory to secure memory. +*/ +unsigned long +kvmppc_h_svm_page_in (struct kvm *kvm,unsigned long gpa, +unsigned long flags,unsigned long page_shift) +{ +unsigned long addr, end; +unsigned long src_pfn, dst_pfn;
These are the host frame numbers correct? Trying to distinguish them from 'gfn' and 'gpa' used in the function.
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +8
-0+struct migrate_vma mig; +struct vm_area_struct *vma; +int srcu_idx; +unsigned long gfn = gpa >> page_shift; +struct kvm_memory_slot *slot; +unsigned long *rmap; +int ret; +
arch/powerpc/kvm/book3s_hv_devm.c line 168
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+if (page_shift !=PAGE_SHIFT ) +return H_P3 ; + +if (flags) +return H_P2 ; + + ret =H_PARAMETER ; +down_read (&kvm->mm->mmap_sem); + srcu_idx =srcu_read_lock (&kvm->srcu); + slot =gfn_to_memslot (kvm, gfn);
Can slot be NULL? could be a bug in UV...
arch/powerpc/kvm/book3s_hv_devm.c line 170
Quoted 2 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +2
-0+ rmap = &slot->arch.rmap[gfn - slot->base_gfn]; + addr =gfn_to_hva (kvm, gpa >> page_shift);
Use 'gfn' as the second parameter?
Nit. for consistency with gpa and gfn, maybe rename 'addr' to 'hva' or to match 'end' maybe to 'start'.
Also, can we check 'kvmppc_rmap_is_devm_pfn(*rmap)' here and bail out if its already shared? We currently do it further down the call chain in kvmppc_devm_get_page() after doing more work.
Quoted 40 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +40
-0+if (kvm_is_error_hva (addr)) + goto out; + + end = addr + (1UL << page_shift); + vma =find_vma_intersection (kvm->mm, addr, end); +if (!vma || vma->vm_start > addr || vma->vm_end < end) + goto out; + +memset (&mig,0 ,sizeof (mig)); + mig.vma = vma; + mig.start = addr; + mig.end = end; + mig.src = &src_pfn; + mig.dst = &dst_pfn; + +if (migrate_vma_setup (&mig)) + goto out; + +if (kvmppc_devm_migrate_alloc_and_copy (&mig, rmap, gpa, + kvm->arch.lpid, page_shift)) + goto out_finalize; + +migrate_vma_pages (&mig); + ret =H_SUCCESS ; +out_finalize: +migrate_vma_finalize (&mig); +out: +srcu_read_unlock (&kvm->srcu, srcu_idx); +up_read (&kvm->mm->mmap_sem); +return ret; +} + +/* +* Provision a new page on HV side and copy over the contents +* from secure memory. +*/ +static int +kvmppc_devm_fault_migrate_alloc_and_copy (struct migrate_vma *mig, +unsigned long page_shift) +{
arch/powerpc/kvm/book3s_hv_devm.c line 220
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+struct page *dpage, *spage; +struct kvmppc_devm_page_pvt *pvt; +unsigned long pfn; +int ret; + + spage =migrate_pfn_to_page (*mig->src); +if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE )) +return 0 ; +if (!is_zone_device_page (spage)) +return 0 ;
What does it mean if its not a zone_device page at this point? Caller would then proceed to migrage_vma_pages() if we return 0 right?
Quoted 51 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +51
-0+ + dpage =alloc_page_vma (GFP_HIGHUSER , mig->vma, mig->start); +if (!dpage) +return -EINVAL ; +lock_page (dpage); + pvt = spage->zone_device_data; + + pfn =page_to_pfn (dpage); + ret =uv_page_out (pvt->lpid, pfn << page_shift, pvt->gpa,0 , + page_shift); +if (ret =U_SUCCESS ) + *mig->dst =migrate_pfn (pfn) |MIGRATE_PFN_LOCKED ; +else { +unlock_page (dpage); +__free_page (dpage); + } +return ret; +} + +/* +* Fault handler callback when HV touches any page that has been +* moved to secure memory, we ask UV to give back the page by +* issuing a UV_PAGE_OUT uvcall. +* +* This eventually results in dropping of device PFN and the newly +* provisioned page/PFN gets populated in QEMU page tables. +*/ +static vm_fault_t kvmppc_devm_migrate_to_ram (struct vm_fault *vmf) +{ +unsigned long src_pfn, dst_pfn =0 ; +struct migrate_vma mig; +int ret =0 ; + +memset (&mig,0 ,sizeof (mig)); + mig.vma = vmf->vma; + mig.start = vmf->address; + mig.end = vmf->address +PAGE_SIZE ; + mig.src = &src_pfn; + mig.dst = &dst_pfn; + +if (migrate_vma_setup (&mig)) { + ret =VM_FAULT_SIGBUS ; + goto out; + } + +if (kvmppc_devm_fault_migrate_alloc_and_copy (&mig,PAGE_SHIFT )) { + ret =VM_FAULT_SIGBUS ; + goto out_finalize; + } + +migrate_vma_pages (&mig);
arch/powerpc/kvm/book3s_hv_devm.c line 281
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+out_finalize: +migrate_vma_finalize (&mig); +out: +return ret; +} + +/* +* Release the device PFN back to the pool +* +* Gets called when secure page becomes a normal page during UV_PAGE_OUT.
Nit: Should that be H_SVM_PAGE_OUT?
arch/powerpc/kvm/book3s_hv_devm.c line 291
Quoted 10 lines
+ */
arch/powerpc/kvm/book3s_hv_devm.c new file +9
-0+static void kvmppc_devm_page_free (struct page *page) +{ +unsigned long pfn =page_to_pfn (page); +unsigned long flags; +struct kvmppc_devm_page_pvt *pvt; + +spin_lock_irqsave (&kvmppc_devm_pfn_lock, flags); + pvt = page->zone_device_data; + page->zone_device_data =NULL ;
If the pfn_lock only protects the bitmap, would be better to move it here?
Quoted 16 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +16
-0+ +bitmap_clear (kvmppc_devm_pfn_bitmap, + pfn - (kvmppc_devm_pgmap.res.start >>PAGE_SHIFT ),1 ); + *pvt->rmap =0 ; +spin_unlock_irqrestore (&kvmppc_devm_pfn_lock, flags); +kfree (pvt); +} + +static const struct dev_pagemap_ops kvmppc_devm_ops = { + .page_free = kvmppc_devm_page_free, + .migrate_to_ram = kvmppc_devm_migrate_to_ram, +}; + +/* +* Move page from secure memory to normal memory. +*/
arch/powerpc/kvm/book3s_hv_devm.c line 317
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+unsigned long +kvmppc_h_svm_page_out (struct kvm *kvm,unsigned long gpa, +unsigned long flags,unsigned long page_shift) +{ +struct migrate_vma mig; +unsigned long addr, end; +struct vm_area_struct *vma; +unsigned long src_pfn, dst_pfn =0 ; +int srcu_idx; +int ret;
Nit: Not sure its a coding style requirement, but many functions seem to "sort" these local variables in descending order of line length for appearance :-) (eg: migrate_vma* functions).
Quoted 24 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +24
-0+ +if (page_shift !=PAGE_SHIFT ) +return H_P3 ; + +if (flags) +return H_P2 ; + + ret =H_PARAMETER ; +down_read (&kvm->mm->mmap_sem); + srcu_idx =srcu_read_lock (&kvm->srcu); + addr =gfn_to_hva (kvm, gpa >> page_shift); +if (kvm_is_error_hva (addr)) + goto out; + + end = addr + (1UL << page_shift); + vma =find_vma_intersection (kvm->mm, addr, end); +if (!vma || vma->vm_start > addr || vma->vm_end < end) + goto out; + +memset (&mig,0 ,sizeof (mig)); + mig.vma = vma; + mig.start = addr; + mig.end = end; + mig.src = &src_pfn;
arch/powerpc/kvm/book3s_hv_devm.c line 351
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+ mig.dst = &dst_pfn; +if (migrate_vma_setup (&mig)) + goto out; + + ret =kvmppc_devm_fault_migrate_alloc_and_copy (&mig, page_shift); +if (ret) + goto out_finalize; + +migrate_vma_pages (&mig); + ret =H_SUCCESS ;
Nit: Blank line here?
Quoted 62 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +62
-0+out_finalize: +migrate_vma_finalize (&mig); +out: +srcu_read_unlock (&kvm->srcu, srcu_idx); +up_read (&kvm->mm->mmap_sem); +return ret; +} + +static u64 kvmppc_get_secmem_size (void ) +{ +struct device_node *np; +int i, len; +const __be32 *prop; +u64 size =0 ; + + np =of_find_compatible_node (NULL ,NULL ,"ibm,uv-firmware" ); +if (!np) + goto out; + + prop =of_get_property (np,"secure-memory-ranges" , &len); +if (!prop) + goto out_put; + +for (i =0 ; i < len / (sizeof (*prop) *4 ); i++) + size +=of_read_number (prop + (i *4 ) +2 ,2 ); + +out_put: +of_node_put (np); +out: +return size; +} + +int kvmppc_devm_init (void ) +{ +int ret =0 ; +unsigned long size; +struct resource *res; +void *addr; +unsigned long pfn_last, pfn_first; + + size =kvmppc_get_secmem_size (); +if (!size) { + ret = -ENODEV ; + goto out; + } + + res =request_free_mem_region (&iomem_resource, size,"kvmppc_devm" ); +if (IS_ERR (res)) { + ret =PTR_ERR (res); + goto out; + } + + kvmppc_devm_pgmap.type =MEMORY_DEVICE_PRIVATE ; + kvmppc_devm_pgmap.res = *res; + kvmppc_devm_pgmap.ops = &kvmppc_devm_ops; + addr =memremap_pages (&kvmppc_devm_pgmap,-1 ); +if (IS_ERR (addr)) { + ret =PTR_ERR (addr); + goto out_free_region; + } + + pfn_first = res->start >>PAGE_SHIFT ;
arch/powerpc/kvm/book3s_hv_devm.c line 423
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+ pfn_last = pfn_first + (resource_size (res) >>PAGE_SHIFT ); + kvmppc_devm_pfn_bitmap =kcalloc (BITS_TO_LONGS (pfn_last - pfn_first), +sizeof (unsigned long ),GFP_KERNEL ); +if (!kvmppc_devm_pfn_bitmap) { + ret = -ENOMEM ; + goto out_unmap; + } + +pr_info ("KVMPPC-DEVM: Secure Memory size 0x%lx\n" , size); +return ret;
Nit: Blank line here?
Quoted 17 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +15
-0+out_unmap: +memunmap_pages (&kvmppc_devm_pgmap); +out_free_region: +release_mem_region (res->start, size); +out: +return ret; +} + +void kvmppc_devm_free (void ) +{ +memunmap_pages (&kvmppc_devm_pgmap); +release_mem_region (kvmppc_devm_pgmap.res.start, +resource_size (&kvmppc_devm_pgmap.res)); +kfree (kvmppc_devm_pfn_bitmap); +}Signature · 1 line
2.21.0
Re: [PATCH v7 1/7] kvmppc: Driver to manage pages of secure guest
Quoted 13 lines · Sukadev Bhattiprolu
Some minor comments/questions below. Overall, the patches look fine to me.
arch/powerpc/kvm/book3s_hv_devm.c line 43
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +8
-0+#include <linux/pagemap.h> +#include <linux/migrate.h> +#include <linux/kvm_host.h> +#include <asm/ultravisor.h> + +static struct dev_pagemap kvmppc_devm_pgmap; +static unsigned long *kvmppc_devm_pfn_bitmap; +static DEFINE_SPINLOCK (kvmppc_devm_pfn_lock);Is this lock protecting just the
pfn_bitmap?
Yes.
Quoted 32 lines
Quoted 32 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +31
-0+ +struct kvmppc_devm_page_pvt { +unsigned long *rmap; +unsigned int lpid; +unsigned long gpa; +}; + +/* +* 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 corresponding 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? +*/ +static struct page *kvmppc_devm_get_page (unsigned long *rmap,unsigned long gpa, +unsigned int lpid) +{ +struct page *dpage =NULL ; +unsigned long bit, devm_pfn; +unsigned long flags; +struct kvmppc_devm_page_pvt *pvt; +unsigned long pfn_last, pfn_first; + +if (kvmppc_rmap_is_devm_pfn (*rmap))
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 80
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+return NULL ; + + pfn_first = kvmppc_devm_pgmap.res.start >>PAGE_SHIFT ; + pfn_last = pfn_first + + (resource_size (&kvmppc_devm_pgmap.res) >>PAGE_SHIFT ); +spin_lock_irqsave (&kvmppc_devm_pfn_lock, flags);Blank lines around
spin_lock()would help.
You mean blank line before lock and after unlock to clearly see where the lock starts and ends?
Quoted 12 lines
arch/powerpc/kvm/book3s_hv_devm.c line 86
Quoted 7 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+ bit =find_first_zero_bit (kvmppc_devm_pfn_bitmap, pfn_last - pfn_first); +if (bit >= (pfn_last - pfn_first)) + goto out; + +bitmap_set (kvmppc_devm_pfn_bitmap, bit,1 ); + devm_pfn = bit + pfn_first;Can we drop the
&kvmppc_devm_pfn_lockhere or after thetrylock_page()? Or does it also protect the ->zone_device_data' assignment below as well? If so, maybe drop the 'pfn_' from the name of the lock?
Quoted 2 lines
Besides, we don't seem to hold this lock when accessing ->zone_device_data in
kvmppc_share_page(). Maybe&kvmppc_devm_pfn_lockjust protects the bitmap?
Will move the unlock to appropriately.
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c line 87
Quoted 3 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +1
-0+ dpage =pfn_to_page (devm_pfn);Does this code and hence
CONFIG_PPC_UVdepend on a specific model likeCONFIG_SPARSEMEM_VMEMMAP?
I don't think so. Irrespective of that pfn_to_page() should just work for us.
Quoted 63 lines
arch/powerpc/kvm/book3s_hv_devm.c line 99
Quoted 12 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +12
-0+ +if (!trylock_page (dpage)) + goto out_clear; + + *rmap = devm_pfn |KVMPPC_RMAP_DEVM_PFN ; + pvt =kzalloc (sizeof (*pvt),GFP_ATOMIC ); +if (!pvt) + goto out_unlock; + pvt->rmap = rmap; + pvt->gpa = gpa; + pvt->lpid = lpid; + dpage->zone_device_data = pvt;->zone_device_data is set after locking the dpage here, but in
kvmppc_share_page()andkvmppc_devm_fault_migrate_alloc_and_copy()it is accessed without locking the page?Quoted 46 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +46
-0+spin_unlock_irqrestore (&kvmppc_devm_pfn_lock, flags); + +get_page (dpage); +return dpage; + +out_unlock: +unlock_page (dpage); +out_clear: +bitmap_clear (kvmppc_devm_pfn_bitmap, devm_pfn - pfn_first,1 ); +out: +spin_unlock_irqrestore (&kvmppc_devm_pfn_lock, flags); +return NULL ; +} + +/* +* Alloc a PFN from private device memory pool and copy page from normal +* memory to secure memory. +*/ +static int +kvmppc_devm_migrate_alloc_and_copy (struct migrate_vma *mig, +unsigned long *rmap,unsigned long gpa, +unsigned int lpid,unsigned long page_shift) +{ +struct page *spage =migrate_pfn_to_page (*mig->src); +unsigned long pfn = *mig->src >>MIGRATE_PFN_SHIFT ; +struct page *dpage; + + *mig->dst =0 ; +if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE )) +return 0 ; + + dpage =kvmppc_devm_get_page (rmap, gpa, lpid); +if (!dpage) +return -EINVAL ; + +if (spage) +uv_page_in (lpid, pfn << page_shift, gpa,0 , page_shift); + + *mig->dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; +return 0 ; +} + +/* +* Move page from normal memory to secure memory. +*/ +unsigned long
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 150
Quoted 5 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +5
-0+kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gpa, + unsigned long flags, unsigned long page_shift) +{ + unsigned long addr, end; +unsigned long src_pfn, dst_pfn;These are the host frame numbers correct? Trying to distinguish them from 'gfn' and 'gpa' used in the function.
Yes host pfns.
Quoted 13 lines
Quoted 13 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +12
-0+struct migrate_vma mig; +struct vm_area_struct *vma; +int srcu_idx; +unsigned long gfn = gpa >> page_shift; +struct kvm_memory_slot *slot; +unsigned long *rmap; +int ret; + +if (page_shift !=PAGE_SHIFT ) +return H_P3 ; + +if (flags)
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 168
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+return H_P2 ; + + ret =H_PARAMETER ; +down_read (&kvm->mm->mmap_sem); + srcu_idx =srcu_read_lock (&kvm->srcu); + slot =gfn_to_memslot (kvm, gfn);Can slot be NULL? could be a bug in UV...
Will add a check to test this failure.
Quoted 5 lines
arch/powerpc/kvm/book3s_hv_devm.c line 170
Quoted 3 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +2
-0+ rmap = &slot->arch.rmap[gfn - slot->base_gfn]; + addr =gfn_to_hva (kvm, gpa >> page_shift);Use 'gfn' as the second parameter?
Yes.
Quoted 3 lines
Nit. for consistency with gpa and gfn, maybe rename 'addr' to 'hva' or to match 'end' maybe to 'start'.
Guess using hva improves readability, sure.
Quoted 4 lines
Also, can we check '
kvmppc_rmap_is_devm_pfn(*rmap)' here and bail out if its already shared? We currently do it further down the call chain inkvmppc_devm_get_page()after doing more work.
If the page is already shared, we just give the same back to UV if UV indeed asks for it to be re-shared.
That said, I think we can have kvmppc_rmap_is_devm_pfn early in regular page-in (non-shared case) path so that we don't even setup anything required for migrate_vma_pages.
Quoted 47 lines
Quoted 47 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +45
-0+if (kvm_is_error_hva (addr)) + goto out; + + end = addr + (1UL << page_shift); + vma =find_vma_intersection (kvm->mm, addr, end); +if (!vma || vma->vm_start > addr || vma->vm_end < end) + goto out; + +memset (&mig,0 ,sizeof (mig)); + mig.vma = vma; + mig.start = addr; + mig.end = end; + mig.src = &src_pfn; + mig.dst = &dst_pfn; + +if (migrate_vma_setup (&mig)) + goto out; + +if (kvmppc_devm_migrate_alloc_and_copy (&mig, rmap, gpa, + kvm->arch.lpid, page_shift)) + goto out_finalize; + +migrate_vma_pages (&mig); + ret =H_SUCCESS ; +out_finalize: +migrate_vma_finalize (&mig); +out: +srcu_read_unlock (&kvm->srcu, srcu_idx); +up_read (&kvm->mm->mmap_sem); +return ret; +} + +/* +* Provision a new page on HV side and copy over the contents +* from secure memory. +*/ +static int +kvmppc_devm_fault_migrate_alloc_and_copy (struct migrate_vma *mig, +unsigned long page_shift) +{ +struct page *dpage, *spage; +struct kvmppc_devm_page_pvt *pvt; +unsigned long pfn; +int ret; +
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 220
Quoted 5 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +5
-0+ spage =migrate_pfn_to_page (*mig->src); +if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE )) +return 0 ; +if (!is_zone_device_page (spage)) +return 0 ;What does it mean if its not a
zone_devicepage at this point? Caller would then proceed tomigrage_vma_pages()if we return 0 right?
kvmppc_devm_fault_migrate_alloc_and_copy() can be called from two paths:
1. Fault path when HV touches the secure page. In this case the page has to be a device page.
2. When page-out is issued for a page that is already paged-in. In this case also it has be a device page.
For both the above cases, that check is redundant.
There is a 3rd case which is possible. If UV ever issues a page-out for a shared page, this check will result in page-out hcall silently succeeding w/o doing any migration (as we don't populate the dst_pfn)
Quoted 56 lines
Quoted 56 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +55
-0+ + dpage =alloc_page_vma (GFP_HIGHUSER , mig->vma, mig->start); +if (!dpage) +return -EINVAL ; +lock_page (dpage); + pvt = spage->zone_device_data; + + pfn =page_to_pfn (dpage); + ret =uv_page_out (pvt->lpid, pfn << page_shift, pvt->gpa,0 , + page_shift); +if (ret =U_SUCCESS ) + *mig->dst =migrate_pfn (pfn) |MIGRATE_PFN_LOCKED ; +else { +unlock_page (dpage); +__free_page (dpage); + } +return ret; +} + +/* +* Fault handler callback when HV touches any page that has been +* moved to secure memory, we ask UV to give back the page by +* issuing a UV_PAGE_OUT uvcall. +* +* This eventually results in dropping of device PFN and the newly +* provisioned page/PFN gets populated in QEMU page tables. +*/ +static vm_fault_t kvmppc_devm_migrate_to_ram (struct vm_fault *vmf) +{ +unsigned long src_pfn, dst_pfn =0 ; +struct migrate_vma mig; +int ret =0 ; + +memset (&mig,0 ,sizeof (mig)); + mig.vma = vmf->vma; + mig.start = vmf->address; + mig.end = vmf->address +PAGE_SIZE ; + mig.src = &src_pfn; + mig.dst = &dst_pfn; + +if (migrate_vma_setup (&mig)) { + ret =VM_FAULT_SIGBUS ; + goto out; + } + +if (kvmppc_devm_fault_migrate_alloc_and_copy (&mig,PAGE_SHIFT )) { + ret =VM_FAULT_SIGBUS ; + goto out_finalize; + } + +migrate_vma_pages (&mig); +out_finalize: +migrate_vma_finalize (&mig); +out: +return ret;
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 281
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+} + +/* +* Release the device PFN back to the pool +* +* Gets called when secure page becomes a normal page during UV_PAGE_OUT. Nit: Should that be
H_SVM_PAGE_OUT?
Yes, will reword.
Quoted 14 lines
arch/powerpc/kvm/book3s_hv_devm.c line 291
Quoted 11 lines
+ */
arch/powerpc/kvm/book3s_hv_devm.c new file +9
-0+static void kvmppc_devm_page_free (struct page *page) +{ +unsigned long pfn =page_to_pfn (page); +unsigned long flags; +struct kvmppc_devm_page_pvt *pvt; + +spin_lock_irqsave (&kvmppc_devm_pfn_lock, flags); + pvt = page->zone_device_data; + page->zone_device_data =NULL ;If the
pfn_lockonly protects the bitmap, would be better to move it here?
Yes.
Quoted 23 lines
Quoted 23 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +22
-0+ +bitmap_clear (kvmppc_devm_pfn_bitmap, + pfn - (kvmppc_devm_pgmap.res.start >>PAGE_SHIFT ),1 ); + *pvt->rmap =0 ; +spin_unlock_irqrestore (&kvmppc_devm_pfn_lock, flags); +kfree (pvt); +} + +static const struct dev_pagemap_ops kvmppc_devm_ops = { + .page_free = kvmppc_devm_page_free, + .migrate_to_ram = kvmppc_devm_migrate_to_ram, +}; + +/* +* Move page from secure memory to normal memory. +*/ +unsigned long +kvmppc_h_svm_page_out (struct kvm *kvm,unsigned long gpa, +unsigned long flags,unsigned long page_shift) +{ +struct migrate_vma mig; +unsigned long addr, end;
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 317
Quoted 4 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +4
-0+struct vm_area_struct *vma; +unsigned long src_pfn, dst_pfn =0 ; +int srcu_idx; +int ret;Nit: Not sure its a coding style requirement, but many functions seem to "sort" these local variables in descending order of line length for appearance :-) (eg: migrate_vma* functions).
It has ended up like this over multiple versions when variables got added, moved and re-added.
Quoted 29 lines
Quoted 29 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +28
-0+ +if (page_shift !=PAGE_SHIFT ) +return H_P3 ; + +if (flags) +return H_P2 ; + + ret =H_PARAMETER ; +down_read (&kvm->mm->mmap_sem); + srcu_idx =srcu_read_lock (&kvm->srcu); + addr =gfn_to_hva (kvm, gpa >> page_shift); +if (kvm_is_error_hva (addr)) + goto out; + + end = addr + (1UL << page_shift); + vma =find_vma_intersection (kvm->mm, addr, end); +if (!vma || vma->vm_start > addr || vma->vm_end < end) + goto out; + +memset (&mig,0 ,sizeof (mig)); + mig.vma = vma; + mig.start = addr; + mig.end = end; + mig.src = &src_pfn; + mig.dst = &dst_pfn; +if (migrate_vma_setup (&mig)) + goto out; +
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 351
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+ ret =kvmppc_devm_fault_migrate_alloc_and_copy (&mig, page_shift); +if (ret) + goto out_finalize; + +migrate_vma_pages (&mig); + ret =H_SUCCESS ;Nit: Blank line here?
With a blank like above the label line (which is blank for the most part), it looks a bit too much of blank to me :)
However I do have blank line at a few other places. I have been removing them whenever I touch the surrounding lines.
Thanks for your review.
Christoph - You did review this patch in the last iteration. Do you have any additional comments?
Regards,
Bharata.
Re: [PATCH v7 1/7] kvmppc: Driver to manage pages of secure guest
Quoted 30 lines · Bharata B Rao [[email protected]] wrote:
Quoted 13 lines · Sukadev Bhattiprolu
Some minor comments/questions below. Overall, the patches look fine to me.
arch/powerpc/kvm/book3s_hv_devm.c line 43
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +8
-0+#include <linux/pagemap.h> +#include <linux/migrate.h> +#include <linux/kvm_host.h> +#include <asm/ultravisor.h> + +static struct dev_pagemap kvmppc_devm_pgmap; +static unsigned long *kvmppc_devm_pfn_bitmap; +static DEFINE_SPINLOCK (kvmppc_devm_pfn_lock);Is this lock protecting just the
pfn_bitmap?Yes.
Quoted 13 lines
Quoted 13 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +12
-0+ +struct kvmppc_devm_page_pvt { +unsigned long *rmap; +unsigned int lpid; +unsigned long gpa; +}; + +/* +* 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.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
+ *
+ * @rmap here is the slot in the rmap array that corresponds to @gpa.
+ * Thus a non-zero rmap entry indicates that the corresponding 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 uselock_rmap()here?
Where do we serialize two threads attempting to H_SVM_PAGE_IN the same gfn at the same time? Or one thread issuing a H_SVM_PAGE_IN and another a H_SVM_PAGE_OUT for the same page?
Quoted 50 lines
Quoted 11 lines
Quoted 11 lines
+ */
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+static struct page *kvmppc_devm_get_page (unsigned long *rmap,unsigned long gpa, +unsigned int lpid) +{ +struct page *dpage =NULL ; +unsigned long bit, devm_pfn; +unsigned long flags; +struct kvmppc_devm_page_pvt *pvt; +unsigned long pfn_last, pfn_first; + +if (kvmppc_rmap_is_devm_pfn (*rmap))Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 80
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+return NULL ; + + pfn_first = kvmppc_devm_pgmap.res.start >>PAGE_SHIFT ; + pfn_last = pfn_first + + (resource_size (&kvmppc_devm_pgmap.res) >>PAGE_SHIFT ); +spin_lock_irqsave (&kvmppc_devm_pfn_lock, flags);Blank lines around
spin_lock()would help.You mean blank line before lock and after unlock to clearly see where the lock starts and ends?
Quoted 12 lines
arch/powerpc/kvm/book3s_hv_devm.c line 86
Quoted 7 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+ bit =find_first_zero_bit (kvmppc_devm_pfn_bitmap, pfn_last - pfn_first); +if (bit >= (pfn_last - pfn_first)) + goto out; + +bitmap_set (kvmppc_devm_pfn_bitmap, bit,1 ); + devm_pfn = bit + pfn_first;Can we drop the
&kvmppc_devm_pfn_lockhere or after thetrylock_page()? Or does it also protect the ->zone_device_data' assignment below as well? If so, maybe drop the 'pfn_' from the name of the lock?Quoted 2 lines
Besides, we don't seem to hold this lock when accessing ->zone_device_data in
kvmppc_share_page(). Maybe&kvmppc_devm_pfn_lockjust protects the bitmap?Will move the unlock to appropriately.
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c line 87
Quoted 3 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +1
-0+ dpage =pfn_to_page (devm_pfn);Does this code and hence
CONFIG_PPC_UVdepend on a specific model likeCONFIG_SPARSEMEM_VMEMMAP?I don't think so. Irrespective of that
pfn_to_page()should just work for us.
Quoted 8 lines
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 95
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +8
-0+ +if (!trylock_page (dpage)) + goto out_clear; + + *rmap = devm_pfn |KVMPPC_RMAP_DEVM_PFN ; + pvt =kzalloc (sizeof (*pvt),GFP_ATOMIC ); +if (!pvt) + goto out_unlock;
If we fail to alloc, we don't clear the KVMPPC_RMAP_DEVM_PFN?
Also, when/where do we clear this flag on an uv-page-out? kvmppc_devm_drop_pages() drops the flag on a local variable but not in the rmap? If we don't clear the flag on page-out, would the subsequent H_SVM_PAGE_IN of this page fail?
Quoted 177 lines
Quoted 55 lines
arch/powerpc/kvm/book3s_hv_devm.c line 99
Quoted 4 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +4
-0+ pvt->rmap = rmap; + pvt->gpa = gpa; + pvt->lpid = lpid; + dpage->zone_device_data = pvt;->zone_device_data is set after locking the dpage here, but in
kvmppc_share_page()andkvmppc_devm_fault_migrate_alloc_and_copy()it is accessed without locking the page?Quoted 46 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +46
-0+spin_unlock_irqrestore (&kvmppc_devm_pfn_lock, flags); + +get_page (dpage); +return dpage; + +out_unlock: +unlock_page (dpage); +out_clear: +bitmap_clear (kvmppc_devm_pfn_bitmap, devm_pfn - pfn_first,1 ); +out: +spin_unlock_irqrestore (&kvmppc_devm_pfn_lock, flags); +return NULL ; +} + +/* +* Alloc a PFN from private device memory pool and copy page from normal +* memory to secure memory. +*/ +static int +kvmppc_devm_migrate_alloc_and_copy (struct migrate_vma *mig, +unsigned long *rmap,unsigned long gpa, +unsigned int lpid,unsigned long page_shift) +{ +struct page *spage =migrate_pfn_to_page (*mig->src); +unsigned long pfn = *mig->src >>MIGRATE_PFN_SHIFT ; +struct page *dpage; + + *mig->dst =0 ; +if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE )) +return 0 ; + + dpage =kvmppc_devm_get_page (rmap, gpa, lpid); +if (!dpage) +return -EINVAL ; + +if (spage) +uv_page_in (lpid, pfn << page_shift, gpa,0 , page_shift); + + *mig->dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; +return 0 ; +} + +/* +* Move page from normal memory to secure memory. +*/ +unsigned long Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 150
Quoted 5 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +5
-0+kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gpa, + unsigned long flags, unsigned long page_shift) +{ + unsigned long addr, end; +unsigned long src_pfn, dst_pfn;These are the host frame numbers correct? Trying to distinguish them from 'gfn' and 'gpa' used in the function.
Yes host pfns.
Quoted 13 lines
Quoted 13 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +12
-0+struct migrate_vma mig; +struct vm_area_struct *vma; +int srcu_idx; +unsigned long gfn = gpa >> page_shift; +struct kvm_memory_slot *slot; +unsigned long *rmap; +int ret; + +if (page_shift !=PAGE_SHIFT ) +return H_P3 ; + +if (flags)Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 168
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+return H_P2 ; + + ret =H_PARAMETER ; +down_read (&kvm->mm->mmap_sem); + srcu_idx =srcu_read_lock (&kvm->srcu); + slot =gfn_to_memslot (kvm, gfn);Can slot be NULL? could be a bug in UV...
Will add a check to test this failure.
Quoted 5 lines
arch/powerpc/kvm/book3s_hv_devm.c line 170
Quoted 3 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +2
-0+ rmap = &slot->arch.rmap[gfn - slot->base_gfn]; + addr =gfn_to_hva (kvm, gpa >> page_shift);Use 'gfn' as the second parameter?
Yes.
Quoted 3 lines
Nit. for consistency with gpa and gfn, maybe rename 'addr' to 'hva' or to match 'end' maybe to 'start'.
Guess using hva improves readability, sure.
Quoted 4 lines
Also, can we check '
kvmppc_rmap_is_devm_pfn(*rmap)' here and bail out if its already shared? We currently do it further down the call chain inkvmppc_devm_get_page()after doing more work.If the page is already shared, we just give the same back to UV if UV indeed asks for it to be re-shared.
That said, I think we can have
kvmppc_rmap_is_devm_pfnearly in regular page-in (non-shared case) path so that we don't even setup anything required formigrate_vma_pages.Quoted 47 lines
Quoted 47 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +45
-0+if (kvm_is_error_hva (addr)) + goto out; + + end = addr + (1UL << page_shift); + vma =find_vma_intersection (kvm->mm, addr, end); +if (!vma || vma->vm_start > addr || vma->vm_end < end) + goto out; + +memset (&mig,0 ,sizeof (mig)); + mig.vma = vma; + mig.start = addr; + mig.end = end; + mig.src = &src_pfn; + mig.dst = &dst_pfn; + +if (migrate_vma_setup (&mig)) + goto out; + +if (kvmppc_devm_migrate_alloc_and_copy (&mig, rmap, gpa, + kvm->arch.lpid, page_shift)) + goto out_finalize; + +migrate_vma_pages (&mig); + ret =H_SUCCESS ; +out_finalize: +migrate_vma_finalize (&mig); +out: +srcu_read_unlock (&kvm->srcu, srcu_idx); +up_read (&kvm->mm->mmap_sem); +return ret; +} + +/* +* Provision a new page on HV side and copy over the contents +* from secure memory. +*/ +static int +kvmppc_devm_fault_migrate_alloc_and_copy (struct migrate_vma *mig, +unsigned long page_shift) +{ +struct page *dpage, *spage; +struct kvmppc_devm_page_pvt *pvt; +unsigned long pfn; +int ret; +Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 220
Quoted 5 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +5
-0+ spage =migrate_pfn_to_page (*mig->src); +if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE )) +return 0 ; +if (!is_zone_device_page (spage)) +return 0 ;What does it mean if its not a
zone_devicepage at this point? Caller would then proceed tomigrage_vma_pages()if we return 0 right?
kvmppc_devm_fault_migrate_alloc_and_copy()can be called from two paths:1. Fault path when HV touches the secure page. In this case the page has to be a device page.
Quoted 8 lines
2. When page-out is issued for a page that is already paged-in. In this case also it has be a device page.
For both the above cases, that check is redundant.
There is a 3rd case which is possible. If UV ever issues a page-out for a shared page, this check will result in page-out hcall silently succeeding w/o doing any migration (as we don't populate the
dst_pfn)
Ok. Nit. thought we can drop the "_fault" in the function name but would collide the other "alloc_and_copy" function used during H_SVM_PAGE_IN. If the two alloc_and_copy functions are symmetric, maybe they could have "page_in" and "page_out" in the (already long) names.
Quoted 171 lines
Quoted 57 lines
Quoted 57 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +55
-0+ + dpage =alloc_page_vma (GFP_HIGHUSER , mig->vma, mig->start); +if (!dpage) +return -EINVAL ; +lock_page (dpage); + pvt = spage->zone_device_data; + + pfn =page_to_pfn (dpage); + ret =uv_page_out (pvt->lpid, pfn << page_shift, pvt->gpa,0 , + page_shift); +if (ret =U_SUCCESS ) + *mig->dst =migrate_pfn (pfn) |MIGRATE_PFN_LOCKED ; +else { +unlock_page (dpage); +__free_page (dpage); + } +return ret; +} + +/* +* Fault handler callback when HV touches any page that has been +* moved to secure memory, we ask UV to give back the page by +* issuing a UV_PAGE_OUT uvcall. +* +* This eventually results in dropping of device PFN and the newly +* provisioned page/PFN gets populated in QEMU page tables. +*/ +static vm_fault_t kvmppc_devm_migrate_to_ram (struct vm_fault *vmf) +{ +unsigned long src_pfn, dst_pfn =0 ; +struct migrate_vma mig; +int ret =0 ; + +memset (&mig,0 ,sizeof (mig)); + mig.vma = vmf->vma; + mig.start = vmf->address; + mig.end = vmf->address +PAGE_SIZE ; + mig.src = &src_pfn; + mig.dst = &dst_pfn; + +if (migrate_vma_setup (&mig)) { + ret =VM_FAULT_SIGBUS ; + goto out; + } + +if (kvmppc_devm_fault_migrate_alloc_and_copy (&mig,PAGE_SHIFT )) { + ret =VM_FAULT_SIGBUS ; + goto out_finalize; + } + +migrate_vma_pages (&mig); +out_finalize: +migrate_vma_finalize (&mig); +out: +return ret;Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 281
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+} + +/* +* Release the device PFN back to the pool +* +* Gets called when secure page becomes a normal page during UV_PAGE_OUT. Nit: Should that be
H_SVM_PAGE_OUT?Yes, will reword.
Quoted 14 lines
arch/powerpc/kvm/book3s_hv_devm.c line 291
Quoted 11 lines
+ */
arch/powerpc/kvm/book3s_hv_devm.c new file +9
-0+static void kvmppc_devm_page_free (struct page *page) +{ +unsigned long pfn =page_to_pfn (page); +unsigned long flags; +struct kvmppc_devm_page_pvt *pvt; + +spin_lock_irqsave (&kvmppc_devm_pfn_lock, flags); + pvt = page->zone_device_data; + page->zone_device_data =NULL ;If the
pfn_lockonly protects the bitmap, would be better to move it here?Yes.
Quoted 23 lines
Quoted 23 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +22
-0+ +bitmap_clear (kvmppc_devm_pfn_bitmap, + pfn - (kvmppc_devm_pgmap.res.start >>PAGE_SHIFT ),1 ); + *pvt->rmap =0 ; +spin_unlock_irqrestore (&kvmppc_devm_pfn_lock, flags); +kfree (pvt); +} + +static const struct dev_pagemap_ops kvmppc_devm_ops = { + .page_free = kvmppc_devm_page_free, + .migrate_to_ram = kvmppc_devm_migrate_to_ram, +}; + +/* +* Move page from secure memory to normal memory. +*/ +unsigned long +kvmppc_h_svm_page_out (struct kvm *kvm,unsigned long gpa, +unsigned long flags,unsigned long page_shift) +{ +struct migrate_vma mig; +unsigned long addr, end;Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 317
Quoted 4 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +4
-0+struct vm_area_struct *vma; +unsigned long src_pfn, dst_pfn =0 ; +int srcu_idx; +int ret;Nit: Not sure its a coding style requirement, but many functions seem to "sort" these local variables in descending order of line length for appearance :-) (eg: migrate_vma* functions).
It has ended up like this over multiple versions when variables got added, moved and re-added.
Quoted 29 lines
Quoted 29 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +28
-0+ +if (page_shift !=PAGE_SHIFT ) +return H_P3 ; + +if (flags) +return H_P2 ; + + ret =H_PARAMETER ; +down_read (&kvm->mm->mmap_sem); + srcu_idx =srcu_read_lock (&kvm->srcu); + addr =gfn_to_hva (kvm, gpa >> page_shift); +if (kvm_is_error_hva (addr)) + goto out; + + end = addr + (1UL << page_shift); + vma =find_vma_intersection (kvm->mm, addr, end); +if (!vma || vma->vm_start > addr || vma->vm_end < end) + goto out; + +memset (&mig,0 ,sizeof (mig)); + mig.vma = vma; + mig.start = addr; + mig.end = end; + mig.src = &src_pfn; + mig.dst = &dst_pfn; +if (migrate_vma_setup (&mig)) + goto out; +Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 351
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+ ret =kvmppc_devm_fault_migrate_alloc_and_copy (&mig, page_shift); +if (ret) + goto out_finalize; + +migrate_vma_pages (&mig); + ret =H_SUCCESS ;Nit: Blank line here?
With a blank like above the label line (which is blank for the most part), it looks a bit too much of blank to me :)
However I do have blank line at a few other places. I have been removing them whenever I touch the surrounding lines.
Thanks for your review.
Christoph - You did review this patch in the last iteration. Do you have any additional comments?
Regards,
Bharata.
Re: [PATCH v7 1/7] kvmppc: Driver to manage pages of secure guest
Quoted 5 lines · Sukadev Bhattiprolu
Quoted 1 line · Bharata B Rao [[email protected]] wrote:
On Wed, Aug 28, 2019 at 08:02:19PM -0700, Sukadev Bhattiprolu wrote:
Where do we serialize two threads attempting to
H_SVM_PAGE_INthe same gfn at the same time? Or one thread issuing aH_SVM_PAGE_INand another aH_SVM_PAGE_OUTfor the same page?
I am not not serializing page-in/out calls on same gfn, I thought you take care of that in UV, guess UV doesn't yet.
I can probably use rmap_lock() and serialize such calls in HV if UV can't prevent such calls easily.
Quoted 10 lines
Quoted 8 lines
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 95
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +8
-0+ +if (!trylock_page (dpage)) + goto out_clear; + + *rmap = devm_pfn |KVMPPC_RMAP_DEVM_PFN ; + pvt =kzalloc (sizeof (*pvt),GFP_ATOMIC ); +if (!pvt) + goto out_unlock;If we fail to alloc, we don't clear the
KVMPPC_RMAP_DEVM_PFN?
Right, I will move the assignment to *rmap to after kzalloc.
Quoted 5 lines
Also, when/where do we clear this flag on an uv-page-out?
kvmppc_devm_drop_pages()drops the flag on a local variable but not in the rmap? If we don't clear the flag on page-out, would the subsequentH_SVM_PAGE_INof this page fail?
It gets cleared in kvmppc_devm_page_free().
Quoted 5 lines
Ok. Nit. thought we can drop the "
_fault" in the function name but would collide the other "alloc_and_copy" function used duringH_SVM_PAGE_IN. If the twoalloc_and_copyfunctions are symmetric, maybe they could have "page_in" and "page_out" in the (already long) names.
Christoph also suggested to reorganize these two calls. Will take care.
Regards,
Bharata.
Re: [PATCH v7 1/7] kvmppc: Driver to manage pages of secure guest
In reply to Bharata B Raoarch/powerpc/include/asm/kvm_host.h struct revmap_entry { · line 249
Quoted 5 lines · Bharata B Rao
arch/powerpc/include/asm/kvm_host.h +5
-0+/* +* Bits 60:56 in the rmap entry will be used to identify the +* different uses/functions of rmap. +*/ +#define KVMPPC_RMAP_DEVM_PFN (0x2ULL << 56)
How did you come up with this specific value?
arch/powerpc/include/asm/kvm_host.h struct revmap_entry { · line 254
Quoted 5 lines
arch/powerpc/include/asm/kvm_host.h +5
-0+ +static inline bool kvmppc_rmap_is_devm_pfn (unsigned long pfn) +{ +return !!(pfn &KVMPPC_RMAP_DEVM_PFN ); +}
No need for !! when returning a bool. Also the helper seems a little pointless, just opencoding it would make the code more readable in my opinion.
arch/powerpc/include/asm/kvm_host.h static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {} · line 865
Quoted 3 lines
arch/powerpc/include/asm/kvm_host.h +3
-0+#ifdef CONFIG_PPC_UV +extern int kvmppc_devm_init (void ); +extern void kvmppc_devm_free (void );
There is no need for extern in a function declaration.
Quoted 13 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +13
-0+static int +kvmppc_devm_migrate_alloc_and_copy (struct migrate_vma *mig, +unsigned long *rmap,unsigned long gpa, +unsigned int lpid,unsigned long page_shift) +{ +struct page *spage =migrate_pfn_to_page (*mig->src); +unsigned long pfn = *mig->src >>MIGRATE_PFN_SHIFT ; +struct page *dpage; + + *mig->dst =0 ; +if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE )) +return 0 ; +
arch/powerpc/kvm/book3s_hv_devm.c line 140
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+ dpage =kvmppc_devm_get_page (rmap, gpa, lpid); +if (!dpage) +return -EINVAL ; + +if (spage) +uv_page_in (lpid, pfn << page_shift, gpa,0 , page_shift); + + *mig->dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; +return 0 ; +}
I think you can just merge this trivial helper into the only caller.
Quoted 22 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +22
-0+static int +kvmppc_devm_fault_migrate_alloc_and_copy (struct migrate_vma *mig, +unsigned long page_shift) +{ +struct page *dpage, *spage; +struct kvmppc_devm_page_pvt *pvt; +unsigned long pfn; +int ret; + + spage =migrate_pfn_to_page (*mig->src); +if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE )) +return 0 ; +if (!is_zone_device_page (spage)) +return 0 ; + + dpage =alloc_page_vma (GFP_HIGHUSER , mig->vma, mig->start); +if (!dpage) +return -EINVAL ; +lock_page (dpage); + pvt = spage->zone_device_data; + + pfn =page_to_pfn (dpage);
arch/powerpc/kvm/book3s_hv_devm.c line 238
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +10
-0+ ret =uv_page_out (pvt->lpid, pfn << page_shift, pvt->gpa,0 , + page_shift); +if (ret =U_SUCCESS ) + *mig->dst =migrate_pfn (pfn) |MIGRATE_PFN_LOCKED ; +else { +unlock_page (dpage); +__free_page (dpage); + } +return ret; +}
Here we actually have two callers, but they have a fair amount of duplicate code in them. I think you want to move that common code (including setting up the migrate_vma structure) into this function and maybe also give it a more descriptive name.
arch/powerpc/kvm/book3s_hv_devm.c line 294
Quoted 12 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +12
-0+static void kvmppc_devm_page_free (struct page *page) +{ +unsigned long pfn =page_to_pfn (page); +unsigned long flags; +struct kvmppc_devm_page_pvt *pvt; + +spin_lock_irqsave (&kvmppc_devm_pfn_lock, flags); + pvt = page->zone_device_data; + page->zone_device_data =NULL ; + +bitmap_clear (kvmppc_devm_pfn_bitmap, + pfn - (kvmppc_devm_pgmap.res.start >>PAGE_SHIFT ),1 );
Nit: I'd just initialize pfn to the value you want from the start. That makes the code a little easier to read, and keeps a tiny bit more code outside the spinlock.
unsigned long pfn = page_to_pfn(page) - (kvmppc_devm_pgmap.res.start >> PAGE_SHIFT); .. bitmap_clear(kvmppc_devm_pfn_bitmap, pfn, 1);
arch/powerpc/kvm/book3s_hv_devm.c line 407
Quoted 4 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +4
-0+ kvmppc_devm_pgmap.type =MEMORY_DEVICE_PRIVATE ; + kvmppc_devm_pgmap.res = *res; + kvmppc_devm_pgmap.ops = &kvmppc_devm_ops; + addr =memremap_pages (&kvmppc_devm_pgmap,-1 );
This -1 should be NUMA_NO_NODE for clarity.
Re: [PATCH v7 1/7] kvmppc: Driver to manage pages of secure guest
Quoted 8 lines · Christoph Hellwig
arch/powerpc/include/asm/kvm_host.h struct revmap_entry { · line 249
Quoted 5 lines · Bharata B Rao
arch/powerpc/include/asm/kvm_host.h +5
-0+/* +* Bits 60:56 in the rmap entry will be used to identify the +* different uses/functions of rmap. +*/ +#define KVMPPC_RMAP_DEVM_PFN (0x2ULL << 56)How did you come up with this specific value?
Different usage types of RMAP array are being defined.
https://patchwork.ozlabs.org/patch/1149791/
The above value is reserved for device pfn usage.
Quoted 10 lines
arch/powerpc/include/asm/kvm_host.h struct revmap_entry { · line 254
Quoted 6 lines
arch/powerpc/include/asm/kvm_host.h +5
-0+ +static inline bool kvmppc_rmap_is_devm_pfn (unsigned long pfn) +{ +return !!(pfn &KVMPPC_RMAP_DEVM_PFN ); +}No need for !! when returning a bool. Also the helper seems a little pointless, just opencoding it would make the code more readable in my opinion.
I expect similar routines for other usages of RMAP to come up.
Quoted 24 lines
arch/powerpc/include/asm/kvm_host.h static inline void kvm_arch_vcpu_blocking(struct kvm_vcpu *vcpu) {} · line 865
Quoted 4 lines
arch/powerpc/include/asm/kvm_host.h +3
-0+#ifdef CONFIG_PPC_UV +extern int kvmppc_devm_init (void ); +extern void kvmppc_devm_free (void );There is no need for extern in a function declaration.
Quoted 17 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +17
-0+static int +kvmppc_devm_migrate_alloc_and_copy (struct migrate_vma *mig, +unsigned long *rmap,unsigned long gpa, +unsigned int lpid,unsigned long page_shift) +{ +struct page *spage =migrate_pfn_to_page (*mig->src); +unsigned long pfn = *mig->src >>MIGRATE_PFN_SHIFT ; +struct page *dpage; + + *mig->dst =0 ; +if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE )) +return 0 ; + + dpage =kvmppc_devm_get_page (rmap, gpa, lpid); +if (!dpage) +return -EINVAL ; +
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 140
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +6
-0+if (spage) +uv_page_in (lpid, pfn << page_shift, gpa,0 , page_shift); + + *mig->dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; +return 0 ; +}I think you can just merge this trivial helper into the only caller.
Yes I can, but felt it is nicely abstracted out to a function right now.
Quoted 30 lines
Quoted 30 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +29
-0+static int +kvmppc_devm_fault_migrate_alloc_and_copy (struct migrate_vma *mig, +unsigned long page_shift) +{ +struct page *dpage, *spage; +struct kvmppc_devm_page_pvt *pvt; +unsigned long pfn; +int ret; + + spage =migrate_pfn_to_page (*mig->src); +if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE )) +return 0 ; +if (!is_zone_device_page (spage)) +return 0 ; + + dpage =alloc_page_vma (GFP_HIGHUSER , mig->vma, mig->start); +if (!dpage) +return -EINVAL ; +lock_page (dpage); + pvt = spage->zone_device_data; + + pfn =page_to_pfn (dpage); + ret =uv_page_out (pvt->lpid, pfn << page_shift, pvt->gpa,0 , + page_shift); +if (ret ==U_SUCCESS ) + *mig->dst =migrate_pfn (pfn) |MIGRATE_PFN_LOCKED ; +else { +unlock_page (dpage); +__free_page (dpage);
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 201
Quoted 3 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +3
-0+ } +return ret; +}Here we actually have two callers, but they have a fair amount of duplicate code in them. I think you want to move that common code (including setting up the
migrate_vmastructure) into this function and maybe also give it a more descriptive name.
Sure, I will give this a try. The name is already very descriptive, will come up with an appropriate name.
BTW this file and the fuction prefixes in this file started out with kvmppc_hmm, switched to kvmppc_devm when HMM routines weren't used anymore. Now with the use of only non-dev versions, planning to swtich to kvmppc_uvmem_
Quoted 18 lines
arch/powerpc/kvm/book3s_hv_devm.c line 294
Quoted 13 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +12
-0+static void kvmppc_devm_page_free (struct page *page) +{ +unsigned long pfn =page_to_pfn (page); +unsigned long flags; +struct kvmppc_devm_page_pvt *pvt; + +spin_lock_irqsave (&kvmppc_devm_pfn_lock, flags); + pvt = page->zone_device_data; + page->zone_device_data =NULL ; + +bitmap_clear (kvmppc_devm_pfn_bitmap, + pfn - (kvmppc_devm_pgmap.res.start >>PAGE_SHIFT ),1 );Nit: I'd just initialize pfn to the value you want from the start. That makes the code a little easier to read, and keeps a tiny bit more code outside the spinlock.
Quoted 6 lines
unsigned long pfn = page_to_pfn(page) - (kvmppc_devm_pgmap.res.start >> PAGE_SHIFT); .. bitmap_clear(kvmppc_devm_pfn_bitmap, pfn, 1);
Sure.
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c line 407
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +4
-0+ kvmppc_devm_pgmap.type =MEMORY_DEVICE_PRIVATE ; + kvmppc_devm_pgmap.res = *res; + kvmppc_devm_pgmap.ops = &kvmppc_devm_ops; + addr =memremap_pages (&kvmppc_devm_pgmap,-1 );This -1 should be
NUMA_NO_NODEfor clarity.
Right.
Regards,
Bharata.
Re: [PATCH v7 1/7] kvmppc: Driver to manage pages of secure guest
Quoted 10 lines · Bharata B Rao
Quoted 8 lines · Christoph Hellwig
arch/powerpc/include/asm/kvm_host.h struct revmap_entry { · line 249
Quoted 5 lines · Bharata B Rao
arch/powerpc/include/asm/kvm_host.h +5
-0+/* +* Bits 60:56 in the rmap entry will be used to identify the +* different uses/functions of rmap. +*/ +#define KVMPPC_RMAP_DEVM_PFN (0x2ULL << 56)How did you come up with this specific value?
Quoted 4 lines
Different usage types of RMAP array are being defined.
https://patchwork.ozlabs.org/patch/1149791/The above value is reserved for device pfn usage.
Shouldn't all these defintions go in together in a patch? Also is bi t 56+ a set of values, so is there 1 << 56 and 3 << 56 as well? Seems like even that other patch doesn't fully define these "pfn" values.
Quoted 5 lines
Quoted 3 lines
No need for !! when returning a bool. Also the helper seems a little pointless, just opencoding it would make the code more readable in my opinion.
I expect similar routines for other usages of RMAP to come up.
Please drop them all. Having to wade through a header to check for a specific bit that also is set manually elsewhere in related code just obsfucates it for the reader.
Quoted 7 lines
Quoted 5 lines
arch/powerpc/kvm/book3s_hv_devm.c line 140
Quoted 3 lines
arch/powerpc/kvm/book3s_hv_devm.c new file +3
-0+ *mig->dst =migrate_pfn (page_to_pfn (dpage)) |MIGRATE_PFN_LOCKED ; +return 0 ; +}I think you can just merge this trivial helper into the only caller.
Yes I can, but felt it is nicely abstracted out to a function right now.
Not really. It just fits the old calling conventions before I removed the indirection.
Quoted 7 lines
Quoted 4 lines
Here we actually have two callers, but they have a fair amount of duplicate code in them. I think you want to move that common code (including setting up the
migrate_vmastructure) into this function and maybe also give it a more descriptive name.Sure, I will give this a try. The name is already very descriptive, will come up with an appropriate name.
I don't think alloc_and_copy is very helpful. It matches some of the implementation, but not the intent. Why not kvmppc_svm_page_in/out similar to the hypervisor calls calling them? Yes, for one case it also gets called from the pagefault handler, but it still performs these basic page in/out actions.
Quoted 4 lines
BTW this file and the fuction prefixes in this file started out with
kvmppc_hmm, switched tokvmppc_devmwhen HMM routines weren't used anymore. Now with the use of only non-dev versions, planning to swtich tokvmppc_uvmem_
That prefix sounds fine to me as well.
Re: [PATCH v7 1/7] kvmppc: Driver to manage pages of secure guest
Quoted 11 lines · Christoph Hellwig
Quoted 10 lines · Bharata B Rao
Quoted 8 lines · Christoph Hellwig
arch/powerpc/include/asm/kvm_host.h struct revmap_entry { · line 249
Quoted 5 lines · Bharata B Rao
arch/powerpc/include/asm/kvm_host.h +5
-0+/* +* Bits 60:56 in the rmap entry will be used to identify the +* different uses/functions of rmap. +*/ +#define KVMPPC_RMAP_DEVM_PFN (0x2ULL << 56)How did you come up with this specific value?
Quoted 6 lines
Quoted 4 lines
Different usage types of RMAP array are being defined.
https://patchwork.ozlabs.org/patch/1149791/The above value is reserved for device pfn usage.
Shouldn't all these defintions go in together in a patch?
Ideally yes, but the above patch is already in Paul's tree, I will sync up with him about this.
Quoted 2 lines
Also is bit 56+ a set of values, so is there 1 << 56 and 3 << 56 as well? Seems like even that other patch doesn't fully define these "pfn" values.
I realized that the bit numbers have changed, it is no longer bits 60:56, but instead top 8bits.
#define KVMPPC_RMAP_UVMEM_PFN 0x0200000000000000
static inline bool kvmppc_rmap_is_uvmem_pfn(unsigned long *rmap)
{
return ((*rmap & 0xff00000000000000) == KVMPPC_RMAP_UVMEM_PFN);
}Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
No need for !! when returning a bool. Also the helper seems a little pointless, just opencoding it would make the code more readable in my opinion.
I expect similar routines for other usages of RMAP to come up.
Please drop them all. Having to wade through a header to check for a specific bit that also is set manually elsewhere in related code just obsfucates it for the reader.
I am currently using the routine kvmppc_rmap_is_uvmem_pfn() (shown above) instead open coding it at multiple places, but I can drop it if you prefer.
Regards,
Bharata.
Re: [PATCH v7 1/7] kvmppc: Driver to manage pages of secure guest
Quoted 11 lines · Bharata B Rao
Quoted 2 lines
Also is bit 56+ a set of values, so is there 1 << 56 and 3 << 56 as well? Seems like even that other patch doesn't fully define these "pfn" values.
I realized that the bit numbers have changed, it is no longer bits 60:56, but instead top 8bits.
#define
KVMPPC_RMAP_UVMEM_PFN0x0200000000000000
static inline boolkvmppc_rmap_is_uvmem_pfn(unsigned long *rmap){ return ((*rmap & 0xff00000000000000) = KVMPPC_RMAP_UVMEM_PFN); }
In that overall scheme I'd actually much prefer something like (names just made up, they should vaguely match the spec this written to):
static inline unsigned long kvmppc_rmap_type(unsigned long *rmap)
{
return (rmap & 0xff00000000000000);
}And then where you check it you can use:
if (kvmppc_rmap_type(*rmap) = KVMPPC_RMAP_UVMEM_PFN)
and where you set it you do:
*rmap |= KVMPPC_RMAP_UVMEM_PFN;
as in the current patch to keep things symmetric.
PATCH v7 2/7 kvmppc: Shared pages support for secure guests
In reply to Bharata B RaoA 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, the device page is unmapped from the HV side page tables.
--- arch/powerpc/include/asm/hvcall.h | 3 ++ arch/powerpc/kvm/book3s_hv_devm.c | 70 +++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 4 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_devm.c +66 -4
102 lines not on this page · raw message
Signature · 1 line
2.21.0
Re: [PATCH v7 2/7] kvmppc: Shared pages support for secure guests
Quoted 69 lines
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, the device page is unmapped from the HV side page tables.
Signed-off-by Bharata B Rao <[email protected]>--- arch/powerpc/include/asm/hvcall.h | 3 ++ arch/powerpc/kvm/book3s_hv_devm.c | 70 +++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 4 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 0xEF04arch/powerpc/kvm/book3s_hv_devm.c +27
-0@@ -46,6 +46,7 @@ struct kvmppc_devm_page_pvt {unsigned long *rmap;unsigned int lpid;unsigned long gpa; +bool skip_page_out; };/* @@ -139,6 +140,54 @@ kvmppc_devm_migrate_alloc_and_copy(struct migrate_vma *mig,return 0 ; } +/* +* Shares the page with HV, thus making it a normal page. +* +* - If the page is already secure, then provision a new page and share +* - If the page is a normal page, share the existing page +* +* In the former case, uses the dev_pagemap_ops migrate_to_ram handler +* to unmap the device page from QEMU's page tables. +*/ +static unsigned long +kvmppc_share_page (struct kvm *kvm,unsigned long gpa,unsigned long page_shift) +{ + +int ret =H_PARAMETER ; +struct page *devm_page; +struct kvmppc_devm_page_pvt *pvt; +unsigned long pfn; +unsigned long *rmap; +struct kvm_memory_slot *slot; +unsigned long gfn = gpa >> page_shift; +int srcu_idx; + + srcu_idx =srcu_read_lock (&kvm->srcu); + slot =gfn_to_memslot (kvm, gfn); +if (!slot) + goto out;
arch/powerpc/kvm/book3s_hv_devm.c kvmppc_devm_migrate_alloc_and_copy(struct migrate_vma *mig, · line 178
Quoted 10 lines
arch/powerpc/kvm/book3s_hv_devm.c +10
-0+ + rmap = &slot->arch.rmap[gfn - slot->base_gfn]; +if (kvmppc_rmap_is_devm_pfn (*rmap)) { + devm_page =pfn_to_page (*rmap & ~KVMPPC_RMAP_DEVM_PFN ); + pvt = (struct kvmppc_devm_page_pvt *) + devm_page->zone_device_data; + pvt->skip_page_out = true; + } + + pfn =gfn_to_pfn (kvm, gpa >> page_shift);
Use 'gfn'?
arch/powerpc/kvm/book3s_hv_devm.c kvmppc_devm_migrate_alloc_and_copy(struct migrate_vma *mig, · line 185
Quoted 7 lines
arch/powerpc/kvm/book3s_hv_devm.c +7
-0+if (is_error_noslot_pfn (pfn)) + goto out; + + ret =uv_page_in (kvm->arch.lpid, pfn << page_shift, gpa,0 , page_shift); +if (ret =U_SUCCESS ) + ret =H_SUCCESS ; +kvm_release_pfn_clean (pfn);
Nit: Blank line?
Quoted 32 lines
arch/powerpc/kvm/book3s_hv_devm.c +10
-2+out: +srcu_read_unlock (&kvm->srcu, srcu_idx); +return ret; +} +/* * Move page from normal memory to secure memory. */ @@ -159,9 +208,12 @@ kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gpa,if (page_shift !=PAGE_SHIFT )return H_P3 ; -if (flags) +if (flags & ~H_PAGE_IN_SHARED )return H_P2 ; +if (flags &H_PAGE_IN_SHARED ) +return kvmppc_share_page (kvm, gpa, page_shift); + ret =H_PARAMETER ;down_read (&kvm->mm->mmap_sem); srcu_idx =srcu_read_lock (&kvm->srcu);@@ -211,7 +263,7 @@ kvmppc_devm_fault_migrate_alloc_and_copy(struct migrate_vma *mig,struct page *dpage, *spage;struct kvmppc_devm_page_pvt *pvt;unsigned long pfn; -int ret; +int ret =U_SUCCESS ; spage =migrate_pfn_to_page (*mig->src);if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE ))
arch/powerpc/kvm/book3s_hv_devm.c kvmppc_devm_fault_migrate_alloc_and_copy(struct migrate_vma *mig, · line 283
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c +3
-2@@ -226,8 +278,18 @@ kvmppc_devm_fault_migrate_alloc_and_copy(struct migrate_vma *mig, pvt = spage->zone_device_data; pfn =page_to_pfn (dpage); - ret =uv_page_out (pvt->lpid, pfn << page_shift, pvt->gpa,0 , - page_shift); + +/* +* This same function is used in two cases:
Nit: s/same//
arch/powerpc/kvm/book3s_hv_devm.c kvmppc_devm_fault_migrate_alloc_and_copy(struct migrate_vma *mig, · line 284
Quoted 1 line
arch/powerpc/kvm/book3s_hv_devm.c +1
-0+* - When HV touches a secure page, for which we do page-out
Better to qualify page-out with "uv page-out"? its kind of counterintuitive to do a page-out on a fault!
Quoted 13 lines
arch/powerpc/kvm/book3s_hv_devm.c +8
-0+* - When a secure page is converted to shared page, we touch +* the page to essentially unmap the device page. In this +* case we skip page-out. +*/ +if (!pvt->skip_page_out) + ret =uv_page_out (pvt->lpid, pfn << page_shift, pvt->gpa,0 , + page_shift); +if (ret =U_SUCCESS ) *mig->dst =migrate_pfn (pfn) |MIGRATE_PFN_LOCKED ;else {Signature · 1 line
2.21.0
Re: [PATCH v7 2/7] kvmppc: Shared pages support for secure guests
Quoted 73 lines · Sukadev Bhattiprolu
Quoted 73 lines
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, the device page is unmapped from the HV side page tables.
Signed-off-by Bharata B Rao <[email protected]>--- arch/powerpc/include/asm/hvcall.h | 3 ++ arch/powerpc/kvm/book3s_hv_devm.c | 70 +++++++++++++++++++++++++++++-- 2 files changed, 69 insertions(+), 4 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 0xEF04arch/powerpc/kvm/book3s_hv_devm.c +31
-0@@ -46,6 +46,7 @@ struct kvmppc_devm_page_pvt {unsigned long *rmap;unsigned int lpid;unsigned long gpa; +bool skip_page_out; };/* @@ -139,6 +140,54 @@ kvmppc_devm_migrate_alloc_and_copy(struct migrate_vma *mig,return 0 ; } +/* +* Shares the page with HV, thus making it a normal page. +* +* - If the page is already secure, then provision a new page and share +* - If the page is a normal page, share the existing page +* +* In the former case, uses the dev_pagemap_ops migrate_to_ram handler +* to unmap the device page from QEMU's page tables. +*/ +static unsigned long +kvmppc_share_page (struct kvm *kvm,unsigned long gpa,unsigned long page_shift) +{ + +int ret =H_PARAMETER ; +struct page *devm_page; +struct kvmppc_devm_page_pvt *pvt; +unsigned long pfn; +unsigned long *rmap; +struct kvm_memory_slot *slot; +unsigned long gfn = gpa >> page_shift; +int srcu_idx; + + srcu_idx =srcu_read_lock (&kvm->srcu); + slot =gfn_to_memslot (kvm, gfn); +if (!slot) + goto out; + + rmap = &slot->arch.rmap[gfn - slot->base_gfn]; +if (kvmppc_rmap_is_devm_pfn (*rmap)) { + devm_page =pfn_to_page (*rmap & ~KVMPPC_RMAP_DEVM_PFN );
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c kvmppc_devm_migrate_alloc_and_copy(struct migrate_vma *mig, · line 178
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c +6
-0+ pvt = (struct kvmppc_devm_page_pvt *) + devm_page->zone_device_data; + pvt->skip_page_out = true; + } + + pfn =gfn_to_pfn (kvm, gpa >> page_shift);Use 'gfn'?
Yes.
Quoted 44 lines
arch/powerpc/kvm/book3s_hv_devm.c kvmppc_devm_migrate_alloc_and_copy(struct migrate_vma *mig, · line 185
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c +7
-0+if (is_error_noslot_pfn (pfn)) + goto out; + + ret =uv_page_in (kvm->arch.lpid, pfn << page_shift, gpa,0 , page_shift); +if (ret =U_SUCCESS ) + ret =H_SUCCESS ; +kvm_release_pfn_clean (pfn);Nit: Blank line?
Quoted 34 lines
arch/powerpc/kvm/book3s_hv_devm.c +10
-2+out: +srcu_read_unlock (&kvm->srcu, srcu_idx); +return ret; +} +/* * Move page from normal memory to secure memory. */ @@ -159,9 +208,12 @@ kvmppc_h_svm_page_in(struct kvm *kvm, unsigned long gpa,if (page_shift !=PAGE_SHIFT )return H_P3 ; -if (flags) +if (flags & ~H_PAGE_IN_SHARED )return H_P2 ; +if (flags &H_PAGE_IN_SHARED ) +return kvmppc_share_page (kvm, gpa, page_shift); + ret =H_PARAMETER ;down_read (&kvm->mm->mmap_sem); srcu_idx =srcu_read_lock (&kvm->srcu);@@ -211,7 +263,7 @@ kvmppc_devm_fault_migrate_alloc_and_copy(struct migrate_vma *mig,struct page *dpage, *spage;struct kvmppc_devm_page_pvt *pvt;unsigned long pfn; -int ret; +int ret =U_SUCCESS ; spage =migrate_pfn_to_page (*mig->src);if (!spage || !(*mig->src &MIGRATE_PFN_MIGRATE ))@@ -226,8 +278,18 @@ kvmppc_devm_fault_migrate_alloc_and_copy(struct migrate_vma *mig, pvt = spage->zone_device_data;
Quoted 8 lines
arch/powerpc/kvm/book3s_hv_devm.c kvmppc_devm_fault_migrate_alloc_and_copy(struct migrate_vma *mig, · line 283
Quoted 6 lines
arch/powerpc/kvm/book3s_hv_devm.c +3
-2pfn =page_to_pfn (dpage); - ret =uv_page_out (pvt->lpid, pfn << page_shift, pvt->gpa,0 , - page_shift); + +/* +* This same function is used in two cases: Nit: s/same//
Extra emphasis :)
Quoted 5 lines
arch/powerpc/kvm/book3s_hv_devm.c kvmppc_devm_fault_migrate_alloc_and_copy(struct migrate_vma *mig, · line 284
Quoted 2 lines
arch/powerpc/kvm/book3s_hv_devm.c +1
-0+* - When HV touches a secure page, for which we do page-out Better to qualify page-out with "uv page-out"? its kind of counterintuitive to do a page-out on a fault!
Sure.
Regards,
Bharata.
PATCH v7 3/7 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
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
@@ -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
@@ -11,6 +11,8 @@ unsigned long kvmppc_h_svm_page_out(struct kvm *kvm,unsigned long gra, unsigned long flags, unsigned long page_shift); +unsigned long kvmppc_h_svm_init_start (struct kvm *kvm); +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
@@ -272,6 +272,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
@@ -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
@@ -45,4 +45,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
@@ -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
@@ -49,6 +49,40 @@ struct kvmppc_devm_page_pvt {bool skip_page_out; }; +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 ; +} +/* * Get a free device PFN from the pool *
Signature · 1 line
2.21.0
PATCH v7 4/7 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/ultravisor-api.h | 1 + arch/powerpc/include/asm/ultravisor.h | 5 +++++ arch/powerpc/kvm/book3s_hv.c | 17 +++++++++++++++++ 3 files changed, 23 insertions(+)
arch/powerpc/include/asm/ultravisor-api.h +1 -0
@@ -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
@@ -52,4 +52,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 +17 -0
@@ -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,22 @@ 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
Re: [PATCH v7 4/7] kvmppc: Handle memory plug/unplug to secure VM
arch/powerpc/kvm/book3s_hv.c static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm, · line 4524
Quoted 11 lines · Bharata B Rao
arch/powerpc/kvm/book3s_hv.c +10
-0+/* +* 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); }
In preparation for the KVM_MR_MOVE addition just using a switch statement here from the very beginning might make the code a little nicer to read.
PATCH v7 5/7 kvmppc: Radix changes for secure guest
In reply to Bharata B Rao- 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
@@ -867,6 +867,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 ) {
@@ -874,6 +876,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
@@ -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
@@ -57,4 +57,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
@@ -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
@@ -49,6 +49,11 @@ struct kvmppc_devm_page_pvt {bool skip_page_out; }; +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;
@@ -453,6 +458,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
Re: [PATCH v7 5/7] kvmppc: Radix changes for secure guest
Quoted 96 lines
- 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 viaUV_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.Signed-off-by Bharata B Rao <[email protected]>--- 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@@ -867,6 +867,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 ) {@@ -874,6 +876,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@@ -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@@ -57,4 +57,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 +5
-0@@ -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);
arch/powerpc/kvm/book3s_64_mmu_radix.c int kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot, · line 983
Quoted 7 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +4
-0@@ -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 ; + }
If it is a page we share with UV, won't we need to drop the HV mapping for the page?
Quoted 84 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +13
-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@@ -49,6 +49,11 @@ struct kvmppc_devm_page_pvt {bool skip_page_out; }; +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;@@ -453,6 +458,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
Re: [PATCH v7 5/7] kvmppc: Radix changes for secure guest
Quoted 99 lines · Sukadev Bhattiprolu
Quoted 99 lines
- 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 viaUV_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.Signed-off-by Bharata B Rao <[email protected]>--- 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@@ -867,6 +867,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 ) {@@ -874,6 +876,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@@ -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@@ -57,4 +57,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 +5
-0@@ -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;
Quoted 7 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c int kvm_unmap_radix(struct kvm *kvm, struct kvm_memory_slot *memslot, · line 983
Quoted 4 lines
arch/powerpc/kvm/book3s_64_mmu_radix.c +4
-0+if (kvmppc_is_guest_secure (kvm)) { +uv_page_inval (kvm->arch.lpid, gpa,PAGE_SIZE ); +return 0 ; + }If it is a page we share with UV, won't we need to drop the HV mapping for the page?
I believe we come here via MMU notifies only after dropping HV mapping.
Regards,
Bharata.
PATCH v7 6/7 kvmppc: Support reset of secure guest
In reply to Bharata B RaoAdd 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 becomes 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]
--- Documentation/virtual/kvm/api.txt | 19 ++++++ arch/powerpc/include/asm/kvm_book3s_devm.h | 6 ++ 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 | 60 +++++++++++++++++++ arch/powerpc/kvm/powerpc.c | 12 ++++ include/uapi/linux/kvm.h | 1 + 9 files changed, 176 insertions(+)
Documentation/virtual/kvm/api.txt +19 -0
@@ -4111,6 +4111,25 @@ 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, +unpins the VPA pages, reinitializes guest's partition scoped page +tables and releases all the device pages that are used to track the +secure pages by hypervisor. 5. The kvm_run structure ------------------------
arch/powerpc/include/asm/kvm_book3s_devm.h +6 -0
@@ -13,6 +13,7 @@ unsigned long kvmppc_h_svm_page_out(struct kvm *kvm, unsigned long page_shift);unsigned long kvmppc_h_svm_init_start (struct kvm *kvm);unsigned long kvmppc_h_svm_init_done (struct kvm *kvm); +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 +38,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
@@ -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
@@ -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
@@ -62,4 +62,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 +60 -0
75 lines not on this page · raw message
arch/powerpc/kvm/powerpc.c +12 -0
@@ -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
@@ -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
PATCH v7 7/7 KVM: PPC: Ultravisor: Add PPC_UV config option
In reply to Bharata B RaoFrom: Anshuman Khandual <[email protected]>
CONFIG_PPC_UV adds support for ultravisor.
--- arch/powerpc/Kconfig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
arch/powerpc/Kconfig +17 -0
@@ -448,6 +448,23 @@ 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 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). On 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
Quoted 18 lines · Bharata B Rao
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_DEVICEmemory equal to the amount of secure memory available in the platform for running secure guests is created. 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 viamigrate_vma_pages(). The reverse movement is driven viapagemap_ops.migrate_to_ram().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.
Quoted 7 lines
These patches are against hmm.git
(https://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git/log/?h=hmm)plus
Claudio Carvalho's base ultravisor enablement patchset v6
(https://lore.kernel.org/linuxppc-dev/[email protected]/T/#t)
How are you thinking these patches will go upstream? Are you going to send them via the hmm tree?
I assume you need Claudio's patchset as a prerequisite for your series to compile, which means the hmm maintainers would need to pull in a topic branch from Michael Ellerman's powerpc tree, or something like that.
Paul.
Quoted 27 lines · Paul Mackerras
Quoted 18 lines · Bharata B Rao
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_DEVICEmemory equal to the amount of secure memory available in the platform for running secure guests is created. 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 viamigrate_vma_pages(). The reverse movement is driven viapagemap_ops.migrate_to_ram().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.
Quoted 7 lines
These patches are against hmm.git
(https://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git/log/?h=hmm)plus
Claudio Carvalho's base ultravisor enablement patchset v6
(https://lore.kernel.org/linuxppc-dev/[email protected]/T/#t)
Quoted 7 lines
How are you thinking these patches will go upstream? Are you going to send them via the hmm tree?
I assume you need Claudio's patchset as a prerequisite for your series to compile, which means the hmm maintainers would need to pull in a topic branch from Michael Ellerman's powerpc tree, or something like that.
I was hoping that changes required from hmm.git would hit upstream soon, will reflect in mpe's powerpc tree at which time these patches can go via powerpc tree along with or after Claudio's patchset.
Though this depends on migrate_vma and memremap changes that happen to be in hmm.git, this is majorly a kvmppc change. Hence I thought it would be appropriate for this to go via your or mpe's tree together with required dependencies.
Regards,
Bharata.
Michael Ellerman
reviewerQuoted 25 lines · Paul Mackerras
Quoted 16 lines · Bharata B Rao
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_DEVICEmemory equal to the amount of secure memory available in the platform for running secure guests is created. 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 viamigrate_vma_pages(). The reverse movement is driven viapagemap_ops.migrate_to_ram().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.
Quoted 7 lines
These patches are against hmm.git
(https://git.kernel.org/pub/scm/linux/kernel/git/rdma/rdma.git/log/?h=hmm)plus
Claudio Carvalho's base ultravisor enablement patchset v6
(https://lore.kernel.org/linuxppc-dev/[email protected]/T/#t)
Quoted 7 lines
How are you thinking these patches will go upstream? Are you going to send them via the hmm tree?
I assume you need Claudio's patchset as a prerequisite for your series to compile, which means the hmm maintainers would need to pull in a topic branch from Michael Ellerman's powerpc tree, or something like that.
I think more workable would be for me to make a topic branch based on the hmm tree (or some commit from the hmm tree), which I then apply the patches on top of, and merge any required powerpc changes into that. I can then ask Linus to merge that branch late in the merge window once the hmm changes have gone in.
The bigger problem at the moment is the lack of reviews or acks on the bulk of the series.
cheers