Skip to contentSkip to conversation

IOMMU, vtd and iotlb flush rework (v6)

PATCH In review Jean Guyader 3 participants 20 messages Nov 10, 2011 – Nov 11, 2011

lore
Share & tools
lore
#0 lore raw

cover IOMMU, vtd and iotlb flush rework (v6)

In one of my previous email I detailed a bug I was seeing when passing through a Intel GPU on a guest that has more that 4G or RAM.

Allen suggested that I go for the Plan B but after a discussion with Tim we agreed that Plan B was way to disruptive in term of code change.

This patch series implements Plan A.

http://xen.1045712.n5.nabble.com/VTD-Intel-iommu-IOTLB-flush-really-slow-td4952866.html

Changes between v5 and v6:
- Rework the patch queue to make it more readable.
- Modify xatp in place in xenmem_add_to_physmap
- Only check for preemption if we are not at the last iteration
- Copy xatp guest handler back to the guest only in case of continuation
- Add continuation only when dealing with the new xenmem space (XENMAPSPACE_gmfn_range).

Changes between v4 and v5:
- Fix hypercall continuation for add_to_physmap in compat mode.

Changes between v3 and v4:
- Move the loop for gmfn_range from arch_memory_op to xenmem_add_to_physmap.
- Add a comment to comment to explain the purpose of iommu_dont_flush_iotlb.

Changes between v2 and v3:
- Check for the presence iotlb_flush_all callback before calling it.

Changes between v1 and v2:
- Move size in struct xen_add_to_physmap in padding between .domid and .space.
- Store iommu_dont_flush per cpu
- Change the code in hvmloader to relocate by batch of 64K, .size is now 16 bits.

Jean Guyader (7):
vtd: Refactor iotlb flush code
iommu: Introduce iommu_flush and iommu_flush_all.
add_to_physmap: Move the code for XENMEM_add_to_physmap
mm: xenmem_add_to_physmap now takes a pointer on xatp
mm: New XENMEM space, XENMAPSPACE_gmfn_range
hvmloader: Change memory relocation loop when overlap with PCI hole Introduce per cpu flag (iommu_dont_flush_iotlb) to avoid unnecessary
iotlb flush

 tools/firmware/hvmloader/pci.c      |   20 ++-
 xen/arch/x86/mm.c                   |  232 ++++++++++++++++++++++-------------
 xen/arch/x86/x86_64/compat/mm.c     |   12 ++
 xen/drivers/passthrough/iommu.c     |   25 ++++
 xen/drivers/passthrough/vtd/iommu.c |  100 +++++++++-------
 xen/include/public/memory.h         |    4 +
 xen/include/xen/iommu.h             |   17 +++
 7 files changed, 278 insertions(+), 132 deletions(-)

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

#1 lore raw

PATCH 1/7 vtd: Refactor iotlb flush code

Factorize the iotlb flush code from map_page and unmap_page into it's own function.

---
 xen/drivers/passthrough/vtd/iommu.c |   86 +++++++++++++++++-----------------
 1 files changed, 43 insertions(+), 43 deletions(-)
xen/drivers/passthrough/vtd/iommu.c +43 -43
index 7717ab4..5a5b6be 100644
@@ -578,16 +578,53 @@ 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)
+{
+    struct hvm_iommu *hd = domain_hvm_iommu(d);
+    struct acpi_drhd_unit *drhd;
+    struct iommu *iommu;
+    int flush_dev_iotlb;
+    int iommu_domid;
+
+    /*
+     * No need pcideves_lock here because we have flush
+     * when assign/deassign device
+     */
+    for_each_drhd_unit ( drhd )
+    {
+        iommu = drhd->iommu;
+
+        if ( !test_bit(iommu->index, &hd->iommu_bitmap) )
+            continue;
+
+        flush_dev_iotlb = find_ats_dev_drhd(iommu) ? 1 : 0;
+        iommu_domid= domain_iommu_domid(d, iommu);
+        if ( iommu_domid == -1 )
+            continue;
+
+        if ( page_count > 1 || gfn == -1 )
+        {
+            if ( iommu_flush_iotlb_dsi(iommu, iommu_domid,
+                        0, flush_dev_iotlb) )
+                iommu_flush_write_buffer(iommu);
+        }
+        else
+        {
+            if ( iommu_flush_iotlb_psi(iommu, iommu_domid,
+                        (paddr_t)gfn << PAGE_SHIFT_4K, 0,
+                        !dma_old_pte_present, flush_dev_iotlb) )
+                iommu_flush_write_buffer(iommu);
+        }
+    }
+}
+
 /* clear one page's page table */
 static void dma_pte_clear_one(struct domain *domain, u64 addr)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(domain);
-    struct acpi_drhd_unit *drhd;
-    struct iommu *iommu;
     struct dma_pte *page = NULL, *pte = NULL;
     u64 pg_maddr;
-    int flush_dev_iotlb;
-    int iommu_domid;
     struct mapped_rmrr *mrmrr;
 
     spin_lock(&hd->mapping_lock);
@@ -613,21 +650,7 @@ static void dma_pte_clear_one(struct domain *domain, u64 addr)
     spin_unlock(&hd->mapping_lock);
     iommu_flush_cache_entry(pte, sizeof(struct dma_pte));
 
-    /* No need pcidevs_lock here since do that on assign/deassign device*/
-    for_each_drhd_unit ( drhd )
-    {
-        iommu = drhd->iommu;
-        if ( test_bit(iommu->index, &hd->iommu_bitmap) )
-        {
-            flush_dev_iotlb = find_ats_dev_drhd(iommu) ? 1 : 0;
-            iommu_domid= domain_iommu_domid(domain, iommu);
-            if ( iommu_domid == -1 )
-                continue;
-            if ( iommu_flush_iotlb_psi(iommu, iommu_domid, addr,
-                                       0, 0, flush_dev_iotlb) )
-                iommu_flush_write_buffer(iommu);
-        }
-    }
+    __intel_iommu_iotlb_flush(domain, addr >> PAGE_SHIFT_4K , 0, 1);
 
     unmap_vtd_domain_page(page);
 
@@ -1677,12 +1700,8 @@ static int intel_iommu_map_page(
     unsigned int flags)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
-    struct acpi_drhd_unit *drhd;
-    struct iommu *iommu;
     struct dma_pte *page = NULL, *pte = NULL, old, new = { 0 };
     u64 pg_maddr;
-    int flush_dev_iotlb;
-    int iommu_domid;
 
     /* Do nothing if VT-d shares EPT page table */
     if ( iommu_use_hap_pt(d) )
@@ -1724,26 +1743,7 @@ static int intel_iommu_map_page(
     spin_unlock(&hd->mapping_lock);
     unmap_vtd_domain_page(page);
 
-    /*
-     * No need pcideves_lock here because we have flush
-     * when assign/deassign device
-     */
-    for_each_drhd_unit ( drhd )
-    {
-        iommu = drhd->iommu;
-
-        if ( !test_bit(iommu->index, &hd->iommu_bitmap) )
-            continue;
-
-        flush_dev_iotlb = find_ats_dev_drhd(iommu) ? 1 : 0;
-        iommu_domid= domain_iommu_domid(d, iommu);
-        if ( iommu_domid == -1 )
-            continue;
-        if ( iommu_flush_iotlb_psi(iommu, iommu_domid,
-                                   (paddr_t)gfn << PAGE_SHIFT_4K, 0,
-                                   !dma_pte_present(old), flush_dev_iotlb) )
-            iommu_flush_write_buffer(iommu);
-    }
+    __intel_iommu_iotlb_flush(d, gfn, dma_pte_present(old), 1);
 
     return 0;
 }

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

