Skip to contentSkip to conversation

xen: Fix some bugs in scheduling

PATCH Superseded Juergen Gross 3 participants 10 messages Apr 30, 2020 – May 8, 2020

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover xen: Fix some bugs in scheduling

Some bugs I found when trying to find a problem with cpu-on/offlining in core scheduling mode.

Patches 1 and 3 are fixing observed problems, while patch 2 is more of a theoretical issue.

Juergen Gross (3):
xen/sched: allow rcu work to happen when syncing cpus in core
scheduling
xen/sched: fix theoretical races accessing vcpu->dirty_cpu xen/cpupool: fix removing cpu from a cpupool

 xen/arch/x86/domain.c      | 14 ++++++++++----
 xen/common/sched/core.c    | 10 +++++++---
 xen/common/sched/cpupool.c |  3 +++
 xen/include/xen/sched.h    |  2 +-
 xen/include/xen/softirq.h  |  2 +-
 5 files changed, 22 insertions(+), 9 deletions(-)
Signature · 1 line
2.16.4
#1 lore raw

PATCH 1/3 xen/sched: allow rcu work to happen when syncing cpus in core scheduling

With RCU barriers moved from tasklets to normal RCU processing cpu offlining in core scheduling might deadlock due to cpu synchronization required by RCU processing and core scheduling concurrently.

Fix that by bailing out from core scheduling synchronization in case of pending RCU work. Additionally the RCU softirq is now required to be of higher priority than the scheduling softirqs in order to do RCU processing before entering the scheduler again, as bailing out from the core scheduling synchronization requires to raise another softirq SCHED_SLAVE, which would bypass RCU processing again.

---
 xen/common/sched/core.c   | 10 +++++++---
 xen/include/xen/softirq.h |  2 +-
 2 files changed, 8 insertions(+), 4 deletions(-)
xen/common/sched/core.c +7 -3
index d94b95285f..a099e37b0f 100644
@@ -2457,13 +2457,17 @@ static struct sched_unit *sched_wait_rendezvous_in(struct sched_unit *prev,
             v = unit2vcpu_cpu(prev, cpu);
         }
         /*
-         * Coming from idle might need to do tasklet work.
+         * Check for any work to be done which might need cpu synchronization.
+         * This is either pending RCU work, or tasklet work when coming from
+         * idle.
          * In order to avoid deadlocks we can't do that here, but have to
-         * continue the idle loop.
+         * schedule the previous vcpu again, which will lead to the desired
+         * processing to be done.
          * Undo the rendezvous_in_cnt decrement and schedule another call of
          * sched_slave().
          */
-        if ( is_idle_unit(prev) && sched_tasklet_check_cpu(cpu) )
+        if ( rcu_pending(cpu) ||
+             (is_idle_unit(prev) && sched_tasklet_check_cpu(cpu)) )
         {
             struct vcpu *vprev = current;
 
xen/include/xen/softirq.h +1 -1
index b4724f5c8b..1f6c4783da 100644
@@ -4,10 +4,10 @@
 /* Low-latency softirqs come first in the following list. */
 enum {
     TIMER_SOFTIRQ = 0,
+    RCU_SOFTIRQ,
     SCHED_SLAVE_SOFTIRQ,
     SCHEDULE_SOFTIRQ,
     NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ,
-    RCU_SOFTIRQ,
     TASKLET_SOFTIRQ,
     NR_COMMON_SOFTIRQS
 };
Signature · 1 line
2.16.4
Acked-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] xen/sched: allow rcu work to happen when syncing cpus in core scheduling

Quoted 13 lines · Juergen Gross

With RCU barriers moved from tasklets to normal RCU processing cpu offlining in core scheduling might deadlock due to cpu synchronization
required by RCU processing and core scheduling concurrently.

Fix that by bailing out from core scheduling synchronization in case of pending RCU work. Additionally the RCU softirq is now required to be of higher priority than the scheduling softirqs in order to do RCU processing before entering the scheduler again, as bailing out from
the core scheduling synchronization requires to raise another softirq SCHED_SLAVE, which would bypass RCU processing again.

Quoted 4 lines

In general, I'm fine with this patch and it can have my:

I'd ask for one thing, but that doesn't affect the ack, as it's not "my" code. :-)

Quoted 7 lines
xen/include/xen/softirq.h +0 -0
index b4724f5c8b..1f6c4783da 100644
@@ -4,10 +4,10 @@
 /* Low-latency softirqs come first in the following list. */

