Skip to contentSkip to conversation

memory: Optimize replay of guest mapping

PATCH Superseded Zhenzhong Duan 3 participants 6 messages Apr 13, 2023 – Apr 19, 2023

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 memory: Optimize replay of guest mapping

On x86, there are two notifiers registered due to vtd-ir memory region splitting the entire address space. During replay of the address space for each notifier, the whole address space is scanned which is unnecessary. We only need to scan the space belong to notifier monitored space.

While on x86 IOMMU memory region spans over entire address space, but on some other platforms(e.g. arm mps3-an547), IOMMU memory region is only a window in the whole address space. user could register a notifier with arbitrary scope beyond IOMMU memory region. Though in current implementation replay is only triggered by VFIO and dirty page sync with notifiers derived from memory region section, but this isn't guaranteed in the future.

So, we replay the intersection part of IOMMU memory region and IOMMU notifier in memory_region_iommu_replay().

---
v3: Fix assert failure on mps3-an547
v2: Add an assert per Peter
Tested on x86 with a net card passed to guest(kvm/tcg), ping/ssh pass. Also did simple bootup test with mps3-an547

 hw/i386/intel_iommu.c | 2 +-
 softmmu/memory.c      | 5 +++--
 2 files changed, 4 insertions(+), 3 deletions(-)
hw/i386/intel_iommu.c +1 -1
index a62896759c78..faade7def867 100644
@@ -3850,7 +3850,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
                 .domain_id = vtd_get_domain_id(s, &ce, vtd_as->pasid),
             };
 
-            vtd_page_walk(s, &ce, 0, ~0ULL, &info, vtd_as->pasid);
+            vtd_page_walk(s, &ce, n->start, n->end, &info, vtd_as->pasid);
         }
     } else {
         trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
softmmu/memory.c +3 -2
index b1a6cae6f583..f7af691991de 100644
@@ -1925,7 +1925,7 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
 {
     MemoryRegion *mr = MEMORY_REGION(iommu_mr);
     IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
-    hwaddr addr, granularity;
+    hwaddr addr, end, granularity;
     IOMMUTLBEntry iotlb;
 
     /* If the IOMMU has its own replay callback, override */
@@ -1935,8 +1935,9 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
     }
 
     granularity = memory_region_iommu_get_min_page_size(iommu_mr);
+    end = MIN(n->end, memory_region_size(mr));
 
-    for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
+    for (addr = n->start; addr < end; addr += granularity) {
         iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE, n->iommu_idx);
         if (iotlb.perm != IOMMU_NONE) {
             n->notify(n, &iotlb);
Signature · 1 line
2.25.1
Reviewed-by on v3 1/1
#1 lore raw
Quoted 14 lines · Zhenzhong Duan

On x86, there are two notifiers registered due to vtd-ir memory region splitting the entire address space. During replay of the address space for each notifier, the whole address space is scanned which is unnecessary. We only need to scan the space belong to notifier monitored space.

While on x86 IOMMU memory region spans over entire address space, but on some other platforms(e.g. arm mps3-an547), IOMMU memory region is only a window in the whole address space. user could register a notifier with arbitrary scope beyond IOMMU memory region. Though in current implementation replay is only triggered by VFIO and dirty page sync with notifiers derived from memory region section, but this isn't guaranteed in the future.

Quoted 4 lines

So, we replay the intersection part of IOMMU memory region and IOMMU notifier in memory_region_iommu_replay().

Signature · 1 line
Peter Xu
#2 lore raw
In reply to Zhenzhong Duan
Quoted 56 lines · Zhenzhong Duan

On x86, there are two notifiers registered due to vtd-ir memory region splitting the entire address space. During replay of the address space for each notifier, the whole address space is scanned which is unnecessary. We only need to scan the space belong to notifier monitored space.

While on x86 IOMMU memory region spans over entire address space, but on some other platforms(e.g. arm mps3-an547), IOMMU memory region is only a window in the whole address space. user could register a notifier with arbitrary scope beyond IOMMU memory region. Though in current implementation replay is only triggered by VFIO and dirty page sync with notifiers derived from memory region section, but this isn't guaranteed in the future.

So, we replay the intersection part of IOMMU memory region and IOMMU notifier in memory_region_iommu_replay().

---
v3: Fix assert failure on mps3-an547
v2: Add an assert per Peter
Tested on x86 with a net card passed to guest(kvm/tcg), ping/ssh pass. Also did simple bootup test with mps3-an547

 hw/i386/intel_iommu.c | 2 +-
 softmmu/memory.c      | 5 +++--
 2 files changed, 4 insertions(+), 3 deletions(-)
hw/i386/intel_iommu.c +1 -1
index a62896759c78..faade7def867 100644
@@ -3850,7 +3850,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
                 .domain_id = vtd_get_domain_id(s, &ce, vtd_as->pasid),
             };
 
-            vtd_page_walk(s, &ce, 0, ~0ULL, &info, vtd_as->pasid);
+            vtd_page_walk(s, &ce, n->start, n->end, &info, vtd_as->pasid);
         }
     } else {
         trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
softmmu/memory.c +1 -1
index b1a6cae6f583..f7af691991de 100644
@@ -1925,7 +1925,7 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
 {
     MemoryRegion *mr = MEMORY_REGION(iommu_mr);
     IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
-    hwaddr addr, granularity;
+    hwaddr addr, end, granularity;
     IOMMUTLBEntry iotlb;
 
     /* If the IOMMU has its own replay callback, override */

softmmu/memory.c void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n) · line 1943

Quoted 10 lines
softmmu/memory.c +2 -1
@@ -1935,8 +1935,9 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
     }
 
     granularity = memory_region_iommu_get_min_page_size(iommu_mr);