#2 lore raw

PATCH 2/7 iommu: Introduce iommu_flush and iommu_flush_all.

---
 xen/drivers/passthrough/iommu.c     |   20 ++++++++++++++++++++
 xen/drivers/passthrough/vtd/iommu.c |   12 ++++++++++++
 xen/include/xen/iommu.h             |    5 +++++
 3 files changed, 37 insertions(+), 0 deletions(-)
xen/drivers/passthrough/iommu.c +20 -0
index cd6174d..ca7b37b 100644
@@ -301,6 +301,26 @@ int iommu_unmap_page(struct domain *d, unsigned long gfn)
     return hd->platform_ops->unmap_page(d, gfn);
 }
 
+void 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;
+
+    hd->platform_ops->iotlb_flush(d, gfn, page_count);
+}
+
+void 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;
+
+    hd->platform_ops->iotlb_flush_all(d);
+}
+
 /* caller should hold the pcidevs_lock */
 int deassign_device(struct domain *d, u16 seg, u8 bus, u8 devfn)
 {
xen/drivers/passthrough/vtd/iommu.c +12 -0
index 5a5b6be..7ec9541 100644
@@ -619,6 +619,16 @@ static void __intel_iommu_iotlb_flush(struct domain *d, unsigned long gfn,
     }
 }
 
+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);
+}
+
+static void intel_iommu_iotlb_flush_all(struct domain *d)
+{
+    __intel_iommu_iotlb_flush(d, 0, 0, 0);
+}
+
 /* clear one page's page table */
 static void dma_pte_clear_one(struct domain *domain, u64 addr)
 {
@@ -2329,6 +2339,8 @@ const struct iommu_ops intel_iommu_ops = {
     .resume = vtd_resume,
     .share_p2m = iommu_set_pgd,
     .crash_shutdown = vtd_crash_shutdown,
+    .iotlb_flush = intel_iommu_iotlb_flush,
+    .iotlb_flush_all = intel_iommu_iotlb_flush_all,
 };
 
 /*
xen/include/xen/iommu.h +5 -0
index 837e60d..a1034df 100644
@@ -139,6 +139,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);
 };
 
 void iommu_update_ire_from_apic(unsigned int apic, unsigned int reg, unsigned int value);
@@ -155,4 +157,7 @@ void iommu_share_p2m_table(struct domain *d);
 
 int iommu_do_domctl(struct xen_domctl *, XEN_GUEST_HANDLE(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);
+
 #endif /* _IOMMU_H_ */

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

#3 lore raw

PATCH 3/7 add_to_physmap: Move the code for XENMEM_add_to_physmap

Move the code for the XENMEM_add_to_physmap case into it's own function (xenmem_add_to_physmap).

---
 xen/arch/x86/mm.c |  189 ++++++++++++++++++++++++++++-------------------------
 1 files changed, 100 insertions(+), 89 deletions(-)
xen/arch/x86/mm.c +100 -89
index acc1f34..66a07bd 100644
@@ -4592,9 +4592,107 @@ static int handle_iomem_range(unsigned long s, unsigned long e, void *p)
     return 0;
 }
 