xen/include/xen/softirq.h line 13

Quoted 10 lines
xen/include/xen/softirq.h +1 -1
 enum {
     TIMER_SOFTIRQ = 0,
+    RCU_SOFTIRQ,
     SCHED_SLAVE_SOFTIRQ,
     SCHEDULE_SOFTIRQ,
     NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ,
-    RCU_SOFTIRQ,
     TASKLET_SOFTIRQ,
     NR_COMMON_SOFTIRQS
 };

So, until now, it was kind of intuitive (at least, it was to me :-) ) that the TIMER_SOFTIRQ, we want it first, and the SCHEDULE one right after it. And the comment above the enum ("Low-latency softirqs come first in the following list"), although brief, is effective.

With the introduction of SCHED_SLAVE, things became slightly more complex, but it still is not too far a reach to figure out the fact that we want it to be above SCHEDULE, and the reasons for that.

Now that we're moving RCU from (almost) the very bottom to up here, I think we need some more info, there in the code. Sure all the bits and pieces are there in the changelogs, but I think it would be rather helpful to have them easily available to people trying to understand or modifying this code, e.g., with a comment.

I was also thinking that, even better than a comment, would be a (build?) BUG_ON if RCU has no smaller value than SCHED_SLAVE and SLAVE. Not here, of course, but maybe close to some piece of code that relies on this assumption. Something that, if I tomorrow put the SCHED* ones on top again, would catch my attention and tell me that I either take care of that code path too, or I can't do it.

However, I'm not sure whether, e.g., the other hunk of this patch would be a suitable place for something like this. And I can't, out of the top of my head, think of a really good place for where to put it. Therefore, I'm "only" asking for the comment... but if you (or others) have ideas, that'd be cool. :-)

Thanks and Regards

Signature · 6 lines
Dario Faggioli, Ph.D
http://about.me/dario.faggioli
Virtualization Software Engineer
SUSE Labs, SUSE https://www.suse.com/
-------------------------------------------------------------------
<<This happens because _I_ choose it to happen!>> (Raistlin Majere)
#3 lore raw

Re: [PATCH 1/3] xen/sched: allow rcu work to happen when syncing cpus in core scheduling

Quoted 51 lines · Dario Faggioli
Quoted 13 lines · Juergen Gross

With RCU barriers moved from tasklets to normal RCU processing cpu offlining in core scheduling might deadlock due to cpu synchronization
required by RCU processing and core scheduling concurrently.

Fix that by bailing out from core scheduling synchronization in case of pending RCU work. Additionally the RCU softirq is now required to be of higher priority than the scheduling softirqs in order to do RCU processing before entering the scheduler again, as bailing out from
the core scheduling synchronization requires to raise another softirq SCHED_SLAVE, which would bypass RCU processing again.

Quoted 4 lines

In general, I'm fine with this patch and it can have my:

I'd ask for one thing, but that doesn't affect the ack, as it's not "my" code. :-)

Quoted 7 lines
xen/include/xen/softirq.h +0 -0
index b4724f5c8b..1f6c4783da 100644
@@ -4,10 +4,10 @@
  /* Low-latency softirqs come first in the following list. */

xen/include/xen/softirq.h line 13

Quoted 10 lines
xen/include/xen/softirq.h +1 -1
  enum {
      TIMER_SOFTIRQ = 0,
+    RCU_SOFTIRQ,
      SCHED_SLAVE_SOFTIRQ,
      SCHEDULE_SOFTIRQ,
      NEW_TLBFLUSH_CLOCK_PERIOD_SOFTIRQ,
-    RCU_SOFTIRQ,
      TASKLET_SOFTIRQ,
      NR_COMMON_SOFTIRQS
  };

So, until now, it was kind of intuitive (at least, it was to me :-) ) that the TIMER_SOFTIRQ, we want it first, and the SCHEDULE one right after it. And the comment above the enum ("Low-latency softirqs come first in the following list"), although brief, is effective.

With the introduction of SCHED_SLAVE, things became slightly more complex, but it still is not too far a reach to figure out the fact that we want it to be above SCHEDULE, and the reasons for that.

Quoted 5 lines

Now that we're moving RCU from (almost) the very bottom to up here, I think we need some more info, there in the code. Sure all the bits and pieces are there in the changelogs, but I think it would be rather helpful to have them easily available to people trying to understand or modifying this code, e.g., with a comment.

