iommu/dma: s390 DMA API conversion and optimized IOTLB flushing
Subsystems (20)
Niklas Schnelle
maintainerv9 cover iommu/dma: s390 DMA API conversion and optimized IOTLB flushing
Hi All,
This patch series converts s390's PCI support from its platform specific DMA API implementation in arch/s390/pci/pci_dma.c to the common DMA IOMMU layer. The conversion itself is done in patches 3-4 with patch 2 providing the final necessary IOMMU driver improvement to handle s390's special IOTLB flush out-of-resource indication in virtualized environments. Patches 1-2 may be applied independently. The conversion itself only touches the s390 IOMMU driver and s390 arch code moving over remaining functions from the s390 DMA API implementation. No changes to common code are necessary.
After patch 4 the basic conversion is done and on our partitioning machine hypervisor LPAR performance matches or exceeds the existing code. When running under z/VM or KVM however, performance plummets to about half of the existing code due to a much higher rate of IOTLB flushes for unmapped pages. Due to the hypervisors use of IOTLB flushes to synchronize their shadow tables these are very expensive and minimizing them is key for regaining the performance loss.
To this end patches 5-6 add a new, single queue, IOTLB flushing scheme as an alternative to the existing per-CPU flush queues. Introducing an alternative scheme was also suggested by Robin Murphy[1]. In the previous RFC of this conversion Robin suggested reusing more of the existing queuing logic which I incorporated since v2. The single queue mode is introduced in patch 5 together with a new dma_iommu_options struct and tune_dma_iommu callback in IOMMU ops which allows IOMMU drivers to switch to a single flush queue.
Then patch 6 enables variable queue sizes using power of 2 queue sizes and shift/mask to keep performance as close to the existing code as possible. The variable queue size and a variable timeout are added to the dma_iommu_options struct and utilized by s390 in the z/VM and KVM guest cases. As it is implemented in common code the single queue IOTLB flushing scheme can of course be used by other platforms with expensive IOTLB flushes. Particularly virtio-iommu may be a candidate.
In a previous version I verified that the new scheme does work on my x86_64 Ryzen workstation by locally modifying iommu_subsys_init() to default to the single queue mode and verifying its use via "/sys/.../iommu_group/type". I did not find problems with an AMD GPU, Intel NIC (with SR-IOV and KVM pass-through), NVMes or any on board peripherals.
This code is also available in the b4/dma_iommu topic branch of my git.kernel.org repository[3] with tags the version sent.
NOTE: Due to the large drop in performance I think we should not merge the DMA API conversion (patch 4) until we have a more suited IOVA flushing scheme with similar improvements as the proposed changes.
Best regards,
Niklas
[0] https://lore.kernel.org/linux-iommu/[email protected]/
[1] https://lore.kernel.org/linux-iommu/[email protected]/
[2] https://lore.kernel.org/linux-iommu/[email protected]/
[3] https://git.kernel.org/pub/scm/linux/kernel/git/niks/linux.git/
---
Changes in v9:
- Rebased on v6.4-rc2
- Re-ordered iommu_group_store_type() to allow passing the device to iommu_dma_init_fq()
- Link to v8: https://lore.kernel.org/r/[email protected]
---
Niklas Schnelle (6):
s390/ism: Set DMA coherent mask
iommu: Allow .iotlb_sync_map to fail and handle s390's -ENOMEM return s390/pci: prepare is_passed_through() for dma-iommu s390/pci: Use dma-iommu layer
iommu/dma: Allow a single FQ in addition to per-CPU FQs iommu/dma: Make flush queue sizes and timeout driver configurable
Documentation/admin-guide/kernel-parameters.txt | 9 +- arch/s390/include/asm/pci.h | 7 - arch/s390/include/asm/pci_clp.h | 3 + arch/s390/include/asm/pci_dma.h | 119 +--- arch/s390/pci/Makefile | 2 +- arch/s390/pci/pci.c | 22 +- arch/s390/pci/pci_bus.c | 5 - arch/s390/pci/pci_debug.c | 12 +- arch/s390/pci/pci_dma.c | 735 ------------------------ arch/s390/pci/pci_event.c | 17 +- arch/s390/pci/pci_sysfs.c | 19 +- drivers/iommu/Kconfig | 4 +- drivers/iommu/amd/iommu.c | 5 +- drivers/iommu/apple-dart.c | 5 +- drivers/iommu/dma-iommu.c | 191 ++++-- drivers/iommu/dma-iommu.h | 4 +- drivers/iommu/intel/iommu.c | 5 +- drivers/iommu/iommu.c | 38 +- drivers/iommu/msm_iommu.c | 5 +- drivers/iommu/mtk_iommu.c | 5 +- drivers/iommu/s390-iommu.c | 435 +++++++++++++- drivers/iommu/sprd-iommu.c | 5 +- drivers/iommu/sun50i-iommu.c | 4 +- drivers/iommu/tegra-gart.c | 5 +- drivers/s390/net/ism_drv.c | 2 +- include/linux/iommu.h | 29 +- 26 files changed, 682 insertions(+), 1010 deletions(-)
---
base-commit: f1fcbaa18b28dec10281551dfe6ed3a3ed80e3d6
change-id: 20230310-dma_iommu-5e048c538647
Best regards,
Signature · 8 lines
Niklas Schnelle Linux on Z Development IBM Deutschland Research & Development GmbH Vorsitzender des Aufsichtsrats: Gregor Pillen Geschäftsführung: David Faller Sitz der Gesellschaft: Böblingen / Registergericht: Amtsgericht Stuttgart, HRB 243294 IBM Data Privacy Statement - https://www.ibm.com/privacy
Niklas Schnelle
maintainerPATCH v9 1/6 s390/ism: Set DMA coherent mask
A future change will convert the DMA API implementation from the architecture specific arch/s390/pci/pci_dma.c to using the common code drivers/iommu/dma-iommu.c which the utilizes the same IOMMU hardware through the s390-iommu driver. Unlike the s390 specific DMA API this requires devices to correctly call set the coherent mask to be allowed to use IOVAs >2^32 in dma_alloc_coherent(). This was however not done for ISM devices. ISM requires such addresses since currently the DMA aperture for PCI devices starts at 2^32 and all calls to dma_alloc_coherent() would thus fail.
--- drivers/s390/net/ism_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/s390/net/ism_drv.c +1 -1
@@ -660,7 +660,7 @@ static int ism_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (ret) goto err_disable; - ret =dma_set_mask (&pdev->dev,DMA_BIT_MASK (64 )); + ret =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (64 ));if (ret) goto err_resource;
Signature · 1 line
2.39.2
Re: [PATCH v9 1/6] s390/ism: Set DMA coherent mask
Quoted 5 lines · Niklas Schnelle
A future change will convert the DMA API implementation from the architecture specific
arch/s390/pci/pci_dma.cto using the common codedrivers/iommu/dma-iommu.cwhich the utilizes the same IOMMU hardware through the s390-iommu driver. Unlike the s390 specific DMA API this requires devices to correctly call set the coherent mask to be allowed
s/call//
Quoted 18 lines
to use IOVAs >2^32 in
dma_alloc_coherent(). This was however not done for ISM devices. ISM requires such addresses since currently the DMA aperture for PCI devices starts at 2^32 and all calls todma_alloc_coherent()would thus fail.Reviewed-by Alexandra Winter <[email protected]>Reviewed-by Matthew Rosato <[email protected]>Signed-off-by Niklas Schnelle <[email protected]>--- drivers/s390/net/ism_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/s390/net/ism_drv.c +0
-0
drivers/s390/net/ism_drv.c static int ism_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 666
Quoted 8 lines
drivers/s390/net/ism_drv.c +1
-1@@ -660,7 +660,7 @@ static int ism_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (ret) goto err_disable; - ret =dma_set_mask (&pdev->dev,DMA_BIT_MASK (64 )); + ret =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (64 ));if (ret) goto err_resource;
Re: [PATCH v9 1/6] s390/ism: Set DMA coherent mask
In reply to Niklas SchnelleQuoted 22 lines · On Mon, 15 May 2023 11:15:51 +0200 Niklas Schnelle <[email protected]>…
A future change will convert the DMA API implementation from the architecture specific
arch/s390/pci/pci_dma.cto using the common codedrivers/iommu/dma-iommu.cwhich the utilizes the same IOMMU hardware through the s390-iommu driver. Unlike the s390 specific DMA API this requires devices to correctly call set the coherent mask to be allowed to use IOVAs >2^32 indma_alloc_coherent(). This was however not done for ISM devices. ISM requires such addresses since currently the DMA aperture for PCI devices starts at 2^32 and all calls todma_alloc_coherent()would thus fail.Reviewed-by Alexandra Winter <[email protected]>Reviewed-by Matthew Rosato <[email protected]>Signed-off-by Niklas Schnelle <[email protected]>--- drivers/s390/net/ism_drv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/s390/net/ism_drv.c +0
-0
drivers/s390/net/ism_drv.c static int ism_probe(struct pci_dev *pdev, const struct pci_device_id *id) · line 663
Quoted 5 lines
drivers/s390/net/ism_drv.c +1
-1@@ -660,7 +660,7 @@ static int ism_probe(struct pci_dev *pdev, const struct pci_device_id *id)if (ret) goto err_disable; - ret =dma_set_mask (&pdev->dev,DMA_BIT_MASK (64 )); + ret =dma_set_mask_and_coherent (&pdev->dev,DMA_BIT_MASK (64 ));
Ah. I love this change. I have already wondered a few times if the coherent DMA mask for this device may actually be different from dma_mask. Now I know. ;-)
Thanks!
Petr T
Niklas Schnelle
maintainerPATCH v9 2/6 iommu: Allow .iotlb_sync_map to fail and handle s390's -ENOMEM return
In reply to Niklas SchnelleOn s390 when using a paging hypervisor, .iotlb_sync_map is used to sync mappings by letting the hypervisor inspect the synced IOVA range and updating a shadow table. This however means that .iotlb_sync_map can fail as the hypervisor may run out of resources while doing the sync. This can be due to the hypervisor being unable to pin guest pages, due to a limit on mapped addresses such as vfio_iommu_type1.dma_entry_limit or lack of other resources. Either way such a failure to sync a mapping should result in a DMA_MAPPING_ERROR.
Now especially when running with batched IOTLB flushes for unmap it may be that some IOVAs have already been invalidated but not yet synced via .iotlb_sync_map. Thus if the hypervisor indicates running out of resources, first do a global flush allowing the hypervisor to free resources associated with these mappings as well a retry creating the new mappings and only if that also fails report this error to callers.
--- drivers/iommu/amd/iommu.c | 5 +++-- drivers/iommu/apple-dart.c | 5 +++-- drivers/iommu/intel/iommu.c | 5 +++-- drivers/iommu/iommu.c | 20 ++++++++++++++++---- drivers/iommu/msm_iommu.c | 5 +++-- drivers/iommu/mtk_iommu.c | 5 +++-- drivers/iommu/s390-iommu.c | 29 ++++++++++++++++++++++++----- drivers/iommu/sprd-iommu.c | 5 +++-- drivers/iommu/sun50i-iommu.c | 4 +++- drivers/iommu/tegra-gart.c | 5 +++-- include/linux/iommu.h | 4 ++-- 11 files changed, 66 insertions(+), 26 deletions(-)
drivers/iommu/amd/iommu.c +3 -2
@@ -2196,14 +2196,15 @@ static int amd_iommu_attach_device(struct iommu_domain *dom,return ret; } -static void amd_iommu_iotlb_sync_map (struct iommu_domain *dom, -unsigned long iova,size_t size) +static int amd_iommu_iotlb_sync_map (struct iommu_domain *dom, +unsigned long iova,size_t size) {struct protection_domain *domain =to_pdomain (dom);struct io_pgtable_ops *ops = &domain->iop.iop.ops;if (ops->map_pages)domain_flush_np_cache (domain, iova, size); +return 0 ; } static int amd_iommu_map_pages(struct iommu_domain *dom, unsigned long iova,
drivers/iommu/apple-dart.c +3 -2
@@ -506,10 +506,11 @@ static void apple_dart_iotlb_sync(struct iommu_domain *domain,apple_dart_domain_flush_tlb (to_dart_domain (domain)); } -static void apple_dart_iotlb_sync_map (struct iommu_domain *domain, -unsigned long iova,size_t size) +static int apple_dart_iotlb_sync_map (struct iommu_domain *domain, +unsigned long iova,size_t size) {apple_dart_domain_flush_tlb (to_dart_domain (domain)); +return 0 ; } static phys_addr_t apple_dart_iova_to_phys(struct iommu_domain *domain,
drivers/iommu/intel/iommu.c +3 -2
@@ -4704,8 +4704,8 @@ static bool risky_device(struct pci_dev *pdev)return false; } -static void intel_iommu_iotlb_sync_map (struct iommu_domain *domain, -unsigned long iova,size_t size) +static int intel_iommu_iotlb_sync_map (struct iommu_domain *domain, +unsigned long iova,size_t size) {struct dmar_domain *dmar_domain =to_dmar_domain (domain);unsigned long pages =aligned_nrpages (iova, size);
@@ -4715,6 +4715,7 @@ static void intel_iommu_iotlb_sync_map(struct iommu_domain *domain,xa_for_each (&dmar_domain->iommu_array, i, info)__mapping_notify_one (info->iommu, dmar_domain, pfn, pages); +return 0 ; }static void intel_iommu_remove_dev_pasid (struct device *dev,ioasid_t pasid)
drivers/iommu/iommu.c +16 -4
@@ -2440,8 +2440,17 @@ int iommu_map(struct iommu_domain *domain, unsigned long iova,return -EINVAL ; ret =__iommu_map (domain, iova, paddr, size, prot, gfp); -if (ret ==0 && ops->iotlb_sync_map) - ops->iotlb_sync_map (domain, iova, size); +if (ret ==0 && ops->iotlb_sync_map) { + ret = ops->iotlb_sync_map (domain, iova, size); +if (ret) + goto out_err; + } + +return ret; + +out_err: +/* undo mappings already done */ +iommu_unmap (domain, iova, size);return ret; }
@@ -2582,8 +2591,11 @@ ssize_t iommu_map_sg(struct iommu_domain *domain, unsigned long iova, sg =sg_next (sg); } -if (ops->iotlb_sync_map) - ops->iotlb_sync_map (domain, iova, mapped); +if (ops->iotlb_sync_map) { + ret = ops->iotlb_sync_map (domain, iova, mapped); +if (ret) + goto out_err; + }return mapped; out_err:
drivers/iommu/msm_iommu.c +3 -2
@@ -486,12 +486,13 @@ static int msm_iommu_map(struct iommu_domain *domain, unsigned long iova,return ret; } -static void msm_iommu_sync_map (struct iommu_domain *domain,unsigned long iova, -size_t size) +static int msm_iommu_sync_map (struct iommu_domain *domain,unsigned long iova, +size_t size) {struct msm_priv *priv =to_msm_priv (domain);__flush_iotlb_range (iova, size,SZ_4K , false, priv); +return 0 ; } static size_t msm_iommu_unmap(struct iommu_domain *domain, unsigned long iova,
drivers/iommu/mtk_iommu.c +3 -2
@@ -793,12 +793,13 @@ static void mtk_iommu_iotlb_sync(struct iommu_domain *domain,mtk_iommu_tlb_flush_range_sync (gather->start, length, dom->bank); } -static void mtk_iommu_sync_map (struct iommu_domain *domain,unsigned long iova, -size_t size) +static int mtk_iommu_sync_map (struct iommu_domain *domain,unsigned long iova, +size_t size) {struct mtk_iommu_domain *dom =to_mtk_domain (domain);mtk_iommu_tlb_flush_range_sync (iova, size, dom->bank); +return 0 ; } static phys_addr_t mtk_iommu_iova_to_phys(struct iommu_domain *domain,
drivers/iommu/s390-iommu.c +24 -5
@@ -205,6 +205,14 @@ static void s390_iommu_release_device(struct device *dev)__s390_iommu_detach_device (zdev); } + +static int zpci_refresh_all (struct zpci_dev *zdev) +{ +return zpci_refresh_trans ((u64 )zdev->fh <<32 , zdev->start_dma, + zdev->end_dma - zdev->start_dma +1 ); + +} +static void s390_iommu_flush_iotlb_all (struct iommu_domain *domain) {struct s390_domain *s390_domain =to_s390_domain (domain);
@@ -212,8 +220,7 @@ static void s390_iommu_flush_iotlb_all(struct iommu_domain *domain)rcu_read_lock ();list_for_each_entry_rcu (zdev, &s390_domain->devices, iommu_list) { -zpci_refresh_trans ((u64 )zdev->fh <<32 , zdev->start_dma, - zdev->end_dma - zdev->start_dma +1 ); +zpci_refresh_all (zdev); }rcu_read_unlock (); }
@@ -237,20 +244,32 @@ static void s390_iommu_iotlb_sync(struct iommu_domain *domain,rcu_read_unlock (); } -static void s390_iommu_iotlb_sync_map (struct iommu_domain *domain, +static int s390_iommu_iotlb_sync_map (struct iommu_domain *domain,unsigned long iova,size_t size) {struct s390_domain *s390_domain =to_s390_domain (domain);struct zpci_dev *zdev; +int ret =0 ;rcu_read_lock ();list_for_each_entry_rcu (zdev, &s390_domain->devices, iommu_list) {if (!zdev->tlb_refresh)continue ; -zpci_refresh_trans ((u64 )zdev->fh <<32 , - iova, size); + ret =zpci_refresh_trans ((u64 )zdev->fh <<32 , + iova, size); +/* +* let the hypervisor discover invalidated entries +* allowing it to free IOVAs and unpin pages +*/ +if (ret == -ENOMEM ) { + ret =zpci_refresh_all (zdev); +if (ret) +break ; + } }rcu_read_unlock (); + +return ret; } static int s390_iommu_validate_trans(struct s390_domain *s390_domain,
drivers/iommu/sprd-iommu.c +3 -2
@@ -343,8 +343,8 @@ static size_t sprd_iommu_unmap(struct iommu_domain *domain, unsigned long iova,return size; } -static void sprd_iommu_sync_map (struct iommu_domain *domain, -unsigned long iova,size_t size) +static int sprd_iommu_sync_map (struct iommu_domain *domain, +unsigned long iova,size_t size) {struct sprd_iommu_domain *dom =to_sprd_domain (domain);unsigned int reg;
@@ -356,6 +356,7 @@ static void sprd_iommu_sync_map(struct iommu_domain *domain,/* clear IOMMU TLB buffer after page table updated */ sprd_iommu_write (dom->sdev, reg,0xffffffff ); +return 0 ; } static void sprd_iommu_sync(struct iommu_domain *domain,
drivers/iommu/sun50i-iommu.c +3 -1
@@ -402,7 +402,7 @@ static void sun50i_iommu_flush_iotlb_all(struct iommu_domain *domain)spin_unlock_irqrestore (&iommu->iommu_lock, flags); } -static void sun50i_iommu_iotlb_sync_map (struct iommu_domain *domain, +static int sun50i_iommu_iotlb_sync_map (struct iommu_domain *domain,unsigned long iova,size_t size) {struct sun50i_iommu_domain *sun50i_domain =to_sun50i_domain (domain);
@@ -412,6 +412,8 @@ static void sun50i_iommu_iotlb_sync_map(struct iommu_domain *domain,spin_lock_irqsave (&iommu->iommu_lock, flags);sun50i_iommu_zap_range (iommu, iova, size);spin_unlock_irqrestore (&iommu->iommu_lock, flags); + +return 0 ; } static void sun50i_iommu_iotlb_sync(struct iommu_domain *domain,
drivers/iommu/tegra-gart.c +3 -2
@@ -252,10 +252,11 @@ static int gart_iommu_of_xlate(struct device *dev,return 0 ; } -static void gart_iommu_sync_map (struct iommu_domain *domain,unsigned long iova, -size_t size) +static int gart_iommu_sync_map (struct iommu_domain *domain,unsigned long iova, +size_t size) {FLUSH_GART_REGS (gart_handle); +return 0 ; } static void gart_iommu_sync(struct iommu_domain *domain,
include/linux/iommu.h +2 -2
@@ -333,8 +333,8 @@ struct iommu_domain_ops { struct iommu_iotlb_gather *iotlb_gather);void (*flush_iotlb_all)(struct iommu_domain *domain); -void (*iotlb_sync_map)(struct iommu_domain *domain,unsigned long iova, -size_t size); +int (*iotlb_sync_map)(struct iommu_domain *domain,unsigned long iova, +size_t size);void (*iotlb_sync)(struct iommu_domain *domain,struct iommu_iotlb_gather *iotlb_gather);
Signature · 1 line
2.39.2
Niklas Schnelle
maintainerPATCH v9 3/6 s390/pci: prepare is_passed_through() for dma-iommu
In reply to Niklas SchnelleWith the IOMMU always controlled through the IOMMU driver testing for zdev->s390_domain is not a valid indication of the device being passed-through. Instead test if zdev->kzdev is set.
--- arch/s390/pci/pci_event.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
arch/s390/pci/pci_event.c +11 -4
@@ -59,9 +59,16 @@ static inline bool ers_result_indicates_abort(pci_ers_result_t ers_res) } } -static bool is_passed_through (struct zpci_dev *zdev) +static bool is_passed_through (struct pci_dev *pdev) { -return zdev->s390_domain; +struct zpci_dev *zdev =to_zpci (pdev); +bool ret; + +mutex_lock (&zdev->kzdev_lock); + ret = !!zdev->kzdev; +mutex_unlock (&zdev->kzdev_lock); + +return ret; }static bool is_driver_supported (struct pci_driver *driver)
@@ -176,7 +183,7 @@ static pci_ers_result_t zpci_event_attempt_error_recovery(struct pci_dev *pdev) } pdev->error_state = pci_channel_io_frozen; -if (is_passed_through (to_zpci (pdev))) { +if (is_passed_through (pdev)) {pr_info ("%s: Cannot be recovered in the host because it is a pass-through device\n" ,pci_name (pdev)); goto out_unlock;
@@ -239,7 +246,7 @@ static void zpci_event_io_failure(struct pci_dev *pdev, pci_channel_state_t es)* we will inject the error event and let the guest recover the device * itself. */ -if (is_passed_through (to_zpci (pdev))) +if (is_passed_through (pdev)) goto out; driver =to_pci_driver (pdev->dev.driver);if (driver && driver->err_handler && driver->err_handler->error_detected)
Signature · 1 line
2.39.2
Niklas Schnelle
maintainerPATCH v9 4/6 s390/pci: Use dma-iommu layer
In reply to Niklas SchnelleWhile s390 already has a standard IOMMU driver and previous changes have added I/O TLB flushing operations this driver is currently only used for user-space PCI access such as vfio-pci. For the DMA API s390 instead utilizes its own implementation in arch/s390/pci/pci_dma.c which drives the same hardware and shares some code but requires a complex and fragile hand over between DMA API and IOMMU API use of a device and despite code sharing still leads to significant duplication and maintenance effort. Let's utilize the common code DMAP API implementation from drivers/iommu/dma-iommu.c instead allowing us to get rid of arch/s390/pci/pci_dma.c.
--- Documentation/admin-guide/kernel-parameters.txt | 9 +- arch/s390/include/asm/pci.h | 7 - arch/s390/include/asm/pci_clp.h | 3 + arch/s390/include/asm/pci_dma.h | 119 +--- arch/s390/pci/Makefile | 2 +- arch/s390/pci/pci.c | 22 +- arch/s390/pci/pci_bus.c | 5 - arch/s390/pci/pci_debug.c | 12 +- arch/s390/pci/pci_dma.c | 735 ------------------------ arch/s390/pci/pci_event.c | 2 - arch/s390/pci/pci_sysfs.c | 19 +- drivers/iommu/Kconfig | 4 +- drivers/iommu/s390-iommu.c | 390 ++++++++++++- 13 files changed, 406 insertions(+), 923 deletions(-)
Documentation/admin-guide/kernel-parameters.txt +5 -4
@@ -2182,7 +2182,7 @@ forcing Dual Address Cycle for PCI cards supporting greater than 32-bit addressing. - iommu.strict= [ARM64, X86] Configure TLB invalidation behaviour + iommu.strict= [ARM64, X86, S390] Configure TLB invalidation behaviour Format: { "0" | "1" } 0 - Lazy mode. Request that DMA unmap operations use deferred
@@ -5477,9 +5477,10 @@ s390_iommu= [HW,S390] Set s390 IOTLB flushing mode strict - With strict flushing every unmap operation will result in - an IOTLB flush. Default is lazy flushing before reuse, - which is faster. + With strict flushing every unmap operation will result + in an IOTLB flush. Default is lazy flushing before + reuse, which is faster. Deprecated, equivalent to + iommu.strict=1. s390_iommu_aperture= [KNL,S390] Specifies the size of the per device DMA address space
arch/s390/include/asm/pci.h +0 -7
@@ -159,13 +159,6 @@ struct zpci_dev {unsigned long *dma_table;int tlb_refresh; -spinlock_t iommu_bitmap_lock; -unsigned long *iommu_bitmap; -unsigned long *lazy_bitmap; -unsigned long iommu_size; -unsigned long iommu_pages; -unsigned int next_bit; -struct iommu_device iommu_dev;/* IOMMU core handle */ char res_name[16 ];
arch/s390/include/asm/pci_clp.h +3 -0
@@ -50,6 +50,9 @@ struct clp_fh_list_entry {#define CLP_UTIL_STR_LEN 64#define CLP_PFIP_NR_SEGMENTS 4 +/* PCI function type numbers */ +#define PCI_FUNC_TYPE_ISM 0x5/* ISM device */ +extern bool zpci_unique_uid;struct clp_rsp_slpc_pci {
arch/s390/include/asm/pci_dma.h +9 -110
126 lines not on this page · raw message
arch/s390/pci/Makefile +1 -1
@@ -3,7 +3,7 @@ # Makefile for the s390 PCI subsystem. # -obj-$(CONFIG_PCI) += pci.o pci_irq.o pci_dma.o pci_clp.o pci_sysfs.o \ +obj-$(CONFIG_PCI) += pci.o pci_irq.o pci_clp.o pci_sysfs.o \ pci_event.o pci_debug.o pci_insn.o pci_mmio.o \ pci_bus.o pci_kvm_hook.o obj-$(CONFIG_PCI_IOV) += pci_iov.o
arch/s390/pci/pci.c +5 -17
@@ -124,7 +124,11 @@ int zpci_register_ioat(struct zpci_dev *zdev, u8 dmaas,WARN_ON_ONCE (iota &0x3fff ); fib.pba = base; - fib.pal = limit; +/* Work around off by one in ISM virt device */ +if (zdev->pft ==PCI_FUNC_TYPE_ISM && limit > base) + fib.pal = limit + (1 <<12 ); +else + fib.pal = limit; fib.iota = iota |ZPCI_IOTA_RTTO_FLAG ; fib.gd = zdev->gisa; cc =zpci_mod_fc (req, &fib, status);
@@ -619,7 +623,6 @@ int pcibios_device_add(struct pci_dev *pdev) pdev->no_vf_scan =1 ; pdev->dev.groups = zpci_attr_groups; - pdev->dev.dma_ops = &s390_pci_dma_ops;zpci_map_resources (pdev);for (i =0 ; i <PCI_STD_NUM_BARS ; i++) {
@@ -793,8 +796,6 @@ int zpci_hot_reset_device(struct zpci_dev *zdev)if (zdev->dma_table) rc =zpci_register_ioat (zdev,0 , zdev->start_dma, zdev->end_dma,virt_to_phys (zdev->dma_table), &status); -else - rc =zpci_dma_init_device (zdev);if (rc) {zpci_disable_device (zdev);return rc;
@@ -902,11 +903,6 @@ int zpci_deconfigure_device(struct zpci_dev *zdev)if (zdev->zbus->bus)zpci_bus_remove_device (zdev, false); -if (zdev->dma_table) { - rc =zpci_dma_exit_device (zdev); -if (rc) -return rc; - }if (zdev_enabled (zdev)) { rc =zpci_disable_device (zdev);if (rc)
@@ -955,8 +951,6 @@ void zpci_release_device(struct kref *kref)if (zdev->zbus->bus)zpci_bus_remove_device (zdev, false); -if (zdev->dma_table) -zpci_dma_exit_device (zdev);if (zdev_enabled (zdev))zpci_disable_device (zdev);
@@ -1146,10 +1140,6 @@ static int __init pci_base_init(void)if (rc) goto out_irq; - rc =zpci_dma_init (); -if (rc) - goto out_dma; - rc =clp_scan_pci_devices ();if (rc) goto out_find;
@@ -1159,8 +1149,6 @@ static int __init pci_base_init(void)return 0 ; out_find: -zpci_dma_exit (); -out_dma:zpci_irq_exit (); out_irq:zpci_mem_exit ();
arch/s390/pci/pci_bus.c +0 -5
@@ -47,11 +47,6 @@ static int zpci_bus_prepare_device(struct zpci_dev *zdev) rc =zpci_enable_device (zdev);if (rc)return rc; - rc =zpci_dma_init_device (zdev); -if (rc) { -zpci_disable_device (zdev); -return rc; - } }if (!zdev->has_resources) {
arch/s390/pci/pci_debug.c +9 -3
@@ -53,9 +53,11 @@ static char *pci_fmt3_names[] = { };static char *pci_sw_names[] = { -"Allocated pages" ,"Mapped pages" ,"Unmapped pages" , +"Global RPCITs" , +"Sync Map RPCITs" , +"Sync RPCITs" , }; static void pci_fmb_show(struct seq_file *m, char *name[], int length,
@@ -69,10 +71,14 @@ static void pci_fmb_show(struct seq_file *m, char *name[], int length,static void pci_sw_counter_show (struct seq_file *m) { -struct zpci_dev *zdev = m->private; -atomic64_t *counter = &zdev->allocated_pages; +struct zpci_iommu_ctrs *ctrs =zpci_get_iommu_ctrs (m->private); +atomic64_t *counter;int i; +if (!ctrs) +return ; + + counter = &ctrs->mapped_pages;for (i =0 ; i <ARRAY_SIZE (pci_sw_names); i++, counter++)seq_printf (m,"%26s:\t%llu\n" , pci_sw_names[i],atomic64_read (counter));
arch/s390/pci/pci_dma.c deleted +0 -735
735 lines not on this page · raw message
arch/s390/pci/pci_event.c +0 -2
@@ -313,8 +313,6 @@ static void zpci_event_hard_deconfigured(struct zpci_dev *zdev, u32 fh)/* Even though the device is already gone we still * need to free zPCI resources as part of the disable. */ -if (zdev->dma_table) -zpci_dma_exit_device (zdev);if (zdev_enabled (zdev))zpci_disable_device (zdev); zdev->state =ZPCI_FN_STATE_STANDBY ;
arch/s390/pci/pci_sysfs.c +8 -11
@@ -56,6 +56,7 @@ static ssize_t recover_store(struct device *dev, struct device_attribute *attr,struct pci_dev *pdev =to_pci_dev (dev);struct zpci_dev *zdev =to_zpci (pdev);int ret =0 ; +u8 status;/* Can't use device_remove_self() here as that would lead us to lock * the pci_rescan_remove_lock while holding the device' kernfs lock.
@@ -82,12 +83,6 @@ static ssize_t recover_store(struct device *dev, struct device_attribute *attr,pci_lock_rescan_remove ();if (pci_dev_is_added (pdev)) {pci_stop_and_remove_bus_device (pdev); -if (zdev->dma_table) { - ret =zpci_dma_exit_device (zdev); -if (ret) - goto out; - } -if (zdev_enabled (zdev)) { ret =zpci_disable_device (zdev);/*
@@ -105,14 +100,16 @@ static ssize_t recover_store(struct device *dev, struct device_attribute *attr, ret =zpci_enable_device (zdev);if (ret) goto out; - ret =zpci_dma_init_device (zdev); -if (ret) { -zpci_disable_device (zdev); - goto out; + +if (zdev->dma_table) { + ret =zpci_register_ioat (zdev,0 , zdev->start_dma, zdev->end_dma, +virt_to_phys (zdev->dma_table), &status); +if (ret) +zpci_disable_device (zdev); } -pci_rescan_bus (zdev->zbus->bus); } out: +pci_rescan_bus (zdev->zbus->bus);pci_unlock_rescan_remove ();if (kn)sysfs_unbreak_active_protection (kn);
drivers/iommu/Kconfig +2 -2
@@ -91,7 +91,7 @@ config IOMMU_DEBUGFS choice prompt "IOMMU default domain type" depends on IOMMU_API - default IOMMU_DEFAULT_DMA_LAZY if X86 || IA64 + default IOMMU_DEFAULT_DMA_LAZY if X86 || IA64 || S390 default IOMMU_DEFAULT_DMA_STRICT help Choose the type of IOMMU domain used to manage DMA API usage by
@@ -146,7 +146,7 @@ config OF_IOMMU # IOMMU-agnostic DMA-mapping layer config IOMMU_DMA - def_bool ARM64 || IA64 || X86 + def_bool ARM64 || IA64 || X86 || S390 select DMA_OPS select IOMMU_API select IOMMU_IOVA
drivers/iommu/s390-iommu.c +364 -26
505 lines not on this page · raw message
Signature · 1 line
2.39.2
Niklas Schnelle
maintainerPATCH v9 5/6 iommu/dma: Allow a single FQ in addition to per-CPU FQs
In reply to Niklas SchnelleIn some virtualized environments, including s390 paged memory guests, IOTLB flushes are used to update IOMMU shadow tables. Due to this, they are much more expensive than in typical bare metal environments or non-paged s390 guests. In addition they may parallelize more poorly in virtualized environments. This changes the trade off for flushing IOVAs such that minimizing the number of IOTLB flushes trumps any benefit of cheaper queuing operations or increased paralellism.
In this scenario per-CPU flush queues pose several problems. Firstly per-CPU memory is often quite limited prohibiting larger queues. Secondly collecting IOVAs per-CPU but flushing via a global timeout reduces the number of IOVAs flushed for each timeout especially on s390 where PCI interrupts may not be bound to a specific CPU.
Let's introduce a single flush queue mode that reuses the same queue logic but only allocates a single global queue. This mode can be selected as a flag bit in a new dma_iommu_options struct which can be modified from its defaults by IOMMU drivers implementing a new ops.tune_dma_iommu() callback. As a first user the s390 IOMMU driver selects the single queue mode if IOTLB flushes are needed on map which indicates shadow table use. With the unchanged small FQ size and timeouts this setting is worse than per-CPU queues but a follow up patch will make the FQ size and timeout variable. Together this allows the common IOVA flushing code to more closely resemble the global flush behavior used on s390's previous internal DMA API implementation.
--- drivers/iommu/dma-iommu.c | 163 ++++++++++++++++++++++++++++++++++----------- drivers/iommu/dma-iommu.h | 4 +- drivers/iommu/iommu.c | 18 +++-- drivers/iommu/s390-iommu.c | 10 +++ include/linux/iommu.h | 21 ++++++ 5 files changed, 169 insertions(+), 47 deletions(-)
drivers/iommu/dma-iommu.c +124 -39
@@ -49,8 +49,11 @@ struct iommu_dma_cookie {/* Full allocator for IOMMU_DMA_IOVA_COOKIE */ struct {struct iova_domain iovad; - -struct iova_fq __percpu *fq;/* Flush queue */ +/* Flush queue */ +union { +struct iova_fq *single_fq; +struct iova_fq __percpu *percpu_fq; + };/* Number of TLB flushes that have been started */ atomic64_t fq_flush_start_cnt;/* Number of TLB flushes that have been finished */
@@ -67,6 +70,8 @@ struct iommu_dma_cookie {/* Domain for flush queue callback; NULL if flush queue not in use */ struct iommu_domain *fq_domain; +/* Options for dma-iommu use */ +struct dma_iommu_options options;struct mutex mutex; };
@@ -152,25 +157,44 @@ static void fq_flush_iotlb(struct iommu_dma_cookie *cookie)atomic64_inc (&cookie->fq_flush_finish_cnt); } -static void fq_flush_timeout (struct timer_list *t) +static void fq_flush_percpu (struct iommu_dma_cookie *cookie) { -struct iommu_dma_cookie *cookie =from_timer (cookie, t, fq_timer);int cpu; -atomic_set (&cookie->fq_timer_on,0 ); -fq_flush_iotlb (cookie); -for_each_possible_cpu (cpu) {unsigned long flags;struct iova_fq *fq; - fq =per_cpu_ptr (cookie->fq, cpu); + fq =per_cpu_ptr (cookie->percpu_fq, cpu);spin_lock_irqsave (&fq->lock, flags);fq_ring_free (cookie, fq);spin_unlock_irqrestore (&fq->lock, flags); } } +static void fq_flush_single (struct iommu_dma_cookie *cookie) +{ +struct iova_fq *fq = cookie->single_fq; +unsigned long flags; + +spin_lock_irqsave (&fq->lock, flags); +fq_ring_free (cookie, fq); +spin_unlock_irqrestore (&fq->lock, flags); +} + +static void fq_flush_timeout (struct timer_list *t) +{ +struct iommu_dma_cookie *cookie =from_timer (cookie, t, fq_timer); + +atomic_set (&cookie->fq_timer_on,0 ); +fq_flush_iotlb (cookie); + +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) +fq_flush_single (cookie); +else +fq_flush_percpu (cookie); +} +static void queue_iova (struct iommu_dma_cookie *cookie,unsigned long pfn,unsigned long pages,struct list_head *freelist)
@@ -188,7 +212,11 @@ static void queue_iova(struct iommu_dma_cookie *cookie,*/ smp_mb (); - fq =raw_cpu_ptr (cookie->fq); +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) + fq = cookie->single_fq; +else + fq =raw_cpu_ptr (cookie->percpu_fq); +spin_lock_irqsave (&fq->lock, flags);/*
@@ -219,58 +247,114 @@ static void queue_iova(struct iommu_dma_cookie *cookie, jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT)); } -static void iommu_dma_free_fq (struct iommu_dma_cookie *cookie) +static void iommu_dma_free_fq_single (struct iova_fq *fq) +{ +int idx; + +if (!fq) +return ; +fq_ring_for_each (idx, fq) +put_pages_list (&fq->entries[idx].freelist); +vfree (fq); +} + +static void iommu_dma_free_fq_percpu (struct iova_fq __percpu *percpu_fq) {int cpu, idx; -if (!cookie->fq) -return ; - -del_timer_sync (&cookie->fq_timer);/* The IOVAs will be torn down separately, so just free our queued pages */ for_each_possible_cpu (cpu) { -struct iova_fq *fq =per_cpu_ptr (cookie->fq, cpu); +struct iova_fq *fq =per_cpu_ptr (percpu_fq, cpu);fq_ring_for_each (idx, fq)put_pages_list (&fq->entries[idx].freelist); } -free_percpu (cookie->fq); +free_percpu (percpu_fq); +} + +static void iommu_dma_free_fq (struct iommu_dma_cookie *cookie) +{ +if (!cookie->fq_domain) +return ; + +del_timer_sync (&cookie->fq_timer); +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) +iommu_dma_free_fq_single (cookie->single_fq); +else +iommu_dma_free_fq_percpu (cookie->percpu_fq); +} + + +static void iommu_dma_init_one_fq (struct iova_fq *fq) +{ +int i; + + fq->head =0 ; + fq->tail =0 ; + +spin_lock_init (&fq->lock); + +for (i =0 ; i <IOVA_FQ_SIZE ; i++) +INIT_LIST_HEAD (&fq->entries[i].freelist); +} + +static int iommu_dma_init_fq_single (struct iommu_dma_cookie *cookie) +{ +struct iova_fq *queue; + + queue =vzalloc (sizeof (*queue)); +if (!queue) +return -ENOMEM ; +iommu_dma_init_one_fq (queue); + cookie->single_fq = queue; + +return 0 ; +} + +static int iommu_dma_init_fq_percpu (struct iommu_dma_cookie *cookie) +{ +struct iova_fq __percpu *queue; +int cpu; + + queue = alloc_percpu(struct iova_fq); +if (!queue) +return -ENOMEM ; + +for_each_possible_cpu (cpu) +iommu_dma_init_one_fq (per_cpu_ptr (queue, cpu)); + cookie->percpu_fq = queue; +return 0 ; }/* sysfs updates are serialised by the mutex of the group owning @domain */ -int iommu_dma_init_fq (struct iommu_domain *domain) +int iommu_dma_init_fq (struct device *dev,struct iommu_domain *domain) {struct iommu_dma_cookie *cookie = domain->iova_cookie; -struct iova_fq __percpu *queue; -int i, cpu; +const struct iommu_ops *ops =dev_iommu_ops (dev); +int rc;if (cookie->fq_domain)return 0 ; +if (ops->tune_dma_iommu) + ops->tune_dma_iommu (dev, &cookie->options); +atomic64_set (&cookie->fq_flush_start_cnt,0 );atomic64_set (&cookie->fq_flush_finish_cnt,0 ); - queue = alloc_percpu(struct iova_fq); -if (!queue) { +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) + rc =iommu_dma_init_fq_single (cookie); +else + rc =iommu_dma_init_fq_percpu (cookie); + +if (rc) {pr_warn ("iova flush queue initialization failed\n" ); -return -ENOMEM ; +/* fall back to strict mode */ + domain->type =IOMMU_DOMAIN_DMA ; +return rc; } -for_each_possible_cpu (cpu) { -struct iova_fq *fq =per_cpu_ptr (queue, cpu); - - fq->head =0 ; - fq->tail =0 ; - -spin_lock_init (&fq->lock); - -for (i =0 ; i <IOVA_FQ_SIZE ; i++) -INIT_LIST_HEAD (&fq->entries[i].freelist); - } - - cookie->fq = queue; -timer_setup (&cookie->fq_timer, fq_flush_timeout,0 );atomic_set (&cookie->fq_timer_on,0 );/*
@@ -297,6 +381,7 @@ static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)if (cookie) {INIT_LIST_HEAD (&cookie->msi_page_list); cookie->type = type; + cookie->options.flags =IOMMU_DMA_OPTS_PER_CPU_QUEUE ; }return cookie; }
@@ -585,9 +670,9 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,if (ret) goto done_unlock; -/* If the FQ fails we can simply fall back to strict mode */ -if (domain->type ==IOMMU_DOMAIN_DMA_FQ &&iommu_dma_init_fq (domain)) - domain->type =IOMMU_DOMAIN_DMA ; +/* If the FQ fails we fall back to strict mode */ +if (domain->type ==IOMMU_DOMAIN_DMA_FQ ) +iommu_dma_init_fq (dev, domain); ret =iova_reserve_iommu_regions (dev, domain);
drivers/iommu/dma-iommu.h +2 -2
@@ -12,7 +12,7 @@int iommu_get_dma_cookie (struct iommu_domain *domain);void iommu_put_dma_cookie (struct iommu_domain *domain); -int iommu_dma_init_fq (struct iommu_domain *domain); +int iommu_dma_init_fq (struct device *dev,struct iommu_domain *domain);void iommu_dma_get_resv_regions (struct device *dev,struct list_head *list);
@@ -20,7 +20,7 @@ extern bool iommu_dma_forcedac;#else /* CONFIG_IOMMU_DMA */ -static inline int iommu_dma_init_fq (struct iommu_domain *domain) +static inline int iommu_dma_init_fq (struct device *dev,struct iommu_domain *domain) {return -EINVAL ; }
drivers/iommu/iommu.c +12 -6
@@ -2967,10 +2967,19 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,return -EINVAL ;mutex_lock (&group->mutex); +/* Ensure that device exists. */ +if (list_empty (&group->devices)) { +mutex_unlock (&group->mutex); +return -EPERM ; + } + + grp_dev = list_first_entry(&group->devices, struct group_device, list); + dev = grp_dev->dev; +/* We can bring up a flush queue without tearing down the domain. */ if (req_type ==IOMMU_DOMAIN_DMA_FQ && group->default_domain->type ==IOMMU_DOMAIN_DMA ) { - ret =iommu_dma_init_fq (group->default_domain); + ret =iommu_dma_init_fq (dev, group->default_domain);if (!ret) group->default_domain->type =IOMMU_DOMAIN_DMA_FQ ;mutex_unlock (&group->mutex);
@@ -2978,15 +2987,12 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,return ret ?: count; } -/* Otherwise, ensure that device exists and no driver is bound. */ -if (list_empty (&group->devices) || group->owner_cnt) { +/* Otherwise, ensure that no driver is bound. */ +if (group->owner_cnt) {mutex_unlock (&group->mutex);return -EPERM ; } - grp_dev = list_first_entry(&group->devices, struct group_device, list); - dev = grp_dev->dev; - ret =iommu_change_dev_def_domain (group, dev, req_type);/*
drivers/iommu/s390-iommu.c +10 -0
@@ -451,6 +451,15 @@ static void s390_iommu_get_resv_regions(struct device *dev, } } +static void s390_iommu_tune_dma_iommu (struct device *dev, +struct dma_iommu_options *options) +{ +struct zpci_dev *zdev =to_zpci_dev (dev); + +if (zdev->tlb_refresh) + options->flags |=IOMMU_DMA_OPTS_SINGLE_QUEUE ; +} +static struct iommu_device *s390_iommu_probe_device (struct device *dev) {struct zpci_dev *zdev;
@@ -793,6 +802,7 @@ static const struct iommu_ops s390_iommu_ops = { .device_group = generic_device_group, .pgsize_bitmap = SZ_4K, .get_resv_regions = s390_iommu_get_resv_regions, + .tune_dma_iommu = s390_iommu_tune_dma_iommu, .default_domain_ops = &(const struct iommu_domain_ops ) { .attach_dev = s390_iommu_attach_device, .map_pages = s390_iommu_map_pages,
include/linux/iommu.h +21 -0
@@ -219,6 +219,21 @@ struct iommu_iotlb_gather {bool queued; }; +/** +* struct dma_iommu_options - Options for dma-iommu +* +* @flags: Flag bits for enabling/disabling dma-iommu settings +* +* This structure is intended to provide IOMMU drivers a way to influence the +* behavior of the dma-iommu DMA API implementation. This allows optimizing for +* example for a virtualized environment with slow IOTLB flushes. +*/ +struct dma_iommu_options { +#define IOMMU_DMA_OPTS_PER_CPU_QUEUE (0L << 0) +#define IOMMU_DMA_OPTS_SINGLE_QUEUE (1L << 0) +u64 flags; +}; +/** * struct iommu_ops - iommu ops and capabilities * @capable: check capability
@@ -242,6 +257,9 @@ struct iommu_iotlb_gather {* - IOMMU_DOMAIN_IDENTITY: must use an identity domain * - IOMMU_DOMAIN_DMA: must use a dma domain * - 0: use the default setting +* @tune_dma_iommu: Allows the IOMMU driver to modify the default +* options of the dma-iommu layer for a specific +* device. * @default_domain_ops: the default ops for domains * @remove_dev_pasid: Remove any translation configurations of a specific * pasid, so that any DMA transactions with this pasid
@@ -278,6 +296,9 @@ struct iommu_ops {int (*def_domain_type)(struct device *dev);void (*remove_dev_pasid)(struct device *dev,ioasid_t pasid); +void (*tune_dma_iommu)(struct device *dev, +struct dma_iommu_options *options); +const struct iommu_domain_ops *default_domain_ops;unsigned long pgsize_bitmap;struct module *owner;
Signature · 1 line
2.39.2
Re: [PATCH v9 5/6] iommu/dma: Allow a single FQ in addition to per-CPU FQs
Quoted 4 lines · Niklas Schnelle
include/linux/iommu.h +4
-0+/** +* struct dma_iommu_options - Options for dma-iommu +* +* @flags: Flag bits for enabling/disabling dma-iommu settings
include/linux/iommu.h struct iommu_iotlb_gather { · line 235
Quoted 10 lines
include/linux/iommu.h +10
-0+* +* This structure is intended to provide IOMMU drivers a way to influence the +* behavior of the dma-iommu DMA API implementation. This allows optimizing for +* example for a virtualized environment with slow IOTLB flushes. +*/ +struct dma_iommu_options { +#define IOMMU_DMA_OPTS_PER_CPU_QUEUE (0L << 0) +#define IOMMU_DMA_OPTS_SINGLE_QUEUE (1L << 0) +u64 flags; +};
You need to hash it out with robin if we do something like this or use more untyped caps as he put in this series:
https://lore.kernel.org/linux-iommu/[email protected]/
Jason
Niklas Schnelle
maintainerRe: [PATCH v9 5/6] iommu/dma: Allow a single FQ in addition to per-CPU FQs
Quoted 20 lines · Jason Gunthorpe
Quoted 4 lines · Niklas Schnelle
include/linux/iommu.h +4
-0+/** +* struct dma_iommu_options - Options for dma-iommu +* +* @flags: Flag bits for enabling/disabling dma-iommu settings include/linux/iommu.h struct iommu_iotlb_gather { · line 235
Quoted 10 lines
include/linux/iommu.h +10
-0+* +* This structure is intended to provide IOMMU drivers a way to influence the +* behavior of the dma-iommu DMA API implementation. This allows optimizing for +* example for a virtualized environment with slow IOTLB flushes. +*/ +struct dma_iommu_options { +#define IOMMU_DMA_OPTS_PER_CPU_QUEUE (0L << 0) +#define IOMMU_DMA_OPTS_SINGLE_QUEUE (1L << 0) +u64 flags; +};You need to hash it out with robin if we do something like this or use more untyped caps as he put in this series:
Quoted 3 lines
https://lore.kernel.org/linux-iommu/[email protected]/
Jason
Ok. I do wonder how to best represent this as a capability. Semantically I think a capability needs to be something positive i.e. while IOMMU_CAP_EXPENSIVE_FLUSH would technically work having slow IOTLB flushes really isn't a capability. So the best I can think of is maybe IOMMU_CAP_SHADOW_ON_FLUSH. It's a bit specific but does convey that the IOTLB flush does more than dropping hardware caches where the main cost is the then empty TLB not the operation itself. Or maybe to keep thing separate one would have to add capabilities for the existing users IOMMU_CAP_HW_FLUSH and IOMMU_CAP_CONCURRENT_FLUSH.
Not sure though. It does feel more clunky than the tuning op I added and maybe instead these mechanisms should co-exist. After all even though the IOTLB flushes with shadowing are expensive they still benefit from the flush queue just with more entries and less parallelism.
Thanks,
Niklas
Robin Murphy
maintainerRe: [PATCH v9 5/6] iommu/dma: Allow a single FQ in addition to per-CPU FQs
In reply to Niklas SchnelleQuoted 87 lines · Niklas Schnelle
In some virtualized environments, including s390 paged memory guests, IOTLB flushes are used to update IOMMU shadow tables. Due to this, they are much more expensive than in typical bare metal environments or non-paged s390 guests. In addition they may parallelize more poorly in virtualized environments. This changes the trade off for flushing IOVAs such that minimizing the number of IOTLB flushes trumps any benefit of cheaper queuing operations or increased paralellism.
In this scenario per-CPU flush queues pose several problems. Firstly per-CPU memory is often quite limited prohibiting larger queues. Secondly collecting IOVAs per-CPU but flushing via a global timeout reduces the number of IOVAs flushed for each timeout especially on s390 where PCI interrupts may not be bound to a specific CPU.
Let's introduce a single flush queue mode that reuses the same queue logic but only allocates a single global queue. This mode can be selected as a flag bit in a new
dma_iommu_optionsstruct which can be modified from its defaults by IOMMU drivers implementing a newops.tune_dma_iommu()callback. As a first user the s390 IOMMU driver selects the single queue mode if IOTLB flushes are needed on map which indicates shadow table use. With the unchanged small FQ size and timeouts this setting is worse than per-CPU queues but a follow up patch will make the FQ size and timeout variable. Together this allows the common IOVA flushing code to more closely resemble the global flush behavior used on s390's previous internal DMA API implementation.Reviewed-by Matthew Rosato <[email protected]> #s390Signed-off-by Niklas Schnelle <[email protected]>--- drivers/iommu/dma-iommu.c | 163 ++++++++++++++++++++++++++++++++++----------- drivers/iommu/dma-iommu.h | 4 +- drivers/iommu/iommu.c | 18 +++-- drivers/iommu/s390-iommu.c | 10 +++ include/linux/iommu.h | 21 ++++++ 5 files changed, 169 insertions(+), 47 deletions(-)drivers/iommu/dma-iommu.c +9
-8@@ -49,8 +49,11 @@ struct iommu_dma_cookie {/* Full allocator for IOMMU_DMA_IOVA_COOKIE */ struct {struct iova_domain iovad; - -struct iova_fq __percpu *fq;/* Flush queue */ +/* Flush queue */ +union { +struct iova_fq *single_fq; +struct iova_fq __percpu *percpu_fq; + };/* Number of TLB flushes that have been started */ atomic64_t fq_flush_start_cnt;/* Number of TLB flushes that have been finished */ @@ -67,6 +70,8 @@ struct iommu_dma_cookie {/* Domain for flush queue callback; NULL if flush queue not in use */ struct iommu_domain *fq_domain; +/* Options for dma-iommu use */ +struct dma_iommu_options options;struct mutex mutex; };@@ -152,25 +157,44 @@ static void fq_flush_iotlb(struct iommu_dma_cookie *cookie)atomic64_inc (&cookie->fq_flush_finish_cnt); } -static void fq_flush_timeout (struct timer_list *t) +static void fq_flush_percpu (struct iommu_dma_cookie *cookie) { -struct iommu_dma_cookie *cookie =from_timer (cookie, t, fq_timer);int cpu; -atomic_set (&cookie->fq_timer_on,0 ); -fq_flush_iotlb (cookie); -for_each_possible_cpu (cpu) {unsigned long flags;struct iova_fq *fq; - fq =per_cpu_ptr (cookie->fq, cpu); + fq =per_cpu_ptr (cookie->percpu_fq, cpu);spin_lock_irqsave (&fq->lock, flags);fq_ring_free (cookie, fq);spin_unlock_irqrestore (&fq->lock, flags); }
drivers/iommu/dma-iommu.c static void fq_flush_iotlb(struct iommu_dma_cookie *cookie) · line 182
Quoted 10 lines
drivers/iommu/dma-iommu.c +8
-0} +static void fq_flush_single (struct iommu_dma_cookie *cookie) +{ +struct iova_fq *fq = cookie->single_fq; +unsigned long flags; + +spin_lock_irqsave (&fq->lock, flags); +fq_ring_free (cookie, fq); +spin_unlock_irqrestore (&fq->lock, flags)
Nit: this should clearly just be a self-locked version of fq_ring_free() that takes fq as an argument, then both the new case and the existing loop body become trivial one-line calls.
Quoted 143 lines
drivers/iommu/dma-iommu.c +94
-13+} + +static void fq_flush_timeout (struct timer_list *t) +{ +struct iommu_dma_cookie *cookie =from_timer (cookie, t, fq_timer); + +atomic_set (&cookie->fq_timer_on,0 ); +fq_flush_iotlb (cookie); + +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) +fq_flush_single (cookie); +else +fq_flush_percpu (cookie); +} +static void queue_iova (struct iommu_dma_cookie *cookie,unsigned long pfn,unsigned long pages,struct list_head *freelist)@@ -188,7 +212,11 @@ static void queue_iova(struct iommu_dma_cookie *cookie,*/ smp_mb (); - fq =raw_cpu_ptr (cookie->fq); +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) + fq = cookie->single_fq; +else + fq =raw_cpu_ptr (cookie->percpu_fq); +spin_lock_irqsave (&fq->lock, flags);/* @@ -219,58 +247,114 @@ static void queue_iova(struct iommu_dma_cookie *cookie, jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT)); } -static void iommu_dma_free_fq (struct iommu_dma_cookie *cookie) +static void iommu_dma_free_fq_single (struct iova_fq *fq) +{ +int idx; + +if (!fq) +return ; +fq_ring_for_each (idx, fq) +put_pages_list (&fq->entries[idx].freelist); +vfree (fq); +} + +static void iommu_dma_free_fq_percpu (struct iova_fq __percpu *percpu_fq) {int cpu, idx; -if (!cookie->fq) -return ; - -del_timer_sync (&cookie->fq_timer);/* The IOVAs will be torn down separately, so just free our queued pages */ for_each_possible_cpu (cpu) { -struct iova_fq *fq =per_cpu_ptr (cookie->fq, cpu); +struct iova_fq *fq =per_cpu_ptr (percpu_fq, cpu);fq_ring_for_each (idx, fq)put_pages_list (&fq->entries[idx].freelist); } -free_percpu (cookie->fq); +free_percpu (percpu_fq); +} + +static void iommu_dma_free_fq (struct iommu_dma_cookie *cookie) +{ +if (!cookie->fq_domain) +return ; + +del_timer_sync (&cookie->fq_timer); +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) +iommu_dma_free_fq_single (cookie->single_fq); +else +iommu_dma_free_fq_percpu (cookie->percpu_fq); +} + + +static void iommu_dma_init_one_fq (struct iova_fq *fq) +{ +int i; + + fq->head =0 ; + fq->tail =0 ; + +spin_lock_init (&fq->lock); + +for (i =0 ; i <IOVA_FQ_SIZE ; i++) +INIT_LIST_HEAD (&fq->entries[i].freelist); +} + +static int iommu_dma_init_fq_single (struct iommu_dma_cookie *cookie) +{ +struct iova_fq *queue; + + queue =vzalloc (sizeof (*queue)); +if (!queue) +return -ENOMEM ; +iommu_dma_init_one_fq (queue); + cookie->single_fq = queue; + +return 0 ; +} + +static int iommu_dma_init_fq_percpu (struct iommu_dma_cookie *cookie) +{ +struct iova_fq __percpu *queue; +int cpu; + + queue = alloc_percpu(struct iova_fq); +if (!queue) +return -ENOMEM ; + +for_each_possible_cpu (cpu) +iommu_dma_init_one_fq (per_cpu_ptr (queue, cpu)); + cookie->percpu_fq = queue; +return 0 ; }/* sysfs updates are serialised by the mutex of the group owning @domain */ -int iommu_dma_init_fq (struct iommu_domain *domain) +int iommu_dma_init_fq (struct device *dev,struct iommu_domain *domain) {struct iommu_dma_cookie *cookie = domain->iova_cookie; -struct iova_fq __percpu *queue; -int i, cpu; +const struct iommu_ops *ops =dev_iommu_ops (dev); +int rc;if (cookie->fq_domain)return 0 ; +if (ops->tune_dma_iommu) + ops->tune_dma_iommu (dev, &cookie->options); +atomic64_set (&cookie->fq_flush_start_cnt,0 );atomic64_set (&cookie->fq_flush_finish_cnt,0 ); - queue = alloc_percpu(struct iova_fq); -if (!queue) {
drivers/iommu/dma-iommu.c static void queue_iova(struct iommu_dma_cookie *cookie, · line 354
Quoted 10 lines
drivers/iommu/dma-iommu.c +8
-1+if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) + rc =iommu_dma_init_fq_single (cookie); +else + rc =iommu_dma_init_fq_percpu (cookie); + +if (rc) {pr_warn ("iova flush queue initialization failed\n" ); -return -ENOMEM ; +/* fall back to strict mode */ + domain->type =IOMMU_DOMAIN_DMA ;
Why move this? It doesn't logically belong to FQ initialisation itself.
Quoted 146 lines
drivers/iommu/dma-iommu.c +5
-17+return rc; } -for_each_possible_cpu (cpu) { -struct iova_fq *fq =per_cpu_ptr (queue, cpu); - - fq->head =0 ; - fq->tail =0 ; - -spin_lock_init (&fq->lock); - -for (i =0 ; i <IOVA_FQ_SIZE ; i++) -INIT_LIST_HEAD (&fq->entries[i].freelist); - } - - cookie->fq = queue; -timer_setup (&cookie->fq_timer, fq_flush_timeout,0 );atomic_set (&cookie->fq_timer_on,0 );/* @@ -297,6 +381,7 @@ static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)if (cookie) {INIT_LIST_HEAD (&cookie->msi_page_list); cookie->type = type; + cookie->options.flags =IOMMU_DMA_OPTS_PER_CPU_QUEUE ; }return cookie; }@@ -585,9 +670,9 @@ static int iommu_dma_init_domain(struct iommu_domain *domain, dma_addr_t base,if (ret) goto done_unlock; -/* If the FQ fails we can simply fall back to strict mode */ -if (domain->type ==IOMMU_DOMAIN_DMA_FQ &&iommu_dma_init_fq (domain)) - domain->type =IOMMU_DOMAIN_DMA ; +/* If the FQ fails we fall back to strict mode */ +if (domain->type ==IOMMU_DOMAIN_DMA_FQ ) +iommu_dma_init_fq (dev, domain); ret =iova_reserve_iommu_regions (dev, domain);drivers/iommu/dma-iommu.h +2
-2@@ -12,7 +12,7 @@int iommu_get_dma_cookie (struct iommu_domain *domain);void iommu_put_dma_cookie (struct iommu_domain *domain); -int iommu_dma_init_fq (struct iommu_domain *domain); +int iommu_dma_init_fq (struct device *dev,struct iommu_domain *domain);void iommu_dma_get_resv_regions (struct device *dev,struct list_head *list);@@ -20,7 +20,7 @@ extern bool iommu_dma_forcedac;#else /* CONFIG_IOMMU_DMA */ -static inline int iommu_dma_init_fq (struct iommu_domain *domain) +static inline int iommu_dma_init_fq (struct device *dev,struct iommu_domain *domain) {return -EINVAL ; }drivers/iommu/iommu.c +12
-6@@ -2967,10 +2967,19 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,return -EINVAL ;mutex_lock (&group->mutex); +/* Ensure that device exists. */ +if (list_empty (&group->devices)) { +mutex_unlock (&group->mutex); +return -EPERM ; + } + + grp_dev = list_first_entry(&group->devices, struct group_device, list); + dev = grp_dev->dev; +/* We can bring up a flush queue without tearing down the domain. */ if (req_type ==IOMMU_DOMAIN_DMA_FQ && group->default_domain->type ==IOMMU_DOMAIN_DMA ) { - ret =iommu_dma_init_fq (group->default_domain); + ret =iommu_dma_init_fq (dev, group->default_domain);if (!ret) group->default_domain->type =IOMMU_DOMAIN_DMA_FQ ;mutex_unlock (&group->mutex);@@ -2978,15 +2987,12 @@ static ssize_t iommu_group_store_type(struct iommu_group *group,return ret ?: count; } -/* Otherwise, ensure that device exists and no driver is bound. */ -if (list_empty (&group->devices) || group->owner_cnt) { +/* Otherwise, ensure that no driver is bound. */ +if (group->owner_cnt) {mutex_unlock (&group->mutex);return -EPERM ; } - grp_dev = list_first_entry(&group->devices, struct group_device, list); - dev = grp_dev->dev; - ret =iommu_change_dev_def_domain (group, dev, req_type);/* drivers/iommu/s390-iommu.c +10
-0@@ -451,6 +451,15 @@ static void s390_iommu_get_resv_regions(struct device *dev, } } +static void s390_iommu_tune_dma_iommu (struct device *dev, +struct dma_iommu_options *options) +{ +struct zpci_dev *zdev =to_zpci_dev (dev); + +if (zdev->tlb_refresh) + options->flags |=IOMMU_DMA_OPTS_SINGLE_QUEUE ; +} +static struct iommu_device *s390_iommu_probe_device (struct device *dev) {struct zpci_dev *zdev;@@ -793,6 +802,7 @@ static const struct iommu_ops s390_iommu_ops = { .device_group = generic_device_group, .pgsize_bitmap = SZ_4K, .get_resv_regions = s390_iommu_get_resv_regions, + .tune_dma_iommu = s390_iommu_tune_dma_iommu, .default_domain_ops = &(const struct iommu_domain_ops ) { .attach_dev = s390_iommu_attach_device, .map_pages = s390_iommu_map_pages,include/linux/iommu.h +4
-0@@ -219,6 +219,21 @@ struct iommu_iotlb_gather {bool queued; }; +/** +* struct dma_iommu_options - Options for dma-iommu +* +* @flags: Flag bits for enabling/disabling dma-iommu settings
include/linux/iommu.h struct iommu_iotlb_gather { · line 235
Quoted 10 lines
include/linux/iommu.h +10
-0+* +* This structure is intended to provide IOMMU drivers a way to influence the +* behavior of the dma-iommu DMA API implementation. This allows optimizing for +* example for a virtualized environment with slow IOTLB flushes. +*/ +struct dma_iommu_options { +#define IOMMU_DMA_OPTS_PER_CPU_QUEUE (0L << 0) +#define IOMMU_DMA_OPTS_SINGLE_QUEUE (1L << 0) +u64 flags; +};
I think for now this can just use a bit in dev_iommu to indicate that the device will prefer a global flush queue; s390 can set that in .probe_device, then iommu_dma_init_domain() can propagate it to an equivalent flag in the cookie (possibly even a new cookie type?) that iommu_dma_init_fq() can then consume. Then just make the s390 parameters from patch #6 the standard parameters for a global queue.
Thanks,
Robin.
Quoted 25 lines
include/linux/iommu.h +7
-0+/** * struct iommu_ops - iommu ops and capabilities * @capable: check capability @@ -242,6 +257,9 @@ struct iommu_iotlb_gather {* - IOMMU_DOMAIN_IDENTITY: must use an identity domain * - IOMMU_DOMAIN_DMA: must use a dma domain * - 0: use the default setting +* @tune_dma_iommu: Allows the IOMMU driver to modify the default +* options of the dma-iommu layer for a specific +* device. * @default_domain_ops: the default ops for domains * @remove_dev_pasid: Remove any translation configurations of a specific * pasid, so that any DMA transactions with this pasid @@ -278,6 +296,9 @@ struct iommu_ops {int (*def_domain_type)(struct device *dev);void (*remove_dev_pasid)(struct device *dev,ioasid_t pasid); +void (*tune_dma_iommu)(struct device *dev, +struct dma_iommu_options *options); +const struct iommu_domain_ops *default_domain_ops;unsigned long pgsize_bitmap;struct module *owner;
Niklas Schnelle
maintainerRe: [PATCH v9 5/6] iommu/dma: Allow a single FQ in addition to per-CPU FQs
Quoted 90 lines · Robin Murphy
Quoted 89 lines · Niklas Schnelle
In some virtualized environments, including s390 paged memory guests, IOTLB flushes are used to update IOMMU shadow tables. Due to this, they are much more expensive than in typical bare metal environments or non-paged s390 guests. In addition they may parallelize more poorly in virtualized environments. This changes the trade off for flushing IOVAs such that minimizing the number of IOTLB flushes trumps any benefit of cheaper queuing operations or increased paralellism.
In this scenario per-CPU flush queues pose several problems. Firstly per-CPU memory is often quite limited prohibiting larger queues. Secondly collecting IOVAs per-CPU but flushing via a global timeout reduces the number of IOVAs flushed for each timeout especially on s390 where PCI interrupts may not be bound to a specific CPU.
Let's introduce a single flush queue mode that reuses the same queue logic but only allocates a single global queue. This mode can be selected as a flag bit in a new
dma_iommu_optionsstruct which can be modified from its defaults by IOMMU drivers implementing a newops.tune_dma_iommu()callback. As a first user the s390 IOMMU driver selects the single queue mode if IOTLB flushes are needed on map which indicates shadow table use. With the unchanged small FQ size and timeouts this setting is worse than per-CPU queues but a follow up patch will make the FQ size and timeout variable. Together this allows the common IOVA flushing code to more closely resemble the global flush behavior used on s390's previous internal DMA API implementation.Reviewed-by Matthew Rosato <[email protected]> #s390Signed-off-by Niklas Schnelle <[email protected]>--- drivers/iommu/dma-iommu.c | 163 ++++++++++++++++++++++++++++++++++----------- drivers/iommu/dma-iommu.h | 4 +- drivers/iommu/iommu.c | 18 +++-- drivers/iommu/s390-iommu.c | 10 +++ include/linux/iommu.h | 21 ++++++ 5 files changed, 169 insertions(+), 47 deletions(-)drivers/iommu/dma-iommu.c +9
-8@@ -49,8 +49,11 @@ struct iommu_dma_cookie {/* Full allocator for IOMMU_DMA_IOVA_COOKIE */ struct {struct iova_domain iovad; - -struct iova_fq __percpu *fq;/* Flush queue */ +/* Flush queue */ +union { +struct iova_fq *single_fq; +struct iova_fq __percpu *percpu_fq; + };/* Number of TLB flushes that have been started */ atomic64_t fq_flush_start_cnt;/* Number of TLB flushes that have been finished */ @@ -67,6 +70,8 @@ struct iommu_dma_cookie {/* Domain for flush queue callback; NULL if flush queue not in use */ struct iommu_domain *fq_domain; +/* Options for dma-iommu use */ +struct dma_iommu_options options;struct mutex mutex; };@@ -152,25 +157,44 @@ static void fq_flush_iotlb(struct iommu_dma_cookie *cookie)atomic64_inc (&cookie->fq_flush_finish_cnt); } -static void fq_flush_timeout (struct timer_list *t) +static void fq_flush_percpu (struct iommu_dma_cookie *cookie) { -struct iommu_dma_cookie *cookie =from_timer (cookie, t, fq_timer);int cpu; -atomic_set (&cookie->fq_timer_on,0 ); -fq_flush_iotlb (cookie); -for_each_possible_cpu (cpu) {unsigned long flags;struct iova_fq *fq; - fq =per_cpu_ptr (cookie->fq, cpu); + fq =per_cpu_ptr (cookie->percpu_fq, cpu);spin_lock_irqsave (&fq->lock, flags);fq_ring_free (cookie, fq);spin_unlock_irqrestore (&fq->lock, flags); } }
Quoted 12 lines
drivers/iommu/dma-iommu.c static void fq_flush_iotlb(struct iommu_dma_cookie *cookie)
Quoted 8 lines
drivers/iommu/dma-iommu.c +8
-0+static void fq_flush_single (struct iommu_dma_cookie *cookie) +{ +struct iova_fq *fq = cookie->single_fq; +unsigned long flags; + +spin_lock_irqsave (&fq->lock, flags); +fq_ring_free (cookie, fq); +spin_unlock_irqrestore (&fq->lock, flags)Nit: this should clearly just be a self-locked version of
fq_ring_free()that takes fq as an argument, then both the new case and the existing loop body become trivial one-line calls.
Sure will do. Just one question about names. As an example pci_reset_function_locked() means that the relevant lock is already taken with pci_reset_function() adding the lock/unlock. In your wording the implied function names sound the other way around. I can't find anything similar in drivers/iommu so would you mind going the PCI way and having:
fq_ring_free_locked(): Called in queue_iova() with the lock held fr_ring_free(): Called in fq_flush_timeout() takes the lock itself
Or maybe I'm just biased because I've used the PCI ..locked() functions before and there is a better convention.
Quoted 142 lines
Quoted 142 lines
drivers/iommu/dma-iommu.c +94
-11+} + +static void fq_flush_timeout (struct timer_list *t) +{ +struct iommu_dma_cookie *cookie =from_timer (cookie, t, fq_timer); + +atomic_set (&cookie->fq_timer_on,0 ); +fq_flush_iotlb (cookie); + +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) +fq_flush_single (cookie); +else +fq_flush_percpu (cookie); +} +static void queue_iova (struct iommu_dma_cookie *cookie,unsigned long pfn,unsigned long pages,struct list_head *freelist)@@ -188,7 +212,11 @@ static void queue_iova(struct iommu_dma_cookie *cookie,*/ smp_mb (); - fq =raw_cpu_ptr (cookie->fq); +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) + fq = cookie->single_fq; +else + fq =raw_cpu_ptr (cookie->percpu_fq); +spin_lock_irqsave (&fq->lock, flags);/* @@ -219,58 +247,114 @@ static void queue_iova(struct iommu_dma_cookie *cookie, jiffies + msecs_to_jiffies(IOVA_FQ_TIMEOUT)); } -static void iommu_dma_free_fq (struct iommu_dma_cookie *cookie) +static void iommu_dma_free_fq_single (struct iova_fq *fq) +{ +int idx; + +if (!fq) +return ; +fq_ring_for_each (idx, fq) +put_pages_list (&fq->entries[idx].freelist); +vfree (fq); +} + +static void iommu_dma_free_fq_percpu (struct iova_fq __percpu *percpu_fq) {int cpu, idx; -if (!cookie->fq) -return ; - -del_timer_sync (&cookie->fq_timer);/* The IOVAs will be torn down separately, so just free our queued pages */ for_each_possible_cpu (cpu) { -struct iova_fq *fq =per_cpu_ptr (cookie->fq, cpu); +struct iova_fq *fq =per_cpu_ptr (percpu_fq, cpu);fq_ring_for_each (idx, fq)put_pages_list (&fq->entries[idx].freelist); } -free_percpu (cookie->fq); +free_percpu (percpu_fq); +} + +static void iommu_dma_free_fq (struct iommu_dma_cookie *cookie) +{ +if (!cookie->fq_domain) +return ; + +del_timer_sync (&cookie->fq_timer); +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) +iommu_dma_free_fq_single (cookie->single_fq); +else +iommu_dma_free_fq_percpu (cookie->percpu_fq); +} + + +static void iommu_dma_init_one_fq (struct iova_fq *fq) +{ +int i; + + fq->head =0 ; + fq->tail =0 ; + +spin_lock_init (&fq->lock); + +for (i =0 ; i <IOVA_FQ_SIZE ; i++) +INIT_LIST_HEAD (&fq->entries[i].freelist); +} + +static int iommu_dma_init_fq_single (struct iommu_dma_cookie *cookie) +{ +struct iova_fq *queue; + + queue =vzalloc (sizeof (*queue)); +if (!queue) +return -ENOMEM ; +iommu_dma_init_one_fq (queue); + cookie->single_fq = queue; + +return 0 ; +} + +static int iommu_dma_init_fq_percpu (struct iommu_dma_cookie *cookie) +{ +struct iova_fq __percpu *queue; +int cpu; + + queue = alloc_percpu(struct iova_fq); +if (!queue) +return -ENOMEM ; + +for_each_possible_cpu (cpu) +iommu_dma_init_one_fq (per_cpu_ptr (queue, cpu)); + cookie->percpu_fq = queue; +return 0 ; }/* sysfs updates are serialised by the mutex of the group owning @domain */ -int iommu_dma_init_fq (struct iommu_domain *domain) +int iommu_dma_init_fq (struct device *dev,struct iommu_domain *domain) {struct iommu_dma_cookie *cookie = domain->iova_cookie; -struct iova_fq __percpu *queue; -int i, cpu; +const struct iommu_ops *ops =dev_iommu_ops (dev); +int rc;if (cookie->fq_domain)return 0 ; +if (ops->tune_dma_iommu) + ops->tune_dma_iommu (dev, &cookie->options); +atomic64_set (&cookie->fq_flush_start_cnt,0 );atomic64_set (&cookie->fq_flush_finish_cnt,0 );
Quoted 14 lines
drivers/iommu/dma-iommu.c static void queue_iova(struct iommu_dma_cookie *cookie, · line 354
Quoted 12 lines
drivers/iommu/dma-iommu.c +8
-3- queue = alloc_percpu(struct iova_fq); -if (!queue) { +if (cookie->options.flags &IOMMU_DMA_OPTS_SINGLE_QUEUE ) + rc =iommu_dma_init_fq_single (cookie); +else + rc =iommu_dma_init_fq_percpu (cookie); + +if (rc) {pr_warn ("iova flush queue initialization failed\n" ); -return -ENOMEM ; +/* fall back to strict mode */ + domain->type =IOMMU_DOMAIN_DMA ;Why move this? It doesn't logically belong to FQ initialisation itself.
Ah yes this is not needed anymore. Previously when I had a new domain type I think I needed to set domain->type in here and moved the fallback for consistency. Will remove that change.
Quoted 14 lines
Quoted 14 lines
drivers/iommu/dma-iommu.c +1
-10+return rc; } -for_each_possible_cpu (cpu) { -struct iova_fq *fq =per_cpu_ptr (queue, cpu); - - fq->head =0 ; - fq->tail =0 ; - -spin_lock_init (&fq->lock); - -for (i =0 ; i <IOVA_FQ_SIZE ; i++) -INIT_LIST_HEAD (&fq->entries[i].freelist);
Quoted 8 lines
drivers/iommu/dma-iommu.c static void queue_iova(struct iommu_dma_cookie *cookie, · line 360
Quoted 8 lines
drivers/iommu/dma-iommu.c +0
-4- } - - cookie->fq = queue; -timer_setup (&cookie->fq_timer, fq_flush_timeout,0 );atomic_set (&cookie->fq_timer_on,0 );/*
---8<---
Quoted 41 lines
Quoted 23 lines
static struct iommu_device *s390_iommu_probe_device(struct device *dev) { struct zpci_dev *zdev;drivers/iommu/dma-iommu.c +1
-0@@ -793,6 +802,7 @@ static const struct iommu_ops s390_iommu_ops = { .device_group = generic_device_group, .pgsize_bitmap = SZ_4K, .get_resv_regions = s390_iommu_get_resv_regions, + .tune_dma_iommu = s390_iommu_tune_dma_iommu, .default_domain_ops = &(const struct iommu_domain_ops ) { .attach_dev = s390_iommu_attach_device, .map_pages = s390_iommu_map_pages,include/linux/iommu.h +4
-0@@ -219,6 +219,21 @@ struct iommu_iotlb_gather {bool queued; }; +/** +* struct dma_iommu_options - Options for dma-iommu +* +* @flags: Flag bits for enabling/disabling dma-iommu settings include/linux/iommu.h struct iommu_iotlb_gather { · line 235
Quoted 10 lines
include/linux/iommu.h +10
-0+* +* This structure is intended to provide IOMMU drivers a way to influence the +* behavior of the dma-iommu DMA API implementation. This allows optimizing for +* example for a virtualized environment with slow IOTLB flushes. +*/ +struct dma_iommu_options { +#define IOMMU_DMA_OPTS_PER_CPU_QUEUE (0L << 0) +#define IOMMU_DMA_OPTS_SINGLE_QUEUE (1L << 0) +u64 flags; +};I think for now this can just use a bit in
dev_iommuto indicate that the device will prefer a global flush queue; s390 can set that in .probe_device, theniommu_dma_init_domain()can propagate it to an equivalent flag in the cookie (possibly even a new cookie type?) thatiommu_dma_init_fq()can then consume. Then just make the s390 parameters from patch #6 the standard parameters for a global queue.
Quoted 2 lines
Thanks,
Robin.
Sounds good.
Quoted 27 lines
Quoted 26 lines
include/linux/iommu.h +7
-0+/** * struct iommu_ops - iommu ops and capabilities * @capable: check capability @@ -242,6 +257,9 @@ struct iommu_iotlb_gather {* - IOMMU_DOMAIN_IDENTITY: must use an identity domain * - IOMMU_DOMAIN_DMA: must use a dma domain * - 0: use the default setting +* @tune_dma_iommu: Allows the IOMMU driver to modify the default +* options of the dma-iommu layer for a specific +* device. * @default_domain_ops: the default ops for domains * @remove_dev_pasid: Remove any translation configurations of a specific * pasid, so that any DMA transactions with this pasid @@ -278,6 +296,9 @@ struct iommu_ops {int (*def_domain_type)(struct device *dev);void (*remove_dev_pasid)(struct device *dev,ioasid_t pasid); +void (*tune_dma_iommu)(struct device *dev, +struct dma_iommu_options *options); +const struct iommu_domain_ops *default_domain_ops;unsigned long pgsize_bitmap;struct module *owner;
Robin Murphy
maintainerRe: [PATCH v9 5/6] iommu/dma: Allow a single FQ in addition to per-CPU FQs
On 2023-05-23 13:02, Niklas Schnelle wrote:
[...]
Quoted 20 lines
Quoted 4 lines
Quoted 4 lines
drivers/iommu/dma-iommu.c +4
-0+static void fq_flush_single (struct iommu_dma_cookie *cookie) +{ +struct iova_fq *fq = cookie->single_fq; +unsigned long flags;Quoted 8 lines
drivers/iommu/dma-iommu.c static void fq_flush_iotlb(struct iommu_dma_cookie *cookie)
Quoted 4 lines
drivers/iommu/dma-iommu.c +4
-0+ +spin_lock_irqsave (&fq->lock, flags); +fq_ring_free (cookie, fq); +spin_unlock_irqrestore (&fq->lock, flags)Nit: this should clearly just be a self-locked version of
fq_ring_free()that takes fq as an argument, then both the new case and the existing loop body become trivial one-line calls.Sure will do. Just one question about names. As an example
pci_reset_function_locked()means that the relevant lock is already taken withpci_reset_function()adding the lock/unlock. In your wording the implied function names sound the other way around. I can't find anything similar indrivers/iommuso would you mind going the PCI way and having:
Quoted 5 lines
fq_ring_free_locked(): Called inqueue_iova()with the lock heldfr_ring_free(): Called infq_flush_timeout()takes the lock itselfOr maybe I'm just biased because I've used the PCI ..locked() functions before and there is a better convention.
Yes, that's the form that's most familiar to me too - sorry I failed to express it clearly :)
Thanks,
Robin.
Niklas Schnelle
maintainerRe: [PATCH v9 5/6] iommu/dma: Allow a single FQ in addition to per-CPU FQs
In reply to Robin MurphyQuoted 27 lines · Robin Murphy
Quoted 26 lines · Niklas Schnelle
In some virtualized environments, including s390 paged memory guests, IOTLB flushes are used to update IOMMU shadow tables. Due to this, they are much more expensive than in typical bare metal environments or non-paged s390 guests. In addition they may parallelize more poorly in virtualized environments. This changes the trade off for flushing IOVAs such that minimizing the number of IOTLB flushes trumps any benefit of cheaper queuing operations or increased paralellism.
In this scenario per-CPU flush queues pose several problems. Firstly per-CPU memory is often quite limited prohibiting larger queues. Secondly collecting IOVAs per-CPU but flushing via a global timeout reduces the number of IOVAs flushed for each timeout especially on s390 where PCI interrupts may not be bound to a specific CPU.
Let's introduce a single flush queue mode that reuses the same queue logic but only allocates a single global queue. This mode can be selected as a flag bit in a new
dma_iommu_optionsstruct which can be modified from its defaults by IOMMU drivers implementing a newops.tune_dma_iommu()callback. As a first user the s390 IOMMU driver selects the single queue mode if IOTLB flushes are needed on map which indicates shadow table use. With the unchanged small FQ size and timeouts this setting is worse than per-CPU queues but a follow up patch will make the FQ size and timeout variable. Together this allows the common IOVA flushing code to more closely resemble the global flush behavior used on s390's previous internal DMA API implementation.
Quoted 11 lines
Quoted 11 lines
Reviewed-by Matthew Rosato <[email protected]> #s390Signed-off-by Niklas Schnelle <[email protected]>--- drivers/iommu/dma-iommu.c | 163 ++++++++++++++++++++++++++++++++++----------- drivers/iommu/dma-iommu.h | 4 +- drivers/iommu/iommu.c | 18 +++-- drivers/iommu/s390-iommu.c | 10 +++ include/linux/iommu.h | 21 ++++++ 5 files changed, 169 insertions(+), 47 deletions(-)
---8<---
Quoted 23 lines
Quoted 5 lines
include/linux/iommu.h +4
-0+/** +* struct dma_iommu_options - Options for dma-iommu +* +* @flags: Flag bits for enabling/disabling dma-iommu settings include/linux/iommu.h struct iommu_iotlb_gather { · line 235
Quoted 10 lines
include/linux/iommu.h +10
-0+* +* This structure is intended to provide IOMMU drivers a way to influence the +* behavior of the dma-iommu DMA API implementation. This allows optimizing for +* example for a virtualized environment with slow IOTLB flushes. +*/ +struct dma_iommu_options { +#define IOMMU_DMA_OPTS_PER_CPU_QUEUE (0L << 0) +#define IOMMU_DMA_OPTS_SINGLE_QUEUE (1L << 0) +u64 flags; +};I think for now this can just use a bit in
dev_iommuto indicate that the device will prefer a global flush queue; s390 can set that in .probe_device, theniommu_dma_init_domain()can propagate it to an equivalent flag in the cookie (possibly even a new cookie type?) thatiommu_dma_init_fq()can then consume. Then just make the s390 parameters from patch #6 the standard parameters for a global queue.
Quoted 2 lines
Thanks,
Robin.
Working on this now. How about I move the struct dma_iommu_options definition into dma-iommu.c keeping it as part of struct iommu_dma_cookie. That way we can still have the flags, timeout and queue size organized the same but internal to dma-iommu.c. We then set them in iommu_dma_init_domain() triggered by a "shadow_on_flush" flag in struct dev_iommu. That way we can keep most of the same code but only add a single flag as external interface. The flag would also be an explicit fact about a distinctly IOMMU device thing just stating that the IOTLB flushes do extra shadowing work. This leaves the decision to then use a longer timeout and queue size within the responsibility of dma-iommu.c. I think that's overall a better match of responsibilities.
Thanks,
Niklas
Niklas Schnelle
maintainerPATCH v9 6/6 iommu/dma: Make flush queue sizes and timeout driver configurable
In reply to Niklas SchnelleFlush queues currently use a fixed compile time size of 256 entries. This being a power of 2 allows the compiler to use shift and mask instead of more expensive modulo operations. With per-CPU flush queues larger queue sizes would hit per-CPU allocation limits, with a single flush queue these limits do not apply however. Also with single queues being particularly suitable for virtualized environments with expensive IOTLB flushes these benefit especially from larger queues and thus fewer flushes.
To this end re-order struct iova_fq so we can use a dynamic array and introduce the flush queue size and timeouts as new options in the dma_iommu_options struct. So as not to lose the shift and mask optimization, check that the variable length is a power of 2 and use explicit shift and mask instead of letting the compiler optimize this.
In the s390 IOMMU driver a large fixed queue size and timeout is then set together with single queue mode bringing its performance on s390 paged memory guests on par with the previous s390 specific DMA API implementation.
--- drivers/iommu/dma-iommu.c | 40 +++++++++++++++++++++++++--------------- drivers/iommu/s390-iommu.c | 8 +++++++- include/linux/iommu.h | 6 +++++- 3 files changed, 37 insertions(+), 17 deletions(-)
drivers/iommu/dma-iommu.c +25 -15
@@ -89,10 +89,10 @@ static int __init iommu_dma_forcedac_setup(char *str)early_param ("iommu.forcedac" , iommu_dma_forcedac_setup);/* Number of entries per flush queue */ -#define IOVA_FQ_SIZE 256 +#define IOVA_DEFAULT_FQ_SIZE 256/* Timeout (in ms) after which entries are flushed from the queue */ -#define IOVA_FQ_TIMEOUT 10 +#define IOVA_DEFAULT_FQ_TIMEOUT 10/* Flush queue entry for deferred flushing */ struct iova_fq_entry {
@@ -104,18 +104,19 @@ struct iova_fq_entry {/* Per-CPU flush queue structure */ struct iova_fq { -struct iova_fq_entry entries[IOVA_FQ_SIZE ]; -unsigned int head, tail;spinlock_t lock; +unsigned int head, tail; +unsigned int mod_mask; +struct iova_fq_entry entries[]; };#define fq_ring_for_each (i, fq) \ - for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) % IOVA_FQ_SIZE) + for ((i) = (fq)->head; (i) != (fq)->tail; (i) = ((i) + 1) & (fq)->mod_mask)static inline bool fq_full (struct iova_fq *fq) {assert_spin_locked (&fq->lock); -return (((fq->tail +1 ) %IOVA_FQ_SIZE ) == fq->head); +return (((fq->tail +1 ) & fq->mod_mask) == fq->head); }static inline unsigned int fq_ring_add (struct iova_fq *fq)
@@ -124,7 +125,7 @@ static inline unsigned int fq_ring_add(struct iova_fq *fq)assert_spin_locked (&fq->lock); - fq->tail = (idx +1 ) %IOVA_FQ_SIZE ; + fq->tail = (idx +1 ) & fq->mod_mask;return idx; }
@@ -146,7 +147,7 @@ static void fq_ring_free(struct iommu_dma_cookie *cookie, struct iova_fq *fq) fq->entries[idx].iova_pfn, fq->entries[idx].pages); - fq->head = (fq->head +1 ) %IOVA_FQ_SIZE ; + fq->head = (fq->head +1 ) & fq->mod_mask; } }
@@ -244,7 +245,7 @@ static void queue_iova(struct iommu_dma_cookie *cookie,if (!atomic_read (&cookie->fq_timer_on) && !atomic_xchg (&cookie->fq_timer_on,1 ))mod_timer (&cookie->fq_timer, - jiffies +msecs_to_jiffies (IOVA_FQ_TIMEOUT )); + jiffies +msecs_to_jiffies (cookie->options.fq_timeout)); }static void iommu_dma_free_fq_single (struct iova_fq *fq)
@@ -286,27 +287,29 @@ static void iommu_dma_free_fq(struct iommu_dma_cookie *cookie) } -static void iommu_dma_init_one_fq (struct iova_fq *fq) +static void iommu_dma_init_one_fq (struct iova_fq *fq,size_t fq_size) {int i; fq->head =0 ; fq->tail =0 ; + fq->mod_mask = fq_size -1 ;spin_lock_init (&fq->lock); -for (i =0 ; i <IOVA_FQ_SIZE ; i++) +for (i =0 ; i < fq_size; i++)INIT_LIST_HEAD (&fq->entries[i].freelist); }static int iommu_dma_init_fq_single (struct iommu_dma_cookie *cookie) { +size_t fq_size = cookie->options.fq_size;struct iova_fq *queue; - queue =vzalloc (sizeof (*queue)); + queue =vzalloc (struct_size (queue, entries, fq_size));if (!queue)return -ENOMEM ; -iommu_dma_init_one_fq (queue); +iommu_dma_init_one_fq (queue, fq_size); cookie->single_fq = queue;return 0 ;
@@ -314,15 +317,17 @@ static int iommu_dma_init_fq_single(struct iommu_dma_cookie *cookie)static int iommu_dma_init_fq_percpu (struct iommu_dma_cookie *cookie) { +size_t fq_size = cookie->options.fq_size;struct iova_fq __percpu *queue;int cpu; - queue = alloc_percpu(struct iova_fq); + queue =__alloc_percpu (struct_size (queue, entries, fq_size), + __alignof__(*queue));if (!queue)return -ENOMEM ;for_each_possible_cpu (cpu) -iommu_dma_init_one_fq (per_cpu_ptr (queue, cpu)); +iommu_dma_init_one_fq (per_cpu_ptr (queue, cpu), fq_size); cookie->percpu_fq = queue;return 0 ; }
@@ -340,6 +345,9 @@ int iommu_dma_init_fq(struct device *dev, struct iommu_domain *domain)if (ops->tune_dma_iommu) ops->tune_dma_iommu (dev, &cookie->options); +if (WARN_ON_ONCE (!is_power_of_2 (cookie->options.fq_size))) + cookie->options.fq_size =IOVA_DEFAULT_FQ_SIZE ; +atomic64_set (&cookie->fq_flush_start_cnt,0 );atomic64_set (&cookie->fq_flush_finish_cnt,0 );
@@ -382,6 +390,8 @@ static struct iommu_dma_cookie *cookie_alloc(enum iommu_dma_cookie_type type)INIT_LIST_HEAD (&cookie->msi_page_list); cookie->type = type; cookie->options.flags =IOMMU_DMA_OPTS_PER_CPU_QUEUE ; + cookie->options.fq_size =IOVA_DEFAULT_FQ_SIZE ; + cookie->options.fq_timeout =IOVA_DEFAULT_FQ_TIMEOUT ; }return cookie; }
drivers/iommu/s390-iommu.c +7 -1
@@ -451,13 +451,19 @@ static void s390_iommu_get_resv_regions(struct device *dev, } } +#define S390_IOMMU_SINGLE_FQ_SIZE 32768 +#define S390_IOMMU_SINGLE_FQ_TIMEOUT 1000 +static void s390_iommu_tune_dma_iommu (struct device *dev,struct dma_iommu_options *options) {struct zpci_dev *zdev =to_zpci_dev (dev); -if (zdev->tlb_refresh) +if (zdev->tlb_refresh) { options->flags |=IOMMU_DMA_OPTS_SINGLE_QUEUE ; + options->fq_size =S390_IOMMU_SINGLE_FQ_SIZE ; + options->fq_timeout =S390_IOMMU_SINGLE_FQ_TIMEOUT ; + } }static struct iommu_device *s390_iommu_probe_device (struct device *dev)
include/linux/iommu.h +5 -1
@@ -223,6 +223,8 @@ struct iommu_iotlb_gather {* struct dma_iommu_options - Options for dma-iommu * * @flags: Flag bits for enabling/disabling dma-iommu settings +* @fq_size: Size of the IOTLB flush queue(s), must be a power of two +* @fq_timeout: Timeout used for queued IOTLB flushes * * This structure is intended to provide IOMMU drivers a way to influence the * behavior of the dma-iommu DMA API implementation. This allows optimizing for
@@ -231,7 +233,9 @@ struct iommu_iotlb_gather {struct dma_iommu_options {#define IOMMU_DMA_OPTS_PER_CPU_QUEUE (0L << 0)#define IOMMU_DMA_OPTS_SINGLE_QUEUE (1L << 0) -u64 flags; +u64 flags; +size_t fq_size; +unsigned int fq_timeout; };/**
Signature · 1 line
2.39.2
Joerg Roedel
maintainerRe: [PATCH v9 6/6] iommu/dma: Make flush queue sizes and timeout driver configurable
Quoted 4 lines · Niklas Schnelle
In the s390 IOMMU driver a large fixed queue size and timeout is then set together with single queue mode bringing its performance on s390 paged memory guests on par with the previous s390 specific DMA API implementation.
Hmm, the right flush-queue size and timeout settings are more a function of the endpoint device and device driver than of the iommu driver, no? I think something like this could also help solving the recently reported scalability problems in the fq-code, if done right.
Regards,
Joerg
Niklas Schnelle
maintainerRe: [PATCH v9 6/6] iommu/dma: Make flush queue sizes and timeout driver configurable
Quoted 6 lines · Joerg Roedel
Quoted 4 lines · Niklas Schnelle
In the s390 IOMMU driver a large fixed queue size and timeout is then set together with single queue mode bringing its performance on s390 paged memory guests on par with the previous s390 specific DMA API implementation.
Quoted 9 lines
Hmm, the right flush-queue size and timeout settings are more a function of the endpoint device and device driver than of the iommu driver, no? I think something like this could also help solving the recently reported scalability problems in the fq-code, if done right.
Regards,
Joerg
In our case the large flush queue and timeout is needed because the IOTLB flushes of the virtualized s390 IOMMU are used by KVM and z/VM to synchronize their IOMMU shadow tables thus making them more expensive. This then applies to all pass-through PCI devices without their drivers knowing about the IOMMU being virtualized like that. But yes of course there could be cases where the device driver knows better.
Thanks,
Niklas