deadline.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  1. /*
  2. * Deadline Scheduling Class (SCHED_DEADLINE)
  3. *
  4. * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS).
  5. *
  6. * Tasks that periodically executes their instances for less than their
  7. * runtime won't miss any of their deadlines.
  8. * Tasks that are not periodic or sporadic or that tries to execute more
  9. * than their reserved bandwidth will be slowed down (and may potentially
  10. * miss some of their deadlines), and won't affect any other task.
  11. *
  12. * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>,
  13. * Juri Lelli <juri.lelli@gmail.com>,
  14. * Michael Trimarchi <michael@amarulasolutions.com>,
  15. * Fabio Checconi <fchecconi@gmail.com>
  16. */
  17. #include "sched.h"
  18. #include <linux/slab.h>
  19. struct dl_bandwidth def_dl_bandwidth;
  20. static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
  21. {
  22. return container_of(dl_se, struct task_struct, dl);
  23. }
  24. static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
  25. {
  26. return container_of(dl_rq, struct rq, dl);
  27. }
  28. static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
  29. {
  30. struct task_struct *p = dl_task_of(dl_se);
  31. struct rq *rq = task_rq(p);
  32. return &rq->dl;
  33. }
  34. static inline int on_dl_rq(struct sched_dl_entity *dl_se)
  35. {
  36. return !RB_EMPTY_NODE(&dl_se->rb_node);
  37. }
  38. static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
  39. {
  40. struct sched_dl_entity *dl_se = &p->dl;
  41. return dl_rq->rb_leftmost == &dl_se->rb_node;
  42. }
  43. void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
  44. {
  45. raw_spin_lock_init(&dl_b->dl_runtime_lock);
  46. dl_b->dl_period = period;
  47. dl_b->dl_runtime = runtime;
  48. }
  49. void init_dl_bw(struct dl_bw *dl_b)
  50. {
  51. raw_spin_lock_init(&dl_b->lock);
  52. raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
  53. if (global_rt_runtime() == RUNTIME_INF)
  54. dl_b->bw = -1;
  55. else
  56. dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
  57. raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
  58. dl_b->total_bw = 0;
  59. }
  60. void init_dl_rq(struct dl_rq *dl_rq)
  61. {
  62. dl_rq->rb_root = RB_ROOT;
  63. #ifdef CONFIG_SMP
  64. /* zero means no -deadline tasks */
  65. dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
  66. dl_rq->dl_nr_migratory = 0;
  67. dl_rq->overloaded = 0;
  68. dl_rq->pushable_dl_tasks_root = RB_ROOT;
  69. #else
  70. init_dl_bw(&dl_rq->dl_bw);
  71. #endif
  72. }
  73. #ifdef CONFIG_SMP
  74. static inline int dl_overloaded(struct rq *rq)
  75. {
  76. return atomic_read(&rq->rd->dlo_count);
  77. }
  78. static inline void dl_set_overload(struct rq *rq)
  79. {
  80. if (!rq->online)
  81. return;
  82. cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
  83. /*
  84. * Must be visible before the overload count is
  85. * set (as in sched_rt.c).
  86. *
  87. * Matched by the barrier in pull_dl_task().
  88. */
  89. smp_wmb();
  90. atomic_inc(&rq->rd->dlo_count);
  91. }
  92. static inline void dl_clear_overload(struct rq *rq)
  93. {
  94. if (!rq->online)
  95. return;
  96. atomic_dec(&rq->rd->dlo_count);
  97. cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
  98. }
  99. static void update_dl_migration(struct dl_rq *dl_rq)
  100. {
  101. if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
  102. if (!dl_rq->overloaded) {
  103. dl_set_overload(rq_of_dl_rq(dl_rq));
  104. dl_rq->overloaded = 1;
  105. }
  106. } else if (dl_rq->overloaded) {
  107. dl_clear_overload(rq_of_dl_rq(dl_rq));
  108. dl_rq->overloaded = 0;
  109. }
  110. }
  111. static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  112. {
  113. struct task_struct *p = dl_task_of(dl_se);
  114. if (tsk_nr_cpus_allowed(p) > 1)
  115. dl_rq->dl_nr_migratory++;
  116. update_dl_migration(dl_rq);
  117. }
  118. static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  119. {
  120. struct task_struct *p = dl_task_of(dl_se);
  121. if (tsk_nr_cpus_allowed(p) > 1)
  122. dl_rq->dl_nr_migratory--;
  123. update_dl_migration(dl_rq);
  124. }
  125. /*
  126. * The list of pushable -deadline task is not a plist, like in
  127. * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
  128. */
  129. static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  130. {
  131. struct dl_rq *dl_rq = &rq->dl;
  132. struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node;
  133. struct rb_node *parent = NULL;
  134. struct task_struct *entry;
  135. int leftmost = 1;
  136. BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
  137. while (*link) {
  138. parent = *link;
  139. entry = rb_entry(parent, struct task_struct,
  140. pushable_dl_tasks);
  141. if (dl_entity_preempt(&p->dl, &entry->dl))
  142. link = &parent->rb_left;
  143. else {
  144. link = &parent->rb_right;
  145. leftmost = 0;
  146. }
  147. }
  148. if (leftmost) {
  149. dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
  150. dl_rq->earliest_dl.next = p->dl.deadline;
  151. }
  152. rb_link_node(&p->pushable_dl_tasks, parent, link);
  153. rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
  154. }
  155. static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  156. {
  157. struct dl_rq *dl_rq = &rq->dl;
  158. if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
  159. return;
  160. if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
  161. struct rb_node *next_node;
  162. next_node = rb_next(&p->pushable_dl_tasks);
  163. dl_rq->pushable_dl_tasks_leftmost = next_node;
  164. if (next_node) {
  165. dl_rq->earliest_dl.next = rb_entry(next_node,
  166. struct task_struct, pushable_dl_tasks)->dl.deadline;
  167. }
  168. }
  169. rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
  170. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  171. }
  172. static inline int has_pushable_dl_tasks(struct rq *rq)
  173. {
  174. return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
  175. }
  176. static int push_dl_task(struct rq *rq);
  177. static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
  178. {
  179. return dl_task(prev);
  180. }
  181. static DEFINE_PER_CPU(struct callback_head, dl_push_head);
  182. static DEFINE_PER_CPU(struct callback_head, dl_pull_head);
  183. static void push_dl_tasks(struct rq *);
  184. static void pull_dl_task(struct rq *);
  185. static inline void queue_push_tasks(struct rq *rq)
  186. {
  187. if (!has_pushable_dl_tasks(rq))
  188. return;
  189. queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
  190. }
  191. static inline void queue_pull_task(struct rq *rq)
  192. {
  193. queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
  194. }
  195. static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
  196. static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
  197. {
  198. struct rq *later_rq = NULL;
  199. later_rq = find_lock_later_rq(p, rq);
  200. if (!later_rq) {
  201. int cpu;
  202. /*
  203. * If we cannot preempt any rq, fall back to pick any
  204. * online cpu.
  205. */
  206. cpu = cpumask_any_and(cpu_active_mask, tsk_cpus_allowed(p));
  207. if (cpu >= nr_cpu_ids) {
  208. /*
  209. * Fail to find any suitable cpu.
  210. * The task will never come back!
  211. */
  212. BUG_ON(dl_bandwidth_enabled());
  213. /*
  214. * If admission control is disabled we
  215. * try a little harder to let the task
  216. * run.
  217. */
  218. cpu = cpumask_any(cpu_active_mask);
  219. }
  220. later_rq = cpu_rq(cpu);
  221. double_lock_balance(rq, later_rq);
  222. }
  223. set_task_cpu(p, later_rq->cpu);
  224. double_unlock_balance(later_rq, rq);
  225. return later_rq;
  226. }
  227. #else
  228. static inline
  229. void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  230. {
  231. }
  232. static inline
  233. void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  234. {
  235. }
  236. static inline
  237. void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  238. {
  239. }
  240. static inline
  241. void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  242. {
  243. }
  244. static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
  245. {
  246. return false;
  247. }
  248. static inline void pull_dl_task(struct rq *rq)
  249. {
  250. }
  251. static inline void queue_push_tasks(struct rq *rq)
  252. {
  253. }
  254. static inline void queue_pull_task(struct rq *rq)
  255. {
  256. }
  257. #endif /* CONFIG_SMP */
  258. static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  259. static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  260. static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
  261. int flags);
  262. /*
  263. * We are being explicitly informed that a new instance is starting,
  264. * and this means that:
  265. * - the absolute deadline of the entity has to be placed at
  266. * current time + relative deadline;
  267. * - the runtime of the entity has to be set to the maximum value.
  268. *
  269. * The capability of specifying such event is useful whenever a -deadline
  270. * entity wants to (try to!) synchronize its behaviour with the scheduler's
  271. * one, and to (try to!) reconcile itself with its own scheduling
  272. * parameters.
  273. */
  274. static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
  275. {
  276. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  277. struct rq *rq = rq_of_dl_rq(dl_rq);
  278. WARN_ON(dl_se->dl_boosted);
  279. WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
  280. /*
  281. * We are racing with the deadline timer. So, do nothing because
  282. * the deadline timer handler will take care of properly recharging
  283. * the runtime and postponing the deadline
  284. */
  285. if (dl_se->dl_throttled)
  286. return;
  287. /*
  288. * We use the regular wall clock time to set deadlines in the
  289. * future; in fact, we must consider execution overheads (time
  290. * spent on hardirq context, etc.).
  291. */
  292. dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
  293. dl_se->runtime = dl_se->dl_runtime;
  294. }
  295. /*
  296. * Pure Earliest Deadline First (EDF) scheduling does not deal with the
  297. * possibility of a entity lasting more than what it declared, and thus
  298. * exhausting its runtime.
  299. *
  300. * Here we are interested in making runtime overrun possible, but we do
  301. * not want a entity which is misbehaving to affect the scheduling of all
  302. * other entities.
  303. * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
  304. * is used, in order to confine each entity within its own bandwidth.
  305. *
  306. * This function deals exactly with that, and ensures that when the runtime
  307. * of a entity is replenished, its deadline is also postponed. That ensures
  308. * the overrunning entity can't interfere with other entity in the system and
  309. * can't make them miss their deadlines. Reasons why this kind of overruns
  310. * could happen are, typically, a entity voluntarily trying to overcome its
  311. * runtime, or it just underestimated it during sched_setattr().
  312. */
  313. static void replenish_dl_entity(struct sched_dl_entity *dl_se,
  314. struct sched_dl_entity *pi_se)
  315. {
  316. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  317. struct rq *rq = rq_of_dl_rq(dl_rq);
  318. BUG_ON(pi_se->dl_runtime <= 0);
  319. /*
  320. * This could be the case for a !-dl task that is boosted.
  321. * Just go with full inherited parameters.
  322. */
  323. if (dl_se->dl_deadline == 0) {
  324. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  325. dl_se->runtime = pi_se->dl_runtime;
  326. }
  327. if (dl_se->dl_yielded && dl_se->runtime > 0)
  328. dl_se->runtime = 0;
  329. /*
  330. * We keep moving the deadline away until we get some
  331. * available runtime for the entity. This ensures correct
  332. * handling of situations where the runtime overrun is
  333. * arbitrary large.
  334. */
  335. while (dl_se->runtime <= 0) {
  336. dl_se->deadline += pi_se->dl_period;
  337. dl_se->runtime += pi_se->dl_runtime;
  338. }
  339. /*
  340. * At this point, the deadline really should be "in
  341. * the future" with respect to rq->clock. If it's
  342. * not, we are, for some reason, lagging too much!
  343. * Anyway, after having warn userspace abut that,
  344. * we still try to keep the things running by
  345. * resetting the deadline and the budget of the
  346. * entity.
  347. */
  348. if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
  349. printk_deferred_once("sched: DL replenish lagged too much\n");
  350. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  351. dl_se->runtime = pi_se->dl_runtime;
  352. }
  353. if (dl_se->dl_yielded)
  354. dl_se->dl_yielded = 0;
  355. if (dl_se->dl_throttled)
  356. dl_se->dl_throttled = 0;
  357. }
  358. /*
  359. * Here we check if --at time t-- an entity (which is probably being
  360. * [re]activated or, in general, enqueued) can use its remaining runtime
  361. * and its current deadline _without_ exceeding the bandwidth it is
  362. * assigned (function returns true if it can't). We are in fact applying
  363. * one of the CBS rules: when a task wakes up, if the residual runtime
  364. * over residual deadline fits within the allocated bandwidth, then we
  365. * can keep the current (absolute) deadline and residual budget without
  366. * disrupting the schedulability of the system. Otherwise, we should
  367. * refill the runtime and set the deadline a period in the future,
  368. * because keeping the current (absolute) deadline of the task would
  369. * result in breaking guarantees promised to other tasks (refer to
  370. * Documentation/scheduler/sched-deadline.txt for more informations).
  371. *
  372. * This function returns true if:
  373. *
  374. * runtime / (deadline - t) > dl_runtime / dl_deadline ,
  375. *
  376. * IOW we can't recycle current parameters.
  377. *
  378. * Notice that the bandwidth check is done against the deadline. For
  379. * task with deadline equal to period this is the same of using
  380. * dl_period instead of dl_deadline in the equation above.
  381. */
  382. static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
  383. struct sched_dl_entity *pi_se, u64 t)
  384. {
  385. u64 left, right;
  386. /*
  387. * left and right are the two sides of the equation above,
  388. * after a bit of shuffling to use multiplications instead
  389. * of divisions.
  390. *
  391. * Note that none of the time values involved in the two
  392. * multiplications are absolute: dl_deadline and dl_runtime
  393. * are the relative deadline and the maximum runtime of each
  394. * instance, runtime is the runtime left for the last instance
  395. * and (deadline - t), since t is rq->clock, is the time left
  396. * to the (absolute) deadline. Even if overflowing the u64 type
  397. * is very unlikely to occur in both cases, here we scale down
  398. * as we want to avoid that risk at all. Scaling down by 10
  399. * means that we reduce granularity to 1us. We are fine with it,
  400. * since this is only a true/false check and, anyway, thinking
  401. * of anything below microseconds resolution is actually fiction
  402. * (but still we want to give the user that illusion >;).
  403. */
  404. left = (pi_se->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
  405. right = ((dl_se->deadline - t) >> DL_SCALE) *
  406. (pi_se->dl_runtime >> DL_SCALE);
  407. return dl_time_before(right, left);
  408. }
  409. /*
  410. * Revised wakeup rule [1]: For self-suspending tasks, rather then
  411. * re-initializing task's runtime and deadline, the revised wakeup
  412. * rule adjusts the task's runtime to avoid the task to overrun its
  413. * density.
  414. *
  415. * Reasoning: a task may overrun the density if:
  416. * runtime / (deadline - t) > dl_runtime / dl_deadline
  417. *
  418. * Therefore, runtime can be adjusted to:
  419. * runtime = (dl_runtime / dl_deadline) * (deadline - t)
  420. *
  421. * In such way that runtime will be equal to the maximum density
  422. * the task can use without breaking any rule.
  423. *
  424. * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
  425. * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
  426. */
  427. static void
  428. update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq)
  429. {
  430. u64 laxity = dl_se->deadline - rq_clock(rq);
  431. /*
  432. * If the task has deadline < period, and the deadline is in the past,
  433. * it should already be throttled before this check.
  434. *
  435. * See update_dl_entity() comments for further details.
  436. */
  437. WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq)));
  438. dl_se->runtime = (dl_se->dl_density * laxity) >> 20;
  439. }
  440. /*
  441. * Regarding the deadline, a task with implicit deadline has a relative
  442. * deadline == relative period. A task with constrained deadline has a
  443. * relative deadline <= relative period.
  444. *
  445. * We support constrained deadline tasks. However, there are some restrictions
  446. * applied only for tasks which do not have an implicit deadline. See
  447. * update_dl_entity() to know more about such restrictions.
  448. *
  449. * The dl_is_implicit() returns true if the task has an implicit deadline.
  450. */
  451. static inline bool dl_is_implicit(struct sched_dl_entity *dl_se)
  452. {
  453. return dl_se->dl_deadline == dl_se->dl_period;
  454. }
  455. /*
  456. * When a deadline entity is placed in the runqueue, its runtime and deadline
  457. * might need to be updated. This is done by a CBS wake up rule. There are two
  458. * different rules: 1) the original CBS; and 2) the Revisited CBS.
  459. *
  460. * When the task is starting a new period, the Original CBS is used. In this
  461. * case, the runtime is replenished and a new absolute deadline is set.
  462. *
  463. * When a task is queued before the begin of the next period, using the
  464. * remaining runtime and deadline could make the entity to overflow, see
  465. * dl_entity_overflow() to find more about runtime overflow. When such case
  466. * is detected, the runtime and deadline need to be updated.
  467. *
  468. * If the task has an implicit deadline, i.e., deadline == period, the Original
  469. * CBS is applied. the runtime is replenished and a new absolute deadline is
  470. * set, as in the previous cases.
  471. *
  472. * However, the Original CBS does not work properly for tasks with
  473. * deadline < period, which are said to have a constrained deadline. By
  474. * applying the Original CBS, a constrained deadline task would be able to run
  475. * runtime/deadline in a period. With deadline < period, the task would
  476. * overrun the runtime/period allowed bandwidth, breaking the admission test.
  477. *
  478. * In order to prevent this misbehave, the Revisited CBS is used for
  479. * constrained deadline tasks when a runtime overflow is detected. In the
  480. * Revisited CBS, rather than replenishing & setting a new absolute deadline,
  481. * the remaining runtime of the task is reduced to avoid runtime overflow.
  482. * Please refer to the comments update_dl_revised_wakeup() function to find
  483. * more about the Revised CBS rule.
  484. */
  485. static void update_dl_entity(struct sched_dl_entity *dl_se,
  486. struct sched_dl_entity *pi_se)
  487. {
  488. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  489. struct rq *rq = rq_of_dl_rq(dl_rq);
  490. if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
  491. dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
  492. if (unlikely(!dl_is_implicit(dl_se) &&
  493. !dl_time_before(dl_se->deadline, rq_clock(rq)) &&
  494. !dl_se->dl_boosted)){
  495. update_dl_revised_wakeup(dl_se, rq);
  496. return;
  497. }
  498. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  499. dl_se->runtime = pi_se->dl_runtime;
  500. }
  501. }
  502. static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
  503. {
  504. return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
  505. }
  506. /*
  507. * If the entity depleted all its runtime, and if we want it to sleep
  508. * while waiting for some new execution time to become available, we
  509. * set the bandwidth replenishment timer to the replenishment instant
  510. * and try to activate it.
  511. *
  512. * Notice that it is important for the caller to know if the timer
  513. * actually started or not (i.e., the replenishment instant is in
  514. * the future or in the past).
  515. */
  516. static int start_dl_timer(struct task_struct *p)
  517. {
  518. struct sched_dl_entity *dl_se = &p->dl;
  519. struct hrtimer *timer = &dl_se->dl_timer;
  520. struct rq *rq = task_rq(p);
  521. ktime_t now, act;
  522. s64 delta;
  523. lockdep_assert_held(&rq->lock);
  524. /*
  525. * We want the timer to fire at the deadline, but considering
  526. * that it is actually coming from rq->clock and not from
  527. * hrtimer's time base reading.
  528. */
  529. act = ns_to_ktime(dl_next_period(dl_se));
  530. now = hrtimer_cb_get_time(timer);
  531. delta = ktime_to_ns(now) - rq_clock(rq);
  532. act = ktime_add_ns(act, delta);
  533. /*
  534. * If the expiry time already passed, e.g., because the value
  535. * chosen as the deadline is too small, don't even try to
  536. * start the timer in the past!
  537. */
  538. if (ktime_us_delta(act, now) < 0)
  539. return 0;
  540. /*
  541. * !enqueued will guarantee another callback; even if one is already in
  542. * progress. This ensures a balanced {get,put}_task_struct().
  543. *
  544. * The race against __run_timer() clearing the enqueued state is
  545. * harmless because we're holding task_rq()->lock, therefore the timer
  546. * expiring after we've done the check will wait on its task_rq_lock()
  547. * and observe our state.
  548. */
  549. if (!hrtimer_is_queued(timer)) {
  550. get_task_struct(p);
  551. hrtimer_start(timer, act, HRTIMER_MODE_ABS);
  552. }
  553. return 1;
  554. }
  555. /*
  556. * This is the bandwidth enforcement timer callback. If here, we know
  557. * a task is not on its dl_rq, since the fact that the timer was running
  558. * means the task is throttled and needs a runtime replenishment.
  559. *
  560. * However, what we actually do depends on the fact the task is active,
  561. * (it is on its rq) or has been removed from there by a call to
  562. * dequeue_task_dl(). In the former case we must issue the runtime
  563. * replenishment and add the task back to the dl_rq; in the latter, we just
  564. * do nothing but clearing dl_throttled, so that runtime and deadline
  565. * updating (and the queueing back to dl_rq) will be done by the
  566. * next call to enqueue_task_dl().
  567. */
  568. static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
  569. {
  570. struct sched_dl_entity *dl_se = container_of(timer,
  571. struct sched_dl_entity,
  572. dl_timer);
  573. struct task_struct *p = dl_task_of(dl_se);
  574. struct rq_flags rf;
  575. struct rq *rq;
  576. rq = task_rq_lock(p, &rf);
  577. /*
  578. * The task might have changed its scheduling policy to something
  579. * different than SCHED_DEADLINE (through switched_fromd_dl()).
  580. */
  581. if (!dl_task(p)) {
  582. __dl_clear_params(p);
  583. goto unlock;
  584. }
  585. /*
  586. * The task might have been boosted by someone else and might be in the
  587. * boosting/deboosting path, its not throttled.
  588. */
  589. if (dl_se->dl_boosted)
  590. goto unlock;
  591. /*
  592. * Spurious timer due to start_dl_timer() race; or we already received
  593. * a replenishment from rt_mutex_setprio().
  594. */
  595. if (!dl_se->dl_throttled)
  596. goto unlock;
  597. sched_clock_tick();
  598. update_rq_clock(rq);
  599. /*
  600. * If the throttle happened during sched-out; like:
  601. *
  602. * schedule()
  603. * deactivate_task()
  604. * dequeue_task_dl()
  605. * update_curr_dl()
  606. * start_dl_timer()
  607. * __dequeue_task_dl()
  608. * prev->on_rq = 0;
  609. *
  610. * We can be both throttled and !queued. Replenish the counter
  611. * but do not enqueue -- wait for our wakeup to do that.
  612. */
  613. if (!task_on_rq_queued(p)) {
  614. replenish_dl_entity(dl_se, dl_se);
  615. goto unlock;
  616. }
  617. #ifdef CONFIG_SMP
  618. if (unlikely(!rq->online)) {
  619. /*
  620. * If the runqueue is no longer available, migrate the
  621. * task elsewhere. This necessarily changes rq.
  622. */
  623. lockdep_unpin_lock(&rq->lock, rf.cookie);
  624. rq = dl_task_offline_migration(rq, p);
  625. rf.cookie = lockdep_pin_lock(&rq->lock);
  626. update_rq_clock(rq);
  627. /*
  628. * Now that the task has been migrated to the new RQ and we
  629. * have that locked, proceed as normal and enqueue the task
  630. * there.
  631. */
  632. }
  633. #endif
  634. enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
  635. if (dl_task(rq->curr))
  636. check_preempt_curr_dl(rq, p, 0);
  637. else
  638. resched_curr(rq);
  639. #ifdef CONFIG_SMP
  640. /*
  641. * Queueing this task back might have overloaded rq, check if we need
  642. * to kick someone away.
  643. */
  644. if (has_pushable_dl_tasks(rq)) {
  645. /*
  646. * Nothing relies on rq->lock after this, so its safe to drop
  647. * rq->lock.
  648. */
  649. lockdep_unpin_lock(&rq->lock, rf.cookie);
  650. push_dl_task(rq);
  651. lockdep_repin_lock(&rq->lock, rf.cookie);
  652. }
  653. #endif
  654. unlock:
  655. task_rq_unlock(rq, p, &rf);
  656. /*
  657. * This can free the task_struct, including this hrtimer, do not touch
  658. * anything related to that after this.
  659. */
  660. put_task_struct(p);
  661. return HRTIMER_NORESTART;
  662. }
  663. void init_dl_task_timer(struct sched_dl_entity *dl_se)
  664. {
  665. struct hrtimer *timer = &dl_se->dl_timer;
  666. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  667. timer->function = dl_task_timer;
  668. }
  669. /*
  670. * During the activation, CBS checks if it can reuse the current task's
  671. * runtime and period. If the deadline of the task is in the past, CBS
  672. * cannot use the runtime, and so it replenishes the task. This rule
  673. * works fine for implicit deadline tasks (deadline == period), and the
  674. * CBS was designed for implicit deadline tasks. However, a task with
  675. * constrained deadline (deadine < period) might be awakened after the
  676. * deadline, but before the next period. In this case, replenishing the
  677. * task would allow it to run for runtime / deadline. As in this case
  678. * deadline < period, CBS enables a task to run for more than the
  679. * runtime / period. In a very loaded system, this can cause a domino
  680. * effect, making other tasks miss their deadlines.
  681. *
  682. * To avoid this problem, in the activation of a constrained deadline
  683. * task after the deadline but before the next period, throttle the
  684. * task and set the replenishing timer to the begin of the next period,
  685. * unless it is boosted.
  686. */
  687. static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
  688. {
  689. struct task_struct *p = dl_task_of(dl_se);
  690. struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se));
  691. if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
  692. dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
  693. if (unlikely(dl_se->dl_boosted || !start_dl_timer(p)))
  694. return;
  695. dl_se->dl_throttled = 1;
  696. if (dl_se->runtime > 0)
  697. dl_se->runtime = 0;
  698. }
  699. }
  700. static
  701. int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
  702. {
  703. return (dl_se->runtime <= 0);
  704. }
  705. extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
  706. /*
  707. * Update the current task's runtime statistics (provided it is still
  708. * a -deadline task and has not been removed from the dl_rq).
  709. */
  710. static void update_curr_dl(struct rq *rq)
  711. {
  712. struct task_struct *curr = rq->curr;
  713. struct sched_dl_entity *dl_se = &curr->dl;
  714. u64 delta_exec;
  715. if (!dl_task(curr) || !on_dl_rq(dl_se))
  716. return;
  717. /*
  718. * Consumed budget is computed considering the time as
  719. * observed by schedulable tasks (excluding time spent
  720. * in hardirq context, etc.). Deadlines are instead
  721. * computed using hard walltime. This seems to be the more
  722. * natural solution, but the full ramifications of this
  723. * approach need further study.
  724. */
  725. delta_exec = rq_clock_task(rq) - curr->se.exec_start;
  726. if (unlikely((s64)delta_exec <= 0)) {
  727. if (unlikely(dl_se->dl_yielded))
  728. goto throttle;
  729. return;
  730. }
  731. /* kick cpufreq (see the comment in kernel/sched/sched.h). */
  732. cpufreq_update_this_cpu(rq, SCHED_CPUFREQ_DL);
  733. schedstat_set(curr->se.statistics.exec_max,
  734. max(curr->se.statistics.exec_max, delta_exec));
  735. curr->se.sum_exec_runtime += delta_exec;
  736. account_group_exec_runtime(curr, delta_exec);
  737. curr->se.exec_start = rq_clock_task(rq);
  738. cpuacct_charge(curr, delta_exec);
  739. sched_rt_avg_update(rq, delta_exec);
  740. dl_se->runtime -= delta_exec;
  741. throttle:
  742. if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
  743. dl_se->dl_throttled = 1;
  744. __dequeue_task_dl(rq, curr, 0);
  745. if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr)))
  746. enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
  747. if (!is_leftmost(curr, &rq->dl))
  748. resched_curr(rq);
  749. }
  750. /*
  751. * Because -- for now -- we share the rt bandwidth, we need to
  752. * account our runtime there too, otherwise actual rt tasks
  753. * would be able to exceed the shared quota.
  754. *
  755. * Account to the root rt group for now.
  756. *
  757. * The solution we're working towards is having the RT groups scheduled
  758. * using deadline servers -- however there's a few nasties to figure
  759. * out before that can happen.
  760. */
  761. if (rt_bandwidth_enabled()) {
  762. struct rt_rq *rt_rq = &rq->rt;
  763. raw_spin_lock(&rt_rq->rt_runtime_lock);
  764. /*
  765. * We'll let actual RT tasks worry about the overflow here, we
  766. * have our own CBS to keep us inline; only account when RT
  767. * bandwidth is relevant.
  768. */
  769. if (sched_rt_bandwidth_account(rt_rq))
  770. rt_rq->rt_time += delta_exec;
  771. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  772. }
  773. }
  774. #ifdef CONFIG_SMP
  775. static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  776. {
  777. struct rq *rq = rq_of_dl_rq(dl_rq);
  778. if (dl_rq->earliest_dl.curr == 0 ||
  779. dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
  780. dl_rq->earliest_dl.curr = deadline;
  781. cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
  782. }
  783. }
  784. static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  785. {
  786. struct rq *rq = rq_of_dl_rq(dl_rq);
  787. /*
  788. * Since we may have removed our earliest (and/or next earliest)
  789. * task we must recompute them.
  790. */
  791. if (!dl_rq->dl_nr_running) {
  792. dl_rq->earliest_dl.curr = 0;
  793. dl_rq->earliest_dl.next = 0;
  794. cpudl_clear(&rq->rd->cpudl, rq->cpu);
  795. } else {
  796. struct rb_node *leftmost = dl_rq->rb_leftmost;
  797. struct sched_dl_entity *entry;
  798. entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
  799. dl_rq->earliest_dl.curr = entry->deadline;
  800. cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
  801. }
  802. }
  803. #else
  804. static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  805. static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  806. #endif /* CONFIG_SMP */
  807. static inline
  808. void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  809. {
  810. int prio = dl_task_of(dl_se)->prio;
  811. u64 deadline = dl_se->deadline;
  812. WARN_ON(!dl_prio(prio));
  813. dl_rq->dl_nr_running++;
  814. add_nr_running(rq_of_dl_rq(dl_rq), 1);
  815. inc_dl_deadline(dl_rq, deadline);
  816. inc_dl_migration(dl_se, dl_rq);
  817. }
  818. static inline
  819. void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  820. {
  821. int prio = dl_task_of(dl_se)->prio;
  822. WARN_ON(!dl_prio(prio));
  823. WARN_ON(!dl_rq->dl_nr_running);
  824. dl_rq->dl_nr_running--;
  825. sub_nr_running(rq_of_dl_rq(dl_rq), 1);
  826. dec_dl_deadline(dl_rq, dl_se->deadline);
  827. dec_dl_migration(dl_se, dl_rq);
  828. }
  829. static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
  830. {
  831. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  832. struct rb_node **link = &dl_rq->rb_root.rb_node;
  833. struct rb_node *parent = NULL;
  834. struct sched_dl_entity *entry;
  835. int leftmost = 1;
  836. BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
  837. while (*link) {
  838. parent = *link;
  839. entry = rb_entry(parent, struct sched_dl_entity, rb_node);
  840. if (dl_time_before(dl_se->deadline, entry->deadline))
  841. link = &parent->rb_left;
  842. else {
  843. link = &parent->rb_right;
  844. leftmost = 0;
  845. }
  846. }
  847. if (leftmost)
  848. dl_rq->rb_leftmost = &dl_se->rb_node;
  849. rb_link_node(&dl_se->rb_node, parent, link);
  850. rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
  851. inc_dl_tasks(dl_se, dl_rq);
  852. }
  853. static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
  854. {
  855. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  856. if (RB_EMPTY_NODE(&dl_se->rb_node))
  857. return;
  858. if (dl_rq->rb_leftmost == &dl_se->rb_node) {
  859. struct rb_node *next_node;
  860. next_node = rb_next(&dl_se->rb_node);
  861. dl_rq->rb_leftmost = next_node;
  862. }
  863. rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
  864. RB_CLEAR_NODE(&dl_se->rb_node);
  865. dec_dl_tasks(dl_se, dl_rq);
  866. }
  867. static void
  868. enqueue_dl_entity(struct sched_dl_entity *dl_se,
  869. struct sched_dl_entity *pi_se, int flags)
  870. {
  871. BUG_ON(on_dl_rq(dl_se));
  872. /*
  873. * If this is a wakeup or a new instance, the scheduling
  874. * parameters of the task might need updating. Otherwise,
  875. * we want a replenishment of its runtime.
  876. */
  877. if (flags & ENQUEUE_WAKEUP)
  878. update_dl_entity(dl_se, pi_se);
  879. else if (flags & ENQUEUE_REPLENISH)
  880. replenish_dl_entity(dl_se, pi_se);
  881. __enqueue_dl_entity(dl_se);
  882. }
  883. static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
  884. {
  885. __dequeue_dl_entity(dl_se);
  886. }
  887. static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  888. {
  889. struct task_struct *pi_task = rt_mutex_get_top_task(p);
  890. struct sched_dl_entity *pi_se = &p->dl;
  891. /*
  892. * Use the scheduling parameters of the top pi-waiter
  893. * task if we have one and its (absolute) deadline is
  894. * smaller than our one... OTW we keep our runtime and
  895. * deadline.
  896. */
  897. if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) {
  898. pi_se = &pi_task->dl;
  899. } else if (!dl_prio(p->normal_prio)) {
  900. /*
  901. * Special case in which we have a !SCHED_DEADLINE task
  902. * that is going to be deboosted, but exceedes its
  903. * runtime while doing so. No point in replenishing
  904. * it, as it's going to return back to its original
  905. * scheduling class after this.
  906. */
  907. BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
  908. return;
  909. }
  910. /*
  911. * Check if a constrained deadline task was activated
  912. * after the deadline but before the next period.
  913. * If that is the case, the task will be throttled and
  914. * the replenishment timer will be set to the next period.
  915. */
  916. if (!p->dl.dl_throttled && !dl_is_implicit(&p->dl))
  917. dl_check_constrained_dl(&p->dl);
  918. /*
  919. * If p is throttled, we do nothing. In fact, if it exhausted
  920. * its budget it needs a replenishment and, since it now is on
  921. * its rq, the bandwidth timer callback (which clearly has not
  922. * run yet) will take care of this.
  923. */
  924. if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH))
  925. return;
  926. enqueue_dl_entity(&p->dl, pi_se, flags);
  927. if (!task_current(rq, p) && tsk_nr_cpus_allowed(p) > 1)
  928. enqueue_pushable_dl_task(rq, p);
  929. }
  930. static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  931. {
  932. dequeue_dl_entity(&p->dl);
  933. dequeue_pushable_dl_task(rq, p);
  934. }
  935. static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  936. {
  937. update_curr_dl(rq);
  938. __dequeue_task_dl(rq, p, flags);
  939. }
  940. /*
  941. * Yield task semantic for -deadline tasks is:
  942. *
  943. * get off from the CPU until our next instance, with
  944. * a new runtime. This is of little use now, since we
  945. * don't have a bandwidth reclaiming mechanism. Anyway,
  946. * bandwidth reclaiming is planned for the future, and
  947. * yield_task_dl will indicate that some spare budget
  948. * is available for other task instances to use it.
  949. */
  950. static void yield_task_dl(struct rq *rq)
  951. {
  952. /*
  953. * We make the task go to sleep until its current deadline by
  954. * forcing its runtime to zero. This way, update_curr_dl() stops
  955. * it and the bandwidth timer will wake it up and will give it
  956. * new scheduling parameters (thanks to dl_yielded=1).
  957. */
  958. rq->curr->dl.dl_yielded = 1;
  959. update_rq_clock(rq);
  960. update_curr_dl(rq);
  961. /*
  962. * Tell update_rq_clock() that we've just updated,
  963. * so we don't do microscopic update in schedule()
  964. * and double the fastpath cost.
  965. */
  966. rq_clock_skip_update(rq, true);
  967. }
  968. #ifdef CONFIG_SMP
  969. static int find_later_rq(struct task_struct *task);
  970. static int
  971. select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
  972. {
  973. struct task_struct *curr;
  974. struct rq *rq;
  975. if (sd_flag != SD_BALANCE_WAKE)
  976. goto out;
  977. rq = cpu_rq(cpu);
  978. rcu_read_lock();
  979. curr = READ_ONCE(rq->curr); /* unlocked access */
  980. /*
  981. * If we are dealing with a -deadline task, we must
  982. * decide where to wake it up.
  983. * If it has a later deadline and the current task
  984. * on this rq can't move (provided the waking task
  985. * can!) we prefer to send it somewhere else. On the
  986. * other hand, if it has a shorter deadline, we
  987. * try to make it stay here, it might be important.
  988. */
  989. if (unlikely(dl_task(curr)) &&
  990. (tsk_nr_cpus_allowed(curr) < 2 ||
  991. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  992. (tsk_nr_cpus_allowed(p) > 1)) {
  993. int target = find_later_rq(p);
  994. if (target != -1 &&
  995. (dl_time_before(p->dl.deadline,
  996. cpu_rq(target)->dl.earliest_dl.curr) ||
  997. (cpu_rq(target)->dl.dl_nr_running == 0)))
  998. cpu = target;
  999. }
  1000. rcu_read_unlock();
  1001. out:
  1002. return cpu;
  1003. }
  1004. static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  1005. {
  1006. /*
  1007. * Current can't be migrated, useless to reschedule,
  1008. * let's hope p can move out.
  1009. */
  1010. if (tsk_nr_cpus_allowed(rq->curr) == 1 ||
  1011. cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
  1012. return;
  1013. /*
  1014. * p is migratable, so let's not schedule it and
  1015. * see if it is pushed or pulled somewhere else.
  1016. */
  1017. if (tsk_nr_cpus_allowed(p) != 1 &&
  1018. cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
  1019. return;
  1020. resched_curr(rq);
  1021. }
  1022. #endif /* CONFIG_SMP */
  1023. /*
  1024. * Only called when both the current and waking task are -deadline
  1025. * tasks.
  1026. */
  1027. static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
  1028. int flags)
  1029. {
  1030. if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
  1031. resched_curr(rq);
  1032. return;
  1033. }
  1034. #ifdef CONFIG_SMP
  1035. /*
  1036. * In the unlikely case current and p have the same deadline
  1037. * let us try to decide what's the best thing to do...
  1038. */
  1039. if ((p->dl.deadline == rq->curr->dl.deadline) &&
  1040. !test_tsk_need_resched(rq->curr))
  1041. check_preempt_equal_dl(rq, p);
  1042. #endif /* CONFIG_SMP */
  1043. }
  1044. #ifdef CONFIG_SCHED_HRTICK
  1045. static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
  1046. {
  1047. hrtick_start(rq, p->dl.runtime);
  1048. }
  1049. #else /* !CONFIG_SCHED_HRTICK */
  1050. static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
  1051. {
  1052. }
  1053. #endif
  1054. static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
  1055. struct dl_rq *dl_rq)
  1056. {
  1057. struct rb_node *left = dl_rq->rb_leftmost;
  1058. if (!left)
  1059. return NULL;
  1060. return rb_entry(left, struct sched_dl_entity, rb_node);
  1061. }
  1062. struct task_struct *
  1063. pick_next_task_dl(struct rq *rq, struct task_struct *prev, struct pin_cookie cookie)
  1064. {
  1065. struct sched_dl_entity *dl_se;
  1066. struct task_struct *p;
  1067. struct dl_rq *dl_rq;
  1068. dl_rq = &rq->dl;
  1069. if (need_pull_dl_task(rq, prev)) {
  1070. /*
  1071. * This is OK, because current is on_cpu, which avoids it being
  1072. * picked for load-balance and preemption/IRQs are still
  1073. * disabled avoiding further scheduler activity on it and we're
  1074. * being very careful to re-start the picking loop.
  1075. */
  1076. lockdep_unpin_lock(&rq->lock, cookie);
  1077. pull_dl_task(rq);
  1078. lockdep_repin_lock(&rq->lock, cookie);
  1079. /*
  1080. * pull_rt_task() can drop (and re-acquire) rq->lock; this
  1081. * means a stop task can slip in, in which case we need to
  1082. * re-start task selection.
  1083. */
  1084. if (rq->stop && task_on_rq_queued(rq->stop))
  1085. return RETRY_TASK;
  1086. }
  1087. /*
  1088. * When prev is DL, we may throttle it in put_prev_task().
  1089. * So, we update time before we check for dl_nr_running.
  1090. */
  1091. if (prev->sched_class == &dl_sched_class)
  1092. update_curr_dl(rq);
  1093. if (unlikely(!dl_rq->dl_nr_running))
  1094. return NULL;
  1095. put_prev_task(rq, prev);
  1096. dl_se = pick_next_dl_entity(rq, dl_rq);
  1097. BUG_ON(!dl_se);
  1098. p = dl_task_of(dl_se);
  1099. p->se.exec_start = rq_clock_task(rq);
  1100. /* Running task will never be pushed. */
  1101. dequeue_pushable_dl_task(rq, p);
  1102. if (hrtick_enabled(rq))
  1103. start_hrtick_dl(rq, p);
  1104. queue_push_tasks(rq);
  1105. return p;
  1106. }
  1107. static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
  1108. {
  1109. update_curr_dl(rq);
  1110. if (on_dl_rq(&p->dl) && tsk_nr_cpus_allowed(p) > 1)
  1111. enqueue_pushable_dl_task(rq, p);
  1112. }
  1113. static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
  1114. {
  1115. update_curr_dl(rq);
  1116. /*
  1117. * Even when we have runtime, update_curr_dl() might have resulted in us
  1118. * not being the leftmost task anymore. In that case NEED_RESCHED will
  1119. * be set and schedule() will start a new hrtick for the next task.
  1120. */
  1121. if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
  1122. is_leftmost(p, &rq->dl))
  1123. start_hrtick_dl(rq, p);
  1124. }
  1125. static void task_fork_dl(struct task_struct *p)
  1126. {
  1127. /*
  1128. * SCHED_DEADLINE tasks cannot fork and this is achieved through
  1129. * sched_fork()
  1130. */
  1131. }
  1132. static void task_dead_dl(struct task_struct *p)
  1133. {
  1134. struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
  1135. /*
  1136. * Since we are TASK_DEAD we won't slip out of the domain!
  1137. */
  1138. raw_spin_lock_irq(&dl_b->lock);
  1139. /* XXX we should retain the bw until 0-lag */
  1140. dl_b->total_bw -= p->dl.dl_bw;
  1141. raw_spin_unlock_irq(&dl_b->lock);
  1142. }
  1143. static void set_curr_task_dl(struct rq *rq)
  1144. {
  1145. struct task_struct *p = rq->curr;
  1146. p->se.exec_start = rq_clock_task(rq);
  1147. /* You can't push away the running task */
  1148. dequeue_pushable_dl_task(rq, p);
  1149. }
  1150. #ifdef CONFIG_SMP
  1151. /* Only try algorithms three times */
  1152. #define DL_MAX_TRIES 3
  1153. static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
  1154. {
  1155. if (!task_running(rq, p) &&
  1156. cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
  1157. return 1;
  1158. return 0;
  1159. }
  1160. /*
  1161. * Return the earliest pushable rq's task, which is suitable to be executed
  1162. * on the CPU, NULL otherwise:
  1163. */
  1164. static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
  1165. {
  1166. struct rb_node *next_node = rq->dl.pushable_dl_tasks_leftmost;
  1167. struct task_struct *p = NULL;
  1168. if (!has_pushable_dl_tasks(rq))
  1169. return NULL;
  1170. next_node:
  1171. if (next_node) {
  1172. p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
  1173. if (pick_dl_task(rq, p, cpu))
  1174. return p;
  1175. next_node = rb_next(next_node);
  1176. goto next_node;
  1177. }
  1178. return NULL;
  1179. }
  1180. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
  1181. static int find_later_rq(struct task_struct *task)
  1182. {
  1183. struct sched_domain *sd;
  1184. struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
  1185. int this_cpu = smp_processor_id();
  1186. int best_cpu, cpu = task_cpu(task);
  1187. /* Make sure the mask is initialized first */
  1188. if (unlikely(!later_mask))
  1189. return -1;
  1190. if (tsk_nr_cpus_allowed(task) == 1)
  1191. return -1;
  1192. /*
  1193. * We have to consider system topology and task affinity
  1194. * first, then we can look for a suitable cpu.
  1195. */
  1196. best_cpu = cpudl_find(&task_rq(task)->rd->cpudl,
  1197. task, later_mask);
  1198. if (best_cpu == -1)
  1199. return -1;
  1200. /*
  1201. * If we are here, some target has been found,
  1202. * the most suitable of which is cached in best_cpu.
  1203. * This is, among the runqueues where the current tasks
  1204. * have later deadlines than the task's one, the rq
  1205. * with the latest possible one.
  1206. *
  1207. * Now we check how well this matches with task's
  1208. * affinity and system topology.
  1209. *
  1210. * The last cpu where the task run is our first
  1211. * guess, since it is most likely cache-hot there.
  1212. */
  1213. if (cpumask_test_cpu(cpu, later_mask))
  1214. return cpu;
  1215. /*
  1216. * Check if this_cpu is to be skipped (i.e., it is
  1217. * not in the mask) or not.
  1218. */
  1219. if (!cpumask_test_cpu(this_cpu, later_mask))
  1220. this_cpu = -1;
  1221. rcu_read_lock();
  1222. for_each_domain(cpu, sd) {
  1223. if (sd->flags & SD_WAKE_AFFINE) {
  1224. /*
  1225. * If possible, preempting this_cpu is
  1226. * cheaper than migrating.
  1227. */
  1228. if (this_cpu != -1 &&
  1229. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  1230. rcu_read_unlock();
  1231. return this_cpu;
  1232. }
  1233. /*
  1234. * Last chance: if best_cpu is valid and is
  1235. * in the mask, that becomes our choice.
  1236. */
  1237. if (best_cpu < nr_cpu_ids &&
  1238. cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
  1239. rcu_read_unlock();
  1240. return best_cpu;
  1241. }
  1242. }
  1243. }
  1244. rcu_read_unlock();
  1245. /*
  1246. * At this point, all our guesses failed, we just return
  1247. * 'something', and let the caller sort the things out.
  1248. */
  1249. if (this_cpu != -1)
  1250. return this_cpu;
  1251. cpu = cpumask_any(later_mask);
  1252. if (cpu < nr_cpu_ids)
  1253. return cpu;
  1254. return -1;
  1255. }
  1256. /* Locks the rq it finds */
  1257. static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
  1258. {
  1259. struct rq *later_rq = NULL;
  1260. int tries;
  1261. int cpu;
  1262. for (tries = 0; tries < DL_MAX_TRIES; tries++) {
  1263. cpu = find_later_rq(task);
  1264. if ((cpu == -1) || (cpu == rq->cpu))
  1265. break;
  1266. later_rq = cpu_rq(cpu);
  1267. if (later_rq->dl.dl_nr_running &&
  1268. !dl_time_before(task->dl.deadline,
  1269. later_rq->dl.earliest_dl.curr)) {
  1270. /*
  1271. * Target rq has tasks of equal or earlier deadline,
  1272. * retrying does not release any lock and is unlikely
  1273. * to yield a different result.
  1274. */
  1275. later_rq = NULL;
  1276. break;
  1277. }
  1278. /* Retry if something changed. */
  1279. if (double_lock_balance(rq, later_rq)) {
  1280. if (unlikely(task_rq(task) != rq ||
  1281. !cpumask_test_cpu(later_rq->cpu,
  1282. tsk_cpus_allowed(task)) ||
  1283. task_running(rq, task) ||
  1284. !dl_task(task) ||
  1285. !task_on_rq_queued(task))) {
  1286. double_unlock_balance(rq, later_rq);
  1287. later_rq = NULL;
  1288. break;
  1289. }
  1290. }
  1291. /*
  1292. * If the rq we found has no -deadline task, or
  1293. * its earliest one has a later deadline than our
  1294. * task, the rq is a good one.
  1295. */
  1296. if (!later_rq->dl.dl_nr_running ||
  1297. dl_time_before(task->dl.deadline,
  1298. later_rq->dl.earliest_dl.curr))
  1299. break;
  1300. /* Otherwise we try again. */
  1301. double_unlock_balance(rq, later_rq);
  1302. later_rq = NULL;
  1303. }
  1304. return later_rq;
  1305. }
  1306. static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  1307. {
  1308. struct task_struct *p;
  1309. if (!has_pushable_dl_tasks(rq))
  1310. return NULL;
  1311. p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
  1312. struct task_struct, pushable_dl_tasks);
  1313. BUG_ON(rq->cpu != task_cpu(p));
  1314. BUG_ON(task_current(rq, p));
  1315. BUG_ON(tsk_nr_cpus_allowed(p) <= 1);
  1316. BUG_ON(!task_on_rq_queued(p));
  1317. BUG_ON(!dl_task(p));
  1318. return p;
  1319. }
  1320. /*
  1321. * See if the non running -deadline tasks on this rq
  1322. * can be sent to some other CPU where they can preempt
  1323. * and start executing.
  1324. */
  1325. static int push_dl_task(struct rq *rq)
  1326. {
  1327. struct task_struct *next_task;
  1328. struct rq *later_rq;
  1329. int ret = 0;
  1330. if (!rq->dl.overloaded)
  1331. return 0;
  1332. next_task = pick_next_pushable_dl_task(rq);
  1333. if (!next_task)
  1334. return 0;
  1335. retry:
  1336. if (unlikely(next_task == rq->curr)) {
  1337. WARN_ON(1);
  1338. return 0;
  1339. }
  1340. /*
  1341. * If next_task preempts rq->curr, and rq->curr
  1342. * can move away, it makes sense to just reschedule
  1343. * without going further in pushing next_task.
  1344. */
  1345. if (dl_task(rq->curr) &&
  1346. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  1347. tsk_nr_cpus_allowed(rq->curr) > 1) {
  1348. resched_curr(rq);
  1349. return 0;
  1350. }
  1351. /* We might release rq lock */
  1352. get_task_struct(next_task);
  1353. /* Will lock the rq it'll find */
  1354. later_rq = find_lock_later_rq(next_task, rq);
  1355. if (!later_rq) {
  1356. struct task_struct *task;
  1357. /*
  1358. * We must check all this again, since
  1359. * find_lock_later_rq releases rq->lock and it is
  1360. * then possible that next_task has migrated.
  1361. */
  1362. task = pick_next_pushable_dl_task(rq);
  1363. if (task_cpu(next_task) == rq->cpu && task == next_task) {
  1364. /*
  1365. * The task is still there. We don't try
  1366. * again, some other cpu will pull it when ready.
  1367. */
  1368. goto out;
  1369. }
  1370. if (!task)
  1371. /* No more tasks */
  1372. goto out;
  1373. put_task_struct(next_task);
  1374. next_task = task;
  1375. goto retry;
  1376. }
  1377. deactivate_task(rq, next_task, 0);
  1378. set_task_cpu(next_task, later_rq->cpu);
  1379. activate_task(later_rq, next_task, 0);
  1380. ret = 1;
  1381. resched_curr(later_rq);
  1382. double_unlock_balance(rq, later_rq);
  1383. out:
  1384. put_task_struct(next_task);
  1385. return ret;
  1386. }
  1387. static void push_dl_tasks(struct rq *rq)
  1388. {
  1389. /* push_dl_task() will return true if it moved a -deadline task */
  1390. while (push_dl_task(rq))
  1391. ;
  1392. }
  1393. static void pull_dl_task(struct rq *this_rq)
  1394. {
  1395. int this_cpu = this_rq->cpu, cpu;
  1396. struct task_struct *p;
  1397. bool resched = false;
  1398. struct rq *src_rq;
  1399. u64 dmin = LONG_MAX;
  1400. if (likely(!dl_overloaded(this_rq)))
  1401. return;
  1402. /*
  1403. * Match the barrier from dl_set_overloaded; this guarantees that if we
  1404. * see overloaded we must also see the dlo_mask bit.
  1405. */
  1406. smp_rmb();
  1407. for_each_cpu(cpu, this_rq->rd->dlo_mask) {
  1408. if (this_cpu == cpu)
  1409. continue;
  1410. src_rq = cpu_rq(cpu);
  1411. /*
  1412. * It looks racy, abd it is! However, as in sched_rt.c,
  1413. * we are fine with this.
  1414. */
  1415. if (this_rq->dl.dl_nr_running &&
  1416. dl_time_before(this_rq->dl.earliest_dl.curr,
  1417. src_rq->dl.earliest_dl.next))
  1418. continue;
  1419. /* Might drop this_rq->lock */
  1420. double_lock_balance(this_rq, src_rq);
  1421. /*
  1422. * If there are no more pullable tasks on the
  1423. * rq, we're done with it.
  1424. */
  1425. if (src_rq->dl.dl_nr_running <= 1)
  1426. goto skip;
  1427. p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
  1428. /*
  1429. * We found a task to be pulled if:
  1430. * - it preempts our current (if there's one),
  1431. * - it will preempt the last one we pulled (if any).
  1432. */
  1433. if (p && dl_time_before(p->dl.deadline, dmin) &&
  1434. (!this_rq->dl.dl_nr_running ||
  1435. dl_time_before(p->dl.deadline,
  1436. this_rq->dl.earliest_dl.curr))) {
  1437. WARN_ON(p == src_rq->curr);
  1438. WARN_ON(!task_on_rq_queued(p));
  1439. /*
  1440. * Then we pull iff p has actually an earlier
  1441. * deadline than the current task of its runqueue.
  1442. */
  1443. if (dl_time_before(p->dl.deadline,
  1444. src_rq->curr->dl.deadline))
  1445. goto skip;
  1446. resched = true;
  1447. deactivate_task(src_rq, p, 0);
  1448. set_task_cpu(p, this_cpu);
  1449. activate_task(this_rq, p, 0);
  1450. dmin = p->dl.deadline;
  1451. /* Is there any other task even earlier? */
  1452. }
  1453. skip:
  1454. double_unlock_balance(this_rq, src_rq);
  1455. }
  1456. if (resched)
  1457. resched_curr(this_rq);
  1458. }
  1459. /*
  1460. * Since the task is not running and a reschedule is not going to happen
  1461. * anytime soon on its runqueue, we try pushing it away now.
  1462. */
  1463. static void task_woken_dl(struct rq *rq, struct task_struct *p)
  1464. {
  1465. if (!task_running(rq, p) &&
  1466. !test_tsk_need_resched(rq->curr) &&
  1467. tsk_nr_cpus_allowed(p) > 1 &&
  1468. dl_task(rq->curr) &&
  1469. (tsk_nr_cpus_allowed(rq->curr) < 2 ||
  1470. !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
  1471. push_dl_tasks(rq);
  1472. }
  1473. }
  1474. static void set_cpus_allowed_dl(struct task_struct *p,
  1475. const struct cpumask *new_mask)
  1476. {
  1477. struct root_domain *src_rd;
  1478. struct rq *rq;
  1479. BUG_ON(!dl_task(p));
  1480. rq = task_rq(p);
  1481. src_rd = rq->rd;
  1482. /*
  1483. * Migrating a SCHED_DEADLINE task between exclusive
  1484. * cpusets (different root_domains) entails a bandwidth
  1485. * update. We already made space for us in the destination
  1486. * domain (see cpuset_can_attach()).
  1487. */
  1488. if (!cpumask_intersects(src_rd->span, new_mask)) {
  1489. struct dl_bw *src_dl_b;
  1490. src_dl_b = dl_bw_of(cpu_of(rq));
  1491. /*
  1492. * We now free resources of the root_domain we are migrating
  1493. * off. In the worst case, sched_setattr() may temporary fail
  1494. * until we complete the update.
  1495. */
  1496. raw_spin_lock(&src_dl_b->lock);
  1497. __dl_clear(src_dl_b, p->dl.dl_bw);
  1498. raw_spin_unlock(&src_dl_b->lock);
  1499. }
  1500. set_cpus_allowed_common(p, new_mask);
  1501. }
  1502. /* Assumes rq->lock is held */
  1503. static void rq_online_dl(struct rq *rq)
  1504. {
  1505. if (rq->dl.overloaded)
  1506. dl_set_overload(rq);
  1507. cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
  1508. if (rq->dl.dl_nr_running > 0)
  1509. cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
  1510. }
  1511. /* Assumes rq->lock is held */
  1512. static void rq_offline_dl(struct rq *rq)
  1513. {
  1514. if (rq->dl.overloaded)
  1515. dl_clear_overload(rq);
  1516. cpudl_clear(&rq->rd->cpudl, rq->cpu);
  1517. cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
  1518. }
  1519. void __init init_sched_dl_class(void)
  1520. {
  1521. unsigned int i;
  1522. for_each_possible_cpu(i)
  1523. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
  1524. GFP_KERNEL, cpu_to_node(i));
  1525. }
  1526. #endif /* CONFIG_SMP */
  1527. static void switched_from_dl(struct rq *rq, struct task_struct *p)
  1528. {
  1529. /*
  1530. * Start the deadline timer; if we switch back to dl before this we'll
  1531. * continue consuming our current CBS slice. If we stay outside of
  1532. * SCHED_DEADLINE until the deadline passes, the timer will reset the
  1533. * task.
  1534. */
  1535. if (!start_dl_timer(p))
  1536. __dl_clear_params(p);
  1537. /*
  1538. * Since this might be the only -deadline task on the rq,
  1539. * this is the right place to try to pull some other one
  1540. * from an overloaded cpu, if any.
  1541. */
  1542. if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
  1543. return;
  1544. queue_pull_task(rq);
  1545. }
  1546. /*
  1547. * When switching to -deadline, we may overload the rq, then
  1548. * we try to push someone off, if possible.
  1549. */
  1550. static void switched_to_dl(struct rq *rq, struct task_struct *p)
  1551. {
  1552. /* If p is not queued we will update its parameters at next wakeup. */
  1553. if (!task_on_rq_queued(p))
  1554. return;
  1555. /*
  1556. * If p is boosted we already updated its params in
  1557. * rt_mutex_setprio()->enqueue_task(..., ENQUEUE_REPLENISH),
  1558. * p's deadline being now already after rq_clock(rq).
  1559. */
  1560. if (dl_time_before(p->dl.deadline, rq_clock(rq)))
  1561. setup_new_dl_entity(&p->dl);
  1562. if (rq->curr != p) {
  1563. #ifdef CONFIG_SMP
  1564. if (tsk_nr_cpus_allowed(p) > 1 && rq->dl.overloaded)
  1565. queue_push_tasks(rq);
  1566. #endif
  1567. if (dl_task(rq->curr))
  1568. check_preempt_curr_dl(rq, p, 0);
  1569. else
  1570. resched_curr(rq);
  1571. }
  1572. }
  1573. /*
  1574. * If the scheduling parameters of a -deadline task changed,
  1575. * a push or pull operation might be needed.
  1576. */
  1577. static void prio_changed_dl(struct rq *rq, struct task_struct *p,
  1578. int oldprio)
  1579. {
  1580. if (task_on_rq_queued(p) || rq->curr == p) {
  1581. #ifdef CONFIG_SMP
  1582. /*
  1583. * This might be too much, but unfortunately
  1584. * we don't have the old deadline value, and
  1585. * we can't argue if the task is increasing
  1586. * or lowering its prio, so...
  1587. */
  1588. if (!rq->dl.overloaded)
  1589. queue_pull_task(rq);
  1590. /*
  1591. * If we now have a earlier deadline task than p,
  1592. * then reschedule, provided p is still on this
  1593. * runqueue.
  1594. */
  1595. if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
  1596. resched_curr(rq);
  1597. #else
  1598. /*
  1599. * Again, we don't know if p has a earlier
  1600. * or later deadline, so let's blindly set a
  1601. * (maybe not needed) rescheduling point.
  1602. */
  1603. resched_curr(rq);
  1604. #endif /* CONFIG_SMP */
  1605. }
  1606. }
  1607. const struct sched_class dl_sched_class = {
  1608. .next = &rt_sched_class,
  1609. .enqueue_task = enqueue_task_dl,
  1610. .dequeue_task = dequeue_task_dl,
  1611. .yield_task = yield_task_dl,
  1612. .check_preempt_curr = check_preempt_curr_dl,
  1613. .pick_next_task = pick_next_task_dl,
  1614. .put_prev_task = put_prev_task_dl,
  1615. #ifdef CONFIG_SMP
  1616. .select_task_rq = select_task_rq_dl,
  1617. .set_cpus_allowed = set_cpus_allowed_dl,
  1618. .rq_online = rq_online_dl,
  1619. .rq_offline = rq_offline_dl,
  1620. .task_woken = task_woken_dl,
  1621. #endif
  1622. .set_curr_task = set_curr_task_dl,
  1623. .task_tick = task_tick_dl,
  1624. .task_fork = task_fork_dl,
  1625. .task_dead = task_dead_dl,
  1626. .prio_changed = prio_changed_dl,
  1627. .switched_from = switched_from_dl,
  1628. .switched_to = switched_to_dl,
  1629. .update_curr = update_curr_dl,
  1630. };
  1631. #ifdef CONFIG_SCHED_DEBUG
  1632. extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
  1633. void print_dl_stats(struct seq_file *m, int cpu)
  1634. {
  1635. print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
  1636. }
  1637. #endif /* CONFIG_SCHED_DEBUG */