+int xenmem_add_to_physmap(struct domain *d,
+                          struct xen_add_to_physmap xatp)
+{
+    struct page_info* page = NULL;
+    unsigned long mfn = 0;
+    unsigned long prev_mfn, gpfn;
+    int rc;
+
+    switch ( xatp.space )
+    {
+    case XENMAPSPACE_shared_info:
+        if ( xatp.idx == 0 )
+            mfn = virt_to_mfn(d->shared_info);
+        break;
+    case XENMAPSPACE_grant_table:
+        spin_lock(&d->grant_table->lock);
+
+        if ( d->grant_table->gt_version == 0 )
+            d->grant_table->gt_version = 1;
+
+        if ( d->grant_table->gt_version == 2 &&
+             (xatp.idx & XENMAPIDX_grant_table_status) )
+        {
+            xatp.idx &= ~XENMAPIDX_grant_table_status;
+            if ( xatp.idx < nr_status_frames(d->grant_table) )
+                mfn = virt_to_mfn(d->grant_table->status[xatp.idx]);
+        }
+        else
+        {
+            if ( (xatp.idx >= nr_grant_frames(d->grant_table)) &&
+                 (xatp.idx < max_nr_grant_frames) )
+                gnttab_grow_table(d, xatp.idx + 1);
+
+            if ( xatp.idx < nr_grant_frames(d->grant_table) )
+                mfn = virt_to_mfn(d->grant_table->shared_raw[xatp.idx]);
+        }
+
+        spin_unlock(&d->grant_table->lock);
+        break;
+    case XENMAPSPACE_gmfn:
+    {
+        p2m_type_t p2mt;
+
+        xatp.idx = mfn_x(gfn_to_mfn_unshare(d, xatp.idx, &p2mt));
+        /* If the page is still shared, exit early */
+        if ( p2m_is_shared(p2mt) )
+        {
+            rcu_unlock_domain(d);
+            return -ENOMEM;
+        }
+        if ( !get_page_from_pagenr(xatp.idx, d) )
+            break;
+        mfn = xatp.idx;
+        page = mfn_to_page(mfn);
+        break;
+    }
+    default:
+        break;
+    }
+
+    if ( !paging_mode_translate(d) || (mfn == 0) )
+    {
+        if ( page )
+            put_page(page);
+        rcu_unlock_domain(d);
+        return -EINVAL;
+    }
+
+    domain_lock(d);
+
+    if ( page )
+        put_page(page);
+
+    /* Remove previously mapped page if it was present. */
+    prev_mfn = gmfn_to_mfn(d, xatp.gpfn);
+    if ( mfn_valid(prev_mfn) )
+    {
+        if ( is_xen_heap_mfn(prev_mfn) )
+            /* Xen heap frames are simply unhooked from this phys slot. */
+            guest_physmap_remove_page(d, xatp.gpfn, prev_mfn, 0);
+        else
+            /* Normal domain memory is freed, to avoid leaking memory. */
+            guest_remove_page(d, xatp.gpfn);
+    }
+
+    /* Unmap from old location, if any. */
+    gpfn = get_gpfn_from_mfn(mfn);
+    ASSERT( gpfn != SHARED_M2P_ENTRY );
+    if ( gpfn != INVALID_M2P_ENTRY )
+        guest_physmap_remove_page(d, gpfn, mfn, 0);
+
+    /* Map at new location. */
+    rc = guest_physmap_add_page(d, xatp.gpfn, mfn, 0);
+
+    domain_unlock(d);
+
+    return rc;
+}
+
 long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
 {
-    struct page_info *page = NULL;
     int rc;
 
     switch ( op )
@@ -4602,7 +4700,6 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
     case XENMEM_add_to_physmap:
     {
         struct xen_add_to_physmap xatp;
-        unsigned long prev_mfn, mfn = 0, gpfn;
         struct domain *d;
 
         if ( copy_from_guest(&xatp, arg, 1) )
@@ -4618,93 +4715,7 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
             return -EPERM;
         }
 
-        switch ( xatp.space )
-        {
-        case XENMAPSPACE_shared_info:
-            if ( xatp.idx == 0 )
-                mfn = virt_to_mfn(d->shared_info);
-            break;
-        case XENMAPSPACE_grant_table:
-            spin_lock(&d->grant_table->lock);
-
-            if ( d->grant_table->gt_version == 0 )
-                d->grant_table->gt_version = 1;
-
-            if ( d->grant_table->gt_version == 2 &&
-                 (xatp.idx & XENMAPIDX_grant_table_status) )
-            {
-                xatp.idx &= ~XENMAPIDX_grant_table_status;
-                if ( xatp.idx < nr_status_frames(d->grant_table) )
-                    mfn = virt_to_mfn(d->grant_table->status[xatp.idx]);
-            }
-            else
-            {
-                if ( (xatp.idx >= nr_grant_frames(d->grant_table)) &&
-                     (xatp.idx < max_nr_grant_frames) )
-                    gnttab_grow_table(d, xatp.idx + 1);
-
-                if ( xatp.idx < nr_grant_frames(d->grant_table) )
-                    mfn = virt_to_mfn(d->grant_table->shared_raw[xatp.idx]);
-            }
-
-            spin_unlock(&d->grant_table->lock);
-            break;
-        case XENMAPSPACE_gmfn:
-        {
-            p2m_type_t p2mt;
-
-            xatp.idx = mfn_x(gfn_to_mfn_unshare(d, xatp.idx, &p2mt));
-            /* If the page is still shared, exit early */
-            if ( p2m_is_shared(p2mt) )
-            {
-                rcu_unlock_domain(d);
-                return -ENOMEM;
-            }
-            if ( !get_page_from_pagenr(xatp.idx, d) )
-                break;
-            mfn = xatp.idx;
-            page = mfn_to_page(mfn);
-            break;
-        }
-        default:
-            break;
-        }
-
-        if ( !paging_mode_translate(d) || (mfn == 0) )
-        {
-            if ( page )
-                put_page(page);
-            rcu_unlock_domain(d);
-            return -EINVAL;
-        }
-
-        domain_lock(d);
-
-        if ( page )
-            put_page(page);
-
-        /* Remove previously mapped page if it was present. */
-        prev_mfn = gmfn_to_mfn(d, xatp.gpfn);
-        if ( mfn_valid(prev_mfn) )
-        {
-            if ( is_xen_heap_mfn(prev_mfn) )
-                /* Xen heap frames are simply unhooked from this phys slot. */
-                guest_physmap_remove_page(d, xatp.gpfn, prev_mfn, 0);
-            else
-                /* Normal domain memory is freed, to avoid leaking memory. */
-                guest_remove_page(d, xatp.gpfn);
-        }
-
-        /* Unmap from old location, if any. */
-        gpfn = get_gpfn_from_mfn(mfn);
-        ASSERT( gpfn != SHARED_M2P_ENTRY );
-        if ( gpfn != INVALID_M2P_ENTRY )
-            guest_physmap_remove_page(d, gpfn, mfn, 0);
-
-        /* Map at new location. */
-        rc = guest_physmap_add_page(d, xatp.gpfn, mfn, 0);
-
-        domain_unlock(d);
+        rc = xenmem_add_to_physmap(d, xatp);
 
         rcu_unlock_domain(d);
 

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

#4 lore raw

PATCH 4/7 mm: xenmem_add_to_physmap now takes a pointer on xatp

---
 xen/arch/x86/mm.c |   40 ++++++++++++++++++++--------------------
 1 files changed, 20 insertions(+), 20 deletions(-)
xen/arch/x86/mm.c +20 -20
index 66a07bd..c0da115 100644
@@ -4593,17 +4593,17 @@ static int handle_iomem_range(unsigned long s, unsigned long e, void *p)
 }
 
 int xenmem_add_to_physmap(struct domain *d,
-                          struct xen_add_to_physmap xatp)
+                          struct xen_add_to_physmap *xatp)
 {
     struct page_info* page = NULL;
     unsigned long mfn = 0;
     unsigned long prev_mfn, gpfn;
     int rc;
 
-    switch ( xatp.space )
+    switch ( xatp->space )
     {
     case XENMAPSPACE_shared_info:
-        if ( xatp.idx == 0 )
+        if ( xatp->idx == 0 )
             mfn = virt_to_mfn(d->shared_info);
         break;
     case XENMAPSPACE_grant_table:
@@ -4613,20 +4613,20 @@ int xenmem_add_to_physmap(struct domain *d,
             d->grant_table->gt_version = 1;
 
         if ( d->grant_table->gt_version == 2 &&
-             (xatp.idx & XENMAPIDX_grant_table_status) )
+             (xatp->idx & XENMAPIDX_grant_table_status) )
         {
-            xatp.idx &= ~XENMAPIDX_grant_table_status;
-            if ( xatp.idx < nr_status_frames(d->grant_table) )
-                mfn = virt_to_mfn(d->grant_table->status[xatp.idx]);
+            xatp->idx &= ~XENMAPIDX_grant_table_status;
+            if ( xatp->idx < nr_status_frames(d->grant_table) )
+                mfn = virt_to_mfn(d->grant_table->status[xatp->idx]);
         }
         else
         {
-            if ( (xatp.idx >= nr_grant_frames(d->grant_table)) &&
-                 (xatp.idx < max_nr_grant_frames) )
-                gnttab_grow_table(d, xatp.idx + 1);
+            if ( (xatp->idx >= nr_grant_frames(d->grant_table)) &&
+                 (xatp->idx < max_nr_grant_frames) )
+                gnttab_grow_table(d, xatp->idx + 1);
 
-            if ( xatp.idx < nr_grant_frames(d->grant_table) )
-                mfn = virt_to_mfn(d->grant_table->shared_raw[xatp.idx]);
+            if ( xatp->idx < nr_grant_frames(d->grant_table) )
+                mfn = virt_to_mfn(d->grant_table->shared_raw[xatp->idx]);
         }
 
         spin_unlock(&d->grant_table->lock);
@@ -4635,16 +4635,16 @@ int xenmem_add_to_physmap(struct domain *d,
     {
         p2m_type_t p2mt;
 
-        xatp.idx = mfn_x(gfn_to_mfn_unshare(d, xatp.idx, &p2mt));
+        xatp->idx = mfn_x(gfn_to_mfn_unshare(d, xatp->idx, &p2mt));
         /* If the page is still shared, exit early */
         if ( p2m_is_shared(p2mt) )
         {
             rcu_unlock_domain(d);
             return -ENOMEM;
         }
-        if ( !get_page_from_pagenr(xatp.idx, d) )
+        if ( !get_page_from_pagenr(xatp->idx, d) )
             break;
-        mfn = xatp.idx;
+        mfn = xatp->idx;
         page = mfn_to_page(mfn);
         break;
     }
@@ -4666,15 +4666,15 @@ int xenmem_add_to_physmap(struct domain *d,
         put_page(page);
 
     /* Remove previously mapped page if it was present. */
-    prev_mfn = gmfn_to_mfn(d, xatp.gpfn);
+    prev_mfn = gmfn_to_mfn(d, xatp->gpfn);
     if ( mfn_valid(prev_mfn) )
     {
         if ( is_xen_heap_mfn(prev_mfn) )
             /* Xen heap frames are simply unhooked from this phys slot. */
-            guest_physmap_remove_page(d, xatp.gpfn, prev_mfn, 0);
+            guest_physmap_remove_page(d, xatp->gpfn, prev_mfn, 0);
         else
             /* Normal domain memory is freed, to avoid leaking memory. */
-            guest_remove_page(d, xatp.gpfn);
+            guest_remove_page(d, xatp->gpfn);
     }
 
     /* Unmap from old location, if any. */
@@ -4684,7 +4684,7 @@ int xenmem_add_to_physmap(struct domain *d,
         guest_physmap_remove_page(d, gpfn, mfn, 0);
 
     /* Map at new location. */
-    rc = guest_physmap_add_page(d, xatp.gpfn, mfn, 0);
+    rc = guest_physmap_add_page(d, xatp->gpfn, mfn, 0);
 
     domain_unlock(d);
 
@@ -4715,7 +4715,7 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
             return -EPERM;
         }
 
-        rc = xenmem_add_to_physmap(d, xatp);
+        rc = xenmem_add_to_physmap(d, &xatp);
 
         rcu_unlock_domain(d);
 

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

#5 lore raw

PATCH 5/7 mm: New XENMEM space, XENMAPSPACE_gmfn_range

XENMAPSPACE_gmfn_range is like XENMAPSPACE_gmfn but it runs on a range of pages. The size of the range is defined in a new field.

This new field .size is located in the 16 bits padding between .domid and .space in struct xen_add_to_physmap to stay compatible with older versions.

---
 xen/arch/x86/mm.c               |   47 +++++++++++++++++++++++++++++++++++++-
 xen/arch/x86/x86_64/compat/mm.c |   12 ++++++++++
 xen/include/public/memory.h     |    4 +++
 3 files changed, 61 insertions(+), 2 deletions(-)
xen/arch/x86/mm.c +45 -2
index c0da115..19d301b 100644
@@ -4592,8 +4592,8 @@ static int handle_iomem_range(unsigned long s, unsigned long e, void *p)
     return 0;
 }
 
-int xenmem_add_to_physmap(struct domain *d,
-                          struct xen_add_to_physmap *xatp)
+int xenmem_add_to_physmap_once(struct domain *d,
+                               struct xen_add_to_physmap *xatp)
 {
     struct page_info* page = NULL;
     unsigned long mfn = 0;
@@ -4631,6 +4631,7 @@ int xenmem_add_to_physmap(struct domain *d,
 
         spin_unlock(&d->grant_table->lock);
         break;
+    case XENMAPSPACE_gmfn_range:
     case XENMAPSPACE_gmfn:
     {
         p2m_type_t p2mt;
@@ -4691,6 +4692,37 @@ int xenmem_add_to_physmap(struct domain *d,
     return rc;
 }
 
+int xenmem_add_to_physmap(struct domain *d,
+                          struct xen_add_to_physmap *xatp)
+{
+    int rc = 0;
+
+    if ( xatp->space == XENMAPSPACE_gmfn_range )
+    {
+        while ( xatp->size > 0 )
+        {
+            rc = xenmem_add_to_physmap_once(d, xatp);
+            if ( rc < 0 )
+                return rc;
+
+            xatp->idx++;
+            xatp->gpfn++;
+            xatp->size--;
+
+            /* Check for continuation if it's not the last interation */
+            if ( xatp->size > 0 && hypercall_preempt_check() )
+            {
+                rc = -EAGAIN;
+                break;
+            }
+        }
+
+        return rc;
+    }
+
+    return xenmem_add_to_physmap_once(d, xatp);
+}
+
 long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
 {
     int rc;
@@ -4716,6 +4748,17 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
         }
 
         rc = xenmem_add_to_physmap(d, &xatp);
+        if ( rc == -EAGAIN )
+        {
+            if ( copy_to_guest(arg, &xatp, 1) )
+            {
+                rcu_unlock_domain(d);
+                return -EFAULT;
+            }
+
+            rc = hypercall_create_continuation(
+                    __HYPERVISOR_memory_op, "ih", op, arg);
+        }
 
         rcu_unlock_domain(d);
 
xen/arch/x86/x86_64/compat/mm.c +12 -0
index 2c05099..8f4be25 100644
@@ -63,6 +63,18 @@ int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
 
         XLAT_add_to_physmap(nat, &cmp);
         rc = arch_memory_op(op, guest_handle_from_ptr(nat, void));
+        if ( rc < 0 )
+            break;
+
+        if ( rc == __HYPERVISOR_memory_op )
+        {
+            hypercall_xlat_continuation(NULL, 0x2, nat, arg);
+
+            XLAT_add_to_physmap(&cmp, nat);
+
+            if ( copy_to_guest(arg, &cmp, 1) )
+                return -EFAULT;
+        }
 
         break;
     }
xen/include/public/memory.h +4 -0
index 08355e3..c5b78a8 100644
@@ -208,10 +208,14 @@ struct xen_add_to_physmap {
     /* Which domain to change the mapping for. */
     domid_t domid;
 
+    /* Number of pages to go through for gmfn_range */
+    uint16_t    size;
+
     /* Source mapping space. */
 #define XENMAPSPACE_shared_info 0 /* shared info page */
 #define XENMAPSPACE_grant_table 1 /* grant table page */
 #define XENMAPSPACE_gmfn        2 /* GMFN */
+#define XENMAPSPACE_gmfn_range  3 /* GMFN range */
     unsigned int space;
 
 #define XENMAPIDX_grant_table_status 0x80000000

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

#6 lore raw

PATCH 6/7 hvmloader: Change memory relocation loop when overlap with PCI hole

Change the way we relocate the memory page if they overlap with pci hole. Use new map space (XENMAPSPACE_gmfn_range) to move the loop into xen.

This code usually get triggered when a device is pass through to a guest and the PCI hole has to be extended to have enough room to map the device BARs. The PCI hole will starts lower and it might overlap with some RAM that has been alocated for the guest. That usually happen if the guest has more than 4G of RAM. We have to relocate those pages in high mem otherwise they won't be accessible.

---
 tools/firmware/hvmloader/pci.c |   20 ++++++++++++++------
 1 files changed, 14 insertions(+), 6 deletions(-)
tools/firmware/hvmloader/pci.c +14 -6
index 29ec011..3bd6ac5 100644
@@ -50,6 +50,7 @@ void pci_setup(void)
         uint32_t devfn, bar_reg, bar_sz;
     } *bars = (struct bars *)scratch_start;
     unsigned int i, nr_bars = 0;
+    unsigned long pci_mem_start_pg;
 
     /* Program PCI-ISA bridge with appropriate link routes. */
     isa_irq = 0;
@@ -185,17 +186,24 @@ void pci_setup(void)
             ((pci_mem_start << 1) != 0) )
         pci_mem_start <<= 1;
 
-    while ( (pci_mem_start >> PAGE_SHIFT) < hvm_info->low_mem_pgend )
+    /* Relocate RAM that overlaps (in 64K chunks) */
+    pci_mem_start_pg = (pci_mem_start >> PAGE_SHIFT);
+    while (pci_mem_start_pg < hvm_info->low_mem_pgend)
     {
         struct xen_add_to_physmap xatp;
-        if ( hvm_info->high_mem_pgend == 0 )
-            hvm_info->high_mem_pgend = 1ull << (32 - PAGE_SHIFT);
+        unsigned int size = hvm_info->low_mem_pgend - pci_mem_start_pg;
         xatp.domid = DOMID_SELF;
-        xatp.space = XENMAPSPACE_gmfn;
-        xatp.idx   = --hvm_info->low_mem_pgend;
-        xatp.gpfn  = hvm_info->high_mem_pgend++;
+        xatp.space = XENMAPSPACE_gmfn_range;
+        xatp.idx = pci_mem_start_pg;
+        xatp.gpfn = hvm_info->high_mem_pgend;
+        size = size > ((1 << 16) - 1) ? ((1 << 16) - 1) : size;
+        xatp.size = size;
+
         if ( hypercall_memory_op(XENMEM_add_to_physmap, &xatp) != 0 )
             BUG();
+        pci_mem_start_pg += size;
+        hvm_info->high_mem_pgend += size;
+        hvm_info->low_mem_pgend = pci_mem_start_pg;
     }
 
     mem_resource.base = pci_mem_start;

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

#7 lore raw

PATCH 7/7 Introduce per cpu flag (iommu_dont_flush_iotlb) to avoid unnecessary iotlb flush

Add cpu flag that will be checked by the iommu low level code to skip iotlb flushes. iommu_iotlb_flush shall be called explicitly.

---
 xen/arch/x86/mm.c                   |   12 ++++++++++++
 xen/drivers/passthrough/iommu.c     |    5 +++++
 xen/drivers/passthrough/vtd/iommu.c |    6 ++++--
 xen/include/xen/iommu.h             |   12 ++++++++++++
 4 files changed, 33 insertions(+), 2 deletions(-)
xen/arch/x86/mm.c +12 -0
index 19d301b..186b0c7 100644
@@ -4695,10 +4695,15 @@ int xenmem_add_to_physmap_once(struct domain *d,
 int xenmem_add_to_physmap(struct domain *d,
                           struct xen_add_to_physmap *xatp)
 {
+    struct xen_add_to_physmap start_xatp;
     int rc = 0;
 
     if ( xatp->space == XENMAPSPACE_gmfn_range )
     {
+        if ( need_iommu(d) )
+            this_cpu(iommu_dont_flush_iotlb) = 1;
+
+        start_xatp = *xatp;
         while ( xatp->size > 0 )
         {
             rc = xenmem_add_to_physmap_once(d, xatp);
@@ -4717,6 +4722,13 @@ int xenmem_add_to_physmap(struct domain *d,
             }
         }
 
+        if ( need_iommu(d) )
+        {
+            this_cpu(iommu_dont_flush_iotlb) = 0;
+            iommu_iotlb_flush(d, start_xatp.idx, start_xatp.size - xatp->size);
+            iommu_iotlb_flush(d, start_xatp.gpfn, start_xatp.size - xatp->size);
+        }
+
         return rc;
     }
 
xen/drivers/passthrough/iommu.c +5 -0
index ca7b37b..bacca11 100644
@@ -52,6 +52,8 @@ bool_t __read_mostly iommu_hap_pt_share = 1;
 bool_t __read_mostly iommu_debug;
 bool_t __read_mostly amd_iommu_perdev_intremap;
 
+DEFINE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
+
 static void __init parse_iommu_param(char *s)
 {
     char *ss;
@@ -227,6 +229,7 @@ static int iommu_populate_page_table(struct domain *d)
 
     spin_lock(&d->page_alloc_lock);
 
+    this_cpu(iommu_dont_flush_iotlb) = 1;
     page_list_for_each ( page, &d->page_list )
     {
         if ( is_hvm_domain(d) ||
@@ -244,6 +247,8 @@ static int iommu_populate_page_table(struct domain *d)
             }
         }
     }
+    this_cpu(iommu_dont_flush_iotlb) = 0;
+    iommu_iotlb_flush_all(d);
     spin_unlock(&d->page_alloc_lock);
     return 0;
 }
xen/drivers/passthrough/vtd/iommu.c +4 -2
index 7ec9541..a3dd018 100644
@@ -660,7 +660,8 @@ static void dma_pte_clear_one(struct domain *domain, u64 addr)
     spin_unlock(&hd->mapping_lock);
     iommu_flush_cache_entry(pte, sizeof(struct dma_pte));
 
-    __intel_iommu_iotlb_flush(domain, addr >> PAGE_SHIFT_4K , 0, 1);
+    if ( !this_cpu(iommu_dont_flush_iotlb) )
+        __intel_iommu_iotlb_flush(domain, addr >> PAGE_SHIFT_4K , 0, 1);
 
     unmap_vtd_domain_page(page);
 
@@ -1753,7 +1754,8 @@ static int intel_iommu_map_page(
     spin_unlock(&hd->mapping_lock);
     unmap_vtd_domain_page(page);
 
-    __intel_iommu_iotlb_flush(d, gfn, dma_pte_present(old), 1);
+    if ( !this_cpu(iommu_dont_flush_iotlb) )
+        __intel_iommu_iotlb_flush(d, gfn, dma_pte_present(old), 1);
 
     return 0;
 }
xen/include/xen/iommu.h +12 -0
index a1034df..6f7fbf7 100644
@@ -160,4 +160,16 @@ int iommu_do_domctl(struct xen_domctl *, XEN_GUEST_HANDLE(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);
 
+/*
+ * The purpose of the iommu_dont_flush_iotlb optional cpu flag is to
+ * avoid unecessary iotlb_flush in the low level IOMMU code.
+ *
+ * iommu_map_page/iommu_unmap_page must flush the iotlb but somethimes
+ * this operation can be really expensive. This flag will be set by the
+ * caller to notify the low level IOMMU code to avoid the iotlb flushes.
+ * iommu_iotlb_flush/iommu_iotlb_flush_all will be explicitly called by
+ * the caller.
+ */
+DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
+
 #endif /* _IOMMU_H_ */

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

#17 lore raw

Re: [PATCH 4/7] mm: xenmem_add_to_physmap now takes a pointer on xatp

In reply to Jean Guyader
Quoted 1 line
Quoted 1 line
Quoted 1 line · Jean Guyader

Any reason why this can't be done right away in patch 3?

Quoted 4 lines
---
 xen/arch/x86/mm.c |   40 ++++++++++++++++++++--------------------
 1 files changed, 20 insertions(+), 20 deletions(-)
#8 lore raw

Re: [PATCH 5/7] mm: New XENMEM space, XENMAPSPACE_gmfn_range

In reply to Jean Guyader

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4751

Quoted 6 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
xen/arch/x86/mm.c +1 -0
@@ -4716,6 +4748,17 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
        }
 
        rc = xenmem_add_to_physmap(d, &xatp);
+        if ( rc == -EAGAIN )

if ( rc )

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4757

Quoted 6 lines
xen/arch/x86/mm.c +6 -0
+        {
+            if ( copy_to_guest(arg, &xatp, 1) )
+            {
+                rcu_unlock_domain(d);
+                return -EFAULT;
+            }
        }
        if ( rc == -EAGAIN )

(with some room for further simplification). Without that (or the minimal alternative of copying back just .size or yet some other mechanism), as pointed out before, the caller won't have a way to know how far into the batch things succeeded.

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4763

Quoted 6 lines
xen/arch/x86/mm.c +4 -0
+
+            rc = hypercall_create_continuation(
+                    __HYPERVISOR_memory_op, "ih", op, arg);
+        }
 
        rcu_unlock_domain(d);

Also, the whole block above can be move past this rcu_unlock_domain(), eliminating the need to do it separately in the above error path(s).

xen/arch/x86/x86_64/compat/mm.c int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 68

Quoted 10 lines
xen/arch/x86/x86_64/compat/mm.c +3 -0
@@ -63,6 +63,18 @@ int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
 
        XLAT_add_to_physmap(nat, &cmp);
        rc = arch_memory_op(op, guest_handle_from_ptr(nat, void));
+        if ( rc < 0 )
+            break;
+

With the way the code below is currently this is superfluous. But just as above you will need to provide some indication to the caller *where* the failure occurred.

xen/arch/x86/x86_64/compat/mm.c int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 76

Quoted 8 lines
xen/arch/x86/x86_64/compat/mm.c +8 -0
+        if ( rc == __HYPERVISOR_memory_op )
+        {
+            hypercall_xlat_continuation(NULL, 0x2, nat, arg);
+
+            XLAT_add_to_physmap(&cmp, nat);
+
+            if ( copy_to_guest(arg, &cmp, 1) )
+                return -EFAULT;

I realize that this is the same way in the code handling XENMEM_[gs]et_pod_target, but unfortunately that's wrong (that's why I'm copying you, George): Once a continuation was set up, you mustn't change the return value anymore, since the continuation was established by adjusting the guest's rIP.

As for other memory ops, the continuation can be encoded in "op" (see xen/common/memory.c and xen/common/compat/memory.c). However, while suitable here I don't think that's usable for the PoD variant. The alternative is to cancel the continuation (would require quite a bit of new code I think) or to adjust the low level hypercall handler code (at least the compat mode one) to special case rAX values in the negative errno range, leaving rAX unchanged instead of returning -ENOSYS. Keir?

Jan

Quoted 4 lines
xen/arch/x86/x86_64/compat/mm.c +1 -0
+        }
 
        break;
    }
#18 lore raw

Re: [PATCH 4/7] mm: xenmem_add_to_physmap now takes a pointer on xatp

In reply to Jan Beulich
Quoted 4 lines · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line · Jean Guyader

Any reason why this can't be done right away in patch 3?

I think that moving and changing code at the same time make it headers to read.

The preview patch didn't change anything any code at all, just moved some code around.

Jean

#19 lore raw

Re: [PATCH 4/7] mm: xenmem_add_to_physmap now takes a pointer on xatp

Quoted 9 lines · Jean Guyader
Quoted 4 lines · Jan Beulich
Quoted 1 line
Quoted 1 line
Quoted 1 line · Jean Guyader

Any reason why this can't be done right away in patch 3?

I think that moving and changing code at the same time make it headers to read.

     harder...
Quoted 4 lines

The preview patch didn't change anything any code at all, just moved some code around.

#9 lore raw

Re: [PATCH 5/7] mm: New XENMEM space, XENMAPSPACE_gmfn_range

In reply to Jan Beulich
Quoted 64 lines · Jan Beulich

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4751

Quoted 6 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
(quoted diff) +1 -0
@@ -4716,6 +4748,17 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
        }
 
        rc = xenmem_add_to_physmap(d, &xatp);
+        if ( rc == -EAGAIN )

if ( rc )

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4757

Quoted 6 lines
(quoted diff) +6 -0
+        {
+            if ( copy_to_guest(arg, &xatp, 1) )
+            {
+                rcu_unlock_domain(d);
+                return -EFAULT;
+            }
        }
        if ( rc == -EAGAIN )

(with some room for further simplification). Without that (or the minimal alternative of copying back just .size or yet some other mechanism), as pointed out before, the caller won't have a way to know how far into the batch things succeeded.

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4763

Quoted 6 lines
(quoted diff) +4 -0
+
+            rc = hypercall_create_continuation(
+                    __HYPERVISOR_memory_op, "ih", op, arg);
+        }
 
        rcu_unlock_domain(d);

Also, the whole block above can be move past this rcu_unlock_domain(), eliminating the need to do it separately in the above error path(s).

xen/arch/x86/x86_64/compat/mm.c int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 68

Quoted 10 lines
xen/arch/x86/x86_64/compat/mm.c +3 -0
@@ -63,6 +63,18 @@ int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
 
        XLAT_add_to_physmap(nat, &cmp);
        rc = arch_memory_op(op, guest_handle_from_ptr(nat, void));
+        if ( rc < 0 )
+            break;
+

With the way the code below is currently this is superfluous. But just as above you will need to provide some indication to the caller *where* the failure occurred.

xen/arch/x86/x86_64/compat/mm.c int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 76

Quoted 8 lines
xen/arch/x86/x86_64/compat/mm.c +8 -0
+        if ( rc == __HYPERVISOR_memory_op )
+        {
+            hypercall_xlat_continuation(NULL, 0x2, nat, arg);
+
+            XLAT_add_to_physmap(&cmp, nat);
+
+            if ( copy_to_guest(arg, &cmp, 1) )
+                return -EFAULT;

I realize that this is the same way in the code handling XENMEM_[gs]et_pod_target, but unfortunately that's wrong (that's why I'm copying you, George): Once a continuation was set up, you mustn't change the return value anymore, since the continuation was established by adjusting the guest's rIP.

Quoted 9 lines

As for other memory ops, the continuation can be encoded in "op" (see xen/common/memory.c and xen/common/compat/memory.c). However, while suitable here I don't think that's usable for the PoD variant. The alternative is to cancel the continuation (would require quite a bit of new code I think) or to adjust the low level hypercall handler code (at least the compat mode one) to special case rAX values in the negative errno range, leaving rAX unchanged instead of returning -ENOSYS. Keir?

Jan, did you have something like the attached patch in mind?

We will return EFAULT to the hypercall without touching the guest memory because the copy_to_guest failed.

For the example I ignored the multicalls.

Jean

xen/arch/x86/domain.c +10 -0
index 52c7f37..6b3620b 100644
@@ -1723,6 +1723,16 @@ void sync_vcpu_execstate(struct vcpu *v)
     __arg;                                                                  \
 })
 
