Skip to contentSkip to conversation

Check VT-d Device-TLB flush error

PATCH Superseded Quan Xu 5 participants 39 messages Apr 29, 2016 – May 6, 2016

v3 of 10 Superseded Latest is v10 →

lore
Share & tools
lore

Quan Xu

author
#0 lore raw

v3 cover Check VT-d Device-TLB flush error

This patch set is a prereq patch set for Patch:'VT-d Device-TLB flush issue'.

While IOMMU Device-TLB flush timed out, xen calls panic() at present. However the existing panic() is going to be eliminated, so we must propagate the IOMMU Device-TLB flush error up to the call trees.

This patch set is also based on the discussion of 'abstract model of IOMMU unmaping/mapping failures'

In this v3,
- best effort flushing when an error.
- adding __must_check.

Quan Xu (10):
vt-d: fix the IOMMU flush issue
IOMMU: handle IOMMU mapping and unmapping failures IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping
vt-d: propagate IOMMU Device-TLB flush error up to IOMMU unmapping.
vt-d: propagate IOMMU Device-TLB flush error up to IOMMU mapping. propagate IOMMU Device-TLB flush error up to iommu_iotlb_flush{,_all}
(top level ones).
IOMMU: propagate IOMMU Device-TLB flush error up to
iommu_iotlb_flush{,_all} (leaf ones).
vt-d/ept: propagate IOMMU Device-TLB flush error up to EPT update.
IOMMU: propagate IOMMU Device-TLB flush error up to IOMMU suspending
vt-d: propagate error up to ME phantom function mapping and unmapping

 xen/arch/arm/p2m.c                            |   5 +-
 xen/arch/x86/acpi/power.c                     |  15 ++-
 xen/arch/x86/mm.c                             |  13 +-
 xen/arch/x86/mm/p2m-ept.c                     |  30 ++++-
 xen/arch/x86/mm/p2m-pt.c                      |  24 +++-
 xen/arch/x86/mm/p2m.c                         |  11 +-
 xen/common/memory.c                           |  14 ++-
 xen/drivers/passthrough/amd/iommu_init.c      |   9 +-
 xen/drivers/passthrough/amd/pci_amd_iommu.c   |   2 +-
 xen/drivers/passthrough/arm/smmu.c            |  12 +-
 xen/drivers/passthrough/iommu.c               |  64 ++++++++--
 xen/drivers/passthrough/vtd/extern.h          |   3 +-
 xen/drivers/passthrough/vtd/iommu.c           | 168 ++++++++++++++++++--------
 xen/drivers/passthrough/vtd/quirks.c          |  28 +++--
 xen/drivers/passthrough/x86/iommu.c           |   5 +-
 xen/include/asm-x86/hvm/svm/amd-iommu-proto.h |   2 +-
 xen/include/asm-x86/iommu.h                   |   3 +-
 xen/include/xen/iommu.h                       |  13 +-
 18 files changed, 313 insertions(+), 108 deletions(-)
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
author
#1 lore raw

PATCH v3 1/10 vt-d: fix the IOMMU flush issue

The propagation value from IOMMU flush interfaces may be positive, which indicates callers need to flush cache, not one of faliures.

when the propagation value is positive, this patch fixes this flush issue as follows:
- call iommu_flush_write_buffer() to flush cache.
- return zero.

CC Kevin Tian <[email protected]>
CC Feng Wu <[email protected]>
CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
---
 xen/drivers/passthrough/vtd/iommu.c | 94 +++++++++++++++++++++++++------------
 xen/include/asm-x86/iommu.h         |  2 +-
 2 files changed, 64 insertions(+), 32 deletions(-)
xen/drivers/passthrough/vtd/iommu.c +63 -31
index 5ad25dc..6e2e43a 100644
@@ -558,14 +558,16 @@ static void iommu_flush_all(void)
     }
 }
 
-static void __intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn,
-        int dma_old_pte_present, unsigned int page_count)
+static int iommu_flush_iotlb(struct domain *d, unsigned long gfn,
+                             bool_t dma_old_pte_present,
+                             unsigned int page_count)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
     struct acpi_drhd_unit *drhd;
     struct iommu *iommu;
     int flush_dev_iotlb;
     int iommu_domid;
+    int rc, ret = 0;
 
     /*
      * No need pcideves_lock here because we have flush
@@ -584,29 +586,35 @@ static void __intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn,
             continue;
 
         if ( page_count != 1 || gfn == INVALID_GFN )
-        {
-            if ( iommu_flush_iotlb_dsi(iommu, iommu_domid,
-                        0, flush_dev_iotlb) )
-                iommu_flush_write_buffer(iommu);
-        }
+            rc = iommu_flush_iotlb_dsi(iommu, iommu_domid,
+                                       0, flush_dev_iotlb);
         else
+            rc = iommu_flush_iotlb_psi(iommu, iommu_domid,
+                                       (paddr_t)gfn << PAGE_SHIFT_4K,
+                                       PAGE_ORDER_4K,
+                                       !dma_old_pte_present,
+                                       flush_dev_iotlb);
+
+        if ( rc > 0 )
         {
-            if ( iommu_flush_iotlb_psi(iommu, iommu_domid,
-                        (paddr_t)gfn << PAGE_SHIFT_4K, PAGE_ORDER_4K,
-                        !dma_old_pte_present, flush_dev_iotlb) )
-                iommu_flush_write_buffer(iommu);
+            iommu_flush_write_buffer(iommu);
+            ret = 0;
         }
+        else if ( rc < 0 )
+            ret = rc;
     }
+
+    return ret;
 }
 
 static void intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn, unsigned int page_count)
 {
-    __intel_iommu_iotlb_flush(d, gfn, 1, page_count);
+    iommu_flush_iotlb(d, gfn, 1, page_count);
 }
 
 static void intel_iommu_iotlb_flush_all(struct domain *d)
 {
-    __intel_iommu_iotlb_flush(d, INVALID_GFN, 0, 0);
+    iommu_flush_iotlb(d, INVALID_GFN, 0, 0);
 }
 
 /* clear one page's page table */
@@ -640,7 +648,7 @@ static void dma_pte_clear_one(struct domain *domain, u64 addr)
     iommu_flush_cache_entry(pte, sizeof(struct dma_pte));
 
     if ( !this_cpu(iommu_dont_flush_iotlb) )
-        __intel_iommu_iotlb_flush(domain, addr >> PAGE_SHIFT_4K, 1, 1);
+        iommu_flush_iotlb(domain, addr >> PAGE_SHIFT_4K, 1, 1);
 
     unmap_vtd_domain_page(page);
 }
