deadline.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882
  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. push_dl_task(rq);
  576. #endif
  577. unlock:
  578. task_rq_unlock(rq, p, &flags);
  579. /*
  580. * This can free the task_struct, including this hrtimer, do not touch
  581. * anything related to that after this.
  582. */
  583. put_task_struct(p);
  584. return HRTIMER_NORESTART;
  585. }
  586. void init_dl_task_timer(struct sched_dl_entity *dl_se)
  587. {
  588. struct hrtimer *timer = &dl_se->dl_timer;
  589. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  590. timer->function = dl_task_timer;
  591. }
  592. static
  593. int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
  594. {
  595. return (dl_se->runtime <= 0);
  596. }
  597. extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
  598. /*
  599. * Update the current task's runtime statistics (provided it is still
  600. * a -deadline task and has not been removed from the dl_rq).
  601. */
  602. static void update_curr_dl(struct rq *rq)
  603. {
  604. struct task_struct *curr = rq->curr;
  605. struct sched_dl_entity *dl_se = &curr->dl;
  606. u64 delta_exec;
  607. if (!dl_task(curr) || !on_dl_rq(dl_se))
  608. return;
  609. /*
  610. * Consumed budget is computed considering the time as
  611. * observed by schedulable tasks (excluding time spent
  612. * in hardirq context, etc.). Deadlines are instead
  613. * computed using hard walltime. This seems to be the more
  614. * natural solution, but the full ramifications of this
  615. * approach need further study.
  616. */
  617. delta_exec = rq_clock_task(rq) - curr->se.exec_start;
  618. if (unlikely((s64)delta_exec <= 0))
  619. return;
  620. schedstat_set(curr->se.statistics.exec_max,
  621. max(curr->se.statistics.exec_max, delta_exec));
  622. curr->se.sum_exec_runtime += delta_exec;
  623. account_group_exec_runtime(curr, delta_exec);
  624. curr->se.exec_start = rq_clock_task(rq);
  625. cpuacct_charge(curr, delta_exec);
  626. sched_rt_avg_update(rq, delta_exec);
  627. dl_se->runtime -= dl_se->dl_yielded ? 0 : delta_exec;
  628. if (dl_runtime_exceeded(dl_se)) {
  629. dl_se->dl_throttled = 1;
  630. __dequeue_task_dl(rq, curr, 0);
  631. if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr)))
  632. enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
  633. if (!is_leftmost(curr, &rq->dl))
  634. resched_curr(rq);
  635. }
  636. /*
  637. * Because -- for now -- we share the rt bandwidth, we need to
  638. * account our runtime there too, otherwise actual rt tasks
  639. * would be able to exceed the shared quota.
  640. *
  641. * Account to the root rt group for now.
  642. *
  643. * The solution we're working towards is having the RT groups scheduled
  644. * using deadline servers -- however there's a few nasties to figure
  645. * out before that can happen.
  646. */
  647. if (rt_bandwidth_enabled()) {
  648. struct rt_rq *rt_rq = &rq->rt;
  649. raw_spin_lock(&rt_rq->rt_runtime_lock);
  650. /*
  651. * We'll let actual RT tasks worry about the overflow here, we
  652. * have our own CBS to keep us inline; only account when RT
  653. * bandwidth is relevant.
  654. */
  655. if (sched_rt_bandwidth_account(rt_rq))
  656. rt_rq->rt_time += delta_exec;
  657. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  658. }
  659. }
  660. #ifdef CONFIG_SMP
  661. static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu);
  662. static inline u64 next_deadline(struct rq *rq)
  663. {
  664. struct task_struct *next = pick_next_earliest_dl_task(rq, rq->cpu);
  665. if (next && dl_prio(next->prio))
  666. return next->dl.deadline;
  667. else
  668. return 0;
  669. }
  670. static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  671. {
  672. struct rq *rq = rq_of_dl_rq(dl_rq);
  673. if (dl_rq->earliest_dl.curr == 0 ||
  674. dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
  675. /*
  676. * If the dl_rq had no -deadline tasks, or if the new task
  677. * has shorter deadline than the current one on dl_rq, we
  678. * know that the previous earliest becomes our next earliest,
  679. * as the new task becomes the earliest itself.
  680. */
  681. dl_rq->earliest_dl.next = dl_rq->earliest_dl.curr;
  682. dl_rq->earliest_dl.curr = deadline;
  683. cpudl_set(&rq->rd->cpudl, rq->cpu, deadline, 1);
  684. } else if (dl_rq->earliest_dl.next == 0 ||
  685. dl_time_before(deadline, dl_rq->earliest_dl.next)) {
  686. /*
  687. * On the other hand, if the new -deadline task has a
  688. * a later deadline than the earliest one on dl_rq, but
  689. * it is earlier than the next (if any), we must
  690. * recompute the next-earliest.
  691. */
  692. dl_rq->earliest_dl.next = next_deadline(rq);
  693. }
  694. }
  695. static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  696. {
  697. struct rq *rq = rq_of_dl_rq(dl_rq);
  698. /*
  699. * Since we may have removed our earliest (and/or next earliest)
  700. * task we must recompute them.
  701. */
  702. if (!dl_rq->dl_nr_running) {
  703. dl_rq->earliest_dl.curr = 0;
  704. dl_rq->earliest_dl.next = 0;
  705. cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
  706. } else {
  707. struct rb_node *leftmost = dl_rq->rb_leftmost;
  708. struct sched_dl_entity *entry;
  709. entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
  710. dl_rq->earliest_dl.curr = entry->deadline;
  711. dl_rq->earliest_dl.next = next_deadline(rq);
  712. cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline, 1);
  713. }
  714. }
  715. #else
  716. static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  717. static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  718. #endif /* CONFIG_SMP */
  719. static inline
  720. void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  721. {
  722. int prio = dl_task_of(dl_se)->prio;
  723. u64 deadline = dl_se->deadline;
  724. WARN_ON(!dl_prio(prio));
  725. dl_rq->dl_nr_running++;
  726. add_nr_running(rq_of_dl_rq(dl_rq), 1);
  727. inc_dl_deadline(dl_rq, deadline);
  728. inc_dl_migration(dl_se, dl_rq);
  729. }
  730. static inline
  731. void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  732. {
  733. int prio = dl_task_of(dl_se)->prio;
  734. WARN_ON(!dl_prio(prio));
  735. WARN_ON(!dl_rq->dl_nr_running);
  736. dl_rq->dl_nr_running--;
  737. sub_nr_running(rq_of_dl_rq(dl_rq), 1);
  738. dec_dl_deadline(dl_rq, dl_se->deadline);
  739. dec_dl_migration(dl_se, dl_rq);
  740. }
  741. static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
  742. {
  743. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  744. struct rb_node **link = &dl_rq->rb_root.rb_node;
  745. struct rb_node *parent = NULL;
  746. struct sched_dl_entity *entry;
  747. int leftmost = 1;
  748. BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
  749. while (*link) {
  750. parent = *link;
  751. entry = rb_entry(parent, struct sched_dl_entity, rb_node);
  752. if (dl_time_before(dl_se->deadline, entry->deadline))
  753. link = &parent->rb_left;
  754. else {
  755. link = &parent->rb_right;
  756. leftmost = 0;
  757. }
  758. }
  759. if (leftmost)
  760. dl_rq->rb_leftmost = &dl_se->rb_node;
  761. rb_link_node(&dl_se->rb_node, parent, link);
  762. rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
  763. inc_dl_tasks(dl_se, dl_rq);
  764. }
  765. static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
  766. {
  767. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  768. if (RB_EMPTY_NODE(&dl_se->rb_node))
  769. return;
  770. if (dl_rq->rb_leftmost == &dl_se->rb_node) {
  771. struct rb_node *next_node;
  772. next_node = rb_next(&dl_se->rb_node);
  773. dl_rq->rb_leftmost = next_node;
  774. }
  775. rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
  776. RB_CLEAR_NODE(&dl_se->rb_node);
  777. dec_dl_tasks(dl_se, dl_rq);
  778. }
  779. static void
  780. enqueue_dl_entity(struct sched_dl_entity *dl_se,
  781. struct sched_dl_entity *pi_se, int flags)
  782. {
  783. BUG_ON(on_dl_rq(dl_se));
  784. /*
  785. * If this is a wakeup or a new instance, the scheduling
  786. * parameters of the task might need updating. Otherwise,
  787. * we want a replenishment of its runtime.
  788. */
  789. if (dl_se->dl_new || flags & ENQUEUE_WAKEUP)
  790. update_dl_entity(dl_se, pi_se);
  791. else if (flags & ENQUEUE_REPLENISH)
  792. replenish_dl_entity(dl_se, pi_se);
  793. __enqueue_dl_entity(dl_se);
  794. }
  795. static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
  796. {
  797. __dequeue_dl_entity(dl_se);
  798. }
  799. static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  800. {
  801. struct task_struct *pi_task = rt_mutex_get_top_task(p);
  802. struct sched_dl_entity *pi_se = &p->dl;
  803. /*
  804. * Use the scheduling parameters of the top pi-waiter
  805. * task if we have one and its (relative) deadline is
  806. * smaller than our one... OTW we keep our runtime and
  807. * deadline.
  808. */
  809. if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) {
  810. pi_se = &pi_task->dl;
  811. } else if (!dl_prio(p->normal_prio)) {
  812. /*
  813. * Special case in which we have a !SCHED_DEADLINE task
  814. * that is going to be deboosted, but exceedes its
  815. * runtime while doing so. No point in replenishing
  816. * it, as it's going to return back to its original
  817. * scheduling class after this.
  818. */
  819. BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
  820. return;
  821. }
  822. /*
  823. * If p is throttled, we do nothing. In fact, if it exhausted
  824. * its budget it needs a replenishment and, since it now is on
  825. * its rq, the bandwidth timer callback (which clearly has not
  826. * run yet) will take care of this.
  827. */
  828. if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH))
  829. return;
  830. enqueue_dl_entity(&p->dl, pi_se, flags);
  831. if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  832. enqueue_pushable_dl_task(rq, p);
  833. }
  834. static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  835. {
  836. dequeue_dl_entity(&p->dl);
  837. dequeue_pushable_dl_task(rq, p);
  838. }
  839. static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  840. {
  841. update_curr_dl(rq);
  842. __dequeue_task_dl(rq, p, flags);
  843. }
  844. /*
  845. * Yield task semantic for -deadline tasks is:
  846. *
  847. * get off from the CPU until our next instance, with
  848. * a new runtime. This is of little use now, since we
  849. * don't have a bandwidth reclaiming mechanism. Anyway,
  850. * bandwidth reclaiming is planned for the future, and
  851. * yield_task_dl will indicate that some spare budget
  852. * is available for other task instances to use it.
  853. */
  854. static void yield_task_dl(struct rq *rq)
  855. {
  856. struct task_struct *p = rq->curr;
  857. /*
  858. * We make the task go to sleep until its current deadline by
  859. * forcing its runtime to zero. This way, update_curr_dl() stops
  860. * it and the bandwidth timer will wake it up and will give it
  861. * new scheduling parameters (thanks to dl_yielded=1).
  862. */
  863. if (p->dl.runtime > 0) {
  864. rq->curr->dl.dl_yielded = 1;
  865. p->dl.runtime = 0;
  866. }
  867. update_rq_clock(rq);
  868. update_curr_dl(rq);
  869. /*
  870. * Tell update_rq_clock() that we've just updated,
  871. * so we don't do microscopic update in schedule()
  872. * and double the fastpath cost.
  873. */
  874. rq_clock_skip_update(rq, true);
  875. }
  876. #ifdef CONFIG_SMP
  877. static int find_later_rq(struct task_struct *task);
  878. static int
  879. select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
  880. {
  881. struct task_struct *curr;
  882. struct rq *rq;
  883. if (sd_flag != SD_BALANCE_WAKE)
  884. goto out;
  885. rq = cpu_rq(cpu);
  886. rcu_read_lock();
  887. curr = READ_ONCE(rq->curr); /* unlocked access */
  888. /*
  889. * If we are dealing with a -deadline task, we must
  890. * decide where to wake it up.
  891. * If it has a later deadline and the current task
  892. * on this rq can't move (provided the waking task
  893. * can!) we prefer to send it somewhere else. On the
  894. * other hand, if it has a shorter deadline, we
  895. * try to make it stay here, it might be important.
  896. */
  897. if (unlikely(dl_task(curr)) &&
  898. (curr->nr_cpus_allowed < 2 ||
  899. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  900. (p->nr_cpus_allowed > 1)) {
  901. int target = find_later_rq(p);
  902. if (target != -1 &&
  903. dl_time_before(p->dl.deadline,
  904. cpu_rq(target)->dl.earliest_dl.curr))
  905. cpu = target;
  906. }
  907. rcu_read_unlock();
  908. out:
  909. return cpu;
  910. }
  911. static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  912. {
  913. /*
  914. * Current can't be migrated, useless to reschedule,
  915. * let's hope p can move out.
  916. */
  917. if (rq->curr->nr_cpus_allowed == 1 ||
  918. cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
  919. return;
  920. /*
  921. * p is migratable, so let's not schedule it and
  922. * see if it is pushed or pulled somewhere else.
  923. */
  924. if (p->nr_cpus_allowed != 1 &&
  925. cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
  926. return;
  927. resched_curr(rq);
  928. }
  929. #endif /* CONFIG_SMP */
  930. /*
  931. * Only called when both the current and waking task are -deadline
  932. * tasks.
  933. */
  934. static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
  935. int flags)
  936. {
  937. if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
  938. resched_curr(rq);
  939. return;
  940. }
  941. #ifdef CONFIG_SMP
  942. /*
  943. * In the unlikely case current and p have the same deadline
  944. * let us try to decide what's the best thing to do...
  945. */
  946. if ((p->dl.deadline == rq->curr->dl.deadline) &&
  947. !test_tsk_need_resched(rq->curr))
  948. check_preempt_equal_dl(rq, p);
  949. #endif /* CONFIG_SMP */
  950. }
  951. #ifdef CONFIG_SCHED_HRTICK
  952. static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
  953. {
  954. hrtick_start(rq, p->dl.runtime);
  955. }
  956. #else /* !CONFIG_SCHED_HRTICK */
  957. static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
  958. {
  959. }
  960. #endif
  961. static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
  962. struct dl_rq *dl_rq)
  963. {
  964. struct rb_node *left = dl_rq->rb_leftmost;
  965. if (!left)
  966. return NULL;
  967. return rb_entry(left, struct sched_dl_entity, rb_node);
  968. }
  969. struct task_struct *pick_next_task_dl(struct rq *rq, struct task_struct *prev)
  970. {
  971. struct sched_dl_entity *dl_se;
  972. struct task_struct *p;
  973. struct dl_rq *dl_rq;
  974. dl_rq = &rq->dl;
  975. if (need_pull_dl_task(rq, prev)) {
  976. /*
  977. * This is OK, because current is on_cpu, which avoids it being
  978. * picked for load-balance and preemption/IRQs are still
  979. * disabled avoiding further scheduler activity on it and we're
  980. * being very careful to re-start the picking loop.
  981. */
  982. lockdep_unpin_lock(&rq->lock);
  983. pull_dl_task(rq);
  984. lockdep_pin_lock(&rq->lock);
  985. /*
  986. * pull_rt_task() can drop (and re-acquire) rq->lock; this
  987. * means a stop task can slip in, in which case we need to
  988. * re-start task selection.
  989. */
  990. if (rq->stop && task_on_rq_queued(rq->stop))
  991. return RETRY_TASK;
  992. }
  993. /*
  994. * When prev is DL, we may throttle it in put_prev_task().
  995. * So, we update time before we check for dl_nr_running.
  996. */
  997. if (prev->sched_class == &dl_sched_class)
  998. update_curr_dl(rq);
  999. if (unlikely(!dl_rq->dl_nr_running))
  1000. return NULL;
  1001. put_prev_task(rq, prev);
  1002. dl_se = pick_next_dl_entity(rq, dl_rq);
  1003. BUG_ON(!dl_se);
  1004. p = dl_task_of(dl_se);
  1005. p->se.exec_start = rq_clock_task(rq);
  1006. /* Running task will never be pushed. */
  1007. dequeue_pushable_dl_task(rq, p);
  1008. if (hrtick_enabled(rq))
  1009. start_hrtick_dl(rq, p);
  1010. queue_push_tasks(rq);
  1011. return p;
  1012. }
  1013. static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
  1014. {
  1015. update_curr_dl(rq);
  1016. if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
  1017. enqueue_pushable_dl_task(rq, p);
  1018. }
  1019. static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
  1020. {
  1021. update_curr_dl(rq);
  1022. /*
  1023. * Even when we have runtime, update_curr_dl() might have resulted in us
  1024. * not being the leftmost task anymore. In that case NEED_RESCHED will
  1025. * be set and schedule() will start a new hrtick for the next task.
  1026. */
  1027. if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
  1028. is_leftmost(p, &rq->dl))
  1029. start_hrtick_dl(rq, p);
  1030. }
  1031. static void task_fork_dl(struct task_struct *p)
  1032. {
  1033. /*
  1034. * SCHED_DEADLINE tasks cannot fork and this is achieved through
  1035. * sched_fork()
  1036. */
  1037. }
  1038. static void task_dead_dl(struct task_struct *p)
  1039. {
  1040. struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
  1041. /*
  1042. * Since we are TASK_DEAD we won't slip out of the domain!
  1043. */
  1044. raw_spin_lock_irq(&dl_b->lock);
  1045. /* XXX we should retain the bw until 0-lag */
  1046. dl_b->total_bw -= p->dl.dl_bw;
  1047. raw_spin_unlock_irq(&dl_b->lock);
  1048. }
  1049. static void set_curr_task_dl(struct rq *rq)
  1050. {
  1051. struct task_struct *p = rq->curr;
  1052. p->se.exec_start = rq_clock_task(rq);
  1053. /* You can't push away the running task */
  1054. dequeue_pushable_dl_task(rq, p);
  1055. }
  1056. #ifdef CONFIG_SMP
  1057. /* Only try algorithms three times */
  1058. #define DL_MAX_TRIES 3
  1059. static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
  1060. {
  1061. if (!task_running(rq, p) &&
  1062. cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
  1063. return 1;
  1064. return 0;
  1065. }
  1066. /* Returns the second earliest -deadline task, NULL otherwise */
  1067. static struct task_struct *pick_next_earliest_dl_task(struct rq *rq, int cpu)
  1068. {
  1069. struct rb_node *next_node = rq->dl.rb_leftmost;
  1070. struct sched_dl_entity *dl_se;
  1071. struct task_struct *p = NULL;
  1072. next_node:
  1073. next_node = rb_next(next_node);
  1074. if (next_node) {
  1075. dl_se = rb_entry(next_node, struct sched_dl_entity, rb_node);
  1076. p = dl_task_of(dl_se);
  1077. if (pick_dl_task(rq, p, cpu))
  1078. return p;
  1079. goto next_node;
  1080. }
  1081. return NULL;
  1082. }
  1083. /*
  1084. * Return the earliest pushable rq's task, which is suitable to be executed
  1085. * on the CPU, NULL otherwise:
  1086. */
  1087. static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
  1088. {
  1089. struct rb_node *next_node = rq->dl.pushable_dl_tasks_leftmost;
  1090. struct task_struct *p = NULL;
  1091. if (!has_pushable_dl_tasks(rq))
  1092. return NULL;
  1093. next_node:
  1094. if (next_node) {
  1095. p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
  1096. if (pick_dl_task(rq, p, cpu))
  1097. return p;
  1098. next_node = rb_next(next_node);
  1099. goto next_node;
  1100. }
  1101. return NULL;
  1102. }
  1103. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
  1104. static int find_later_rq(struct task_struct *task)
  1105. {
  1106. struct sched_domain *sd;
  1107. struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
  1108. int this_cpu = smp_processor_id();
  1109. int best_cpu, cpu = task_cpu(task);
  1110. /* Make sure the mask is initialized first */
  1111. if (unlikely(!later_mask))
  1112. return -1;
  1113. if (task->nr_cpus_allowed == 1)
  1114. return -1;
  1115. /*
  1116. * We have to consider system topology and task affinity
  1117. * first, then we can look for a suitable cpu.
  1118. */
  1119. best_cpu = cpudl_find(&task_rq(task)->rd->cpudl,
  1120. task, later_mask);
  1121. if (best_cpu == -1)
  1122. return -1;
  1123. /*
  1124. * If we are here, some target has been found,
  1125. * the most suitable of which is cached in best_cpu.
  1126. * This is, among the runqueues where the current tasks
  1127. * have later deadlines than the task's one, the rq
  1128. * with the latest possible one.
  1129. *
  1130. * Now we check how well this matches with task's
  1131. * affinity and system topology.
  1132. *
  1133. * The last cpu where the task run is our first
  1134. * guess, since it is most likely cache-hot there.
  1135. */
  1136. if (cpumask_test_cpu(cpu, later_mask))
  1137. return cpu;
  1138. /*
  1139. * Check if this_cpu is to be skipped (i.e., it is
  1140. * not in the mask) or not.
  1141. */
  1142. if (!cpumask_test_cpu(this_cpu, later_mask))
  1143. this_cpu = -1;
  1144. rcu_read_lock();
  1145. for_each_domain(cpu, sd) {
  1146. if (sd->flags & SD_WAKE_AFFINE) {
  1147. /*
  1148. * If possible, preempting this_cpu is
  1149. * cheaper than migrating.
  1150. */
  1151. if (this_cpu != -1 &&
  1152. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  1153. rcu_read_unlock();
  1154. return this_cpu;
  1155. }
  1156. /*
  1157. * Last chance: if best_cpu is valid and is
  1158. * in the mask, that becomes our choice.
  1159. */
  1160. if (best_cpu < nr_cpu_ids &&
  1161. cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
  1162. rcu_read_unlock();
  1163. return best_cpu;
  1164. }
  1165. }
  1166. }
  1167. rcu_read_unlock();
  1168. /*
  1169. * At this point, all our guesses failed, we just return
  1170. * 'something', and let the caller sort the things out.
  1171. */
  1172. if (this_cpu != -1)
  1173. return this_cpu;
  1174. cpu = cpumask_any(later_mask);
  1175. if (cpu < nr_cpu_ids)
  1176. return cpu;
  1177. return -1;
  1178. }
  1179. /* Locks the rq it finds */
  1180. static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
  1181. {
  1182. struct rq *later_rq = NULL;
  1183. int tries;
  1184. int cpu;
  1185. for (tries = 0; tries < DL_MAX_TRIES; tries++) {
  1186. cpu = find_later_rq(task);
  1187. if ((cpu == -1) || (cpu == rq->cpu))
  1188. break;
  1189. later_rq = cpu_rq(cpu);
  1190. if (!dl_time_before(task->dl.deadline,
  1191. later_rq->dl.earliest_dl.curr)) {
  1192. /*
  1193. * Target rq has tasks of equal or earlier deadline,
  1194. * retrying does not release any lock and is unlikely
  1195. * to yield a different result.
  1196. */
  1197. later_rq = NULL;
  1198. break;
  1199. }
  1200. /* Retry if something changed. */
  1201. if (double_lock_balance(rq, later_rq)) {
  1202. if (unlikely(task_rq(task) != rq ||
  1203. !cpumask_test_cpu(later_rq->cpu,
  1204. &task->cpus_allowed) ||
  1205. task_running(rq, task) ||
  1206. !task_on_rq_queued(task))) {
  1207. double_unlock_balance(rq, later_rq);
  1208. later_rq = NULL;
  1209. break;
  1210. }
  1211. }
  1212. /*
  1213. * If the rq we found has no -deadline task, or
  1214. * its earliest one has a later deadline than our
  1215. * task, the rq is a good one.
  1216. */
  1217. if (!later_rq->dl.dl_nr_running ||
  1218. dl_time_before(task->dl.deadline,
  1219. later_rq->dl.earliest_dl.curr))
  1220. break;
  1221. /* Otherwise we try again. */
  1222. double_unlock_balance(rq, later_rq);
  1223. later_rq = NULL;
  1224. }
  1225. return later_rq;
  1226. }
  1227. static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  1228. {
  1229. struct task_struct *p;
  1230. if (!has_pushable_dl_tasks(rq))
  1231. return NULL;
  1232. p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
  1233. struct task_struct, pushable_dl_tasks);
  1234. BUG_ON(rq->cpu != task_cpu(p));
  1235. BUG_ON(task_current(rq, p));
  1236. BUG_ON(p->nr_cpus_allowed <= 1);
  1237. BUG_ON(!task_on_rq_queued(p));
  1238. BUG_ON(!dl_task(p));
  1239. return p;
  1240. }
  1241. /*
  1242. * See if the non running -deadline tasks on this rq
  1243. * can be sent to some other CPU where they can preempt
  1244. * and start executing.
  1245. */
  1246. static int push_dl_task(struct rq *rq)
  1247. {
  1248. struct task_struct *next_task;
  1249. struct rq *later_rq;
  1250. int ret = 0;
  1251. if (!rq->dl.overloaded)
  1252. return 0;
  1253. next_task = pick_next_pushable_dl_task(rq);
  1254. if (!next_task)
  1255. return 0;
  1256. retry:
  1257. if (unlikely(next_task == rq->curr)) {
  1258. WARN_ON(1);
  1259. return 0;
  1260. }
  1261. /*
  1262. * If next_task preempts rq->curr, and rq->curr
  1263. * can move away, it makes sense to just reschedule
  1264. * without going further in pushing next_task.
  1265. */
  1266. if (dl_task(rq->curr) &&
  1267. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  1268. rq->curr->nr_cpus_allowed > 1) {
  1269. resched_curr(rq);
  1270. return 0;
  1271. }
  1272. /* We might release rq lock */
  1273. get_task_struct(next_task);
  1274. /* Will lock the rq it'll find */
  1275. later_rq = find_lock_later_rq(next_task, rq);
  1276. if (!later_rq) {
  1277. struct task_struct *task;
  1278. /*
  1279. * We must check all this again, since
  1280. * find_lock_later_rq releases rq->lock and it is
  1281. * then possible that next_task has migrated.
  1282. */
  1283. task = pick_next_pushable_dl_task(rq);
  1284. if (task_cpu(next_task) == rq->cpu && task == next_task) {
  1285. /*
  1286. * The task is still there. We don't try
  1287. * again, some other cpu will pull it when ready.
  1288. */
  1289. goto out;
  1290. }
  1291. if (!task)
  1292. /* No more tasks */
  1293. goto out;
  1294. put_task_struct(next_task);
  1295. next_task = task;
  1296. goto retry;
  1297. }
  1298. deactivate_task(rq, next_task, 0);
  1299. set_task_cpu(next_task, later_rq->cpu);
  1300. activate_task(later_rq, next_task, 0);
  1301. ret = 1;
  1302. resched_curr(later_rq);
  1303. double_unlock_balance(rq, later_rq);
  1304. out:
  1305. put_task_struct(next_task);
  1306. return ret;
  1307. }
  1308. static void push_dl_tasks(struct rq *rq)
  1309. {
  1310. /* Terminates as it moves a -deadline task */
  1311. while (push_dl_task(rq))
  1312. ;
  1313. }
  1314. static void pull_dl_task(struct rq *this_rq)
  1315. {
  1316. int this_cpu = this_rq->cpu, cpu;
  1317. struct task_struct *p;
  1318. bool resched = false;
  1319. struct rq *src_rq;
  1320. u64 dmin = LONG_MAX;
  1321. if (likely(!dl_overloaded(this_rq)))
  1322. return;
  1323. /*
  1324. * Match the barrier from dl_set_overloaded; this guarantees that if we
  1325. * see overloaded we must also see the dlo_mask bit.
  1326. */
  1327. smp_rmb();
  1328. for_each_cpu(cpu, this_rq->rd->dlo_mask) {
  1329. if (this_cpu == cpu)
  1330. continue;
  1331. src_rq = cpu_rq(cpu);
  1332. /*
  1333. * It looks racy, abd it is! However, as in sched_rt.c,
  1334. * we are fine with this.
  1335. */
  1336. if (this_rq->dl.dl_nr_running &&
  1337. dl_time_before(this_rq->dl.earliest_dl.curr,
  1338. src_rq->dl.earliest_dl.next))
  1339. continue;
  1340. /* Might drop this_rq->lock */
  1341. double_lock_balance(this_rq, src_rq);
  1342. /*
  1343. * If there are no more pullable tasks on the
  1344. * rq, we're done with it.
  1345. */
  1346. if (src_rq->dl.dl_nr_running <= 1)
  1347. goto skip;
  1348. p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
  1349. /*
  1350. * We found a task to be pulled if:
  1351. * - it preempts our current (if there's one),
  1352. * - it will preempt the last one we pulled (if any).
  1353. */
  1354. if (p && dl_time_before(p->dl.deadline, dmin) &&
  1355. (!this_rq->dl.dl_nr_running ||
  1356. dl_time_before(p->dl.deadline,
  1357. this_rq->dl.earliest_dl.curr))) {
  1358. WARN_ON(p == src_rq->curr);
  1359. WARN_ON(!task_on_rq_queued(p));
  1360. /*
  1361. * Then we pull iff p has actually an earlier
  1362. * deadline than the current task of its runqueue.
  1363. */
  1364. if (dl_time_before(p->dl.deadline,
  1365. src_rq->curr->dl.deadline))
  1366. goto skip;
  1367. resched = true;
  1368. deactivate_task(src_rq, p, 0);
  1369. set_task_cpu(p, this_cpu);
  1370. activate_task(this_rq, p, 0);
  1371. dmin = p->dl.deadline;
  1372. /* Is there any other task even earlier? */
  1373. }
  1374. skip:
  1375. double_unlock_balance(this_rq, src_rq);
  1376. }
  1377. if (resched)
  1378. resched_curr(this_rq);
  1379. }
  1380. /*
  1381. * Since the task is not running and a reschedule is not going to happen
  1382. * anytime soon on its runqueue, we try pushing it away now.
  1383. */
  1384. static void task_woken_dl(struct rq *rq, struct task_struct *p)
  1385. {
  1386. if (!task_running(rq, p) &&
  1387. !test_tsk_need_resched(rq->curr) &&
  1388. has_pushable_dl_tasks(rq) &&
  1389. p->nr_cpus_allowed > 1 &&
  1390. dl_task(rq->curr) &&
  1391. (rq->curr->nr_cpus_allowed < 2 ||
  1392. !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
  1393. push_dl_tasks(rq);
  1394. }
  1395. }
  1396. static void set_cpus_allowed_dl(struct task_struct *p,
  1397. const struct cpumask *new_mask)
  1398. {
  1399. struct rq *rq;
  1400. struct root_domain *src_rd;
  1401. int weight;
  1402. BUG_ON(!dl_task(p));
  1403. rq = task_rq(p);
  1404. src_rd = rq->rd;
  1405. /*
  1406. * Migrating a SCHED_DEADLINE task between exclusive
  1407. * cpusets (different root_domains) entails a bandwidth
  1408. * update. We already made space for us in the destination
  1409. * domain (see cpuset_can_attach()).
  1410. */
  1411. if (!cpumask_intersects(src_rd->span, new_mask)) {
  1412. struct dl_bw *src_dl_b;
  1413. src_dl_b = dl_bw_of(cpu_of(rq));
  1414. /*
  1415. * We now free resources of the root_domain we are migrating
  1416. * off. In the worst case, sched_setattr() may temporary fail
  1417. * until we complete the update.
  1418. */
  1419. raw_spin_lock(&src_dl_b->lock);
  1420. __dl_clear(src_dl_b, p->dl.dl_bw);
  1421. raw_spin_unlock(&src_dl_b->lock);
  1422. }
  1423. /*
  1424. * Update only if the task is actually running (i.e.,
  1425. * it is on the rq AND it is not throttled).
  1426. */
  1427. if (!on_dl_rq(&p->dl))
  1428. return;
  1429. weight = cpumask_weight(new_mask);
  1430. /*
  1431. * Only update if the process changes its state from whether it
  1432. * can migrate or not.
  1433. */
  1434. if ((p->nr_cpus_allowed > 1) == (weight > 1))
  1435. return;
  1436. /*
  1437. * The process used to be able to migrate OR it can now migrate
  1438. */
  1439. if (weight <= 1) {
  1440. if (!task_current(rq, p))
  1441. dequeue_pushable_dl_task(rq, p);
  1442. BUG_ON(!rq->dl.dl_nr_migratory);
  1443. rq->dl.dl_nr_migratory--;
  1444. } else {
  1445. if (!task_current(rq, p))
  1446. enqueue_pushable_dl_task(rq, p);
  1447. rq->dl.dl_nr_migratory++;
  1448. }
  1449. update_dl_migration(&rq->dl);
  1450. }
  1451. /* Assumes rq->lock is held */
  1452. static void rq_online_dl(struct rq *rq)
  1453. {
  1454. if (rq->dl.overloaded)
  1455. dl_set_overload(rq);
  1456. cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
  1457. if (rq->dl.dl_nr_running > 0)
  1458. cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr, 1);
  1459. }
  1460. /* Assumes rq->lock is held */
  1461. static void rq_offline_dl(struct rq *rq)
  1462. {
  1463. if (rq->dl.overloaded)
  1464. dl_clear_overload(rq);
  1465. cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
  1466. cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
  1467. }
  1468. void __init init_sched_dl_class(void)
  1469. {
  1470. unsigned int i;
  1471. for_each_possible_cpu(i)
  1472. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
  1473. GFP_KERNEL, cpu_to_node(i));
  1474. }
  1475. #endif /* CONFIG_SMP */
  1476. static void switched_from_dl(struct rq *rq, struct task_struct *p)
  1477. {
  1478. /*
  1479. * Start the deadline timer; if we switch back to dl before this we'll
  1480. * continue consuming our current CBS slice. If we stay outside of
  1481. * SCHED_DEADLINE until the deadline passes, the timer will reset the
  1482. * task.
  1483. */
  1484. if (!start_dl_timer(p))
  1485. __dl_clear_params(p);
  1486. /*
  1487. * Since this might be the only -deadline task on the rq,
  1488. * this is the right place to try to pull some other one
  1489. * from an overloaded cpu, if any.
  1490. */
  1491. if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
  1492. return;
  1493. queue_pull_task(rq);
  1494. }
  1495. /*
  1496. * When switching to -deadline, we may overload the rq, then
  1497. * we try to push someone off, if possible.
  1498. */
  1499. static void switched_to_dl(struct rq *rq, struct task_struct *p)
  1500. {
  1501. if (task_on_rq_queued(p) && rq->curr != p) {
  1502. #ifdef CONFIG_SMP
  1503. if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
  1504. queue_push_tasks(rq);
  1505. #else
  1506. if (dl_task(rq->curr))
  1507. check_preempt_curr_dl(rq, p, 0);
  1508. else
  1509. resched_curr(rq);
  1510. #endif
  1511. }
  1512. }
  1513. /*
  1514. * If the scheduling parameters of a -deadline task changed,
  1515. * a push or pull operation might be needed.
  1516. */
  1517. static void prio_changed_dl(struct rq *rq, struct task_struct *p,
  1518. int oldprio)
  1519. {
  1520. if (task_on_rq_queued(p) || rq->curr == p) {
  1521. #ifdef CONFIG_SMP
  1522. /*
  1523. * This might be too much, but unfortunately
  1524. * we don't have the old deadline value, and
  1525. * we can't argue if the task is increasing
  1526. * or lowering its prio, so...
  1527. */
  1528. if (!rq->dl.overloaded)
  1529. queue_pull_task(rq);
  1530. /*
  1531. * If we now have a earlier deadline task than p,
  1532. * then reschedule, provided p is still on this
  1533. * runqueue.
  1534. */
  1535. if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
  1536. resched_curr(rq);
  1537. #else
  1538. /*
  1539. * Again, we don't know if p has a earlier
  1540. * or later deadline, so let's blindly set a
  1541. * (maybe not needed) rescheduling point.
  1542. */
  1543. resched_curr(rq);
  1544. #endif /* CONFIG_SMP */
  1545. } else
  1546. switched_to_dl(rq, p);
  1547. }
  1548. const struct sched_class dl_sched_class = {
  1549. .next = &rt_sched_class,
  1550. .enqueue_task = enqueue_task_dl,
  1551. .dequeue_task = dequeue_task_dl,
  1552. .yield_task = yield_task_dl,
  1553. .check_preempt_curr = check_preempt_curr_dl,
  1554. .pick_next_task = pick_next_task_dl,
  1555. .put_prev_task = put_prev_task_dl,
  1556. #ifdef CONFIG_SMP
  1557. .select_task_rq = select_task_rq_dl,
  1558. .set_cpus_allowed = set_cpus_allowed_dl,
  1559. .rq_online = rq_online_dl,
  1560. .rq_offline = rq_offline_dl,
  1561. .task_woken = task_woken_dl,
  1562. #endif
  1563. .set_curr_task = set_curr_task_dl,
  1564. .task_tick = task_tick_dl,
  1565. .task_fork = task_fork_dl,
  1566. .task_dead = task_dead_dl,
  1567. .prio_changed = prio_changed_dl,
  1568. .switched_from = switched_from_dl,
  1569. .switched_to = switched_to_dl,
  1570. .update_curr = update_curr_dl,
  1571. };
  1572. #ifdef CONFIG_SCHED_DEBUG
  1573. extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
  1574. void print_dl_stats(struct seq_file *m, int cpu)
  1575. {
  1576. print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
  1577. }
  1578. #endif /* CONFIG_SCHED_DEBUG */