deadline.c 41 KB

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