deadline.c 45 KB

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