Skip to contentSkip to conversation

VT-d: always clean up dpci timers.

RFC PATCH In review Tim Deegan 4 participants 8 messages Jul 18, 2011 – Jul 25, 2011

lore
Share & tools
lore

Tim Deegan

author
#0 lore raw

RFC PATCH 1/1 VT-d: always clean up dpci timers.

If a VM has all its PCI devices deassigned, need_iommu(d) becomes false but it might still have DPCI EOI timers that were init_timer()d but not yet kill_timer()d. That causes xen to crash later because the linked list of inactive timers gets corrupted, e.g.:

(XEN) Xen call trace:

(XEN)    [<ffff82c480126256>] set_timer+0x1c2/0x24f
(XEN)    [<ffff82c48011fbf8>] schedule+0x129/0x5dd
(XEN)    [<ffff82c480122c1e>] __do_softirq+0x7e/0x89
(XEN)    [<ffff82c480122c9d>] do_softirq+0x26/0x28
(XEN)    [<ffff82c480153c85>] idle_loop+0x5a/0x5c

(XEN)
(XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) Assertion 'entry->next->prev == entry' failed at /local/scratch/tdeegan/xen-unstable.hg/xen/include:172 (XEN) ****************************************

The following patch makes sure that the domain destruction path always clears up the DPCI state even if !needs_iommu(d).

Although it fixes the crash for me, I'm sufficiently confused by this code that I don't know whether it's enough. If the dpci timer state gets freed earlier than pci_clean_dpci_irqs() then there's still a race, and some other function (reassign_device_ownership() ?) needs to sort out the timers when the PCI card is deassigned.

Allen, can you comment?

