Skip to contentSkip to conversation

vfio/type1/pci: IOMMU PFNMAP invalidation

PATCH In review Alex Williamson 3 participants 13 messages May 14, 2020 – May 20, 2020

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover vfio/type1/pci: IOMMU PFNMAP invalidation

This is a follow-on series to "vfio-pci: Block user access to disabled device MMIO"[1], which extends user access blocking of disabled MMIO ranges to include unmapping the ranges from the IOMMU. The first patch adds an invalidation callback path, allowing vfio bus drivers to signal the IOMMU backend to unmap ranges with vma level granularity. This signaling is done both when the MMIO range becomes inaccessible due to memory disabling, as well as when a vma is closed, making up for the lack of tracking or pinning for non-page backed vmas. The second patch adds registration and testing interfaces such that the IOMMU backend driver can test whether a given PFNMAP vma is provided by a vfio bus driver supporting invalidation. We can then implement more restricted semantics to only allow PFNMAP DMA mappings when we have such support, which becomes the new default.

Jason, if you'd like Suggested-by credit for the ideas here I'd be glad to add it. Thanks,

Alex

[1]https://lore.kernel.org/kvm/[email protected]/

---

Alex Williamson (2):
vfio: Introduce bus driver to IOMMU invalidation interface
vfio: Introduce strict PFNMAP mappings

 drivers/vfio/pci/vfio_pci.c         |   41 ++++++++++-
 drivers/vfio/pci/vfio_pci_private.h |    1 
 drivers/vfio/vfio.c                 |   76 ++++++++++++++++++++
 drivers/vfio/vfio_iommu_type1.c     |  130 +++++++++++++++++++++++++++--------
 include/linux/vfio.h                |    9 ++
 5 files changed, 222 insertions(+), 35 deletions(-)
#1 lore raw

PATCH 1/2 vfio: Introduce bus driver to IOMMU invalidation interface

VFIO bus drivers, like vfio-pci, can allow mmaps of non-page backed device memory, such as MMIO regions of the device. The user may then map these ranges through the IOMMU, for example to enable peer-to-peer DMA between devices. When these ranges are zapped or removed from the user, such as when the MMIO region is disabled or the device is released, we should also remove the IOMMU mapping. This provides kernel level enforcement of the behavior we already see from userspace drivers like QEMU, where these ranges are unmapped when they become inaccessible. This userspace behavior is still recommended as this support only provides invalidation, dropping unmapped vmas. Those vmas are not automatically re-installed when re-mapped.

---
 drivers/vfio/pci/vfio_pci.c         |   34 +++++++++-
 drivers/vfio/pci/vfio_pci_private.h |    1 
 drivers/vfio/vfio.c                 |   14 ++++
 drivers/vfio/vfio_iommu_type1.c     |  123 ++++++++++++++++++++++++++---------
 include/linux/vfio.h                |    5 +
 5 files changed, 142 insertions(+), 35 deletions(-)
drivers/vfio/pci/vfio_pci.c +30 -4
index 49ae9faa6099..100fe5f6bc22 100644
@@ -521,6 +521,8 @@ static void vfio_pci_release(void *device_data)
 		vfio_pci_vf_token_user_add(vdev, -1);
 		vfio_spapr_pci_eeh_release(vdev->pdev);
 		vfio_pci_disable(vdev);
+		vfio_group_put_external_user(vdev->group);
+		vdev->group = NULL;
 	}
 
 	mutex_unlock(&vdev->reflck->lock);
@@ -539,6 +541,15 @@ static int vfio_pci_open(void *device_data)
 	mutex_lock(&vdev->reflck->lock);
 
 	if (!vdev->refcnt) {
+		struct pci_dev *pdev = vdev->pdev;
+
+		vdev->group = vfio_group_get_external_user_from_dev(&pdev->dev);
+		if (IS_ERR_OR_NULL(vdev->group)) {
+			ret = PTR_ERR(vdev->group);
+			vdev->group = NULL;
+			goto error;
+		}
+
 		ret = vfio_pci_enable(vdev);
 		if (ret)
 			goto error;
@@ -549,8 +560,13 @@ static int vfio_pci_open(void *device_data)
 	vdev->refcnt++;
 error:
 	mutex_unlock(&vdev->reflck->lock);
-	if (ret)
+	if (ret) {
 		module_put(THIS_MODULE);
+		if (vdev->group) {
+			vfio_group_put_external_user(vdev->group);
+			vdev->group = NULL;
+		}
+	}
 	return ret;
 }
 
