deadline.c 52 KB

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