Skip to contentSkip to conversation

iommu / p2m: add a page_order parameter to iommu_map/unmap_page()

PATCH Superseded Paul Durrant 3 participants 6 messages Oct 17, 2018 – Oct 29, 2018

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu / p2m: add a page_order parameter to iommu_map/unmap_page()

The P2M code currently contains many loops to deal with the fact that, while it may be require to handle page orders greater than 4k, the IOMMU map and unmap functions do not.
This patch adds a page_order parameter to those functions and implements the necessary loops within. This allows the P2M code to be sunstantially simplified.

NOTE: This patch does not modify the underlying vendor IOMMU
implementations to deal with page orders of more than 4k.

---
Cc: Jan Beulich <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Wei Liu <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Julien Grall <[email protected]>
Cc: Konrad Rzeszutek Wilk <[email protected]>
Cc: Stefano Stabellini <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Jun Nakajima <[email protected]>
Cc: Kevin Tian <[email protected]>

---
 xen/arch/x86/mm.c                   |  4 ++-
 xen/arch/x86/mm/p2m-ept.c           | 30 ++---------------
 xen/arch/x86/mm/p2m-pt.c            | 47 ++++++---------------------
 xen/arch/x86/mm/p2m.c               | 49 ++++------------------------
 xen/arch/x86/x86_64/mm.c            |  4 ++-
 xen/common/grant_table.c            |  7 ++--
 xen/drivers/passthrough/iommu.c     | 65 ++++++++++++++++++++++++-------------
 xen/drivers/passthrough/x86/iommu.c |  2 +-
 xen/include/xen/iommu.h             |  8 +++--
 9 files changed, 80 insertions(+), 136 deletions(-)
xen/arch/x86/mm.c +3 -1
index c53bc86a68..f0ae016e7a 100644
@@ -2794,9 +2794,11 @@ static int _get_page_type(struct page_info *page, unsigned long type,
             mfn_t mfn = page_to_mfn(page);
 
             if ( (x & PGT_type_mask) == PGT_writable_page )
-                iommu_ret = iommu_unmap_page(d, _dfn(mfn_x(mfn)));
+                iommu_ret = iommu_unmap_page(d, _dfn(mfn_x(mfn)),
+                                             PAGE_ORDER_4K);
             else if ( type == PGT_writable_page )
                 iommu_ret = iommu_map_page(d, _dfn(mfn_x(mfn)), mfn,
+                                           PAGE_ORDER_4K,
                                            IOMMUF_readable |
                                            IOMMUF_writable);
         }
xen/arch/x86/mm/p2m-ept.c +3 -27
index 407e299e50..656c8dd3ac 100644
@@ -880,33 +880,9 @@ out:
         if ( iommu_use_hap_pt(d) )
             rc = iommu_pte_flush(d, gfn, &ept_entry->epte, order, vtd_pte_present);
         else if ( need_iommu_pt_sync(d) )
-        {
-            dfn_t dfn = _dfn(gfn);
-
-            if ( iommu_flags )
-                for ( i = 0; i < (1 << order); i++ )
-                {
-                    rc = iommu_map_page(d, dfn_add(dfn, i),
-                                        mfn_add(mfn, i), iommu_flags);
-                    if ( unlikely(rc) )
-                    {
-                        while ( i-- )
-                            /* If statement to satisfy __must_check. */
-                            if ( iommu_unmap_page(p2m->domain,
-                                                  dfn_add(dfn, i)) )
-                                continue;
-
-                        break;
-                    }
-                }
-            else
-                for ( i = 0; i < (1 << order); i++ )
-                {
-                    ret = iommu_unmap_page(d, dfn_add(dfn, i));
-                    if ( !rc )
-                        rc = ret;
-                }
-        }
+            rc = iommu_flags ?
+                iommu_map_page(d, _dfn(gfn), mfn, order, iommu_flags) :
+                iommu_unmap_page(d, _dfn(gfn), order);
     }
 
     unmap_domain_page(table);
xen/arch/x86/mm/p2m-pt.c +10 -37

68 lines not on this page · raw message

xen/arch/x86/mm/p2m.c +7 -42

73 lines not on this page · raw message

