Skip to contentSkip to conversation

Fix UNMAP notifier for intel-iommu

PATCH Superseded Jason Wang 6 participants 35 messages Nov 29, 2022 – Feb 23, 2023

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Jason Wang

author
#0 lore raw

cover Fix UNMAP notifier for intel-iommu

Hi All:

According to ATS, device should work if ATS is disabled. This is not correctly implemented in the current intel-iommu since it doesn't handle the UNMAP notifier correctly. This breaks the vhost-net + vIOMMU without dt.

The root casue is that the when there's a device IOTLB miss (note that it's not specific to PCI so it can work without ATS), Qemu doesn't build the IOVA tree, so when guest start an IOTLB invalidation, Qemu won't trigger the UNMAP notifier.

Fixing by build IOVA tree during IOMMU translsation.

Thanks

Jason Wang (3):
intel-iommu: fail MAP notifier without caching mode
intel-iommu: fail DEVIOTLB_UNMAP without dt mode
intel-iommu: build iova tree during IOMMU translation

 hw/i386/intel_iommu.c | 58 ++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 25 deletions(-)
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 1/3 intel-iommu: fail MAP notifier without caching mode

Without caching mode, MAP notifier won't work correctly since guest won't send IOTLB update event when it establishes new mappings in the I/O page tables. Let's fail the IOMMU notifiers early instead of misbehaving silently.

---
 hw/i386/intel_iommu.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +7 -0
index a08ee85edf..9143376677 100644
@@ -3186,6 +3186,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          "Snoop Control with vhost or VFIO is not supported");
         return -ENOTSUP;
     }
+    if (!s->caching_mode && (new & IOMMU_NOTIFIER_MAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires caching mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }
 
     /* Update per-address-space notifier flags */
     vtd_as->notifier_flags = new;
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 1/3] intel-iommu: fail MAP notifier without caching mode

Quoted 15 lines · Jason Wang

Without caching mode, MAP notifier won't work correctly since guest won't send IOTLB update event when it establishes new mappings in the I/O page tables. Let's fail the IOMMU notifiers early instead of misbehaving silently.

---
 hw/i386/intel_iommu.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +0 -0
index a08ee85edf..9143376677 100644

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3195

Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
@@ -3186,6 +3186,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          "Snoop Control with vhost or VFIO is not supported");
         return -ENOTSUP;
     }
+    if (!s->caching_mode && (new & IOMMU_NOTIFIER_MAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires caching mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }

We used to have that but got reverted because it's too late to fail, so we moved it over even though not as clean..

https://lore.kernel.org/all/[email protected]/

Thanks,

Signature · 1 line
Peter Xu
author
#3 lore raw

Re: [PATCH 1/3] intel-iommu: fail MAP notifier without caching mode

Quoted 31 lines · Peter Xu
Quoted 15 lines · Jason Wang

Without caching mode, MAP notifier won't work correctly since guest won't send IOTLB update event when it establishes new mappings in the I/O page tables. Let's fail the IOMMU notifiers early instead of misbehaving silently.

---
 hw/i386/intel_iommu.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +0 -0
index a08ee85edf..9143376677 100644

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3195

Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
@@ -3186,6 +3186,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          "Snoop Control with vhost or VFIO is not supported");
         return -ENOTSUP;
     }
+    if (!s->caching_mode && (new & IOMMU_NOTIFIER_MAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires caching mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }

We used to have that but got reverted because it's too late to fail, so we moved it over even though not as clean..

Quoted 1 line

https://lore.kernel.org/all/[email protected]/

One of the difference is that the patch doesn't do exit() here. I think it's better to fail instead of misbehving silently, this is what other vIOMMU did:

E.g in smmu we had:

    if (new & IOMMU_NOTIFIER_MAP) {
        error_setg(errp,
                   "device %02x.%02x.%x requires iommu MAP notifier which is "
                   "not currently supported", pci_bus_num(sdev->bus),
                   PCI_SLOT(sdev->devfn), PCI_FUNC(sdev->devfn));
        return -EINVAL;
    }

So did for amd iommu.

Thanks

Quoted 6 lines

Thanks,

--
Peter Xu

Reviewed-by
#4 lore raw

Re: [PATCH 1/3] intel-iommu: fail MAP notifier without caching mode

Quoted 48 lines · Jason Wang
Quoted 31 lines · Peter Xu
Quoted 15 lines · Jason Wang

Without caching mode, MAP notifier won't work correctly since guest won't send IOTLB update event when it establishes new mappings in the I/O page tables. Let's fail the IOMMU notifiers early instead of misbehaving silently.

---
 hw/i386/intel_iommu.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +0 -0
index a08ee85edf..9143376677 100644

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3195

Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
@@ -3186,6 +3186,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          "Snoop Control with vhost or VFIO is not supported");
         return -ENOTSUP;
     }
+    if (!s->caching_mode && (new & IOMMU_NOTIFIER_MAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires caching mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }

We used to have that but got reverted because it's too late to fail, so we moved it over even though not as clean..

Quoted 1 line

https://lore.kernel.org/all/[email protected]/

One of the difference is that the patch doesn't do exit() here. I think it's better to fail instead of misbehving silently, this is what other vIOMMU did:

E.g in smmu we had:

    if (new & IOMMU_NOTIFIER_MAP) {
        error_setg(errp,
                   "device %02x.%02x.%x requires iommu MAP notifier which is "
                   "not currently supported", pci_bus_num(sdev->bus),
                   PCI_SLOT(sdev->devfn), PCI_FUNC(sdev->devfn));
        return -EINVAL;
    }
Quoted 1 line

So did for amd iommu.

Yeah that's fine. Would you mind add the explanation (and also above link, which might be helpful to pick up the history..) to the commit message? With that:

Thanks,

Signature · 1 line
Peter Xu
author
#5 lore raw

Re: [PATCH 1/3] intel-iommu: fail MAP notifier without caching mode

Quoted 50 lines · Peter Xu
Quoted 48 lines · Jason Wang
Quoted 31 lines · Peter Xu
Quoted 15 lines · Jason Wang

Without caching mode, MAP notifier won't work correctly since guest won't send IOTLB update event when it establishes new mappings in the I/O page tables. Let's fail the IOMMU notifiers early instead of misbehaving silently.

---
 hw/i386/intel_iommu.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +0 -0
index a08ee85edf..9143376677 100644

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3195

Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
@@ -3186,6 +3186,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          "Snoop Control with vhost or VFIO is not supported");
         return -ENOTSUP;
     }
+    if (!s->caching_mode && (new & IOMMU_NOTIFIER_MAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires caching mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }

We used to have that but got reverted because it's too late to fail, so we moved it over even though not as clean..

Quoted 1 line

https://lore.kernel.org/all/[email protected]/

One of the difference is that the patch doesn't do exit() here. I think it's better to fail instead of misbehving silently, this is what other vIOMMU did:

E.g in smmu we had:

    if (new & IOMMU_NOTIFIER_MAP) {
        error_setg(errp,
                   "device %02x.%02x.%x requires iommu MAP notifier which is "
                   "not currently supported", pci_bus_num(sdev->bus),
                   PCI_SLOT(sdev->devfn), PCI_FUNC(sdev->devfn));
        return -EINVAL;
    }
Quoted 4 lines
Quoted 1 line

So did for amd iommu.

Yeah that's fine. Would you mind add the explanation (and also above link, which might be helpful to pick up the history..) to the commit message?

Will do.

Quoted 5 lines

With that:

Thanks,

Thanks

Quoted 4 lines

--
Peter Xu

Reviewed-by on v1 1/3
#6 lore raw

Re: [PATCH 1/3] intel-iommu: fail MAP notifier without caching mode

In reply to Jason Wang

Hi Jason,

Quoted 6 lines · Jason Wang

Without caching mode, MAP notifier won't work correctly since guest won't send IOTLB update event when it establishes new mappings in the I/O page tables. Let's fail the IOMMU notifiers early instead of misbehaving silently.

Thanks

Eric

Quoted 22 lines
---
 hw/i386/intel_iommu.c | 7 +++++++
 1 file changed, 7 insertions(+)
hw/i386/intel_iommu.c +7 -0
index a08ee85edf..9143376677 100644
@@ -3186,6 +3186,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          "Snoop Control with vhost or VFIO is not supported");
         return -ENOTSUP;
     }
+    if (!s->caching_mode && (new & IOMMU_NOTIFIER_MAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires caching mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }
 
     /* Update per-address-space notifier flags */
     vtd_as->notifier_flags = new;
author
#7 lore raw

PATCH 2/3 intel-iommu: fail DEVIOTLB_UNMAP without dt mode

In reply to Jason Wang

Without dt mode, device IOTLB notifier won't work since guest won't send device IOTLB invalidation descriptor in this case. Let's fail early instead of misbehaving silently.

---
 hw/i386/intel_iommu.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/i386/intel_iommu.c +8 -0
index 9143376677..d025ef2873 100644
@@ -3179,6 +3179,7 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
 {
     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
     IntelIOMMUState *s = vtd_as->iommu_state;
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
     /* TODO: add support for VFIO and vhost users */
     if (s->snoop_control) {
@@ -3193,6 +3194,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          PCI_FUNC(vtd_as->devfn));
         return -ENOTSUP;
     }
+    if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires device IOTLB mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }
 
     /* Update per-address-space notifier flags */
     vtd_as->notifier_flags = new;
Signature · 1 line
2.25.1
Reviewed-by on v1 2/3
#8 lore raw

