deadline.c 43 KB

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