+    end = MIN(n->end, memory_region_size(mr));
 
-    for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
+    for (addr = n->start; addr < end; addr += granularity) {
         iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE, n->iommu_idx);
         if (iotlb.perm != IOMMU_NONE) {
             n->notify(n, &iotlb);

The documentation for the replay method of IOMMUMemoryRegionClass says:
* The default implementation of memory_region_iommu_replay() is to
* call the IOMMU translate method for every page in the address space
* with flag == IOMMU_NONE and then call the notifier if translate
* returns a valid mapping. If this method is implemented then it
* overrides the default behaviour, and must provide the full semantics
* of memory_region_iommu_replay(), by calling @notifier for every
* translation present in the IOMMU.

This commit changes the default implementation so it's no longer doing this for every page in the address space. If the change is correct, we should update the doc comment too.

Oddly, the doc comment for memory_region_iommu_replay() itself doesn't very clearly state what its semantics are; it could probably be improved.

Anyway, this change is OK for the TCG use of iommu notifiers, because that doesn't care about replay.

thanks
-- PMM

#3 lore raw
Quoted 83 lines · Peter Maydell
Quoted 56 lines · Zhenzhong Duan

On x86, there are two notifiers registered due to vtd-ir memory region splitting the entire address space. During replay of the address space for each notifier, the whole address space is scanned which is unnecessary. We only need to scan the space belong to notifier monitored space.

While on x86 IOMMU memory region spans over entire address space, but on some other platforms(e.g. arm mps3-an547), IOMMU memory region is only a window in the whole address space. user could register a notifier with arbitrary scope beyond IOMMU memory region. Though in current implementation replay is only triggered by VFIO and dirty page sync with notifiers derived from memory region section, but this isn't guaranteed in the future.

So, we replay the intersection part of IOMMU memory region and IOMMU notifier in memory_region_iommu_replay().

---
v3: Fix assert failure on mps3-an547
v2: Add an assert per Peter
Tested on x86 with a net card passed to guest(kvm/tcg), ping/ssh pass. Also did simple bootup test with mps3-an547

 hw/i386/intel_iommu.c | 2 +-
 softmmu/memory.c      | 5 +++--
 2 files changed, 4 insertions(+), 3 deletions(-)
hw/i386/intel_iommu.c +1 -1
index a62896759c78..faade7def867 100644
@@ -3850,7 +3850,7 @@ static void vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
                 .domain_id = vtd_get_domain_id(s, &ce, vtd_as->pasid),
             };
 
