deadline.c 44 KB

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