Make the iommu driver no-snoop block feature consistent
Subsystems (5)
v3 cover Make the iommu driver no-snoop block feature consistent
PCIe defines a 'no-snoop' bit in each the TLP which is usually implemented by a platform as bypassing elements in the DMA coherent CPU cache hierarchy. A driver can command a device to set this bit on some of its transactions as a micro-optimization.
However, the driver is now responsible to synchronize the CPU cache with the DMA that bypassed it. On x86 this may be done through the wbinvd instruction, and the i915 GPU driver is the only Linux DMA driver that calls it.
The problem comes that KVM on x86 will normally disable the wbinvd instruction in the guest and render it a NOP. As the driver running in the guest is not aware the wbinvd doesn't work it may still cause the device to set the no-snoop bit and the platform will bypass the CPU cache. Without a working wbinvd there is no way to re-synchronize the CPU cache and the driver in the VM has data corruption.
Thus, we see a general direction on x86 that the IOMMU HW is able to block the no-snoop bit in the TLP. This NOP's the optimization and allows KVM to to NOP the wbinvd without causing any data corruption.
This control for Intel IOMMU was exposed by using IOMMU_CACHE and IOMMU_CAP_CACHE_COHERENCY, however these two values now have multiple meanings and usages beyond blocking no-snoop and the whole thing has become confused. AMD IOMMU has the same feature and same IOPTE bits however it unconditionally blocks no-snoop.
Change it so that:
- IOMMU_CACHE is only about the DMA coherence of normal DMAs from a device. It is used by the DMA API/VFIO/etc when the user of the iommu_domain will not be doing manual cache coherency operations.
- IOMMU_CAP_CACHE_COHERENCY indicates if IOMMU_CACHE can be used with the device.
- The new optional domain op enforce_cache_coherency() will cause the entire domain to block no-snoop requests - ie there is no way for any device attached to the domain to opt out of the IOMMU_CACHE behavior. This is permanent on the domain and must apply to any future devices attached to it.
Ideally an iommu driver should implement enforce_cache_coherency() so that DMA API domains allow the no-snoop optimization. This leaves it available to kernel drivers like i915. VFIO will call enforce_cache_coherency() before establishing any mappings and the domain should then permanently block no-snoop.
If enforce_cache_coherency() fails VFIO will communicate back through to KVM into the arch code via kvm_arch_register_noncoherent_dma() (only implemented by x86) which triggers a working wbinvd to be made available to the VM.
While other iommu drivers are certainly welcome to implement enforce_cache_coherency(), it is not clear there is any benefit in doing so right now.
This is on github: https://github.com/jgunthorpe/linux/commits/intel_no_snoop
Joerg, I think we are ready with this now - if there are no futher comments I won't resend it unless there is some conflict with rc3. Thanks everyone!
v3:
- Rename dmar_domain->enforce_no_snoop to dmar_domain->force_snooping
- Accumulate acks
- Rebase to 5.18-rc2
- Revise commit language
v2: https://lore.kernel.org/r/[email protected]
- Abandon removing IOMMU_CAP_CACHE_COHERENCY - instead make it the cap flag that indicates IOMMU_CACHE is supported
- Put the VFIO tests for IOMMU_CACHE at VFIO device registration
- In the Intel driver remove the domain->iommu_snooping value, this is global not per-domain
v1: https://lore.kernel.org/r/[email protected]
Jason Gunthorpe (4):
iommu: Introduce the domain op enforce_cache_coherency()
vfio: Move the Intel no-snoop control off of IOMMU_CACHE
iommu: Redefine IOMMU_CAP_CACHE_COHERENCY as the cap flag for
IOMMU_CACHE
vfio: Require that devices support DMA cache coherence
drivers/iommu/amd/iommu.c | 7 +++++++ drivers/iommu/intel/iommu.c | 17 +++++++++++++---- drivers/vfio/vfio.c | 7 +++++++ drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++----------- include/linux/intel-iommu.h | 2 +- include/linux/iommu.h | 7 +++++-- 6 files changed, 52 insertions(+), 18 deletions(-)
base-commit: ce522ba9ef7e2d9fb22a39eb3371c0c64e2a433e
Signature · 6 lines
2.35.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v3 1/4 iommu: Introduce the domain op enforce_cache_coherency()
This new mechanism will replace using IOMMU_CAP_CACHE_COHERENCY and IOMMU_CACHE to control the no-snoop blocking behavior of the IOMMU.
Currently only Intel and AMD IOMMUs are known to support this feature. They both implement it as an IOPTE bit, that when set, will cause PCIe TLPs to that IOVA with the no-snoop bit set to be treated as though the no-snoop bit was clear.
The new API is triggered by calling enforce_cache_coherency() before mapping any IOVA to the domain which globally switches on no-snoop blocking. This allows other implementations that might block no-snoop globally and outside the IOPTE - AMD also documents such a HW capability.
Leave AMD out of sync with Intel and have it block no-snoop even for in-kernel users. This can be trivially resolved in a follow up patch.
Only VFIO needs to call this API because it does not have detailed control over the device to avoid requesting no-snoop behavior at the device level. Other places using domains with real kernel drivers should simply avoid asking their devices to set the no-snoop bit.
--- drivers/iommu/amd/iommu.c | 7 +++++++ drivers/iommu/intel/iommu.c | 14 +++++++++++++- include/linux/intel-iommu.h | 1 + include/linux/iommu.h | 4 ++++ 4 files changed, 25 insertions(+), 1 deletion(-)
drivers/iommu/amd/iommu.c +7 -0
@@ -2271,6 +2271,12 @@ static int amd_iommu_def_domain_type(struct device *dev)return 0 ; } +static bool amd_iommu_enforce_cache_coherency (struct iommu_domain *domain) +{ +/* IOMMU_PTE_FC is always set */ +return true; +} +const struct iommu_ops amd_iommu_ops = { .capable = amd_iommu_capable, .domain_alloc = amd_iommu_domain_alloc,
@@ -2293,6 +2299,7 @@ const struct iommu_ops amd_iommu_ops = { .flush_iotlb_all = amd_iommu_flush_iotlb_all, .iotlb_sync = amd_iommu_iotlb_sync, .free = amd_iommu_domain_free, + .enforce_cache_coherency = amd_iommu_enforce_cache_coherency, } };
drivers/iommu/intel/iommu.c +13 -1
@@ -4422,7 +4422,8 @@ static int intel_iommu_map(struct iommu_domain *domain, prot |=DMA_PTE_READ ;if (iommu_prot &IOMMU_WRITE ) prot |=DMA_PTE_WRITE ; -if ((iommu_prot &IOMMU_CACHE ) && dmar_domain->iommu_snooping) +if (((iommu_prot &IOMMU_CACHE ) && dmar_domain->iommu_snooping) || + dmar_domain->force_snooping) prot |=DMA_PTE_SNP ; max_addr = iova + size;
@@ -4545,6 +4546,16 @@ static phys_addr_t intel_iommu_iova_to_phys(struct iommu_domain *domain,return phys; } +static bool intel_iommu_enforce_cache_coherency (struct iommu_domain *domain) +{ +struct dmar_domain *dmar_domain =to_dmar_domain (domain); + +if (!dmar_domain->iommu_snooping) +return false; + dmar_domain->force_snooping = true; +return true; +} +static bool intel_iommu_capable (enum iommu_cap cap) {if (cap ==IOMMU_CAP_CACHE_COHERENCY )
@@ -4898,6 +4909,7 @@ const struct iommu_ops intel_iommu_ops = { .iotlb_sync = intel_iommu_tlb_sync, .iova_to_phys = intel_iommu_iova_to_phys, .free = intel_iommu_domain_free, + .enforce_cache_coherency = intel_iommu_enforce_cache_coherency, } };
include/linux/intel-iommu.h +1 -0
@@ -540,6 +540,7 @@ struct dmar_domain { u8 has_iotlb_device: 1; u8 iommu_coherency: 1; /* indicate coherency of iommu access */ u8 iommu_snooping: 1; /* indicate snooping control feature */ + u8 force_snooping : 1; /* Create IOPTEs with snoop control */struct list_head devices;/* all devices' list */ struct iova_domain iovad;/* iova's that belong to this domain */
include/linux/iommu.h +4 -0
@@ -272,6 +272,9 @@ struct iommu_ops {* @iotlb_sync: Flush all queued ranges from the hardware TLBs and empty flush * queue * @iova_to_phys: translate iova to physical address +* @enforce_cache_coherency: Prevent any kind of DMA from bypassing IOMMU_CACHE, +* including no-snoop TLPs on PCIe or other platform +* specific mechanisms. * @enable_nesting: Enable nesting * @set_pgtable_quirks: Set io page table quirks (IO_PGTABLE_QUIRK_*) * @free: Release the domain after use.
@@ -300,6 +303,7 @@ struct iommu_domain_ops {phys_addr_t (*iova_to_phys)(struct iommu_domain *domain,dma_addr_t iova); +bool (*enforce_cache_coherency)(struct iommu_domain *domain);int (*enable_nesting)(struct iommu_domain *domain);int (*set_pgtable_quirks)(struct iommu_domain *domain,unsigned long quirks);
Signature · 6 lines
2.35.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v3 2/4 vfio: Move the Intel no-snoop control off of IOMMU_CACHE
In reply to Jason GunthorpeIOMMU_CACHE means "normal DMA to this iommu_domain's IOVA should be cache coherent" and is used by the DMA API. The definition allows for special non-coherent DMA to exist - ie processing of the no-snoop flag in PCIe TLPs - so long as this behavior is opt-in by the device driver.
The flag is mainly used by the DMA API to synchronize the IOMMU setting with the expected cache behavior of the DMA master. eg based on dev_is_dma_coherent() in some case.
For Intel IOMMU IOMMU_CACHE was redefined to mean 'force all DMA to be cache coherent' which has the practical effect of causing the IOMMU to ignore the no-snoop bit in a PCIe TLP.
x86 platforms are always IOMMU_CACHE, so Intel should ignore this flag.
Instead use the new domain op enforce_cache_coherency() which causes every IOPTE created in the domain to have the no-snoop blocking behavior.
Reconfigure VFIO to always use IOMMU_CACHE and call enforce_cache_coherency() to operate the special Intel behavior.
Remove the IOMMU_CACHE test from Intel IOMMU.
Ultimately VFIO plumbs the result of enforce_cache_coherency() back into the x86 platform code through kvm_arch_register_noncoherent_dma() which controls if the WBINVD instruction is available in the guest. No other archs implement kvm_arch_register_noncoherent_dma() nor are there any other known consumers of VFIO_DMA_CC_IOMMU that might be affected by the user visible result change on non-x86 archs.
--- drivers/iommu/intel/iommu.c | 7 ++----- drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++----------- include/linux/intel-iommu.h | 1 - 3 files changed, 21 insertions(+), 17 deletions(-)
drivers/iommu/intel/iommu.c +2 -5
@@ -641,7 +641,6 @@ static unsigned long domain_super_pgsize_bitmap(struct dmar_domain *domain)static void domain_update_iommu_cap (struct dmar_domain *domain) {domain_update_iommu_coherency (domain); - domain->iommu_snooping =domain_update_iommu_snooping (NULL ); domain->iommu_superpage =domain_update_iommu_superpage (domain,NULL );/*
@@ -4283,7 +4282,6 @@ static int md_domain_init(struct dmar_domain *domain, int guest_width) domain->agaw =width_to_agaw (adjust_width); domain->iommu_coherency = false; - domain->iommu_snooping = false; domain->iommu_superpage =0 ; domain->max_addr =0 ;
@@ -4422,8 +4420,7 @@ static int intel_iommu_map(struct iommu_domain *domain, prot |=DMA_PTE_READ ;if (iommu_prot &IOMMU_WRITE ) prot |=DMA_PTE_WRITE ; -if (((iommu_prot &IOMMU_CACHE ) && dmar_domain->iommu_snooping) || - dmar_domain->force_snooping) +if (dmar_domain->force_snooping) prot |=DMA_PTE_SNP ; max_addr = iova + size;
@@ -4550,7 +4547,7 @@ static bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain) {struct dmar_domain *dmar_domain =to_dmar_domain (domain); -if (!dmar_domain->iommu_snooping) +if (!domain_update_iommu_snooping (NULL ))return false; dmar_domain->force_snooping = true;return true;
drivers/vfio/vfio_iommu_type1.c +19 -11
@@ -84,8 +84,8 @@ struct vfio_domain {struct iommu_domain *domain;struct list_head next;struct list_head group_list; -int prot;/* IOMMU_CACHE */ -bool fgsp;/* Fine-grained super pages */ + bool fgsp : 1; /* Fine-grained super pages */ + bool enforce_cache_coherency : 1; };struct vfio_dma {
@@ -1461,7 +1461,7 @@ static int vfio_iommu_map(struct vfio_iommu *iommu, dma_addr_t iova,list_for_each_entry (d, &iommu->domain_list, next) { ret =iommu_map (d->domain, iova, (phys_addr_t )pfn <<PAGE_SHIFT , - npage <<PAGE_SHIFT , prot | d->prot); + npage <<PAGE_SHIFT , prot |IOMMU_CACHE );if (ret) goto unwind;
@@ -1771,7 +1771,7 @@ static int vfio_iommu_replay(struct vfio_iommu *iommu, } ret =iommu_map (domain->domain, iova, phys, - size, dma->prot | domain->prot); + size, dma->prot |IOMMU_CACHE );if (ret) {if (!dma->iommu_mapped) { vfio_unpin_pages_remote(dma, iova,
@@ -1859,7 +1859,7 @@ static void vfio_test_domain_fgsp(struct vfio_domain *domain)return ; ret =iommu_map (domain->domain,0 ,page_to_phys (pages),PAGE_SIZE *2 , -IOMMU_READ |IOMMU_WRITE | domain->prot); +IOMMU_READ |IOMMU_WRITE |IOMMU_CACHE );if (!ret) {size_t unmapped =iommu_unmap (domain->domain,0 ,PAGE_SIZE );
@@ -2267,8 +2267,15 @@ static int vfio_iommu_type1_attach_group(void *iommu_data, goto out_detach; } -if (iommu_capable (bus,IOMMU_CAP_CACHE_COHERENCY )) - domain->prot |=IOMMU_CACHE ; +/* +* If the IOMMU can block non-coherent operations (ie PCIe TLPs with +* no-snoop set) then VFIO always turns this feature on because on Intel +* platforms it optimizes KVM to disable wbinvd emulation. +*/ +if (domain->domain->ops->enforce_cache_coherency) + domain->enforce_cache_coherency = + domain->domain->ops->enforce_cache_coherency ( + domain->domain);/* * Try to match an existing compatible domain. We don't want to
@@ -2279,7 +2286,8 @@ static int vfio_iommu_type1_attach_group(void *iommu_data,*/ list_for_each_entry (d, &iommu->domain_list, next) {if (d->domain->ops == domain->domain->ops && - d->prot == domain->prot) { + d->enforce_cache_coherency == + domain->enforce_cache_coherency) {iommu_detach_group (domain->domain, group->iommu_group);if (!iommu_attach_group (d->domain, group->iommu_group)) {
@@ -2611,14 +2619,14 @@ static void vfio_iommu_type1_release(void *iommu_data)kfree (iommu); } -static int vfio_domains_have_iommu_cache (struct vfio_iommu *iommu) +static int vfio_domains_have_enforce_cache_coherency (struct vfio_iommu *iommu) {struct vfio_domain *domain;int ret =1 ;mutex_lock (&iommu->lock);list_for_each_entry (domain, &iommu->domain_list, next) { -if (!(domain->prot &IOMMU_CACHE )) { +if (!(domain->enforce_cache_coherency)) { ret =0 ;break ; }
@@ -2641,7 +2649,7 @@ static int vfio_iommu_type1_check_extension(struct vfio_iommu *iommu,case VFIO_DMA_CC_IOMMU :if (!iommu)return 0 ; -return vfio_domains_have_iommu_cache (iommu); +return vfio_domains_have_enforce_cache_coherency (iommu);default :return 0 ; }
include/linux/intel-iommu.h +0 -1
@@ -539,7 +539,6 @@ struct dmar_domain { u8 has_iotlb_device: 1; u8 iommu_coherency: 1; /* indicate coherency of iommu access */ - u8 iommu_snooping: 1; /* indicate snooping control feature */ u8 force_snooping : 1; /* Create IOPTEs with snoop control */struct list_head devices;/* all devices' list */
Signature · 6 lines
2.35.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v3 3/4 iommu: Redefine IOMMU_CAP_CACHE_COHERENCY as the cap flag for IOMMU_CACHE
In reply to Jason GunthorpeWhile the comment was correct that this flag was intended to convey the block no-snoop support in the IOMMU, it has become widely implemented and used to mean the IOMMU supports IOMMU_CACHE as a map flag. Only the Intel driver was different.
Now that the Intel driver is using enforce_cache_coherency() update the comment to make it clear that IOMMU_CAP_CACHE_COHERENCY is only about IOMMU_CACHE. Fix the Intel driver to return true since IOMMU_CACHE always works.
The two places that test this flag, usnic and vdpa, are both assigning userspace pages to a driver controlled iommu_domain and require IOMMU_CACHE behavior as they offer no way for userspace to synchronize caches.
--- drivers/iommu/intel/iommu.c | 2 +- include/linux/iommu.h | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-)
drivers/iommu/intel/iommu.c +1 -1
@@ -4556,7 +4556,7 @@ static bool intel_iommu_enforce_cache_coherency(struct iommu_domain *domain)static bool intel_iommu_capable (enum iommu_cap cap) {if (cap ==IOMMU_CAP_CACHE_COHERENCY ) -return domain_update_iommu_snooping (NULL ); +return true;if (cap ==IOMMU_CAP_INTR_REMAP )return irq_remapping_enabled ==1 ;
include/linux/iommu.h +1 -2
@@ -103,8 +103,7 @@ static inline bool iommu_is_dma_domain(struct iommu_domain *domain) }enum iommu_cap { -IOMMU_CAP_CACHE_COHERENCY ,/* IOMMU can enforce cache coherent DMA -transactions */ +IOMMU_CAP_CACHE_COHERENCY ,/* IOMMU_CACHE is supported */ IOMMU_CAP_INTR_REMAP ,/* IOMMU supports interrupt isolation */ IOMMU_CAP_NOEXEC ,/* IOMMU_NOEXEC flag */ };
Signature · 6 lines
2.35.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
PATCH v3 4/4 vfio: Require that devices support DMA cache coherence
In reply to Jason GunthorpeIOMMU_CACHE means that normal DMAs do not require any additional coherency mechanism and is the basic uAPI that VFIO exposes to userspace. For instance VFIO applications like DPDK will not work if additional coherency operations are required.
Therefore check IOMMU_CAP_CACHE_COHERENCY like vdpa & usnic do before allowing an IOMMU backed VFIO device to be created.
--- drivers/vfio/vfio.c | 7 +++++++ 1 file changed, 7 insertions(+)
drivers/vfio/vfio.c +7 -0
@@ -815,6 +815,13 @@ static int __vfio_register_dev(struct vfio_device *device,int vfio_register_group_dev (struct vfio_device *device) { +/* +* VFIO always sets IOMMU_CACHE because we offer no way for userspace to +* restore cache coherency. +*/ +if (!iommu_capable (device->dev->bus,IOMMU_CAP_CACHE_COHERENCY )) +return -EINVAL ; +return __vfio_register_dev (device,vfio_group_find_or_alloc (device->dev)); }
Signature · 6 lines
2.35.1 _______________________________________________ iommu mailing list [email protected] https://lists.linuxfoundation.org/mailman/listinfo/iommu
Re: [PATCH v3 4/4] vfio: Require that devices support DMA cache coherence
Hi,
We encounter a issue with the patch: our platform is ARM64, and we run DPDK with smmu disable on VM (without iommu=smmuv3 etc),
so we use noiommu mode with enable_unsafe_noiommu_mode=1 to passthrough the device to VM with following steps (those steps are on VM) :
insmod vfio.ko enable_unsafe_noiommu_mode=1
insmod vfio_virqfd.ko
insmod vfio-pci-core.ko
insmdo vfio-pci.ko
insmod vfio_iommu_type1.ko
echo vfio-pci > /sys/bus/pci/devices/0000:00:02.0/driver_override echo 0000:00:02.0 > /sys/bus/pci/drivers_probe ------------------ failed
I find that vfio-pci device is not probed because of the additional check. It works well without this patch.
Do we need to skip the check if enable_unsafe_noiommu_mode=1?
Best regards,
Xiang Chen
Quoted 34 lines · Jason Gunthorpe
IOMMU_CACHEmeans that normal DMAs do not require any additional coherency mechanism and is the basic uAPI that VFIO exposes to userspace. For instance VFIO applications like DPDK will not work if additional coherency operations are required.Therefore check
IOMMU_CAP_CACHE_COHERENCYlike vdpa & usnic do before allowing an IOMMU backed VFIO device to be created.Reviewed-by Kevin Tian <[email protected]>Acked-by Alex Williamson <[email protected]>Acked-by Robin Murphy <[email protected]>Signed-off-by Jason Gunthorpe <[email protected]>--- drivers/vfio/vfio.c | 7 +++++++ 1 file changed, 7 insertions(+)drivers/vfio/vfio.c +7
-0@@ -815,6 +815,13 @@ static int __vfio_register_dev(struct vfio_device *device,int vfio_register_group_dev (struct vfio_device *device) { +/* +* VFIO always sets IOMMU_CACHE because we offer no way for userspace to +* restore cache coherency. +*/ +if (!iommu_capable (device->dev->bus,IOMMU_CAP_CACHE_COHERENCY )) +return -EINVAL ; +return __vfio_register_dev (device,vfio_group_find_or_alloc (device->dev)); }
Re: [PATCH v3 4/4] vfio: Require that devices support DMA cache coherence
Quoted 14 lines · chenxiang (M)
Hi,
We encounter a issue with the patch: our platform is ARM64, and we run DPDK with smmu disable on VM (without iommu=smmuv3 etc),
so we use noiommu mode with enable_unsafe_noiommu_mode=1 to passthrough the device to VM with following steps (those steps are on VM) :
insmod vfio.ko enable_unsafe_noiommu_mode=1
insmod vfio_virqfd.ko
insmod vfio-pci-core.ko
insmdo vfio-pci.ko
insmod vfio_iommu_type1.ko
Quoted 7 lines
echo vfio-pci > /sys/bus/pci/devices/0000:00:02.0/driver_override echo 0000:00:02.0 >
/sys/bus/pci/drivers_probe------------------ failedI find that vfio-pci device is not probed because of the additional check. It works well without this patch.
Do we need to skip the check if enable_unsafe_noiommu_mode=1?
Yes, that is definately an unintended mistake.
Thanks,
Jason
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu
Joerg Roedel
maintainerQuoted 7 lines · Jason Gunthorpe
Jason Gunthorpe (4):
iommu: Introduce the domain openforce_cache_coherency()
vfio: Move the Intel no-snoop control off ofIOMMU_CACHE
iommu: RedefineIOMMU_CAP_CACHE_COHERENCYas the cap flag for
IOMMU_CACHE
vfio: Require that devices support DMA cache coherence
Quoted 7 lines
drivers/iommu/amd/iommu.c | 7 +++++++ drivers/iommu/intel/iommu.c | 17 +++++++++++++---- drivers/vfio/vfio.c | 7 +++++++ drivers/vfio/vfio_iommu_type1.c | 30 +++++++++++++++++++----------- include/linux/intel-iommu.h | 2 +- include/linux/iommu.h | 7 +++++-- 6 files changed, 52 insertions(+), 18 deletions(-)
Applied to core branch, thanks everyone.
_______________________________________________
iommu mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/iommu