That's reasonable.

Quoted 8 lines

I was also thinking that, even better than a comment, would be a (build?) BUG_ON if RCU has no smaller value than SCHED_SLAVE and SLAVE. Not here, of course, but maybe close to some piece of code that relies on this assumption. Something that, if I tomorrow put the SCHED* ones on top again, would catch my attention and tell me that I either take care of that code path too, or I can't do it.

Quoted 5 lines

However, I'm not sure whether, e.g., the other hunk of this patch would be a suitable place for something like this. And I can't, out of the top of my head, think of a really good place for where to put it. Therefore, I'm "only" asking for the comment... but if you (or others) have ideas, that'd be cool. :-)

I think the other hunk is exactly where the BUILD_BUG_ON() should be. And this is a perfect place for the comment, too, as its placement will explain the context very well.

Juergen

#4 lore raw

PATCH 2/3 xen/sched: fix theoretical races accessing vcpu->dirty_cpu

In reply to Juergen Gross

The dirty_cpu field of struct vcpu denotes which cpu still holds data of a vcpu. All accesses to this field should be atomic in case the vcpu could just be running, as it is accessed without any lock held in most cases.

There are some instances where accesses are not atomically done, and even worse where multiple accesses are done when a single one would be mandated.

Correct that in order to avoid potential problems.

Add some assertions to verify dirty_cpu is handled properly.

---
 xen/arch/x86/domain.c   | 14 ++++++++++----
 xen/include/xen/sched.h |  2 +-
 2 files changed, 11 insertions(+), 5 deletions(-)
