PREEMPT_RT: sched/rr, sched/fair: defer CFS scheduler put_prev_task()
PATCH 1/1 PREEMPT_RT: sched/rr, sched/fair: defer CFS scheduler put_prev_task()
Defer calling put_prev_task() on a CFS task_struct when there is a pending RT task to run. Instead wait until the next pick_next_task_fair() and do the work there.
The put_prev_task() call for a SCHED_OTHER task is currently a source of non determinism in the latency of scheduling a SCHED_FIFO task. This results in a priority inversion as the CFS scheduler is updating load average and balancing the rq rbtree while the SCHED_FIFO task is waiting to run.
Instrumented results on a quad core ARM A57. This is measured just across the put_prev_task() in pick_next_task_rt().
before patch
cpu: 0max_instr: 1114min_instr: 9avg_instr: 11min_time: 32nsmax_time: 2528nsavg_time: 64ns
cpu: 1max_instr: 1122min_instr: 9avg_instr: 11min_time: 32nsmax_time: 3040nsavg_time: 64ns
cpu: 2max_instr: 1118min_instr: 9avg_instr: 11min_time: 32nsmax_time: 3584nsavg_time: 64ns
cpu: 3max_instr: 1122min_instr: 9avg_instr: 11min_time: 32nsmax_time: 3456nsavg_time: 64ns
after patch
cpu: 0max_instr: 23min_instr: 9avg_instr: 11min_time: 64nsmax_time: 480nsavg_time: 64ns
cpu: 1max_instr: 23min_instr: 9avg_instr: 11min_time: 64nsmax_time: 224nsavg_time: 64ns
cpu: 2max_instr: 12min_instr: 9avg_instr: 10min_time: 64nsmax_time: 224nsavg_time: 64ns
cpu: 3max_instr: 44min_instr: 9avg_instr: 11min_time: 64nsmax_time: 160nsavg_time: 64ns
--- include/linux/sched.h | 4 ++++ kernel/sched/core.c | 4 ++++ kernel/sched/fair.c | 41 +++++++++++++++++++++++++++++++++++++++++ kernel/sched/rt.c | 6 ++++++ kernel/sched/sched.h | 3 +++ 5 files changed, 58 insertions(+)
include/linux/sched.h +4 -0
@@ -1126,6 +1126,10 @@ struct task_struct {void *security;#endif +#ifdef CONFIG_PREEMPT_RT_FULL +int rt_preempt; +#endif +/* * New fields for task_struct should be added above here, so that * they are included in the randomized portion of task_struct.
kernel/sched/core.c +4 -0
@@ -6016,6 +6016,10 @@ void __init sched_init(void)INIT_LIST_HEAD (&rq->cfs_tasks); +#ifdef CONFIG_PREEMPT_RT_FULL + rq->cfs_deferred_task =NULL ; +#endif +rq_attach_root (rq, &def_root_domain);#ifdef CONFIG_NO_HZ_COMMON rq->last_load_update_tick = jiffies;
kernel/sched/fair.c +41 -0
71 lines not on this page · raw message
kernel/sched/rt.c +6 -0
@@ -1571,7 +1571,13 @@ pick_next_task_rt(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)if (!rt_rq->rt_queued)return NULL ; +#ifdef CONFIG_PREEMPT_RT_FULL + prev->rt_preempt = true; +#endif put_prev_task (rq, prev); +#ifdef CONFIG_PREEMPT_RT_FULL + prev->rt_preempt = false; +#endif p =_pick_next_task_rt (rq);
kernel/sched/sched.h +3 -0
@@ -742,6 +742,9 @@ struct rq {int online;struct list_head cfs_tasks; +#ifdef CONFIG_PREEMPT_RT_FULL +struct task_struct *cfs_deferred_task; +#endif u64 rt_avg;u64 age_stamp;
Signature · 1 line
2.7.4
Quoted 3 lines · Allen Martin
Defer calling
put_prev_task()on a CFStask_structwhen there is a pending RT task to run. Instead wait until the nextpick_next_task_fair()and do the work there.
Which donates execution time of the rt class task to the fair class task, mucking up fairness. Nogo. To make that work, you'd have to at least squirrel away the time, but..
Quoted 5 lines
The
put_prev_task()call for aSCHED_OTHERtask is currently a source of non determinism in the latency of scheduling aSCHED_FIFOtask. This results in a priority inversion as the CFS scheduler is updating load average and balancing the rq rbtree while theSCHED_FIFOtask is waiting to run.
How can determinism really be improved by adding fast path cycles to move other fast path cycles from one fast path spot to another fast path spot? What prevents an rt task from trying to arrive while those merely relocated cycles are executing?
To make cycles cease and desist negatively affecting determinism, you have to make those cycles become an invariant, best version thereof being they become an invariant zero. Xenomai (co-kernel) tries to get closer to that optimal zero by making the entire kernel that fair class lives in go the hell away when rt wants to run, which drops average rt latency quite a bit, but worst case remained about the same in my light testing, rendering it's numbers the same as yours.. prettier on average, but not really any more deterministic.
-Mike
Peter Zijlstra
maintainerOn Wed, Oct 25, 2017 at 03:26:43PM -0700, Allen Martin wrote:
This is a _really_ ugly patch.