Skip to content

Commit 2d3d891

Browse files
dfaggioliingomolnar
authored andcommitted
sched/deadline: Add SCHED_DEADLINE inheritance logic
Some method to deal with rt-mutexes and make sched_dl interact with the current PI-coded is needed, raising all but trivial issues, that needs (according to us) to be solved with some restructuring of the pi-code (i.e., going toward a proxy execution-ish implementation). This is under development, in the meanwhile, as a temporary solution, what this commits does is: - ensure a pi-lock owner with waiters is never throttled down. Instead, when it runs out of runtime, it immediately gets replenished and it's deadline is postponed; - the scheduling parameters (relative deadline and default runtime) used for that replenishments --during the whole period it holds the pi-lock-- are the ones of the waiting task with earliest deadline. Acting this way, we provide some kind of boosting to the lock-owner, still by using the existing (actually, slightly modified by the previous commit) pi-architecture. We would stress the fact that this is only a surely needed, all but clean solution to the problem. In the end it's only a way to re-start discussion within the community. So, as always, comments, ideas, rants, etc.. are welcome! :-) Signed-off-by: Dario Faggioli <raistlin@linux.it> Signed-off-by: Juri Lelli <juri.lelli@gmail.com> [ Added !RT_MUTEXES build fix. ] Signed-off-by: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/1383831828-15501-11-git-send-email-juri.lelli@gmail.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent fb00aca commit 2d3d891

9 files changed

