aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Bristot de Oliveira <bristot@redhat.com>2017-03-02 09:10:58 -0500
committerIngo Molnar <mingo@kernel.org>2017-03-16 04:37:38 -0400
commitdf8eac8cafce7d086be3bd5cf5a838fa37594dfb (patch)
tree43c74c1f15a452bccf9bf5cc469f9261d4d55d70
parent5ac69d37784b237707a7b15d199cdb6c6fdb6780 (diff)
sched/deadline: Throttle a constrained deadline task activated after the deadline
During the activation, CBS checks if it can reuse the current task's runtime and period. If the deadline of the task is in the past, CBS cannot use the runtime, and so it replenishes the task. This rule works fine for implicit deadline tasks (deadline == period), and the CBS was designed for implicit deadline tasks. However, a task with constrained deadline (deadine < period) might be awakened after the deadline, but before the next period. In this case, replenishing the task would allow it to run for runtime / deadline. As in this case deadline < period, CBS enables a task to run for more than the runtime / period. In a very loaded system, this can cause a domino effect, making other tasks miss their deadlines. To avoid this problem, in the activation of a constrained deadline task after the deadline but before the next period, throttle the task and set the replenishing timer to the begin of the next period, unless it is boosted. Reproducer: --------------- %< --------------- int main (int argc, char **argv) { int ret; int flags = 0; unsigned long l = 0; struct timespec ts; struct sched_attr attr; memset(&attr, 0, sizeof(attr)); attr.size = sizeof(attr); attr.sched_policy = SCHED_DEADLINE; attr.sched_runtime = 2 * 1000 * 1000; /* 2 ms */ attr.sched_deadline = 2 * 1000 * 1000; /* 2 ms */ attr.sched_period = 2 * 1000 * 1000 * 1000; /* 2 s */ ts.tv_sec = 0; ts.tv_nsec = 2000 * 1000; /* 2 ms */ ret = sched_setattr(0, &attr, flags); if (ret < 0) { perror("sched_setattr"); exit(-1); } for(;;) { /* XXX: you may need to adjust the loop */ for (l = 0; l < 150000; l++); /* * The ideia is to go to sleep right before the deadline * and then wake up before the next period to receive * a new replenishment. */ nanosleep(&ts, NULL); } exit(0); } --------------- >% --------------- On my box, this reproducer uses almost 50% of the CPU time, which is obviously wrong for a task with 2/2000 reservation. Signed-off-by: Daniel Bristot de Oliveira <bristot@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Juri Lelli <juri.lelli@arm.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Luca Abeni <luca.abeni@santannapisa.it> Cc: Mike Galbraith <efault@gmx.de> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Romulo Silva de Oliveira <romulo.deoliveira@ufsc.br> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Tommaso Cucinotta <tommaso.cucinotta@sssup.it> Link: http://lkml.kernel.org/r/edf58354e01db46bf42df8d2dd32418833f68c89.1488392936.git.bristot@redhat.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
-rw-r--r--kernel/sched/deadline.c45
1 files changed, 45 insertions, 0 deletions
diff --git a/kernel/sched/deadline.c b/kernel/sched/deadline.c
index 445e2787bf80..736d8b9d9bab 100644
--- a/kernel/sched/deadline.c
+++ b/kernel/sched/deadline.c
@@ -695,6 +695,37 @@ void init_dl_task_timer(struct sched_dl_entity *dl_se)
695 timer->function = dl_task_timer; 695 timer->function = dl_task_timer;
696} 696}
697 697
698/*
699 * During the activation, CBS checks if it can reuse the current task's
700 * runtime and period. If the deadline of the task is in the past, CBS
701 * cannot use the runtime, and so it replenishes the task. This rule
702 * works fine for implicit deadline tasks (deadline == period), and the
703 * CBS was designed for implicit deadline tasks. However, a task with
704 * constrained deadline (deadine < period) might be awakened after the
705 * deadline, but before the next period. In this case, replenishing the
706 * task would allow it to run for runtime / deadline. As in this case
707 * deadline < period, CBS enables a task to run for more than the
708 * runtime / period. In a very loaded system, this can cause a domino
709 * effect, making other tasks miss their deadlines.
710 *
711 * To avoid this problem, in the activation of a constrained deadline
712 * task after the deadline but before the next period, throttle the
713 * task and set the replenishing timer to the begin of the next period,
714 * unless it is boosted.
715 */
716static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
717{
718 struct task_struct *p = dl_task_of(dl_se);
719 struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se));
720
721 if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
722 dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
723 if (unlikely(dl_se->dl_boosted || !start_dl_timer(p)))
724 return;
725 dl_se->dl_throttled = 1;
726 }
727}
728
698static 729static
699int dl_runtime_exceeded(struct sched_dl_entity *dl_se) 730int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
700{ 731{
@@ -928,6 +959,11 @@ static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
928 __dequeue_dl_entity(dl_se); 959 __dequeue_dl_entity(dl_se);
929} 960}
930 961
962static inline bool dl_is_constrained(struct sched_dl_entity *dl_se)
963{
964 return dl_se->dl_deadline < dl_se->dl_period;
965}
966
931static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags) 967static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
932{ 968{
933 struct task_struct *pi_task = rt_mutex_get_top_task(p); 969 struct task_struct *pi_task = rt_mutex_get_top_task(p);
@@ -954,6 +990,15 @@ static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
954 } 990 }
955 991
956 /* 992 /*
993 * Check if a constrained deadline task was activated
994 * after the deadline but before the next period.
995 * If that is the case, the task will be throttled and
996 * the replenishment timer will be set to the next period.
997 */
998 if (!p->dl.dl_throttled && dl_is_constrained(&p->dl))
999 dl_check_constrained_dl(&p->dl);
1000
1001 /*
957 * If p is throttled, we do nothing. In fact, if it exhausted 1002 * If p is throttled, we do nothing. In fact, if it exhausted
958 * its budget it needs a replenishment and, since it now is on 1003 * its budget it needs a replenishment and, since it now is on
959 * its rq, the bandwidth timer callback (which clearly has not 1004 * its rq, the bandwidth timer callback (which clearly has not