Skip to contentSkip to conversation

AMD IOMMU: Defer IO pagetable construction until device assignment

PATCH Wei Wang2 2 participants 3 messages Apr 2, 2008 – Apr 4, 2008 1 message not in the archive

lore
Share & tools
lore

Wei Wang2

author
#1 lore raw
In reply to a message not in the archive
Quoted 3 lines · Keir Fraser

Then the logic is wrong because new p2m entries could be created after construct_p2m_mapping() is completed but before reassign_device() makes the guest's pdev_list non-empty.

Yes, thanks for catching that.

Quoted 3 lines

I think you should leave p2m.c as it is, and then synchronise on creation of the iommu tables from within amd_iommu_map_page(). You already have a mapping_lock in there which might do the trick.

I will update it. Maybe I need a new flag to indicate that I/O page tables has been synchronised..

Wei

Quoted 34 lines

-- Keir

Quoted 27 lines · On 2/4/08 15:56, "Wei Wang2" <[email protected]> wrote:

I just want to make sure that no new p2m entries are written during the copy procedure.
Wei

Quoted 21 lines · Keir Fraser
Quoted 2 lines · On 2/4/08 15:13, "Wei Wang2" <[email protected]> wrote:

During HVM domain creation, I/O page tables are filled by coping p2m entries from p2m table, which is a useless step for non-passthru

domain.

Quoted 1 line

This patch defers I/O page table construction until the moment of

device

Quoted 1 line

assignment. In case that pci devices are never assigned or hot

plugged,

Quoted 1 line

the unnecessary duplication will be avoid.

You can't duplicate p2m_lock() and p2m_unlock(). Why do you need to protect
your new function with that lock anyway?

-- Keir

#2 lore raw
Quoted 5 lines · On 2/4/08 16:25, "Wei Wang2" <[email protected]> wrote:
Quoted 3 lines

I think you should leave p2m.c as it is, and then synchronise on creation of the iommu tables from within amd_iommu_map_page(). You already have a mapping_lock in there which might do the trick.

I will update it. Maybe I need a new flag to indicate that I/O page tables has been synchronised..

Set and checked under the mapping lock. Yes, that would indeed work.

-- Keir

author
#3 lore raw

Retry for comment, Thanks!

During HVM domain creation, I/O page tables are filled by coping p2m entries from p2m table, which is a useless step for non-passthru domain. This patch defers I/O page table construction until the moment of device assignment. In case that pci devices are never assigned or hot plugged, the unnecessary duplication will be avoid.

Wei

Signature · 12 lines
AMD Saxony, Dresden, Germany
Operating System Research Center

Legal Information:
AMD Saxony Limited Liability Company & Co. KG
Sitz (Geschäftsanschrift):
   Wilschdorfer Landstr. 101, 01109 Dresden, Deutschland
Registergericht Dresden: HRA 4896
vertretungsberechtigter Komplementär:
   AMD Saxony LLC (Sitz Wilmington, Delaware, USA)
Geschäftsführer der AMD Saxony LLC:
   Dr. Hans-R. Deppe, Thomas McCoy 
Quoted 7 lines · Keir Fraser
Quoted 3 lines · On 2/4/08 16:25, "Wei Wang2" <[email protected]> wrote:
Quoted 3 lines

I think you should leave p2m.c as it is, and then synchronise on creation of the iommu tables from within amd_iommu_map_page(). You already have a mapping_lock in there which might do the trick.

Quoted 9 lines
Quoted 2 lines

I will update it. Maybe I need a new flag to indicate that I/O page tables has been synchronised..

Set and checked under the mapping lock. Yes, that would indeed work.

-- Keir

