Skip to contentSkip to conversation

xen/sched: fix sched_move_domain() for domain without vcpus

PATCH Superseded Juergen Gross 3 participants 6 messages Sep 6, 2021 – Sep 7, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xen/sched: fix sched_move_domain() for domain without vcpus

In case a domain is created with a cpupool other than Pool-0 specified it will be moved to that cpupool before any vcpus are allocated.

This will lead to a NULL pointer dereference in sched_move_domain().

Fix that by tolerating vcpus not being allocated yet.

Fixes 70fadc41635b9b6 ("xen/cpupool: support moving domain between cpupools with different granularity")
---
 xen/common/sched/core.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/common/sched/core.c +4 -0
index 8d178baf3d..79c9100680 100644
@@ -671,6 +671,10 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
     for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
     {
+        /* Special case for move at domain creation time. */
+        if ( !d->vcpu[unit_idx * gran] )
+            break;
+
         unit = sched_alloc_unit_mem();
         if ( unit )
         {
Signature · 1 line
2.26.2
#1 lore raw
Quoted 20 lines · Juergen Gross

In case a domain is created with a cpupool other than Pool-0 specified it will be moved to that cpupool before any vcpus are allocated.

This will lead to a NULL pointer dereference in sched_move_domain().

Fix that by tolerating vcpus not being allocated yet.

Fixes 70fadc41635b9b6 ("xen/cpupool: support moving domain between cpupools with different granularity")
---
 xen/common/sched/core.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/common/sched/core.c +0 -0
index 8d178baf3d..79c9100680 100644

xen/common/sched/core.c int sched_move_domain(struct domain *d, struct cpupool *c) · line 680

Quoted 10 lines
xen/common/sched/core.c +4 -0
@@ -671,6 +671,10 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
     for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
     {
+        /* Special case for move at domain creation time. */
+        if ( !d->vcpu[unit_idx * gran] )
+            break;
+
         unit = sched_alloc_unit_mem();
         if ( unit )
         {

I think the logic would be clearer if you wrap the entire for loop in if ( d->max_vcpus ).  This loop is only allocating units in the new scheduler for existing vcpus, so there's no point entering the loop at all during domain creation.

Also, this removes a non-speculatively-guarded d->vcpu[] deference.

~Andrew

#2 lore raw
Quoted 22 lines · Andrew Cooper
Quoted 21 lines · Juergen Gross

In case a domain is created with a cpupool other than Pool-0 specified it will be moved to that cpupool before any vcpus are allocated.

This will lead to a NULL pointer dereference in sched_move_domain().

Fix that by tolerating vcpus not being allocated yet.

Fixes 70fadc41635b9b6 ("xen/cpupool: support moving domain between cpupools with different granularity")
---
 xen/common/sched/core.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/common/sched/core.c +0 -0
index 8d178baf3d..79c9100680 100644
@@ -671,6 +671,10 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
Quoted 11 lines

xen/common/sched/core.c int sched_move_domain(struct domain *d, struct cpupool *c) · line 680

Quoted 9 lines
xen/common/sched/core.c +4 -0
     for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
     {
+        /* Special case for move at domain creation time. */
+        if ( !d->vcpu[unit_idx * gran] )
+            break;
+
         unit = sched_alloc_unit_mem();
         if ( unit )
         {

I think the logic would be clearer if you wrap the entire for loop in if ( d->max_vcpus ).

And of course, this is wrong.  Turns out the domain_has_vcpus() predicate still hasn't been committed, but d->vcpu[0] is the correct internal.

~Andrew

Quoted 9 lines

  This loop is only allocating units in the new
scheduler for existing vcpus, so there's no point entering the loop at all during domain creation.

Also, this removes a non-speculatively-guarded d->vcpu[] deference.

~Andrew

#3 lore raw
Quoted 35 lines · Andrew Cooper
Quoted 22 lines · Andrew Cooper
Quoted 21 lines · Juergen Gross

In case a domain is created with a cpupool other than Pool-0 specified it will be moved to that cpupool before any vcpus are allocated.

This will lead to a NULL pointer dereference in sched_move_domain().

Fix that by tolerating vcpus not being allocated yet.

Fixes 70fadc41635b9b6 ("xen/cpupool: support moving domain between cpupools with different granularity")
---
 xen/common/sched/core.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/common/sched/core.c +0 -0
index 8d178baf3d..79c9100680 100644
@@ -671,6 +671,10 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
Quoted 11 lines

xen/common/sched/core.c int sched_move_domain(struct domain *d, struct cpupool *c) · line 680

Quoted 9 lines
xen/common/sched/core.c +4 -0
     for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
     {
+        /* Special case for move at domain creation time. */
+        if ( !d->vcpu[unit_idx * gran] )
+            break;
+
         unit = sched_alloc_unit_mem();
         if ( unit )
         {

I think the logic would be clearer if you wrap the entire for loop in if ( d->max_vcpus ).

Quoted 3 lines

And of course, this is wrong.  Turns out the domain_has_vcpus() predicate still hasn't been committed, but d->vcpu[0] is the correct internal.

Which in turn might want to be done by setting n_units to zero when d->vcpus[0] is NULL?

Jan

#4 lore raw
Quoted 36 lines · Jan Beulich
Quoted 35 lines · Andrew Cooper
Quoted 33 lines · Andrew Cooper
Quoted 20 lines · Juergen Gross

In case a domain is created with a cpupool other than Pool-0 specified it will be moved to that cpupool before any vcpus are allocated.

This will lead to a NULL pointer dereference in sched_move_domain().

Fix that by tolerating vcpus not being allocated yet.

Fixes 70fadc41635b9b6 ("xen/cpupool: support moving domain between cpupools with different granularity")
---
  xen/common/sched/core.c | 4 ++++
  1 file changed, 4 insertions(+)
xen/common/sched/core.c +0 -0
index 8d178baf3d..79c9100680 100644

xen/common/sched/core.c int sched_move_domain(struct domain *d, struct cpupool *c) · line 680

Quoted 10 lines
xen/common/sched/core.c +4 -0
@@ -671,6 +671,10 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
      for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
      {
+        /* Special case for move at domain creation time. */
+        if ( !d->vcpu[unit_idx * gran] )
+            break;
+
          unit = sched_alloc_unit_mem();
          if ( unit )
          {

I think the logic would be clearer if you wrap the entire for loop in if ( d->max_vcpus ).

Quoted 6 lines
Quoted 3 lines

And of course, this is wrong.  Turns out the domain_has_vcpus() predicate still hasn't been committed, but d->vcpu[0] is the correct internal.

Which in turn might want to be done by setting n_units to zero when d->vcpus[0] is NULL?

Yes, this would be possible.

OTOH my variant is more robust in case not all vcpus are allocated, but I guess this will explode somewhere else anyway.

In case I don't get any other comment today I'll change the patch to set n_units to 0 if d->vcpus[0] is NULL.

Juergen

#5 lore raw
In reply to Andrew Cooper
Quoted 22 lines · Andrew Cooper
Quoted 21 lines · Juergen Gross

In case a domain is created with a cpupool other than Pool-0 specified it will be moved to that cpupool before any vcpus are allocated.

This will lead to a NULL pointer dereference in sched_move_domain().

Fix that by tolerating vcpus not being allocated yet.

Fixes 70fadc41635b9b6 ("xen/cpupool: support moving domain between cpupools with different granularity")
---
  xen/common/sched/core.c | 4 ++++
  1 file changed, 4 insertions(+)
xen/common/sched/core.c +0 -0
index 8d178baf3d..79c9100680 100644
@@ -671,6 +671,10 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
Quoted 12 lines

xen/common/sched/core.c int sched_move_domain(struct domain *d, struct cpupool *c) · line 680

Quoted 9 lines
xen/common/sched/core.c +4 -0
      for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
      {
+        /* Special case for move at domain creation time. */
+        if ( !d->vcpu[unit_idx * gran] )
+            break;
+
          unit = sched_alloc_unit_mem();
          if ( unit )
          {

I think the logic would be clearer if you wrap the entire for loop in if ( d->max_vcpus ).

No, d->max_vcpus is not 0 here, otherwise n_units would be 0.

Quoted 5 lines

This loop is only allocating units in the new
scheduler for existing vcpus, so there's no point entering the loop at all during domain creation.

Also, this removes a non-speculatively-guarded d->vcpu[] deference.

I don't think this dereference is a real problem. In case you are worried about it we should replace the one further below, too.

Juergen