xen/drivers/passthrough/pci.c +1 -1
diff -r ab6551e30841 xen/drivers/passthrough/pci.c
@@ -269,7 +269,7 @@ static void pci_clean_dpci_irqs(struct d
     if ( !iommu_enabled )
         return;
 
-    if ( !is_hvm_domain(d) || !need_iommu(d) )
+    if ( !is_hvm_domain(d) )
         return;
 
     spin_lock(&d->event_lock);
#1 lore raw

Hi Tim,

Can you provide the code flow that can cause this failure?

In pci_release_devices(), pci_clean_dpci_irqs() is called before "d->need_iommu = 0" in deassign_device(). If this path is taken, then it should not return from !need_iommu(d) in pci_clean_dpci_irqs().

Allen

-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Monday, July 18, 2011 9:39 AM
To: [email protected]
Cc: [email protected]; Kay, Allen M
Subject: [PATCH] [RFC] VT-d: always clean up dpci timers.

If a VM has all its PCI devices deassigned, need_iommu(d) becomes false but it might still have DPCI EOI timers that were init_timer()d but not yet kill_timer()d. That causes xen to crash later because the linked list of inactive timers gets corrupted, e.g.:

(XEN) Xen call trace:

(XEN)    [<ffff82c480126256>] set_timer+0x1c2/0x24f
(XEN)    [<ffff82c48011fbf8>] schedule+0x129/0x5dd
(XEN)    [<ffff82c480122c1e>] __do_softirq+0x7e/0x89
(XEN)    [<ffff82c480122c9d>] do_softirq+0x26/0x28
(XEN)    [<ffff82c480153c85>] idle_loop+0x5a/0x5c

(XEN)
(XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) Assertion 'entry->next->prev == entry' failed at /local/scratch/tdeegan/xen-unstable.hg/xen/include:172 (XEN) ****************************************

The following patch makes sure that the domain destruction path always clears up the DPCI state even if !needs_iommu(d).

Although it fixes the crash for me, I'm sufficiently confused by this code that I don't know whether it's enough. If the dpci timer state gets freed earlier than pci_clean_dpci_irqs() then there's still a race, and some other function (reassign_device_ownership() ?) needs to sort out the timers when the PCI card is deassigned.

Allen, can you comment?

xen/drivers/passthrough/pci.c +1 -1
diff -r ab6551e30841 xen/drivers/passthrough/pci.c
@@ -269,7 +269,7 @@ static void pci_clean_dpci_irqs(struct d
     if ( !iommu_enabled )
         return;
 
-    if ( !is_hvm_domain(d) || !need_iommu(d) )
+    if ( !is_hvm_domain(d) )
         return;
 
     spin_lock(&d->event_lock);
author
#2 lore raw
Quoted 7 lines · Allen M

Hi Tim,

Can you provide the code flow that can cause this failure?

In pci_release_devices(), pci_clean_dpci_irqs() is called before "d->need_iommu = 0" in deassign_device(). If this path is taken, then it should not return from !need_iommu(d) in pci_clean_dpci_irqs().

The problem is that the xl toolstack has already deassigned the domain's devices, using a hypercall to invoke deassign_device(), so by the time the domain is destroyed, pci_release_devices() can't tell that it once had a PCI device passed through.

It seems like the Right Thing[tm] would be for deassign_device() to find and undo the relevant IRQ plumbing but I couldn't see how. Is there a mapping from bdf to irq in the iommu code or are they handled entirely separately?

Tim.

Quoted 57 lines

Allen

-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Monday, July 18, 2011 9:39 AM
To: [email protected]
Cc: [email protected]; Kay, Allen M
Subject: [PATCH] [RFC] VT-d: always clean up dpci timers.

If a VM has all its PCI devices deassigned, need_iommu(d) becomes false but it might still have DPCI EOI timers that were init_timer()d but not yet kill_timer()d. That causes xen to crash later because the linked list of inactive timers gets corrupted, e.g.:

(XEN) Xen call trace:

(XEN)    [<ffff82c480126256>] set_timer+0x1c2/0x24f
(XEN)    [<ffff82c48011fbf8>] schedule+0x129/0x5dd
(XEN)    [<ffff82c480122c1e>] __do_softirq+0x7e/0x89
(XEN)    [<ffff82c480122c9d>] do_softirq+0x26/0x28
(XEN)    [<ffff82c480153c85>] idle_loop+0x5a/0x5c

(XEN)
(XEN)
(XEN) ****************************************
(XEN) Panic on CPU 0:
(XEN) Assertion 'entry->next->prev == entry' failed at /local/scratch/tdeegan/xen-unstable.hg/xen/include:172 (XEN) ****************************************

The following patch makes sure that the domain destruction path always clears up the DPCI state even if !needs_iommu(d).

Although it fixes the crash for me, I'm sufficiently confused by this code that I don't know whether it's enough. If the dpci timer state gets freed earlier than pci_clean_dpci_irqs() then there's still a race, and some other function (reassign_device_ownership() ?) needs to sort out the timers when the PCI card is deassigned.

Allen, can you comment?

diff -r ab6551e30841 xen/drivers/passthrough/pci.c

xen/drivers/passthrough/pci.c +1 -1
@@ -269,7 +269,7 @@ static void pci_clean_dpci_irqs(struct d
     if ( !iommu_enabled )
         return;
 
-    if ( !is_hvm_domain(d) || !need_iommu(d) )
+    if ( !is_hvm_domain(d) )
         return;
 
     spin_lock(&d->event_lock);

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

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Xen Platform Team
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)
#3 lore raw
Quoted 14 lines · Tim Deegan
Quoted 7 lines · Allen M

Hi Tim,

Can you provide the code flow that can cause this failure?

In pci_release_devices(), pci_clean_dpci_irqs() is called before "d->need_iommu = 0" in deassign_device(). If this path is taken, then it should not return from !need_iommu(d) in pci_clean_dpci_irqs().

The problem is that the xl toolstack has already deassigned the domain's devices, using a hypercall to invoke deassign_device(), so by the time the domain is destroyed, pci_release_devices() can't tell that it once had a PCI device passed through.

Quoted 4 lines

It seems like the Right Thing[tm] would be for deassign_device() to find and undo the relevant IRQ plumbing but I couldn't see how. Is there a mapping from bdf to irq in the iommu code or are they handled entirely separately?

Could we make need_iommu(d) sticky? Being able to clear it doesn't seem an important case (such a domain is probably being torn down anyway) and clearly it can lead to fragility. The fact that presumably we'd end up doing unnecessary IOMMU PT work for the remaining lifetime of the domain doesn't seem a major downside to me.

-- Keir

Quoted 60 lines

Tim.

Quoted 58 lines

Allen

-----Original Message-----
From: Tim Deegan [mailto:[email protected]]
Sent: Monday, July 18, 2011 9:39 AM
To: [email protected]
Cc: [email protected]; Kay, Allen M
Subject: [PATCH] [RFC] VT-d: always clean up dpci timers.

If a VM has all its PCI devices deassigned, need_iommu(d) becomes false but it might still have DPCI EOI timers that were init_timer()d but not yet kill_timer()d. That causes xen to crash later because the linked list of inactive timers gets corrupted, e.g.:

(XEN) Xen call trace:

(XEN)    [<ffff82c480126256>] set_timer+0x1c2/0x24f
(XEN)    [<ffff82c48011fbf8>] schedule+0x129/0x5dd
(XEN)    [<ffff82c480122c1e>] __do_softirq+0x7e/0x89
(XEN)    [<ffff82c480122c9d>] do_softirq+0x26/0x28
(XEN)    [<ffff82c480153c85>] idle_loop+0x5a/0x5c

(XEN)
(XEN)
(XEN) **************************************** (XEN) Panic on CPU 0:
(XEN) Assertion 'entry->next->prev == entry' failed at /local/scratch/tdeegan/xen-unstable.hg/xen/include:172 (XEN) ****************************************

The following patch makes sure that the domain destruction path always clears up the DPCI state even if !needs_iommu(d).

Although it fixes the crash for me, I'm sufficiently confused by this code that I don't know whether it's enough. If the dpci timer state gets freed earlier than pci_clean_dpci_irqs() then there's still a race, and some other function (reassign_device_ownership() ?) needs to sort out the timers when the PCI card is deassigned.

Allen, can you comment?

diff -r ab6551e30841 xen/drivers/passthrough/pci.c

xen/drivers/passthrough/pci.c Mon Jul 18 17:22:48 2011 +0100 renamed from xen/drivers/passthrough/pci.c Mon Jul 18 10:59:44 2011 +0100 +1 -1
@@ -269,7 +269,7 @@ static void pci_clean_dpci_irqs(struct d
     if ( !iommu_enabled )
         return;
 
-    if ( !is_hvm_domain(d) || !need_iommu(d) )
+    if ( !is_hvm_domain(d) )
         return;
 
     spin_lock(&d->event_lock);

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

#7 lore raw
In reply to Tim Deegan
Quoted 2 lines

Is there a mapping from bdf to irq in the iommu code or are they handled entirely separately?

I think they are handled separately inside of xen. Irq stuff is mostly about mirq to girq mapping. There should be BDF to irq correlation in user mode code.

Allen

author
#4 lore raw
In reply to Keir Fraser
Quoted 21 lines · Keir Fraser
Quoted 14 lines · Tim Deegan
Quoted 7 lines · Allen M

Hi Tim,

Can you provide the code flow that can cause this failure?

In pci_release_devices(), pci_clean_dpci_irqs() is called before "d->need_iommu = 0" in deassign_device(). If this path is taken, then it should not return from !need_iommu(d) in pci_clean_dpci_irqs().

The problem is that the xl toolstack has already deassigned the domain's devices, using a hypercall to invoke deassign_device(), so by the time the domain is destroyed, pci_release_devices() can't tell that it once had a PCI device passed through.

Quoted 4 lines

It seems like the Right Thing[tm] would be for deassign_device() to find and undo the relevant IRQ plumbing but I couldn't see how. Is there a mapping from bdf to irq in the iommu code or are they handled entirely separately?

Quoted 5 lines

Could we make need_iommu(d) sticky? Being able to clear it doesn't seem an important case (such a domain is probably being torn down anyway) and clearly it can lead to fragility. The fact that presumably we'd end up doing unnecessary IOMMU PT work for the remaining lifetime of the domain doesn't seem a major downside to me.

If you prefer it that way. TBH I think I prefer the other way though:
things that gate on need_iommu() should be cleaned up by iommu->teardown().

--8<----

PCI passthrough: don't tear down IOMMU when the last device is deassigned.

Otherwise there's a risk that not all iommu-related state will get torn down during domain destruction.

xen/drivers/passthrough/iommu.c +0 -6
diff -r 9dbbf1631193 xen/drivers/passthrough/iommu.c
@@ -296,12 +296,6 @@ int deassign_device(struct domain *d, u8
         return ret;
     }
 
-    if ( !has_arch_pdevs(d) && need_iommu(d) )
-    {
-        d->need_iommu = 0;
-        hd->platform_ops->teardown(d);
-    }
-
     return ret;
 }
#5 lore raw
Quoted 9 lines · Tim Deegan
Quoted 5 lines

Could we make need_iommu(d) sticky? Being able to clear it doesn't seem an important case (such a domain is probably being torn down anyway) and clearly it can lead to fragility. The fact that presumably we'd end up doing unnecessary IOMMU PT work for the remaining lifetime of the domain doesn't seem a major downside to me.

If you prefer it that way. TBH I think I prefer the other way though:
things that gate on need_iommu() should be cleaned up by iommu->teardown().

Is your original patch still proposed as a valid alternative to this one?

-- Keir

author
#6 lore raw
Quoted 8 lines · Keir Fraser
Quoted 6 lines · Tim Deegan
Quoted 5 lines

Could we make need_iommu(d) sticky? Being able to clear it doesn't seem an important case (such a domain is probably being torn down anyway) and clearly it can lead to fragility. The fact that presumably we'd end up doing unnecessary IOMMU PT work for the remaining lifetime of the domain doesn't seem a major downside to me.

Quoted 5 lines
Quoted 3 lines

If you prefer it that way. TBH I think I prefer the other way though:
things that gate on need_iommu() should be cleaned up by iommu->teardown().

Is your original patch still proposed as a valid alternative to this one?

Yes; I prefer the original.

Tim.

Signature · 3 lines
Tim Deegan <[email protected]>
Principal Software Engineer, Xen Platform Team
Citrix Systems UK Ltd.  (Company #02937203, SL9 0BG)