Re: [PATCH 2/3] intel-iommu: fail DEVIOTLB_UNMAP without dt mode

Quoted 22 lines · Jason Wang

Without dt mode, device IOTLB notifier won't work since guest won't send device IOTLB invalidation descriptor in this case. Let's fail early instead of misbehaving silently.

---
 hw/i386/intel_iommu.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 9143376677..d025ef2873 100644
@@ -3179,6 +3179,7 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
 {
     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
     IntelIOMMUState *s = vtd_as->iommu_state;
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
     /* TODO: add support for VFIO and vhost users */
     if (s->snoop_control) {

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3203

Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
@@ -3193,6 +3194,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          PCI_FUNC(vtd_as->devfn));
         return -ENOTSUP;
     }
+    if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires device IOTLB mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }

Hmm I thought we have had this already, so we don't?... :-(

Signature · 1 line
Peter Xu
#9 lore raw

Re: [PATCH 2/3] intel-iommu: fail DEVIOTLB_UNMAP without dt mode

In reply to Jason Wang
Quoted 22 lines · Jason Wang

Without dt mode, device IOTLB notifier won't work since guest won't send device IOTLB invalidation descriptor in this case. Let's fail early instead of misbehaving silently.

---
 hw/i386/intel_iommu.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 9143376677..d025ef2873 100644
@@ -3179,6 +3179,7 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
 {
     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
     IntelIOMMUState *s = vtd_as->iommu_state;
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
     /* TODO: add support for VFIO and vhost users */
     if (s->snoop_control) {

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3203

Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
@@ -3193,6 +3194,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          PCI_FUNC(vtd_as->devfn));
         return -ENOTSUP;
     }
+    if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires device IOTLB mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }

While my r-b holds.. let's also do this for amd-iommu in the same patch? dt never supported there, so we can fail as long as DEVIOTLB registered.

Quoted 6 lines
hw/i386/intel_iommu.c +0 -0
     /* Update per-address-space notifier flags */
     vtd_as->notifier_flags = new;
Signature · 1 line
2.25.1
Signature · 1 line
Peter Xu
author
#10 lore raw

Re: [PATCH 2/3] intel-iommu: fail DEVIOTLB_UNMAP without dt mode

Quoted 21 lines · Peter Xu
Quoted 19 lines · Jason Wang

Without dt mode, device IOTLB notifier won't work since guest won't send device IOTLB invalidation descriptor in this case. Let's fail early instead of misbehaving silently.

---
 hw/i386/intel_iommu.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 9143376677..d025ef2873 100644
@@ -3179,6 +3179,7 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
 {
     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
     IntelIOMMUState *s = vtd_as->iommu_state;
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
Quoted 16 lines

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3203

Quoted 13 lines
hw/i386/intel_iommu.c +7 -0
     /* TODO: add support for VFIO and vhost users */
     if (s->snoop_control) {
@@ -3193,6 +3194,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          PCI_FUNC(vtd_as->devfn));
         return -ENOTSUP;
     }
+    if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires device IOTLB mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }

While my r-b holds.. let's also do this for amd-iommu in the same patch? dt never supported there, so we can fail as long as DEVIOTLB registered.

Looks like there's one implementation:

Per spec:

""
The INVALIDATE_IOTLB_PAGES command is only present in IOMMU implementations that support remote IOTLB caching of translations (see Capability Offset 00h[IotlbSup]). This command instructs the specified device to invalidate the given range of addresses in its IOTLB. The size of the invalidate command is determined by the S bit and the address.
""

And it has one implementation (though buggy) iommu_inval_iotlb() which doesn't trigger any DEVIOTLB_UNMAP notifier.

We can leave this for the future.

(Last time I tried amd-iommu it didn't even boot).

Thanks

Quoted 11 lines
Quoted 7 lines
hw/i386/intel_iommu.c +0 -1
     /* Update per-address-space notifier flags */
     vtd_as->notifier_flags = new;
--

2.25.1

--
Peter Xu

Reviewed-by on v1 2/3
#11 lore raw

Re: [PATCH 2/3] intel-iommu: fail DEVIOTLB_UNMAP without dt mode

In reply to Jason Wang

Hi jason,

Quoted 22 lines · Jason Wang

Without dt mode, device IOTLB notifier won't work since guest won't send device IOTLB invalidation descriptor in this case. Let's fail early instead of misbehaving silently.

---
 hw/i386/intel_iommu.c | 8 ++++++++
 1 file changed, 8 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 9143376677..d025ef2873 100644
@@ -3179,6 +3179,7 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
 {
     VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
     IntelIOMMUState *s = vtd_as->iommu_state;
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
     /* TODO: add support for VFIO and vhost users */
     if (s->snoop_control) {

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3199

Quoted 6 lines
hw/i386/intel_iommu.c +3 -0
@@ -3193,6 +3194,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                          PCI_FUNC(vtd_as->devfn));
         return -ENOTSUP;
     }
+    if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires device IOTLB mode",

maybe precise INTEL IOMMU device-IOTLB mode. otherwise this may be confused with device ATS capability?

While thinking about those error handlings (including the SMMU ones) nothing should really prevent you from registering a notifier that is not signalled. Maybe we should add in the documentation that any attempt to register an IOMMU notifier to an IOMMU MR that is not able to signal it will return an error.

Besides

Eric

Quoted 7 lines
hw/i386/intel_iommu.c +4 -0
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }
 
     /* Update per-address-space notifier flags */
     vtd_as->notifier_flags = new;
Reviewed-by Tested-by on v1 2/3
#12 lore raw

Re: [PATCH 2/3] intel-iommu: fail DEVIOTLB_UNMAP without dt mode

In reply to Jason Wang
Quoted 25 lines · Jason Wang

Without dt mode, device IOTLB notifier won't work since guest won't send device IOTLB invalidation descriptor in this case. Let's fail early instead of misbehaving silently.

---
  hw/i386/intel_iommu.c | 8 ++++++++
  1 file changed, 8 insertions(+)
hw/i386/intel_iommu.c +1 -0
index 9143376677..d025ef2873 100644
@@ -3179,6 +3179,7 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
  {
      VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
      IntelIOMMUState *s = vtd_as->iommu_state;
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
 
      /* TODO: add support for VFIO and vhost users */
      if (s->snoop_control) {
@@ -3193,6 +3194,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                           PCI_FUNC(vtd_as->devfn));
          return -ENOTSUP;
      }

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3206

Quoted 10 lines
hw/i386/intel_iommu.c +7 -0
+    if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires device IOTLB mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
+        return -ENOTSUP;
+    }
 
      /* Update per-address-space notifier flags */
      vtd_as->notifier_flags = new;
#13 lore raw

Re: [PATCH 2/3] intel-iommu: fail DEVIOTLB_UNMAP without dt mode

Quoted 30 lines · Laurent Vivier
Quoted 29 lines · Jason Wang

Without dt mode, device IOTLB notifier won't work since guest won't send device IOTLB invalidation descriptor in this case. Let's fail early instead of misbehaving silently.

---
  hw/i386/intel_iommu.c | 8 ++++++++

  1 file changed, 8 insertions(+)
hw/i386/intel_iommu.c +6 -0
index 9143376677..d025ef2873 100644
@@ -3179,6 +3179,7 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
   {
       VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
       IntelIOMMUState *s = vtd_as->iommu_state;
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
       /* TODO: add support for VFIO and vhost users */
       if (s->snoop_control) {
@@ -3193,6 +3194,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                            PCI_FUNC(vtd_as->devfn));
           return -ENOTSUP;
       }
+    if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires device IOTLB mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
Quoted 8 lines

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3206

Quoted 4 lines
hw/i386/intel_iommu.c +2 -0
+        return -ENOTSUP;
+    }
       /* Update per-address-space notifier flags */
       vtd_as->notifier_flags = new;

Is this possible to have this patch merged?
It fixes a real problem and it is really trivial.

Thanks,
Laurent

#14 lore raw

Re: [PATCH 2/3] intel-iommu: fail DEVIOTLB_UNMAP without dt mode

Quoted 40 lines · Laurent Vivier
Quoted 30 lines · Laurent Vivier
Quoted 29 lines · Jason Wang

Without dt mode, device IOTLB notifier won't work since guest won't send device IOTLB invalidation descriptor in this case. Let's fail early instead of misbehaving silently.

---
  hw/i386/intel_iommu.c | 8 ++++++++

  1 file changed, 8 insertions(+)
hw/i386/intel_iommu.c +6 -0
index 9143376677..d025ef2873 100644
@@ -3179,6 +3179,7 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
   {
       VTDAddressSpace *vtd_as = container_of(iommu, VTDAddressSpace, iommu);
       IntelIOMMUState *s = vtd_as->iommu_state;
+    X86IOMMUState *x86_iommu = X86_IOMMU_DEVICE(s);
       /* TODO: add support for VFIO and vhost users */
       if (s->snoop_control) {
@@ -3193,6 +3194,13 @@ static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu,
                            PCI_FUNC(vtd_as->devfn));
           return -ENOTSUP;
       }
