deadline.c 41 KB

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