Skip to contentSkip to conversation

XSA-77 follow-ups

PATCH Superseded Jan Beulich 6 participants 35 messages Dec 10, 2013 – Jan 7, 2014

lore
Share & tools
lore
#0 lore raw

cover XSA-77 follow-ups

1: IOMMU: make page table population preemptible
2: IOMMU: make page table deallocation preemptible
3: x86/p2m: restrict auditing to debug builds
4: HVM: prevent leaking heap data from hvm_save_one()
5: x86/PV: don't commit debug register values early in arch_set_info_guest()

#1 lore raw

PATCH 1/5 IOMMU: make page table population preemptible

Since this can take an arbitrary amount of time, the rooting domctl as well as all involved code must become aware of this requiring a continuation.

The subject domain's rel_mem_list is being (ab)used for this, in a way similar to and compatible with broken page offlining.

Further, operations get slightly re-ordered in assign_device(): IOMMU page tables now get set up _before_ the first device gets assigned, at once closing a small timing window in which the guest may already see the device but wouldn't be able to access it.

xen/arch/x86/domain.c +6 -0
@@ -1923,6 +1923,12 @@ int domain_relinquish_resources(struct d
         }
 
         d->arch.relmem = RELMEM_xen;
+
+        spin_lock(&d->page_alloc_lock);
+        page_list_splice(&d->arch.relmem_list, &d->page_list);
+        INIT_PAGE_LIST_HEAD(&d->arch.relmem_list);
+        spin_unlock(&d->page_alloc_lock);
+
         /* Fallthrough. Relinquish every page of memory. */
     case RELMEM_xen:
         ret = relinquish_memory(d, &d->xenpage_list, ~0UL);
xen/arch/x86/mm/p2m-pod.c +2 -1
@@ -459,7 +459,8 @@ p2m_pod_offline_or_broken_hit(struct pag
 
 pod_hit:
     lock_page_alloc(p2m);
-    page_list_add_tail(p, &d->arch.relmem_list);
+    /* Insertion must be at list head (see iommu_populate_page_table()). */
+    page_list_add(p, &d->arch.relmem_list);
     unlock_page_alloc(p2m);
     pod_unlock(p2m);
     return 1;
xen/drivers/passthrough/iommu.c +63 -11

125 lines not on this page · raw message

xen/include/xen/sched.h +1 -1
@@ -323,7 +323,7 @@ struct domain
 
 #ifdef HAS_PASSTHROUGH
     /* Does this guest need iommu mappings? */
-    bool_t           need_iommu;
+    s8               need_iommu;
 #endif
     /* is node-affinity automatically computed? */
     bool_t           auto_node_affinity;

IOMMU: make page table population preemptible

Since this can take an arbitrary amount of time, the rooting domctl as well as all involved code must become aware of this requiring a continuation.

The subject domain's rel_mem_list is being (ab)used for this, in a way similar to and compatible with broken page offlining.

Further, operations get slightly re-ordered in assign_device(): IOMMU page tables now get set up _before_ the first device gets assigned, at once closing a small timing window in which the guest may already see the device but wouldn't be able to access it.

xen/arch/x86/domain.c +6 -0
@@ -1923,6 +1923,12 @@ int domain_relinquish_resources(struct d
         }
 
         d->arch.relmem = RELMEM_xen;
+
+        spin_lock(&d->page_alloc_lock);
+        page_list_splice(&d->arch.relmem_list, &d->page_list);
+        INIT_PAGE_LIST_HEAD(&d->arch.relmem_list);
+        spin_unlock(&d->page_alloc_lock);
+
         /* Fallthrough. Relinquish every page of memory. */
     case RELMEM_xen:
         ret = relinquish_memory(d, &d->xenpage_list, ~0UL);
xen/arch/x86/mm/p2m-pod.c +2 -1
@@ -459,7 +459,8 @@ p2m_pod_offline_or_broken_hit(struct pag
 
 pod_hit:
     lock_page_alloc(p2m);
-    page_list_add_tail(p, &d->arch.relmem_list);
+    /* Insertion must be at list head (see iommu_populate_page_table()). */
+    page_list_add(p, &d->arch.relmem_list);
     unlock_page_alloc(p2m);
     pod_unlock(p2m);
     return 1;
xen/drivers/passthrough/iommu.c +63 -11

125 lines not on this page · raw message

xen/include/xen/sched.h +1 -1
@@ -323,7 +323,7 @@ struct domain
 
 #ifdef HAS_PASSTHROUGH
     /* Does this guest need iommu mappings? */
-    bool_t           need_iommu;
+    s8               need_iommu;
 #endif
     /* is node-affinity automatically computed? */
     bool_t           auto_node_affinity;

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

#2 lore raw

Re: [PATCH 1/5] IOMMU: make page table population preemptible

Quoted 118 lines · Jan Beulich

Since this can take an arbitrary amount of time, the rooting domctl as well as all involved code must become aware of this requiring a continuation.

The subject domain's rel_mem_list is being (ab)used for this, in a way similar to and compatible with broken page offlining.

Further, operations get slightly re-ordered in assign_device(): IOMMU page tables now get set up _before_ the first device gets assigned, at once closing a small timing window in which the guest may already see the device but wouldn't be able to access it.

xen/arch/x86/domain.c +6 -0
@@ -1923,6 +1923,12 @@ int domain_relinquish_resources(struct d
         }
 
         d->arch.relmem = RELMEM_xen;
+
+        spin_lock(&d->page_alloc_lock);
+        page_list_splice(&d->arch.relmem_list, &d->page_list);
+        INIT_PAGE_LIST_HEAD(&d->arch.relmem_list);
+        spin_unlock(&d->page_alloc_lock);
+
         /* Fallthrough. Relinquish every page of memory. */
     case RELMEM_xen:
         ret = relinquish_memory(d, &d->xenpage_list, ~0UL);
xen/arch/x86/mm/p2m-pod.c +2 -1
@@ -459,7 +459,8 @@ p2m_pod_offline_or_broken_hit(struct pag
 
 pod_hit:
     lock_page_alloc(p2m);
-    page_list_add_tail(p, &d->arch.relmem_list);
+    /* Insertion must be at list head (see iommu_populate_page_table()). */
+    page_list_add(p, &d->arch.relmem_list);
     unlock_page_alloc(p2m);
     pod_unlock(p2m);
     return 1;
xen/drivers/passthrough/iommu.c +30 -9
@@ -18,6 +18,7 @@
 #include <asm/hvm/iommu.h>
 #include <xen/paging.h>
 #include <xen/guest_access.h>
+#include <xen/event.h>
 #include <xen/softirq.h>
 #include <xen/keyhandler.h>
 #include <xsm/xsm.h>
@@ -265,7 +266,23 @@ static int assign_device(struct domain *
              d->mem_event->paging.ring_page)) )
         return -EXDEV;
 
-    spin_lock(&pcidevs_lock);
+    if ( !spin_trylock(&pcidevs_lock) )
+        return -ERESTART;
+
+    if ( need_iommu(d) <= 0 )
+    {
+        if ( !iommu_use_hap_pt(d) )
+        {
+            rc = iommu_populate_page_table(d);
+            if ( rc )
+            {
+                spin_unlock(&pcidevs_lock);
+                return rc;
+            }
+        }
+        d->need_iommu = 1;
+    }
+
     pdev = pci_get_pdev_by_domain(dom0, seg, bus, devfn);
     if ( !pdev )
     {
@@ -290,15 +307,14 @@ static int assign_device(struct domain *
                    rc);
     }
 
-    if ( has_arch_pdevs(d) && !need_iommu(d) )
+ done:
+    if ( !has_arch_pdevs(d) && need_iommu(d) )
     {
-        d->need_iommu = 1;
-        if ( !iommu_use_hap_pt(d) )
-            rc = iommu_populate_page_table(d);
-        goto done;
+        d->need_iommu = 0;
+        hd->platform_ops->teardown(d);
     }
-done:
     spin_unlock(&pcidevs_lock);
+
     return rc;
 }
 