@@ -1281,6 +1289,7 @@ int domain_context_mapping_one(
     u64 maddr, pgd_maddr;
     u16 seg = iommu->intel->drhd->segment;
     int agaw;
+    int rc;
 
     ASSERT(pcidevs_locked());
     spin_lock(&iommu->lock);
@@ -1394,13 +1403,19 @@ int domain_context_mapping_one(
     spin_unlock(&iommu->lock);
 
     /* Context entry was previously non-present (with domid 0). */
-    if ( iommu_flush_context_device(iommu, 0, (((u16)bus) << 8) | devfn,
-                                    DMA_CCMD_MASK_NOBIT, 1) )
-        iommu_flush_write_buffer(iommu);
-    else
+    rc = iommu_flush_context_device(iommu, 0, (((u16)bus) << 8) | devfn,
+                                    DMA_CCMD_MASK_NOBIT, 1);
+
+    if ( !rc )
     {
         int flush_dev_iotlb = find_ats_dev_drhd(iommu) ? 1 : 0;
-        iommu_flush_iotlb_dsi(iommu, 0, 1, flush_dev_iotlb);
+        rc = iommu_flush_iotlb_dsi(iommu, 0, 1, flush_dev_iotlb);
+    }
+
+    if ( rc > 0 )
+    {
+        iommu_flush_write_buffer(iommu);
+        rc = 0;
     }
 
     set_bit(iommu->index, &hd->arch.iommu_bitmap);
@@ -1410,7 +1425,7 @@ int domain_context_mapping_one(
     if ( !seg )
         me_wifi_quirk(domain, bus, devfn, MAP_ME_PHANTOM_FUNC);
 
-    return 0;
+    return rc;
 }
 
 static int domain_context_mapping(
@@ -1505,6 +1520,7 @@ int domain_context_unmap_one(
     struct context_entry *context, *context_entries;
     u64 maddr;
     int iommu_domid;
+    int rc;
 
     ASSERT(pcidevs_locked());
     spin_lock(&iommu->lock);
@@ -1532,14 +1548,20 @@ int domain_context_unmap_one(
         return -EINVAL;
     }
 
-    if ( iommu_flush_context_device(iommu, iommu_domid,
+    rc = iommu_flush_context_device(iommu, iommu_domid,
                                     (((u16)bus) << 8) | devfn,
-                                    DMA_CCMD_MASK_NOBIT, 0) )
-        iommu_flush_write_buffer(iommu);
-    else
+                                    DMA_CCMD_MASK_NOBIT, 0);
+
+    if ( !rc )
     {
         int flush_dev_iotlb = find_ats_dev_drhd(iommu) ? 1 : 0;
-        iommu_flush_iotlb_dsi(iommu, iommu_domid, 0, flush_dev_iotlb);
+        rc = iommu_flush_iotlb_dsi(iommu, iommu_domid, 0, flush_dev_iotlb);
+    }
+
+    if ( rc > 0 )
+    {
+        iommu_flush_write_buffer(iommu);
+        rc = 0;
     }
 
     spin_unlock(&iommu->lock);
@@ -1548,7 +1570,7 @@ int domain_context_unmap_one(
     if ( !iommu->intel->drhd->segment )
         me_wifi_quirk(domain, bus, devfn, UNMAP_ME_PHANTOM_FUNC);
 
-    return 0;
+    return rc;
 }
 
 static int domain_context_unmap(
@@ -1738,7 +1760,7 @@ static int intel_iommu_map_page(
     unmap_vtd_domain_page(page);
 
     if ( !this_cpu(iommu_dont_flush_iotlb) )
-        __intel_iommu_iotlb_flush(d, gfn, dma_pte_present(old), 1);
+        iommu_flush_iotlb(d, gfn, dma_pte_present(old), 1);
 
     return 0;
 }
@@ -1754,14 +1776,15 @@ static int intel_iommu_unmap_page(struct domain *d, unsigned long gfn)
     return 0;
 }
 
-void iommu_pte_flush(struct domain *d, u64 gfn, u64 *pte,
-                     int order, int present)
+int iommu_pte_flush(struct domain *d, u64 gfn, u64 *pte,
+                    int order, bool_t present)
 {
     struct acpi_drhd_unit *drhd;
     struct iommu *iommu = NULL;
     struct hvm_iommu *hd = domain_hvm_iommu(d);
     int flush_dev_iotlb;
     int iommu_domid;
+    int rc, ret = 0;
 
     iommu_flush_cache_entry(pte, sizeof(struct dma_pte));
 
@@ -1775,11 +1798,20 @@ void iommu_pte_flush(struct domain *d, u64 gfn, u64 *pte,
         iommu_domid= domain_iommu_domid(d, iommu);
         if ( iommu_domid == -1 )
             continue;
-        if ( iommu_flush_iotlb_psi(iommu, iommu_domid,
+
+        rc = iommu_flush_iotlb_psi(iommu, iommu_domid,
                                    (paddr_t)gfn << PAGE_SHIFT_4K,
-                                   order, !present, flush_dev_iotlb) )
+                                   order, !present, flush_dev_iotlb);
+        if ( rc > 0 )
+        {
             iommu_flush_write_buffer(iommu);
+            ret = 0;
+        }
+        else if ( rc < 0 )
+            ret = rc;
     }
+
+    return ret;
 }
 
 static int __init vtd_ept_page_compatible(struct iommu *iommu)
xen/include/asm-x86/iommu.h +1 -1
index f22b3a5..1ae0c5a 100644
@@ -26,7 +26,7 @@ int iommu_setup_hpet_msi(struct msi_desc *);
 
 /* While VT-d specific, this must get declared in a generic header. */
 int adjust_vtd_irq_affinities(void);
-void iommu_pte_flush(struct domain *d, u64 gfn, u64 *pte, int order, int present);
+int iommu_pte_flush(struct domain *d, u64 gfn, u64 *pte, int order, bool_t present);
 bool_t iommu_supports_eim(void);
 int iommu_enable_x2apic_IR(void);
 void iommu_disable_x2apic_IR(void);
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#2 lore raw

Re: [PATCH v3 01/10] vt-d: fix the IOMMU flush issue

Quoted 65 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

The propagation value from IOMMU flush interfaces may be positive, which indicates callers need to flush cache, not one of faliures.

when the propagation value is positive, this patch fixes this flush issue as follows:
- call iommu_flush_write_buffer() to flush cache.
- return zero.

CC Kevin Tian <[email protected]>
CC Feng Wu <[email protected]>
CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
---
 xen/drivers/passthrough/vtd/iommu.c | 94
+++++++++++++++++++++++++------------
 xen/include/asm-x86/iommu.h         |  2 +-
 2 files changed, 64 insertions(+), 32 deletions(-)
xen/drivers/passthrough/vtd/iommu.c +10 -7
index 5ad25dc..6e2e43a 100644
@@ -558,14 +558,16 @@ static void iommu_flush_all(void)
     }
 }
 
-static void __intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn,
-        int dma_old_pte_present, unsigned int page_count)
+static int iommu_flush_iotlb(struct domain *d, unsigned long gfn,
+                             bool_t dma_old_pte_present,
+                             unsigned int page_count)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
     struct acpi_drhd_unit *drhd;
     struct iommu *iommu;
     int flush_dev_iotlb;
     int iommu_domid;
+    int rc, ret = 0;
 
     /*
      * No need pcideves_lock here because we have flush
@@ -584,29 +586,35 @@ static void __intel_iommu_iotlb_flush(struct domain *d,
 unsigned long gfn,
             continue;
 
         if ( page_count != 1 || gfn == INVALID_GFN )
-        {
-            if ( iommu_flush_iotlb_dsi(iommu, iommu_domid,
-                        0, flush_dev_iotlb) )
-                iommu_flush_write_buffer(iommu);
-        }
+            rc = iommu_flush_iotlb_dsi(iommu, iommu_domid,
+                                       0, flush_dev_iotlb);
         else
+            rc = iommu_flush_iotlb_psi(iommu, iommu_domid,
+                                       (paddr_t)gfn << PAGE_SHIFT_4K,
+                                       PAGE_ORDER_4K,
+                                       !dma_old_pte_present,

xen/drivers/passthrough/vtd/iommu.c static void __intel_iommu_iotlb_flush(struct domain *d, · line 602

Quoted 10 lines
xen/drivers/passthrough/vtd/iommu.c +5 -4
+                                       flush_dev_iotlb);
+
+        if ( rc > 0 )
         {
-            if ( iommu_flush_iotlb_psi(iommu, iommu_domid,
-                        (paddr_t)gfn << PAGE_SHIFT_4K, PAGE_ORDER_4K,
-                        !dma_old_pte_present, flush_dev_iotlb) )
-                iommu_flush_write_buffer(iommu);
+            iommu_flush_write_buffer(iommu);
+            ret = 0;

You don't need 'ret' here. Just change 'rc' to 0.

xen/drivers/passthrough/vtd/iommu.c static void __intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn, · line 604

Quoted 3 lines
xen/drivers/passthrough/vtd/iommu.c +2 -0
         }
+        else if ( rc < 0 )
+            ret = rc;

Then this change is not required

xen/drivers/passthrough/vtd/iommu.c static void __intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn, · line 607

Quoted 3 lines
xen/drivers/passthrough/vtd/iommu.c +2 -0
     }
+
+    return ret;

Then return 'rc' instead.

xen/drivers/passthrough/vtd/iommu.c static void __intel_iommu_iotlb_flush(struct domain *d, · line 612

Quoted 4 lines
xen/drivers/passthrough/vtd/iommu.c +0 -0
 }
 
 static void intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn, unsigned int
 page_count)

could we remove "Intel_" prefix completely? You can rename this one to iommu_flush_iotlb_page...

xen/drivers/passthrough/vtd/iommu.c static void __intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn, · line 615

Quoted 6 lines
xen/drivers/passthrough/vtd/iommu.c +1 -1
 {
-    __intel_iommu_iotlb_flush(d, gfn, 1, page_count);
+    iommu_flush_iotlb(d, gfn, 1, page_count);
 }
 
 static void intel_iommu_iotlb_flush_all(struct domain *d)

and this one just iommu_flush_iotlb_all

xen/drivers/passthrough/vtd/iommu.c static void __intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn, · line 618

Quoted 5 lines
xen/drivers/passthrough/vtd/iommu.c +1 -1
 {
-    __intel_iommu_iotlb_flush(d, INVALID_GFN, 0, 0);
+    iommu_flush_iotlb(d, INVALID_GFN, 0, 0);
 }

Thanks
Kevin

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#3 lore raw

Re: [PATCH v3 01/10] vt-d: fix the IOMMU flush issue

Quoted 69 lines · Kevin
Quoted 69 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

The propagation value from IOMMU flush interfaces may be positive, which indicates callers need to flush cache, not one of faliures.

when the propagation value is positive, this patch fixes this flush issue as follows:
- call iommu_flush_write_buffer() to flush cache.
- return zero.

CC Kevin Tian <[email protected]>
CC Feng Wu <[email protected]>
CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
---
 xen/drivers/passthrough/vtd/iommu.c | 94
+++++++++++++++++++++++++------------
 xen/include/asm-x86/iommu.h         |  2 +-
 2 files changed, 64 insertions(+), 32 deletions(-)
xen/drivers/passthrough/vtd/iommu.c +5 -2
index 5ad25dc..6e2e43a 100644
@@ -558,14 +558,16 @@ static void iommu_flush_all(void)
     }
 }
 
-static void __intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn,
-        int dma_old_pte_present, unsigned int page_count)
+static int iommu_flush_iotlb(struct domain *d, unsigned long gfn,
+                             bool_t dma_old_pte_present,
+                             unsigned int page_count)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
     struct acpi_drhd_unit *drhd;
     struct iommu *iommu;
     int flush_dev_iotlb;
     int iommu_domid;
+    int rc, ret = 0;
 
     /*
      * No need pcideves_lock here because we have flush @@ -584,29
+586,35 @@ static void __intel_iommu_iotlb_flush(struct domain *d,

unsigned long gfn,

             continue;

         if ( page_count != 1 || gfn == INVALID_GFN )
xen/drivers/passthrough/vtd/iommu.c +9 -5
-        {
-            if ( iommu_flush_iotlb_dsi(iommu, iommu_domid,
-                        0, flush_dev_iotlb) )
-                iommu_flush_write_buffer(iommu);
-        }
+            rc = iommu_flush_iotlb_dsi(iommu, iommu_domid,
+                                       0, flush_dev_iotlb);
         else
+            rc = iommu_flush_iotlb_psi(iommu, iommu_domid,
+                                       (paddr_t)gfn << PAGE_SHIFT_4K,
+                                       PAGE_ORDER_4K,
+                                       !dma_old_pte_present,
+                                       flush_dev_iotlb);
+
+        if ( rc > 0 )
         {
Quoted 9 lines

xen/drivers/passthrough/vtd/iommu.c static void __intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn, · line 601

Quoted 6 lines
xen/drivers/passthrough/vtd/iommu.c +2 -4
-            if ( iommu_flush_iotlb_psi(iommu, iommu_domid,
-                        (paddr_t)gfn << PAGE_SHIFT_4K, PAGE_ORDER_4K,
-                        !dma_old_pte_present, flush_dev_iotlb) )
-                iommu_flush_write_buffer(iommu);
+            iommu_flush_write_buffer(iommu);
+            ret = 0;

You don't need 'ret' here. Just change 'rc' to 0.

Agreed, this is really a good idea.

Quoted 6 lines

xen/drivers/passthrough/vtd/iommu.c

Quoted 3 lines
         }
xen/drivers/passthrough/vtd/iommu.c +2 -0
+        else if ( rc < 0 )
+            ret = rc;

Then this change is not required

Ditto.

Quoted 6 lines

xen/drivers/passthrough/vtd/iommu.c

Quoted 3 lines
     }
xen/drivers/passthrough/vtd/iommu.c +2 -0
+
+    return ret;

Then return 'rc' instead.

Ditto.

Quoted 9 lines
Quoted 5 lines

}

static void intel_iommu_iotlb_flush(struct domain *d, unsigned long
gfn, unsigned int
page_count)

could we remove "Intel_" prefix completely? You can rename this one to iommu_flush_iotlb_page...

Sure, I am ok.
I wonder why to remove "Intel_" prefix.
In this xen/drivers/passthrough/vtd/iommu.c file, most of functions are beginning with "intel_" as intel specific. In xen/drivers/passthrough/iommu.c file, most of functions are beginning with 'iommu_' as common part.

Quan

Quoted 5 lines
Quoted 5 lines

{

xen/drivers/passthrough/vtd/iommu.c +1 -1
-    __intel_iommu_iotlb_flush(d, gfn, 1, page_count);
+    iommu_flush_iotlb(d, gfn, 1, page_count);
 }
Quoted 10 lines
Quoted 1 line

static void intel_iommu_iotlb_flush_all(struct domain *d)

and this one just iommu_flush_iotlb_all

Quoted 5 lines

{

xen/drivers/passthrough/vtd/iommu.c +1 -1
-    __intel_iommu_iotlb_flush(d, INVALID_GFN, 0, 0);
+    iommu_flush_iotlb(d, INVALID_GFN, 0, 0);
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#4 lore raw

Re: [PATCH v3 01/10] vt-d: fix the IOMMU flush issue

Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 04.05.16 at 14:09, <[email protected]> wrote:
Quoted 9 lines · Kevin
Quoted 5 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM
static void intel_iommu_iotlb_flush(struct domain *d, unsigned long
gfn, unsigned int
page_count)

could we remove "Intel_" prefix completely? You can rename this one to iommu_flush_iotlb_page...

Quoted 6 lines

Sure, I am ok.
I wonder why to remove "Intel_" prefix.
In this xen/drivers/passthrough/vtd/iommu.c file, most of functions are beginning with "intel_" as intel specific.
In xen/drivers/passthrough/iommu.c file, most of functions are beginning with 'iommu_' as common part.

For non-static functions I'd suggest to keep the prefix (or use vtd_ alternatively). For static functions there's no need to disambiguate them, and hence an intel_ prefix is just mostly useless baggage.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#5 lore raw

Re: [PATCH v3 01/10] vt-d: fix the IOMMU flush issue

Quoted 19 lines · Jan Beulich
Quoted 12 lines
Quoted 1 line
Quoted 1 line · On 04.05.16 at 14:09, <[email protected]> wrote:
Quoted 9 lines · Kevin
Quoted 5 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM static void intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn, unsigned int
page_count)

could we remove "Intel_" prefix completely? You can rename this one to iommu_flush_iotlb_page...

Quoted 6 lines

Sure, I am ok.
I wonder why to remove "Intel_" prefix.
In this xen/drivers/passthrough/vtd/iommu.c file, most of functions are beginning with "intel_" as intel specific.
In xen/drivers/passthrough/iommu.c file, most of functions are beginning with 'iommu_' as common part.

Quoted 4 lines

For non-static functions I'd suggest to keep the prefix (or use vtd_ alternatively). For static functions there's no need to disambiguate them, and hence an intel_ prefix is just mostly useless baggage.

Thanks. The above two are both static functions. So I will follow Kevin's suggestion. Quan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#6 lore raw

PATCH v3 2/10 IOMMU: handle IOMMU mapping and unmapping failures

In reply to Quan Xu

Treat IOMMU mapping and unmapping failures as a fatal to the domain (with the exception of the hardware domain).

If IOMMU mapping and unmapping failed, crash the domain (with the exception of the hardware domain) and propagate the error up to the call trees.

CC Jan Beulich <[email protected]>
---
 xen/drivers/passthrough/iommu.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
xen/drivers/passthrough/iommu.c +28 -2
index b64676f..a0003ac 100644
@@ -243,21 +243,47 @@ int iommu_map_page(struct domain *d, unsigned long gfn, unsigned long mfn,
                    unsigned int flags)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
+    int rc;
 
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    return hd->platform_ops->map_page(d, gfn, mfn, flags);
+    rc = hd->platform_ops->map_page(d, gfn, mfn, flags);
+
+    if ( rc )
+    {
+        if ( is_hardware_domain(d) )
+            printk(XENLOG_ERR
+                   "iommu_map_page: IOMMU mapping gfn %#lx mfn %#lx failed for dom%d.",
+                   gfn, mfn, d->domain_id);
+        else
+            domain_crash(d);
+    }
+
+    return rc;
 }
 
 int iommu_unmap_page(struct domain *d, unsigned long gfn)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
+    int rc;
 
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    return hd->platform_ops->unmap_page(d, gfn);
+    rc = hd->platform_ops->unmap_page(d, gfn);
+
+    if ( rc )
+    {
+        if ( is_hardware_domain(d) )
+            printk(XENLOG_ERR
+                   "iommu_unmap_page: IOMMU unmapping gfn %#lx failed for dom%d.",
+                   gfn, d->domain_id);
+        else
+            domain_crash(d);
+    }
+
+    return rc;
 }
 
 static void iommu_free_pagetables(unsigned long unused)
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#7 lore raw

Re: [PATCH v3 02/10] IOMMU: handle IOMMU mapping and unmapping failures

Quoted 35 lines

From: Quan Xu
Sent: Friday, April 29, 2016 5:25 PM

Treat IOMMU mapping and unmapping failures as a fatal to the domain (with the exception of the hardware domain).

If IOMMU mapping and unmapping failed, crash the domain (with the exception of the hardware domain) and propagate the error up to the call trees.

CC Jan Beulich <[email protected]>
---
 xen/drivers/passthrough/iommu.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
xen/drivers/passthrough/iommu.c +3 -1
index b64676f..a0003ac 100644
@@ -243,21 +243,47 @@ int iommu_map_page(struct domain *d, unsigned long gfn,
 unsigned long mfn,
                    unsigned int flags)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
+    int rc;
 
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    return hd->platform_ops->map_page(d, gfn, mfn, flags);
+    rc = hd->platform_ops->map_page(d, gfn, mfn, flags);
+

xen/drivers/passthrough/iommu.c int iommu_map_page(struct domain *d, unsigned long gfn, · line 263

Quoted 10 lines
xen/drivers/passthrough/iommu.c +9 -0
+    if ( rc )
+    {
+        if ( is_hardware_domain(d) )
+            printk(XENLOG_ERR
+                   "iommu_map_page: IOMMU mapping gfn %#lx mfn %#lx failed for
 dom%d.",
+                   gfn, mfn, d->domain_id);
+        else
+            domain_crash(d);
+    }

It makes sense to print error messages for all domains, and then selectively crash domain:

printk(XENLOG_ERR ...);
if ( !is_hardware_domain(d) )

	domain_crash(d);

Thanks
Kevin
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#8 lore raw

Re: [PATCH v3 02/10] IOMMU: handle IOMMU mapping and unmapping failures

Quoted 47 lines · Kevin

xen/drivers/passthrough/iommu.c int iommu_map_page(struct domain *d,

Quoted 23 lines

From: Quan Xu
Sent: Friday, April 29, 2016 5:25 PM

Treat IOMMU mapping and unmapping failures as a fatal to the domain (with the exception of the hardware domain).

If IOMMU mapping and unmapping failed, crash the domain (with the exception of the hardware domain) and propagate the error up to the call trees.

CC Jan Beulich <[email protected]>
---
 xen/drivers/passthrough/iommu.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/xen/drivers/passthrough/iommu.c index b64676f..a0003ac 100644

xen/drivers/passthrough/iommu.c +0 -0
@@ -243,21 +243,47 @@ int iommu_map_page(struct domain *d,

unsigned

Quoted 12 lines
xen/drivers/passthrough/iommu.c +4 -1
 long gfn, unsigned long mfn,
                    unsigned int flags)  {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
+    int rc;
 
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    return hd->platform_ops->map_page(d, gfn, mfn, flags);
+    rc = hd->platform_ops->map_page(d, gfn, mfn, flags);
+
+    if ( rc )

xen/drivers/passthrough/iommu.c int iommu_map_page(struct domain *d, unsigned long gfn, unsigned long mfn, · line 261

Quoted 10 lines
xen/drivers/passthrough/iommu.c +9 -0
+    {
+        if ( is_hardware_domain(d) )
+            printk(XENLOG_ERR
+                   "iommu_map_page: IOMMU mapping gfn %#lx mfn %#lx
+ failed for
 dom%d.",
+                   gfn, mfn, d->domain_id);
+        else
+            domain_crash(d);
+    }
Quoted 7 lines

It makes sense to print error messages for all domains, and then selectively crash domain:

printk(XENLOG_ERR ...);
if ( !is_hardware_domain(d) )

	domain_crash(d);

But Jan said,
..

    if ( is_hardware_domain() )
        printk();
    else
        domain_crash();

..
is the better approach.

I remain neutral for this point. I think this is not a technical problem, but a matter of preference. This gap is subject to preventing spamming the log.

For iommu_map_page(), I think Kevin's suggestion is better, much more information for the crash domain, And also won't spam the log as we stop mapping against any error.

For iommu_unmap_page(),IOMMU unmapping should perhaps continue despite an error, in an attempt to do best effort cleanup. Then, Kevin's suggestion may spam the log.

Quan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#9 lore raw

Re: [PATCH v3 02/10] IOMMU: handle IOMMU mapping and unmapping failures

Quoted 57 lines
Quoted 1 line
Quoted 1 line · On 04.05.16 at 13:49, <[email protected]> wrote:
Quoted 47 lines · Kevin

xen/drivers/passthrough/iommu.c int iommu_map_page(struct domain *d,

Quoted 23 lines

From: Quan Xu
Sent: Friday, April 29, 2016 5:25 PM

Treat IOMMU mapping and unmapping failures as a fatal to the domain (with the exception of the hardware domain).

If IOMMU mapping and unmapping failed, crash the domain (with the exception of the hardware domain) and propagate the error up to the call trees.

CC Jan Beulich <[email protected]>
---
 xen/drivers/passthrough/iommu.c | 30 ++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/xen/drivers/passthrough/iommu.c index b64676f..a0003ac 100644

xen/drivers/passthrough/iommu.c +0 -0
@@ -243,21 +243,47 @@ int iommu_map_page(struct domain *d,

unsigned

Quoted 12 lines
xen/drivers/passthrough/iommu.c +4 -1
 long gfn, unsigned long mfn,
                    unsigned int flags)  {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
+    int rc;
 
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    return hd->platform_ops->map_page(d, gfn, mfn, flags);
+    rc = hd->platform_ops->map_page(d, gfn, mfn, flags);
+
+    if ( rc )

xen/drivers/passthrough/iommu.c int iommu_map_page(struct domain *d, unsigned long gfn, unsigned long mfn, · line 261

Quoted 10 lines
xen/drivers/passthrough/iommu.c +9 -0
+    {
+        if ( is_hardware_domain(d) )
+            printk(XENLOG_ERR
+                   "iommu_map_page: IOMMU mapping gfn %#lx mfn %#lx
+ failed for
 dom%d.",
+                   gfn, mfn, d->domain_id);
+        else
+            domain_crash(d);
+    }
Quoted 7 lines

It makes sense to print error messages for all domains, and then selectively crash domain:

printk(XENLOG_ERR ...);
if ( !is_hardware_domain(d) )

	domain_crash(d);
Quoted 8 lines

But Jan said,
..

    if ( is_hardware_domain() )
        printk();
    else
        domain_crash();

..
is the better approach.

Not exactly. All I complained about was that the Dom0 case went completely silently.

Quoted 2 lines

I remain neutral for this point. I think this is not a technical problem, but a matter of preference.

Indeed.

Quoted 9 lines

This gap is subject to preventing spamming the log.

For iommu_map_page(), I think Kevin's suggestion is better, much more information for the crash domain,
And also won't spam the log as we stop mapping against any error.

For iommu_unmap_page(),IOMMU unmapping should perhaps continue despite an error, in an attempt
to do best effort cleanup. Then, Kevin's suggestion may spam the log.

But I've always been saying that for multiple successive operations you shouldn't issue one message each.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#10 lore raw

Re: [PATCH v3 02/10] IOMMU: handle IOMMU mapping and unmapping failures

Quoted 67 lines · Jan Beulich
Quoted 58 lines
Quoted 1 line
Quoted 1 line · On 04.05.16 at 13:49, <[email protected]> wrote:
Quoted 48 lines · Kevin

xen/drivers/passthrough/iommu.c int iommu_map_page(struct domain *d,

Quoted 24 lines

From: Quan Xu
Sent: Friday, April 29, 2016 5:25 PM

Treat IOMMU mapping and unmapping failures as a fatal to the domain (with the exception of the hardware domain).

If IOMMU mapping and unmapping failed, crash the domain (with the exception of the hardware domain) and propagate the error up to the call trees.

CC Jan Beulich <[email protected]>
---
 xen/drivers/passthrough/iommu.c | 30
++++++++++++++++++++++++++++--
 1 file changed, 28 insertions(+), 2 deletions(-)
(quoted diff) +0 -0

b/xen/drivers/passthrough/iommu.c index b64676f..a0003ac 100644

xen/drivers/passthrough/iommu.c +0 -0
@@ -243,21 +243,47 @@ int iommu_map_page(struct domain *d,

unsigned

Quoted 12 lines
xen/drivers/passthrough/iommu.c +4 -1
 long gfn, unsigned long mfn,
                    unsigned int flags)  {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
+    int rc;
 
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    return hd->platform_ops->map_page(d, gfn, mfn, flags);
+    rc = hd->platform_ops->map_page(d, gfn, mfn, flags);
+
+    if ( rc )

xen/drivers/passthrough/iommu.c int iommu_map_page(struct domain *d, unsigned long gfn, unsigned long mfn, · line 261

Quoted 10 lines
xen/drivers/passthrough/iommu.c +9 -0
+    {
+        if ( is_hardware_domain(d) )
+            printk(XENLOG_ERR
+                   "iommu_map_page: IOMMU mapping gfn %#lx mfn
+ %#lx failed for
 dom%d.",
+                   gfn, mfn, d->domain_id);
+        else
+            domain_crash(d);
+    }
Quoted 7 lines

It makes sense to print error messages for all domains, and then selectively crash domain:

printk(XENLOG_ERR ...);
if ( !is_hardware_domain(d) )

	domain_crash(d);
Quoted 8 lines

But Jan said,
..

    if ( is_hardware_domain() )
        printk();
    else
        domain_crash();

..
is the better approach.

Quoted 8 lines

Not exactly. All I complained about was that the Dom0 case went completely silently.

Quoted 2 lines

I remain neutral for this point. I think this is not a technical problem, but a matter of preference.

Indeed.

If no other suggestions, I'll take Kevin's suggestion as a final conclusion for both iommu_map_page() and iommu_unmap_page().

Quan

Quoted 6 lines
Quoted 6 lines

This gap is subject to preventing spamming the log.

For iommu_map_page(), I think Kevin's suggestion is better, much more information for the crash domain, And also won't spam the log as we stop mapping against any error.

Quoted 7 lines
Quoted 1 line

For iommu_unmap_page(),IOMMU unmapping should perhaps continue

despite

Quoted 2 lines

an error, in an attempt to do best effort cleanup. Then, Kevin's suggestion may spam the log.

But I've always been saying that for multiple successive operations you shouldn't issue one message each.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#11 lore raw

PATCH v3 3/10 IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping

In reply to Quan Xu

When IOMMU mapping is failed, we issue a best effort rollback, stopping IOMMU mapping, unmapping the previous IOMMU maps and then reporting the error up to the call trees. When rollback is not feasible (in early initialization phase or trade-off of complexity) for the hardware domain, we do things on a best effort basis, only throwing out an error message.

IOMMU unmapping should perhaps continue despite an error, in an attempt to do best effort cleanup.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
CC Jun Nakajima <[email protected]>
CC Kevin Tian <[email protected]>
CC George Dunlap <[email protected]>
---
 xen/arch/x86/mm.c               | 13 ++++++++-----
 xen/arch/x86/mm/p2m-ept.c       | 27 +++++++++++++++++++++++++--
 xen/arch/x86/mm/p2m-pt.c        | 24 ++++++++++++++++++++----
 xen/arch/x86/mm/p2m.c           | 11 +++++++++--
 xen/drivers/passthrough/iommu.c | 14 +++++++++++++-
 5 files changed, 75 insertions(+), 14 deletions(-)
xen/arch/x86/mm.c +8 -5
index a42097f..427097d 100644
@@ -2467,7 +2467,7 @@ static int __get_page_type(struct page_info *page, unsigned long type,
                            int preemptible)
 {
     unsigned long nx, x, y = page->u.inuse.type_info;
-    int rc = 0;
+    int rc = 0, ret = 0;
 
     ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
 
@@ -2578,11 +2578,11 @@ static int __get_page_type(struct page_info *page, unsigned long type,
         if ( d && is_pv_domain(d) && unlikely(need_iommu(d)) )
         {
             if ( (x & PGT_type_mask) == PGT_writable_page )
-                iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
+                ret = iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
             else if ( type == PGT_writable_page )
-                iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
-                               page_to_mfn(page),
-                               IOMMUF_readable|IOMMUF_writable);
+                ret = iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
+                                     page_to_mfn(page),
+                                     IOMMUF_readable|IOMMUF_writable);
         }
     }
 
@@ -2599,6 +2599,9 @@ static int __get_page_type(struct page_info *page, unsigned long type,
     if ( (x & PGT_partial) && !(nx & PGT_partial) )
         put_page(page);
 
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }
 
xen/arch/x86/mm/p2m-ept.c +25 -2
index 1ed5b47..df87944 100644
@@ -821,6 +821,8 @@ out:
     if ( needs_sync )
         ept_sync_domain(p2m);
 
+    ret = 0;
+
     /* For host p2m, may need to change VT-d page table.*/
     if ( rc == 0 && p2m_is_hostp2m(p2m) && need_iommu(d) &&
          need_modify_vtd_table )
@@ -831,11 +833,29 @@ out:
         {
             if ( iommu_flags )
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+                {
+                    rc = iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+
+                    if ( !ret && unlikely(rc) )
+                    {
+                        while ( i-- )
+                            iommu_unmap_page(d, gfn + i);
+
+                        ret = rc;
+                        break;
+                    }
+                }
             else
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_unmap_page(d, gfn + i);
+                {
+                    rc = iommu_unmap_page(d, gfn + i);
+
+                    if ( !ret && unlikely(rc) )
+                        ret = rc;
+                }
         }
+
+        rc = 0;
     }
 
     unmap_domain_page(table);