-            vtd_page_walk(s, &ce, 0, ~0ULL, &info, vtd_as->pasid);
+            vtd_page_walk(s, &ce, n->start, n->end, &info, vtd_as->pasid);
         }
     } else {
         trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
softmmu/memory.c +1 -1
index b1a6cae6f583..f7af691991de 100644
@@ -1925,7 +1925,7 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
 {
     MemoryRegion *mr = MEMORY_REGION(iommu_mr);
     IOMMUMemoryRegionClass *imrc = IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);
-    hwaddr addr, granularity;
+    hwaddr addr, end, granularity;
     IOMMUTLBEntry iotlb;
 
     /* If the IOMMU has its own replay callback, override */

softmmu/memory.c void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n) · line 1943

Quoted 10 lines
softmmu/memory.c +2 -1
@@ -1935,8 +1935,9 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
     }
 
     granularity = memory_region_iommu_get_min_page_size(iommu_mr);
+    end = MIN(n->end, memory_region_size(mr));
 
-    for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
+    for (addr = n->start; addr < end; addr += granularity) {
         iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE, n->iommu_idx);
         if (iotlb.perm != IOMMU_NONE) {
             n->notify(n, &iotlb);

The documentation for the replay method of IOMMUMemoryRegionClass says:
* The default implementation of memory_region_iommu_replay() is to
* call the IOMMU translate method for every page in the address space
* with flag == IOMMU_NONE and then call the notifier if translate
* returns a valid mapping. If this method is implemented then it
* overrides the default behaviour, and must provide the full semantics
* of memory_region_iommu_replay(), by calling @notifier for every
* translation present in the IOMMU.

This commit changes the default implementation so it's no longer doing this for every page in the address space. If the change is correct, we should update the doc comment too.

Quoted 6 lines

Oddly, the doc comment for memory_region_iommu_replay() itself doesn't very clearly state what its semantics are; it could probably be improved.

Anyway, this change is OK for the TCG use of iommu notifiers, because that doesn't care about replay.

Since the notifier contains the range information I'd say the change shouldn't affect any caller but only a pure performance difference. Indeed it'll be nicer the documentation can be updated too. Thanks,

Signature · 1 line
Peter Xu
#4 lore raw
Quoted 111 lines

-----Original Message-----
From: Peter Xu <[email protected]>
Sent: Tuesday, April 18, 2023 9:56 PM
To: Peter Maydell <[email protected]>
Cc: Duan, Zhenzhong <[email protected]>; qemu- [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH v3] memory: Optimize replay of guest mapping

Quoted 1 line · Peter Maydell

On Thu, 13 Apr 2023 at 12:12, Zhenzhong Duan

Quoted 29 lines · <[email protected]> wrote:
Quoted 29 lines

On x86, there are two notifiers registered due to vtd-ir memory region splitting the entire address space. During replay of the address space for each notifier, the whole address space is scanned which is unnecessary. We only need to scan the space belong to notifier monitored space.

While on x86 IOMMU memory region spans over entire address space, but on some other platforms(e.g. arm mps3-an547), IOMMU memory region is only a window in the whole address space. user could register a notifier with arbitrary scope beyond IOMMU memory region. Though in current implementation replay is only triggered by VFIO and dirty page sync with notifiers derived from memory region section, but this isn't guaranteed in the future.

So, we replay the intersection part of IOMMU memory region and IOMMU notifier in memory_region_iommu_replay().

---
v3: Fix assert failure on mps3-an547
v2: Add an assert per Peter
Tested on x86 with a net card passed to guest(kvm/tcg), ping/ssh pass. Also did simple bootup test with mps3-an547

 hw/i386/intel_iommu.c | 2 +-
 softmmu/memory.c      | 5 +++--
 2 files changed, 4 insertions(+), 3 deletions(-)
Quoted 5 lines

hw/i386/intel_iommu.c static void

Quoted 5 lines
hw/i386/intel_iommu.c index renamed from hw/i386/intel_iommu.c +0 -0

a62896759c78..faade7def867 100644

hw/i386/intel_iommu.c +0 -0
@@ -3850,7 +3850,7 @@ static void

vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)

Quoted 15 lines
Quoted 14 lines
hw/i386/intel_iommu.c +2 -1
                 .domain_id = vtd_get_domain_id(s, &ce, vtd_as->pasid),
             };
 
