Skip to contentSkip to conversation

xen/sched: fix 2 bugs related to credit scheduler

PATCH In review Juergen Gross 3 participants 6 messages Jul 29

lore
Share & tools
lore
#0 lore raw

cover xen/sched: fix 2 bugs related to credit scheduler

Juergen Gross (2):
xen/credit: fix race with schedule_cpu_rm() in csched_timer() xen/sched: fix schedule_dump() handling of free cpus

 xen/common/sched/core.c   | 2 +-
 xen/common/sched/credit.c | 4 ++++
 2 files changed, 5 insertions(+), 1 deletion(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/2 xen/credit: fix race with schedule_cpu_rm() in csched_timer()

When removing a CPU from a cpupool running the credit scheduler, a race might happen between schedule_cpu_rm() and csched_timer(), resulting in a NULL dereference.

The timer associated with csched_timer() is killed only in csched_deinit_pdata(), which is called by schedule_cpu_rm() after setting the scheduler's per-cpu data to NULL AND after enabling interrupts again. This can result in the timer to fire before it is being killed, so csched_timer() needs to test the per-cpu data to be set before accessing it.

Fixes 78be3dbbfefa ("cpupools [1/6]: hypervisor changes")
---
 xen/common/sched/credit.c | 4 ++++
 1 file changed, 4 insertions(+)
xen/common/sched/credit.c +4 -0
index 328c802d0c..4dde2ede12 100644
@@ -1589,6 +1589,10 @@ static void cf_check csched_tick(void *_cpu)
     struct csched_pcpu *spc = CSCHED_PCPU(cpu);
     struct csched_private *prv = CSCHED_PRIV(sr->scheduler);
 
+    /* Handle race of timer disabling vs. firing when switching scheduler. */
+    if ( !spc )
+        return;
+
     spc->tick++;
 
     /*
Signature · 1 line
2.55.0
Acked-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] xen/credit: fix race with schedule_cpu_rm() in csched_timer()

Quoted 9 lines · Juergen Gross

When removing a CPU from a cpupool running the credit scheduler, a race might happen between schedule_cpu_rm() and csched_timer(), resulting in a NULL dereference.

The timer associated with csched_timer() is killed only in csched_deinit_pdata(), which is called by schedule_cpu_rm() after setting the scheduler's per-cpu data to NULL AND after enabling interrupts again. This can result in the timer to fire before it is being killed,

"... the timer firing before being killed,"

Quoted 6 lines

so csched_timer() needs to test the per-cpu data to be
set before accessing it.

Fixes 78be3dbbfefa ("cpupools [1/6]: hypervisor changes")
#3 lore raw

Re: [PATCH 1/2] xen/credit: fix race with schedule_cpu_rm() in csched_timer()

Quoted 13 lines · Andrew Cooper
Quoted 9 lines · Juergen Gross

When removing a CPU from a cpupool running the credit scheduler, a race might happen between schedule_cpu_rm() and csched_timer(), resulting in a NULL dereference.

The timer associated with csched_timer() is killed only in csched_deinit_pdata(), which is called by schedule_cpu_rm() after setting the scheduler's per-cpu data to NULL AND after enabling interrupts again. This can result in the timer to fire before it is being killed,

"... the timer firing before being killed,"

Quoted 9 lines
Quoted 6 lines

so csched_timer() needs to test the per-cpu data to be
set before accessing it.

Fixes 78be3dbbfefa ("cpupools [1/6]: hypervisor changes")

Despite the fact that the race is very unlikely, the fix is very simple and still resolves a race condition, so we could consider including it in 4.22:

Thanks.

~ Oleksii

#4 lore raw

PATCH 2/2 xen/sched: fix schedule_dump() handling of free cpus

In reply to Juergen Gross

With the introduction of core scheduling cpus not used by any cpupool have been switched to use the very simple idle scheduler instead of the default scheduler.

Trying to obtain diagnostic scheduling data via the "r" debug key is scanning through all cpupools using the per-cpupool scheduler to print the related data. Unfortunately the switch to use the idle scheduler for free cpus wasn't reflected in schedule_dump(), causing the default scheduler's dump_cpu_state callback to be used for free cpus.

This causes NULL dereferences in case the default scheduler is one of credit or null.

Fix that by using the idle scheduler instead of the default scheduler for free cpus. Note that it is fine for a scheduler to have no dump_cpu_state callback.

Fixes f855dd962523 ("sched: add minimalistic idle scheduler for free cpus")
---
 xen/common/sched/core.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/sched/core.c +1 -1
index 55b71301cf..9ccf5811bf 100644
@@ -3452,7 +3452,7 @@ void schedule_dump(struct cpupool *c)
     }
     else
     {
-        sched = &operations;
+        sched = &sched_idle_ops;
         cpus = &cpupool_free_cpus;
     }
 
Signature · 1 line
2.55.0
#5 lore raw

Re: [PATCH 2/2] xen/sched: fix schedule_dump() handling of free cpus

Quoted 1 line · Juergen Gross

With the introduction of core scheduling cpus not used by any cpupool

You want a comma between "scheduling, cpus".  It took a little while to figure out the correct parsing of this sentence.

As written, it's ambiguous with one interpretation being "With the introduction of core scheduling cpus, ..." but with a grammatically invalid remainder of the sentence.

Quoted 12 lines

have been switched to use the very simple idle scheduler instead of the default scheduler.

Trying to obtain diagnostic scheduling data via the "r" debug key is scanning through all cpupools using the per-cpupool scheduler to print the related data. Unfortunately the switch to use the idle scheduler for free cpus wasn't reflected in schedule_dump(), causing the default scheduler's dump_cpu_state callback to be used for free cpus.

This causes NULL dereferences in case the default scheduler is one of credit or null.

Quoted 8 lines

Fix that by using the idle scheduler instead of the default scheduler for free cpus. Note that it is fine for a scheduler to have no dump_cpu_state callback.

Fixes f855dd962523 ("sched: add minimalistic idle scheduler for free cpus")

FWIW, Acked-by: Andrew Cooper <[email protected]>