deadline.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554
  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 <linux/cpuset.h>
  19. /*
  20. * Default limits for DL period; on the top end we guard against small util
  21. * tasks still getting ridiculously long effective runtimes, on the bottom end we
  22. * guard against timer DoS.
  23. */
  24. static unsigned int sysctl_sched_dl_period_max = 1 << 22; /* ~4 seconds */
  25. static unsigned int sysctl_sched_dl_period_min = 100; /* 100 us */
  26. #ifdef CONFIG_SYSCTL
  27. static struct ctl_table sched_dl_sysctls[] = {
  28. {
  29. .procname = "sched_deadline_period_max_us",
  30. .data = &sysctl_sched_dl_period_max,
  31. .maxlen = sizeof(unsigned int),
  32. .mode = 0644,
  33. .proc_handler = proc_douintvec_minmax,
  34. .extra1 = (void *)&sysctl_sched_dl_period_min,
  35. },
  36. {
  37. .procname = "sched_deadline_period_min_us",
  38. .data = &sysctl_sched_dl_period_min,
  39. .maxlen = sizeof(unsigned int),
  40. .mode = 0644,
  41. .proc_handler = proc_douintvec_minmax,
  42. .extra2 = (void *)&sysctl_sched_dl_period_max,
  43. },
  44. };
  45. static int __init sched_dl_sysctl_init(void)
  46. {
  47. register_sysctl_init("kernel", sched_dl_sysctls);
  48. return 0;
  49. }
  50. late_initcall(sched_dl_sysctl_init);
  51. #endif
  52. static bool dl_server(struct sched_dl_entity *dl_se)
  53. {
  54. return dl_se->dl_server;
  55. }
  56. static inline struct task_struct *dl_task_of(struct sched_dl_entity *dl_se)
  57. {
  58. BUG_ON(dl_server(dl_se));
  59. return container_of(dl_se, struct task_struct, dl);
  60. }
  61. static inline struct rq *rq_of_dl_rq(struct dl_rq *dl_rq)
  62. {
  63. return container_of(dl_rq, struct rq, dl);
  64. }
  65. static inline struct rq *rq_of_dl_se(struct sched_dl_entity *dl_se)
  66. {
  67. struct rq *rq = dl_se->rq;
  68. if (!dl_server(dl_se))
  69. rq = task_rq(dl_task_of(dl_se));
  70. return rq;
  71. }
  72. static inline struct dl_rq *dl_rq_of_se(struct sched_dl_entity *dl_se)
  73. {
  74. return &rq_of_dl_se(dl_se)->dl;
  75. }
  76. static inline int on_dl_rq(struct sched_dl_entity *dl_se)
  77. {
  78. return !RB_EMPTY_NODE(&dl_se->rb_node);
  79. }
  80. #ifdef CONFIG_RT_MUTEXES
  81. static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
  82. {
  83. return dl_se->pi_se;
  84. }
  85. static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
  86. {
  87. return pi_of(dl_se) != dl_se;
  88. }
  89. #else
  90. static inline struct sched_dl_entity *pi_of(struct sched_dl_entity *dl_se)
  91. {
  92. return dl_se;
  93. }
  94. static inline bool is_dl_boosted(struct sched_dl_entity *dl_se)
  95. {
  96. return false;
  97. }
  98. #endif
  99. #ifdef CONFIG_SMP
  100. static inline struct dl_bw *dl_bw_of(int i)
  101. {
  102. RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
  103. "sched RCU must be held");
  104. return &cpu_rq(i)->rd->dl_bw;
  105. }
  106. static inline int dl_bw_cpus(int i)
  107. {
  108. struct root_domain *rd = cpu_rq(i)->rd;
  109. int cpus;
  110. RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
  111. "sched RCU must be held");
  112. if (cpumask_subset(rd->span, cpu_active_mask))
  113. return cpumask_weight(rd->span);
  114. cpus = 0;
  115. for_each_cpu_and(i, rd->span, cpu_active_mask)
  116. cpus++;
  117. return cpus;
  118. }
  119. static inline unsigned long __dl_bw_capacity(const struct cpumask *mask)
  120. {
  121. unsigned long cap = 0;
  122. int i;
  123. for_each_cpu_and(i, mask, cpu_active_mask)
  124. cap += arch_scale_cpu_capacity(i);
  125. return cap;
  126. }
  127. /*
  128. * XXX Fix: If 'rq->rd == def_root_domain' perform AC against capacity
  129. * of the CPU the task is running on rather rd's \Sum CPU capacity.
  130. */
  131. static inline unsigned long dl_bw_capacity(int i)
  132. {
  133. if (!sched_asym_cpucap_active() &&
  134. arch_scale_cpu_capacity(i) == SCHED_CAPACITY_SCALE) {
  135. return dl_bw_cpus(i) << SCHED_CAPACITY_SHIFT;
  136. } else {
  137. RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
  138. "sched RCU must be held");
  139. return __dl_bw_capacity(cpu_rq(i)->rd->span);
  140. }
  141. }
  142. static inline bool dl_bw_visited(int cpu, u64 gen)
  143. {
  144. struct root_domain *rd = cpu_rq(cpu)->rd;
  145. if (rd->visit_gen == gen)
  146. return true;
  147. rd->visit_gen = gen;
  148. return false;
  149. }
  150. static inline
  151. void __dl_update(struct dl_bw *dl_b, s64 bw)
  152. {
  153. struct root_domain *rd = container_of(dl_b, struct root_domain, dl_bw);
  154. int i;
  155. RCU_LOCKDEP_WARN(!rcu_read_lock_sched_held(),
  156. "sched RCU must be held");
  157. for_each_cpu_and(i, rd->span, cpu_active_mask) {
  158. struct rq *rq = cpu_rq(i);
  159. rq->dl.extra_bw += bw;
  160. }
  161. }
  162. #else
  163. static inline struct dl_bw *dl_bw_of(int i)
  164. {
  165. return &cpu_rq(i)->dl.dl_bw;
  166. }
  167. static inline int dl_bw_cpus(int i)
  168. {
  169. return 1;
  170. }
  171. static inline unsigned long dl_bw_capacity(int i)
  172. {
  173. return SCHED_CAPACITY_SCALE;
  174. }
  175. static inline bool dl_bw_visited(int cpu, u64 gen)
  176. {
  177. return false;
  178. }
  179. static inline
  180. void __dl_update(struct dl_bw *dl_b, s64 bw)
  181. {
  182. struct dl_rq *dl = container_of(dl_b, struct dl_rq, dl_bw);
  183. dl->extra_bw += bw;
  184. }
  185. #endif
  186. static inline
  187. void __dl_sub(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
  188. {
  189. dl_b->total_bw -= tsk_bw;
  190. __dl_update(dl_b, (s32)tsk_bw / cpus);
  191. }
  192. static inline
  193. void __dl_add(struct dl_bw *dl_b, u64 tsk_bw, int cpus)
  194. {
  195. dl_b->total_bw += tsk_bw;
  196. __dl_update(dl_b, -((s32)tsk_bw / cpus));
  197. }
  198. static inline bool
  199. __dl_overflow(struct dl_bw *dl_b, unsigned long cap, u64 old_bw, u64 new_bw)
  200. {
  201. return dl_b->bw != -1 &&
  202. cap_scale(dl_b->bw, cap) < dl_b->total_bw - old_bw + new_bw;
  203. }
  204. static inline
  205. void __add_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
  206. {
  207. u64 old = dl_rq->running_bw;
  208. lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
  209. dl_rq->running_bw += dl_bw;
  210. SCHED_WARN_ON(dl_rq->running_bw < old); /* overflow */
  211. SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
  212. /* kick cpufreq (see the comment in kernel/sched/sched.h). */
  213. cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
  214. }
  215. static inline
  216. void __sub_running_bw(u64 dl_bw, struct dl_rq *dl_rq)
  217. {
  218. u64 old = dl_rq->running_bw;
  219. lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
  220. dl_rq->running_bw -= dl_bw;
  221. SCHED_WARN_ON(dl_rq->running_bw > old); /* underflow */
  222. if (dl_rq->running_bw > old)
  223. dl_rq->running_bw = 0;
  224. /* kick cpufreq (see the comment in kernel/sched/sched.h). */
  225. cpufreq_update_util(rq_of_dl_rq(dl_rq), 0);
  226. }
  227. static inline
  228. void __add_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
  229. {
  230. u64 old = dl_rq->this_bw;
  231. lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
  232. dl_rq->this_bw += dl_bw;
  233. SCHED_WARN_ON(dl_rq->this_bw < old); /* overflow */
  234. }
  235. static inline
  236. void __sub_rq_bw(u64 dl_bw, struct dl_rq *dl_rq)
  237. {
  238. u64 old = dl_rq->this_bw;
  239. lockdep_assert_rq_held(rq_of_dl_rq(dl_rq));
  240. dl_rq->this_bw -= dl_bw;
  241. SCHED_WARN_ON(dl_rq->this_bw > old); /* underflow */
  242. if (dl_rq->this_bw > old)
  243. dl_rq->this_bw = 0;
  244. SCHED_WARN_ON(dl_rq->running_bw > dl_rq->this_bw);
  245. }
  246. static inline
  247. void add_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  248. {
  249. if (!dl_entity_is_special(dl_se))
  250. __add_rq_bw(dl_se->dl_bw, dl_rq);
  251. }
  252. static inline
  253. void sub_rq_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  254. {
  255. if (!dl_entity_is_special(dl_se))
  256. __sub_rq_bw(dl_se->dl_bw, dl_rq);
  257. }
  258. static inline
  259. void add_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  260. {
  261. if (!dl_entity_is_special(dl_se))
  262. __add_running_bw(dl_se->dl_bw, dl_rq);
  263. }
  264. static inline
  265. void sub_running_bw(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  266. {
  267. if (!dl_entity_is_special(dl_se))
  268. __sub_running_bw(dl_se->dl_bw, dl_rq);
  269. }
  270. static void dl_rq_change_utilization(struct rq *rq, struct sched_dl_entity *dl_se, u64 new_bw)
  271. {
  272. if (dl_se->dl_non_contending) {
  273. sub_running_bw(dl_se, &rq->dl);
  274. dl_se->dl_non_contending = 0;
  275. /*
  276. * If the timer handler is currently running and the
  277. * timer cannot be canceled, inactive_task_timer()
  278. * will see that dl_not_contending is not set, and
  279. * will not touch the rq's active utilization,
  280. * so we are still safe.
  281. */
  282. if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1) {
  283. if (!dl_server(dl_se))
  284. put_task_struct(dl_task_of(dl_se));
  285. }
  286. }
  287. __sub_rq_bw(dl_se->dl_bw, &rq->dl);
  288. __add_rq_bw(new_bw, &rq->dl);
  289. }
  290. static void dl_change_utilization(struct task_struct *p, u64 new_bw)
  291. {
  292. WARN_ON_ONCE(p->dl.flags & SCHED_FLAG_SUGOV);
  293. if (task_on_rq_queued(p))
  294. return;
  295. dl_rq_change_utilization(task_rq(p), &p->dl, new_bw);
  296. }
  297. static void __dl_clear_params(struct sched_dl_entity *dl_se);
  298. /*
  299. * The utilization of a task cannot be immediately removed from
  300. * the rq active utilization (running_bw) when the task blocks.
  301. * Instead, we have to wait for the so called "0-lag time".
  302. *
  303. * If a task blocks before the "0-lag time", a timer (the inactive
  304. * timer) is armed, and running_bw is decreased when the timer
  305. * fires.
  306. *
  307. * If the task wakes up again before the inactive timer fires,
  308. * the timer is canceled, whereas if the task wakes up after the
  309. * inactive timer fired (and running_bw has been decreased) the
  310. * task's utilization has to be added to running_bw again.
  311. * A flag in the deadline scheduling entity (dl_non_contending)
  312. * is used to avoid race conditions between the inactive timer handler
  313. * and task wakeups.
  314. *
  315. * The following diagram shows how running_bw is updated. A task is
  316. * "ACTIVE" when its utilization contributes to running_bw; an
  317. * "ACTIVE contending" task is in the TASK_RUNNING state, while an
  318. * "ACTIVE non contending" task is a blocked task for which the "0-lag time"
  319. * has not passed yet. An "INACTIVE" task is a task for which the "0-lag"
  320. * time already passed, which does not contribute to running_bw anymore.
  321. * +------------------+
  322. * wakeup | ACTIVE |
  323. * +------------------>+ contending |
  324. * | add_running_bw | |
  325. * | +----+------+------+
  326. * | | ^
  327. * | dequeue | |
  328. * +--------+-------+ | |
  329. * | | t >= 0-lag | | wakeup
  330. * | INACTIVE |<---------------+ |
  331. * | | sub_running_bw | |
  332. * +--------+-------+ | |
  333. * ^ | |
  334. * | t < 0-lag | |
  335. * | | |
  336. * | V |
  337. * | +----+------+------+
  338. * | sub_running_bw | ACTIVE |
  339. * +-------------------+ |
  340. * inactive timer | non contending |
  341. * fired +------------------+
  342. *
  343. * The task_non_contending() function is invoked when a task
  344. * blocks, and checks if the 0-lag time already passed or
  345. * not (in the first case, it directly updates running_bw;
  346. * in the second case, it arms the inactive timer).
  347. *
  348. * The task_contending() function is invoked when a task wakes
  349. * up, and checks if the task is still in the "ACTIVE non contending"
  350. * state or not (in the second case, it updates running_bw).
  351. */
  352. static void task_non_contending(struct sched_dl_entity *dl_se)
  353. {
  354. struct hrtimer *timer = &dl_se->inactive_timer;
  355. struct rq *rq = rq_of_dl_se(dl_se);
  356. struct dl_rq *dl_rq = &rq->dl;
  357. s64 zerolag_time;
  358. /*
  359. * If this is a non-deadline task that has been boosted,
  360. * do nothing
  361. */
  362. if (dl_se->dl_runtime == 0)
  363. return;
  364. if (dl_entity_is_special(dl_se))
  365. return;
  366. WARN_ON(dl_se->dl_non_contending);
  367. zerolag_time = dl_se->deadline -
  368. div64_long((dl_se->runtime * dl_se->dl_period),
  369. dl_se->dl_runtime);
  370. /*
  371. * Using relative times instead of the absolute "0-lag time"
  372. * allows to simplify the code
  373. */
  374. zerolag_time -= rq_clock(rq);
  375. /*
  376. * If the "0-lag time" already passed, decrease the active
  377. * utilization now, instead of starting a timer
  378. */
  379. if ((zerolag_time < 0) || hrtimer_active(&dl_se->inactive_timer)) {
  380. if (dl_server(dl_se)) {
  381. sub_running_bw(dl_se, dl_rq);
  382. } else {
  383. struct task_struct *p = dl_task_of(dl_se);
  384. if (dl_task(p))
  385. sub_running_bw(dl_se, dl_rq);
  386. if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD) {
  387. struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
  388. if (READ_ONCE(p->__state) == TASK_DEAD)
  389. sub_rq_bw(dl_se, &rq->dl);
  390. raw_spin_lock(&dl_b->lock);
  391. __dl_sub(dl_b, dl_se->dl_bw, dl_bw_cpus(task_cpu(p)));
  392. raw_spin_unlock(&dl_b->lock);
  393. __dl_clear_params(dl_se);
  394. }
  395. }
  396. return;
  397. }
  398. dl_se->dl_non_contending = 1;
  399. if (!dl_server(dl_se))
  400. get_task_struct(dl_task_of(dl_se));
  401. hrtimer_start(timer, ns_to_ktime(zerolag_time), HRTIMER_MODE_REL_HARD);
  402. }
  403. static void task_contending(struct sched_dl_entity *dl_se, int flags)
  404. {
  405. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  406. /*
  407. * If this is a non-deadline task that has been boosted,
  408. * do nothing
  409. */
  410. if (dl_se->dl_runtime == 0)
  411. return;
  412. if (flags & ENQUEUE_MIGRATED)
  413. add_rq_bw(dl_se, dl_rq);
  414. if (dl_se->dl_non_contending) {
  415. dl_se->dl_non_contending = 0;
  416. /*
  417. * If the timer handler is currently running and the
  418. * timer cannot be canceled, inactive_task_timer()
  419. * will see that dl_not_contending is not set, and
  420. * will not touch the rq's active utilization,
  421. * so we are still safe.
  422. */
  423. if (hrtimer_try_to_cancel(&dl_se->inactive_timer) == 1) {
  424. if (!dl_server(dl_se))
  425. put_task_struct(dl_task_of(dl_se));
  426. }
  427. } else {
  428. /*
  429. * Since "dl_non_contending" is not set, the
  430. * task's utilization has already been removed from
  431. * active utilization (either when the task blocked,
  432. * when the "inactive timer" fired).
  433. * So, add it back.
  434. */
  435. add_running_bw(dl_se, dl_rq);
  436. }
  437. }
  438. static inline int is_leftmost(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  439. {
  440. return rb_first_cached(&dl_rq->root) == &dl_se->rb_node;
  441. }
  442. static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq);
  443. void init_dl_bw(struct dl_bw *dl_b)
  444. {
  445. raw_spin_lock_init(&dl_b->lock);
  446. if (global_rt_runtime() == RUNTIME_INF)
  447. dl_b->bw = -1;
  448. else
  449. dl_b->bw = to_ratio(global_rt_period(), global_rt_runtime());
  450. dl_b->total_bw = 0;
  451. }
  452. void init_dl_rq(struct dl_rq *dl_rq)
  453. {
  454. dl_rq->root = RB_ROOT_CACHED;
  455. #ifdef CONFIG_SMP
  456. /* zero means no -deadline tasks */
  457. dl_rq->earliest_dl.curr = dl_rq->earliest_dl.next = 0;
  458. dl_rq->overloaded = 0;
  459. dl_rq->pushable_dl_tasks_root = RB_ROOT_CACHED;
  460. #else
  461. init_dl_bw(&dl_rq->dl_bw);
  462. #endif
  463. dl_rq->running_bw = 0;
  464. dl_rq->this_bw = 0;
  465. init_dl_rq_bw_ratio(dl_rq);
  466. }
  467. #ifdef CONFIG_SMP
  468. static inline int dl_overloaded(struct rq *rq)
  469. {
  470. return atomic_read(&rq->rd->dlo_count);
  471. }
  472. static inline void dl_set_overload(struct rq *rq)
  473. {
  474. if (!rq->online)
  475. return;
  476. cpumask_set_cpu(rq->cpu, rq->rd->dlo_mask);
  477. /*
  478. * Must be visible before the overload count is
  479. * set (as in sched_rt.c).
  480. *
  481. * Matched by the barrier in pull_dl_task().
  482. */
  483. smp_wmb();
  484. atomic_inc(&rq->rd->dlo_count);
  485. }
  486. static inline void dl_clear_overload(struct rq *rq)
  487. {
  488. if (!rq->online)
  489. return;
  490. atomic_dec(&rq->rd->dlo_count);
  491. cpumask_clear_cpu(rq->cpu, rq->rd->dlo_mask);
  492. }
  493. #define __node_2_pdl(node) \
  494. rb_entry((node), struct task_struct, pushable_dl_tasks)
  495. static inline bool __pushable_less(struct rb_node *a, const struct rb_node *b)
  496. {
  497. return dl_entity_preempt(&__node_2_pdl(a)->dl, &__node_2_pdl(b)->dl);
  498. }
  499. static inline int has_pushable_dl_tasks(struct rq *rq)
  500. {
  501. return !RB_EMPTY_ROOT(&rq->dl.pushable_dl_tasks_root.rb_root);
  502. }
  503. /*
  504. * The list of pushable -deadline task is not a plist, like in
  505. * sched_rt.c, it is an rb-tree with tasks ordered by deadline.
  506. */
  507. static void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  508. {
  509. struct rb_node *leftmost;
  510. WARN_ON_ONCE(!RB_EMPTY_NODE(&p->pushable_dl_tasks));
  511. leftmost = rb_add_cached(&p->pushable_dl_tasks,
  512. &rq->dl.pushable_dl_tasks_root,
  513. __pushable_less);
  514. if (leftmost)
  515. rq->dl.earliest_dl.next = p->dl.deadline;
  516. if (!rq->dl.overloaded) {
  517. dl_set_overload(rq);
  518. rq->dl.overloaded = 1;
  519. }
  520. }
  521. static void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  522. {
  523. struct dl_rq *dl_rq = &rq->dl;
  524. struct rb_root_cached *root = &dl_rq->pushable_dl_tasks_root;
  525. struct rb_node *leftmost;
  526. if (RB_EMPTY_NODE(&p->pushable_dl_tasks))
  527. return;
  528. leftmost = rb_erase_cached(&p->pushable_dl_tasks, root);
  529. if (leftmost)
  530. dl_rq->earliest_dl.next = __node_2_pdl(leftmost)->dl.deadline;
  531. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  532. if (!has_pushable_dl_tasks(rq) && rq->dl.overloaded) {
  533. dl_clear_overload(rq);
  534. rq->dl.overloaded = 0;
  535. }
  536. }
  537. static int push_dl_task(struct rq *rq);
  538. static inline bool need_pull_dl_task(struct rq *rq, struct task_struct *prev)
  539. {
  540. return rq->online && dl_task(prev);
  541. }
  542. static DEFINE_PER_CPU(struct balance_callback, dl_push_head);
  543. static DEFINE_PER_CPU(struct balance_callback, dl_pull_head);
  544. static void push_dl_tasks(struct rq *);
  545. static void pull_dl_task(struct rq *);
  546. static inline void deadline_queue_push_tasks(struct rq *rq)
  547. {
  548. if (!has_pushable_dl_tasks(rq))
  549. return;
  550. queue_balance_callback(rq, &per_cpu(dl_push_head, rq->cpu), push_dl_tasks);
  551. }
  552. static inline void deadline_queue_pull_task(struct rq *rq)
  553. {
  554. queue_balance_callback(rq, &per_cpu(dl_pull_head, rq->cpu), pull_dl_task);
  555. }
  556. static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq);
  557. static struct rq *dl_task_offline_migration(struct rq *rq, struct task_struct *p)
  558. {
  559. struct rq *later_rq = NULL;
  560. struct dl_bw *dl_b;
  561. later_rq = find_lock_later_rq(p, rq);
  562. if (!later_rq) {
  563. int cpu;
  564. /*
  565. * If we cannot preempt any rq, fall back to pick any
  566. * online CPU:
  567. */
  568. cpu = cpumask_any_and(cpu_active_mask, p->cpus_ptr);
  569. if (cpu >= nr_cpu_ids) {
  570. /*
  571. * Failed to find any suitable CPU.
  572. * The task will never come back!
  573. */
  574. WARN_ON_ONCE(dl_bandwidth_enabled());
  575. /*
  576. * If admission control is disabled we
  577. * try a little harder to let the task
  578. * run.
  579. */
  580. cpu = cpumask_any(cpu_active_mask);
  581. }
  582. later_rq = cpu_rq(cpu);
  583. double_lock_balance(rq, later_rq);
  584. }
  585. if (p->dl.dl_non_contending || p->dl.dl_throttled) {
  586. /*
  587. * Inactive timer is armed (or callback is running, but
  588. * waiting for us to release rq locks). In any case, when it
  589. * will fire (or continue), it will see running_bw of this
  590. * task migrated to later_rq (and correctly handle it).
  591. */
  592. sub_running_bw(&p->dl, &rq->dl);
  593. sub_rq_bw(&p->dl, &rq->dl);
  594. add_rq_bw(&p->dl, &later_rq->dl);
  595. add_running_bw(&p->dl, &later_rq->dl);
  596. } else {
  597. sub_rq_bw(&p->dl, &rq->dl);
  598. add_rq_bw(&p->dl, &later_rq->dl);
  599. }
  600. /*
  601. * And we finally need to fix up root_domain(s) bandwidth accounting,
  602. * since p is still hanging out in the old (now moved to default) root
  603. * domain.
  604. */
  605. dl_b = &rq->rd->dl_bw;
  606. raw_spin_lock(&dl_b->lock);
  607. __dl_sub(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
  608. raw_spin_unlock(&dl_b->lock);
  609. dl_b = &later_rq->rd->dl_bw;
  610. raw_spin_lock(&dl_b->lock);
  611. __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(later_rq->rd->span));
  612. raw_spin_unlock(&dl_b->lock);
  613. set_task_cpu(p, later_rq->cpu);
  614. double_unlock_balance(later_rq, rq);
  615. return later_rq;
  616. }
  617. #else
  618. static inline
  619. void enqueue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  620. {
  621. }
  622. static inline
  623. void dequeue_pushable_dl_task(struct rq *rq, struct task_struct *p)
  624. {
  625. }
  626. static inline
  627. void inc_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  628. {
  629. }
  630. static inline
  631. void dec_dl_migration(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  632. {
  633. }
  634. static inline void deadline_queue_push_tasks(struct rq *rq)
  635. {
  636. }
  637. static inline void deadline_queue_pull_task(struct rq *rq)
  638. {
  639. }
  640. #endif /* CONFIG_SMP */
  641. static void
  642. enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags);
  643. static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags);
  644. static void dequeue_dl_entity(struct sched_dl_entity *dl_se, int flags);
  645. static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p, int flags);
  646. static inline void replenish_dl_new_period(struct sched_dl_entity *dl_se,
  647. struct rq *rq)
  648. {
  649. /* for non-boosted task, pi_of(dl_se) == dl_se */
  650. dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
  651. dl_se->runtime = pi_of(dl_se)->dl_runtime;
  652. /*
  653. * If it is a deferred reservation, and the server
  654. * is not handling an starvation case, defer it.
  655. */
  656. if (dl_se->dl_defer && !dl_se->dl_defer_running) {
  657. dl_se->dl_throttled = 1;
  658. dl_se->dl_defer_armed = 1;
  659. }
  660. }
  661. /*
  662. * We are being explicitly informed that a new instance is starting,
  663. * and this means that:
  664. * - the absolute deadline of the entity has to be placed at
  665. * current time + relative deadline;
  666. * - the runtime of the entity has to be set to the maximum value.
  667. *
  668. * The capability of specifying such event is useful whenever a -deadline
  669. * entity wants to (try to!) synchronize its behaviour with the scheduler's
  670. * one, and to (try to!) reconcile itself with its own scheduling
  671. * parameters.
  672. */
  673. static inline void setup_new_dl_entity(struct sched_dl_entity *dl_se)
  674. {
  675. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  676. struct rq *rq = rq_of_dl_rq(dl_rq);
  677. WARN_ON(is_dl_boosted(dl_se));
  678. WARN_ON(dl_time_before(rq_clock(rq), dl_se->deadline));
  679. /*
  680. * We are racing with the deadline timer. So, do nothing because
  681. * the deadline timer handler will take care of properly recharging
  682. * the runtime and postponing the deadline
  683. */
  684. if (dl_se->dl_throttled)
  685. return;
  686. /*
  687. * We use the regular wall clock time to set deadlines in the
  688. * future; in fact, we must consider execution overheads (time
  689. * spent on hardirq context, etc.).
  690. */
  691. replenish_dl_new_period(dl_se, rq);
  692. }
  693. static int start_dl_timer(struct sched_dl_entity *dl_se);
  694. static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t);
  695. /*
  696. * Pure Earliest Deadline First (EDF) scheduling does not deal with the
  697. * possibility of a entity lasting more than what it declared, and thus
  698. * exhausting its runtime.
  699. *
  700. * Here we are interested in making runtime overrun possible, but we do
  701. * not want a entity which is misbehaving to affect the scheduling of all
  702. * other entities.
  703. * Therefore, a budgeting strategy called Constant Bandwidth Server (CBS)
  704. * is used, in order to confine each entity within its own bandwidth.
  705. *
  706. * This function deals exactly with that, and ensures that when the runtime
  707. * of a entity is replenished, its deadline is also postponed. That ensures
  708. * the overrunning entity can't interfere with other entity in the system and
  709. * can't make them miss their deadlines. Reasons why this kind of overruns
  710. * could happen are, typically, a entity voluntarily trying to overcome its
  711. * runtime, or it just underestimated it during sched_setattr().
  712. */
  713. static void replenish_dl_entity(struct sched_dl_entity *dl_se)
  714. {
  715. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  716. struct rq *rq = rq_of_dl_rq(dl_rq);
  717. WARN_ON_ONCE(pi_of(dl_se)->dl_runtime <= 0);
  718. /*
  719. * This could be the case for a !-dl task that is boosted.
  720. * Just go with full inherited parameters.
  721. *
  722. * Or, it could be the case of a deferred reservation that
  723. * was not able to consume its runtime in background and
  724. * reached this point with current u > U.
  725. *
  726. * In both cases, set a new period.
  727. */
  728. if (dl_se->dl_deadline == 0 ||
  729. (dl_se->dl_defer_armed && dl_entity_overflow(dl_se, rq_clock(rq)))) {
  730. dl_se->deadline = rq_clock(rq) + pi_of(dl_se)->dl_deadline;
  731. dl_se->runtime = pi_of(dl_se)->dl_runtime;
  732. }
  733. if (dl_se->dl_yielded && dl_se->runtime > 0)
  734. dl_se->runtime = 0;
  735. /*
  736. * We keep moving the deadline away until we get some
  737. * available runtime for the entity. This ensures correct
  738. * handling of situations where the runtime overrun is
  739. * arbitrary large.
  740. */
  741. while (dl_se->runtime <= 0) {
  742. dl_se->deadline += pi_of(dl_se)->dl_period;
  743. dl_se->runtime += pi_of(dl_se)->dl_runtime;
  744. }
  745. /*
  746. * At this point, the deadline really should be "in
  747. * the future" with respect to rq->clock. If it's
  748. * not, we are, for some reason, lagging too much!
  749. * Anyway, after having warn userspace abut that,
  750. * we still try to keep the things running by
  751. * resetting the deadline and the budget of the
  752. * entity.
  753. */
  754. if (dl_time_before(dl_se->deadline, rq_clock(rq))) {
  755. printk_deferred_once("sched: DL replenish lagged too much\n");
  756. replenish_dl_new_period(dl_se, rq);
  757. }
  758. if (dl_se->dl_yielded)
  759. dl_se->dl_yielded = 0;
  760. if (dl_se->dl_throttled)
  761. dl_se->dl_throttled = 0;
  762. /*
  763. * If this is the replenishment of a deferred reservation,
  764. * clear the flag and return.
  765. */
  766. if (dl_se->dl_defer_armed) {
  767. dl_se->dl_defer_armed = 0;
  768. return;
  769. }
  770. /*
  771. * A this point, if the deferred server is not armed, and the deadline
  772. * is in the future, if it is not running already, throttle the server
  773. * and arm the defer timer.
  774. */
  775. if (dl_se->dl_defer && !dl_se->dl_defer_running &&
  776. dl_time_before(rq_clock(dl_se->rq), dl_se->deadline - dl_se->runtime)) {
  777. if (!is_dl_boosted(dl_se) && dl_se->server_has_tasks(dl_se)) {
  778. /*
  779. * Set dl_se->dl_defer_armed and dl_throttled variables to
  780. * inform the start_dl_timer() that this is a deferred
  781. * activation.
  782. */
  783. dl_se->dl_defer_armed = 1;
  784. dl_se->dl_throttled = 1;
  785. if (!start_dl_timer(dl_se)) {
  786. /*
  787. * If for whatever reason (delays), a previous timer was
  788. * queued but not serviced, cancel it and clean the
  789. * deferrable server variables intended for start_dl_timer().
  790. */
  791. hrtimer_try_to_cancel(&dl_se->dl_timer);
  792. dl_se->dl_defer_armed = 0;
  793. dl_se->dl_throttled = 0;
  794. }
  795. }
  796. }
  797. }
  798. /*
  799. * Here we check if --at time t-- an entity (which is probably being
  800. * [re]activated or, in general, enqueued) can use its remaining runtime
  801. * and its current deadline _without_ exceeding the bandwidth it is
  802. * assigned (function returns true if it can't). We are in fact applying
  803. * one of the CBS rules: when a task wakes up, if the residual runtime
  804. * over residual deadline fits within the allocated bandwidth, then we
  805. * can keep the current (absolute) deadline and residual budget without
  806. * disrupting the schedulability of the system. Otherwise, we should
  807. * refill the runtime and set the deadline a period in the future,
  808. * because keeping the current (absolute) deadline of the task would
  809. * result in breaking guarantees promised to other tasks (refer to
  810. * Documentation/scheduler/sched-deadline.rst for more information).
  811. *
  812. * This function returns true if:
  813. *
  814. * runtime / (deadline - t) > dl_runtime / dl_deadline ,
  815. *
  816. * IOW we can't recycle current parameters.
  817. *
  818. * Notice that the bandwidth check is done against the deadline. For
  819. * task with deadline equal to period this is the same of using
  820. * dl_period instead of dl_deadline in the equation above.
  821. */
  822. static bool dl_entity_overflow(struct sched_dl_entity *dl_se, u64 t)
  823. {
  824. u64 left, right;
  825. /*
  826. * left and right are the two sides of the equation above,
  827. * after a bit of shuffling to use multiplications instead
  828. * of divisions.
  829. *
  830. * Note that none of the time values involved in the two
  831. * multiplications are absolute: dl_deadline and dl_runtime
  832. * are the relative deadline and the maximum runtime of each
  833. * instance, runtime is the runtime left for the last instance
  834. * and (deadline - t), since t is rq->clock, is the time left
  835. * to the (absolute) deadline. Even if overflowing the u64 type
  836. * is very unlikely to occur in both cases, here we scale down
  837. * as we want to avoid that risk at all. Scaling down by 10
  838. * means that we reduce granularity to 1us. We are fine with it,
  839. * since this is only a true/false check and, anyway, thinking
  840. * of anything below microseconds resolution is actually fiction
  841. * (but still we want to give the user that illusion >;).
  842. */
  843. left = (pi_of(dl_se)->dl_deadline >> DL_SCALE) * (dl_se->runtime >> DL_SCALE);
  844. right = ((dl_se->deadline - t) >> DL_SCALE) *
  845. (pi_of(dl_se)->dl_runtime >> DL_SCALE);
  846. return dl_time_before(right, left);
  847. }
  848. /*
  849. * Revised wakeup rule [1]: For self-suspending tasks, rather then
  850. * re-initializing task's runtime and deadline, the revised wakeup
  851. * rule adjusts the task's runtime to avoid the task to overrun its
  852. * density.
  853. *
  854. * Reasoning: a task may overrun the density if:
  855. * runtime / (deadline - t) > dl_runtime / dl_deadline
  856. *
  857. * Therefore, runtime can be adjusted to:
  858. * runtime = (dl_runtime / dl_deadline) * (deadline - t)
  859. *
  860. * In such way that runtime will be equal to the maximum density
  861. * the task can use without breaking any rule.
  862. *
  863. * [1] Luca Abeni, Giuseppe Lipari, and Juri Lelli. 2015. Constant
  864. * bandwidth server revisited. SIGBED Rev. 11, 4 (January 2015), 19-24.
  865. */
  866. static void
  867. update_dl_revised_wakeup(struct sched_dl_entity *dl_se, struct rq *rq)
  868. {
  869. u64 laxity = dl_se->deadline - rq_clock(rq);
  870. /*
  871. * If the task has deadline < period, and the deadline is in the past,
  872. * it should already be throttled before this check.
  873. *
  874. * See update_dl_entity() comments for further details.
  875. */
  876. WARN_ON(dl_time_before(dl_se->deadline, rq_clock(rq)));
  877. dl_se->runtime = (dl_se->dl_density * laxity) >> BW_SHIFT;
  878. }
  879. /*
  880. * Regarding the deadline, a task with implicit deadline has a relative
  881. * deadline == relative period. A task with constrained deadline has a
  882. * relative deadline <= relative period.
  883. *
  884. * We support constrained deadline tasks. However, there are some restrictions
  885. * applied only for tasks which do not have an implicit deadline. See
  886. * update_dl_entity() to know more about such restrictions.
  887. *
  888. * The dl_is_implicit() returns true if the task has an implicit deadline.
  889. */
  890. static inline bool dl_is_implicit(struct sched_dl_entity *dl_se)
  891. {
  892. return dl_se->dl_deadline == dl_se->dl_period;
  893. }
  894. /*
  895. * When a deadline entity is placed in the runqueue, its runtime and deadline
  896. * might need to be updated. This is done by a CBS wake up rule. There are two
  897. * different rules: 1) the original CBS; and 2) the Revisited CBS.
  898. *
  899. * When the task is starting a new period, the Original CBS is used. In this
  900. * case, the runtime is replenished and a new absolute deadline is set.
  901. *
  902. * When a task is queued before the begin of the next period, using the
  903. * remaining runtime and deadline could make the entity to overflow, see
  904. * dl_entity_overflow() to find more about runtime overflow. When such case
  905. * is detected, the runtime and deadline need to be updated.
  906. *
  907. * If the task has an implicit deadline, i.e., deadline == period, the Original
  908. * CBS is applied. The runtime is replenished and a new absolute deadline is
  909. * set, as in the previous cases.
  910. *
  911. * However, the Original CBS does not work properly for tasks with
  912. * deadline < period, which are said to have a constrained deadline. By
  913. * applying the Original CBS, a constrained deadline task would be able to run
  914. * runtime/deadline in a period. With deadline < period, the task would
  915. * overrun the runtime/period allowed bandwidth, breaking the admission test.
  916. *
  917. * In order to prevent this misbehave, the Revisited CBS is used for
  918. * constrained deadline tasks when a runtime overflow is detected. In the
  919. * Revisited CBS, rather than replenishing & setting a new absolute deadline,
  920. * the remaining runtime of the task is reduced to avoid runtime overflow.
  921. * Please refer to the comments update_dl_revised_wakeup() function to find
  922. * more about the Revised CBS rule.
  923. */
  924. static void update_dl_entity(struct sched_dl_entity *dl_se)
  925. {
  926. struct rq *rq = rq_of_dl_se(dl_se);
  927. if (dl_time_before(dl_se->deadline, rq_clock(rq)) ||
  928. dl_entity_overflow(dl_se, rq_clock(rq))) {
  929. if (unlikely(!dl_is_implicit(dl_se) &&
  930. !dl_time_before(dl_se->deadline, rq_clock(rq)) &&
  931. !is_dl_boosted(dl_se))) {
  932. update_dl_revised_wakeup(dl_se, rq);
  933. return;
  934. }
  935. replenish_dl_new_period(dl_se, rq);
  936. } else if (dl_server(dl_se) && dl_se->dl_defer) {
  937. /*
  938. * The server can still use its previous deadline, so check if
  939. * it left the dl_defer_running state.
  940. */
  941. if (!dl_se->dl_defer_running) {
  942. dl_se->dl_defer_armed = 1;
  943. dl_se->dl_throttled = 1;
  944. }
  945. }
  946. }
  947. static inline u64 dl_next_period(struct sched_dl_entity *dl_se)
  948. {
  949. return dl_se->deadline - dl_se->dl_deadline + dl_se->dl_period;
  950. }
  951. /*
  952. * If the entity depleted all its runtime, and if we want it to sleep
  953. * while waiting for some new execution time to become available, we
  954. * set the bandwidth replenishment timer to the replenishment instant
  955. * and try to activate it.
  956. *
  957. * Notice that it is important for the caller to know if the timer
  958. * actually started or not (i.e., the replenishment instant is in
  959. * the future or in the past).
  960. */
  961. static int start_dl_timer(struct sched_dl_entity *dl_se)
  962. {
  963. struct hrtimer *timer = &dl_se->dl_timer;
  964. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  965. struct rq *rq = rq_of_dl_rq(dl_rq);
  966. ktime_t now, act;
  967. s64 delta;
  968. lockdep_assert_rq_held(rq);
  969. /*
  970. * We want the timer to fire at the deadline, but considering
  971. * that it is actually coming from rq->clock and not from
  972. * hrtimer's time base reading.
  973. *
  974. * The deferred reservation will have its timer set to
  975. * (deadline - runtime). At that point, the CBS rule will decide
  976. * if the current deadline can be used, or if a replenishment is
  977. * required to avoid add too much pressure on the system
  978. * (current u > U).
  979. */
  980. if (dl_se->dl_defer_armed) {
  981. WARN_ON_ONCE(!dl_se->dl_throttled);
  982. act = ns_to_ktime(dl_se->deadline - dl_se->runtime);
  983. } else {
  984. /* act = deadline - rel-deadline + period */
  985. act = ns_to_ktime(dl_next_period(dl_se));
  986. }
  987. now = hrtimer_cb_get_time(timer);
  988. delta = ktime_to_ns(now) - rq_clock(rq);
  989. act = ktime_add_ns(act, delta);
  990. /*
  991. * If the expiry time already passed, e.g., because the value
  992. * chosen as the deadline is too small, don't even try to
  993. * start the timer in the past!
  994. */
  995. if (ktime_us_delta(act, now) < 0)
  996. return 0;
  997. /*
  998. * !enqueued will guarantee another callback; even if one is already in
  999. * progress. This ensures a balanced {get,put}_task_struct().
  1000. *
  1001. * The race against __run_timer() clearing the enqueued state is
  1002. * harmless because we're holding task_rq()->lock, therefore the timer
  1003. * expiring after we've done the check will wait on its task_rq_lock()
  1004. * and observe our state.
  1005. */
  1006. if (!hrtimer_is_queued(timer)) {
  1007. if (!dl_server(dl_se))
  1008. get_task_struct(dl_task_of(dl_se));
  1009. hrtimer_start(timer, act, HRTIMER_MODE_ABS_HARD);
  1010. }
  1011. return 1;
  1012. }
  1013. static void __push_dl_task(struct rq *rq, struct rq_flags *rf)
  1014. {
  1015. #ifdef CONFIG_SMP
  1016. /*
  1017. * Queueing this task back might have overloaded rq, check if we need
  1018. * to kick someone away.
  1019. */
  1020. if (has_pushable_dl_tasks(rq)) {
  1021. /*
  1022. * Nothing relies on rq->lock after this, so its safe to drop
  1023. * rq->lock.
  1024. */
  1025. rq_unpin_lock(rq, rf);
  1026. push_dl_task(rq);
  1027. rq_repin_lock(rq, rf);
  1028. }
  1029. #endif
  1030. }
  1031. /* a defer timer will not be reset if the runtime consumed was < dl_server_min_res */
  1032. static const u64 dl_server_min_res = 1 * NSEC_PER_MSEC;
  1033. static enum hrtimer_restart dl_server_timer(struct hrtimer *timer, struct sched_dl_entity *dl_se)
  1034. {
  1035. struct rq *rq = rq_of_dl_se(dl_se);
  1036. u64 fw;
  1037. scoped_guard (rq_lock, rq) {
  1038. struct rq_flags *rf = &scope.rf;
  1039. if (!dl_se->dl_throttled || !dl_se->dl_runtime)
  1040. return HRTIMER_NORESTART;
  1041. sched_clock_tick();
  1042. update_rq_clock(rq);
  1043. if (!dl_se->dl_runtime)
  1044. return HRTIMER_NORESTART;
  1045. if (!dl_se->server_has_tasks(dl_se)) {
  1046. replenish_dl_entity(dl_se);
  1047. return HRTIMER_NORESTART;
  1048. }
  1049. if (dl_se->dl_defer_armed) {
  1050. /*
  1051. * First check if the server could consume runtime in background.
  1052. * If so, it is possible to push the defer timer for this amount
  1053. * of time. The dl_server_min_res serves as a limit to avoid
  1054. * forwarding the timer for a too small amount of time.
  1055. */
  1056. if (dl_time_before(rq_clock(dl_se->rq),
  1057. (dl_se->deadline - dl_se->runtime - dl_server_min_res))) {
  1058. /* reset the defer timer */
  1059. fw = dl_se->deadline - rq_clock(dl_se->rq) - dl_se->runtime;
  1060. hrtimer_forward_now(timer, ns_to_ktime(fw));
  1061. return HRTIMER_RESTART;
  1062. }
  1063. dl_se->dl_defer_running = 1;
  1064. }
  1065. enqueue_dl_entity(dl_se, ENQUEUE_REPLENISH);
  1066. if (!dl_task(dl_se->rq->curr) || dl_entity_preempt(dl_se, &dl_se->rq->curr->dl))
  1067. resched_curr(rq);
  1068. __push_dl_task(rq, rf);
  1069. }
  1070. return HRTIMER_NORESTART;
  1071. }
  1072. /*
  1073. * This is the bandwidth enforcement timer callback. If here, we know
  1074. * a task is not on its dl_rq, since the fact that the timer was running
  1075. * means the task is throttled and needs a runtime replenishment.
  1076. *
  1077. * However, what we actually do depends on the fact the task is active,
  1078. * (it is on its rq) or has been removed from there by a call to
  1079. * dequeue_task_dl(). In the former case we must issue the runtime
  1080. * replenishment and add the task back to the dl_rq; in the latter, we just
  1081. * do nothing but clearing dl_throttled, so that runtime and deadline
  1082. * updating (and the queueing back to dl_rq) will be done by the
  1083. * next call to enqueue_task_dl().
  1084. */
  1085. static enum hrtimer_restart dl_task_timer(struct hrtimer *timer)
  1086. {
  1087. struct sched_dl_entity *dl_se = container_of(timer,
  1088. struct sched_dl_entity,
  1089. dl_timer);
  1090. struct task_struct *p;
  1091. struct rq_flags rf;
  1092. struct rq *rq;
  1093. if (dl_server(dl_se))
  1094. return dl_server_timer(timer, dl_se);
  1095. p = dl_task_of(dl_se);
  1096. rq = task_rq_lock(p, &rf);
  1097. /*
  1098. * The task might have changed its scheduling policy to something
  1099. * different than SCHED_DEADLINE (through switched_from_dl()).
  1100. */
  1101. if (!dl_task(p))
  1102. goto unlock;
  1103. /*
  1104. * The task might have been boosted by someone else and might be in the
  1105. * boosting/deboosting path, its not throttled.
  1106. */
  1107. if (is_dl_boosted(dl_se))
  1108. goto unlock;
  1109. /*
  1110. * Spurious timer due to start_dl_timer() race; or we already received
  1111. * a replenishment from rt_mutex_setprio().
  1112. */
  1113. if (!dl_se->dl_throttled)
  1114. goto unlock;
  1115. sched_clock_tick();
  1116. update_rq_clock(rq);
  1117. /*
  1118. * If the throttle happened during sched-out; like:
  1119. *
  1120. * schedule()
  1121. * deactivate_task()
  1122. * dequeue_task_dl()
  1123. * update_curr_dl()
  1124. * start_dl_timer()
  1125. * __dequeue_task_dl()
  1126. * prev->on_rq = 0;
  1127. *
  1128. * We can be both throttled and !queued. Replenish the counter
  1129. * but do not enqueue -- wait for our wakeup to do that.
  1130. */
  1131. if (!task_on_rq_queued(p)) {
  1132. replenish_dl_entity(dl_se);
  1133. goto unlock;
  1134. }
  1135. #ifdef CONFIG_SMP
  1136. if (unlikely(!rq->online)) {
  1137. /*
  1138. * If the runqueue is no longer available, migrate the
  1139. * task elsewhere. This necessarily changes rq.
  1140. */
  1141. lockdep_unpin_lock(__rq_lockp(rq), rf.cookie);
  1142. rq = dl_task_offline_migration(rq, p);
  1143. rf.cookie = lockdep_pin_lock(__rq_lockp(rq));
  1144. update_rq_clock(rq);
  1145. /*
  1146. * Now that the task has been migrated to the new RQ and we
  1147. * have that locked, proceed as normal and enqueue the task
  1148. * there.
  1149. */
  1150. }
  1151. #endif
  1152. enqueue_task_dl(rq, p, ENQUEUE_REPLENISH);
  1153. if (dl_task(rq->curr))
  1154. wakeup_preempt_dl(rq, p, 0);
  1155. else
  1156. resched_curr(rq);
  1157. __push_dl_task(rq, &rf);
  1158. unlock:
  1159. task_rq_unlock(rq, p, &rf);
  1160. /*
  1161. * This can free the task_struct, including this hrtimer, do not touch
  1162. * anything related to that after this.
  1163. */
  1164. put_task_struct(p);
  1165. return HRTIMER_NORESTART;
  1166. }
  1167. static void init_dl_task_timer(struct sched_dl_entity *dl_se)
  1168. {
  1169. struct hrtimer *timer = &dl_se->dl_timer;
  1170. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
  1171. timer->function = dl_task_timer;
  1172. }
  1173. /*
  1174. * During the activation, CBS checks if it can reuse the current task's
  1175. * runtime and period. If the deadline of the task is in the past, CBS
  1176. * cannot use the runtime, and so it replenishes the task. This rule
  1177. * works fine for implicit deadline tasks (deadline == period), and the
  1178. * CBS was designed for implicit deadline tasks. However, a task with
  1179. * constrained deadline (deadline < period) might be awakened after the
  1180. * deadline, but before the next period. In this case, replenishing the
  1181. * task would allow it to run for runtime / deadline. As in this case
  1182. * deadline < period, CBS enables a task to run for more than the
  1183. * runtime / period. In a very loaded system, this can cause a domino
  1184. * effect, making other tasks miss their deadlines.
  1185. *
  1186. * To avoid this problem, in the activation of a constrained deadline
  1187. * task after the deadline but before the next period, throttle the
  1188. * task and set the replenishing timer to the begin of the next period,
  1189. * unless it is boosted.
  1190. */
  1191. static inline void dl_check_constrained_dl(struct sched_dl_entity *dl_se)
  1192. {
  1193. struct rq *rq = rq_of_dl_se(dl_se);
  1194. if (dl_time_before(dl_se->deadline, rq_clock(rq)) &&
  1195. dl_time_before(rq_clock(rq), dl_next_period(dl_se))) {
  1196. if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(dl_se)))
  1197. return;
  1198. dl_se->dl_throttled = 1;
  1199. if (dl_se->runtime > 0)
  1200. dl_se->runtime = 0;
  1201. }
  1202. }
  1203. static
  1204. int dl_runtime_exceeded(struct sched_dl_entity *dl_se)
  1205. {
  1206. return (dl_se->runtime <= 0);
  1207. }
  1208. /*
  1209. * This function implements the GRUB accounting rule. According to the
  1210. * GRUB reclaiming algorithm, the runtime is not decreased as "dq = -dt",
  1211. * but as "dq = -(max{u, (Umax - Uinact - Uextra)} / Umax) dt",
  1212. * where u is the utilization of the task, Umax is the maximum reclaimable
  1213. * utilization, Uinact is the (per-runqueue) inactive utilization, computed
  1214. * as the difference between the "total runqueue utilization" and the
  1215. * "runqueue active utilization", and Uextra is the (per runqueue) extra
  1216. * reclaimable utilization.
  1217. * Since rq->dl.running_bw and rq->dl.this_bw contain utilizations multiplied
  1218. * by 2^BW_SHIFT, the result has to be shifted right by BW_SHIFT.
  1219. * Since rq->dl.bw_ratio contains 1 / Umax multiplied by 2^RATIO_SHIFT, dl_bw
  1220. * is multiplied by rq->dl.bw_ratio and shifted right by RATIO_SHIFT.
  1221. * Since delta is a 64 bit variable, to have an overflow its value should be
  1222. * larger than 2^(64 - 20 - 8), which is more than 64 seconds. So, overflow is
  1223. * not an issue here.
  1224. */
  1225. static u64 grub_reclaim(u64 delta, struct rq *rq, struct sched_dl_entity *dl_se)
  1226. {
  1227. u64 u_act;
  1228. u64 u_inact = rq->dl.this_bw - rq->dl.running_bw; /* Utot - Uact */
  1229. /*
  1230. * Instead of computing max{u, (u_max - u_inact - u_extra)}, we
  1231. * compare u_inact + u_extra with u_max - u, because u_inact + u_extra
  1232. * can be larger than u_max. So, u_max - u_inact - u_extra would be
  1233. * negative leading to wrong results.
  1234. */
  1235. if (u_inact + rq->dl.extra_bw > rq->dl.max_bw - dl_se->dl_bw)
  1236. u_act = dl_se->dl_bw;
  1237. else
  1238. u_act = rq->dl.max_bw - u_inact - rq->dl.extra_bw;
  1239. u_act = (u_act * rq->dl.bw_ratio) >> RATIO_SHIFT;
  1240. return (delta * u_act) >> BW_SHIFT;
  1241. }
  1242. s64 dl_scaled_delta_exec(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec)
  1243. {
  1244. s64 scaled_delta_exec;
  1245. /*
  1246. * For tasks that participate in GRUB, we implement GRUB-PA: the
  1247. * spare reclaimed bandwidth is used to clock down frequency.
  1248. *
  1249. * For the others, we still need to scale reservation parameters
  1250. * according to current frequency and CPU maximum capacity.
  1251. */
  1252. if (unlikely(dl_se->flags & SCHED_FLAG_RECLAIM)) {
  1253. scaled_delta_exec = grub_reclaim(delta_exec, rq, dl_se);
  1254. } else {
  1255. int cpu = cpu_of(rq);
  1256. unsigned long scale_freq = arch_scale_freq_capacity(cpu);
  1257. unsigned long scale_cpu = arch_scale_cpu_capacity(cpu);
  1258. scaled_delta_exec = cap_scale(delta_exec, scale_freq);
  1259. scaled_delta_exec = cap_scale(scaled_delta_exec, scale_cpu);
  1260. }
  1261. return scaled_delta_exec;
  1262. }
  1263. static inline void
  1264. update_stats_dequeue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se,
  1265. int flags);
  1266. static void update_curr_dl_se(struct rq *rq, struct sched_dl_entity *dl_se, s64 delta_exec)
  1267. {
  1268. s64 scaled_delta_exec;
  1269. if (unlikely(delta_exec <= 0)) {
  1270. if (unlikely(dl_se->dl_yielded))
  1271. goto throttle;
  1272. return;
  1273. }
  1274. if (dl_server(dl_se) && dl_se->dl_throttled && !dl_se->dl_defer)
  1275. return;
  1276. if (dl_entity_is_special(dl_se))
  1277. return;
  1278. scaled_delta_exec = delta_exec;
  1279. if (!dl_server(dl_se))
  1280. scaled_delta_exec = dl_scaled_delta_exec(rq, dl_se, delta_exec);
  1281. dl_se->runtime -= scaled_delta_exec;
  1282. /*
  1283. * The fair server can consume its runtime while throttled (not queued/
  1284. * running as regular CFS).
  1285. *
  1286. * If the server consumes its entire runtime in this state. The server
  1287. * is not required for the current period. Thus, reset the server by
  1288. * starting a new period, pushing the activation.
  1289. */
  1290. if (dl_se->dl_defer && dl_se->dl_throttled && dl_runtime_exceeded(dl_se)) {
  1291. /*
  1292. * If the server was previously activated - the starving condition
  1293. * took place, it this point it went away because the fair scheduler
  1294. * was able to get runtime in background. So return to the initial
  1295. * state.
  1296. */
  1297. dl_se->dl_defer_running = 0;
  1298. hrtimer_try_to_cancel(&dl_se->dl_timer);
  1299. replenish_dl_new_period(dl_se, dl_se->rq);
  1300. /*
  1301. * Not being able to start the timer seems problematic. If it could not
  1302. * be started for whatever reason, we need to "unthrottle" the DL server
  1303. * and queue right away. Otherwise nothing might queue it. That's similar
  1304. * to what enqueue_dl_entity() does on start_dl_timer==0. For now, just warn.
  1305. */
  1306. WARN_ON_ONCE(!start_dl_timer(dl_se));
  1307. return;
  1308. }
  1309. throttle:
  1310. if (dl_runtime_exceeded(dl_se) || dl_se->dl_yielded) {
  1311. dl_se->dl_throttled = 1;
  1312. /* If requested, inform the user about runtime overruns. */
  1313. if (dl_runtime_exceeded(dl_se) &&
  1314. (dl_se->flags & SCHED_FLAG_DL_OVERRUN))
  1315. dl_se->dl_overrun = 1;
  1316. dequeue_dl_entity(dl_se, 0);
  1317. if (!dl_server(dl_se)) {
  1318. update_stats_dequeue_dl(&rq->dl, dl_se, 0);
  1319. dequeue_pushable_dl_task(rq, dl_task_of(dl_se));
  1320. }
  1321. if (unlikely(is_dl_boosted(dl_se) || !start_dl_timer(dl_se))) {
  1322. if (dl_server(dl_se))
  1323. enqueue_dl_entity(dl_se, ENQUEUE_REPLENISH);
  1324. else
  1325. enqueue_task_dl(rq, dl_task_of(dl_se), ENQUEUE_REPLENISH);
  1326. }
  1327. if (!is_leftmost(dl_se, &rq->dl))
  1328. resched_curr(rq);
  1329. }
  1330. /*
  1331. * The fair server (sole dl_server) does not account for real-time
  1332. * workload because it is running fair work.
  1333. */
  1334. if (dl_se == &rq->fair_server)
  1335. return;
  1336. #ifdef CONFIG_RT_GROUP_SCHED
  1337. /*
  1338. * Because -- for now -- we share the rt bandwidth, we need to
  1339. * account our runtime there too, otherwise actual rt tasks
  1340. * would be able to exceed the shared quota.
  1341. *
  1342. * Account to the root rt group for now.
  1343. *
  1344. * The solution we're working towards is having the RT groups scheduled
  1345. * using deadline servers -- however there's a few nasties to figure
  1346. * out before that can happen.
  1347. */
  1348. if (rt_bandwidth_enabled()) {
  1349. struct rt_rq *rt_rq = &rq->rt;
  1350. raw_spin_lock(&rt_rq->rt_runtime_lock);
  1351. /*
  1352. * We'll let actual RT tasks worry about the overflow here, we
  1353. * have our own CBS to keep us inline; only account when RT
  1354. * bandwidth is relevant.
  1355. */
  1356. if (sched_rt_bandwidth_account(rt_rq))
  1357. rt_rq->rt_time += delta_exec;
  1358. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  1359. }
  1360. #endif
  1361. }
  1362. /*
  1363. * In the non-defer mode, the idle time is not accounted, as the
  1364. * server provides a guarantee.
  1365. *
  1366. * If the dl_server is in defer mode, the idle time is also considered
  1367. * as time available for the fair server, avoiding a penalty for the
  1368. * rt scheduler that did not consumed that time.
  1369. */
  1370. void dl_server_update_idle_time(struct rq *rq, struct task_struct *p)
  1371. {
  1372. s64 delta_exec;
  1373. if (!rq->fair_server.dl_defer)
  1374. return;
  1375. /* no need to discount more */
  1376. if (rq->fair_server.runtime < 0)
  1377. return;
  1378. delta_exec = rq_clock_task(rq) - p->se.exec_start;
  1379. if (delta_exec < 0)
  1380. return;
  1381. rq->fair_server.runtime -= delta_exec;
  1382. if (rq->fair_server.runtime < 0) {
  1383. rq->fair_server.dl_defer_running = 0;
  1384. rq->fair_server.runtime = 0;
  1385. }
  1386. p->se.exec_start = rq_clock_task(rq);
  1387. }
  1388. void dl_server_update(struct sched_dl_entity *dl_se, s64 delta_exec)
  1389. {
  1390. /* 0 runtime = fair server disabled */
  1391. if (dl_se->dl_runtime)
  1392. update_curr_dl_se(dl_se->rq, dl_se, delta_exec);
  1393. }
  1394. void dl_server_start(struct sched_dl_entity *dl_se)
  1395. {
  1396. struct rq *rq = dl_se->rq;
  1397. /*
  1398. * XXX: the apply do not work fine at the init phase for the
  1399. * fair server because things are not yet set. We need to improve
  1400. * this before getting generic.
  1401. */
  1402. if (!dl_server(dl_se)) {
  1403. u64 runtime = 50 * NSEC_PER_MSEC;
  1404. u64 period = 1000 * NSEC_PER_MSEC;
  1405. dl_server_apply_params(dl_se, runtime, period, 1);
  1406. dl_se->dl_server = 1;
  1407. dl_se->dl_defer = 1;
  1408. setup_new_dl_entity(dl_se);
  1409. }
  1410. if (!dl_se->dl_runtime)
  1411. return;
  1412. dl_se->dl_server_active = 1;
  1413. enqueue_dl_entity(dl_se, ENQUEUE_WAKEUP);
  1414. if (!dl_task(dl_se->rq->curr) || dl_entity_preempt(dl_se, &rq->curr->dl))
  1415. resched_curr(dl_se->rq);
  1416. }
  1417. void dl_server_stop(struct sched_dl_entity *dl_se)
  1418. {
  1419. if (!dl_se->dl_runtime)
  1420. return;
  1421. dequeue_dl_entity(dl_se, DEQUEUE_SLEEP);
  1422. hrtimer_try_to_cancel(&dl_se->dl_timer);
  1423. dl_se->dl_defer_armed = 0;
  1424. dl_se->dl_throttled = 0;
  1425. dl_se->dl_server_active = 0;
  1426. }
  1427. void dl_server_init(struct sched_dl_entity *dl_se, struct rq *rq,
  1428. dl_server_has_tasks_f has_tasks,
  1429. dl_server_pick_f pick_task)
  1430. {
  1431. dl_se->rq = rq;
  1432. dl_se->server_has_tasks = has_tasks;
  1433. dl_se->server_pick_task = pick_task;
  1434. }
  1435. void __dl_server_attach_root(struct sched_dl_entity *dl_se, struct rq *rq)
  1436. {
  1437. u64 new_bw = dl_se->dl_bw;
  1438. int cpu = cpu_of(rq);
  1439. struct dl_bw *dl_b;
  1440. dl_b = dl_bw_of(cpu_of(rq));
  1441. guard(raw_spinlock)(&dl_b->lock);
  1442. if (!dl_bw_cpus(cpu))
  1443. return;
  1444. __dl_add(dl_b, new_bw, dl_bw_cpus(cpu));
  1445. }
  1446. int dl_server_apply_params(struct sched_dl_entity *dl_se, u64 runtime, u64 period, bool init)
  1447. {
  1448. u64 old_bw = init ? 0 : to_ratio(dl_se->dl_period, dl_se->dl_runtime);
  1449. u64 new_bw = to_ratio(period, runtime);
  1450. struct rq *rq = dl_se->rq;
  1451. int cpu = cpu_of(rq);
  1452. struct dl_bw *dl_b;
  1453. unsigned long cap;
  1454. int retval = 0;
  1455. int cpus;
  1456. dl_b = dl_bw_of(cpu);
  1457. guard(raw_spinlock)(&dl_b->lock);
  1458. cpus = dl_bw_cpus(cpu);
  1459. cap = dl_bw_capacity(cpu);
  1460. if (__dl_overflow(dl_b, cap, old_bw, new_bw))
  1461. return -EBUSY;
  1462. if (init) {
  1463. __add_rq_bw(new_bw, &rq->dl);
  1464. __dl_add(dl_b, new_bw, cpus);
  1465. } else {
  1466. __dl_sub(dl_b, dl_se->dl_bw, cpus);
  1467. __dl_add(dl_b, new_bw, cpus);
  1468. dl_rq_change_utilization(rq, dl_se, new_bw);
  1469. }
  1470. dl_se->dl_runtime = runtime;
  1471. dl_se->dl_deadline = period;
  1472. dl_se->dl_period = period;
  1473. dl_se->runtime = 0;
  1474. dl_se->deadline = 0;
  1475. dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
  1476. dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
  1477. return retval;
  1478. }
  1479. /*
  1480. * Update the current task's runtime statistics (provided it is still
  1481. * a -deadline task and has not been removed from the dl_rq).
  1482. */
  1483. static void update_curr_dl(struct rq *rq)
  1484. {
  1485. struct task_struct *curr = rq->curr;
  1486. struct sched_dl_entity *dl_se = &curr->dl;
  1487. s64 delta_exec;
  1488. if (!dl_task(curr) || !on_dl_rq(dl_se))
  1489. return;
  1490. /*
  1491. * Consumed budget is computed considering the time as
  1492. * observed by schedulable tasks (excluding time spent
  1493. * in hardirq context, etc.). Deadlines are instead
  1494. * computed using hard walltime. This seems to be the more
  1495. * natural solution, but the full ramifications of this
  1496. * approach need further study.
  1497. */
  1498. delta_exec = update_curr_common(rq);
  1499. update_curr_dl_se(rq, dl_se, delta_exec);
  1500. }
  1501. static enum hrtimer_restart inactive_task_timer(struct hrtimer *timer)
  1502. {
  1503. struct sched_dl_entity *dl_se = container_of(timer,
  1504. struct sched_dl_entity,
  1505. inactive_timer);
  1506. struct task_struct *p = NULL;
  1507. struct rq_flags rf;
  1508. struct rq *rq;
  1509. if (!dl_server(dl_se)) {
  1510. p = dl_task_of(dl_se);
  1511. rq = task_rq_lock(p, &rf);
  1512. } else {
  1513. rq = dl_se->rq;
  1514. rq_lock(rq, &rf);
  1515. }
  1516. sched_clock_tick();
  1517. update_rq_clock(rq);
  1518. if (dl_server(dl_se))
  1519. goto no_task;
  1520. if (!dl_task(p) || READ_ONCE(p->__state) == TASK_DEAD) {
  1521. struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
  1522. if (READ_ONCE(p->__state) == TASK_DEAD && dl_se->dl_non_contending) {
  1523. sub_running_bw(&p->dl, dl_rq_of_se(&p->dl));
  1524. sub_rq_bw(&p->dl, dl_rq_of_se(&p->dl));
  1525. dl_se->dl_non_contending = 0;
  1526. }
  1527. raw_spin_lock(&dl_b->lock);
  1528. __dl_sub(dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
  1529. raw_spin_unlock(&dl_b->lock);
  1530. __dl_clear_params(dl_se);
  1531. goto unlock;
  1532. }
  1533. no_task:
  1534. if (dl_se->dl_non_contending == 0)
  1535. goto unlock;
  1536. sub_running_bw(dl_se, &rq->dl);
  1537. dl_se->dl_non_contending = 0;
  1538. unlock:
  1539. if (!dl_server(dl_se)) {
  1540. task_rq_unlock(rq, p, &rf);
  1541. put_task_struct(p);
  1542. } else {
  1543. rq_unlock(rq, &rf);
  1544. }
  1545. return HRTIMER_NORESTART;
  1546. }
  1547. static void init_dl_inactive_task_timer(struct sched_dl_entity *dl_se)
  1548. {
  1549. struct hrtimer *timer = &dl_se->inactive_timer;
  1550. hrtimer_init(timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_HARD);
  1551. timer->function = inactive_task_timer;
  1552. }
  1553. #define __node_2_dle(node) \
  1554. rb_entry((node), struct sched_dl_entity, rb_node)
  1555. #ifdef CONFIG_SMP
  1556. static void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  1557. {
  1558. struct rq *rq = rq_of_dl_rq(dl_rq);
  1559. if (dl_rq->earliest_dl.curr == 0 ||
  1560. dl_time_before(deadline, dl_rq->earliest_dl.curr)) {
  1561. if (dl_rq->earliest_dl.curr == 0)
  1562. cpupri_set(&rq->rd->cpupri, rq->cpu, CPUPRI_HIGHER);
  1563. dl_rq->earliest_dl.curr = deadline;
  1564. cpudl_set(&rq->rd->cpudl, rq->cpu, deadline);
  1565. }
  1566. }
  1567. static void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline)
  1568. {
  1569. struct rq *rq = rq_of_dl_rq(dl_rq);
  1570. /*
  1571. * Since we may have removed our earliest (and/or next earliest)
  1572. * task we must recompute them.
  1573. */
  1574. if (!dl_rq->dl_nr_running) {
  1575. dl_rq->earliest_dl.curr = 0;
  1576. dl_rq->earliest_dl.next = 0;
  1577. cpudl_clear(&rq->rd->cpudl, rq->cpu);
  1578. cpupri_set(&rq->rd->cpupri, rq->cpu, rq->rt.highest_prio.curr);
  1579. } else {
  1580. struct rb_node *leftmost = rb_first_cached(&dl_rq->root);
  1581. struct sched_dl_entity *entry = __node_2_dle(leftmost);
  1582. dl_rq->earliest_dl.curr = entry->deadline;
  1583. cpudl_set(&rq->rd->cpudl, rq->cpu, entry->deadline);
  1584. }
  1585. }
  1586. #else
  1587. static inline void inc_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  1588. static inline void dec_dl_deadline(struct dl_rq *dl_rq, u64 deadline) {}
  1589. #endif /* CONFIG_SMP */
  1590. static inline
  1591. void inc_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  1592. {
  1593. u64 deadline = dl_se->deadline;
  1594. dl_rq->dl_nr_running++;
  1595. add_nr_running(rq_of_dl_rq(dl_rq), 1);
  1596. inc_dl_deadline(dl_rq, deadline);
  1597. }
  1598. static inline
  1599. void dec_dl_tasks(struct sched_dl_entity *dl_se, struct dl_rq *dl_rq)
  1600. {
  1601. WARN_ON(!dl_rq->dl_nr_running);
  1602. dl_rq->dl_nr_running--;
  1603. sub_nr_running(rq_of_dl_rq(dl_rq), 1);
  1604. dec_dl_deadline(dl_rq, dl_se->deadline);
  1605. }
  1606. static inline bool __dl_less(struct rb_node *a, const struct rb_node *b)
  1607. {
  1608. return dl_time_before(__node_2_dle(a)->deadline, __node_2_dle(b)->deadline);
  1609. }
  1610. static __always_inline struct sched_statistics *
  1611. __schedstats_from_dl_se(struct sched_dl_entity *dl_se)
  1612. {
  1613. if (!schedstat_enabled())
  1614. return NULL;
  1615. if (dl_server(dl_se))
  1616. return NULL;
  1617. return &dl_task_of(dl_se)->stats;
  1618. }
  1619. static inline void
  1620. update_stats_wait_start_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se)
  1621. {
  1622. struct sched_statistics *stats = __schedstats_from_dl_se(dl_se);
  1623. if (stats)
  1624. __update_stats_wait_start(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats);
  1625. }
  1626. static inline void
  1627. update_stats_wait_end_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se)
  1628. {
  1629. struct sched_statistics *stats = __schedstats_from_dl_se(dl_se);
  1630. if (stats)
  1631. __update_stats_wait_end(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats);
  1632. }
  1633. static inline void
  1634. update_stats_enqueue_sleeper_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se)
  1635. {
  1636. struct sched_statistics *stats = __schedstats_from_dl_se(dl_se);
  1637. if (stats)
  1638. __update_stats_enqueue_sleeper(rq_of_dl_rq(dl_rq), dl_task_of(dl_se), stats);
  1639. }
  1640. static inline void
  1641. update_stats_enqueue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se,
  1642. int flags)
  1643. {
  1644. if (!schedstat_enabled())
  1645. return;
  1646. if (flags & ENQUEUE_WAKEUP)
  1647. update_stats_enqueue_sleeper_dl(dl_rq, dl_se);
  1648. }
  1649. static inline void
  1650. update_stats_dequeue_dl(struct dl_rq *dl_rq, struct sched_dl_entity *dl_se,
  1651. int flags)
  1652. {
  1653. struct task_struct *p = dl_task_of(dl_se);
  1654. if (!schedstat_enabled())
  1655. return;
  1656. if ((flags & DEQUEUE_SLEEP)) {
  1657. unsigned int state;
  1658. state = READ_ONCE(p->__state);
  1659. if (state & TASK_INTERRUPTIBLE)
  1660. __schedstat_set(p->stats.sleep_start,
  1661. rq_clock(rq_of_dl_rq(dl_rq)));
  1662. if (state & TASK_UNINTERRUPTIBLE)
  1663. __schedstat_set(p->stats.block_start,
  1664. rq_clock(rq_of_dl_rq(dl_rq)));
  1665. }
  1666. }
  1667. static void __enqueue_dl_entity(struct sched_dl_entity *dl_se)
  1668. {
  1669. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  1670. WARN_ON_ONCE(!RB_EMPTY_NODE(&dl_se->rb_node));
  1671. rb_add_cached(&dl_se->rb_node, &dl_rq->root, __dl_less);
  1672. inc_dl_tasks(dl_se, dl_rq);
  1673. }
  1674. static void __dequeue_dl_entity(struct sched_dl_entity *dl_se)
  1675. {
  1676. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  1677. if (RB_EMPTY_NODE(&dl_se->rb_node))
  1678. return;
  1679. rb_erase_cached(&dl_se->rb_node, &dl_rq->root);
  1680. RB_CLEAR_NODE(&dl_se->rb_node);
  1681. dec_dl_tasks(dl_se, dl_rq);
  1682. }
  1683. static void
  1684. enqueue_dl_entity(struct sched_dl_entity *dl_se, int flags)
  1685. {
  1686. WARN_ON_ONCE(on_dl_rq(dl_se));
  1687. update_stats_enqueue_dl(dl_rq_of_se(dl_se), dl_se, flags);
  1688. /*
  1689. * Check if a constrained deadline task was activated
  1690. * after the deadline but before the next period.
  1691. * If that is the case, the task will be throttled and
  1692. * the replenishment timer will be set to the next period.
  1693. */
  1694. if (!dl_se->dl_throttled && !dl_is_implicit(dl_se))
  1695. dl_check_constrained_dl(dl_se);
  1696. if (flags & (ENQUEUE_RESTORE|ENQUEUE_MIGRATING)) {
  1697. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  1698. add_rq_bw(dl_se, dl_rq);
  1699. add_running_bw(dl_se, dl_rq);
  1700. }
  1701. /*
  1702. * If p is throttled, we do not enqueue it. In fact, if it exhausted
  1703. * its budget it needs a replenishment and, since it now is on
  1704. * its rq, the bandwidth timer callback (which clearly has not
  1705. * run yet) will take care of this.
  1706. * However, the active utilization does not depend on the fact
  1707. * that the task is on the runqueue or not (but depends on the
  1708. * task's state - in GRUB parlance, "inactive" vs "active contending").
  1709. * In other words, even if a task is throttled its utilization must
  1710. * be counted in the active utilization; hence, we need to call
  1711. * add_running_bw().
  1712. */
  1713. if (!dl_se->dl_defer && dl_se->dl_throttled && !(flags & ENQUEUE_REPLENISH)) {
  1714. if (flags & ENQUEUE_WAKEUP)
  1715. task_contending(dl_se, flags);
  1716. return;
  1717. }
  1718. /*
  1719. * If this is a wakeup or a new instance, the scheduling
  1720. * parameters of the task might need updating. Otherwise,
  1721. * we want a replenishment of its runtime.
  1722. */
  1723. if (flags & ENQUEUE_WAKEUP) {
  1724. task_contending(dl_se, flags);
  1725. update_dl_entity(dl_se);
  1726. } else if (flags & ENQUEUE_REPLENISH) {
  1727. replenish_dl_entity(dl_se);
  1728. } else if ((flags & ENQUEUE_RESTORE) &&
  1729. !is_dl_boosted(dl_se) &&
  1730. dl_time_before(dl_se->deadline, rq_clock(rq_of_dl_se(dl_se)))) {
  1731. setup_new_dl_entity(dl_se);
  1732. }
  1733. /*
  1734. * If the reservation is still throttled, e.g., it got replenished but is a
  1735. * deferred task and still got to wait, don't enqueue.
  1736. */
  1737. if (dl_se->dl_throttled && start_dl_timer(dl_se))
  1738. return;
  1739. /*
  1740. * We're about to enqueue, make sure we're not ->dl_throttled!
  1741. * In case the timer was not started, say because the defer time
  1742. * has passed, mark as not throttled and mark unarmed.
  1743. * Also cancel earlier timers, since letting those run is pointless.
  1744. */
  1745. if (dl_se->dl_throttled) {
  1746. hrtimer_try_to_cancel(&dl_se->dl_timer);
  1747. dl_se->dl_defer_armed = 0;
  1748. dl_se->dl_throttled = 0;
  1749. }
  1750. __enqueue_dl_entity(dl_se);
  1751. }
  1752. static void dequeue_dl_entity(struct sched_dl_entity *dl_se, int flags)
  1753. {
  1754. __dequeue_dl_entity(dl_se);
  1755. if (flags & (DEQUEUE_SAVE|DEQUEUE_MIGRATING)) {
  1756. struct dl_rq *dl_rq = dl_rq_of_se(dl_se);
  1757. sub_running_bw(dl_se, dl_rq);
  1758. sub_rq_bw(dl_se, dl_rq);
  1759. }
  1760. /*
  1761. * This check allows to start the inactive timer (or to immediately
  1762. * decrease the active utilization, if needed) in two cases:
  1763. * when the task blocks and when it is terminating
  1764. * (p->state == TASK_DEAD). We can handle the two cases in the same
  1765. * way, because from GRUB's point of view the same thing is happening
  1766. * (the task moves from "active contending" to "active non contending"
  1767. * or "inactive")
  1768. */
  1769. if (flags & DEQUEUE_SLEEP)
  1770. task_non_contending(dl_se);
  1771. }
  1772. static void enqueue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  1773. {
  1774. if (is_dl_boosted(&p->dl)) {
  1775. /*
  1776. * Because of delays in the detection of the overrun of a
  1777. * thread's runtime, it might be the case that a thread
  1778. * goes to sleep in a rt mutex with negative runtime. As
  1779. * a consequence, the thread will be throttled.
  1780. *
  1781. * While waiting for the mutex, this thread can also be
  1782. * boosted via PI, resulting in a thread that is throttled
  1783. * and boosted at the same time.
  1784. *
  1785. * In this case, the boost overrides the throttle.
  1786. */
  1787. if (p->dl.dl_throttled) {
  1788. /*
  1789. * The replenish timer needs to be canceled. No
  1790. * problem if it fires concurrently: boosted threads
  1791. * are ignored in dl_task_timer().
  1792. *
  1793. * If the timer callback was running (hrtimer_try_to_cancel == -1),
  1794. * it will eventually call put_task_struct().
  1795. */
  1796. if (hrtimer_try_to_cancel(&p->dl.dl_timer) == 1 &&
  1797. !dl_server(&p->dl))
  1798. put_task_struct(p);
  1799. p->dl.dl_throttled = 0;
  1800. }
  1801. } else if (!dl_prio(p->normal_prio)) {
  1802. /*
  1803. * Special case in which we have a !SCHED_DEADLINE task that is going
  1804. * to be deboosted, but exceeds its runtime while doing so. No point in
  1805. * replenishing it, as it's going to return back to its original
  1806. * scheduling class after this. If it has been throttled, we need to
  1807. * clear the flag, otherwise the task may wake up as throttled after
  1808. * being boosted again with no means to replenish the runtime and clear
  1809. * the throttle.
  1810. */
  1811. p->dl.dl_throttled = 0;
  1812. if (!(flags & ENQUEUE_REPLENISH))
  1813. printk_deferred_once("sched: DL de-boosted task PID %d: REPLENISH flag missing\n",
  1814. task_pid_nr(p));
  1815. return;
  1816. }
  1817. check_schedstat_required();
  1818. update_stats_wait_start_dl(dl_rq_of_se(&p->dl), &p->dl);
  1819. if (p->on_rq == TASK_ON_RQ_MIGRATING)
  1820. flags |= ENQUEUE_MIGRATING;
  1821. enqueue_dl_entity(&p->dl, flags);
  1822. if (dl_server(&p->dl))
  1823. return;
  1824. if (!task_current(rq, p) && !p->dl.dl_throttled && p->nr_cpus_allowed > 1)
  1825. enqueue_pushable_dl_task(rq, p);
  1826. }
  1827. static bool dequeue_task_dl(struct rq *rq, struct task_struct *p, int flags)
  1828. {
  1829. update_curr_dl(rq);
  1830. if (p->on_rq == TASK_ON_RQ_MIGRATING)
  1831. flags |= DEQUEUE_MIGRATING;
  1832. dequeue_dl_entity(&p->dl, flags);
  1833. if (!p->dl.dl_throttled && !dl_server(&p->dl))
  1834. dequeue_pushable_dl_task(rq, p);
  1835. return true;
  1836. }
  1837. /*
  1838. * Yield task semantic for -deadline tasks is:
  1839. *
  1840. * get off from the CPU until our next instance, with
  1841. * a new runtime. This is of little use now, since we
  1842. * don't have a bandwidth reclaiming mechanism. Anyway,
  1843. * bandwidth reclaiming is planned for the future, and
  1844. * yield_task_dl will indicate that some spare budget
  1845. * is available for other task instances to use it.
  1846. */
  1847. static void yield_task_dl(struct rq *rq)
  1848. {
  1849. /*
  1850. * We make the task go to sleep until its current deadline by
  1851. * forcing its runtime to zero. This way, update_curr_dl() stops
  1852. * it and the bandwidth timer will wake it up and will give it
  1853. * new scheduling parameters (thanks to dl_yielded=1).
  1854. */
  1855. rq->curr->dl.dl_yielded = 1;
  1856. update_rq_clock(rq);
  1857. update_curr_dl(rq);
  1858. /*
  1859. * Tell update_rq_clock() that we've just updated,
  1860. * so we don't do microscopic update in schedule()
  1861. * and double the fastpath cost.
  1862. */
  1863. rq_clock_skip_update(rq);
  1864. }
  1865. #ifdef CONFIG_SMP
  1866. static inline bool dl_task_is_earliest_deadline(struct task_struct *p,
  1867. struct rq *rq)
  1868. {
  1869. return (!rq->dl.dl_nr_running ||
  1870. dl_time_before(p->dl.deadline,
  1871. rq->dl.earliest_dl.curr));
  1872. }
  1873. static int find_later_rq(struct task_struct *task);
  1874. static int
  1875. select_task_rq_dl(struct task_struct *p, int cpu, int flags)
  1876. {
  1877. struct task_struct *curr;
  1878. bool select_rq;
  1879. struct rq *rq;
  1880. if (!(flags & WF_TTWU))
  1881. goto out;
  1882. rq = cpu_rq(cpu);
  1883. rcu_read_lock();
  1884. curr = READ_ONCE(rq->curr); /* unlocked access */
  1885. /*
  1886. * If we are dealing with a -deadline task, we must
  1887. * decide where to wake it up.
  1888. * If it has a later deadline and the current task
  1889. * on this rq can't move (provided the waking task
  1890. * can!) we prefer to send it somewhere else. On the
  1891. * other hand, if it has a shorter deadline, we
  1892. * try to make it stay here, it might be important.
  1893. */
  1894. select_rq = unlikely(dl_task(curr)) &&
  1895. (curr->nr_cpus_allowed < 2 ||
  1896. !dl_entity_preempt(&p->dl, &curr->dl)) &&
  1897. p->nr_cpus_allowed > 1;
  1898. /*
  1899. * Take the capacity of the CPU into account to
  1900. * ensure it fits the requirement of the task.
  1901. */
  1902. if (sched_asym_cpucap_active())
  1903. select_rq |= !dl_task_fits_capacity(p, cpu);
  1904. if (select_rq) {
  1905. int target = find_later_rq(p);
  1906. if (target != -1 &&
  1907. dl_task_is_earliest_deadline(p, cpu_rq(target)))
  1908. cpu = target;
  1909. }
  1910. rcu_read_unlock();
  1911. out:
  1912. return cpu;
  1913. }
  1914. static void migrate_task_rq_dl(struct task_struct *p, int new_cpu __maybe_unused)
  1915. {
  1916. struct rq_flags rf;
  1917. struct rq *rq;
  1918. if (READ_ONCE(p->__state) != TASK_WAKING)
  1919. return;
  1920. rq = task_rq(p);
  1921. /*
  1922. * Since p->state == TASK_WAKING, set_task_cpu() has been called
  1923. * from try_to_wake_up(). Hence, p->pi_lock is locked, but
  1924. * rq->lock is not... So, lock it
  1925. */
  1926. rq_lock(rq, &rf);
  1927. if (p->dl.dl_non_contending) {
  1928. update_rq_clock(rq);
  1929. sub_running_bw(&p->dl, &rq->dl);
  1930. p->dl.dl_non_contending = 0;
  1931. /*
  1932. * If the timer handler is currently running and the
  1933. * timer cannot be canceled, inactive_task_timer()
  1934. * will see that dl_not_contending is not set, and
  1935. * will not touch the rq's active utilization,
  1936. * so we are still safe.
  1937. */
  1938. if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
  1939. put_task_struct(p);
  1940. }
  1941. sub_rq_bw(&p->dl, &rq->dl);
  1942. rq_unlock(rq, &rf);
  1943. }
  1944. static void check_preempt_equal_dl(struct rq *rq, struct task_struct *p)
  1945. {
  1946. /*
  1947. * Current can't be migrated, useless to reschedule,
  1948. * let's hope p can move out.
  1949. */
  1950. if (rq->curr->nr_cpus_allowed == 1 ||
  1951. !cpudl_find(&rq->rd->cpudl, rq->curr, NULL))
  1952. return;
  1953. /*
  1954. * p is migratable, so let's not schedule it and
  1955. * see if it is pushed or pulled somewhere else.
  1956. */
  1957. if (p->nr_cpus_allowed != 1 &&
  1958. cpudl_find(&rq->rd->cpudl, p, NULL))
  1959. return;
  1960. resched_curr(rq);
  1961. }
  1962. static int balance_dl(struct rq *rq, struct task_struct *p, struct rq_flags *rf)
  1963. {
  1964. if (!on_dl_rq(&p->dl) && need_pull_dl_task(rq, p)) {
  1965. /*
  1966. * This is OK, because current is on_cpu, which avoids it being
  1967. * picked for load-balance and preemption/IRQs are still
  1968. * disabled avoiding further scheduler activity on it and we've
  1969. * not yet started the picking loop.
  1970. */
  1971. rq_unpin_lock(rq, rf);
  1972. pull_dl_task(rq);
  1973. rq_repin_lock(rq, rf);
  1974. }
  1975. return sched_stop_runnable(rq) || sched_dl_runnable(rq);
  1976. }
  1977. #endif /* CONFIG_SMP */
  1978. /*
  1979. * Only called when both the current and waking task are -deadline
  1980. * tasks.
  1981. */
  1982. static void wakeup_preempt_dl(struct rq *rq, struct task_struct *p,
  1983. int flags)
  1984. {
  1985. if (dl_entity_preempt(&p->dl, &rq->curr->dl)) {
  1986. resched_curr(rq);
  1987. return;
  1988. }
  1989. #ifdef CONFIG_SMP
  1990. /*
  1991. * In the unlikely case current and p have the same deadline
  1992. * let us try to decide what's the best thing to do...
  1993. */
  1994. if ((p->dl.deadline == rq->curr->dl.deadline) &&
  1995. !test_tsk_need_resched(rq->curr))
  1996. check_preempt_equal_dl(rq, p);
  1997. #endif /* CONFIG_SMP */
  1998. }
  1999. #ifdef CONFIG_SCHED_HRTICK
  2000. static void start_hrtick_dl(struct rq *rq, struct sched_dl_entity *dl_se)
  2001. {
  2002. hrtick_start(rq, dl_se->runtime);
  2003. }
  2004. #else /* !CONFIG_SCHED_HRTICK */
  2005. static void start_hrtick_dl(struct rq *rq, struct sched_dl_entity *dl_se)
  2006. {
  2007. }
  2008. #endif
  2009. static void set_next_task_dl(struct rq *rq, struct task_struct *p, bool first)
  2010. {
  2011. struct sched_dl_entity *dl_se = &p->dl;
  2012. struct dl_rq *dl_rq = &rq->dl;
  2013. p->se.exec_start = rq_clock_task(rq);
  2014. if (on_dl_rq(&p->dl))
  2015. update_stats_wait_end_dl(dl_rq, dl_se);
  2016. /* You can't push away the running task */
  2017. dequeue_pushable_dl_task(rq, p);
  2018. if (!first)
  2019. return;
  2020. if (rq->curr->sched_class != &dl_sched_class)
  2021. update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
  2022. deadline_queue_push_tasks(rq);
  2023. if (hrtick_enabled_dl(rq))
  2024. start_hrtick_dl(rq, &p->dl);
  2025. }
  2026. static struct sched_dl_entity *pick_next_dl_entity(struct dl_rq *dl_rq)
  2027. {
  2028. struct rb_node *left = rb_first_cached(&dl_rq->root);
  2029. if (!left)
  2030. return NULL;
  2031. return __node_2_dle(left);
  2032. }
  2033. /*
  2034. * __pick_next_task_dl - Helper to pick the next -deadline task to run.
  2035. * @rq: The runqueue to pick the next task from.
  2036. */
  2037. static struct task_struct *__pick_task_dl(struct rq *rq)
  2038. {
  2039. struct sched_dl_entity *dl_se;
  2040. struct dl_rq *dl_rq = &rq->dl;
  2041. struct task_struct *p;
  2042. again:
  2043. if (!sched_dl_runnable(rq))
  2044. return NULL;
  2045. dl_se = pick_next_dl_entity(dl_rq);
  2046. WARN_ON_ONCE(!dl_se);
  2047. if (dl_server(dl_se)) {
  2048. p = dl_se->server_pick_task(dl_se);
  2049. if (!p) {
  2050. if (dl_server_active(dl_se)) {
  2051. dl_se->dl_yielded = 1;
  2052. update_curr_dl_se(rq, dl_se, 0);
  2053. }
  2054. goto again;
  2055. }
  2056. rq->dl_server = dl_se;
  2057. } else {
  2058. p = dl_task_of(dl_se);
  2059. }
  2060. return p;
  2061. }
  2062. static struct task_struct *pick_task_dl(struct rq *rq)
  2063. {
  2064. return __pick_task_dl(rq);
  2065. }
  2066. static void put_prev_task_dl(struct rq *rq, struct task_struct *p, struct task_struct *next)
  2067. {
  2068. struct sched_dl_entity *dl_se = &p->dl;
  2069. struct dl_rq *dl_rq = &rq->dl;
  2070. if (on_dl_rq(&p->dl))
  2071. update_stats_wait_start_dl(dl_rq, dl_se);
  2072. update_curr_dl(rq);
  2073. update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
  2074. if (on_dl_rq(&p->dl) && p->nr_cpus_allowed > 1)
  2075. enqueue_pushable_dl_task(rq, p);
  2076. }
  2077. /*
  2078. * scheduler tick hitting a task of our scheduling class.
  2079. *
  2080. * NOTE: This function can be called remotely by the tick offload that
  2081. * goes along full dynticks. Therefore no local assumption can be made
  2082. * and everything must be accessed through the @rq and @curr passed in
  2083. * parameters.
  2084. */
  2085. static void task_tick_dl(struct rq *rq, struct task_struct *p, int queued)
  2086. {
  2087. update_curr_dl(rq);
  2088. update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 1);
  2089. /*
  2090. * Even when we have runtime, update_curr_dl() might have resulted in us
  2091. * not being the leftmost task anymore. In that case NEED_RESCHED will
  2092. * be set and schedule() will start a new hrtick for the next task.
  2093. */
  2094. if (hrtick_enabled_dl(rq) && queued && p->dl.runtime > 0 &&
  2095. is_leftmost(&p->dl, &rq->dl))
  2096. start_hrtick_dl(rq, &p->dl);
  2097. }
  2098. static void task_fork_dl(struct task_struct *p)
  2099. {
  2100. /*
  2101. * SCHED_DEADLINE tasks cannot fork and this is achieved through
  2102. * sched_fork()
  2103. */
  2104. }
  2105. #ifdef CONFIG_SMP
  2106. /* Only try algorithms three times */
  2107. #define DL_MAX_TRIES 3
  2108. static int pick_dl_task(struct rq *rq, struct task_struct *p, int cpu)
  2109. {
  2110. if (!task_on_cpu(rq, p) &&
  2111. cpumask_test_cpu(cpu, &p->cpus_mask))
  2112. return 1;
  2113. return 0;
  2114. }
  2115. /*
  2116. * Return the earliest pushable rq's task, which is suitable to be executed
  2117. * on the CPU, NULL otherwise:
  2118. */
  2119. static struct task_struct *pick_earliest_pushable_dl_task(struct rq *rq, int cpu)
  2120. {
  2121. struct task_struct *p = NULL;
  2122. struct rb_node *next_node;
  2123. if (!has_pushable_dl_tasks(rq))
  2124. return NULL;
  2125. next_node = rb_first_cached(&rq->dl.pushable_dl_tasks_root);
  2126. next_node:
  2127. if (next_node) {
  2128. p = __node_2_pdl(next_node);
  2129. if (pick_dl_task(rq, p, cpu))
  2130. return p;
  2131. next_node = rb_next(next_node);
  2132. goto next_node;
  2133. }
  2134. return NULL;
  2135. }
  2136. static DEFINE_PER_CPU(cpumask_var_t, local_cpu_mask_dl);
  2137. static int find_later_rq(struct task_struct *task)
  2138. {
  2139. struct sched_domain *sd;
  2140. struct cpumask *later_mask = this_cpu_cpumask_var_ptr(local_cpu_mask_dl);
  2141. int this_cpu = smp_processor_id();
  2142. int cpu = task_cpu(task);
  2143. /* Make sure the mask is initialized first */
  2144. if (unlikely(!later_mask))
  2145. return -1;
  2146. if (task->nr_cpus_allowed == 1)
  2147. return -1;
  2148. /*
  2149. * We have to consider system topology and task affinity
  2150. * first, then we can look for a suitable CPU.
  2151. */
  2152. if (!cpudl_find(&task_rq(task)->rd->cpudl, task, later_mask))
  2153. return -1;
  2154. /*
  2155. * If we are here, some targets have been found, including
  2156. * the most suitable which is, among the runqueues where the
  2157. * current tasks have later deadlines than the task's one, the
  2158. * rq with the latest possible one.
  2159. *
  2160. * Now we check how well this matches with task's
  2161. * affinity and system topology.
  2162. *
  2163. * The last CPU where the task run is our first
  2164. * guess, since it is most likely cache-hot there.
  2165. */
  2166. if (cpumask_test_cpu(cpu, later_mask))
  2167. return cpu;
  2168. /*
  2169. * Check if this_cpu is to be skipped (i.e., it is
  2170. * not in the mask) or not.
  2171. */
  2172. if (!cpumask_test_cpu(this_cpu, later_mask))
  2173. this_cpu = -1;
  2174. rcu_read_lock();
  2175. for_each_domain(cpu, sd) {
  2176. if (sd->flags & SD_WAKE_AFFINE) {
  2177. int best_cpu;
  2178. /*
  2179. * If possible, preempting this_cpu is
  2180. * cheaper than migrating.
  2181. */
  2182. if (this_cpu != -1 &&
  2183. cpumask_test_cpu(this_cpu, sched_domain_span(sd))) {
  2184. rcu_read_unlock();
  2185. return this_cpu;
  2186. }
  2187. best_cpu = cpumask_any_and_distribute(later_mask,
  2188. sched_domain_span(sd));
  2189. /*
  2190. * Last chance: if a CPU being in both later_mask
  2191. * and current sd span is valid, that becomes our
  2192. * choice. Of course, the latest possible CPU is
  2193. * already under consideration through later_mask.
  2194. */
  2195. if (best_cpu < nr_cpu_ids) {
  2196. rcu_read_unlock();
  2197. return best_cpu;
  2198. }
  2199. }
  2200. }
  2201. rcu_read_unlock();
  2202. /*
  2203. * At this point, all our guesses failed, we just return
  2204. * 'something', and let the caller sort the things out.
  2205. */
  2206. if (this_cpu != -1)
  2207. return this_cpu;
  2208. cpu = cpumask_any_distribute(later_mask);
  2209. if (cpu < nr_cpu_ids)
  2210. return cpu;
  2211. return -1;
  2212. }
  2213. static struct task_struct *pick_next_pushable_dl_task(struct rq *rq)
  2214. {
  2215. struct task_struct *p;
  2216. if (!has_pushable_dl_tasks(rq))
  2217. return NULL;
  2218. p = __node_2_pdl(rb_first_cached(&rq->dl.pushable_dl_tasks_root));
  2219. WARN_ON_ONCE(rq->cpu != task_cpu(p));
  2220. WARN_ON_ONCE(task_current(rq, p));
  2221. WARN_ON_ONCE(p->nr_cpus_allowed <= 1);
  2222. WARN_ON_ONCE(!task_on_rq_queued(p));
  2223. WARN_ON_ONCE(!dl_task(p));
  2224. return p;
  2225. }
  2226. /* Locks the rq it finds */
  2227. static struct rq *find_lock_later_rq(struct task_struct *task, struct rq *rq)
  2228. {
  2229. struct rq *later_rq = NULL;
  2230. int tries;
  2231. int cpu;
  2232. for (tries = 0; tries < DL_MAX_TRIES; tries++) {
  2233. cpu = find_later_rq(task);
  2234. if ((cpu == -1) || (cpu == rq->cpu))
  2235. break;
  2236. later_rq = cpu_rq(cpu);
  2237. if (!dl_task_is_earliest_deadline(task, later_rq)) {
  2238. /*
  2239. * Target rq has tasks of equal or earlier deadline,
  2240. * retrying does not release any lock and is unlikely
  2241. * to yield a different result.
  2242. */
  2243. later_rq = NULL;
  2244. break;
  2245. }
  2246. /* Retry if something changed. */
  2247. if (double_lock_balance(rq, later_rq)) {
  2248. /*
  2249. * double_lock_balance had to release rq->lock, in the
  2250. * meantime, task may no longer be fit to be migrated.
  2251. * Check the following to ensure that the task is
  2252. * still suitable for migration:
  2253. * 1. It is possible the task was scheduled,
  2254. * migrate_disabled was set and then got preempted,
  2255. * so we must check the task migration disable
  2256. * flag.
  2257. * 2. The CPU picked is in the task's affinity.
  2258. * 3. For throttled task (dl_task_offline_migration),
  2259. * check the following:
  2260. * - the task is not on the rq anymore (it was
  2261. * migrated)
  2262. * - the task is not on CPU anymore
  2263. * - the task is still a dl task
  2264. * - the task is not queued on the rq anymore
  2265. * 4. For the non-throttled task (push_dl_task), the
  2266. * check to ensure that this task is still at the
  2267. * head of the pushable tasks list is enough.
  2268. */
  2269. if (unlikely(is_migration_disabled(task) ||
  2270. !cpumask_test_cpu(later_rq->cpu, &task->cpus_mask) ||
  2271. (task->dl.dl_throttled &&
  2272. (task_rq(task) != rq ||
  2273. task_on_cpu(rq, task) ||
  2274. !dl_task(task) ||
  2275. !task_on_rq_queued(task))) ||
  2276. (!task->dl.dl_throttled &&
  2277. task != pick_next_pushable_dl_task(rq)))) {
  2278. double_unlock_balance(rq, later_rq);
  2279. later_rq = NULL;
  2280. break;
  2281. }
  2282. }
  2283. /*
  2284. * If the rq we found has no -deadline task, or
  2285. * its earliest one has a later deadline than our
  2286. * task, the rq is a good one.
  2287. */
  2288. if (dl_task_is_earliest_deadline(task, later_rq))
  2289. break;
  2290. /* Otherwise we try again. */
  2291. double_unlock_balance(rq, later_rq);
  2292. later_rq = NULL;
  2293. }
  2294. return later_rq;
  2295. }
  2296. /*
  2297. * See if the non running -deadline tasks on this rq
  2298. * can be sent to some other CPU where they can preempt
  2299. * and start executing.
  2300. */
  2301. static int push_dl_task(struct rq *rq)
  2302. {
  2303. struct task_struct *next_task;
  2304. struct rq *later_rq;
  2305. int ret = 0;
  2306. next_task = pick_next_pushable_dl_task(rq);
  2307. if (!next_task)
  2308. return 0;
  2309. retry:
  2310. /*
  2311. * If next_task preempts rq->curr, and rq->curr
  2312. * can move away, it makes sense to just reschedule
  2313. * without going further in pushing next_task.
  2314. */
  2315. if (dl_task(rq->curr) &&
  2316. dl_time_before(next_task->dl.deadline, rq->curr->dl.deadline) &&
  2317. rq->curr->nr_cpus_allowed > 1) {
  2318. resched_curr(rq);
  2319. return 0;
  2320. }
  2321. if (is_migration_disabled(next_task))
  2322. return 0;
  2323. if (WARN_ON(next_task == rq->curr))
  2324. return 0;
  2325. /* We might release rq lock */
  2326. get_task_struct(next_task);
  2327. /* Will lock the rq it'll find */
  2328. later_rq = find_lock_later_rq(next_task, rq);
  2329. if (!later_rq) {
  2330. struct task_struct *task;
  2331. /*
  2332. * We must check all this again, since
  2333. * find_lock_later_rq releases rq->lock and it is
  2334. * then possible that next_task has migrated.
  2335. */
  2336. task = pick_next_pushable_dl_task(rq);
  2337. if (task == next_task) {
  2338. /*
  2339. * The task is still there. We don't try
  2340. * again, some other CPU will pull it when ready.
  2341. */
  2342. goto out;
  2343. }
  2344. if (!task)
  2345. /* No more tasks */
  2346. goto out;
  2347. put_task_struct(next_task);
  2348. next_task = task;
  2349. goto retry;
  2350. }
  2351. deactivate_task(rq, next_task, 0);
  2352. set_task_cpu(next_task, later_rq->cpu);
  2353. activate_task(later_rq, next_task, 0);
  2354. ret = 1;
  2355. resched_curr(later_rq);
  2356. double_unlock_balance(rq, later_rq);
  2357. out:
  2358. put_task_struct(next_task);
  2359. return ret;
  2360. }
  2361. static void push_dl_tasks(struct rq *rq)
  2362. {
  2363. /* push_dl_task() will return true if it moved a -deadline task */
  2364. while (push_dl_task(rq))
  2365. ;
  2366. }
  2367. static void pull_dl_task(struct rq *this_rq)
  2368. {
  2369. int this_cpu = this_rq->cpu, cpu;
  2370. struct task_struct *p, *push_task;
  2371. bool resched = false;
  2372. struct rq *src_rq;
  2373. u64 dmin = LONG_MAX;
  2374. if (likely(!dl_overloaded(this_rq)))
  2375. return;
  2376. /*
  2377. * Match the barrier from dl_set_overloaded; this guarantees that if we
  2378. * see overloaded we must also see the dlo_mask bit.
  2379. */
  2380. smp_rmb();
  2381. for_each_cpu(cpu, this_rq->rd->dlo_mask) {
  2382. if (this_cpu == cpu)
  2383. continue;
  2384. src_rq = cpu_rq(cpu);
  2385. /*
  2386. * It looks racy, and it is! However, as in sched_rt.c,
  2387. * we are fine with this.
  2388. */
  2389. if (this_rq->dl.dl_nr_running &&
  2390. dl_time_before(this_rq->dl.earliest_dl.curr,
  2391. src_rq->dl.earliest_dl.next))
  2392. continue;
  2393. /* Might drop this_rq->lock */
  2394. push_task = NULL;
  2395. double_lock_balance(this_rq, src_rq);
  2396. /*
  2397. * If there are no more pullable tasks on the
  2398. * rq, we're done with it.
  2399. */
  2400. if (src_rq->dl.dl_nr_running <= 1)
  2401. goto skip;
  2402. p = pick_earliest_pushable_dl_task(src_rq, this_cpu);
  2403. /*
  2404. * We found a task to be pulled if:
  2405. * - it preempts our current (if there's one),
  2406. * - it will preempt the last one we pulled (if any).
  2407. */
  2408. if (p && dl_time_before(p->dl.deadline, dmin) &&
  2409. dl_task_is_earliest_deadline(p, this_rq)) {
  2410. WARN_ON(p == src_rq->curr);
  2411. WARN_ON(!task_on_rq_queued(p));
  2412. /*
  2413. * Then we pull iff p has actually an earlier
  2414. * deadline than the current task of its runqueue.
  2415. */
  2416. if (dl_time_before(p->dl.deadline,
  2417. src_rq->curr->dl.deadline))
  2418. goto skip;
  2419. if (is_migration_disabled(p)) {
  2420. push_task = get_push_task(src_rq);
  2421. } else {
  2422. deactivate_task(src_rq, p, 0);
  2423. set_task_cpu(p, this_cpu);
  2424. activate_task(this_rq, p, 0);
  2425. dmin = p->dl.deadline;
  2426. resched = true;
  2427. }
  2428. /* Is there any other task even earlier? */
  2429. }
  2430. skip:
  2431. double_unlock_balance(this_rq, src_rq);
  2432. if (push_task) {
  2433. preempt_disable();
  2434. raw_spin_rq_unlock(this_rq);
  2435. stop_one_cpu_nowait(src_rq->cpu, push_cpu_stop,
  2436. push_task, &src_rq->push_work);
  2437. preempt_enable();
  2438. raw_spin_rq_lock(this_rq);
  2439. }
  2440. }
  2441. if (resched)
  2442. resched_curr(this_rq);
  2443. }
  2444. /*
  2445. * Since the task is not running and a reschedule is not going to happen
  2446. * anytime soon on its runqueue, we try pushing it away now.
  2447. */
  2448. static void task_woken_dl(struct rq *rq, struct task_struct *p)
  2449. {
  2450. if (!task_on_cpu(rq, p) &&
  2451. !test_tsk_need_resched(rq->curr) &&
  2452. p->nr_cpus_allowed > 1 &&
  2453. dl_task(rq->curr) &&
  2454. (rq->curr->nr_cpus_allowed < 2 ||
  2455. !dl_entity_preempt(&p->dl, &rq->curr->dl))) {
  2456. push_dl_tasks(rq);
  2457. }
  2458. }
  2459. static void set_cpus_allowed_dl(struct task_struct *p,
  2460. struct affinity_context *ctx)
  2461. {
  2462. struct root_domain *src_rd;
  2463. struct rq *rq;
  2464. WARN_ON_ONCE(!dl_task(p));
  2465. rq = task_rq(p);
  2466. src_rd = rq->rd;
  2467. /*
  2468. * Migrating a SCHED_DEADLINE task between exclusive
  2469. * cpusets (different root_domains) entails a bandwidth
  2470. * update. We already made space for us in the destination
  2471. * domain (see cpuset_can_attach()).
  2472. */
  2473. if (!cpumask_intersects(src_rd->span, ctx->new_mask)) {
  2474. struct dl_bw *src_dl_b;
  2475. src_dl_b = dl_bw_of(cpu_of(rq));
  2476. /*
  2477. * We now free resources of the root_domain we are migrating
  2478. * off. In the worst case, sched_setattr() may temporary fail
  2479. * until we complete the update.
  2480. */
  2481. raw_spin_lock(&src_dl_b->lock);
  2482. __dl_sub(src_dl_b, p->dl.dl_bw, dl_bw_cpus(task_cpu(p)));
  2483. raw_spin_unlock(&src_dl_b->lock);
  2484. }
  2485. set_cpus_allowed_common(p, ctx);
  2486. }
  2487. /* Assumes rq->lock is held */
  2488. static void rq_online_dl(struct rq *rq)
  2489. {
  2490. if (rq->dl.overloaded)
  2491. dl_set_overload(rq);
  2492. cpudl_set_freecpu(&rq->rd->cpudl, rq->cpu);
  2493. if (rq->dl.dl_nr_running > 0)
  2494. cpudl_set(&rq->rd->cpudl, rq->cpu, rq->dl.earliest_dl.curr);
  2495. }
  2496. /* Assumes rq->lock is held */
  2497. static void rq_offline_dl(struct rq *rq)
  2498. {
  2499. if (rq->dl.overloaded)
  2500. dl_clear_overload(rq);
  2501. cpudl_clear(&rq->rd->cpudl, rq->cpu);
  2502. cpudl_clear_freecpu(&rq->rd->cpudl, rq->cpu);
  2503. }
  2504. void __init init_sched_dl_class(void)
  2505. {
  2506. unsigned int i;
  2507. for_each_possible_cpu(i)
  2508. zalloc_cpumask_var_node(&per_cpu(local_cpu_mask_dl, i),
  2509. GFP_KERNEL, cpu_to_node(i));
  2510. }
  2511. void dl_add_task_root_domain(struct task_struct *p)
  2512. {
  2513. struct rq_flags rf;
  2514. struct rq *rq;
  2515. struct dl_bw *dl_b;
  2516. raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
  2517. if (!dl_task(p)) {
  2518. raw_spin_unlock_irqrestore(&p->pi_lock, rf.flags);
  2519. return;
  2520. }
  2521. rq = __task_rq_lock(p, &rf);
  2522. dl_b = &rq->rd->dl_bw;
  2523. raw_spin_lock(&dl_b->lock);
  2524. __dl_add(dl_b, p->dl.dl_bw, cpumask_weight(rq->rd->span));
  2525. raw_spin_unlock(&dl_b->lock);
  2526. task_rq_unlock(rq, p, &rf);
  2527. }
  2528. void dl_clear_root_domain(struct root_domain *rd)
  2529. {
  2530. unsigned long flags;
  2531. raw_spin_lock_irqsave(&rd->dl_bw.lock, flags);
  2532. rd->dl_bw.total_bw = 0;
  2533. raw_spin_unlock_irqrestore(&rd->dl_bw.lock, flags);
  2534. }
  2535. #endif /* CONFIG_SMP */
  2536. static void switched_from_dl(struct rq *rq, struct task_struct *p)
  2537. {
  2538. /*
  2539. * task_non_contending() can start the "inactive timer" (if the 0-lag
  2540. * time is in the future). If the task switches back to dl before
  2541. * the "inactive timer" fires, it can continue to consume its current
  2542. * runtime using its current deadline. If it stays outside of
  2543. * SCHED_DEADLINE until the 0-lag time passes, inactive_task_timer()
  2544. * will reset the task parameters.
  2545. */
  2546. if (task_on_rq_queued(p) && p->dl.dl_runtime)
  2547. task_non_contending(&p->dl);
  2548. /*
  2549. * In case a task is setscheduled out from SCHED_DEADLINE we need to
  2550. * keep track of that on its cpuset (for correct bandwidth tracking).
  2551. */
  2552. dec_dl_tasks_cs(p);
  2553. if (!task_on_rq_queued(p)) {
  2554. /*
  2555. * Inactive timer is armed. However, p is leaving DEADLINE and
  2556. * might migrate away from this rq while continuing to run on
  2557. * some other class. We need to remove its contribution from
  2558. * this rq running_bw now, or sub_rq_bw (below) will complain.
  2559. */
  2560. if (p->dl.dl_non_contending)
  2561. sub_running_bw(&p->dl, &rq->dl);
  2562. sub_rq_bw(&p->dl, &rq->dl);
  2563. }
  2564. /*
  2565. * We cannot use inactive_task_timer() to invoke sub_running_bw()
  2566. * at the 0-lag time, because the task could have been migrated
  2567. * while SCHED_OTHER in the meanwhile.
  2568. */
  2569. if (p->dl.dl_non_contending)
  2570. p->dl.dl_non_contending = 0;
  2571. /*
  2572. * Since this might be the only -deadline task on the rq,
  2573. * this is the right place to try to pull some other one
  2574. * from an overloaded CPU, if any.
  2575. */
  2576. if (!task_on_rq_queued(p) || rq->dl.dl_nr_running)
  2577. return;
  2578. deadline_queue_pull_task(rq);
  2579. }
  2580. /*
  2581. * When switching to -deadline, we may overload the rq, then
  2582. * we try to push someone off, if possible.
  2583. */
  2584. static void switched_to_dl(struct rq *rq, struct task_struct *p)
  2585. {
  2586. if (hrtimer_try_to_cancel(&p->dl.inactive_timer) == 1)
  2587. put_task_struct(p);
  2588. /*
  2589. * In case a task is setscheduled to SCHED_DEADLINE we need to keep
  2590. * track of that on its cpuset (for correct bandwidth tracking).
  2591. */
  2592. inc_dl_tasks_cs(p);
  2593. /* If p is not queued we will update its parameters at next wakeup. */
  2594. if (!task_on_rq_queued(p)) {
  2595. add_rq_bw(&p->dl, &rq->dl);
  2596. return;
  2597. }
  2598. if (rq->curr != p) {
  2599. #ifdef CONFIG_SMP
  2600. if (p->nr_cpus_allowed > 1 && rq->dl.overloaded)
  2601. deadline_queue_push_tasks(rq);
  2602. #endif
  2603. if (dl_task(rq->curr))
  2604. wakeup_preempt_dl(rq, p, 0);
  2605. else
  2606. resched_curr(rq);
  2607. } else {
  2608. update_dl_rq_load_avg(rq_clock_pelt(rq), rq, 0);
  2609. }
  2610. }
  2611. /*
  2612. * If the scheduling parameters of a -deadline task changed,
  2613. * a push or pull operation might be needed.
  2614. */
  2615. static void prio_changed_dl(struct rq *rq, struct task_struct *p,
  2616. int oldprio)
  2617. {
  2618. if (!task_on_rq_queued(p))
  2619. return;
  2620. #ifdef CONFIG_SMP
  2621. /*
  2622. * This might be too much, but unfortunately
  2623. * we don't have the old deadline value, and
  2624. * we can't argue if the task is increasing
  2625. * or lowering its prio, so...
  2626. */
  2627. if (!rq->dl.overloaded)
  2628. deadline_queue_pull_task(rq);
  2629. if (task_current(rq, p)) {
  2630. /*
  2631. * If we now have a earlier deadline task than p,
  2632. * then reschedule, provided p is still on this
  2633. * runqueue.
  2634. */
  2635. if (dl_time_before(rq->dl.earliest_dl.curr, p->dl.deadline))
  2636. resched_curr(rq);
  2637. } else {
  2638. /*
  2639. * Current may not be deadline in case p was throttled but we
  2640. * have just replenished it (e.g. rt_mutex_setprio()).
  2641. *
  2642. * Otherwise, if p was given an earlier deadline, reschedule.
  2643. */
  2644. if (!dl_task(rq->curr) ||
  2645. dl_time_before(p->dl.deadline, rq->curr->dl.deadline))
  2646. resched_curr(rq);
  2647. }
  2648. #else
  2649. /*
  2650. * We don't know if p has a earlier or later deadline, so let's blindly
  2651. * set a (maybe not needed) rescheduling point.
  2652. */
  2653. resched_curr(rq);
  2654. #endif
  2655. }
  2656. #ifdef CONFIG_SCHED_CORE
  2657. static int task_is_throttled_dl(struct task_struct *p, int cpu)
  2658. {
  2659. return p->dl.dl_throttled;
  2660. }
  2661. #endif
  2662. DEFINE_SCHED_CLASS(dl) = {
  2663. .enqueue_task = enqueue_task_dl,
  2664. .dequeue_task = dequeue_task_dl,
  2665. .yield_task = yield_task_dl,
  2666. .wakeup_preempt = wakeup_preempt_dl,
  2667. .pick_task = pick_task_dl,
  2668. .put_prev_task = put_prev_task_dl,
  2669. .set_next_task = set_next_task_dl,
  2670. #ifdef CONFIG_SMP
  2671. .balance = balance_dl,
  2672. .select_task_rq = select_task_rq_dl,
  2673. .migrate_task_rq = migrate_task_rq_dl,
  2674. .set_cpus_allowed = set_cpus_allowed_dl,
  2675. .rq_online = rq_online_dl,
  2676. .rq_offline = rq_offline_dl,
  2677. .task_woken = task_woken_dl,
  2678. .find_lock_rq = find_lock_later_rq,
  2679. #endif
  2680. .task_tick = task_tick_dl,
  2681. .task_fork = task_fork_dl,
  2682. .prio_changed = prio_changed_dl,
  2683. .switched_from = switched_from_dl,
  2684. .switched_to = switched_to_dl,
  2685. .update_curr = update_curr_dl,
  2686. #ifdef CONFIG_SCHED_CORE
  2687. .task_is_throttled = task_is_throttled_dl,
  2688. #endif
  2689. };
  2690. /* Used for dl_bw check and update, used under sched_rt_handler()::mutex */
  2691. static u64 dl_generation;
  2692. int sched_dl_global_validate(void)
  2693. {
  2694. u64 runtime = global_rt_runtime();
  2695. u64 period = global_rt_period();
  2696. u64 new_bw = to_ratio(period, runtime);
  2697. u64 gen = ++dl_generation;
  2698. struct dl_bw *dl_b;
  2699. int cpu, cpus, ret = 0;
  2700. unsigned long flags;
  2701. /*
  2702. * Here we want to check the bandwidth not being set to some
  2703. * value smaller than the currently allocated bandwidth in
  2704. * any of the root_domains.
  2705. */
  2706. for_each_online_cpu(cpu) {
  2707. rcu_read_lock_sched();
  2708. if (dl_bw_visited(cpu, gen))
  2709. goto next;
  2710. dl_b = dl_bw_of(cpu);
  2711. cpus = dl_bw_cpus(cpu);
  2712. raw_spin_lock_irqsave(&dl_b->lock, flags);
  2713. if (new_bw * cpus < dl_b->total_bw)
  2714. ret = -EBUSY;
  2715. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  2716. next:
  2717. rcu_read_unlock_sched();
  2718. if (ret)
  2719. break;
  2720. }
  2721. return ret;
  2722. }
  2723. static void init_dl_rq_bw_ratio(struct dl_rq *dl_rq)
  2724. {
  2725. if (global_rt_runtime() == RUNTIME_INF) {
  2726. dl_rq->bw_ratio = 1 << RATIO_SHIFT;
  2727. dl_rq->max_bw = dl_rq->extra_bw = 1 << BW_SHIFT;
  2728. } else {
  2729. dl_rq->bw_ratio = to_ratio(global_rt_runtime(),
  2730. global_rt_period()) >> (BW_SHIFT - RATIO_SHIFT);
  2731. dl_rq->max_bw = dl_rq->extra_bw =
  2732. to_ratio(global_rt_period(), global_rt_runtime());
  2733. }
  2734. }
  2735. void sched_dl_do_global(void)
  2736. {
  2737. u64 new_bw = -1;
  2738. u64 gen = ++dl_generation;
  2739. struct dl_bw *dl_b;
  2740. int cpu;
  2741. unsigned long flags;
  2742. if (global_rt_runtime() != RUNTIME_INF)
  2743. new_bw = to_ratio(global_rt_period(), global_rt_runtime());
  2744. for_each_possible_cpu(cpu)
  2745. init_dl_rq_bw_ratio(&cpu_rq(cpu)->dl);
  2746. for_each_possible_cpu(cpu) {
  2747. rcu_read_lock_sched();
  2748. if (dl_bw_visited(cpu, gen)) {
  2749. rcu_read_unlock_sched();
  2750. continue;
  2751. }
  2752. dl_b = dl_bw_of(cpu);
  2753. raw_spin_lock_irqsave(&dl_b->lock, flags);
  2754. dl_b->bw = new_bw;
  2755. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  2756. rcu_read_unlock_sched();
  2757. }
  2758. }
  2759. /*
  2760. * We must be sure that accepting a new task (or allowing changing the
  2761. * parameters of an existing one) is consistent with the bandwidth
  2762. * constraints. If yes, this function also accordingly updates the currently
  2763. * allocated bandwidth to reflect the new situation.
  2764. *
  2765. * This function is called while holding p's rq->lock.
  2766. */
  2767. int sched_dl_overflow(struct task_struct *p, int policy,
  2768. const struct sched_attr *attr)
  2769. {
  2770. u64 period = attr->sched_period ?: attr->sched_deadline;
  2771. u64 runtime = attr->sched_runtime;
  2772. u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
  2773. int cpus, err = -1, cpu = task_cpu(p);
  2774. struct dl_bw *dl_b = dl_bw_of(cpu);
  2775. unsigned long cap;
  2776. if (attr->sched_flags & SCHED_FLAG_SUGOV)
  2777. return 0;
  2778. /* !deadline task may carry old deadline bandwidth */
  2779. if (new_bw == p->dl.dl_bw && task_has_dl_policy(p))
  2780. return 0;
  2781. /*
  2782. * Either if a task, enters, leave, or stays -deadline but changes
  2783. * its parameters, we may need to update accordingly the total
  2784. * allocated bandwidth of the container.
  2785. */
  2786. raw_spin_lock(&dl_b->lock);
  2787. cpus = dl_bw_cpus(cpu);
  2788. cap = dl_bw_capacity(cpu);
  2789. if (dl_policy(policy) && !task_has_dl_policy(p) &&
  2790. !__dl_overflow(dl_b, cap, 0, new_bw)) {
  2791. if (hrtimer_active(&p->dl.inactive_timer))
  2792. __dl_sub(dl_b, p->dl.dl_bw, cpus);
  2793. __dl_add(dl_b, new_bw, cpus);
  2794. err = 0;
  2795. } else if (dl_policy(policy) && task_has_dl_policy(p) &&
  2796. !__dl_overflow(dl_b, cap, p->dl.dl_bw, new_bw)) {
  2797. /*
  2798. * XXX this is slightly incorrect: when the task
  2799. * utilization decreases, we should delay the total
  2800. * utilization change until the task's 0-lag point.
  2801. * But this would require to set the task's "inactive
  2802. * timer" when the task is not inactive.
  2803. */
  2804. __dl_sub(dl_b, p->dl.dl_bw, cpus);
  2805. __dl_add(dl_b, new_bw, cpus);
  2806. dl_change_utilization(p, new_bw);
  2807. err = 0;
  2808. } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
  2809. /*
  2810. * Do not decrease the total deadline utilization here,
  2811. * switched_from_dl() will take care to do it at the correct
  2812. * (0-lag) time.
  2813. */
  2814. err = 0;
  2815. }
  2816. raw_spin_unlock(&dl_b->lock);
  2817. return err;
  2818. }
  2819. /*
  2820. * This function initializes the sched_dl_entity of a newly becoming
  2821. * SCHED_DEADLINE task.
  2822. *
  2823. * Only the static values are considered here, the actual runtime and the
  2824. * absolute deadline will be properly calculated when the task is enqueued
  2825. * for the first time with its new policy.
  2826. */
  2827. void __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
  2828. {
  2829. struct sched_dl_entity *dl_se = &p->dl;
  2830. dl_se->dl_runtime = attr->sched_runtime;
  2831. dl_se->dl_deadline = attr->sched_deadline;
  2832. dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
  2833. dl_se->flags = attr->sched_flags & SCHED_DL_FLAGS;
  2834. dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
  2835. dl_se->dl_density = to_ratio(dl_se->dl_deadline, dl_se->dl_runtime);
  2836. }
  2837. void __getparam_dl(struct task_struct *p, struct sched_attr *attr)
  2838. {
  2839. struct sched_dl_entity *dl_se = &p->dl;
  2840. attr->sched_priority = p->rt_priority;
  2841. attr->sched_runtime = dl_se->dl_runtime;
  2842. attr->sched_deadline = dl_se->dl_deadline;
  2843. attr->sched_period = dl_se->dl_period;
  2844. attr->sched_flags &= ~SCHED_DL_FLAGS;
  2845. attr->sched_flags |= dl_se->flags;
  2846. }
  2847. /*
  2848. * This function validates the new parameters of a -deadline task.
  2849. * We ask for the deadline not being zero, and greater or equal
  2850. * than the runtime, as well as the period of being zero or
  2851. * greater than deadline. Furthermore, we have to be sure that
  2852. * user parameters are above the internal resolution of 1us (we
  2853. * check sched_runtime only since it is always the smaller one) and
  2854. * below 2^63 ns (we have to check both sched_deadline and
  2855. * sched_period, as the latter can be zero).
  2856. */
  2857. bool __checkparam_dl(const struct sched_attr *attr)
  2858. {
  2859. u64 period, max, min;
  2860. /* special dl tasks don't actually use any parameter */
  2861. if (attr->sched_flags & SCHED_FLAG_SUGOV)
  2862. return true;
  2863. /* deadline != 0 */
  2864. if (attr->sched_deadline == 0)
  2865. return false;
  2866. /*
  2867. * Since we truncate DL_SCALE bits, make sure we're at least
  2868. * that big.
  2869. */
  2870. if (attr->sched_runtime < (1ULL << DL_SCALE))
  2871. return false;
  2872. /*
  2873. * Since we use the MSB for wrap-around and sign issues, make
  2874. * sure it's not set (mind that period can be equal to zero).
  2875. */
  2876. if (attr->sched_deadline & (1ULL << 63) ||
  2877. attr->sched_period & (1ULL << 63))
  2878. return false;
  2879. period = attr->sched_period;
  2880. if (!period)
  2881. period = attr->sched_deadline;
  2882. /* runtime <= deadline <= period (if period != 0) */
  2883. if (period < attr->sched_deadline ||
  2884. attr->sched_deadline < attr->sched_runtime)
  2885. return false;
  2886. max = (u64)READ_ONCE(sysctl_sched_dl_period_max) * NSEC_PER_USEC;
  2887. min = (u64)READ_ONCE(sysctl_sched_dl_period_min) * NSEC_PER_USEC;
  2888. if (period < min || period > max)
  2889. return false;
  2890. return true;
  2891. }
  2892. /*
  2893. * This function clears the sched_dl_entity static params.
  2894. */
  2895. static void __dl_clear_params(struct sched_dl_entity *dl_se)
  2896. {
  2897. dl_se->dl_runtime = 0;
  2898. dl_se->dl_deadline = 0;
  2899. dl_se->dl_period = 0;
  2900. dl_se->flags = 0;
  2901. dl_se->dl_bw = 0;
  2902. dl_se->dl_density = 0;
  2903. dl_se->dl_throttled = 0;
  2904. dl_se->dl_yielded = 0;
  2905. dl_se->dl_non_contending = 0;
  2906. dl_se->dl_overrun = 0;
  2907. dl_se->dl_server = 0;
  2908. #ifdef CONFIG_RT_MUTEXES
  2909. dl_se->pi_se = dl_se;
  2910. #endif
  2911. }
  2912. void init_dl_entity(struct sched_dl_entity *dl_se)
  2913. {
  2914. RB_CLEAR_NODE(&dl_se->rb_node);
  2915. init_dl_task_timer(dl_se);
  2916. init_dl_inactive_task_timer(dl_se);
  2917. __dl_clear_params(dl_se);
  2918. }
  2919. bool dl_param_changed(struct task_struct *p, const struct sched_attr *attr)
  2920. {
  2921. struct sched_dl_entity *dl_se = &p->dl;
  2922. if (dl_se->dl_runtime != attr->sched_runtime ||
  2923. dl_se->dl_deadline != attr->sched_deadline ||
  2924. dl_se->dl_period != attr->sched_period ||
  2925. dl_se->flags != (attr->sched_flags & SCHED_DL_FLAGS))
  2926. return true;
  2927. return false;
  2928. }
  2929. #ifdef CONFIG_SMP
  2930. int dl_cpuset_cpumask_can_shrink(const struct cpumask *cur,
  2931. const struct cpumask *trial)
  2932. {
  2933. unsigned long flags, cap;
  2934. struct dl_bw *cur_dl_b;
  2935. int ret = 1;
  2936. rcu_read_lock_sched();
  2937. cur_dl_b = dl_bw_of(cpumask_any(cur));
  2938. cap = __dl_bw_capacity(trial);
  2939. raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
  2940. if (__dl_overflow(cur_dl_b, cap, 0, 0))
  2941. ret = 0;
  2942. raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
  2943. rcu_read_unlock_sched();
  2944. return ret;
  2945. }
  2946. enum dl_bw_request {
  2947. dl_bw_req_check_overflow = 0,
  2948. dl_bw_req_alloc,
  2949. dl_bw_req_free
  2950. };
  2951. static int dl_bw_manage(enum dl_bw_request req, int cpu, u64 dl_bw)
  2952. {
  2953. unsigned long flags;
  2954. struct dl_bw *dl_b;
  2955. bool overflow = 0;
  2956. rcu_read_lock_sched();
  2957. dl_b = dl_bw_of(cpu);
  2958. raw_spin_lock_irqsave(&dl_b->lock, flags);
  2959. if (req == dl_bw_req_free) {
  2960. __dl_sub(dl_b, dl_bw, dl_bw_cpus(cpu));
  2961. } else {
  2962. unsigned long cap = dl_bw_capacity(cpu);
  2963. overflow = __dl_overflow(dl_b, cap, 0, dl_bw);
  2964. if (req == dl_bw_req_alloc && !overflow) {
  2965. /*
  2966. * We reserve space in the destination
  2967. * root_domain, as we can't fail after this point.
  2968. * We will free resources in the source root_domain
  2969. * later on (see set_cpus_allowed_dl()).
  2970. */
  2971. __dl_add(dl_b, dl_bw, dl_bw_cpus(cpu));
  2972. }
  2973. }
  2974. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  2975. rcu_read_unlock_sched();
  2976. return overflow ? -EBUSY : 0;
  2977. }
  2978. int dl_bw_check_overflow(int cpu)
  2979. {
  2980. return dl_bw_manage(dl_bw_req_check_overflow, cpu, 0);
  2981. }
  2982. int dl_bw_alloc(int cpu, u64 dl_bw)
  2983. {
  2984. return dl_bw_manage(dl_bw_req_alloc, cpu, dl_bw);
  2985. }
  2986. void dl_bw_free(int cpu, u64 dl_bw)
  2987. {
  2988. dl_bw_manage(dl_bw_req_free, cpu, dl_bw);
  2989. }
  2990. #endif
  2991. #ifdef CONFIG_SCHED_DEBUG
  2992. void print_dl_stats(struct seq_file *m, int cpu)
  2993. {
  2994. print_dl_rq(m, cpu, &cpu_rq(cpu)->dl);
  2995. }
  2996. #endif /* CONFIG_SCHED_DEBUG */