deadline.c 46 KB

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