deadline.c 42 KB

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