deadline.c 75 KB

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