xen/arch/x86/x86_64/mm.c +3 -1
index 543ea030e3..c0ce5673ba 100644
@@ -1437,13 +1437,15 @@ int memory_add(unsigned long spfn, unsigned long epfn, unsigned int pxm)
     {
         for ( i = spfn; i < epfn; i++ )
             if ( iommu_map_page(hardware_domain, _dfn(i), _mfn(i),
+                                PAGE_ORDER_4K,
                                 IOMMUF_readable | IOMMUF_writable) )
                 break;
         if ( i != epfn )
         {
             while (i-- > old_max)
                 /* If statement to satisfy __must_check. */
-                if ( iommu_unmap_page(hardware_domain, _dfn(i)) )
+                if ( iommu_unmap_page(hardware_domain, _dfn(i),
+                                      PAGE_ORDER_4K) )
                     continue;
 
             goto destroy_m2p;
xen/common/grant_table.c +5 -2
index 878e668bf5..971c6b100c 100644
@@ -1142,12 +1142,14 @@ map_grant_ref(
         {
             if ( !(kind & MAPKIND_WRITE) )
                 err = iommu_map_page(ld, _dfn(mfn_x(mfn)), mfn,
+                                     PAGE_ORDER_4K,
                                      IOMMUF_readable | IOMMUF_writable);
         }
         else if ( act_pin && !old_pin )
         {
             if ( !kind )
                 err = iommu_map_page(ld, _dfn(mfn_x(mfn)), mfn,
+                                     PAGE_ORDER_4K,
                                      IOMMUF_readable);
         }
         if ( err )
@@ -1396,10 +1398,11 @@ unmap_common(
 
         kind = mapkind(lgt, rd, op->mfn);
         if ( !kind )
-            err = iommu_unmap_page(ld, _dfn(mfn_x(op->mfn)));
+            err = iommu_unmap_page(ld, _dfn(mfn_x(op->mfn)),
+                                   PAGE_ORDER_4K);
         else if ( !(kind & MAPKIND_WRITE) )
             err = iommu_map_page(ld, _dfn(mfn_x(op->mfn)), op->mfn,
-                                 IOMMUF_readable);
+                                 PAGE_ORDER_4K, IOMMUF_readable);
 
         double_gt_unlock(lgt, rgt);
 
xen/drivers/passthrough/iommu.c +43 -22

93 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +1 -1
index b20bad17de..4fd3eb1dca 100644
@@ -239,7 +239,7 @@ void __hwdom_init arch_iommu_hwdom_init(struct domain *d)
         if ( paging_mode_translate(d) )
             rc = set_identity_p2m_entry(d, pfn, p2m_access_rw, 0);
         else
-            rc = iommu_map_page(d, _dfn(pfn), _mfn(pfn),
+            rc = iommu_map_page(d, _dfn(pfn), _mfn(pfn), PAGE_ORDER_4K,
                                 IOMMUF_readable | IOMMUF_writable);
         if ( rc )
             printk(XENLOG_WARNING " d%d: IOMMU mapping failed: %d\n",
xen/include/xen/iommu.h +5 -3
index c75333c077..a003d82204 100644
@@ -89,9 +89,11 @@ void iommu_teardown(struct domain *d);
 #define IOMMUF_readable  (1u<<_IOMMUF_readable)
 #define _IOMMUF_writable 1
 #define IOMMUF_writable  (1u<<_IOMMUF_writable)
-int __must_check iommu_map_page(struct domain *d, dfn_t dfn,
-                                mfn_t mfn, unsigned int flags);
-int __must_check iommu_unmap_page(struct domain *d, dfn_t dfn);
+int __must_check iommu_map_page(struct domain *d, dfn_t dfn, mfn_t mfn,
+                                unsigned int page_order,
+                                unsigned int flags);
+int __must_check iommu_unmap_page(struct domain *d, dfn_t dfn,
+                                  unsigned int page_order);
 int __must_check iommu_lookup_page(struct domain *d, dfn_t dfn, mfn_t *mfn,
                                    unsigned int *flags);
 
Signature · 7 lines
2.11.0


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
#1 lore raw
Quoted 40 lines · Paul Durrant
xen/arch/x86/mm/p2m-pt.c +0 -32
index 55df18501e..b264a97bd8 100644
@@ -683,41 +684,13 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn,
     {
         ASSERT(rc == 0);
 
-        if ( iommu_use_hap_pt(p2m->domain) )
-        {
-            if ( iommu_old_flags )
-                amd_iommu_flush_pages(p2m->domain, gfn, page_order);
-        }
-        else if ( need_iommu_pt_sync(p2m->domain) )
-        {
-            dfn_t dfn = _dfn(gfn);
-
-            if ( iommu_pte_flags )
-                for ( i = 0; i < (1UL << page_order); i++ )
-                {
-                    rc = iommu_map_page(p2m->domain, dfn_add(dfn, i),
-                                        mfn_add(mfn, i), iommu_pte_flags);
-                    if ( unlikely(rc) )
-                    {
-                        while ( i-- )
-                            /* If statement to satisfy __must_check. */
-                            if ( iommu_unmap_page(p2m->domain,
-                                                  dfn_add(dfn, i)) )
-                                continue;
-
-                        break;
-                    }
-                }
-            else
-                for ( i = 0; i < (1UL << page_order); i++ )
-                {
-                    int ret = iommu_unmap_page(p2m->domain,
-                                               dfn_add(dfn, i));
-
-                    if ( !rc )

xen/arch/x86/mm/p2m-pt.c p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn, · line 693

Quoted 10 lines
xen/arch/x86/mm/p2m-pt.c +7 -3
-                        rc = ret;
-                }
-        }
+        if ( need_iommu_pt_sync(p2m->domain) )
+            rc = iommu_pte_flags ?
+                iommu_map_page(d, _dfn(gfn), mfn, page_order,
+                               iommu_pte_flags) :
+                iommu_unmap_page(d, _dfn(gfn), page_order);
+        else if ( iommu_use_hap_pt(d) && iommu_old_flags )
+            amd_iommu_flush_pages(p2m->domain, gfn, page_order);

This logically reverses the iommu_use_hap_pt(d)/need_iommu_pt_sync(p2m->domain) conditions.

I'd be tempted confine this change to the else if ( need_iommu_pt_sync(p2m->domain) ) block.

Tangentially related, calling amd_iommu_flush_pages() is a laying violation here because this is supposedly common code.  In reality, it is the NPT code, so might perhaps be better named as p2m-npt.c.  George?

Quoted 21 lines
xen/arch/x86/mm/p2m-pt.c +0 -0
     }
 
     /*
xen/arch/x86/mm/p2m.c +0 -10
index f1df1debc7..3fa559da01 100644
@@ -718,24 +718,8 @@ p2m_remove_page(struct p2m_domain *p2m, unsigned long gfn_l, unsigned long mfn,
     p2m_access_t a;
 
     if ( !paging_mode_translate(p2m->domain) )
-    {
-        int rc = 0;
-
-        if ( need_iommu_pt_sync(p2m->domain) )
-        {
-            dfn_t dfn = _dfn(mfn);
-
-            for ( i = 0; i < (1 << page_order); i++ )
-            {
-                int ret = iommu_unmap_page(p2m->domain, dfn_add(dfn, i));

xen/arch/x86/mm/p2m.c p2m_remove_page(struct p2m_domain *p2m, unsigned long gfn_l, unsigned long mfn, · line 722

Quoted 10 lines
xen/arch/x86/mm/p2m.c +2 -8
-
-                if ( !rc )
-                    rc = ret;
-            }
-        }
-
-        return rc;
-    }
+        return need_iommu_pt_sync(p2m->domain) ?
+            iommu_unmap_page(p2m->domain, _dfn(mfn), page_order) : 0;

TBH, I think this is harder to read than the non ternary alternative.

Quoted 28 lines
xen/arch/x86/mm/p2m.c +0 -0
     ASSERT(gfn_locked_by_me(p2m, gfn));
     P2M_DEBUG("removing gfn=%#lx mfn=%#lx\n", gfn_l, mfn);
xen/drivers/passthrough/iommu.c +3 -7
index 8b438ae4bc..40db9e7849 100644
@@ -305,50 +305,71 @@ void iommu_domain_destroy(struct domain *d)
 }
 
 int iommu_map_page(struct domain *d, dfn_t dfn, mfn_t mfn,
-                   unsigned int flags)
+                   unsigned int page_order, unsigned int flags)
 {
     const struct domain_iommu *hd = dom_iommu(d);
-    int rc;
+    unsigned long i;
 
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    rc = hd->platform_ops->map_page(d, dfn, mfn, flags);
-    if ( unlikely(rc) )
+    for ( i = 0; i < (1ul << page_order); i++ )
     {
-        if ( !d->is_shutting_down && printk_ratelimit() )
-            printk(XENLOG_ERR
-                   "d%d: IOMMU mapping dfn %"PRI_dfn" to mfn %"PRI_mfn" failed: %d\n",

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 324

Quoted 10 lines
xen/drivers/passthrough/iommu.c +6 -3
-                   d->domain_id, dfn_x(dfn), mfn_x(mfn), rc);
+        int ignored, rc = hd->platform_ops->map_page(d, dfn_add(dfn, i),
+                                                     mfn_add(mfn, i),
+                                                     flags);
 
-        if ( !is_hardware_domain(d) )
-            domain_crash(d);
+        if ( unlikely(rc) )
+        {
+            while (i--)

Spaces, but you're also off-by-one when cleaning up i = 0.  Wouldn't it be easier to reuse iommu_unmap_page() rather than opencode it?

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#2 lore raw
Quoted 61 lines

-----Original Message-----
From: Andrew Cooper
Sent: 17 October 2018 12:20
To: Paul Durrant <[email protected]>; [email protected]
Cc: Jan Beulich <[email protected]>; Wei Liu <[email protected]>; George Dunlap <[email protected]>; Ian Jackson <[email protected]>; Julien Grall <[email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Stefano Stabellini <[email protected]>; Tim (Xen.org) <[email protected]>; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>
Subject: Re: [PATCH] iommu / p2m: add a page_order parameter to iommu_map/unmap_page()

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

Quoted 5 lines · Paul Durrant
xen/arch/x86/mm/p2m-pt.c +0 -0
index 55df18501e..b264a97bd8 100644
@@ -683,41 +684,13 @@ p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t

gfn_, mfn_t mfn,

Quoted 7 lines
xen/arch/x86/mm/p2m-pt.c +0 -4
     {
         ASSERT(rc == 0);
 
-        if ( iommu_use_hap_pt(p2m->domain) )
-        {
-            if ( iommu_old_flags )
-                amd_iommu_flush_pages(p2m->domain, gfn, page_order);

xen/arch/x86/mm/p2m-pt.c p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn,

Quoted 10 lines
xen/arch/x86/mm/p2m-pt.c +0 -10
-        }
-        else if ( need_iommu_pt_sync(p2m->domain) )
-        {
-            dfn_t dfn = _dfn(gfn);
-
-            if ( iommu_pte_flags )
-                for ( i = 0; i < (1UL << page_order); i++ )
-                {
-                    rc = iommu_map_page(p2m->domain, dfn_add(dfn, i),
-                                        mfn_add(mfn, i),

iommu_pte_flags);

Quoted 23 lines
xen/arch/x86/mm/p2m-pt.c +2 -21
-                    if ( unlikely(rc) )
-                    {
-                        while ( i-- )
-                            /* If statement to satisfy __must_check. */
-                            if ( iommu_unmap_page(p2m->domain,
-                                                  dfn_add(dfn, i)) )
-                                continue;
-
-                        break;
-                    }
-                }
-            else
-                for ( i = 0; i < (1UL << page_order); i++ )
-                {
-                    int ret = iommu_unmap_page(p2m->domain,
-                                               dfn_add(dfn, i));
-
-                    if ( !rc )
-                        rc = ret;
-                }
-        }
+        if ( need_iommu_pt_sync(p2m->domain) )
+            rc = iommu_pte_flags ?
Quoted 8 lines

xen/arch/x86/mm/p2m-pt.c p2m_pt_set_entry(struct p2m_domain *p2m, gfn_t gfn_, mfn_t mfn, · line 693

Quoted 5 lines
xen/arch/x86/mm/p2m-pt.c +5 -0
+                iommu_map_page(d, _dfn(gfn), mfn, page_order,
+                               iommu_pte_flags) :
+                iommu_unmap_page(d, _dfn(gfn), page_order);
+        else if ( iommu_use_hap_pt(d) && iommu_old_flags )
+            amd_iommu_flush_pages(p2m->domain, gfn, page_order);

This logically reverses the iommu_use_hap_pt(d)/need_iommu_pt_sync(p2m->domain) conditions.

Yes it does, but I think this I ok as they will never both be true at the same time. Doing it this way allowed me to get rid of the nested if.

Quoted 9 lines

I'd be tempted confine this change to the else if ( need_iommu_pt_sync(p2m->domain) ) block.

Tangentially related, calling amd_iommu_flush_pages() is a laying violation here because this is supposedly common code.  In reality, it is the NPT code, so might perhaps be better named as p2m-npt.c.  George?

The boilerplate says:

/******************************************************************************
* arch/x86/mm/p2m-pt.c
*
* Implementation of p2m datastructures as pagetables, for use by
* NPT and shadow-pagetable code
*

so calling AMD IOMMU functions is not really a layering violation.

Quoted 23 lines

xen/arch/x86/mm/p2m.c p2m_remove_page(struct p2m_domain *p2m, unsigned

Quoted 8 lines
xen/arch/x86/mm/p2m-pt.c +0 -0
     }
 
     /*
xen/arch/x86/mm/p2m.c +0 -0
index f1df1debc7..3fa559da01 100644
@@ -718,24 +718,8 @@ p2m_remove_page(struct p2m_domain *p2m, unsigned

long gfn_l, unsigned long mfn,

xen/arch/x86/mm/p2m.c p2m_remove_page(struct p2m_domain *p2m, unsigned long gfn_l, unsigned long mfn,

Quoted 13 lines
xen/arch/x86/mm/p2m.c +0 -10
     p2m_access_t a;
 
     if ( !paging_mode_translate(p2m->domain) )
-    {
-        int rc = 0;
-
-        if ( need_iommu_pt_sync(p2m->domain) )
-        {
-            dfn_t dfn = _dfn(mfn);
-
-            for ( i = 0; i < (1 << page_order); i++ )
-            {
-                int ret = iommu_unmap_page(p2m->domain, dfn_add(dfn,

i));

Quoted 13 lines

xen/arch/x86/mm/p2m.c p2m_remove_page(struct p2m_domain *p2m, unsigned long gfn_l, unsigned long mfn, · line 722

Quoted 10 lines
xen/arch/x86/mm/p2m.c +2 -8
-
-                if ( !rc )
-                    rc = ret;
-            }
-        }
-
-        return rc;
-    }
+        return need_iommu_pt_sync(p2m->domain) ?
+            iommu_unmap_page(p2m->domain, _dfn(mfn), page_order) : 0;

TBH, I think this is harder to read than the non ternary alternative.

Ok. I'm not fussed either way.

Quoted 36 lines

Review

Quoted 4 lines
xen/arch/x86/mm/p2m.c +0 -0
     ASSERT(gfn_locked_by_me(p2m, gfn));
     P2M_DEBUG("removing gfn=%#lx mfn=%#lx\n", gfn_l, mfn);
(quoted diff) +0 -0

b/xen/drivers/passthrough/iommu.c

Quoted 14 lines

index 8b438ae4bc..40db9e7849 100644

xen/drivers/passthrough/iommu.c +2 -2
@@ -305,50 +305,71 @@ void iommu_domain_destroy(struct domain *d)
 }
 
 int iommu_map_page(struct domain *d, dfn_t dfn, mfn_t mfn,
-                   unsigned int flags)
+                   unsigned int page_order, unsigned int flags)
 {
     const struct domain_iommu *hd = dom_iommu(d);
-    int rc;
+    unsigned long i;
 

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 321

Quoted 10 lines
xen/drivers/passthrough/iommu.c +1 -5
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    rc = hd->platform_ops->map_page(d, dfn, mfn, flags);
-    if ( unlikely(rc) )
+    for ( i = 0; i < (1ul << page_order); i++ )
     {
-        if ( !d->is_shutting_down && printk_ratelimit() )
-            printk(XENLOG_ERR
-                   "d%d: IOMMU mapping dfn %"PRI_dfn" to mfn %"PRI_mfn"

failed: %d\n",

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 318

Quoted 2 lines
xen/drivers/passthrough/iommu.c +1 -1
-                   d->domain_id, dfn_x(dfn), mfn_x(mfn), rc);
+        int ignored, rc = hd->platform_ops->map_page(d, dfn_add(dfn,

i),

Quoted 3 lines
xen/drivers/passthrough/iommu.c +2 -0
+                                                     mfn_add(mfn, i),
+                                                     flags);
 
Quoted 9 lines

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 324

Quoted 5 lines
xen/drivers/passthrough/iommu.c +3 -2
-        if ( !is_hardware_domain(d) )
-            domain_crash(d);
+        if ( unlikely(rc) )
+        {
+            while (i--)

Spaces, but you're also off-by-one when cleaning up i = 0.  Wouldn't it be easier to reuse iommu_unmap_page() rather than opencode it?

I don't think this is off by one. When unmap_page() is called then i will have been decremented, so the last iteration of the loop will call unmap_page() with the base dfn. Calling iommu_unmap_page() comes with baggage I'd rather avoid, such as the possibility of the domain crash occurring there instead. I'll fix the indent.

Paul

Quoted 1 line

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#3 lore raw
In reply to Paul Durrant
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 17.10.18 at 10:19, <[email protected]> wrote:
xen/arch/x86/mm/p2m.c +0 -14
@@ -781,28 +765,9 @@ guest_physmap_add_entry(struct domain *d, gfn_t gfn, mfn_t mfn,
     int rc = 0;
 
     if ( !paging_mode_translate(d) )
-    {
-        if ( need_iommu_pt_sync(d) && t == p2m_ram_rw )
-        {
-            dfn_t dfn = _dfn(mfn_x(mfn));
-
-            for ( i = 0; i < (1 << page_order); i++ )
-            {
-                rc = iommu_map_page(d, dfn_add(dfn, i), mfn_add(mfn, i),
-                                    IOMMUF_readable|IOMMUF_writable);
-                if ( rc != 0 )
-                {
-                    while ( i-- > 0 )
-                        /* If statement to satisfy __must_check. */
-                        if ( iommu_unmap_page(d, dfn_add(dfn, i)) )

xen/arch/x86/mm/p2m.c guest_physmap_add_entry(struct domain *d, gfn_t gfn, mfn_t mfn, · line 769

Quoted 10 lines
xen/arch/x86/mm/p2m.c +2 -8
-                            continue;
-
-                    return rc;
-                }
-            }
-        }
-        return 0;
-    }
+        return (need_iommu_pt_sync(d) && t == p2m_ram_rw) ?
+            iommu_map_page(d, _dfn(mfn_x(mfn)), mfn, PAGE_ORDER_4K,

page_order?

Quoted 9 lines
xen/drivers/passthrough/iommu.c +1 -1
@@ -305,50 +305,71 @@ void iommu_domain_destroy(struct domain *d)
 }
 
 int iommu_map_page(struct domain *d, dfn_t dfn, mfn_t mfn,
-                   unsigned int flags)
+                   unsigned int page_order, unsigned int flags)
 {

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 316

Quoted 10 lines
xen/drivers/passthrough/iommu.c +2 -3
     const struct domain_iommu *hd = dom_iommu(d);
-    int rc;
+    unsigned long i;
 
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    rc = hd->platform_ops->map_page(d, dfn, mfn, flags);
-    if ( unlikely(rc) )
+    for ( i = 0; i < (1ul << page_order); i++ )

Above here, can you please check (or assert) that the low bits of dfn and mfn satisfy page_order?

Quoted 7 lines
xen/drivers/passthrough/iommu.c +2 -4
     {
-        if ( !d->is_shutting_down && printk_ratelimit() )
-            printk(XENLOG_ERR
-                   "d%d: IOMMU mapping dfn %"PRI_dfn" to mfn %"PRI_mfn" failed: %d\n",
-                   d->domain_id, dfn_x(dfn), mfn_x(mfn), rc);
+        int ignored, rc = hd->platform_ops->map_page(d, dfn_add(dfn, i),
+                                                     mfn_add(mfn, i),

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 327

Quoted 10 lines
xen/drivers/passthrough/iommu.c +7 -2
+                                                     flags);
 
-        if ( !is_hardware_domain(d) )
-            domain_crash(d);
+        if ( unlikely(rc) )
+        {
+            while (i--)
+            {
+                /* assign to something to avoid compiler warning */
+                ignored = hd->platform_ops->unmap_page(d, dfn_add(dfn, i));

I have to admit that I'd prefer the original "if() continue;" approach.

Quoted 22 lines
xen/drivers/passthrough/iommu.c +15 -2
+            }
+
+            if ( !d->is_shutting_down && printk_ratelimit() )
+                printk(XENLOG_ERR
+                       "d%d: IOMMU order %u mapping dfn %"PRI_dfn" to mfn %"PRI_mfn" failed: %d\n",
+                       d->domain_id, page_order, dfn_x(dfn), mfn_x(mfn),
+                       rc);
+
+            if ( !is_hardware_domain(d) )
+                domain_crash(d);
+
+            return rc;
+        }
     }
 
-    return rc;
+    return 0;
 }
 
