xref: /OK3568_Linux_fs/kernel/kernel/sched/deadline.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Deadline Scheduling Class (SCHED_DEADLINE)
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS).
6*4882a593Smuzhiyun  *
7*4882a593Smuzhiyun  * Tasks that periodically executes their instances for less than their
8*4882a593Smuzhiyun  * runtime won't miss any of their deadlines.
9*4882a593Smuzhiyun  * Tasks that are not periodic or sporadic or that tries to execute more
10*4882a593Smuzhiyun  * than their reserved bandwidth will be slowed down (and may potentially
11*4882a593Smuzhiyun  * miss some of their deadlines), and won't affect any other task.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>,
14*4882a593Smuzhiyun  *                    Juri Lelli <juri.lelli@gmail.com>,
15*4882a593Smuzhiyun  *                    Michael Trimarchi <michael@amarulasolutions.com>,
16*4882a593Smuzhiyun  *                    Fabio Checconi <fchecconi@gmail.com>
17*4882a593Smuzhiyun  */
18*4882a593Smuzhiyun #include "sched.h"
19*4882a593Smuzhiyun #include "pelt.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun struct dl_bandwidth def_dl_bandwidth;
22*4882a593Smuzhiyun 
dl_task_of(struct sched_dl_entity * dl_se)23*4882a593Smuzhiyun static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
24*4882a593Smuzhiyun {
25*4882a593Smuzhiyun 	return container_of(dl_se, struct task_struct, dl);
26*4882a593Smuzhiyun }
27*4882a593Smuzhiyun 
rq_of_dl_rq(struct dl_rq * dl_rq)28*4882a593Smuzhiyun static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	return container_of(dl_rq, struct rq, dl);
31*4882a593Smuzhiyun }
32*4882a593Smuzhiyun 
dl_rq_of_se(struct sched_dl_entity * dl_se)33*4882a593Smuzhiyun static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
34*4882a593Smuzhiyun {
35*4882a593Smuzhiyun 	struct task_struct *p = dl_task_of(dl_se);
36*4882a593Smuzhiyun 	struct rq *rq = task_rq(p);
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	return &rq->dl;
39*4882a593Smuzhiyun }
40*4882a593Smuzhiyun 
on_dl_rq(struct sched_dl_entity * dl_se)41*4882a593Smuzhiyun static inline int on_dl_rq(struct sched_dl_entity *dl_se)
42*4882a593Smuzhiyun {
43*4882a593Smuzhiyun 	return !RB_EMPTY_NODE(&dl_se->rb_node);
44*4882a593Smuzhiyun }
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun #ifdef CONFIG_RT_MUTEXES
pi_of(struct sched_dl_entity * dl_se)47*4882a593Smuzhiyun static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	return dl_se->pi_se;
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun 
is_dl_boosted(struct sched_dl_entity * dl_se)52*4882a593Smuzhiyun static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	return pi_of(dl_se) != dl_se;
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun #else
pi_of(struct sched_dl_entity * dl_se)57*4882a593Smuzhiyun static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	return dl_se;
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
is_dl_boosted(struct sched_dl_entity * dl_se)62*4882a593Smuzhiyun static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
63*4882a593Smuzhiyun {
64*4882a593Smuzhiyun 	return false;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun #endif
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun #ifdef CONFIG_SMP
dl_bw_of(int i)69*4882a593Smuzhiyun static inline struct dl_bw *dl_bw_of(int i)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
72*4882a593Smuzhiyun 			 "sched RCU must be held");
73*4882a593Smuzhiyun 	return &cpu_rq(i)->rd->dl_bw;
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
dl_bw_cpus(int i)76*4882a593Smuzhiyun static inline int dl_bw_cpus(int i)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun 	struct root_domain *rd = cpu_rq(i)->rd;
79*4882a593Smuzhiyun 	int cpus;
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
82*4882a593Smuzhiyun 			 "sched RCU must be held");
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	if (cpumask_subset(rd->span, cpu_active_mask))
85*4882a593Smuzhiyun 		return cpumask_weight(rd->span);
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	cpus = 0;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	for_each_cpu_and(i, rd->span, cpu_active_mask)
90*4882a593Smuzhiyun 		cpus++;
91*4882a593Smuzhiyun 
92*4882a593Smuzhiyun 	return cpus;
93*4882a593Smuzhiyun }
94*4882a593Smuzhiyun 
__dl_bw_capacity(int i)95*4882a593Smuzhiyun static inline unsigned long __dl_bw_capacity(int i)
96*4882a593Smuzhiyun {
97*4882a593Smuzhiyun 	struct root_domain *rd = cpu_rq(i)->rd;
98*4882a593Smuzhiyun 	unsigned long cap = 0;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
101*4882a593Smuzhiyun 			 "sched RCU must be held");
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 	for_each_cpu_and(i, rd->span, cpu_active_mask)
104*4882a593Smuzhiyun 		cap += capacity_orig_of(i);
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	return cap;
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
109*4882a593Smuzhiyun /*
110*4882a593Smuzhiyun  * XXX Fix: If 'rq->rd == def_root_domain' perform AC against capacity
111*4882a593Smuzhiyun  * of the CPU the task is running on rather rd's \Sum CPU capacity.
112*4882a593Smuzhiyun  */
dl_bw_capacity(int i)113*4882a593Smuzhiyun static inline unsigned long dl_bw_capacity(int i)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	if (!static_branch_unlikely(&sched_asym_cpucapacity) &&
116*4882a593Smuzhiyun 	    capacity_orig_of(i) == SCHED_CAPACITY_SCALE) {
117*4882a593Smuzhiyun 		return dl_bw_cpus(i) << SCHED_CAPACITY_SHIFT;
118*4882a593Smuzhiyun 	} else {
119*4882a593Smuzhiyun 		return __dl_bw_capacity(i);
120*4882a593Smuzhiyun 	}
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun #else
dl_bw_of(int i)123*4882a593Smuzhiyun static inline struct dl_bw *dl_bw_of(int i)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	return &cpu_rq(i)->dl.dl_bw;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun 
dl_bw_cpus(int i)128*4882a593Smuzhiyun static inline int dl_bw_cpus(int i)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun 	return 1;
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
dl_bw_capacity(int i)133*4882a593Smuzhiyun static inline unsigned long dl_bw_capacity(int i)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	return SCHED_CAPACITY_SCALE;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun #endif
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun static inline
__add_running_bw(u64 dl_bw,struct dl_rq * dl_rq)140*4882a593Smuzhiyun void __add_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
141*4882a593Smuzhiyun {
142*4882a593Smuzhiyun 	u64 old = dl_rq->running_bw;
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
145*4882a593Smuzhiyun 	dl_rq->running_bw += dl_bw;
146*4882a593Smuzhiyun 	SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */
147*4882a593Smuzhiyun 	SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
148*4882a593Smuzhiyun 	/* kick cpufreq (see the comment in kernel/sched/sched.h). */
149*4882a593Smuzhiyun 	cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun static inline
__sub_running_bw(u64 dl_bw,struct dl_rq * dl_rq)153*4882a593Smuzhiyun void __sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
154*4882a593Smuzhiyun {
155*4882a593Smuzhiyun 	u64 old = dl_rq->running_bw;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
158*4882a593Smuzhiyun 	dl_rq->running_bw -= dl_bw;
159*4882a593Smuzhiyun 	SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */
160*4882a593Smuzhiyun 	if (dl_rq->running_bw > old)
161*4882a593Smuzhiyun 		dl_rq->running_bw = 0;
162*4882a593Smuzhiyun 	/* kick cpufreq (see the comment in kernel/sched/sched.h). */
163*4882a593Smuzhiyun 	cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun static inline
__add_rq_bw(u64 dl_bw,struct dl_rq * dl_rq)167*4882a593Smuzhiyun void __add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
168*4882a593Smuzhiyun {
169*4882a593Smuzhiyun 	u64 old = dl_rq->this_bw;
170*4882a593Smuzhiyun 
171*4882a593Smuzhiyun 	lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
172*4882a593Smuzhiyun 	dl_rq->this_bw += dl_bw;
173*4882a593Smuzhiyun 	SCHED_WARN_ON(dl_rq->this_bw < old); /* overflow */
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun static inline
__sub_rq_bw(u64 dl_bw,struct dl_rq * dl_rq)177*4882a593Smuzhiyun void __sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	u64 old = dl_rq->this_bw;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
182*4882a593Smuzhiyun 	dl_rq->this_bw -= dl_bw;
183*4882a593Smuzhiyun 	SCHED_WARN_ON(dl_rq->this_bw > old); /* underflow */
184*4882a593Smuzhiyun 	if (dl_rq->this_bw > old)
185*4882a593Smuzhiyun 		dl_rq->this_bw = 0;
186*4882a593Smuzhiyun 	SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
187*4882a593Smuzhiyun }
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun static inline
add_rq_bw(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)190*4882a593Smuzhiyun void add_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	if (!dl_entity_is_special(dl_se))
193*4882a593Smuzhiyun 		__add_rq_bw(dl_se->dl_bw, dl_rq);
194*4882a593Smuzhiyun }
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun static inline
sub_rq_bw(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)197*4882a593Smuzhiyun void sub_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
198*4882a593Smuzhiyun {
199*4882a593Smuzhiyun 	if (!dl_entity_is_special(dl_se))
200*4882a593Smuzhiyun 		__sub_rq_bw(dl_se->dl_bw, dl_rq);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun static inline
add_running_bw(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)204*4882a593Smuzhiyun void add_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun 	if (!dl_entity_is_special(dl_se))
207*4882a593Smuzhiyun 		__add_running_bw(dl_se->dl_bw, dl_rq);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun static inline
sub_running_bw(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)211*4882a593Smuzhiyun void sub_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun 	if (!dl_entity_is_special(dl_se))
214*4882a593Smuzhiyun 		__sub_running_bw(dl_se->dl_bw, dl_rq);
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun 
dl_change_utilization(struct task_struct * p,u64 new_bw)217*4882a593Smuzhiyun static void dl_change_utilization(struct task_struct *p, u64 new_bw)
218*4882a593Smuzhiyun {
219*4882a593Smuzhiyun 	struct rq *rq;
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	BUG_ON(p->dl.flags & SCHED_FLAG_SUGOV);
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	if (task_on_rq_queued(p))
224*4882a593Smuzhiyun 		return;
225*4882a593Smuzhiyun 
226*4882a593Smuzhiyun 	rq = task_rq(p);
227*4882a593Smuzhiyun 	if (p->dl.dl_non_contending) {
228*4882a593Smuzhiyun 		sub_running_bw(&p->dl, &rq->dl);
229*4882a593Smuzhiyun 		p->dl.dl_non_contending = 0;
230*4882a593Smuzhiyun 		/*
231*4882a593Smuzhiyun 		 * If the timer handler is currently running and the
232*4882a593Smuzhiyun 		 * timer cannot be cancelled, inactive_task_timer()
233*4882a593Smuzhiyun 		 * will see that dl_not_contending is not set, and
234*4882a593Smuzhiyun 		 * will not touch the rq's active utilization,
235*4882a593Smuzhiyun 		 * so we are still safe.
236*4882a593Smuzhiyun 		 */
237*4882a593Smuzhiyun 		if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
238*4882a593Smuzhiyun 			put_task_struct(p);
239*4882a593Smuzhiyun 	}
240*4882a593Smuzhiyun 	__sub_rq_bw(p->dl.dl_bw, &rq->dl);
241*4882a593Smuzhiyun 	__add_rq_bw(new_bw, &rq->dl);
242*4882a593Smuzhiyun }
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun /*
245*4882a593Smuzhiyun  * The utilization of a task cannot be immediately removed from
246*4882a593Smuzhiyun  * the rq active utilization (running_bw) when the task blocks.
247*4882a593Smuzhiyun  * Instead, we have to wait for the so called "0-lag time".
248*4882a593Smuzhiyun  *
249*4882a593Smuzhiyun  * If a task blocks before the "0-lag time", a timer (the inactive
250*4882a593Smuzhiyun  * timer) is armed, and running_bw is decreased when the timer
251*4882a593Smuzhiyun  * fires.
252*4882a593Smuzhiyun  *
253*4882a593Smuzhiyun  * If the task wakes up again before the inactive timer fires,
254*4882a593Smuzhiyun  * the timer is cancelled, whereas if the task wakes up after the
255*4882a593Smuzhiyun  * inactive timer fired (and running_bw has been decreased) the
256*4882a593Smuzhiyun  * task's utilization has to be added to running_bw again.
257*4882a593Smuzhiyun  * A flag in the deadline scheduling entity (dl_non_contending)
258*4882a593Smuzhiyun  * is used to avoid race conditions between the inactive timer handler
259*4882a593Smuzhiyun  * and task wakeups.
260*4882a593Smuzhiyun  *
261*4882a593Smuzhiyun  * The following diagram shows how running_bw is updated. A task is
262*4882a593Smuzhiyun  * "ACTIVE" when its utilization contributes to running_bw; an
263*4882a593Smuzhiyun  * "ACTIVE contending" task is in the TASK_RUNNING state, while an
264*4882a593Smuzhiyun  * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
265*4882a593Smuzhiyun  * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
266*4882a593Smuzhiyun  * time already passed, which does not contribute to running_bw anymore.
267*4882a593Smuzhiyun  *                              +------------------+
268*4882a593Smuzhiyun  *             wakeup           |    ACTIVE        |
269*4882a593Smuzhiyun  *          +------------------>+   contending     |
270*4882a593Smuzhiyun  *          | add_running_bw    |                  |
271*4882a593Smuzhiyun  *          |                   +----+------+------+
272*4882a593Smuzhiyun  *          |                        |      ^
273*4882a593Smuzhiyun  *          |                dequeue |      |
274*4882a593Smuzhiyun  * +--------+-------+                |      |
275*4882a593Smuzhiyun  * |                |   t >= 0-lag   |      | wakeup
276*4882a593Smuzhiyun  * |    INACTIVE    |<---------------+      |
277*4882a593Smuzhiyun  * |                | sub_running_bw |      |
278*4882a593Smuzhiyun  * +--------+-------+                |      |
279*4882a593Smuzhiyun  *          ^                        |      |
280*4882a593Smuzhiyun  *          |              t < 0-lag |      |
281*4882a593Smuzhiyun  *          |                        |      |
282*4882a593Smuzhiyun  *          |                        V      |
283*4882a593Smuzhiyun  *          |                   +----+------+------+
284*4882a593Smuzhiyun  *          | sub_running_bw    |    ACTIVE        |
285*4882a593Smuzhiyun  *          +-------------------+                  |
286*4882a593Smuzhiyun  *            inactive timer    |  non contending  |
287*4882a593Smuzhiyun  *            fired             +------------------+
288*4882a593Smuzhiyun  *
289*4882a593Smuzhiyun  * The task_non_contending() function is invoked when a task
290*4882a593Smuzhiyun  * blocks, and checks if the 0-lag time already passed or
291*4882a593Smuzhiyun  * not (in the first case, it directly updates running_bw;
292*4882a593Smuzhiyun  * in the second case, it arms the inactive timer).
293*4882a593Smuzhiyun  *
294*4882a593Smuzhiyun  * The task_contending() function is invoked when a task wakes
295*4882a593Smuzhiyun  * up, and checks if the task is still in the "ACTIVE non contending"
296*4882a593Smuzhiyun  * state or not (in the second case, it updates running_bw).
297*4882a593Smuzhiyun  */
task_non_contending(struct task_struct * p)298*4882a593Smuzhiyun static void task_non_contending(struct task_struct *p)
299*4882a593Smuzhiyun {
300*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se = &p->dl;
301*4882a593Smuzhiyun 	struct hrtimer *timer = &dl_se->inactive_timer;
302*4882a593Smuzhiyun 	struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
303*4882a593Smuzhiyun 	struct rq *rq = rq_of_dl_rq(dl_rq);
304*4882a593Smuzhiyun 	s64 zerolag_time;
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	/*
307*4882a593Smuzhiyun 	 * If this is a non-deadline task that has been boosted,
308*4882a593Smuzhiyun 	 * do nothing
309*4882a593Smuzhiyun 	 */
310*4882a593Smuzhiyun 	if (dl_se->dl_runtime == 0)
311*4882a593Smuzhiyun 		return;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	if (dl_entity_is_special(dl_se))
314*4882a593Smuzhiyun 		return;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	WARN_ON(dl_se->dl_non_contending);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	zerolag_time = dl_se->deadline -
319*4882a593Smuzhiyun 		 div64_long((dl_se->runtime * dl_se->dl_period),
320*4882a593Smuzhiyun 			dl_se->dl_runtime);
321*4882a593Smuzhiyun 
322*4882a593Smuzhiyun 	/*
323*4882a593Smuzhiyun 	 * Using relative times instead of the absolute "0-lag time"
324*4882a593Smuzhiyun 	 * allows to simplify the code
325*4882a593Smuzhiyun 	 */
326*4882a593Smuzhiyun 	zerolag_time -= rq_clock(rq);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	/*
329*4882a593Smuzhiyun 	 * If the "0-lag time" already passed, decrease the active
330*4882a593Smuzhiyun 	 * utilization now, instead of starting a timer
331*4882a593Smuzhiyun 	 */
332*4882a593Smuzhiyun 	if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) {
333*4882a593Smuzhiyun 		if (dl_task(p))
334*4882a593Smuzhiyun 			sub_running_bw(dl_se, dl_rq);
335*4882a593Smuzhiyun 		if (!dl_task(p) || p->state == TASK_DEAD) {
336*4882a593Smuzhiyun 			struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
337*4882a593Smuzhiyun 
338*4882a593Smuzhiyun 			if (p->state == TASK_DEAD)
339*4882a593Smuzhiyun 				sub_rq_bw(&p->dl, &rq->dl);
340*4882a593Smuzhiyun 			raw_spin_lock(&dl_b->lock);
341*4882a593Smuzhiyun 			__dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
342*4882a593Smuzhiyun 			__dl_clear_params(p);
343*4882a593Smuzhiyun 			raw_spin_unlock(&dl_b->lock);
344*4882a593Smuzhiyun 		}
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 		return;
347*4882a593Smuzhiyun 	}
348*4882a593Smuzhiyun 
349*4882a593Smuzhiyun 	dl_se->dl_non_contending = 1;
350*4882a593Smuzhiyun 	get_task_struct(p);
351*4882a593Smuzhiyun 	hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL_HARD);
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun 
task_contending(struct sched_dl_entity * dl_se,int flags)354*4882a593Smuzhiyun static void task_contending(struct sched_dl_entity *dl_se, int flags)
355*4882a593Smuzhiyun {
356*4882a593Smuzhiyun 	struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	/*
359*4882a593Smuzhiyun 	 * If this is a non-deadline task that has been boosted,
360*4882a593Smuzhiyun 	 * do nothing
361*4882a593Smuzhiyun 	 */
362*4882a593Smuzhiyun 	if (dl_se->dl_runtime == 0)
363*4882a593Smuzhiyun 		return;
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun 	if (flags & ENQUEUE_MIGRATED)
366*4882a593Smuzhiyun 		add_rq_bw(dl_se, dl_rq);
367*4882a593Smuzhiyun 
368*4882a593Smuzhiyun 	if (dl_se->dl_non_contending) {
369*4882a593Smuzhiyun 		dl_se->dl_non_contending = 0;
370*4882a593Smuzhiyun 		/*
371*4882a593Smuzhiyun 		 * If the timer handler is currently running and the
372*4882a593Smuzhiyun 		 * timer cannot be cancelled, inactive_task_timer()
373*4882a593Smuzhiyun 		 * will see that dl_not_contending is not set, and
374*4882a593Smuzhiyun 		 * will not touch the rq's active utilization,
375*4882a593Smuzhiyun 		 * so we are still safe.
376*4882a593Smuzhiyun 		 */
377*4882a593Smuzhiyun 		if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1)
378*4882a593Smuzhiyun 			put_task_struct(dl_task_of(dl_se));
379*4882a593Smuzhiyun 	} else {
380*4882a593Smuzhiyun 		/*
381*4882a593Smuzhiyun 		 * Since "dl_non_contending" is not set, the
382*4882a593Smuzhiyun 		 * task's utilization has already been removed from
383*4882a593Smuzhiyun 		 * active utilization (either when the task blocked,
384*4882a593Smuzhiyun 		 * when the "inactive timer" fired).
385*4882a593Smuzhiyun 		 * So, add it back.
386*4882a593Smuzhiyun 		 */
387*4882a593Smuzhiyun 		add_running_bw(dl_se, dl_rq);
388*4882a593Smuzhiyun 	}
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun 
is_leftmost(struct task_struct * p,struct dl_rq * dl_rq)391*4882a593Smuzhiyun static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se = &p->dl;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	return dl_rq->root.rb_leftmost == &dl_se->rb_node;
396*4882a593Smuzhiyun }
397*4882a593Smuzhiyun 
398*4882a593Smuzhiyun static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
399*4882a593Smuzhiyun 
init_dl_bandwidth(struct dl_bandwidth * dl_b,u64 period,u64 runtime)400*4882a593Smuzhiyun void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun 	raw_spin_lock_init(&dl_b->dl_runtime_lock);
403*4882a593Smuzhiyun 	dl_b->dl_period = period;
404*4882a593Smuzhiyun 	dl_b->dl_runtime = runtime;
405*4882a593Smuzhiyun }
406*4882a593Smuzhiyun 
init_dl_bw(struct dl_bw * dl_b)407*4882a593Smuzhiyun void init_dl_bw(struct dl_bw *dl_b)
408*4882a593Smuzhiyun {
409*4882a593Smuzhiyun 	raw_spin_lock_init(&dl_b->lock);
410*4882a593Smuzhiyun 	raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
411*4882a593Smuzhiyun 	if (global_rt_runtime() == RUNTIME_INF)
412*4882a593Smuzhiyun 		dl_b->bw = -1;
413*4882a593Smuzhiyun 	else
414*4882a593Smuzhiyun 		dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
415*4882a593Smuzhiyun 	raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
416*4882a593Smuzhiyun 	dl_b->total_bw = 0;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
init_dl_rq(struct dl_rq * dl_rq)419*4882a593Smuzhiyun void init_dl_rq(struct dl_rq *dl_rq)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	dl_rq->root = RB_ROOT_CACHED;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun #ifdef CONFIG_SMP
424*4882a593Smuzhiyun 	/* zero means no -deadline tasks */
425*4882a593Smuzhiyun 	dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
426*4882a593Smuzhiyun 
427*4882a593Smuzhiyun 	dl_rq->dl_nr_migratory = 0;
428*4882a593Smuzhiyun 	dl_rq->overloaded = 0;
429*4882a593Smuzhiyun 	dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED;
430*4882a593Smuzhiyun #else
431*4882a593Smuzhiyun 	init_dl_bw(&dl_rq->dl_bw);
432*4882a593Smuzhiyun #endif
433*4882a593Smuzhiyun 
434*4882a593Smuzhiyun 	dl_rq->running_bw = 0;
435*4882a593Smuzhiyun 	dl_rq->this_bw = 0;
436*4882a593Smuzhiyun 	init_dl_rq_bw_ratio(dl_rq);
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun #ifdef CONFIG_SMP
440*4882a593Smuzhiyun 
dl_overloaded(struct rq * rq)441*4882a593Smuzhiyun static inline int dl_overloaded(struct rq *rq)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	return atomic_read(&rq->rd->dlo_count);
444*4882a593Smuzhiyun }
445*4882a593Smuzhiyun 
dl_set_overload(struct rq * rq)446*4882a593Smuzhiyun static inline void dl_set_overload(struct rq *rq)
447*4882a593Smuzhiyun {
448*4882a593Smuzhiyun 	if (!rq->online)
449*4882a593Smuzhiyun 		return;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
452*4882a593Smuzhiyun 	/*
453*4882a593Smuzhiyun 	 * Must be visible before the overload count is
454*4882a593Smuzhiyun 	 * set (as in sched_rt.c).
455*4882a593Smuzhiyun 	 *
456*4882a593Smuzhiyun 	 * Matched by the barrier in pull_dl_task().
457*4882a593Smuzhiyun 	 */
458*4882a593Smuzhiyun 	smp_wmb();
459*4882a593Smuzhiyun 	atomic_inc(&rq->rd->dlo_count);
460*4882a593Smuzhiyun }
461*4882a593Smuzhiyun 
dl_clear_overload(struct rq * rq)462*4882a593Smuzhiyun static inline void dl_clear_overload(struct rq *rq)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun 	if (!rq->online)
465*4882a593Smuzhiyun 		return;
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	atomic_dec(&rq->rd->dlo_count);
468*4882a593Smuzhiyun 	cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun 
update_dl_migration(struct dl_rq * dl_rq)471*4882a593Smuzhiyun static void update_dl_migration(struct dl_rq *dl_rq)
472*4882a593Smuzhiyun {
473*4882a593Smuzhiyun 	if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
474*4882a593Smuzhiyun 		if (!dl_rq->overloaded) {
475*4882a593Smuzhiyun 			dl_set_overload(rq_of_dl_rq(dl_rq));
476*4882a593Smuzhiyun 			dl_rq->overloaded = 1;
477*4882a593Smuzhiyun 		}
478*4882a593Smuzhiyun 	} else if (dl_rq->overloaded) {
479*4882a593Smuzhiyun 		dl_clear_overload(rq_of_dl_rq(dl_rq));
480*4882a593Smuzhiyun 		dl_rq->overloaded = 0;
481*4882a593Smuzhiyun 	}
482*4882a593Smuzhiyun }
483*4882a593Smuzhiyun 
inc_dl_migration(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)484*4882a593Smuzhiyun static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun 	struct task_struct *p = dl_task_of(dl_se);
487*4882a593Smuzhiyun 
488*4882a593Smuzhiyun 	if (p->nr_cpus_allowed > 1)
489*4882a593Smuzhiyun 		dl_rq->dl_nr_migratory++;
490*4882a593Smuzhiyun 
491*4882a593Smuzhiyun 	update_dl_migration(dl_rq);
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun 
dec_dl_migration(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)494*4882a593Smuzhiyun static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun 	struct task_struct *p = dl_task_of(dl_se);
497*4882a593Smuzhiyun 
498*4882a593Smuzhiyun 	if (p->nr_cpus_allowed > 1)
499*4882a593Smuzhiyun 		dl_rq->dl_nr_migratory--;
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	update_dl_migration(dl_rq);
502*4882a593Smuzhiyun }
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun /*
505*4882a593Smuzhiyun  * The list of pushable -deadline task is not a plist, like in
506*4882a593Smuzhiyun  * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
507*4882a593Smuzhiyun  */
enqueue_pushable_dl_task(struct rq * rq,struct task_struct * p)508*4882a593Smuzhiyun static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	struct dl_rq *dl_rq = &rq->dl;
511*4882a593Smuzhiyun 	struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_root.rb_node;
512*4882a593Smuzhiyun 	struct rb_node *parent = NULL;
513*4882a593Smuzhiyun 	struct task_struct *entry;
514*4882a593Smuzhiyun 	bool leftmost = true;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
517*4882a593Smuzhiyun 
518*4882a593Smuzhiyun 	while (*link) {
519*4882a593Smuzhiyun 		parent = *link;
520*4882a593Smuzhiyun 		entry = rb_entry(parent, struct task_struct,
521*4882a593Smuzhiyun 				 pushable_dl_tasks);
522*4882a593Smuzhiyun 		if (dl_entity_preempt(&p->dl, &entry->dl))
523*4882a593Smuzhiyun 			link = &parent->rb_left;
524*4882a593Smuzhiyun 		else {
525*4882a593Smuzhiyun 			link = &parent->rb_right;
526*4882a593Smuzhiyun 			leftmost = false;
527*4882a593Smuzhiyun 		}
528*4882a593Smuzhiyun 	}
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	if (leftmost)
531*4882a593Smuzhiyun 		dl_rq->earliest_dl.next = p->dl.deadline;
532*4882a593Smuzhiyun 
533*4882a593Smuzhiyun 	rb_link_node(&p->pushable_dl_tasks, parent, link);
534*4882a593Smuzhiyun 	rb_insert_color_cached(&p->pushable_dl_tasks,
535*4882a593Smuzhiyun 			       &dl_rq->pushable_dl_tasks_root, leftmost);
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun 
dequeue_pushable_dl_task(struct rq * rq,struct task_struct * p)538*4882a593Smuzhiyun static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
539*4882a593Smuzhiyun {
540*4882a593Smuzhiyun 	struct dl_rq *dl_rq = &rq->dl;
541*4882a593Smuzhiyun 
542*4882a593Smuzhiyun 	if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
543*4882a593Smuzhiyun 		return;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	if (dl_rq->pushable_dl_tasks_root.rb_leftmost == &p->pushable_dl_tasks) {
546*4882a593Smuzhiyun 		struct rb_node *next_node;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 		next_node = rb_next(&p->pushable_dl_tasks);
549*4882a593Smuzhiyun 		if (next_node) {
550*4882a593Smuzhiyun 			dl_rq->earliest_dl.next = rb_entry(next_node,
551*4882a593Smuzhiyun 				struct task_struct, pushable_dl_tasks)->dl.deadline;
552*4882a593Smuzhiyun 		}
553*4882a593Smuzhiyun 	}
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun 	rb_erase_cached(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
556*4882a593Smuzhiyun 	RB_CLEAR_NODE(&p->pushable_dl_tasks);
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun 
has_pushable_dl_tasks(struct rq * rq)559*4882a593Smuzhiyun static inline int has_pushable_dl_tasks(struct rq *rq)
560*4882a593Smuzhiyun {
561*4882a593Smuzhiyun 	return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root);
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun static int push_dl_task(struct rq *rq);
565*4882a593Smuzhiyun 
need_pull_dl_task(struct rq * rq,struct task_struct * prev)566*4882a593Smuzhiyun static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
567*4882a593Smuzhiyun {
568*4882a593Smuzhiyun 	return dl_task(prev);
569*4882a593Smuzhiyun }
570*4882a593Smuzhiyun 
571*4882a593Smuzhiyun static DEFINE_PER_CPU(struct callback_head, dl_push_head);
572*4882a593Smuzhiyun static DEFINE_PER_CPU(struct callback_head, dl_pull_head);
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun static void push_dl_tasks(struct rq *);
575*4882a593Smuzhiyun static void pull_dl_task(struct rq *);
576*4882a593Smuzhiyun 
deadline_queue_push_tasks(struct rq * rq)577*4882a593Smuzhiyun static inline void deadline_queue_push_tasks(struct rq *rq)
578*4882a593Smuzhiyun {
579*4882a593Smuzhiyun 	if (!has_pushable_dl_tasks(rq))
580*4882a593Smuzhiyun 		return;
581*4882a593Smuzhiyun 
582*4882a593Smuzhiyun 	queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
583*4882a593Smuzhiyun }
584*4882a593Smuzhiyun 
deadline_queue_pull_task(struct rq * rq)585*4882a593Smuzhiyun static inline void deadline_queue_pull_task(struct rq *rq)
586*4882a593Smuzhiyun {
587*4882a593Smuzhiyun 	queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
591*4882a593Smuzhiyun 
dl_task_offline_migration(struct rq * rq,struct task_struct * p)592*4882a593Smuzhiyun static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
593*4882a593Smuzhiyun {
594*4882a593Smuzhiyun 	struct rq *later_rq = NULL;
595*4882a593Smuzhiyun 	struct dl_bw *dl_b;
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun 	later_rq = find_lock_later_rq(p, rq);
598*4882a593Smuzhiyun 	if (!later_rq) {
599*4882a593Smuzhiyun 		int cpu;
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 		/*
602*4882a593Smuzhiyun 		 * If we cannot preempt any rq, fall back to pick any
603*4882a593Smuzhiyun 		 * online CPU:
604*4882a593Smuzhiyun 		 */
605*4882a593Smuzhiyun 		cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr);
606*4882a593Smuzhiyun 		if (cpu >= nr_cpu_ids) {
607*4882a593Smuzhiyun 			/*
608*4882a593Smuzhiyun 			 * Failed to find any suitable CPU.
609*4882a593Smuzhiyun 			 * The task will never come back!
610*4882a593Smuzhiyun 			 */
611*4882a593Smuzhiyun 			BUG_ON(dl_bandwidth_enabled());
612*4882a593Smuzhiyun 
613*4882a593Smuzhiyun 			/*
614*4882a593Smuzhiyun 			 * If admission control is disabled we
615*4882a593Smuzhiyun 			 * try a little harder to let the task
616*4882a593Smuzhiyun 			 * run.
617*4882a593Smuzhiyun 			 */
618*4882a593Smuzhiyun 			cpu = cpumask_any(cpu_active_mask);
619*4882a593Smuzhiyun 		}
620*4882a593Smuzhiyun 		later_rq = cpu_rq(cpu);
621*4882a593Smuzhiyun 		double_lock_balance(rq, later_rq);
622*4882a593Smuzhiyun 	}
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	if (p->dl.dl_non_contending || p->dl.dl_throttled) {
625*4882a593Smuzhiyun 		/*
626*4882a593Smuzhiyun 		 * Inactive timer is armed (or callback is running, but
627*4882a593Smuzhiyun 		 * waiting for us to release rq locks). In any case, when it
628*4882a593Smuzhiyun 		 * will fire (or continue), it will see running_bw of this
629*4882a593Smuzhiyun 		 * task migrated to later_rq (and correctly handle it).
630*4882a593Smuzhiyun 		 */
631*4882a593Smuzhiyun 		sub_running_bw(&p->dl, &rq->dl);
632*4882a593Smuzhiyun 		sub_rq_bw(&p->dl, &rq->dl);
633*4882a593Smuzhiyun 
634*4882a593Smuzhiyun 		add_rq_bw(&p->dl, &later_rq->dl);
635*4882a593Smuzhiyun 		add_running_bw(&p->dl, &later_rq->dl);
636*4882a593Smuzhiyun 	} else {
637*4882a593Smuzhiyun 		sub_rq_bw(&p->dl, &rq->dl);
638*4882a593Smuzhiyun 		add_rq_bw(&p->dl, &later_rq->dl);
639*4882a593Smuzhiyun 	}
640*4882a593Smuzhiyun 
641*4882a593Smuzhiyun 	/*
642*4882a593Smuzhiyun 	 * And we finally need to fixup root_domain(s) bandwidth accounting,
643*4882a593Smuzhiyun 	 * since p is still hanging out in the old (now moved to default) root
644*4882a593Smuzhiyun 	 * domain.
645*4882a593Smuzhiyun 	 */
646*4882a593Smuzhiyun 	dl_b = &rq->rd->dl_bw;
647*4882a593Smuzhiyun 	raw_spin_lock(&dl_b->lock);
648*4882a593Smuzhiyun 	__dl_sub(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
649*4882a593Smuzhiyun 	raw_spin_unlock(&dl_b->lock);
650*4882a593Smuzhiyun 
651*4882a593Smuzhiyun 	dl_b = &later_rq->rd->dl_bw;
652*4882a593Smuzhiyun 	raw_spin_lock(&dl_b->lock);
653*4882a593Smuzhiyun 	__dl_add(dl_b, p->dl.dl_bw, cpumask_weight(later_rq->rd->span));
654*4882a593Smuzhiyun 	raw_spin_unlock(&dl_b->lock);
655*4882a593Smuzhiyun 
656*4882a593Smuzhiyun 	set_task_cpu(p, later_rq->cpu);
657*4882a593Smuzhiyun 	double_unlock_balance(later_rq, rq);
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun 	return later_rq;
660*4882a593Smuzhiyun }
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun #else
663*4882a593Smuzhiyun 
664*4882a593Smuzhiyun static inline
enqueue_pushable_dl_task(struct rq * rq,struct task_struct * p)665*4882a593Smuzhiyun void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
666*4882a593Smuzhiyun {
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun 
669*4882a593Smuzhiyun static inline
dequeue_pushable_dl_task(struct rq * rq,struct task_struct * p)670*4882a593Smuzhiyun void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
671*4882a593Smuzhiyun {
672*4882a593Smuzhiyun }
673*4882a593Smuzhiyun 
674*4882a593Smuzhiyun static inline
inc_dl_migration(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)675*4882a593Smuzhiyun void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
676*4882a593Smuzhiyun {
677*4882a593Smuzhiyun }
678*4882a593Smuzhiyun 
679*4882a593Smuzhiyun static inline
dec_dl_migration(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)680*4882a593Smuzhiyun void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
681*4882a593Smuzhiyun {
682*4882a593Smuzhiyun }
683*4882a593Smuzhiyun 
need_pull_dl_task(struct rq * rq,struct task_struct * prev)684*4882a593Smuzhiyun static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
685*4882a593Smuzhiyun {
686*4882a593Smuzhiyun 	return false;
687*4882a593Smuzhiyun }
688*4882a593Smuzhiyun 
pull_dl_task(struct rq * rq)689*4882a593Smuzhiyun static inline void pull_dl_task(struct rq *rq)
690*4882a593Smuzhiyun {
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun 
deadline_queue_push_tasks(struct rq * rq)693*4882a593Smuzhiyun static inline void deadline_queue_push_tasks(struct rq *rq)
694*4882a593Smuzhiyun {
695*4882a593Smuzhiyun }
696*4882a593Smuzhiyun 
deadline_queue_pull_task(struct rq * rq)697*4882a593Smuzhiyun static inline void deadline_queue_pull_task(struct rq *rq)
698*4882a593Smuzhiyun {
699*4882a593Smuzhiyun }
700*4882a593Smuzhiyun #endif /* CONFIG_SMP */
701*4882a593Smuzhiyun 
702*4882a593Smuzhiyun static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
703*4882a593Smuzhiyun static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
704*4882a593Smuzhiyun static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, int flags);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun /*
707*4882a593Smuzhiyun  * We are being explicitly informed that a new instance is starting,
708*4882a593Smuzhiyun  * and this means that:
709*4882a593Smuzhiyun  *  - the absolute deadline of the entity has to be placed at
710*4882a593Smuzhiyun  *    current time + relative deadline;
711*4882a593Smuzhiyun  *  - the runtime of the entity has to be set to the maximum value.
712*4882a593Smuzhiyun  *
713*4882a593Smuzhiyun  * The capability of specifying such event is useful whenever a -deadline
714*4882a593Smuzhiyun  * entity wants to (try to!) synchronize its behaviour with the scheduler's
715*4882a593Smuzhiyun  * one, and to (try to!) reconcile itself with its own scheduling
716*4882a593Smuzhiyun  * parameters.
717*4882a593Smuzhiyun  */
setup_new_dl_entity(struct sched_dl_entity * dl_se)718*4882a593Smuzhiyun static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
719*4882a593Smuzhiyun {
720*4882a593Smuzhiyun 	struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
721*4882a593Smuzhiyun 	struct rq *rq = rq_of_dl_rq(dl_rq);
722*4882a593Smuzhiyun 
723*4882a593Smuzhiyun 	WARN_ON(is_dl_boosted(dl_se));
724*4882a593Smuzhiyun 	WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
725*4882a593Smuzhiyun 
726*4882a593Smuzhiyun 	/*
727*4882a593Smuzhiyun 	 * We are racing with the deadline timer. So, do nothing because
728*4882a593Smuzhiyun 	 * the deadline timer handler will take care of properly recharging
729*4882a593Smuzhiyun 	 * the runtime and postponing the deadline
730*4882a593Smuzhiyun 	 */
731*4882a593Smuzhiyun 	if (dl_se->dl_throttled)
732*4882a593Smuzhiyun 		return;
733*4882a593Smuzhiyun 
734*4882a593Smuzhiyun 	/*
735*4882a593Smuzhiyun 	 * We use the regular wall clock time to set deadlines in the
736*4882a593Smuzhiyun 	 * future; in fact, we must consider execution overheads (time
737*4882a593Smuzhiyun 	 * spent on hardirq context, etc.).
738*4882a593Smuzhiyun 	 */
739*4882a593Smuzhiyun 	dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
740*4882a593Smuzhiyun 	dl_se->runtime = dl_se->dl_runtime;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun /*
744*4882a593Smuzhiyun  * Pure Earliest Deadline First (EDF) scheduling does not deal with the
745*4882a593Smuzhiyun  * possibility of a entity lasting more than what it declared, and thus
746*4882a593Smuzhiyun  * exhausting its runtime.
747*4882a593Smuzhiyun  *
748*4882a593Smuzhiyun  * Here we are interested in making runtime overrun possible, but we do
749*4882a593Smuzhiyun  * not want a entity which is misbehaving to affect the scheduling of all
750*4882a593Smuzhiyun  * other entities.
751*4882a593Smuzhiyun  * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
752*4882a593Smuzhiyun  * is used, in order to confine each entity within its own bandwidth.
753*4882a593Smuzhiyun  *
754*4882a593Smuzhiyun  * This function deals exactly with that, and ensures that when the runtime
755*4882a593Smuzhiyun  * of a entity is replenished, its deadline is also postponed. That ensures
756*4882a593Smuzhiyun  * the overrunning entity can't interfere with other entity in the system and
757*4882a593Smuzhiyun  * can't make them miss their deadlines. Reasons why this kind of overruns
758*4882a593Smuzhiyun  * could happen are, typically, a entity voluntarily trying to overcome its
759*4882a593Smuzhiyun  * runtime, or it just underestimated it during sched_setattr().
760*4882a593Smuzhiyun  */
replenish_dl_entity(struct sched_dl_entity * dl_se)761*4882a593Smuzhiyun static void replenish_dl_entity(struct sched_dl_entity *dl_se)
762*4882a593Smuzhiyun {
763*4882a593Smuzhiyun 	struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
764*4882a593Smuzhiyun 	struct rq *rq = rq_of_dl_rq(dl_rq);
765*4882a593Smuzhiyun 
766*4882a593Smuzhiyun 	BUG_ON(pi_of(dl_se)->dl_runtime <= 0);
767*4882a593Smuzhiyun 
768*4882a593Smuzhiyun 	/*
769*4882a593Smuzhiyun 	 * This could be the case for a !-dl task that is boosted.
770*4882a593Smuzhiyun 	 * Just go with full inherited parameters.
771*4882a593Smuzhiyun 	 */
772*4882a593Smuzhiyun 	if (dl_se->dl_deadline == 0) {
773*4882a593Smuzhiyun 		dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
774*4882a593Smuzhiyun 		dl_se->runtime = pi_of(dl_se)->dl_runtime;
775*4882a593Smuzhiyun 	}
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun 	if (dl_se->dl_yielded && dl_se->runtime > 0)
778*4882a593Smuzhiyun 		dl_se->runtime = 0;
779*4882a593Smuzhiyun 
780*4882a593Smuzhiyun 	/*
781*4882a593Smuzhiyun 	 * We keep moving the deadline away until we get some
782*4882a593Smuzhiyun 	 * available runtime for the entity. This ensures correct
783*4882a593Smuzhiyun 	 * handling of situations where the runtime overrun is
784*4882a593Smuzhiyun 	 * arbitrary large.
785*4882a593Smuzhiyun 	 */
786*4882a593Smuzhiyun 	while (dl_se->runtime <= 0) {
787*4882a593Smuzhiyun 		dl_se->deadline += pi_of(dl_se)->dl_period;
788*4882a593Smuzhiyun 		dl_se->runtime += pi_of(dl_se)->dl_runtime;
789*4882a593Smuzhiyun 	}
790*4882a593Smuzhiyun 
791*4882a593Smuzhiyun 	/*
792*4882a593Smuzhiyun 	 * At this point, the deadline really should be "in
793*4882a593Smuzhiyun 	 * the future" with respect to rq->clock. If it's
794*4882a593Smuzhiyun 	 * not, we are, for some reason, lagging too much!
795*4882a593Smuzhiyun 	 * Anyway, after having warn userspace abut that,
796*4882a593Smuzhiyun 	 * we still try to keep the things running by
797*4882a593Smuzhiyun 	 * resetting the deadline and the budget of the
798*4882a593Smuzhiyun 	 * entity.
799*4882a593Smuzhiyun 	 */
800*4882a593Smuzhiyun 	if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
801*4882a593Smuzhiyun 		printk_deferred_once("sched: DL replenish lagged too much\n");
802*4882a593Smuzhiyun 		dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
803*4882a593Smuzhiyun 		dl_se->runtime = pi_of(dl_se)->dl_runtime;
804*4882a593Smuzhiyun 	}
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun 	if (dl_se->dl_yielded)
807*4882a593Smuzhiyun 		dl_se->dl_yielded = 0;
808*4882a593Smuzhiyun 	if (dl_se->dl_throttled)
809*4882a593Smuzhiyun 		dl_se->dl_throttled = 0;
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun 
812*4882a593Smuzhiyun /*
813*4882a593Smuzhiyun  * Here we check if --at time t-- an entity (which is probably being
814*4882a593Smuzhiyun  * [re]activated or, in general, enqueued) can use its remaining runtime
815*4882a593Smuzhiyun  * and its current deadline _without_ exceeding the bandwidth it is
816*4882a593Smuzhiyun  * assigned (function returns true if it can't). We are in fact applying
817*4882a593Smuzhiyun  * one of the CBS rules: when a task wakes up, if the residual runtime
818*4882a593Smuzhiyun  * over residual deadline fits within the allocated bandwidth, then we
819*4882a593Smuzhiyun  * can keep the current (absolute) deadline and residual budget without
820*4882a593Smuzhiyun  * disrupting the schedulability of the system. Otherwise, we should
821*4882a593Smuzhiyun  * refill the runtime and set the deadline a period in the future,
822*4882a593Smuzhiyun  * because keeping the current (absolute) deadline of the task would
823*4882a593Smuzhiyun  * result in breaking guarantees promised to other tasks (refer to
824*4882a593Smuzhiyun  * Documentation/scheduler/sched-deadline.rst for more information).
825*4882a593Smuzhiyun  *
826*4882a593Smuzhiyun  * This function returns true if:
827*4882a593Smuzhiyun  *
828*4882a593Smuzhiyun  *   runtime / (deadline - t) > dl_runtime / dl_deadline ,
829*4882a593Smuzhiyun  *
830*4882a593Smuzhiyun  * IOW we can't recycle current parameters.
831*4882a593Smuzhiyun  *
832*4882a593Smuzhiyun  * Notice that the bandwidth check is done against the deadline. For
833*4882a593Smuzhiyun  * task with deadline equal to period this is the same of using
834*4882a593Smuzhiyun  * dl_period instead of dl_deadline in the equation above.
835*4882a593Smuzhiyun  */
dl_entity_overflow(struct sched_dl_entity * dl_se,u64 t)836*4882a593Smuzhiyun static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
837*4882a593Smuzhiyun {
838*4882a593Smuzhiyun 	u64 left, right;
839*4882a593Smuzhiyun 
840*4882a593Smuzhiyun 	/*
841*4882a593Smuzhiyun 	 * left and right are the two sides of the equation above,
842*4882a593Smuzhiyun 	 * after a bit of shuffling to use multiplications instead
843*4882a593Smuzhiyun 	 * of divisions.
844*4882a593Smuzhiyun 	 *
845*4882a593Smuzhiyun 	 * Note that none of the time values involved in the two
846*4882a593Smuzhiyun 	 * multiplications are absolute: dl_deadline and dl_runtime
847*4882a593Smuzhiyun 	 * are the relative deadline and the maximum runtime of each
848*4882a593Smuzhiyun 	 * instance, runtime is the runtime left for the last instance
849*4882a593Smuzhiyun 	 * and (deadline - t), since t is rq->clock, is the time left
850*4882a593Smuzhiyun 	 * to the (absolute) deadline. Even if overflowing the u64 type
851*4882a593Smuzhiyun 	 * is very unlikely to occur in both cases, here we scale down
852*4882a593Smuzhiyun 	 * as we want to avoid that risk at all. Scaling down by 10
853*4882a593Smuzhiyun 	 * means that we reduce granularity to 1us. We are fine with it,
854*4882a593Smuzhiyun 	 * since this is only a true/false check and, anyway, thinking
855*4882a593Smuzhiyun 	 * of anything below microseconds resolution is actually fiction
856*4882a593Smuzhiyun 	 * (but still we want to give the user that illusion >;).
857*4882a593Smuzhiyun 	 */
858*4882a593Smuzhiyun 	left = (pi_of(dl_se)->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
859*4882a593Smuzhiyun 	right = ((dl_se->deadline - t) >> DL_SCALE) *
860*4882a593Smuzhiyun 		(pi_of(dl_se)->dl_runtime >> DL_SCALE);
861*4882a593Smuzhiyun 
862*4882a593Smuzhiyun 	return dl_time_before(right, left);
863*4882a593Smuzhiyun }
864*4882a593Smuzhiyun 
865*4882a593Smuzhiyun /*
866*4882a593Smuzhiyun  * Revised wakeup rule [1]: For self-suspending tasks, rather then
867*4882a593Smuzhiyun  * re-initializing task's runtime and deadline, the revised wakeup
868*4882a593Smuzhiyun  * rule adjusts the task's runtime to avoid the task to overrun its
869*4882a593Smuzhiyun  * density.
870*4882a593Smuzhiyun  *
871*4882a593Smuzhiyun  * Reasoning: a task may overrun the density if:
872*4882a593Smuzhiyun  *    runtime / (deadline - t) > dl_runtime / dl_deadline
873*4882a593Smuzhiyun  *
874*4882a593Smuzhiyun  * Therefore, runtime can be adjusted to:
875*4882a593Smuzhiyun  *     runtime = (dl_runtime / dl_deadline) * (deadline - t)
876*4882a593Smuzhiyun  *
877*4882a593Smuzhiyun  * In such way that runtime will be equal to the maximum density
878*4882a593Smuzhiyun  * the task can use without breaking any rule.
879*4882a593Smuzhiyun  *
880*4882a593Smuzhiyun  * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
881*4882a593Smuzhiyun  * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
882*4882a593Smuzhiyun  */
883*4882a593Smuzhiyun static void
update_dl_revised_wakeup(struct sched_dl_entity * dl_se,struct rq * rq)884*4882a593Smuzhiyun update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq)
885*4882a593Smuzhiyun {
886*4882a593Smuzhiyun 	u64 laxity = dl_se->deadline - rq_clock(rq);
887*4882a593Smuzhiyun 
888*4882a593Smuzhiyun 	/*
889*4882a593Smuzhiyun 	 * If the task has deadline < period, and the deadline is in the past,
890*4882a593Smuzhiyun 	 * it should already be throttled before this check.
891*4882a593Smuzhiyun 	 *
892*4882a593Smuzhiyun 	 * See update_dl_entity() comments for further details.
893*4882a593Smuzhiyun 	 */
894*4882a593Smuzhiyun 	WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq)));
895*4882a593Smuzhiyun 
896*4882a593Smuzhiyun 	dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT;
897*4882a593Smuzhiyun }
898*4882a593Smuzhiyun 
899*4882a593Smuzhiyun /*
900*4882a593Smuzhiyun  * Regarding the deadline, a task with implicit deadline has a relative
901*4882a593Smuzhiyun  * deadline == relative period. A task with constrained deadline has a
902*4882a593Smuzhiyun  * relative deadline <= relative period.
903*4882a593Smuzhiyun  *
904*4882a593Smuzhiyun  * We support constrained deadline tasks. However, there are some restrictions
905*4882a593Smuzhiyun  * applied only for tasks which do not have an implicit deadline. See
906*4882a593Smuzhiyun  * update_dl_entity() to know more about such restrictions.
907*4882a593Smuzhiyun  *
908*4882a593Smuzhiyun  * The dl_is_implicit() returns true if the task has an implicit deadline.
909*4882a593Smuzhiyun  */
dl_is_implicit(struct sched_dl_entity * dl_se)910*4882a593Smuzhiyun static inline bool dl_is_implicit(struct sched_dl_entity *dl_se)
911*4882a593Smuzhiyun {
912*4882a593Smuzhiyun 	return dl_se->dl_deadline == dl_se->dl_period;
913*4882a593Smuzhiyun }
914*4882a593Smuzhiyun 
915*4882a593Smuzhiyun /*
916*4882a593Smuzhiyun  * When a deadline entity is placed in the runqueue, its runtime and deadline
917*4882a593Smuzhiyun  * might need to be updated. This is done by a CBS wake up rule. There are two
918*4882a593Smuzhiyun  * different rules: 1) the original CBS; and 2) the Revisited CBS.
919*4882a593Smuzhiyun  *
920*4882a593Smuzhiyun  * When the task is starting a new period, the Original CBS is used. In this
921*4882a593Smuzhiyun  * case, the runtime is replenished and a new absolute deadline is set.
922*4882a593Smuzhiyun  *
923*4882a593Smuzhiyun  * When a task is queued before the begin of the next period, using the
924*4882a593Smuzhiyun  * remaining runtime and deadline could make the entity to overflow, see
925*4882a593Smuzhiyun  * dl_entity_overflow() to find more about runtime overflow. When such case
926*4882a593Smuzhiyun  * is detected, the runtime and deadline need to be updated.
927*4882a593Smuzhiyun  *
928*4882a593Smuzhiyun  * If the task has an implicit deadline, i.e., deadline == period, the Original
929*4882a593Smuzhiyun  * CBS is applied. the runtime is replenished and a new absolute deadline is
930*4882a593Smuzhiyun  * set, as in the previous cases.
931*4882a593Smuzhiyun  *
932*4882a593Smuzhiyun  * However, the Original CBS does not work properly for tasks with
933*4882a593Smuzhiyun  * deadline < period, which are said to have a constrained deadline. By
934*4882a593Smuzhiyun  * applying the Original CBS, a constrained deadline task would be able to run
935*4882a593Smuzhiyun  * runtime/deadline in a period. With deadline < period, the task would
936*4882a593Smuzhiyun  * overrun the runtime/period allowed bandwidth, breaking the admission test.
937*4882a593Smuzhiyun  *
938*4882a593Smuzhiyun  * In order to prevent this misbehave, the Revisited CBS is used for
939*4882a593Smuzhiyun  * constrained deadline tasks when a runtime overflow is detected. In the
940*4882a593Smuzhiyun  * Revisited CBS, rather than replenishing & setting a new absolute deadline,
941*4882a593Smuzhiyun  * the remaining runtime of the task is reduced to avoid runtime overflow.
942*4882a593Smuzhiyun  * Please refer to the comments update_dl_revised_wakeup() function to find
943*4882a593Smuzhiyun  * more about the Revised CBS rule.
944*4882a593Smuzhiyun  */
update_dl_entity(struct sched_dl_entity * dl_se)945*4882a593Smuzhiyun static void update_dl_entity(struct sched_dl_entity *dl_se)
946*4882a593Smuzhiyun {
947*4882a593Smuzhiyun 	struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
948*4882a593Smuzhiyun 	struct rq *rq = rq_of_dl_rq(dl_rq);
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
951*4882a593Smuzhiyun 	    dl_entity_overflow(dl_se, rq_clock(rq))) {
952*4882a593Smuzhiyun 
953*4882a593Smuzhiyun 		if (unlikely(!dl_is_implicit(dl_se) &&
954*4882a593Smuzhiyun 			     !dl_time_before(dl_se->deadline, rq_clock(rq)) &&
955*4882a593Smuzhiyun 			     !is_dl_boosted(dl_se))) {
956*4882a593Smuzhiyun 			update_dl_revised_wakeup(dl_se, rq);
957*4882a593Smuzhiyun 			return;
958*4882a593Smuzhiyun 		}
959*4882a593Smuzhiyun 
960*4882a593Smuzhiyun 		dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
961*4882a593Smuzhiyun 		dl_se->runtime = pi_of(dl_se)->dl_runtime;
962*4882a593Smuzhiyun 	}
963*4882a593Smuzhiyun }
964*4882a593Smuzhiyun 
dl_next_period(struct sched_dl_entity * dl_se)965*4882a593Smuzhiyun static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
966*4882a593Smuzhiyun {
967*4882a593Smuzhiyun 	return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun 
970*4882a593Smuzhiyun /*
971*4882a593Smuzhiyun  * If the entity depleted all its runtime, and if we want it to sleep
972*4882a593Smuzhiyun  * while waiting for some new execution time to become available, we
973*4882a593Smuzhiyun  * set the bandwidth replenishment timer to the replenishment instant
974*4882a593Smuzhiyun  * and try to activate it.
975*4882a593Smuzhiyun  *
976*4882a593Smuzhiyun  * Notice that it is important for the caller to know if the timer
977*4882a593Smuzhiyun  * actually started or not (i.e., the replenishment instant is in
978*4882a593Smuzhiyun  * the future or in the past).
979*4882a593Smuzhiyun  */
start_dl_timer(struct task_struct * p)980*4882a593Smuzhiyun static int start_dl_timer(struct task_struct *p)
981*4882a593Smuzhiyun {
982*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se = &p->dl;
983*4882a593Smuzhiyun 	struct hrtimer *timer = &dl_se->dl_timer;
984*4882a593Smuzhiyun 	struct rq *rq = task_rq(p);
985*4882a593Smuzhiyun 	ktime_t now, act;
986*4882a593Smuzhiyun 	s64 delta;
987*4882a593Smuzhiyun 
988*4882a593Smuzhiyun 	lockdep_assert_held(&rq->lock);
989*4882a593Smuzhiyun 
990*4882a593Smuzhiyun 	/*
991*4882a593Smuzhiyun 	 * We want the timer to fire at the deadline, but considering
992*4882a593Smuzhiyun 	 * that it is actually coming from rq->clock and not from
993*4882a593Smuzhiyun 	 * hrtimer's time base reading.
994*4882a593Smuzhiyun 	 */
995*4882a593Smuzhiyun 	act = ns_to_ktime(dl_next_period(dl_se));
996*4882a593Smuzhiyun 	now = hrtimer_cb_get_time(timer);
997*4882a593Smuzhiyun 	delta = ktime_to_ns(now) - rq_clock(rq);
998*4882a593Smuzhiyun 	act = ktime_add_ns(act, delta);
999*4882a593Smuzhiyun 
1000*4882a593Smuzhiyun 	/*
1001*4882a593Smuzhiyun 	 * If the expiry time already passed, e.g., because the value
1002*4882a593Smuzhiyun 	 * chosen as the deadline is too small, don't even try to
1003*4882a593Smuzhiyun 	 * start the timer in the past!
1004*4882a593Smuzhiyun 	 */
1005*4882a593Smuzhiyun 	if (ktime_us_delta(act, now) < 0)
1006*4882a593Smuzhiyun 		return 0;
1007*4882a593Smuzhiyun 
1008*4882a593Smuzhiyun 	/*
1009*4882a593Smuzhiyun 	 * !enqueued will guarantee another callback; even if one is already in
1010*4882a593Smuzhiyun 	 * progress. This ensures a balanced {get,put}_task_struct().
1011*4882a593Smuzhiyun 	 *
1012*4882a593Smuzhiyun 	 * The race against __run_timer() clearing the enqueued state is
1013*4882a593Smuzhiyun 	 * harmless because we're holding task_rq()->lock, therefore the timer
1014*4882a593Smuzhiyun 	 * expiring after we've done the check will wait on its task_rq_lock()
1015*4882a593Smuzhiyun 	 * and observe our state.
1016*4882a593Smuzhiyun 	 */
1017*4882a593Smuzhiyun 	if (!hrtimer_is_queued(timer)) {
1018*4882a593Smuzhiyun 		get_task_struct(p);
1019*4882a593Smuzhiyun 		hrtimer_start(timer, act, HRTIMER_MODE_ABS_HARD);
1020*4882a593Smuzhiyun 	}
1021*4882a593Smuzhiyun 
1022*4882a593Smuzhiyun 	return 1;
1023*4882a593Smuzhiyun }
1024*4882a593Smuzhiyun 
1025*4882a593Smuzhiyun /*
1026*4882a593Smuzhiyun  * This is the bandwidth enforcement timer callback. If here, we know
1027*4882a593Smuzhiyun  * a task is not on its dl_rq, since the fact that the timer was running
1028*4882a593Smuzhiyun  * means the task is throttled and needs a runtime replenishment.
1029*4882a593Smuzhiyun  *
1030*4882a593Smuzhiyun  * However, what we actually do depends on the fact the task is active,
1031*4882a593Smuzhiyun  * (it is on its rq) or has been removed from there by a call to
1032*4882a593Smuzhiyun  * dequeue_task_dl(). In the former case we must issue the runtime
1033*4882a593Smuzhiyun  * replenishment and add the task back to the dl_rq; in the latter, we just
1034*4882a593Smuzhiyun  * do nothing but clearing dl_throttled, so that runtime and deadline
1035*4882a593Smuzhiyun  * updating (and the queueing back to dl_rq) will be done by the
1036*4882a593Smuzhiyun  * next call to enqueue_task_dl().
1037*4882a593Smuzhiyun  */
dl_task_timer(struct hrtimer * timer)1038*4882a593Smuzhiyun static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
1039*4882a593Smuzhiyun {
1040*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se = container_of(timer,
1041*4882a593Smuzhiyun 						     struct sched_dl_entity,
1042*4882a593Smuzhiyun 						     dl_timer);
1043*4882a593Smuzhiyun 	struct task_struct *p = dl_task_of(dl_se);
1044*4882a593Smuzhiyun 	struct rq_flags rf;
1045*4882a593Smuzhiyun 	struct rq *rq;
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun 	rq = task_rq_lock(p, &rf);
1048*4882a593Smuzhiyun 
1049*4882a593Smuzhiyun 	/*
1050*4882a593Smuzhiyun 	 * The task might have changed its scheduling policy to something
1051*4882a593Smuzhiyun 	 * different than SCHED_DEADLINE (through switched_from_dl()).
1052*4882a593Smuzhiyun 	 */
1053*4882a593Smuzhiyun 	if (!dl_task(p))
1054*4882a593Smuzhiyun 		goto unlock;
1055*4882a593Smuzhiyun 
1056*4882a593Smuzhiyun 	/*
1057*4882a593Smuzhiyun 	 * The task might have been boosted by someone else and might be in the
1058*4882a593Smuzhiyun 	 * boosting/deboosting path, its not throttled.
1059*4882a593Smuzhiyun 	 */
1060*4882a593Smuzhiyun 	if (is_dl_boosted(dl_se))
1061*4882a593Smuzhiyun 		goto unlock;
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun 	/*
1064*4882a593Smuzhiyun 	 * Spurious timer due to start_dl_timer() race; or we already received
1065*4882a593Smuzhiyun 	 * a replenishment from rt_mutex_setprio().
1066*4882a593Smuzhiyun 	 */
1067*4882a593Smuzhiyun 	if (!dl_se->dl_throttled)
1068*4882a593Smuzhiyun 		goto unlock;
1069*4882a593Smuzhiyun 
1070*4882a593Smuzhiyun 	sched_clock_tick();
1071*4882a593Smuzhiyun 	update_rq_clock(rq);
1072*4882a593Smuzhiyun 
1073*4882a593Smuzhiyun 	/*
1074*4882a593Smuzhiyun 	 * If the throttle happened during sched-out; like:
1075*4882a593Smuzhiyun 	 *
1076*4882a593Smuzhiyun 	 *   schedule()
1077*4882a593Smuzhiyun 	 *     deactivate_task()
1078*4882a593Smuzhiyun 	 *       dequeue_task_dl()
1079*4882a593Smuzhiyun 	 *         update_curr_dl()
1080*4882a593Smuzhiyun 	 *           start_dl_timer()
1081*4882a593Smuzhiyun 	 *         __dequeue_task_dl()
1082*4882a593Smuzhiyun 	 *     prev->on_rq = 0;
1083*4882a593Smuzhiyun 	 *
1084*4882a593Smuzhiyun 	 * We can be both throttled and !queued. Replenish the counter
1085*4882a593Smuzhiyun 	 * but do not enqueue -- wait for our wakeup to do that.
1086*4882a593Smuzhiyun 	 */
1087*4882a593Smuzhiyun 	if (!task_on_rq_queued(p)) {
1088*4882a593Smuzhiyun 		replenish_dl_entity(dl_se);
1089*4882a593Smuzhiyun 		goto unlock;
1090*4882a593Smuzhiyun 	}
1091*4882a593Smuzhiyun 
1092*4882a593Smuzhiyun #ifdef CONFIG_SMP
1093*4882a593Smuzhiyun 	if (unlikely(!rq->online)) {
1094*4882a593Smuzhiyun 		/*
1095*4882a593Smuzhiyun 		 * If the runqueue is no longer available, migrate the
1096*4882a593Smuzhiyun 		 * task elsewhere. This necessarily changes rq.
1097*4882a593Smuzhiyun 		 */
1098*4882a593Smuzhiyun 		lockdep_unpin_lock(&rq->lock, rf.cookie);
1099*4882a593Smuzhiyun 		rq = dl_task_offline_migration(rq, p);
1100*4882a593Smuzhiyun 		rf.cookie = lockdep_pin_lock(&rq->lock);
1101*4882a593Smuzhiyun 		update_rq_clock(rq);
1102*4882a593Smuzhiyun 
1103*4882a593Smuzhiyun 		/*
1104*4882a593Smuzhiyun 		 * Now that the task has been migrated to the new RQ and we
1105*4882a593Smuzhiyun 		 * have that locked, proceed as normal and enqueue the task
1106*4882a593Smuzhiyun 		 * there.
1107*4882a593Smuzhiyun 		 */
1108*4882a593Smuzhiyun 	}
1109*4882a593Smuzhiyun #endif
1110*4882a593Smuzhiyun 
1111*4882a593Smuzhiyun 	enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
1112*4882a593Smuzhiyun 	if (dl_task(rq->curr))
1113*4882a593Smuzhiyun 		check_preempt_curr_dl(rq, p, 0);
1114*4882a593Smuzhiyun 	else
1115*4882a593Smuzhiyun 		resched_curr(rq);
1116*4882a593Smuzhiyun 
1117*4882a593Smuzhiyun #ifdef CONFIG_SMP
1118*4882a593Smuzhiyun 	/*
1119*4882a593Smuzhiyun 	 * Queueing this task back might have overloaded rq, check if we need
1120*4882a593Smuzhiyun 	 * to kick someone away.
1121*4882a593Smuzhiyun 	 */
1122*4882a593Smuzhiyun 	if (has_pushable_dl_tasks(rq)) {
1123*4882a593Smuzhiyun 		/*
1124*4882a593Smuzhiyun 		 * Nothing relies on rq->lock after this, so its safe to drop
1125*4882a593Smuzhiyun 		 * rq->lock.
1126*4882a593Smuzhiyun 		 */
1127*4882a593Smuzhiyun 		rq_unpin_lock(rq, &rf);
1128*4882a593Smuzhiyun 		push_dl_task(rq);
1129*4882a593Smuzhiyun 		rq_repin_lock(rq, &rf);
1130*4882a593Smuzhiyun 	}
1131*4882a593Smuzhiyun #endif
1132*4882a593Smuzhiyun 
1133*4882a593Smuzhiyun unlock:
1134*4882a593Smuzhiyun 	task_rq_unlock(rq, p, &rf);
1135*4882a593Smuzhiyun 
1136*4882a593Smuzhiyun 	/*
1137*4882a593Smuzhiyun 	 * This can free the task_struct, including this hrtimer, do not touch
1138*4882a593Smuzhiyun 	 * anything related to that after this.
1139*4882a593Smuzhiyun 	 */
1140*4882a593Smuzhiyun 	put_task_struct(p);
1141*4882a593Smuzhiyun 
1142*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
1143*4882a593Smuzhiyun }
1144*4882a593Smuzhiyun 
init_dl_task_timer(struct sched_dl_entity * dl_se)1145*4882a593Smuzhiyun void init_dl_task_timer(struct sched_dl_entity *dl_se)
1146*4882a593Smuzhiyun {
1147*4882a593Smuzhiyun 	struct hrtimer *timer = &dl_se->dl_timer;
1148*4882a593Smuzhiyun 
1149*4882a593Smuzhiyun 	hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
1150*4882a593Smuzhiyun 	timer->function = dl_task_timer;
1151*4882a593Smuzhiyun }
1152*4882a593Smuzhiyun 
1153*4882a593Smuzhiyun /*
1154*4882a593Smuzhiyun  * During the activation, CBS checks if it can reuse the current task's
1155*4882a593Smuzhiyun  * runtime and period. If the deadline of the task is in the past, CBS
1156*4882a593Smuzhiyun  * cannot use the runtime, and so it replenishes the task. This rule
1157*4882a593Smuzhiyun  * works fine for implicit deadline tasks (deadline == period), and the
1158*4882a593Smuzhiyun  * CBS was designed for implicit deadline tasks. However, a task with
1159*4882a593Smuzhiyun  * constrained deadline (deadline < period) might be awakened after the
1160*4882a593Smuzhiyun  * deadline, but before the next period. In this case, replenishing the
1161*4882a593Smuzhiyun  * task would allow it to run for runtime / deadline. As in this case
1162*4882a593Smuzhiyun  * deadline < period, CBS enables a task to run for more than the
1163*4882a593Smuzhiyun  * runtime / period. In a very loaded system, this can cause a domino
1164*4882a593Smuzhiyun  * effect, making other tasks miss their deadlines.
1165*4882a593Smuzhiyun  *
1166*4882a593Smuzhiyun  * To avoid this problem, in the activation of a constrained deadline
1167*4882a593Smuzhiyun  * task after the deadline but before the next period, throttle the
1168*4882a593Smuzhiyun  * task and set the replenishing timer to the begin of the next period,
1169*4882a593Smuzhiyun  * unless it is boosted.
1170*4882a593Smuzhiyun  */
dl_check_constrained_dl(struct sched_dl_entity * dl_se)1171*4882a593Smuzhiyun static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
1172*4882a593Smuzhiyun {
1173*4882a593Smuzhiyun 	struct task_struct *p = dl_task_of(dl_se);
1174*4882a593Smuzhiyun 	struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se));
1175*4882a593Smuzhiyun 
1176*4882a593Smuzhiyun 	if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
1177*4882a593Smuzhiyun 	    dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
1178*4882a593Smuzhiyun 		if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(p)))
1179*4882a593Smuzhiyun 			return;
1180*4882a593Smuzhiyun 		dl_se->dl_throttled = 1;
1181*4882a593Smuzhiyun 		if (dl_se->runtime > 0)
1182*4882a593Smuzhiyun 			dl_se->runtime = 0;
1183*4882a593Smuzhiyun 	}
1184*4882a593Smuzhiyun }
1185*4882a593Smuzhiyun 
1186*4882a593Smuzhiyun static
dl_runtime_exceeded(struct sched_dl_entity * dl_se)1187*4882a593Smuzhiyun int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
1188*4882a593Smuzhiyun {
1189*4882a593Smuzhiyun 	return (dl_se->runtime <= 0);
1190*4882a593Smuzhiyun }
1191*4882a593Smuzhiyun 
1192*4882a593Smuzhiyun extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
1193*4882a593Smuzhiyun 
1194*4882a593Smuzhiyun /*
1195*4882a593Smuzhiyun  * This function implements the GRUB accounting rule:
1196*4882a593Smuzhiyun  * according to the GRUB reclaiming algorithm, the runtime is
1197*4882a593Smuzhiyun  * not decreased as "dq = -dt", but as
1198*4882a593Smuzhiyun  * "dq = -max{u / Umax, (1 - Uinact - Uextra)} dt",
1199*4882a593Smuzhiyun  * where u is the utilization of the task, Umax is the maximum reclaimable
1200*4882a593Smuzhiyun  * utilization, Uinact is the (per-runqueue) inactive utilization, computed
1201*4882a593Smuzhiyun  * as the difference between the "total runqueue utilization" and the
1202*4882a593Smuzhiyun  * runqueue active utilization, and Uextra is the (per runqueue) extra
1203*4882a593Smuzhiyun  * reclaimable utilization.
1204*4882a593Smuzhiyun  * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations
1205*4882a593Smuzhiyun  * multiplied by 2^BW_SHIFT, the result has to be shifted right by
1206*4882a593Smuzhiyun  * BW_SHIFT.
1207*4882a593Smuzhiyun  * Since rq->dl.bw_ratio contains 1 / Umax multipled by 2^RATIO_SHIFT,
1208*4882a593Smuzhiyun  * dl_bw is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT.
1209*4882a593Smuzhiyun  * Since delta is a 64 bit variable, to have an overflow its value
1210*4882a593Smuzhiyun  * should be larger than 2^(64 - 20 - 8), which is more than 64 seconds.
1211*4882a593Smuzhiyun  * So, overflow is not an issue here.
1212*4882a593Smuzhiyun  */
grub_reclaim(u64 delta,struct rq * rq,struct sched_dl_entity * dl_se)1213*4882a593Smuzhiyun static u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se)
1214*4882a593Smuzhiyun {
1215*4882a593Smuzhiyun 	u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */
1216*4882a593Smuzhiyun 	u64 u_act;
1217*4882a593Smuzhiyun 	u64 u_act_min = (dl_se->dl_bw * rq->dl.bw_ratio) >> RATIO_SHIFT;
1218*4882a593Smuzhiyun 
1219*4882a593Smuzhiyun 	/*
1220*4882a593Smuzhiyun 	 * Instead of computing max{u * bw_ratio, (1 - u_inact - u_extra)},
1221*4882a593Smuzhiyun 	 * we compare u_inact + rq->dl.extra_bw with
1222*4882a593Smuzhiyun 	 * 1 - (u * rq->dl.bw_ratio >> RATIO_SHIFT), because
1223*4882a593Smuzhiyun 	 * u_inact + rq->dl.extra_bw can be larger than
1224*4882a593Smuzhiyun 	 * 1 * (so, 1 - u_inact - rq->dl.extra_bw would be negative
1225*4882a593Smuzhiyun 	 * leading to wrong results)
1226*4882a593Smuzhiyun 	 */
1227*4882a593Smuzhiyun 	if (u_inact + rq->dl.extra_bw > BW_UNIT - u_act_min)
1228*4882a593Smuzhiyun 		u_act = u_act_min;
1229*4882a593Smuzhiyun 	else
1230*4882a593Smuzhiyun 		u_act = BW_UNIT - u_inact - rq->dl.extra_bw;
1231*4882a593Smuzhiyun 
1232*4882a593Smuzhiyun 	return (delta * u_act) >> BW_SHIFT;
1233*4882a593Smuzhiyun }
1234*4882a593Smuzhiyun 
1235*4882a593Smuzhiyun /*
1236*4882a593Smuzhiyun  * Update the current task's runtime statistics (provided it is still
1237*4882a593Smuzhiyun  * a -deadline task and has not been removed from the dl_rq).
1238*4882a593Smuzhiyun  */
update_curr_dl(struct rq * rq)1239*4882a593Smuzhiyun static void update_curr_dl(struct rq *rq)
1240*4882a593Smuzhiyun {
1241*4882a593Smuzhiyun 	struct task_struct *curr = rq->curr;
1242*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se = &curr->dl;
1243*4882a593Smuzhiyun 	u64 delta_exec, scaled_delta_exec;
1244*4882a593Smuzhiyun 	int cpu = cpu_of(rq);
1245*4882a593Smuzhiyun 	u64 now;
1246*4882a593Smuzhiyun 
1247*4882a593Smuzhiyun 	if (!dl_task(curr) || !on_dl_rq(dl_se))
1248*4882a593Smuzhiyun 		return;
1249*4882a593Smuzhiyun 
1250*4882a593Smuzhiyun 	/*
1251*4882a593Smuzhiyun 	 * Consumed budget is computed considering the time as
1252*4882a593Smuzhiyun 	 * observed by schedulable tasks (excluding time spent
1253*4882a593Smuzhiyun 	 * in hardirq context, etc.). Deadlines are instead
1254*4882a593Smuzhiyun 	 * computed using hard walltime. This seems to be the more
1255*4882a593Smuzhiyun 	 * natural solution, but the full ramifications of this
1256*4882a593Smuzhiyun 	 * approach need further study.
1257*4882a593Smuzhiyun 	 */
1258*4882a593Smuzhiyun 	now = rq_clock_task(rq);
1259*4882a593Smuzhiyun 	delta_exec = now - curr->se.exec_start;
1260*4882a593Smuzhiyun 	if (unlikely((s64)delta_exec <= 0)) {
1261*4882a593Smuzhiyun 		if (unlikely(dl_se->dl_yielded))
1262*4882a593Smuzhiyun 			goto throttle;
1263*4882a593Smuzhiyun 		return;
1264*4882a593Smuzhiyun 	}
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	schedstat_set(curr->se.statistics.exec_max,
1267*4882a593Smuzhiyun 		      max(curr->se.statistics.exec_max, delta_exec));
1268*4882a593Smuzhiyun 
1269*4882a593Smuzhiyun 	curr->se.sum_exec_runtime += delta_exec;
1270*4882a593Smuzhiyun 	account_group_exec_runtime(curr, delta_exec);
1271*4882a593Smuzhiyun 
1272*4882a593Smuzhiyun 	curr->se.exec_start = now;
1273*4882a593Smuzhiyun 	cgroup_account_cputime(curr, delta_exec);
1274*4882a593Smuzhiyun 
1275*4882a593Smuzhiyun 	if (dl_entity_is_special(dl_se))
1276*4882a593Smuzhiyun 		return;
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	/*
1279*4882a593Smuzhiyun 	 * For tasks that participate in GRUB, we implement GRUB-PA: the
1280*4882a593Smuzhiyun 	 * spare reclaimed bandwidth is used to clock down frequency.
1281*4882a593Smuzhiyun 	 *
1282*4882a593Smuzhiyun 	 * For the others, we still need to scale reservation parameters
1283*4882a593Smuzhiyun 	 * according to current frequency and CPU maximum capacity.
1284*4882a593Smuzhiyun 	 */
1285*4882a593Smuzhiyun 	if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) {
1286*4882a593Smuzhiyun 		scaled_delta_exec = grub_reclaim(delta_exec,
1287*4882a593Smuzhiyun 						 rq,
1288*4882a593Smuzhiyun 						 &curr->dl);
1289*4882a593Smuzhiyun 	} else {
1290*4882a593Smuzhiyun 		unsigned long scale_freq = arch_scale_freq_capacity(cpu);
1291*4882a593Smuzhiyun 		unsigned long scale_cpu = arch_scale_cpu_capacity(cpu);
1292*4882a593Smuzhiyun 
1293*4882a593Smuzhiyun 		scaled_delta_exec = cap_scale(delta_exec, scale_freq);
1294*4882a593Smuzhiyun 		scaled_delta_exec = cap_scale(scaled_delta_exec, scale_cpu);
1295*4882a593Smuzhiyun 	}
1296*4882a593Smuzhiyun 
1297*4882a593Smuzhiyun 	dl_se->runtime -= scaled_delta_exec;
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun throttle:
1300*4882a593Smuzhiyun 	if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
1301*4882a593Smuzhiyun 		dl_se->dl_throttled = 1;
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun 		/* If requested, inform the user about runtime overruns. */
1304*4882a593Smuzhiyun 		if (dl_runtime_exceeded(dl_se) &&
1305*4882a593Smuzhiyun 		    (dl_se->flags & SCHED_FLAG_DL_OVERRUN))
1306*4882a593Smuzhiyun 			dl_se->dl_overrun = 1;
1307*4882a593Smuzhiyun 
1308*4882a593Smuzhiyun 		__dequeue_task_dl(rq, curr, 0);
1309*4882a593Smuzhiyun 		if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(curr)))
1310*4882a593Smuzhiyun 			enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
1311*4882a593Smuzhiyun 
1312*4882a593Smuzhiyun 		if (!is_leftmost(curr, &rq->dl))
1313*4882a593Smuzhiyun 			resched_curr(rq);
1314*4882a593Smuzhiyun 	}
1315*4882a593Smuzhiyun 
1316*4882a593Smuzhiyun 	/*
1317*4882a593Smuzhiyun 	 * Because -- for now -- we share the rt bandwidth, we need to
1318*4882a593Smuzhiyun 	 * account our runtime there too, otherwise actual rt tasks
1319*4882a593Smuzhiyun 	 * would be able to exceed the shared quota.
1320*4882a593Smuzhiyun 	 *
1321*4882a593Smuzhiyun 	 * Account to the root rt group for now.
1322*4882a593Smuzhiyun 	 *
1323*4882a593Smuzhiyun 	 * The solution we're working towards is having the RT groups scheduled
1324*4882a593Smuzhiyun 	 * using deadline servers -- however there's a few nasties to figure
1325*4882a593Smuzhiyun 	 * out before that can happen.
1326*4882a593Smuzhiyun 	 */
1327*4882a593Smuzhiyun 	if (rt_bandwidth_enabled()) {
1328*4882a593Smuzhiyun 		struct rt_rq *rt_rq = &rq->rt;
1329*4882a593Smuzhiyun 
1330*4882a593Smuzhiyun 		raw_spin_lock(&rt_rq->rt_runtime_lock);
1331*4882a593Smuzhiyun 		/*
1332*4882a593Smuzhiyun 		 * We'll let actual RT tasks worry about the overflow here, we
1333*4882a593Smuzhiyun 		 * have our own CBS to keep us inline; only account when RT
1334*4882a593Smuzhiyun 		 * bandwidth is relevant.
1335*4882a593Smuzhiyun 		 */
1336*4882a593Smuzhiyun 		if (sched_rt_bandwidth_account(rt_rq))
1337*4882a593Smuzhiyun 			rt_rq->rt_time += delta_exec;
1338*4882a593Smuzhiyun 		raw_spin_unlock(&rt_rq->rt_runtime_lock);
1339*4882a593Smuzhiyun 	}
1340*4882a593Smuzhiyun }
1341*4882a593Smuzhiyun 
inactive_task_timer(struct hrtimer * timer)1342*4882a593Smuzhiyun static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
1343*4882a593Smuzhiyun {
1344*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se = container_of(timer,
1345*4882a593Smuzhiyun 						     struct sched_dl_entity,
1346*4882a593Smuzhiyun 						     inactive_timer);
1347*4882a593Smuzhiyun 	struct task_struct *p = dl_task_of(dl_se);
1348*4882a593Smuzhiyun 	struct rq_flags rf;
1349*4882a593Smuzhiyun 	struct rq *rq;
1350*4882a593Smuzhiyun 
1351*4882a593Smuzhiyun 	rq = task_rq_lock(p, &rf);
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 	sched_clock_tick();
1354*4882a593Smuzhiyun 	update_rq_clock(rq);
1355*4882a593Smuzhiyun 
1356*4882a593Smuzhiyun 	if (!dl_task(p) || p->state == TASK_DEAD) {
1357*4882a593Smuzhiyun 		struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 		if (p->state == TASK_DEAD && dl_se->dl_non_contending) {
1360*4882a593Smuzhiyun 			sub_running_bw(&p->dl, dl_rq_of_se(&p->dl));
1361*4882a593Smuzhiyun 			sub_rq_bw(&p->dl, dl_rq_of_se(&p->dl));
1362*4882a593Smuzhiyun 			dl_se->dl_non_contending = 0;
1363*4882a593Smuzhiyun 		}
1364*4882a593Smuzhiyun 
1365*4882a593Smuzhiyun 		raw_spin_lock(&dl_b->lock);
1366*4882a593Smuzhiyun 		__dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
1367*4882a593Smuzhiyun 		raw_spin_unlock(&dl_b->lock);
1368*4882a593Smuzhiyun 		__dl_clear_params(p);
1369*4882a593Smuzhiyun 
1370*4882a593Smuzhiyun 		goto unlock;
1371*4882a593Smuzhiyun 	}
1372*4882a593Smuzhiyun 	if (dl_se->dl_non_contending == 0)
1373*4882a593Smuzhiyun 		goto unlock;
1374*4882a593Smuzhiyun 
1375*4882a593Smuzhiyun 	sub_running_bw(dl_se, &rq->dl);
1376*4882a593Smuzhiyun 	dl_se->dl_non_contending = 0;
1377*4882a593Smuzhiyun unlock:
1378*4882a593Smuzhiyun 	task_rq_unlock(rq, p, &rf);
1379*4882a593Smuzhiyun 	put_task_struct(p);
1380*4882a593Smuzhiyun 
1381*4882a593Smuzhiyun 	return HRTIMER_NORESTART;
1382*4882a593Smuzhiyun }
1383*4882a593Smuzhiyun 
init_dl_inactive_task_timer(struct sched_dl_entity * dl_se)1384*4882a593Smuzhiyun void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
1385*4882a593Smuzhiyun {
1386*4882a593Smuzhiyun 	struct hrtimer *timer = &dl_se->inactive_timer;
1387*4882a593Smuzhiyun 
1388*4882a593Smuzhiyun 	hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
1389*4882a593Smuzhiyun 	timer->function = inactive_task_timer;
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun 
1392*4882a593Smuzhiyun #ifdef CONFIG_SMP
1393*4882a593Smuzhiyun 
inc_dl_deadline(struct dl_rq * dl_rq,u64 deadline)1394*4882a593Smuzhiyun static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1395*4882a593Smuzhiyun {
1396*4882a593Smuzhiyun 	struct rq *rq = rq_of_dl_rq(dl_rq);
1397*4882a593Smuzhiyun 
1398*4882a593Smuzhiyun 	if (dl_rq->earliest_dl.curr == 0 ||
1399*4882a593Smuzhiyun 	    dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
1400*4882a593Smuzhiyun 		dl_rq->earliest_dl.curr = deadline;
1401*4882a593Smuzhiyun 		cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
1402*4882a593Smuzhiyun 	}
1403*4882a593Smuzhiyun }
1404*4882a593Smuzhiyun 
dec_dl_deadline(struct dl_rq * dl_rq,u64 deadline)1405*4882a593Smuzhiyun static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
1406*4882a593Smuzhiyun {
1407*4882a593Smuzhiyun 	struct rq *rq = rq_of_dl_rq(dl_rq);
1408*4882a593Smuzhiyun 
1409*4882a593Smuzhiyun 	/*
1410*4882a593Smuzhiyun 	 * Since we may have removed our earliest (and/or next earliest)
1411*4882a593Smuzhiyun 	 * task we must recompute them.
1412*4882a593Smuzhiyun 	 */
1413*4882a593Smuzhiyun 	if (!dl_rq->dl_nr_running) {
1414*4882a593Smuzhiyun 		dl_rq->earliest_dl.curr = 0;
1415*4882a593Smuzhiyun 		dl_rq->earliest_dl.next = 0;
1416*4882a593Smuzhiyun 		cpudl_clear(&rq->rd->cpudl, rq->cpu);
1417*4882a593Smuzhiyun 	} else {
1418*4882a593Smuzhiyun 		struct rb_node *leftmost = dl_rq->root.rb_leftmost;
1419*4882a593Smuzhiyun 		struct sched_dl_entity *entry;
1420*4882a593Smuzhiyun 
1421*4882a593Smuzhiyun 		entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
1422*4882a593Smuzhiyun 		dl_rq->earliest_dl.curr = entry->deadline;
1423*4882a593Smuzhiyun 		cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
1424*4882a593Smuzhiyun 	}
1425*4882a593Smuzhiyun }
1426*4882a593Smuzhiyun 
1427*4882a593Smuzhiyun #else
1428*4882a593Smuzhiyun 
inc_dl_deadline(struct dl_rq * dl_rq,u64 deadline)1429*4882a593Smuzhiyun static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
dec_dl_deadline(struct dl_rq * dl_rq,u64 deadline)1430*4882a593Smuzhiyun static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
1431*4882a593Smuzhiyun 
1432*4882a593Smuzhiyun #endif /* CONFIG_SMP */
1433*4882a593Smuzhiyun 
1434*4882a593Smuzhiyun static inline
inc_dl_tasks(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)1435*4882a593Smuzhiyun void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1436*4882a593Smuzhiyun {
1437*4882a593Smuzhiyun 	int prio = dl_task_of(dl_se)->prio;
1438*4882a593Smuzhiyun 	u64 deadline = dl_se->deadline;
1439*4882a593Smuzhiyun 
1440*4882a593Smuzhiyun 	WARN_ON(!dl_prio(prio));
1441*4882a593Smuzhiyun 	dl_rq->dl_nr_running++;
1442*4882a593Smuzhiyun 	add_nr_running(rq_of_dl_rq(dl_rq), 1);
1443*4882a593Smuzhiyun 
1444*4882a593Smuzhiyun 	inc_dl_deadline(dl_rq, deadline);
1445*4882a593Smuzhiyun 	inc_dl_migration(dl_se, dl_rq);
1446*4882a593Smuzhiyun }
1447*4882a593Smuzhiyun 
1448*4882a593Smuzhiyun static inline
dec_dl_tasks(struct sched_dl_entity * dl_se,struct dl_rq * dl_rq)1449*4882a593Smuzhiyun void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
1450*4882a593Smuzhiyun {
1451*4882a593Smuzhiyun 	int prio = dl_task_of(dl_se)->prio;
1452*4882a593Smuzhiyun 
1453*4882a593Smuzhiyun 	WARN_ON(!dl_prio(prio));
1454*4882a593Smuzhiyun 	WARN_ON(!dl_rq->dl_nr_running);
1455*4882a593Smuzhiyun 	dl_rq->dl_nr_running--;
1456*4882a593Smuzhiyun 	sub_nr_running(rq_of_dl_rq(dl_rq), 1);
1457*4882a593Smuzhiyun 
1458*4882a593Smuzhiyun 	dec_dl_deadline(dl_rq, dl_se->deadline);
1459*4882a593Smuzhiyun 	dec_dl_migration(dl_se, dl_rq);
1460*4882a593Smuzhiyun }
1461*4882a593Smuzhiyun 
__enqueue_dl_entity(struct sched_dl_entity * dl_se)1462*4882a593Smuzhiyun static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
1463*4882a593Smuzhiyun {
1464*4882a593Smuzhiyun 	struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1465*4882a593Smuzhiyun 	struct rb_node **link = &dl_rq->root.rb_root.rb_node;
1466*4882a593Smuzhiyun 	struct rb_node *parent = NULL;
1467*4882a593Smuzhiyun 	struct sched_dl_entity *entry;
1468*4882a593Smuzhiyun 	int leftmost = 1;
1469*4882a593Smuzhiyun 
1470*4882a593Smuzhiyun 	BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
1471*4882a593Smuzhiyun 
1472*4882a593Smuzhiyun 	while (*link) {
1473*4882a593Smuzhiyun 		parent = *link;
1474*4882a593Smuzhiyun 		entry = rb_entry(parent, struct sched_dl_entity, rb_node);
1475*4882a593Smuzhiyun 		if (dl_time_before(dl_se->deadline, entry->deadline))
1476*4882a593Smuzhiyun 			link = &parent->rb_left;
1477*4882a593Smuzhiyun 		else {
1478*4882a593Smuzhiyun 			link = &parent->rb_right;
1479*4882a593Smuzhiyun 			leftmost = 0;
1480*4882a593Smuzhiyun 		}
1481*4882a593Smuzhiyun 	}
1482*4882a593Smuzhiyun 
1483*4882a593Smuzhiyun 	rb_link_node(&dl_se->rb_node, parent, link);
1484*4882a593Smuzhiyun 	rb_insert_color_cached(&dl_se->rb_node, &dl_rq->root, leftmost);
1485*4882a593Smuzhiyun 
1486*4882a593Smuzhiyun 	inc_dl_tasks(dl_se, dl_rq);
1487*4882a593Smuzhiyun }
1488*4882a593Smuzhiyun 
__dequeue_dl_entity(struct sched_dl_entity * dl_se)1489*4882a593Smuzhiyun static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
1490*4882a593Smuzhiyun {
1491*4882a593Smuzhiyun 	struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
1492*4882a593Smuzhiyun 
1493*4882a593Smuzhiyun 	if (RB_EMPTY_NODE(&dl_se->rb_node))
1494*4882a593Smuzhiyun 		return;
1495*4882a593Smuzhiyun 
1496*4882a593Smuzhiyun 	rb_erase_cached(&dl_se->rb_node, &dl_rq->root);
1497*4882a593Smuzhiyun 	RB_CLEAR_NODE(&dl_se->rb_node);
1498*4882a593Smuzhiyun 
1499*4882a593Smuzhiyun 	dec_dl_tasks(dl_se, dl_rq);
1500*4882a593Smuzhiyun }
1501*4882a593Smuzhiyun 
1502*4882a593Smuzhiyun static void
enqueue_dl_entity(struct sched_dl_entity * dl_se,int flags)1503*4882a593Smuzhiyun enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)
1504*4882a593Smuzhiyun {
1505*4882a593Smuzhiyun 	BUG_ON(on_dl_rq(dl_se));
1506*4882a593Smuzhiyun 
1507*4882a593Smuzhiyun 	/*
1508*4882a593Smuzhiyun 	 * If this is a wakeup or a new instance, the scheduling
1509*4882a593Smuzhiyun 	 * parameters of the task might need updating. Otherwise,
1510*4882a593Smuzhiyun 	 * we want a replenishment of its runtime.
1511*4882a593Smuzhiyun 	 */
1512*4882a593Smuzhiyun 	if (flags & ENQUEUE_WAKEUP) {
1513*4882a593Smuzhiyun 		task_contending(dl_se, flags);
1514*4882a593Smuzhiyun 		update_dl_entity(dl_se);
1515*4882a593Smuzhiyun 	} else if (flags & ENQUEUE_REPLENISH) {
1516*4882a593Smuzhiyun 		replenish_dl_entity(dl_se);
1517*4882a593Smuzhiyun 	} else if ((flags & ENQUEUE_RESTORE) &&
1518*4882a593Smuzhiyun 		  dl_time_before(dl_se->deadline,
1519*4882a593Smuzhiyun 				 rq_clock(rq_of_dl_rq(dl_rq_of_se(dl_se))))) {
1520*4882a593Smuzhiyun 		setup_new_dl_entity(dl_se);
1521*4882a593Smuzhiyun 	}
1522*4882a593Smuzhiyun 
1523*4882a593Smuzhiyun 	__enqueue_dl_entity(dl_se);
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun 
dequeue_dl_entity(struct sched_dl_entity * dl_se)1526*4882a593Smuzhiyun static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
1527*4882a593Smuzhiyun {
1528*4882a593Smuzhiyun 	__dequeue_dl_entity(dl_se);
1529*4882a593Smuzhiyun }
1530*4882a593Smuzhiyun 
enqueue_task_dl(struct rq * rq,struct task_struct * p,int flags)1531*4882a593Smuzhiyun static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1532*4882a593Smuzhiyun {
1533*4882a593Smuzhiyun 	if (is_dl_boosted(&p->dl)) {
1534*4882a593Smuzhiyun 		/*
1535*4882a593Smuzhiyun 		 * Because of delays in the detection of the overrun of a
1536*4882a593Smuzhiyun 		 * thread's runtime, it might be the case that a thread
1537*4882a593Smuzhiyun 		 * goes to sleep in a rt mutex with negative runtime. As
1538*4882a593Smuzhiyun 		 * a consequence, the thread will be throttled.
1539*4882a593Smuzhiyun 		 *
1540*4882a593Smuzhiyun 		 * While waiting for the mutex, this thread can also be
1541*4882a593Smuzhiyun 		 * boosted via PI, resulting in a thread that is throttled
1542*4882a593Smuzhiyun 		 * and boosted at the same time.
1543*4882a593Smuzhiyun 		 *
1544*4882a593Smuzhiyun 		 * In this case, the boost overrides the throttle.
1545*4882a593Smuzhiyun 		 */
1546*4882a593Smuzhiyun 		if (p->dl.dl_throttled) {
1547*4882a593Smuzhiyun 			/*
1548*4882a593Smuzhiyun 			 * The replenish timer needs to be canceled. No
1549*4882a593Smuzhiyun 			 * problem if it fires concurrently: boosted threads
1550*4882a593Smuzhiyun 			 * are ignored in dl_task_timer().
1551*4882a593Smuzhiyun 			 */
1552*4882a593Smuzhiyun 			hrtimer_try_to_cancel(&p->dl.dl_timer);
1553*4882a593Smuzhiyun 			p->dl.dl_throttled = 0;
1554*4882a593Smuzhiyun 		}
1555*4882a593Smuzhiyun 	} else if (!dl_prio(p->normal_prio)) {
1556*4882a593Smuzhiyun 		/*
1557*4882a593Smuzhiyun 		 * Special case in which we have a !SCHED_DEADLINE task that is going
1558*4882a593Smuzhiyun 		 * to be deboosted, but exceeds its runtime while doing so. No point in
1559*4882a593Smuzhiyun 		 * replenishing it, as it's going to return back to its original
1560*4882a593Smuzhiyun 		 * scheduling class after this. If it has been throttled, we need to
1561*4882a593Smuzhiyun 		 * clear the flag, otherwise the task may wake up as throttled after
1562*4882a593Smuzhiyun 		 * being boosted again with no means to replenish the runtime and clear
1563*4882a593Smuzhiyun 		 * the throttle.
1564*4882a593Smuzhiyun 		 */
1565*4882a593Smuzhiyun 		p->dl.dl_throttled = 0;
1566*4882a593Smuzhiyun 		if (!(flags & ENQUEUE_REPLENISH))
1567*4882a593Smuzhiyun 			printk_deferred_once("sched: DL de-boosted task PID %d: REPLENISH flag missing\n",
1568*4882a593Smuzhiyun 					     task_pid_nr(p));
1569*4882a593Smuzhiyun 
1570*4882a593Smuzhiyun 		return;
1571*4882a593Smuzhiyun 	}
1572*4882a593Smuzhiyun 
1573*4882a593Smuzhiyun 	/*
1574*4882a593Smuzhiyun 	 * Check if a constrained deadline task was activated
1575*4882a593Smuzhiyun 	 * after the deadline but before the next period.
1576*4882a593Smuzhiyun 	 * If that is the case, the task will be throttled and
1577*4882a593Smuzhiyun 	 * the replenishment timer will be set to the next period.
1578*4882a593Smuzhiyun 	 */
1579*4882a593Smuzhiyun 	if (!p->dl.dl_throttled && !dl_is_implicit(&p->dl))
1580*4882a593Smuzhiyun 		dl_check_constrained_dl(&p->dl);
1581*4882a593Smuzhiyun 
1582*4882a593Smuzhiyun 	if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE) {
1583*4882a593Smuzhiyun 		add_rq_bw(&p->dl, &rq->dl);
1584*4882a593Smuzhiyun 		add_running_bw(&p->dl, &rq->dl);
1585*4882a593Smuzhiyun 	}
1586*4882a593Smuzhiyun 
1587*4882a593Smuzhiyun 	/*
1588*4882a593Smuzhiyun 	 * If p is throttled, we do not enqueue it. In fact, if it exhausted
1589*4882a593Smuzhiyun 	 * its budget it needs a replenishment and, since it now is on
1590*4882a593Smuzhiyun 	 * its rq, the bandwidth timer callback (which clearly has not
1591*4882a593Smuzhiyun 	 * run yet) will take care of this.
1592*4882a593Smuzhiyun 	 * However, the active utilization does not depend on the fact
1593*4882a593Smuzhiyun 	 * that the task is on the runqueue or not (but depends on the
1594*4882a593Smuzhiyun 	 * task's state - in GRUB parlance, "inactive" vs "active contending").
1595*4882a593Smuzhiyun 	 * In other words, even if a task is throttled its utilization must
1596*4882a593Smuzhiyun 	 * be counted in the active utilization; hence, we need to call
1597*4882a593Smuzhiyun 	 * add_running_bw().
1598*4882a593Smuzhiyun 	 */
1599*4882a593Smuzhiyun 	if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
1600*4882a593Smuzhiyun 		if (flags & ENQUEUE_WAKEUP)
1601*4882a593Smuzhiyun 			task_contending(&p->dl, flags);
1602*4882a593Smuzhiyun 
1603*4882a593Smuzhiyun 		return;
1604*4882a593Smuzhiyun 	}
1605*4882a593Smuzhiyun 
1606*4882a593Smuzhiyun 	enqueue_dl_entity(&p->dl, flags);
1607*4882a593Smuzhiyun 
1608*4882a593Smuzhiyun 	if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
1609*4882a593Smuzhiyun 		enqueue_pushable_dl_task(rq, p);
1610*4882a593Smuzhiyun }
1611*4882a593Smuzhiyun 
__dequeue_task_dl(struct rq * rq,struct task_struct * p,int flags)1612*4882a593Smuzhiyun static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1613*4882a593Smuzhiyun {
1614*4882a593Smuzhiyun 	dequeue_dl_entity(&p->dl);
1615*4882a593Smuzhiyun 	dequeue_pushable_dl_task(rq, p);
1616*4882a593Smuzhiyun }
1617*4882a593Smuzhiyun 
dequeue_task_dl(struct rq * rq,struct task_struct * p,int flags)1618*4882a593Smuzhiyun static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
1619*4882a593Smuzhiyun {
1620*4882a593Smuzhiyun 	update_curr_dl(rq);
1621*4882a593Smuzhiyun 	__dequeue_task_dl(rq, p, flags);
1622*4882a593Smuzhiyun 
1623*4882a593Smuzhiyun 	if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE) {
1624*4882a593Smuzhiyun 		sub_running_bw(&p->dl, &rq->dl);
1625*4882a593Smuzhiyun 		sub_rq_bw(&p->dl, &rq->dl);
1626*4882a593Smuzhiyun 	}
1627*4882a593Smuzhiyun 
1628*4882a593Smuzhiyun 	/*
1629*4882a593Smuzhiyun 	 * This check allows to start the inactive timer (or to immediately
1630*4882a593Smuzhiyun 	 * decrease the active utilization, if needed) in two cases:
1631*4882a593Smuzhiyun 	 * when the task blocks and when it is terminating
1632*4882a593Smuzhiyun 	 * (p->state == TASK_DEAD). We can handle the two cases in the same
1633*4882a593Smuzhiyun 	 * way, because from GRUB's point of view the same thing is happening
1634*4882a593Smuzhiyun 	 * (the task moves from "active contending" to "active non contending"
1635*4882a593Smuzhiyun 	 * or "inactive")
1636*4882a593Smuzhiyun 	 */
1637*4882a593Smuzhiyun 	if (flags & DEQUEUE_SLEEP)
1638*4882a593Smuzhiyun 		task_non_contending(p);
1639*4882a593Smuzhiyun }
1640*4882a593Smuzhiyun 
1641*4882a593Smuzhiyun /*
1642*4882a593Smuzhiyun  * Yield task semantic for -deadline tasks is:
1643*4882a593Smuzhiyun  *
1644*4882a593Smuzhiyun  *   get off from the CPU until our next instance, with
1645*4882a593Smuzhiyun  *   a new runtime. This is of little use now, since we
1646*4882a593Smuzhiyun  *   don't have a bandwidth reclaiming mechanism. Anyway,
1647*4882a593Smuzhiyun  *   bandwidth reclaiming is planned for the future, and
1648*4882a593Smuzhiyun  *   yield_task_dl will indicate that some spare budget
1649*4882a593Smuzhiyun  *   is available for other task instances to use it.
1650*4882a593Smuzhiyun  */
yield_task_dl(struct rq * rq)1651*4882a593Smuzhiyun static void yield_task_dl(struct rq *rq)
1652*4882a593Smuzhiyun {
1653*4882a593Smuzhiyun 	/*
1654*4882a593Smuzhiyun 	 * We make the task go to sleep until its current deadline by
1655*4882a593Smuzhiyun 	 * forcing its runtime to zero. This way, update_curr_dl() stops
1656*4882a593Smuzhiyun 	 * it and the bandwidth timer will wake it up and will give it
1657*4882a593Smuzhiyun 	 * new scheduling parameters (thanks to dl_yielded=1).
1658*4882a593Smuzhiyun 	 */
1659*4882a593Smuzhiyun 	rq->curr->dl.dl_yielded = 1;
1660*4882a593Smuzhiyun 
1661*4882a593Smuzhiyun 	update_rq_clock(rq);
1662*4882a593Smuzhiyun 	update_curr_dl(rq);
1663*4882a593Smuzhiyun 	/*
1664*4882a593Smuzhiyun 	 * Tell update_rq_clock() that we've just updated,
1665*4882a593Smuzhiyun 	 * so we don't do microscopic update in schedule()
1666*4882a593Smuzhiyun 	 * and double the fastpath cost.
1667*4882a593Smuzhiyun 	 */
1668*4882a593Smuzhiyun 	rq_clock_skip_update(rq);
1669*4882a593Smuzhiyun }
1670*4882a593Smuzhiyun 
1671*4882a593Smuzhiyun #ifdef CONFIG_SMP
1672*4882a593Smuzhiyun 
1673*4882a593Smuzhiyun static int find_later_rq(struct task_struct *task);
1674*4882a593Smuzhiyun 
1675*4882a593Smuzhiyun static int
select_task_rq_dl(struct task_struct * p,int cpu,int sd_flag,int flags)1676*4882a593Smuzhiyun select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
1677*4882a593Smuzhiyun {
1678*4882a593Smuzhiyun 	struct task_struct *curr;
1679*4882a593Smuzhiyun 	bool select_rq;
1680*4882a593Smuzhiyun 	struct rq *rq;
1681*4882a593Smuzhiyun 
1682*4882a593Smuzhiyun 	if (sd_flag != SD_BALANCE_WAKE)
1683*4882a593Smuzhiyun 		goto out;
1684*4882a593Smuzhiyun 
1685*4882a593Smuzhiyun 	rq = cpu_rq(cpu);
1686*4882a593Smuzhiyun 
1687*4882a593Smuzhiyun 	rcu_read_lock();
1688*4882a593Smuzhiyun 	curr = READ_ONCE(rq->curr); /* unlocked access */
1689*4882a593Smuzhiyun 
1690*4882a593Smuzhiyun 	/*
1691*4882a593Smuzhiyun 	 * If we are dealing with a -deadline task, we must
1692*4882a593Smuzhiyun 	 * decide where to wake it up.
1693*4882a593Smuzhiyun 	 * If it has a later deadline and the current task
1694*4882a593Smuzhiyun 	 * on this rq can't move (provided the waking task
1695*4882a593Smuzhiyun 	 * can!) we prefer to send it somewhere else. On the
1696*4882a593Smuzhiyun 	 * other hand, if it has a shorter deadline, we
1697*4882a593Smuzhiyun 	 * try to make it stay here, it might be important.
1698*4882a593Smuzhiyun 	 */
1699*4882a593Smuzhiyun 	select_rq = unlikely(dl_task(curr)) &&
1700*4882a593Smuzhiyun 		    (curr->nr_cpus_allowed < 2 ||
1701*4882a593Smuzhiyun 		     !dl_entity_preempt(&p->dl, &curr->dl)) &&
1702*4882a593Smuzhiyun 		    p->nr_cpus_allowed > 1;
1703*4882a593Smuzhiyun 
1704*4882a593Smuzhiyun 	/*
1705*4882a593Smuzhiyun 	 * Take the capacity of the CPU into account to
1706*4882a593Smuzhiyun 	 * ensure it fits the requirement of the task.
1707*4882a593Smuzhiyun 	 */
1708*4882a593Smuzhiyun 	if (static_branch_unlikely(&sched_asym_cpucapacity))
1709*4882a593Smuzhiyun 		select_rq |= !dl_task_fits_capacity(p, cpu);
1710*4882a593Smuzhiyun 
1711*4882a593Smuzhiyun 	if (select_rq) {
1712*4882a593Smuzhiyun 		int target = find_later_rq(p);
1713*4882a593Smuzhiyun 
1714*4882a593Smuzhiyun 		if (target != -1 &&
1715*4882a593Smuzhiyun 				(dl_time_before(p->dl.deadline,
1716*4882a593Smuzhiyun 					cpu_rq(target)->dl.earliest_dl.curr) ||
1717*4882a593Smuzhiyun 				(cpu_rq(target)->dl.dl_nr_running == 0)))
1718*4882a593Smuzhiyun 			cpu = target;
1719*4882a593Smuzhiyun 	}
1720*4882a593Smuzhiyun 	rcu_read_unlock();
1721*4882a593Smuzhiyun 
1722*4882a593Smuzhiyun out:
1723*4882a593Smuzhiyun 	return cpu;
1724*4882a593Smuzhiyun }
1725*4882a593Smuzhiyun 
migrate_task_rq_dl(struct task_struct * p,int new_cpu __maybe_unused)1726*4882a593Smuzhiyun static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
1727*4882a593Smuzhiyun {
1728*4882a593Smuzhiyun 	struct rq *rq;
1729*4882a593Smuzhiyun 
1730*4882a593Smuzhiyun 	if (p->state != TASK_WAKING)
1731*4882a593Smuzhiyun 		return;
1732*4882a593Smuzhiyun 
1733*4882a593Smuzhiyun 	rq = task_rq(p);
1734*4882a593Smuzhiyun 	/*
1735*4882a593Smuzhiyun 	 * Since p->state == TASK_WAKING, set_task_cpu() has been called
1736*4882a593Smuzhiyun 	 * from try_to_wake_up(). Hence, p->pi_lock is locked, but
1737*4882a593Smuzhiyun 	 * rq->lock is not... So, lock it
1738*4882a593Smuzhiyun 	 */
1739*4882a593Smuzhiyun 	raw_spin_lock(&rq->lock);
1740*4882a593Smuzhiyun 	if (p->dl.dl_non_contending) {
1741*4882a593Smuzhiyun 		update_rq_clock(rq);
1742*4882a593Smuzhiyun 		sub_running_bw(&p->dl, &rq->dl);
1743*4882a593Smuzhiyun 		p->dl.dl_non_contending = 0;
1744*4882a593Smuzhiyun 		/*
1745*4882a593Smuzhiyun 		 * If the timer handler is currently running and the
1746*4882a593Smuzhiyun 		 * timer cannot be cancelled, inactive_task_timer()
1747*4882a593Smuzhiyun 		 * will see that dl_not_contending is not set, and
1748*4882a593Smuzhiyun 		 * will not touch the rq's active utilization,
1749*4882a593Smuzhiyun 		 * so we are still safe.
1750*4882a593Smuzhiyun 		 */
1751*4882a593Smuzhiyun 		if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
1752*4882a593Smuzhiyun 			put_task_struct(p);
1753*4882a593Smuzhiyun 	}
1754*4882a593Smuzhiyun 	sub_rq_bw(&p->dl, &rq->dl);
1755*4882a593Smuzhiyun 	raw_spin_unlock(&rq->lock);
1756*4882a593Smuzhiyun }
1757*4882a593Smuzhiyun 
check_preempt_equal_dl(struct rq * rq,struct task_struct * p)1758*4882a593Smuzhiyun static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
1759*4882a593Smuzhiyun {
1760*4882a593Smuzhiyun 	/*
1761*4882a593Smuzhiyun 	 * Current can't be migrated, useless to reschedule,
1762*4882a593Smuzhiyun 	 * let's hope p can move out.
1763*4882a593Smuzhiyun 	 */
1764*4882a593Smuzhiyun 	if (rq->curr->nr_cpus_allowed == 1 ||
1765*4882a593Smuzhiyun 	    !cpudl_find(&rq->rd->cpudl, rq->curr, NULL))
1766*4882a593Smuzhiyun 		return;
1767*4882a593Smuzhiyun 
1768*4882a593Smuzhiyun 	/*
1769*4882a593Smuzhiyun 	 * p is migratable, so let's not schedule it and
1770*4882a593Smuzhiyun 	 * see if it is pushed or pulled somewhere else.
1771*4882a593Smuzhiyun 	 */
1772*4882a593Smuzhiyun 	if (p->nr_cpus_allowed != 1 &&
1773*4882a593Smuzhiyun 	    cpudl_find(&rq->rd->cpudl, p, NULL))
1774*4882a593Smuzhiyun 		return;
1775*4882a593Smuzhiyun 
1776*4882a593Smuzhiyun 	resched_curr(rq);
1777*4882a593Smuzhiyun }
1778*4882a593Smuzhiyun 
balance_dl(struct rq * rq,struct task_struct * p,struct rq_flags * rf)1779*4882a593Smuzhiyun static int balance_dl(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
1780*4882a593Smuzhiyun {
1781*4882a593Smuzhiyun 	if (!on_dl_rq(&p->dl) && need_pull_dl_task(rq, p)) {
1782*4882a593Smuzhiyun 		/*
1783*4882a593Smuzhiyun 		 * This is OK, because current is on_cpu, which avoids it being
1784*4882a593Smuzhiyun 		 * picked for load-balance and preemption/IRQs are still
1785*4882a593Smuzhiyun 		 * disabled avoiding further scheduler activity on it and we've
1786*4882a593Smuzhiyun 		 * not yet started the picking loop.
1787*4882a593Smuzhiyun 		 */
1788*4882a593Smuzhiyun 		rq_unpin_lock(rq, rf);
1789*4882a593Smuzhiyun 		pull_dl_task(rq);
1790*4882a593Smuzhiyun 		rq_repin_lock(rq, rf);
1791*4882a593Smuzhiyun 	}
1792*4882a593Smuzhiyun 
1793*4882a593Smuzhiyun 	return sched_stop_runnable(rq) || sched_dl_runnable(rq);
1794*4882a593Smuzhiyun }
1795*4882a593Smuzhiyun #endif /* CONFIG_SMP */
1796*4882a593Smuzhiyun 
1797*4882a593Smuzhiyun /*
1798*4882a593Smuzhiyun  * Only called when both the current and waking task are -deadline
1799*4882a593Smuzhiyun  * tasks.
1800*4882a593Smuzhiyun  */
check_preempt_curr_dl(struct rq * rq,struct task_struct * p,int flags)1801*4882a593Smuzhiyun static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
1802*4882a593Smuzhiyun 				  int flags)
1803*4882a593Smuzhiyun {
1804*4882a593Smuzhiyun 	if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
1805*4882a593Smuzhiyun 		resched_curr(rq);
1806*4882a593Smuzhiyun 		return;
1807*4882a593Smuzhiyun 	}
1808*4882a593Smuzhiyun 
1809*4882a593Smuzhiyun #ifdef CONFIG_SMP
1810*4882a593Smuzhiyun 	/*
1811*4882a593Smuzhiyun 	 * In the unlikely case current and p have the same deadline
1812*4882a593Smuzhiyun 	 * let us try to decide what's the best thing to do...
1813*4882a593Smuzhiyun 	 */
1814*4882a593Smuzhiyun 	if ((p->dl.deadline == rq->curr->dl.deadline) &&
1815*4882a593Smuzhiyun 	    !test_tsk_need_resched(rq->curr))
1816*4882a593Smuzhiyun 		check_preempt_equal_dl(rq, p);
1817*4882a593Smuzhiyun #endif /* CONFIG_SMP */
1818*4882a593Smuzhiyun }
1819*4882a593Smuzhiyun 
1820*4882a593Smuzhiyun #ifdef CONFIG_SCHED_HRTICK
start_hrtick_dl(struct rq * rq,struct task_struct * p)1821*4882a593Smuzhiyun static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1822*4882a593Smuzhiyun {
1823*4882a593Smuzhiyun 	hrtick_start(rq, p->dl.runtime);
1824*4882a593Smuzhiyun }
1825*4882a593Smuzhiyun #else /* !CONFIG_SCHED_HRTICK */
start_hrtick_dl(struct rq * rq,struct task_struct * p)1826*4882a593Smuzhiyun static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
1827*4882a593Smuzhiyun {
1828*4882a593Smuzhiyun }
1829*4882a593Smuzhiyun #endif
1830*4882a593Smuzhiyun 
set_next_task_dl(struct rq * rq,struct task_struct * p,bool first)1831*4882a593Smuzhiyun static void set_next_task_dl(struct rq *rq, struct task_struct *p, bool first)
1832*4882a593Smuzhiyun {
1833*4882a593Smuzhiyun 	p->se.exec_start = rq_clock_task(rq);
1834*4882a593Smuzhiyun 
1835*4882a593Smuzhiyun 	/* You can't push away the running task */
1836*4882a593Smuzhiyun 	dequeue_pushable_dl_task(rq, p);
1837*4882a593Smuzhiyun 
1838*4882a593Smuzhiyun 	if (!first)
1839*4882a593Smuzhiyun 		return;
1840*4882a593Smuzhiyun 
1841*4882a593Smuzhiyun 	if (hrtick_enabled(rq))
1842*4882a593Smuzhiyun 		start_hrtick_dl(rq, p);
1843*4882a593Smuzhiyun 
1844*4882a593Smuzhiyun 	if (rq->curr->sched_class != &dl_sched_class)
1845*4882a593Smuzhiyun 		update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
1846*4882a593Smuzhiyun 
1847*4882a593Smuzhiyun 	deadline_queue_push_tasks(rq);
1848*4882a593Smuzhiyun }
1849*4882a593Smuzhiyun 
pick_next_dl_entity(struct rq * rq,struct dl_rq * dl_rq)1850*4882a593Smuzhiyun static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
1851*4882a593Smuzhiyun 						   struct dl_rq *dl_rq)
1852*4882a593Smuzhiyun {
1853*4882a593Smuzhiyun 	struct rb_node *left = rb_first_cached(&dl_rq->root);
1854*4882a593Smuzhiyun 
1855*4882a593Smuzhiyun 	if (!left)
1856*4882a593Smuzhiyun 		return NULL;
1857*4882a593Smuzhiyun 
1858*4882a593Smuzhiyun 	return rb_entry(left, struct sched_dl_entity, rb_node);
1859*4882a593Smuzhiyun }
1860*4882a593Smuzhiyun 
pick_next_task_dl(struct rq * rq)1861*4882a593Smuzhiyun static struct task_struct *pick_next_task_dl(struct rq *rq)
1862*4882a593Smuzhiyun {
1863*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se;
1864*4882a593Smuzhiyun 	struct dl_rq *dl_rq = &rq->dl;
1865*4882a593Smuzhiyun 	struct task_struct *p;
1866*4882a593Smuzhiyun 
1867*4882a593Smuzhiyun 	if (!sched_dl_runnable(rq))
1868*4882a593Smuzhiyun 		return NULL;
1869*4882a593Smuzhiyun 
1870*4882a593Smuzhiyun 	dl_se = pick_next_dl_entity(rq, dl_rq);
1871*4882a593Smuzhiyun 	BUG_ON(!dl_se);
1872*4882a593Smuzhiyun 	p = dl_task_of(dl_se);
1873*4882a593Smuzhiyun 	set_next_task_dl(rq, p, true);
1874*4882a593Smuzhiyun 	return p;
1875*4882a593Smuzhiyun }
1876*4882a593Smuzhiyun 
put_prev_task_dl(struct rq * rq,struct task_struct * p)1877*4882a593Smuzhiyun static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
1878*4882a593Smuzhiyun {
1879*4882a593Smuzhiyun 	update_curr_dl(rq);
1880*4882a593Smuzhiyun 
1881*4882a593Smuzhiyun 	update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
1882*4882a593Smuzhiyun 	if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
1883*4882a593Smuzhiyun 		enqueue_pushable_dl_task(rq, p);
1884*4882a593Smuzhiyun }
1885*4882a593Smuzhiyun 
1886*4882a593Smuzhiyun /*
1887*4882a593Smuzhiyun  * scheduler tick hitting a task of our scheduling class.
1888*4882a593Smuzhiyun  *
1889*4882a593Smuzhiyun  * NOTE: This function can be called remotely by the tick offload that
1890*4882a593Smuzhiyun  * goes along full dynticks. Therefore no local assumption can be made
1891*4882a593Smuzhiyun  * and everything must be accessed through the @rq and @curr passed in
1892*4882a593Smuzhiyun  * parameters.
1893*4882a593Smuzhiyun  */
task_tick_dl(struct rq * rq,struct task_struct * p,int queued)1894*4882a593Smuzhiyun static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
1895*4882a593Smuzhiyun {
1896*4882a593Smuzhiyun 	update_curr_dl(rq);
1897*4882a593Smuzhiyun 
1898*4882a593Smuzhiyun 	update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
1899*4882a593Smuzhiyun 	/*
1900*4882a593Smuzhiyun 	 * Even when we have runtime, update_curr_dl() might have resulted in us
1901*4882a593Smuzhiyun 	 * not being the leftmost task anymore. In that case NEED_RESCHED will
1902*4882a593Smuzhiyun 	 * be set and schedule() will start a new hrtick for the next task.
1903*4882a593Smuzhiyun 	 */
1904*4882a593Smuzhiyun 	if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
1905*4882a593Smuzhiyun 	    is_leftmost(p, &rq->dl))
1906*4882a593Smuzhiyun 		start_hrtick_dl(rq, p);
1907*4882a593Smuzhiyun }
1908*4882a593Smuzhiyun 
task_fork_dl(struct task_struct * p)1909*4882a593Smuzhiyun static void task_fork_dl(struct task_struct *p)
1910*4882a593Smuzhiyun {
1911*4882a593Smuzhiyun 	/*
1912*4882a593Smuzhiyun 	 * SCHED_DEADLINE tasks cannot fork and this is achieved through
1913*4882a593Smuzhiyun 	 * sched_fork()
1914*4882a593Smuzhiyun 	 */
1915*4882a593Smuzhiyun }
1916*4882a593Smuzhiyun 
1917*4882a593Smuzhiyun #ifdef CONFIG_SMP
1918*4882a593Smuzhiyun 
1919*4882a593Smuzhiyun /* Only try algorithms three times */
1920*4882a593Smuzhiyun #define DL_MAX_TRIES 3
1921*4882a593Smuzhiyun 
pick_dl_task(struct rq * rq,struct task_struct * p,int cpu)1922*4882a593Smuzhiyun static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
1923*4882a593Smuzhiyun {
1924*4882a593Smuzhiyun 	if (!task_running(rq, p) &&
1925*4882a593Smuzhiyun 	    cpumask_test_cpu(cpu, p->cpus_ptr))
1926*4882a593Smuzhiyun 		return 1;
1927*4882a593Smuzhiyun 	return 0;
1928*4882a593Smuzhiyun }
1929*4882a593Smuzhiyun 
1930*4882a593Smuzhiyun /*
1931*4882a593Smuzhiyun  * Return the earliest pushable rq's task, which is suitable to be executed
1932*4882a593Smuzhiyun  * on the CPU, NULL otherwise:
1933*4882a593Smuzhiyun  */
pick_earliest_pushable_dl_task(struct rq * rq,int cpu)1934*4882a593Smuzhiyun static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
1935*4882a593Smuzhiyun {
1936*4882a593Smuzhiyun 	struct rb_node *next_node = rq->dl.pushable_dl_tasks_root.rb_leftmost;
1937*4882a593Smuzhiyun 	struct task_struct *p = NULL;
1938*4882a593Smuzhiyun 
1939*4882a593Smuzhiyun 	if (!has_pushable_dl_tasks(rq))
1940*4882a593Smuzhiyun 		return NULL;
1941*4882a593Smuzhiyun 
1942*4882a593Smuzhiyun next_node:
1943*4882a593Smuzhiyun 	if (next_node) {
1944*4882a593Smuzhiyun 		p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
1945*4882a593Smuzhiyun 
1946*4882a593Smuzhiyun 		if (pick_dl_task(rq, p, cpu))
1947*4882a593Smuzhiyun 			return p;
1948*4882a593Smuzhiyun 
1949*4882a593Smuzhiyun 		next_node = rb_next(next_node);
1950*4882a593Smuzhiyun 		goto next_node;
1951*4882a593Smuzhiyun 	}
1952*4882a593Smuzhiyun 
1953*4882a593Smuzhiyun 	return NULL;
1954*4882a593Smuzhiyun }
1955*4882a593Smuzhiyun 
1956*4882a593Smuzhiyun static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
1957*4882a593Smuzhiyun 
find_later_rq(struct task_struct * task)1958*4882a593Smuzhiyun static int find_later_rq(struct task_struct *task)
1959*4882a593Smuzhiyun {
1960*4882a593Smuzhiyun 	struct sched_domain *sd;
1961*4882a593Smuzhiyun 	struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
1962*4882a593Smuzhiyun 	int this_cpu = smp_processor_id();
1963*4882a593Smuzhiyun 	int cpu = task_cpu(task);
1964*4882a593Smuzhiyun 
1965*4882a593Smuzhiyun 	/* Make sure the mask is initialized first */
1966*4882a593Smuzhiyun 	if (unlikely(!later_mask))
1967*4882a593Smuzhiyun 		return -1;
1968*4882a593Smuzhiyun 
1969*4882a593Smuzhiyun 	if (task->nr_cpus_allowed == 1)
1970*4882a593Smuzhiyun 		return -1;
1971*4882a593Smuzhiyun 
1972*4882a593Smuzhiyun 	/*
1973*4882a593Smuzhiyun 	 * We have to consider system topology and task affinity
1974*4882a593Smuzhiyun 	 * first, then we can look for a suitable CPU.
1975*4882a593Smuzhiyun 	 */
1976*4882a593Smuzhiyun 	if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask))
1977*4882a593Smuzhiyun 		return -1;
1978*4882a593Smuzhiyun 
1979*4882a593Smuzhiyun 	/*
1980*4882a593Smuzhiyun 	 * If we are here, some targets have been found, including
1981*4882a593Smuzhiyun 	 * the most suitable which is, among the runqueues where the
1982*4882a593Smuzhiyun 	 * current tasks have later deadlines than the task's one, the
1983*4882a593Smuzhiyun 	 * rq with the latest possible one.
1984*4882a593Smuzhiyun 	 *
1985*4882a593Smuzhiyun 	 * Now we check how well this matches with task's
1986*4882a593Smuzhiyun 	 * affinity and system topology.
1987*4882a593Smuzhiyun 	 *
1988*4882a593Smuzhiyun 	 * The last CPU where the task run is our first
1989*4882a593Smuzhiyun 	 * guess, since it is most likely cache-hot there.
1990*4882a593Smuzhiyun 	 */
1991*4882a593Smuzhiyun 	if (cpumask_test_cpu(cpu, later_mask))
1992*4882a593Smuzhiyun 		return cpu;
1993*4882a593Smuzhiyun 	/*
1994*4882a593Smuzhiyun 	 * Check if this_cpu is to be skipped (i.e., it is
1995*4882a593Smuzhiyun 	 * not in the mask) or not.
1996*4882a593Smuzhiyun 	 */
1997*4882a593Smuzhiyun 	if (!cpumask_test_cpu(this_cpu, later_mask))
1998*4882a593Smuzhiyun 		this_cpu = -1;
1999*4882a593Smuzhiyun 
2000*4882a593Smuzhiyun 	rcu_read_lock();
2001*4882a593Smuzhiyun 	for_each_domain(cpu, sd) {
2002*4882a593Smuzhiyun 		if (sd->flags & SD_WAKE_AFFINE) {
2003*4882a593Smuzhiyun 			int best_cpu;
2004*4882a593Smuzhiyun 
2005*4882a593Smuzhiyun 			/*
2006*4882a593Smuzhiyun 			 * If possible, preempting this_cpu is
2007*4882a593Smuzhiyun 			 * cheaper than migrating.
2008*4882a593Smuzhiyun 			 */
2009*4882a593Smuzhiyun 			if (this_cpu != -1 &&
2010*4882a593Smuzhiyun 			    cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
2011*4882a593Smuzhiyun 				rcu_read_unlock();
2012*4882a593Smuzhiyun 				return this_cpu;
2013*4882a593Smuzhiyun 			}
2014*4882a593Smuzhiyun 
2015*4882a593Smuzhiyun 			best_cpu = cpumask_first_and(later_mask,
2016*4882a593Smuzhiyun 							sched_domain_span(sd));
2017*4882a593Smuzhiyun 			/*
2018*4882a593Smuzhiyun 			 * Last chance: if a CPU being in both later_mask
2019*4882a593Smuzhiyun 			 * and current sd span is valid, that becomes our
2020*4882a593Smuzhiyun 			 * choice. Of course, the latest possible CPU is
2021*4882a593Smuzhiyun 			 * already under consideration through later_mask.
2022*4882a593Smuzhiyun 			 */
2023*4882a593Smuzhiyun 			if (best_cpu < nr_cpu_ids) {
2024*4882a593Smuzhiyun 				rcu_read_unlock();
2025*4882a593Smuzhiyun 				return best_cpu;
2026*4882a593Smuzhiyun 			}
2027*4882a593Smuzhiyun 		}
2028*4882a593Smuzhiyun 	}
2029*4882a593Smuzhiyun 	rcu_read_unlock();
2030*4882a593Smuzhiyun 
2031*4882a593Smuzhiyun 	/*
2032*4882a593Smuzhiyun 	 * At this point, all our guesses failed, we just return
2033*4882a593Smuzhiyun 	 * 'something', and let the caller sort the things out.
2034*4882a593Smuzhiyun 	 */
2035*4882a593Smuzhiyun 	if (this_cpu != -1)
2036*4882a593Smuzhiyun 		return this_cpu;
2037*4882a593Smuzhiyun 
2038*4882a593Smuzhiyun 	cpu = cpumask_any(later_mask);
2039*4882a593Smuzhiyun 	if (cpu < nr_cpu_ids)
2040*4882a593Smuzhiyun 		return cpu;
2041*4882a593Smuzhiyun 
2042*4882a593Smuzhiyun 	return -1;
2043*4882a593Smuzhiyun }
2044*4882a593Smuzhiyun 
2045*4882a593Smuzhiyun /* Locks the rq it finds */
find_lock_later_rq(struct task_struct * task,struct rq * rq)2046*4882a593Smuzhiyun static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
2047*4882a593Smuzhiyun {
2048*4882a593Smuzhiyun 	struct rq *later_rq = NULL;
2049*4882a593Smuzhiyun 	int tries;
2050*4882a593Smuzhiyun 	int cpu;
2051*4882a593Smuzhiyun 
2052*4882a593Smuzhiyun 	for (tries = 0; tries < DL_MAX_TRIES; tries++) {
2053*4882a593Smuzhiyun 		cpu = find_later_rq(task);
2054*4882a593Smuzhiyun 
2055*4882a593Smuzhiyun 		if ((cpu == -1) || (cpu == rq->cpu))
2056*4882a593Smuzhiyun 			break;
2057*4882a593Smuzhiyun 
2058*4882a593Smuzhiyun 		later_rq = cpu_rq(cpu);
2059*4882a593Smuzhiyun 
2060*4882a593Smuzhiyun 		if (later_rq->dl.dl_nr_running &&
2061*4882a593Smuzhiyun 		    !dl_time_before(task->dl.deadline,
2062*4882a593Smuzhiyun 					later_rq->dl.earliest_dl.curr)) {
2063*4882a593Smuzhiyun 			/*
2064*4882a593Smuzhiyun 			 * Target rq has tasks of equal or earlier deadline,
2065*4882a593Smuzhiyun 			 * retrying does not release any lock and is unlikely
2066*4882a593Smuzhiyun 			 * to yield a different result.
2067*4882a593Smuzhiyun 			 */
2068*4882a593Smuzhiyun 			later_rq = NULL;
2069*4882a593Smuzhiyun 			break;
2070*4882a593Smuzhiyun 		}
2071*4882a593Smuzhiyun 
2072*4882a593Smuzhiyun 		/* Retry if something changed. */
2073*4882a593Smuzhiyun 		if (double_lock_balance(rq, later_rq)) {
2074*4882a593Smuzhiyun 			if (unlikely(task_rq(task) != rq ||
2075*4882a593Smuzhiyun 				     !cpumask_test_cpu(later_rq->cpu, task->cpus_ptr) ||
2076*4882a593Smuzhiyun 				     task_running(rq, task) ||
2077*4882a593Smuzhiyun 				     !dl_task(task) ||
2078*4882a593Smuzhiyun 				     !task_on_rq_queued(task))) {
2079*4882a593Smuzhiyun 				double_unlock_balance(rq, later_rq);
2080*4882a593Smuzhiyun 				later_rq = NULL;
2081*4882a593Smuzhiyun 				break;
2082*4882a593Smuzhiyun 			}
2083*4882a593Smuzhiyun 		}
2084*4882a593Smuzhiyun 
2085*4882a593Smuzhiyun 		/*
2086*4882a593Smuzhiyun 		 * If the rq we found has no -deadline task, or
2087*4882a593Smuzhiyun 		 * its earliest one has a later deadline than our
2088*4882a593Smuzhiyun 		 * task, the rq is a good one.
2089*4882a593Smuzhiyun 		 */
2090*4882a593Smuzhiyun 		if (!later_rq->dl.dl_nr_running ||
2091*4882a593Smuzhiyun 		    dl_time_before(task->dl.deadline,
2092*4882a593Smuzhiyun 				   later_rq->dl.earliest_dl.curr))
2093*4882a593Smuzhiyun 			break;
2094*4882a593Smuzhiyun 
2095*4882a593Smuzhiyun 		/* Otherwise we try again. */
2096*4882a593Smuzhiyun 		double_unlock_balance(rq, later_rq);
2097*4882a593Smuzhiyun 		later_rq = NULL;
2098*4882a593Smuzhiyun 	}
2099*4882a593Smuzhiyun 
2100*4882a593Smuzhiyun 	return later_rq;
2101*4882a593Smuzhiyun }
2102*4882a593Smuzhiyun 
pick_next_pushable_dl_task(struct rq * rq)2103*4882a593Smuzhiyun static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
2104*4882a593Smuzhiyun {
2105*4882a593Smuzhiyun 	struct task_struct *p;
2106*4882a593Smuzhiyun 
2107*4882a593Smuzhiyun 	if (!has_pushable_dl_tasks(rq))
2108*4882a593Smuzhiyun 		return NULL;
2109*4882a593Smuzhiyun 
2110*4882a593Smuzhiyun 	p = rb_entry(rq->dl.pushable_dl_tasks_root.rb_leftmost,
2111*4882a593Smuzhiyun 		     struct task_struct, pushable_dl_tasks);
2112*4882a593Smuzhiyun 
2113*4882a593Smuzhiyun 	BUG_ON(rq->cpu != task_cpu(p));
2114*4882a593Smuzhiyun 	BUG_ON(task_current(rq, p));
2115*4882a593Smuzhiyun 	BUG_ON(p->nr_cpus_allowed <= 1);
2116*4882a593Smuzhiyun 
2117*4882a593Smuzhiyun 	BUG_ON(!task_on_rq_queued(p));
2118*4882a593Smuzhiyun 	BUG_ON(!dl_task(p));
2119*4882a593Smuzhiyun 
2120*4882a593Smuzhiyun 	return p;
2121*4882a593Smuzhiyun }
2122*4882a593Smuzhiyun 
2123*4882a593Smuzhiyun /*
2124*4882a593Smuzhiyun  * See if the non running -deadline tasks on this rq
2125*4882a593Smuzhiyun  * can be sent to some other CPU where they can preempt
2126*4882a593Smuzhiyun  * and start executing.
2127*4882a593Smuzhiyun  */
push_dl_task(struct rq * rq)2128*4882a593Smuzhiyun static int push_dl_task(struct rq *rq)
2129*4882a593Smuzhiyun {
2130*4882a593Smuzhiyun 	struct task_struct *next_task;
2131*4882a593Smuzhiyun 	struct rq *later_rq;
2132*4882a593Smuzhiyun 	int ret = 0;
2133*4882a593Smuzhiyun 
2134*4882a593Smuzhiyun 	if (!rq->dl.overloaded)
2135*4882a593Smuzhiyun 		return 0;
2136*4882a593Smuzhiyun 
2137*4882a593Smuzhiyun 	next_task = pick_next_pushable_dl_task(rq);
2138*4882a593Smuzhiyun 	if (!next_task)
2139*4882a593Smuzhiyun 		return 0;
2140*4882a593Smuzhiyun 
2141*4882a593Smuzhiyun retry:
2142*4882a593Smuzhiyun 	if (WARN_ON(next_task == rq->curr))
2143*4882a593Smuzhiyun 		return 0;
2144*4882a593Smuzhiyun 
2145*4882a593Smuzhiyun 	/*
2146*4882a593Smuzhiyun 	 * If next_task preempts rq->curr, and rq->curr
2147*4882a593Smuzhiyun 	 * can move away, it makes sense to just reschedule
2148*4882a593Smuzhiyun 	 * without going further in pushing next_task.
2149*4882a593Smuzhiyun 	 */
2150*4882a593Smuzhiyun 	if (dl_task(rq->curr) &&
2151*4882a593Smuzhiyun 	    dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
2152*4882a593Smuzhiyun 	    rq->curr->nr_cpus_allowed > 1) {
2153*4882a593Smuzhiyun 		resched_curr(rq);
2154*4882a593Smuzhiyun 		return 0;
2155*4882a593Smuzhiyun 	}
2156*4882a593Smuzhiyun 
2157*4882a593Smuzhiyun 	/* We might release rq lock */
2158*4882a593Smuzhiyun 	get_task_struct(next_task);
2159*4882a593Smuzhiyun 
2160*4882a593Smuzhiyun 	/* Will lock the rq it'll find */
2161*4882a593Smuzhiyun 	later_rq = find_lock_later_rq(next_task, rq);
2162*4882a593Smuzhiyun 	if (!later_rq) {
2163*4882a593Smuzhiyun 		struct task_struct *task;
2164*4882a593Smuzhiyun 
2165*4882a593Smuzhiyun 		/*
2166*4882a593Smuzhiyun 		 * We must check all this again, since
2167*4882a593Smuzhiyun 		 * find_lock_later_rq releases rq->lock and it is
2168*4882a593Smuzhiyun 		 * then possible that next_task has migrated.
2169*4882a593Smuzhiyun 		 */
2170*4882a593Smuzhiyun 		task = pick_next_pushable_dl_task(rq);
2171*4882a593Smuzhiyun 		if (task == next_task) {
2172*4882a593Smuzhiyun 			/*
2173*4882a593Smuzhiyun 			 * The task is still there. We don't try
2174*4882a593Smuzhiyun 			 * again, some other CPU will pull it when ready.
2175*4882a593Smuzhiyun 			 */
2176*4882a593Smuzhiyun 			goto out;
2177*4882a593Smuzhiyun 		}
2178*4882a593Smuzhiyun 
2179*4882a593Smuzhiyun 		if (!task)
2180*4882a593Smuzhiyun 			/* No more tasks */
2181*4882a593Smuzhiyun 			goto out;
2182*4882a593Smuzhiyun 
2183*4882a593Smuzhiyun 		put_task_struct(next_task);
2184*4882a593Smuzhiyun 		next_task = task;
2185*4882a593Smuzhiyun 		goto retry;
2186*4882a593Smuzhiyun 	}
2187*4882a593Smuzhiyun 
2188*4882a593Smuzhiyun 	deactivate_task(rq, next_task, 0);
2189*4882a593Smuzhiyun 	set_task_cpu(next_task, later_rq->cpu);
2190*4882a593Smuzhiyun 
2191*4882a593Smuzhiyun 	/*
2192*4882a593Smuzhiyun 	 * Update the later_rq clock here, because the clock is used
2193*4882a593Smuzhiyun 	 * by the cpufreq_update_util() inside __add_running_bw().
2194*4882a593Smuzhiyun 	 */
2195*4882a593Smuzhiyun 	update_rq_clock(later_rq);
2196*4882a593Smuzhiyun 	activate_task(later_rq, next_task, ENQUEUE_NOCLOCK);
2197*4882a593Smuzhiyun 	ret = 1;
2198*4882a593Smuzhiyun 
2199*4882a593Smuzhiyun 	resched_curr(later_rq);
2200*4882a593Smuzhiyun 
2201*4882a593Smuzhiyun 	double_unlock_balance(rq, later_rq);
2202*4882a593Smuzhiyun 
2203*4882a593Smuzhiyun out:
2204*4882a593Smuzhiyun 	put_task_struct(next_task);
2205*4882a593Smuzhiyun 
2206*4882a593Smuzhiyun 	return ret;
2207*4882a593Smuzhiyun }
2208*4882a593Smuzhiyun 
push_dl_tasks(struct rq * rq)2209*4882a593Smuzhiyun static void push_dl_tasks(struct rq *rq)
2210*4882a593Smuzhiyun {
2211*4882a593Smuzhiyun 	/* push_dl_task() will return true if it moved a -deadline task */
2212*4882a593Smuzhiyun 	while (push_dl_task(rq))
2213*4882a593Smuzhiyun 		;
2214*4882a593Smuzhiyun }
2215*4882a593Smuzhiyun 
pull_dl_task(struct rq * this_rq)2216*4882a593Smuzhiyun static void pull_dl_task(struct rq *this_rq)
2217*4882a593Smuzhiyun {
2218*4882a593Smuzhiyun 	int this_cpu = this_rq->cpu, cpu;
2219*4882a593Smuzhiyun 	struct task_struct *p;
2220*4882a593Smuzhiyun 	bool resched = false;
2221*4882a593Smuzhiyun 	struct rq *src_rq;
2222*4882a593Smuzhiyun 	u64 dmin = LONG_MAX;
2223*4882a593Smuzhiyun 
2224*4882a593Smuzhiyun 	if (likely(!dl_overloaded(this_rq)))
2225*4882a593Smuzhiyun 		return;
2226*4882a593Smuzhiyun 
2227*4882a593Smuzhiyun 	/*
2228*4882a593Smuzhiyun 	 * Match the barrier from dl_set_overloaded; this guarantees that if we
2229*4882a593Smuzhiyun 	 * see overloaded we must also see the dlo_mask bit.
2230*4882a593Smuzhiyun 	 */
2231*4882a593Smuzhiyun 	smp_rmb();
2232*4882a593Smuzhiyun 
2233*4882a593Smuzhiyun 	for_each_cpu(cpu, this_rq->rd->dlo_mask) {
2234*4882a593Smuzhiyun 		if (this_cpu == cpu)
2235*4882a593Smuzhiyun 			continue;
2236*4882a593Smuzhiyun 
2237*4882a593Smuzhiyun 		src_rq = cpu_rq(cpu);
2238*4882a593Smuzhiyun 
2239*4882a593Smuzhiyun 		/*
2240*4882a593Smuzhiyun 		 * It looks racy, abd it is! However, as in sched_rt.c,
2241*4882a593Smuzhiyun 		 * we are fine with this.
2242*4882a593Smuzhiyun 		 */
2243*4882a593Smuzhiyun 		if (this_rq->dl.dl_nr_running &&
2244*4882a593Smuzhiyun 		    dl_time_before(this_rq->dl.earliest_dl.curr,
2245*4882a593Smuzhiyun 				   src_rq->dl.earliest_dl.next))
2246*4882a593Smuzhiyun 			continue;
2247*4882a593Smuzhiyun 
2248*4882a593Smuzhiyun 		/* Might drop this_rq->lock */
2249*4882a593Smuzhiyun 		double_lock_balance(this_rq, src_rq);
2250*4882a593Smuzhiyun 
2251*4882a593Smuzhiyun 		/*
2252*4882a593Smuzhiyun 		 * If there are no more pullable tasks on the
2253*4882a593Smuzhiyun 		 * rq, we're done with it.
2254*4882a593Smuzhiyun 		 */
2255*4882a593Smuzhiyun 		if (src_rq->dl.dl_nr_running <= 1)
2256*4882a593Smuzhiyun 			goto skip;
2257*4882a593Smuzhiyun 
2258*4882a593Smuzhiyun 		p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
2259*4882a593Smuzhiyun 
2260*4882a593Smuzhiyun 		/*
2261*4882a593Smuzhiyun 		 * We found a task to be pulled if:
2262*4882a593Smuzhiyun 		 *  - it preempts our current (if there's one),
2263*4882a593Smuzhiyun 		 *  - it will preempt the last one we pulled (if any).
2264*4882a593Smuzhiyun 		 */
2265*4882a593Smuzhiyun 		if (p && dl_time_before(p->dl.deadline, dmin) &&
2266*4882a593Smuzhiyun 		    (!this_rq->dl.dl_nr_running ||
2267*4882a593Smuzhiyun 		     dl_time_before(p->dl.deadline,
2268*4882a593Smuzhiyun 				    this_rq->dl.earliest_dl.curr))) {
2269*4882a593Smuzhiyun 			WARN_ON(p == src_rq->curr);
2270*4882a593Smuzhiyun 			WARN_ON(!task_on_rq_queued(p));
2271*4882a593Smuzhiyun 
2272*4882a593Smuzhiyun 			/*
2273*4882a593Smuzhiyun 			 * Then we pull iff p has actually an earlier
2274*4882a593Smuzhiyun 			 * deadline than the current task of its runqueue.
2275*4882a593Smuzhiyun 			 */
2276*4882a593Smuzhiyun 			if (dl_time_before(p->dl.deadline,
2277*4882a593Smuzhiyun 					   src_rq->curr->dl.deadline))
2278*4882a593Smuzhiyun 				goto skip;
2279*4882a593Smuzhiyun 
2280*4882a593Smuzhiyun 			resched = true;
2281*4882a593Smuzhiyun 
2282*4882a593Smuzhiyun 			deactivate_task(src_rq, p, 0);
2283*4882a593Smuzhiyun 			set_task_cpu(p, this_cpu);
2284*4882a593Smuzhiyun 			activate_task(this_rq, p, 0);
2285*4882a593Smuzhiyun 			dmin = p->dl.deadline;
2286*4882a593Smuzhiyun 
2287*4882a593Smuzhiyun 			/* Is there any other task even earlier? */
2288*4882a593Smuzhiyun 		}
2289*4882a593Smuzhiyun skip:
2290*4882a593Smuzhiyun 		double_unlock_balance(this_rq, src_rq);
2291*4882a593Smuzhiyun 	}
2292*4882a593Smuzhiyun 
2293*4882a593Smuzhiyun 	if (resched)
2294*4882a593Smuzhiyun 		resched_curr(this_rq);
2295*4882a593Smuzhiyun }
2296*4882a593Smuzhiyun 
2297*4882a593Smuzhiyun /*
2298*4882a593Smuzhiyun  * Since the task is not running and a reschedule is not going to happen
2299*4882a593Smuzhiyun  * anytime soon on its runqueue, we try pushing it away now.
2300*4882a593Smuzhiyun  */
task_woken_dl(struct rq * rq,struct task_struct * p)2301*4882a593Smuzhiyun static void task_woken_dl(struct rq *rq, struct task_struct *p)
2302*4882a593Smuzhiyun {
2303*4882a593Smuzhiyun 	if (!task_running(rq, p) &&
2304*4882a593Smuzhiyun 	    !test_tsk_need_resched(rq->curr) &&
2305*4882a593Smuzhiyun 	    p->nr_cpus_allowed > 1 &&
2306*4882a593Smuzhiyun 	    dl_task(rq->curr) &&
2307*4882a593Smuzhiyun 	    (rq->curr->nr_cpus_allowed < 2 ||
2308*4882a593Smuzhiyun 	     !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
2309*4882a593Smuzhiyun 		push_dl_tasks(rq);
2310*4882a593Smuzhiyun 	}
2311*4882a593Smuzhiyun }
2312*4882a593Smuzhiyun 
set_cpus_allowed_dl(struct task_struct * p,const struct cpumask * new_mask)2313*4882a593Smuzhiyun static void set_cpus_allowed_dl(struct task_struct *p,
2314*4882a593Smuzhiyun 				const struct cpumask *new_mask)
2315*4882a593Smuzhiyun {
2316*4882a593Smuzhiyun 	struct root_domain *src_rd;
2317*4882a593Smuzhiyun 	struct rq *rq;
2318*4882a593Smuzhiyun 
2319*4882a593Smuzhiyun 	BUG_ON(!dl_task(p));
2320*4882a593Smuzhiyun 
2321*4882a593Smuzhiyun 	rq = task_rq(p);
2322*4882a593Smuzhiyun 	src_rd = rq->rd;
2323*4882a593Smuzhiyun 	/*
2324*4882a593Smuzhiyun 	 * Migrating a SCHED_DEADLINE task between exclusive
2325*4882a593Smuzhiyun 	 * cpusets (different root_domains) entails a bandwidth
2326*4882a593Smuzhiyun 	 * update. We already made space for us in the destination
2327*4882a593Smuzhiyun 	 * domain (see cpuset_can_attach()).
2328*4882a593Smuzhiyun 	 */
2329*4882a593Smuzhiyun 	if (!cpumask_intersects(src_rd->span, new_mask)) {
2330*4882a593Smuzhiyun 		struct dl_bw *src_dl_b;
2331*4882a593Smuzhiyun 
2332*4882a593Smuzhiyun 		src_dl_b = dl_bw_of(cpu_of(rq));
2333*4882a593Smuzhiyun 		/*
2334*4882a593Smuzhiyun 		 * We now free resources of the root_domain we are migrating
2335*4882a593Smuzhiyun 		 * off. In the worst case, sched_setattr() may temporary fail
2336*4882a593Smuzhiyun 		 * until we complete the update.
2337*4882a593Smuzhiyun 		 */
2338*4882a593Smuzhiyun 		raw_spin_lock(&src_dl_b->lock);
2339*4882a593Smuzhiyun 		__dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
2340*4882a593Smuzhiyun 		raw_spin_unlock(&src_dl_b->lock);
2341*4882a593Smuzhiyun 	}
2342*4882a593Smuzhiyun 
2343*4882a593Smuzhiyun 	set_cpus_allowed_common(p, new_mask);
2344*4882a593Smuzhiyun }
2345*4882a593Smuzhiyun 
2346*4882a593Smuzhiyun /* Assumes rq->lock is held */
rq_online_dl(struct rq * rq)2347*4882a593Smuzhiyun static void rq_online_dl(struct rq *rq)
2348*4882a593Smuzhiyun {
2349*4882a593Smuzhiyun 	if (rq->dl.overloaded)
2350*4882a593Smuzhiyun 		dl_set_overload(rq);
2351*4882a593Smuzhiyun 
2352*4882a593Smuzhiyun 	cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
2353*4882a593Smuzhiyun 	if (rq->dl.dl_nr_running > 0)
2354*4882a593Smuzhiyun 		cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
2355*4882a593Smuzhiyun }
2356*4882a593Smuzhiyun 
2357*4882a593Smuzhiyun /* Assumes rq->lock is held */
rq_offline_dl(struct rq * rq)2358*4882a593Smuzhiyun static void rq_offline_dl(struct rq *rq)
2359*4882a593Smuzhiyun {
2360*4882a593Smuzhiyun 	if (rq->dl.overloaded)
2361*4882a593Smuzhiyun 		dl_clear_overload(rq);
2362*4882a593Smuzhiyun 
2363*4882a593Smuzhiyun 	cpudl_clear(&rq->rd->cpudl, rq->cpu);
2364*4882a593Smuzhiyun 	cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
2365*4882a593Smuzhiyun }
2366*4882a593Smuzhiyun 
init_sched_dl_class(void)2367*4882a593Smuzhiyun void __init init_sched_dl_class(void)
2368*4882a593Smuzhiyun {
2369*4882a593Smuzhiyun 	unsigned int i;
2370*4882a593Smuzhiyun 
2371*4882a593Smuzhiyun 	for_each_possible_cpu(i)
2372*4882a593Smuzhiyun 		zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
2373*4882a593Smuzhiyun 					GFP_KERNEL, cpu_to_node(i));
2374*4882a593Smuzhiyun }
2375*4882a593Smuzhiyun 
dl_add_task_root_domain(struct task_struct * p)2376*4882a593Smuzhiyun void dl_add_task_root_domain(struct task_struct *p)
2377*4882a593Smuzhiyun {
2378*4882a593Smuzhiyun 	struct rq_flags rf;
2379*4882a593Smuzhiyun 	struct rq *rq;
2380*4882a593Smuzhiyun 	struct dl_bw *dl_b;
2381*4882a593Smuzhiyun 
2382*4882a593Smuzhiyun 	raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
2383*4882a593Smuzhiyun 	if (!dl_task(p)) {
2384*4882a593Smuzhiyun 		raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags);
2385*4882a593Smuzhiyun 		return;
2386*4882a593Smuzhiyun 	}
2387*4882a593Smuzhiyun 
2388*4882a593Smuzhiyun 	rq = __task_rq_lock(p, &rf);
2389*4882a593Smuzhiyun 
2390*4882a593Smuzhiyun 	dl_b = &rq->rd->dl_bw;
2391*4882a593Smuzhiyun 	raw_spin_lock(&dl_b->lock);
2392*4882a593Smuzhiyun 
2393*4882a593Smuzhiyun 	__dl_add(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
2394*4882a593Smuzhiyun 
2395*4882a593Smuzhiyun 	raw_spin_unlock(&dl_b->lock);
2396*4882a593Smuzhiyun 
2397*4882a593Smuzhiyun 	task_rq_unlock(rq, p, &rf);
2398*4882a593Smuzhiyun }
2399*4882a593Smuzhiyun 
dl_clear_root_domain(struct root_domain * rd)2400*4882a593Smuzhiyun void dl_clear_root_domain(struct root_domain *rd)
2401*4882a593Smuzhiyun {
2402*4882a593Smuzhiyun 	unsigned long flags;
2403*4882a593Smuzhiyun 
2404*4882a593Smuzhiyun 	raw_spin_lock_irqsave(&rd->dl_bw.lock, flags);
2405*4882a593Smuzhiyun 	rd->dl_bw.total_bw = 0;
2406*4882a593Smuzhiyun 	raw_spin_unlock_irqrestore(&rd->dl_bw.lock, flags);
2407*4882a593Smuzhiyun }
2408*4882a593Smuzhiyun 
2409*4882a593Smuzhiyun #endif /* CONFIG_SMP */
2410*4882a593Smuzhiyun 
switched_from_dl(struct rq * rq,struct task_struct * p)2411*4882a593Smuzhiyun static void switched_from_dl(struct rq *rq, struct task_struct *p)
2412*4882a593Smuzhiyun {
2413*4882a593Smuzhiyun 	/*
2414*4882a593Smuzhiyun 	 * task_non_contending() can start the "inactive timer" (if the 0-lag
2415*4882a593Smuzhiyun 	 * time is in the future). If the task switches back to dl before
2416*4882a593Smuzhiyun 	 * the "inactive timer" fires, it can continue to consume its current
2417*4882a593Smuzhiyun 	 * runtime using its current deadline. If it stays outside of
2418*4882a593Smuzhiyun 	 * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
2419*4882a593Smuzhiyun 	 * will reset the task parameters.
2420*4882a593Smuzhiyun 	 */
2421*4882a593Smuzhiyun 	if (task_on_rq_queued(p) && p->dl.dl_runtime)
2422*4882a593Smuzhiyun 		task_non_contending(p);
2423*4882a593Smuzhiyun 
2424*4882a593Smuzhiyun 	if (!task_on_rq_queued(p)) {
2425*4882a593Smuzhiyun 		/*
2426*4882a593Smuzhiyun 		 * Inactive timer is armed. However, p is leaving DEADLINE and
2427*4882a593Smuzhiyun 		 * might migrate away from this rq while continuing to run on
2428*4882a593Smuzhiyun 		 * some other class. We need to remove its contribution from
2429*4882a593Smuzhiyun 		 * this rq running_bw now, or sub_rq_bw (below) will complain.
2430*4882a593Smuzhiyun 		 */
2431*4882a593Smuzhiyun 		if (p->dl.dl_non_contending)
2432*4882a593Smuzhiyun 			sub_running_bw(&p->dl, &rq->dl);
2433*4882a593Smuzhiyun 		sub_rq_bw(&p->dl, &rq->dl);
2434*4882a593Smuzhiyun 	}
2435*4882a593Smuzhiyun 
2436*4882a593Smuzhiyun 	/*
2437*4882a593Smuzhiyun 	 * We cannot use inactive_task_timer() to invoke sub_running_bw()
2438*4882a593Smuzhiyun 	 * at the 0-lag time, because the task could have been migrated
2439*4882a593Smuzhiyun 	 * while SCHED_OTHER in the meanwhile.
2440*4882a593Smuzhiyun 	 */
2441*4882a593Smuzhiyun 	if (p->dl.dl_non_contending)
2442*4882a593Smuzhiyun 		p->dl.dl_non_contending = 0;
2443*4882a593Smuzhiyun 
2444*4882a593Smuzhiyun 	/*
2445*4882a593Smuzhiyun 	 * Since this might be the only -deadline task on the rq,
2446*4882a593Smuzhiyun 	 * this is the right place to try to pull some other one
2447*4882a593Smuzhiyun 	 * from an overloaded CPU, if any.
2448*4882a593Smuzhiyun 	 */
2449*4882a593Smuzhiyun 	if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
2450*4882a593Smuzhiyun 		return;
2451*4882a593Smuzhiyun 
2452*4882a593Smuzhiyun 	deadline_queue_pull_task(rq);
2453*4882a593Smuzhiyun }
2454*4882a593Smuzhiyun 
2455*4882a593Smuzhiyun /*
2456*4882a593Smuzhiyun  * When switching to -deadline, we may overload the rq, then
2457*4882a593Smuzhiyun  * we try to push someone off, if possible.
2458*4882a593Smuzhiyun  */
switched_to_dl(struct rq * rq,struct task_struct * p)2459*4882a593Smuzhiyun static void switched_to_dl(struct rq *rq, struct task_struct *p)
2460*4882a593Smuzhiyun {
2461*4882a593Smuzhiyun 	if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
2462*4882a593Smuzhiyun 		put_task_struct(p);
2463*4882a593Smuzhiyun 
2464*4882a593Smuzhiyun 	/* If p is not queued we will update its parameters at next wakeup. */
2465*4882a593Smuzhiyun 	if (!task_on_rq_queued(p)) {
2466*4882a593Smuzhiyun 		add_rq_bw(&p->dl, &rq->dl);
2467*4882a593Smuzhiyun 
2468*4882a593Smuzhiyun 		return;
2469*4882a593Smuzhiyun 	}
2470*4882a593Smuzhiyun 
2471*4882a593Smuzhiyun 	if (rq->curr != p) {
2472*4882a593Smuzhiyun #ifdef CONFIG_SMP
2473*4882a593Smuzhiyun 		if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
2474*4882a593Smuzhiyun 			deadline_queue_push_tasks(rq);
2475*4882a593Smuzhiyun #endif
2476*4882a593Smuzhiyun 		if (dl_task(rq->curr))
2477*4882a593Smuzhiyun 			check_preempt_curr_dl(rq, p, 0);
2478*4882a593Smuzhiyun 		else
2479*4882a593Smuzhiyun 			resched_curr(rq);
2480*4882a593Smuzhiyun 	} else {
2481*4882a593Smuzhiyun 		update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
2482*4882a593Smuzhiyun 	}
2483*4882a593Smuzhiyun }
2484*4882a593Smuzhiyun 
2485*4882a593Smuzhiyun /*
2486*4882a593Smuzhiyun  * If the scheduling parameters of a -deadline task changed,
2487*4882a593Smuzhiyun  * a push or pull operation might be needed.
2488*4882a593Smuzhiyun  */
prio_changed_dl(struct rq * rq,struct task_struct * p,int oldprio)2489*4882a593Smuzhiyun static void prio_changed_dl(struct rq *rq, struct task_struct *p,
2490*4882a593Smuzhiyun 			    int oldprio)
2491*4882a593Smuzhiyun {
2492*4882a593Smuzhiyun 	if (task_on_rq_queued(p) || rq->curr == p) {
2493*4882a593Smuzhiyun #ifdef CONFIG_SMP
2494*4882a593Smuzhiyun 		/*
2495*4882a593Smuzhiyun 		 * This might be too much, but unfortunately
2496*4882a593Smuzhiyun 		 * we don't have the old deadline value, and
2497*4882a593Smuzhiyun 		 * we can't argue if the task is increasing
2498*4882a593Smuzhiyun 		 * or lowering its prio, so...
2499*4882a593Smuzhiyun 		 */
2500*4882a593Smuzhiyun 		if (!rq->dl.overloaded)
2501*4882a593Smuzhiyun 			deadline_queue_pull_task(rq);
2502*4882a593Smuzhiyun 
2503*4882a593Smuzhiyun 		/*
2504*4882a593Smuzhiyun 		 * If we now have a earlier deadline task than p,
2505*4882a593Smuzhiyun 		 * then reschedule, provided p is still on this
2506*4882a593Smuzhiyun 		 * runqueue.
2507*4882a593Smuzhiyun 		 */
2508*4882a593Smuzhiyun 		if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
2509*4882a593Smuzhiyun 			resched_curr(rq);
2510*4882a593Smuzhiyun #else
2511*4882a593Smuzhiyun 		/*
2512*4882a593Smuzhiyun 		 * Again, we don't know if p has a earlier
2513*4882a593Smuzhiyun 		 * or later deadline, so let's blindly set a
2514*4882a593Smuzhiyun 		 * (maybe not needed) rescheduling point.
2515*4882a593Smuzhiyun 		 */
2516*4882a593Smuzhiyun 		resched_curr(rq);
2517*4882a593Smuzhiyun #endif /* CONFIG_SMP */
2518*4882a593Smuzhiyun 	}
2519*4882a593Smuzhiyun }
2520*4882a593Smuzhiyun 
2521*4882a593Smuzhiyun const struct sched_class dl_sched_class
2522*4882a593Smuzhiyun 	__section("__dl_sched_class") = {
2523*4882a593Smuzhiyun 	.enqueue_task		= enqueue_task_dl,
2524*4882a593Smuzhiyun 	.dequeue_task		= dequeue_task_dl,
2525*4882a593Smuzhiyun 	.yield_task		= yield_task_dl,
2526*4882a593Smuzhiyun 
2527*4882a593Smuzhiyun 	.check_preempt_curr	= check_preempt_curr_dl,
2528*4882a593Smuzhiyun 
2529*4882a593Smuzhiyun 	.pick_next_task		= pick_next_task_dl,
2530*4882a593Smuzhiyun 	.put_prev_task		= put_prev_task_dl,
2531*4882a593Smuzhiyun 	.set_next_task		= set_next_task_dl,
2532*4882a593Smuzhiyun 
2533*4882a593Smuzhiyun #ifdef CONFIG_SMP
2534*4882a593Smuzhiyun 	.balance		= balance_dl,
2535*4882a593Smuzhiyun 	.select_task_rq		= select_task_rq_dl,
2536*4882a593Smuzhiyun 	.migrate_task_rq	= migrate_task_rq_dl,
2537*4882a593Smuzhiyun 	.set_cpus_allowed       = set_cpus_allowed_dl,
2538*4882a593Smuzhiyun 	.rq_online              = rq_online_dl,
2539*4882a593Smuzhiyun 	.rq_offline             = rq_offline_dl,
2540*4882a593Smuzhiyun 	.task_woken		= task_woken_dl,
2541*4882a593Smuzhiyun #endif
2542*4882a593Smuzhiyun 
2543*4882a593Smuzhiyun 	.task_tick		= task_tick_dl,
2544*4882a593Smuzhiyun 	.task_fork              = task_fork_dl,
2545*4882a593Smuzhiyun 
2546*4882a593Smuzhiyun 	.prio_changed           = prio_changed_dl,
2547*4882a593Smuzhiyun 	.switched_from		= switched_from_dl,
2548*4882a593Smuzhiyun 	.switched_to		= switched_to_dl,
2549*4882a593Smuzhiyun 
2550*4882a593Smuzhiyun 	.update_curr		= update_curr_dl,
2551*4882a593Smuzhiyun };
2552*4882a593Smuzhiyun 
sched_dl_global_validate(void)2553*4882a593Smuzhiyun int sched_dl_global_validate(void)
2554*4882a593Smuzhiyun {
2555*4882a593Smuzhiyun 	u64 runtime = global_rt_runtime();
2556*4882a593Smuzhiyun 	u64 period = global_rt_period();
2557*4882a593Smuzhiyun 	u64 new_bw = to_ratio(period, runtime);
2558*4882a593Smuzhiyun 	struct dl_bw *dl_b;
2559*4882a593Smuzhiyun 	int cpu, cpus, ret = 0;
2560*4882a593Smuzhiyun 	unsigned long flags;
2561*4882a593Smuzhiyun 
2562*4882a593Smuzhiyun 	/*
2563*4882a593Smuzhiyun 	 * Here we want to check the bandwidth not being set to some
2564*4882a593Smuzhiyun 	 * value smaller than the currently allocated bandwidth in
2565*4882a593Smuzhiyun 	 * any of the root_domains.
2566*4882a593Smuzhiyun 	 *
2567*4882a593Smuzhiyun 	 * FIXME: Cycling on all the CPUs is overdoing, but simpler than
2568*4882a593Smuzhiyun 	 * cycling on root_domains... Discussion on different/better
2569*4882a593Smuzhiyun 	 * solutions is welcome!
2570*4882a593Smuzhiyun 	 */
2571*4882a593Smuzhiyun 	for_each_possible_cpu(cpu) {
2572*4882a593Smuzhiyun 		rcu_read_lock_sched();
2573*4882a593Smuzhiyun 		dl_b = dl_bw_of(cpu);
2574*4882a593Smuzhiyun 		cpus = dl_bw_cpus(cpu);
2575*4882a593Smuzhiyun 
2576*4882a593Smuzhiyun 		raw_spin_lock_irqsave(&dl_b->lock, flags);
2577*4882a593Smuzhiyun 		if (new_bw * cpus < dl_b->total_bw)
2578*4882a593Smuzhiyun 			ret = -EBUSY;
2579*4882a593Smuzhiyun 		raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2580*4882a593Smuzhiyun 
2581*4882a593Smuzhiyun 		rcu_read_unlock_sched();
2582*4882a593Smuzhiyun 
2583*4882a593Smuzhiyun 		if (ret)
2584*4882a593Smuzhiyun 			break;
2585*4882a593Smuzhiyun 	}
2586*4882a593Smuzhiyun 
2587*4882a593Smuzhiyun 	return ret;
2588*4882a593Smuzhiyun }
2589*4882a593Smuzhiyun 
init_dl_rq_bw_ratio(struct dl_rq * dl_rq)2590*4882a593Smuzhiyun static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq)
2591*4882a593Smuzhiyun {
2592*4882a593Smuzhiyun 	if (global_rt_runtime() == RUNTIME_INF) {
2593*4882a593Smuzhiyun 		dl_rq->bw_ratio = 1 << RATIO_SHIFT;
2594*4882a593Smuzhiyun 		dl_rq->extra_bw = 1 << BW_SHIFT;
2595*4882a593Smuzhiyun 	} else {
2596*4882a593Smuzhiyun 		dl_rq->bw_ratio = to_ratio(global_rt_runtime(),
2597*4882a593Smuzhiyun 			  global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT);
2598*4882a593Smuzhiyun 		dl_rq->extra_bw = to_ratio(global_rt_period(),
2599*4882a593Smuzhiyun 						    global_rt_runtime());
2600*4882a593Smuzhiyun 	}
2601*4882a593Smuzhiyun }
2602*4882a593Smuzhiyun 
sched_dl_do_global(void)2603*4882a593Smuzhiyun void sched_dl_do_global(void)
2604*4882a593Smuzhiyun {
2605*4882a593Smuzhiyun 	u64 new_bw = -1;
2606*4882a593Smuzhiyun 	struct dl_bw *dl_b;
2607*4882a593Smuzhiyun 	int cpu;
2608*4882a593Smuzhiyun 	unsigned long flags;
2609*4882a593Smuzhiyun 
2610*4882a593Smuzhiyun 	def_dl_bandwidth.dl_period = global_rt_period();
2611*4882a593Smuzhiyun 	def_dl_bandwidth.dl_runtime = global_rt_runtime();
2612*4882a593Smuzhiyun 
2613*4882a593Smuzhiyun 	if (global_rt_runtime() != RUNTIME_INF)
2614*4882a593Smuzhiyun 		new_bw = to_ratio(global_rt_period(), global_rt_runtime());
2615*4882a593Smuzhiyun 
2616*4882a593Smuzhiyun 	/*
2617*4882a593Smuzhiyun 	 * FIXME: As above...
2618*4882a593Smuzhiyun 	 */
2619*4882a593Smuzhiyun 	for_each_possible_cpu(cpu) {
2620*4882a593Smuzhiyun 		rcu_read_lock_sched();
2621*4882a593Smuzhiyun 		dl_b = dl_bw_of(cpu);
2622*4882a593Smuzhiyun 
2623*4882a593Smuzhiyun 		raw_spin_lock_irqsave(&dl_b->lock, flags);
2624*4882a593Smuzhiyun 		dl_b->bw = new_bw;
2625*4882a593Smuzhiyun 		raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2626*4882a593Smuzhiyun 
2627*4882a593Smuzhiyun 		rcu_read_unlock_sched();
2628*4882a593Smuzhiyun 		init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl);
2629*4882a593Smuzhiyun 	}
2630*4882a593Smuzhiyun }
2631*4882a593Smuzhiyun 
2632*4882a593Smuzhiyun /*
2633*4882a593Smuzhiyun  * We must be sure that accepting a new task (or allowing changing the
2634*4882a593Smuzhiyun  * parameters of an existing one) is consistent with the bandwidth
2635*4882a593Smuzhiyun  * constraints. If yes, this function also accordingly updates the currently
2636*4882a593Smuzhiyun  * allocated bandwidth to reflect the new situation.
2637*4882a593Smuzhiyun  *
2638*4882a593Smuzhiyun  * This function is called while holding p's rq->lock.
2639*4882a593Smuzhiyun  */
sched_dl_overflow(struct task_struct * p,int policy,const struct sched_attr * attr)2640*4882a593Smuzhiyun int sched_dl_overflow(struct task_struct *p, int policy,
2641*4882a593Smuzhiyun 		      const struct sched_attr *attr)
2642*4882a593Smuzhiyun {
2643*4882a593Smuzhiyun 	u64 period = attr->sched_period ?: attr->sched_deadline;
2644*4882a593Smuzhiyun 	u64 runtime = attr->sched_runtime;
2645*4882a593Smuzhiyun 	u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
2646*4882a593Smuzhiyun 	int cpus, err = -1, cpu = task_cpu(p);
2647*4882a593Smuzhiyun 	struct dl_bw *dl_b = dl_bw_of(cpu);
2648*4882a593Smuzhiyun 	unsigned long cap;
2649*4882a593Smuzhiyun 
2650*4882a593Smuzhiyun 	if (attr->sched_flags & SCHED_FLAG_SUGOV)
2651*4882a593Smuzhiyun 		return 0;
2652*4882a593Smuzhiyun 
2653*4882a593Smuzhiyun 	/* !deadline task may carry old deadline bandwidth */
2654*4882a593Smuzhiyun 	if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
2655*4882a593Smuzhiyun 		return 0;
2656*4882a593Smuzhiyun 
2657*4882a593Smuzhiyun 	/*
2658*4882a593Smuzhiyun 	 * Either if a task, enters, leave, or stays -deadline but changes
2659*4882a593Smuzhiyun 	 * its parameters, we may need to update accordingly the total
2660*4882a593Smuzhiyun 	 * allocated bandwidth of the container.
2661*4882a593Smuzhiyun 	 */
2662*4882a593Smuzhiyun 	raw_spin_lock(&dl_b->lock);
2663*4882a593Smuzhiyun 	cpus = dl_bw_cpus(cpu);
2664*4882a593Smuzhiyun 	cap = dl_bw_capacity(cpu);
2665*4882a593Smuzhiyun 
2666*4882a593Smuzhiyun 	if (dl_policy(policy) && !task_has_dl_policy(p) &&
2667*4882a593Smuzhiyun 	    !__dl_overflow(dl_b, cap, 0, new_bw)) {
2668*4882a593Smuzhiyun 		if (hrtimer_active(&p->dl.inactive_timer))
2669*4882a593Smuzhiyun 			__dl_sub(dl_b, p->dl.dl_bw, cpus);
2670*4882a593Smuzhiyun 		__dl_add(dl_b, new_bw, cpus);
2671*4882a593Smuzhiyun 		err = 0;
2672*4882a593Smuzhiyun 	} else if (dl_policy(policy) && task_has_dl_policy(p) &&
2673*4882a593Smuzhiyun 		   !__dl_overflow(dl_b, cap, p->dl.dl_bw, new_bw)) {
2674*4882a593Smuzhiyun 		/*
2675*4882a593Smuzhiyun 		 * XXX this is slightly incorrect: when the task
2676*4882a593Smuzhiyun 		 * utilization decreases, we should delay the total
2677*4882a593Smuzhiyun 		 * utilization change until the task's 0-lag point.
2678*4882a593Smuzhiyun 		 * But this would require to set the task's "inactive
2679*4882a593Smuzhiyun 		 * timer" when the task is not inactive.
2680*4882a593Smuzhiyun 		 */
2681*4882a593Smuzhiyun 		__dl_sub(dl_b, p->dl.dl_bw, cpus);
2682*4882a593Smuzhiyun 		__dl_add(dl_b, new_bw, cpus);
2683*4882a593Smuzhiyun 		dl_change_utilization(p, new_bw);
2684*4882a593Smuzhiyun 		err = 0;
2685*4882a593Smuzhiyun 	} else if (!dl_policy(policy) && task_has_dl_policy(p)) {
2686*4882a593Smuzhiyun 		/*
2687*4882a593Smuzhiyun 		 * Do not decrease the total deadline utilization here,
2688*4882a593Smuzhiyun 		 * switched_from_dl() will take care to do it at the correct
2689*4882a593Smuzhiyun 		 * (0-lag) time.
2690*4882a593Smuzhiyun 		 */
2691*4882a593Smuzhiyun 		err = 0;
2692*4882a593Smuzhiyun 	}
2693*4882a593Smuzhiyun 	raw_spin_unlock(&dl_b->lock);
2694*4882a593Smuzhiyun 
2695*4882a593Smuzhiyun 	return err;
2696*4882a593Smuzhiyun }
2697*4882a593Smuzhiyun 
2698*4882a593Smuzhiyun /*
2699*4882a593Smuzhiyun  * This function initializes the sched_dl_entity of a newly becoming
2700*4882a593Smuzhiyun  * SCHED_DEADLINE task.
2701*4882a593Smuzhiyun  *
2702*4882a593Smuzhiyun  * Only the static values are considered here, the actual runtime and the
2703*4882a593Smuzhiyun  * absolute deadline will be properly calculated when the task is enqueued
2704*4882a593Smuzhiyun  * for the first time with its new policy.
2705*4882a593Smuzhiyun  */
__setparam_dl(struct task_struct * p,const struct sched_attr * attr)2706*4882a593Smuzhiyun void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
2707*4882a593Smuzhiyun {
2708*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se = &p->dl;
2709*4882a593Smuzhiyun 
2710*4882a593Smuzhiyun 	dl_se->dl_runtime = attr->sched_runtime;
2711*4882a593Smuzhiyun 	dl_se->dl_deadline = attr->sched_deadline;
2712*4882a593Smuzhiyun 	dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
2713*4882a593Smuzhiyun 	dl_se->flags = attr->sched_flags & SCHED_DL_FLAGS;
2714*4882a593Smuzhiyun 	dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
2715*4882a593Smuzhiyun 	dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
2716*4882a593Smuzhiyun }
2717*4882a593Smuzhiyun 
__getparam_dl(struct task_struct * p,struct sched_attr * attr)2718*4882a593Smuzhiyun void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
2719*4882a593Smuzhiyun {
2720*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se = &p->dl;
2721*4882a593Smuzhiyun 
2722*4882a593Smuzhiyun 	attr->sched_priority = p->rt_priority;
2723*4882a593Smuzhiyun 	attr->sched_runtime = dl_se->dl_runtime;
2724*4882a593Smuzhiyun 	attr->sched_deadline = dl_se->dl_deadline;
2725*4882a593Smuzhiyun 	attr->sched_period = dl_se->dl_period;
2726*4882a593Smuzhiyun 	attr->sched_flags &= ~SCHED_DL_FLAGS;
2727*4882a593Smuzhiyun 	attr->sched_flags |= dl_se->flags;
2728*4882a593Smuzhiyun }
2729*4882a593Smuzhiyun 
2730*4882a593Smuzhiyun /*
2731*4882a593Smuzhiyun  * Default limits for DL period; on the top end we guard against small util
2732*4882a593Smuzhiyun  * tasks still getting rediculous long effective runtimes, on the bottom end we
2733*4882a593Smuzhiyun  * guard against timer DoS.
2734*4882a593Smuzhiyun  */
2735*4882a593Smuzhiyun unsigned int sysctl_sched_dl_period_max = 1 << 22; /* ~4 seconds */
2736*4882a593Smuzhiyun unsigned int sysctl_sched_dl_period_min = 100;     /* 100 us */
2737*4882a593Smuzhiyun 
2738*4882a593Smuzhiyun /*
2739*4882a593Smuzhiyun  * This function validates the new parameters of a -deadline task.
2740*4882a593Smuzhiyun  * We ask for the deadline not being zero, and greater or equal
2741*4882a593Smuzhiyun  * than the runtime, as well as the period of being zero or
2742*4882a593Smuzhiyun  * greater than deadline. Furthermore, we have to be sure that
2743*4882a593Smuzhiyun  * user parameters are above the internal resolution of 1us (we
2744*4882a593Smuzhiyun  * check sched_runtime only since it is always the smaller one) and
2745*4882a593Smuzhiyun  * below 2^63 ns (we have to check both sched_deadline and
2746*4882a593Smuzhiyun  * sched_period, as the latter can be zero).
2747*4882a593Smuzhiyun  */
__checkparam_dl(const struct sched_attr * attr)2748*4882a593Smuzhiyun bool __checkparam_dl(const struct sched_attr *attr)
2749*4882a593Smuzhiyun {
2750*4882a593Smuzhiyun 	u64 period, max, min;
2751*4882a593Smuzhiyun 
2752*4882a593Smuzhiyun 	/* special dl tasks don't actually use any parameter */
2753*4882a593Smuzhiyun 	if (attr->sched_flags & SCHED_FLAG_SUGOV)
2754*4882a593Smuzhiyun 		return true;
2755*4882a593Smuzhiyun 
2756*4882a593Smuzhiyun 	/* deadline != 0 */
2757*4882a593Smuzhiyun 	if (attr->sched_deadline == 0)
2758*4882a593Smuzhiyun 		return false;
2759*4882a593Smuzhiyun 
2760*4882a593Smuzhiyun 	/*
2761*4882a593Smuzhiyun 	 * Since we truncate DL_SCALE bits, make sure we're at least
2762*4882a593Smuzhiyun 	 * that big.
2763*4882a593Smuzhiyun 	 */
2764*4882a593Smuzhiyun 	if (attr->sched_runtime < (1ULL << DL_SCALE))
2765*4882a593Smuzhiyun 		return false;
2766*4882a593Smuzhiyun 
2767*4882a593Smuzhiyun 	/*
2768*4882a593Smuzhiyun 	 * Since we use the MSB for wrap-around and sign issues, make
2769*4882a593Smuzhiyun 	 * sure it's not set (mind that period can be equal to zero).
2770*4882a593Smuzhiyun 	 */
2771*4882a593Smuzhiyun 	if (attr->sched_deadline & (1ULL << 63) ||
2772*4882a593Smuzhiyun 	    attr->sched_period & (1ULL << 63))
2773*4882a593Smuzhiyun 		return false;
2774*4882a593Smuzhiyun 
2775*4882a593Smuzhiyun 	period = attr->sched_period;
2776*4882a593Smuzhiyun 	if (!period)
2777*4882a593Smuzhiyun 		period = attr->sched_deadline;
2778*4882a593Smuzhiyun 
2779*4882a593Smuzhiyun 	/* runtime <= deadline <= period (if period != 0) */
2780*4882a593Smuzhiyun 	if (period < attr->sched_deadline ||
2781*4882a593Smuzhiyun 	    attr->sched_deadline < attr->sched_runtime)
2782*4882a593Smuzhiyun 		return false;
2783*4882a593Smuzhiyun 
2784*4882a593Smuzhiyun 	max = (u64)READ_ONCE(sysctl_sched_dl_period_max) * NSEC_PER_USEC;
2785*4882a593Smuzhiyun 	min = (u64)READ_ONCE(sysctl_sched_dl_period_min) * NSEC_PER_USEC;
2786*4882a593Smuzhiyun 
2787*4882a593Smuzhiyun 	if (period < min || period > max)
2788*4882a593Smuzhiyun 		return false;
2789*4882a593Smuzhiyun 
2790*4882a593Smuzhiyun 	return true;
2791*4882a593Smuzhiyun }
2792*4882a593Smuzhiyun 
2793*4882a593Smuzhiyun /*
2794*4882a593Smuzhiyun  * This function clears the sched_dl_entity static params.
2795*4882a593Smuzhiyun  */
__dl_clear_params(struct task_struct * p)2796*4882a593Smuzhiyun void __dl_clear_params(struct task_struct *p)
2797*4882a593Smuzhiyun {
2798*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se = &p->dl;
2799*4882a593Smuzhiyun 
2800*4882a593Smuzhiyun 	dl_se->dl_runtime		= 0;
2801*4882a593Smuzhiyun 	dl_se->dl_deadline		= 0;
2802*4882a593Smuzhiyun 	dl_se->dl_period		= 0;
2803*4882a593Smuzhiyun 	dl_se->flags			= 0;
2804*4882a593Smuzhiyun 	dl_se->dl_bw			= 0;
2805*4882a593Smuzhiyun 	dl_se->dl_density		= 0;
2806*4882a593Smuzhiyun 
2807*4882a593Smuzhiyun 	dl_se->dl_throttled		= 0;
2808*4882a593Smuzhiyun 	dl_se->dl_yielded		= 0;
2809*4882a593Smuzhiyun 	dl_se->dl_non_contending	= 0;
2810*4882a593Smuzhiyun 	dl_se->dl_overrun		= 0;
2811*4882a593Smuzhiyun 
2812*4882a593Smuzhiyun #ifdef CONFIG_RT_MUTEXES
2813*4882a593Smuzhiyun 	dl_se->pi_se			= dl_se;
2814*4882a593Smuzhiyun #endif
2815*4882a593Smuzhiyun }
2816*4882a593Smuzhiyun 
dl_param_changed(struct task_struct * p,const struct sched_attr * attr)2817*4882a593Smuzhiyun bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
2818*4882a593Smuzhiyun {
2819*4882a593Smuzhiyun 	struct sched_dl_entity *dl_se = &p->dl;
2820*4882a593Smuzhiyun 
2821*4882a593Smuzhiyun 	if (dl_se->dl_runtime != attr->sched_runtime ||
2822*4882a593Smuzhiyun 	    dl_se->dl_deadline != attr->sched_deadline ||
2823*4882a593Smuzhiyun 	    dl_se->dl_period != attr->sched_period ||
2824*4882a593Smuzhiyun 	    dl_se->flags != (attr->sched_flags & SCHED_DL_FLAGS))
2825*4882a593Smuzhiyun 		return true;
2826*4882a593Smuzhiyun 
2827*4882a593Smuzhiyun 	return false;
2828*4882a593Smuzhiyun }
2829*4882a593Smuzhiyun 
2830*4882a593Smuzhiyun #ifdef CONFIG_SMP
dl_cpuset_cpumask_can_shrink(const struct cpumask * cur,const struct cpumask * trial)2831*4882a593Smuzhiyun int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
2832*4882a593Smuzhiyun 				 const struct cpumask *trial)
2833*4882a593Smuzhiyun {
2834*4882a593Smuzhiyun 	int ret = 1, trial_cpus;
2835*4882a593Smuzhiyun 	struct dl_bw *cur_dl_b;
2836*4882a593Smuzhiyun 	unsigned long flags;
2837*4882a593Smuzhiyun 
2838*4882a593Smuzhiyun 	rcu_read_lock_sched();
2839*4882a593Smuzhiyun 	cur_dl_b = dl_bw_of(cpumask_any(cur));
2840*4882a593Smuzhiyun 	trial_cpus = cpumask_weight(trial);
2841*4882a593Smuzhiyun 
2842*4882a593Smuzhiyun 	raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
2843*4882a593Smuzhiyun 	if (cur_dl_b->bw != -1 &&
2844*4882a593Smuzhiyun 	    cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
2845*4882a593Smuzhiyun 		ret = 0;
2846*4882a593Smuzhiyun 	raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
2847*4882a593Smuzhiyun 	rcu_read_unlock_sched();
2848*4882a593Smuzhiyun 
2849*4882a593Smuzhiyun 	return ret;
2850*4882a593Smuzhiyun }
2851*4882a593Smuzhiyun 
dl_cpu_busy(int cpu,struct task_struct * p)2852*4882a593Smuzhiyun int dl_cpu_busy(int cpu, struct task_struct *p)
2853*4882a593Smuzhiyun {
2854*4882a593Smuzhiyun 	unsigned long flags, cap;
2855*4882a593Smuzhiyun 	struct dl_bw *dl_b;
2856*4882a593Smuzhiyun 	bool overflow;
2857*4882a593Smuzhiyun 
2858*4882a593Smuzhiyun 	rcu_read_lock_sched();
2859*4882a593Smuzhiyun 	dl_b = dl_bw_of(cpu);
2860*4882a593Smuzhiyun 	raw_spin_lock_irqsave(&dl_b->lock, flags);
2861*4882a593Smuzhiyun 	cap = dl_bw_capacity(cpu);
2862*4882a593Smuzhiyun 	overflow = __dl_overflow(dl_b, cap, 0, p ? p->dl.dl_bw : 0);
2863*4882a593Smuzhiyun 
2864*4882a593Smuzhiyun 	if (!overflow && p) {
2865*4882a593Smuzhiyun 		/*
2866*4882a593Smuzhiyun 		 * We reserve space for this task in the destination
2867*4882a593Smuzhiyun 		 * root_domain, as we can't fail after this point.
2868*4882a593Smuzhiyun 		 * We will free resources in the source root_domain
2869*4882a593Smuzhiyun 		 * later on (see set_cpus_allowed_dl()).
2870*4882a593Smuzhiyun 		 */
2871*4882a593Smuzhiyun 		__dl_add(dl_b, p->dl.dl_bw, dl_bw_cpus(cpu));
2872*4882a593Smuzhiyun 	}
2873*4882a593Smuzhiyun 
2874*4882a593Smuzhiyun 	raw_spin_unlock_irqrestore(&dl_b->lock, flags);
2875*4882a593Smuzhiyun 	rcu_read_unlock_sched();
2876*4882a593Smuzhiyun 
2877*4882a593Smuzhiyun 	return overflow ? -EBUSY : 0;
2878*4882a593Smuzhiyun }
2879*4882a593Smuzhiyun #endif
2880*4882a593Smuzhiyun 
2881*4882a593Smuzhiyun #ifdef CONFIG_SCHED_DEBUG
print_dl_stats(struct seq_file * m,int cpu)2882*4882a593Smuzhiyun void print_dl_stats(struct seq_file *m, int cpu)
2883*4882a593Smuzhiyun {
2884*4882a593Smuzhiyun 	print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
2885*4882a593Smuzhiyun }
2886*4882a593Smuzhiyun #endif /* CONFIG_SCHED_DEBUG */
2887