deadline.c 46 KB

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