Lines changed: 134 additions & 54 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1124,8 +1124,12 @@ struct sched_dl_entity {
11241124
* @dl_new tells if a new instance arrived. If so we must
11251125
* start executing it with full runtime and reset its absolute
11261126
* deadline;
1127+
*
1128+
* @dl_boosted tells if we are boosted due to DI. If so we are
1129+
* outside bandwidth enforcement mechanism (but only until we
1130+
* exit the critical section).
11271131
*/
1128-
int dl_throttled, dl_new;
1132+
int dl_throttled, dl_new, dl_boosted;
11291133

11301134
/*
11311135
* Bandwidth enforcement timer. Each -deadline task has its
@@ -1359,6 +1363,8 @@ struct task_struct {
13591363
struct rb_node *pi_waiters_leftmost;
13601364
/* Deadlock detection and priority inheritance handling */
13611365
struct rt_mutex_waiter *pi_blocked_on;
1366+
/* Top pi_waiters task */
1367+
struct task_struct *pi_top_task;
13621368
#endif
13631369

13641370
#ifdef CONFIG_DEBUG_MUTEXES

‎include/linux/sched/rt.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static inline int rt_task(struct task_struct *p)
3535
#ifdef CONFIG_RT_MUTEXES
3636
extern int rt_mutex_getprio(struct task_struct *p);
3737
extern void rt_mutex_setprio(struct task_struct *p, int prio);
38+
extern struct task_struct *rt_mutex_get_top_task(struct task_struct *task);
3839
extern void rt_mutex_adjust_pi(struct task_struct *p);
3940
static inline bool tsk_is_pi_blocked(struct task_struct *tsk)
4041
{
@@ -45,6 +46,10 @@ static inline int rt_mutex_getprio(struct task_struct *p)
4546
{
4647
return p->normal_prio;
4748
}
49+
static inline struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
50+
{
51+
return NULL;
52+
}
4853
# define rt_mutex_adjust_pi(p) do { } while (0)
4954
static inline bool tsk_is_pi_blocked(struct task_struct *tsk)
5055
{

‎kernel/fork.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,7 @@ static void rt_mutex_init_task(struct task_struct *p)
10901090
p->pi_waiters = RB_ROOT;
10911091
p->pi_waiters_leftmost = NULL;
10921092
p->pi_blocked_on = NULL;
1093+
p->pi_top_task = NULL;
10931094
#endif
10941095
}
10951096

‎kernel/locking/rtmutex.c‎

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,16 @@ static inline int
9696
rt_mutex_waiter_less(struct rt_mutex_waiter *left,
9797
struct rt_mutex_waiter *right)
9898
{
99-
if (left->task->prio < right->task->prio)
99+
if (left->prio < right->prio)
100100
return 1;
101101

102102
/*
103-
* If both tasks are dl_task(), we check their deadlines.
103+
* If both waiters have dl_prio(), we check the deadlines of the
104+
* associated tasks.
105+
* If left waiter has a dl_prio(), and we didn't return 1 above,
106+
* then right waiter has a dl_prio() too.
104107
*/
105-
if (dl_prio(left->task->prio) && dl_prio(right->task->prio))
108+
if (dl_prio(left->prio))
106109
return (left->task->dl.deadline < right->task->dl.deadline);
107110

108111
return 0;
@@ -197,10 +200,18 @@ int rt_mutex_getprio(struct task_struct *task)
197200
if (likely(!task_has_pi_waiters(task)))
198201
return task->normal_prio;
199202

200-
return min(task_top_pi_waiter(task)->task->prio,
203+
return min(task_top_pi_waiter(task)->prio,
201204
task->normal_prio);
202205
}
203206

207+
struct task_struct *rt_mutex_get_top_task(struct task_struct *task)
208+
{
209+
if (likely(!task_has_pi_waiters(task)))
210+
return NULL;
211+
212+
return task_top_pi_waiter(task)->task;
213+
}
214+
204215
/*
205216
* Adjust the priority of a task, after its pi_waiters got modified.
206217
*
@@ -210,7 +221,7 @@ static void __rt_mutex_adjust_prio(struct task_struct *task)
210221
{
211222
int prio = rt_mutex_getprio(task);
212223

213-
if (task->prio != prio)
224+
if (task->prio != prio || dl_prio(prio))
214225
rt_mutex_setprio(task, prio);
215226
}
216227

@@ -328,7 +339,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
328339
* When deadlock detection is off then we check, if further
329340
* priority adjustment is necessary.
330341
*/
331-
if (!detect_deadlock && waiter->task->prio == task->prio)
342+
if (!detect_deadlock && waiter->prio == task->prio)
332343
goto out_unlock_pi;
333344

334345
lock = waiter->lock;
@@ -350,7 +361,7 @@ static int rt_mutex_adjust_prio_chain(struct task_struct *task,
350361

351362
/* Requeue the waiter */
352363
rt_mutex_dequeue(lock, waiter);
353-
waiter->task->prio = task->prio;
364+
waiter->prio = task->prio;
354365
rt_mutex_enqueue(lock, waiter);
355366

356367
/* Release the task */
@@ -448,7 +459,7 @@ static int try_to_take_rt_mutex(struct rt_mutex *lock, struct task_struct *task,
448459
* 3) it is top waiter
449460
*/
450461
if (rt_mutex_has_waiters(lock)) {
451-
if (task->prio >= rt_mutex_top_waiter(lock)->task->prio) {
462+
if (task->prio >= rt_mutex_top_waiter(lock)->prio) {
452463
if (!waiter || waiter != rt_mutex_top_waiter(lock))
453464
return 0;
454465
}
@@ -508,6 +519,7 @@ static int task_blocks_on_rt_mutex(struct rt_mutex *lock,
508519
__rt_mutex_adjust_prio(task);
509520
waiter->task = task;
510521
waiter->lock = lock;
522+
waiter->prio = task->prio;
511523

512524
/* Get the top priority waiter on the lock */
513525
if (rt_mutex_has_waiters(lock))
@@ -653,7 +665,8 @@ void rt_mutex_adjust_pi(struct task_struct *task)
653665
raw_spin_lock_irqsave(&task->pi_lock, flags);
654666

655667
waiter = task->pi_blocked_on;
656-
if (!waiter || waiter->task->prio == task->prio) {
668+
if (!waiter || (waiter->prio == task->prio &&
669+
!dl_prio(task->prio))) {
657670
raw_spin_unlock_irqrestore(&task->pi_lock, flags);
658671
return;
659672
}

‎kernel/locking/rtmutex_common.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ struct rt_mutex_waiter {
5454
struct pid *deadlock_task_pid;
5555
struct rt_mutex *deadlock_lock;
5656
#endif
57+
int prio;
5758
};
5859

5960
/*

‎kernel/sched/core.c‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,7 @@ static inline void check_class_changed(struct rq *rq, struct task_struct *p,
947947
if (prev_class->switched_from)
948948
prev_class->switched_from(rq, p);
949949
p->sched_class->switched_to(rq, p);
950-
} else if (oldprio != p->prio)
950+
} else if (oldprio != p->prio || dl_task(p))
951951
p->sched_class->prio_changed(rq, p, oldprio);
952952
}
953953

@@ -2781,7 +2781,7 @@ EXPORT_SYMBOL(sleep_on_timeout);
27812781
*/
27822782
void rt_mutex_setprio(struct task_struct *p, int prio)
27832783
{
2784-
int oldprio, on_rq, running;
2784+
int oldprio, on_rq, running, enqueue_flag = 0;
27852785
struct rq *rq;
27862786
const struct sched_class *prev_class;
27872787

@@ -2808,6 +2808,7 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
28082808
}
28092809

28102810
trace_sched_pi_setprio(p, prio);
2811+
p->pi_top_task = rt_mutex_get_top_task(p);
28112812
oldprio = p->prio;
28122813
prev_class = p->sched_class;
28132814
on_rq = p->on_rq;
@@ -2817,19 +2818,42 @@ void rt_mutex_setprio(struct task_struct *p, int prio)
28172818
if (running)
28182819
p->sched_class->put_prev_task(rq, p);
28192820

2820-
if (dl_prio(prio))
2821+
/*
2822+
* Boosting condition are:
2823+
* 1. -rt task is running and holds mutex A
2824+
* --> -dl task blocks on mutex A
2825+
*
2826+
* 2. -dl task is running and holds mutex A
2827+
* --> -dl task blocks on mutex A and could preempt the
2828+
* running task
2829+
*/
2830+
if (dl_prio(prio)) {
2831+
if (!dl_prio(p->normal_prio) || (p->pi_top_task &&
2832+
dl_entity_preempt(&p->pi_top_task->dl, &p->dl))) {
2833+
p->dl.dl_boosted = 1;
2834+
p->dl.dl_throttled = 0;
2835+
enqueue_flag = ENQUEUE_REPLENISH;
2836+
} else
2837+
p->dl.dl_boosted = 0;
28212838
p->sched_class = &dl_sched_class;
2822-
else if (rt_prio(prio))
2839+
} else if (rt_prio(prio)) {
2840+
if (dl_prio(oldprio))
2841+
p->dl.dl_boosted = 0;
2842+
if (oldprio < prio)
2843+
enqueue_flag = ENQUEUE_HEAD;
28232844
p->sched_class = &rt_sched_class;
2824-
else
2845+
} else {
2846+
if (dl_prio(oldprio))
2847+
p->dl.dl_boosted = 0;
28252848
p->sched_class = &fair_sched_class;
2849+
}
28262850

28272851
p->prio = prio;
28282852

28292853
if (running)
28302854
p->sched_class->set_curr_task(rq);
28312855
if (on_rq)
2832-
enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
2856+
enqueue_task(rq, p, enqueue_flag);
28332857

28342858
check_class_changed(rq, p, prev_class, oldprio);
28352859
out_unlock:

0 commit comments

Comments
 (0)