@@ -850,6 +870,9 @@ out:
     if ( rc == 0 && p2m_is_hostp2m(p2m) )
         p2m_altp2m_propagate_change(d, _gfn(gfn), mfn, order, p2mt, p2ma);
 
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }
 
xen/arch/x86/mm/p2m-pt.c +20 -4
index 3d80612..9f5539e 100644
@@ -498,7 +498,7 @@ p2m_pt_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
     l1_pgentry_t intermediate_entry = l1e_empty();
     l2_pgentry_t l2e_content;
     l3_pgentry_t l3e_content;
-    int rc;
+    int rc, ret;
     unsigned int iommu_pte_flags = p2m_get_iommu_flags(p2mt);
     /*
      * old_mfn and iommu_old_flags control possible flush/update needs on the
@@ -680,11 +680,27 @@ p2m_pt_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
         }
         else if ( iommu_pte_flags )
             for ( i = 0; i < (1UL << page_order); i++ )
-                iommu_map_page(p2m->domain, gfn + i, mfn_x(mfn) + i,
-                               iommu_pte_flags);
+            {
+                ret = iommu_map_page(p2m->domain, gfn + i, mfn_x(mfn) + i,
+                                    iommu_pte_flags);
+
+                if ( !rc && unlikely(ret) )
+                {
+                    while ( i-- )
+                        iommu_unmap_page(p2m->domain, gfn + i);
+
+                    rc = ret;
+                    break;
+                }
+            }
         else
             for ( i = 0; i < (1UL << page_order); i++ )
-                iommu_unmap_page(p2m->domain, gfn + i);
+            {
+                ret = iommu_unmap_page(p2m->domain, gfn + i);
+
+                if ( !rc && unlikely(ret) )
+                    rc = ret;
+            }
     }
 
     /*
xen/arch/x86/mm/p2m.c +9 -2
index 6eef2f3..6a9bba1 100644
@@ -636,13 +636,20 @@ p2m_remove_page(struct p2m_domain *p2m, unsigned long gfn, unsigned long mfn,
     mfn_t mfn_return;
     p2m_type_t t;
     p2m_access_t a;
+    int rc = 0, ret;
 
     if ( !paging_mode_translate(p2m->domain) )
     {
         if ( need_iommu(p2m->domain) )
             for ( i = 0; i < (1 << page_order); i++ )
-                iommu_unmap_page(p2m->domain, mfn + i);
-        return 0;
+            {
+               ret = iommu_unmap_page(p2m->domain, mfn + i);
+
+               if ( !rc && unlikely(ret) )
+                   rc = ret;
+            }
+
+        return rc;
     }
 
     ASSERT(gfn_locked_by_me(p2m, gfn));
xen/drivers/passthrough/iommu.c +13 -1
index a0003ac..d74433d 100644
@@ -172,6 +172,8 @@ void __hwdom_init iommu_hwdom_init(struct domain *d)
     {
         struct page_info *page;
         unsigned int i = 0;
+        int ret, rc = 0;
+
         page_list_for_each ( page, &d->page_list )
         {
             unsigned long mfn = page_to_mfn(page);
@@ -182,10 +184,20 @@ void __hwdom_init iommu_hwdom_init(struct domain *d)
                  ((page->u.inuse.type_info & PGT_type_mask)
                   == PGT_writable_page) )
                 mapping |= IOMMUF_writable;
-            hd->platform_ops->map_page(d, gfn, mfn, mapping);
+
+            ret = hd->platform_ops->map_page(d, gfn, mfn, mapping);
+
+            if ( unlikely(ret) )
+                rc = ret;
+
             if ( !(i++ & 0xfffff) )
                 process_pending_softirqs();
         }
+
+        if ( rc )
+            printk(XENLOG_WARNING
+                   "iommu_hwdom_init: IOMMU mapping failed for dom%d.",
+                   d->domain_id);
     }
 
     return hd->platform_ops->hwdom_init(d);
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#12 lore raw

Re: [PATCH v3 03/10] IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping

Quoted 60 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

When IOMMU mapping is failed, we issue a best effort rollback, stopping IOMMU mapping, unmapping the previous IOMMU maps and then reporting the error up to the call trees. When rollback is not feasible (in early initialization phase or trade-off of complexity) for the hardware domain, we do things on a best effort basis, only throwing out an error message.

IOMMU unmapping should perhaps continue despite an error, in an attempt to do best effort cleanup.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
CC Jun Nakajima <[email protected]>
CC Kevin Tian <[email protected]>
CC George Dunlap <[email protected]>
---
 xen/arch/x86/mm.c               | 13 ++++++++-----
 xen/arch/x86/mm/p2m-ept.c       | 27 +++++++++++++++++++++++++--
 xen/arch/x86/mm/p2m-pt.c        | 24 ++++++++++++++++++++----
 xen/arch/x86/mm/p2m.c           | 11 +++++++++--
 xen/drivers/passthrough/iommu.c | 14 +++++++++++++-
 5 files changed, 75 insertions(+), 14 deletions(-)
xen/arch/x86/mm.c +5 -5
index a42097f..427097d 100644
@@ -2467,7 +2467,7 @@ static int __get_page_type(struct page_info *page, unsigned
 long type,
                            int preemptible)
 {
     unsigned long nx, x, y = page->u.inuse.type_info;
-    int rc = 0;
+    int rc = 0, ret = 0;
 
     ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
@@ -2578,11 +2578,11 @@ static int __get_page_type(struct page_info *page, unsigned
 long type,
         if ( d && is_pv_domain(d) && unlikely(need_iommu(d)) )
         {
             if ( (x & PGT_type_mask) == PGT_writable_page )
-                iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
+                ret = iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
             else if ( type == PGT_writable_page )
-                iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
-                               page_to_mfn(page),
-                               IOMMUF_readable|IOMMUF_writable);
+                ret = iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
+                                     page_to_mfn(page),
+                                     IOMMUF_readable|IOMMUF_writable);
         }
     }

xen/arch/x86/mm.c static int __get_page_type(struct page_info *page, unsigned · line 2607

Quoted 9 lines
xen/arch/x86/mm.c +3 -0
@@ -2599,6 +2599,9 @@ static int __get_page_type(struct page_info *page, unsigned
 long type,
     if ( (x & PGT_partial) && !(nx & PGT_partial) )
         put_page(page);
 
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }

I know there were quite some discussions before around above change (sorry I didn't remember all of them). Just based on mental picture we should return error where it firstly occurs. However above change looks favoring errors in later "rc = alloc_page_type" over earlier iommu_map/unmap_page error. Is it what we want? If there is a reason that we cannot return immediately upon iommu_map/unmap, it's more reasonable to revert above check like below:

	if ( !ret )
		ret = rc;

	return ret;
Quoted 15 lines
xen/arch/x86/mm/p2m-ept.c +2 -0
index 1ed5b47..df87944 100644
@@ -821,6 +821,8 @@ out:
     if ( needs_sync )
         ept_sync_domain(p2m);
 
+    ret = 0;
+
     /* For host p2m, may need to change VT-d page table.*/
     if ( rc == 0 && p2m_is_hostp2m(p2m) && need_iommu(d) &&
          need_modify_vtd_table )

xen/arch/x86/mm/p2m-ept.c out: · line 839

Quoted 8 lines
xen/arch/x86/mm/p2m-ept.c +4 -1
@@ -831,11 +833,29 @@ out:
         {
             if ( iommu_flags )
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+                {
+                    rc = iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+
+                    if ( !ret && unlikely(rc) )

I think you should move check of ret before iommu_map_page, since we should stop map against any error (either from best-effort unmap error side).

Quoted 12 lines
xen/arch/x86/mm/p2m-ept.c +9 -1
+                    {
+                        while ( i-- )
+                            iommu_unmap_page(d, gfn + i);
+
+                        ret = rc;
+                        break;
+                    }
+                }
             else
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_unmap_page(d, gfn + i);
+                {

xen/arch/x86/mm/p2m-ept.c out: · line 860

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +7 -0
+                    rc = iommu_unmap_page(d, gfn + i);
+
+                    if ( !ret && unlikely(rc) )
+                        ret = rc;
+                }
         }
+
+        rc = 0;
     }
 

Thanks
Kevin

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#13 lore raw

Re: [PATCH v3 03/10] IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping

Quoted 37 lines
Quoted 37 lines
Quoted 1 line · On 04.05.16 at 03:45, <[email protected]> wrote:

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

xen/arch/x86/mm.c +5 -5
@@ -2467,7 +2467,7 @@ static int __get_page_type(struct page_info *page, unsigned
 long type,
                            int preemptible)
 {
     unsigned long nx, x, y = page->u.inuse.type_info;
-    int rc = 0;
+    int rc = 0, ret = 0;
 
     ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
@@ -2578,11 +2578,11 @@ static int __get_page_type(struct page_info *page, unsigned
 long type,
         if ( d && is_pv_domain(d) && unlikely(need_iommu(d)) )
         {
             if ( (x & PGT_type_mask) == PGT_writable_page )
-                iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
+                ret = iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
             else if ( type == PGT_writable_page )
-                iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
-                               page_to_mfn(page),
-                               IOMMUF_readable|IOMMUF_writable);
+                ret = iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
+                                     page_to_mfn(page),
+                                     IOMMUF_readable|IOMMUF_writable);
         }
     }