xen/drivers/passthrough/amd/iommu_map.c +60 -10
diff -r da35b52c4fd7 xen/drivers/passthrough/amd/iommu_map.c
@@ -388,17 +388,17 @@ int amd_iommu_map_page(struct domain *d,
     unsigned long flags;
     u64 maddr;
     struct hvm_iommu *hd = domain_hvm_iommu(d);
-    int iw, ir;
+    int iw = IOMMU_IO_WRITE_ENABLED;
+    int ir = IOMMU_IO_READ_ENABLED;
 
     BUG_ON( !hd->root_table );
 
+    spin_lock_irqsave(&hd->mapping_lock, flags);
+
+    if ( is_hvm_domain(d) && (!hd->p2m_synchronized) )
+        goto out;
+
     maddr = (u64)mfn << PAGE_SHIFT;
-
-    iw = IOMMU_IO_WRITE_ENABLED;
-    ir = IOMMU_IO_READ_ENABLED;
-
-    spin_lock_irqsave(&hd->mapping_lock, flags);
-
     pte = get_pte_from_page_tables(hd->root_table, hd->paging_mode, gfn);
     if ( pte == NULL )
     {
@@ -409,7 +409,7 @@ int amd_iommu_map_page(struct domain *d,
     }
 
     set_page_table_entry_present((u32 *)pte, maddr, iw, ir);
-
+out:
     spin_unlock_irqrestore(&hd->mapping_lock, flags);
     return 0;
 }
@@ -425,10 +425,16 @@ int amd_iommu_unmap_page(struct domain *
 
     BUG_ON( !hd->root_table );
 
+    spin_lock_irqsave(&hd->mapping_lock, flags);
+
+    if ( is_hvm_domain(d) && (!hd->p2m_synchronized) )
+    {
+        spin_unlock_irqrestore(&hd->mapping_lock, flags);
+        return 0;
+    }
+
     requestor_id = hd->domain_id;
     io_addr = (u64)gfn << PAGE_SHIFT;
-
-    spin_lock_irqsave(&hd->mapping_lock, flags);
 
     pte = get_pte_from_page_tables(hd->root_table, hd->paging_mode, gfn);
     if ( pte == NULL )
@@ -486,3 +492,47 @@ int amd_iommu_reserve_domain_unity_map(
     spin_unlock_irqrestore(&hd->mapping_lock, flags);
     return 0;
 }
+
+int amd_iommu_sync_p2m(struct domain *d)
+{
+    unsigned long mfn, gfn, flags;
+    void *pte;
+    u64 maddr;
+    struct list_head *entry;
+    struct page_info *page;
+    struct hvm_iommu *hd = domain_hvm_iommu(d);
+    int iw = IOMMU_IO_WRITE_ENABLED;
+    int ir = IOMMU_IO_READ_ENABLED;
+
+    spin_lock_irqsave(&hd->mapping_lock, flags);
+
+    if ( hd->p2m_synchronized )
+        goto out;
+
+    for ( entry = d->page_list.next; entry != &d->page_list;
+            entry = entry->next )
+    {
+        page = list_entry(entry, struct page_info, list);
+        mfn = page_to_mfn(page);
+        gfn = get_gpfn_from_mfn(mfn);
+
+        if ( gfn == INVALID_M2P_ENTRY )
+            continue;
+
+        maddr = (u64)mfn << PAGE_SHIFT;
+        pte = get_pte_from_page_tables(hd->root_table, hd->paging_mode, gfn);
+        if ( pte == NULL )
+        {
+            dprintk(XENLOG_ERR,
+                    "AMD IOMMU: Invalid IO pagetable entry gfn = %lx\n", gfn);
+            spin_unlock_irqrestore(&hd->mapping_lock, flags);
+            return -EFAULT;
+        }
+        set_page_table_entry_present((u32 *)pte, maddr, iw, ir);
+    }
+    hd->p2m_synchronized = 1;
+
+out:
+    spin_unlock_irqrestore(&hd->mapping_lock, flags);
+    return 0;
+}
xen/drivers/passthrough/amd/pci_amd_iommu.c +5 -2
diff -r da35b52c4fd7 xen/drivers/passthrough/amd/pci_amd_iommu.c
@@ -553,8 +553,11 @@ int amd_iommu_assign_device(struct domai
 int amd_iommu_assign_device(struct domain *d, u8 bus, u8 devfn)
 {
     int bdf = (bus << 8) | devfn;
-    int req_id;
-    req_id = ivrs_mappings[bdf].dte_requestor_id;
+    int req_id = ivrs_mappings[bdf].dte_requestor_id;
+    struct hvm_iommu *hd = domain_hvm_iommu(d);
+
+    if ( is_hvm_domain(d) && list_empty(&hd->pdev_list) )
+        amd_iommu_sync_p2m(d);
 
     if ( ivrs_mappings[req_id].unity_map_enable )
     {
xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +1 -0
diff -r da35b52c4fd7 xen/include/asm-x86/hvm/svm/amd-iommu-proto.h
@@ -57,6 +57,7 @@ void *amd_iommu_get_vptr_from_page_table
 void *amd_iommu_get_vptr_from_page_table_entry(u32 *entry);
 int amd_iommu_reserve_domain_unity_map(struct domain *domain,
         unsigned long phys_addr, unsigned long size, int iw, int ir);
+int amd_iommu_sync_p2m(struct domain *d);
 
 /* device table functions */
 void amd_iommu_set_dev_table_entry(u32 *dte, u64 root_ptr,
xen/include/xen/hvm/iommu.h +1 -0
diff -r da35b52c4fd7 xen/include/xen/hvm/iommu.h
@@ -48,6 +48,7 @@ struct hvm_iommu {
     int domain_id;
     int paging_mode;
     void *root_table;
+    bool_t p2m_synchronized;
 
     /* iommu_ops */
     struct iommu_ops *platform_ops;

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