Skip to contentSkip to conversation

xen/sched: fixes and cleanup related to cpupools

PATCH In review Juergen Gross 3 participants 9 messages Dec 4, 2023 – Dec 7, 2023

lore
Share & tools
lore
#0 lore raw

v2 cover xen/sched: fixes and cleanup related to cpupools

Fix 2 bugs related to cpupool handling and do some related cleanups.

V2:
- send out the fixes tagged properly as "PATCH"
- add cleanup patch

Juergen Gross (3):
xen/sched: fix adding offline cpu to cpupool xen/sched: fix sched_move_domain()
xen/sched: do some minor cleanup of sched_move_domain()

 xen/common/sched/core.c    | 74 ++++++++++++++++++++------------------
 xen/common/sched/cpupool.c |  2 ++
 2 files changed, 41 insertions(+), 35 deletions(-)
Signature · 1 line
2.35.3
#1 lore raw

PATCH v2 1/3 xen/sched: fix adding offline cpu to cpupool

Trying to add an offline cpu to a cpupool can crash the hypervisor, as the probably non-existing percpu area of the cpu is accessed before the availability of the cpu is being tested. This can happen in case the cpupool's granularity is "core" or "socket".

Fix that by testing the cpu to be online.

Fixes cb563d7665f2 ("xen/sched: support core scheduling for moving cpus to/from cpupools")

---
V2:
- enhance commit message

---
 xen/common/sched/cpupool.c | 2 ++
 1 file changed, 2 insertions(+)
xen/common/sched/cpupool.c +2 -0
index 2e094b0cfa..ad8f608462 100644
@@ -892,6 +892,8 @@ int cpupool_do_sysctl(struct xen_sysctl_cpupool_op *op)
         if ( cpu >= nr_cpu_ids )
             goto addcpu_out;
         ret = -ENODEV;
+        if ( !cpu_online(cpu) )
+            goto addcpu_out;
         cpus = sched_get_opt_cpumask(c->gran, cpu);
         if ( !cpumask_subset(cpus, &cpupool_free_cpus) ||
              cpumask_intersects(cpus, &cpupool_locked_cpus) )
Signature · 1 line
2.35.3
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] xen/sched: fix adding offline cpu to cpupool

Quoted 10 lines · Juergen Gross

Trying to add an offline cpu to a cpupool can crash the hypervisor, as the probably non-existing percpu area of the cpu is accessed before the availability of the cpu is being tested. This can happen in case the cpupool's granularity is "core" or "socket".

Fix that by testing the cpu to be online.

Fixes cb563d7665f2 ("xen/sched: support core scheduling for moving cpus to/from cpupools")
Acked-by
#3 lore raw

Re: [PATCH v2 1/3] xen/sched: fix adding offline cpu to cpupool

Quoted 7 lines · Jan Beulich
Quoted 5 lines · Juergen Gross

Trying to add an offline cpu to a cpupool can crash the hypervisor, as the probably non-existing percpu area of the cpu is accessed before the availability of the cpu is being tested. This can happen in case the cpupool's granularity is "core" or "socket".

Quoted 7 lines
Quoted 5 lines

Fix that by testing the cpu to be online.

Fixes cb563d7665f2 ("xen/sched: support core scheduling for moving cpus to/from cpupools")

I feel like there should be a more robust way to protect against this sort of thing; but I don't see anything obvious, and this does fix a bug, so:

#4 lore raw

PATCH v2 2/3 xen/sched: fix sched_move_domain()

In reply to Juergen Gross

Do cleanup in sched_move_domain() in a dedicated service function, which is called either in error case with newly allocated data, or in success case with the old data to be freed.

This will at once fix some subtle bugs which sneaked in due to forgetting to overwrite some pointers in the error case.

Fixes 70fadc41635b ("xen/cpupool: support moving domain between cpupools with different granularity")

---
V2:
- make ops parameter of new function const (Jan Beulich)

---
 xen/common/sched/core.c | 47 +++++++++++++++++++++++------------------
 1 file changed, 27 insertions(+), 20 deletions(-)