+void hypercall_cancel_continuation(void)
+{
+    struct cpu_user_regs *regs = guest_cpu_user_regs();
+
+    if ( !is_hvm_vcpu(current) )
+        regs->eip += 2; /* skip re-execute 'syscall' / 'int $xx' */
+    else
+        current->arch.hvm_vcpu.hcall_preempted = 0;
+}
+
 unsigned long hypercall_create_continuation(
     unsigned int op, const char *format, ...)
 {
xen/arch/x86/x86_64/compat/mm.c +3 -0
index 2c05099..577da6a 100644
@@ -133,7 +133,10 @@ int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
         XLAT_pod_target(&cmp, nat);
 
         if ( copy_to_guest(arg, &cmp, 1) )
+        {
+            hypercall_cancel_continuation();
             rc = -EFAULT;
+        }
 
         break;
     }
xen/include/xen/sched.h +1 -0
index 3ba5495..80d5c4b 100644
@@ -554,6 +554,7 @@ extern void (*dead_idle) (void);
  */
 unsigned long hypercall_create_continuation(
     unsigned int op, const char *format, ...);
+void hypercall_cancel_continuation(void);
 
 #define hypercall_preempt_check() (unlikely(    \
         softirq_pending(smp_processor_id()) |   \

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

#10 lore raw

