deadline.c 83 KB

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