@@ -1370,7 +1386,7 @@ static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
 /* Return 1 on zap and vma_lock acquired, 0 on contention (only with @try) */
 static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try)
 {
-	struct vfio_pci_mmap_vma *mmap_vma, *tmp;
+	struct vfio_pci_mmap_vma *mmap_vma;
 
 	/*
 	 * Lock ordering:
@@ -1420,6 +1436,7 @@ static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try)
 			return 1;
 		mutex_unlock(&vdev->vma_lock);
 
+again:
 		if (try) {
 			if (!down_read_trylock(&mm->mmap_sem)) {
 				mmput(mm);
@@ -1438,8 +1455,8 @@ static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try)
 			} else {
 				mutex_lock(&vdev->vma_lock);
 			}
-			list_for_each_entry_safe(mmap_vma, tmp,
-						 &vdev->vma_list, vma_next) {
+			list_for_each_entry(mmap_vma,
+					    &vdev->vma_list, vma_next) {
 				struct vm_area_struct *vma = mmap_vma->vma;
 
 				if (vma->vm_mm != mm)
@@ -1450,6 +1467,10 @@ static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try)
 
 				zap_vma_ptes(vma, vma->vm_start,
 					     vma->vm_end - vma->vm_start);
+				mutex_unlock(&vdev->vma_lock);
+				up_read(&mm->mmap_sem);
+				vfio_invalidate_pfnmap_vma(vdev->group, vma);
+				goto again;
 			}
 			mutex_unlock(&vdev->vma_lock);
 		}
@@ -1494,16 +1515,21 @@ static void vfio_pci_mmap_close(struct vm_area_struct *vma)
 {
 	struct vfio_pci_device *vdev = vma->vm_private_data;
 	struct vfio_pci_mmap_vma *mmap_vma;
+	bool found = false;
 
 	mutex_lock(&vdev->vma_lock);
 	list_for_each_entry(mmap_vma, &vdev->vma_list, vma_next) {
 		if (mmap_vma->vma == vma) {
 			list_del(&mmap_vma->vma_next);
 			kfree(mmap_vma);
+			found = true;
 			break;
 		}
 	}
 	mutex_unlock(&vdev->vma_lock);
+
+	if (found)
+		vfio_invalidate_pfnmap_vma(vdev->group, vma);
 }
 
 static vm_fault_t vfio_pci_mmap_fault(struct vm_fault *vmf)
drivers/vfio/pci/vfio_pci_private.h +1 -0
index c4f25f1e80d7..abfb60674506 100644
@@ -140,6 +140,7 @@ struct vfio_pci_device {
 	struct mutex		vma_lock;
 	struct list_head	vma_list;
 	struct rw_semaphore	memory_lock;
+	struct vfio_group	*group;
 };
 
 #define is_intx(vdev) (vdev->irq_type == VFIO_PCI_INTX_IRQ_INDEX)
drivers/vfio/vfio.c +14 -0
index 765e0e5d83ed..0fff057b7cd9 100644
@@ -2151,6 +2151,20 @@ int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
 }
 EXPORT_SYMBOL(vfio_dma_rw);
 
+void vfio_invalidate_pfnmap_vma(struct vfio_group *group,
+				struct vm_area_struct *vma)
+{
+	struct vfio_container *container;
+	struct vfio_iommu_driver *driver;
+
+	container = group->container;
+	driver = container->iommu_driver;
+
+	if (likely(driver && driver->ops->invalidate_pfnmap_vma))
+		driver->ops->invalidate_pfnmap_vma(container->iommu_data, vma);
+}
+EXPORT_SYMBOL(vfio_invalidate_pfnmap_vma);
+
 static int vfio_register_iommu_notifier(struct vfio_group *group,
 					unsigned long *events,
 					struct notifier_block *nb)
drivers/vfio/vfio_iommu_type1.c +92 -31

190 lines not on this page · raw message

include/linux/vfio.h +5 -0
index 5d92ee15d098..13abfecc1530 100644
@@ -88,6 +88,8 @@ struct vfio_iommu_driver_ops {
 					       struct notifier_block *nb);
 	int		(*dma_rw)(void *iommu_data, dma_addr_t user_iova,
 				  void *data, size_t count, bool write);
+	void		(*invalidate_pfnmap_vma)(void *iommu_data,
+						 struct vm_area_struct *vma);
 };
 
 extern int vfio_register_iommu_driver(const struct vfio_iommu_driver_ops *ops);
@@ -124,6 +126,9 @@ extern int vfio_group_unpin_pages(struct vfio_group *group,
 extern int vfio_dma_rw(struct vfio_group *group, dma_addr_t user_iova,
 		       void *data, size_t len, bool write);
 
+extern void vfio_invalidate_pfnmap_vma(struct vfio_group *group,
+				       struct vm_area_struct *vma);
+
 /* each type has independent events */
 enum vfio_notify_type {
 	VFIO_IOMMU_NOTIFY = 0,
#2 lore raw

Re: [PATCH 1/2] vfio: Introduce bus driver to IOMMU invalidation interface

drivers/vfio/pci/vfio_pci.c static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try) · line 1473

Quoted 8 lines · Alex Williamson
drivers/vfio/pci/vfio_pci.c +4 -0
@@ -1450,6 +1467,10 @@ static int vfio_pci_zap_and_vma_lock(struct vfio_pci_device *vdev, bool try)
 
 				zap_vma_ptes(vma, vma->vm_start,
 					     vma->vm_end - vma->vm_start);
+				mutex_unlock(&vdev->vma_lock);
+				up_read(&mm->mmap_sem);
+				vfio_invalidate_pfnmap_vma(vdev->group, vma);
+				goto again;

The vma pointer can't leave the read side of the mmap_sem

drivers/vfio/vfio_iommu_type1.c struct vfio_dma { · line 94

Quoted 8 lines
drivers/vfio/vfio_iommu_type1.c +2 -0
index 4a4cb7cd86b2..62ba6bd8a486 100644
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -91,6 +91,7 @@ struct vfio_dma {
 	bool			lock_cap;	/* capable(CAP_IPC_LOCK) */
 	struct task_struct	*task;
 	struct rb_root		pfn_list;	/* Ex-user pinned pfn list */
+	struct vm_area_struct	*pfnmap_vma;

This is also confusing, how does it prevent pfnmap_vma from becoming freed?

Jason

#3 lore raw

PATCH 2/2 vfio: Introduce strict PFNMAP mappings

In reply to Alex Williamson

We can't pin PFNMAP IOMMU mappings like we can standard page-backed mappings, therefore without an invalidation mechanism we can't know if we should have revoked a user's mapping. Now that we have an invalidation callback mechanism we can create an interface for vfio bus drivers to indicate their support for invalidation by registering supported vm_ops functions with vfio-core. A vfio IOMMU backend driver can then test a vma against the registered vm_ops with this support to determine whether to allow such a mapping. The type1 backend then adopts a new 'strict_mmio_maps' module option, enabled by default, restricting IOMMU mapping of PFNMAP vmas to only those supporting invalidation callbacks. vfio-pci is updated to register vfio_pci_mmap_ops as supporting this feature.

---
 drivers/vfio/pci/vfio_pci.c     |    7 ++++
 drivers/vfio/vfio.c             |   62 +++++++++++++++++++++++++++++++++++++++
 drivers/vfio/vfio_iommu_type1.c |    9 +++++-
 include/linux/vfio.h            |    4 +++
 4 files changed, 81 insertions(+), 1 deletion(-)
drivers/vfio/pci/vfio_pci.c +7 -0
index 100fe5f6bc22..dbfe6a11aa74 100644
@@ -2281,6 +2281,7 @@ static void vfio_pci_try_bus_reset(struct vfio_pci_device *vdev)
 
 static void __exit vfio_pci_cleanup(void)
 {
+	vfio_unregister_vma_inv_ops(&vfio_pci_mmap_ops);
 	pci_unregister_driver(&vfio_pci_driver);
 	vfio_pci_uninit_perm_bits();
 }
@@ -2340,10 +2341,16 @@ static int __init vfio_pci_init(void)
 	if (ret)
 		goto out_driver;
 
+	ret = vfio_register_vma_inv_ops(&vfio_pci_mmap_ops);
+	if (ret)
+		goto out_inv_ops;
+
 	vfio_pci_fill_ids();
 
 	return 0;
 
+out_inv_ops:
+	pci_unregister_driver(&vfio_pci_driver);
 out_driver:
 	vfio_pci_uninit_perm_bits();
 	return ret;
drivers/vfio/vfio.c +62 -0

94 lines not on this page · raw message

drivers/vfio/vfio_iommu_type1.c +8 -1
index 62ba6bd8a486..8d6286d89230 100644
@@ -61,6 +61,11 @@ module_param_named(dma_entry_limit, dma_entry_limit, uint, 0644);
 MODULE_PARM_DESC(dma_entry_limit,
 		 "Maximum number of user DMA mappings per container (65535).");
 
+static bool strict_mmio_maps = true;
+module_param_named(strict_mmio_maps, strict_mmio_maps, bool, 0644);
+MODULE_PARM_DESC(strict_mmio_maps,
+		 "Restrict DMA mappings of MMIO to those provided by vfio bus drivers supporting invalidation (true).");
+
 struct vfio_iommu {
 	struct list_head	domain_list;
 	struct list_head	iova_list;
@@ -375,7 +380,9 @@ static int vaddr_get_pfn(struct vfio_dma *dma, struct mm_struct *mm,
 
 	if (vma && vma->vm_flags & VM_PFNMAP) {
 		if ((dma->pfnmap_vma && dma->pfnmap_vma != vma) ||
-		    (!dma->pfnmap_vma && vaddr != dma->vaddr)) {
+		    (!dma->pfnmap_vma && (vaddr != dma->vaddr ||
+					  (strict_mmio_maps &&
+					   !vfio_vma_has_inv_ops(vma))))) {
 			ret = -EPERM;
 			goto done;
 		}
include/linux/vfio.h +4 -0
index 13abfecc1530..edc393f1287d 100644
@@ -149,6 +149,10 @@ extern int vfio_unregister_notifier(struct device *dev,
 				    enum vfio_notify_type type,
 				    struct notifier_block *nb);
 
+extern int vfio_register_vma_inv_ops(const struct vm_operations_struct *ops);
+extern void vfio_unregister_vma_inv_ops(const struct vm_operations_struct *ops);
+extern bool vfio_vma_has_inv_ops(struct vm_area_struct *vma);
+
 struct kvm;
 extern void vfio_group_set_kvm(struct vfio_group *group, struct kvm *kvm);
 
#4 lore raw

Re: [PATCH 2/2] vfio: Introduce strict PFNMAP mappings

drivers/vfio/vfio_iommu_type1.c module_param_named(dma_entry_limit, dma_entry_limit, uint, 0644); · line 68

Quoted 13 lines · Alex Williamson
drivers/vfio/vfio.c +0 -0
 	vfio_unregister_iommu_driver(&vfio_noiommu_ops);
drivers/vfio/vfio_iommu_type1.c +6 -0
index 62ba6bd8a486..8d6286d89230 100644
+++ b/drivers/vfio/vfio_iommu_type1.c
@@ -61,6 +61,11 @@ module_param_named(dma_entry_limit, dma_entry_limit, uint, 0644);
 MODULE_PARM_DESC(dma_entry_limit,
 		 "Maximum number of user DMA mappings per container (65535).");
 
+static bool strict_mmio_maps = true;
+module_param_named(strict_mmio_maps, strict_mmio_maps, bool, 0644);
+MODULE_PARM_DESC(strict_mmio_maps,
+		 "Restrict DMA mappings of MMIO to those provided by vfio bus drivers supporting invalidation (true).");
+

This should probably explain that 'false' allows some kind of security issue and maybe taint the kernel?

Do you think there is a reason to have this anyhow?

Jason

#5 lore raw
In reply to Alex Williamson
Quoted 5 lines · Alex Williamson

This is a follow-on series to "vfio-pci: Block user access to disabled device MMIO"[1], which extends user access blocking of disabled MMIO ranges to include unmapping the ranges from the IOMMU. The first patch adds an invalidation callback path, allowing vfio bus drivers to signal the IOMMU backend to unmap ranges with vma level granularity. This

Quoted 8 lines

signaling is done both when the MMIO range becomes inaccessible due to memory disabling, as well as when a vma is closed, making up for the lack of tracking or pinning for non-page backed vmas. The second patch adds registration and testing interfaces such that the IOMMU backend driver can test whether a given PFNMAP vma is provided by a vfio bus driver supporting invalidation. We can then implement more restricted semantics to only allow PFNMAP DMA mappings when we have such support, which becomes the new default.

Hi, Alex,

IIUC we'll directly tearing down the IOMMU page table without telling the userspace for those PFNMAP pages. I'm thinking whether there be any side effect on the userspace side when userspace cached these mapping information somehow. E.g., is there a way for userspace to know this event?

Currently, QEMU VT-d will maintain all the IOVA mappings for each assigned device when used with vfio-pci. In this case, QEMU will probably need to depend some invalidations sent from the guest (either userspace or kernel) device drivers to invalidate such IOVA mappings after they got removed from the hardware IOMMU page table underneath. I haven't thought deeper on what would happen if the vIOMMU has got an inconsistent mapping of the real device.

Thanks,

Signature · 1 line
Peter Xu
#6 lore raw
Quoted 22 lines · On Thu, 14 May 2020 17:25:38 -0400 Peter Xu <[email protected]> wrote:
Quoted 5 lines · Alex Williamson

This is a follow-on series to "vfio-pci: Block user access to disabled device MMIO"[1], which extends user access blocking of disabled MMIO ranges to include unmapping the ranges from the IOMMU. The first patch adds an invalidation callback path, allowing vfio bus drivers to signal the IOMMU backend to unmap ranges with vma level granularity. This

Quoted 8 lines

signaling is done both when the MMIO range becomes inaccessible due to memory disabling, as well as when a vma is closed, making up for the lack of tracking or pinning for non-page backed vmas. The second patch adds registration and testing interfaces such that the IOMMU backend driver can test whether a given PFNMAP vma is provided by a vfio bus driver supporting invalidation. We can then implement more restricted semantics to only allow PFNMAP DMA mappings when we have such support, which becomes the new default.

Hi, Alex,

IIUC we'll directly tearing down the IOMMU page table without telling the userspace for those PFNMAP pages. I'm thinking whether there be any side effect on the userspace side when userspace cached these mapping information somehow. E.g., is there a way for userspace to know this event?

Quoted 6 lines

Currently, QEMU VT-d will maintain all the IOVA mappings for each assigned device when used with vfio-pci. In this case, QEMU will probably need to depend some invalidations sent from the guest (either userspace or kernel) device drivers to invalidate such IOVA mappings after they got removed from the hardware IOMMU page table underneath. I haven't thought deeper on what would happen if the vIOMMU has got an inconsistent mapping of the real device.

Full disclosure, I haven't tested vIOMMU, there might be issues. Let's puzzle through this. Without a vIOMMU the vfio MemoryListener in QEMU makes use of address_space_memory, which is essentially the vCPU view of memory. When the memory bit of a PCI device is disabled, QEMU correctly removes the MMIO regions of the device from this AddressSpace. When re-enabled, they get re-added. In that case what we're doing here is a little bit redundant, the IOMMU mappings get dropped in response to the memory bit and the subsequent callback from the MemoryListener is effectively a no-op since the range is already unmapped. When the memory bit is re-enabled, the AddressSpace gets updated, the MemoryListener fires and we re-fault the mmap as we're re-adding the IOMMU mapping.

When we have a vIOMMU, the address_space_memory behavior should be the same as above; the vIOMMU isn't involved in vCPU to device access. So I think our concern is explicit mappings, much like vfio itself makes. That feels like a gap. When the vma is being closed, I think dropping those mappings out from under the user is probably still the right approach and I think this series would still be useful if we only did that much. I think this would also address Jason's primary concern. It's better to get an IOMMU fault from the user trying to access those mappings than it is to leave them in place.

OTOH, if we're dropping mappings in response to disabling the memory bit, we're changing a potential disabled MMIO access fault into an IOMMU fault, where the platform response might very well be fatal in either case. Maybe we need to look at a more temporary invalidation due to the memory enable bit. If we could remap the range to a kernel page we could maybe avoid the IOMMU fault and maybe even have a crude test for whether any data was written to the page while that mapping was in place (ie. simulating more restricted error handling, though more asynchronous than done at the platform level). Let me look into it. Thanks,

Alex

#7 lore raw
Quoted 3 lines · Alex Williamson

that much. I think this would also address Jason's primary concern. It's better to get an IOMMU fault from the user trying to access those mappings than it is to leave them in place.

Yes, there are few options here - if the pages are available for use by the IOMMU and *asynchronously* someone else revokes them, then the only way to protect the kernel is to block them from the IOMMUU.

For this to be sane the revokation must be under complete control of the VFIO user. ie if a user decides to disable MMIO traffic then of course the IOMMU should block P2P transfer to the MMIO bar. It is user error to have not disabled those transfers in the first place.

When this is all done inside a guest the whole logic applies. On bare metal you might get some AER or crash or MCE. In virtualization you'll get an IOMMU fault.

Quoted 5 lines

due to the memory enable bit. If we could remap the range to a kernel page we could maybe avoid the IOMMU fault and maybe even have a crude test for whether any data was written to the page while that mapping was in place (ie. simulating more restricted error handling, though more asynchronous than done at the platform level).

I'm not if this makes sense, can't we arrange to directly trap the IOMMU failure and route it into qemu if that is what is desired?

I'll try to look at this next week, swamped right now

Thanks,
Jason

#8 lore raw
Quoted 19 lines · On Thu, 14 May 2020 19:24:15 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 3 lines · Alex Williamson

that much. I think this would also address Jason's primary concern. It's better to get an IOMMU fault from the user trying to access those mappings than it is to leave them in place.

Yes, there are few options here - if the pages are available for use by the IOMMU and *asynchronously* someone else revokes them, then the only way to protect the kernel is to block them from the IOMMUU.

For this to be sane the revokation must be under complete control of the VFIO user. ie if a user decides to disable MMIO traffic then of course the IOMMU should block P2P transfer to the MMIO bar. It is user error to have not disabled those transfers in the first place.

When this is all done inside a guest the whole logic applies. On bare metal you might get some AER or crash or MCE. In virtualization you'll get an IOMMU fault.

Quoted 8 lines
Quoted 5 lines

due to the memory enable bit. If we could remap the range to a kernel page we could maybe avoid the IOMMU fault and maybe even have a crude test for whether any data was written to the page while that mapping was in place (ie. simulating more restricted error handling, though more asynchronous than done at the platform level).

I'm not if this makes sense, can't we arrange to directly trap the IOMMU failure and route it into qemu if that is what is desired?

Can't guarantee it, some systems wire that directly into their management processor so that they can "protect their users" regardless of whether they want or need it. Yay firmware first error handling, *sigh*. Thanks,

Alex

#9 lore raw
Quoted 7 lines · Alex Williamson
Quoted 2 lines

I'm not if this makes sense, can't we arrange to directly trap the IOMMU failure and route it into qemu if that is what is desired?

Can't guarantee it, some systems wire that directly into their management processor so that they can "protect their users" regardless of whether they want or need it. Yay firmware first error handling, *sigh*. Thanks,

Sorry to be slightly out of topic - Alex, does this mean the general approach of fault reporting from vfio to the userspace is not gonna work too?

Thanks,

Signature · 1 line
Peter Xu
#10 lore raw
Quoted 11 lines · On Fri, 15 May 2020 11:22:51 -0400 Peter Xu <[email protected]> wrote:
Quoted 7 lines · Alex Williamson
Quoted 2 lines

I'm not if this makes sense, can't we arrange to directly trap the IOMMU failure and route it into qemu if that is what is desired?

Can't guarantee it, some systems wire that directly into their management processor so that they can "protect their users" regardless of whether they want or need it. Yay firmware first error handling, *sigh*. Thanks,

Sorry to be slightly out of topic - Alex, does this mean the general approach of fault reporting from vfio to the userspace is not gonna work too?

AFAIK these platforms only generate a fatal fault on certain classes of access which imply a potential for data loss, for example a DMA write to an invalid PTE entry. The actual IOMMU page faulting mechanism should not be affected by this, or at least one would hope. Thanks,

Alex

#11 lore raw
In reply to Alex Williamson
Quoted 28 lines · Alex Williamson
Quoted 25 lines · On Thu, 14 May 2020 19:24:15 -0300 Jason Gunthorpe <[email protected]> wrote:
Quoted 3 lines · Alex Williamson

that much. I think this would also address Jason's primary concern. It's better to get an IOMMU fault from the user trying to access those mappings than it is to leave them in place.

Yes, there are few options here - if the pages are available for use by the IOMMU and *asynchronously* someone else revokes them, then the only way to protect the kernel is to block them from the IOMMUU.

For this to be sane the revokation must be under complete control of the VFIO user. ie if a user decides to disable MMIO traffic then of course the IOMMU should block P2P transfer to the MMIO bar. It is user error to have not disabled those transfers in the first place.

When this is all done inside a guest the whole logic applies. On bare metal you might get some AER or crash or MCE. In virtualization you'll get an IOMMU fault.

Quoted 5 lines

due to the memory enable bit. If we could remap the range to a kernel page we could maybe avoid the IOMMU fault and maybe even have a crude test for whether any data was written to the page while that mapping was in place (ie. simulating more restricted error handling, though more asynchronous than done at the platform level).

Quoted 7 lines
Quoted 2 lines

I'm not if this makes sense, can't we arrange to directly trap the IOMMU failure and route it into qemu if that is what is desired?

Can't guarantee it, some systems wire that directly into their management processor so that they can "protect their users" regardless of whether they want or need it. Yay firmware first error handling, *sigh*. Thanks,

I feel like those system should just loose the ability to reliably mirror IOMMU errors to their guests - trying to emulate it by scanning memory/etc sounds too horrible.

Jason

#12 lore raw
In reply to Alex Williamson
Quoted 14 lines · Alex Williamson

This is a follow-on series to "vfio-pci: Block user access to disabled device MMIO"[1], which extends user access blocking of disabled MMIO ranges to include unmapping the ranges from the IOMMU. The first patch adds an invalidation callback path, allowing vfio bus drivers to signal the IOMMU backend to unmap ranges with vma level granularity. This signaling is done both when the MMIO range becomes inaccessible due to memory disabling, as well as when a vma is closed, making up for the lack of tracking or pinning for non-page backed vmas. The second patch adds registration and testing interfaces such that the IOMMU backend driver can test whether a given PFNMAP vma is provided by a vfio bus driver supporting invalidation. We can then implement more restricted semantics to only allow PFNMAP DMA mappings when we have such support, which becomes the new default.

Quoted 2 lines

Jason, if you'd like Suggested-by credit for the ideas here I'd be glad to add it. Thanks,

Certainly a Reported-by would be OK

The only thing I don't like here is this makes some P2P DMA mapping scheme for VMAs with invalidation that is completely private to vfio.

Many of us want this in other subsystems, and there are legimiate uses for vfio to import BAR memory for P2P from other places than itself.

So I would really rather this be supported by the core kernel in some way.

That said, this is a bug fix, and we still don't have much agreement on what the core kernel version should look like, let alone how it should work with an IOMMU.

So maybe this goes ahead as is and we can figure out how to replace it with something general later on?

Jason