Re: [PATCH 5/7] mm: New XENMEM space, XENMAPSPACE_gmfn_range

Quoted 78 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 2 lines · Jan Beulich

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void)

Quoted 2 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
(quoted diff) +0 -0
@@ -4716,6 +4748,17 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) 

arg)

Quoted 29 lines

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void) · line 4751

Quoted 4 lines
(quoted diff) +1 -0
        }
 
        rc = xenmem_add_to_physmap(d, &xatp);
+        if ( rc == -EAGAIN )

if ( rc )

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void) · line 4757

Quoted 6 lines
(quoted diff) +6 -0
+        {
+            if ( copy_to_guest(arg, &xatp, 1) )
+            {
+                rcu_unlock_domain(d);
+                return -EFAULT;
+            }
        }
        if ( rc == -EAGAIN )

(with some room for further simplification). Without that (or the minimal alternative of copying back just .size or yet some other mechanism), as pointed out before, the caller won't have a way to know how far into the batch things succeeded.

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void) · line 4763

Quoted 6 lines
(quoted diff) +4 -0
+
+            rc = hypercall_create_continuation(
+                    __HYPERVISOR_memory_op, "ih", op, arg);
+        }
 
        rcu_unlock_domain(d);
Quoted 7 lines

Also, the whole block above can be move past this rcu_unlock_domain(), eliminating the need to do it separately in the above error path(s).

