Skip to contentSkip to conversation

Avoid needless flush cache when guest change MTRR

PATCH In review Liang Li 4 participants 5 messages Feb 4, 2015

lore
Share & tools
lore

Liang Li

author
#0 lore raw

PATCH 1/1 Avoid needless flush cache when guest change MTRR

Flushing cache is needed only when guest has IOMMU device, using need_iommu(d) can minimize the impact to guest with device assigned, since a guest may be hot plugged with a device thus there may be dirty cache lines before need_iommu(d) becoming true, force the flush_all when the first device is assigned to guest to amend this issue.

---
 xen/arch/x86/hvm/mtrr.c       | 2 +-
 xen/drivers/passthrough/pci.c | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/mtrr.c +1 -1
index ee18553..858fb7e 100644
@@ -791,7 +791,7 @@ HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save_mtrr_msr, hvm_load_mtrr_msr,
 
 void memory_type_changed(struct domain *d)
 {
-    if ( iommu_enabled && d->vcpu && d->vcpu[0] )
+    if ( need_iommu(d) && d->vcpu && d->vcpu[0] )
     {
         p2m_memory_type_changed(d);
         flush_all(FLUSH_CACHE);
xen/drivers/passthrough/pci.c +5 -0
index 78c6977..a023d1d 100644
@@ -1365,6 +1365,11 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn)
             }
         }
         d->need_iommu = 1;
+        /* There may be dirty cache lines when a device is assigned
+         * and before need_iommu(d) becoming true, this will cause
+         * memory_type_changed lose effect if memory type changes.
+         * Call memory_type_changed here to amend this. */
+        memory_type_changed(d);
     }
 
     pdev = pci_get_pdev_by_domain(hardware_domain, seg, bus, devfn);
Signature · 1 line
1.9.1
#1 lore raw
Quoted 35 lines

From: Li, Liang Z
Sent: Wednesday, February 04, 2015 11:28 AM

Flushing cache is needed only when guest has IOMMU device, using need_iommu(d) can minimize the impact to guest with device assigned, since a guest may be hot plugged with a device thus there may be dirty cache lines before need_iommu(d) becoming true, force the flush_all when the first device is assigned to guest to amend this issue.

---
 xen/arch/x86/hvm/mtrr.c       | 2 +-
 xen/drivers/passthrough/pci.c | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/mtrr.c +1 -1
index ee18553..858fb7e 100644
@@ -791,7 +791,7 @@ HVM_REGISTER_SAVE_RESTORE(MTRR,
 hvm_save_mtrr_msr, hvm_load_mtrr_msr,
 
 void memory_type_changed(struct domain *d)
 {
-    if ( iommu_enabled && d->vcpu && d->vcpu[0] )
+    if ( need_iommu(d) && d->vcpu && d->vcpu[0] )
     {
         p2m_memory_type_changed(d);
         flush_all(FLUSH_CACHE);
xen/drivers/passthrough/pci.c +0 -0
index 78c6977..a023d1d 100644

xen/drivers/passthrough/pci.c static int assign_device(struct domain *d, u16 seg, · line 1373

Quoted 9 lines
xen/drivers/passthrough/pci.c +5 -0
@@ -1365,6 +1365,11 @@ static int assign_device(struct domain *d, u16 seg,
 u8 bus, u8 devfn)
             }
         }
         d->need_iommu = 1;
+        /* There may be dirty cache lines when a device is assigned
+         * and before need_iommu(d) becoming true, this will cause
+         * memory_type_changed lose effect if memory type changes.
+         * Call memory_type_changed here to amend this. */
+        memory_type_changed(d);

could we relax this force flush only on the 1st assigned device which actually changes need_iommu(d) from false to true?

Quoted 5 lines
xen/drivers/passthrough/pci.c +0 -0
     }
 
     pdev = pci_get_pdev_by_domain(hardware_domain, seg, bus, devfn);

--
1.9.1

#2 lore raw

Tian, Kevin wrote on 2015-02-04:

Quoted 36 lines
Quoted 36 lines

From: Li, Liang Z
Sent: Wednesday, February 04, 2015 11:28 AM

Flushing cache is needed only when guest has IOMMU device, using need_iommu(d) can minimize the impact to guest with device assigned, since a guest may be hot plugged with a device thus there may be dirty cache lines before need_iommu(d) becoming true, force the flush_all when the first device is assigned to guest to amend this issue.

---
 xen/arch/x86/hvm/mtrr.c       | 2 +-
 xen/drivers/passthrough/pci.c | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/mtrr.c index renamed from xen/arch/x86/hvm/mtrr.c +0 -0

ee18553..858fb7e 100644 --- a/xen/arch/x86/hvm/mtrr.c +++ b/xen/arch/x86/hvm/mtrr.c @@ -791,7 +791,7 @@ HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save_mtrr_msr, hvm_load_mtrr_msr,

void memory_type_changed(struct domain *d) {

xen/arch/x86/hvm/mtrr.c index renamed from xen/arch/x86/hvm/mtrr.c +1 -1
-    if ( iommu_enabled && d->vcpu && d->vcpu[0] )
+    if ( need_iommu(d) && d->vcpu && d->vcpu[0] )
     {
         p2m_memory_type_changed(d);
         flush_all(FLUSH_CACHE);
(quoted diff) +0 -0

b/xen/drivers/passthrough/pci.c index 78c6977..a023d1d 100644

xen/drivers/passthrough/pci.c +0 -0
@@ -1365,6 +1365,11 @@ static int assign_device(struct domain *d,
 u16 seg,
 u8 bus, u8 devfn)
             }
         }
         d->need_iommu = 1;
Quoted 8 lines

xen/drivers/passthrough/pci.c static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn) · line 1372

