Skip to contentSkip to conversation

VMX: avoid taking locks with irqs disabled

PATCH In review Tim Deegan 3 participants 7 messages Oct 20, 2008 – Oct 21, 2008

lore
Share & tools
lore

Tim Deegan

author
#0 lore raw

PATCH 1/1 VMX: avoid taking locks with irqs disabled

Shuffle the bits of the vmexit handler that run with EFLAGS.IF == 0 up to the top. Otherwise we end up calling spin_lock() with interrupts disabled, which can deadlock against the time-synchronization rendezvous code.

xen/arch/x86/hvm/vmx/vmx.c +7 -3
diff -r 4129f0f2f2ba xen/arch/x86/hvm/vmx/vmx.c
@@ -2056,8 +2056,12 @@ asmlinkage void vmx_vmexit_handler(struc
 
     perfc_incra(vmexits, exit_reason);
 
-    if ( exit_reason != EXIT_REASON_EXTERNAL_INTERRUPT )
-        local_irq_enable();
+    /* Handle the interrupt we missed before allowing any more in */
+    if ( exit_reason == EXIT_REASON_EXTERNAL_INTERRUPT )
+        vmx_do_extint(regs);
+
+    /* Now enable interrupts so it's safe to take locks. */
+    local_irq_enable();
 
     if ( unlikely(exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) )
         return vmx_failed_vmentry(exit_reason, regs);
@@ -2185,7 +2189,7 @@ asmlinkage void vmx_vmexit_handler(struc
         break;
     }
     case EXIT_REASON_EXTERNAL_INTERRUPT:
-        vmx_do_extint(regs);
+        /* Already handled above */
         break;
     case EXIT_REASON_TRIPLE_FAULT:
         hvm_triple_fault();

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

#1 lore raw

Oh yes. That's a class of race that I hadn't realised would be introduced by the new rendezvous code. Spinlocks which are ever taken with IRQs disabled must *always* be taken with IRQs disabled. Spinlocks which are ever taken with IRQs enabled must *always* be taken with IRQs enabled.

It's quite an extra constraint on spinlock usage actually. :-(

Actually this new partitioning of locks into two equivalence classes is begging for some run-time checking in debug builds. I'll sort out a patch for that.

-- Keir

Quoted 10 lines · Tim Deegan

Shuffle the bits of the vmexit handler that run with EFLAGS.IF == 0 up to the top. Otherwise we end up calling spin_lock() with interrupts disabled, which can deadlock against the time-synchronization rendezvous code.

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

#2 lore raw
Quoted 10 lines

From: Keir Fraser
Sent: Monday, October 20, 2008 6:09 PM

Oh yes. That's a class of race that I hadn't realised would be introduced by
the new rendezvous code. Spinlocks which are ever taken with IRQs disabled
must *always* be taken with IRQs disabled. Spinlocks which are ever taken
with IRQs enabled must *always* be taken with IRQs enabled.

We encountered a similar bug today, when Jimmy is developing FSB HPET broadcast feature. The reason is same that above guideline is not obeyed.

Quoted 8 lines

It's quite an extra constraint on spinlock usage actually. :-(

Actually this new partitioning of locks into two equivalence classes is begging for some run-time checking in debug builds. I'll sort out a patch
for that.

Did you flush out the patch into xen bits? I guess there may be other instances breaking that guideline. Just curious whether Linux side already recognized similar constraints. It's possibly not. Then another angle is, instead of pushing constraint on spinlock usage, could we instead change timer rendezvous style? Use softirq instead of call function should release the constraints like stop_machine. :-)

Thanks,
Kevin

#3 lore raw
Quoted 5 lines · Kevin
Quoted 4 lines

Actually this new partitioning of locks into two equivalence classes is begging for some run-time checking in debug builds. I'll sort out a patch
for that.

Quoted 7 lines

Did you flush out the patch into xen bits? I guess there may be other instances breaking that guideline. Just curious whether Linux side already recognized similar constraints. It's possibly not. Then another angle is, instead of pushing constraint on spinlock usage, could we instead change timer rendezvous style? Use softirq instead of call function should release the constraints like stop_machine. :-)

I'm flushing out fixes for this class of race before I check in the debug code. I don't think it's an unreasonable new constraint, it just hasn't been enforced before and so various Xen code breaks. I should get the patches flushed out later today.

-- Keir

#4 lore raw
Quoted 18 lines

From: Keir Fraser [mailto:[email protected]]
Sent: Tuesday, October 21, 2008 8:37 PM

Quoted 1 line · Kevin
Quoted 1 line

Actually this new partitioning of locks into two

equivalence classes is

Quoted 11 lines
Quoted 3 lines

begging for some run-time checking in debug builds. I'll sort out a patch
for that.

Did you flush out the patch into xen bits? I guess there may be other instances breaking that guideline. Just curious whether Linux side already recognized similar constraints. It's possibly not. Then another angle is, instead of pushing constraint on spinlock usage, could we instead change timer rendezvous style? Use softirq instead of call function should release the constraints like stop_machine. :-)

Quoted 7 lines

I'm flushing out fixes for this class of race before I check in the debug
code. I don't think it's an unreasonable new constraint, it just hasn't been
enforced before and so various Xen code breaks. I should get the patches
flushed out later today.

I'm a bit curious why call funtion ipi is required here, or why rendezvous is required here. All the rendezvous stuff in current ipi function is just:
a) cpu0 waits for all other cpus entering rendezvous loop, and then update master_stime
b) other cpus enter loop and wait for cpu0 to update master_stime