+    if (!x86_iommu->dt_supported && (new & IOMMU_NOTIFIER_DEVIOTLB_UNMAP)) {
+        error_setg_errno(errp, ENOTSUP,
+                         "device %02x.%02x.%x requires device IOTLB mode",
+                         pci_bus_num(vtd_as->bus), PCI_SLOT(vtd_as->devfn),
+                         PCI_FUNC(vtd_as->devfn));
Quoted 8 lines

hw/i386/intel_iommu.c static int vtd_iommu_notify_flag_changed(IOMMUMemoryRegion *iommu, · line 3206

Quoted 4 lines
hw/i386/intel_iommu.c +2 -0
+        return -ENOTSUP;
+    }
       /* Update per-address-space notifier flags */
       vtd_as->notifier_flags = new;
Quoted 2 lines

Is this possible to have this patch merged?
It fixes a real problem and it is really trivial.

AFAIU Jason will post a new version soon for this whole set. But I also agree if Michael has an earlier pull we can add this in even earlier.

Thanks,

Signature · 1 line
Peter Xu
author
#15 lore raw

PATCH 3/3 intel-iommu: build iova tree during IOMMU translation

In reply to Jason Wang

The IOVA tree is only built during page walk this breaks the device that tries to use UNMAP notifier only. One example is vhost-net, it tries to use UNMAP notifier when vIOMMU doesn't support DEVIOTLB_UNMAP notifier (e.g when dt mode is not enabled). The interesting part is that it doesn't use MAP since it can query the IOMMU translation by itself upon a IOTLB miss.

This doesn't work since Qemu doesn't build IOVA tree in IOMMU translation which means the UNMAP notifier won't be triggered during the page walk since Qemu think it is never mapped. This could be noticed when vIOMMU is used with vhost_net but dt is disabled.

Fixing this by build the iova tree during IOMMU translation, this makes sure the UNMAP notifier event could be identified during page walk. And we need to walk page table not only for UNMAP notifier but for MAP notifier during PSI.

---
 hw/i386/intel_iommu.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)
hw/i386/intel_iommu.c +18 -25
index d025ef2873..edeb62f4b2 100644
@@ -1834,6 +1834,8 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     uint8_t access_flags;
     bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
     VTDIOTLBEntry *iotlb_entry;
+    const DMAMap *mapped;
+    DMAMap target;
 
     /*
      * We have standalone memory region for interrupt addresses, we
@@ -1954,6 +1956,21 @@ out:
     entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
     entry->addr_mask = ~page_mask;
     entry->perm = access_flags;
+
+    target.iova = entry->iova;
+    target.size = entry->addr_mask;
+    target.translated_addr = entry->translated_addr;
+    target.perm = entry->perm;
+
+    mapped = iova_tree_find(vtd_as->iova_tree, &target);
+    if (!mapped) {
+        /* To make UNMAP notifier work, we need build iova tree here
+         * in order to have the UNMAP iommu notifier to be triggered
+         * during the page walk.
+         */
+        iova_tree_insert(vtd_as->iova_tree, &target);
+    }
+
     return true;
 
 error:
@@ -2161,31 +2178,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                        vtd_as->devfn, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            if (vtd_as_has_map_notifier(vtd_as)) {
-                /*
-                 * As long as we have MAP notifications registered in
-                 * any of our IOMMU notifiers, we need to sync the
-                 * shadow page table.
-                 */
-                vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
-            } else {
-                /*
-                 * For UNMAP-only notifiers, we don't need to walk the
-                 * page tables.  We just deliver the PSI down to
-                 * invalidate caches.
-                 */
-                IOMMUTLBEvent event = {
-                    .type = IOMMU_NOTIFIER_UNMAP,
-                    .entry = {
-                        .target_as = &address_space_memory,
-                        .iova = addr,
-                        .translated_addr = 0,
-                        .addr_mask = size - 1,
-                        .perm = IOMMU_NONE,
-                    },
-                };
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);
-            }
+            vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
         }
     }
 }
Signature · 1 line
2.25.1
#16 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

Quoted 75 lines · Jason Wang

The IOVA tree is only built during page walk this breaks the device that tries to use UNMAP notifier only. One example is vhost-net, it tries to use UNMAP notifier when vIOMMU doesn't support DEVIOTLB_UNMAP notifier (e.g when dt mode is not enabled). The interesting part is that it doesn't use MAP since it can query the IOMMU translation by itself upon a IOTLB miss.

This doesn't work since Qemu doesn't build IOVA tree in IOMMU translation which means the UNMAP notifier won't be triggered during the page walk since Qemu think it is never mapped. This could be noticed when vIOMMU is used with vhost_net but dt is disabled.

Fixing this by build the iova tree during IOMMU translation, this makes sure the UNMAP notifier event could be identified during page walk. And we need to walk page table not only for UNMAP notifier but for MAP notifier during PSI.

---
 hw/i386/intel_iommu.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)
hw/i386/intel_iommu.c +17 -14
index d025ef2873..edeb62f4b2 100644
@@ -1834,6 +1834,8 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     uint8_t access_flags;
     bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
     VTDIOTLBEntry *iotlb_entry;
+    const DMAMap *mapped;
+    DMAMap target;
 
     /*
      * We have standalone memory region for interrupt addresses, we
@@ -1954,6 +1956,21 @@ out:
     entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
     entry->addr_mask = ~page_mask;
     entry->perm = access_flags;
+
+    target.iova = entry->iova;
+    target.size = entry->addr_mask;
+    target.translated_addr = entry->translated_addr;
+    target.perm = entry->perm;
+
+    mapped = iova_tree_find(vtd_as->iova_tree, &target);
+    if (!mapped) {
+        /* To make UNMAP notifier work, we need build iova tree here
+         * in order to have the UNMAP iommu notifier to be triggered
+         * during the page walk.
+         */
+        iova_tree_insert(vtd_as->iova_tree, &target);
+    }
+
     return true;
 
 error:
@@ -2161,31 +2178,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                        vtd_as->devfn, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            if (vtd_as_has_map_notifier(vtd_as)) {
-                /*
-                 * As long as we have MAP notifications registered in
-                 * any of our IOMMU notifiers, we need to sync the
-                 * shadow page table.
-                 */
-                vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
-            } else {
-                /*
-                 * For UNMAP-only notifiers, we don't need to walk the
-                 * page tables.  We just deliver the PSI down to
-                 * invalidate caches.
-                 */
-                IOMMUTLBEvent event = {

hw/i386/intel_iommu.c static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, · line 2187

Quoted 10 lines
hw/i386/intel_iommu.c +0 -10
-                    .type = IOMMU_NOTIFIER_UNMAP,
-                    .entry = {
-                        .target_as = &address_space_memory,
-                        .iova = addr,
-                        .translated_addr = 0,
-                        .addr_mask = size - 1,
-                        .perm = IOMMU_NONE,
-                    },
-                };
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);

Isn't this path the one that will be responsible for pass-through the UNMAP events from guest to vhost when there's no MAP notifier requested?

At least that's what I expected when introducing the iova tree, because for unmap-only device hierachy I thought we didn't need the tree at all.

Jason, do you know where I miss?

Thanks,

Quoted 8 lines
hw/i386/intel_iommu.c +1 -1
-            }
+            vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
         }
     }
 }
Signature · 1 line
2.25.1
Signature · 1 line
Peter Xu
author
#17 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

Quoted 82 lines · Peter Xu
Quoted 80 lines · Jason Wang

The IOVA tree is only built during page walk this breaks the device that tries to use UNMAP notifier only. One example is vhost-net, it tries to use UNMAP notifier when vIOMMU doesn't support DEVIOTLB_UNMAP notifier (e.g when dt mode is not enabled). The interesting part is that it doesn't use MAP since it can query the IOMMU translation by itself upon a IOTLB miss.

This doesn't work since Qemu doesn't build IOVA tree in IOMMU translation which means the UNMAP notifier won't be triggered during the page walk since Qemu think it is never mapped. This could be noticed when vIOMMU is used with vhost_net but dt is disabled.

Fixing this by build the iova tree during IOMMU translation, this makes sure the UNMAP notifier event could be identified during page walk. And we need to walk page table not only for UNMAP notifier but for MAP notifier during PSI.

---
 hw/i386/intel_iommu.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)
hw/i386/intel_iommu.c +17 -19
index d025ef2873..edeb62f4b2 100644
@@ -1834,6 +1834,8 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     uint8_t access_flags;
     bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
     VTDIOTLBEntry *iotlb_entry;
+    const DMAMap *mapped;
+    DMAMap target;
 
     /*
      * We have standalone memory region for interrupt addresses, we
@@ -1954,6 +1956,21 @@ out:
     entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
     entry->addr_mask = ~page_mask;
     entry->perm = access_flags;
+
+    target.iova = entry->iova;
+    target.size = entry->addr_mask;
+    target.translated_addr = entry->translated_addr;
+    target.perm = entry->perm;
+
+    mapped = iova_tree_find(vtd_as->iova_tree, &target);
+    if (!mapped) {
+        /* To make UNMAP notifier work, we need build iova tree here
+         * in order to have the UNMAP iommu notifier to be triggered
+         * during the page walk.
+         */
+        iova_tree_insert(vtd_as->iova_tree, &target);
+    }
+
     return true;
 
 error:
@@ -2161,31 +2178,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                        vtd_as->devfn, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            if (vtd_as_has_map_notifier(vtd_as)) {
-                /*
-                 * As long as we have MAP notifications registered in
-                 * any of our IOMMU notifiers, we need to sync the
-                 * shadow page table.
-                 */
-                vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
-            } else {
-                /*
-                 * For UNMAP-only notifiers, we don't need to walk the
-                 * page tables.  We just deliver the PSI down to
-                 * invalidate caches.
-                 */
-                IOMMUTLBEvent event = {
-                    .type = IOMMU_NOTIFIER_UNMAP,
-                    .entry = {
-                        .target_as = &address_space_memory,
-                        .iova = addr,
-                        .translated_addr = 0,
Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, · line 2187

Quoted 5 lines
hw/i386/intel_iommu.c +0 -5
-                        .addr_mask = size - 1,
-                        .perm = IOMMU_NONE,
-                    },
-                };
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);

