deadline.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639
  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. extern unsigned long to_ratio(u64 period, u64 runtime);
  50. void init_dl_bw(struct dl_bw *dl_b)
  51. {
  52. raw_spin_lock_init(&dl_b->lock);
  53. raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
  54. if (global_rt_runtime() == RUNTIME_INF)
  55. dl_b->bw = -1;
  56. else
  57. dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
  58. raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
  59. dl_b->total_bw = 0;
  60. }
  61. void init_dl_rq(struct dl_rq *dl_rq, struct rq *rq)
  62. {
  63. dl_rq->rb_root = RB_ROOT;
  64. #ifdef CONFIG_SMP
  65. /* zero means no -deadline tasks */
  66. dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
  67. dl_rq->dl_nr_migratory = 0;
  68. dl_rq->overloaded = 0;
  69. dl_rq->pushable_dl_tasks_root = RB_ROOT;
  70. #else
  71. init_dl_bw(&dl_rq->dl_bw);
  72. #endif
  73. }
  74. #ifdef CONFIG_SMP
  75. static inline int dl_overloaded(struct rq *rq)
  76. {
  77. return atomic_read(&rq->rd->dlo_count);
  78. }
  79. static inline void dl_set_overload(struct rq *rq)
  80. {
  81. if (!rq->online)
  82. return;
  83. cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
  84. /*
  85. * Must be visible before the overload count is
  86. * set (as in sched_rt.c).
  87. *
  88. * Matched by the barrier in pull_dl_task().
  89. */
  90. smp_wmb();
  91. atomic_inc(&rq->rd->dlo_count);
  92. }
  93. static inline void dl_clear_overload(struct rq *rq)
  94. {
  95. if (!rq->online)
  96. return;
  97. atomic_dec(&rq->rd->dlo_count);
  98. cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
  99. }
  100. static void update_dl_migration(struct dl_rq *dl_rq)
  101. {
  102. if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
  103. if (!dl_rq->overloaded) {
  104. dl_set_overload(rq_of_dl_rq(dl_rq));
  105. dl_rq->overloaded = 1;
  106. }
  107. } else if (dl_rq->overloaded) {
  108. dl_clear_overload(rq_of_dl_rq(dl_rq));
  109. dl_rq->overloaded = 0;
  110. }
  111. }
  112. static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  113. {
  114. struct task_struct *p = dl_task_of(dl_se);
  115. if (p->nr_cpus_allowed > 1)
  116. dl_rq->dl_nr_migratory++;
  117. update_dl_migration(dl_rq);
  118. }
  119. static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  120. {
  121. struct task_struct *p = dl_task_of(dl_se);
  122. if (p->nr_cpus_allowed > 1)
  123. dl_rq->dl_nr_migratory--;
  124. update_dl_migration(dl_rq);
  125. }
  126. /*
  127. * The list of pushable -deadline task is not a plist, like in
  128. * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
  129. */
  130. static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  131. {
  132. struct dl_rq *dl_rq = &rq->dl;
  133. struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_node;
  134. struct rb_node *parent = NULL;
  135. struct task_struct *entry;
  136. int leftmost = 1;
  137. BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
  138. while (*link) {
  139. parent = *link;
  140. entry = rb_entry(parent, struct task_struct,
  141. pushable_dl_tasks);
  142. if (dl_entity_preempt(&p->dl, &entry->dl))
  143. link = &parent->rb_left;
  144. else {
  145. link = &parent->rb_right;
  146. leftmost = 0;
  147. }
  148. }
  149. if (leftmost)
  150. dl_rq->pushable_dl_tasks_leftmost = &p->pushable_dl_tasks;
  151. rb_link_node(&p->pushable_dl_tasks, parent, link);
  152. rb_insert_color(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
  153. }
  154. static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  155. {
  156. struct dl_rq *dl_rq = &rq->dl;
  157. if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
  158. return;
  159. if (dl_rq->pushable_dl_tasks_leftmost == &p->pushable_dl_tasks) {
  160. struct rb_node *next_node;
  161. next_node = rb_next(&p->pushable_dl_tasks);
  162. dl_rq->pushable_dl_tasks_leftmost = next_node;
  163. }
  164. rb_erase(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
  165. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  166. }
  167. static inline int has_pushable_dl_tasks(struct rq *rq)
  168. {
  169. return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root);
  170. }
  171. static int push_dl_task(struct rq *rq);
  172. #else
  173. static inline
  174. void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  175. {
  176. }
  177. static inline
  178. void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  179. {
  180. }
  181. static inline
  182. void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  183. {
  184. }
  185. static inline
  186. void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  187. {
  188. }
  189. #endif /* CONFIG_SMP */
  190. static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  191. static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  192. static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
  193. int flags);
  194. /*
  195. * We are being explicitly informed that a new instance is starting,
  196. * and this means that:
  197. * - the absolute deadline of the entity has to be placed at
  198. * current time + relative deadline;
  199. * - the runtime of the entity has to be set to the maximum value.
  200. *
  201. * The capability of specifying such event is useful whenever a -deadline
  202. * entity wants to (try to!) synchronize its behaviour with the scheduler's
  203. * one, and to (try to!) reconcile itself with its own scheduling
  204. * parameters.
  205. */
  206. static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se,
  207. struct sched_dl_entity *pi_se)
  208. {
  209. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  210. struct rq *rq = rq_of_dl_rq(dl_rq);
  211. WARN_ON(!dl_se->dl_new || dl_se->dl_throttled);
  212. /*
  213. * We use the regular wall clock time to set deadlines in the
  214. * future; in fact, we must consider execution overheads (time
  215. * spent on hardirq context, etc.).
  216. */
  217. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  218. dl_se->runtime = pi_se->dl_runtime;
  219. dl_se->dl_new = 0;
  220. }
  221. /*
  222. * Pure Earliest Deadline First (EDF) scheduling does not deal with the
  223. * possibility of a entity lasting more than what it declared, and thus
  224. * exhausting its runtime.
  225. *
  226. * Here we are interested in making runtime overrun possible, but we do
  227. * not want a entity which is misbehaving to affect the scheduling of all
  228. * other entities.
  229. * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
  230. * is used, in order to confine each entity within its own bandwidth.
  231. *
  232. * This function deals exactly with that, and ensures that when the runtime
  233. * of a entity is replenished, its deadline is also postponed. That ensures
  234. * the overrunning entity can't interfere with other entity in the system and
  235. * can't make them miss their deadlines. Reasons why this kind of overruns
  236. * could happen are, typically, a entity voluntarily trying to overcome its
  237. * runtime, or it just underestimated it during sched_setscheduler_ex().
  238. */
  239. static void replenish_dl_entity(struct sched_dl_entity *dl_se,
  240. struct sched_dl_entity *pi_se)
  241. {
  242. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  243. struct rq *rq = rq_of_dl_rq(dl_rq);
  244. BUG_ON(pi_se->dl_runtime <= 0);
  245. /*
  246. * This could be the case for a !-dl task that is boosted.
  247. * Just go with full inherited parameters.
  248. */
  249. if (dl_se->dl_deadline == 0) {
  250. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  251. dl_se->runtime = pi_se->dl_runtime;
  252. }
  253. /*
  254. * We keep moving the deadline away until we get some
  255. * available runtime for the entity. This ensures correct
  256. * handling of situations where the runtime overrun is
  257. * arbitrary large.
  258. */
  259. while (dl_se->runtime <= 0) {
  260. dl_se->deadline += pi_se->dl_period;
  261. dl_se->runtime += pi_se->dl_runtime;
  262. }
  263. /*
  264. * At this point, the deadline really should be "in
  265. * the future" with respect to rq->clock. If it's
  266. * not, we are, for some reason, lagging too much!
  267. * Anyway, after having warn userspace abut that,
  268. * we still try to keep the things running by
  269. * resetting the deadline and the budget of the
  270. * entity.
  271. */
  272. if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
  273. static bool lag_once = false;
  274. if (!lag_once) {
  275. lag_once = true;
  276. printk_sched("sched: DL replenish lagged to much\n");
  277. }
  278. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  279. dl_se->runtime = pi_se->dl_runtime;
  280. }
  281. }
  282. /*
  283. * Here we check if --at time t-- an entity (which is probably being
  284. * [re]activated or, in general, enqueued) can use its remaining runtime
  285. * and its current deadline _without_ exceeding the bandwidth it is
  286. * assigned (function returns true if it can't). We are in fact applying
  287. * one of the CBS rules: when a task wakes up, if the residual runtime
  288. * over residual deadline fits within the allocated bandwidth, then we
  289. * can keep the current (absolute) deadline and residual budget without
  290. * disrupting the schedulability of the system. Otherwise, we should
  291. * refill the runtime and set the deadline a period in the future,
  292. * because keeping the current (absolute) deadline of the task would
  293. * result in breaking guarantees promised to other tasks (refer to
  294. * Documentation/scheduler/sched-deadline.txt for more informations).
  295. *
  296. * This function returns true if:
  297. *
  298. * runtime / (deadline - t) > dl_runtime / dl_period ,
  299. *
  300. * IOW we can't recycle current parameters.
  301. *
  302. * Notice that the bandwidth check is done against the period. For
  303. * task with deadline equal to period this is the same of using
  304. * dl_deadline instead of dl_period in the equation above.
  305. */
  306. static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
  307. struct sched_dl_entity *pi_se, u64 t)
  308. {
  309. u64 left, right;
  310. /*
  311. * left and right are the two sides of the equation above,
  312. * after a bit of shuffling to use multiplications instead
  313. * of divisions.
  314. *
  315. * Note that none of the time values involved in the two
  316. * multiplications are absolute: dl_deadline and dl_runtime
  317. * are the relative deadline and the maximum runtime of each
  318. * instance, runtime is the runtime left for the last instance
  319. * and (deadline - t), since t is rq->clock, is the time left
  320. * to the (absolute) deadline. Even if overflowing the u64 type
  321. * is very unlikely to occur in both cases, here we scale down
  322. * as we want to avoid that risk at all. Scaling down by 10
  323. * means that we reduce granularity to 1us. We are fine with it,
  324. * since this is only a true/false check and, anyway, thinking
  325. * of anything below microseconds resolution is actually fiction
  326. * (but still we want to give the user that illusion >;).
  327. */
  328. left = (pi_se->dl_period >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
  329. right = ((dl_se->deadline - t) >> DL_SCALE) *
  330. (pi_se->dl_runtime >> DL_SCALE);
  331. return dl_time_before(right, left);
  332. }
  333. /*
  334. * When a -deadline entity is queued back on the runqueue, its runtime and
  335. * deadline might need updating.
  336. *
  337. * The policy here is that we update the deadline of the entity only if:
  338. * - the current deadline is in the past,
  339. * - using the remaining runtime with the current deadline would make
  340. * the entity exceed its bandwidth.
  341. */
  342. static void update_dl_entity(struct sched_dl_entity *dl_se,
  343. struct sched_dl_entity *pi_se)
  344. {
  345. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  346. struct rq *rq = rq_of_dl_rq(dl_rq);
  347. /*
  348. * The arrival of a new instance needs special treatment, i.e.,
  349. * the actual scheduling parameters have to be "renewed".
  350. */
  351. if (dl_se->dl_new) {
  352. setup_new_dl_entity(dl_se, pi_se);
  353. return;
  354. }
  355. if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
  356. dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
  357. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  358. dl_se->runtime = pi_se->dl_runtime;
  359. }
  360. }
  361. /*
  362. * If the entity depleted all its runtime, and if we want it to sleep
  363. * while waiting for some new execution time to become available, we
  364. * set the bandwidth enforcement timer to the replenishment instant
  365. * and try to activate it.
  366. *
  367. * Notice that it is important for the caller to know if the timer
  368. * actually started or not (i.e., the replenishment instant is in
  369. * the future or in the past).
  370. */
  371. static int start_dl_timer(struct sched_dl_entity *dl_se, bool boosted)
  372. {
  373. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  374. struct rq *rq = rq_of_dl_rq(dl_rq);
  375. ktime_t now, act;
  376. ktime_t soft, hard;
  377. unsigned long range;
  378. s64 delta;
  379. if (boosted)
  380. return 0;
  381. /*
  382. * We want the timer to fire at the deadline, but considering
  383. * that it is actually coming from rq->clock and not from
  384. * hrtimer's time base reading.
  385. */
  386. act = ns_to_ktime(dl_se->deadline);
  387. now = hrtimer_cb_get_time(&dl_se->dl_timer);
  388. delta = ktime_to_ns(now) - rq_clock(rq);
  389. act = ktime_add_ns(act, delta);
  390. /*
  391. * If the expiry time already passed, e.g., because the value
  392. * chosen as the deadline is too small, don't even try to
  393. * start the timer in the past!
  394. */
  395. if (ktime_us_delta(act, now) < 0)
  396. return 0;
  397. hrtimer_set_expires(&dl_se->dl_timer, act);
  398. soft = hrtimer_get_softexpires(&dl_se->dl_timer);
  399. hard = hrtimer_get_expires(&dl_se->dl_timer);
  400. range = ktime_to_ns(ktime_sub(hard, soft));
  401. __hrtimer_start_range_ns(&dl_se->dl_timer, soft,
  402. range, HRTIMER_MODE_ABS, 0);
  403. return hrtimer_active(&dl_se->dl_timer);
  404. }
  405. /*
  406. * This is the bandwidth enforcement timer callback. If here, we know
  407. * a task is not on its dl_rq, since the fact that the timer was running
  408. * means the task is throttled and needs a runtime replenishment.
  409. *
  410. * However, what we actually do depends on the fact the task is active,
  411. * (it is on its rq) or has been removed from there by a call to
  412. * dequeue_task_dl(). In the former case we must issue the runtime
  413. * replenishment and add the task back to the dl_rq; in the latter, we just
  414. * do nothing but clearing dl_throttled, so that runtime and deadline
  415. * updating (and the queueing back to dl_rq) will be done by the
  416. * next call to enqueue_task_dl().
  417. */
  418. static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
  419. {
  420. struct sched_dl_entity *dl_se = container_of(timer,
  421. struct sched_dl_entity,
  422. dl_timer);
  423. struct task_struct *p = dl_task_of(dl_se);
  424. struct rq *rq = task_rq(p);
  425. raw_spin_lock(&rq->lock);
  426. /*
  427. * We need to take care of a possible races here. In fact, the
  428. * task might have changed its scheduling policy to something
  429. * different from SCHED_DEADLINE or changed its reservation
  430. * parameters (through sched_setscheduler()).
  431. */
  432. if (!dl_task(p) || dl_se->dl_new)
  433. goto unlock;
  434. sched_clock_tick();
  435. update_rq_clock(rq);
  436. dl_se->dl_throttled = 0;
  437. if (p->on_rq) {
  438. enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
  439. if (task_has_dl_policy(rq->curr))
  440. check_preempt_curr_dl(rq, p, 0);
  441. else
  442. resched_task(rq->curr);
  443. #ifdef CONFIG_SMP
  444. /*
  445. * Queueing this task back might have overloaded rq,
  446. * check if we need to kick someone away.
  447. */
  448. if (has_pushable_dl_tasks(rq))
  449. push_dl_task(rq);
  450. #endif
  451. }
  452. unlock:
  453. raw_spin_unlock(&rq->lock);
  454. return HRTIMER_NORESTART;
  455. }
  456. void init_dl_task_timer(struct sched_dl_entity *dl_se)
  457. {
  458. struct hrtimer *timer = &dl_se->dl_timer;
  459. if (hrtimer_active(timer)) {
  460. hrtimer_try_to_cancel(timer);
  461. return;
  462. }
  463. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  464. timer->function = dl_task_timer;
  465. }
  466. static
  467. int dl_runtime_exceeded(struct rq *rq, struct sched_dl_entity *dl_se)
  468. {
  469. int dmiss = dl_time_before(dl_se->deadline, rq_clock(rq));
  470. int rorun = dl_se->runtime <= 0;
  471. if (!rorun && !dmiss)
  472. return 0;
  473. /*
  474. * If we are beyond our current deadline and we are still
  475. * executing, then we have already used some of the runtime of
  476. * the next instance. Thus, if we do not account that, we are
  477. * stealing bandwidth from the system at each deadline miss!
  478. */
  479. if (dmiss) {
  480. dl_se->runtime = rorun ? dl_se->runtime : 0;
  481. dl_se->runtime -= rq_clock(rq) - dl_se->deadline;
  482. }
  483. return 1;
  484. }
  485. extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
  486. /*
  487. * Update the current task's runtime statistics (provided it is still
  488. * a -deadline task and has not been removed from the dl_rq).
  489. */
  490. static void update_curr_dl(struct rq *rq)
  491. {
  492. struct task_struct *curr = rq->curr;
  493. struct sched_dl_entity *dl_se = &curr->dl;
  494. u64 delta_exec;
  495. if (!dl_task(curr) || !on_dl_rq(dl_se))
  496. return;
  497. /*
  498. * Consumed budget is computed considering the time as
  499. * observed by schedulable tasks (excluding time spent
  500. * in hardirq context, etc.). Deadlines are instead
  501. * computed using hard walltime. This seems to be the more
  502. * natural solution, but the full ramifications of this
  503. * approach need further study.
  504. */
  505. delta_exec = rq_clock_task(rq) - curr->se.exec_start;
  506. if (unlikely((s64)delta_exec < 0))
  507. delta_exec = 0;
  508. schedstat_set(curr->se.statistics.exec_max,
  509. max(curr->se.statistics.exec_max, delta_exec));
  510. curr->se.sum_exec_runtime += delta_exec;
  511. account_group_exec_runtime(curr, delta_exec);
  512. curr->se.exec_start = rq_clock_task(rq);
  513. cpuacct_charge(curr, delta_exec);
  514. sched_rt_avg_update(rq, delta_exec);
  515. dl_se->runtime -= delta_exec;
  516. if (dl_runtime_exceeded(rq, dl_se)) {
  517. __dequeue_task_dl(rq, curr, 0);
  518. if (likely(start_dl_timer(dl_se, curr->dl.dl_boosted)))
  519. dl_se->dl_throttled = 1;
  520. else
  521. enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
  522. if (!is_leftmost(curr, &rq->dl))
  523. resched_task(curr);
  524. }
  525. /*
  526. * Because -- for now -- we share the rt bandwidth, we need to
  527. * account our runtime there too, otherwise actual rt tasks
  528. * would be able to exceed the shared quota.
  529. *
  530. * Account to the root rt group for now.
  531. *
  532. * The solution we're working towards is having the RT groups scheduled
  533. * using deadline servers -- however there's a few nasties to figure
  534. * out before that can happen.
  535. */
  536. if (rt_bandwidth_enabled()) {
  537. struct rt_rq *rt_rq = &rq->rt;
  538. raw_spin_lock(&rt_rq->rt_runtime_lock);
  539. /*
  540. * We'll let actual RT tasks worry about the overflow here, we
  541. * have our own CBS to keep us inline; only account when RT
  542. * bandwidth is relevant.
  543. */
  544. if (sched_rt_bandwidth_account(rt_rq))
  545. rt_rq->rt_time += delta_exec;
  546. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  547. }
  548. }
  549. #ifdef CONFIG_SMP
  550. static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu);
  551. static inline u64 next_deadline(struct rq *rq)
  552. {
  553. struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu);
  554. if (next && dl_prio(next->prio))
  555. return next->dl.deadline;
  556. else
  557. return 0;
  558. }
  559. static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  560. {
  561. struct rq *rq = rq_of_dl_rq(dl_rq);
  562. if (dl_rq->earliest_dl.curr == 0 ||
  563. dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
  564. /*
  565. * If the dl_rq had no -deadline tasks, or if the new task
  566. * has shorter deadline than the current one on dl_rq, we
  567. * know that the previous earliest becomes our next earliest,
  568. * as the new task becomes the earliest itself.
  569. */
  570. dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr;
  571. dl_rq->earliest_dl.curr = deadline;
  572. cpudl_set(&rq->rd->cpudl, rq->cpu, deadline, 1);
  573. } else if (dl_rq->earliest_dl.next == 0 ||
  574. dl_time_before(deadline, dl_rq->earliest_dl.next)) {
  575. /*
  576. * On the other hand, if the new -deadline task has a
  577. * a later deadline than the earliest one on dl_rq, but
  578. * it is earlier than the next (if any), we must
  579. * recompute the next-earliest.
  580. */
  581. dl_rq->earliest_dl.next = next_deadline(rq);
  582. }
  583. }
  584. static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  585. {
  586. struct rq *rq = rq_of_dl_rq(dl_rq);
  587. /*
  588. * Since we may have removed our earliest (and/or next earliest)
  589. * task we must recompute them.
  590. */
  591. if (!dl_rq->dl_nr_running) {
  592. dl_rq->earliest_dl.curr = 0;
  593. dl_rq->earliest_dl.next = 0;
  594. cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
  595. } else {
  596. struct rb_node *leftmost = dl_rq->rb_leftmost;
  597. struct sched_dl_entity *entry;
  598. entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
  599. dl_rq->earliest_dl.curr = entry->deadline;
  600. dl_rq->earliest_dl.next = next_deadline(rq);
  601. cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline, 1);
  602. }
  603. }
  604. #else
  605. static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  606. static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  607. #endif /* CONFIG_SMP */
  608. static inline
  609. void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  610. {
  611. int prio = dl_task_of(dl_se)->prio;
  612. u64 deadline = dl_se->deadline;
  613. WARN_ON(!dl_prio(prio));
  614. dl_rq->dl_nr_running++;
  615. inc_nr_running(rq_of_dl_rq(dl_rq));
  616. inc_dl_deadline(dl_rq, deadline);
  617. inc_dl_migration(dl_se, dl_rq);
  618. }
  619. static inline
  620. void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  621. {
  622. int prio = dl_task_of(dl_se)->prio;
  623. WARN_ON(!dl_prio(prio));
  624. WARN_ON(!dl_rq->dl_nr_running);
  625. dl_rq->dl_nr_running--;
  626. dec_nr_running(rq_of_dl_rq(dl_rq));
  627. dec_dl_deadline(dl_rq, dl_se->deadline);
  628. dec_dl_migration(dl_se, dl_rq);
  629. }
  630. static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
  631. {
  632. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  633. struct rb_node **link = &dl_rq->rb_root.rb_node;
  634. struct rb_node *parent = NULL;
  635. struct sched_dl_entity *entry;
  636. int leftmost = 1;
  637. BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
  638. while (*link) {
  639. parent = *link;
  640. entry = rb_entry(parent, struct sched_dl_entity, rb_node);
  641. if (dl_time_before(dl_se->deadline, entry->deadline))
  642. link = &parent->rb_left;
  643. else {
  644. link = &parent->rb_right;
  645. leftmost = 0;
  646. }
  647. }
  648. if (leftmost)
  649. dl_rq->rb_leftmost = &dl_se->rb_node;
  650. rb_link_node(&dl_se->rb_node, parent, link);
  651. rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
  652. inc_dl_tasks(dl_se, dl_rq);
  653. }
  654. static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
  655. {
  656. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  657. if (RB_EMPTY_NODE(&dl_se->rb_node))
  658. return;
  659. if (dl_rq->rb_leftmost == &dl_se->rb_node) {
  660. struct rb_node *next_node;
  661. next_node = rb_next(&dl_se->rb_node);
  662. dl_rq->rb_leftmost = next_node;
  663. }
  664. rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
  665. RB_CLEAR_NODE(&dl_se->rb_node);
  666. dec_dl_tasks(dl_se, dl_rq);
  667. }
  668. static void
  669. enqueue_dl_entity(struct sched_dl_entity *dl_se,
  670. struct sched_dl_entity *pi_se, int flags)
  671. {
  672. BUG_ON(on_dl_rq(dl_se));
  673. /*
  674. * If this is a wakeup or a new instance, the scheduling
  675. * parameters of the task might need updating. Otherwise,
  676. * we want a replenishment of its runtime.
  677. */
  678. if (!dl_se->dl_new && flags & ENQUEUE_REPLENISH)
  679. replenish_dl_entity(dl_se, pi_se);
  680. else
  681. update_dl_entity(dl_se, pi_se);
  682. __enqueue_dl_entity(dl_se);
  683. }
  684. static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
  685. {
  686. __dequeue_dl_entity(dl_se);
  687. }
  688. static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  689. {
  690. struct task_struct *pi_task = rt_mutex_get_top_task(p);
  691. struct sched_dl_entity *pi_se = &p->dl;
  692. /*
  693. * Use the scheduling parameters of the top pi-waiter
  694. * task if we have one and its (relative) deadline is
  695. * smaller than our one... OTW we keep our runtime and
  696. * deadline.
  697. */
  698. if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio))
  699. pi_se = &pi_task->dl;
  700. /*
  701. * If p is throttled, we do nothing. In fact, if it exhausted
  702. * its budget it needs a replenishment and, since it now is on
  703. * its rq, the bandwidth timer callback (which clearly has not
  704. * run yet) will take care of this.
  705. */
  706. if (p->dl.dl_throttled)
  707. return;
  708. enqueue_dl_entity(&p->dl, pi_se, flags);
  709. if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  710. enqueue_pushable_dl_task(rq, p);
  711. }
  712. static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  713. {
  714. dequeue_dl_entity(&p->dl);
  715. dequeue_pushable_dl_task(rq, p);
  716. }
  717. static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  718. {
  719. update_curr_dl(rq);
  720. __dequeue_task_dl(rq, p, flags);
  721. }
  722. /*
  723. * Yield task semantic for -deadline tasks is:
  724. *
  725. * get off from the CPU until our next instance, with
  726. * a new runtime. This is of little use now, since we
  727. * don't have a bandwidth reclaiming mechanism. Anyway,
  728. * bandwidth reclaiming is planned for the future, and
  729. * yield_task_dl will indicate that some spare budget
  730. * is available for other task instances to use it.
  731. */
  732. static void yield_task_dl(struct rq *rq)
  733. {
  734. struct task_struct *p = rq->curr;
  735. /*
  736. * We make the task go to sleep until its current deadline by
  737. * forcing its runtime to zero. This way, update_curr_dl() stops
  738. * it and the bandwidth timer will wake it up and will give it
  739. * new scheduling parameters (thanks to dl_new=1).
  740. */
  741. if (p->dl.runtime > 0) {
  742. rq->curr->dl.dl_new = 1;
  743. p->dl.runtime = 0;
  744. }
  745. update_curr_dl(rq);
  746. }
  747. #ifdef CONFIG_SMP
  748. static int find_later_rq(struct task_struct *task);
  749. static int
  750. select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
  751. {
  752. struct task_struct *curr;
  753. struct rq *rq;
  754. if (sd_flag != SD_BALANCE_WAKE && sd_flag != SD_BALANCE_FORK)
  755. goto out;
  756. rq = cpu_rq(cpu);
  757. rcu_read_lock();
  758. curr = ACCESS_ONCE(rq->curr); /* unlocked access */
  759. /*
  760. * If we are dealing with a -deadline task, we must
  761. * decide where to wake it up.
  762. * If it has a later deadline and the current task
  763. * on this rq can't move (provided the waking task
  764. * can!) we prefer to send it somewhere else. On the
  765. * other hand, if it has a shorter deadline, we
  766. * try to make it stay here, it might be important.
  767. */
  768. if (unlikely(dl_task(curr)) &&
  769. (curr->nr_cpus_allowed < 2 ||
  770. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  771. (p->nr_cpus_allowed > 1)) {
  772. int target = find_later_rq(p);
  773. if (target != -1)
  774. cpu = target;
  775. }
  776. rcu_read_unlock();
  777. out:
  778. return cpu;
  779. }
  780. static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  781. {
  782. /*
  783. * Current can't be migrated, useless to reschedule,
  784. * let's hope p can move out.
  785. */
  786. if (rq->curr->nr_cpus_allowed == 1 ||
  787. cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
  788. return;
  789. /*
  790. * p is migratable, so let's not schedule it and
  791. * see if it is pushed or pulled somewhere else.
  792. */
  793. if (p->nr_cpus_allowed != 1 &&
  794. cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
  795. return;
  796. resched_task(rq->curr);
  797. }
  798. #endif /* CONFIG_SMP */
  799. /*
  800. * Only called when both the current and waking task are -deadline
  801. * tasks.
  802. */
  803. static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
  804. int flags)
  805. {
  806. if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
  807. resched_task(rq->curr);
  808. return;
  809. }
  810. #ifdef CONFIG_SMP
  811. /*
  812. * In the unlikely case current and p have the same deadline
  813. * let us try to decide what's the best thing to do...
  814. */
  815. if ((p->dl.deadline == rq->curr->dl.deadline) &&
  816. !test_tsk_need_resched(rq->curr))
  817. check_preempt_equal_dl(rq, p);
  818. #endif /* CONFIG_SMP */
  819. }
  820. #ifdef CONFIG_SCHED_HRTICK
  821. static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
  822. {
  823. s64 delta = p->dl.dl_runtime - p->dl.runtime;
  824. if (delta > 10000)
  825. hrtick_start(rq, p->dl.runtime);
  826. }
  827. #endif
  828. static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
  829. struct dl_rq *dl_rq)
  830. {
  831. struct rb_node *left = dl_rq->rb_leftmost;
  832. if (!left)
  833. return NULL;
  834. return rb_entry(left, struct sched_dl_entity, rb_node);
  835. }
  836. struct task_struct *pick_next_task_dl(struct rq *rq)
  837. {
  838. struct sched_dl_entity *dl_se;
  839. struct task_struct *p;
  840. struct dl_rq *dl_rq;
  841. dl_rq = &rq->dl;
  842. if (unlikely(!dl_rq->dl_nr_running))
  843. return NULL;
  844. dl_se = pick_next_dl_entity(rq, dl_rq);
  845. BUG_ON(!dl_se);
  846. p = dl_task_of(dl_se);
  847. p->se.exec_start = rq_clock_task(rq);
  848. /* Running task will never be pushed. */
  849. dequeue_pushable_dl_task(rq, p);
  850. #ifdef CONFIG_SCHED_HRTICK
  851. if (hrtick_enabled(rq))
  852. start_hrtick_dl(rq, p);
  853. #endif
  854. #ifdef CONFIG_SMP
  855. rq->post_schedule = has_pushable_dl_tasks(rq);
  856. #endif /* CONFIG_SMP */
  857. return p;
  858. }
  859. static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
  860. {
  861. update_curr_dl(rq);
  862. if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
  863. enqueue_pushable_dl_task(rq, p);
  864. }
  865. static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
  866. {
  867. update_curr_dl(rq);
  868. #ifdef CONFIG_SCHED_HRTICK
  869. if (hrtick_enabled(rq) && queued && p->dl.runtime > 0)
  870. start_hrtick_dl(rq, p);
  871. #endif
  872. }
  873. static void task_fork_dl(struct task_struct *p)
  874. {
  875. /*
  876. * SCHED_DEADLINE tasks cannot fork and this is achieved through
  877. * sched_fork()
  878. */
  879. }
  880. static void task_dead_dl(struct task_struct *p)
  881. {
  882. struct hrtimer *timer = &p->dl.dl_timer;
  883. struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
  884. /*
  885. * Since we are TASK_DEAD we won't slip out of the domain!
  886. */
  887. raw_spin_lock_irq(&dl_b->lock);
  888. dl_b->total_bw -= p->dl.dl_bw;
  889. raw_spin_unlock_irq(&dl_b->lock);
  890. hrtimer_cancel(timer);
  891. }
  892. static void set_curr_task_dl(struct rq *rq)
  893. {
  894. struct task_struct *p = rq->curr;
  895. p->se.exec_start = rq_clock_task(rq);
  896. /* You can't push away the running task */
  897. dequeue_pushable_dl_task(rq, p);
  898. }
  899. #ifdef CONFIG_SMP
  900. /* Only try algorithms three times */
  901. #define DL_MAX_TRIES 3
  902. static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
  903. {
  904. if (!task_running(rq, p) &&
  905. (cpu < 0 || cpumask_test_cpu(cpu, &p->cpus_allowed)) &&
  906. (p->nr_cpus_allowed > 1))
  907. return 1;
  908. return 0;
  909. }
  910. /* Returns the second earliest -deadline task, NULL otherwise */
  911. static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu)
  912. {
  913. struct rb_node *next_node = rq->dl.rb_leftmost;
  914. struct sched_dl_entity *dl_se;
  915. struct task_struct *p = NULL;
  916. next_node:
  917. next_node = rb_next(next_node);
  918. if (next_node) {
  919. dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node);
  920. p = dl_task_of(dl_se);
  921. if (pick_dl_task(rq, p, cpu))
  922. return p;
  923. goto next_node;
  924. }
  925. return NULL;
  926. }
  927. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
  928. static int find_later_rq(struct task_struct *task)
  929. {
  930. struct sched_domain *sd;
  931. struct cpumask *later_mask = __get_cpu_var(local_cpu_mask_dl);
  932. int this_cpu = smp_processor_id();
  933. int best_cpu, cpu = task_cpu(task);
  934. /* Make sure the mask is initialized first */
  935. if (unlikely(!later_mask))
  936. return -1;
  937. if (task->nr_cpus_allowed == 1)
  938. return -1;
  939. best_cpu = cpudl_find(&task_rq(task)->rd->cpudl,
  940. task, later_mask);
  941. if (best_cpu == -1)
  942. return -1;
  943. /*
  944. * If we are here, some target has been found,
  945. * the most suitable of which is cached in best_cpu.
  946. * This is, among the runqueues where the current tasks
  947. * have later deadlines than the task's one, the rq
  948. * with the latest possible one.
  949. *
  950. * Now we check how well this matches with task's
  951. * affinity and system topology.
  952. *
  953. * The last cpu where the task run is our first
  954. * guess, since it is most likely cache-hot there.
  955. */
  956. if (cpumask_test_cpu(cpu, later_mask))
  957. return cpu;
  958. /*
  959. * Check if this_cpu is to be skipped (i.e., it is
  960. * not in the mask) or not.
  961. */
  962. if (!cpumask_test_cpu(this_cpu, later_mask))
  963. this_cpu = -1;
  964. rcu_read_lock();
  965. for_each_domain(cpu, sd) {
  966. if (sd->flags & SD_WAKE_AFFINE) {
  967. /*
  968. * If possible, preempting this_cpu is
  969. * cheaper than migrating.
  970. */
  971. if (this_cpu != -1 &&
  972. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  973. rcu_read_unlock();
  974. return this_cpu;
  975. }
  976. /*
  977. * Last chance: if best_cpu is valid and is
  978. * in the mask, that becomes our choice.
  979. */
  980. if (best_cpu < nr_cpu_ids &&
  981. cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
  982. rcu_read_unlock();
  983. return best_cpu;
  984. }
  985. }
  986. }
  987. rcu_read_unlock();
  988. /*
  989. * At this point, all our guesses failed, we just return
  990. * 'something', and let the caller sort the things out.
  991. */
  992. if (this_cpu != -1)
  993. return this_cpu;
  994. cpu = cpumask_any(later_mask);
  995. if (cpu < nr_cpu_ids)
  996. return cpu;
  997. return -1;
  998. }
  999. /* Locks the rq it finds */
  1000. static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
  1001. {
  1002. struct rq *later_rq = NULL;
  1003. int tries;
  1004. int cpu;
  1005. for (tries = 0; tries < DL_MAX_TRIES; tries++) {
  1006. cpu = find_later_rq(task);
  1007. if ((cpu == -1) || (cpu == rq->cpu))
  1008. break;
  1009. later_rq = cpu_rq(cpu);
  1010. /* Retry if something changed. */
  1011. if (double_lock_balance(rq, later_rq)) {
  1012. if (unlikely(task_rq(task) != rq ||
  1013. !cpumask_test_cpu(later_rq->cpu,
  1014. &task->cpus_allowed) ||
  1015. task_running(rq, task) || !task->on_rq)) {
  1016. double_unlock_balance(rq, later_rq);
  1017. later_rq = NULL;
  1018. break;
  1019. }
  1020. }
  1021. /*
  1022. * If the rq we found has no -deadline task, or
  1023. * its earliest one has a later deadline than our
  1024. * task, the rq is a good one.
  1025. */
  1026. if (!later_rq->dl.dl_nr_running ||
  1027. dl_time_before(task->dl.deadline,
  1028. later_rq->dl.earliest_dl.curr))
  1029. break;
  1030. /* Otherwise we try again. */
  1031. double_unlock_balance(rq, later_rq);
  1032. later_rq = NULL;
  1033. }
  1034. return later_rq;
  1035. }
  1036. static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  1037. {
  1038. struct task_struct *p;
  1039. if (!has_pushable_dl_tasks(rq))
  1040. return NULL;
  1041. p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
  1042. struct task_struct, pushable_dl_tasks);
  1043. BUG_ON(rq->cpu != task_cpu(p));
  1044. BUG_ON(task_current(rq, p));
  1045. BUG_ON(p->nr_cpus_allowed <= 1);
  1046. BUG_ON(!p->on_rq);
  1047. BUG_ON(!dl_task(p));
  1048. return p;
  1049. }
  1050. /*
  1051. * See if the non running -deadline tasks on this rq
  1052. * can be sent to some other CPU where they can preempt
  1053. * and start executing.
  1054. */
  1055. static int push_dl_task(struct rq *rq)
  1056. {
  1057. struct task_struct *next_task;
  1058. struct rq *later_rq;
  1059. if (!rq->dl.overloaded)
  1060. return 0;
  1061. next_task = pick_next_pushable_dl_task(rq);
  1062. if (!next_task)
  1063. return 0;
  1064. retry:
  1065. if (unlikely(next_task == rq->curr)) {
  1066. WARN_ON(1);
  1067. return 0;
  1068. }
  1069. /*
  1070. * If next_task preempts rq->curr, and rq->curr
  1071. * can move away, it makes sense to just reschedule
  1072. * without going further in pushing next_task.
  1073. */
  1074. if (dl_task(rq->curr) &&
  1075. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  1076. rq->curr->nr_cpus_allowed > 1) {
  1077. resched_task(rq->curr);
  1078. return 0;
  1079. }
  1080. /* We might release rq lock */
  1081. get_task_struct(next_task);
  1082. /* Will lock the rq it'll find */
  1083. later_rq = find_lock_later_rq(next_task, rq);
  1084. if (!later_rq) {
  1085. struct task_struct *task;
  1086. /*
  1087. * We must check all this again, since
  1088. * find_lock_later_rq releases rq->lock and it is
  1089. * then possible that next_task has migrated.
  1090. */
  1091. task = pick_next_pushable_dl_task(rq);
  1092. if (task_cpu(next_task) == rq->cpu && task == next_task) {
  1093. /*
  1094. * The task is still there. We don't try
  1095. * again, some other cpu will pull it when ready.
  1096. */
  1097. dequeue_pushable_dl_task(rq, next_task);
  1098. goto out;
  1099. }
  1100. if (!task)
  1101. /* No more tasks */
  1102. goto out;
  1103. put_task_struct(next_task);
  1104. next_task = task;
  1105. goto retry;
  1106. }
  1107. deactivate_task(rq, next_task, 0);
  1108. set_task_cpu(next_task, later_rq->cpu);
  1109. activate_task(later_rq, next_task, 0);
  1110. resched_task(later_rq->curr);
  1111. double_unlock_balance(rq, later_rq);
  1112. out:
  1113. put_task_struct(next_task);
  1114. return 1;
  1115. }
  1116. static void push_dl_tasks(struct rq *rq)
  1117. {
  1118. /* Terminates as it moves a -deadline task */
  1119. while (push_dl_task(rq))
  1120. ;
  1121. }
  1122. static int pull_dl_task(struct rq *this_rq)
  1123. {
  1124. int this_cpu = this_rq->cpu, ret = 0, cpu;
  1125. struct task_struct *p;
  1126. struct rq *src_rq;
  1127. u64 dmin = LONG_MAX;
  1128. if (likely(!dl_overloaded(this_rq)))
  1129. return 0;
  1130. /*
  1131. * Match the barrier from dl_set_overloaded; this guarantees that if we
  1132. * see overloaded we must also see the dlo_mask bit.
  1133. */
  1134. smp_rmb();
  1135. for_each_cpu(cpu, this_rq->rd->dlo_mask) {
  1136. if (this_cpu == cpu)
  1137. continue;
  1138. src_rq = cpu_rq(cpu);
  1139. /*
  1140. * It looks racy, abd it is! However, as in sched_rt.c,
  1141. * we are fine with this.
  1142. */
  1143. if (this_rq->dl.dl_nr_running &&
  1144. dl_time_before(this_rq->dl.earliest_dl.curr,
  1145. src_rq->dl.earliest_dl.next))
  1146. continue;
  1147. /* Might drop this_rq->lock */
  1148. double_lock_balance(this_rq, src_rq);
  1149. /*
  1150. * If there are no more pullable tasks on the
  1151. * rq, we're done with it.
  1152. */
  1153. if (src_rq->dl.dl_nr_running <= 1)
  1154. goto skip;
  1155. p = pick_next_earliest_dl_task(src_rq, this_cpu);
  1156. /*
  1157. * We found a task to be pulled if:
  1158. * - it preempts our current (if there's one),
  1159. * - it will preempt the last one we pulled (if any).
  1160. */
  1161. if (p && dl_time_before(p->dl.deadline, dmin) &&
  1162. (!this_rq->dl.dl_nr_running ||
  1163. dl_time_before(p->dl.deadline,
  1164. this_rq->dl.earliest_dl.curr))) {
  1165. WARN_ON(p == src_rq->curr);
  1166. WARN_ON(!p->on_rq);
  1167. /*
  1168. * Then we pull iff p has actually an earlier
  1169. * deadline than the current task of its runqueue.
  1170. */
  1171. if (dl_time_before(p->dl.deadline,
  1172. src_rq->curr->dl.deadline))
  1173. goto skip;
  1174. ret = 1;
  1175. deactivate_task(src_rq, p, 0);
  1176. set_task_cpu(p, this_cpu);
  1177. activate_task(this_rq, p, 0);
  1178. dmin = p->dl.deadline;
  1179. /* Is there any other task even earlier? */
  1180. }
  1181. skip:
  1182. double_unlock_balance(this_rq, src_rq);
  1183. }
  1184. return ret;
  1185. }
  1186. static void pre_schedule_dl(struct rq *rq, struct task_struct *prev)
  1187. {
  1188. /* Try to pull other tasks here */
  1189. if (dl_task(prev))
  1190. pull_dl_task(rq);
  1191. }
  1192. static void post_schedule_dl(struct rq *rq)
  1193. {
  1194. push_dl_tasks(rq);
  1195. }
  1196. /*
  1197. * Since the task is not running and a reschedule is not going to happen
  1198. * anytime soon on its runqueue, we try pushing it away now.
  1199. */
  1200. static void task_woken_dl(struct rq *rq, struct task_struct *p)
  1201. {
  1202. if (!task_running(rq, p) &&
  1203. !test_tsk_need_resched(rq->curr) &&
  1204. has_pushable_dl_tasks(rq) &&
  1205. p->nr_cpus_allowed > 1 &&
  1206. dl_task(rq->curr) &&
  1207. (rq->curr->nr_cpus_allowed < 2 ||
  1208. dl_entity_preempt(&rq->curr->dl, &p->dl))) {
  1209. push_dl_tasks(rq);
  1210. }
  1211. }
  1212. static void set_cpus_allowed_dl(struct task_struct *p,
  1213. const struct cpumask *new_mask)
  1214. {
  1215. struct rq *rq;
  1216. int weight;
  1217. BUG_ON(!dl_task(p));
  1218. /*
  1219. * Update only if the task is actually running (i.e.,
  1220. * it is on the rq AND it is not throttled).
  1221. */
  1222. if (!on_dl_rq(&p->dl))
  1223. return;
  1224. weight = cpumask_weight(new_mask);
  1225. /*
  1226. * Only update if the process changes its state from whether it
  1227. * can migrate or not.
  1228. */
  1229. if ((p->nr_cpus_allowed > 1) == (weight > 1))
  1230. return;
  1231. rq = task_rq(p);
  1232. /*
  1233. * The process used to be able to migrate OR it can now migrate
  1234. */
  1235. if (weight <= 1) {
  1236. if (!task_current(rq, p))
  1237. dequeue_pushable_dl_task(rq, p);
  1238. BUG_ON(!rq->dl.dl_nr_migratory);
  1239. rq->dl.dl_nr_migratory--;
  1240. } else {
  1241. if (!task_current(rq, p))
  1242. enqueue_pushable_dl_task(rq, p);
  1243. rq->dl.dl_nr_migratory++;
  1244. }
  1245. update_dl_migration(&rq->dl);
  1246. }
  1247. /* Assumes rq->lock is held */
  1248. static void rq_online_dl(struct rq *rq)
  1249. {
  1250. if (rq->dl.overloaded)
  1251. dl_set_overload(rq);
  1252. if (rq->dl.dl_nr_running > 0)
  1253. cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr, 1);
  1254. }
  1255. /* Assumes rq->lock is held */
  1256. static void rq_offline_dl(struct rq *rq)
  1257. {
  1258. if (rq->dl.overloaded)
  1259. dl_clear_overload(rq);
  1260. cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
  1261. }
  1262. void init_sched_dl_class(void)
  1263. {
  1264. unsigned int i;
  1265. for_each_possible_cpu(i)
  1266. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
  1267. GFP_KERNEL, cpu_to_node(i));
  1268. }
  1269. #endif /* CONFIG_SMP */
  1270. static void switched_from_dl(struct rq *rq, struct task_struct *p)
  1271. {
  1272. if (hrtimer_active(&p->dl.dl_timer) && !dl_policy(p->policy))
  1273. hrtimer_try_to_cancel(&p->dl.dl_timer);
  1274. #ifdef CONFIG_SMP
  1275. /*
  1276. * Since this might be the only -deadline task on the rq,
  1277. * this is the right place to try to pull some other one
  1278. * from an overloaded cpu, if any.
  1279. */
  1280. if (!rq->dl.dl_nr_running)
  1281. pull_dl_task(rq);
  1282. #endif
  1283. }
  1284. /*
  1285. * When switching to -deadline, we may overload the rq, then
  1286. * we try to push someone off, if possible.
  1287. */
  1288. static void switched_to_dl(struct rq *rq, struct task_struct *p)
  1289. {
  1290. int check_resched = 1;
  1291. /*
  1292. * If p is throttled, don't consider the possibility
  1293. * of preempting rq->curr, the check will be done right
  1294. * after its runtime will get replenished.
  1295. */
  1296. if (unlikely(p->dl.dl_throttled))
  1297. return;
  1298. if (p->on_rq || rq->curr != p) {
  1299. #ifdef CONFIG_SMP
  1300. if (rq->dl.overloaded && push_dl_task(rq) && rq != task_rq(p))
  1301. /* Only reschedule if pushing failed */
  1302. check_resched = 0;
  1303. #endif /* CONFIG_SMP */
  1304. if (check_resched && task_has_dl_policy(rq->curr))
  1305. check_preempt_curr_dl(rq, p, 0);
  1306. }
  1307. }
  1308. /*
  1309. * If the scheduling parameters of a -deadline task changed,
  1310. * a push or pull operation might be needed.
  1311. */
  1312. static void prio_changed_dl(struct rq *rq, struct task_struct *p,
  1313. int oldprio)
  1314. {
  1315. if (p->on_rq || rq->curr == p) {
  1316. #ifdef CONFIG_SMP
  1317. /*
  1318. * This might be too much, but unfortunately
  1319. * we don't have the old deadline value, and
  1320. * we can't argue if the task is increasing
  1321. * or lowering its prio, so...
  1322. */
  1323. if (!rq->dl.overloaded)
  1324. pull_dl_task(rq);
  1325. /*
  1326. * If we now have a earlier deadline task than p,
  1327. * then reschedule, provided p is still on this
  1328. * runqueue.
  1329. */
  1330. if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline) &&
  1331. rq->curr == p)
  1332. resched_task(p);
  1333. #else
  1334. /*
  1335. * Again, we don't know if p has a earlier
  1336. * or later deadline, so let's blindly set a
  1337. * (maybe not needed) rescheduling point.
  1338. */
  1339. resched_task(p);
  1340. #endif /* CONFIG_SMP */
  1341. } else
  1342. switched_to_dl(rq, p);
  1343. }
  1344. const struct sched_class dl_sched_class = {
  1345. .next = &rt_sched_class,
  1346. .enqueue_task = enqueue_task_dl,
  1347. .dequeue_task = dequeue_task_dl,
  1348. .yield_task = yield_task_dl,
  1349. .check_preempt_curr = check_preempt_curr_dl,
  1350. .pick_next_task = pick_next_task_dl,
  1351. .put_prev_task = put_prev_task_dl,
  1352. #ifdef CONFIG_SMP
  1353. .select_task_rq = select_task_rq_dl,
  1354. .set_cpus_allowed = set_cpus_allowed_dl,
  1355. .rq_online = rq_online_dl,
  1356. .rq_offline = rq_offline_dl,
  1357. .pre_schedule = pre_schedule_dl,
  1358. .post_schedule = post_schedule_dl,
  1359. .task_woken = task_woken_dl,
  1360. #endif
  1361. .set_curr_task = set_curr_task_dl,
  1362. .task_tick = task_tick_dl,
  1363. .task_fork = task_fork_dl,
  1364. .task_dead = task_dead_dl,
  1365. .prio_changed = prio_changed_dl,
  1366. .switched_from = switched_from_dl,
  1367. .switched_to = switched_to_dl,
  1368. };