Then each cpu continues with rest stuff independently. In this case, it seems enough to just ensure master_stime updated before sending softirq, and thus ipi is actually not required. Do I miss anything? :-)

Thanks,
Kevin

#5 lore raw

BTW, I just meant for time rendezvous code, not for what you're adding to spinlock which seems safer to avoid similar dead lock when real rendezvous usage is introduced in future.

Thanks,
Kevin

Quoted 49 lines

From: Tian, Kevin
Sent: Tuesday, October 21, 2008 8:50 PM

Quoted 18 lines

From: Keir Fraser [mailto:[email protected]]
Sent: Tuesday, October 21, 2008 8:37 PM

Quoted 1 line · Kevin
Quoted 1 line

Actually this new partitioning of locks into two

equivalence classes is

Quoted 11 lines
Quoted 3 lines

begging for some run-time checking in debug builds. I'll sort out a patch
for that.

Did you flush out the patch into xen bits? I guess there may be other instances breaking that guideline. Just curious whether Linux side already recognized similar constraints. It's possibly not. Then another angle is, instead of pushing constraint on spinlock usage, could we instead change timer rendezvous style? Use softirq instead of call function should release the constraints like stop_machine. :-)

Quoted 7 lines

I'm flushing out fixes for this class of race before I check in the debug
code. I don't think it's an unreasonable new constraint, it just hasn't been
enforced before and so various Xen code breaks. I should get the patches
flushed out later today.

I'm a bit curious why call funtion ipi is required here, or why rendezvous is required here. All the rendezvous stuff in current ipi function is just:
a) cpu0 waits for all other cpus entering rendezvous loop, and then update master_stime
b) other cpus enter loop and wait for cpu0 to update master_stime

Then each cpu continues with rest stuff independently. In this case, it seems enough to just ensure master_stime updated before sending softirq, and thus ipi is actually not required. Do I miss anything? :-)

Thanks,
Kevin

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

#6 lore raw
In reply to Tian, Kevin
Quoted 8 lines · Kevin

I'm a bit curious why call funtion ipi is required here, or why rendezvous is required here. All the rendezvous stuff in current ipi function is just:
a) cpu0 waits for all other cpus entering rendezvous loop, and then update master_stime
b) other cpus enter loop and wait for cpu0 to update master_stime

Quoted 4 lines

Then each cpu continues with rest stuff independently. In this case, it seems enough to just ensure master_stime updated before sending softirq, and thus ipi is actually not required. Do I miss anything? :-)

We want to gather all timestamps as close together as possible. Dan measured that this produced vastly less system-time skew across CPUs. Hence we do all the stamp gathering in IRQ context.

-- Keir