Isn't this path the one that will be responsible for pass-through the UNMAP events from guest to vhost when there's no MAP notifier requested?

Yes, but it doesn't do the iova tree removing. More below.

Quoted 3 lines

At least that's what I expected when introducing the iova tree, because for unmap-only device hierachy I thought we didn't need the tree at all.

Then the problem is the UNMAP notifier won't be trigger at all during DSI page walk in vtd_page_walk_one() because there's no DMAMap stored in the iova tree.:

        if (!mapped) {
            /* Skip since we didn't map this range at all */
            trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
            return 0;
        }

So I choose to build the iova tree in translate then we won't go within the above condition.

Thanks

Quoted 17 lines

Jason, do you know where I miss?

Thanks,

Quoted 8 lines
hw/i386/intel_iommu.c +1 -2
-            }
+            vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
         }
     }
 }
--

2.25.1

--
Peter Xu

#18 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

Quoted 102 lines · Jason Wang
Quoted 82 lines · Peter Xu
Quoted 80 lines · Jason Wang

The IOVA tree is only built during page walk this breaks the device that tries to use UNMAP notifier only. One example is vhost-net, it tries to use UNMAP notifier when vIOMMU doesn't support DEVIOTLB_UNMAP notifier (e.g when dt mode is not enabled). The interesting part is that it doesn't use MAP since it can query the IOMMU translation by itself upon a IOTLB miss.

This doesn't work since Qemu doesn't build IOVA tree in IOMMU translation which means the UNMAP notifier won't be triggered during the page walk since Qemu think it is never mapped. This could be noticed when vIOMMU is used with vhost_net but dt is disabled.

Fixing this by build the iova tree during IOMMU translation, this makes sure the UNMAP notifier event could be identified during page walk. And we need to walk page table not only for UNMAP notifier but for MAP notifier during PSI.

---
 hw/i386/intel_iommu.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)
hw/i386/intel_iommu.c +17 -19
index d025ef2873..edeb62f4b2 100644
@@ -1834,6 +1834,8 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     uint8_t access_flags;
     bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
     VTDIOTLBEntry *iotlb_entry;
+    const DMAMap *mapped;
+    DMAMap target;
 
     /*
      * We have standalone memory region for interrupt addresses, we
@@ -1954,6 +1956,21 @@ out:
     entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
     entry->addr_mask = ~page_mask;
     entry->perm = access_flags;
+
+    target.iova = entry->iova;
+    target.size = entry->addr_mask;
+    target.translated_addr = entry->translated_addr;
+    target.perm = entry->perm;
+
+    mapped = iova_tree_find(vtd_as->iova_tree, &target);
+    if (!mapped) {
+        /* To make UNMAP notifier work, we need build iova tree here
+         * in order to have the UNMAP iommu notifier to be triggered
+         * during the page walk.
+         */
+        iova_tree_insert(vtd_as->iova_tree, &target);
+    }
+
     return true;
 
 error:
@@ -2161,31 +2178,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                        vtd_as->devfn, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            if (vtd_as_has_map_notifier(vtd_as)) {
-                /*
-                 * As long as we have MAP notifications registered in
-                 * any of our IOMMU notifiers, we need to sync the
-                 * shadow page table.
-                 */
-                vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
-            } else {
-                /*
-                 * For UNMAP-only notifiers, we don't need to walk the
-                 * page tables.  We just deliver the PSI down to
-                 * invalidate caches.
-                 */
-                IOMMUTLBEvent event = {
-                    .type = IOMMU_NOTIFIER_UNMAP,
-                    .entry = {
-                        .target_as = &address_space_memory,
-                        .iova = addr,
-                        .translated_addr = 0,
Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, · line 2187

Quoted 5 lines
hw/i386/intel_iommu.c +0 -5
-                        .addr_mask = size - 1,
-                        .perm = IOMMU_NONE,
-                    },
-                };
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);

Isn't this path the one that will be responsible for pass-through the UNMAP events from guest to vhost when there's no MAP notifier requested?

Yes, but it doesn't do the iova tree removing. More below.

Quoted 3 lines

At least that's what I expected when introducing the iova tree, because for unmap-only device hierachy I thought we didn't need the tree at all.

Then the problem is the UNMAP notifier won't be trigger at all during DSI page walk in vtd_page_walk_one() because there's no DMAMap stored in the iova tree.:

Quoted 8 lines
        if (!mapped) {
            /* Skip since we didn't map this range at all */
            trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
            return 0;
        }

So I choose to build the iova tree in translate then we won't go within the above condition.

That's also why it's weird because IIUC we should never walk a page table at all if there's no MAP notifier regiestered.

When I'm looking at the walk callers I found that indeed there's one path missing where can cause it to actually walk the pgtables without !MAP, then I also noticed commit f7701e2c7983b6, and I'm wondering what we really want is something like this:

hw/i386/intel_iommu.c +1 -1
index a08ee85edf..c46f3db992 100644
@@ -1536,7 +1536,7 @@ static int vtd_sync_shadow_page_table(VTDAddressSpace *vtd_as)
     VTDContextEntry ce;
     IOMMUNotifier *n;
 
-    if (!(vtd_as->iommu.iommu_notify_flags & IOMMU_NOTIFIER_IOTLB_EVENTS)) {
+    if (!vtd_as_has_map_notifier(vtd_as)) {
         return 0;
     }
 

So I'm not sure whether this patch is the problem resolver; so far I feel like it's patch 2 who does the real fix. Then we can have the above oneliner so we stop any walks when there's no map notifiers.

Thanks,

Signature · 1 line
Peter Xu
author
#19 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

Quoted 110 lines · Peter Xu
Quoted 108 lines · Jason Wang
Quoted 82 lines · Peter Xu
Quoted 80 lines · Jason Wang

The IOVA tree is only built during page walk this breaks the device that tries to use UNMAP notifier only. One example is vhost-net, it tries to use UNMAP notifier when vIOMMU doesn't support DEVIOTLB_UNMAP notifier (e.g when dt mode is not enabled). The interesting part is that it doesn't use MAP since it can query the IOMMU translation by itself upon a IOTLB miss.

This doesn't work since Qemu doesn't build IOVA tree in IOMMU translation which means the UNMAP notifier won't be triggered during the page walk since Qemu think it is never mapped. This could be noticed when vIOMMU is used with vhost_net but dt is disabled.

Fixing this by build the iova tree during IOMMU translation, this makes sure the UNMAP notifier event could be identified during page walk. And we need to walk page table not only for UNMAP notifier but for MAP notifier during PSI.

---
 hw/i386/intel_iommu.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)
hw/i386/intel_iommu.c +17 -19
index d025ef2873..edeb62f4b2 100644
@@ -1834,6 +1834,8 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     uint8_t access_flags;
     bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
     VTDIOTLBEntry *iotlb_entry;
+    const DMAMap *mapped;
+    DMAMap target;
 
     /*
      * We have standalone memory region for interrupt addresses, we
@@ -1954,6 +1956,21 @@ out:
     entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
     entry->addr_mask = ~page_mask;
     entry->perm = access_flags;
+
+    target.iova = entry->iova;
+    target.size = entry->addr_mask;
+    target.translated_addr = entry->translated_addr;
+    target.perm = entry->perm;
+
+    mapped = iova_tree_find(vtd_as->iova_tree, &target);
+    if (!mapped) {
+        /* To make UNMAP notifier work, we need build iova tree here
+         * in order to have the UNMAP iommu notifier to be triggered
+         * during the page walk.
+         */
+        iova_tree_insert(vtd_as->iova_tree, &target);
+    }
+
     return true;
 
 error:
@@ -2161,31 +2178,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                        vtd_as->devfn, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            if (vtd_as_has_map_notifier(vtd_as)) {
-                /*
-                 * As long as we have MAP notifications registered in
-                 * any of our IOMMU notifiers, we need to sync the
-                 * shadow page table.
-                 */
-                vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
-            } else {
-                /*
-                 * For UNMAP-only notifiers, we don't need to walk the
-                 * page tables.  We just deliver the PSI down to
-                 * invalidate caches.
-                 */
-                IOMMUTLBEvent event = {
-                    .type = IOMMU_NOTIFIER_UNMAP,
-                    .entry = {
-                        .target_as = &address_space_memory,
-                        .iova = addr,
-                        .translated_addr = 0,
Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, · line 2187

Quoted 5 lines
hw/i386/intel_iommu.c +0 -5
-                        .addr_mask = size - 1,
-                        .perm = IOMMU_NONE,
-                    },
-                };
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);

Isn't this path the one that will be responsible for pass-through the UNMAP events from guest to vhost when there's no MAP notifier requested?

Yes, but it doesn't do the iova tree removing. More below.

Quoted 3 lines

At least that's what I expected when introducing the iova tree, because for unmap-only device hierachy I thought we didn't need the tree at all.

Then the problem is the UNMAP notifier won't be trigger at all during DSI page walk in vtd_page_walk_one() because there's no DMAMap stored in the iova tree.:

        if (!mapped) {
            /* Skip since we didn't map this range at all */
            trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
            return 0;
        }
Quoted 5 lines
Quoted 2 lines

So I choose to build the iova tree in translate then we won't go within the above condition.

That's also why it's weird because IIUC we should never walk a page table at all if there's no MAP notifier regiestered.

If this is true, we probably need to document this somewhere.