-int iommu_unmap_page(struct domain *d, dfn_t dfn)
+int iommu_unmap_page(struct domain *d, dfn_t dfn, unsigned int page_order)
 {

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 354

Quoted 10 lines
xen/drivers/passthrough/iommu.c +2 -3
     const struct domain_iommu *hd = dom_iommu(d);
-    int rc;
+    unsigned long i;
 
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    rc = hd->platform_ops->unmap_page(d, dfn);
-    if ( unlikely(rc) )
+    for ( i = 0; i < (1ul << page_order); i++ )

Check/assert above here again please.

Quoted 10 lines
xen/drivers/passthrough/iommu.c +2 -6
     {
-        if ( !d->is_shutting_down && printk_ratelimit() )
-            printk(XENLOG_ERR
-                   "d%d: IOMMU unmapping dfn %"PRI_dfn" failed: %d\n",
-                   d->domain_id, dfn_x(dfn), rc);
+        int rc = hd->platform_ops->unmap_page(d, dfn_add(dfn, i));
 
-        if ( !is_hardware_domain(d) )
-            domain_crash(d);
+        if ( unlikely(rc) )

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 368

Quoted 10 lines
xen/drivers/passthrough/iommu.c +10 -0
+        {
+            if ( !d->is_shutting_down && printk_ratelimit() )
+                printk(XENLOG_ERR
+                       "d%d: IOMMU unmapping dfn %"PRI_dfn" failed: %d\n",
+                       d->domain_id, dfn_x(dfn), rc);
+
+            if ( !is_hardware_domain(d) )
+                domain_crash(d);
+
+            return rc;

This is not in line with the code you drop: Unmap should continue the loop, and just record (and then report) the first (if any) error, to be on the safe side if callers assume the unmap won't fail (which can't be excluded despite the __must_check, as the code in iommu_map_page() shows). Perhaps the logging then should also be restricted to just the first case of error, and in any event please with the correct dfn (or with the order also logged).

xen/include/xen/iommu.h void iommu_teardown(struct domain *d); · line 94

Quoted 12 lines
xen/include/xen/iommu.h +3 -3
@@ -89,9 +89,11 @@ void iommu_teardown(struct domain *d);
 #define IOMMUF_readable  (1u<<_IOMMUF_readable)
 #define _IOMMUF_writable 1
 #define IOMMUF_writable  (1u<<_IOMMUF_writable)
-int __must_check iommu_map_page(struct domain *d, dfn_t dfn,
-                                mfn_t mfn, unsigned int flags);
-int __must_check iommu_unmap_page(struct domain *d, dfn_t dfn);
+int __must_check iommu_map_page(struct domain *d, dfn_t dfn, mfn_t mfn,
+                                unsigned int page_order,
+                                unsigned int flags);

Can't these two lines be combined?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#4 lore raw
Quoted 46 lines

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: 26 October 2018 16:46
To: Paul Durrant <[email protected]>
Cc: Julien Grall <[email protected]>; Andrew Cooper <[email protected]>; George Dunlap <[email protected]>; Wei Liu <[email protected]>; Ian Jackson <[email protected]>; Jun Nakajima <[email protected]>; Kevin Tian <[email protected]>; Stefano Stabellini <[email protected]>; xen-devel <xen- [email protected]>; Konrad Rzeszutek Wilk <[email protected]>; Tim (Xen.org) <[email protected]>
Subject: Re: [PATCH] iommu / p2m: add a page_order parameter to iommu_map/unmap_page()

Review guest_physmap_add_entry(struct domain *d, gfn_t

Quoted 2 lines
Quoted 1 line
Quoted 1 line · On 17.10.18 at 10:19, <[email protected]> wrote:
(quoted diff) +0 -0
@@ -781,28 +765,9 @@ guest_physmap_add_entry(struct domain *d, gfn_t

gfn, mfn_t mfn,

xen/arch/x86/mm/p2m.c guest_physmap_add_entry(struct domain *d, gfn_t gfn, mfn_t mfn,

Quoted 11 lines
xen/arch/x86/mm/p2m.c +0 -8
     int rc = 0;
 
     if ( !paging_mode_translate(d) )
-    {
-        if ( need_iommu_pt_sync(d) && t == p2m_ram_rw )
-        {
-            dfn_t dfn = _dfn(mfn_x(mfn));
-
-            for ( i = 0; i < (1 << page_order); i++ )
-            {
-                rc = iommu_map_page(d, dfn_add(dfn, i), mfn_add(mfn,

i),

Quoted 6 lines
xen/arch/x86/mm/p2m.c +0 -6
-                                    IOMMUF_readable|IOMMUF_writable);
-                if ( rc != 0 )
-                {
-                    while ( i-- > 0 )
-                        /* If statement to satisfy __must_check. */
-                        if ( iommu_unmap_page(d, dfn_add(dfn, i)) )

xen/arch/x86/mm/p2m.c guest_physmap_add_entry(struct domain *d, gfn_t gfn, mfn_t mfn, · line 769

Quoted 10 lines
xen/arch/x86/mm/p2m.c +2 -8
-                            continue;
-
-                    return rc;
-                }
-            }
-        }
-        return 0;
-    }
+        return (need_iommu_pt_sync(d) && t == p2m_ram_rw) ?
+            iommu_map_page(d, _dfn(mfn_x(mfn)), mfn, PAGE_ORDER_4K,
Quoted 1 line

page_order?

Oh yes. Good spot.

Quoted 17 lines
Quoted 17 lines
xen/drivers/passthrough/iommu.c +2 -2
@@ -305,50 +305,71 @@ void iommu_domain_destroy(struct domain *d)
 }
 
 int iommu_map_page(struct domain *d, dfn_t dfn, mfn_t mfn,
-                   unsigned int flags)
+                   unsigned int page_order, unsigned int flags)
 {
     const struct domain_iommu *hd = dom_iommu(d);
-    int rc;
+    unsigned long i;
 
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
Quoted 7 lines

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 316

Quoted 3 lines
xen/drivers/passthrough/iommu.c +1 -2
-    rc = hd->platform_ops->map_page(d, dfn, mfn, flags);
-    if ( unlikely(rc) )
+    for ( i = 0; i < (1ul << page_order); i++ )

Above here, can you please check (or assert) that the low bits of dfn and mfn satisfy page_order?

Yes, that would be a good idea.

Quoted 19 lines

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d)

Quoted 4 lines
xen/drivers/passthrough/iommu.c +0 -3
     {
-        if ( !d->is_shutting_down && printk_ratelimit() )
-            printk(XENLOG_ERR
-                   "d%d: IOMMU mapping dfn %"PRI_dfn" to mfn %"PRI_mfn"

failed: %d\n",

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 318

Quoted 2 lines
xen/drivers/passthrough/iommu.c +1 -1
-                   d->domain_id, dfn_x(dfn), mfn_x(mfn), rc);
+        int ignored, rc = hd->platform_ops->map_page(d, dfn_add(dfn,

i),

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d)

Quoted 11 lines
xen/drivers/passthrough/iommu.c +8 -2
+                                                     mfn_add(mfn, i),
+                                                     flags);
 
-        if ( !is_hardware_domain(d) )
-            domain_crash(d);
+        if ( unlikely(rc) )
+        {
+            while (i--)
+            {
+                /* assign to something to avoid compiler warning */
+                ignored = hd->platform_ops->unmap_page(d, dfn_add(dfn,
Quoted 4 lines

i));

I have to admit that I'd prefer the original "if() continue;" approach.

Presumably to reduce nesting... I'll restructure it.

Quoted 29 lines

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d)

Quoted 5 lines
xen/drivers/passthrough/iommu.c +5 -0
+            }
+
+            if ( !d->is_shutting_down && printk_ratelimit() )
+                printk(XENLOG_ERR
+                       "d%d: IOMMU order %u mapping dfn %"PRI_dfn" to

mfn %"PRI_mfn" failed: %d\n",

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 333

Quoted 1 line
xen/drivers/passthrough/iommu.c +1 -0
+                       d->domain_id, page_order, dfn_x(dfn),

mfn_x(mfn),

Quoted 5 lines
xen/drivers/passthrough/iommu.c +5 -0
+                       rc);
+
+            if ( !is_hardware_domain(d) )
+                domain_crash(d);
+

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d)

Quoted 10 lines
xen/drivers/passthrough/iommu.c +4 -2
+            return rc;
+        }
     }
 
-    return rc;
+    return 0;
 }
 
-int iommu_unmap_page(struct domain *d, dfn_t dfn)
+int iommu_unmap_page(struct domain *d, dfn_t dfn, unsigned int

page_order)

Quoted 5 lines
xen/drivers/passthrough/iommu.c +1 -1
 {
     const struct domain_iommu *hd = dom_iommu(d);
-    int rc;
+    unsigned long i;
 
Quoted 9 lines

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 354

Quoted 6 lines
xen/drivers/passthrough/iommu.c +1 -2
     if ( !iommu_enabled || !hd->platform_ops )
         return 0;
 
-    rc = hd->platform_ops->unmap_page(d, dfn);
-    if ( unlikely(rc) )
+    for ( i = 0; i < (1ul << page_order); i++ )

Check/assert above here again please.

Sure.

Quoted 15 lines
Quoted 4 lines
xen/drivers/passthrough/iommu.c +0 -3
     {
-        if ( !d->is_shutting_down && printk_ratelimit() )
-            printk(XENLOG_ERR
-                   "d%d: IOMMU unmapping dfn %"PRI_dfn" failed: %d\n",

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d)

Quoted 10 lines
xen/drivers/passthrough/iommu.c +6 -3
-                   d->domain_id, dfn_x(dfn), rc);
+        int rc = hd->platform_ops->unmap_page(d, dfn_add(dfn, i));
 
-        if ( !is_hardware_domain(d) )
-            domain_crash(d);
+        if ( unlikely(rc) )
+        {
+            if ( !d->is_shutting_down && printk_ratelimit() )
+                printk(XENLOG_ERR
+                       "d%d: IOMMU unmapping dfn %"PRI_dfn" failed:

%d\n",

Quoted 15 lines

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 368

Quoted 6 lines
xen/drivers/passthrough/iommu.c +6 -0
+                       d->domain_id, dfn_x(dfn), rc);
+
+            if ( !is_hardware_domain(d) )
+                domain_crash(d);
+
+            return rc;

This is not in line with the code you drop: Unmap should continue the loop, and just record (and then report) the first (if any) error, to be on the safe side if callers assume the unmap won't fail (which can't be excluded despite the __must_check, as the code in iommu_map_page() shows). Perhaps the logging then should also be restricted to just the first case of error, and in any event please with the correct dfn (or with the order also logged).

Ok. I guess unmapping as much as is possible for the h/w domain is reasonable. Any other domain will of course be crashed, so it would seem better to bail out of the loop early in that case.

Quoted 6 lines
Quoted 6 lines
xen/include/xen/iommu.h +0 -0
@@ -89,9 +89,11 @@ void iommu_teardown(struct domain *d);
 #define IOMMUF_readable  (1u<<_IOMMUF_readable)
 #define _IOMMUF_writable 1
 #define IOMMUF_writable  (1u<<_IOMMUF_writable)
Quoted 9 lines

xen/include/xen/iommu.h void iommu_teardown(struct domain *d); · line 94

Quoted 6 lines
xen/include/xen/iommu.h +3 -3
-int __must_check iommu_map_page(struct domain *d, dfn_t dfn,
-                                mfn_t mfn, unsigned int flags);
-int __must_check iommu_unmap_page(struct domain *d, dfn_t dfn);
+int __must_check iommu_map_page(struct domain *d, dfn_t dfn, mfn_t mfn,
+                                unsigned int page_order,
+                                unsigned int flags);

Can't these two lines be combined?

Looks like it. I'll check.

Paul

Quoted 1 line

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel

#5 lore raw
Quoted 36 lines
Quoted 20 lines
Quoted 1 line · On 26.10.18 at 17:52, <[email protected]> wrote:

From: Jan Beulich [mailto:[email protected]]
Sent: 26 October 2018 16:46

Quoted 5 lines
Quoted 1 line
Quoted 1 line · On 17.10.18 at 10:19, <[email protected]> wrote:
     {
xen/drivers/passthrough/iommu.c +0 -3
-        if ( !d->is_shutting_down && printk_ratelimit() )
-            printk(XENLOG_ERR
-                   "d%d: IOMMU unmapping dfn %"PRI_dfn" failed: %d\n",

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d)

Quoted 10 lines
xen/drivers/passthrough/iommu.c +6 -3
-                   d->domain_id, dfn_x(dfn), rc);
+        int rc = hd->platform_ops->unmap_page(d, dfn_add(dfn, i));
 
-        if ( !is_hardware_domain(d) )
-            domain_crash(d);
+        if ( unlikely(rc) )
+        {
+            if ( !d->is_shutting_down && printk_ratelimit() )
+                printk(XENLOG_ERR
+                       "d%d: IOMMU unmapping dfn %"PRI_dfn" failed:

%d\n",

Quoted 15 lines

xen/drivers/passthrough/iommu.c void iommu_domain_destroy(struct domain *d) · line 368

Quoted 6 lines
xen/drivers/passthrough/iommu.c +6 -0
+                       d->domain_id, dfn_x(dfn), rc);
+
+            if ( !is_hardware_domain(d) )
+                domain_crash(d);
+
+            return rc;

This is not in line with the code you drop: Unmap should continue the loop, and just record (and then report) the first (if any) error, to be on the safe side if callers assume the unmap won't fail (which can't be excluded despite the __must_check, as the code in iommu_map_page() shows). Perhaps the logging then should also be restricted to just the first case of error, and in any event please with the correct dfn (or with the order also logged).

Quoted 3 lines

Ok. I guess unmapping as much as is possible for the h/w domain is reasonable. Any other domain will of course be crashed, so it would seem better to bail out of the loop early in that case.

Fine with me.

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel