deadline.c 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Deadline Scheduling Class (SCHED_DEADLINE)
  4. *
  5. * Earliest Deadline First (EDF) + Constant Bandwidth Server (CBS).
  6. *
  7. * Tasks that periodically executes their instances for less than their
  8. * runtime won't miss any of their deadlines.
  9. * Tasks that are not periodic or sporadic or that tries to execute more
  10. * than their reserved bandwidth will be slowed down (and may potentially
  11. * miss some of their deadlines), and won't affect any other task.
  12. *
  13. * Copyright (C) 2012 Dario Faggioli <raistlin@linux.it>,
  14. * Juri Lelli <juri.lelli@gmail.com>,
  15. * Michael Trimarchi <michael@amarulasolutions.com>,
  16. * Fabio Checconi <fchecconi@gmail.com>
  17. */
  18. #include "sched.h"
  19. #include "pelt.h"
  20. struct dl_bandwidth def_dl_bandwidth;
  21. static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
  22. {
  23. return container_of(dl_se, struct task_struct, dl);
  24. }
  25. static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
  26. {
  27. return container_of(dl_rq, struct rq, dl);
  28. }
  29. static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
  30. {
  31. struct task_struct *p = dl_task_of(dl_se);
  32. struct rq *rq = task_rq(p);
  33. return &rq->dl;
  34. }
  35. static inline int on_dl_rq(struct sched_dl_entity *dl_se)
  36. {
  37. return !RB_EMPTY_NODE(&dl_se->rb_node);
  38. }
  39. #ifdef CONFIG_SMP
  40. static inline struct dl_bw *dl_bw_of(int i)
  41. {
  42. RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
  43. "sched RCU must be held");
  44. return &cpu_rq(i)->rd->dl_bw;
  45. }
  46. static inline int dl_bw_cpus(int i)
  47. {
  48. struct root_domain *rd = cpu_rq(i)->rd;
  49. int cpus = 0;
  50. RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
  51. "sched RCU must be held");
  52. for_each_cpu_and(i, rd->span, cpu_active_mask)
  53. cpus++;
  54. return cpus;
  55. }
  56. #else
  57. static inline struct dl_bw *dl_bw_of(int i)
  58. {
  59. return &cpu_rq(i)->dl.dl_bw;
  60. }
  61. static inline int dl_bw_cpus(int i)
  62. {
  63. return 1;
  64. }
  65. #endif
  66. static inline
  67. void __add_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
  68. {
  69. u64 old = dl_rq->running_bw;
  70. lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
  71. dl_rq->running_bw += dl_bw;
  72. SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */
  73. SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
  74. /* kick cpufreq (see the comment in kernel/sched/sched.h). */
  75. cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
  76. }
  77. static inline
  78. void __sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
  79. {
  80. u64 old = dl_rq->running_bw;
  81. lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
  82. dl_rq->running_bw -= dl_bw;
  83. SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */
  84. if (dl_rq->running_bw > old)
  85. dl_rq->running_bw = 0;
  86. /* kick cpufreq (see the comment in kernel/sched/sched.h). */
  87. cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
  88. }
  89. static inline
  90. void __add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
  91. {
  92. u64 old = dl_rq->this_bw;
  93. lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
  94. dl_rq->this_bw += dl_bw;
  95. SCHED_WARN_ON(dl_rq->this_bw < old); /* overflow */
  96. }
  97. static inline
  98. void __sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
  99. {
  100. u64 old = dl_rq->this_bw;
  101. lockdep_assert_held(&(rq_of_dl_rq(dl_rq))->lock);
  102. dl_rq->this_bw -= dl_bw;
  103. SCHED_WARN_ON(dl_rq->this_bw > old); /* underflow */
  104. if (dl_rq->this_bw > old)
  105. dl_rq->this_bw = 0;
  106. SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
  107. }
  108. static inline
  109. void add_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  110. {
  111. if (!dl_entity_is_special(dl_se))
  112. __add_rq_bw(dl_se->dl_bw, dl_rq);
  113. }
  114. static inline
  115. void sub_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  116. {
  117. if (!dl_entity_is_special(dl_se))
  118. __sub_rq_bw(dl_se->dl_bw, dl_rq);
  119. }
  120. static inline
  121. void add_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  122. {
  123. if (!dl_entity_is_special(dl_se))
  124. __add_running_bw(dl_se->dl_bw, dl_rq);
  125. }
  126. static inline
  127. void sub_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  128. {
  129. if (!dl_entity_is_special(dl_se))
  130. __sub_running_bw(dl_se->dl_bw, dl_rq);
  131. }
  132. void dl_change_utilization(struct task_struct *p, u64 new_bw)
  133. {
  134. struct rq *rq;
  135. BUG_ON(p->dl.flags & SCHED_FLAG_SUGOV);
  136. if (task_on_rq_queued(p))
  137. return;
  138. rq = task_rq(p);
  139. if (p->dl.dl_non_contending) {
  140. sub_running_bw(&p->dl, &rq->dl);
  141. p->dl.dl_non_contending = 0;
  142. /*
  143. * If the timer handler is currently running and the
  144. * timer cannot be cancelled, inactive_task_timer()
  145. * will see that dl_not_contending is not set, and
  146. * will not touch the rq's active utilization,
  147. * so we are still safe.
  148. */
  149. if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
  150. put_task_struct(p);
  151. }
  152. __sub_rq_bw(p->dl.dl_bw, &rq->dl);
  153. __add_rq_bw(new_bw, &rq->dl);
  154. }
  155. /*
  156. * The utilization of a task cannot be immediately removed from
  157. * the rq active utilization (running_bw) when the task blocks.
  158. * Instead, we have to wait for the so called "0-lag time".
  159. *
  160. * If a task blocks before the "0-lag time", a timer (the inactive
  161. * timer) is armed, and running_bw is decreased when the timer
  162. * fires.
  163. *
  164. * If the task wakes up again before the inactive timer fires,
  165. * the timer is cancelled, whereas if the task wakes up after the
  166. * inactive timer fired (and running_bw has been decreased) the
  167. * task's utilization has to be added to running_bw again.
  168. * A flag in the deadline scheduling entity (dl_non_contending)
  169. * is used to avoid race conditions between the inactive timer handler
  170. * and task wakeups.
  171. *
  172. * The following diagram shows how running_bw is updated. A task is
  173. * "ACTIVE" when its utilization contributes to running_bw; an
  174. * "ACTIVE contending" task is in the TASK_RUNNING state, while an
  175. * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
  176. * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
  177. * time already passed, which does not contribute to running_bw anymore.
  178. * +------------------+
  179. * wakeup | ACTIVE |
  180. * +------------------>+ contending |
  181. * | add_running_bw | |
  182. * | +----+------+------+
  183. * | | ^
  184. * | dequeue | |
  185. * +--------+-------+ | |
  186. * | | t >= 0-lag | | wakeup
  187. * | INACTIVE |<---------------+ |
  188. * | | sub_running_bw | |
  189. * +--------+-------+ | |
  190. * ^ | |
  191. * | t < 0-lag | |
  192. * | | |
  193. * | V |
  194. * | +----+------+------+
  195. * | sub_running_bw | ACTIVE |
  196. * +-------------------+ |
  197. * inactive timer | non contending |
  198. * fired +------------------+
  199. *
  200. * The task_non_contending() function is invoked when a task
  201. * blocks, and checks if the 0-lag time already passed or
  202. * not (in the first case, it directly updates running_bw;
  203. * in the second case, it arms the inactive timer).
  204. *
  205. * The task_contending() function is invoked when a task wakes
  206. * up, and checks if the task is still in the "ACTIVE non contending"
  207. * state or not (in the second case, it updates running_bw).
  208. */
  209. static void task_non_contending(struct task_struct *p)
  210. {
  211. struct sched_dl_entity *dl_se = &p->dl;
  212. struct hrtimer *timer = &dl_se->inactive_timer;
  213. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  214. struct rq *rq = rq_of_dl_rq(dl_rq);
  215. s64 zerolag_time;
  216. /*
  217. * If this is a non-deadline task that has been boosted,
  218. * do nothing
  219. */
  220. if (dl_se->dl_runtime == 0)
  221. return;
  222. if (dl_entity_is_special(dl_se))
  223. return;
  224. WARN_ON(dl_se->dl_non_contending);
  225. zerolag_time = dl_se->deadline -
  226. div64_long((dl_se->runtime * dl_se->dl_period),
  227. dl_se->dl_runtime);
  228. /*
  229. * Using relative times instead of the absolute "0-lag time"
  230. * allows to simplify the code
  231. */
  232. zerolag_time -= rq_clock(rq);
  233. /*
  234. * If the "0-lag time" already passed, decrease the active
  235. * utilization now, instead of starting a timer
  236. */
  237. if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) {
  238. if (dl_task(p))
  239. sub_running_bw(dl_se, dl_rq);
  240. if (!dl_task(p) || p->state == TASK_DEAD) {
  241. struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
  242. if (p->state == TASK_DEAD)
  243. sub_rq_bw(&p->dl, &rq->dl);
  244. raw_spin_lock(&dl_b->lock);
  245. __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
  246. __dl_clear_params(p);
  247. raw_spin_unlock(&dl_b->lock);
  248. }
  249. return;
  250. }
  251. dl_se->dl_non_contending = 1;
  252. get_task_struct(p);
  253. hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL);
  254. }
  255. static void task_contending(struct sched_dl_entity *dl_se, int flags)
  256. {
  257. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  258. /*
  259. * If this is a non-deadline task that has been boosted,
  260. * do nothing
  261. */
  262. if (dl_se->dl_runtime == 0)
  263. return;
  264. if (flags & ENQUEUE_MIGRATED)
  265. add_rq_bw(dl_se, dl_rq);
  266. if (dl_se->dl_non_contending) {
  267. dl_se->dl_non_contending = 0;
  268. /*
  269. * If the timer handler is currently running and the
  270. * timer cannot be cancelled, inactive_task_timer()
  271. * will see that dl_not_contending is not set, and
  272. * will not touch the rq's active utilization,
  273. * so we are still safe.
  274. */
  275. if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1)
  276. put_task_struct(dl_task_of(dl_se));
  277. } else {
  278. /*
  279. * Since "dl_non_contending" is not set, the
  280. * task's utilization has already been removed from
  281. * active utilization (either when the task blocked,
  282. * when the "inactive timer" fired).
  283. * So, add it back.
  284. */
  285. add_running_bw(dl_se, dl_rq);
  286. }
  287. }
  288. static inline int is_leftmost(struct task_struct *p, struct dl_rq *dl_rq)
  289. {
  290. struct sched_dl_entity *dl_se = &p->dl;
  291. return dl_rq->root.rb_leftmost == &dl_se->rb_node;
  292. }
  293. void init_dl_bandwidth(struct dl_bandwidth *dl_b, u64 period, u64 runtime)
  294. {
  295. raw_spin_lock_init(&dl_b->dl_runtime_lock);
  296. dl_b->dl_period = period;
  297. dl_b->dl_runtime = runtime;
  298. }
  299. void init_dl_bw(struct dl_bw *dl_b)
  300. {
  301. raw_spin_lock_init(&dl_b->lock);
  302. raw_spin_lock(&def_dl_bandwidth.dl_runtime_lock);
  303. if (global_rt_runtime() == RUNTIME_INF)
  304. dl_b->bw = -1;
  305. else
  306. dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
  307. raw_spin_unlock(&def_dl_bandwidth.dl_runtime_lock);
  308. dl_b->total_bw = 0;
  309. }
  310. void init_dl_rq(struct dl_rq *dl_rq)
  311. {
  312. dl_rq->root = RB_ROOT_CACHED;
  313. #ifdef CONFIG_SMP
  314. /* zero means no -deadline tasks */
  315. dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
  316. dl_rq->dl_nr_migratory = 0;
  317. dl_rq->overloaded = 0;
  318. dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED;
  319. #else
  320. init_dl_bw(&dl_rq->dl_bw);
  321. #endif
  322. dl_rq->running_bw = 0;
  323. dl_rq->this_bw = 0;
  324. init_dl_rq_bw_ratio(dl_rq);
  325. }
  326. #ifdef CONFIG_SMP
  327. static inline int dl_overloaded(struct rq *rq)
  328. {
  329. return atomic_read(&rq->rd->dlo_count);
  330. }
  331. static inline void dl_set_overload(struct rq *rq)
  332. {
  333. if (!rq->online)
  334. return;
  335. cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
  336. /*
  337. * Must be visible before the overload count is
  338. * set (as in sched_rt.c).
  339. *
  340. * Matched by the barrier in pull_dl_task().
  341. */
  342. smp_wmb();
  343. atomic_inc(&rq->rd->dlo_count);
  344. }
  345. static inline void dl_clear_overload(struct rq *rq)
  346. {
  347. if (!rq->online)
  348. return;
  349. atomic_dec(&rq->rd->dlo_count);
  350. cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
  351. }
  352. static void update_dl_migration(struct dl_rq *dl_rq)
  353. {
  354. if (dl_rq->dl_nr_migratory && dl_rq->dl_nr_running > 1) {
  355. if (!dl_rq->overloaded) {
  356. dl_set_overload(rq_of_dl_rq(dl_rq));
  357. dl_rq->overloaded = 1;
  358. }
  359. } else if (dl_rq->overloaded) {
  360. dl_clear_overload(rq_of_dl_rq(dl_rq));
  361. dl_rq->overloaded = 0;
  362. }
  363. }
  364. static void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  365. {
  366. struct task_struct *p = dl_task_of(dl_se);
  367. if (p->nr_cpus_allowed > 1)
  368. dl_rq->dl_nr_migratory++;
  369. update_dl_migration(dl_rq);
  370. }
  371. static void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  372. {
  373. struct task_struct *p = dl_task_of(dl_se);
  374. if (p->nr_cpus_allowed > 1)
  375. dl_rq->dl_nr_migratory--;
  376. update_dl_migration(dl_rq);
  377. }
  378. /*
  379. * The list of pushable -deadline task is not a plist, like in
  380. * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
  381. */
  382. static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  383. {
  384. struct dl_rq *dl_rq = &rq->dl;
  385. struct rb_node **link = &dl_rq->pushable_dl_tasks_root.rb_root.rb_node;
  386. struct rb_node *parent = NULL;
  387. struct task_struct *entry;
  388. bool leftmost = true;
  389. BUG_ON(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
  390. while (*link) {
  391. parent = *link;
  392. entry = rb_entry(parent, struct task_struct,
  393. pushable_dl_tasks);
  394. if (dl_entity_preempt(&p->dl, &entry->dl))
  395. link = &parent->rb_left;
  396. else {
  397. link = &parent->rb_right;
  398. leftmost = false;
  399. }
  400. }
  401. if (leftmost)
  402. dl_rq->earliest_dl.next = p->dl.deadline;
  403. rb_link_node(&p->pushable_dl_tasks, parent, link);
  404. rb_insert_color_cached(&p->pushable_dl_tasks,
  405. &dl_rq->pushable_dl_tasks_root, leftmost);
  406. }
  407. static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  408. {
  409. struct dl_rq *dl_rq = &rq->dl;
  410. if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
  411. return;
  412. if (dl_rq->pushable_dl_tasks_root.rb_leftmost == &p->pushable_dl_tasks) {
  413. struct rb_node *next_node;
  414. next_node = rb_next(&p->pushable_dl_tasks);
  415. if (next_node) {
  416. dl_rq->earliest_dl.next = rb_entry(next_node,
  417. struct task_struct, pushable_dl_tasks)->dl.deadline;
  418. }
  419. }
  420. rb_erase_cached(&p->pushable_dl_tasks, &dl_rq->pushable_dl_tasks_root);
  421. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  422. }
  423. static inline int has_pushable_dl_tasks(struct rq *rq)
  424. {
  425. return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root);
  426. }
  427. static int push_dl_task(struct rq *rq);
  428. static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
  429. {
  430. return dl_task(prev);
  431. }
  432. static DEFINE_PER_CPU(struct callback_head, dl_push_head);
  433. static DEFINE_PER_CPU(struct callback_head, dl_pull_head);
  434. static void push_dl_tasks(struct rq *);
  435. static void pull_dl_task(struct rq *);
  436. static inline void deadline_queue_push_tasks(struct rq *rq)
  437. {
  438. if (!has_pushable_dl_tasks(rq))
  439. return;
  440. queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
  441. }
  442. static inline void deadline_queue_pull_task(struct rq *rq)
  443. {
  444. queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
  445. }
  446. static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
  447. static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
  448. {
  449. struct rq *later_rq = NULL;
  450. struct dl_bw *dl_b;
  451. later_rq = find_lock_later_rq(p, rq);
  452. if (!later_rq) {
  453. int cpu;
  454. /*
  455. * If we cannot preempt any rq, fall back to pick any
  456. * online CPU:
  457. */
  458. cpu = cpumask_any_and(cpu_active_mask, &p->cpus_allowed);
  459. if (cpu >= nr_cpu_ids) {
  460. /*
  461. * Failed to find any suitable CPU.
  462. * The task will never come back!
  463. */
  464. BUG_ON(dl_bandwidth_enabled());
  465. /*
  466. * If admission control is disabled we
  467. * try a little harder to let the task
  468. * run.
  469. */
  470. cpu = cpumask_any(cpu_active_mask);
  471. }
  472. later_rq = cpu_rq(cpu);
  473. double_lock_balance(rq, later_rq);
  474. }
  475. if (p->dl.dl_non_contending || p->dl.dl_throttled) {
  476. /*
  477. * Inactive timer is armed (or callback is running, but
  478. * waiting for us to release rq locks). In any case, when it
  479. * will fire (or continue), it will see running_bw of this
  480. * task migrated to later_rq (and correctly handle it).
  481. */
  482. sub_running_bw(&p->dl, &rq->dl);
  483. sub_rq_bw(&p->dl, &rq->dl);
  484. add_rq_bw(&p->dl, &later_rq->dl);
  485. add_running_bw(&p->dl, &later_rq->dl);
  486. } else {
  487. sub_rq_bw(&p->dl, &rq->dl);
  488. add_rq_bw(&p->dl, &later_rq->dl);
  489. }
  490. /*
  491. * And we finally need to fixup root_domain(s) bandwidth accounting,
  492. * since p is still hanging out in the old (now moved to default) root
  493. * domain.
  494. */
  495. dl_b = &rq->rd->dl_bw;
  496. raw_spin_lock(&dl_b->lock);
  497. __dl_sub(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
  498. raw_spin_unlock(&dl_b->lock);
  499. dl_b = &later_rq->rd->dl_bw;
  500. raw_spin_lock(&dl_b->lock);
  501. __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(later_rq->rd->span));
  502. raw_spin_unlock(&dl_b->lock);
  503. set_task_cpu(p, later_rq->cpu);
  504. double_unlock_balance(later_rq, rq);
  505. return later_rq;
  506. }
  507. #else
  508. static inline
  509. void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  510. {
  511. }
  512. static inline
  513. void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  514. {
  515. }
  516. static inline
  517. void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  518. {
  519. }
  520. static inline
  521. void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  522. {
  523. }
  524. static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
  525. {
  526. return false;
  527. }
  528. static inline void pull_dl_task(struct rq *rq)
  529. {
  530. }
  531. static inline void deadline_queue_push_tasks(struct rq *rq)
  532. {
  533. }
  534. static inline void deadline_queue_pull_task(struct rq *rq)
  535. {
  536. }
  537. #endif /* CONFIG_SMP */
  538. static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  539. static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  540. static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p, int flags);
  541. /*
  542. * We are being explicitly informed that a new instance is starting,
  543. * and this means that:
  544. * - the absolute deadline of the entity has to be placed at
  545. * current time + relative deadline;
  546. * - the runtime of the entity has to be set to the maximum value.
  547. *
  548. * The capability of specifying such event is useful whenever a -deadline
  549. * entity wants to (try to!) synchronize its behaviour with the scheduler's
  550. * one, and to (try to!) reconcile itself with its own scheduling
  551. * parameters.
  552. */
  553. static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
  554. {
  555. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  556. struct rq *rq = rq_of_dl_rq(dl_rq);
  557. WARN_ON(dl_se->dl_boosted);
  558. WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
  559. /*
  560. * We are racing with the deadline timer. So, do nothing because
  561. * the deadline timer handler will take care of properly recharging
  562. * the runtime and postponing the deadline
  563. */
  564. if (dl_se->dl_throttled)
  565. return;
  566. /*
  567. * We use the regular wall clock time to set deadlines in the
  568. * future; in fact, we must consider execution overheads (time
  569. * spent on hardirq context, etc.).
  570. */
  571. dl_se->deadline = rq_clock(rq) + dl_se->dl_deadline;
  572. dl_se->runtime = dl_se->dl_runtime;
  573. }
  574. /*
  575. * Pure Earliest Deadline First (EDF) scheduling does not deal with the
  576. * possibility of a entity lasting more than what it declared, and thus
  577. * exhausting its runtime.
  578. *
  579. * Here we are interested in making runtime overrun possible, but we do
  580. * not want a entity which is misbehaving to affect the scheduling of all
  581. * other entities.
  582. * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
  583. * is used, in order to confine each entity within its own bandwidth.
  584. *
  585. * This function deals exactly with that, and ensures that when the runtime
  586. * of a entity is replenished, its deadline is also postponed. That ensures
  587. * the overrunning entity can't interfere with other entity in the system and
  588. * can't make them miss their deadlines. Reasons why this kind of overruns
  589. * could happen are, typically, a entity voluntarily trying to overcome its
  590. * runtime, or it just underestimated it during sched_setattr().
  591. */
  592. static void replenish_dl_entity(struct sched_dl_entity *dl_se,
  593. struct sched_dl_entity *pi_se)
  594. {
  595. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  596. struct rq *rq = rq_of_dl_rq(dl_rq);
  597. BUG_ON(pi_se->dl_runtime <= 0);
  598. /*
  599. * This could be the case for a !-dl task that is boosted.
  600. * Just go with full inherited parameters.
  601. */
  602. if (dl_se->dl_deadline == 0) {
  603. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  604. dl_se->runtime = pi_se->dl_runtime;
  605. }
  606. if (dl_se->dl_yielded && dl_se->runtime > 0)
  607. dl_se->runtime = 0;
  608. /*
  609. * We keep moving the deadline away until we get some
  610. * available runtime for the entity. This ensures correct
  611. * handling of situations where the runtime overrun is
  612. * arbitrary large.
  613. */
  614. while (dl_se->runtime <= 0) {
  615. dl_se->deadline += pi_se->dl_period;
  616. dl_se->runtime += pi_se->dl_runtime;
  617. }
  618. /*
  619. * At this point, the deadline really should be "in
  620. * the future" with respect to rq->clock. If it's
  621. * not, we are, for some reason, lagging too much!
  622. * Anyway, after having warn userspace abut that,
  623. * we still try to keep the things running by
  624. * resetting the deadline and the budget of the
  625. * entity.
  626. */
  627. if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
  628. printk_deferred_once("sched: DL replenish lagged too much\n");
  629. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  630. dl_se->runtime = pi_se->dl_runtime;
  631. }
  632. if (dl_se->dl_yielded)
  633. dl_se->dl_yielded = 0;
  634. if (dl_se->dl_throttled)
  635. dl_se->dl_throttled = 0;
  636. }
  637. /*
  638. * Here we check if --at time t-- an entity (which is probably being
  639. * [re]activated or, in general, enqueued) can use its remaining runtime
  640. * and its current deadline _without_ exceeding the bandwidth it is
  641. * assigned (function returns true if it can't). We are in fact applying
  642. * one of the CBS rules: when a task wakes up, if the residual runtime
  643. * over residual deadline fits within the allocated bandwidth, then we
  644. * can keep the current (absolute) deadline and residual budget without
  645. * disrupting the schedulability of the system. Otherwise, we should
  646. * refill the runtime and set the deadline a period in the future,
  647. * because keeping the current (absolute) deadline of the task would
  648. * result in breaking guarantees promised to other tasks (refer to
  649. * Documentation/scheduler/sched-deadline.txt for more informations).
  650. *
  651. * This function returns true if:
  652. *
  653. * runtime / (deadline - t) > dl_runtime / dl_deadline ,
  654. *
  655. * IOW we can't recycle current parameters.
  656. *
  657. * Notice that the bandwidth check is done against the deadline. For
  658. * task with deadline equal to period this is the same of using
  659. * dl_period instead of dl_deadline in the equation above.
  660. */
  661. static bool dl_entity_overflow(struct sched_dl_entity *dl_se,
  662. struct sched_dl_entity *pi_se, u64 t)
  663. {
  664. u64 left, right;
  665. /*
  666. * left and right are the two sides of the equation above,
  667. * after a bit of shuffling to use multiplications instead
  668. * of divisions.
  669. *
  670. * Note that none of the time values involved in the two
  671. * multiplications are absolute: dl_deadline and dl_runtime
  672. * are the relative deadline and the maximum runtime of each
  673. * instance, runtime is the runtime left for the last instance
  674. * and (deadline - t), since t is rq->clock, is the time left
  675. * to the (absolute) deadline. Even if overflowing the u64 type
  676. * is very unlikely to occur in both cases, here we scale down
  677. * as we want to avoid that risk at all. Scaling down by 10
  678. * means that we reduce granularity to 1us. We are fine with it,
  679. * since this is only a true/false check and, anyway, thinking
  680. * of anything below microseconds resolution is actually fiction
  681. * (but still we want to give the user that illusion >;).
  682. */
  683. left = (pi_se->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
  684. right = ((dl_se->deadline - t) >> DL_SCALE) *
  685. (pi_se->dl_runtime >> DL_SCALE);
  686. return dl_time_before(right, left);
  687. }
  688. /*
  689. * Revised wakeup rule [1]: For self-suspending tasks, rather then
  690. * re-initializing task's runtime and deadline, the revised wakeup
  691. * rule adjusts the task's runtime to avoid the task to overrun its
  692. * density.
  693. *
  694. * Reasoning: a task may overrun the density if:
  695. * runtime / (deadline - t) > dl_runtime / dl_deadline
  696. *
  697. * Therefore, runtime can be adjusted to:
  698. * runtime = (dl_runtime / dl_deadline) * (deadline - t)
  699. *
  700. * In such way that runtime will be equal to the maximum density
  701. * the task can use without breaking any rule.
  702. *
  703. * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
  704. * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
  705. */
  706. static void
  707. update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq)
  708. {
  709. u64 laxity = dl_se->deadline - rq_clock(rq);
  710. /*
  711. * If the task has deadline < period, and the deadline is in the past,
  712. * it should already be throttled before this check.
  713. *
  714. * See update_dl_entity() comments for further details.
  715. */
  716. WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq)));
  717. dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT;
  718. }
  719. /*
  720. * Regarding the deadline, a task with implicit deadline has a relative
  721. * deadline == relative period. A task with constrained deadline has a
  722. * relative deadline <= relative period.
  723. *
  724. * We support constrained deadline tasks. However, there are some restrictions
  725. * applied only for tasks which do not have an implicit deadline. See
  726. * update_dl_entity() to know more about such restrictions.
  727. *
  728. * The dl_is_implicit() returns true if the task has an implicit deadline.
  729. */
  730. static inline bool dl_is_implicit(struct sched_dl_entity *dl_se)
  731. {
  732. return dl_se->dl_deadline == dl_se->dl_period;
  733. }
  734. /*
  735. * When a deadline entity is placed in the runqueue, its runtime and deadline
  736. * might need to be updated. This is done by a CBS wake up rule. There are two
  737. * different rules: 1) the original CBS; and 2) the Revisited CBS.
  738. *
  739. * When the task is starting a new period, the Original CBS is used. In this
  740. * case, the runtime is replenished and a new absolute deadline is set.
  741. *
  742. * When a task is queued before the begin of the next period, using the
  743. * remaining runtime and deadline could make the entity to overflow, see
  744. * dl_entity_overflow() to find more about runtime overflow. When such case
  745. * is detected, the runtime and deadline need to be updated.
  746. *
  747. * If the task has an implicit deadline, i.e., deadline == period, the Original
  748. * CBS is applied. the runtime is replenished and a new absolute deadline is
  749. * set, as in the previous cases.
  750. *
  751. * However, the Original CBS does not work properly for tasks with
  752. * deadline < period, which are said to have a constrained deadline. By
  753. * applying the Original CBS, a constrained deadline task would be able to run
  754. * runtime/deadline in a period. With deadline < period, the task would
  755. * overrun the runtime/period allowed bandwidth, breaking the admission test.
  756. *
  757. * In order to prevent this misbehave, the Revisited CBS is used for
  758. * constrained deadline tasks when a runtime overflow is detected. In the
  759. * Revisited CBS, rather than replenishing & setting a new absolute deadline,
  760. * the remaining runtime of the task is reduced to avoid runtime overflow.
  761. * Please refer to the comments update_dl_revised_wakeup() function to find
  762. * more about the Revised CBS rule.
  763. */
  764. static void update_dl_entity(struct sched_dl_entity *dl_se,
  765. struct sched_dl_entity *pi_se)
  766. {
  767. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  768. struct rq *rq = rq_of_dl_rq(dl_rq);
  769. if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
  770. dl_entity_overflow(dl_se, pi_se, rq_clock(rq))) {
  771. if (unlikely(!dl_is_implicit(dl_se) &&
  772. !dl_time_before(dl_se->deadline, rq_clock(rq)) &&
  773. !dl_se->dl_boosted)){
  774. update_dl_revised_wakeup(dl_se, rq);
  775. return;
  776. }
  777. dl_se->deadline = rq_clock(rq) + pi_se->dl_deadline;
  778. dl_se->runtime = pi_se->dl_runtime;
  779. }
  780. }
  781. static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
  782. {
  783. return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
  784. }
  785. /*
  786. * If the entity depleted all its runtime, and if we want it to sleep
  787. * while waiting for some new execution time to become available, we
  788. * set the bandwidth replenishment timer to the replenishment instant
  789. * and try to activate it.
  790. *
  791. * Notice that it is important for the caller to know if the timer
  792. * actually started or not (i.e., the replenishment instant is in
  793. * the future or in the past).
  794. */
  795. static int start_dl_timer(struct task_struct *p)
  796. {
  797. struct sched_dl_entity *dl_se = &p->dl;
  798. struct hrtimer *timer = &dl_se->dl_timer;
  799. struct rq *rq = task_rq(p);
  800. ktime_t now, act;
  801. s64 delta;
  802. lockdep_assert_held(&rq->lock);
  803. /*
  804. * We want the timer to fire at the deadline, but considering
  805. * that it is actually coming from rq->clock and not from
  806. * hrtimer's time base reading.
  807. */
  808. act = ns_to_ktime(dl_next_period(dl_se));
  809. now = hrtimer_cb_get_time(timer);
  810. delta = ktime_to_ns(now) - rq_clock(rq);
  811. act = ktime_add_ns(act, delta);
  812. /*
  813. * If the expiry time already passed, e.g., because the value
  814. * chosen as the deadline is too small, don't even try to
  815. * start the timer in the past!
  816. */
  817. if (ktime_us_delta(act, now) < 0)
  818. return 0;
  819. /*
  820. * !enqueued will guarantee another callback; even if one is already in
  821. * progress. This ensures a balanced {get,put}_task_struct().
  822. *
  823. * The race against __run_timer() clearing the enqueued state is
  824. * harmless because we're holding task_rq()->lock, therefore the timer
  825. * expiring after we've done the check will wait on its task_rq_lock()
  826. * and observe our state.
  827. */
  828. if (!hrtimer_is_queued(timer)) {
  829. get_task_struct(p);
  830. hrtimer_start(timer, act, HRTIMER_MODE_ABS);
  831. }
  832. return 1;
  833. }
  834. /*
  835. * This is the bandwidth enforcement timer callback. If here, we know
  836. * a task is not on its dl_rq, since the fact that the timer was running
  837. * means the task is throttled and needs a runtime replenishment.
  838. *
  839. * However, what we actually do depends on the fact the task is active,
  840. * (it is on its rq) or has been removed from there by a call to
  841. * dequeue_task_dl(). In the former case we must issue the runtime
  842. * replenishment and add the task back to the dl_rq; in the latter, we just
  843. * do nothing but clearing dl_throttled, so that runtime and deadline
  844. * updating (and the queueing back to dl_rq) will be done by the
  845. * next call to enqueue_task_dl().
  846. */
  847. static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
  848. {
  849. struct sched_dl_entity *dl_se = container_of(timer,
  850. struct sched_dl_entity,
  851. dl_timer);
  852. struct task_struct *p = dl_task_of(dl_se);
  853. struct rq_flags rf;
  854. struct rq *rq;
  855. rq = task_rq_lock(p, &rf);
  856. /*
  857. * The task might have changed its scheduling policy to something
  858. * different than SCHED_DEADLINE (through switched_from_dl()).
  859. */
  860. if (!dl_task(p))
  861. goto unlock;
  862. /*
  863. * The task might have been boosted by someone else and might be in the
  864. * boosting/deboosting path, its not throttled.
  865. */
  866. if (dl_se->dl_boosted)
  867. goto unlock;
  868. /*
  869. * Spurious timer due to start_dl_timer() race; or we already received
  870. * a replenishment from rt_mutex_setprio().
  871. */
  872. if (!dl_se->dl_throttled)
  873. goto unlock;
  874. sched_clock_tick();
  875. update_rq_clock(rq);
  876. /*
  877. * If the throttle happened during sched-out; like:
  878. *
  879. * schedule()
  880. * deactivate_task()
  881. * dequeue_task_dl()
  882. * update_curr_dl()
  883. * start_dl_timer()
  884. * __dequeue_task_dl()
  885. * prev->on_rq = 0;
  886. *
  887. * We can be both throttled and !queued. Replenish the counter
  888. * but do not enqueue -- wait for our wakeup to do that.
  889. */
  890. if (!task_on_rq_queued(p)) {
  891. replenish_dl_entity(dl_se, dl_se);
  892. goto unlock;
  893. }
  894. #ifdef CONFIG_SMP
  895. if (unlikely(!rq->online)) {
  896. /*
  897. * If the runqueue is no longer available, migrate the
  898. * task elsewhere. This necessarily changes rq.
  899. */
  900. lockdep_unpin_lock(&rq->lock, rf.cookie);
  901. rq = dl_task_offline_migration(rq, p);
  902. rf.cookie = lockdep_pin_lock(&rq->lock);
  903. update_rq_clock(rq);
  904. /*
  905. * Now that the task has been migrated to the new RQ and we
  906. * have that locked, proceed as normal and enqueue the task
  907. * there.
  908. */
  909. }
  910. #endif
  911. enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
  912. if (dl_task(rq->curr))
  913. check_preempt_curr_dl(rq, p, 0);
  914. else
  915. resched_curr(rq);
  916. #ifdef CONFIG_SMP
  917. /*
  918. * Queueing this task back might have overloaded rq, check if we need
  919. * to kick someone away.
  920. */
  921. if (has_pushable_dl_tasks(rq)) {
  922. /*
  923. * Nothing relies on rq->lock after this, so its safe to drop
  924. * rq->lock.
  925. */
  926. rq_unpin_lock(rq, &rf);
  927. push_dl_task(rq);
  928. rq_repin_lock(rq, &rf);
  929. }
  930. #endif
  931. unlock:
  932. task_rq_unlock(rq, p, &rf);
  933. /*
  934. * This can free the task_struct, including this hrtimer, do not touch
  935. * anything related to that after this.
  936. */
  937. put_task_struct(p);
  938. return HRTIMER_NORESTART;
  939. }
  940. void init_dl_task_timer(struct sched_dl_entity *dl_se)
  941. {
  942. struct hrtimer *timer = &dl_se->dl_timer;
  943. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  944. timer->function = dl_task_timer;
  945. }
  946. /*
  947. * During the activation, CBS checks if it can reuse the current task's
  948. * runtime and period. If the deadline of the task is in the past, CBS
  949. * cannot use the runtime, and so it replenishes the task. This rule
  950. * works fine for implicit deadline tasks (deadline == period), and the
  951. * CBS was designed for implicit deadline tasks. However, a task with
  952. * constrained deadline (deadine < period) might be awakened after the
  953. * deadline, but before the next period. In this case, replenishing the
  954. * task would allow it to run for runtime / deadline. As in this case
  955. * deadline < period, CBS enables a task to run for more than the
  956. * runtime / period. In a very loaded system, this can cause a domino
  957. * effect, making other tasks miss their deadlines.
  958. *
  959. * To avoid this problem, in the activation of a constrained deadline
  960. * task after the deadline but before the next period, throttle the
  961. * task and set the replenishing timer to the begin of the next period,
  962. * unless it is boosted.
  963. */
  964. static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
  965. {
  966. struct task_struct *p = dl_task_of(dl_se);
  967. struct rq *rq = rq_of_dl_rq(dl_rq_of_se(dl_se));
  968. if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
  969. dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
  970. if (unlikely(dl_se->dl_boosted || !start_dl_timer(p)))
  971. return;
  972. dl_se->dl_throttled = 1;
  973. if (dl_se->runtime > 0)
  974. dl_se->runtime = 0;
  975. }
  976. }
  977. static
  978. int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
  979. {
  980. return (dl_se->runtime <= 0);
  981. }
  982. extern bool sched_rt_bandwidth_account(struct rt_rq *rt_rq);
  983. /*
  984. * This function implements the GRUB accounting rule:
  985. * according to the GRUB reclaiming algorithm, the runtime is
  986. * not decreased as "dq = -dt", but as
  987. * "dq = -max{u / Umax, (1 - Uinact - Uextra)} dt",
  988. * where u is the utilization of the task, Umax is the maximum reclaimable
  989. * utilization, Uinact is the (per-runqueue) inactive utilization, computed
  990. * as the difference between the "total runqueue utilization" and the
  991. * runqueue active utilization, and Uextra is the (per runqueue) extra
  992. * reclaimable utilization.
  993. * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations
  994. * multiplied by 2^BW_SHIFT, the result has to be shifted right by
  995. * BW_SHIFT.
  996. * Since rq->dl.bw_ratio contains 1 / Umax multipled by 2^RATIO_SHIFT,
  997. * dl_bw is multiped by rq->dl.bw_ratio and shifted right by RATIO_SHIFT.
  998. * Since delta is a 64 bit variable, to have an overflow its value
  999. * should be larger than 2^(64 - 20 - 8), which is more than 64 seconds.
  1000. * So, overflow is not an issue here.
  1001. */
  1002. static u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se)
  1003. {
  1004. u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */
  1005. u64 u_act;
  1006. u64 u_act_min = (dl_se->dl_bw * rq->dl.bw_ratio) >> RATIO_SHIFT;
  1007. /*
  1008. * Instead of computing max{u * bw_ratio, (1 - u_inact - u_extra)},
  1009. * we compare u_inact + rq->dl.extra_bw with
  1010. * 1 - (u * rq->dl.bw_ratio >> RATIO_SHIFT), because
  1011. * u_inact + rq->dl.extra_bw can be larger than
  1012. * 1 * (so, 1 - u_inact - rq->dl.extra_bw would be negative
  1013. * leading to wrong results)
  1014. */
  1015. if (u_inact + rq->dl.extra_bw > BW_UNIT - u_act_min)
  1016. u_act = u_act_min;
  1017. else
  1018. u_act = BW_UNIT - u_inact - rq->dl.extra_bw;
  1019. return (delta * u_act) >> BW_SHIFT;
  1020. }
  1021. /*
  1022. * Update the current task's runtime statistics (provided it is still
  1023. * a -deadline task and has not been removed from the dl_rq).
  1024. */
  1025. static void update_curr_dl(struct rq *rq)
  1026. {
  1027. struct task_struct *curr = rq->curr;
  1028. struct sched_dl_entity *dl_se = &curr->dl;
  1029. u64 delta_exec, scaled_delta_exec;
  1030. int cpu = cpu_of(rq);
  1031. u64 now;
  1032. if (!dl_task(curr) || !on_dl_rq(dl_se))
  1033. return;
  1034. /*
  1035. * Consumed budget is computed considering the time as
  1036. * observed by schedulable tasks (excluding time spent
  1037. * in hardirq context, etc.). Deadlines are instead
  1038. * computed using hard walltime. This seems to be the more
  1039. * natural solution, but the full ramifications of this
  1040. * approach need further study.
  1041. */
  1042. now = rq_clock_task(rq);
  1043. delta_exec = now - curr->se.exec_start;
  1044. if (unlikely((s64)delta_exec <= 0)) {
  1045. if (unlikely(dl_se->dl_yielded))
  1046. goto throttle;
  1047. return;
  1048. }
  1049. schedstat_set(curr->se.statistics.exec_max,
  1050. max(curr->se.statistics.exec_max, delta_exec));
  1051. curr->se.sum_exec_runtime += delta_exec;
  1052. account_group_exec_runtime(curr, delta_exec);
  1053. curr->se.exec_start = now;
  1054. cgroup_account_cputime(curr, delta_exec);
  1055. if (dl_entity_is_special(dl_se))
  1056. return;
  1057. /*
  1058. * For tasks that participate in GRUB, we implement GRUB-PA: the
  1059. * spare reclaimed bandwidth is used to clock down frequency.
  1060. *
  1061. * For the others, we still need to scale reservation parameters
  1062. * according to current frequency and CPU maximum capacity.
  1063. */
  1064. if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) {
  1065. scaled_delta_exec = grub_reclaim(delta_exec,
  1066. rq,
  1067. &curr->dl);
  1068. } else {
  1069. unsigned long scale_freq = arch_scale_freq_capacity(cpu);
  1070. unsigned long scale_cpu = arch_scale_cpu_capacity(NULL, cpu);
  1071. scaled_delta_exec = cap_scale(delta_exec, scale_freq);
  1072. scaled_delta_exec = cap_scale(scaled_delta_exec, scale_cpu);
  1073. }
  1074. dl_se->runtime -= scaled_delta_exec;
  1075. throttle:
  1076. if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
  1077. dl_se->dl_throttled = 1;
  1078. /* If requested, inform the user about runtime overruns. */
  1079. if (dl_runtime_exceeded(dl_se) &&
  1080. (dl_se->flags & SCHED_FLAG_DL_OVERRUN))
  1081. dl_se->dl_overrun = 1;
  1082. __dequeue_task_dl(rq, curr, 0);
  1083. if (unlikely(dl_se->dl_boosted || !start_dl_timer(curr)))
  1084. enqueue_task_dl(rq, curr, ENQUEUE_REPLENISH);
  1085. if (!is_leftmost(curr, &rq->dl))
  1086. resched_curr(rq);
  1087. }
  1088. /*
  1089. * Because -- for now -- we share the rt bandwidth, we need to
  1090. * account our runtime there too, otherwise actual rt tasks
  1091. * would be able to exceed the shared quota.
  1092. *
  1093. * Account to the root rt group for now.
  1094. *
  1095. * The solution we're working towards is having the RT groups scheduled
  1096. * using deadline servers -- however there's a few nasties to figure
  1097. * out before that can happen.
  1098. */
  1099. if (rt_bandwidth_enabled()) {
  1100. struct rt_rq *rt_rq = &rq->rt;
  1101. raw_spin_lock(&rt_rq->rt_runtime_lock);
  1102. /*
  1103. * We'll let actual RT tasks worry about the overflow here, we
  1104. * have our own CBS to keep us inline; only account when RT
  1105. * bandwidth is relevant.
  1106. */
  1107. if (sched_rt_bandwidth_account(rt_rq))
  1108. rt_rq->rt_time += delta_exec;
  1109. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  1110. }
  1111. }
  1112. static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
  1113. {
  1114. struct sched_dl_entity *dl_se = container_of(timer,
  1115. struct sched_dl_entity,
  1116. inactive_timer);
  1117. struct task_struct *p = dl_task_of(dl_se);
  1118. struct rq_flags rf;
  1119. struct rq *rq;
  1120. rq = task_rq_lock(p, &rf);
  1121. sched_clock_tick();
  1122. update_rq_clock(rq);
  1123. if (!dl_task(p) || p->state == TASK_DEAD) {
  1124. struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
  1125. if (p->state == TASK_DEAD && dl_se->dl_non_contending) {
  1126. sub_running_bw(&p->dl, dl_rq_of_se(&p->dl));
  1127. sub_rq_bw(&p->dl, dl_rq_of_se(&p->dl));
  1128. dl_se->dl_non_contending = 0;
  1129. }
  1130. raw_spin_lock(&dl_b->lock);
  1131. __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
  1132. raw_spin_unlock(&dl_b->lock);
  1133. __dl_clear_params(p);
  1134. goto unlock;
  1135. }
  1136. if (dl_se->dl_non_contending == 0)
  1137. goto unlock;
  1138. sub_running_bw(dl_se, &rq->dl);
  1139. dl_se->dl_non_contending = 0;
  1140. unlock:
  1141. task_rq_unlock(rq, p, &rf);
  1142. put_task_struct(p);
  1143. return HRTIMER_NORESTART;
  1144. }
  1145. void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
  1146. {
  1147. struct hrtimer *timer = &dl_se->inactive_timer;
  1148. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1149. timer->function = inactive_task_timer;
  1150. }
  1151. #ifdef CONFIG_SMP
  1152. static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  1153. {
  1154. struct rq *rq = rq_of_dl_rq(dl_rq);
  1155. if (dl_rq->earliest_dl.curr == 0 ||
  1156. dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
  1157. dl_rq->earliest_dl.curr = deadline;
  1158. cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
  1159. }
  1160. }
  1161. static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  1162. {
  1163. struct rq *rq = rq_of_dl_rq(dl_rq);
  1164. /*
  1165. * Since we may have removed our earliest (and/or next earliest)
  1166. * task we must recompute them.
  1167. */
  1168. if (!dl_rq->dl_nr_running) {
  1169. dl_rq->earliest_dl.curr = 0;
  1170. dl_rq->earliest_dl.next = 0;
  1171. cpudl_clear(&rq->rd->cpudl, rq->cpu);
  1172. } else {
  1173. struct rb_node *leftmost = dl_rq->root.rb_leftmost;
  1174. struct sched_dl_entity *entry;
  1175. entry = rb_entry(leftmost, struct sched_dl_entity, rb_node);
  1176. dl_rq->earliest_dl.curr = entry->deadline;
  1177. cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
  1178. }
  1179. }
  1180. #else
  1181. static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  1182. static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  1183. #endif /* CONFIG_SMP */
  1184. static inline
  1185. void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  1186. {
  1187. int prio = dl_task_of(dl_se)->prio;
  1188. u64 deadline = dl_se->deadline;
  1189. WARN_ON(!dl_prio(prio));
  1190. dl_rq->dl_nr_running++;
  1191. add_nr_running(rq_of_dl_rq(dl_rq), 1);
  1192. inc_dl_deadline(dl_rq, deadline);
  1193. inc_dl_migration(dl_se, dl_rq);
  1194. }
  1195. static inline
  1196. void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  1197. {
  1198. int prio = dl_task_of(dl_se)->prio;
  1199. WARN_ON(!dl_prio(prio));
  1200. WARN_ON(!dl_rq->dl_nr_running);
  1201. dl_rq->dl_nr_running--;
  1202. sub_nr_running(rq_of_dl_rq(dl_rq), 1);
  1203. dec_dl_deadline(dl_rq, dl_se->deadline);
  1204. dec_dl_migration(dl_se, dl_rq);
  1205. }
  1206. static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
  1207. {
  1208. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  1209. struct rb_node **link = &dl_rq->root.rb_root.rb_node;
  1210. struct rb_node *parent = NULL;
  1211. struct sched_dl_entity *entry;
  1212. int leftmost = 1;
  1213. BUG_ON(!RB_EMPTY_NODE(&dl_se->rb_node));
  1214. while (*link) {
  1215. parent = *link;
  1216. entry = rb_entry(parent, struct sched_dl_entity, rb_node);
  1217. if (dl_time_before(dl_se->deadline, entry->deadline))
  1218. link = &parent->rb_left;
  1219. else {
  1220. link = &parent->rb_right;
  1221. leftmost = 0;
  1222. }
  1223. }
  1224. rb_link_node(&dl_se->rb_node, parent, link);
  1225. rb_insert_color_cached(&dl_se->rb_node, &dl_rq->root, leftmost);
  1226. inc_dl_tasks(dl_se, dl_rq);
  1227. }
  1228. static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
  1229. {
  1230. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  1231. if (RB_EMPTY_NODE(&dl_se->rb_node))
  1232. return;
  1233. rb_erase_cached(&dl_se->rb_node, &dl_rq->root);
  1234. RB_CLEAR_NODE(&dl_se->rb_node);
  1235. dec_dl_tasks(dl_se, dl_rq);
  1236. }
  1237. static void
  1238. enqueue_dl_entity(struct sched_dl_entity *dl_se,
  1239. struct sched_dl_entity *pi_se, int flags)
  1240. {
  1241. BUG_ON(on_dl_rq(dl_se));
  1242. /*
  1243. * If this is a wakeup or a new instance, the scheduling
  1244. * parameters of the task might need updating. Otherwise,
  1245. * we want a replenishment of its runtime.
  1246. */
  1247. if (flags & ENQUEUE_WAKEUP) {
  1248. task_contending(dl_se, flags);
  1249. update_dl_entity(dl_se, pi_se);
  1250. } else if (flags & ENQUEUE_REPLENISH) {
  1251. replenish_dl_entity(dl_se, pi_se);
  1252. } else if ((flags & ENQUEUE_RESTORE) &&
  1253. dl_time_before(dl_se->deadline,
  1254. rq_clock(rq_of_dl_rq(dl_rq_of_se(dl_se))))) {
  1255. setup_new_dl_entity(dl_se);
  1256. }
  1257. __enqueue_dl_entity(dl_se);
  1258. }
  1259. static void dequeue_dl_entity(struct sched_dl_entity *dl_se)
  1260. {
  1261. __dequeue_dl_entity(dl_se);
  1262. }
  1263. static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  1264. {
  1265. struct task_struct *pi_task = rt_mutex_get_top_task(p);
  1266. struct sched_dl_entity *pi_se = &p->dl;
  1267. /*
  1268. * Use the scheduling parameters of the top pi-waiter task if:
  1269. * - we have a top pi-waiter which is a SCHED_DEADLINE task AND
  1270. * - our dl_boosted is set (i.e. the pi-waiter's (absolute) deadline is
  1271. * smaller than our deadline OR we are a !SCHED_DEADLINE task getting
  1272. * boosted due to a SCHED_DEADLINE pi-waiter).
  1273. * Otherwise we keep our runtime and deadline.
  1274. */
  1275. if (pi_task && dl_prio(pi_task->normal_prio) && p->dl.dl_boosted) {
  1276. pi_se = &pi_task->dl;
  1277. } else if (!dl_prio(p->normal_prio)) {
  1278. /*
  1279. * Special case in which we have a !SCHED_DEADLINE task
  1280. * that is going to be deboosted, but exceeds its
  1281. * runtime while doing so. No point in replenishing
  1282. * it, as it's going to return back to its original
  1283. * scheduling class after this.
  1284. */
  1285. BUG_ON(!p->dl.dl_boosted || flags != ENQUEUE_REPLENISH);
  1286. return;
  1287. }
  1288. /*
  1289. * Check if a constrained deadline task was activated
  1290. * after the deadline but before the next period.
  1291. * If that is the case, the task will be throttled and
  1292. * the replenishment timer will be set to the next period.
  1293. */
  1294. if (!p->dl.dl_throttled && !dl_is_implicit(&p->dl))
  1295. dl_check_constrained_dl(&p->dl);
  1296. if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & ENQUEUE_RESTORE) {
  1297. add_rq_bw(&p->dl, &rq->dl);
  1298. add_running_bw(&p->dl, &rq->dl);
  1299. }
  1300. /*
  1301. * If p is throttled, we do not enqueue it. In fact, if it exhausted
  1302. * its budget it needs a replenishment and, since it now is on
  1303. * its rq, the bandwidth timer callback (which clearly has not
  1304. * run yet) will take care of this.
  1305. * However, the active utilization does not depend on the fact
  1306. * that the task is on the runqueue or not (but depends on the
  1307. * task's state - in GRUB parlance, "inactive" vs "active contending").
  1308. * In other words, even if a task is throttled its utilization must
  1309. * be counted in the active utilization; hence, we need to call
  1310. * add_running_bw().
  1311. */
  1312. if (p->dl.dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
  1313. if (flags & ENQUEUE_WAKEUP)
  1314. task_contending(&p->dl, flags);
  1315. return;
  1316. }
  1317. enqueue_dl_entity(&p->dl, pi_se, flags);
  1318. if (!task_current(rq, p) && p->nr_cpus_allowed > 1)
  1319. enqueue_pushable_dl_task(rq, p);
  1320. }
  1321. static void __dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  1322. {
  1323. dequeue_dl_entity(&p->dl);
  1324. dequeue_pushable_dl_task(rq, p);
  1325. }
  1326. static void dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  1327. {
  1328. update_curr_dl(rq);
  1329. __dequeue_task_dl(rq, p, flags);
  1330. if (p->on_rq == TASK_ON_RQ_MIGRATING || flags & DEQUEUE_SAVE) {
  1331. sub_running_bw(&p->dl, &rq->dl);
  1332. sub_rq_bw(&p->dl, &rq->dl);
  1333. }
  1334. /*
  1335. * This check allows to start the inactive timer (or to immediately
  1336. * decrease the active utilization, if needed) in two cases:
  1337. * when the task blocks and when it is terminating
  1338. * (p->state == TASK_DEAD). We can handle the two cases in the same
  1339. * way, because from GRUB's point of view the same thing is happening
  1340. * (the task moves from "active contending" to "active non contending"
  1341. * or "inactive")
  1342. */
  1343. if (flags & DEQUEUE_SLEEP)
  1344. task_non_contending(p);
  1345. }
  1346. /*
  1347. * Yield task semantic for -deadline tasks is:
  1348. *
  1349. * get off from the CPU until our next instance, with
  1350. * a new runtime. This is of little use now, since we
  1351. * don't have a bandwidth reclaiming mechanism. Anyway,
  1352. * bandwidth reclaiming is planned for the future, and
  1353. * yield_task_dl will indicate that some spare budget
  1354. * is available for other task instances to use it.
  1355. */
  1356. static void yield_task_dl(struct rq *rq)
  1357. {
  1358. /*
  1359. * We make the task go to sleep until its current deadline by
  1360. * forcing its runtime to zero. This way, update_curr_dl() stops
  1361. * it and the bandwidth timer will wake it up and will give it
  1362. * new scheduling parameters (thanks to dl_yielded=1).
  1363. */
  1364. rq->curr->dl.dl_yielded = 1;
  1365. update_rq_clock(rq);
  1366. update_curr_dl(rq);
  1367. /*
  1368. * Tell update_rq_clock() that we've just updated,
  1369. * so we don't do microscopic update in schedule()
  1370. * and double the fastpath cost.
  1371. */
  1372. rq_clock_skip_update(rq);
  1373. }
  1374. #ifdef CONFIG_SMP
  1375. static int find_later_rq(struct task_struct *task);
  1376. static int
  1377. select_task_rq_dl(struct task_struct *p, int cpu, int sd_flag, int flags)
  1378. {
  1379. struct task_struct *curr;
  1380. struct rq *rq;
  1381. if (sd_flag != SD_BALANCE_WAKE)
  1382. goto out;
  1383. rq = cpu_rq(cpu);
  1384. rcu_read_lock();
  1385. curr = READ_ONCE(rq->curr); /* unlocked access */
  1386. /*
  1387. * If we are dealing with a -deadline task, we must
  1388. * decide where to wake it up.
  1389. * If it has a later deadline and the current task
  1390. * on this rq can't move (provided the waking task
  1391. * can!) we prefer to send it somewhere else. On the
  1392. * other hand, if it has a shorter deadline, we
  1393. * try to make it stay here, it might be important.
  1394. */
  1395. if (unlikely(dl_task(curr)) &&
  1396. (curr->nr_cpus_allowed < 2 ||
  1397. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  1398. (p->nr_cpus_allowed > 1)) {
  1399. int target = find_later_rq(p);
  1400. if (target != -1 &&
  1401. (dl_time_before(p->dl.deadline,
  1402. cpu_rq(target)->dl.earliest_dl.curr) ||
  1403. (cpu_rq(target)->dl.dl_nr_running == 0)))
  1404. cpu = target;
  1405. }
  1406. rcu_read_unlock();
  1407. out:
  1408. return cpu;
  1409. }
  1410. static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
  1411. {
  1412. struct rq *rq;
  1413. if (p->state != TASK_WAKING)
  1414. return;
  1415. rq = task_rq(p);
  1416. /*
  1417. * Since p->state == TASK_WAKING, set_task_cpu() has been called
  1418. * from try_to_wake_up(). Hence, p->pi_lock is locked, but
  1419. * rq->lock is not... So, lock it
  1420. */
  1421. raw_spin_lock(&rq->lock);
  1422. if (p->dl.dl_non_contending) {
  1423. sub_running_bw(&p->dl, &rq->dl);
  1424. p->dl.dl_non_contending = 0;
  1425. /*
  1426. * If the timer handler is currently running and the
  1427. * timer cannot be cancelled, inactive_task_timer()
  1428. * will see that dl_not_contending is not set, and
  1429. * will not touch the rq's active utilization,
  1430. * so we are still safe.
  1431. */
  1432. if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
  1433. put_task_struct(p);
  1434. }
  1435. sub_rq_bw(&p->dl, &rq->dl);
  1436. raw_spin_unlock(&rq->lock);
  1437. }
  1438. static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  1439. {
  1440. /*
  1441. * Current can't be migrated, useless to reschedule,
  1442. * let's hope p can move out.
  1443. */
  1444. if (rq->curr->nr_cpus_allowed == 1 ||
  1445. !cpudl_find(&rq->rd->cpudl, rq->curr, NULL))
  1446. return;
  1447. /*
  1448. * p is migratable, so let's not schedule it and
  1449. * see if it is pushed or pulled somewhere else.
  1450. */
  1451. if (p->nr_cpus_allowed != 1 &&
  1452. cpudl_find(&rq->rd->cpudl, p, NULL))
  1453. return;
  1454. resched_curr(rq);
  1455. }
  1456. #endif /* CONFIG_SMP */
  1457. /*
  1458. * Only called when both the current and waking task are -deadline
  1459. * tasks.
  1460. */
  1461. static void check_preempt_curr_dl(struct rq *rq, struct task_struct *p,
  1462. int flags)
  1463. {
  1464. if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
  1465. resched_curr(rq);
  1466. return;
  1467. }
  1468. #ifdef CONFIG_SMP
  1469. /*
  1470. * In the unlikely case current and p have the same deadline
  1471. * let us try to decide what's the best thing to do...
  1472. */
  1473. if ((p->dl.deadline == rq->curr->dl.deadline) &&
  1474. !test_tsk_need_resched(rq->curr))
  1475. check_preempt_equal_dl(rq, p);
  1476. #endif /* CONFIG_SMP */
  1477. }
  1478. #ifdef CONFIG_SCHED_HRTICK
  1479. static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
  1480. {
  1481. hrtick_start(rq, p->dl.runtime);
  1482. }
  1483. #else /* !CONFIG_SCHED_HRTICK */
  1484. static void start_hrtick_dl(struct rq *rq, struct task_struct *p)
  1485. {
  1486. }
  1487. #endif
  1488. static struct sched_dl_entity *pick_next_dl_entity(struct rq *rq,
  1489. struct dl_rq *dl_rq)
  1490. {
  1491. struct rb_node *left = rb_first_cached(&dl_rq->root);
  1492. if (!left)
  1493. return NULL;
  1494. return rb_entry(left, struct sched_dl_entity, rb_node);
  1495. }
  1496. static struct task_struct *
  1497. pick_next_task_dl(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
  1498. {
  1499. struct sched_dl_entity *dl_se;
  1500. struct task_struct *p;
  1501. struct dl_rq *dl_rq;
  1502. dl_rq = &rq->dl;
  1503. if (need_pull_dl_task(rq, prev)) {
  1504. /*
  1505. * This is OK, because current is on_cpu, which avoids it being
  1506. * picked for load-balance and preemption/IRQs are still
  1507. * disabled avoiding further scheduler activity on it and we're
  1508. * being very careful to re-start the picking loop.
  1509. */
  1510. rq_unpin_lock(rq, rf);
  1511. pull_dl_task(rq);
  1512. rq_repin_lock(rq, rf);
  1513. /*
  1514. * pull_dl_task() can drop (and re-acquire) rq->lock; this
  1515. * means a stop task can slip in, in which case we need to
  1516. * re-start task selection.
  1517. */
  1518. if (rq->stop && task_on_rq_queued(rq->stop))
  1519. return RETRY_TASK;
  1520. }
  1521. /*
  1522. * When prev is DL, we may throttle it in put_prev_task().
  1523. * So, we update time before we check for dl_nr_running.
  1524. */
  1525. if (prev->sched_class == &dl_sched_class)
  1526. update_curr_dl(rq);
  1527. if (unlikely(!dl_rq->dl_nr_running))
  1528. return NULL;
  1529. put_prev_task(rq, prev);
  1530. dl_se = pick_next_dl_entity(rq, dl_rq);
  1531. BUG_ON(!dl_se);
  1532. p = dl_task_of(dl_se);
  1533. p->se.exec_start = rq_clock_task(rq);
  1534. /* Running task will never be pushed. */
  1535. dequeue_pushable_dl_task(rq, p);
  1536. if (hrtick_enabled(rq))
  1537. start_hrtick_dl(rq, p);
  1538. deadline_queue_push_tasks(rq);
  1539. if (rq->curr->sched_class != &dl_sched_class)
  1540. update_dl_rq_load_avg(rq_clock_task(rq), rq, 0);
  1541. return p;
  1542. }
  1543. static void put_prev_task_dl(struct rq *rq, struct task_struct *p)
  1544. {
  1545. update_curr_dl(rq);
  1546. update_dl_rq_load_avg(rq_clock_task(rq), rq, 1);
  1547. if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
  1548. enqueue_pushable_dl_task(rq, p);
  1549. }
  1550. /*
  1551. * scheduler tick hitting a task of our scheduling class.
  1552. *
  1553. * NOTE: This function can be called remotely by the tick offload that
  1554. * goes along full dynticks. Therefore no local assumption can be made
  1555. * and everything must be accessed through the @rq and @curr passed in
  1556. * parameters.
  1557. */
  1558. static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
  1559. {
  1560. update_curr_dl(rq);
  1561. update_dl_rq_load_avg(rq_clock_task(rq), rq, 1);
  1562. /*
  1563. * Even when we have runtime, update_curr_dl() might have resulted in us
  1564. * not being the leftmost task anymore. In that case NEED_RESCHED will
  1565. * be set and schedule() will start a new hrtick for the next task.
  1566. */
  1567. if (hrtick_enabled(rq) && queued && p->dl.runtime > 0 &&
  1568. is_leftmost(p, &rq->dl))
  1569. start_hrtick_dl(rq, p);
  1570. }
  1571. static void task_fork_dl(struct task_struct *p)
  1572. {
  1573. /*
  1574. * SCHED_DEADLINE tasks cannot fork and this is achieved through
  1575. * sched_fork()
  1576. */
  1577. }
  1578. static void set_curr_task_dl(struct rq *rq)
  1579. {
  1580. struct task_struct *p = rq->curr;
  1581. p->se.exec_start = rq_clock_task(rq);
  1582. /* You can't push away the running task */
  1583. dequeue_pushable_dl_task(rq, p);
  1584. }
  1585. #ifdef CONFIG_SMP
  1586. /* Only try algorithms three times */
  1587. #define DL_MAX_TRIES 3
  1588. static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
  1589. {
  1590. if (!task_running(rq, p) &&
  1591. cpumask_test_cpu(cpu, &p->cpus_allowed))
  1592. return 1;
  1593. return 0;
  1594. }
  1595. /*
  1596. * Return the earliest pushable rq's task, which is suitable to be executed
  1597. * on the CPU, NULL otherwise:
  1598. */
  1599. static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
  1600. {
  1601. struct rb_node *next_node = rq->dl.pushable_dl_tasks_root.rb_leftmost;
  1602. struct task_struct *p = NULL;
  1603. if (!has_pushable_dl_tasks(rq))
  1604. return NULL;
  1605. next_node:
  1606. if (next_node) {
  1607. p = rb_entry(next_node, struct task_struct, pushable_dl_tasks);
  1608. if (pick_dl_task(rq, p, cpu))
  1609. return p;
  1610. next_node = rb_next(next_node);
  1611. goto next_node;
  1612. }
  1613. return NULL;
  1614. }
  1615. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
  1616. static int find_later_rq(struct task_struct *task)
  1617. {
  1618. struct sched_domain *sd;
  1619. struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
  1620. int this_cpu = smp_processor_id();
  1621. int cpu = task_cpu(task);
  1622. /* Make sure the mask is initialized first */
  1623. if (unlikely(!later_mask))
  1624. return -1;
  1625. if (task->nr_cpus_allowed == 1)
  1626. return -1;
  1627. /*
  1628. * We have to consider system topology and task affinity
  1629. * first, then we can look for a suitable CPU.
  1630. */
  1631. if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask))
  1632. return -1;
  1633. /*
  1634. * If we are here, some targets have been found, including
  1635. * the most suitable which is, among the runqueues where the
  1636. * current tasks have later deadlines than the task's one, the
  1637. * rq with the latest possible one.
  1638. *
  1639. * Now we check how well this matches with task's
  1640. * affinity and system topology.
  1641. *
  1642. * The last CPU where the task run is our first
  1643. * guess, since it is most likely cache-hot there.
  1644. */
  1645. if (cpumask_test_cpu(cpu, later_mask))
  1646. return cpu;
  1647. /*
  1648. * Check if this_cpu is to be skipped (i.e., it is
  1649. * not in the mask) or not.
  1650. */
  1651. if (!cpumask_test_cpu(this_cpu, later_mask))
  1652. this_cpu = -1;
  1653. rcu_read_lock();
  1654. for_each_domain(cpu, sd) {
  1655. if (sd->flags & SD_WAKE_AFFINE) {
  1656. int best_cpu;
  1657. /*
  1658. * If possible, preempting this_cpu is
  1659. * cheaper than migrating.
  1660. */
  1661. if (this_cpu != -1 &&
  1662. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  1663. rcu_read_unlock();
  1664. return this_cpu;
  1665. }
  1666. best_cpu = cpumask_first_and(later_mask,
  1667. sched_domain_span(sd));
  1668. /*
  1669. * Last chance: if a CPU being in both later_mask
  1670. * and current sd span is valid, that becomes our
  1671. * choice. Of course, the latest possible CPU is
  1672. * already under consideration through later_mask.
  1673. */
  1674. if (best_cpu < nr_cpu_ids) {
  1675. rcu_read_unlock();
  1676. return best_cpu;
  1677. }
  1678. }
  1679. }
  1680. rcu_read_unlock();
  1681. /*
  1682. * At this point, all our guesses failed, we just return
  1683. * 'something', and let the caller sort the things out.
  1684. */
  1685. if (this_cpu != -1)
  1686. return this_cpu;
  1687. cpu = cpumask_any(later_mask);
  1688. if (cpu < nr_cpu_ids)
  1689. return cpu;
  1690. return -1;
  1691. }
  1692. /* Locks the rq it finds */
  1693. static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
  1694. {
  1695. struct rq *later_rq = NULL;
  1696. int tries;
  1697. int cpu;
  1698. for (tries = 0; tries < DL_MAX_TRIES; tries++) {
  1699. cpu = find_later_rq(task);
  1700. if ((cpu == -1) || (cpu == rq->cpu))
  1701. break;
  1702. later_rq = cpu_rq(cpu);
  1703. if (later_rq->dl.dl_nr_running &&
  1704. !dl_time_before(task->dl.deadline,
  1705. later_rq->dl.earliest_dl.curr)) {
  1706. /*
  1707. * Target rq has tasks of equal or earlier deadline,
  1708. * retrying does not release any lock and is unlikely
  1709. * to yield a different result.
  1710. */
  1711. later_rq = NULL;
  1712. break;
  1713. }
  1714. /* Retry if something changed. */
  1715. if (double_lock_balance(rq, later_rq)) {
  1716. if (unlikely(task_rq(task) != rq ||
  1717. !cpumask_test_cpu(later_rq->cpu, &task->cpus_allowed) ||
  1718. task_running(rq, task) ||
  1719. !dl_task(task) ||
  1720. !task_on_rq_queued(task))) {
  1721. double_unlock_balance(rq, later_rq);
  1722. later_rq = NULL;
  1723. break;
  1724. }
  1725. }
  1726. /*
  1727. * If the rq we found has no -deadline task, or
  1728. * its earliest one has a later deadline than our
  1729. * task, the rq is a good one.
  1730. */
  1731. if (!later_rq->dl.dl_nr_running ||
  1732. dl_time_before(task->dl.deadline,
  1733. later_rq->dl.earliest_dl.curr))
  1734. break;
  1735. /* Otherwise we try again. */
  1736. double_unlock_balance(rq, later_rq);
  1737. later_rq = NULL;
  1738. }
  1739. return later_rq;
  1740. }
  1741. static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  1742. {
  1743. struct task_struct *p;
  1744. if (!has_pushable_dl_tasks(rq))
  1745. return NULL;
  1746. p = rb_entry(rq->dl.pushable_dl_tasks_root.rb_leftmost,
  1747. struct task_struct, pushable_dl_tasks);
  1748. BUG_ON(rq->cpu != task_cpu(p));
  1749. BUG_ON(task_current(rq, p));
  1750. BUG_ON(p->nr_cpus_allowed <= 1);
  1751. BUG_ON(!task_on_rq_queued(p));
  1752. BUG_ON(!dl_task(p));
  1753. return p;
  1754. }
  1755. /*
  1756. * See if the non running -deadline tasks on this rq
  1757. * can be sent to some other CPU where they can preempt
  1758. * and start executing.
  1759. */
  1760. static int push_dl_task(struct rq *rq)
  1761. {
  1762. struct task_struct *next_task;
  1763. struct rq *later_rq;
  1764. int ret = 0;
  1765. if (!rq->dl.overloaded)
  1766. return 0;
  1767. next_task = pick_next_pushable_dl_task(rq);
  1768. if (!next_task)
  1769. return 0;
  1770. retry:
  1771. if (unlikely(next_task == rq->curr)) {
  1772. WARN_ON(1);
  1773. return 0;
  1774. }
  1775. /*
  1776. * If next_task preempts rq->curr, and rq->curr
  1777. * can move away, it makes sense to just reschedule
  1778. * without going further in pushing next_task.
  1779. */
  1780. if (dl_task(rq->curr) &&
  1781. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  1782. rq->curr->nr_cpus_allowed > 1) {
  1783. resched_curr(rq);
  1784. return 0;
  1785. }
  1786. /* We might release rq lock */
  1787. get_task_struct(next_task);
  1788. /* Will lock the rq it'll find */
  1789. later_rq = find_lock_later_rq(next_task, rq);
  1790. if (!later_rq) {
  1791. struct task_struct *task;
  1792. /*
  1793. * We must check all this again, since
  1794. * find_lock_later_rq releases rq->lock and it is
  1795. * then possible that next_task has migrated.
  1796. */
  1797. task = pick_next_pushable_dl_task(rq);
  1798. if (task == next_task) {
  1799. /*
  1800. * The task is still there. We don't try
  1801. * again, some other CPU will pull it when ready.
  1802. */
  1803. goto out;
  1804. }
  1805. if (!task)
  1806. /* No more tasks */
  1807. goto out;
  1808. put_task_struct(next_task);
  1809. next_task = task;
  1810. goto retry;
  1811. }
  1812. deactivate_task(rq, next_task, 0);
  1813. sub_running_bw(&next_task->dl, &rq->dl);
  1814. sub_rq_bw(&next_task->dl, &rq->dl);
  1815. set_task_cpu(next_task, later_rq->cpu);
  1816. add_rq_bw(&next_task->dl, &later_rq->dl);
  1817. /*
  1818. * Update the later_rq clock here, because the clock is used
  1819. * by the cpufreq_update_util() inside __add_running_bw().
  1820. */
  1821. update_rq_clock(later_rq);
  1822. add_running_bw(&next_task->dl, &later_rq->dl);
  1823. activate_task(later_rq, next_task, ENQUEUE_NOCLOCK);
  1824. ret = 1;
  1825. resched_curr(later_rq);
  1826. double_unlock_balance(rq, later_rq);
  1827. out:
  1828. put_task_struct(next_task);
  1829. return ret;
  1830. }
  1831. static void push_dl_tasks(struct rq *rq)
  1832. {
  1833. /* push_dl_task() will return true if it moved a -deadline task */
  1834. while (push_dl_task(rq))
  1835. ;
  1836. }
  1837. static void pull_dl_task(struct rq *this_rq)
  1838. {
  1839. int this_cpu = this_rq->cpu, cpu;
  1840. struct task_struct *p;
  1841. bool resched = false;
  1842. struct rq *src_rq;
  1843. u64 dmin = LONG_MAX;
  1844. if (likely(!dl_overloaded(this_rq)))
  1845. return;
  1846. /*
  1847. * Match the barrier from dl_set_overloaded; this guarantees that if we
  1848. * see overloaded we must also see the dlo_mask bit.
  1849. */
  1850. smp_rmb();
  1851. for_each_cpu(cpu, this_rq->rd->dlo_mask) {
  1852. if (this_cpu == cpu)
  1853. continue;
  1854. src_rq = cpu_rq(cpu);
  1855. /*
  1856. * It looks racy, abd it is! However, as in sched_rt.c,
  1857. * we are fine with this.
  1858. */
  1859. if (this_rq->dl.dl_nr_running &&
  1860. dl_time_before(this_rq->dl.earliest_dl.curr,
  1861. src_rq->dl.earliest_dl.next))
  1862. continue;
  1863. /* Might drop this_rq->lock */
  1864. double_lock_balance(this_rq, src_rq);
  1865. /*
  1866. * If there are no more pullable tasks on the
  1867. * rq, we're done with it.
  1868. */
  1869. if (src_rq->dl.dl_nr_running <= 1)
  1870. goto skip;
  1871. p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
  1872. /*
  1873. * We found a task to be pulled if:
  1874. * - it preempts our current (if there's one),
  1875. * - it will preempt the last one we pulled (if any).
  1876. */
  1877. if (p && dl_time_before(p->dl.deadline, dmin) &&
  1878. (!this_rq->dl.dl_nr_running ||
  1879. dl_time_before(p->dl.deadline,
  1880. this_rq->dl.earliest_dl.curr))) {
  1881. WARN_ON(p == src_rq->curr);
  1882. WARN_ON(!task_on_rq_queued(p));
  1883. /*
  1884. * Then we pull iff p has actually an earlier
  1885. * deadline than the current task of its runqueue.
  1886. */
  1887. if (dl_time_before(p->dl.deadline,
  1888. src_rq->curr->dl.deadline))
  1889. goto skip;
  1890. resched = true;
  1891. deactivate_task(src_rq, p, 0);
  1892. sub_running_bw(&p->dl, &src_rq->dl);
  1893. sub_rq_bw(&p->dl, &src_rq->dl);
  1894. set_task_cpu(p, this_cpu);
  1895. add_rq_bw(&p->dl, &this_rq->dl);
  1896. add_running_bw(&p->dl, &this_rq->dl);
  1897. activate_task(this_rq, p, 0);
  1898. dmin = p->dl.deadline;
  1899. /* Is there any other task even earlier? */
  1900. }
  1901. skip:
  1902. double_unlock_balance(this_rq, src_rq);
  1903. }
  1904. if (resched)
  1905. resched_curr(this_rq);
  1906. }
  1907. /*
  1908. * Since the task is not running and a reschedule is not going to happen
  1909. * anytime soon on its runqueue, we try pushing it away now.
  1910. */
  1911. static void task_woken_dl(struct rq *rq, struct task_struct *p)
  1912. {
  1913. if (!task_running(rq, p) &&
  1914. !test_tsk_need_resched(rq->curr) &&
  1915. p->nr_cpus_allowed > 1 &&
  1916. dl_task(rq->curr) &&
  1917. (rq->curr->nr_cpus_allowed < 2 ||
  1918. !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
  1919. push_dl_tasks(rq);
  1920. }
  1921. }
  1922. static void set_cpus_allowed_dl(struct task_struct *p,
  1923. const struct cpumask *new_mask)
  1924. {
  1925. struct root_domain *src_rd;
  1926. struct rq *rq;
  1927. BUG_ON(!dl_task(p));
  1928. rq = task_rq(p);
  1929. src_rd = rq->rd;
  1930. /*
  1931. * Migrating a SCHED_DEADLINE task between exclusive
  1932. * cpusets (different root_domains) entails a bandwidth
  1933. * update. We already made space for us in the destination
  1934. * domain (see cpuset_can_attach()).
  1935. */
  1936. if (!cpumask_intersects(src_rd->span, new_mask)) {
  1937. struct dl_bw *src_dl_b;
  1938. src_dl_b = dl_bw_of(cpu_of(rq));
  1939. /*
  1940. * We now free resources of the root_domain we are migrating
  1941. * off. In the worst case, sched_setattr() may temporary fail
  1942. * until we complete the update.
  1943. */
  1944. raw_spin_lock(&src_dl_b->lock);
  1945. __dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
  1946. raw_spin_unlock(&src_dl_b->lock);
  1947. }
  1948. set_cpus_allowed_common(p, new_mask);
  1949. }
  1950. /* Assumes rq->lock is held */
  1951. static void rq_online_dl(struct rq *rq)
  1952. {
  1953. if (rq->dl.overloaded)
  1954. dl_set_overload(rq);
  1955. cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
  1956. if (rq->dl.dl_nr_running > 0)
  1957. cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
  1958. }
  1959. /* Assumes rq->lock is held */
  1960. static void rq_offline_dl(struct rq *rq)
  1961. {
  1962. if (rq->dl.overloaded)
  1963. dl_clear_overload(rq);
  1964. cpudl_clear(&rq->rd->cpudl, rq->cpu);
  1965. cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
  1966. }
  1967. void __init init_sched_dl_class(void)
  1968. {
  1969. unsigned int i;
  1970. for_each_possible_cpu(i)
  1971. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
  1972. GFP_KERNEL, cpu_to_node(i));
  1973. }
  1974. #endif /* CONFIG_SMP */
  1975. static void switched_from_dl(struct rq *rq, struct task_struct *p)
  1976. {
  1977. /*
  1978. * task_non_contending() can start the "inactive timer" (if the 0-lag
  1979. * time is in the future). If the task switches back to dl before
  1980. * the "inactive timer" fires, it can continue to consume its current
  1981. * runtime using its current deadline. If it stays outside of
  1982. * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
  1983. * will reset the task parameters.
  1984. */
  1985. if (task_on_rq_queued(p) && p->dl.dl_runtime)
  1986. task_non_contending(p);
  1987. if (!task_on_rq_queued(p)) {
  1988. /*
  1989. * Inactive timer is armed. However, p is leaving DEADLINE and
  1990. * might migrate away from this rq while continuing to run on
  1991. * some other class. We need to remove its contribution from
  1992. * this rq running_bw now, or sub_rq_bw (below) will complain.
  1993. */
  1994. if (p->dl.dl_non_contending)
  1995. sub_running_bw(&p->dl, &rq->dl);
  1996. sub_rq_bw(&p->dl, &rq->dl);
  1997. }
  1998. /*
  1999. * We cannot use inactive_task_timer() to invoke sub_running_bw()
  2000. * at the 0-lag time, because the task could have been migrated
  2001. * while SCHED_OTHER in the meanwhile.
  2002. */
  2003. if (p->dl.dl_non_contending)
  2004. p->dl.dl_non_contending = 0;
  2005. /*
  2006. * Since this might be the only -deadline task on the rq,
  2007. * this is the right place to try to pull some other one
  2008. * from an overloaded CPU, if any.
  2009. */
  2010. if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
  2011. return;
  2012. deadline_queue_pull_task(rq);
  2013. }
  2014. /*
  2015. * When switching to -deadline, we may overload the rq, then
  2016. * we try to push someone off, if possible.
  2017. */
  2018. static void switched_to_dl(struct rq *rq, struct task_struct *p)
  2019. {
  2020. if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
  2021. put_task_struct(p);
  2022. /* If p is not queued we will update its parameters at next wakeup. */
  2023. if (!task_on_rq_queued(p)) {
  2024. add_rq_bw(&p->dl, &rq->dl);
  2025. return;
  2026. }
  2027. if (rq->curr != p) {
  2028. #ifdef CONFIG_SMP
  2029. if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
  2030. deadline_queue_push_tasks(rq);
  2031. #endif
  2032. if (dl_task(rq->curr))
  2033. check_preempt_curr_dl(rq, p, 0);
  2034. else
  2035. resched_curr(rq);
  2036. }
  2037. }
  2038. /*
  2039. * If the scheduling parameters of a -deadline task changed,
  2040. * a push or pull operation might be needed.
  2041. */
  2042. static void prio_changed_dl(struct rq *rq, struct task_struct *p,
  2043. int oldprio)
  2044. {
  2045. if (task_on_rq_queued(p) || rq->curr == p) {
  2046. #ifdef CONFIG_SMP
  2047. /*
  2048. * This might be too much, but unfortunately
  2049. * we don't have the old deadline value, and
  2050. * we can't argue if the task is increasing
  2051. * or lowering its prio, so...
  2052. */
  2053. if (!rq->dl.overloaded)
  2054. deadline_queue_pull_task(rq);
  2055. /*
  2056. * If we now have a earlier deadline task than p,
  2057. * then reschedule, provided p is still on this
  2058. * runqueue.
  2059. */
  2060. if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
  2061. resched_curr(rq);
  2062. #else
  2063. /*
  2064. * Again, we don't know if p has a earlier
  2065. * or later deadline, so let's blindly set a
  2066. * (maybe not needed) rescheduling point.
  2067. */
  2068. resched_curr(rq);
  2069. #endif /* CONFIG_SMP */
  2070. }
  2071. }
  2072. const struct sched_class dl_sched_class = {
  2073. .next = &rt_sched_class,
  2074. .enqueue_task = enqueue_task_dl,
  2075. .dequeue_task = dequeue_task_dl,
  2076. .yield_task = yield_task_dl,
  2077. .check_preempt_curr = check_preempt_curr_dl,
  2078. .pick_next_task = pick_next_task_dl,
  2079. .put_prev_task = put_prev_task_dl,
  2080. #ifdef CONFIG_SMP
  2081. .select_task_rq = select_task_rq_dl,
  2082. .migrate_task_rq = migrate_task_rq_dl,
  2083. .set_cpus_allowed = set_cpus_allowed_dl,
  2084. .rq_online = rq_online_dl,
  2085. .rq_offline = rq_offline_dl,
  2086. .task_woken = task_woken_dl,
  2087. #endif
  2088. .set_curr_task = set_curr_task_dl,
  2089. .task_tick = task_tick_dl,
  2090. .task_fork = task_fork_dl,
  2091. .prio_changed = prio_changed_dl,
  2092. .switched_from = switched_from_dl,
  2093. .switched_to = switched_to_dl,
  2094. .update_curr = update_curr_dl,
  2095. };
  2096. int sched_dl_global_validate(void)
  2097. {
  2098. u64 runtime = global_rt_runtime();
  2099. u64 period = global_rt_period();
  2100. u64 new_bw = to_ratio(period, runtime);
  2101. struct dl_bw *dl_b;
  2102. int cpu, cpus, ret = 0;
  2103. unsigned long flags;
  2104. /*
  2105. * Here we want to check the bandwidth not being set to some
  2106. * value smaller than the currently allocated bandwidth in
  2107. * any of the root_domains.
  2108. *
  2109. * FIXME: Cycling on all the CPUs is overdoing, but simpler than
  2110. * cycling on root_domains... Discussion on different/better
  2111. * solutions is welcome!
  2112. */
  2113. for_each_possible_cpu(cpu) {
  2114. rcu_read_lock_sched();
  2115. dl_b = dl_bw_of(cpu);
  2116. cpus = dl_bw_cpus(cpu);
  2117. raw_spin_lock_irqsave(&dl_b->lock, flags);
  2118. if (new_bw * cpus < dl_b->total_bw)
  2119. ret = -EBUSY;
  2120. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  2121. rcu_read_unlock_sched();
  2122. if (ret)
  2123. break;
  2124. }
  2125. return ret;
  2126. }
  2127. void init_dl_rq_bw_ratio(struct dl_rq *dl_rq)
  2128. {
  2129. if (global_rt_runtime() == RUNTIME_INF) {
  2130. dl_rq->bw_ratio = 1 << RATIO_SHIFT;
  2131. dl_rq->extra_bw = 1 << BW_SHIFT;
  2132. } else {
  2133. dl_rq->bw_ratio = to_ratio(global_rt_runtime(),
  2134. global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT);
  2135. dl_rq->extra_bw = to_ratio(global_rt_period(),
  2136. global_rt_runtime());
  2137. }
  2138. }
  2139. void sched_dl_do_global(void)
  2140. {
  2141. u64 new_bw = -1;
  2142. struct dl_bw *dl_b;
  2143. int cpu;
  2144. unsigned long flags;
  2145. def_dl_bandwidth.dl_period = global_rt_period();
  2146. def_dl_bandwidth.dl_runtime = global_rt_runtime();
  2147. if (global_rt_runtime() != RUNTIME_INF)
  2148. new_bw = to_ratio(global_rt_period(), global_rt_runtime());
  2149. /*
  2150. * FIXME: As above...
  2151. */
  2152. for_each_possible_cpu(cpu) {
  2153. rcu_read_lock_sched();
  2154. dl_b = dl_bw_of(cpu);
  2155. raw_spin_lock_irqsave(&dl_b->lock, flags);
  2156. dl_b->bw = new_bw;
  2157. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  2158. rcu_read_unlock_sched();
  2159. init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl);
  2160. }
  2161. }
  2162. /*
  2163. * We must be sure that accepting a new task (or allowing changing the
  2164. * parameters of an existing one) is consistent with the bandwidth
  2165. * constraints. If yes, this function also accordingly updates the currently
  2166. * allocated bandwidth to reflect the new situation.
  2167. *
  2168. * This function is called while holding p's rq->lock.
  2169. */
  2170. int sched_dl_overflow(struct task_struct *p, int policy,
  2171. const struct sched_attr *attr)
  2172. {
  2173. struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
  2174. u64 period = attr->sched_period ?: attr->sched_deadline;
  2175. u64 runtime = attr->sched_runtime;
  2176. u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
  2177. int cpus, err = -1;
  2178. if (attr->sched_flags & SCHED_FLAG_SUGOV)
  2179. return 0;
  2180. /* !deadline task may carry old deadline bandwidth */
  2181. if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
  2182. return 0;
  2183. /*
  2184. * Either if a task, enters, leave, or stays -deadline but changes
  2185. * its parameters, we may need to update accordingly the total
  2186. * allocated bandwidth of the container.
  2187. */
  2188. raw_spin_lock(&dl_b->lock);
  2189. cpus = dl_bw_cpus(task_cpu(p));
  2190. if (dl_policy(policy) && !task_has_dl_policy(p) &&
  2191. !__dl_overflow(dl_b, cpus, 0, new_bw)) {
  2192. if (hrtimer_active(&p->dl.inactive_timer))
  2193. __dl_sub(dl_b, p->dl.dl_bw, cpus);
  2194. __dl_add(dl_b, new_bw, cpus);
  2195. err = 0;
  2196. } else if (dl_policy(policy) && task_has_dl_policy(p) &&
  2197. !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
  2198. /*
  2199. * XXX this is slightly incorrect: when the task
  2200. * utilization decreases, we should delay the total
  2201. * utilization change until the task's 0-lag point.
  2202. * But this would require to set the task's "inactive
  2203. * timer" when the task is not inactive.
  2204. */
  2205. __dl_sub(dl_b, p->dl.dl_bw, cpus);
  2206. __dl_add(dl_b, new_bw, cpus);
  2207. dl_change_utilization(p, new_bw);
  2208. err = 0;
  2209. } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
  2210. /*
  2211. * Do not decrease the total deadline utilization here,
  2212. * switched_from_dl() will take care to do it at the correct
  2213. * (0-lag) time.
  2214. */
  2215. err = 0;
  2216. }
  2217. raw_spin_unlock(&dl_b->lock);
  2218. return err;
  2219. }
  2220. /*
  2221. * This function initializes the sched_dl_entity of a newly becoming
  2222. * SCHED_DEADLINE task.
  2223. *
  2224. * Only the static values are considered here, the actual runtime and the
  2225. * absolute deadline will be properly calculated when the task is enqueued
  2226. * for the first time with its new policy.
  2227. */
  2228. void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
  2229. {
  2230. struct sched_dl_entity *dl_se = &p->dl;
  2231. dl_se->dl_runtime = attr->sched_runtime;
  2232. dl_se->dl_deadline = attr->sched_deadline;
  2233. dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
  2234. dl_se->flags = attr->sched_flags;
  2235. dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
  2236. dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
  2237. }
  2238. void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
  2239. {
  2240. struct sched_dl_entity *dl_se = &p->dl;
  2241. attr->sched_priority = p->rt_priority;
  2242. attr->sched_runtime = dl_se->dl_runtime;
  2243. attr->sched_deadline = dl_se->dl_deadline;
  2244. attr->sched_period = dl_se->dl_period;
  2245. attr->sched_flags = dl_se->flags;
  2246. }
  2247. /*
  2248. * This function validates the new parameters of a -deadline task.
  2249. * We ask for the deadline not being zero, and greater or equal
  2250. * than the runtime, as well as the period of being zero or
  2251. * greater than deadline. Furthermore, we have to be sure that
  2252. * user parameters are above the internal resolution of 1us (we
  2253. * check sched_runtime only since it is always the smaller one) and
  2254. * below 2^63 ns (we have to check both sched_deadline and
  2255. * sched_period, as the latter can be zero).
  2256. */
  2257. bool __checkparam_dl(const struct sched_attr *attr)
  2258. {
  2259. /* special dl tasks don't actually use any parameter */
  2260. if (attr->sched_flags & SCHED_FLAG_SUGOV)
  2261. return true;
  2262. /* deadline != 0 */
  2263. if (attr->sched_deadline == 0)
  2264. return false;
  2265. /*
  2266. * Since we truncate DL_SCALE bits, make sure we're at least
  2267. * that big.
  2268. */
  2269. if (attr->sched_runtime < (1ULL << DL_SCALE))
  2270. return false;
  2271. /*
  2272. * Since we use the MSB for wrap-around and sign issues, make
  2273. * sure it's not set (mind that period can be equal to zero).
  2274. */
  2275. if (attr->sched_deadline & (1ULL << 63) ||
  2276. attr->sched_period & (1ULL << 63))
  2277. return false;
  2278. /* runtime <= deadline <= period (if period != 0) */
  2279. if ((attr->sched_period != 0 &&
  2280. attr->sched_period < attr->sched_deadline) ||
  2281. attr->sched_deadline < attr->sched_runtime)
  2282. return false;
  2283. return true;
  2284. }
  2285. /*
  2286. * This function clears the sched_dl_entity static params.
  2287. */
  2288. void __dl_clear_params(struct task_struct *p)
  2289. {
  2290. struct sched_dl_entity *dl_se = &p->dl;
  2291. dl_se->dl_runtime = 0;
  2292. dl_se->dl_deadline = 0;
  2293. dl_se->dl_period = 0;
  2294. dl_se->flags = 0;
  2295. dl_se->dl_bw = 0;
  2296. dl_se->dl_density = 0;
  2297. dl_se->dl_boosted = 0;
  2298. dl_se->dl_throttled = 0;
  2299. dl_se->dl_yielded = 0;
  2300. dl_se->dl_non_contending = 0;
  2301. dl_se->dl_overrun = 0;
  2302. }
  2303. bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
  2304. {
  2305. struct sched_dl_entity *dl_se = &p->dl;
  2306. if (dl_se->dl_runtime != attr->sched_runtime ||
  2307. dl_se->dl_deadline != attr->sched_deadline ||
  2308. dl_se->dl_period != attr->sched_period ||
  2309. dl_se->flags != attr->sched_flags)
  2310. return true;
  2311. return false;
  2312. }
  2313. #ifdef CONFIG_SMP
  2314. int dl_task_can_attach(struct task_struct *p, const struct cpumask *cs_cpus_allowed)
  2315. {
  2316. unsigned int dest_cpu;
  2317. struct dl_bw *dl_b;
  2318. bool overflow;
  2319. int cpus, ret;
  2320. unsigned long flags;
  2321. dest_cpu = cpumask_any_and(cpu_active_mask, cs_cpus_allowed);
  2322. rcu_read_lock_sched();
  2323. dl_b = dl_bw_of(dest_cpu);
  2324. raw_spin_lock_irqsave(&dl_b->lock, flags);
  2325. cpus = dl_bw_cpus(dest_cpu);
  2326. overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
  2327. if (overflow) {
  2328. ret = -EBUSY;
  2329. } else {
  2330. /*
  2331. * We reserve space for this task in the destination
  2332. * root_domain, as we can't fail after this point.
  2333. * We will free resources in the source root_domain
  2334. * later on (see set_cpus_allowed_dl()).
  2335. */
  2336. __dl_add(dl_b, p->dl.dl_bw, cpus);
  2337. ret = 0;
  2338. }
  2339. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  2340. rcu_read_unlock_sched();
  2341. return ret;
  2342. }
  2343. int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
  2344. const struct cpumask *trial)
  2345. {
  2346. int ret = 1, trial_cpus;
  2347. struct dl_bw *cur_dl_b;
  2348. unsigned long flags;
  2349. rcu_read_lock_sched();
  2350. cur_dl_b = dl_bw_of(cpumask_any(cur));
  2351. trial_cpus = cpumask_weight(trial);
  2352. raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
  2353. if (cur_dl_b->bw != -1 &&
  2354. cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
  2355. ret = 0;
  2356. raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
  2357. rcu_read_unlock_sched();
  2358. return ret;
  2359. }
  2360. bool dl_cpu_busy(unsigned int cpu)
  2361. {
  2362. unsigned long flags;
  2363. struct dl_bw *dl_b;
  2364. bool overflow;
  2365. int cpus;
  2366. rcu_read_lock_sched();
  2367. dl_b = dl_bw_of(cpu);
  2368. raw_spin_lock_irqsave(&dl_b->lock, flags);
  2369. cpus = dl_bw_cpus(cpu);
  2370. overflow = __dl_overflow(dl_b, cpus, 0, 0);
  2371. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  2372. rcu_read_unlock_sched();
  2373. return overflow;
  2374. }
  2375. #endif
  2376. #ifdef CONFIG_SCHED_DEBUG
  2377. void print_dl_stats(struct seq_file *m, int cpu)
  2378. {
  2379. print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
  2380. }
  2381. #endif /* CONFIG_SCHED_DEBUG */