Quoted 23 lines

When I'm looking at the walk callers I found that indeed there's one path missing where can cause it to actually walk the pgtables without !MAP, then I also noticed commit f7701e2c7983b6, and I'm wondering what we really want is something like this:

hw/i386/intel_iommu.c +1 -1
index a08ee85edf..c46f3db992 100644
@@ -1536,7 +1536,7 @@ static int vtd_sync_shadow_page_table(VTDAddressSpace *vtd_as)
     VTDContextEntry ce;
     IOMMUNotifier *n;
 
-    if (!(vtd_as->iommu.iommu_notify_flags & IOMMU_NOTIFIER_IOTLB_EVENTS)) {
+    if (!vtd_as_has_map_notifier(vtd_as)) {
         return 0;
     }
 

So I'm not sure whether this patch is the problem resolver; so far I feel like it's patch 2 who does the real fix. Then we can have the above oneliner so we stop any walks when there's no map notifiers.

Quoted 1 line

Thanks,

I may miss something but as state above, the problem is a missing UNMAP notification during DSI when there's only UNMAP notifier.

To solve it we might have two ways:

1) build the iova tree during iommu translation then we can correctly trigger UNMAP during page walk caused by DSI
2) don't do the iova tree walk for !MAP notifier, need new logic to trigger UNMAP notifier in PSI/DSI

This patch choose to go 1) (which seems easier at least for -stable). Do you mean you prefer to go with 2)?

Thanks

Quoted 4 lines

--
Peter Xu

#20 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

Quoted 83 lines · Jason Wang
Quoted 82 lines · Peter Xu
Quoted 80 lines · Jason Wang
Quoted 79 lines · Peter Xu
Quoted 77 lines · Jason Wang

The IOVA tree is only built during page walk this breaks the device that tries to use UNMAP notifier only. One example is vhost-net, it tries to use UNMAP notifier when vIOMMU doesn't support DEVIOTLB_UNMAP notifier (e.g when dt mode is not enabled). The interesting part is that it doesn't use MAP since it can query the IOMMU translation by itself upon a IOTLB miss.

This doesn't work since Qemu doesn't build IOVA tree in IOMMU translation which means the UNMAP notifier won't be triggered during the page walk since Qemu think it is never mapped. This could be noticed when vIOMMU is used with vhost_net but dt is disabled.

Fixing this by build the iova tree during IOMMU translation, this makes sure the UNMAP notifier event could be identified during page walk. And we need to walk page table not only for UNMAP notifier but for MAP notifier during PSI.

---
 hw/i386/intel_iommu.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)
hw/i386/intel_iommu.c +17 -16
index d025ef2873..edeb62f4b2 100644
@@ -1834,6 +1834,8 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     uint8_t access_flags;
     bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
     VTDIOTLBEntry *iotlb_entry;
+    const DMAMap *mapped;
+    DMAMap target;
 
     /*
      * We have standalone memory region for interrupt addresses, we
@@ -1954,6 +1956,21 @@ out:
     entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
     entry->addr_mask = ~page_mask;
     entry->perm = access_flags;
+
+    target.iova = entry->iova;
+    target.size = entry->addr_mask;
+    target.translated_addr = entry->translated_addr;
+    target.perm = entry->perm;
+
+    mapped = iova_tree_find(vtd_as->iova_tree, &target);
+    if (!mapped) {
+        /* To make UNMAP notifier work, we need build iova tree here
+         * in order to have the UNMAP iommu notifier to be triggered
+         * during the page walk.
+         */
+        iova_tree_insert(vtd_as->iova_tree, &target);
+    }
+
     return true;
 
 error:
@@ -2161,31 +2178,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                        vtd_as->devfn, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            if (vtd_as_has_map_notifier(vtd_as)) {
-                /*
-                 * As long as we have MAP notifications registered in
-                 * any of our IOMMU notifiers, we need to sync the
-                 * shadow page table.
-                 */
-                vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
-            } else {
-                /*
-                 * For UNMAP-only notifiers, we don't need to walk the
-                 * page tables.  We just deliver the PSI down to
-                 * invalidate caches.
-                 */
-                IOMMUTLBEvent event = {
-                    .type = IOMMU_NOTIFIER_UNMAP,
-                    .entry = {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, · line 2187

Quoted 8 lines
hw/i386/intel_iommu.c +0 -8
-                        .target_as = &address_space_memory,
-                        .iova = addr,
-                        .translated_addr = 0,
-                        .addr_mask = size - 1,
-                        .perm = IOMMU_NONE,
-                    },
-                };
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);

[1]

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 3 lines

Isn't this path the one that will be responsible for pass-through the UNMAP events from guest to vhost when there's no MAP notifier requested?

Yes, but it doesn't do the iova tree removing. More below.

Quoted 3 lines

At least that's what I expected when introducing the iova tree, because for unmap-only device hierachy I thought we didn't need the tree at all.

Then the problem is the UNMAP notifier won't be trigger at all during DSI page walk in vtd_page_walk_one() because there's no DMAMap stored in the iova tree.:

        if (!mapped) {
            /* Skip since we didn't map this range at all */
            trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
            return 0;
        }
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

So I choose to build the iova tree in translate then we won't go within the above condition.

That's also why it's weird because IIUC we should never walk a page table at all if there's no MAP notifier regiestered.

If this is true, we probably need to document this somewhere.

Agree. I'll post a patch.

Quoted 20 lines
Quoted 20 lines

When I'm looking at the walk callers I found that indeed there's one path missing where can cause it to actually walk the pgtables without !MAP, then I also noticed commit f7701e2c7983b6, and I'm wondering what we really want is something like this:

hw/i386/intel_iommu.c +1 -1
index a08ee85edf..c46f3db992 100644
@@ -1536,7 +1536,7 @@ static int vtd_sync_shadow_page_table(VTDAddressSpace *vtd_as)
     VTDContextEntry ce;
     IOMMUNotifier *n;
 
-    if (!(vtd_as->iommu.iommu_notify_flags & IOMMU_NOTIFIER_IOTLB_EVENTS)) {
+    if (!vtd_as_has_map_notifier(vtd_as)) {
         return 0;
     }
 
Quoted 8 lines
Quoted 5 lines

So I'm not sure whether this patch is the problem resolver; so far I feel like it's patch 2 who does the real fix. Then we can have the above oneliner so we stop any walks when there's no map notifiers.

Thanks,

I may miss something but as state above, the problem is a missing UNMAP notification during DSI when there's only UNMAP notifier.

I got confused too on why we didn't notify UNMAP for DSI already, that's so weird because I thought it should be there or it should be broken for a long time.. as we discussed multiple times around this one:

		/*
		 * Fallback to domain selective flush if no PSI support or
		 * the size is too big.
		 */
		if (!cap_pgsel_inv(iommu->cap) ||
		    mask > cap_max_amask_val(iommu->cap))
			iommu->flush.flush_iotlb(iommu, did, 0, 0,
							DMA_TLB_DSI_FLUSH);
		else
			iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
							DMA_TLB_PSI_FLUSH);

I guess we were just always lucky?..

Quoted 10 lines

To solve it we might have two ways:

1) build the iova tree during iommu translation then we can correctly trigger UNMAP during page walk caused by DSI
2) don't do the iova tree walk for !MAP notifier, need new logic to trigger UNMAP notifier in PSI/DSI

This patch choose to go 1) (which seems easier at least for -stable). Do you mean you prefer to go with 2)?

Yes.

IOVA tree is unnecessary overhead IMHO because UNMAP (both IOTLB or DEVIOTLB unmap) shouldn't need that complexity at all. Using the iova tree can be accurate on which page got unmapped when the kernel driver used DSI for a large PSI as shown above, however IMHO it needs more justification that the pgtable walk is worth the effort. Not to mention if a device in QEMU that wants to use the iova tree for some reason, one can just register with MAP and ignore all MAP events, while by default we keep UNMAP simple.

So we could do:

(1) Rename vtd_sync_shadow_page_table() to vtd_sync_domain()
(2) instead of optimizing dev-iotlb only there:

if (!(vtd_as->iommu.iommu_notify_flags & IOMMU_NOTIFIER_IOTLB_EVENTS)) {
return 0;
}

we should firstly check if UNMAP or DEVUNMAP registered, we directly send a notification to the whole domain. We need to choose the event that the register happens with but not both.

This also reminded me that whether we should sanity check on iommu notifiers on some invalid cases. E.g. it seems to me when registered with DEVIOTLB_UNMAP it should not register with either MAP or UNMAP anymore or it doesn't make sense.

One step further, I'm wondering whether the DEV_IOTLB event should exist at all. Maybe we want to have DEV_IOTLB typed iommu notifier only, but then when we got dev-iotlb PSI/DSI we notify with type=UNMAP just like normal UNMAP events, then in above (2) we can send UNMAP constantly as long as !MAP. But even if so that'll just be another optional change on top.

Thanks,

Signature · 1 line
Peter Xu
author
#21 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

