deadline.c 42 KB

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