Skip to contentSkip to conversation

PREEMPT_RT: sched/rr, sched/fair: defer CFS scheduler put_prev_task()

PATCH In review Allen Martin 3 participants 3 messages Oct 25, 2017 – Oct 26, 2017 Scheduler

lore
Share & tools
lore
#0 lore raw

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: 0
max_instr: 1114
min_instr: 9
avg_instr: 11
min_time: 32ns
max_time: 2528ns
avg_time: 64ns

cpu: 1
max_instr: 1122
min_instr: 9
avg_instr: 11
min_time: 32ns
max_time: 3040ns
avg_time: 64ns

cpu: 2
max_instr: 1118
min_instr: 9
avg_instr: 11
min_time: 32ns
max_time: 3584ns
avg_time: 64ns

cpu: 3
max_instr: 1122
min_instr: 9
avg_instr: 11
min_time: 32ns
max_time: 3456ns
avg_time: 64ns

after patch

cpu: 0
max_instr: 23
min_instr: 9
avg_instr: 11
min_time: 64ns
max_time: 480ns
avg_time: 64ns

cpu: 1
max_instr: 23
min_instr: 9
avg_instr: 11
min_time: 64ns
max_time: 224ns
avg_time: 64ns

cpu: 2
max_instr: 12
min_instr: 9
avg_instr: 10
min_time: 64ns
max_time: 224ns
avg_time: 64ns

cpu: 3
max_instr: 44
min_instr: 9
avg_instr: 11
min_time: 64ns
max_time: 160ns
avg_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
index e7ae9273a809..fdaf4ae2383e 100644
@@ -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
index 3a9899fc26f7..5cd3e1d25238 100644
@@ -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
index 3249423cdabd..45c7ff1a30ac 100644
@@ -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
index 72983b9066c5..0da3dd449676 100644
@@ -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
#1 lore raw
Quoted 3 lines · Allen Martin

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.

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 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.

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