deadline.c 41 KB

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