KVM: PPC: Allocate guest TCEs on demand too
Subsystems (2)
PATCH v4 1/1 KVM: PPC: Allocate guest TCEs on demand too
We already allocate hardware TCE tables in multiple levels and skip intermediate levels when we can, now it is a turn of the KVM TCE tables. Thankfully these are allocated already in 2 levels.
This moves the table's last level allocation from the creating helper to kvmppc_tce_put() and kvm_spapr_tce_fault(). Since such allocation cannot be done in real mode, this creates a virtual mode version of kvmppc_tce_put() which handles allocations.
This adds kvmppc_rm_ioba_validate() to do an additional test if the consequent kvmppc_tce_put() needs a page which has not been allocated; if this is the case, we bail out to virtual mode handlers.
The allocations are protected by a new mutex as kvm->lock is not suitable for the task because the fault handler is called with the mmap_sem held but kvmhv_setup_mmu() locks kvm->lock and mmap_sem in the reverse order.
---
Changes:
v4:
* redid locking
* added a separate virtual mode's kvmppc_tce_put to avoid alloc_page() in otherwise realmode-only code
v3:
* fixed alignments in kvmppc_rm_ioba_validate
v2:
* added kvm mutex around alloc_page to prevent races; in both place we test the pointer, if NULL, then take a lock and check again so on a fast path we do not take a lock at all
---
For NVLink2 pass through guests with 128TiB DMA windows and very fragmented system RAM the difference is gigabytes of RAM.
Fun fact: after this patch the only remaining real+virtual function is kvmppc_find_table().
--- arch/powerpc/include/asm/kvm_host.h | 3 ++ arch/powerpc/include/asm/kvm_ppc.h | 2 - arch/powerpc/kvm/book3s_64_vio.c | 72 +++++++++++++++++++++++------ arch/powerpc/kvm/book3s_64_vio_hv.c | 66 +++++++++++++++++++------- 4 files changed, 110 insertions(+), 33 deletions(-)
arch/powerpc/include/asm/kvm_host.h +3 -0
@@ -201,6 +201,8 @@ struct kvmppc_spapr_tce_iommu_table {struct kref kref; }; +#define TCES_PER_PAGE (PAGE_SIZE / sizeof(u64)) +struct kvmppc_spapr_tce_table {struct list_head list;struct kvm *kvm;
@@ -210,6 +212,7 @@ struct kvmppc_spapr_tce_table {u64 offset;/* in pages */ u64 size;/* window size in pages */ struct list_head iommu_tables; +struct mutex alloc_lock;struct page *pages[0 ]; };
arch/powerpc/include/asm/kvm_ppc.h +0 -2
@@ -197,8 +197,6 @@ extern struct kvmppc_spapr_tce_table *kvmppc_find_table( (iommu_tce_check_ioba ((stt)->page_shift, (stt)->offset, \ (stt)->size, (ioba), (npages)) ? \ H_PARAMETER : H_SUCCESS) -extern void kvmppc_tce_put (struct kvmppc_spapr_tce_table *tt, -unsigned long idx,unsigned long tce);extern long kvmppc_h_put_tce (struct kvm_vcpu *vcpu,unsigned long liobn,unsigned long ioba,unsigned long tce); extern long kvmppc_h_put_tce_indirect(struct kvm_vcpu *vcpu,
arch/powerpc/kvm/book3s_64_vio.c +58 -14
114 lines not on this page · raw message
arch/powerpc/kvm/book3s_64_vio_hv.c +49 -17
138 lines not on this page · raw message
Signature · 1 line
2.17.1