`

Quoted 171 lines · Peter Xu
Quoted 83 lines · Jason Wang
Quoted 82 lines · Peter Xu
Quoted 80 lines · Jason Wang
Quoted 79 lines · Peter Xu
Quoted 77 lines · Jason Wang

The IOVA tree is only built during page walk this breaks the device that tries to use UNMAP notifier only. One example is vhost-net, it tries to use UNMAP notifier when vIOMMU doesn't support DEVIOTLB_UNMAP notifier (e.g when dt mode is not enabled). The interesting part is that it doesn't use MAP since it can query the IOMMU translation by itself upon a IOTLB miss.

This doesn't work since Qemu doesn't build IOVA tree in IOMMU translation which means the UNMAP notifier won't be triggered during the page walk since Qemu think it is never mapped. This could be noticed when vIOMMU is used with vhost_net but dt is disabled.

Fixing this by build the iova tree during IOMMU translation, this makes sure the UNMAP notifier event could be identified during page walk. And we need to walk page table not only for UNMAP notifier but for MAP notifier during PSI.

---
 hw/i386/intel_iommu.c | 43 ++++++++++++++++++-------------------------
 1 file changed, 18 insertions(+), 25 deletions(-)
hw/i386/intel_iommu.c +17 -16
index d025ef2873..edeb62f4b2 100644
@@ -1834,6 +1834,8 @@ static bool vtd_do_iommu_translate(VTDAddressSpace *vtd_as, PCIBus *bus,
     uint8_t access_flags;
     bool rid2pasid = (pasid == PCI_NO_PASID) && s->root_scalable;
     VTDIOTLBEntry *iotlb_entry;
+    const DMAMap *mapped;
+    DMAMap target;
 
     /*
      * We have standalone memory region for interrupt addresses, we
@@ -1954,6 +1956,21 @@ out:
     entry->translated_addr = vtd_get_slpte_addr(slpte, s->aw_bits) & page_mask;
     entry->addr_mask = ~page_mask;
     entry->perm = access_flags;
+
+    target.iova = entry->iova;
+    target.size = entry->addr_mask;
+    target.translated_addr = entry->translated_addr;
+    target.perm = entry->perm;
+
+    mapped = iova_tree_find(vtd_as->iova_tree, &target);
+    if (!mapped) {
+        /* To make UNMAP notifier work, we need build iova tree here
+         * in order to have the UNMAP iommu notifier to be triggered
+         * during the page walk.
+         */
+        iova_tree_insert(vtd_as->iova_tree, &target);
+    }
+
     return true;
 
 error:
@@ -2161,31 +2178,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
         ret = vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                        vtd_as->devfn, &ce);
         if (!ret && domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            if (vtd_as_has_map_notifier(vtd_as)) {
-                /*
-                 * As long as we have MAP notifications registered in
-                 * any of our IOMMU notifiers, we need to sync the
-                 * shadow page table.
-                 */
-                vtd_sync_shadow_page_table_range(vtd_as, &ce, addr, size);
-            } else {
-                /*
-                 * For UNMAP-only notifiers, we don't need to walk the
-                 * page tables.  We just deliver the PSI down to
-                 * invalidate caches.
-                 */
-                IOMMUTLBEvent event = {
-                    .type = IOMMU_NOTIFIER_UNMAP,
-                    .entry = {
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

hw/i386/intel_iommu.c static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s, · line 2187

Quoted 8 lines
hw/i386/intel_iommu.c +0 -8
-                        .target_as = &address_space_memory,
-                        .iova = addr,
-                        .translated_addr = 0,
-                        .addr_mask = size - 1,
-                        .perm = IOMMU_NONE,
-                    },
-                };
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);

[1]

Quoted 20 lines
Quoted 20 lines
Quoted 20 lines
Quoted 3 lines

Isn't this path the one that will be responsible for pass-through the UNMAP events from guest to vhost when there's no MAP notifier requested?

Yes, but it doesn't do the iova tree removing. More below.

Quoted 3 lines

At least that's what I expected when introducing the iova tree, because for unmap-only device hierachy I thought we didn't need the tree at all.

Then the problem is the UNMAP notifier won't be trigger at all during DSI page walk in vtd_page_walk_one() because there's no DMAMap stored in the iova tree.:

        if (!mapped) {
            /* Skip since we didn't map this range at all */
            trace_vtd_page_walk_one_skip_unmap(entry->iova, entry->addr_mask);
            return 0;
        }
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

So I choose to build the iova tree in translate then we won't go within the above condition.

That's also why it's weird because IIUC we should never walk a page table at all if there's no MAP notifier regiestered.

If this is true, we probably need to document this somewhere.

Agree. I'll post a patch.

Quoted 20 lines
Quoted 20 lines

When I'm looking at the walk callers I found that indeed there's one path missing where can cause it to actually walk the pgtables without !MAP, then I also noticed commit f7701e2c7983b6, and I'm wondering what we really want is something like this:

hw/i386/intel_iommu.c +1 -1
index a08ee85edf..c46f3db992 100644
@@ -1536,7 +1536,7 @@ static int vtd_sync_shadow_page_table(VTDAddressSpace *vtd_as)
     VTDContextEntry ce;
     IOMMUNotifier *n;
 
-    if (!(vtd_as->iommu.iommu_notify_flags & IOMMU_NOTIFIER_IOTLB_EVENTS)) {
+    if (!vtd_as_has_map_notifier(vtd_as)) {
         return 0;
     }
 
Quoted 8 lines
Quoted 5 lines

So I'm not sure whether this patch is the problem resolver; so far I feel like it's patch 2 who does the real fix. Then we can have the above oneliner so we stop any walks when there's no map notifiers.

Thanks,

I may miss something but as state above, the problem is a missing UNMAP notification during DSI when there's only UNMAP notifier.

I got confused too on why we didn't notify UNMAP for DSI already, that's so weird because I thought it should be there or it should be broken for a long time.. as we discussed multiple times around this one:

                /*
                 * Fallback to domain selective flush if no PSI support or
                 * the size is too big.
                 */
                if (!cap_pgsel_inv(iommu->cap) ||
                    mask > cap_max_amask_val(iommu->cap))
                        iommu->flush.flush_iotlb(iommu, did, 0, 0,
                                                        DMA_TLB_DSI_FLUSH);
                else
                        iommu->flush.flush_iotlb(iommu, did, addr | ih, mask,
                                                        DMA_TLB_PSI_FLUSH);
Quoted 1 line

I guess we were just always lucky?..

Probably, or the reason is that we a notifier with UNMAP only is not commonly used before until patch 2.

Quoted 31 lines
Quoted 11 lines

To solve it we might have two ways:

1) build the iova tree during iommu translation then we can correctly trigger UNMAP during page walk caused by DSI
2) don't do the iova tree walk for !MAP notifier, need new logic to trigger UNMAP notifier in PSI/DSI

This patch choose to go 1) (which seems easier at least for -stable). Do you mean you prefer to go with 2)?

Yes.

IOVA tree is unnecessary overhead IMHO because UNMAP (both IOTLB or DEVIOTLB unmap) shouldn't need that complexity at all. Using the iova tree can be accurate on which page got unmapped when the kernel driver used DSI for a large PSI as shown above, however IMHO it needs more justification that the pgtable walk is worth the effort. Not to mention if a device in QEMU that wants to use the iova tree for some reason, one can just register with MAP and ignore all MAP events, while by default we keep UNMAP simple.

So we could do:

(1) Rename vtd_sync_shadow_page_table() to vtd_sync_domain()
(2) instead of optimizing dev-iotlb only there:

if (!(vtd_as->iommu.iommu_notify_flags & IOMMU_NOTIFIER_IOTLB_EVENTS)) {
return 0;
}

Quoted 8 lines

we should firstly check if UNMAP or DEVUNMAP registered, we directly send a notification to the whole domain. We need to choose the event that the register happens with but not both.

This also reminded me that whether we should sanity check on iommu notifiers on some invalid cases. E.g. it seems to me when registered with DEVIOTLB_UNMAP it should not register with either MAP or UNMAP anymore or it doesn't make sense.

It seems it doesn't' harm to allow both UNMAP and DEVIOTLB_UNMAP work.

Quoted 3 lines

One step further, I'm wondering whether the DEV_IOTLB event should exist at all. Maybe we want to have DEV_IOTLB typed iommu notifier only,

This seems cleaner ( I remember we had some discussion before ).

Quoted 4 lines

but then
when we got dev-iotlb PSI/DSI we notify with type=UNMAP just like normal UNMAP events, then in above (2) we can send UNMAP constantly as long as !MAP. But even if so that'll just be another optional change on top.

I'm fine to go without iova-tree. Would you mind to post patches for fix? I can test and include it in this series then.

Thanks

Quoted 6 lines

Thanks,

--
Peter Xu

#22 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

Jason,

Quoted 2 lines · Jason Wang

I'm fine to go without iova-tree. Would you mind to post patches for fix? I can test and include it in this series then.

One sample patch attached, only compile tested.

I can also work on this but I'll be slow in making progress, so I'll add it into my todo. If you can help to fix this issue it'll be more than great. No worry on the ownership or authorship of the patch if you agree on the change and moving forward with this when modifying - just take it over!

Thanks!

Signature · 1 line
Peter Xu

From 57e5cab805c94d56f801a7e21098389a77584e34 Mon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Mon, 5 Dec 2022 11:14:02 -0500
Subject: [PATCH] memory: sanity check flatview deref on mr transactions
Content-type: text/plain

---
 include/exec/memory.h | 9 +++++++++
 softmmu/memory.c      | 2 +-
 2 files changed, 10 insertions(+), 1 deletion(-)
include/exec/memory.h +9 -0
index 91f8a2395a..e136ab9558 100644
@@ -1069,8 +1069,17 @@ struct FlatView {
     MemoryRegion *root;
 };
 
+extern unsigned memory_region_transaction_depth;
+
 static inline FlatView *address_space_to_flatview(AddressSpace *as)
 {
+    /*
+     * Before using any flatview, sanity check we're not during a memory
+     * region transaction or the map can be invalid.  Note that this can
+     * also be called during commit phase of memory transaction, but that
+     * should also only happen when the depth decreases to 0 first.
+     */
+    assert(memory_region_transaction_depth == 0);
     return qatomic_rcu_read(&as->current_map);
 }
 
softmmu/memory.c +1 -1
index bc0be3f62c..7cfcf5dffe 100644
@@ -37,7 +37,7 @@
 
 //#define DEBUG_UNASSIGNED
 
-static unsigned memory_region_transaction_depth;
+unsigned memory_region_transaction_depth;
 static bool memory_region_update_pending;
 static bool ioeventfd_update_pending;
 unsigned int global_dirty_tracking;
Signature · 1 line
2.37.3
author
#23 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

Quoted 8 lines · Peter Xu

Jason,

Quoted 2 lines · Jason Wang

I'm fine to go without iova-tree. Would you mind to post patches for fix? I can test and include it in this series then.

One sample patch attached, only compile tested.

I don't see any direct connection between the attached patch and the intel-iommu?

Quoted 3 lines

I can also work on this but I'll be slow in making progress, so I'll add it into my todo. If you can help to fix this issue it'll be more than great.

Ok, let me try but it might take some time :)

Quoted 2 lines

No worry on the ownership or authorship of the patch if you agree on the change and moving forward with this when modifying - just take it over!

Ok.

Thanks

Quoted 5 lines

Thanks!

--
Peter Xu

#24 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

Quoted 4 lines · Jason Wang
Quoted 3 lines · Peter Xu

Jason,

Quoted 8 lines
Quoted 5 lines · Jason Wang
Quoted 2 lines

I'm fine to go without iova-tree. Would you mind to post patches for fix? I can test and include it in this series then.

One sample patch attached, only compile tested.

I don't see any direct connection between the attached patch and the intel-iommu?

Sorry! Wrong tree dumped... Trying again.

Quoted 6 lines
Quoted 4 lines

I can also work on this but I'll be slow in making progress, so I'll add it into my todo. If you can help to fix this issue it'll be more than great.

Ok, let me try but it might take some time :)

Sure. :)

I'll also add it into my todo (and I think the other similar one has been there for a while.. :( ).

Signature · 1 line
Peter Xu

From 37c743761d20c16891856c5bef2e7b3fb89893b6 Mon Sep 17 00:00:00 2001
From: Peter Xu <[email protected]>
Date: Mon, 5 Dec 2022 18:11:36 -0500
Subject: [PATCH] intel-iommu: Send unmap notifications for domain or global
inv desc
Content-type: text/plain

---
 hw/i386/intel_iommu.c | 41 +++++++++++++++++++++++++----------------
 1 file changed, 25 insertions(+), 16 deletions(-)
hw/i386/intel_iommu.c +25 -16
index a08ee85edf..2c6ca68df0 100644
@@ -206,6 +206,23 @@ static inline gboolean vtd_as_has_map_notifier(VTDAddressSpace *as)
     return as->notifier_flags & IOMMU_NOTIFIER_MAP;
 }
 
+static void vtd_as_notify_unmap(VTDAddressSpace *as, hwaddr iova,
+                                hwaddr addr_mask)
+{
+    IOMMUTLBEvent event = {
+        .type = IOMMU_NOTIFIER_UNMAP,
+        .entry = {
+            .target_as = &address_space_memory,
+            .iova = iova,
+            .translated_addr = 0,
+            .addr_mask = addr_mask,
+            .perm = IOMMU_NONE,
+        },
+    };
+
+    memory_region_notify_iommu(&as->iommu, 0, event);
+}
+
 /* GHashTable functions */
 static gboolean vtd_iotlb_equal(gconstpointer v1, gconstpointer v2)
 {
@@ -1530,13 +1547,15 @@ static int vtd_sync_shadow_page_table_range(VTDAddressSpace *vtd_as,
     return vtd_page_walk(s, ce, addr, addr + size, &info, vtd_as->pasid);
 }
 
-static int vtd_sync_shadow_page_table(VTDAddressSpace *vtd_as)
+static int vtd_address_space_sync(VTDAddressSpace *vtd_as)
 {
     int ret;
     VTDContextEntry ce;
     IOMMUNotifier *n;
 
-    if (!(vtd_as->iommu.iommu_notify_flags & IOMMU_NOTIFIER_IOTLB_EVENTS)) {
+    /* If no MAP notifier registered, we simply invalidate all the cache */
+    if (!vtd_as_has_map_notifier(vtd_as)) {
+        vtd_as_notify_unmap(vtd_as, 0, HWADDR_MAX);
         return 0;
     }
 
@@ -2000,7 +2019,7 @@ static void vtd_iommu_replay_all(IntelIOMMUState *s)
     VTDAddressSpace *vtd_as;
 
     QLIST_FOREACH(vtd_as, &s->vtd_as_with_notifiers, next) {
-        vtd_sync_shadow_page_table(vtd_as);
+        vtd_address_space_sync(vtd_as);
     }
 }
 
@@ -2082,7 +2101,7 @@ static void vtd_context_device_invalidate(IntelIOMMUState *s,
              * framework will skip MAP notifications if that
              * happened.
              */
-            vtd_sync_shadow_page_table(vtd_as);
+            vtd_address_space_sync(vtd_as);
         }
     }
 }
@@ -2140,7 +2159,7 @@ static void vtd_iotlb_domain_invalidate(IntelIOMMUState *s, uint16_t domain_id)
         if (!vtd_dev_to_context_entry(s, pci_bus_num(vtd_as->bus),
                                       vtd_as->devfn, &ce) &&
             domain_id == vtd_get_domain_id(s, &ce, vtd_as->pasid)) {
-            vtd_sync_shadow_page_table(vtd_as);
+            vtd_address_space_sync(vtd_as);
         }
     }
 }
@@ -2174,17 +2193,7 @@ static void vtd_iotlb_page_invalidate_notify(IntelIOMMUState *s,
                  * page tables.  We just deliver the PSI down to
                  * invalidate caches.
                  */
-                IOMMUTLBEvent event = {
-                    .type = IOMMU_NOTIFIER_UNMAP,
-                    .entry = {
-                        .target_as = &address_space_memory,
-                        .iova = addr,
-                        .translated_addr = 0,
-                        .addr_mask = size - 1,
-                        .perm = IOMMU_NONE,
-                    },
-                };
-                memory_region_notify_iommu(&vtd_as->iommu, 0, event);
+                vtd_as_notify_unmap(vtd_as, addr, size - 1);
             }
         }
     }
Signature · 1 line
2.37.3
author
#25 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

Quoted 10 lines · Peter Xu
Quoted 8 lines · Jason Wang
Quoted 7 lines · Peter Xu

Jason,

Quoted 2 lines · Jason Wang

I'm fine to go without iova-tree. Would you mind to post patches for fix? I can test and include it in this series then.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

One sample patch attached, only compile tested.

I don't see any direct connection between the attached patch and the intel-iommu?

Sorry! Wrong tree dumped... Trying again.

The HWADDR breaks memory_region_notify_iommu_one():

qemu-system-x86_64: ../softmmu/memory.c:1991:
memory_region_notify_iommu_one: Assertion `entry->iova >= notifier->start && entry_end <= notifier->end' failed.

I wonder if we need either:

1) remove the assert

or

2) introduce a new memory_region_notify_unmap_all() to unmap from notifier->start to notifier->end.

Thanks

Quoted 15 lines
Quoted 7 lines
Quoted 5 lines

I can also work on this but I'll be slow in making progress, so I'll add it into my todo. If you can help to fix this issue it'll be more than great.

Ok, let me try but it might take some time :)

Sure. :)

I'll also add it into my todo (and I think the other similar one has been there for a while.. :( ).

--
Peter Xu

#26 lore raw

Re: [PATCH 3/3] intel-iommu: build iova tree during IOMMU translation

Quoted 20 lines · Jason Wang
Quoted 10 lines · Peter Xu
Quoted 8 lines · Jason Wang
Quoted 7 lines · Peter Xu

Jason,

Quoted 2 lines · Jason Wang

I'm fine to go without iova-tree. Would you mind to post patches for fix? I can test and include it in this series then.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

One sample patch attached, only compile tested.

I don't see any direct connection between the attached patch and the intel-iommu?

Sorry! Wrong tree dumped... Trying again.

The HWADDR breaks memory_region_notify_iommu_one():

Quoted 7 lines

qemu-system-x86_64: ../softmmu/memory.c:1991:
memory_region_notify_iommu_one: Assertion `entry->iova >= notifier->start && entry_end <= notifier->end' failed.

I wonder if we need either:

1) remove the assert

I vote for this one. Not only removing the assertion, we should probably crop the range too just like dev-iotlb unmaps?

Thanks,

Quoted 5 lines

or

2) introduce a new memory_region_notify_unmap_all() to unmap from notifier->start to notifier->end.

Signature · 1 line
Peter Xu
#27 lore raw
In reply to Jason Wang
Quoted 7 lines · Jason Wang

Hi All:

According to ATS, device should work if ATS is disabled. This is not correctly implemented in the current intel-iommu since it doesn't handle the UNMAP notifier correctly. This breaks the vhost-net + vIOMMU without dt.

Quoted 8 lines

The root casue is that the when there's a device IOTLB miss (note that it's not specific to PCI so it can work without ATS), Qemu doesn't build the IOVA tree, so when guest start an IOTLB invalidation, Qemu won't trigger the UNMAP notifier.

Fixing by build IOVA tree during IOMMU translsation.

Thanks

Any changes of Fixes tags? this is 8.0 yes?

Quoted 10 lines

Jason Wang (3):
intel-iommu: fail MAP notifier without caching mode
intel-iommu: fail DEVIOTLB_UNMAP without dt mode
intel-iommu: build iova tree during IOMMU translation

 hw/i386/intel_iommu.c | 58 ++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 25 deletions(-)
Signature · 1 line
2.25.1
author
#28 lore raw
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Jason Wang

Hi All:

According to ATS, device should work if ATS is disabled. This is not correctly implemented in the current intel-iommu since it doesn't handle the UNMAP notifier correctly. This breaks the vhost-net + vIOMMU without dt.

The root casue is that the when there's a device IOTLB miss (note that it's not specific to PCI so it can work without ATS), Qemu doesn't build the IOVA tree, so when guest start an IOTLB invalidation, Qemu won't trigger the UNMAP notifier.

Quoted 5 lines
Quoted 3 lines

Fixing by build IOVA tree during IOMMU translsation.

Thanks

Any changes of Fixes tags? this is 8.0 yes?

Yes, it's for 8.0.

Thanks

Quoted 12 lines
Quoted 11 lines

Jason Wang (3):
intel-iommu: fail MAP notifier without caching mode
intel-iommu: fail DEVIOTLB_UNMAP without dt mode
intel-iommu: build iova tree during IOMMU translation

 hw/i386/intel_iommu.c | 58 ++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 25 deletions(-)

--
2.25.1

#29 lore raw
In reply to Jason Wang
Quoted 7 lines · Jason Wang

Hi All:

According to ATS, device should work if ATS is disabled. This is not correctly implemented in the current intel-iommu since it doesn't handle the UNMAP notifier correctly. This breaks the vhost-net + vIOMMU without dt.

Quoted 8 lines

The root casue is that the when there's a device IOTLB miss (note that it's not specific to PCI so it can work without ATS), Qemu doesn't build the IOVA tree, so when guest start an IOTLB invalidation, Qemu won't trigger the UNMAP notifier.

Fixing by build IOVA tree during IOMMU translsation.

Thanks

IIUC you were going to post v2? At least commit log fixes.

Quoted 10 lines

Jason Wang (3):
intel-iommu: fail MAP notifier without caching mode
intel-iommu: fail DEVIOTLB_UNMAP without dt mode
intel-iommu: build iova tree during IOMMU translation

 hw/i386/intel_iommu.c | 58 ++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 25 deletions(-)
Signature · 1 line
2.25.1
author
#30 lore raw
Quoted 14 lines · Michael S. Tsirkin
Quoted 12 lines · Jason Wang

Hi All:

According to ATS, device should work if ATS is disabled. This is not correctly implemented in the current intel-iommu since it doesn't handle the UNMAP notifier correctly. This breaks the vhost-net + vIOMMU without dt.

The root casue is that the when there's a device IOTLB miss (note that it's not specific to PCI so it can work without ATS), Qemu doesn't build the IOVA tree, so when guest start an IOTLB invalidation, Qemu won't trigger the UNMAP notifier.

Quoted 6 lines
Quoted 3 lines

Fixing by build IOVA tree during IOMMU translsation.

Thanks

IIUC you were going to post v2? At least commit log fixes.

Yes.

Thanks

Quoted 12 lines
Quoted 11 lines

Jason Wang (3):
intel-iommu: fail MAP notifier without caching mode
intel-iommu: fail DEVIOTLB_UNMAP without dt mode
intel-iommu: build iova tree during IOMMU translation

 hw/i386/intel_iommu.c | 58 ++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 25 deletions(-)

--
2.25.1

Tested-by on the v1 series
#31 lore raw
In reply to Jason Wang
Quoted 22 lines · Jason Wang

Hi All:

According to ATS, device should work if ATS is disabled. This is not correctly implemented in the current intel-iommu since it doesn't handle the UNMAP notifier correctly. This breaks the vhost-net + vIOMMU without dt.

The root casue is that the when there's a device IOTLB miss (note that it's not specific to PCI so it can work without ATS), Qemu doesn't build the IOVA tree, so when guest start an IOTLB invalidation, Qemu won't trigger the UNMAP notifier.

Fixing by build IOVA tree during IOMMU translsation.

Thanks

Jason Wang (3):
intel-iommu: fail MAP notifier without caching mode
intel-iommu: fail DEVIOTLB_UNMAP without dt mode
intel-iommu: build iova tree during IOMMU translation

Quoted 6 lines
 hw/i386/intel_iommu.c | 58 ++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 25 deletions(-)

--
2.25.1

Hi Jason,

I've tried the series with Windows Server 2022 guest with vhost and intel-iommu (device-iotlb=off) and now networking on this system has become working.
So, as we discussed, I'm waiting for the series to be accepted in some form to continue my work about supporting guests who refuse Device-TLB on systems with device-iotlb=on.

Best regards,
Viktor Prutyanov

author
#32 lore raw
Quoted 33 lines · Viktor Prutyanov
Quoted 22 lines · Jason Wang

Hi All:

According to ATS, device should work if ATS is disabled. This is not correctly implemented in the current intel-iommu since it doesn't handle the UNMAP notifier correctly. This breaks the vhost-net + vIOMMU without dt.

The root casue is that the when there's a device IOTLB miss (note that it's not specific to PCI so it can work without ATS), Qemu doesn't build the IOVA tree, so when guest start an IOTLB invalidation, Qemu won't trigger the UNMAP notifier.

Fixing by build IOVA tree during IOMMU translsation.

Thanks

Jason Wang (3):
intel-iommu: fail MAP notifier without caching mode
intel-iommu: fail DEVIOTLB_UNMAP without dt mode
intel-iommu: build iova tree during IOMMU translation

Quoted 6 lines
 hw/i386/intel_iommu.c | 58 ++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 25 deletions(-)

--
2.25.1

Hi Jason,

Quoted 8 lines

I've tried the series with Windows Server 2022 guest with vhost and intel-iommu (device-iotlb=off) and now networking on this system has become working.
So, as we discussed, I'm waiting for the series to be accepted in some form to continue my work about supporting guests who refuse Device-TLB on systems with device-iotlb=on.

Great, Peter has some comments on this series, so I will probably send a new version (probably after the chinese new year).

Thanks

Quoted 4 lines

Best regards,
Viktor Prutyanov

#33 lore raw
Quoted 41 lines · Jason Wang
Quoted 40 lines · Viktor Prutyanov
Quoted 22 lines · Jason Wang

Hi All:

According to ATS, device should work if ATS is disabled. This is not correctly implemented in the current intel-iommu since it doesn't handle the UNMAP notifier correctly. This breaks the vhost-net + vIOMMU without dt.

The root casue is that the when there's a device IOTLB miss (note that it's not specific to PCI so it can work without ATS), Qemu doesn't build the IOVA tree, so when guest start an IOTLB invalidation, Qemu won't trigger the UNMAP notifier.

Fixing by build IOVA tree during IOMMU translsation.

Thanks

Jason Wang (3):
intel-iommu: fail MAP notifier without caching mode
intel-iommu: fail DEVIOTLB_UNMAP without dt mode
intel-iommu: build iova tree during IOMMU translation

Quoted 6 lines
 hw/i386/intel_iommu.c | 58 ++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 25 deletions(-)

--
2.25.1

Hi Jason,

I've tried the series with Windows Server 2022 guest with vhost and intel-iommu (device-iotlb=off) and now networking on this system has become working.
So, as we discussed, I'm waiting for the series to be accepted in some form to continue my work about supporting guests who refuse Device-TLB on systems with device-iotlb=on.

Quoted 6 lines
Quoted 1 line

Great, Peter has some comments on this series, so I will probably send a new version (probably after the chinese new year).

Thanks

Were you going to post a new version?

Quoted 4 lines
Quoted 4 lines

Best regards,
Viktor Prutyanov

author
#34 lore raw
Quoted 43 lines · Michael S. Tsirkin
Quoted 41 lines · Jason Wang
Quoted 40 lines · Viktor Prutyanov
Quoted 22 lines · Jason Wang

Hi All:

According to ATS, device should work if ATS is disabled. This is not correctly implemented in the current intel-iommu since it doesn't handle the UNMAP notifier correctly. This breaks the vhost-net + vIOMMU without dt.

The root casue is that the when there's a device IOTLB miss (note that it's not specific to PCI so it can work without ATS), Qemu doesn't build the IOVA tree, so when guest start an IOTLB invalidation, Qemu won't trigger the UNMAP notifier.

Fixing by build IOVA tree during IOMMU translsation.

Thanks

Jason Wang (3):
intel-iommu: fail MAP notifier without caching mode
intel-iommu: fail DEVIOTLB_UNMAP without dt mode
intel-iommu: build iova tree during IOMMU translation

Quoted 6 lines
 hw/i386/intel_iommu.c | 58 ++++++++++++++++++++++++-------------------
 1 file changed, 33 insertions(+), 25 deletions(-)

--
2.25.1

Hi Jason,

I've tried the series with Windows Server 2022 guest with vhost and intel-iommu (device-iotlb=off) and now networking on this system has become working.
So, as we discussed, I'm waiting for the series to be accepted in some form to continue my work about supporting guests who refuse Device-TLB on systems with device-iotlb=on.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Great, Peter has some comments on this series, so I will probably send a new version (probably after the chinese new year).

Thanks

Were you going to post a new version?

Yes.

Thanks

Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Best regards,
Viktor Prutyanov