Skip to contentSkip to conversation

IOMMU/x86: avoid pages without GFN in page table creation/updating

PATCH In review Jan Beulich 4 participants 8 messages May 7, 2015 – May 13, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 IOMMU/x86: avoid pages without GFN in page table creation/updating

Handing INVALID_GFN to functions like hd->platform_ops->map_page() just can't do any good, and the ioreq server code results in such pages being on the list of ones owned by a guest.

While - as suggested by Tim - we should use get_gfn()/put_gfn() there to eliminate races, we really can't due to holding the domain's page alloc lock. Ultimately arch_iommu_populate_page_table() may need to be switched to be GFN based. Here is what Tim said in this regard:
"Ideally this loop would be iterating over all gfns in the p2m rather
than over all owned MFNs. As long as needs_iommu gets set first, such a loop could safely be paused and restarted without worrying about concurrent updates. The code sould even stay in this file, though exposing an iterator from the p2m code would be a lot more efficient."

Original by Andrew Cooper <[email protected]>, using further suggestions from Tim Deegan <[email protected]>.

xen/drivers/passthrough/amd/iommu_map.c +10 -3
@@ -557,6 +557,10 @@ static int update_paging_mode(struct dom
     unsigned long old_root_mfn;
     struct hvm_iommu *hd = domain_hvm_iommu(d);
 
+    if ( gfn == INVALID_MFN )
+        return -EADDRNOTAVAIL;
+    ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+
     level = hd->arch.paging_mode;
     old_root = hd->arch.root_table;
     offset = gfn >> (PTE_PER_TABLE_SHIFT * (level - 1));
@@ -729,12 +733,15 @@ int amd_iommu_unmap_page(struct domain *
      * we might need a deeper page table for lager gfn now */
     if ( is_hvm_domain(d) )
     {
-        if ( update_paging_mode(d, gfn) )
+        int rc = update_paging_mode(d, gfn);
+
+        if ( rc )
         {
             spin_unlock(&hd->arch.mapping_lock);
             AMD_IOMMU_DEBUG("Update page mode failed gfn = %lx\n", gfn);
-            domain_crash(d);
-            return -EFAULT;
+            if ( rc != -EADDRNOTAVAIL )
+                domain_crash(d);
+            return rc;
         }
     }
 
xen/drivers/passthrough/vtd/iommu.h +0 -1
@@ -482,7 +482,6 @@ struct qinval_entry {
 #define VTD_PAGE_TABLE_LEVEL_3  3
 #define VTD_PAGE_TABLE_LEVEL_4  4
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
 #define MAX_IOMMU_REGS 0xc0
 
 extern struct list_head acpi_drhd_units;
xen/drivers/passthrough/x86/iommu.c +11 -4
@@ -59,10 +59,17 @@ int arch_iommu_populate_page_table(struc
         if ( has_hvm_container_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page )
         {
-            BUG_ON(SHARED_M2P(mfn_to_gmfn(d, page_to_mfn(page))));
-            rc = hd->platform_ops->map_page(
-                d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
-                IOMMUF_readable|IOMMUF_writable);
+            unsigned long mfn = page_to_mfn(page);
+            unsigned long gfn = mfn_to_gmfn(d, mfn);
+
+            if ( gfn != INVALID_MFN )
+            {
+                ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+                BUG_ON(SHARED_M2P(gfn));
+                rc = hd->platform_ops->map_page(d, gfn, mfn,
+                                                IOMMUF_readable |
+                                                IOMMUF_writable);
+            }
             if ( rc )
             {
                 page_list_add(page, &d->page_list);
xen/include/asm-x86/hvm/iommu.h +2 -0
@@ -46,6 +46,8 @@ struct g2m_ioport {
     unsigned int np;
 };
 
+#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
+
 struct arch_hvm_iommu
 {
     u64 pgd_maddr;                 /* io page directory machine address */
xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +0 -2
@@ -464,8 +464,6 @@
 #define IOMMU_CONTROL_DISABLED	0
 #define IOMMU_CONTROL_ENABLED	1
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH    48
-
 /* interrupt remapping table */
 #define INT_REMAP_ENTRY_REMAPEN_MASK    0x00000001
 #define INT_REMAP_ENTRY_REMAPEN_SHIFT   0

IOMMU/x86: avoid pages without GFN in page table creation/updating

Handing INVALID_GFN to functions like hd->platform_ops->map_page() just can't do any good, and the ioreq server code results in such pages being on the list of ones owned by a guest.

While - as suggested by Tim - we should use get_gfn()/put_gfn() there to eliminate races, we really can't due to holding the domain's page alloc lock. Ultimately arch_iommu_populate_page_table() may need to be switched to be GFN based. Here is what Tim said in this regard:
"Ideally this loop would be iterating over all gfns in the p2m rather
than over all owned MFNs. As long as needs_iommu gets set first, such a loop could safely be paused and restarted without worrying about concurrent updates. The code sould even stay in this file, though exposing an iterator from the p2m code would be a lot more efficient."

Original by Andrew Cooper <[email protected]>, using further suggestions from Tim Deegan <[email protected]>.

xen/drivers/passthrough/amd/iommu_map.c +10 -3
@@ -557,6 +557,10 @@ static int update_paging_mode(struct dom
     unsigned long old_root_mfn;
     struct hvm_iommu *hd = domain_hvm_iommu(d);
 
+    if ( gfn == INVALID_MFN )
+        return -EADDRNOTAVAIL;
+    ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+
     level = hd->arch.paging_mode;
     old_root = hd->arch.root_table;
     offset = gfn >> (PTE_PER_TABLE_SHIFT * (level - 1));
@@ -729,12 +733,15 @@ int amd_iommu_unmap_page(struct domain *
      * we might need a deeper page table for lager gfn now */
     if ( is_hvm_domain(d) )
     {
-        if ( update_paging_mode(d, gfn) )
+        int rc = update_paging_mode(d, gfn);
+
+        if ( rc )
         {
             spin_unlock(&hd->arch.mapping_lock);
             AMD_IOMMU_DEBUG("Update page mode failed gfn = %lx\n", gfn);
-            domain_crash(d);
-            return -EFAULT;
+            if ( rc != -EADDRNOTAVAIL )
+                domain_crash(d);
+            return rc;
         }
     }
 
xen/drivers/passthrough/vtd/iommu.h +0 -1
@@ -482,7 +482,6 @@ struct qinval_entry {
 #define VTD_PAGE_TABLE_LEVEL_3  3
 #define VTD_PAGE_TABLE_LEVEL_4  4
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
 #define MAX_IOMMU_REGS 0xc0
 
 extern struct list_head acpi_drhd_units;
xen/drivers/passthrough/x86/iommu.c +11 -4
@@ -59,10 +59,17 @@ int arch_iommu_populate_page_table(struc
         if ( has_hvm_container_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page )
         {
-            BUG_ON(SHARED_M2P(mfn_to_gmfn(d, page_to_mfn(page))));
-            rc = hd->platform_ops->map_page(
-                d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
-                IOMMUF_readable|IOMMUF_writable);
+            unsigned long mfn = page_to_mfn(page);
+            unsigned long gfn = mfn_to_gmfn(d, mfn);
+
+            if ( gfn != INVALID_MFN )
+            {
+                ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+                BUG_ON(SHARED_M2P(gfn));
+                rc = hd->platform_ops->map_page(d, gfn, mfn,
+                                                IOMMUF_readable |
+                                                IOMMUF_writable);
+            }
             if ( rc )
             {
                 page_list_add(page, &d->page_list);
xen/include/asm-x86/hvm/iommu.h +2 -0
@@ -46,6 +46,8 @@ struct g2m_ioport {
     unsigned int np;
 };
 
+#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
+
 struct arch_hvm_iommu
 {
     u64 pgd_maddr;                 /* io page directory machine address */
xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +0 -2
@@ -464,8 +464,6 @@
 #define IOMMU_CONTROL_DISABLED	0
 #define IOMMU_CONTROL_ENABLED	1
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH    48
-
 /* interrupt remapping table */
 #define INT_REMAP_ENTRY_REMAPEN_MASK    0x00000001
 #define INT_REMAP_ENTRY_REMAPEN_SHIFT   0

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

Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Jan Beulich

Handing INVALID_GFN to functions like hd->platform_ops->map_page() just can't do any good, and the ioreq server code results in such pages being on the list of ones owned by a guest.

While - as suggested by Tim - we should use get_gfn()/put_gfn() there to eliminate races, we really can't due to holding the domain's page alloc lock. Ultimately arch_iommu_populate_page_table() may need to be switched to be GFN based. Here is what Tim said in this regard:
"Ideally this loop would be iterating over all gfns in the p2m rather
than over all owned MFNs. As long as needs_iommu gets set first, such a loop could safely be paused and restarted without worrying about concurrent updates. The code sould even stay in this file, though exposing an iterator from the p2m code would be a lot more efficient."

Quoted 7 lines

Original by Andrew Cooper <[email protected]>, using further suggestions from Tim Deegan <[email protected]>.

further suggestion.

Quoted 15 lines
xen/drivers/passthrough/amd/iommu_map.c +4 -0
@@ -557,6 +557,10 @@ static int update_paging_mode(struct dom
     unsigned long old_root_mfn;
     struct hvm_iommu *hd = domain_hvm_iommu(d);
 
+    if ( gfn == INVALID_MFN )
+        return -EADDRNOTAVAIL;
+    ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+
     level = hd->arch.paging_mode;
     old_root = hd->arch.root_table;
     offset = gfn >> (PTE_PER_TABLE_SHIFT * (level - 1));

xen/drivers/passthrough/amd/iommu_map.c int amd_iommu_unmap_page(struct domain * · line 741

Quoted 10 lines
xen/drivers/passthrough/amd/iommu_map.c +3 -1
@@ -729,12 +733,15 @@ int amd_iommu_unmap_page(struct domain *
      * we might need a deeper page table for lager gfn now */
     if ( is_hvm_domain(d) )
     {
-        if ( update_paging_mode(d, gfn) )
+        int rc = update_paging_mode(d, gfn);
+
+        if ( rc )
         {
             spin_unlock(&hd->arch.mapping_lock);
             AMD_IOMMU_DEBUG("Update page mode failed gfn = %lx\n", gfn);

Given the domain_crash(), this should probably at error priority rather than debug.

~Andrew

Quoted 66 lines
xen/drivers/passthrough/amd/iommu_map.c +3 -2
-            domain_crash(d);
-            return -EFAULT;
+            if ( rc != -EADDRNOTAVAIL )
+                domain_crash(d);
+            return rc;
         }
     }
 
xen/drivers/passthrough/vtd/iommu.h +0 -1
@@ -482,7 +482,6 @@ struct qinval_entry {
 #define VTD_PAGE_TABLE_LEVEL_3  3
 #define VTD_PAGE_TABLE_LEVEL_4  4
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
 #define MAX_IOMMU_REGS 0xc0
 
 extern struct list_head acpi_drhd_units;
xen/drivers/passthrough/x86/iommu.c +11 -4
@@ -59,10 +59,17 @@ int arch_iommu_populate_page_table(struc
         if ( has_hvm_container_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page )
         {
-            BUG_ON(SHARED_M2P(mfn_to_gmfn(d, page_to_mfn(page))));
-            rc = hd->platform_ops->map_page(
-                d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
-                IOMMUF_readable|IOMMUF_writable);
+            unsigned long mfn = page_to_mfn(page);
+            unsigned long gfn = mfn_to_gmfn(d, mfn);
+
+            if ( gfn != INVALID_MFN )
+            {
+                ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+                BUG_ON(SHARED_M2P(gfn));
+                rc = hd->platform_ops->map_page(d, gfn, mfn,
+                                                IOMMUF_readable |
+                                                IOMMUF_writable);
+            }
             if ( rc )
             {
                 page_list_add(page, &d->page_list);
xen/include/asm-x86/hvm/iommu.h +2 -0
@@ -46,6 +46,8 @@ struct g2m_ioport {
     unsigned int np;
 };
 
+#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
+
 struct arch_hvm_iommu
 {
     u64 pgd_maddr;                 /* io page directory machine address */
xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +0 -2
@@ -464,8 +464,6 @@
 #define IOMMU_CONTROL_DISABLED	0
 #define IOMMU_CONTROL_ENABLED	1
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH    48
-
 /* interrupt remapping table */
 #define INT_REMAP_ENTRY_REMAPEN_MASK    0x00000001
 #define INT_REMAP_ENTRY_REMAPEN_SHIFT   0
#2 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 07.05.15 at 15:13, <[email protected]> wrote:

xen/drivers/passthrough/amd/iommu_map.c int amd_iommu_unmap_page(struct domain * · line 741

Quoted 11 lines · Jan Beulich
xen/drivers/passthrough/amd/iommu_map.c +3 -1
@@ -729,12 +733,15 @@ int amd_iommu_unmap_page(struct domain *
      * we might need a deeper page table for lager gfn now */
     if ( is_hvm_domain(d) )
     {
-        if ( update_paging_mode(d, gfn) )
+        int rc = update_paging_mode(d, gfn);
+
+        if ( rc )
         {
             spin_unlock(&hd->arch.mapping_lock);
             AMD_IOMMU_DEBUG("Update page mode failed gfn = %lx\n", gfn);

Given the domain_crash(), this should probably at error priority rather than debug.

Not sure (and it's not debug but info level anyway, but behind an iommu_debug check). Being unrelated to the change here, I think this ought to be a separate change, perhaps by the AMD IOMMU maintainers.

Jan

#3 lore raw
In reply to Jan Beulich

Jan Beulich wrote on 2015-05-07:

Quoted 74 lines

Handing INVALID_GFN to functions like hd->platform_ops->map_page() just can't do any good, and the ioreq server code results in such pages being on the list of ones owned by a guest.

While - as suggested by Tim - we should use get_gfn()/put_gfn() there to eliminate races, we really can't due to holding the domain's page alloc lock. Ultimately arch_iommu_populate_page_table() may need to be switched to be GFN based. Here is what Tim said in this regard:
"Ideally this loop would be iterating over all gfns in the p2m rather than over all owned MFNs. As long as needs_iommu gets set first, such a loop could safely be paused and restarted without worrying about concurrent updates. The code sould even stay in this file, though exposing an iterator from the p2m code would be a lot more efficient."

Original by Andrew Cooper <[email protected]>, using further suggestions from Tim Deegan <[email protected]>.

xen/drivers/passthrough/amd/iommu_map.c +8 -1
@@ -557,6 +557,10 @@ static int update_paging_mode(struct dom
     unsigned long old_root_mfn;
     struct hvm_iommu *hd = domain_hvm_iommu(d);
 
+    if ( gfn == INVALID_MFN )
+        return -EADDRNOTAVAIL;
+    ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+
     level = hd->arch.paging_mode;
     old_root = hd->arch.root_table;
     offset = gfn >> (PTE_PER_TABLE_SHIFT * (level - 1)); @@ -729,12
+733,15 @@ int amd_iommu_unmap_page(struct domain *
      * we might need a deeper page table for lager gfn now */
     if ( is_hvm_domain(d) )
     {
-        if ( update_paging_mode(d, gfn) )
+        int rc = update_paging_mode(d, gfn);
+
+        if ( rc )
         {
             spin_unlock(&hd->arch.mapping_lock);
             AMD_IOMMU_DEBUG("Update page mode failed gfn = %lx\n",

gfn);

xen/drivers/passthrough/amd/iommu_map.c +3 -2
-            domain_crash(d);
-            return -EFAULT;
+            if ( rc != -EADDRNOTAVAIL )
+                domain_crash(d);
+            return rc;
         }
     }
xen/drivers/passthrough/vtd/iommu.h +0 -0
@@ -482,7 +482,6 @@ struct qinval_entry {  #define

VTD_PAGE_TABLE_LEVEL_3 3 #define VTD_PAGE_TABLE_LEVEL_4 4

-#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48 #define
MAX_IOMMU_REGS 0xc0

extern struct list_head acpi_drhd_units;

xen/drivers/passthrough/x86/iommu.c +0 -2
@@ -59,10 +59,17 @@ int arch_iommu_populate_page_table(struc
         if ( has_hvm_container_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) ==
 PGT_writable_page )
         {
-            BUG_ON(SHARED_M2P(mfn_to_gmfn(d,
 page_to_mfn(page))));
-            rc = hd->platform_ops->map_page(

xen/drivers/passthrough/x86/iommu.c int arch_iommu_populate_page_table(struc · line 71

Quoted 10 lines
xen/drivers/passthrough/x86/iommu.c +7 -2
-                d, mfn_to_gmfn(d, page_to_mfn(page)),
 page_to_mfn(page),
-                IOMMUF_readable|IOMMUF_writable);
+            unsigned long mfn = page_to_mfn(page);
+            unsigned long gfn = mfn_to_gmfn(d, mfn);
+
+            if ( gfn != INVALID_MFN )
+            {
+                ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+                BUG_ON(SHARED_M2P(gfn));

It seems ASSERT() is unnecessary. BUG_ON() is enough to cover it.

Quoted 25 lines
xen/drivers/passthrough/x86/iommu.c +2 -0
+                rc = hd->platform_ops->map_page(d, gfn, mfn,
+

IOMMUF_readable |
+
IOMMUF_writable);

xen/drivers/passthrough/x86/iommu.c +1 -0
+            }
             if ( rc )
             {
                 page_list_add(page, &d->page_list);
xen/include/asm-x86/hvm/iommu.h +2 -0
@@ -46,6 +46,8 @@ struct g2m_ioport {
     unsigned int np;
 };
 
+#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
+
 struct arch_hvm_iommu
 {
     u64 pgd_maddr;                 /* io page directory machine

address */

xen/include/asm-x86/hvm/svm/amd-iommu-defs.h line 469

Quoted 8 lines
xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +0 -2
@@ -464,8 +464,6 @@
 #define IOMMU_CONTROL_DISABLED	0
 #define IOMMU_CONTROL_ENABLED	1
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH    48
-
 /* interrupt remapping table */
 #define INT_REMAP_ENTRY_REMAPEN_MASK    0x00000001
 #define INT_REMAP_ENTRY_REMAPEN_SHIFT   0

Best regards,
Yang

#4 lore raw
Quoted 16 lines
Quoted 1 line
Quoted 1 line · On 11.05.15 at 04:53, <[email protected]> wrote:

Jan Beulich wrote on 2015-05-07:

Quoted 14 lines
xen/drivers/passthrough/x86/iommu.c +1 -4
@@ -59,10 +59,17 @@ int arch_iommu_populate_page_table(struc
         if ( has_hvm_container_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) ==
 PGT_writable_page )
         {
-            BUG_ON(SHARED_M2P(mfn_to_gmfn(d,
 page_to_mfn(page))));
-            rc = hd->platform_ops->map_page(
-                d, mfn_to_gmfn(d, page_to_mfn(page)),
 page_to_mfn(page),
-                IOMMUF_readable|IOMMUF_writable);
+            unsigned long mfn = page_to_mfn(page);
Quoted 8 lines

xen/drivers/passthrough/x86/iommu.c int arch_iommu_populate_page_table(struc · line 68

Quoted 6 lines
xen/drivers/passthrough/x86/iommu.c +6 -0
+            unsigned long gfn = mfn_to_gmfn(d, mfn);
+
+            if ( gfn != INVALID_MFN )
+            {
+                ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+                BUG_ON(SHARED_M2P(gfn));

It seems ASSERT() is unnecessary. BUG_ON() is enough to cover it.

The two check completely different things, so I don't see how the BUG_ON() would help with out of bounds, yet also not INVALID_MFN GFNs. Please clarify what you mean here.

Jan

Acked-by
#5 lore raw

Jan Beulich wrote on 2015-05-11:

Quoted 20 lines
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 11.05.15 at 04:53, <[email protected]> wrote:

Jan Beulich wrote on 2015-05-07:

Quoted 9 lines
xen/drivers/passthrough/x86/iommu.c +0 -1
@@ -59,10 +59,17 @@ int arch_iommu_populate_page_table(struc
         if ( has_hvm_container_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) ==
 PGT_writable_page )
         {
-            BUG_ON(SHARED_M2P(mfn_to_gmfn(d, page_to_mfn(page)))); - 
          rc = hd->platform_ops->map_page( -                d,
Quoted 8 lines
Quoted 6 lines

mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page), -
IOMMUF_readable|IOMMUF_writable); + unsigned long mfn =
page_to_mfn(page); + unsigned long gfn = mfn_to_gmfn(d,
mfn); + + if ( gfn != INVALID_MFN ) + { +
ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH)); +
BUG_ON(SHARED_M2P(gfn));

It seems ASSERT() is unnecessary. BUG_ON() is enough to cover it.

Quoted 3 lines

The two check completely different things, so I don't see how the BUG_ON() would help with out of bounds, yet also not INVALID_MFN GFNs. Please clarify what you mean here.

You are right. I misread the code as BUG_ON(!SHARED_M2P(gfn)).

For Intel VT-d part:

Quoted 2 lines

Jan

Best regards,
Yang

#6 lore raw

Ping: [PATCH] IOMMU/x86: avoid pages without GFN in page table creation/updating

In reply to Jan Beulich

Still waiting for AMD IOMMU maintainer feedback...

Thanks, Jan

Quoted 112 lines
Quoted 1 line
Quoted 1 line · On 07.05.15 at 14:59, <[email protected]> wrote:

Handing INVALID_GFN to functions like hd->platform_ops->map_page() just can't do any good, and the ioreq server code results in such pages being on the list of ones owned by a guest.

While - as suggested by Tim - we should use get_gfn()/put_gfn() there to eliminate races, we really can't due to holding the domain's page alloc lock. Ultimately arch_iommu_populate_page_table() may need to be switched to be GFN based. Here is what Tim said in this regard:
"Ideally this loop would be iterating over all gfns in the p2m rather
than over all owned MFNs. As long as needs_iommu gets set first, such a loop could safely be paused and restarted without worrying about concurrent updates. The code sould even stay in this file, though exposing an iterator from the p2m code would be a lot more efficient."

Original by Andrew Cooper <[email protected]>, using further suggestions from Tim Deegan <[email protected]>.

xen/drivers/passthrough/amd/iommu_map.c +10 -3
@@ -557,6 +557,10 @@ static int update_paging_mode(struct dom
     unsigned long old_root_mfn;
     struct hvm_iommu *hd = domain_hvm_iommu(d);
 
+    if ( gfn == INVALID_MFN )
+        return -EADDRNOTAVAIL;
+    ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+
     level = hd->arch.paging_mode;
     old_root = hd->arch.root_table;
     offset = gfn >> (PTE_PER_TABLE_SHIFT * (level - 1));
@@ -729,12 +733,15 @@ int amd_iommu_unmap_page(struct domain *
      * we might need a deeper page table for lager gfn now */
     if ( is_hvm_domain(d) )
     {
-        if ( update_paging_mode(d, gfn) )
+        int rc = update_paging_mode(d, gfn);
+
+        if ( rc )
         {
             spin_unlock(&hd->arch.mapping_lock);
             AMD_IOMMU_DEBUG("Update page mode failed gfn = %lx\n", gfn);
-            domain_crash(d);
-            return -EFAULT;
+            if ( rc != -EADDRNOTAVAIL )
+                domain_crash(d);
+            return rc;
         }
     }
 
xen/drivers/passthrough/vtd/iommu.h +0 -1
@@ -482,7 +482,6 @@ struct qinval_entry {
 #define VTD_PAGE_TABLE_LEVEL_3  3
 #define VTD_PAGE_TABLE_LEVEL_4  4
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
 #define MAX_IOMMU_REGS 0xc0
 
 extern struct list_head acpi_drhd_units;
xen/drivers/passthrough/x86/iommu.c +11 -4
@@ -59,10 +59,17 @@ int arch_iommu_populate_page_table(struc
         if ( has_hvm_container_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page )
         {
-            BUG_ON(SHARED_M2P(mfn_to_gmfn(d, page_to_mfn(page))));
-            rc = hd->platform_ops->map_page(
-                d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
-                IOMMUF_readable|IOMMUF_writable);
+            unsigned long mfn = page_to_mfn(page);
+            unsigned long gfn = mfn_to_gmfn(d, mfn);
+
+            if ( gfn != INVALID_MFN )
+            {
+                ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+                BUG_ON(SHARED_M2P(gfn));
+                rc = hd->platform_ops->map_page(d, gfn, mfn,
+                                                IOMMUF_readable |
+                                                IOMMUF_writable);
+            }
             if ( rc )
             {
                 page_list_add(page, &d->page_list);
xen/include/asm-x86/hvm/iommu.h +2 -0
@@ -46,6 +46,8 @@ struct g2m_ioport {
     unsigned int np;
 };
 
+#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
+
 struct arch_hvm_iommu
 {
     u64 pgd_maddr;                 /* io page directory machine address */
xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +0 -2
@@ -464,8 +464,6 @@
 #define IOMMU_CONTROL_DISABLED	0
 #define IOMMU_CONTROL_ENABLED	1
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH    48
-
 /* interrupt remapping table */
 #define INT_REMAP_ENTRY_REMAPEN_MASK    0x00000001
 #define INT_REMAP_ENTRY_REMAPEN_SHIFT   0
Acked-by on v1 1/1
#7 lore raw
In reply to Jan Beulich

Thanks,

Suravee

Quoted 113 lines · Jan Beulich

Handing INVALID_GFN to functions like hd->platform_ops->map_page() just can't do any good, and the ioreq server code results in such pages being on the list of ones owned by a guest.

While - as suggested by Tim - we should use get_gfn()/put_gfn() there to eliminate races, we really can't due to holding the domain's page alloc lock. Ultimately arch_iommu_populate_page_table() may need to be switched to be GFN based. Here is what Tim said in this regard:
"Ideally this loop would be iterating over all gfns in the p2m rather
than over all owned MFNs. As long as needs_iommu gets set first, such a loop could safely be paused and restarted without worrying about concurrent updates. The code sould even stay in this file, though exposing an iterator from the p2m code would be a lot more efficient."

Original by Andrew Cooper <[email protected]>, using further suggestions from Tim Deegan <[email protected]>.

xen/drivers/passthrough/amd/iommu_map.c +10 -3
@@ -557,6 +557,10 @@ static int update_paging_mode(struct dom
      unsigned long old_root_mfn;
      struct hvm_iommu *hd = domain_hvm_iommu(d);
 
+    if ( gfn == INVALID_MFN )
+        return -EADDRNOTAVAIL;
+    ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+
      level = hd->arch.paging_mode;
      old_root = hd->arch.root_table;
      offset = gfn >> (PTE_PER_TABLE_SHIFT * (level - 1));
@@ -729,12 +733,15 @@ int amd_iommu_unmap_page(struct domain *
       * we might need a deeper page table for lager gfn now */
      if ( is_hvm_domain(d) )
      {
-        if ( update_paging_mode(d, gfn) )
+        int rc = update_paging_mode(d, gfn);
+
+        if ( rc )
          {
              spin_unlock(&hd->arch.mapping_lock);
              AMD_IOMMU_DEBUG("Update page mode failed gfn = %lx\n", gfn);
-            domain_crash(d);
-            return -EFAULT;
+            if ( rc != -EADDRNOTAVAIL )
+                domain_crash(d);
+            return rc;
          }
      }
 
xen/drivers/passthrough/vtd/iommu.h +0 -1
@@ -482,7 +482,6 @@ struct qinval_entry {
  #define VTD_PAGE_TABLE_LEVEL_3  3
  #define VTD_PAGE_TABLE_LEVEL_4  4
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
  #define MAX_IOMMU_REGS 0xc0
 
  extern struct list_head acpi_drhd_units;
xen/drivers/passthrough/x86/iommu.c +11 -4
@@ -59,10 +59,17 @@ int arch_iommu_populate_page_table(struc
          if ( has_hvm_container_domain(d) ||
              (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page )
          {
-            BUG_ON(SHARED_M2P(mfn_to_gmfn(d, page_to_mfn(page))));
-            rc = hd->platform_ops->map_page(
-                d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
-                IOMMUF_readable|IOMMUF_writable);
+            unsigned long mfn = page_to_mfn(page);
+            unsigned long gfn = mfn_to_gmfn(d, mfn);
+
+            if ( gfn != INVALID_MFN )
+            {
+                ASSERT(!(gfn >> DEFAULT_DOMAIN_ADDRESS_WIDTH));
+                BUG_ON(SHARED_M2P(gfn));
+                rc = hd->platform_ops->map_page(d, gfn, mfn,
+                                                IOMMUF_readable |
+                                                IOMMUF_writable);
+            }
              if ( rc )
              {
                  page_list_add(page, &d->page_list);
xen/include/asm-x86/hvm/iommu.h +2 -0
@@ -46,6 +46,8 @@ struct g2m_ioport {
      unsigned int np;
  };
 
+#define DEFAULT_DOMAIN_ADDRESS_WIDTH 48
+
  struct arch_hvm_iommu
  {
      u64 pgd_maddr;                 /* io page directory machine address */
xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +0 -2
@@ -464,8 +464,6 @@
  #define IOMMU_CONTROL_DISABLED	0
  #define IOMMU_CONTROL_ENABLED	1
 
-#define DEFAULT_DOMAIN_ADDRESS_WIDTH    48
-
  /* interrupt remapping table */
  #define INT_REMAP_ENTRY_REMAPEN_MASK    0x00000001
  #define INT_REMAP_ENTRY_REMAPEN_SHIFT   0