xen/arch/x86/x86_64/compat/mm.c int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void)

Quoted 4 lines
xen/arch/x86/x86_64/compat/mm.c +0 -0
@@ -63,6 +63,18 @@ int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) 

arg)

Quoted 26 lines

xen/arch/x86/x86_64/compat/mm.c int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) · line 67

Quoted 6 lines
xen/arch/x86/x86_64/compat/mm.c +3 -0
        XLAT_add_to_physmap(nat, &cmp);
        rc = arch_memory_op(op, guest_handle_from_ptr(nat, void));
+        if ( rc < 0 )
+            break;
+

With the way the code below is currently this is superfluous. But just as above you will need to provide some indication to the caller *where* the failure occurred.

xen/arch/x86/x86_64/compat/mm.c int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) · line 75

Quoted 8 lines
xen/arch/x86/x86_64/compat/mm.c +8 -0
+        if ( rc == __HYPERVISOR_memory_op )
+        {
+            hypercall_xlat_continuation(NULL, 0x2, nat, arg);
+
+            XLAT_add_to_physmap(&cmp, nat);
+
+            if ( copy_to_guest(arg, &cmp, 1) )
+                return -EFAULT;

I realize that this is the same way in the code handling XENMEM_[gs]et_pod_target, but unfortunately that's wrong (that's why I'm copying you, George): Once a continuation was set up, you mustn't change the return value anymore, since the continuation was established by adjusting the guest's rIP.

Quoted 9 lines

As for other memory ops, the continuation can be encoded in "op" (see xen/common/memory.c and xen/common/compat/memory.c). However, while suitable here I don't think that's usable for the PoD variant. The alternative is to cancel the continuation (would require quite a bit of new code I think) or to adjust the low level hypercall handler code (at least the compat mode one) to special case rAX values in the negative errno range, leaving rAX unchanged instead of returning -ENOSYS. Keir?

Quoted 1 line

Jan, did you have something like the attached patch in mind?

Indeed, and it's fortunately much simpler than I would have thought.

Quoted 4 lines

We will return EFAULT to the hypercall without touching the guest memory because the copy_to_guest failed.

For the example I ignored the multicalls.

Adding the support for that would seem to be strait forward too.

Jan

#11 lore raw

Re: [PATCH 5/7] mm: New XENMEM space, XENMAPSPACE_gmfn_range

In reply to Jan Beulich
Quoted 16 lines · Jan Beulich

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4751

Quoted 6 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
xen/arch/x86/mm.c +1 -0
@@ -4716,6 +4748,17 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
        }
 
        rc = xenmem_add_to_physmap(d, &xatp);
