Re: [PATCH kernel 4/6] powerpc/powernv: Add indirect levels to it_userspace
On Fri, Jun 08, 2018 at 03:46:31PM +1000, Alexey Kardashevskiy wrote: > We want to support sparse memory and therefore huge chunks of DMA windows > do not need to be mapped. If a DMA window big enough to require 2 or more > indirect levels, and a DMA window is used to map all RAM (which is > a default case for 64bit window), we can actually save some memory by > not allocation TCE for regions which we are not going to map anyway. > > The hardware tables alreary support indirect levels but we also keep > host-physical-to-userspace translation array which is allocated by > vmalloc() and is a flat array which might use quite some memory. > > This converts it_userspace from vmalloc'ed array to a multi level table. > > As the format becomes platform dependend, this replaces the direct access > to it_usespace with a iommu_table_ops::useraddrptr hook which returns > a pointer to the userspace copy of a TCE; future extension will return > NULL if the level was not allocated. > > This should not change non-KVM handling of TCE tables and it_userspace > will not be allocated for non-KVM tables. > > Signed-off-by: Alexey Kardashevskiy Reviewed-by: David Gibson > --- > arch/powerpc/include/asm/iommu.h | 6 +-- > arch/powerpc/platforms/powernv/pci.h | 3 +- > arch/powerpc/kvm/book3s_64_vio_hv.c | 8 > arch/powerpc/platforms/powernv/pci-ioda-tce.c | 65 > +-- > arch/powerpc/platforms/powernv/pci-ioda.c | 31 ++--- > drivers/vfio/vfio_iommu_spapr_tce.c | 46 --- > 6 files changed, 81 insertions(+), 78 deletions(-) > > diff --git a/arch/powerpc/include/asm/iommu.h > b/arch/powerpc/include/asm/iommu.h > index 803ac70..4bdcf22 100644 > --- a/arch/powerpc/include/asm/iommu.h > +++ b/arch/powerpc/include/asm/iommu.h > @@ -69,6 +69,8 @@ struct iommu_table_ops { > long index, > unsigned long *hpa, > enum dma_data_direction *direction); > + > + __be64 *(*useraddrptr)(struct iommu_table *tbl, long index); > #endif > void (*clear)(struct iommu_table *tbl, > long index, long npages); > @@ -123,9 +125,7 @@ struct iommu_table { > }; > > #define IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry) \ > - ((tbl)->it_userspace ? \ > - &((tbl)->it_userspace[(entry) - (tbl)->it_offset]) : \ > - NULL) > + ((tbl)->it_ops->useraddrptr((tbl), (entry))) > > /* Pure 2^n version of get_order */ > static inline __attribute_const__ > diff --git a/arch/powerpc/platforms/powernv/pci.h > b/arch/powerpc/platforms/powernv/pci.h > index f507baf..5e02408 100644 > --- a/arch/powerpc/platforms/powernv/pci.h > +++ b/arch/powerpc/platforms/powernv/pci.h > @@ -268,11 +268,12 @@ extern int pnv_tce_build(struct iommu_table *tbl, long > index, long npages, > extern void pnv_tce_free(struct iommu_table *tbl, long index, long npages); > extern int pnv_tce_xchg(struct iommu_table *tbl, long index, > unsigned long *hpa, enum dma_data_direction *direction); > +extern __be64 *pnv_tce_useraddrptr(struct iommu_table *tbl, long index); > extern unsigned long pnv_tce_get(struct iommu_table *tbl, long index); > > extern long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset, > __u32 page_shift, __u64 window_size, __u32 levels, > - struct iommu_table *tbl); > + bool alloc_userspace_copy, struct iommu_table *tbl); > extern void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl); > > extern long pnv_pci_link_table_and_group(int node, int num, > diff --git a/arch/powerpc/kvm/book3s_64_vio_hv.c > b/arch/powerpc/kvm/book3s_64_vio_hv.c > index 18109f3..db0490c 100644 > --- a/arch/powerpc/kvm/book3s_64_vio_hv.c > +++ b/arch/powerpc/kvm/book3s_64_vio_hv.c > @@ -206,10 +206,6 @@ static long kvmppc_rm_tce_iommu_mapped_dec(struct kvm > *kvm, > /* it_userspace allocation might be delayed */ > return H_TOO_HARD; > > - pua = (void *) vmalloc_to_phys(pua); > - if (WARN_ON_ONCE_RM(!pua)) > - return H_HARDWARE; > - > mem = mm_iommu_lookup_rm(kvm->mm, be64_to_cpu(*pua), pgsize); > if (!mem) > return H_TOO_HARD; > @@ -282,10 +278,6 @@ static long kvmppc_rm_tce_iommu_do_map(struct kvm *kvm, > struct iommu_table *tbl, > if (WARN_ON_ONCE_RM(mm_iommu_ua_to_hpa_rm(mem, ua, &hpa))) > return H_HARDWARE; > > - pua = (void *) vmalloc_to_phys(pua); > - if (WARN_ON_ONCE_RM(!pua)) > - return H_HARDWARE; > - > if (WARN_ON_ONCE_RM(mm_iommu_mapped_inc(mem))) > return H_CLOSED; > > diff --git a/arch/powerpc/platforms/powernv/pci-ioda-tce.c > b/arch/powerpc/platforms/powernv/pci-ioda-tce.c > index 700ceb1..f14b282 100644 > --- a/arch/powerpc/platforms/powernv/pci-ioda-tce.c > +++ b/arch/powerpc/platforms/powernv/pci-ioda-tce.c
[PATCH kernel 4/6] powerpc/powernv: Add indirect levels to it_userspace
We want to support sparse memory and therefore huge chunks of DMA windows do not need to be mapped. If a DMA window big enough to require 2 or more indirect levels, and a DMA window is used to map all RAM (which is a default case for 64bit window), we can actually save some memory by not allocation TCE for regions which we are not going to map anyway. The hardware tables alreary support indirect levels but we also keep host-physical-to-userspace translation array which is allocated by vmalloc() and is a flat array which might use quite some memory. This converts it_userspace from vmalloc'ed array to a multi level table. As the format becomes platform dependend, this replaces the direct access to it_usespace with a iommu_table_ops::useraddrptr hook which returns a pointer to the userspace copy of a TCE; future extension will return NULL if the level was not allocated. This should not change non-KVM handling of TCE tables and it_userspace will not be allocated for non-KVM tables. Signed-off-by: Alexey Kardashevskiy --- arch/powerpc/include/asm/iommu.h | 6 +-- arch/powerpc/platforms/powernv/pci.h | 3 +- arch/powerpc/kvm/book3s_64_vio_hv.c | 8 arch/powerpc/platforms/powernv/pci-ioda-tce.c | 65 +-- arch/powerpc/platforms/powernv/pci-ioda.c | 31 ++--- drivers/vfio/vfio_iommu_spapr_tce.c | 46 --- 6 files changed, 81 insertions(+), 78 deletions(-) diff --git a/arch/powerpc/include/asm/iommu.h b/arch/powerpc/include/asm/iommu.h index 803ac70..4bdcf22 100644 --- a/arch/powerpc/include/asm/iommu.h +++ b/arch/powerpc/include/asm/iommu.h @@ -69,6 +69,8 @@ struct iommu_table_ops { long index, unsigned long *hpa, enum dma_data_direction *direction); + + __be64 *(*useraddrptr)(struct iommu_table *tbl, long index); #endif void (*clear)(struct iommu_table *tbl, long index, long npages); @@ -123,9 +125,7 @@ struct iommu_table { }; #define IOMMU_TABLE_USERSPACE_ENTRY(tbl, entry) \ - ((tbl)->it_userspace ? \ - &((tbl)->it_userspace[(entry) - (tbl)->it_offset]) : \ - NULL) + ((tbl)->it_ops->useraddrptr((tbl), (entry))) /* Pure 2^n version of get_order */ static inline __attribute_const__ diff --git a/arch/powerpc/platforms/powernv/pci.h b/arch/powerpc/platforms/powernv/pci.h index f507baf..5e02408 100644 --- a/arch/powerpc/platforms/powernv/pci.h +++ b/arch/powerpc/platforms/powernv/pci.h @@ -268,11 +268,12 @@ extern int pnv_tce_build(struct iommu_table *tbl, long index, long npages, extern void pnv_tce_free(struct iommu_table *tbl, long index, long npages); extern int pnv_tce_xchg(struct iommu_table *tbl, long index, unsigned long *hpa, enum dma_data_direction *direction); +extern __be64 *pnv_tce_useraddrptr(struct iommu_table *tbl, long index); extern unsigned long pnv_tce_get(struct iommu_table *tbl, long index); extern long pnv_pci_ioda2_table_alloc_pages(int nid, __u64 bus_offset, __u32 page_shift, __u64 window_size, __u32 levels, - struct iommu_table *tbl); + bool alloc_userspace_copy, struct iommu_table *tbl); extern void pnv_pci_ioda2_table_free_pages(struct iommu_table *tbl); extern long pnv_pci_link_table_and_group(int node, int num, diff --git a/arch/powerpc/kvm/book3s_64_vio_hv.c b/arch/powerpc/kvm/book3s_64_vio_hv.c index 18109f3..db0490c 100644 --- a/arch/powerpc/kvm/book3s_64_vio_hv.c +++ b/arch/powerpc/kvm/book3s_64_vio_hv.c @@ -206,10 +206,6 @@ static long kvmppc_rm_tce_iommu_mapped_dec(struct kvm *kvm, /* it_userspace allocation might be delayed */ return H_TOO_HARD; - pua = (void *) vmalloc_to_phys(pua); - if (WARN_ON_ONCE_RM(!pua)) - return H_HARDWARE; - mem = mm_iommu_lookup_rm(kvm->mm, be64_to_cpu(*pua), pgsize); if (!mem) return H_TOO_HARD; @@ -282,10 +278,6 @@ static long kvmppc_rm_tce_iommu_do_map(struct kvm *kvm, struct iommu_table *tbl, if (WARN_ON_ONCE_RM(mm_iommu_ua_to_hpa_rm(mem, ua, &hpa))) return H_HARDWARE; - pua = (void *) vmalloc_to_phys(pua); - if (WARN_ON_ONCE_RM(!pua)) - return H_HARDWARE; - if (WARN_ON_ONCE_RM(mm_iommu_mapped_inc(mem))) return H_CLOSED; diff --git a/arch/powerpc/platforms/powernv/pci-ioda-tce.c b/arch/powerpc/platforms/powernv/pci-ioda-tce.c index 700ceb1..f14b282 100644 --- a/arch/powerpc/platforms/powernv/pci-ioda-tce.c +++ b/arch/powerpc/platforms/powernv/pci-ioda-tce.c @@ -31,9 +31,9 @@ void pnv_pci_setup_iommu_table(struct iommu_table *tbl, tbl->it_type = TCE_PCI; } -static __be64 *pnv_tce(struct iommu_table *tbl, long idx) +static __be64 *pnv_tce(struct iommu_table *tbl, bool user, long idx) { - __be64 *tmp