deadline.c 42 KB

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