+        if ( rc == -EAGAIN )

if ( rc )

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4757

Quoted 6 lines
xen/arch/x86/mm.c +6 -0
+        {
+            if ( copy_to_guest(arg, &xatp, 1) )
+            {
+                rcu_unlock_domain(d);
+                return -EFAULT;
+            }
Quoted 8 lines
        }
        if ( rc == -EAGAIN )

(with some room for further simplification). Without that (or the minimal alternative of copying back just .size or yet some other mechanism), as pointed out before, the caller won't have a way to know how far into the batch things succeeded.

In xenmem_add_to_physmap I modify xatp in place so when I exit this function xatp will contain the updated value (new start value in .gpfn and .idx, how far do I need to go in .size).

The idea here was to call the copy_to_guest only when we got preempted. If I copy xatp back to the guest I should get the updated value in xatp from the copy_from_guest when I'll be called by the continuation.

Quoted 10 lines

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4763

Quoted 6 lines
xen/arch/x86/mm.c +4 -0
+
+            rc = hypercall_create_continuation(
+                    __HYPERVISOR_memory_op, "ih", op, arg);
+        }
 
        rcu_unlock_domain(d);

Also, the whole block above can be move past this rcu_unlock_domain(), eliminating the need to do it separately in the above error path(s).

Ok.

Jean

Quoted 46 lines

xen/arch/x86/x86_64/compat/mm.c int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 68

Quoted 10 lines
xen/arch/x86/x86_64/compat/mm.c +3 -0
@@ -63,6 +63,18 @@ int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg)
 
        XLAT_add_to_physmap(nat, &cmp);
        rc = arch_memory_op(op, guest_handle_from_ptr(nat, void));
+        if ( rc < 0 )
+            break;
+

With the way the code below is currently this is superfluous. But just as above you will need to provide some indication to the caller *where* the failure occurred.

xen/arch/x86/x86_64/compat/mm.c int compat_arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 76

Quoted 8 lines
xen/arch/x86/x86_64/compat/mm.c +8 -0
+        if ( rc == __HYPERVISOR_memory_op )
+        {
+            hypercall_xlat_continuation(NULL, 0x2, nat, arg);
+
+            XLAT_add_to_physmap(&cmp, nat);
+
+            if ( copy_to_guest(arg, &cmp, 1) )
+                return -EFAULT;

I realize that this is the same way in the code handling XENMEM_[gs]et_pod_target, but unfortunately that's wrong (that's why I'm copying you, George): Once a continuation was set up, you mustn't change the return value anymore, since the continuation was established by adjusting the guest's rIP.

As for other memory ops, the continuation can be encoded in "op" (see xen/common/memory.c and xen/common/compat/memory.c). However, while suitable here I don't think that's usable for the PoD variant. The alternative is to cancel the continuation (would require quite a bit of new code I think) or to adjust the low level hypercall handler code (at least the compat mode one) to special case rAX values in the negative errno range, leaving rAX unchanged instead of returning -ENOSYS. Keir?

Jan

Quoted 4 lines
xen/arch/x86/x86_64/compat/mm.c +1 -0
+        }
 
        break;
    }
#12 lore raw

Re: [PATCH 5/7] mm: New XENMEM space, XENMAPSPACE_gmfn_range

Quoted 28 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 2 lines · Jan Beulich

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void)

Quoted 2 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
(quoted diff) +0 -0
@@ -4716,6 +4748,17 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) 

arg)

Quoted 14 lines

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4751

Quoted 4 lines
xen/arch/x86/mm.c +1 -0
        }
 
        rc = xenmem_add_to_physmap(d, &xatp);
+        if ( rc == -EAGAIN )

if ( rc )

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4757

Quoted 6 lines
xen/arch/x86/mm.c +6 -0
+        {
+            if ( copy_to_guest(arg, &xatp, 1) )
+            {
+                rcu_unlock_domain(d);
+                return -EFAULT;
+            }
Quoted 8 lines
        }
        if ( rc == -EAGAIN )

(with some room for further simplification). Without that (or the minimal alternative of copying back just .size or yet some other mechanism), as pointed out before, the caller won't have a way to know how far into the batch things succeeded.

Quoted 7 lines

In xenmem_add_to_physmap I modify xatp in place so when I exit this function xatp will contain the updated value (new start value in .gpfn and .idx, how far do I need to go in .size).

The idea here was to call the copy_to_guest only when we got preempted. If I copy xatp back to the guest I should get the updated value in xatp from the copy_from_guest when I'll be called by the continuation.

I understand the continuation aspect. But you appear to have not read my comments completely: I'm asking how your caller, in the event of failure, would know how much of the batch was processed successfully.

Jan

#13 lore raw

Re: Re: [PATCH 5/7] mm: New XENMEM space, XENMAPSPACE_gmfn_range

Quoted 32 lines · Jan Beulich
Quoted 32 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 2 lines · Jan Beulich

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void)

Quoted 2 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
(quoted diff) +0 -0
@@ -4716,6 +4748,17 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) 

arg)

Quoted 14 lines

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4751

Quoted 4 lines
xen/arch/x86/mm.c +1 -0
        }
 
        rc = xenmem_add_to_physmap(d, &xatp);
+        if ( rc == -EAGAIN )

if ( rc )

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4757

Quoted 6 lines
xen/arch/x86/mm.c +6 -0
+        {
+            if ( copy_to_guest(arg, &xatp, 1) )
+            {
+                rcu_unlock_domain(d);
+                return -EFAULT;
+            }
Quoted 8 lines
        }
        if ( rc == -EAGAIN )