xen/common/sched/core.c +27 -20
index eba0cea4bb..901782bbb4 100644
@@ -647,6 +647,24 @@ static void sched_move_irqs(const struct sched_unit *unit)
         vcpu_move_irqs(v);
 }
 
+static void sched_move_domain_cleanup(const struct scheduler *ops,
+                                      struct sched_unit *units,
+                                      void *domdata)
+{
+    struct sched_unit *unit, *old_unit;
+
+    for ( unit = units; unit; )
+    {
+        if ( unit->priv )
+            sched_free_udata(ops, unit->priv);
+        old_unit = unit;
+        unit = unit->next_in_list;
+        xfree(old_unit);
+    }
+
+    sched_free_domdata(ops, domdata);
+}
+
 /*
  * Move a domain from one cpupool to another.
  *
@@ -686,7 +704,6 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
     void *old_domdata;
     unsigned int gran = cpupool_get_granularity(c);
     unsigned int n_units = d->vcpu[0] ? DIV_ROUND_UP(d->max_vcpus, gran) : 0;
-    int ret = 0;
 
     for_each_vcpu ( d, v )
     {
@@ -699,8 +716,9 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
     domdata = sched_alloc_domdata(c->sched, d);
     if ( IS_ERR(domdata) )
     {
-        ret = PTR_ERR(domdata);
-        goto out;
+        rcu_read_unlock(&sched_res_rculock);
+
+        return PTR_ERR(domdata);
     }
 
     for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
@@ -718,10 +736,10 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
         if ( !unit || !unit->priv )
         {
-            old_units = new_units;
-            old_domdata = domdata;
-            ret = -ENOMEM;
-            goto out_free;
+            sched_move_domain_cleanup(c->sched, new_units, domdata);
+            rcu_read_unlock(&sched_res_rculock);
+
+            return -ENOMEM;
         }
 
         unit_ptr = &unit->next_in_list;
@@ -808,22 +826,11 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
     domain_unpause(d);
 
- out_free:
-    for ( unit = old_units; unit; )
-    {
-        if ( unit->priv )
-            sched_free_udata(c->sched, unit->priv);
-        old_unit = unit;
-        unit = unit->next_in_list;
-        xfree(old_unit);
-    }
-
-    sched_free_domdata(old_ops, old_domdata);
+    sched_move_domain_cleanup(old_ops, old_units, old_domdata);
 
- out:
     rcu_read_unlock(&sched_res_rculock);
 
-    return ret;
+    return 0;
 }
 
 void sched_destroy_vcpu(struct vcpu *v)
Signature · 1 line
2.35.3
Reviewed-by on v2 2/3
#5 lore raw

Re: [PATCH v2 2/3] xen/sched: fix sched_move_domain()

Quoted 4 lines · Juergen Gross

Do cleanup in sched_move_domain() in a dedicated service function, which is called either in error case with newly allocated data, or in success case with the old data to be freed.

Quoted 8 lines

This will at once fix some subtle bugs which sneaked in due to forgetting to overwrite some pointers in the error case.

Fixes 70fadc41635b ("xen/cpupool: support moving domain between cpupools with different granularity")
Acked-by
#6 lore raw

Re: [PATCH v2 2/3] xen/sched: fix sched_move_domain()

Quoted 9 lines · Jan Beulich
Quoted 7 lines · Juergen Gross

Do cleanup in sched_move_domain() in a dedicated service function, which is called either in error case with newly allocated data, or in success case with the old data to be freed.

This will at once fix some subtle bugs which sneaked in due to forgetting to overwrite some pointers in the error case.

Quoted 7 lines
Quoted 5 lines
Fixes 70fadc41635b ("xen/cpupool: support moving domain between cpupools with different granularity")

Still not a fan of removing the "out:" label, but anyway:

#7 lore raw

PATCH v2 3/3 xen/sched: do some minor cleanup of sched_move_domain()

In reply to Juergen Gross

Do some minor cleanups:

- Move setting of old_domdata and old_units next to each other
- Drop incrementing unit_idx in the final loop of sched_move_domain() as it isn't used afterwards
- Rename new_p to new_cpu and unit_p to unit_cpu

---
V2:
- new patch

---
 xen/common/sched/core.c | 27 ++++++++++++---------------
 1 file changed, 12 insertions(+), 15 deletions(-)
xen/common/sched/core.c +12 -15
index 901782bbb4..f6ac1e5af8 100644
@@ -698,7 +698,7 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
     struct sched_unit *unit, *old_unit;
     struct sched_unit *new_units = NULL, *old_units;
     struct sched_unit **unit_ptr = &new_units;
-    unsigned int new_p, unit_idx;
+    unsigned int new_cpu, unit_idx;
     void *domdata;
     struct scheduler *old_ops = dom_scheduler(d);
     void *old_domdata;
@@ -748,13 +748,14 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
     domain_pause(d);
 
     old_domdata = d->sched_priv;
+    old_units = d->sched_unit_list;
 
     /*
      * Remove all units from the old scheduler, and temporarily move them to
      * the same processor to make locking easier when moving the new units to
      * new processors.
      */
-    new_p = cpumask_first(d->cpupool->cpu_valid);
+    new_cpu = cpumask_first(d->cpupool->cpu_valid);
     for_each_sched_unit ( d, unit )
     {
         spinlock_t *lock;
@@ -762,12 +763,10 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
         sched_remove_unit(old_ops, unit);
 
         lock = unit_schedule_lock_irq(unit);
-        sched_set_res(unit, get_sched_res(new_p));
+        sched_set_res(unit, get_sched_res(new_cpu));
         spin_unlock_irq(lock);
     }
 
-    old_units = d->sched_unit_list;
-
     d->cpupool = c;
     d->sched_priv = domdata;
 
@@ -781,32 +780,32 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
         unit->state_entry_time = old_unit->state_entry_time;
         unit->runstate_cnt[v->runstate.state]++;
         /* Temporarily use old resource assignment */
-        unit->res = get_sched_res(new_p);
+        unit->res = get_sched_res(new_cpu);
 
         v->sched_unit = unit;
     }
 
     d->sched_unit_list = new_units;
 
-    new_p = cpumask_first(c->cpu_valid);
+    new_cpu = cpumask_first(c->cpu_valid);
     for_each_sched_unit ( d, unit )
     {
         spinlock_t *lock;
-        unsigned int unit_p = new_p;
+        unsigned int unit_cpu = new_cpu;
 
         for_each_sched_unit_vcpu ( unit, v )
         {
-            migrate_timer(&v->periodic_timer, new_p);
-            migrate_timer(&v->singleshot_timer, new_p);
-            migrate_timer(&v->poll_timer, new_p);
-            new_p = cpumask_cycle(new_p, c->cpu_valid);
+            migrate_timer(&v->periodic_timer, new_cpu);
+            migrate_timer(&v->singleshot_timer, new_cpu);
+            migrate_timer(&v->poll_timer, new_cpu);
+            new_cpu = cpumask_cycle(new_cpu, c->cpu_valid);
         }
 
         lock = unit_schedule_lock_irq(unit);
 
         sched_set_affinity(unit, &cpumask_all, &cpumask_all);
 
-        sched_set_res(unit, get_sched_res(unit_p));
+        sched_set_res(unit, get_sched_res(unit_cpu));
         /*
          * With v->processor modified we must not
          * - make any further changes assuming we hold the scheduler lock,
@@ -818,8 +817,6 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
             sched_move_irqs(unit);
 
         sched_insert_unit(c->sched, unit);
-
-        unit_idx++;
     }
 
     domain_update_node_affinity(d);
Signature · 1 line
2.35.3
Reviewed-by on v2 3/3
#8 lore raw

Re: [PATCH v2 3/3] xen/sched: do some minor cleanup of sched_move_domain()

Quoted 9 lines · Juergen Gross

Do some minor cleanups:

- Move setting of old_domdata and old_units next to each other
- Drop incrementing unit_idx in the final loop of sched_move_domain() as it isn't used afterwards
- Rename new_p to new_cpu and unit_p to unit_cpu