Skip to contentSkip to conversation

xen/sched: fix error handling in cpu_schedule_up()

PATCH In review Juergen Gross 2 participants 3 messages Jun 26, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xen/sched: fix error handling in cpu_schedule_up()

In case cpu_schedule_up() is failing, it needs to undo all externally visible changes it has done before.

Reason is that cpu_schedule_callback() won't be called with the CPU_UP_CANCELED notifier in case cpu_schedule_up() did fail.

Fixes 207589dbacd4 ("xen/sched: move per cpu scheduler private data into struct sched_resource")

---
V2:
- undo changes in cpu_schedule_up() in case of failure

---
 xen/common/sched/core.c | 63 +++++++++++++++++++++--------------------
 1 file changed, 33 insertions(+), 30 deletions(-)
xen/common/sched/core.c +33 -30
index d84b65f197..c466711e9e 100644
@@ -2755,6 +2755,36 @@ static struct sched_resource *sched_alloc_res(void)
     return sr;
 }
 
+static void cf_check sched_res_free(struct rcu_head *head)
+{
+    struct sched_resource *sr = container_of(head, struct sched_resource, rcu);
+
+    free_cpumask_var(sr->cpus);
+    if ( sr->sched_unit_idle )
+        sched_free_unit_mem(sr->sched_unit_idle);
+    xfree(sr);
+}
+
+static void cpu_schedule_down(unsigned int cpu)
+{
+    struct sched_resource *sr;
+
+    rcu_read_lock(&sched_res_rculock);
+
+    sr = get_sched_res(cpu);
+
+    kill_timer(&sr->s_timer);
+
+    cpumask_clear_cpu(cpu, &sched_res_mask);
+    set_sched_res(cpu, NULL);
+
+    /* Keep idle unit. */
+    sr->sched_unit_idle = NULL;
+    call_rcu(&sr->rcu, sched_res_free);
+
+    rcu_read_unlock(&sched_res_rculock);
+}
+
 static int cpu_schedule_up(unsigned int cpu)
 {
     struct sched_resource *sr;
@@ -2794,7 +2824,10 @@ static int cpu_schedule_up(unsigned int cpu)
         idle_vcpu[cpu]->sched_unit->res = sr;
 
     if ( idle_vcpu[cpu] == NULL )
+    {
+        cpu_schedule_down(cpu);
         return -ENOMEM;
+    }
 
     idle_vcpu[cpu]->sched_unit->rendezvous_in_cnt = 0;
 
@@ -2812,36 +2845,6 @@ static int cpu_schedule_up(unsigned int cpu)
     return 0;
 }
 
-static void cf_check sched_res_free(struct rcu_head *head)
-{
-    struct sched_resource *sr = container_of(head, struct sched_resource, rcu);
-
-    free_cpumask_var(sr->cpus);
-    if ( sr->sched_unit_idle )
-        sched_free_unit_mem(sr->sched_unit_idle);
-    xfree(sr);
-}
-
-static void cpu_schedule_down(unsigned int cpu)
-{
-    struct sched_resource *sr;
-
-    rcu_read_lock(&sched_res_rculock);
-
-    sr = get_sched_res(cpu);
-
-    kill_timer(&sr->s_timer);
-
-    cpumask_clear_cpu(cpu, &sched_res_mask);
-    set_sched_res(cpu, NULL);
-
-    /* Keep idle unit. */
-    sr->sched_unit_idle = NULL;
-    call_rcu(&sr->rcu, sched_res_free);
-
-    rcu_read_unlock(&sched_res_rculock);
-}
-
 void sched_rm_cpu(unsigned int cpu)
 {
     int rc;
Signature · 1 line
2.35.3
Reviewed-by on v2 1/1
#1 lore raw
Quoted 9 lines · Juergen Gross

In case cpu_schedule_up() is failing, it needs to undo all externally visible changes it has done before.

Reason is that cpu_schedule_callback() won't be called with the CPU_UP_CANCELED notifier in case cpu_schedule_up() did fail.

Fixes 207589dbacd4 ("xen/sched: move per cpu scheduler private data into struct sched_resource")

with two questions, just for my own reassurance:

Quoted 25 lines
xen/common/sched/core.c +19 -0
@@ -2755,6 +2755,36 @@ static struct sched_resource *sched_alloc_res(void)
     return sr;
 }
 
