Skip to contentSkip to conversation

memory: Optimize replay of guest mapping

PATCH Superseded Zhenzhong Duan 3 participants 5 messages Feb 14, 2023 – Feb 15, 2023

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memory: Optimize replay of guest mapping

On x86, there are two notifiers registered due to vtd-ir memory region splitting the whole address space. During replay of the address space for each notifier, the whole address space is scanned which is unnecessory.

We only need to scan the space belong to notifier montiored space.

---
Tested only on x86 with a net card passed to guest, ping/ssh pass.

 hw/i386/intel_iommu.c | 2 +-
 softmmu/memory.c      | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)
hw/i386/intel_iommu.c +1 -1
index 98a5c304a7d7..6b1de80e8573 100644
@@ -3831,7 +3831,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 -2
index 9d64efca269b..f096716e6e78 100644
@@ -1923,7 +1923,6 @@ uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr)
 
 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;
     IOMMUTLBEntry iotlb;
@@ -1936,7 +1935,7 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
 
     granularity = memory_region_iommu_get_min_page_size(iommu_mr);
 
-    for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
+    for (addr = n->start; addr < n->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
#1 lore raw
Quoted 42 lines · Zhenzhong Duan

On x86, there are two notifiers registered due to vtd-ir memory region splitting the whole address space. During replay of the address space for each notifier, the whole address space is scanned which is unnecessory.

We only need to scan the space belong to notifier montiored space.

---
Tested only on x86 with a net card passed to guest, ping/ssh pass.

 hw/i386/intel_iommu.c | 2 +-
 softmmu/memory.c      | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)
hw/i386/intel_iommu.c +1 -1
index 98a5c304a7d7..6b1de80e8573 100644
@@ -3831,7 +3831,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 +0 -1
index 9d64efca269b..f096716e6e78 100644
@@ -1923,7 +1923,6 @@ uint64_t memory_region_iommu_get_min_page_size(IOMMUMemoryRegion *iommu_mr)
 
 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;
     IOMMUTLBEntry iotlb;

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

Quoted 5 lines
softmmu/memory.c +1 -1
@@ -1936,7 +1935,7 @@ void memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)
 
     granularity = memory_region_iommu_get_min_page_size(iommu_mr);
 
-    for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
+    for (addr = n->start; addr < n->end; addr += granularity) {

Is [n->start, n->end] guaranteed to be the subset of memory_region_size(mr)?

Thanks

Quoted 7 lines
softmmu/memory.c +0 -1
         iotlb = imrc->translate(iommu_mr, addr, IOMMU_NONE, n->iommu_idx);
         if (iotlb.perm != IOMMU_NONE) {
             n->notify(n, &iotlb);
--

2.25.1

#2 lore raw
Quoted 62 lines

-----Original Message-----
From: Jason Wang <[email protected]>
Sent: Tuesday, February 14, 2023 2:25 PM
To: Duan, Zhenzhong <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] memory: Optimize replay of guest mapping

hw/i386/intel_iommu.c static void

Quoted 21 lines · Zhenzhong Duan

On x86, there are two notifiers registered due to vtd-ir memory region splitting the whole address space. During replay of the address space for each notifier, the whole address space is scanned which is unnecessory.

We only need to scan the space belong to notifier montiored space.

---
Tested only on x86 with a net card passed to guest, ping/ssh pass.

 hw/i386/intel_iommu.c | 2 +-
 softmmu/memory.c      | 3 +--
 2 files changed, 2 insertions(+), 3 deletions(-)
hw/i386/intel_iommu.c index renamed from hw/i386/intel_iommu.c +0 -0

98a5c304a7d7..6b1de80e8573 100644

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

vtd_iommu_replay(IOMMUMemoryRegion *iommu_mr, IOMMUNotifier *n)

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

9d64efca269b..f096716e6e78 100644

softmmu/memory.c +0 -0
@@ -1923,7 +1923,6 @@ uint64_t
Quoted 1 line

memory_region_iommu_get_min_page_size(IOMMUMemoryRegion

*iommu_mr)

Quoted 5 lines

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);

softmmu/memory.c void · line 1935

Quoted 4 lines
     hwaddr addr, granularity;
     IOMMUTLBEntry iotlb;
softmmu/memory.c +0 -0
@@ -1936,7 +1935,7 @@ void
 memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr,

IOMMUNotifier

Quoted 8 lines

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

Quoted 6 lines
softmmu/memory.c +1 -1
 *n)
 
     granularity = memory_region_iommu_get_min_page_size(iommu_mr);
 
-    for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
+    for (addr = n->start; addr < n->end; addr += granularity) {

Is [n->start, n->end] guaranteed to be the subset of memory_region_size(mr)?

In current implementation it is.
[n->start, n->end] of notifier is derived from iommu memory region's section which is a subset of iommu memory region itself.

Thanks
Zhenzhong

#3 lore raw
Quoted 7 lines · Zhenzhong
Quoted 7 lines

Review void · line 1935

Quoted 2 lines
(quoted diff) +0 -0
@@ -1936,7 +1935,7 @@ void
 memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr,

IOMMUNotifier

Quoted 4 lines
softmmu/memory.c +0 -0
 *n)
 
     granularity = memory_region_iommu_get_min_page_size(iommu_mr);
 
Quoted 8 lines
Quoted 4 lines

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

Quoted 2 lines
softmmu/memory.c +1 -1
-    for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
+    for (addr = n->start; addr < n->end; addr += granularity) {

Is [n->start, n->end] guaranteed to be the subset of memory_region_size(mr)?

In current implementation it is.
[n->start, n->end] of notifier is derived from iommu memory region's section which is a subset of iommu memory region itself.

Yes, currently it seems to be guaranteed by the callers assuming they're always doing the right thing.

Maybe it'll worth it to have memory_region_register_iommu_notifier() assert properly to make sure it always hold true?

The patch itself looks good here, thanks.

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

-----Original Message-----
From: Peter Xu <[email protected]>
Sent: Wednesday, February 15, 2023 1:29 AM
To: Duan, Zhenzhong <[email protected]>
Cc: Jason Wang <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Subject: Re: [PATCH] memory: Optimize replay of guest mapping

Quoted 11 lines · Zhenzhong
Quoted 11 lines

Review void · line 1935

Quoted 2 lines
(quoted diff) +0 -0
@@ -1936,7 +1935,7 @@ void
 memory_region_iommu_replay(IOMMUMemoryRegion *iommu_mr,

IOMMUNotifier

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

Quoted 6 lines
softmmu/memory.c +1 -1
 *n)
 
     granularity = memory_region_iommu_get_min_page_size(iommu_mr);
 
-    for (addr = 0; addr < memory_region_size(mr); addr += granularity) {
+    for (addr = n->start; addr < n->end; addr += granularity) {

Is [n->start, n->end] guaranteed to be the subset of

memory_region_size(mr)?

Quoted 4 lines

In current implementation it is.
[n->start, n->end] of notifier is derived from iommu memory region's section which is a subset of iommu memory region itself.

Quoted 5 lines

Yes, currently it seems to be guaranteed by the callers assuming they're always doing the right thing.

Maybe it'll worth it to have memory_region_register_iommu_notifier() assert properly to make sure it always hold true?

Yea, good suggestion, will do in v2.

Thanks
Zhenzhong