@@ -2599,6 +2599,9 @@ static int __get_page_type(struct page_info *page, unsigned
 long type,
     if ( (x & PGT_partial) && !(nx & PGT_partial) )
         put_page(page);
 
Quoted 11 lines

xen/arch/x86/mm.c static int __get_page_type(struct page_info *page, unsigned long type, · line 2606

Quoted 5 lines
xen/arch/x86/mm.c +3 -0
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }

I know there were quite some discussions before around above change (sorry I didn't remember all of them). Just based on mental picture we should return error where it firstly occurs. However above change looks favoring errors in later "rc = alloc_page_type" over earlier iommu_map/unmap_page error. Is it what we want?

Yes, as that's the primary operation here.

Quoted 2 lines

If there is a reason that we cannot return immediately upon iommu_map/unmap,

Since for Dom0 we don't call domain_crash(), we must not bypass alloc_page_type() here. And even for DomU it would seem at least fragile if we did - we better don't alter the refcounting behavior.

Quoted 15 lines
Quoted 15 lines
xen/arch/x86/mm/p2m-ept.c +2 -0
@@ -821,6 +821,8 @@ out:
     if ( needs_sync )
         ept_sync_domain(p2m);
 
+    ret = 0;
+
     /* For host p2m, may need to change VT-d page table.*/
     if ( rc == 0 && p2m_is_hostp2m(p2m) && need_iommu(d) &&
          need_modify_vtd_table )
@@ -831,11 +833,29 @@ out:
         {
             if ( iommu_flags )
                 for ( i = 0; i < (1 << order); i++ )
Quoted 8 lines

xen/arch/x86/mm/p2m-ept.c out: · line 839

Quoted 5 lines
xen/arch/x86/mm/p2m-ept.c +4 -1
-                    iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+                {
+                    rc = iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+
+                    if ( !ret && unlikely(rc) )

I think you should move check of ret before iommu_map_page, since we should stop map against any error (either from best-effort unmap error side).

Considering ret getting set to zero ahead of the loop plus ...

Quoted 6 lines

xen/arch/x86/mm/p2m-ept.c out: · line 845

Quoted 6 lines
xen/arch/x86/mm/p2m-ept.c +6 -0
+                    {
+                        while ( i-- )
+                            iommu_unmap_page(d, gfn + i);
+
+                        ret = rc;
+                        break;

... this, it looks to me as if the checking of ret above is simply unnecessary.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#14 lore raw

Re: [PATCH v3 03/10] IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping

Quoted 50 lines · Jan Beulich
Quoted 38 lines
Quoted 38 lines
Quoted 1 line · On 04.05.16 at 03:45, <[email protected]> wrote:

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

xen/arch/x86/mm.c +7 -5
@@ -2467,7 +2467,7 @@ static int __get_page_type(struct page_info
 *page, unsigned long type,
                            int preemptible)  {
     unsigned long nx, x, y = page->u.inuse.type_info;
-    int rc = 0;
+    int rc = 0, ret = 0;
 
     ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
 
@@ -2578,11 +2578,11 @@ static int __get_page_type(struct page_info
 *page, unsigned long type,
         if ( d && is_pv_domain(d) && unlikely(need_iommu(d)) )
         {
             if ( (x & PGT_type_mask) == PGT_writable_page )
-                iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
+                ret = iommu_unmap_page(d, mfn_to_gmfn(d,
+ page_to_mfn(page)));
             else if ( type == PGT_writable_page )
-                iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
-                               page_to_mfn(page),
-                               IOMMUF_readable|IOMMUF_writable);
+                ret = iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
+                                     page_to_mfn(page),
+
+ IOMMUF_readable|IOMMUF_writable);
         }
     }
@@ -2599,6 +2599,9 @@ static int __get_page_type(struct page_info
 *page, unsigned long type,
     if ( (x & PGT_partial) && !(nx & PGT_partial) )
         put_page(page);
 
Quoted 11 lines

xen/arch/x86/mm.c static int __get_page_type(struct page_info *page, unsigned long type, · line 2606

Quoted 5 lines
xen/arch/x86/mm.c +3 -0
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }

I know there were quite some discussions before around above change (sorry I didn't remember all of them). Just based on mental picture we should return error where it firstly occurs. However above change looks favoring errors in later "rc = alloc_page_type" over earlier iommu_map/unmap_page error. Is it what we want?

Quoted 9 lines

Yes, as that's the primary operation here.

Quoted 2 lines

If there is a reason that we cannot return immediately upon iommu_map/unmap,

Since for Dom0 we don't call domain_crash(), we must not bypass alloc_page_type() here. And even for DomU it would seem at least fragile if we did - we better don't alter the refcounting behavior.

A little bit confused.
Check it, for this __get_page_type(), can I leave my modification as is?

Quoted 28 lines
Quoted 16 lines
Quoted 16 lines
xen/arch/x86/mm/p2m-ept.c +2 -1
@@ -821,6 +821,8 @@ out:
     if ( needs_sync )
         ept_sync_domain(p2m);
 
+    ret = 0;
+
     /* For host p2m, may need to change VT-d page table.*/
     if ( rc == 0 && p2m_is_hostp2m(p2m) && need_iommu(d) &&
          need_modify_vtd_table )
@@ -831,11 +833,29 @@ out:
         {
             if ( iommu_flags )
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
Quoted 8 lines

xen/arch/x86/mm/p2m-ept.c out: · line 839

Quoted 5 lines
xen/arch/x86/mm/p2m-ept.c +5 -0
+                {
+                    rc = iommu_map_page(d, gfn + i, mfn_x(mfn) + i,
+ iommu_flags);
+
+                    if ( !ret && unlikely(rc) )

I think you should move check of ret before iommu_map_page, since we should stop map against any error (either from best-effort unmap error

side).

Considering ret getting set to zero ahead of the loop plus ...

Quoted 9 lines
Quoted 6 lines

xen/arch/x86/mm/p2m-ept.c out: · line 845

Quoted 6 lines
xen/arch/x86/mm/p2m-ept.c +6 -0
+                    {
+                        while ( i-- )
+                            iommu_unmap_page(d, gfn + i);
+
+                        ret = rc;
+                        break;

... this, it looks to me as if the checking of ret above is simply unnecessary.

Make sense. I'll drop ret check.

Quan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#15 lore raw

Re: [PATCH v3 03/10] IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping

Quoted 57 lines
Quoted 1 line
Quoted 1 line · On 04.05.16 at 11:13, <[email protected]> wrote:
Quoted 55 lines · Jan Beulich
Quoted 38 lines
Quoted 38 lines
Quoted 1 line · On 04.05.16 at 03:45, <[email protected]> wrote:

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

xen/arch/x86/mm.c +7 -5
@@ -2467,7 +2467,7 @@ static int __get_page_type(struct page_info
 *page, unsigned long type,
                            int preemptible)  {
     unsigned long nx, x, y = page->u.inuse.type_info;
-    int rc = 0;
+    int rc = 0, ret = 0;
 
     ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
 
@@ -2578,11 +2578,11 @@ static int __get_page_type(struct page_info
 *page, unsigned long type,
         if ( d && is_pv_domain(d) && unlikely(need_iommu(d)) )
         {
             if ( (x & PGT_type_mask) == PGT_writable_page )
-                iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
+                ret = iommu_unmap_page(d, mfn_to_gmfn(d,
+ page_to_mfn(page)));
             else if ( type == PGT_writable_page )
-                iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
-                               page_to_mfn(page),
-                               IOMMUF_readable|IOMMUF_writable);
+                ret = iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
+                                     page_to_mfn(page),
+
+ IOMMUF_readable|IOMMUF_writable);
         }
     }
@@ -2599,6 +2599,9 @@ static int __get_page_type(struct page_info
 *page, unsigned long type,
     if ( (x & PGT_partial) && !(nx & PGT_partial) )
         put_page(page);
 
Quoted 11 lines

xen/arch/x86/mm.c static int __get_page_type(struct page_info *page, unsigned long type, · line 2606

Quoted 5 lines
xen/arch/x86/mm.c +3 -0
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }

I know there were quite some discussions before around above change (sorry I didn't remember all of them). Just based on mental picture we should return error where it firstly occurs. However above change looks favoring errors in later "rc = alloc_page_type" over earlier iommu_map/unmap_page error. Is it what we want?

Yes, as that's the primary operation here.

Quoted 2 lines

If there is a reason that we cannot return immediately upon iommu_map/unmap,

Quoted 7 lines
Quoted 4 lines

Since for Dom0 we don't call domain_crash(), we must not bypass alloc_page_type() here. And even for DomU it would seem at least fragile if we did - we better don't alter the refcounting behavior.

A little bit confused.
Check it, for this __get_page_type(), can I leave my modification as is?

Yes. I merely tried to explain the reason to Kevin.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#16 lore raw

Re: [PATCH v3 03/10] IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping

Quoted 66 lines

From: Jan Beulich [mailto:[email protected]]
Sent: Wednesday, May 04, 2016 5:29 PM

Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 04.05.16 at 11:13, <[email protected]> wrote:
Quoted 14 lines · Jan Beulich
Quoted 14 lines
Quoted 14 lines
Quoted 1 line · On 04.05.16 at 03:45, <[email protected]> wrote:

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

xen/arch/x86/mm.c +1 -1
@@ -2467,7 +2467,7 @@ static int __get_page_type(struct page_info
 *page, unsigned long type,
                            int preemptible)  {
     unsigned long nx, x, y = page->u.inuse.type_info;
-    int rc = 0;
+    int rc = 0, ret = 0;
 
     ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
 
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
xen/arch/x86/mm.c +0 -0
@@ -2578,11 +2578,11 @@ static int __get_page_type(struct page_info
 *page, unsigned long type,
         if ( d && is_pv_domain(d) && unlikely(need_iommu(d)) )
         {
             if ( (x & PGT_type_mask) == PGT_writable_page )
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/mm.c static int __get_page_type(struct page_info *page, unsigned long type,

Quoted 8 lines
xen/arch/x86/mm.c +3 -4
-                iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
+                ret = iommu_unmap_page(d, mfn_to_gmfn(d,
+ page_to_mfn(page)));
             else if ( type == PGT_writable_page )
-                iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
-                               page_to_mfn(page),
-                               IOMMUF_readable|IOMMUF_writable);
+                ret = iommu_map_page(d, mfn_to_gmfn(d,

page_to_mfn(page)),

Quoted 33 lines
Quoted 32 lines
Quoted 11 lines
Quoted 11 lines
xen/arch/x86/mm.c +3 -0
+                                     page_to_mfn(page),
+
+ IOMMUF_readable|IOMMUF_writable);
         }
     }
@@ -2599,6 +2599,9 @@ static int __get_page_type(struct page_info
 *page, unsigned long type,
     if ( (x & PGT_partial) && !(nx & PGT_partial) )
         put_page(page);
 
Quoted 11 lines

xen/arch/x86/mm.c static int __get_page_type(struct page_info *page, unsigned long type, · line 2606

Quoted 5 lines
xen/arch/x86/mm.c +3 -0
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }

I know there were quite some discussions before around above change (sorry I didn't remember all of them). Just based on mental picture we should return error where it firstly occurs. However above change looks favoring errors in later "rc = alloc_page_type" over earlier iommu_map/unmap_page error. Is it what we want?

Yes, as that's the primary operation here.

Quoted 2 lines

If there is a reason that we cannot return immediately upon iommu_map/unmap,

Since for Dom0 we don't call domain_crash(), we must not bypass alloc_page_type() here. And even for DomU it would seem at least fragile if we did - we better don't alter the refcounting behavior.

Quoted 5 lines
Quoted 2 lines

A little bit confused.
Check it, for this __get_page_type(), can I leave my modification as is?

Yes. I merely tried to explain the reason to Kevin.

Understand now.

Thanks
Kevin

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#17 lore raw

Re: [PATCH v3 03/10] IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping

In reply to Quan Xu
Quoted 97 lines · Quan Xu

When IOMMU mapping is failed, we issue a best effort rollback, stopping IOMMU mapping, unmapping the previous IOMMU maps and then reporting the error up to the call trees. When rollback is not feasible (in early initialization phase or trade-off of complexity) for the hardware domain, we do things on a best effort basis, only throwing out an error message.

IOMMU unmapping should perhaps continue despite an error, in an attempt to do best effort cleanup.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
CC Jun Nakajima <[email protected]>
CC Kevin Tian <[email protected]>
CC George Dunlap <[email protected]>
---
 xen/arch/x86/mm.c               | 13 ++++++++-----
 xen/arch/x86/mm/p2m-ept.c       | 27 +++++++++++++++++++++++++--
 xen/arch/x86/mm/p2m-pt.c        | 24 ++++++++++++++++++++----
 xen/arch/x86/mm/p2m.c           | 11 +++++++++--
 xen/drivers/passthrough/iommu.c | 14 +++++++++++++-
 5 files changed, 75 insertions(+), 14 deletions(-)
xen/arch/x86/mm.c +8 -5
index a42097f..427097d 100644
@@ -2467,7 +2467,7 @@ static int __get_page_type(struct page_info *page, unsigned long type,
                            int preemptible)
 {
     unsigned long nx, x, y = page->u.inuse.type_info;
-    int rc = 0;
+    int rc = 0, ret = 0;
 
     ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
 
@@ -2578,11 +2578,11 @@ static int __get_page_type(struct page_info *page, unsigned long type,
         if ( d && is_pv_domain(d) && unlikely(need_iommu(d)) )
         {
             if ( (x & PGT_type_mask) == PGT_writable_page )
-                iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
+                ret = iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
             else if ( type == PGT_writable_page )
-                iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
-                               page_to_mfn(page),
-                               IOMMUF_readable|IOMMUF_writable);
+                ret = iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
+                                     page_to_mfn(page),
+                                     IOMMUF_readable|IOMMUF_writable);
         }
     }
 
@@ -2599,6 +2599,9 @@ static int __get_page_type(struct page_info *page, unsigned long type,
     if ( (x & PGT_partial) && !(nx & PGT_partial) )
         put_page(page);
 
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }
 
xen/arch/x86/mm/p2m-ept.c +14 -2
index 1ed5b47..df87944 100644
@@ -821,6 +821,8 @@ out:
     if ( needs_sync )
         ept_sync_domain(p2m);
 
+    ret = 0;
+
     /* For host p2m, may need to change VT-d page table.*/
     if ( rc == 0 && p2m_is_hostp2m(p2m) && need_iommu(d) &&
          need_modify_vtd_table )
@@ -831,11 +833,29 @@ out:
         {
             if ( iommu_flags )
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+                {
+                    rc = iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+
+                    if ( !ret && unlikely(rc) )
+                    {
+                        while ( i-- )
+                            iommu_unmap_page(d, gfn + i);
+
+                        ret = rc;
+                        break;
+                    }
+                }
             else
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_unmap_page(d, gfn + i);

xen/arch/x86/mm/p2m-ept.c out: · line 859

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +8 -0
+                {
+                    rc = iommu_unmap_page(d, gfn + i);
+
+                    if ( !ret && unlikely(rc) )
+                        ret = rc;
+                }
         }
+
+        rc = 0;
     }

So the reason for doing this thing with setting ret=0, then using rc, then setting rc to zero, is to make sure that the change is propagated to the altp2m if necessary?

I'm not a fan of this sort of "implied signalling". The check at the altp2m conditional is meant to say, "everything went fine, go ahead and propagate the change". But with your changes, that's not what we want anymore -- we want, "If the change actually made it into the hostp2m, propagate it to the altp2m."

As such, I think it would be a lot clearer to just make a new boolean variable, maybe "entry_written", which we initialize to false and then set to true when the entry is actually written; and then change the condition re the altp2m to "if ( entry_written && p2m_is_hostp2m(..) )".

Then I think you could make the ret / rc use mirror what you do elsewhere, where ret is used to store the return value of the function call, and it's propagated to rc only if rc is not already set.

Quoted 8 lines
xen/arch/x86/mm/p2m-ept.c +2 -2
@@ -680,11 +680,27 @@ p2m_pt_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
         }
         else if ( iommu_pte_flags )
             for ( i = 0; i < (1UL << page_order); i++ )
-                iommu_map_page(p2m->domain, gfn + i, mfn_x(mfn) + i,
-                               iommu_pte_flags);
+            {
+                ret = iommu_map_page(p2m->domain, gfn + i, mfn_x(mfn) + i,

xen/arch/x86/mm/p2m-ept.c p2m_pt_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn, · line 694

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +10 -0
+                                    iommu_pte_flags);
+
+                if ( !rc && unlikely(ret) )
+                {
+                    while ( i-- )
+                        iommu_unmap_page(p2m->domain, gfn + i);
+
+                    rc = ret;
+                    break;
+                }

Hmm, is this conditional here right? What the code actually says to do is to always map pages, but to only unmap them on an error if there have been no other errors in the function so far.

As it happens, at the moment rc cannot be non-zero here since any time it's non-zero the code jumps down to the 'out' label, skipping this. But if that ever changed, this would fail to unmap when it probably should.

It seems like this be:

if ( unlikely(ret) )
{
while ( i-- )

    iommu_unmap_page(p2m->domain, gfn + i);
  if ( !rc )
    rc = ret;

break;
}

Or if you want to assume that rc will never be zero, then put an ASSERT() just before the for loop here.

Everything else looks good to me. Thanks again for your work on this.

-George

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#18 lore raw

Re: [PATCH v3 03/10] IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping

Quoted 130 lines · George Dunlap
Quoted 2 lines · Quan Xu

When IOMMU mapping is failed, we issue a best effort rollback, stopping IOMMU mapping, unmapping the previous IOMMU maps and

then

Quoted 3 lines

reporting the error up to the call trees. When rollback is not feasible (in early initialization phase or trade-off of complexity) for the hardware domain, we do things on a best effort basis, only throwing

out an error message.

xen/arch/x86/mm.c static int __get_page_type(struct page_info

Quoted 25 lines

IOMMU unmapping should perhaps continue despite an error, in an attempt to do best effort cleanup.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
CC Jun Nakajima <[email protected]>
CC Kevin Tian <[email protected]>
CC George Dunlap <[email protected]>
---
 xen/arch/x86/mm.c               | 13 ++++++++-----
 xen/arch/x86/mm/p2m-ept.c       | 27 +++++++++++++++++++++++++--
 xen/arch/x86/mm/p2m-pt.c        | 24 ++++++++++++++++++++----
 xen/arch/x86/mm/p2m.c           | 11 +++++++++--
 xen/drivers/passthrough/iommu.c | 14 +++++++++++++-
 5 files changed, 75 insertions(+), 14 deletions(-)
xen/arch/x86/mm.c index renamed from xen/arch/x86/mm.c +0 -0

a42097f..427097d 100644

xen/arch/x86/mm.c +0 -0
@@ -2467,7 +2467,7 @@ static int __get_page_type(struct page_info

*page, unsigned long type,

xen/arch/x86/mm.c static int __get_page_type(struct page_info

Quoted 8 lines
xen/arch/x86/mm.c +1 -1
                            int preemptible)  {
     unsigned long nx, x, y = page->u.inuse.type_info;
-    int rc = 0;
+    int rc = 0, ret = 0;
 
     ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
 
@@ -2578,11 +2578,11 @@ static int __get_page_type(struct page_info

*page, unsigned long type,

xen/arch/x86/mm.c static int __get_page_type(struct page_info · line 2581

Quoted 5 lines
xen/arch/x86/mm.c +1 -1
         if ( d && is_pv_domain(d) && unlikely(need_iommu(d)) )
         {
             if ( (x & PGT_type_mask) == PGT_writable_page )
-                iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
+                ret = iommu_unmap_page(d, mfn_to_gmfn(d,

page_to_mfn(page)));

xen/arch/x86/mm.c static int __get_page_type(struct page_info

Quoted 11 lines
xen/arch/x86/mm.c +3 -3
             else if ( type == PGT_writable_page )
-                iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
-                               page_to_mfn(page),
-                               IOMMUF_readable|IOMMUF_writable);
+                ret = iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
+                                     page_to_mfn(page),
+                                     IOMMUF_readable|IOMMUF_writable);
         }
     }
 
@@ -2599,6 +2599,9 @@ static int __get_page_type(struct page_info

*page, unsigned long type,

Quoted 42 lines
xen/arch/x86/mm.c +3 -0
     if ( (x & PGT_partial) && !(nx & PGT_partial) )
         put_page(page);
 
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }
 
xen/arch/x86/mm/p2m-ept.c +14 -2
index 1ed5b47..df87944 100644
@@ -821,6 +821,8 @@ out:
     if ( needs_sync )
         ept_sync_domain(p2m);
 
+    ret = 0;
+
     /* For host p2m, may need to change VT-d page table.*/
     if ( rc == 0 && p2m_is_hostp2m(p2m) && need_iommu(d) &&
          need_modify_vtd_table )
@@ -831,11 +833,29 @@ out:
         {
             if ( iommu_flags )
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+                {
+                    rc = iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+
+                    if ( !ret && unlikely(rc) )
+                    {
+                        while ( i-- )
+                            iommu_unmap_page(d, gfn + i);
+
+                        ret = rc;
+                        break;
+                    }
+                }
             else
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_unmap_page(d, gfn + i);

xen/arch/x86/mm/p2m-ept.c out: · line 859

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +8 -0
+                {
+                    rc = iommu_unmap_page(d, gfn + i);
+
+                    if ( !ret && unlikely(rc) )
+                        ret = rc;
+                }
         }
+
+        rc = 0;
     }

So the reason for doing this thing with setting ret=0, then using rc, then setting rc to zero, is to make sure that the change is propagated to the altp2m if necessary?

I'm not a fan of this sort of "implied signalling". The check at the altp2m conditional is meant to say, "everything went fine, go ahead and propagate the change". But with your changes, that's not what we want anymore -- we want, "If the change actually made it into the hostp2m, propagate it to the altp2m."

As such, I think it would be a lot clearer to just make a new boolean variable, maybe "entry_written", which we initialize to false and then set to true when the entry is actually written; and then change the condition re the altp2m to "if ( entry_written && p2m_is_hostp2m(..) )".

Quoted 4 lines

Then I think you could make the ret / rc use mirror what you do elsewhere, where ret is used to store the return value of the function call, and it's propagated to rc only if rc is not already set.

George,

Thanks for your detailed explanation. This seems an another matter of preference.
Of course, I'd follow your suggestion in p2m field, while I need to hear the other maintainers' options as well (especially when it comes from Kevin/Jan, as they do spend a lot of time for me).

A little bit of difference here, IMO, an int 'entry_written' is much better, as we also need to propagate the 'entry_written' up to callers, i.e. the errors returned from atomic_write_ept_entry() are '-EINVAL', '-ESRCH' and '-EBUSY'. Then, the follow is my modification (feel free to correct me):

xen/arch/x86/mm/p2m-ept.c +29 -8
@@ -666,7 +666,7 @@ ept_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
     ept_entry_t *table, *ept_entry = NULL;
     unsigned long gfn_remainder = gfn;
     unsigned int i, target = order / EPT_TABLE_ORDER;
-    int ret, rc = 0;
+    int ret, rc = 0, entry_written = 0;
     bool_t direct_mmio = (p2mt == p2m_mmio_direct);
     uint8_t ipat = 0;
     bool_t need_modify_vtd_table = 1;
@@ -763,8 +763,8 @@ ept_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
 
         /* now install the newly split ept sub-tree */
         /* NB: please make sure domian is paused and no in-fly VT-d DMA. */
-        rc = atomic_write_ept_entry(ept_entry, split_ept_entry, i);
-        ASSERT(rc == 0);
+        entry_written = atomic_write_ept_entry(ept_entry, split_ept_entry, i);
+        ASSERT(entry_written == 0);
 
         /* then move to the level we want to make real changes */
         for ( ; i > target; i-- )
@@ -809,9 +809,12 @@ ept_set_entry(struct p2m_domain *p2m, unsigned long gfn, mfn_t mfn,
         new_entry.suppress_ve = is_epte_valid(&old_entry) ?
                                     old_entry.suppress_ve : 1;
 
-    rc = atomic_write_ept_entry(ept_entry, new_entry, target);
-    if ( unlikely(rc) )
+    entry_written = atomic_write_ept_entry(ept_entry, new_entry, target);
+    if ( unlikely(entry_written) )
+    {
         old_entry.epte = 0;
+        rc = entry_written;
+    }
     else if ( p2mt != p2m_invalid &&
               (gfn + (1UL << order) - 1 > p2m->max_mapped_pfn) )
         /* Track the highest gfn for which we have ever had a valid mapping */
@@ -822,7 +825,7 @@ out:
         ept_sync_domain(p2m);
 
     /* For host p2m, may need to change VT-d page table.*/
-    if ( rc == 0 && p2m_is_hostp2m(p2m) && need_iommu(d) &&
+    if ( entry_written == 0 && p2m_is_hostp2m(p2m) && need_iommu(d) &&
          need_modify_vtd_table )
     {
         if ( iommu_hap_pt_share )
@@ -831,10 +834,28 @@ out:
         {
             if ( iommu_flags )
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+                {
+                    ret = iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+
+                    if ( unlikely(ret) )
+                    {
+                        while ( i-- )
+                            iommu_unmap_page(p2m->domain, gfn + i);
+
+                        if ( !rc )
+                            rc = ret;
+
+                        break;
+                    }
+                }
             else
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_unmap_page(d, gfn + i);
+                {
+                    ret = iommu_unmap_page(d, gfn + i);
+
+                    if ( !rc && unlikely(ret) )
+                        rc = ret;
+                }
         }
     }
 
Quoted 31 lines

xen/arch/x86/mm/p2m-ept.c p2m_pt_set_entry(struct p2m_domain *p2m,

Quoted 1 line
xen/arch/x86/mm/p2m-ept.c +0 -0
@@ -680,11 +680,27 @@ p2m_pt_set_entry(struct p2m_domain *p2m,

unsigned long gfn, mfn_t mfn,

Quoted 7 lines
xen/arch/x86/mm/p2m-ept.c +2 -2
         }
         else if ( iommu_pte_flags )
             for ( i = 0; i < (1UL << page_order); i++ )
-                iommu_map_page(p2m->domain, gfn + i, mfn_x(mfn) + i,
-                               iommu_pte_flags);
+            {
+                ret = iommu_map_page(p2m->domain, gfn + i, mfn_x(mfn) + i,

xen/arch/x86/mm/p2m-ept.c p2m_pt_set_entry(struct p2m_domain *p2m, · line 694

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +10 -0
+                                    iommu_pte_flags);
+
+                if ( !rc && unlikely(ret) )
+                {
+                    while ( i-- )
+                        iommu_unmap_page(p2m->domain, gfn + i);
+
+                    rc = ret;
+                    break;
+                }

Hmm, is this conditional here right? What the code actually says to do is to always map pages, but to only unmap them on an error if there have been no other errors in the function so far.

As it happens, at the moment rc cannot be non-zero here since any time it's non-zero the code jumps down to the 'out' label, skipping this. But if that ever changed, this would fail to unmap when it probably should.

It seems like this be:

Quoted 9 lines

if ( unlikely(ret) )
{
while ( i-- )

    iommu_unmap_page(p2m->domain, gfn + i);
  if ( !rc )
    rc = ret;

break;
}

Looks good. Thanks.

Quoted 5 lines

Or if you want to assume that rc will never be zero, then put an ASSERT() just before the for loop here.

Everything else looks good to me. Thanks again for your work on this.

Quan
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#19 lore raw

Re: [PATCH v3 03/10] IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping

Quoted 136 lines · Quan
Quoted 130 lines · George Dunlap
Quoted 2 lines · Quan Xu

When IOMMU mapping is failed, we issue a best effort rollback, stopping IOMMU mapping, unmapping the previous IOMMU maps and

then

Quoted 3 lines

reporting the error up to the call trees. When rollback is not feasible (in early initialization phase or trade-off of complexity) for the hardware domain, we do things on a best effort basis, only throwing

out an error message.

xen/arch/x86/mm.c static int __get_page_type(struct page_info

Quoted 25 lines

IOMMU unmapping should perhaps continue despite an error, in an attempt to do best effort cleanup.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
CC Jun Nakajima <[email protected]>
CC Kevin Tian <[email protected]>
CC George Dunlap <[email protected]>
---
 xen/arch/x86/mm.c               | 13 ++++++++-----
 xen/arch/x86/mm/p2m-ept.c       | 27 +++++++++++++++++++++++++--
 xen/arch/x86/mm/p2m-pt.c        | 24 ++++++++++++++++++++----
 xen/arch/x86/mm/p2m.c           | 11 +++++++++--
 xen/drivers/passthrough/iommu.c | 14 +++++++++++++-
 5 files changed, 75 insertions(+), 14 deletions(-)
xen/arch/x86/mm.c index renamed from xen/arch/x86/mm.c +0 -0

a42097f..427097d 100644

xen/arch/x86/mm.c +0 -0
@@ -2467,7 +2467,7 @@ static int __get_page_type(struct page_info

*page, unsigned long type,

xen/arch/x86/mm.c static int __get_page_type(struct page_info

Quoted 8 lines
xen/arch/x86/mm.c +1 -1
                            int preemptible)  {
     unsigned long nx, x, y = page->u.inuse.type_info;
-    int rc = 0;
+    int rc = 0, ret = 0;
 
     ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
 
@@ -2578,11 +2578,11 @@ static int __get_page_type(struct page_info

*page, unsigned long type,

xen/arch/x86/mm.c static int __get_page_type(struct page_info · line 2581

Quoted 5 lines
xen/arch/x86/mm.c +1 -1
         if ( d && is_pv_domain(d) && unlikely(need_iommu(d)) )
         {
             if ( (x & PGT_type_mask) == PGT_writable_page )
-                iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
+                ret = iommu_unmap_page(d, mfn_to_gmfn(d,

page_to_mfn(page)));

xen/arch/x86/mm.c static int __get_page_type(struct page_info

Quoted 11 lines
xen/arch/x86/mm.c +3 -3
             else if ( type == PGT_writable_page )
-                iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
-                               page_to_mfn(page),
-                               IOMMUF_readable|IOMMUF_writable);
+                ret = iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
+                                     page_to_mfn(page),
+                                     IOMMUF_readable|IOMMUF_writable);
         }
     }
 
@@ -2599,6 +2599,9 @@ static int __get_page_type(struct page_info

*page, unsigned long type,

Quoted 42 lines
xen/arch/x86/mm.c +3 -0
     if ( (x & PGT_partial) && !(nx & PGT_partial) )
         put_page(page);
 
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }
 
xen/arch/x86/mm/p2m-ept.c +14 -2
index 1ed5b47..df87944 100644
@@ -821,6 +821,8 @@ out:
     if ( needs_sync )
         ept_sync_domain(p2m);
 
+    ret = 0;
+
     /* For host p2m, may need to change VT-d page table.*/
     if ( rc == 0 && p2m_is_hostp2m(p2m) && need_iommu(d) &&
          need_modify_vtd_table )
@@ -831,11 +833,29 @@ out:
         {
             if ( iommu_flags )
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+                {
+                    rc = iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+
+                    if ( !ret && unlikely(rc) )
+                    {
+                        while ( i-- )
+                            iommu_unmap_page(d, gfn + i);
+
+                        ret = rc;
+                        break;
+                    }
+                }
             else
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_unmap_page(d, gfn + i);

xen/arch/x86/mm/p2m-ept.c out: · line 859

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +8 -0
+                {
+                    rc = iommu_unmap_page(d, gfn + i);
+
+                    if ( !ret && unlikely(rc) )
+                        ret = rc;
+                }
         }
+
+        rc = 0;
     }

So the reason for doing this thing with setting ret=0, then using rc, then setting rc to zero, is to make sure that the change is propagated to the altp2m if necessary?

I'm not a fan of this sort of "implied signalling". The check at the altp2m conditional is meant to say, "everything went fine, go ahead and propagate the change". But with your changes, that's not what we want anymore -- we want, "If the change actually made it into the hostp2m, propagate it to the altp2m."

As such, I think it would be a lot clearer to just make a new boolean variable, maybe "entry_written", which we initialize to false and then set to true when the entry is actually written; and then change the condition re the altp2m to "if ( entry_written && p2m_is_hostp2m(..) )".

Quoted 4 lines

Then I think you could make the ret / rc use mirror what you do elsewhere, where ret is used to store the return value of the function call, and it's propagated to rc only if rc is not already set.

Quoted 8 lines

George,

Thanks for your detailed explanation. This seems an another matter of preference.
Of course, I'd follow your suggestion in p2m field, while I need to hear the other maintainers' options as well (especially when it comes from Kevin/Jan, as they do spend a lot of time for me).

A little bit of difference here, IMO, an int 'entry_written' is much better, as we also need to propagate the 'entry_written' up to callers, i.e. the errors returned from atomic_write_ept_entry() are '-EINVAL', '-ESRCH' and '-EBUSY'. Then, the follow is my modification (feel free to correct me):

But it doesn't look like you're actually taking advantage of the fact that entry_written is non-boolean -- other than immediately copying the value into rc, all the places you're only checking if it's zero or not (where "zero" in this case actually means "the entry was written").

The code you had before was, as far as I can tell, functionally correct.
The point of introducing the extra variable is to decrease cognitive
load on people coming along to modify the code later. To do that, you want to maximize the things you do that are within expectation, and minimize the things you do outside of that.

The name "entry_written" sounds like a boolean; developers will expect "0 / false" to mean "not written" and "1/true" to mean "entry written".
Furthermore, rc looks like a return value; if coders see "rc =
atomic_write_ept_entry()" they immediately have a framework for what's going on; whereas if they see "entry_written == [...]; if (entry_written) { ... rc= entry_written; }", it takes some thinking to figure out. Not much, but every little bit of unnecessary thinking adds up.

My suggestion remains to:
1. Declare entry_written as a bool, initialized to false
2. In both atomic_write_ept_entry() calls, use rc to collect the return value
3. In the second one (the one which may fail), add an else { entry_written = true; }
4. In the conditional deciding whether to update the iommu, use "entry_written", I think. At this point rc == 0 and entry_written = true will be identical, but if we ever insert something in between, we want the iommu update to be attempted any time the entry is successfully written.
5. Use "if ( entry_written && ...)" when deciding whether to propagate the change to the altp2m.

Thanks,
-George

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#20 lore raw

Re: [PATCH v3 03/10] IOMMU/MMU: enhance the call trees of IOMMU unmapping and mapping

Quoted 174 lines · George Dunlap
Quoted 1 line · Quan

On May 04, 2016 9:49 PM, George Dunlap <[email protected]>

Quoted 49 lines · wrote:
Quoted 49 lines · Quan Xu
Quoted 2 lines

When IOMMU mapping is failed, we issue a best effort rollback, stopping IOMMU mapping, unmapping the previous IOMMU maps and

then

Quoted 4 lines

reporting the error up to the call trees. When rollback is not feasible (in early initialization phase or trade-off of complexity) for the hardware domain, we do things on a best effort basis, only throwing

out an error message.

xen/arch/x86/mm.c static int __get_page_type(struct page_info

Quoted 25 lines

IOMMU unmapping should perhaps continue despite an error, in an attempt to do best effort cleanup.

CC Keir Fraser <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
CC Jun Nakajima <[email protected]>
CC Kevin Tian <[email protected]>
CC George Dunlap <[email protected]>
---
 xen/arch/x86/mm.c               | 13 ++++++++-----
 xen/arch/x86/mm/p2m-ept.c       | 27 +++++++++++++++++++++++++--
 xen/arch/x86/mm/p2m-pt.c        | 24 ++++++++++++++++++++----
 xen/arch/x86/mm/p2m.c           | 11 +++++++++--
 xen/drivers/passthrough/iommu.c | 14 +++++++++++++-
 5 files changed, 75 insertions(+), 14 deletions(-)
xen/arch/x86/mm.c index renamed from xen/arch/x86/mm.c +0 -0

a42097f..427097d 100644

xen/arch/x86/mm.c +0 -0
@@ -2467,7 +2467,7 @@ static int __get_page_type(struct page_info

*page, unsigned long type,

xen/arch/x86/mm.c static int __get_page_type(struct page_info

Quoted 8 lines
xen/arch/x86/mm.c +1 -1
                            int preemptible)  {
     unsigned long nx, x, y = page->u.inuse.type_info;
-    int rc = 0;
+    int rc = 0, ret = 0;
 
     ASSERT(!(type & ~(PGT_type_mask | PGT_pae_xen_l2)));
 
@@ -2578,11 +2578,11 @@ static int __get_page_type(struct page_info

*page, unsigned long type,

Quoted 5 lines
xen/arch/x86/mm.c +1 -1
         if ( d && is_pv_domain(d) && unlikely(need_iommu(d)) )
         {
             if ( (x & PGT_type_mask) == PGT_writable_page )
-                iommu_unmap_page(d, mfn_to_gmfn(d, page_to_mfn(page)));
+                ret = iommu_unmap_page(d, mfn_to_gmfn(d,
Quoted 6 lines
Quoted 6 lines

page_to_mfn(page)));

Quoted 5 lines
xen/arch/x86/mm.c +1 -3
             else if ( type == PGT_writable_page )
-                iommu_map_page(d, mfn_to_gmfn(d, page_to_mfn(page)),
-                               page_to_mfn(page),
-                               IOMMUF_readable|IOMMUF_writable);
+                ret = iommu_map_page(d, mfn_to_gmfn(d,

page_to_mfn(page)),

Quoted 78 lines
Quoted 78 lines

xen/arch/x86/mm.c static int __get_page_type(struct page_info

Quoted 7 lines
xen/arch/x86/mm.c +3 -0
+                                     page_to_mfn(page),
+
+ IOMMUF_readable|IOMMUF_writable);
         }
     }
@@ -2599,6 +2599,9 @@ static int __get_page_type(struct page_info

*page, unsigned long type,

Quoted 43 lines
xen/arch/x86/mm.c +3 -0
     if ( (x & PGT_partial) && !(nx & PGT_partial) )
         put_page(page);
 
+    if ( !rc )
+        rc = ret;
+
     return rc;
 }
 
xen/arch/x86/mm/p2m-ept.c +15 -2
index 1ed5b47..df87944 100644
@@ -821,6 +821,8 @@ out:
     if ( needs_sync )
         ept_sync_domain(p2m);
 
+    ret = 0;
+
     /* For host p2m, may need to change VT-d page table.*/
     if ( rc == 0 && p2m_is_hostp2m(p2m) && need_iommu(d) &&
          need_modify_vtd_table )
@@ -831,11 +833,29 @@ out:
         {
             if ( iommu_flags )
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_map_page(d, gfn + i, mfn_x(mfn) + i, iommu_flags);
+                {
+                    rc = iommu_map_page(d, gfn + i, mfn_x(mfn) + i,
+ iommu_flags);
+
+                    if ( !ret && unlikely(rc) )
+                    {
+                        while ( i-- )
+                            iommu_unmap_page(d, gfn + i);
+
+                        ret = rc;
+                        break;
+                    }
+                }
             else
                 for ( i = 0; i < (1 << order); i++ )
-                    iommu_unmap_page(d, gfn + i);

xen/arch/x86/mm/p2m-ept.c out: · line 860

Quoted 10 lines
xen/arch/x86/mm/p2m-ept.c +8 -0
+                {
+                    rc = iommu_unmap_page(d, gfn + i);
+
+                    if ( !ret && unlikely(rc) )
+                        ret = rc;
+                }
         }
+
+        rc = 0;
     }

So the reason for doing this thing with setting ret=0, then using rc, then setting rc to zero, is to make sure that the change is propagated to the altp2m if necessary?

I'm not a fan of this sort of "implied signalling". The check at the altp2m conditional is meant to say, "everything went fine, go ahead and propagate the change". But with your changes, that's not what we want anymore -- we want, "If the change actually made it into the hostp2m, propagate it to the altp2m."

As such, I think it would be a lot clearer to just make a new boolean variable, maybe "entry_written", which we initialize to false and then set to true when the entry is actually written; and then change the condition re the altp2m to "if ( entry_written && p2m_is_hostp2m(..) )".

Quoted 8 lines
Quoted 4 lines

Then I think you could make the ret / rc use mirror what you do elsewhere, where ret is used to store the return value of the function call, and it's propagated to rc only if rc is not already set.

George,

Thanks for your detailed explanation. This seems an another matter of

preference.

Quoted 2 lines

Of course, I'd follow your suggestion in p2m field, while I need to hear the other maintainers' options as well (especially when it comes from

Kevin/Jan, as they do spend a lot of time for me).

Quoted 3 lines

A little bit of difference here, IMO, an int 'entry_written' is much better, as we also need to propagate the 'entry_written' up to callers, i.e. the

errors returned from atomic_write_ept_entry() are '-EINVAL', '-ESRCH' and '- EBUSY'. Then, the follow is my modification (feel free to correct me):

But it doesn't look like you're actually taking advantage of the fact that entry_written is non-boolean -- other than immediately copying the value into rc, all the places you're only checking if it's zero or not (where "zero" in this case actually means "the entry was written").

The code you had before was, as far as I can tell, functionally correct.
The point of introducing the extra variable is to decrease cognitive load on
people coming along to modify the code later. To do that, you want to maximize the things you do that are within expectation, and minimize the things you do outside of that.

The name "entry_written" sounds like a boolean; developers will expect "0 / false" to mean "not written" and "1/true" to mean "entry written".
Furthermore, rc looks like a return value; if coders see "rc =
atomic_write_ept_entry()" they immediately have a framework for what's going on; whereas if they see "entry_written == [...]; if (entry_written) { ... rc= entry_written; }", it takes some thinking to figure out. Not much, but every little bit of unnecessary thinking adds up.

Quoted 11 lines

My suggestion remains to:
1. Declare entry_written as a bool, initialized to false 2. In both atomic_write_ept_entry() calls, use rc to collect the return value 3. In the second one (the one which may fail), add an else { entry_written = true; } 4. In the conditional deciding whether to update the iommu, use "entry_written", I think. At this point rc == 0 and entry_written = true will be identical, but if we ever insert something in between, we want the iommu update to be attempted any time the entry is successfully written.
5. Use "if ( entry_written && ...)" when deciding whether to propagate the change to the altp2m.

George,
Thanks. It is very clear now, and I will follow it in next v4.

Quan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#21 lore raw

PATCH v3 4/10 vt-d: propagate IOMMU Device-TLB flush error up to IOMMU unmapping.

In reply to Quan Xu

Propagate the IOMMU Device-TLB flush error up to IOMMU unmapping.

CC Kevin Tian <[email protected]>
CC Feng Wu <[email protected]>
CC Jan Beulich <[email protected]>
---
 xen/drivers/passthrough/vtd/iommu.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)
xen/drivers/passthrough/vtd/iommu.c +8 -7
index 6e2e43a..e908046 100644
@@ -618,11 +618,12 @@ static void intel_iommu_iotlb_flush_all(struct domain *d)
 }
 
 /* clear one page's page table */
-static void dma_pte_clear_one(struct domain *domain, u64 addr)
+static int __must_check dma_pte_clear_one(struct domain *domain, u64 addr)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(domain);
     struct dma_pte *page = NULL, *pte = NULL;
     u64 pg_maddr;
+    int rc = 0;
 
     spin_lock(&hd->arch.mapping_lock);
     /* get last level pte */
@@ -630,7 +631,7 @@ static void dma_pte_clear_one(struct domain *domain, u64 addr)
     if ( pg_maddr == 0 )
     {
         spin_unlock(&hd->arch.mapping_lock);
-        return;
+        return 0;
     }
 
     page = (struct dma_pte *)map_vtd_domain_page(pg_maddr);
@@ -640,7 +641,7 @@ static void dma_pte_clear_one(struct domain *domain, u64 addr)
     {
         spin_unlock(&hd->arch.mapping_lock);
         unmap_vtd_domain_page(page);
-        return;
+        return 0;
     }
 
     dma_clear_pte(*pte);
@@ -648,9 +649,11 @@ static void dma_pte_clear_one(struct domain *domain, u64 addr)
     iommu_flush_cache_entry(pte, sizeof(struct dma_pte));
 
     if ( !this_cpu(iommu_dont_flush_iotlb) )
-        iommu_flush_iotlb(domain, addr >> PAGE_SHIFT_4K, 1, 1);
+        rc = iommu_flush_iotlb(domain, addr >> PAGE_SHIFT_4K, 1, 1);
 
     unmap_vtd_domain_page(page);
+
+    return rc;
 }
 
 static void iommu_free_pagetable(u64 pt_maddr, int level)
@@ -1771,9 +1774,7 @@ static int intel_iommu_unmap_page(struct domain *d, unsigned long gfn)
     if ( iommu_passthrough && is_hardware_domain(d) )
         return 0;
 
-    dma_pte_clear_one(d, (paddr_t)gfn << PAGE_SHIFT_4K);
-
-    return 0;
+    return dma_pte_clear_one(d, (paddr_t)gfn << PAGE_SHIFT_4K);
 }
 
 int iommu_pte_flush(struct domain *d, u64 gfn, u64 *pte,
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
author
#22 lore raw

PATCH v3 5/10 vt-d: propagate IOMMU Device-TLB flush error up to IOMMU mapping.

In reply to Quan Xu

Propagate the IOMMU Device-TLB flush error up to IOMMU mapping.

CC Kevin Tian <[email protected]>
CC Feng Wu <[email protected]>
CC Jan Beulich <[email protected]>
---
 xen/drivers/passthrough/vtd/iommu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
xen/drivers/passthrough/vtd/iommu.c +3 -2
index e908046..802de02 100644
@@ -1721,6 +1721,7 @@ static int intel_iommu_map_page(
     struct hvm_iommu *hd = domain_hvm_iommu(d);
     struct dma_pte *page = NULL, *pte = NULL, old, new = { 0 };
     u64 pg_maddr;
+    int rc = 0;
 
     /* Do nothing if VT-d shares EPT page table */
     if ( iommu_use_hap_pt(d) )
@@ -1763,9 +1764,9 @@ static int intel_iommu_map_page(
     unmap_vtd_domain_page(page);
 
     if ( !this_cpu(iommu_dont_flush_iotlb) )
-        iommu_flush_iotlb(d, gfn, dma_pte_present(old), 1);
+        rc = iommu_flush_iotlb(d, gfn, dma_pte_present(old), 1);
 
-    return 0;
+    return rc;
 }
 
 static int intel_iommu_unmap_page(struct domain *d, unsigned long gfn)
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
author
#23 lore raw

PATCH v3 6/10 propagate IOMMU Device-TLB flush error up to iommu_iotlb_flush{, _all} (top level ones).

In reply to Quan Xu

Propagate the IOMMU Device-TLB flush error up to the iommu_iotlb_flush{,_all}.

This patch fixes the top level ones (this patch doesn't fix the leaf ones but the next patch does).

CC Stefano Stabellini <[email protected]>
CC Julien Grall <[email protected]>
CC Jan Beulich <[email protected]>
CC Kevin Tian <[email protected]>
---
 xen/arch/arm/p2m.c                  |  5 ++++-
 xen/common/memory.c                 | 14 ++++++++++++--
 xen/drivers/passthrough/iommu.c     | 13 +++++++++----
 xen/drivers/passthrough/x86/iommu.c |  5 +++--
 xen/include/xen/iommu.h             |  5 +++--
 5 files changed, 31 insertions(+), 11 deletions(-)
xen/arch/arm/p2m.c +4 -1
index db21433..63df5b1 100644
@@ -1178,7 +1178,10 @@ out:
     if ( flush )
     {
         flush_tlb_domain(d);
-        iommu_iotlb_flush(d, sgfn, egfn - sgfn);
+        ret = iommu_iotlb_flush(d, sgfn, egfn - sgfn);
+
+        if ( !rc && unlikely(ret) )
+            rc = ret;
     }
 
     while ( (pg = page_list_remove_head(&free_pages)) )
xen/common/memory.c +12 -2
index 644f81a..21b8098 100644
@@ -677,9 +677,19 @@ static int xenmem_add_to_physmap(struct domain *d,
 #ifdef CONFIG_HAS_PASSTHROUGH
     if ( need_iommu(d) )
     {
+        int ret;
+
         this_cpu(iommu_dont_flush_iotlb) = 0;
-        iommu_iotlb_flush(d, xatp->idx - done, done);
-        iommu_iotlb_flush(d, xatp->gpfn - done, done);
+
+        ret = iommu_iotlb_flush(d, xatp->idx - done, done);
+
+        if ( rc >= 0 && unlikely(ret) )
+            rc = ret;
+
+        ret = iommu_iotlb_flush(d, xatp->gpfn - done, done);
+
+        if ( rc >= 0 && unlikely(ret) )
+            rc = ret;
     }
 #endif
 
xen/drivers/passthrough/iommu.c +9 -4
index d74433d..b380638 100644
@@ -315,24 +315,29 @@ static void iommu_free_pagetables(unsigned long unused)
                             cpumask_cycle(smp_processor_id(), &cpu_online_map));
 }
 
-void iommu_iotlb_flush(struct domain *d, unsigned long gfn, unsigned int page_count)
+int __must_check iommu_iotlb_flush(struct domain *d, unsigned long gfn,
+                                   unsigned int page_count)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
 
     if ( !iommu_enabled || !hd->platform_ops || !hd->platform_ops->iotlb_flush )
-        return;
+        return 0;
 
     hd->platform_ops->iotlb_flush(d, gfn, page_count);
+
+    return 0;
 }
 
-void iommu_iotlb_flush_all(struct domain *d)
+int __must_check iommu_iotlb_flush_all(struct domain *d)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
 
     if ( !iommu_enabled || !hd->platform_ops || !hd->platform_ops->iotlb_flush_all )
-        return;
+        return 0;
 
     hd->platform_ops->iotlb_flush_all(d);
+
+    return 0;
 }
 
 int __init iommu_setup(void)
xen/drivers/passthrough/x86/iommu.c +3 -2
index 8cbb655..c2c1ee3 100644
@@ -104,8 +104,9 @@ int arch_iommu_populate_page_table(struct domain *d)
     this_cpu(iommu_dont_flush_iotlb) = 0;
 
     if ( !rc )
-        iommu_iotlb_flush_all(d);
-    else if ( rc != -ERESTART )
+        rc = iommu_iotlb_flush_all(d);
+
+    if ( rc && rc != -ERESTART )
         iommu_teardown(d);
 
     return rc;
xen/include/xen/iommu.h +3 -2
index 8217cb7..0f4c18d 100644
@@ -182,8 +182,9 @@ int iommu_do_pci_domctl(struct xen_domctl *, struct domain *d,
 int iommu_do_domctl(struct xen_domctl *, struct domain *d,
                     XEN_GUEST_HANDLE_PARAM(xen_domctl_t));
 
-void iommu_iotlb_flush(struct domain *d, unsigned long gfn, unsigned int page_count);
-void iommu_iotlb_flush_all(struct domain *d);
+int __must_check iommu_iotlb_flush(struct domain *d, unsigned long gfn,
+                                   unsigned int page_count);
+int __must_check iommu_iotlb_flush_all(struct domain *d);
 
 /*
  * The purpose of the iommu_dont_flush_iotlb optional cpu flag is to
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
author
#24 lore raw

PATCH v3 7/10 IOMMU: propagate IOMMU Device-TLB flush error up to iommu_iotlb_flush{, _all} (leaf ones).

In reply to Quan Xu

Propagate the IOMMU Device-TLB flush error up to the iommu_iotlb_flush{,_all}.

This patch fixes the leaf ones.

CC Stefano Stabellini <[email protected]>
CC Julien Grall <[email protected]>
CC Jan Beulich <[email protected]>
CC Kevin Tian <[email protected]>
CC Feng Wu <[email protected]>
---
 xen/drivers/passthrough/arm/smmu.c  | 12 +++++++-----
 xen/drivers/passthrough/iommu.c     |  8 ++------
 xen/drivers/passthrough/vtd/iommu.c |  9 +++++----
 xen/include/xen/iommu.h             |  4 ++--
 4 files changed, 16 insertions(+), 17 deletions(-)
xen/drivers/passthrough/arm/smmu.c +7 -5
index 62da087..c83030b 100644
@@ -2540,7 +2540,7 @@ static int force_stage = 2;
  */
 static u32 platform_features = ARM_SMMU_FEAT_COHERENT_WALK;
 
-static void arm_smmu_iotlb_flush_all(struct domain *d)
+static int arm_smmu_iotlb_flush_all(struct domain *d)
 {
 	struct arm_smmu_xen_domain *smmu_domain = domain_hvm_iommu(d)->arch.priv;
 	struct iommu_domain *cfg;
@@ -2557,13 +2557,15 @@ static void arm_smmu_iotlb_flush_all(struct domain *d)
 		arm_smmu_tlb_inv_context(cfg->priv);
 	}
 	spin_unlock(&smmu_domain->lock);
+
+	return 0;
 }
 
-static void arm_smmu_iotlb_flush(struct domain *d, unsigned long gfn,
-                                 unsigned int page_count)
+static int arm_smmu_iotlb_flush(struct domain *d, unsigned long gfn,
+                                unsigned int page_count)
 {
-    /* ARM SMMU v1 doesn't have flush by VMA and VMID */
-    arm_smmu_iotlb_flush_all(d);
+	/* ARM SMMU v1 doesn't have flush by VMA and VMID */
+	return arm_smmu_iotlb_flush_all(d);
 }
 
 static struct iommu_domain *arm_smmu_get_domain(struct domain *d,
xen/drivers/passthrough/iommu.c +2 -6
index b380638..839852f 100644
@@ -323,9 +323,7 @@ int __must_check iommu_iotlb_flush(struct domain *d, unsigned long gfn,
     if ( !iommu_enabled || !hd->platform_ops || !hd->platform_ops->iotlb_flush )
         return 0;
 
-    hd->platform_ops->iotlb_flush(d, gfn, page_count);
-
-    return 0;
+    return hd->platform_ops->iotlb_flush(d, gfn, page_count);
 }
 
 int __must_check iommu_iotlb_flush_all(struct domain *d)
@@ -335,9 +333,7 @@ int __must_check iommu_iotlb_flush_all(struct domain *d)
     if ( !iommu_enabled || !hd->platform_ops || !hd->platform_ops->iotlb_flush_all )
         return 0;
 
-    hd->platform_ops->iotlb_flush_all(d);
-
-    return 0;
+    return hd->platform_ops->iotlb_flush_all(d);
 }
 
 int __init iommu_setup(void)
xen/drivers/passthrough/vtd/iommu.c +5 -4
index 802de02..e2cb1e1 100644
@@ -607,14 +607,15 @@ static int iommu_flush_iotlb(struct domain *d, unsigned long gfn,
     return ret;
 }
 
-static void intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn, unsigned int page_count)
+static int intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn,
+                                   unsigned int page_count)
 {
-    iommu_flush_iotlb(d, gfn, 1, page_count);
+    return iommu_flush_iotlb(d, gfn, 1, page_count);
 }
 
-static void intel_iommu_iotlb_flush_all(struct domain *d)
+static int intel_iommu_iotlb_flush_all(struct domain *d)
 {
-    iommu_flush_iotlb(d, INVALID_GFN, 0, 0);
+    return iommu_flush_iotlb(d, INVALID_GFN, 0, 0);
 }
 
 /* clear one page's page table */
xen/include/xen/iommu.h +2 -2
index 0f4c18d..fe52428 100644
@@ -161,8 +161,8 @@ struct iommu_ops {
     void (*resume)(void);
     void (*share_p2m)(struct domain *d);
     void (*crash_shutdown)(void);
-    void (*iotlb_flush)(struct domain *d, unsigned long gfn, unsigned int page_count);
-    void (*iotlb_flush_all)(struct domain *d);
+    int (*iotlb_flush)(struct domain *d, unsigned long gfn, unsigned int page_count);
+    int (*iotlb_flush_all)(struct domain *d);
     int (*get_reserved_device_memory)(iommu_grdm_t *, void *);
     void (*dump_p2m_table)(struct domain *d);
 };
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Acked-by on v3 7/10
#25 lore raw

Re: [PATCH v3 07/10] IOMMU: propagate IOMMU Device-TLB flush error up to iommu_iotlb_flush{, _all} (leaf ones).

Quoted 7 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

Propagate the IOMMU Device-TLB flush error up to the iommu_iotlb_flush{,_all}.

This patch fixes the leaf ones.

Quoted 7 lines
CC Stefano Stabellini <[email protected]>
CC Julien Grall <[email protected]>
CC Jan Beulich <[email protected]>
CC Kevin Tian <[email protected]>
CC Feng Wu <[email protected]>

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#26 lore raw

PATCH v3 8/10 vt-d/ept: propagate IOMMU Device-TLB flush error up to EPT update.

In reply to Quan Xu

Propagate the IOMMU Device-TLB flush error up to the ept_set_entry(), when VT-d shares EPT page table.

CC Jun Nakajima <[email protected]>
CC Kevin Tian <[email protected]>
CC George Dunlap <[email protected]>
CC Jan Beulich <[email protected]>
CC Andrew Cooper <[email protected]>
CC Feng Wu <[email protected]>
---
 xen/arch/x86/mm/p2m-ept.c           | 3 ++-
 xen/drivers/passthrough/vtd/iommu.c | 4 ++--
 xen/include/asm-x86/iommu.h         | 3 ++-
 3 files changed, 6 insertions(+), 4 deletions(-)
xen/arch/x86/mm/p2m-ept.c +2 -1
index df87944..d31650f 100644
@@ -828,7 +828,8 @@ out:
          need_modify_vtd_table )
     {
         if ( iommu_hap_pt_share )
-            iommu_pte_flush(d, gfn, &ept_entry->epte, order, vtd_pte_present);
+            ret = iommu_pte_flush(d, gfn, &ept_entry->epte,
+                                  order, vtd_pte_present);
         else
         {
             if ( iommu_flags )
xen/drivers/passthrough/vtd/iommu.c +2 -2
index e2cb1e1..bf8c9c1 100644
@@ -1779,8 +1779,8 @@ static int intel_iommu_unmap_page(struct domain *d, unsigned long gfn)
     return dma_pte_clear_one(d, (paddr_t)gfn << PAGE_SHIFT_4K);
 }
 
-int iommu_pte_flush(struct domain *d, u64 gfn, u64 *pte,
-                    int order, bool_t present)
+int __must_check iommu_pte_flush(struct domain *d, u64 gfn, u64 *pte,
+                                 int order, bool_t present)
 {
     struct acpi_drhd_unit *drhd;
     struct iommu *iommu = NULL;
xen/include/asm-x86/iommu.h +2 -1
index 1ae0c5a..fbfb1a4 100644
@@ -26,7 +26,8 @@ int iommu_setup_hpet_msi(struct msi_desc *);
 
 /* While VT-d specific, this must get declared in a generic header. */
 int adjust_vtd_irq_affinities(void);
-int iommu_pte_flush(struct domain *d, u64 gfn, u64 *pte, int order, bool_t present);
+int __must_check iommu_pte_flush(struct domain *d, u64 gfn, u64 *pte,
+                                 int order, bool_t present);
 bool_t iommu_supports_eim(void);
 int iommu_enable_x2apic_IR(void);
 void iommu_disable_x2apic_IR(void);
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Acked-by on v3 8/10
#27 lore raw

Re: [PATCH v3 08/10] vt-d/ept: propagate IOMMU Device-TLB flush error up to EPT update.

Quoted 8 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

Propagate the IOMMU Device-TLB flush error up to the ept_set_entry(), when VT-d shares EPT page table.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#28 lore raw

PATCH v3 9/10 IOMMU: propagate IOMMU Device-TLB flush error up to IOMMU suspending

In reply to Quan Xu

Propagate the IOMMU Device-TLB flush error up to IOMMU suspending.

CC Jan Beulich <[email protected]>
CC Liu Jinsong <[email protected]>
CC Keir Fraser <[email protected]>
CC Andrew Cooper <[email protected]>
CC Suravee Suthikulpanit <[email protected]>
CC Stefano Stabellini <[email protected]>
CC Julien Grall <[email protected]>
CC Kevin Tian <[email protected]>
CC Feng Wu <[email protected]>
---
 xen/arch/x86/acpi/power.c                     | 15 ++++++++++-
 xen/drivers/passthrough/amd/iommu_init.c      |  9 ++++++-
 xen/drivers/passthrough/amd/pci_amd_iommu.c   |  2 +-
 xen/drivers/passthrough/iommu.c               |  7 +++--
 xen/drivers/passthrough/vtd/iommu.c           | 39 ++++++++++++++++++++-------
 xen/include/asm-x86/hvm/svm/amd-iommu-proto.h |  2 +-
 xen/include/xen/iommu.h                       |  4 +--
 7 files changed, 61 insertions(+), 17 deletions(-)
xen/arch/x86/acpi/power.c +14 -1
index 2885e31..9097333 100644
@@ -45,6 +45,8 @@ void do_suspend_lowlevel(void);
 
 static int device_power_down(void)
 {
+    int err;
+
     console_suspend();
 
     time_suspend();
@@ -53,11 +55,22 @@ static int device_power_down(void)
 
     ioapic_suspend();
 
-    iommu_suspend();
+    err = iommu_suspend();
+
+    if ( err )
+        goto iommu_suspend_error;
 
     lapic_suspend();
 
     return 0;
+
+ iommu_suspend_error:
+    ioapic_resume();
+    i8259A_resume();
+    time_resume();
+    console_resume();
+
+    return err;
 }
 
 static void device_power_up(void)
xen/drivers/passthrough/amd/iommu_init.c +8 -1
index 4536106..02588aa 100644
@@ -1339,12 +1339,19 @@ static void invalidate_all_devices(void)
     iterate_ivrs_mappings(_invalidate_all_devices);
 }
 
-void amd_iommu_suspend(void)
+int amd_iommu_suspend(void)
 {
     struct amd_iommu *iommu;
 
     for_each_amd_iommu ( iommu )
         disable_iommu(iommu);
+
+    return 0;
+}
+
+void amd_iommu_crash_shutdown(void)
+{
+    amd_iommu_suspend();
 }
 
 void amd_iommu_resume(void)
xen/drivers/passthrough/amd/pci_amd_iommu.c +1 -1
index c8ee8dc..fb8e816 100644
@@ -628,6 +628,6 @@ const struct iommu_ops amd_iommu_ops = {
     .suspend = amd_iommu_suspend,
     .resume = amd_iommu_resume,
     .share_p2m = amd_iommu_share_p2m,
-    .crash_shutdown = amd_iommu_suspend,
+    .crash_shutdown = amd_iommu_crash_shutdown,
     .dump_p2m_table = amd_dump_p2m_table,
 };
xen/drivers/passthrough/iommu.c +5 -2
index 839852f..c565c66 100644
@@ -407,11 +407,14 @@ int iommu_do_domctl(
     return ret;
 }
 
-void iommu_suspend()
+int __must_check iommu_suspend()
 {
     const struct iommu_ops *ops = iommu_get_ops();
+
     if ( iommu_enabled )
-        ops->suspend();
+        return ops->suspend();
+
+    return 0;
 }
 
 void iommu_share_p2m_table(struct domain* d)
xen/drivers/passthrough/vtd/iommu.c +30 -9
index bf8c9c1..cf847ec 100644
@@ -542,11 +542,12 @@ static int iommu_flush_iotlb_psi(
     return status;
 }
 
-static void iommu_flush_all(void)
+static int __must_check iommu_flush_all(void)
 {
     struct acpi_drhd_unit *drhd;
     struct iommu *iommu;
     int flush_dev_iotlb;
+    int rc = 0, ret;
 
     flush_all_cache();
     for_each_drhd_unit ( drhd )
@@ -554,8 +555,13 @@ static void iommu_flush_all(void)
         iommu = drhd->iommu;
         iommu_flush_context_global(iommu, 0);
         flush_dev_iotlb = find_ats_dev_drhd(iommu) ? 1 : 0;
-        iommu_flush_iotlb_global(iommu, 0, flush_dev_iotlb);
+        ret = iommu_flush_iotlb_global(iommu, 0, flush_dev_iotlb);
+
+        if ( ret )
+            rc = ret;
     }
+
+    return rc;
 }
 
 static int iommu_flush_iotlb(struct domain *d, unsigned long gfn,
@@ -1272,7 +1278,9 @@ static void __hwdom_init intel_iommu_hwdom_init(struct domain *d)
     setup_hwdom_pci_devices(d, setup_hwdom_device);
     setup_hwdom_rmrr(d);
 
-    iommu_flush_all();
+    if ( iommu_flush_all() )
+        printk(XENLOG_WARNING VTDPREFIX
+               " intel_iommu_hwdom_init: IOMMU flush all failed.\n");
 
     for_each_drhd_unit ( drhd )
     {
@@ -2134,8 +2142,8 @@ static int init_vtd_hw(void)
             return -EIO;
         }
     }
-    iommu_flush_all();
-    return 0;
+
+    return iommu_flush_all();
 }
 
 static void __hwdom_init setup_hwdom_rmrr(struct domain *d)
@@ -2424,16 +2432,25 @@ static int intel_iommu_group_id(u16 seg, u8 bus, u8 devfn)
 }
 
 static u32 iommu_state[MAX_IOMMUS][MAX_IOMMU_REGS];
-static void vtd_suspend(void)
+
+static int vtd_suspend(void)
 {
     struct acpi_drhd_unit *drhd;
     struct iommu *iommu;
     u32    i;
+    int rc;
 
     if ( !iommu_enabled )
-        return;
+        return 0;
 
-    iommu_flush_all();
+    rc = iommu_flush_all();
+
+    if ( rc )
+    {
+        printk(XENLOG_WARNING VTDPREFIX
+               " vtd_suspend: IOMMU flush all failed.\n");
+        return rc;
+    }
 
     for_each_drhd_unit ( drhd )
     {
@@ -2462,6 +2479,8 @@ static void vtd_suspend(void)
         if ( !iommu_intremap && iommu_qinval )
             disable_qinval(iommu);
     }
+
+    return 0;
 }
 
 static void vtd_crash_shutdown(void)
@@ -2472,7 +2491,9 @@ static void vtd_crash_shutdown(void)
     if ( !iommu_enabled )
         return;
 
-    iommu_flush_all();
+    if ( iommu_flush_all() )
+        printk(XENLOG_WARNING VTDPREFIX
+               " vtd_crash_shutdown: IOMMU flush all failed.\n");
 
     for_each_drhd_unit ( drhd )
     {
xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +1 -1
index 9c51172..f540fc8 100644
@@ -119,7 +119,7 @@ extern unsigned long *shared_intremap_inuse;
 
 /* power management support */
 void amd_iommu_resume(void);
-void amd_iommu_suspend(void);
+int amd_iommu_suspend(void);
 void amd_iommu_crash_shutdown(void);
 
 /* guest iommu support */
xen/include/xen/iommu.h +2 -2
index fe52428..dfa52ef 100644
@@ -157,7 +157,7 @@ struct iommu_ops {
     unsigned int (*read_apic_from_ire)(unsigned int apic, unsigned int reg);
     int (*setup_hpet_msi)(struct msi_desc *);
 #endif /* CONFIG_X86 */
-    void (*suspend)(void);
+    int (*suspend)(void);
     void (*resume)(void);
     void (*share_p2m)(struct domain *d);
     void (*crash_shutdown)(void);
@@ -167,7 +167,7 @@ struct iommu_ops {
     void (*dump_p2m_table)(struct domain *d);
 };
 
-void iommu_suspend(void);
+int __must_check iommu_suspend(void);
 void iommu_resume(void);
 void iommu_crash_shutdown(void);
 int iommu_get_reserved_device_memory(iommu_grdm_t *, void *);
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#29 lore raw

Re: [PATCH v3 09/10] IOMMU: propagate IOMMU Device-TLB flush error up to IOMMU suspending

Quoted 27 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

xen/arch/x86/acpi/power.c +6 -1
index 2885e31..9097333 100644
@@ -45,6 +45,8 @@ void do_suspend_lowlevel(void);
 
 static int device_power_down(void)
 {
+    int err;
+
     console_suspend();
 
     time_suspend();
@@ -53,11 +55,22 @@ static int device_power_down(void)
 
     ioapic_suspend();
 
-    iommu_suspend();
+    err = iommu_suspend();
+
+    if ( err )
+        goto iommu_suspend_error;
 
     lapic_suspend();
 

xen/arch/x86/acpi/power.c static int device_power_down(void) · line 74

Quoted 10 lines
xen/arch/x86/acpi/power.c +8 -0
     return 0;
+
+ iommu_suspend_error:
+    ioapic_resume();
+    i8259A_resume();
+    time_resume();
+    console_resume();
+
+    return err;
 }

Jan had comment to better reuse device_power_up... looks no change in this version.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#30 lore raw

Re: [PATCH v3 09/10] IOMMU: propagate IOMMU Device-TLB flush error up to IOMMU suspending

Quoted 27 lines · Kevin
Quoted 27 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

xen/arch/x86/acpi/power.c +6 -1
index 2885e31..9097333 100644
@@ -45,6 +45,8 @@ void do_suspend_lowlevel(void);
 
 static int device_power_down(void)
 {
+    int err;
+
     console_suspend();
 
     time_suspend();
@@ -53,11 +55,22 @@ static int device_power_down(void)
 
     ioapic_suspend();
 
-    iommu_suspend();
+    err = iommu_suspend();
+
+    if ( err )
+        goto iommu_suspend_error;
 
     lapic_suspend();
 
Quoted 13 lines

xen/arch/x86/acpi/power.c static int device_power_down(void) · line 74

Quoted 10 lines
xen/arch/x86/acpi/power.c +8 -0
     return 0;
+
+ iommu_suspend_error:
+    ioapic_resume();
+    i8259A_resume();
+    time_resume();
+    console_resume();
+
+    return err;
 }

Jan had comment to better reuse device_power_up... looks no change in this version.

Yes, __iiuc__, this may be an optimization, but not a must. We can discuss this in detail In this version.

Quan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#31 lore raw

Re: [PATCH v3 09/10] IOMMU: propagate IOMMU Device-TLB flush error up to IOMMU suspending

Quoted 43 lines
Quoted 1 line
Quoted 1 line · On 04.05.16 at 04:14, <[email protected]> wrote:
Quoted 27 lines · Kevin
Quoted 27 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

xen/arch/x86/acpi/power.c +6 -1
index 2885e31..9097333 100644
@@ -45,6 +45,8 @@ void do_suspend_lowlevel(void);
 
 static int device_power_down(void)
 {
+    int err;
+
     console_suspend();
 
     time_suspend();
@@ -53,11 +55,22 @@ static int device_power_down(void)
 
     ioapic_suspend();
 
-    iommu_suspend();
+    err = iommu_suspend();
+
+    if ( err )
+        goto iommu_suspend_error;
 
     lapic_suspend();
 
Quoted 13 lines

xen/arch/x86/acpi/power.c static int device_power_down(void) · line 74

Quoted 10 lines
xen/arch/x86/acpi/power.c +8 -0
     return 0;
+
+ iommu_suspend_error:
+    ioapic_resume();
+    i8259A_resume();
+    time_resume();
+    console_resume();
+
+    return err;
 }

Jan had comment to better reuse device_power_up... looks no change in this version.

Quoted 2 lines

Yes, __iiuc__, this may be an optimization, but not a must. We can discuss this in detail In this version.

As an optimization it would indeed be quite pointless here. My request was more for maintainability: By re-using the function future changes don't need to go to two places, and hence there's no risk of one of them getting forgotten.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#32 lore raw

Re: [PATCH v3 09/10] IOMMU: propagate IOMMU Device-TLB flush error up to IOMMU suspending

Quoted 42 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 04.05.16 at 04:14, <[email protected]> wrote:
Quoted 39 lines · Kevin
Quoted 26 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM diff --git a/xen/arch/x86/acpi/power.c b/xen/arch/x86/acpi/power.c index 2885e31..9097333 100644

xen/arch/x86/acpi/power.c +6 -1
@@ -45,6 +45,8 @@ void do_suspend_lowlevel(void);
 
 static int device_power_down(void)  {
+    int err;
+
     console_suspend();
 
     time_suspend();
@@ -53,11 +55,22 @@ static int device_power_down(void)
 
     ioapic_suspend();
 
-    iommu_suspend();
+    err = iommu_suspend();
+
+    if ( err )
+        goto iommu_suspend_error;
 
     lapic_suspend();
 

xen/arch/x86/acpi/power.c static int device_power_down(void) · line 74

Quoted 10 lines
xen/arch/x86/acpi/power.c +8 -0
     return 0;
+
+ iommu_suspend_error:
+    ioapic_resume();
+    i8259A_resume();
+    time_resume();
+    console_resume();
+
+    return err;
 }

Jan had comment to better reuse device_power_up... looks no change in this version.

Quoted 8 lines
Quoted 2 lines

Yes, __iiuc__, this may be an optimization, but not a must. We can discuss this in detail In this version.

As an optimization it would indeed be quite pointless here. My request was more for maintainability: By re-using the function future changes don't need to go to two places, and hence there's no risk of one of them getting forgotten.

Got it, I will fix it in next v4.

Quan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#33 lore raw

Re: [PATCH v3 09/10] IOMMU: propagate IOMMU Device-TLB flush error up to IOMMU suspending

In reply to Jan Beulich
Quoted 42 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · On 04.05.16 at 04:14, <[email protected]> wrote:
Quoted 39 lines · Kevin
Quoted 26 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM diff --git a/xen/arch/x86/acpi/power.c b/xen/arch/x86/acpi/power.c index 2885e31..9097333 100644

xen/arch/x86/acpi/power.c +6 -1
@@ -45,6 +45,8 @@ void do_suspend_lowlevel(void);
 
 static int device_power_down(void)  {
+    int err;
+
     console_suspend();
 
     time_suspend();
@@ -53,11 +55,22 @@ static int device_power_down(void)
 
     ioapic_suspend();
 
-    iommu_suspend();
+    err = iommu_suspend();
+
+    if ( err )
+        goto iommu_suspend_error;
 
     lapic_suspend();
 

xen/arch/x86/acpi/power.c static int device_power_down(void) · line 74

Quoted 10 lines
xen/arch/x86/acpi/power.c +8 -0
     return 0;
+
+ iommu_suspend_error:
+    ioapic_resume();
+    i8259A_resume();
+    time_resume();
+    console_resume();
+
+    return err;
 }

Jan had comment to better reuse device_power_up... looks no change in this version.

Quoted 8 lines
Quoted 2 lines

Yes, __iiuc__, this may be an optimization, but not a must. We can discuss this in detail In this version.

As an optimization it would indeed be quite pointless here. My request was more for maintainability: By re-using the function future changes don't need to go to two places, and hence there's no risk of one of them getting forgotten.

Jan,
What about this one as below:

xen/arch/x86/acpi/power.c +35 -14
@@ -43,6 +43,17 @@ struct acpi_sleep_info acpi_sinfo;
 
 void do_suspend_lowlevel(void);
 
+enum dev_power_type {
+    TYPE_ALL,
+    TYPE_CONSOLE,
+    TYPE_TIME,
+    TYPE_I8259A,
+    TYPE_IOAPIC,
+    TYPE_IOMMU,
+    TYPE_LAPIC,
+    TYPE_UNKNOWN
+};
+
 static int device_power_down(void)
 {
     console_suspend();
@@ -53,26 +64,35 @@ static int device_power_down(void)
 
     ioapic_suspend();
 
-    iommu_suspend();
+    if ( iommu_suspend() )
+        return TYPE_IOMMU;
 
     lapic_suspend();
 
     return 0;
 }
 
-static void device_power_up(void)
+static void device_power_up(enum dev_power_type type)
 {
-    lapic_resume();
-
-    iommu_resume();
-
-    ioapic_resume();
-
-    i8259A_resume();
-
-    time_resume();
-
-    console_resume();
+    switch ( type ) {
+    case TYPE_ALL:
+    case TYPE_LAPIC:
+        lapic_resume();
+    case TYPE_IOMMU:
+        iommu_resume();
+    case TYPE_IOAPIC:
+        ioapic_resume();
+    case TYPE_I8259A:
+        i8259A_resume();
+    case TYPE_TIME:
+        time_resume();
+    case TYPE_CONSOLE:
+        console_resume();
+        break;
+    default:
+        BUG();
+        break;
+    }
 }
 
 static void freeze_domains(void)
@@ -169,6 +189,7 @@ static int enter_state(u32 state)
     {
         printk(XENLOG_ERR "Some devices failed to power down.");
         system_state = SYS_STATE_resume;
+        device_power_up(error);
         goto done;
     }
 
@@ -196,7 +217,7 @@ static int enter_state(u32 state)
     write_cr4(cr4 & ~X86_CR4_MCE);
     write_efer(read_efer());
 
-    device_power_up();
+    device_power_up(TYPE_ALL);
 
     mcheck_init(&boot_cpu_data, 0);
     write_cr4(cr4);

--
Quan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#34 lore raw

Re: [PATCH v3 09/10] IOMMU: propagate IOMMU Device-TLB flush error up to IOMMU suspending

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.05.16 at 12:18, <[email protected]> wrote:

What about this one as below:

If done properly (coding style, comments to silence Coverity, consistency throughout not just your change, but its effects on the rest of the code in that file), that's certainly an option.

Jan

Quoted 88 lines
xen/arch/x86/acpi/power.c +35 -15
@@ -43,6 +43,17 @@ struct acpi_sleep_info acpi_sinfo;
 
 void do_suspend_lowlevel(void);
 
+enum dev_power_type {
+    TYPE_ALL,
+    TYPE_CONSOLE,
+    TYPE_TIME,
+    TYPE_I8259A,
+    TYPE_IOAPIC,
+    TYPE_IOMMU,
+    TYPE_LAPIC,
+    TYPE_UNKNOWN
+};
+
 static int device_power_down(void)
 {
     console_suspend();
@@ -53,26 +64,35 @@ static int device_power_down(void)
 
     ioapic_suspend();
 
-    iommu_suspend();
+    if ( iommu_suspend() )
+        return TYPE_IOMMU;
 
     lapic_suspend();
 
     return 0;
 }
 
-static void device_power_up(void)
+static void device_power_up(enum dev_power_type type)
 {
-    lapic_resume();
-
-    iommu_resume();
-
-    ioapic_resume();
-
-    i8259A_resume();
-
-    time_resume();
-
-    console_resume();
+    switch ( type ) {
+    case TYPE_ALL:
+    case TYPE_LAPIC:
+        lapic_resume();
+    case TYPE_IOMMU:
+        iommu_resume();
+    case TYPE_IOAPIC:
+        ioapic_resume();
+    case TYPE_I8259A:
+        i8259A_resume();
+    case TYPE_TIME:
+        time_resume();
+    case TYPE_CONSOLE:
+        console_resume();
+        break;
+    default:
+        BUG();
+        break;
+    }
 }
 
 static void freeze_domains(void)
@@ -169,6 +189,7 @@ static int enter_state(u32 state)
     {
         printk(XENLOG_ERR "Some devices failed to power down.");
         system_state = SYS_STATE_resume;
+        device_power_up(error);
         goto done;
     }
 
@@ -196,7 +217,7 @@ static int enter_state(u32 state)
     write_cr4(cr4 & ~X86_CR4_MCE);
     write_efer(read_efer());
 
-    device_power_up();
+    device_power_up(TYPE_ALL);
 
     mcheck_init(&boot_cpu_data, 0);
     write_cr4(cr4);
 
--
Quoted 1 line

Quan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#35 lore raw

Re: [PATCH v3 09/10] IOMMU: propagate IOMMU Device-TLB flush error up to IOMMU suspending

Quoted 7 lines · Jan Beulich
Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 05.05.16 at 12:18, <[email protected]> wrote:

What about this one as below:

If done properly (coding style, comments to silence Coverity, consistency throughout not just your change, but its effects on the rest of the code in that file), that's certainly an option.

Thanks. I'll try it in next v4.

-Quan

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#36 lore raw

PATCH v3 10/10 vt-d: propagate error up to ME phantom function mapping and unmapping

In reply to Quan Xu

Propagate the IOMMU Device-TLB flush error up to ME phantom function mapping and unmapping.

CC Jan Beulich <[email protected]>
CC Kevin Tian <[email protected]>
CC Feng Wu <[email protected]>
---
 xen/drivers/passthrough/vtd/extern.h |  3 ++-
 xen/drivers/passthrough/vtd/iommu.c  | 18 ++++++++++++++----
 xen/drivers/passthrough/vtd/quirks.c | 28 ++++++++++++++++++----------
 3 files changed, 34 insertions(+), 15 deletions(-)
xen/drivers/passthrough/vtd/extern.h +2 -1
index cbe0286..6772839 100644
@@ -91,7 +91,8 @@ int is_igd_vt_enabled_quirk(void);
 void platform_quirks_init(void);
 void vtd_ops_preamble_quirk(struct iommu* iommu);
 void vtd_ops_postamble_quirk(struct iommu* iommu);
-void me_wifi_quirk(struct domain *domain, u8 bus, u8 devfn, int map);
+int __must_check me_wifi_quirk(struct domain *domain,
+                               u8 bus, u8 devfn, int map);
 void pci_vtd_quirk(const struct pci_dev *);
 bool_t platform_supports_intremap(void);
 bool_t platform_supports_x2apic(void);
xen/drivers/passthrough/vtd/iommu.c +14 -4
index cf847ec..bfee299 100644
@@ -1301,7 +1301,7 @@ int domain_context_mapping_one(
     u64 maddr, pgd_maddr;
     u16 seg = iommu->intel->drhd->segment;
     int agaw;
-    int rc;
+    int rc, ret;
 
     ASSERT(pcidevs_locked());
     spin_lock(&iommu->lock);
@@ -1435,7 +1435,12 @@ int domain_context_mapping_one(
     unmap_vtd_domain_page(context_entries);
 
     if ( !seg )
-        me_wifi_quirk(domain, bus, devfn, MAP_ME_PHANTOM_FUNC);
+    {
+        ret = me_wifi_quirk(domain, bus, devfn, MAP_ME_PHANTOM_FUNC);
+
+        if ( !rc && unlikely(ret) )
+            rc = ret;
+    }
 
     return rc;
 }
@@ -1532,7 +1537,7 @@ int domain_context_unmap_one(
     struct context_entry *context, *context_entries;
     u64 maddr;
     int iommu_domid;
-    int rc;
+    int rc, ret;
 
     ASSERT(pcidevs_locked());
     spin_lock(&iommu->lock);
@@ -1580,7 +1585,12 @@ int domain_context_unmap_one(
     unmap_vtd_domain_page(context_entries);
 
     if ( !iommu->intel->drhd->segment )
-        me_wifi_quirk(domain, bus, devfn, UNMAP_ME_PHANTOM_FUNC);
+    {
+        ret = me_wifi_quirk(domain, bus, devfn, UNMAP_ME_PHANTOM_FUNC);
+
+        if ( !rc && unlikely(ret) )
+            rc = ret;
+    }
 
     return rc;
 }
xen/drivers/passthrough/vtd/quirks.c +18 -10
index 49df41d..bc17d44 100644
@@ -332,10 +332,12 @@ void __init platform_quirks_init(void)
  * assigning Intel integrated wifi device to a guest.
  */
 
-static void map_me_phantom_function(struct domain *domain, u32 dev, int map)
+static int __must_check map_me_phantom_function(struct domain *domain,
+                                                u32 dev, int map)
 {
     struct acpi_drhd_unit *drhd;
     struct pci_dev *pdev;
+    int rc;
 
     /* find ME VT-d engine base on a real ME device */
     pdev = pci_get_pdev(0, 0, PCI_DEVFN(dev, 0));
@@ -343,23 +345,27 @@ static void map_me_phantom_function(struct domain *domain, u32 dev, int map)
 
     /* map or unmap ME phantom function */
     if ( map )
-        domain_context_mapping_one(domain, drhd->iommu, 0,
-                                   PCI_DEVFN(dev, 7), NULL);
+        rc = domain_context_mapping_one(domain, drhd->iommu, 0,
+                                        PCI_DEVFN(dev, 7), NULL);
     else
-        domain_context_unmap_one(domain, drhd->iommu, 0,
-                                 PCI_DEVFN(dev, 7));
+        rc = domain_context_unmap_one(domain, drhd->iommu, 0,
+                                      PCI_DEVFN(dev, 7));
+
+    return rc;
 }
 
-void me_wifi_quirk(struct domain *domain, u8 bus, u8 devfn, int map)
+int __must_check me_wifi_quirk(struct domain *domain,
+                               u8 bus, u8 devfn, int map)
 {
     u32 id;
+    int rc = 0;
 
     id = pci_conf_read32(0, 0, 0, 0, 0);
     if ( IS_CTG(id) )
     {
         /* quit if ME does not exist */
         if ( pci_conf_read32(0, 0, 3, 0, 0) == 0xffffffff )
-            return;
+            return 0;
 
         /* if device is WLAN device, map ME phantom device 0:3.7 */
         id = pci_conf_read32(0, bus, PCI_SLOT(devfn), PCI_FUNC(devfn), 0);
@@ -373,7 +379,7 @@ void me_wifi_quirk(struct domain *domain, u8 bus, u8 devfn, int map)
             case 0x423b8086:
             case 0x423c8086:
             case 0x423d8086:
-                map_me_phantom_function(domain, 3, map);
+                rc = map_me_phantom_function(domain, 3, map);
                 break;
             default:
                 break;
@@ -383,7 +389,7 @@ void me_wifi_quirk(struct domain *domain, u8 bus, u8 devfn, int map)
     {
         /* quit if ME does not exist */
         if ( pci_conf_read32(0, 0, 22, 0, 0) == 0xffffffff )
-            return;
+            return 0;
 
         /* if device is WLAN device, map ME phantom device 0:22.7 */
         id = pci_conf_read32(0, bus, PCI_SLOT(devfn), PCI_FUNC(devfn), 0);
@@ -399,12 +405,14 @@ void me_wifi_quirk(struct domain *domain, u8 bus, u8 devfn, int map)
             case 0x42388086:        /* Puma Peak */
             case 0x422b8086:
             case 0x422c8086:
-                map_me_phantom_function(domain, 22, map);
+                rc = map_me_phantom_function(domain, 22, map);
                 break;
             default:
                 break;
         }
     }
+
+    return rc;
 }
 
 void pci_vtd_quirk(const struct pci_dev *pdev)
Signature · 7 lines
1.9.1


_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
#37 lore raw

Re: [PATCH v3 10/10] vt-d: propagate error up to ME phantom function mapping and unmapping

Quoted 17 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

xen/drivers/passthrough/vtd/iommu.c +1 -1
index cf847ec..bfee299 100644
@@ -1301,7 +1301,7 @@ int domain_context_mapping_one(
     u64 maddr, pgd_maddr;
     u16 seg = iommu->intel->drhd->segment;
     int agaw;
-    int rc;
+    int rc, ret;
 
     ASSERT(pcidevs_locked());
     spin_lock(&iommu->lock);

xen/drivers/passthrough/vtd/iommu.c int domain_context_mapping_one( · line 1443

Quoted 10 lines
xen/drivers/passthrough/vtd/iommu.c +6 -1
@@ -1435,7 +1435,12 @@ int domain_context_mapping_one(
     unmap_vtd_domain_page(context_entries);
 
     if ( !seg )
-        me_wifi_quirk(domain, bus, devfn, MAP_ME_PHANTOM_FUNC);
+    {
+        ret = me_wifi_quirk(domain, bus, devfn, MAP_ME_PHANTOM_FUNC);
+
+        if ( !rc && unlikely(ret) )
+            rc = ret;
+    }

similarly to make it simple no need to check ret again.

xen/drivers/passthrough/vtd/iommu.c int domain_context_mapping_one( · line 1445

Quoted 3 lines
xen/drivers/passthrough/vtd/iommu.c +0 -0
     return rc;
 }

Thanks
Kevin

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

author
#38 lore raw

Re: [PATCH v3 10/10] vt-d: propagate error up to ME phantom function mapping and unmapping

Quoted 19 lines · Kevin
Quoted 19 lines

From: Xu, Quan
Sent: Friday, April 29, 2016 5:25 PM

xen/drivers/passthrough/vtd/iommu.c +1 -1
index cf847ec..bfee299 100644
@@ -1301,7 +1301,7 @@ int domain_context_mapping_one(
     u64 maddr, pgd_maddr;
     u16 seg = iommu->intel->drhd->segment;
     int agaw;
-    int rc;
+    int rc, ret;
 
     ASSERT(pcidevs_locked());
     spin_lock(&iommu->lock);
@@ -1435,7 +1435,12 @@ int domain_context_mapping_one(
     unmap_vtd_domain_page(context_entries);
 
Quoted 10 lines

xen/drivers/passthrough/vtd/iommu.c int domain_context_mapping_one( · line 1443

Quoted 8 lines
xen/drivers/passthrough/vtd/iommu.c +6 -1
     if ( !seg )
-        me_wifi_quirk(domain, bus, devfn, MAP_ME_PHANTOM_FUNC);
+    {
+        ret = me_wifi_quirk(domain, bus, devfn, MAP_ME_PHANTOM_FUNC);
+
+        if ( !rc && unlikely(ret) )
+            rc = ret;
+    }

similarly to make it simple no need to check ret again.

Agreed, I also hesitated to add this check. I'll drop similar ret check in this patch set.

Quan

Quoted 4 lines

xen/drivers/passthrough/vtd/iommu.c int domain_context_mapping_one( · line 1445

Quoted 4 lines
xen/drivers/passthrough/vtd/iommu.c +0 -0
     return rc;
 }

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel