Skip to contentSkip to conversation

xen/sched: fix crashes when vcpu creation fails

PATCH Superseded Furkan Caliskan 3 participants 16 messages Aug 19 – Sep 18

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover xen/sched: fix crashes when vcpu creation fails

Furkan Caliskan (2):
xen/sched: core: skip missing vcpu slots in sched_move_domain() xen/sched: core: kill unarmed timers on sched_init_vcpu() failure

 xen/common/sched/core.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v2 1/2 xen/sched: core: skip missing vcpu slots in sched_move_domain()

sched_move_domain() derives the number of units to rebuild from d->max_vcpus, which is fixed at domain creation and never rolled back if vcpu_create() fails partway through building a domain. So d->vcpu[i] can be NULL for some i even though max_vcpus still counts it - this happens if sched_alloc_udata() returns NULL.

The per-unit loop doesn't check for this: it sets unit->vcpu_list = d->vcpu[unit_id] (NULL) and hands that broken unit straight to the destination scheduler's alloc_udata(), which assumes vcpu_list is always valid and crashes Xen when it is not.

Reproduced by building a domain in a non-default cpupool where vcpu creation fails partway through, then destroying it. domain_kill() moves the domain back to the default cpupool via sched_move_domain() before actually destroying it, crashing inside the destination scheduler's alloc_udata() (seen in Credit2's csched2_alloc_udata() -> is_idle_unit() -> NULL deref).

Before building a unit in sched_move_domain(), check whether all vpcu slots belonging to that unit are populated. If any of its vpcus is missing:
- For a dying domain, skip the unit allocation.
- For an active domain, abort the move and return -EINVAL to prevent running with dropped vCPUs.

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

---
v2:
- Fail with -EINVAL if vcpu slots are missing in an active domain.
- Added Fixes: tag.

---
 xen/common/sched/core.c | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
xen/common/sched/core.c +32 -0
index d3a0a97e1d..a9daa42339 100644
@@ -745,6 +745,38 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
     for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
     {
+        /*
+         * A vcpu slot can be missing if creation failed partway
+         * through. A dying domain is being torn down regardless, so
+         * skip the unit -- but a domain that isn't dying still needs
+         * every vcpu it has schedulable, so fail instead of silently
+         * dropping some of them.
+         */
+        bool vcpu_failed = false;
+
+        for ( unsigned int i = 0;
+              i < gran && unit_idx * gran + i < d->max_vcpus; i++ )
+        {
+            if ( !d->vcpu[unit_idx * gran + i] )
+            {
+                vcpu_failed = true;
+                break;
+            }
+        }
+
+        if ( vcpu_failed )
+        {
+            if ( !d->is_dying )
+            {
+                sched_move_domain_cleanup(c->sched, new_units, domdata);
+                rcu_read_unlock(&sched_res_rculock);
+
+                return -EINVAL;
+            }
+
+            continue;
+        }
+
         unit = sched_alloc_unit_mem();
         if ( unit )
         {
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v2 1/2] xen/sched: core: skip missing vcpu slots in sched_move_domain()

Quoted 9 lines · Furkan Caliskan
xen/common/sched/core.c +3 -0
@@ -745,6 +745,38 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
     for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
     {
+        /*
+         * A vcpu slot can be missing if creation failed partway
+         * through. A dying domain is being torn down regardless, so

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

Quoted 10 lines
xen/common/sched/core.c +10 -0
+         * skip the unit -- but a domain that isn't dying still needs
+         * every vcpu it has schedulable, so fail instead of silently
+         * dropping some of them.
+         */
+        bool vcpu_failed = false;
+
+        for ( unsigned int i = 0;
+              i < gran && unit_idx * gran + i < d->max_vcpus; i++ )
+        {
+            if ( !d->vcpu[unit_idx * gran + i] )

Is there a particular reason domain_vcpu() cannot be used here?

Jan

#3 lore raw

Re: [PATCH v2 1/2] xen/sched: core: skip missing vcpu slots in sched_move_domain()

Quoted 23 lines · Jan Beulich
Quoted 9 lines · Furkan Caliskan
xen/common/sched/core.c +3 -0
@@ -745,6 +745,38 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
     for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
     {
+        /*
+         * A vcpu slot can be missing if creation failed partway
+         * through. A dying domain is being torn down regardless, so

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

Quoted 10 lines
xen/common/sched/core.c +10 -0
+         * skip the unit -- but a domain that isn't dying still needs
+         * every vcpu it has schedulable, so fail instead of silently
+         * dropping some of them.
+         */
+        bool vcpu_failed = false;
+
+        for ( unsigned int i = 0;
+              i < gran && unit_idx * gran + i < d->max_vcpus; i++ )
+        {
+            if ( !d->vcpu[unit_idx * gran + i] )

Is there a particular reason domain_vcpu() cannot be used here?

Quoted 1 line

Jan

We still need to guard against d->max_vcpus so that out-of-bounds indices in a partially filled unit don't get treated as missing vCPUs by domain_vcpu() returning NULL.

However, domain_vcpu(unit_idx * gran + i) can be used here instead of d->vcpu[unit_idx * gran + i]. I simply used d->vcpu[] because the rest of the function uses it that way.

Furkan

#4 lore raw

Re: [PATCH v2 1/2] xen/sched: core: skip missing vcpu slots in sched_move_domain()

Quoted 26 lines · Furkan Çalışkan
Quoted 14 lines · Jan Beulich
Quoted 13 lines · Furkan Caliskan
xen/common/sched/core.c +7 -0
@@ -745,6 +745,38 @@ int sched_move_domain(struct domain *d, struct cpupool *c)
 
     for ( unit_idx = 0; unit_idx < n_units; unit_idx++ )
     {
+        /*
+         * A vcpu slot can be missing if creation failed partway
+         * through. A dying domain is being torn down regardless, so
+         * skip the unit -- but a domain that isn't dying still needs
+         * every vcpu it has schedulable, so fail instead of silently
+         * dropping some of them.
+         */
Quoted 8 lines

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

Quoted 6 lines
xen/common/sched/core.c +6 -0
+        bool vcpu_failed = false;
+
+        for ( unsigned int i = 0;
+              i < gran && unit_idx * gran + i < d->max_vcpus; i++ )
+        {
+            if ( !d->vcpu[unit_idx * gran + i] )

Is there a particular reason domain_vcpu() cannot be used here?

Quoted 3 lines

We still need to guard against d->max_vcpus so that out-of-bounds indices in a partially filled unit don't get treated as missing vCPUs by domain_vcpu() returning NULL.

Ah, right - the bounds check cannot really be folded here. Then ...

Quoted 3 lines

However, domain_vcpu(unit_idx * gran + i) can be used here instead of d->vcpu[unit_idx * gran + i]. I simply used d->vcpu[] because the rest of the function uses it that way.

... best wait for Jürgen to comment. Outside of the scheduler we're trying to replace open-coding of domain_vcpu(), but inside the scheduler things may be different.

Jan

#5 lore raw

Re: [PATCH v2 1/2] xen/sched: core: skip missing vcpu slots in sched_move_domain()

In reply to Furkan Caliskan
Quoted 26 lines · Furkan Caliskan

sched_move_domain() derives the number of units to rebuild from d->max_vcpus, which is fixed at domain creation and never rolled back if vcpu_create() fails partway through building a domain. So d->vcpu[i] can be NULL for some i even though max_vcpus still counts it - this happens if sched_alloc_udata() returns NULL.

The per-unit loop doesn't check for this: it sets unit->vcpu_list = d->vcpu[unit_id] (NULL) and hands that broken unit straight to the destination scheduler's alloc_udata(), which assumes vcpu_list is always valid and crashes Xen when it is not.

Reproduced by building a domain in a non-default cpupool where vcpu creation fails partway through, then destroying it. domain_kill() moves the domain back to the default cpupool via sched_move_domain() before actually destroying it, crashing inside the destination scheduler's alloc_udata() (seen in Credit2's csched2_alloc_udata() -> is_idle_unit() -> NULL deref).

Before building a unit in sched_move_domain(), check whether all vpcu slots belonging to that unit are populated. If any of its vpcus is missing:
- For a dying domain, skip the unit allocation.
- For an active domain, abort the move and return -EINVAL to prevent running with dropped vCPUs.

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

Sorry for realizing this only now, but I think this problem should be solved completely differently.

Today there are multiple places in the hypervisor where vcpus are being setup via vcpu_create(). For vcpu-ids other than 0 this is always done in a loop with ascending ids, up to d->max_vcpus. Whenever vcpu_create() is failing, the loop is terminated. The only special case is the idle-domain, which will never have your problem.

So the easy fix would be to:

- have only one function vcpus_create() creating all the vcpus for a domain and use this function everywhere instead of said loop

- in case a single vcpu can't be created by vcpus_create(), d->max_vcpus should be reset to the id of the vcpu which couldn't be created

This will avoid any potential NULL derefs elsewhere, as vcpu loops will just be ending before hitting a NULL pointer.

In case you are not feeling comfortable writing this patch, just speak up and I will do it.

Juergen

#6 lore raw

Re: [PATCH v2 1/2] xen/sched: core: skip missing vcpu slots in sched_move_domain()

Quoted 47 lines · Juergen Gross
Quoted 26 lines · Furkan Caliskan

sched_move_domain() derives the number of units to rebuild from d->max_vcpus, which is fixed at domain creation and never rolled back if vcpu_create() fails partway through building a domain. So d->vcpu[i] can be NULL for some i even though max_vcpus still counts it - this happens if sched_alloc_udata() returns NULL.

The per-unit loop doesn't check for this: it sets unit->vcpu_list = d->vcpu[unit_id] (NULL) and hands that broken unit straight to the destination scheduler's alloc_udata(), which assumes vcpu_list is always valid and crashes Xen when it is not.

Reproduced by building a domain in a non-default cpupool where vcpu creation fails partway through, then destroying it. domain_kill() moves the domain back to the default cpupool via sched_move_domain() before actually destroying it, crashing inside the destination scheduler's alloc_udata() (seen in Credit2's csched2_alloc_udata() -> is_idle_unit() -> NULL deref).

Before building a unit in sched_move_domain(), check whether all vpcu slots belonging to that unit are populated. If any of its vpcus is missing:
  - For a dying domain, skip the unit allocation.
  - For an active domain, abort the move and return -EINVAL to prevent running with dropped vCPUs.

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

Sorry for realizing this only now, but I think this problem should be solved completely differently.

Today there are multiple places in the hypervisor where vcpus are being setup via vcpu_create(). For vcpu-ids other than 0 this is always done in a loop with ascending ids, up to d->max_vcpus. Whenever vcpu_create() is failing, the loop is terminated. The only special case is the idle-domain, which will never have your problem.

So the easy fix would be to:

- have only one function vcpus_create() creating all the vcpus for a domain and use this function everywhere instead of said loop

- in case a single vcpu can't be created by vcpus_create(), d->max_vcpus should be reset to the id of the vcpu which couldn't be created

Quoted 8 lines

This will avoid any potential NULL derefs elsewhere, as vcpu loops will just be ending before hitting a NULL pointer.

In case you are not feeling comfortable writing this patch, just speak up and I will do it.

Juergen

Agreed, that's the better approach. I'll take it on and send a new version.

Furkan

Reviewed-by on v2 1/2
#7 lore raw

Re: [PATCH v2 1/2] xen/sched: core: skip missing vcpu slots in sched_move_domain()

In reply to Furkan Caliskan
Quoted 26 lines · Furkan Caliskan

sched_move_domain() derives the number of units to rebuild from d->max_vcpus, which is fixed at domain creation and never rolled back if vcpu_create() fails partway through building a domain. So d->vcpu[i] can be NULL for some i even though max_vcpus still counts it - this happens if sched_alloc_udata() returns NULL.

The per-unit loop doesn't check for this: it sets unit->vcpu_list = d->vcpu[unit_id] (NULL) and hands that broken unit straight to the destination scheduler's alloc_udata(), which assumes vcpu_list is always valid and crashes Xen when it is not.

Reproduced by building a domain in a non-default cpupool where vcpu creation fails partway through, then destroying it. domain_kill() moves the domain back to the default cpupool via sched_move_domain() before actually destroying it, crashing inside the destination scheduler's alloc_udata() (seen in Credit2's csched2_alloc_udata() -> is_idle_unit() -> NULL deref).

Before building a unit in sched_move_domain(), check whether all vpcu slots belonging to that unit are populated. If any of its vpcus is missing:
- For a dying domain, skip the unit allocation.
- For an active domain, abort the move and return -EINVAL to prevent running with dropped vCPUs.

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

With the V3 approach not being easily doable:

Juergen

#8 lore raw

PATCH v2 2/2 xen/sched: core: kill unarmed timers on sched_init_vcpu() failure

In reply to Furkan Caliskan

sched_init_vcpu() calls init_timer() for a vcpu's periodic_timer, singleshot_timer and poll_timer before it can fail -- these become live, linked into their target pCPU's per-cpu timer list regardless of what happens next. If the sched_alloc_udata() call further down then fails, the function frees the sched_unit via sched_free_unit() and returns 1, but never unlinks these three timers.

The caller, vcpu_create(), makes this worse: on sched_init_vcpu() returning nonzero it jumps to fail_wq, skipping fail_sched and thus sched_destroy_vcpu() -- the only function on this path that calls kill_timer() on them. vcpu_destroy() then frees the vcpu, and the three timers embedded in it, while they are still linked into that shared list.

This silently corrupts that list. It only shows up later, when something else touches a neighboring timer: sched_move_domain() crashed with "Assertion 'entry->prev->next == entry' failed" on a completely unrelated, valid vcpu's timer.

Kill all three timers in sched_init_vcpu()'s own failure branch, so it doesn't depend on the caller reaching sched_destroy_vcpu() to undo what it set up itself.

Fixes 1ad5dad74cde ("[XEN] Re-jig VCPU initialisation -- VMX init requires generic VCPU")

---
v2:
- Added Fixes: tag.

---
 xen/common/sched/core.c | 3 +++
 1 file changed, 3 insertions(+)
xen/common/sched/core.c +3 -0
index a9daa42339..5777096592 100644
@@ -589,6 +589,9 @@ int sched_init_vcpu(struct vcpu *v)
     unit->priv = sched_alloc_udata(dom_scheduler(d), unit, d->sched_priv);
     if ( unit->priv == NULL )
     {
+        kill_timer(&v->periodic_timer);
+        kill_timer(&v->singleshot_timer);
+        kill_timer(&v->poll_timer);
         sched_free_unit(unit, v);
         rcu_read_unlock(&sched_res_rculock);
         return 1;
Signature · 1 line
2.34.1
#9 lore raw

Re: [PATCH v2 2/2] xen/sched: core: kill unarmed timers on sched_init_vcpu() failure

Quoted 20 lines · Furkan Caliskan

sched_init_vcpu() calls init_timer() for a vcpu's periodic_timer, singleshot_timer and poll_timer before it can fail -- these become live, linked into their target pCPU's per-cpu timer list regardless of what happens next. If the sched_alloc_udata() call further down then fails, the function frees the sched_unit via sched_free_unit() and returns 1, but never unlinks these three timers.

The caller, vcpu_create(), makes this worse: on sched_init_vcpu() returning nonzero it jumps to fail_wq, skipping fail_sched and thus sched_destroy_vcpu() -- the only function on this path that calls kill_timer() on them. vcpu_destroy() then frees the vcpu, and the three timers embedded in it, while they are still linked into that shared list.

This silently corrupts that list. It only shows up later, when something else touches a neighboring timer: sched_move_domain() crashed with "Assertion 'entry->prev->next == entry' failed" on a completely unrelated, valid vcpu's timer.

Quoted 5 lines

Kill all three timers in sched_init_vcpu()'s own failure branch, so it doesn't depend on the caller reaching sched_destroy_vcpu() to undo what it set up itself.

Fixes 1ad5dad74cde ("[XEN] Re-jig VCPU initialisation -- VMX init requires generic VCPU")

How did you arrive at this commit? It doesn't even touch sched_init_vcpu(). All it does is move kill_timer() invocations around. I think it's d884b1077817, as that's where the "return SCHED_OP(init_vcpu, v)" was introduced (i.e. where kill_timer() would have been necessary to call in the error case). (I can't exclude the issue was pre-existing already at that time, but that would require more analysis than I think is worth to invest.)

xen/common/sched/core.c int sched_init_vcpu(struct vcpu *v) · line 597

Quoted 12 lines
xen/common/sched/core.c +3 -0
@@ -589,6 +589,9 @@ int sched_init_vcpu(struct vcpu *v)
     unit->priv = sched_alloc_udata(dom_scheduler(d), unit, d->sched_priv);
     if ( unit->priv == NULL )
     {
+        kill_timer(&v->periodic_timer);
+        kill_timer(&v->singleshot_timer);
+        kill_timer(&v->poll_timer);
         sched_free_unit(unit, v);
         rcu_read_unlock(&sched_res_rculock);
         return 1;

This almost, but not quite open-codes sched_destroy_vcpu(). Would be nice if the cleanup logic was shared. The sched_free_unit() call there could be leveraged here as well; what would need skipping are the sched_free_udata() and sched_remove_unit(). And of course the RCU-locking would need sorting.

Jan

#10 lore raw

Re: [PATCH v2 2/2] xen/sched: core: kill unarmed timers on sched_init_vcpu() failure

Quoted 27 lines · Jan Beulich
Quoted 20 lines · Furkan Caliskan

sched_init_vcpu() calls init_timer() for a vcpu's periodic_timer, singleshot_timer and poll_timer before it can fail -- these become live, linked into their target pCPU's per-cpu timer list regardless of what happens next. If the sched_alloc_udata() call further down then fails, the function frees the sched_unit via sched_free_unit() and returns 1, but never unlinks these three timers.

The caller, vcpu_create(), makes this worse: on sched_init_vcpu() returning nonzero it jumps to fail_wq, skipping fail_sched and thus sched_destroy_vcpu() -- the only function on this path that calls kill_timer() on them. vcpu_destroy() then frees the vcpu, and the three timers embedded in it, while they are still linked into that shared list.

This silently corrupts that list. It only shows up later, when something else touches a neighboring timer: sched_move_domain() crashed with "Assertion 'entry->prev->next == entry' failed" on a completely unrelated, valid vcpu's timer.

Quoted 5 lines

Kill all three timers in sched_init_vcpu()'s own failure branch, so it doesn't depend on the caller reaching sched_destroy_vcpu() to undo what it set up itself.

Fixes 1ad5dad74cde ("[XEN] Re-jig VCPU initialisation -- VMX init requires generic VCPU")
Quoted 7 lines

How did you arrive at this commit? It doesn't even touch sched_init_vcpu(). All it does is move kill_timer() invocations around. I think it's d884b1077817, as that's where the "return SCHED_OP(init_vcpu, v)" was introduced (i.e. where kill_timer() would have been necessary to call in the error case). (I can't exclude the issue was pre-existing already at that time, but that would require more analysis than I think is worth to invest.)

Commit 1ad5dad74cde moved kill_timer() calls into sched_destroy_vcpu() function, which is not called if sched_init_vcpu() fails. Before that commit, kill_timer() calls were in sched_destroy_domain(), which is called if sched_init_vcpu() returns non-zero to its caller, alloc_vcpu().

        for ( i = 0; i < max; i++ )
        {
            if ( d->vcpu[i] != NULL )
                continue;

            cpu = (i == 0) ?
                default_vcpu0_location() :
                (d->vcpu[i-1]->processor + 1) % num_online_cpus();

            if ( alloc_vcpu(d, i, cpu) == NULL )
                goto maxvcpu_out;
        }

        ret = 0;

    maxvcpu_out:
        domain_unpause(d);
        put_domain(d);
    }
    break;

put_domain() calls domain_destroy(), which then calls free_domain(), which ultimately calls sched_destroy_domain().

Quoted 19 lines

xen/common/sched/core.c int sched_init_vcpu(struct vcpu *v) · line 597

Quoted 13 lines
xen/common/sched/core.c +3 -0
@@ -589,6 +589,9 @@ int sched_init_vcpu(struct vcpu *v)
     unit->priv = sched_alloc_udata(dom_scheduler(d), unit, d->sched_priv);
     if ( unit->priv == NULL )
     {
+        kill_timer(&v->periodic_timer);
+        kill_timer(&v->singleshot_timer);
+        kill_timer(&v->poll_timer);
         sched_free_unit(unit, v);
         rcu_read_unlock(&sched_res_rculock);
         return 1;

This almost, but not quite open-codes sched_destroy_vcpu(). Would be nice if the cleanup logic was shared. The sched_free_unit() call there could be leveraged here as well; what would need skipping are the sched_free_udata() and sched_remove_unit(). And of course the RCU-locking would need sorting.

Quoted 1 line

Jan

Furkan

#11 lore raw

Re: [PATCH v2 2/2] xen/sched: core: kill unarmed timers on sched_init_vcpu() failure

Quoted 65 lines · Furkan Çalışkan
Quoted 27 lines · Jan Beulich
Quoted 20 lines · Furkan Caliskan

sched_init_vcpu() calls init_timer() for a vcpu's periodic_timer, singleshot_timer and poll_timer before it can fail -- these become live, linked into their target pCPU's per-cpu timer list regardless of what happens next. If the sched_alloc_udata() call further down then fails, the function frees the sched_unit via sched_free_unit() and returns 1, but never unlinks these three timers.

The caller, vcpu_create(), makes this worse: on sched_init_vcpu() returning nonzero it jumps to fail_wq, skipping fail_sched and thus sched_destroy_vcpu() -- the only function on this path that calls kill_timer() on them. vcpu_destroy() then frees the vcpu, and the three timers embedded in it, while they are still linked into that shared list.

This silently corrupts that list. It only shows up later, when something else touches a neighboring timer: sched_move_domain() crashed with "Assertion 'entry->prev->next == entry' failed" on a completely unrelated, valid vcpu's timer.

Quoted 5 lines

Kill all three timers in sched_init_vcpu()'s own failure branch, so it doesn't depend on the caller reaching sched_destroy_vcpu() to undo what it set up itself.

Fixes 1ad5dad74cde ("[XEN] Re-jig VCPU initialisation -- VMX init requires generic VCPU")
Quoted 7 lines

How did you arrive at this commit? It doesn't even touch sched_init_vcpu(). All it does is move kill_timer() invocations around. I think it's d884b1077817, as that's where the "return SCHED_OP(init_vcpu, v)" was introduced (i.e. where kill_timer() would have been necessary to call in the error case). (I can't exclude the issue was pre-existing already at that time, but that would require more analysis than I think is worth to invest.)

Commit 1ad5dad74cde moved kill_timer() calls into sched_destroy_vcpu() function, which is not called if sched_init_vcpu() fails. Before that commit, kill_timer() calls were in sched_destroy_domain(), which is called if sched_init_vcpu() returns non-zero to its caller, alloc_vcpu().

        for ( i = 0; i < max; i++ )
        {
            if ( d->vcpu[i] != NULL )
                continue;

            cpu = (i == 0) ?
                default_vcpu0_location() :
                (d->vcpu[i-1]->processor + 1) % num_online_cpus();

            if ( alloc_vcpu(d, i, cpu) == NULL )
                goto maxvcpu_out;
        }

        ret = 0;

    maxvcpu_out:
        domain_unpause(d);
        put_domain(d);
    }
    break;
Quoted 2 lines

put_domain() calls domain_destroy(), which then calls free_domain(), which ultimately calls sched_destroy_domain().

Well, yes, except that - how would that have helped for a vCPU which failed to be properly constructed? The function loops over all vCPU-s in the domain, but that wouldn't include the vCPU in question. alloc_vcpu() would (of course) insert the vCPU into the list only when sched_init_vcpu() succeeds.

Jan

#12 lore raw

Re: [PATCH v2 2/2] xen/sched: core: kill unarmed timers on sched_init_vcpu() failure

Quoted 69 lines · Jan Beulich
Quoted 65 lines · Furkan Çalışkan
Quoted 27 lines · Jan Beulich
Quoted 20 lines · Furkan Caliskan

sched_init_vcpu() calls init_timer() for a vcpu's periodic_timer, singleshot_timer and poll_timer before it can fail -- these become live, linked into their target pCPU's per-cpu timer list regardless of what happens next. If the sched_alloc_udata() call further down then fails, the function frees the sched_unit via sched_free_unit() and returns 1, but never unlinks these three timers.

The caller, vcpu_create(), makes this worse: on sched_init_vcpu() returning nonzero it jumps to fail_wq, skipping fail_sched and thus sched_destroy_vcpu() -- the only function on this path that calls kill_timer() on them. vcpu_destroy() then frees the vcpu, and the three timers embedded in it, while they are still linked into that shared list.

This silently corrupts that list. It only shows up later, when something else touches a neighboring timer: sched_move_domain() crashed with "Assertion 'entry->prev->next == entry' failed" on a completely unrelated, valid vcpu's timer.

Quoted 5 lines

Kill all three timers in sched_init_vcpu()'s own failure branch, so it doesn't depend on the caller reaching sched_destroy_vcpu() to undo what it set up itself.

Fixes 1ad5dad74cde ("[XEN] Re-jig VCPU initialisation -- VMX init requires generic VCPU")
Quoted 7 lines

How did you arrive at this commit? It doesn't even touch sched_init_vcpu(). All it does is move kill_timer() invocations around. I think it's d884b1077817, as that's where the "return SCHED_OP(init_vcpu, v)" was introduced (i.e. where kill_timer() would have been necessary to call in the error case). (I can't exclude the issue was pre-existing already at that time, but that would require more analysis than I think is worth to invest.)

Commit 1ad5dad74cde moved kill_timer() calls into sched_destroy_vcpu() function, which is not called if sched_init_vcpu() fails. Before that commit, kill_timer() calls were in sched_destroy_domain(), which is called if sched_init_vcpu() returns non-zero to its caller, alloc_vcpu().

        for ( i = 0; i < max; i++ )
        {
            if ( d->vcpu[i] != NULL )
                continue;

            cpu = (i == 0) ?
                default_vcpu0_location() :
                (d->vcpu[i-1]->processor + 1) % num_online_cpus();

            if ( alloc_vcpu(d, i, cpu) == NULL )
                goto maxvcpu_out;
        }

        ret = 0;

    maxvcpu_out:
        domain_unpause(d);
        put_domain(d);
    }
    break;
Quoted 2 lines

put_domain() calls domain_destroy(), which then calls free_domain(), which ultimately calls sched_destroy_domain().

Quoted 7 lines

Well, yes, except that - how would that have helped for a vCPU which failed to be properly constructed? The function loops over all vCPU-s in the domain, but that wouldn't include the vCPU in question. alloc_vcpu() would (of course) insert the vCPU into the list only when sched_init_vcpu() succeeds.

Jan

Ah, I missed that compeletely - you're right. sched_destroy_domain() wouldn't have reached the failed vCPU anyway.

In that case, d884b1077817 makes total sense here.

Furkan

#13 lore raw

Re: [PATCH v2 2/2] xen/sched: core: kill unarmed timers on sched_init_vcpu() failure

In reply to Jan Beulich
Quoted 53 lines · Jan Beulich
Quoted 20 lines · Furkan Caliskan

sched_init_vcpu() calls init_timer() for a vcpu's periodic_timer, singleshot_timer and poll_timer before it can fail -- these become live, linked into their target pCPU's per-cpu timer list regardless of what happens next. If the sched_alloc_udata() call further down then fails, the function frees the sched_unit via sched_free_unit() and returns 1, but never unlinks these three timers.

The caller, vcpu_create(), makes this worse: on sched_init_vcpu() returning nonzero it jumps to fail_wq, skipping fail_sched and thus sched_destroy_vcpu() -- the only function on this path that calls kill_timer() on them. vcpu_destroy() then frees the vcpu, and the three timers embedded in it, while they are still linked into that shared list.

This silently corrupts that list. It only shows up later, when something else touches a neighboring timer: sched_move_domain() crashed with "Assertion 'entry->prev->next == entry' failed" on a completely unrelated, valid vcpu's timer.

Quoted 5 lines

Kill all three timers in sched_init_vcpu()'s own failure branch, so it doesn't depend on the caller reaching sched_destroy_vcpu() to undo what it set up itself.

Fixes 1ad5dad74cde ("[XEN] Re-jig VCPU initialisation -- VMX init requires generic VCPU")

How did you arrive at this commit? It doesn't even touch sched_init_vcpu(). All it does is move kill_timer() invocations around. I think it's d884b1077817, as that's where the "return SCHED_OP(init_vcpu, v)" was introduced (i.e. where kill_timer() would have been necessary to call in the error case). (I can't exclude the issue was pre-existing already at that time, but that would require more analysis than I think is worth to invest.)

xen/common/sched/core.c int sched_init_vcpu(struct vcpu *v) · line 597

Quoted 12 lines
xen/common/sched/core.c +3 -0
@@ -589,6 +589,9 @@ int sched_init_vcpu(struct vcpu *v)
     unit->priv = sched_alloc_udata(dom_scheduler(d), unit, d->sched_priv);
     if ( unit->priv == NULL )
     {
+        kill_timer(&v->periodic_timer);
+        kill_timer(&v->singleshot_timer);
+        kill_timer(&v->poll_timer);
         sched_free_unit(unit, v);
         rcu_read_unlock(&sched_res_rculock);
         return 1;

This almost, but not quite open-codes sched_destroy_vcpu(). Would be nice if the cleanup logic was shared. The sched_free_unit() call there could be leveraged here as well; what would need skipping are the sched_free_udata() and sched_remove_unit(). And of course the RCU-locking would need sorting.

Quoted 1 line

Jan

Would something like below be okay?

xen/common/sched/core.c +6 -3
index d3a0a97e1d..36704a5836 100644
@@ -589,8 +589,8 @@ int sched_init_vcpu(struct vcpu *v)
     unit->priv = sched_alloc_udata(dom_scheduler(d), unit, d->sched_priv);
     if ( unit->priv == NULL )
     {
-        sched_free_unit(unit, v);
         rcu_read_unlock(&sched_res_rculock);
+        sched_destroy_vcpu(v);
         return 1;
     }
 
@@ -869,8 +869,11 @@ void sched_destroy_vcpu(struct vcpu *v)
     {
         rcu_read_lock(&sched_res_rculock);
 
-        sched_remove_unit(vcpu_scheduler(v), unit);
-        sched_free_udata(vcpu_scheduler(v), unit->priv);
+        if ( unit->priv )
+        {
+            sched_remove_unit(vcpu_scheduler(v), unit);
+            sched_free_udata(vcpu_scheduler(v), unit->priv);
+        }
         sched_free_unit(unit, v);
 
         rcu_read_unlock(&sched_res_rculock);

Furkan

#14 lore raw

Re: [PATCH v2 2/2] xen/sched: core: kill unarmed timers on sched_init_vcpu() failure

Quoted 20 lines · Furkan Çalışkan
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Furkan Caliskan
xen/common/sched/core.c +3 -0
@@ -589,6 +589,9 @@ int sched_init_vcpu(struct vcpu *v)
     unit->priv = sched_alloc_udata(dom_scheduler(d), unit, d->sched_priv);
     if ( unit->priv == NULL )
     {
+        kill_timer(&v->periodic_timer);
+        kill_timer(&v->singleshot_timer);
+        kill_timer(&v->poll_timer);
Quoted 8 lines

xen/common/sched/core.c void sched_destroy_vcpu(struct vcpu *v)

Quoted 3 lines
xen/common/sched/core.c +0 -0
         sched_free_unit(unit, v);
         rcu_read_unlock(&sched_res_rculock);
         return 1;

This almost, but not quite open-codes sched_destroy_vcpu(). Would be nice if the cleanup logic was shared. The sched_free_unit() call there could be leveraged here as well; what would need skipping are the sched_free_udata() and sched_remove_unit(). And of course the RCU-locking would need sorting.

Quoted 1 line

Would something like below be okay?

Maybe, but you need to ask the maintainers of this code, which I'm not a part of. What I in particular can't easily judge is whether ...

xen/common/sched/core.c int sched_init_vcpu(struct vcpu *v) · line 595

Quoted 11 lines
xen/common/sched/core.c +1 -1
@@ -589,8 +589,8 @@ int sched_init_vcpu(struct vcpu *v)
     unit->priv = sched_alloc_udata(dom_scheduler(d), unit, d->sched_priv);
     if ( unit->priv == NULL )
     {
-        sched_free_unit(unit, v);
         rcu_read_unlock(&sched_res_rculock);
+        sched_destroy_vcpu(v);
         return 1;
     }

... this intermediate dropping of the lock is entirely okay (it looks to be at the first glance).

Jan

Quoted 17 lines
xen/common/sched/core.c +5 -2
@@ -869,8 +869,11 @@ void sched_destroy_vcpu(struct vcpu *v)
     {
         rcu_read_lock(&sched_res_rculock);
 
-        sched_remove_unit(vcpu_scheduler(v), unit);
-        sched_free_udata(vcpu_scheduler(v), unit->priv);
+        if ( unit->priv )
+        {
+            sched_remove_unit(vcpu_scheduler(v), unit);
+            sched_free_udata(vcpu_scheduler(v), unit->priv);
+        }
         sched_free_unit(unit, v);
 
         rcu_read_unlock(&sched_res_rculock);

Furkan

#15 lore raw

Re: [PATCH v2 2/2] xen/sched: core: kill unarmed timers on sched_init_vcpu() failure

Quoted 26 lines · Jan Beulich
Quoted 20 lines · Furkan Çalışkan
Quoted 10 lines · Jan Beulich
Quoted 9 lines · Furkan Caliskan
xen/common/sched/core.c +3 -0
@@ -589,6 +589,9 @@ int sched_init_vcpu(struct vcpu *v)
      unit->priv = sched_alloc_udata(dom_scheduler(d), unit, d->sched_priv);
      if ( unit->priv == NULL )
      {
+        kill_timer(&v->periodic_timer);
+        kill_timer(&v->singleshot_timer);
+        kill_timer(&v->poll_timer);
Quoted 8 lines

xen/common/sched/core.c void sched_destroy_vcpu(struct vcpu *v)

Quoted 3 lines
xen/common/sched/core.c +0 -0
          sched_free_unit(unit, v);
          rcu_read_unlock(&sched_res_rculock);
          return 1;

This almost, but not quite open-codes sched_destroy_vcpu(). Would be nice if the cleanup logic was shared. The sched_free_unit() call there could be leveraged here as well; what would need skipping are the sched_free_udata() and sched_remove_unit(). And of course the RCU-locking would need sorting.

Quoted 1 line

Would something like below be okay?

Maybe, but you need to ask the maintainers of this code, which I'm not a part of. What I in particular can't easily judge is whether ...

Quoted 14 lines

xen/common/sched/core.c int sched_init_vcpu(struct vcpu *v) · line 595

Quoted 11 lines
xen/common/sched/core.c +1 -1
@@ -589,8 +589,8 @@ int sched_init_vcpu(struct vcpu *v)
      unit->priv = sched_alloc_udata(dom_scheduler(d), unit, d->sched_priv);
      if ( unit->priv == NULL )
      {
-        sched_free_unit(unit, v);
          rcu_read_unlock(&sched_res_rculock);
+        sched_destroy_vcpu(v);
          return 1;
      }

... this intermediate dropping of the lock is entirely okay (it looks to be at the first glance).

Why don't you just replace sched_free_unit() with sched_destroy_vcpu()?

rcu_read_lock() calls can be nested, even for the same lock. This works because there is no rcu_write_lock() (see comment in rcupdate.h).

Juergen