deadline.c 47 KB

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