+static void cf_check sched_res_free(struct rcu_head *head)
+{
+    struct sched_resource *sr = container_of(head, struct sched_resource, rcu);
+
+    free_cpumask_var(sr->cpus);
+    if ( sr->sched_unit_idle )
+        sched_free_unit_mem(sr->sched_unit_idle);
+    xfree(sr);
+}
+
+static void cpu_schedule_down(unsigned int cpu)
+{
+    struct sched_resource *sr;
+
+    rcu_read_lock(&sched_res_rculock);
+
+    sr = get_sched_res(cpu);
+
+    kill_timer(&sr->s_timer);

xen/common/sched/core.c static struct sched_resource *sched_alloc_res(void) · line 2786

Quoted 10 lines
xen/common/sched/core.c +10 -0
+
+    cpumask_clear_cpu(cpu, &sched_res_mask);
+    set_sched_res(cpu, NULL);
+
+    /* Keep idle unit. */
+    sr->sched_unit_idle = NULL;
+    call_rcu(&sr->rcu, sched_res_free);
+
+    rcu_read_unlock(&sched_res_rculock);
+}

Eyeballing suggests these two functions don't change at all; they're solely being moved up?

Also, for the purpose here, use of RCU to effect the freeing isn't strictly necessary. It's just that it also doesn't hurt doing it that way, and it would be more code to directly free when coming from cpu_schedule_up()?

Jan

#2 lore raw
Quoted 44 lines · Jan Beulich
Quoted 9 lines · Juergen Gross

In case cpu_schedule_up() is failing, it needs to undo all externally visible changes it has done before.

Reason is that cpu_schedule_callback() won't be called with the CPU_UP_CANCELED notifier in case cpu_schedule_up() did fail.

Fixes 207589dbacd4 ("xen/sched: move per cpu scheduler private data into struct sched_resource")

with two questions, just for my own reassurance:

Quoted 30 lines
xen/common/sched/core.c +24 -0
@@ -2755,6 +2755,36 @@ static struct sched_resource *sched_alloc_res(void)
      return sr;
  }
 
+static void cf_check sched_res_free(struct rcu_head *head)
+{
+    struct sched_resource *sr = container_of(head, struct sched_resource, rcu);
+
+    free_cpumask_var(sr->cpus);
+    if ( sr->sched_unit_idle )
+        sched_free_unit_mem(sr->sched_unit_idle);
+    xfree(sr);
+}
+
+static void cpu_schedule_down(unsigned int cpu)
+{
+    struct sched_resource *sr;
+
+    rcu_read_lock(&sched_res_rculock);
+
+    sr = get_sched_res(cpu);
+
+    kill_timer(&sr->s_timer);
+
+    cpumask_clear_cpu(cpu, &sched_res_mask);
+    set_sched_res(cpu, NULL);
+
+    /* Keep idle unit. */
Quoted 8 lines

xen/common/sched/core.c static struct sched_resource *sched_alloc_res(void) · line 2786

Quoted 5 lines
xen/common/sched/core.c +5 -0
+    sr->sched_unit_idle = NULL;
+    call_rcu(&sr->rcu, sched_res_free);
+
+    rcu_read_unlock(&sched_res_rculock);
+}

Eyeballing suggests these two functions don't change at all; they're solely being moved up?

Correct.

Quoted 3 lines

Also, for the purpose here, use of RCU to effect the freeing isn't strictly necessary. It's just that it also doesn't hurt doing it that way, and it would be more code to directly free when coming from cpu_schedule_up()?

Yes.

Juergen