deadline.c 46 KB

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