On Wed, Jun 02, 2021 at 01:00:53PM -0600, Alex Williamson wrote: > On Wed, 2 Jun 2021 15:09:25 -0300 > Jason Gunthorpe <jgg@xxxxxxxxxx> wrote: > > > On Wed, Jun 02, 2021 at 12:01:11PM -0600, Alex Williamson wrote: > > > On Wed, 2 Jun 2021 14:35:10 -0300 > > > Jason Gunthorpe <jgg@xxxxxxxxxx> wrote: > > > > > > > On Wed, Jun 02, 2021 at 11:11:17AM -0600, Alex Williamson wrote: > > > > > > > > > > > > present and be able to test if DMA for that device is cache > > > > > > > > coherent. > > > > > > > > > > > > Why is this such a strong linkage to VFIO and not just a 'hey kvm > > > > > > emulate wbinvd' flag from qemu? > > > > > > > > > > IIRC, wbinvd has host implications, a malicious user could tell KVM to > > > > > emulate wbinvd then run the op in a loop and induce a disproportionate > > > > > load on the system. We therefore wanted a way that it would only be > > > > > enabled when required. > > > > > > > > I think the non-coherentness is vfio_device specific? eg a specific > > > > device will decide if it is coherent or not? > > > > > > No, this is specifically whether DMA is cache coherent to the > > > processor, ie. in the case of wbinvd whether the processor needs to > > > invalidate its cache in order to see data from DMA. > > > > I'm confused. This is x86, all DMA is cache coherent unless the device > > is doing something special. > > > > > > If yes I'd recast this to call kvm_arch_register_noncoherent_dma() > > > > from the VFIO_GROUP_NOTIFY_SET_KVM in the struct vfio_device > > > > implementation and not link it through the IOMMU. > > > > > > The IOMMU tells us if DMA is cache coherent, VFIO_DMA_CC_IOMMU maps to > > > IOMMU_CAP_CACHE_COHERENCY for all domains within a container. > > > > And this special IOMMU mode is basically requested by the device > > driver, right? Because if you use this mode you have to also use > > special programming techniques. > > > > This smells like all the "snoop bypass" stuff from PCIE (for GPUs > > even) in a different guise - it is device triggered, not platform > > triggered behavior. > > Right, the device can generate the no-snoop transactions, but it's the > IOMMU that essentially determines whether those transactions are > actually still cache coherent, AIUI. Wow, this is really confusing stuff in the code. At the PCI level there is a TLP bit called no-snoop that is platform specific. The general intention is to allow devices to selectively bypass the CPU caching for DMAs. GPUs like to use this feature for performance. I assume there is some exciting security issues here. Looks like allowing cache bypass does something bad inside VMs? Looks like allowing the VM to use the cache clear instruction that is mandatory with cache bypass DMA causes some QOS issues? OK. So how does it work? What I see in the intel/iommu.c is that some domains support "snoop control" or not, based on some HW flag. This indicates if the DMA_PTE_SNP bit is supported on a page by page basis or not. Since x86 always leans toward "DMA cache coherent" I'm reading some tea leaves here: IOMMU_CAP_CACHE_COHERENCY, /* IOMMU can enforce cache coherent DMA transactions */ And guessing that IOMMUs that implement DMA_PTE_SNP will ignore the snoop bit in TLPs for IOVA's that have DMA_PTE_SNP set? Further, I guess IOMMUs that don't support PTE_SNP, or have DMA_PTE_SNP clear will always honour the snoop bit. (backwards compat and all) So, IOMMU_CAP_CACHE_COHERENCY does not mean the IOMMU is DMA incoherent with the CPU caches, it just means that that snoop bit in the TLP cannot be enforced. ie the device *could* do no-shoop DMA if it wants. Devices that never do no-snoop remain DMA coherent on x86, as they always have been. IOMMU_CACHE does not mean the IOMMU is DMA cache coherent, it means the PCI device is blocked from using no-snoop in its TLPs. I wonder if ARM implemented this consistently? I see VDPA is confused.. I was confused. What a terrible set of names. In VFIO generic code I see it always sets IOMMU_CACHE: if (iommu_capable(bus, IOMMU_CAP_CACHE_COHERENCY)) domain->prot |= IOMMU_CACHE; And thus also always provides IOMMU_CACHE to iommu_map: ret = iommu_map(d->domain, iova, (phys_addr_t)pfn << PAGE_SHIFT, npage << PAGE_SHIFT, prot | d->prot); So when the IOMMU supports the no-snoop blocking security feature VFIO turns it on and blocks no-snoop to all pages? Ok.. But I must be missing something big because *something* in the IOVA map should work with no-snoopable DMA, right? Otherwise what is the point of exposing the invalidate instruction to the guest? I would think userspace should be relaying the DMA_PTE_SNP bit from the guest's page tables up to here?? The KVM hookup is driven by IOMMU_CACHE which is driven by IOMMU_CAP_CACHE_COHERENCY. So we turn on the special KVM support only if the IOMMU can block the SNP bit? And then we map all the pages to block the snoop bit? Huh? Your explanation makes perfect sense: Block guests from using the dangerous cache invalidate instruction unless a device that uses no-snoop is plugged in. Block devices from using no-snoop because something about it is insecure. Ok. But the conditions I'm looking for "device that uses no-snoop" is: - The device will issue no-snoop TLPs at all - The IOMMU will let no-snoop through - The platform will honor no-snoop Only if all three are met we should allow the dangerous instruction in KVM, right? Which brings me back to my original point - this is at least partially a device specific behavior. It depends on the content of the IOMMU page table, it depends if the device even supports no-snoop at all. My guess is this works correctly for the mdev Intel kvmgt which probably somehow allows no-snoop DMA throught the mdev SW iommu mappings. (assuming I didn't miss a tricky iommu_map without IOMMU_CACHe set in the type1 code?) But why is vfio-pci using it? Hmm? Confused, Jason