-            vtd_page_walk(s, &ce, 0, ~0ULL, &info, vtd_as->pasid);
+            vtd_page_walk(s, &ce, n->start, n->end, &info,
+ vtd_as->pasid);
         }
     } else {
         trace_vtd_replay_ce_invalid(bus_n, PCI_SLOT(vtd_as->devfn),
softmmu/memory.c index renamed from softmmu/memory.c +0 -0

b1a6cae6f583..f7af691991de 100644

softmmu/memory.c void · line 1925

Quoted 1 line
softmmu/memory.c +0 -0
@@ -1925,7 +1925,7 @@ void
 memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr,

IOMMUNotifier *n) {

Quoted 2 lines

softmmu/memory.c void · line 1927

Quoted 2 lines
softmmu/memory.c +0 -0
     MemoryRegion *mr = MEMORY_REGION(iommu_mr);
     IOMMUMemoryRegionClass *imrc =

IOMMU_MEMORY_REGION_GET_CLASS(iommu_mr);

Quoted 6 lines

softmmu/memory.c void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)

Quoted 6 lines
softmmu/memory.c +1 -2
-    hwaddr addr, granularity;
+    hwaddr addr, end, granularity;
     IOMMUTLBEntry iotlb;
 
     /* If the IOMMU has its own replay callback, override */ @@
-1935,8 +1935,9 @@ void

memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)

Quoted 27 lines

softmmu/memory.c void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)

Quoted 8 lines
     }

     granularity = memory_region_iommu_get_min_page_size(iommu_mr);
softmmu/memory.c +2 -1
+    end = MIN(n->end, memory_region_size(mr));
 
-    for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
+    for (addr = n->start; addr < end; addr += granularity) {
         iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE, n-

iommu_idx);

Quoted 2 lines
         if (iotlb.perm != IOMMU_NONE) {
             n->notify(n, &iotlb);

The documentation for the replay method of IOMMUMemoryRegionClass says:
* The default implementation of memory_region_iommu_replay() is to
* call the IOMMU translate method for every page in the address space
* with flag == IOMMU_NONE and then call the notifier if translate
* returns a valid mapping. If this method is implemented then it
* overrides the default behaviour, and must provide the full semantics
* of memory_region_iommu_replay(), by calling @notifier for every
* translation present in the IOMMU.

This commit changes the default implementation so it's no longer doing this for every page in the address space. If the change is correct, we should update the doc comment too.

Quoted 6 lines

Oddly, the doc comment for memory_region_iommu_replay() itself doesn't very clearly state what its semantics are; it could probably be improved.

Anyway, this change is OK for the TCG use of iommu notifiers, because that doesn't care about replay.

Quoted 6 lines

Since the notifier contains the range information I'd say the change shouldn't affect any caller but only a pure performance difference. Indeed it'll be nicer the documentation can be updated too. Thanks,

--
Peter Xu

Thanks Peter Maydell and Peter Xu's comments, will add doc update. May I ask if it's preferred to add doc update to this patch or a separate patch?

Regards
Zhenzhong

#5 lore raw
Quoted 21 lines · Zhenzhong
Quoted 21 lines
Quoted 20 lines

The documentation for the replay method of IOMMUMemoryRegionClass says:
* The default implementation of memory_region_iommu_replay() is to
* call the IOMMU translate method for every page in the address space
* with flag == IOMMU_NONE and then call the notifier if translate
* returns a valid mapping. If this method is implemented then it
* overrides the default behaviour, and must provide the full semantics
* of memory_region_iommu_replay(), by calling @notifier for every
* translation present in the IOMMU.

This commit changes the default implementation so it's no longer doing this for every page in the address space. If the change is correct, we should update the doc comment too.

Oddly, the doc comment for memory_region_iommu_replay() itself doesn't very clearly state what its semantics are; it could probably be improved.

Anyway, this change is OK for the TCG use of iommu notifiers, because that doesn't care about replay.

Quoted 6 lines
Quoted 3 lines

Since the notifier contains the range information I'd say the change shouldn't affect any caller but only a pure performance difference. Indeed it'll be nicer the documentation can be updated too. Thanks,

Thanks Peter Maydell and Peter Xu's comments, will add doc update. May I ask if it's preferred to add doc update to this patch or a separate patch?

I suggest doing it in this same patch.

thanks
-- PMM