deadline.c 46 KB

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