Quoted 5 lines
xen/drivers/passthrough/pci.c +5 -0
+        /* There may be dirty cache lines when a device is assigned
+         * and before need_iommu(d) becoming true, this will cause
+         * memory_type_changed lose effect if memory type changes.
+         * Call memory_type_changed here to amend this. */
+        memory_type_changed(d);

could we relax this force flush only on the 1st assigned device which actually changes need_iommu(d) from false to true?

It is already covered by current code when updating need_iommu, but the patch doesn't show it:

if ( need_iommu(d) <= 0 )

{    
    .....
    d->need_iommu = 1;
    memory_type_changed(d);
}
Quoted 7 lines
Quoted 7 lines
xen/drivers/passthrough/pci.c +0 -0
     }

pdev = pci_get_pdev_by_domain(hardware_domain, seg, bus,
devfn);
--
1.9.1

Best regards,
Yang

Acked-by
#3 lore raw
Quoted 53 lines

From: Zhang, Yang Z
Sent: Wednesday, February 04, 2015 1:54 PM

Tian, Kevin wrote on 2015-02-04:

Quoted 9 lines
Quoted 9 lines

From: Li, Liang Z
Sent: Wednesday, February 04, 2015 11:28 AM

Flushing cache is needed only when guest has IOMMU device, using need_iommu(d) can minimize the impact to guest with device assigned, since a guest may be hot plugged with a device thus there may be dirty cache lines before need_iommu(d) becoming true, force the flush_all when the first device is assigned to guest to amend this issue.

Quoted 10 lines
Quoted 10 lines
---
 xen/arch/x86/hvm/mtrr.c       | 2 +-
 xen/drivers/passthrough/pci.c | 5 +++++
 2 files changed, 6 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/mtrr.c index renamed from xen/arch/x86/hvm/mtrr.c +0 -0

ee18553..858fb7e 100644 --- a/xen/arch/x86/hvm/mtrr.c +++ b/xen/arch/x86/hvm/mtrr.c @@ -791,7 +791,7 @@ HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save_mtrr_msr,

hvm_load_mtrr_msr,

Quoted 17 lines
Quoted 17 lines

void memory_type_changed(struct domain *d) {

xen/arch/x86/hvm/mtrr.c index renamed from xen/arch/x86/hvm/mtrr.c +1 -1
-    if ( iommu_enabled && d->vcpu && d->vcpu[0] )
+    if ( need_iommu(d) && d->vcpu && d->vcpu[0] )
     {
         p2m_memory_type_changed(d);
         flush_all(FLUSH_CACHE);
(quoted diff) +0 -0

b/xen/drivers/passthrough/pci.c index 78c6977..a023d1d 100644

xen/drivers/passthrough/pci.c +0 -0
@@ -1365,6 +1365,11 @@ static int assign_device(struct domain *d,
 u16 seg,
 u8 bus, u8 devfn)
             }
         }
         d->need_iommu = 1;
Quoted 8 lines

xen/drivers/passthrough/pci.c static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn) · line 1372

Quoted 5 lines
xen/drivers/passthrough/pci.c +5 -0
+        /* There may be dirty cache lines when a device is assigned
+         * and before need_iommu(d) becoming true, this will cause
+         * memory_type_changed lose effect if memory type changes.
+         * Call memory_type_changed here to amend this. */
+        memory_type_changed(d);

could we relax this force flush only on the 1st assigned device which actually changes need_iommu(d) from false to true?

It is already covered by current code when updating need_iommu, but the patch doesn't show it:

Quoted 7 lines

if ( need_iommu(d) <= 0 )

{
    .....
    d->need_iommu = 1;
    memory_type_changed(d);
}

You're right. :-)

Thanks
Kevin

#4 lore raw
In reply to Liang Li

xen/arch/x86/hvm/mtrr.c HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save_mtrr_msr, hvm_load_mtrr_msr, · line 797

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 04.02.15 at 04:28, <[email protected]> wrote:
xen/arch/x86/hvm/mtrr.c +1 -1
@@ -791,7 +791,7 @@ HVM_REGISTER_SAVE_RESTORE(MTRR, hvm_save_mtrr_msr, hvm_load_mtrr_msr,
 
 void memory_type_changed(struct domain *d)
 {
-    if ( iommu_enabled && d->vcpu && d->vcpu[0] )
+    if ( need_iommu(d) && d->vcpu && d->vcpu[0] )
     {
         p2m_memory_type_changed(d);
         flush_all(FLUSH_CACHE);

This doesn't do what title and description say: You not only suppress the flush, but also the EPT table adjustments. Either adjust at least the description accordingly, or make the code do what the description says.

xen/drivers/passthrough/pci.c static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn) · line 1372

Quoted 11 lines
xen/drivers/passthrough/pci.c +5 -0
@@ -1365,6 +1365,11 @@ static int assign_device(struct domain *d, u16 seg, u8 bus, u8 devfn)
             }
         }
         d->need_iommu = 1;
+        /* There may be dirty cache lines when a device is assigned
+         * and before need_iommu(d) becoming true, this will cause
+         * memory_type_changed lose effect if memory type changes.
+         * Call memory_type_changed here to amend this. */
+        memory_type_changed(d);

The comment formatting does not adhere to ./CODING_STYLE.

Jan