Skip to contentSkip to conversation

xen: credit1: fix a race when picking initial pCPU for a vCPU

PATCH Superseded Dario Faggioli 2 participants 4 messages Aug 12, 2016

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen: credit1: fix a race when picking initial pCPU for a vCPU

In the Credit1 hunk of 9f358ddd69463 ("xen: Have schedulers revise initial placement") csched_cpu_pick() is called without taking the runqueue lock of the (temporary) pCPU that the vCPU has been assigned to (e.g., in XEN_DOMCTL_max_vcpus).

However, although 'hidden' in the IS_RUNQ_IDLE() macro, that function does access the runq (for doing load balancing calculations), and hence the appropriate lock must be taken.

Races have been observed, in the form of IS_RUNQ_IDLE() falling over LIST_POISON.

---
Cc: George Dunlap <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Jan Beulich <[email protected]>

---
 xen/common/sched_credit.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
xen/common/sched_credit.c +5 -1
index 220ff0d..b6f82e8 100644
@@ -998,9 +998,13 @@ csched_vcpu_insert(const struct scheduler *ops, struct vcpu *vc)
 
     BUG_ON( is_idle_vcpu(vc) );
 
-    /* This is safe because vc isn't yet being scheduled */
+    /* csched_cpu_pick() looks in vc->processor's runq, so we need the lock. */
+    lock = vcpu_schedule_lock_irq(vc);
+
     vc->processor = csched_cpu_pick(ops, vc);
 
+    spin_unlock_irq(lock);
+
     lock = vcpu_schedule_lock_irq(vc);
 
     if ( !__vcpu_on_runq(svc) && vcpu_runnable(vc) && !vc->is_running )

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

Acked-by on v1 1/1
#1 lore raw
Quoted 11 lines · Dario Faggioli

In the Credit1 hunk of 9f358ddd69463 ("xen: Have schedulers revise initial placement") csched_cpu_pick() is called without taking the runqueue lock of the (temporary) pCPU that the vCPU has been assigned to (e.g., in XEN_DOMCTL_max_vcpus).

However, although 'hidden' in the IS_RUNQ_IDLE() macro, that function does access the runq (for doing load balancing calculations), and hence the appropriate lock must be taken.

Quoted 5 lines

Races have been observed, in the form of IS_RUNQ_IDLE() falling over LIST_POISON.

It might be nice if we could add an ASSERT() that the appropriate runqueue was locked, to make sure we don't get caught out again like this in the future, but I think that would probably require turning it into a static inline (which probably wouldn't be so bad anyway).

But in any case:

Let me know if you want me to check this in as-is or if you think you might send a follow-up patch adding an ASSERT.

-George

Quoted 18 lines

---
Cc: George Dunlap <[email protected]>
Cc: Andrew Cooper <[email protected]>
Cc: Jan Beulich <[email protected]>

---
 xen/common/sched_credit.c |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
xen/common/sched_credit.c +0 -1
index 220ff0d..b6f82e8 100644
@@ -998,9 +998,13 @@ csched_vcpu_insert(const struct scheduler *ops, struct vcpu *vc)
 
     BUG_ON( is_idle_vcpu(vc) );
 
-    /* This is safe because vc isn't yet being scheduled */

xen/common/sched_credit.c csched_vcpu_insert(const struct scheduler *ops, struct vcpu *vc) · line 1010

Quoted 10 lines
xen/common/sched_credit.c +5 -0
+    /* csched_cpu_pick() looks in vc->processor's runq, so we need the lock. */
+    lock = vcpu_schedule_lock_irq(vc);
+
     vc->processor = csched_cpu_pick(ops, vc);
 
+    spin_unlock_irq(lock);
+
     lock = vcpu_schedule_lock_irq(vc);
 
     if ( !__vcpu_on_runq(svc) && vcpu_runnable(vc) && !vc->is_running )

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

#2 lore raw
Quoted 10 lines · George Dunlap
Quoted 3 lines · Dario Faggioli

It might be nice if we could add an ASSERT() that the appropriate runqueue was locked, to make sure we don't get caught out again like this in the future, but I think that would probably require turning it
into a static inline (which probably wouldn't be so bad anyway).

Mmm... good point.

Quoted 7 lines

But in any case:

Let me know if you want me to check this in as-is or if you think you might send a follow-up patch adding an ASSERT.

Yes, I'll send a new patch.

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

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

#3 lore raw
In reply to George Dunlap
Quoted 5 lines · George Dunlap

On 12/08/16 05:07, Dario Faggioli wrote:

Let me know if you want me to check this in as-is or if you think you might send a follow-up patch adding an ASSERT.

Done, and it actually explodes like this:

(XEN) [    4.870128] Xen call trace:
(XEN) [    4.870130]    [<ffff82d080131cba>] spinlock.c#check_lock+0x42/0x46 (XEN) [    4.870133]    [<ffff82d080131db2>] _spin_is_locked+0x11/0x4d (XEN) [    4.870139]    [<ffff82d080126c2b>] sched_credit.c#_csched_cpu_pick+0x1a9/0x632 (XEN) [    4.870142]    [<ffff82d08012747f>] sched_credit.c#csched_tick+0x1fd/0x385 (XEN) [    4.870146]    [<ffff82d080134a16>] timer.c#execute_timer+0x47/0x62 (XEN) [    4.870148]    [<ffff82d080134b0c>] timer.c#timer_softirq_action+0xdb/0x22c (XEN) [    4.870151]    [<ffff82d080131487>] softirq.c#__do_softirq+0x7f/0x8a (XEN) [    4.870153]    [<ffff82d0801314dc>] do_softirq+0x13/0x15
(XEN) [    4.870157]    [<ffff82d080243e01>] entry.o#process_softirqs+0x21/0x30 (XEN) [    4.870159]
(XEN) [    5.619096]
(XEN) [    5.621085] ****************************************
(XEN) [    5.626536] Panic on CPU 0:
(XEN) [    5.629826] Xen BUG at spinlock.c:48
(XEN) [    5.633895] ****************************************

And if I look at csched_tick(), it indeed is the case that we call csched_vcpu_acct() **without** holding the runq lock.

It in turns calls things like burn_credits(), accesses current, and other stuff, which I'm having a little bit of an hard time convincing myself it is safe... Although it must be, if there have been no issues after all these years. :-O

csched_runq_sort(), called later, still by csched_tick(), acquires the lock by itself, and we can't acquire it in csched_tick(), because __csched_vcpu_acct_start() acquires the private lock, and we'd violate the nesting rule.

In summary, this is looking more complicated than it seemed, and I'll have to look at this again on Tuesday (it's public holiday, here, on Monday).

Gosh, how much I hate this scheduler!! :-/

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

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