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 (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. 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_se->dl_new || dl_se->dl_throttled);
  289. /*
  290. * We use the regular wall clock time to set deadlines in the
  291. * future; in fact, we must consider execution overheads (time
  292. * spent on hardirq context, etc.).
  293. */
  294. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  295. dl_se->runtime = pi_se->dl_runtime;
  296. dl_se->dl_new = 0;
  297. }
  298. /*
  299. * Pure Earliest Deadline First (EDF) scheduling does not deal with the
  300. * possibility of a entity lasting more than what it declared, and thus
  301. * exhausting its runtime.
  302. *
  303. * Here we are interested in making runtime overrun possible, but we do
  304. * not want a entity which is misbehaving to affect the scheduling of all
  305. * other entities.
  306. * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
  307. * is used, in order to confine each entity within its own bandwidth.
  308. *
  309. * This function deals exactly with that, and ensures that when the runtime
  310. * of a entity is replenished, its deadline is also postponed. That ensures
  311. * the overrunning entity can't interfere with other entity in the system and
  312. * can't make them miss their deadlines. Reasons why this kind of overruns
  313. * could happen are, typically, a entity voluntarily trying to overcome its
  314. * runtime, or it just underestimated it during sched_setattr().
  315. */
  316. static void replenish_dl_entity(struct sched_dl_entity *dl_se,
  317. struct sched_dl_entity *pi_se)
  318. {
  319. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  320. struct rq *rq = rq_of_dl_rq(dl_rq);
  321. BUG_ON(pi_se->dl_runtime <= 0);
  322. /*
  323. * This could be the case for a !-dl task that is boosted.
  324. * Just go with full inherited parameters.
  325. */
  326. if (dl_se->dl_deadline == 0) {
  327. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  328. dl_se->runtime = pi_se->dl_runtime;
  329. }
  330. /*
  331. * We keep moving the deadline away until we get some
  332. * available runtime for the entity. This ensures correct
  333. * handling of situations where the runtime overrun is
  334. * arbitrary large.
  335. */
  336. while (dl_se->runtime <= 0) {
  337. dl_se->deadline += pi_se->dl_period;
  338. dl_se->runtime += pi_se->dl_runtime;
  339. }
  340. /*
  341. * At this point, the deadline really should be "in
  342. * the future" with respect to rq->clock. If it's
  343. * not, we are, for some reason, lagging too much!
  344. * Anyway, after having warn userspace abut that,
  345. * we still try to keep the things running by
  346. * resetting the deadline and the budget of the
  347. * entity.
  348. */
  349. if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
  350. printk_deferred_once("sched: DL replenish lagged to much\n");
  351. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  352. dl_se->runtime = pi_se->dl_runtime;
  353. }
  354. if (dl_se->dl_yielded)
  355. dl_se->dl_yielded = 0;
  356. if (dl_se->dl_throttled)
  357. dl_se->dl_throttled = 0;
  358. }
  359. /*
  360. * Here we check if --at time t-- an entity (which is probably being
  361. * [re]activated or, in general, enqueued) can use its remaining runtime
  362. * and its current deadline _without_ exceeding the bandwidth it is
  363. * assigned (function returns true if it can't). We are in fact applying
  364. * one of the CBS rules: when a task wakes up, if the residual runtime
  365. * over residual deadline fits within the allocated bandwidth, then we
  366. * can keep the current (absolute) deadline and residual budget without
  367. * disrupting the schedulability of the system. Otherwise, we should
  368. * refill the runtime and set the deadline a period in the future,
  369. * because keeping the current (absolute) deadline of the task would
  370. * result in breaking guarantees promised to other tasks (refer to
  371. * Documentation/scheduler/sched-deadline.txt for more informations).
  372. *
  373. * This function returns true if:
  374. *
  375. * runtime / (deadline - t) > dl_runtime / dl_period ,
  376. *
  377. * IOW we can't recycle current parameters.
  378. *
  379. * Notice that the bandwidth check is done against the period. For
  380. * task with deadline equal to period this is the same of using
  381. * dl_deadline instead of dl_period in the equation above.
  382. */
  383. static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
  384. struct sched_dl_entity *pi_se, u64 t)
  385. {
  386. u64 left, right;
  387. /*
  388. * left and right are the two sides of the equation above,
  389. * after a bit of shuffling to use multiplications instead
  390. * of divisions.
  391. *
  392. * Note that none of the time values involved in the two
  393. * multiplications are absolute: dl_deadline and dl_runtime
  394. * are the relative deadline and the maximum runtime of each
  395. * instance, runtime is the runtime left for the last instance
  396. * and (deadline - t), since t is rq->clock, is the time left
  397. * to the (absolute) deadline. Even if overflowing the u64 type
  398. * is very unlikely to occur in both cases, here we scale down
  399. * as we want to avoid that risk at all. Scaling down by 10
  400. * means that we reduce granularity to 1us. We are fine with it,
  401. * since this is only a true/false check and, anyway, thinking
  402. * of anything below microseconds resolution is actually fiction
  403. * (but still we want to give the user that illusion >;).
  404. */
  405. left = (pi_se->dl_period >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
  406. right = ((dl_se->deadline - t) >> DL_SCALE) *
  407. (pi_se->dl_runtime >> DL_SCALE);
  408. return dl_time_before(right, left);
  409. }
  410. /*
  411. * When a -deadline entity is queued back on the runqueue, its runtime and
  412. * deadline might need updating.
  413. *
  414. * The policy here is that we update the deadline of the entity only if:
  415. * - the current deadline is in the past,
  416. * - using the remaining runtime with the current deadline would make
  417. * the entity exceed its bandwidth.
  418. */
  419. static void update_dl_entity(struct sched_dl_entity *dl_se,
  420. struct sched_dl_entity *pi_se)
  421. {
  422. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  423. struct rq *rq = rq_of_dl_rq(dl_rq);
  424. /*
  425. * The arrival of a new instance needs special treatment, i.e.,
  426. * the actual scheduling parameters have to be "renewed".
  427. */
  428. if (dl_se->dl_new) {
  429. setup_new_dl_entity(dl_se, pi_se);
  430. return;
  431. }
  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. unsigned long flags;
  507. struct rq *rq;
  508. rq = task_rq_lock(p, &flags);
  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. * This is possible if switched_from_dl() raced against a running
  519. * callback that took the above !dl_task() path and we've since then
  520. * switched back into SCHED_DEADLINE.
  521. *
  522. * There's nothing to do except drop our task reference.
  523. */
  524. if (dl_se->dl_new)
  525. goto unlock;
  526. /*
  527. * The task might have been boosted by someone else and might be in the
  528. * boosting/deboosting path, its not throttled.
  529. */
  530. if (dl_se->dl_boosted)
  531. goto unlock;
  532. /*
  533. * Spurious timer due to start_dl_timer() race; or we already received
  534. * a replenishment from rt_mutex_setprio().
  535. */
  536. if (!dl_se->dl_throttled)
  537. goto unlock;
  538. sched_clock_tick();
  539. update_rq_clock(rq);
  540. /*
  541. * If the throttle happened during sched-out; like:
  542. *
  543. * schedule()
  544. * deactivate_task()
  545. * dequeue_task_dl()
  546. * update_curr_dl()
  547. * start_dl_timer()
  548. * __dequeue_task_dl()
  549. * prev->on_rq = 0;
  550. *
  551. * We can be both throttled and !queued. Replenish the counter
  552. * but do not enqueue -- wait for our wakeup to do that.
  553. */
  554. if (!task_on_rq_queued(p)) {
  555. replenish_dl_entity(dl_se, dl_se);
  556. goto unlock;
  557. }
  558. enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
  559. if (dl_task(rq->curr))
  560. check_preempt_curr_dl(rq, p, 0);
  561. else
  562. resched_curr(rq);
  563. #ifdef CONFIG_SMP
  564. /*
  565. * Perform balancing operations here; after the replenishments. We
  566. * cannot drop rq->lock before this, otherwise the assertion in
  567. * start_dl_timer() about not missing updates is not true.
  568. *
  569. * If we find that the rq the task was on is no longer available, we
  570. * need to select a new rq.
  571. *
  572. * XXX figure out if select_task_rq_dl() deals with offline cpus.
  573. */
  574. if (unlikely(!rq->online))
  575. rq = dl_task_offline_migration(rq, p);
  576. /*
  577. * Queueing this task back might have overloaded rq, check if we need
  578. * to kick someone away.
  579. */
  580. if (has_pushable_dl_tasks(rq)) {
  581. /*
  582. * Nothing relies on rq->lock after this, so its safe to drop
  583. * rq->lock.
  584. */
  585. lockdep_unpin_lock(&rq->lock);
  586. push_dl_task(rq);
  587. lockdep_pin_lock(&rq->lock);
  588. }
  589. #endif
  590. unlock:
  591. task_rq_unlock(rq, p, &flags);
  592. /*
  593. * This can free the task_struct, including this hrtimer, do not touch
  594. * anything related to that after this.
  595. */
  596. put_task_struct(p);
  597. return HRTIMER_NORESTART;
  598. }
  599. void init_dl_task_timer(struct sched_dl_entity *dl_se)
  600. {
  601. struct hrtimer *timer = &dl_se->dl_timer;
  602. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  603. timer->function = dl_task_timer;
  604. }
  605. static
  606. int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
  607. {
  608. return (dl_se->runtime <= 0);
  609. }
  610. extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
  611. /*
  612. * Update the current task's runtime statistics (provided it is still
  613. * a -deadline task and has not been removed from the dl_rq).
  614. */
  615. static void update_curr_dl(struct rq *rq)
  616. {
  617. struct task_struct *curr = rq->curr;
  618. struct sched_dl_entity *dl_se = &curr->dl;
  619. u64 delta_exec;
  620. if (!dl_task(curr) || !on_dl_rq(dl_se))
  621. return;
  622. /*
  623. * Consumed budget is computed considering the time as
  624. * observed by schedulable tasks (excluding time spent
  625. * in hardirq context, etc.). Deadlines are instead
  626. * computed using hard walltime. This seems to be the more
  627. * natural solution, but the full ramifications of this
  628. * approach need further study.
  629. */
  630. delta_exec = rq_clock_task(rq) - curr->se.exec_start;
  631. if (unlikely((s64)delta_exec <= 0))
  632. return;
  633. schedstat_set(curr->se.statistics.exec_max,
  634. max(curr->se.statistics.exec_max, delta_exec));
  635. curr->se.sum_exec_runtime += delta_exec;
  636. account_group_exec_runtime(curr, delta_exec);
  637. curr->se.exec_start = rq_clock_task(rq);
  638. cpuacct_charge(curr, delta_exec);
  639. sched_rt_avg_update(rq, delta_exec);
  640. dl_se->runtime -= dl_se->dl_yielded ? 0 : delta_exec;
  641. if (dl_runtime_exceeded(dl_se)) {
  642. dl_se->dl_throttled = 1;
  643. __dequeue_task_dl(rq, curr, 0);
  644. if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr)))
  645. enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
  646. if (!is_leftmost(curr, &rq->dl))
  647. resched_curr(rq);
  648. }
  649. /*
  650. * Because -- for now -- we share the rt bandwidth, we need to
  651. * account our runtime there too, otherwise actual rt tasks
  652. * would be able to exceed the shared quota.
  653. *
  654. * Account to the root rt group for now.
  655. *
  656. * The solution we're working towards is having the RT groups scheduled
  657. * using deadline servers -- however there's a few nasties to figure
  658. * out before that can happen.
  659. */
  660. if (rt_bandwidth_enabled()) {
  661. struct rt_rq *rt_rq = &rq->rt;
  662. raw_spin_lock(&rt_rq->rt_runtime_lock);
  663. /*
  664. * We'll let actual RT tasks worry about the overflow here, we
  665. * have our own CBS to keep us inline; only account when RT
  666. * bandwidth is relevant.
  667. */
  668. if (sched_rt_bandwidth_account(rt_rq))
  669. rt_rq->rt_time += delta_exec;
  670. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  671. }
  672. }
  673. #ifdef CONFIG_SMP
  674. static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  675. {
  676. struct rq *rq = rq_of_dl_rq(dl_rq);
  677. if (dl_rq->earliest_dl.curr == 0 ||
  678. dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
  679. dl_rq->earliest_dl.curr = deadline;
  680. cpudl_set(&rq->rd->cpudl, rq->cpu, deadline, 1);
  681. }
  682. }
  683. static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  684. {
  685. struct rq *rq = rq_of_dl_rq(dl_rq);
  686. /*
  687. * Since we may have removed our earliest (and/or next earliest)
  688. * task we must recompute them.
  689. */
  690. if (!dl_rq->dl_nr_running) {
  691. dl_rq->earliest_dl.curr = 0;
  692. dl_rq->earliest_dl.next = 0;
  693. cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
  694. } else {
  695. struct rb_node *leftmost = dl_rq->rb_leftmost;
  696. struct sched_dl_entity *entry;
  697. entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
  698. dl_rq->earliest_dl.curr = entry->deadline;
  699. cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline, 1);
  700. }
  701. }
  702. #else
  703. static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  704. static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  705. #endif /* CONFIG_SMP */
  706. static inline
  707. void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  708. {
  709. int prio = dl_task_of(dl_se)->prio;
  710. u64 deadline = dl_se->deadline;
  711. WARN_ON(!dl_prio(prio));
  712. dl_rq->dl_nr_running++;
  713. add_nr_running(rq_of_dl_rq(dl_rq), 1);
  714. inc_dl_deadline(dl_rq, deadline);
  715. inc_dl_migration(dl_se, dl_rq);
  716. }
  717. static inline
  718. void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  719. {
  720. int prio = dl_task_of(dl_se)->prio;
  721. WARN_ON(!dl_prio(prio));
  722. WARN_ON(!dl_rq->dl_nr_running);
  723. dl_rq->dl_nr_running--;
  724. sub_nr_running(rq_of_dl_rq(dl_rq), 1);
  725. dec_dl_deadline(dl_rq, dl_se->deadline);
  726. dec_dl_migration(dl_se, dl_rq);
  727. }
  728. static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
  729. {
  730. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  731. struct rb_node **link = &dl_rq->rb_root.rb_node;
  732. struct rb_node *parent = NULL;
  733. struct sched_dl_entity *entry;
  734. int leftmost = 1;
  735. BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
  736. while (*link) {
  737. parent = *link;
  738. entry = rb_entry(parent, struct sched_dl_entity, rb_node);
  739. if (dl_time_before(dl_se->deadline, entry->deadline))
  740. link = &parent->rb_left;
  741. else {
  742. link = &parent->rb_right;
  743. leftmost = 0;
  744. }
  745. }
  746. if (leftmost)
  747. dl_rq->rb_leftmost = &dl_se->rb_node;
  748. rb_link_node(&dl_se->rb_node, parent, link);
  749. rb_insert_color(&dl_se->rb_node, &dl_rq->rb_root);
  750. inc_dl_tasks(dl_se, dl_rq);
  751. }
  752. static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
  753. {
  754. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  755. if (RB_EMPTY_NODE(&dl_se->rb_node))
  756. return;
  757. if (dl_rq->rb_leftmost == &dl_se->rb_node) {
  758. struct rb_node *next_node;
  759. next_node = rb_next(&dl_se->rb_node);
  760. dl_rq->rb_leftmost = next_node;
  761. }
  762. rb_erase(&dl_se->rb_node, &dl_rq->rb_root);
  763. RB_CLEAR_NODE(&dl_se->rb_node);
  764. dec_dl_tasks(dl_se, dl_rq);
  765. }
  766. static void
  767. enqueue_dl_entity(struct sched_dl_entity *dl_se,
  768. struct sched_dl_entity *pi_se, int flags)
  769. {
  770. BUG_ON(on_dl_rq(dl_se));
  771. /*
  772. * If this is a wakeup or a new instance, the scheduling
  773. * parameters of the task might need updating. Otherwise,
  774. * we want a replenishment of its runtime.
  775. */
  776. if (dl_se->dl_new || flags & ENQUEUE_WAKEUP)
  777. update_dl_entity(dl_se, pi_se);
  778. else if (flags & ENQUEUE_REPLENISH)
  779. replenish_dl_entity(dl_se, pi_se);
  780. __enqueue_dl_entity(dl_se);
  781. }
  782. static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
  783. {
  784. __dequeue_dl_entity(dl_se);
  785. }
  786. static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  787. {
  788. struct task_struct *pi_task = rt_mutex_get_top_task(p);
  789. struct sched_dl_entity *pi_se = &p->dl;
  790. /*
  791. * Use the scheduling parameters of the top pi-waiter
  792. * task if we have one and its (absolute) deadline is
  793. * smaller than our one... OTW we keep our runtime and
  794. * deadline.
  795. */
  796. if (pi_task && p->dl.dl_boosted && dl_prio(pi_task->normal_prio)) {
  797. pi_se = &pi_task->dl;
  798. } else if (!dl_prio(p->normal_prio)) {
  799. /*
  800. * Special case in which we have a !SCHED_DEADLINE task
  801. * that is going to be deboosted, but exceedes its
  802. * runtime while doing so. No point in replenishing
  803. * it, as it's going to return back to its original
  804. * scheduling class after this.
  805. */
  806. BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
  807. return;
  808. }
  809. /*
  810. * If p is throttled, we do nothing. In fact, if it exhausted
  811. * its budget it needs a replenishment and, since it now is on
  812. * its rq, the bandwidth timer callback (which clearly has not
  813. * run yet) will take care of this.
  814. */
  815. if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH))
  816. return;
  817. enqueue_dl_entity(&p->dl, pi_se, flags);
  818. if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  819. enqueue_pushable_dl_task(rq, p);
  820. }
  821. static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  822. {
  823. dequeue_dl_entity(&p->dl);
  824. dequeue_pushable_dl_task(rq, p);
  825. }
  826. static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  827. {
  828. update_curr_dl(rq);
  829. __dequeue_task_dl(rq, p, flags);
  830. }
  831. /*
  832. * Yield task semantic for -deadline tasks is:
  833. *
  834. * get off from the CPU until our next instance, with
  835. * a new runtime. This is of little use now, since we
  836. * don't have a bandwidth reclaiming mechanism. Anyway,
  837. * bandwidth reclaiming is planned for the future, and
  838. * yield_task_dl will indicate that some spare budget
  839. * is available for other task instances to use it.
  840. */
  841. static void yield_task_dl(struct rq *rq)
  842. {
  843. struct task_struct *p = rq->curr;
  844. /*
  845. * We make the task go to sleep until its current deadline by
  846. * forcing its runtime to zero. This way, update_curr_dl() stops
  847. * it and the bandwidth timer will wake it up and will give it
  848. * new scheduling parameters (thanks to dl_yielded=1).
  849. */
  850. if (p->dl.runtime > 0) {
  851. rq->curr->dl.dl_yielded = 1;
  852. p->dl.runtime = 0;
  853. }
  854. update_rq_clock(rq);
  855. update_curr_dl(rq);
  856. /*
  857. * Tell update_rq_clock() that we've just updated,
  858. * so we don't do microscopic update in schedule()
  859. * and double the fastpath cost.
  860. */
  861. rq_clock_skip_update(rq, true);
  862. }
  863. #ifdef CONFIG_SMP
  864. static int find_later_rq(struct task_struct *task);
  865. static int
  866. select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
  867. {
  868. struct task_struct *curr;
  869. struct rq *rq;
  870. if (sd_flag != SD_BALANCE_WAKE)
  871. goto out;
  872. rq = cpu_rq(cpu);
  873. rcu_read_lock();
  874. curr = READ_ONCE(rq->curr); /* unlocked access */
  875. /*
  876. * If we are dealing with a -deadline task, we must
  877. * decide where to wake it up.
  878. * If it has a later deadline and the current task
  879. * on this rq can't move (provided the waking task
  880. * can!) we prefer to send it somewhere else. On the
  881. * other hand, if it has a shorter deadline, we
  882. * try to make it stay here, it might be important.
  883. */
  884. if (unlikely(dl_task(curr)) &&
  885. (curr->nr_cpus_allowed < 2 ||
  886. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  887. (p->nr_cpus_allowed > 1)) {
  888. int target = find_later_rq(p);
  889. if (target != -1 &&
  890. (dl_time_before(p->dl.deadline,
  891. cpu_rq(target)->dl.earliest_dl.curr) ||
  892. (cpu_rq(target)->dl.dl_nr_running == 0)))
  893. cpu = target;
  894. }
  895. rcu_read_unlock();
  896. out:
  897. return cpu;
  898. }
  899. static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  900. {
  901. /*
  902. * Current can't be migrated, useless to reschedule,
  903. * let's hope p can move out.
  904. */
  905. if (rq->curr->nr_cpus_allowed == 1 ||
  906. cpudl_find(&rq->rd->cpudl, rq->curr, NULL) == -1)
  907. return;
  908. /*
  909. * p is migratable, so let's not schedule it and
  910. * see if it is pushed or pulled somewhere else.
  911. */
  912. if (p->nr_cpus_allowed != 1 &&
  913. cpudl_find(&rq->rd->cpudl, p, NULL) != -1)
  914. return;
  915. resched_curr(rq);
  916. }
  917. #endif /* CONFIG_SMP */
  918. /*
  919. * Only called when both the current and waking task are -deadline
  920. * tasks.
  921. */
  922. static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
  923. int flags)
  924. {
  925. if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
  926. resched_curr(rq);
  927. return;
  928. }
  929. #ifdef CONFIG_SMP
  930. /*
  931. * In the unlikely case current and p have the same deadline
  932. * let us try to decide what's the best thing to do...
  933. */
  934. if ((p->dl.deadline == rq->curr->dl.deadline) &&
  935. !test_tsk_need_resched(rq->curr))
  936. check_preempt_equal_dl(rq, p);
  937. #endif /* CONFIG_SMP */
  938. }
  939. #ifdef CONFIG_SCHED_HRTICK
  940. static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
  941. {
  942. hrtick_start(rq, p->dl.runtime);
  943. }
  944. #else /* !CONFIG_SCHED_HRTICK */
  945. static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
  946. {
  947. }
  948. #endif
  949. static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
  950. struct dl_rq *dl_rq)
  951. {
  952. struct rb_node *left = dl_rq->rb_leftmost;
  953. if (!left)
  954. return NULL;
  955. return rb_entry(left, struct sched_dl_entity, rb_node);
  956. }
  957. struct task_struct *pick_next_task_dl(struct rq *rq, struct task_struct *prev)
  958. {
  959. struct sched_dl_entity *dl_se;
  960. struct task_struct *p;
  961. struct dl_rq *dl_rq;
  962. dl_rq = &rq->dl;
  963. if (need_pull_dl_task(rq, prev)) {
  964. /*
  965. * This is OK, because current is on_cpu, which avoids it being
  966. * picked for load-balance and preemption/IRQs are still
  967. * disabled avoiding further scheduler activity on it and we're
  968. * being very careful to re-start the picking loop.
  969. */
  970. lockdep_unpin_lock(&rq->lock);
  971. pull_dl_task(rq);
  972. lockdep_pin_lock(&rq->lock);
  973. /*
  974. * pull_rt_task() can drop (and re-acquire) rq->lock; this
  975. * means a stop task can slip in, in which case we need to
  976. * re-start task selection.
  977. */
  978. if (rq->stop && task_on_rq_queued(rq->stop))
  979. return RETRY_TASK;
  980. }
  981. /*
  982. * When prev is DL, we may throttle it in put_prev_task().
  983. * So, we update time before we check for dl_nr_running.
  984. */
  985. if (prev->sched_class == &dl_sched_class)
  986. update_curr_dl(rq);
  987. if (unlikely(!dl_rq->dl_nr_running))
  988. return NULL;
  989. put_prev_task(rq, prev);
  990. dl_se = pick_next_dl_entity(rq, dl_rq);
  991. BUG_ON(!dl_se);
  992. p = dl_task_of(dl_se);
  993. p->se.exec_start = rq_clock_task(rq);
  994. /* Running task will never be pushed. */
  995. dequeue_pushable_dl_task(rq, p);
  996. if (hrtick_enabled(rq))
  997. start_hrtick_dl(rq, p);
  998. queue_push_tasks(rq);
  999. return p;
  1000. }
  1001. static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
  1002. {
  1003. update_curr_dl(rq);
  1004. if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
  1005. enqueue_pushable_dl_task(rq, p);
  1006. }
  1007. static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
  1008. {
  1009. update_curr_dl(rq);
  1010. /*
  1011. * Even when we have runtime, update_curr_dl() might have resulted in us
  1012. * not being the leftmost task anymore. In that case NEED_RESCHED will
  1013. * be set and schedule() will start a new hrtick for the next task.
  1014. */
  1015. if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
  1016. is_leftmost(p, &rq->dl))
  1017. start_hrtick_dl(rq, p);
  1018. }
  1019. static void task_fork_dl(struct task_struct *p)
  1020. {
  1021. /*
  1022. * SCHED_DEADLINE tasks cannot fork and this is achieved through
  1023. * sched_fork()
  1024. */
  1025. }
  1026. static void task_dead_dl(struct task_struct *p)
  1027. {
  1028. struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
  1029. /*
  1030. * Since we are TASK_DEAD we won't slip out of the domain!
  1031. */
  1032. raw_spin_lock_irq(&dl_b->lock);
  1033. /* XXX we should retain the bw until 0-lag */
  1034. dl_b->total_bw -= p->dl.dl_bw;
  1035. raw_spin_unlock_irq(&dl_b->lock);
  1036. }
  1037. static void set_curr_task_dl(struct rq *rq)
  1038. {
  1039. struct task_struct *p = rq->curr;
  1040. p->se.exec_start = rq_clock_task(rq);
  1041. /* You can't push away the running task */
  1042. dequeue_pushable_dl_task(rq, p);
  1043. }
  1044. #ifdef CONFIG_SMP
  1045. /* Only try algorithms three times */
  1046. #define DL_MAX_TRIES 3
  1047. static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
  1048. {
  1049. if (!task_running(rq, p) &&
  1050. cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
  1051. return 1;
  1052. return 0;
  1053. }
  1054. /*
  1055. * Return the earliest pushable rq's task, which is suitable to be executed
  1056. * on the CPU, NULL otherwise:
  1057. */
  1058. static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
  1059. {
  1060. struct rb_node *next_node = rq->dl.pushable_dl_tasks_leftmost;
  1061. struct task_struct *p = NULL;
  1062. if (!has_pushable_dl_tasks(rq))
  1063. return NULL;
  1064. next_node:
  1065. if (next_node) {
  1066. p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
  1067. if (pick_dl_task(rq, p, cpu))
  1068. return p;
  1069. next_node = rb_next(next_node);
  1070. goto next_node;
  1071. }
  1072. return NULL;
  1073. }
  1074. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
  1075. static int find_later_rq(struct task_struct *task)
  1076. {
  1077. struct sched_domain *sd;
  1078. struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
  1079. int this_cpu = smp_processor_id();
  1080. int best_cpu, cpu = task_cpu(task);
  1081. /* Make sure the mask is initialized first */
  1082. if (unlikely(!later_mask))
  1083. return -1;
  1084. if (task->nr_cpus_allowed == 1)
  1085. return -1;
  1086. /*
  1087. * We have to consider system topology and task affinity
  1088. * first, then we can look for a suitable cpu.
  1089. */
  1090. best_cpu = cpudl_find(&task_rq(task)->rd->cpudl,
  1091. task, later_mask);
  1092. if (best_cpu == -1)
  1093. return -1;
  1094. /*
  1095. * If we are here, some target has been found,
  1096. * the most suitable of which is cached in best_cpu.
  1097. * This is, among the runqueues where the current tasks
  1098. * have later deadlines than the task's one, the rq
  1099. * with the latest possible one.
  1100. *
  1101. * Now we check how well this matches with task's
  1102. * affinity and system topology.
  1103. *
  1104. * The last cpu where the task run is our first
  1105. * guess, since it is most likely cache-hot there.
  1106. */
  1107. if (cpumask_test_cpu(cpu, later_mask))
  1108. return cpu;
  1109. /*
  1110. * Check if this_cpu is to be skipped (i.e., it is
  1111. * not in the mask) or not.
  1112. */
  1113. if (!cpumask_test_cpu(this_cpu, later_mask))
  1114. this_cpu = -1;
  1115. rcu_read_lock();
  1116. for_each_domain(cpu, sd) {
  1117. if (sd->flags & SD_WAKE_AFFINE) {
  1118. /*
  1119. * If possible, preempting this_cpu is
  1120. * cheaper than migrating.
  1121. */
  1122. if (this_cpu != -1 &&
  1123. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  1124. rcu_read_unlock();
  1125. return this_cpu;
  1126. }
  1127. /*
  1128. * Last chance: if best_cpu is valid and is
  1129. * in the mask, that becomes our choice.
  1130. */
  1131. if (best_cpu < nr_cpu_ids &&
  1132. cpumask_test_cpu(best_cpu, sched_domain_span(sd))) {
  1133. rcu_read_unlock();
  1134. return best_cpu;
  1135. }
  1136. }
  1137. }
  1138. rcu_read_unlock();
  1139. /*
  1140. * At this point, all our guesses failed, we just return
  1141. * 'something', and let the caller sort the things out.
  1142. */
  1143. if (this_cpu != -1)
  1144. return this_cpu;
  1145. cpu = cpumask_any(later_mask);
  1146. if (cpu < nr_cpu_ids)
  1147. return cpu;
  1148. return -1;
  1149. }
  1150. /* Locks the rq it finds */
  1151. static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
  1152. {
  1153. struct rq *later_rq = NULL;
  1154. int tries;
  1155. int cpu;
  1156. for (tries = 0; tries < DL_MAX_TRIES; tries++) {
  1157. cpu = find_later_rq(task);
  1158. if ((cpu == -1) || (cpu == rq->cpu))
  1159. break;
  1160. later_rq = cpu_rq(cpu);
  1161. if (later_rq->dl.dl_nr_running &&
  1162. !dl_time_before(task->dl.deadline,
  1163. later_rq->dl.earliest_dl.curr)) {
  1164. /*
  1165. * Target rq has tasks of equal or earlier deadline,
  1166. * retrying does not release any lock and is unlikely
  1167. * to yield a different result.
  1168. */
  1169. later_rq = NULL;
  1170. break;
  1171. }
  1172. /* Retry if something changed. */
  1173. if (double_lock_balance(rq, later_rq)) {
  1174. if (unlikely(task_rq(task) != rq ||
  1175. !cpumask_test_cpu(later_rq->cpu,
  1176. &task->cpus_allowed) ||
  1177. task_running(rq, task) ||
  1178. !task_on_rq_queued(task))) {
  1179. double_unlock_balance(rq, later_rq);
  1180. later_rq = NULL;
  1181. break;
  1182. }
  1183. }
  1184. /*
  1185. * If the rq we found has no -deadline task, or
  1186. * its earliest one has a later deadline than our
  1187. * task, the rq is a good one.
  1188. */
  1189. if (!later_rq->dl.dl_nr_running ||
  1190. dl_time_before(task->dl.deadline,
  1191. later_rq->dl.earliest_dl.curr))
  1192. break;
  1193. /* Otherwise we try again. */
  1194. double_unlock_balance(rq, later_rq);
  1195. later_rq = NULL;
  1196. }
  1197. return later_rq;
  1198. }
  1199. static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  1200. {
  1201. struct task_struct *p;
  1202. if (!has_pushable_dl_tasks(rq))
  1203. return NULL;
  1204. p = rb_entry(rq->dl.pushable_dl_tasks_leftmost,
  1205. struct task_struct, pushable_dl_tasks);
  1206. BUG_ON(rq->cpu != task_cpu(p));
  1207. BUG_ON(task_current(rq, p));
  1208. BUG_ON(p->nr_cpus_allowed <= 1);
  1209. BUG_ON(!task_on_rq_queued(p));
  1210. BUG_ON(!dl_task(p));
  1211. return p;
  1212. }
  1213. /*
  1214. * See if the non running -deadline tasks on this rq
  1215. * can be sent to some other CPU where they can preempt
  1216. * and start executing.
  1217. */
  1218. static int push_dl_task(struct rq *rq)
  1219. {
  1220. struct task_struct *next_task;
  1221. struct rq *later_rq;
  1222. int ret = 0;
  1223. if (!rq->dl.overloaded)
  1224. return 0;
  1225. next_task = pick_next_pushable_dl_task(rq);
  1226. if (!next_task)
  1227. return 0;
  1228. retry:
  1229. if (unlikely(next_task == rq->curr)) {
  1230. WARN_ON(1);
  1231. return 0;
  1232. }
  1233. /*
  1234. * If next_task preempts rq->curr, and rq->curr
  1235. * can move away, it makes sense to just reschedule
  1236. * without going further in pushing next_task.
  1237. */
  1238. if (dl_task(rq->curr) &&
  1239. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  1240. rq->curr->nr_cpus_allowed > 1) {
  1241. resched_curr(rq);
  1242. return 0;
  1243. }
  1244. /* We might release rq lock */
  1245. get_task_struct(next_task);
  1246. /* Will lock the rq it'll find */
  1247. later_rq = find_lock_later_rq(next_task, rq);
  1248. if (!later_rq) {
  1249. struct task_struct *task;
  1250. /*
  1251. * We must check all this again, since
  1252. * find_lock_later_rq releases rq->lock and it is
  1253. * then possible that next_task has migrated.
  1254. */
  1255. task = pick_next_pushable_dl_task(rq);
  1256. if (task_cpu(next_task) == rq->cpu && task == next_task) {
  1257. /*
  1258. * The task is still there. We don't try
  1259. * again, some other cpu will pull it when ready.
  1260. */
  1261. goto out;
  1262. }
  1263. if (!task)
  1264. /* No more tasks */
  1265. goto out;
  1266. put_task_struct(next_task);
  1267. next_task = task;
  1268. goto retry;
  1269. }
  1270. deactivate_task(rq, next_task, 0);
  1271. set_task_cpu(next_task, later_rq->cpu);
  1272. activate_task(later_rq, next_task, 0);
  1273. ret = 1;
  1274. resched_curr(later_rq);
  1275. double_unlock_balance(rq, later_rq);
  1276. out:
  1277. put_task_struct(next_task);
  1278. return ret;
  1279. }
  1280. static void push_dl_tasks(struct rq *rq)
  1281. {
  1282. /* push_dl_task() will return true if it moved a -deadline task */
  1283. while (push_dl_task(rq))
  1284. ;
  1285. }
  1286. static void pull_dl_task(struct rq *this_rq)
  1287. {
  1288. int this_cpu = this_rq->cpu, cpu;
  1289. struct task_struct *p;
  1290. bool resched = false;
  1291. struct rq *src_rq;
  1292. u64 dmin = LONG_MAX;
  1293. if (likely(!dl_overloaded(this_rq)))
  1294. return;
  1295. /*
  1296. * Match the barrier from dl_set_overloaded; this guarantees that if we
  1297. * see overloaded we must also see the dlo_mask bit.
  1298. */
  1299. smp_rmb();
  1300. for_each_cpu(cpu, this_rq->rd->dlo_mask) {
  1301. if (this_cpu == cpu)
  1302. continue;
  1303. src_rq = cpu_rq(cpu);
  1304. /*
  1305. * It looks racy, abd it is! However, as in sched_rt.c,
  1306. * we are fine with this.
  1307. */
  1308. if (this_rq->dl.dl_nr_running &&
  1309. dl_time_before(this_rq->dl.earliest_dl.curr,
  1310. src_rq->dl.earliest_dl.next))
  1311. continue;
  1312. /* Might drop this_rq->lock */
  1313. double_lock_balance(this_rq, src_rq);
  1314. /*
  1315. * If there are no more pullable tasks on the
  1316. * rq, we're done with it.
  1317. */
  1318. if (src_rq->dl.dl_nr_running <= 1)
  1319. goto skip;
  1320. p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
  1321. /*
  1322. * We found a task to be pulled if:
  1323. * - it preempts our current (if there's one),
  1324. * - it will preempt the last one we pulled (if any).
  1325. */
  1326. if (p && dl_time_before(p->dl.deadline, dmin) &&
  1327. (!this_rq->dl.dl_nr_running ||
  1328. dl_time_before(p->dl.deadline,
  1329. this_rq->dl.earliest_dl.curr))) {
  1330. WARN_ON(p == src_rq->curr);
  1331. WARN_ON(!task_on_rq_queued(p));
  1332. /*
  1333. * Then we pull iff p has actually an earlier
  1334. * deadline than the current task of its runqueue.
  1335. */
  1336. if (dl_time_before(p->dl.deadline,
  1337. src_rq->curr->dl.deadline))
  1338. goto skip;
  1339. resched = true;
  1340. deactivate_task(src_rq, p, 0);
  1341. set_task_cpu(p, this_cpu);
  1342. activate_task(this_rq, p, 0);
  1343. dmin = p->dl.deadline;
  1344. /* Is there any other task even earlier? */
  1345. }
  1346. skip:
  1347. double_unlock_balance(this_rq, src_rq);
  1348. }
  1349. if (resched)
  1350. resched_curr(this_rq);
  1351. }
  1352. /*
  1353. * Since the task is not running and a reschedule is not going to happen
  1354. * anytime soon on its runqueue, we try pushing it away now.
  1355. */
  1356. static void task_woken_dl(struct rq *rq, struct task_struct *p)
  1357. {
  1358. if (!task_running(rq, p) &&
  1359. !test_tsk_need_resched(rq->curr) &&
  1360. p->nr_cpus_allowed > 1 &&
  1361. dl_task(rq->curr) &&
  1362. (rq->curr->nr_cpus_allowed < 2 ||
  1363. !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
  1364. push_dl_tasks(rq);
  1365. }
  1366. }
  1367. static void set_cpus_allowed_dl(struct task_struct *p,
  1368. const struct cpumask *new_mask)
  1369. {
  1370. struct root_domain *src_rd;
  1371. struct rq *rq;
  1372. BUG_ON(!dl_task(p));
  1373. rq = task_rq(p);
  1374. src_rd = rq->rd;
  1375. /*
  1376. * Migrating a SCHED_DEADLINE task between exclusive
  1377. * cpusets (different root_domains) entails a bandwidth
  1378. * update. We already made space for us in the destination
  1379. * domain (see cpuset_can_attach()).
  1380. */
  1381. if (!cpumask_intersects(src_rd->span, new_mask)) {
  1382. struct dl_bw *src_dl_b;
  1383. src_dl_b = dl_bw_of(cpu_of(rq));
  1384. /*
  1385. * We now free resources of the root_domain we are migrating
  1386. * off. In the worst case, sched_setattr() may temporary fail
  1387. * until we complete the update.
  1388. */
  1389. raw_spin_lock(&src_dl_b->lock);
  1390. __dl_clear(src_dl_b, p->dl.dl_bw);
  1391. raw_spin_unlock(&src_dl_b->lock);
  1392. }
  1393. set_cpus_allowed_common(p, new_mask);
  1394. }
  1395. /* Assumes rq->lock is held */
  1396. static void rq_online_dl(struct rq *rq)
  1397. {
  1398. if (rq->dl.overloaded)
  1399. dl_set_overload(rq);
  1400. cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
  1401. if (rq->dl.dl_nr_running > 0)
  1402. cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr, 1);
  1403. }
  1404. /* Assumes rq->lock is held */
  1405. static void rq_offline_dl(struct rq *rq)
  1406. {
  1407. if (rq->dl.overloaded)
  1408. dl_clear_overload(rq);
  1409. cpudl_set(&rq->rd->cpudl, rq->cpu, 0, 0);
  1410. cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
  1411. }
  1412. void __init init_sched_dl_class(void)
  1413. {
  1414. unsigned int i;
  1415. for_each_possible_cpu(i)
  1416. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
  1417. GFP_KERNEL, cpu_to_node(i));
  1418. }
  1419. #endif /* CONFIG_SMP */
  1420. static void switched_from_dl(struct rq *rq, struct task_struct *p)
  1421. {
  1422. /*
  1423. * Start the deadline timer; if we switch back to dl before this we'll
  1424. * continue consuming our current CBS slice. If we stay outside of
  1425. * SCHED_DEADLINE until the deadline passes, the timer will reset the
  1426. * task.
  1427. */
  1428. if (!start_dl_timer(p))
  1429. __dl_clear_params(p);
  1430. /*
  1431. * Since this might be the only -deadline task on the rq,
  1432. * this is the right place to try to pull some other one
  1433. * from an overloaded cpu, if any.
  1434. */
  1435. if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
  1436. return;
  1437. queue_pull_task(rq);
  1438. }
  1439. /*
  1440. * When switching to -deadline, we may overload the rq, then
  1441. * we try to push someone off, if possible.
  1442. */
  1443. static void switched_to_dl(struct rq *rq, struct task_struct *p)
  1444. {
  1445. if (task_on_rq_queued(p) && rq->curr != p) {
  1446. #ifdef CONFIG_SMP
  1447. if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
  1448. queue_push_tasks(rq);
  1449. #else
  1450. if (dl_task(rq->curr))
  1451. check_preempt_curr_dl(rq, p, 0);
  1452. else
  1453. resched_curr(rq);
  1454. #endif
  1455. }
  1456. }
  1457. /*
  1458. * If the scheduling parameters of a -deadline task changed,
  1459. * a push or pull operation might be needed.
  1460. */
  1461. static void prio_changed_dl(struct rq *rq, struct task_struct *p,
  1462. int oldprio)
  1463. {
  1464. if (task_on_rq_queued(p) || rq->curr == p) {
  1465. #ifdef CONFIG_SMP
  1466. /*
  1467. * This might be too much, but unfortunately
  1468. * we don't have the old deadline value, and
  1469. * we can't argue if the task is increasing
  1470. * or lowering its prio, so...
  1471. */
  1472. if (!rq->dl.overloaded)
  1473. queue_pull_task(rq);
  1474. /*
  1475. * If we now have a earlier deadline task than p,
  1476. * then reschedule, provided p is still on this
  1477. * runqueue.
  1478. */
  1479. if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
  1480. resched_curr(rq);
  1481. #else
  1482. /*
  1483. * Again, we don't know if p has a earlier
  1484. * or later deadline, so let's blindly set a
  1485. * (maybe not needed) rescheduling point.
  1486. */
  1487. resched_curr(rq);
  1488. #endif /* CONFIG_SMP */
  1489. } else
  1490. switched_to_dl(rq, p);
  1491. }
  1492. const struct sched_class dl_sched_class = {
  1493. .next = &rt_sched_class,
  1494. .enqueue_task = enqueue_task_dl,
  1495. .dequeue_task = dequeue_task_dl,
  1496. .yield_task = yield_task_dl,
  1497. .check_preempt_curr = check_preempt_curr_dl,
  1498. .pick_next_task = pick_next_task_dl,
  1499. .put_prev_task = put_prev_task_dl,
  1500. #ifdef CONFIG_SMP
  1501. .select_task_rq = select_task_rq_dl,
  1502. .set_cpus_allowed = set_cpus_allowed_dl,
  1503. .rq_online = rq_online_dl,
  1504. .rq_offline = rq_offline_dl,
  1505. .task_woken = task_woken_dl,
  1506. #endif
  1507. .set_curr_task = set_curr_task_dl,
  1508. .task_tick = task_tick_dl,
  1509. .task_fork = task_fork_dl,
  1510. .task_dead = task_dead_dl,
  1511. .prio_changed = prio_changed_dl,
  1512. .switched_from = switched_from_dl,
  1513. .switched_to = switched_to_dl,
  1514. .update_curr = update_curr_dl,
  1515. };
  1516. #ifdef CONFIG_SCHED_DEBUG
  1517. extern void print_dl_rq(struct seq_file *m, int cpu, struct dl_rq *dl_rq);
  1518. void print_dl_stats(struct seq_file *m, int cpu)
  1519. {
  1520. print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
  1521. }
  1522. #endif /* CONFIG_SCHED_DEBUG */