(with some room for further simplification). Without that (or the minimal alternative of copying back just .size or yet some other mechanism), as pointed out before, the caller won't have a way to know how far into the batch things succeeded.

In xenmem_add_to_physmap I modify xatp in place so when I exit this function xatp will contain the updated value (new start value in .gpfn and .idx, how far do I need to go in .size).

Quoted 7 lines
Quoted 3 lines

The idea here was to call the copy_to_guest only when we got preempted. If I copy xatp back to the guest I should get the updated value in xatp from the copy_from_guest when I'll be called by the continuation.

I understand the continuation aspect. But you appear to have not read my comments completely: I'm asking how your caller, in the event of failure, would know how much of the batch was processed successfully.

For this sort of flush operation can the caller assume that failure means nothing was flushed, since a flush can always be repeated?

Ian.

Quoted 8 lines

Jan

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

#14 lore raw

Re: Re: [PATCH 5/7] mm: New XENMEM space, XENMAPSPACE_gmfn_range

Quoted 38 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 36 lines · Jan Beulich
Quoted 35 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 2 lines · Jan Beulich

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void)

Quoted 2 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
(quoted diff) +0 -0
@@ -4716,6 +4748,17 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) 

arg)

Quoted 14 lines

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4751

Quoted 4 lines
xen/arch/x86/mm.c +1 -0
        }
 
        rc = xenmem_add_to_physmap(d, &xatp);
+        if ( rc == -EAGAIN )

if ( rc )

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4757

Quoted 6 lines
xen/arch/x86/mm.c +6 -0
+        {
+            if ( copy_to_guest(arg, &xatp, 1) )
+            {
+                rcu_unlock_domain(d);
+                return -EFAULT;
+            }
Quoted 8 lines
        }
        if ( rc == -EAGAIN )

(with some room for further simplification). Without that (or the minimal alternative of copying back just .size or yet some other mechanism), as pointed out before, the caller won't have a way to know how far into the batch things succeeded.

In xenmem_add_to_physmap I modify xatp in place so when I exit this function xatp will contain the updated value (new start value in .gpfn and .idx, how far do I need to go in .size).

The idea here was to call the copy_to_guest only when we got preempted. If I copy xatp back to the guest I should get the updated value in xatp from the copy_from_guest when I'll be called by the continuation.

Quoted 6 lines
Quoted 3 lines

I understand the continuation aspect. But you appear to have not read my comments completely: I'm asking how your caller, in the event of failure, would know how much of the batch was processed successfully.

For this sort of flush operation can the caller assume that failure means nothing was flushed, since a flush can always be repeated?

This is not just a flush - instead, the flush is just a necessary sub- operation of what is being done here. I don't think the actual add- to-physmap should be repeated. And even if it can, at least for diagnostic/debugging purposes knowing where things failed is rather useful.

Jan

#15 lore raw

Re: Re: [PATCH 5/7] mm: New XENMEM space, XENMAPSPACE_gmfn_range

Quoted 42 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 39 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 2 lines · Jan Beulich

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void)

Quoted 2 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
(quoted diff) +0 -0
@@ -4716,6 +4748,17 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) 

arg)

Quoted 14 lines

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4751

Quoted 4 lines
xen/arch/x86/mm.c +1 -0
        }
 
        rc = xenmem_add_to_physmap(d, &xatp);
+        if ( rc == -EAGAIN )

if ( rc )

xen/arch/x86/mm.c long arch_memory_op(int op, XEN_GUEST_HANDLE(void) arg) · line 4757

Quoted 6 lines
xen/arch/x86/mm.c +6 -0
+        {
+            if ( copy_to_guest(arg, &xatp, 1) )
+            {
+                rcu_unlock_domain(d);
+                return -EFAULT;
+            }
Quoted 8 lines
        }
        if ( rc == -EAGAIN )

(with some room for further simplification). Without that (or the minimal alternative of copying back just .size or yet some other mechanism), as pointed out before, the caller won't have a way to know how far into the batch things succeeded.

Quoted 7 lines

In xenmem_add_to_physmap I modify xatp in place so when I exit this function xatp will contain the updated value (new start value in .gpfn and .idx, how far do I need to go in .size).

The idea here was to call the copy_to_guest only when we got preempted. If I copy xatp back to the guest I should get the updated value in xatp from the copy_from_guest when I'll be called by the continuation.

I understand the continuation aspect. But you appear to have not read my comments completely: I'm asking how your caller, in the event of failure, would know how much of the batch was processed successfully.

Quoted 9 lines
Quoted 2 lines

For this sort of flush operation can the caller assume that failure means nothing was flushed, since a flush can always be repeated?

This is not just a flush - instead, the flush is just a necessary sub- operation of what is being done here. I don't think the actual add- to-physmap should be repeated. And even if it can, at least for diagnostic/debugging purposes knowing where things failed is rather useful.

Ok, I'll do the copy_to_guest if rc isn't 0. Do you think I should do it for all the spaces or only for gmfn_range?

Jean

#16 lore raw

Re: Re: [PATCH 5/7] mm: New XENMEM space, XENMAPSPACE_gmfn_range

Quoted 54 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 42 lines · Jan Beulich
Quoted 42 lines
Quoted 1 line
Quoted 1 line · Ian Campbell
Quoted 39 lines · Jan Beulich
Quoted 28 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
Quoted 2 lines · Jan Beulich

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void)

Quoted 2 lines
Quoted 1 line
Quoted 1 line · Jean Guyader
(quoted diff) +0 -0
@@ -4716,6 +4748,17 @@ long arch_memory_op(int op, XEN_GUEST_HANDLE(void) 

arg)

Quoted 14 lines

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void) · line 4751

Quoted 4 lines
(quoted diff) +1 -0
        }
 
        rc = xenmem_add_to_physmap(d, &xatp);
+        if ( rc == -EAGAIN )

if ( rc )

Review long arch_memory_op(int op, XEN_GUEST_HANDLE(void) · line 4757

Quoted 6 lines
(quoted diff) +6 -0
+        {
+            if ( copy_to_guest(arg, &xatp, 1) )
+            {
+                rcu_unlock_domain(d);
+                return -EFAULT;
+            }
Quoted 8 lines
        }
        if ( rc == -EAGAIN )

(with some room for further simplification). Without that (or the minimal alternative of copying back just .size or yet some other mechanism), as pointed out before, the caller won't have a way to know how far into the batch things succeeded.

Quoted 7 lines

In xenmem_add_to_physmap I modify xatp in place so when I exit this function xatp will contain the updated value (new start value in .gpfn and .idx, how far do I need to go in .size).

The idea here was to call the copy_to_guest only when we got preempted. If I copy xatp back to the guest I should get the updated value in xatp from the copy_from_guest when I'll be called by the continuation.

I understand the continuation aspect. But you appear to have not read my comments completely: I'm asking how your caller, in the event of failure, would know how much of the batch was processed successfully.

Quoted 9 lines
Quoted 2 lines

For this sort of flush operation can the caller assume that failure means nothing was flushed, since a flush can always be repeated?

This is not just a flush - instead, the flush is just a necessary sub- operation of what is being done here. I don't think the actual add- to-physmap should be repeated. And even if it can, at least for diagnostic/debugging purposes knowing where things failed is rather useful.

Quoted 2 lines

Ok, I'll do the copy_to_guest if rc isn't 0. Do you think I should do it for all the spaces or only for gmfn_range?

I'd prefer doing it just for the latter - the others have no outputs, and their input could legitimately (albeit unlikely) live in read-only memory.

Jan