@@ -306,12 +322,17 @@ static int iommu_populate_page_table(str
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
     struct page_info *page;
-    int rc = 0;
+    int rc = 0, n = 0;
+
+    d->need_iommu = -1;
 
     this_cpu(iommu_dont_flush_iotlb) = 1;
     spin_lock(&d->page_alloc_lock);
 
-    page_list_for_each ( page, &d->page_list )
+    if ( unlikely(d->is_dying) )
+        rc = -ESRCH;
+
+    while ( !rc && (page = page_list_remove_head(&d->page_list)) )
     {
         if ( is_hvm_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page )

xen/drivers/passthrough/iommu.c static int iommu_populate_page_table(str · line 351

Quoted 10 lines
xen/drivers/passthrough/iommu.c +6 -0
@@ -321,7 +342,32 @@ static int iommu_populate_page_table(str
                 d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
                 IOMMUF_readable|IOMMUF_writable);
             if ( rc )
+            {
+                page_list_add(page, &d->page_list);
                 break;
+            }
+        }
+        page_list_add_tail(page, &d->arch.relmem_list);
+        if ( !(++n & 0xff) && !page_list_empty(&d->page_list) &&

Why the forced restart here? If nothing needs pre-empting, surely it is better to continue?

Or is this about equality on the pcidevs_lock ?

Quoted 50 lines
xen/drivers/passthrough/iommu.c +27 -2
+             hypercall_preempt_check() )
+            rc = -ERESTART;
+    }
+
+    if ( !rc )
+    {
+        /*
+         * The expectation here is that generally there are many normal pages
+         * on relmem_list (the ones we put there) and only few being in an
+         * offline/broken state. The latter ones are always at the head of the
+         * list. Hence we first move the whole list, and then move back the
+         * first few entries.
+         */
+        page_list_move(&d->page_list, &d->arch.relmem_list);
+        while ( (page = page_list_first(&d->page_list)) != NULL &&
+                (page->count_info & (PGC_state|PGC_broken)) )
+        {
+            page_list_del(page, &d->page_list);
+            page_list_add_tail(page, &d->arch.relmem_list);
         }
     }
 
@@ -330,8 +376,11 @@ static int iommu_populate_page_table(str
 
     if ( !rc )
         iommu_iotlb_flush_all(d);
-    else
+    else if ( rc != -ERESTART )
+    {
         hd->platform_ops->teardown(d);
+        d->need_iommu = 0;
+    }
 
     return rc;
 }
@@ -688,7 +737,10 @@ int iommu_do_domctl(
 
         ret = device_assigned(seg, bus, devfn) ?:
               assign_device(d, seg, bus, devfn);
-        if ( ret )
+        if ( ret == -ERESTART )
+            ret = hypercall_create_continuation(__HYPERVISOR_domctl,
+                                                "h", u_domctl);
+        else if ( ret )
             printk(XENLOG_G_ERR "XEN_DOMCTL_assign_device: "
                    "assign %04x:%02x:%02x.%u to dom%d failed (%d)\n",
                    seg, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),

xen/include/xen/sched.h struct domain · line 326

Quoted 5 lines
xen/include/xen/sched.h +1 -1
@@ -323,7 +323,7 @@ struct domain
 
 #ifdef HAS_PASSTHROUGH
     /* Does this guest need iommu mappings? */
-    bool_t           need_iommu;
+    s8               need_iommu;

I think this change from bool_t to s8 needs a comment explaining that -1 indicates "the iommu mappings are pending creation"

Is there any particular reason that -ERESTART is used when -EAGAIN is the prevailing style for hypercall continuations?

~Andrew

#3 lore raw

Re: [PATCH 1/5] IOMMU: make page table population preemptible

Quoted 17 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

xen/drivers/passthrough/iommu.c static int iommu_populate_page_table(str · line 351

Quoted 11 lines · Jan Beulich
xen/drivers/passthrough/iommu.c +6 -0
@@ -321,7 +342,32 @@ static int iommu_populate_page_table(str
                 d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
                 IOMMUF_readable|IOMMUF_writable);
             if ( rc )
+            {
+                page_list_add(page, &d->page_list);
                 break;
+            }
+        }
+        page_list_add_tail(page, &d->arch.relmem_list);
+        if ( !(++n & 0xff) && !page_list_empty(&d->page_list) &&

Why the forced restart here? If nothing needs pre-empting, surely it is better to continue?

Quoted 3 lines

Or is this about equality on the pcidevs_lock ?

Quoted 1 line
(quoted diff) +1 -0
+             hypercall_preempt_check() )

Did you overlook this part of the condition?

Quoted 11 lines

xen/include/xen/sched.h struct domain · line 326

Quoted 8 lines
xen/include/xen/sched.h +1 -1
@@ -323,7 +323,7 @@ struct domain
 
 #ifdef HAS_PASSTHROUGH
     /* Does this guest need iommu mappings? */
-    bool_t           need_iommu;
+    s8               need_iommu;

I think this change from bool_t to s8 needs a comment explaining that -1 indicates "the iommu mappings are pending creation"

Will do.

Quoted 2 lines

Is there any particular reason that -ERESTART is used when -EAGAIN is the prevailing style for hypercall continuations?

I meanwhile realized that using -EAGAIN was a mistake (iirc taken from certain domctl-s having passed this back up to the caller to request re-invocation a long time ago) - -EAGAIN really has a different meaning, and hence we ought to switch all its current mis-uses to -ERESTART.

Jan

#4 lore raw

Re: [PATCH 1/5] IOMMU: make page table population preemptible

Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

xen/drivers/passthrough/iommu.c static int iommu_populate_page_table(str · line 351

Quoted 11 lines · Jan Beulich
xen/drivers/passthrough/iommu.c +6 -0
@@ -321,7 +342,32 @@ static int iommu_populate_page_table(str
                 d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
                 IOMMUF_readable|IOMMUF_writable);
             if ( rc )
+            {
+                page_list_add(page, &d->page_list);
                 break;
+            }
+        }
+        page_list_add_tail(page, &d->arch.relmem_list);
+        if ( !(++n & 0xff) && !page_list_empty(&d->page_list) &&

Why the forced restart here? If nothing needs pre-empting, surely it is better to continue?

Quoted 4 lines
Quoted 3 lines

Or is this about equality on the pcidevs_lock ?

Quoted 1 line
(quoted diff) +1 -0
+             hypercall_preempt_check() )

Did you overlook this part of the condition?

No, but I did mentally get the logic inverted when trying to work out what was going on.

How about (++n > 0xff) ?

If we have already spent a while in this loop, and the hypercall_preempt_check() doesn't flip to 1 until a few iterations after n is congruent with 0x100, waiting for another 0x100 iterations before checking again seems a little long.

Quoted 15 lines
Quoted 11 lines

xen/include/xen/sched.h struct domain · line 326

Quoted 9 lines
xen/include/xen/sched.h +1 -1
@@ -323,7 +323,7 @@ struct domain
 
 #ifdef HAS_PASSTHROUGH
     /* Does this guest need iommu mappings? */
-    bool_t           need_iommu;
+    s8               need_iommu;

I think this change from bool_t to s8 needs a comment explaining that -1 indicates "the iommu mappings are pending creation"

Will do.

Quoted 2 lines

Is there any particular reason that -ERESTART is used when -EAGAIN is the prevailing style for hypercall continuations?

Quoted 8 lines

I meanwhile realized that using -EAGAIN was a mistake (iirc taken from certain domctl-s having passed this back up to the caller to request re-invocation a long time ago) - -EAGAIN really has a different meaning, and hence we ought to switch all its current mis-uses to -ERESTART.

Jan

Ok.

~Andrew

#5 lore raw

Re: [PATCH 1/5] IOMMU: make page table population preemptible

Quoted 26 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

xen/drivers/passthrough/iommu.c static int iommu_populate_page_table(str · line 351

Quoted 11 lines · Jan Beulich
xen/drivers/passthrough/iommu.c +6 -0
@@ -321,7 +342,32 @@ static int iommu_populate_page_table(str
                 d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
                 IOMMUF_readable|IOMMUF_writable);
             if ( rc )
+            {
+                page_list_add(page, &d->page_list);
                 break;
+            }
+        }
+        page_list_add_tail(page, &d->arch.relmem_list);
+        if ( !(++n & 0xff) && !page_list_empty(&d->page_list) &&

Why the forced restart here? If nothing needs pre-empting, surely it is better to continue?

Quoted 4 lines
Quoted 3 lines

Or is this about equality on the pcidevs_lock ?

Quoted 1 line
(quoted diff) +1 -0
+             hypercall_preempt_check() )

Did you overlook this part of the condition?

No, but I did mentally get the logic inverted when trying to work out what was going on.

Quoted 6 lines

How about (++n > 0xff) ?

If we have already spent a while in this loop, and the hypercall_preempt_check() doesn't flip to 1 until a few iterations after n is congruent with 0x100, waiting for another 0x100 iterations before checking again seems a little long.

The thing I'm trying to avoid is calling hypercall_preempt_check() overly often - especially when the prior if()'s body doesn't get entered we might otherwise do very little useful for per check.

While 256 isn't overly much (covering merely 1Mb of guest space), I could see two possibilities to get this a little more towards what you want: Either we right shift the mask each time we actually call hypercall_preempt_check(), or we bias the increment (adding e.g. 10 when going the expensive route, else 1).

Jan

#6 lore raw

Re: [PATCH 1/5] IOMMU: make page table population preemptible

Quoted 35 lines · Jan Beulich
Quoted 25 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 16 lines · Jan Beulich
Quoted 16 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

xen/drivers/passthrough/iommu.c static int iommu_populate_page_table(str · line 351

Quoted 11 lines · Jan Beulich
xen/drivers/passthrough/iommu.c +6 -0
@@ -321,7 +342,32 @@ static int iommu_populate_page_table(str
                 d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
                 IOMMUF_readable|IOMMUF_writable);
             if ( rc )
+            {
+                page_list_add(page, &d->page_list);
                 break;
+            }
+        }
+        page_list_add_tail(page, &d->arch.relmem_list);
+        if ( !(++n & 0xff) && !page_list_empty(&d->page_list) &&

Why the forced restart here? If nothing needs pre-empting, surely it is better to continue?

Quoted 4 lines
Quoted 3 lines

Or is this about equality on the pcidevs_lock ?

Quoted 1 line
(quoted diff) +1 -0
+             hypercall_preempt_check() )

Did you overlook this part of the condition?

No, but I did mentally get the logic inverted when trying to work out what was going on.

Quoted 6 lines

How about (++n > 0xff) ?

If we have already spent a while in this loop, and the hypercall_preempt_check() doesn't flip to 1 until a few iterations after n is congruent with 0x100, waiting for another 0x100 iterations before checking again seems a little long.

The thing I'm trying to avoid is calling hypercall_preempt_check() overly often - especially when the prior if()'s body doesn't get entered we might otherwise do very little useful for per check.

Quoted 7 lines

While 256 isn't overly much (covering merely 1Mb of guest space), I could see two possibilities to get this a little more towards what you want: Either we right shift the mask each time we actually call hypercall_preempt_check(), or we bias the increment (adding e.g. 10 when going the expensive route, else 1).

Jan

I suppose it probably doesn't matter too much. In the slim case where he loop does manage to get to 257 iterations before the preempt check returns true, doing another 254 iterations isn't going to skew the timings too much. And the net time till completion will be fractionally shorter.

~Andrew

#7 lore raw

PATCH v2 1/5 IOMMU: make page table population preemptible

In reply to Andrew Cooper

Since this can take an arbitrary amount of time, the rooting domctl as well as all involved code must become aware of this requiring a continuation.

The subject domain's rel_mem_list is being (ab)used for this, in a way similar to and compatible with broken page offlining.

Further, operations get slightly re-ordered in assign_device(): IOMMU page tables now get set up _before_ the first device gets assigned, at once closing a small timing window in which the guest may already see the device but wouldn't be able to access it.

---
v2: Extend comment on struct domain's need_iommu field.

xen/arch/x86/domain.c +6 -0
@@ -1924,6 +1924,12 @@ int domain_relinquish_resources(struct d
         }
 
         d->arch.relmem = RELMEM_xen;
+
+        spin_lock(&d->page_alloc_lock);
+        page_list_splice(&d->arch.relmem_list, &d->page_list);
+        INIT_PAGE_LIST_HEAD(&d->arch.relmem_list);
+        spin_unlock(&d->page_alloc_lock);
+
         /* Fallthrough. Relinquish every page of memory. */
     case RELMEM_xen:
         ret = relinquish_memory(d, &d->xenpage_list, ~0UL);
xen/arch/x86/mm/p2m-pod.c +2 -1
@@ -459,7 +459,8 @@ p2m_pod_offline_or_broken_hit(struct pag
 
 pod_hit:
     lock_page_alloc(p2m);
-    page_list_add_tail(p, &d->arch.relmem_list);
+    /* Insertion must be at list head (see iommu_populate_page_table()). */
+    page_list_add(p, &d->arch.relmem_list);
     unlock_page_alloc(p2m);
     pod_unlock(p2m);
     return 1;
xen/drivers/passthrough/iommu.c +63 -11

125 lines not on this page · raw message

xen/include/xen/sched.h +2 -2
@@ -322,8 +322,8 @@ struct domain
     enum guest_type guest_type;
 
 #ifdef HAS_PASSTHROUGH
-    /* Does this guest need iommu mappings? */
-    bool_t           need_iommu;
+    /* Does this guest need iommu mappings (-1 meaning "being set up")? */
+    s8               need_iommu;
 #endif
     /* is node-affinity automatically computed? */
     bool_t           auto_node_affinity;

IOMMU: make page table population preemptible

Since this can take an arbitrary amount of time, the rooting domctl as well as all involved code must become aware of this requiring a continuation.

The subject domain's rel_mem_list is being (ab)used for this, in a way similar to and compatible with broken page offlining.

Further, operations get slightly re-ordered in assign_device(): IOMMU page tables now get set up _before_ the first device gets assigned, at once closing a small timing window in which the guest may already see the device but wouldn't be able to access it.

---
v2: Extend comment on struct domain's need_iommu field.

xen/arch/x86/domain.c +6 -0
@@ -1924,6 +1924,12 @@ int domain_relinquish_resources(struct d
         }
 
         d->arch.relmem = RELMEM_xen;
+
+        spin_lock(&d->page_alloc_lock);
+        page_list_splice(&d->arch.relmem_list, &d->page_list);
+        INIT_PAGE_LIST_HEAD(&d->arch.relmem_list);
+        spin_unlock(&d->page_alloc_lock);
+
         /* Fallthrough. Relinquish every page of memory. */
     case RELMEM_xen:
         ret = relinquish_memory(d, &d->xenpage_list, ~0UL);
xen/arch/x86/mm/p2m-pod.c +2 -1
@@ -459,7 +459,8 @@ p2m_pod_offline_or_broken_hit(struct pag
 
 pod_hit:
     lock_page_alloc(p2m);
-    page_list_add_tail(p, &d->arch.relmem_list);
+    /* Insertion must be at list head (see iommu_populate_page_table()). */
+    page_list_add(p, &d->arch.relmem_list);
     unlock_page_alloc(p2m);
     pod_unlock(p2m);
     return 1;
xen/drivers/passthrough/iommu.c +63 -11

125 lines not on this page · raw message

xen/include/xen/sched.h +2 -2
@@ -322,8 +322,8 @@ struct domain
     enum guest_type guest_type;
 
 #ifdef HAS_PASSTHROUGH
-    /* Does this guest need iommu mappings? */
-    bool_t           need_iommu;
+    /* Does this guest need iommu mappings (-1 meaning "being set up")? */
+    s8               need_iommu;
 #endif
     /* is node-affinity automatically computed? */
     bool_t           auto_node_affinity;

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

Acked-by on v2 1/5
#8 lore raw

Re: [PATCH v2 1/5] IOMMU: make page table population preemptible

Quoted 7 lines · Jan Beulich

Since this can take an arbitrary amount of time, the rooting domctl as well as all involved code must become aware of this requiring a continuation.

The subject domain's rel_mem_list is being (ab)used for this, in a way similar to and compatible with broken page offlining.

Quoted 6 lines

Further, operations get slightly re-ordered in assign_device(): IOMMU page tables now get set up _before_ the first device gets assigned, at once closing a small timing window in which the guest may already see the device but wouldn't be able to access it.

#9 lore raw

Re: [PATCH v2 1/5] IOMMU: make page table population preemptible

In reply to Jan Beulich
Quoted 79 lines · Jan Beulich

Since this can take an arbitrary amount of time, the rooting domctl as well as all involved code must become aware of this requiring a continuation.

The subject domain's rel_mem_list is being (ab)used for this, in a way similar to and compatible with broken page offlining.

Further, operations get slightly re-ordered in assign_device(): IOMMU page tables now get set up _before_ the first device gets assigned, at once closing a small timing window in which the guest may already see the device but wouldn't be able to access it.

---
v2: Extend comment on struct domain's need_iommu field.

xen/arch/x86/domain.c +6 -0
@@ -1924,6 +1924,12 @@ int domain_relinquish_resources(struct d
         }
 
         d->arch.relmem = RELMEM_xen;
+
+        spin_lock(&d->page_alloc_lock);
+        page_list_splice(&d->arch.relmem_list, &d->page_list);
+        INIT_PAGE_LIST_HEAD(&d->arch.relmem_list);
+        spin_unlock(&d->page_alloc_lock);
+
         /* Fallthrough. Relinquish every page of memory. */
     case RELMEM_xen:
         ret = relinquish_memory(d, &d->xenpage_list, ~0UL);
xen/arch/x86/mm/p2m-pod.c +2 -1
@@ -459,7 +459,8 @@ p2m_pod_offline_or_broken_hit(struct pag
 
 pod_hit:
     lock_page_alloc(p2m);
-    page_list_add_tail(p, &d->arch.relmem_list);
+    /* Insertion must be at list head (see iommu_populate_page_table()). */
+    page_list_add(p, &d->arch.relmem_list);
     unlock_page_alloc(p2m);
     pod_unlock(p2m);
     return 1;
xen/drivers/passthrough/iommu.c +18 -1
@@ -18,6 +18,7 @@
 #include <asm/hvm/iommu.h>
 #include <xen/paging.h>
 #include <xen/guest_access.h>
+#include <xen/event.h>
 #include <xen/softirq.h>
 #include <xen/keyhandler.h>
 #include <xsm/xsm.h>
@@ -265,7 +266,23 @@ static int assign_device(struct domain *
              d->mem_event->paging.ring_page)) )
         return -EXDEV;
 
-    spin_lock(&pcidevs_lock);
+    if ( !spin_trylock(&pcidevs_lock) )
+        return -ERESTART;
+
+    if ( need_iommu(d) <= 0 )
+    {
+        if ( !iommu_use_hap_pt(d) )
+        {
+            rc = iommu_populate_page_table(d);
+            if ( rc )
+            {
+                spin_unlock(&pcidevs_lock);
+                return rc;
+            }
+        }
+        d->need_iommu = 1;
+    }
+
     pdev = pci_get_pdev_by_domain(dom0, seg, bus, devfn);
     if ( !pdev )
     {

xen/drivers/passthrough/iommu.c static int assign_device(struct domain * · line 311

Quoted 6 lines
xen/drivers/passthrough/iommu.c +2 -1
@@ -290,15 +307,14 @@ static int assign_device(struct domain *
                    rc);
     }
 
-    if ( has_arch_pdevs(d) && !need_iommu(d) )
+ done:
+    if ( !has_arch_pdevs(d) && need_iommu(d) )

We now have a case where, for the first device, we could set up pagetables for a large domain, get an error with assignment, then tear them all back down. (-EBUSY from pci_get_pdev() looks like a good non-fatal candidate for causing this behaviour)

I am wondering whether this is better for worse than the race condition where a guest couldn't use the device. A guest could not reasonably
expect to use a device before the toolstack is done setting it up. A
buggy toolstack could quite easily tie up a lot of Xen time creating and destroying complete iommu pagetable sets.

~Andrew

Quoted 109 lines
xen/drivers/passthrough/iommu.c +43 -9
     {
-        d->need_iommu = 1;
-        if ( !iommu_use_hap_pt(d) )
-            rc = iommu_populate_page_table(d);
-        goto done;
+        d->need_iommu = 0;
+        hd->platform_ops->teardown(d);
     }
-done:
     spin_unlock(&pcidevs_lock);
+
     return rc;
 }
 
@@ -306,12 +322,17 @@ static int iommu_populate_page_table(str
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
     struct page_info *page;
-    int rc = 0;
+    int rc = 0, n = 0;
+
+    d->need_iommu = -1;
 
     this_cpu(iommu_dont_flush_iotlb) = 1;
     spin_lock(&d->page_alloc_lock);
 
-    page_list_for_each ( page, &d->page_list )
+    if ( unlikely(d->is_dying) )
+        rc = -ESRCH;
+
+    while ( !rc && (page = page_list_remove_head(&d->page_list)) )
     {
         if ( is_hvm_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page )
@@ -321,7 +342,32 @@ static int iommu_populate_page_table(str
                 d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
                 IOMMUF_readable|IOMMUF_writable);
             if ( rc )
+            {
+                page_list_add(page, &d->page_list);
                 break;
+            }
+        }
+        page_list_add_tail(page, &d->arch.relmem_list);
+        if ( !(++n & 0xff) && !page_list_empty(&d->page_list) &&
+             hypercall_preempt_check() )
+            rc = -ERESTART;
+    }
+
+    if ( !rc )
+    {
+        /*
+         * The expectation here is that generally there are many normal pages
+         * on relmem_list (the ones we put there) and only few being in an
+         * offline/broken state. The latter ones are always at the head of the
+         * list. Hence we first move the whole list, and then move back the
+         * first few entries.
+         */
+        page_list_move(&d->page_list, &d->arch.relmem_list);
+        while ( (page = page_list_first(&d->page_list)) != NULL &&
+                (page->count_info & (PGC_state|PGC_broken)) )
+        {
+            page_list_del(page, &d->page_list);
+            page_list_add_tail(page, &d->arch.relmem_list);
         }
     }
 
@@ -330,8 +376,11 @@ static int iommu_populate_page_table(str
 
     if ( !rc )
         iommu_iotlb_flush_all(d);
-    else
+    else if ( rc != -ERESTART )
+    {
+        d->need_iommu = 0;
         hd->platform_ops->teardown(d);
+    }
 
     return rc;
 }
@@ -688,7 +737,10 @@ int iommu_do_domctl(
 
         ret = device_assigned(seg, bus, devfn) ?:
               assign_device(d, seg, bus, devfn);
-        if ( ret )
+        if ( ret == -ERESTART )
+            ret = hypercall_create_continuation(__HYPERVISOR_domctl,
+                                                "h", u_domctl);
+        else if ( ret )
             printk(XENLOG_G_ERR "XEN_DOMCTL_assign_device: "
                    "assign %04x:%02x:%02x.%u to dom%d failed (%d)\n",
                    seg, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
xen/include/xen/sched.h +2 -2
@@ -322,8 +322,8 @@ struct domain
     enum guest_type guest_type;
 
 #ifdef HAS_PASSTHROUGH
-    /* Does this guest need iommu mappings? */
-    bool_t           need_iommu;
+    /* Does this guest need iommu mappings (-1 meaning "being set up")? */
+    s8               need_iommu;
 #endif
     /* is node-affinity automatically computed? */
     bool_t           auto_node_affinity;
Quoted 4 lines

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

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

#10 lore raw

Re: [PATCH v2 1/5] IOMMU: make page table population preemptible

Quoted 15 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

xen/drivers/passthrough/iommu.c static int assign_device(struct domain * · line 311

Quoted 7 lines · Jan Beulich
xen/drivers/passthrough/iommu.c +2 -1
@@ -290,15 +307,14 @@ static int assign_device(struct domain *
                    rc);
     }
 
-    if ( has_arch_pdevs(d) && !need_iommu(d) )
+ done:
+    if ( !has_arch_pdevs(d) && need_iommu(d) )

We now have a case where, for the first device, we could set up pagetables for a large domain, get an error with assignment, then tear them all back down. (-EBUSY from pci_get_pdev() looks like a good non-fatal candidate for causing this behaviour)

Quoted 5 lines

I am wondering whether this is better for worse than the race condition where a guest couldn't use the device. A guest could not reasonably
expect to use a device before the toolstack is done setting it up. A
buggy toolstack could quite easily tie up a lot of Xen time creating and destroying complete iommu pagetable sets.

I don't think it's worth worrying about buggy tool stacks here - they should simply get fixed.

Furthermore this change in operation ordering is only a (nice) side effect, the necessary cleanup seemed easier with the order changed. And said time window would have grown with the added preemption handling.

Jan

Reviewed-by
#11 lore raw

Re: [PATCH v2 1/5] IOMMU: make page table population preemptible

Quoted 22 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

xen/drivers/passthrough/iommu.c static int assign_device(struct domain * · line 311

Quoted 7 lines · Jan Beulich
xen/drivers/passthrough/iommu.c +2 -1
@@ -290,15 +307,14 @@ static int assign_device(struct domain *
                    rc);
     }
 
-    if ( has_arch_pdevs(d) && !need_iommu(d) )
+ done:
+    if ( !has_arch_pdevs(d) && need_iommu(d) )

We now have a case where, for the first device, we could set up pagetables for a large domain, get an error with assignment, then tear them all back down. (-EBUSY from pci_get_pdev() looks like a good non-fatal candidate for causing this behaviour)

Quoted 5 lines

I am wondering whether this is better for worse than the race condition where a guest couldn't use the device. A guest could not reasonably
expect to use a device before the toolstack is done setting it up. A
buggy toolstack could quite easily tie up a lot of Xen time creating and destroying complete iommu pagetable sets.

I don't think it's worth worrying about buggy tool stacks here - they should simply get fixed.

Quoted 7 lines

Furthermore this change in operation ordering is only a (nice) side effect, the necessary cleanup seemed easier with the order changed. And said time window would have grown with the added preemption handling.

Jan

All true.

Acked-by Reviewed-by Andrew Cooper
#12 lore raw

Re: [PATCH v2 1/5] IOMMU: make page table population preemptible

Xiantao
-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: Friday, December 13, 2013 11:44 PM
To: Jan Beulich
Cc: George Dunlap; Zhang, Xiantao; xen-devel; Keir Fraser; Tim Deegan
Subject: Re: [Xen-devel] [PATCH v2 1/5] IOMMU: make page table population preemptible

Quoted 22 lines · Jan Beulich
Quoted 14 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper

xen/drivers/passthrough/iommu.c static int assign_device(struct domain * · line 311

Quoted 7 lines · Jan Beulich
xen/drivers/passthrough/iommu.c +2 -1
@@ -290,15 +307,14 @@ static int assign_device(struct domain *
                    rc);
     }
 
-    if ( has_arch_pdevs(d) && !need_iommu(d) )
+ done:
+    if ( !has_arch_pdevs(d) && need_iommu(d) )

We now have a case where, for the first device, we could set up pagetables for a large domain, get an error with assignment, then tear them all back down. (-EBUSY from pci_get_pdev() looks like a good non-fatal candidate for causing this behaviour)

Quoted 5 lines

I am wondering whether this is better for worse than the race condition where a guest couldn't use the device. A guest could not reasonably
expect to use a device before the toolstack is done setting it up. A
buggy toolstack could quite easily tie up a lot of Xen time creating and destroying complete iommu pagetable sets.

I don't think it's worth worrying about buggy tool stacks here - they should simply get fixed.

Quoted 7 lines

Furthermore this change in operation ordering is only a (nice) side effect, the necessary cleanup seemed easier with the order changed. And said time window would have grown with the added preemption handling.

Jan

All true.

#14 lore raw

Re: [PATCH 1/5] IOMMU: make page table population preemptible

In reply to Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Jan Beulich

Since this can take an arbitrary amount of time, the rooting domctl as well as all involved code must become aware of this requiring a continuation.

The subject domain's rel_mem_list is being (ab)used for this, in a way similar to and compatible with broken page offlining.

Quoted 6 lines

Further, operations get slightly re-ordered in assign_device(): IOMMU page tables now get set up _before_ the first device gets assigned, at once closing a small timing window in which the guest may already see the device but wouldn't be able to access it.

Ping (also for patch 2 of this series)?

Quoted 174 lines
xen/arch/x86/domain.c +6 -0
@@ -1923,6 +1923,12 @@ int domain_relinquish_resources(struct d
         }
 
         d->arch.relmem = RELMEM_xen;
+
+        spin_lock(&d->page_alloc_lock);
+        page_list_splice(&d->arch.relmem_list, &d->page_list);
+        INIT_PAGE_LIST_HEAD(&d->arch.relmem_list);
+        spin_unlock(&d->page_alloc_lock);
+
         /* Fallthrough. Relinquish every page of memory. */
     case RELMEM_xen:
         ret = relinquish_memory(d, &d->xenpage_list, ~0UL);
xen/arch/x86/mm/p2m-pod.c +2 -1
@@ -459,7 +459,8 @@ p2m_pod_offline_or_broken_hit(struct pag
 
 pod_hit:
     lock_page_alloc(p2m);
-    page_list_add_tail(p, &d->arch.relmem_list);
+    /* Insertion must be at list head (see iommu_populate_page_table()). */
+    page_list_add(p, &d->arch.relmem_list);
     unlock_page_alloc(p2m);
     pod_unlock(p2m);
     return 1;
xen/drivers/passthrough/iommu.c +44 -9
@@ -18,6 +18,7 @@
 #include <asm/hvm/iommu.h>
 #include <xen/paging.h>
 #include <xen/guest_access.h>
+#include <xen/event.h>
 #include <xen/softirq.h>
 #include <xen/keyhandler.h>
 #include <xsm/xsm.h>
@@ -265,7 +266,23 @@ static int assign_device(struct domain *
              d->mem_event->paging.ring_page)) )
         return -EXDEV;
 
-    spin_lock(&pcidevs_lock);
+    if ( !spin_trylock(&pcidevs_lock) )
+        return -ERESTART;
+
+    if ( need_iommu(d) <= 0 )
+    {
+        if ( !iommu_use_hap_pt(d) )
+        {
+            rc = iommu_populate_page_table(d);
+            if ( rc )
+            {
+                spin_unlock(&pcidevs_lock);
+                return rc;
+            }
+        }
+        d->need_iommu = 1;
+    }
+
     pdev = pci_get_pdev_by_domain(dom0, seg, bus, devfn);
     if ( !pdev )
     {
@@ -290,15 +307,14 @@ static int assign_device(struct domain *
                    rc);
     }
 
-    if ( has_arch_pdevs(d) && !need_iommu(d) )
+ done:
+    if ( !has_arch_pdevs(d) && need_iommu(d) )
     {
-        d->need_iommu = 1;
-        if ( !iommu_use_hap_pt(d) )
-            rc = iommu_populate_page_table(d);
-        goto done;
+        d->need_iommu = 0;
+        hd->platform_ops->teardown(d);
     }
-done:
     spin_unlock(&pcidevs_lock);
+
     return rc;
 }
 
@@ -306,12 +322,17 @@ static int iommu_populate_page_table(str
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
     struct page_info *page;
-    int rc = 0;
+    int rc = 0, n = 0;
+
+    d->need_iommu = -1;
 
     this_cpu(iommu_dont_flush_iotlb) = 1;
     spin_lock(&d->page_alloc_lock);
 
-    page_list_for_each ( page, &d->page_list )
+    if ( unlikely(d->is_dying) )
+        rc = -ESRCH;
+
+    while ( !rc && (page = page_list_remove_head(&d->page_list)) )
     {
         if ( is_hvm_domain(d) ||
             (page->u.inuse.type_info & PGT_type_mask) == PGT_writable_page )
@@ -321,7 +342,32 @@ static int iommu_populate_page_table(str
                 d, mfn_to_gmfn(d, page_to_mfn(page)), page_to_mfn(page),
                 IOMMUF_readable|IOMMUF_writable);
             if ( rc )
+            {
+                page_list_add(page, &d->page_list);
                 break;
+            }
+        }
+        page_list_add_tail(page, &d->arch.relmem_list);
+        if ( !(++n & 0xff) && !page_list_empty(&d->page_list) &&
+             hypercall_preempt_check() )
+            rc = -ERESTART;
+    }
+
+    if ( !rc )
+    {
+        /*
+         * The expectation here is that generally there are many normal 

pages

xen/drivers/passthrough/iommu.c +2 -0
+         * on relmem_list (the ones we put there) and only few being in an
+         * offline/broken state. The latter ones are always at the head of 

the

xen/drivers/passthrough/iommu.c +17 -2
+         * list. Hence we first move the whole list, and then move back the
+         * first few entries.
+         */
+        page_list_move(&d->page_list, &d->arch.relmem_list);
+        while ( (page = page_list_first(&d->page_list)) != NULL &&
+                (page->count_info & (PGC_state|PGC_broken)) )
+        {
+            page_list_del(page, &d->page_list);
+            page_list_add_tail(page, &d->arch.relmem_list);
         }
     }
@@ -330,8 +376,11 @@ static int iommu_populate_page_table(str
 
     if ( !rc )
         iommu_iotlb_flush_all(d);
-    else
+    else if ( rc != -ERESTART )
+    {
         hd->platform_ops->teardown(d);
+        d->need_iommu = 0;
+    }
 
     return rc;
 }
@@ -688,7 +737,10 @@ int iommu_do_domctl(
 
         ret = device_assigned(seg, bus, devfn) ?:
               assign_device(d, seg, bus, devfn);
-        if ( ret )
+        if ( ret == -ERESTART )
+            ret = hypercall_create_continuation(__HYPERVISOR_domctl,
+                                                "h", u_domctl);
+        else if ( ret )
             printk(XENLOG_G_ERR "XEN_DOMCTL_assign_device: "
                    "assign %04x:%02x:%02x.%u to dom%d failed (%d)\n",
                    seg, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
xen/include/xen/sched.h +1 -1
@@ -323,7 +323,7 @@ struct domain
 
 #ifdef HAS_PASSTHROUGH
     /* Does this guest need iommu mappings? */
-    bool_t           need_iommu;
+    s8               need_iommu;
 #endif
     /* is node-affinity automatically computed? */
     bool_t           auto_node_affinity;
#15 lore raw

PATCH 2/5 IOMMU: make page table deallocation preemptible

In reply to Jan Beulich

This too can take an arbitrary amount of time.

In fact, the bulk of the work is being moved to a tasklet, as handling the necessary preemption logic in line seems close to impossible given that the teardown may also be invoked on error paths.

xen/drivers/passthrough/amd/pci_amd_iommu.c +33 -2
@@ -195,6 +195,8 @@ static int __init amd_iommu_setup_dom0_d
     return 0;
 }
 
+static void deallocate_page_tables(unsigned long unused);
+
 int __init amd_iov_detect(void)
 {
     INIT_LIST_HEAD(&amd_iommu_head);
@@ -215,6 +217,7 @@ int __init amd_iov_detect(void)
         return -ENODEV;
     }
 
+    tasklet_init(&iommu_pt_cleanup_tasklet, deallocate_page_tables, 0);
     init_done = 1;
 
     if ( !amd_iommu_perdev_intremap )
@@ -405,11 +408,21 @@ static int amd_iommu_assign_device(struc
     return reassign_device(dom0, d, devfn, pdev);
 }
 
-static void deallocate_next_page_table(struct page_info* pg, int level)
+static void deallocate_next_page_table(struct page_info *pg, int level)
+{
+    spin_lock(&iommu_pt_cleanup_lock);
+    PFN_ORDER(pg) = level;
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void deallocate_page_table(struct page_info *pg)
 {
     void *table_vaddr, *pde;
     u64 next_table_maddr;
-    int index, next_level;
+    unsigned int index, level = PFN_ORDER(pg), next_level;
+
+    PFN_ORDER(pg) = 0;
 
     if ( level <= 1 )
     {
@@ -439,6 +452,23 @@ static void deallocate_next_page_table(s
     free_amd_iommu_pgtable(pg);
 }
 
+static void deallocate_page_tables(unsigned long unused)
+{
+    do {
+        struct page_info *pg;
+
+        spin_lock(&iommu_pt_cleanup_lock);
+        pg = page_list_remove_head(&iommu_pt_cleanup_list);
+        spin_unlock(&iommu_pt_cleanup_lock);
+        if ( !pg )
+            return;
+        deallocate_page_table(pg);
+    } while ( !softirq_pending(smp_processor_id()) );
+
+    tasklet_schedule_on_cpu(&iommu_pt_cleanup_tasklet,
+                            cpumask_cycle(smp_processor_id(), &cpu_online_map));
+}
+
 static void deallocate_iommu_page_tables(struct domain *d)
 {
     struct hvm_iommu *hd  = domain_hvm_iommu(d);
@@ -451,6 +481,7 @@ static void deallocate_iommu_page_tables
     {
         deallocate_next_page_table(hd->root_table, hd->paging_mode);
         hd->root_table = NULL;
+        tasklet_schedule(&iommu_pt_cleanup_tasklet);
     }
     spin_unlock(&hd->mapping_lock);
 }
xen/drivers/passthrough/iommu.c +4 -0
@@ -58,6 +58,10 @@ bool_t __read_mostly amd_iommu_perdev_in
 
 DEFINE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
 
+DEFINE_SPINLOCK(iommu_pt_cleanup_lock);
+PAGE_LIST_HEAD(iommu_pt_cleanup_list);
+struct tasklet iommu_pt_cleanup_tasklet;
+
 static struct keyhandler iommu_p2m_table = {
     .diagnostic = 0,
     .u.fn = iommu_dump_p2m_table,
xen/drivers/passthrough/vtd/iommu.c +34 -3
@@ -668,13 +668,24 @@ static void dma_pte_clear_one(struct dom
 
 static void iommu_free_pagetable(u64 pt_maddr, int level)
 {
-    int i;
-    struct dma_pte *pt_vaddr, *pte;
-    int next_level = level - 1;
+    struct page_info *pg = maddr_to_page(pt_maddr);
 
     if ( pt_maddr == 0 )
         return;
 
+    spin_lock(&iommu_pt_cleanup_lock);
+    PFN_ORDER(pg) = level;
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void iommu_free_page_table(struct page_info *pg)
+{
+    unsigned int i, next_level = PFN_ORDER(pg) - 1;
+    u64 pt_maddr = page_to_maddr(pg);
+    struct dma_pte *pt_vaddr, *pte;
+
+    PFN_ORDER(pg) = 0;
     pt_vaddr = (struct dma_pte *)map_vtd_domain_page(pt_maddr);
 
     for ( i = 0; i < PTE_NUM; i++ )
@@ -694,6 +705,23 @@ static void iommu_free_pagetable(u64 pt_
     free_pgtable_maddr(pt_maddr);
 }
 
+static void iommu_free_pagetables(unsigned long unused)
+{
+    do {
+        struct page_info *pg;
+
+        spin_lock(&iommu_pt_cleanup_lock);
+        pg = page_list_remove_head(&iommu_pt_cleanup_list);
+        spin_unlock(&iommu_pt_cleanup_lock);
+        if ( !pg )
+            return;
+        iommu_free_page_table(pg);
+    } while ( !softirq_pending(smp_processor_id()) );
+
+    tasklet_schedule_on_cpu(&iommu_pt_cleanup_tasklet,
+                            cpumask_cycle(smp_processor_id(), &cpu_online_map));
+}
+
 static int iommu_set_root_entry(struct iommu *iommu)
 {
     u32 sts;
@@ -1704,6 +1732,8 @@ void iommu_domain_teardown(struct domain
     iommu_free_pagetable(hd->pgd_maddr, agaw_to_level(hd->agaw));
     hd->pgd_maddr = 0;
     spin_unlock(&hd->mapping_lock);
+
+    tasklet_schedule(&iommu_pt_cleanup_tasklet);
 }
 
 static int intel_iommu_map_page(
@@ -2204,6 +2234,7 @@ int __init intel_vtd_setup(void)
     if ( ret )
         goto error;
 
+    tasklet_init(&iommu_pt_cleanup_tasklet, iommu_free_pagetables, 0);
     register_keyhandler('V', &dump_iommu_info_keyhandler);
 
     return 0;
xen/include/xen/iommu.h +4 -0
@@ -151,4 +151,8 @@ int adjust_vtd_irq_affinities(void);
  */
 DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
 
+extern struct spinlock iommu_pt_cleanup_lock;
+extern struct page_list_head iommu_pt_cleanup_list;
+extern struct tasklet iommu_pt_cleanup_tasklet;
+
 #endif /* _IOMMU_H_ */

IOMMU: make page table deallocation preemptible

This too can take an arbitrary amount of time.

In fact, the bulk of the work is being moved to a tasklet, as handling the necessary preemption logic in line seems close to impossible given that the teardown may also be invoked on error paths.

xen/drivers/passthrough/amd/pci_amd_iommu.c +33 -2
@@ -195,6 +195,8 @@ static int __init amd_iommu_setup_dom0_d
     return 0;
 }
 
+static void deallocate_page_tables(unsigned long unused);
+
 int __init amd_iov_detect(void)
 {
     INIT_LIST_HEAD(&amd_iommu_head);
@@ -215,6 +217,7 @@ int __init amd_iov_detect(void)
         return -ENODEV;
     }
 
+    tasklet_init(&iommu_pt_cleanup_tasklet, deallocate_page_tables, 0);
     init_done = 1;
 
     if ( !amd_iommu_perdev_intremap )
@@ -405,11 +408,21 @@ static int amd_iommu_assign_device(struc
     return reassign_device(dom0, d, devfn, pdev);
 }
 
-static void deallocate_next_page_table(struct page_info* pg, int level)
+static void deallocate_next_page_table(struct page_info *pg, int level)
+{
+    spin_lock(&iommu_pt_cleanup_lock);
+    PFN_ORDER(pg) = level;
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void deallocate_page_table(struct page_info *pg)
 {
     void *table_vaddr, *pde;
     u64 next_table_maddr;
-    int index, next_level;
+    unsigned int index, level = PFN_ORDER(pg), next_level;
+
+    PFN_ORDER(pg) = 0;
 
     if ( level <= 1 )
     {
@@ -439,6 +452,23 @@ static void deallocate_next_page_table(s
     free_amd_iommu_pgtable(pg);
 }
 
+static void deallocate_page_tables(unsigned long unused)
+{
+    do {
+        struct page_info *pg;
+
+        spin_lock(&iommu_pt_cleanup_lock);
+        pg = page_list_remove_head(&iommu_pt_cleanup_list);
+        spin_unlock(&iommu_pt_cleanup_lock);
+        if ( !pg )
+            return;
+        deallocate_page_table(pg);
+    } while ( !softirq_pending(smp_processor_id()) );
+
+    tasklet_schedule_on_cpu(&iommu_pt_cleanup_tasklet,
+                            cpumask_cycle(smp_processor_id(), &cpu_online_map));
+}
+
 static void deallocate_iommu_page_tables(struct domain *d)
 {
     struct hvm_iommu *hd  = domain_hvm_iommu(d);
@@ -451,6 +481,7 @@ static void deallocate_iommu_page_tables
     {
         deallocate_next_page_table(hd->root_table, hd->paging_mode);
         hd->root_table = NULL;
+        tasklet_schedule(&iommu_pt_cleanup_tasklet);
     }
     spin_unlock(&hd->mapping_lock);
 }
xen/drivers/passthrough/iommu.c +4 -0
@@ -58,6 +58,10 @@ bool_t __read_mostly amd_iommu_perdev_in
 
 DEFINE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
 
+DEFINE_SPINLOCK(iommu_pt_cleanup_lock);
+PAGE_LIST_HEAD(iommu_pt_cleanup_list);
+struct tasklet iommu_pt_cleanup_tasklet;
+
 static struct keyhandler iommu_p2m_table = {
     .diagnostic = 0,
     .u.fn = iommu_dump_p2m_table,
xen/drivers/passthrough/vtd/iommu.c +34 -3
@@ -668,13 +668,24 @@ static void dma_pte_clear_one(struct dom
 
 static void iommu_free_pagetable(u64 pt_maddr, int level)
 {
-    int i;
-    struct dma_pte *pt_vaddr, *pte;
-    int next_level = level - 1;
+    struct page_info *pg = maddr_to_page(pt_maddr);
 
     if ( pt_maddr == 0 )
         return;
 
+    spin_lock(&iommu_pt_cleanup_lock);
+    PFN_ORDER(pg) = level;
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void iommu_free_page_table(struct page_info *pg)
+{
+    unsigned int i, next_level = PFN_ORDER(pg) - 1;
+    u64 pt_maddr = page_to_maddr(pg);
+    struct dma_pte *pt_vaddr, *pte;
+
+    PFN_ORDER(pg) = 0;
     pt_vaddr = (struct dma_pte *)map_vtd_domain_page(pt_maddr);
 
     for ( i = 0; i < PTE_NUM; i++ )
@@ -694,6 +705,23 @@ static void iommu_free_pagetable(u64 pt_
     free_pgtable_maddr(pt_maddr);
 }
 
+static void iommu_free_pagetables(unsigned long unused)
+{
+    do {
+        struct page_info *pg;
+
+        spin_lock(&iommu_pt_cleanup_lock);
+        pg = page_list_remove_head(&iommu_pt_cleanup_list);
+        spin_unlock(&iommu_pt_cleanup_lock);
+        if ( !pg )
+            return;
+        iommu_free_page_table(pg);
+    } while ( !softirq_pending(smp_processor_id()) );
+
+    tasklet_schedule_on_cpu(&iommu_pt_cleanup_tasklet,
+                            cpumask_cycle(smp_processor_id(), &cpu_online_map));
+}
+
 static int iommu_set_root_entry(struct iommu *iommu)
 {
     u32 sts;
@@ -1704,6 +1732,8 @@ void iommu_domain_teardown(struct domain
     iommu_free_pagetable(hd->pgd_maddr, agaw_to_level(hd->agaw));
     hd->pgd_maddr = 0;
     spin_unlock(&hd->mapping_lock);
+
+    tasklet_schedule(&iommu_pt_cleanup_tasklet);
 }
 
 static int intel_iommu_map_page(
@@ -2204,6 +2234,7 @@ int __init intel_vtd_setup(void)
     if ( ret )
         goto error;
 
+    tasklet_init(&iommu_pt_cleanup_tasklet, iommu_free_pagetables, 0);
     register_keyhandler('V', &dump_iommu_info_keyhandler);
 
     return 0;
xen/include/xen/iommu.h +4 -0
@@ -151,4 +151,8 @@ int adjust_vtd_irq_affinities(void);
  */
 DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
 
+extern struct spinlock iommu_pt_cleanup_lock;
+extern struct page_list_head iommu_pt_cleanup_list;
+extern struct tasklet iommu_pt_cleanup_tasklet;
+
 #endif /* _IOMMU_H_ */

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

#16 lore raw

Re: [PATCH 2/5] IOMMU: make page table deallocation preemptible

Quoted 7 lines · Jan Beulich

This too can take an arbitrary amount of time.

In fact, the bulk of the work is being moved to a tasklet, as handling the necessary preemption logic in line seems close to impossible given that the teardown may also be invoked on error paths.

There is a lot of duplicated code here. I would suggest making most of this infrastructure common, and having a new iommu_op for "void free_io_page_table(struct page_info*);"

~Andrew

Quoted 175 lines
xen/drivers/passthrough/amd/pci_amd_iommu.c +33 -2
@@ -195,6 +195,8 @@ static int __init amd_iommu_setup_dom0_d
     return 0;
 }
 
+static void deallocate_page_tables(unsigned long unused);
+
 int __init amd_iov_detect(void)
 {
     INIT_LIST_HEAD(&amd_iommu_head);
@@ -215,6 +217,7 @@ int __init amd_iov_detect(void)
         return -ENODEV;
     }
 
+    tasklet_init(&iommu_pt_cleanup_tasklet, deallocate_page_tables, 0);
     init_done = 1;
 
     if ( !amd_iommu_perdev_intremap )
@@ -405,11 +408,21 @@ static int amd_iommu_assign_device(struc
     return reassign_device(dom0, d, devfn, pdev);
 }
 
-static void deallocate_next_page_table(struct page_info* pg, int level)
+static void deallocate_next_page_table(struct page_info *pg, int level)
+{
+    spin_lock(&iommu_pt_cleanup_lock);
+    PFN_ORDER(pg) = level;
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void deallocate_page_table(struct page_info *pg)
 {
     void *table_vaddr, *pde;
     u64 next_table_maddr;
-    int index, next_level;
+    unsigned int index, level = PFN_ORDER(pg), next_level;
+
+    PFN_ORDER(pg) = 0;
 
     if ( level <= 1 )
     {
@@ -439,6 +452,23 @@ static void deallocate_next_page_table(s
     free_amd_iommu_pgtable(pg);
 }
 
+static void deallocate_page_tables(unsigned long unused)
+{
+    do {
+        struct page_info *pg;
+
+        spin_lock(&iommu_pt_cleanup_lock);
+        pg = page_list_remove_head(&iommu_pt_cleanup_list);
+        spin_unlock(&iommu_pt_cleanup_lock);
+        if ( !pg )
+            return;
+        deallocate_page_table(pg);
+    } while ( !softirq_pending(smp_processor_id()) );
+
+    tasklet_schedule_on_cpu(&iommu_pt_cleanup_tasklet,
+                            cpumask_cycle(smp_processor_id(), &cpu_online_map));
+}
+
 static void deallocate_iommu_page_tables(struct domain *d)
 {
     struct hvm_iommu *hd  = domain_hvm_iommu(d);
@@ -451,6 +481,7 @@ static void deallocate_iommu_page_tables
     {
         deallocate_next_page_table(hd->root_table, hd->paging_mode);
         hd->root_table = NULL;
+        tasklet_schedule(&iommu_pt_cleanup_tasklet);
     }
     spin_unlock(&hd->mapping_lock);
 }
xen/drivers/passthrough/iommu.c +4 -0
@@ -58,6 +58,10 @@ bool_t __read_mostly amd_iommu_perdev_in
 
 DEFINE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
 
+DEFINE_SPINLOCK(iommu_pt_cleanup_lock);
+PAGE_LIST_HEAD(iommu_pt_cleanup_list);
+struct tasklet iommu_pt_cleanup_tasklet;
+
 static struct keyhandler iommu_p2m_table = {
     .diagnostic = 0,
     .u.fn = iommu_dump_p2m_table,
xen/drivers/passthrough/vtd/iommu.c +34 -3
@@ -668,13 +668,24 @@ static void dma_pte_clear_one(struct dom
 
 static void iommu_free_pagetable(u64 pt_maddr, int level)
 {
-    int i;
-    struct dma_pte *pt_vaddr, *pte;
-    int next_level = level - 1;
+    struct page_info *pg = maddr_to_page(pt_maddr);
 
     if ( pt_maddr == 0 )
         return;
 
+    spin_lock(&iommu_pt_cleanup_lock);
+    PFN_ORDER(pg) = level;
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void iommu_free_page_table(struct page_info *pg)
+{
+    unsigned int i, next_level = PFN_ORDER(pg) - 1;
+    u64 pt_maddr = page_to_maddr(pg);
+    struct dma_pte *pt_vaddr, *pte;
+
+    PFN_ORDER(pg) = 0;
     pt_vaddr = (struct dma_pte *)map_vtd_domain_page(pt_maddr);
 
     for ( i = 0; i < PTE_NUM; i++ )
@@ -694,6 +705,23 @@ static void iommu_free_pagetable(u64 pt_
     free_pgtable_maddr(pt_maddr);
 }
 
+static void iommu_free_pagetables(unsigned long unused)
+{
+    do {
+        struct page_info *pg;
+
+        spin_lock(&iommu_pt_cleanup_lock);
+        pg = page_list_remove_head(&iommu_pt_cleanup_list);
+        spin_unlock(&iommu_pt_cleanup_lock);
+        if ( !pg )
+            return;
+        iommu_free_page_table(pg);
+    } while ( !softirq_pending(smp_processor_id()) );
+
+    tasklet_schedule_on_cpu(&iommu_pt_cleanup_tasklet,
+                            cpumask_cycle(smp_processor_id(), &cpu_online_map));
+}
+
 static int iommu_set_root_entry(struct iommu *iommu)
 {
     u32 sts;
@@ -1704,6 +1732,8 @@ void iommu_domain_teardown(struct domain
     iommu_free_pagetable(hd->pgd_maddr, agaw_to_level(hd->agaw));
     hd->pgd_maddr = 0;
     spin_unlock(&hd->mapping_lock);
+
+    tasklet_schedule(&iommu_pt_cleanup_tasklet);
 }
 
 static int intel_iommu_map_page(
@@ -2204,6 +2234,7 @@ int __init intel_vtd_setup(void)
     if ( ret )
         goto error;
 
+    tasklet_init(&iommu_pt_cleanup_tasklet, iommu_free_pagetables, 0);
     register_keyhandler('V', &dump_iommu_info_keyhandler);
 
     return 0;
xen/include/xen/iommu.h +4 -0
@@ -151,4 +151,8 @@ int adjust_vtd_irq_affinities(void);
  */
 DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
 
+extern struct spinlock iommu_pt_cleanup_lock;
+extern struct page_list_head iommu_pt_cleanup_list;
+extern struct tasklet iommu_pt_cleanup_tasklet;
+
 #endif /* _IOMMU_H_ */
Quoted 4 lines

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

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

#17 lore raw

Re: [PATCH 2/5] IOMMU: make page table deallocation preemptible

Quoted 8 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 6 lines · Jan Beulich

This too can take an arbitrary amount of time.

In fact, the bulk of the work is being moved to a tasklet, as handling the necessary preemption logic in line seems close to impossible given that the teardown may also be invoked on error paths.

Quoted 5 lines
Quoted 1 line

There is a lot of duplicated code here. I would suggest making most of this infrastructure common, and having a new iommu_op for "void free_io_page_table(struct page_info*);"

I guess that could be done - let me see how this works out.

Jan

#18 lore raw

PATCH v2 2/5 IOMMU: make page table deallocation preemptible

In reply to Andrew Cooper

This too can take an arbitrary amount of time.

In fact, the bulk of the work is being moved to a tasklet, as handling the necessary preemption logic in line seems close to impossible given that the teardown may also be invoked on error paths.

---
v2: abstract out tasklet logic

xen/drivers/passthrough/amd/pci_amd_iommu.c +13 -2
@@ -405,11 +405,21 @@ static int amd_iommu_assign_device(struc
     return reassign_device(dom0, d, devfn, pdev);
 }
 
-static void deallocate_next_page_table(struct page_info* pg, int level)
+static void deallocate_next_page_table(struct page_info *pg, int level)
+{
+    PFN_ORDER(pg) = level;
+    spin_lock(&iommu_pt_cleanup_lock);
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void deallocate_page_table(struct page_info *pg)
 {
     void *table_vaddr, *pde;
     u64 next_table_maddr;
-    int index, next_level;
+    unsigned int index, level = PFN_ORDER(pg), next_level;
+
+    PFN_ORDER(pg) = 0;
 
     if ( level <= 1 )
     {
@@ -599,6 +609,7 @@ const struct iommu_ops amd_iommu_ops = {
     .teardown = amd_iommu_domain_destroy,
     .map_page = amd_iommu_map_page,
     .unmap_page = amd_iommu_unmap_page,
+    .free_page_table = deallocate_page_table,
     .reassign_device = reassign_device,
     .get_device_group_id = amd_iommu_group_id,
     .update_ire_from_apic = amd_iommu_ioapic_update_ire,
xen/drivers/passthrough/iommu.c +35 -16

99 lines not on this page · raw message

xen/drivers/passthrough/vtd/iommu.c +15 -3
@@ -668,13 +668,24 @@ static void dma_pte_clear_one(struct dom
 
 static void iommu_free_pagetable(u64 pt_maddr, int level)
 {
-    int i;
-    struct dma_pte *pt_vaddr, *pte;
-    int next_level = level - 1;
+    struct page_info *pg = maddr_to_page(pt_maddr);
 
     if ( pt_maddr == 0 )
         return;
 
+    PFN_ORDER(pg) = level;
+    spin_lock(&iommu_pt_cleanup_lock);
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void iommu_free_page_table(struct page_info *pg)
+{
+    unsigned int i, next_level = PFN_ORDER(pg) - 1;
+    u64 pt_maddr = page_to_maddr(pg);
+    struct dma_pte *pt_vaddr, *pte;
+
+    PFN_ORDER(pg) = 0;
     pt_vaddr = (struct dma_pte *)map_vtd_domain_page(pt_maddr);
 
     for ( i = 0; i < PTE_NUM; i++ )
@@ -2430,6 +2441,7 @@ const struct iommu_ops intel_iommu_ops =
     .teardown = iommu_domain_teardown,
     .map_page = intel_iommu_map_page,
     .unmap_page = intel_iommu_unmap_page,
+    .free_page_table = iommu_free_page_table,
     .reassign_device = reassign_device_ownership,
     .get_device_group_id = intel_iommu_group_id,
     .update_ire_from_apic = io_apic_write_remap_rte,
xen/include/xen/iommu.h +5 -0
@@ -88,6 +88,7 @@ bool_t pt_irq_need_timer(uint32_t flags)
 
 struct msi_desc;
 struct msi_msg;
+struct page_info;
 
 struct iommu_ops {
     int (*init)(struct domain *d);
@@ -100,6 +101,7 @@ struct iommu_ops {
     int (*map_page)(struct domain *d, unsigned long gfn, unsigned long mfn,
                     unsigned int flags);
     int (*unmap_page)(struct domain *d, unsigned long gfn);
+    void (*free_page_table)(struct page_info *);
     int (*reassign_device)(struct domain *s, struct domain *t,
 			   u8 devfn, struct pci_dev *);
     int (*get_device_group_id)(u16 seg, u8 bus, u8 devfn);
@@ -151,4 +153,7 @@ int adjust_vtd_irq_affinities(void);
  */
 DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
 
+extern struct spinlock iommu_pt_cleanup_lock;
+extern struct page_list_head iommu_pt_cleanup_list;
+
 #endif /* _IOMMU_H_ */

IOMMU: make page table deallocation preemptible

This too can take an arbitrary amount of time.

In fact, the bulk of the work is being moved to a tasklet, as handling the necessary preemption logic in line seems close to impossible given that the teardown may also be invoked on error paths.

---
v2: abstract out tasklet logic

xen/drivers/passthrough/amd/pci_amd_iommu.c +13 -2
@@ -405,11 +405,21 @@ static int amd_iommu_assign_device(struc
     return reassign_device(dom0, d, devfn, pdev);
 }
 
-static void deallocate_next_page_table(struct page_info* pg, int level)
+static void deallocate_next_page_table(struct page_info *pg, int level)
+{
+    PFN_ORDER(pg) = level;
+    spin_lock(&iommu_pt_cleanup_lock);
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void deallocate_page_table(struct page_info *pg)
 {
     void *table_vaddr, *pde;
     u64 next_table_maddr;
-    int index, next_level;
+    unsigned int index, level = PFN_ORDER(pg), next_level;
+
+    PFN_ORDER(pg) = 0;
 
     if ( level <= 1 )
     {
@@ -599,6 +609,7 @@ const struct iommu_ops amd_iommu_ops = {
     .teardown = amd_iommu_domain_destroy,
     .map_page = amd_iommu_map_page,
     .unmap_page = amd_iommu_unmap_page,
+    .free_page_table = deallocate_page_table,
     .reassign_device = reassign_device,
     .get_device_group_id = amd_iommu_group_id,
     .update_ire_from_apic = amd_iommu_ioapic_update_ire,
xen/drivers/passthrough/iommu.c +35 -16

99 lines not on this page · raw message

xen/drivers/passthrough/vtd/iommu.c +15 -3
@@ -668,13 +668,24 @@ static void dma_pte_clear_one(struct dom
 
 static void iommu_free_pagetable(u64 pt_maddr, int level)
 {
-    int i;
-    struct dma_pte *pt_vaddr, *pte;
-    int next_level = level - 1;
+    struct page_info *pg = maddr_to_page(pt_maddr);
 
     if ( pt_maddr == 0 )
         return;
 
+    PFN_ORDER(pg) = level;
+    spin_lock(&iommu_pt_cleanup_lock);
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void iommu_free_page_table(struct page_info *pg)
+{
+    unsigned int i, next_level = PFN_ORDER(pg) - 1;
+    u64 pt_maddr = page_to_maddr(pg);
+    struct dma_pte *pt_vaddr, *pte;
+
+    PFN_ORDER(pg) = 0;
     pt_vaddr = (struct dma_pte *)map_vtd_domain_page(pt_maddr);
 
     for ( i = 0; i < PTE_NUM; i++ )
@@ -2430,6 +2441,7 @@ const struct iommu_ops intel_iommu_ops =
     .teardown = iommu_domain_teardown,
     .map_page = intel_iommu_map_page,
     .unmap_page = intel_iommu_unmap_page,
+    .free_page_table = iommu_free_page_table,
     .reassign_device = reassign_device_ownership,
     .get_device_group_id = intel_iommu_group_id,
     .update_ire_from_apic = io_apic_write_remap_rte,
xen/include/xen/iommu.h +5 -0
@@ -88,6 +88,7 @@ bool_t pt_irq_need_timer(uint32_t flags)
 
 struct msi_desc;
 struct msi_msg;
+struct page_info;
 
 struct iommu_ops {
     int (*init)(struct domain *d);
@@ -100,6 +101,7 @@ struct iommu_ops {
     int (*map_page)(struct domain *d, unsigned long gfn, unsigned long mfn,
                     unsigned int flags);
     int (*unmap_page)(struct domain *d, unsigned long gfn);
+    void (*free_page_table)(struct page_info *);
     int (*reassign_device)(struct domain *s, struct domain *t,
 			   u8 devfn, struct pci_dev *);
     int (*get_device_group_id)(u16 seg, u8 bus, u8 devfn);
@@ -151,4 +153,7 @@ int adjust_vtd_irq_affinities(void);
  */
 DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
 
+extern struct spinlock iommu_pt_cleanup_lock;
+extern struct page_list_head iommu_pt_cleanup_list;
+
 #endif /* _IOMMU_H_ */

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

Reviewed-by on v2 2/5
#19 lore raw

Re: [PATCH v2 2/5] IOMMU: make page table deallocation preemptible

Quoted 7 lines · Jan Beulich

This too can take an arbitrary amount of time.

In fact, the bulk of the work is being moved to a tasklet, as handling the necessary preemption logic in line seems close to impossible given that the teardown may also be invoked on error paths.

Quoted 212 lines

---
v2: abstract out tasklet logic

xen/drivers/passthrough/amd/pci_amd_iommu.c +13 -2
@@ -405,11 +405,21 @@ static int amd_iommu_assign_device(struc
     return reassign_device(dom0, d, devfn, pdev);
 }
 
-static void deallocate_next_page_table(struct page_info* pg, int level)
+static void deallocate_next_page_table(struct page_info *pg, int level)
+{
+    PFN_ORDER(pg) = level;
+    spin_lock(&iommu_pt_cleanup_lock);
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void deallocate_page_table(struct page_info *pg)
 {
     void *table_vaddr, *pde;
     u64 next_table_maddr;
-    int index, next_level;
+    unsigned int index, level = PFN_ORDER(pg), next_level;
+
+    PFN_ORDER(pg) = 0;
 
     if ( level <= 1 )
     {
@@ -599,6 +609,7 @@ const struct iommu_ops amd_iommu_ops = {
     .teardown = amd_iommu_domain_destroy,
     .map_page = amd_iommu_map_page,
     .unmap_page = amd_iommu_unmap_page,
+    .free_page_table = deallocate_page_table,
     .reassign_device = reassign_device,
     .get_device_group_id = amd_iommu_group_id,
     .update_ire_from_apic = amd_iommu_ioapic_update_ire,
xen/drivers/passthrough/iommu.c +35 -16
@@ -58,6 +58,10 @@ bool_t __read_mostly amd_iommu_perdev_in
 
 DEFINE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
 
+DEFINE_SPINLOCK(iommu_pt_cleanup_lock);
+PAGE_LIST_HEAD(iommu_pt_cleanup_list);
+static struct tasklet iommu_pt_cleanup_tasklet;
+
 static struct keyhandler iommu_p2m_table = {
     .diagnostic = 0,
     .u.fn = iommu_dump_p2m_table,
@@ -235,6 +239,15 @@ int iommu_remove_device(struct pci_dev *
     return hd->platform_ops->remove_device(pdev->devfn, pdev);
 }
 
+static void iommu_teardown(struct domain *d)
+{
+    const struct hvm_iommu *hd = domain_hvm_iommu(d);
+
+    d->need_iommu = 0;
+    hd->platform_ops->teardown(d);
+    tasklet_schedule(&iommu_pt_cleanup_tasklet);
+}
+
 /*
  * If the device isn't owned by dom0, it means it already
  * has been assigned to other domain, or it doesn't exist.
@@ -309,10 +322,7 @@ static int assign_device(struct domain *
 
  done:
     if ( !has_arch_pdevs(d) && need_iommu(d) )
-    {
-        d->need_iommu = 0;
-        hd->platform_ops->teardown(d);
-    }
+        iommu_teardown(d);
     spin_unlock(&pcidevs_lock);
 
     return rc;
@@ -377,10 +387,7 @@ static int iommu_populate_page_table(str
     if ( !rc )
         iommu_iotlb_flush_all(d);
     else if ( rc != -ERESTART )
-    {
-        d->need_iommu = 0;
-        hd->platform_ops->teardown(d);
-    }
+        iommu_teardown(d);
 
     return rc;
 }
@@ -397,10 +404,7 @@ void iommu_domain_destroy(struct domain 
         return;
 
     if ( need_iommu(d) )
-    {
-        d->need_iommu = 0;
-        hd->platform_ops->teardown(d);
-    }
+        iommu_teardown(d);
 
     list_for_each_safe ( ioport_list, tmp, &hd->g2m_ioport_list )
     {
@@ -438,6 +442,23 @@ int iommu_unmap_page(struct domain *d, u
     return hd->platform_ops->unmap_page(d, gfn);
 }
 
+static void iommu_free_pagetables(unsigned long unused)
+{
+    do {
+        struct page_info *pg;
+
+        spin_lock(&iommu_pt_cleanup_lock);
+        pg = page_list_remove_head(&iommu_pt_cleanup_list);
+        spin_unlock(&iommu_pt_cleanup_lock);
+        if ( !pg )
+            return;
+        iommu_get_ops()->free_page_table(pg);
+    } while ( !softirq_pending(smp_processor_id()) );
+
+    tasklet_schedule_on_cpu(&iommu_pt_cleanup_tasklet,
+                            cpumask_cycle(smp_processor_id(), &cpu_online_map));
+}
+
 void iommu_iotlb_flush(struct domain *d, unsigned long gfn, unsigned int page_count)
 {
     struct hvm_iommu *hd = domain_hvm_iommu(d);
@@ -500,10 +521,7 @@ int deassign_device(struct domain *d, u1
     pdev->fault.count = 0;
 
     if ( !has_arch_pdevs(d) && need_iommu(d) )
-    {
-        d->need_iommu = 0;
-        hd->platform_ops->teardown(d);
-    }
+        iommu_teardown(d);
 
     return ret;
 }
@@ -542,6 +560,7 @@ int __init iommu_setup(void)
                iommu_passthrough ? "Passthrough" :
                iommu_dom0_strict ? "Strict" : "Relaxed");
         printk("Interrupt remapping %sabled\n", iommu_intremap ? "en" : "dis");
+        tasklet_init(&iommu_pt_cleanup_tasklet, iommu_free_pagetables, 0);
     }
 
     return rc;
xen/drivers/passthrough/vtd/iommu.c +15 -3
@@ -668,13 +668,24 @@ static void dma_pte_clear_one(struct dom
 
 static void iommu_free_pagetable(u64 pt_maddr, int level)
 {
-    int i;
-    struct dma_pte *pt_vaddr, *pte;
-    int next_level = level - 1;
+    struct page_info *pg = maddr_to_page(pt_maddr);
 
     if ( pt_maddr == 0 )
         return;
 
+    PFN_ORDER(pg) = level;
+    spin_lock(&iommu_pt_cleanup_lock);
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void iommu_free_page_table(struct page_info *pg)
+{
+    unsigned int i, next_level = PFN_ORDER(pg) - 1;
+    u64 pt_maddr = page_to_maddr(pg);
+    struct dma_pte *pt_vaddr, *pte;
+
+    PFN_ORDER(pg) = 0;
     pt_vaddr = (struct dma_pte *)map_vtd_domain_page(pt_maddr);
 
     for ( i = 0; i < PTE_NUM; i++ )
@@ -2430,6 +2441,7 @@ const struct iommu_ops intel_iommu_ops =
     .teardown = iommu_domain_teardown,
     .map_page = intel_iommu_map_page,
     .unmap_page = intel_iommu_unmap_page,
+    .free_page_table = iommu_free_page_table,
     .reassign_device = reassign_device_ownership,
     .get_device_group_id = intel_iommu_group_id,
     .update_ire_from_apic = io_apic_write_remap_rte,
xen/include/xen/iommu.h +5 -0
@@ -88,6 +88,7 @@ bool_t pt_irq_need_timer(uint32_t flags)
 
 struct msi_desc;
 struct msi_msg;
+struct page_info;
 
 struct iommu_ops {
     int (*init)(struct domain *d);
@@ -100,6 +101,7 @@ struct iommu_ops {
     int (*map_page)(struct domain *d, unsigned long gfn, unsigned long mfn,
                     unsigned int flags);
     int (*unmap_page)(struct domain *d, unsigned long gfn);
+    void (*free_page_table)(struct page_info *);
     int (*reassign_device)(struct domain *s, struct domain *t,
 			   u8 devfn, struct pci_dev *);
     int (*get_device_group_id)(u16 seg, u8 bus, u8 devfn);
@@ -151,4 +153,7 @@ int adjust_vtd_irq_affinities(void);
  */
 DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb);
 
+extern struct spinlock iommu_pt_cleanup_lock;
+extern struct page_list_head iommu_pt_cleanup_list;
+
 #endif /* _IOMMU_H_ */
Acked-by on v2 2/5
#20 lore raw

Re: [PATCH v2 2/5] IOMMU: make page table deallocation preemptible

In reply to Jan Beulich

Jan, Thanks!

-----Original Message-----
From: Jan Beulich [mailto:[email protected]]
Sent: Friday, December 13, 2013 10:00 PM
To: xen-devel
Cc: [email protected]; Andrew Cooper; George Dunlap; Zhang, Xiantao; Keir Fraser
Subject: [PATCH v2 2/5] IOMMU: make page table deallocation preemptible

This too can take an arbitrary amount of time.

In fact, the bulk of the work is being moved to a tasklet, as handling the necessary preemption logic in line seems close to impossible given that the teardown may also be invoked on error paths.

---
v2: abstract out tasklet logic

xen/drivers/passthrough/amd/pci_amd_iommu.c +13 -2
@@ -405,11 +405,21 @@ static int amd_iommu_assign_device(struc
     return reassign_device(dom0, d, devfn, pdev);  }
 
-static void deallocate_next_page_table(struct page_info* pg, int level)
+static void deallocate_next_page_table(struct page_info *pg, int level) 
+{
+    PFN_ORDER(pg) = level;
+    spin_lock(&iommu_pt_cleanup_lock);
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void deallocate_page_table(struct page_info *pg)
 {
     void *table_vaddr, *pde;
     u64 next_table_maddr;
-    int index, next_level;
+    unsigned int index, level = PFN_ORDER(pg), next_level;
+
+    PFN_ORDER(pg) = 0;
 
     if ( level <= 1 )
     {
@@ -599,6 +609,7 @@ const struct iommu_ops amd_iommu_ops = {
     .teardown = amd_iommu_domain_destroy,
     .map_page = amd_iommu_map_page,
     .unmap_page = amd_iommu_unmap_page,
+    .free_page_table = deallocate_page_table,
     .reassign_device = reassign_device,
     .get_device_group_id = amd_iommu_group_id,
     .update_ire_from_apic = amd_iommu_ioapic_update_ire,
xen/drivers/passthrough/iommu.c +31 -12

78 lines not on this page · raw message

     if ( !has_arch_pdevs(d) && need_iommu(d) )

- {
- d->need_iommu = 0;
- hd->platform_ops->teardown(d);
- }
+ iommu_teardown(d);

     return ret;
 }
@@ -542,6 +560,7 @@ int __init iommu_setup(void)
                iommu_passthrough ? "Passthrough" :
                iommu_dom0_strict ? "Strict" : "Relaxed");
         printk("Interrupt remapping %sabled\n", iommu_intremap ? "en" : "dis");
+        tasklet_init(&iommu_pt_cleanup_tasklet, iommu_free_pagetables, 
+ 0);
     }
 
     return rc;
xen/drivers/passthrough/vtd/iommu.c +14 -3
@@ -668,13 +668,24 @@ static void dma_pte_clear_one(struct dom
 
 static void iommu_free_pagetable(u64 pt_maddr, int level)  {
-    int i;
-    struct dma_pte *pt_vaddr, *pte;
-    int next_level = level - 1;
+    struct page_info *pg = maddr_to_page(pt_maddr);
 
     if ( pt_maddr == 0 )
         return;
 
+    PFN_ORDER(pg) = level;
+    spin_lock(&iommu_pt_cleanup_lock);
+    page_list_add_tail(pg, &iommu_pt_cleanup_list);
+    spin_unlock(&iommu_pt_cleanup_lock);
+}
+
+static void iommu_free_page_table(struct page_info *pg) {
+    unsigned int i, next_level = PFN_ORDER(pg) - 1;
+    u64 pt_maddr = page_to_maddr(pg);
+    struct dma_pte *pt_vaddr, *pte;
+
+    PFN_ORDER(pg) = 0;
     pt_vaddr = (struct dma_pte *)map_vtd_domain_page(pt_maddr);
 
     for ( i = 0; i < PTE_NUM; i++ )
@@ -2430,6 +2441,7 @@ const struct iommu_ops intel_iommu_ops =
     .teardown = iommu_domain_teardown,
     .map_page = intel_iommu_map_page,
     .unmap_page = intel_iommu_unmap_page,
+    .free_page_table = iommu_free_page_table,
     .reassign_device = reassign_device_ownership,
     .get_device_group_id = intel_iommu_group_id,
     .update_ire_from_apic = io_apic_write_remap_rte,
xen/include/xen/iommu.h +2 -0
@@ -88,6 +88,7 @@ bool_t pt_irq_need_timer(uint32_t flags)
 
 struct msi_desc;
 struct msi_msg;
+struct page_info;
 
 struct iommu_ops {
     int (*init)(struct domain *d);
@@ -100,6 +101,7 @@ struct iommu_ops {
     int (*map_page)(struct domain *d, unsigned long gfn, unsigned long mfn,
                     unsigned int flags);
     int (*unmap_page)(struct domain *d, unsigned long gfn);
+    void (*free_page_table)(struct page_info *);
     int (*reassign_device)(struct domain *s, struct domain *t,
 			   u8 devfn, struct pci_dev *);
     int (*get_device_group_id)(u16 seg, u8 bus, u8 devfn); @@ -151,4 +153,7 @@ int adjust_vtd_irq_affinities(void);

*/
DECLARE_PER_CPU(bool_t, iommu_dont_flush_iotlb);

+extern struct spinlock iommu_pt_cleanup_lock; extern struct +page_list_head iommu_pt_cleanup_list;
+
#endif /* _IOMMU_H_ */

#21 lore raw

PATCH 3/5 x86/p2m: restrict auditing to debug builds

In reply to Jan Beulich

... since iterating through all of a guest's pages may take unduly long.

xen/include/asm-x86/p2m.h +4 -0
@@ -600,7 +600,11 @@ int set_p2m_entry(struct p2m_domain *p2m
 extern void p2m_pt_init(struct p2m_domain *p2m);
 
 /* Debugging and auditing of the P2M code? */
+#ifndef NDEBUG
 #define P2M_AUDIT     1
+#else
+#define P2M_AUDIT     0
+#endif
 #define P2M_DEBUGGING 0
 
 #if P2M_AUDIT

x86/p2m: restrict auditing to debug builds

... since iterating through all of a guest's pages may take unduly long.

xen/include/asm-x86/p2m.h +4 -0
@@ -600,7 +600,11 @@ int set_p2m_entry(struct p2m_domain *p2m
 extern void p2m_pt_init(struct p2m_domain *p2m);
 
 /* Debugging and auditing of the P2M code? */
+#ifndef NDEBUG
 #define P2M_AUDIT     1
+#else
+#define P2M_AUDIT     0
+#endif
 #define P2M_DEBUGGING 0
 
 #if P2M_AUDIT

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

Reviewed-by on v1 3/5
#22 lore raw

Re: [PATCH 3/5] x86/p2m: restrict auditing to debug builds

Quoted 5 lines · Jan Beulich

... since iterating through all of a guest's pages may take unduly long.

Quoted 20 lines
xen/include/asm-x86/p2m.h +4 -0
@@ -600,7 +600,11 @@ int set_p2m_entry(struct p2m_domain *p2m
 extern void p2m_pt_init(struct p2m_domain *p2m);
 
 /* Debugging and auditing of the P2M code? */
+#ifndef NDEBUG
 #define P2M_AUDIT     1
+#else
+#define P2M_AUDIT     0
+#endif
 #define P2M_DEBUGGING 0
 
 #if P2M_AUDIT
Quoted 4 lines

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

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

#25 lore raw

PATCH 4/5 HVM: prevent leaking heap data from hvm_save_one()

In reply to Jan Beulich

When one or more of the vCPU-s of a guest are offline, no data may be put into the allocated space for them and, due to another bug, such uninitialized data may be passed back to the caller.

xen/common/hvm/save.c +1 -1
@@ -102,7 +102,7 @@ int hvm_save_one(struct domain *d, uint1
         return -EINVAL;
 
     ctxt.size = sz;
-    ctxt.data = xmalloc_bytes(sz);
+    ctxt.data = xzalloc_bytes(sz);
     if ( !ctxt.data )
         return -ENOMEM;
Reviewed-by on v1 4/5
#26 lore raw

Re: [PATCH 4/5] HVM: prevent leaking heap data from hvm_save_one()

Quoted 7 lines · Jan Beulich

When one or more of the vCPU-s of a guest are offline, no data may be put into the allocated space for them and, due to another bug, such uninitialized data may be passed back to the caller.

Quoted 20 lines
xen/common/hvm/save.c +1 -1
@@ -102,7 +102,7 @@ int hvm_save_one(struct domain *d, uint1
         return -EINVAL;
 
     ctxt.size = sz;
-    ctxt.data = xmalloc_bytes(sz);
+    ctxt.data = xzalloc_bytes(sz);
     if ( !ctxt.data )
         return -ENOMEM;
 

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

#27 lore raw

Re: [PATCH 4/5] HVM: prevent leaking heap data from hvm_save_one()

In reply to Jan Beulich
Quoted 7 lines · Jan Beulich

When one or more of the vCPU-s of a guest are offline, no data may be put into the allocated space for them and, due to another bug, such uninitialized data may be passed back to the caller.

Quoted 15 lines
xen/common/hvm/save.c +1 -1
@@ -102,7 +102,7 @@ int hvm_save_one(struct domain *d, uint1
          return -EINVAL;
 
      ctxt.size = sz;
-    ctxt.data = xmalloc_bytes(sz);
+    ctxt.data = xzalloc_bytes(sz);
      if ( !ctxt.data )
          return -ENOMEM;
 
#28 lore raw

Ping: [PATCH 4/5] HVM: prevent leaking heap data from hvm_save_one()

In reply to Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Jan Beulich

When one or more of the vCPU-s of a guest are offline, no data may be put into the allocated space for them and, due to another bug, such uninitialized data may be passed back to the caller.

Keir?

Quoted 11 lines
xen/common/hvm/save.c +1 -1
@@ -102,7 +102,7 @@ int hvm_save_one(struct domain *d, uint1
         return -EINVAL;
 
     ctxt.size = sz;
-    ctxt.data = xmalloc_bytes(sz);
+    ctxt.data = xzalloc_bytes(sz);
     if ( !ctxt.data )
         return -ENOMEM;
 
#29 lore raw

Re: Ping: [PATCH 4/5] HVM: prevent leaking heap data from hvm_save_one()

Quoted 9 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Jan Beulich

When one or more of the vCPU-s of a guest are offline, no data may be put into the allocated space for them and, due to another bug, such uninitialized data may be passed back to the caller.

Keir?

This issue is completely fixed by the latest patch is it not?

With the latest patch, we always copy out of the written subset of ctxt.data, even if ctxt.size is larger.

~Andrew

Quoted 18 lines

xen/common/hvm/save.c int hvm_save_one(struct domain *d, uint1 · line 108

Quoted 12 lines
xen/common/hvm/save.c +1 -1
@@ -102,7 +102,7 @@ int hvm_save_one(struct domain *d, uint1
         return -EINVAL;
 
     ctxt.size = sz;
-    ctxt.data = xmalloc_bytes(sz);
+    ctxt.data = xzalloc_bytes(sz);
     if ( !ctxt.data )
         return -ENOMEM;
 

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

#30 lore raw

Re: Ping: [PATCH 4/5] HVM: prevent leaking heap data from hvm_save_one()

Quoted 10 lines
Quoted 1 line
Quoted 1 line · Andrew Cooper
Quoted 8 lines · Jan Beulich
Quoted 8 lines
Quoted 1 line
Quoted 1 line · Jan Beulich

When one or more of the vCPU-s of a guest are offline, no data may be put into the allocated space for them and, due to another bug, such uninitialized data may be passed back to the caller.

Quoted 6 lines
Quoted 1 line

Keir?

This issue is completely fixed by the latest patch is it not?

With the latest patch, we always copy out of the written subset of ctxt.data, even if ctxt.size is larger.

Indeed, the patch here shouldn't be needed anymore with the latest version of Don's.

Jan

#31 lore raw

PATCH 5/5 x86/PV: don't commit debug register values early in arch_set_info_guest()

In reply to Jan Beulich

They're being taken care of later (via set_debugreg()), and temporarily copying them into struct vcpu means that bad values may end up getting loaded during context switch if the vCPU is already running and the function errors out between the premature and real commit step, leading to the same issue that XSA-12 dealt with.

xen/arch/x86/domain.c +3 -2
@@ -740,11 +740,12 @@ int arch_set_info_guest(
             XLAT_trap_info(v->arch.pv_vcpu.trap_ctxt + i,
                            c.cmp->trap_ctxt + i);
     }
-    for ( i = 0; i < ARRAY_SIZE(v->arch.debugreg); ++i )
-        v->arch.debugreg[i] = c(debugreg[i]);
 
     if ( has_hvm_container_vcpu(v) )
     {
+        for ( i = 0; i < ARRAY_SIZE(v->arch.debugreg); ++i )
+            v->arch.debugreg[i] = c(debugreg[i]);
+
         /*
          * NB: TF_kernel_mode is set unconditionally for HVM guests,
          * so we always use the gs_base_kernel here. If we change this

x86/PV: don't commit debug register values early in arch_set_info_guest()

They're being taken care of later (via set_debugreg()), and temporarily copying them into struct vcpu means that bad values may end up getting loaded during context switch if the vCPU is already running and the function errors out between the premature and real commit step, leading to the same issue that XSA-12 dealt with.

xen/arch/x86/domain.c +3 -2
@@ -740,11 +740,12 @@ int arch_set_info_guest(
             XLAT_trap_info(v->arch.pv_vcpu.trap_ctxt + i,
                            c.cmp->trap_ctxt + i);
     }
-    for ( i = 0; i < ARRAY_SIZE(v->arch.debugreg); ++i )
-        v->arch.debugreg[i] = c(debugreg[i]);
 
     if ( has_hvm_container_vcpu(v) )
     {
+        for ( i = 0; i < ARRAY_SIZE(v->arch.debugreg); ++i )
+            v->arch.debugreg[i] = c(debugreg[i]);
+
         /*
          * NB: TF_kernel_mode is set unconditionally for HVM guests,
          * so we always use the gs_base_kernel here. If we change this

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

Reviewed-by on v1 5/5
#32 lore raw

Re: [PATCH 5/5] x86/PV: don't commit debug register values early in arch_set_info_guest()

Quoted 8 lines · Jan Beulich

They're being taken care of later (via set_debugreg()), and temporarily copying them into struct vcpu means that bad values may end up getting loaded during context switch if the vCPU is already running and the function errors out between the premature and real commit step, leading to the same issue that XSA-12 dealt with.

Quoted 23 lines
xen/arch/x86/domain.c +3 -2
@@ -740,11 +740,12 @@ int arch_set_info_guest(
             XLAT_trap_info(v->arch.pv_vcpu.trap_ctxt + i,
                            c.cmp->trap_ctxt + i);
     }
-    for ( i = 0; i < ARRAY_SIZE(v->arch.debugreg); ++i )
-        v->arch.debugreg[i] = c(debugreg[i]);
 
     if ( has_hvm_container_vcpu(v) )
     {
+        for ( i = 0; i < ARRAY_SIZE(v->arch.debugreg); ++i )
+            v->arch.debugreg[i] = c(debugreg[i]);
+
         /*
          * NB: TF_kernel_mode is set unconditionally for HVM guests,
          * so we always use the gs_base_kernel here. If we change this
Quoted 4 lines

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

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

#33 lore raw

Re: [PATCH 5/5] x86/PV: don't commit debug register values early in arch_set_info_guest()

In reply to Jan Beulich
Quoted 8 lines · Jan Beulich

They're being taken care of later (via set_debugreg()), and temporarily copying them into struct vcpu means that bad values may end up getting loaded during context switch if the vCPU is already running and the function errors out between the premature and real commit step, leading to the same issue that XSA-12 dealt with.

Quoted 21 lines
xen/arch/x86/domain.c +3 -2
@@ -740,11 +740,12 @@ int arch_set_info_guest(
              XLAT_trap_info(v->arch.pv_vcpu.trap_ctxt + i,
                             c.cmp->trap_ctxt + i);
      }
-    for ( i = 0; i < ARRAY_SIZE(v->arch.debugreg); ++i )
-        v->arch.debugreg[i] = c(debugreg[i]);
 
      if ( has_hvm_container_vcpu(v) )
      {
+        for ( i = 0; i < ARRAY_SIZE(v->arch.debugreg); ++i )
+            v->arch.debugreg[i] = c(debugreg[i]);
+
          /*
           * NB: TF_kernel_mode is set unconditionally for HVM guests,
           * so we always use the gs_base_kernel here. If we change this
Acked-by
#34 lore raw

Re: [PATCH 5/5] x86/PV: don't commit debug register values early in arch_set_info_guest()

Quoted 11 lines · George Dunlap
Quoted 8 lines · Jan Beulich

They're being taken care of later (via set_debugreg()), and temporarily copying them into struct vcpu means that bad values may end up getting loaded during context switch if the vCPU is already running and the function errors out between the premature and real commit step, leading to the same issue that XSA-12 dealt with.

Quoted 22 lines
Quoted 21 lines
xen/arch/x86/domain.c +3 -2
@@ -740,11 +740,12 @@ int arch_set_info_guest(
              XLAT_trap_info(v->arch.pv_vcpu.trap_ctxt + i,
                             c.cmp->trap_ctxt + i);
      }
-    for ( i = 0; i < ARRAY_SIZE(v->arch.debugreg); ++i )
-        v->arch.debugreg[i] = c(debugreg[i]);
 
      if ( has_hvm_container_vcpu(v) )
      {
+        for ( i = 0; i < ARRAY_SIZE(v->arch.debugreg); ++i )
+            v->arch.debugreg[i] = c(debugreg[i]);
+
          /*
           * NB: TF_kernel_mode is set unconditionally for HVM guests,
           * so we always use the gs_base_kernel here. If we change this