xen/arch/x86/domain.c +10 -4
index a4428190d5..f0579a56d1 100644
@@ -1769,6 +1769,7 @@ static void __context_switch(void)
 
     if ( !is_idle_domain(pd) )
     {
+        ASSERT(read_atomic(&p->dirty_cpu) == cpu);
         memcpy(&p->arch.user_regs, stack_regs, CTXT_SWITCH_STACK_BYTES);
         vcpu_save_fpu(p);
         pd->arch.ctxt_switch->from(p);
@@ -1832,7 +1833,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
 {
     unsigned int cpu = smp_processor_id();
     const struct domain *prevd = prev->domain, *nextd = next->domain;
-    unsigned int dirty_cpu = next->dirty_cpu;
+    unsigned int dirty_cpu = read_atomic(&next->dirty_cpu);
 
     ASSERT(prev != next);
     ASSERT(local_irq_is_enabled());
@@ -1844,6 +1845,7 @@ void context_switch(struct vcpu *prev, struct vcpu *next)
     {
         /* Remote CPU calls __sync_local_execstate() from flush IPI handler. */
         flush_mask(cpumask_of(dirty_cpu), FLUSH_VCPU_STATE);
+        ASSERT(read_atomic(&next->dirty_cpu) == VCPU_CPU_CLEAN);
     }
 
     _update_runstate_area(prev);
@@ -1956,13 +1958,17 @@ void sync_local_execstate(void)
 
 void sync_vcpu_execstate(struct vcpu *v)
 {
-    if ( v->dirty_cpu == smp_processor_id() )
+    unsigned int dirty_cpu = read_atomic(&v->dirty_cpu);
+
+    if ( dirty_cpu == smp_processor_id() )
         sync_local_execstate();
-    else if ( vcpu_cpu_dirty(v) )
+    else if ( is_vcpu_dirty_cpu(dirty_cpu) )
     {
         /* Remote CPU calls __sync_local_execstate() from flush IPI handler. */
-        flush_mask(cpumask_of(v->dirty_cpu), FLUSH_VCPU_STATE);
+        flush_mask(cpumask_of(dirty_cpu), FLUSH_VCPU_STATE);
     }
+    ASSERT(read_atomic(&v->dirty_cpu) != dirty_cpu ||
+           dirty_cpu == VCPU_CPU_CLEAN);
 }
 
 static int relinquish_memory(
xen/include/xen/sched.h +1 -1
index 195e7ee583..008d3c8861 100644
@@ -844,7 +844,7 @@ static inline bool is_vcpu_dirty_cpu(unsigned int cpu)
 
 static inline bool vcpu_cpu_dirty(const struct vcpu *v)
 {
-    return is_vcpu_dirty_cpu(v->dirty_cpu);
+    return is_vcpu_dirty_cpu(read_atomic(&v->dirty_cpu));
 }
 
 void vcpu_block(void);
Signature · 1 line
2.16.4
#5 lore raw

Re: [PATCH 2/3] xen/sched: fix theoretical races accessing vcpu->dirty_cpu

Quoted 9 lines · Juergen Gross

The dirty_cpu field of struct vcpu denotes which cpu still holds data of a vcpu. All accesses to this field should be atomic in case the vcpu could just be running, as it is accessed without any lock held in most cases.

There are some instances where accesses are not atomically done, and even worse where multiple accesses are done when a single one would be mandated.

Quoted 5 lines

Correct that in order to avoid potential problems.

Add some assertions to verify dirty_cpu is handled properly.

Please ignore this one, just realized it doesn't build for ARM.

Juergen

#6 lore raw

PATCH 3/3 xen/cpupool: fix removing cpu from a cpupool

In reply to Juergen Gross

Commit cb563d7665f2 ("xen/sched: support core scheduling for moving cpus to/from cpupools") introduced a regression when trying to remove an offline cpu from a cpupool, as the system would crash in this situation.

Fix that by testing the cpu to be online.

Fixes cb563d7665f2 ("xen/sched: support core scheduling for moving cpus to/from cpupools")
---
 xen/common/sched/cpupool.c | 3 +++
 1 file changed, 3 insertions(+)
xen/common/sched/cpupool.c +3 -0
index d40345b585..de9e25af84 100644
@@ -520,6 +520,9 @@ static int cpupool_unassign_cpu(struct cpupool *c, unsigned int cpu)
     debugtrace_printk("cpupool_unassign_cpu(pool=%d,cpu=%d)\n",
                       c->cpupool_id, cpu);
 
+    if ( !cpu_online(cpu) )
+        return -EINVAL;
+
     master_cpu = sched_get_resource_cpu(cpu);
     ret = cpupool_unassign_cpu_start(c, master_cpu);
     if ( ret )
Signature · 1 line
2.16.4
Acked-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] xen/cpupool: fix removing cpu from a cpupool

Quoted 11 lines · Juergen Gross

Commit cb563d7665f2 ("xen/sched: support core scheduling for moving cpus to/from cpupools") introduced a regression when trying to remove an offline cpu from a cpupool, as the system would crash in this situation.

Fix that by testing the cpu to be online.

Fixes cb563d7665f2 ("xen/sched: support core scheduling for moving

cpus to/from cpupools")

Thanks and Regards

Signature · 6 lines
Dario Faggioli, Ph.D
http://about.me/dario.faggioli
Virtualization Software Engineer
SUSE Labs, SUSE https://www.suse.com/
-------------------------------------------------------------------
<<This happens because _I_ choose it to happen!>> (Raistlin Majere)
#8 lore raw

Re: [PATCH 3/3] xen/cpupool: fix removing cpu from a cpupool

Quoted 6 lines · Dario Faggioli
Quoted 5 lines · Juergen Gross

Commit cb563d7665f2 ("xen/sched: support core scheduling for moving cpus to/from cpupools") introduced a regression when trying to remove an offline cpu from a cpupool, as the system would crash in this situation.

Quoted 7 lines
Quoted 6 lines

Fix that by testing the cpu to be online.

Fixes cb563d7665f2 ("xen/sched: support core scheduling for moving

cpus to/from cpupools")

Jürgen,

it looks like this is independent of the earlier two patches and hence could go in, while I understand there'll be v2 for the earlier ones. Please confirm.

Jan

#9 lore raw

Re: [PATCH 3/3] xen/cpupool: fix removing cpu from a cpupool

Quoted 13 lines · Jan Beulich
Quoted 12 lines · Dario Faggioli
Quoted 11 lines · Juergen Gross

Commit cb563d7665f2 ("xen/sched: support core scheduling for moving cpus to/from cpupools") introduced a regression when trying to remove an offline cpu from a cpupool, as the system would crash in this situation.

Fix that by testing the cpu to be online.

Fixes cb563d7665f2 ("xen/sched: support core scheduling for moving

cpus to/from cpupools")

Quoted 7 lines
Quoted 1 line

Jürgen,

it looks like this is independent of the earlier two patches and hence could go in, while I understand there'll be v2 for the earlier ones. Please confirm.

Confirmed.

Juergen