sched_fair.c 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711
  1. /*
  2. * Completely Fair Scheduling (CFS) Class (SCHED_NORMAL/SCHED_BATCH)
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc., Ingo Molnar <mingo@redhat.com>
  5. *
  6. * Interactivity improvements by Mike Galbraith
  7. * (C) 2007 Mike Galbraith <efault@gmx.de>
  8. *
  9. * Various enhancements by Dmitry Adamushko.
  10. * (C) 2007 Dmitry Adamushko <dmitry.adamushko@gmail.com>
  11. *
  12. * Group scheduling enhancements by Srivatsa Vaddagiri
  13. * Copyright IBM Corporation, 2007
  14. * Author: Srivatsa Vaddagiri <vatsa@linux.vnet.ibm.com>
  15. *
  16. * Scaled math optimizations by Thomas Gleixner
  17. * Copyright (C) 2007, Thomas Gleixner <tglx@linutronix.de>
  18. *
  19. * Adaptive scheduling granularity, math enhancements by Peter Zijlstra
  20. * Copyright (C) 2007 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  21. */
  22. #include <linux/latencytop.h>
  23. #include <linux/sched.h>
  24. /*
  25. * Targeted preemption latency for CPU-bound tasks:
  26. * (default: 5ms * (1 + ilog(ncpus)), units: nanoseconds)
  27. *
  28. * NOTE: this latency value is not the same as the concept of
  29. * 'timeslice length' - timeslices in CFS are of variable length
  30. * and have no persistent notion like in traditional, time-slice
  31. * based scheduling concepts.
  32. *
  33. * (to see the precise effective timeslice length of your workload,
  34. * run vmstat and monitor the context-switches (cs) field)
  35. */
  36. unsigned int sysctl_sched_latency = 5000000ULL;
  37. unsigned int normalized_sysctl_sched_latency = 5000000ULL;
  38. /*
  39. * The initial- and re-scaling of tunables is configurable
  40. * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
  41. *
  42. * Options are:
  43. * SCHED_TUNABLESCALING_NONE - unscaled, always *1
  44. * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
  45. * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
  46. */
  47. enum sched_tunable_scaling sysctl_sched_tunable_scaling
  48. = SCHED_TUNABLESCALING_LOG;
  49. /*
  50. * Minimal preemption granularity for CPU-bound tasks:
  51. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  52. */
  53. unsigned int sysctl_sched_min_granularity = 1000000ULL;
  54. unsigned int normalized_sysctl_sched_min_granularity = 1000000ULL;
  55. /*
  56. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  57. */
  58. static unsigned int sched_nr_latency = 5;
  59. /*
  60. * After fork, child runs first. If set to 0 (default) then
  61. * parent will (try to) run first.
  62. */
  63. unsigned int sysctl_sched_child_runs_first __read_mostly;
  64. /*
  65. * sys_sched_yield() compat mode
  66. *
  67. * This option switches the agressive yield implementation of the
  68. * old scheduler back on.
  69. */
  70. unsigned int __read_mostly sysctl_sched_compat_yield;
  71. /*
  72. * SCHED_OTHER wake-up granularity.
  73. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  74. *
  75. * This option delays the preemption effects of decoupled workloads
  76. * and reduces their over-scheduling. Synchronous workloads will still
  77. * have immediate wakeup/sleep latencies.
  78. */
  79. unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
  80. unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
  81. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  82. static const struct sched_class fair_sched_class;
  83. /**************************************************************
  84. * CFS operations on generic schedulable entities:
  85. */
  86. #ifdef CONFIG_FAIR_GROUP_SCHED
  87. /* cpu runqueue to which this cfs_rq is attached */
  88. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  89. {
  90. return cfs_rq->rq;
  91. }
  92. /* An entity is a task if it doesn't "own" a runqueue */
  93. #define entity_is_task(se) (!se->my_q)
  94. static inline struct task_struct *task_of(struct sched_entity *se)
  95. {
  96. #ifdef CONFIG_SCHED_DEBUG
  97. WARN_ON_ONCE(!entity_is_task(se));
  98. #endif
  99. return container_of(se, struct task_struct, se);
  100. }
  101. /* Walk up scheduling entities hierarchy */
  102. #define for_each_sched_entity(se) \
  103. for (; se; se = se->parent)
  104. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  105. {
  106. return p->se.cfs_rq;
  107. }
  108. /* runqueue on which this entity is (to be) queued */
  109. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  110. {
  111. return se->cfs_rq;
  112. }
  113. /* runqueue "owned" by this group */
  114. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  115. {
  116. return grp->my_q;
  117. }
  118. /* Given a group's cfs_rq on one cpu, return its corresponding cfs_rq on
  119. * another cpu ('this_cpu')
  120. */
  121. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  122. {
  123. return cfs_rq->tg->cfs_rq[this_cpu];
  124. }
  125. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  126. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  127. list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  128. /* Do the two (enqueued) entities belong to the same group ? */
  129. static inline int
  130. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  131. {
  132. if (se->cfs_rq == pse->cfs_rq)
  133. return 1;
  134. return 0;
  135. }
  136. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  137. {
  138. return se->parent;
  139. }
  140. /* return depth at which a sched entity is present in the hierarchy */
  141. static inline int depth_se(struct sched_entity *se)
  142. {
  143. int depth = 0;
  144. for_each_sched_entity(se)
  145. depth++;
  146. return depth;
  147. }
  148. static void
  149. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  150. {
  151. int se_depth, pse_depth;
  152. /*
  153. * preemption test can be made between sibling entities who are in the
  154. * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
  155. * both tasks until we find their ancestors who are siblings of common
  156. * parent.
  157. */
  158. /* First walk up until both entities are at same depth */
  159. se_depth = depth_se(*se);
  160. pse_depth = depth_se(*pse);
  161. while (se_depth > pse_depth) {
  162. se_depth--;
  163. *se = parent_entity(*se);
  164. }
  165. while (pse_depth > se_depth) {
  166. pse_depth--;
  167. *pse = parent_entity(*pse);
  168. }
  169. while (!is_same_group(*se, *pse)) {
  170. *se = parent_entity(*se);
  171. *pse = parent_entity(*pse);
  172. }
  173. }
  174. #else /* !CONFIG_FAIR_GROUP_SCHED */
  175. static inline struct task_struct *task_of(struct sched_entity *se)
  176. {
  177. return container_of(se, struct task_struct, se);
  178. }
  179. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  180. {
  181. return container_of(cfs_rq, struct rq, cfs);
  182. }
  183. #define entity_is_task(se) 1
  184. #define for_each_sched_entity(se) \
  185. for (; se; se = NULL)
  186. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  187. {
  188. return &task_rq(p)->cfs;
  189. }
  190. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  191. {
  192. struct task_struct *p = task_of(se);
  193. struct rq *rq = task_rq(p);
  194. return &rq->cfs;
  195. }
  196. /* runqueue "owned" by this group */
  197. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  198. {
  199. return NULL;
  200. }
  201. static inline struct cfs_rq *cpu_cfs_rq(struct cfs_rq *cfs_rq, int this_cpu)
  202. {
  203. return &cpu_rq(this_cpu)->cfs;
  204. }
  205. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  206. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  207. static inline int
  208. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  209. {
  210. return 1;
  211. }
  212. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  213. {
  214. return NULL;
  215. }
  216. static inline void
  217. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  218. {
  219. }
  220. #endif /* CONFIG_FAIR_GROUP_SCHED */
  221. /**************************************************************
  222. * Scheduling class tree data structure manipulation methods:
  223. */
  224. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  225. {
  226. s64 delta = (s64)(vruntime - min_vruntime);
  227. if (delta > 0)
  228. min_vruntime = vruntime;
  229. return min_vruntime;
  230. }
  231. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  232. {
  233. s64 delta = (s64)(vruntime - min_vruntime);
  234. if (delta < 0)
  235. min_vruntime = vruntime;
  236. return min_vruntime;
  237. }
  238. static inline int entity_before(struct sched_entity *a,
  239. struct sched_entity *b)
  240. {
  241. return (s64)(a->vruntime - b->vruntime) < 0;
  242. }
  243. static inline s64 entity_key(struct cfs_rq *cfs_rq, struct sched_entity *se)
  244. {
  245. return se->vruntime - cfs_rq->min_vruntime;
  246. }
  247. static void update_min_vruntime(struct cfs_rq *cfs_rq)
  248. {
  249. u64 vruntime = cfs_rq->min_vruntime;
  250. if (cfs_rq->curr)
  251. vruntime = cfs_rq->curr->vruntime;
  252. if (cfs_rq->rb_leftmost) {
  253. struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
  254. struct sched_entity,
  255. run_node);
  256. if (!cfs_rq->curr)
  257. vruntime = se->vruntime;
  258. else
  259. vruntime = min_vruntime(vruntime, se->vruntime);
  260. }
  261. cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
  262. }
  263. /*
  264. * Enqueue an entity into the rb-tree:
  265. */
  266. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  267. {
  268. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  269. struct rb_node *parent = NULL;
  270. struct sched_entity *entry;
  271. s64 key = entity_key(cfs_rq, se);
  272. int leftmost = 1;
  273. /*
  274. * Find the right place in the rbtree:
  275. */
  276. while (*link) {
  277. parent = *link;
  278. entry = rb_entry(parent, struct sched_entity, run_node);
  279. /*
  280. * We dont care about collisions. Nodes with
  281. * the same key stay together.
  282. */
  283. if (key < entity_key(cfs_rq, entry)) {
  284. link = &parent->rb_left;
  285. } else {
  286. link = &parent->rb_right;
  287. leftmost = 0;
  288. }
  289. }
  290. /*
  291. * Maintain a cache of leftmost tree entries (it is frequently
  292. * used):
  293. */
  294. if (leftmost)
  295. cfs_rq->rb_leftmost = &se->run_node;
  296. rb_link_node(&se->run_node, parent, link);
  297. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  298. }
  299. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  300. {
  301. if (cfs_rq->rb_leftmost == &se->run_node) {
  302. struct rb_node *next_node;
  303. next_node = rb_next(&se->run_node);
  304. cfs_rq->rb_leftmost = next_node;
  305. }
  306. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  307. }
  308. static struct sched_entity *__pick_next_entity(struct cfs_rq *cfs_rq)
  309. {
  310. struct rb_node *left = cfs_rq->rb_leftmost;
  311. if (!left)
  312. return NULL;
  313. return rb_entry(left, struct sched_entity, run_node);
  314. }
  315. static struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  316. {
  317. struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
  318. if (!last)
  319. return NULL;
  320. return rb_entry(last, struct sched_entity, run_node);
  321. }
  322. /**************************************************************
  323. * Scheduling class statistics methods:
  324. */
  325. #ifdef CONFIG_SCHED_DEBUG
  326. int sched_proc_update_handler(struct ctl_table *table, int write,
  327. void __user *buffer, size_t *lenp,
  328. loff_t *ppos)
  329. {
  330. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  331. int factor = get_update_sysctl_factor();
  332. if (ret || !write)
  333. return ret;
  334. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  335. sysctl_sched_min_granularity);
  336. #define WRT_SYSCTL(name) \
  337. (normalized_sysctl_##name = sysctl_##name / (factor))
  338. WRT_SYSCTL(sched_min_granularity);
  339. WRT_SYSCTL(sched_latency);
  340. WRT_SYSCTL(sched_wakeup_granularity);
  341. WRT_SYSCTL(sched_shares_ratelimit);
  342. #undef WRT_SYSCTL
  343. return 0;
  344. }
  345. #endif
  346. /*
  347. * delta /= w
  348. */
  349. static inline unsigned long
  350. calc_delta_fair(unsigned long delta, struct sched_entity *se)
  351. {
  352. if (unlikely(se->load.weight != NICE_0_LOAD))
  353. delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
  354. return delta;
  355. }
  356. /*
  357. * The idea is to set a period in which each task runs once.
  358. *
  359. * When there are too many tasks (sysctl_sched_nr_latency) we have to stretch
  360. * this period because otherwise the slices get too small.
  361. *
  362. * p = (nr <= nl) ? l : l*nr/nl
  363. */
  364. static u64 __sched_period(unsigned long nr_running)
  365. {
  366. u64 period = sysctl_sched_latency;
  367. unsigned long nr_latency = sched_nr_latency;
  368. if (unlikely(nr_running > nr_latency)) {
  369. period = sysctl_sched_min_granularity;
  370. period *= nr_running;
  371. }
  372. return period;
  373. }
  374. /*
  375. * We calculate the wall-time slice from the period by taking a part
  376. * proportional to the weight.
  377. *
  378. * s = p*P[w/rw]
  379. */
  380. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  381. {
  382. u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
  383. for_each_sched_entity(se) {
  384. struct load_weight *load;
  385. struct load_weight lw;
  386. cfs_rq = cfs_rq_of(se);
  387. load = &cfs_rq->load;
  388. if (unlikely(!se->on_rq)) {
  389. lw = cfs_rq->load;
  390. update_load_add(&lw, se->load.weight);
  391. load = &lw;
  392. }
  393. slice = calc_delta_mine(slice, se->load.weight, load);
  394. }
  395. return slice;
  396. }
  397. /*
  398. * We calculate the vruntime slice of a to be inserted task
  399. *
  400. * vs = s/w
  401. */
  402. static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  403. {
  404. return calc_delta_fair(sched_slice(cfs_rq, se), se);
  405. }
  406. /*
  407. * Update the current task's runtime statistics. Skip current tasks that
  408. * are not in our scheduling class.
  409. */
  410. static inline void
  411. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  412. unsigned long delta_exec)
  413. {
  414. unsigned long delta_exec_weighted;
  415. schedstat_set(curr->exec_max, max((u64)delta_exec, curr->exec_max));
  416. curr->sum_exec_runtime += delta_exec;
  417. schedstat_add(cfs_rq, exec_clock, delta_exec);
  418. delta_exec_weighted = calc_delta_fair(delta_exec, curr);
  419. curr->vruntime += delta_exec_weighted;
  420. update_min_vruntime(cfs_rq);
  421. }
  422. static void update_curr(struct cfs_rq *cfs_rq)
  423. {
  424. struct sched_entity *curr = cfs_rq->curr;
  425. u64 now = rq_of(cfs_rq)->clock;
  426. unsigned long delta_exec;
  427. if (unlikely(!curr))
  428. return;
  429. /*
  430. * Get the amount of time the current task was running
  431. * since the last time we changed load (this cannot
  432. * overflow on 32 bits):
  433. */
  434. delta_exec = (unsigned long)(now - curr->exec_start);
  435. if (!delta_exec)
  436. return;
  437. __update_curr(cfs_rq, curr, delta_exec);
  438. curr->exec_start = now;
  439. if (entity_is_task(curr)) {
  440. struct task_struct *curtask = task_of(curr);
  441. trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
  442. cpuacct_charge(curtask, delta_exec);
  443. account_group_exec_runtime(curtask, delta_exec);
  444. }
  445. }
  446. static inline void
  447. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  448. {
  449. schedstat_set(se->wait_start, rq_of(cfs_rq)->clock);
  450. }
  451. /*
  452. * Task is being enqueued - update stats:
  453. */
  454. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  455. {
  456. /*
  457. * Are we enqueueing a waiting task? (for current tasks
  458. * a dequeue/enqueue event is a NOP)
  459. */
  460. if (se != cfs_rq->curr)
  461. update_stats_wait_start(cfs_rq, se);
  462. }
  463. static void
  464. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  465. {
  466. schedstat_set(se->wait_max, max(se->wait_max,
  467. rq_of(cfs_rq)->clock - se->wait_start));
  468. schedstat_set(se->wait_count, se->wait_count + 1);
  469. schedstat_set(se->wait_sum, se->wait_sum +
  470. rq_of(cfs_rq)->clock - se->wait_start);
  471. #ifdef CONFIG_SCHEDSTATS
  472. if (entity_is_task(se)) {
  473. trace_sched_stat_wait(task_of(se),
  474. rq_of(cfs_rq)->clock - se->wait_start);
  475. }
  476. #endif
  477. schedstat_set(se->wait_start, 0);
  478. }
  479. static inline void
  480. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  481. {
  482. /*
  483. * Mark the end of the wait period if dequeueing a
  484. * waiting task:
  485. */
  486. if (se != cfs_rq->curr)
  487. update_stats_wait_end(cfs_rq, se);
  488. }
  489. /*
  490. * We are picking a new current task - update its stats:
  491. */
  492. static inline void
  493. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  494. {
  495. /*
  496. * We are starting a new run period:
  497. */
  498. se->exec_start = rq_of(cfs_rq)->clock;
  499. }
  500. /**************************************************
  501. * Scheduling class queueing methods:
  502. */
  503. #if defined CONFIG_SMP && defined CONFIG_FAIR_GROUP_SCHED
  504. static void
  505. add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
  506. {
  507. cfs_rq->task_weight += weight;
  508. }
  509. #else
  510. static inline void
  511. add_cfs_task_weight(struct cfs_rq *cfs_rq, unsigned long weight)
  512. {
  513. }
  514. #endif
  515. static void
  516. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  517. {
  518. update_load_add(&cfs_rq->load, se->load.weight);
  519. if (!parent_entity(se))
  520. inc_cpu_load(rq_of(cfs_rq), se->load.weight);
  521. if (entity_is_task(se)) {
  522. add_cfs_task_weight(cfs_rq, se->load.weight);
  523. list_add(&se->group_node, &cfs_rq->tasks);
  524. }
  525. cfs_rq->nr_running++;
  526. se->on_rq = 1;
  527. }
  528. static void
  529. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  530. {
  531. update_load_sub(&cfs_rq->load, se->load.weight);
  532. if (!parent_entity(se))
  533. dec_cpu_load(rq_of(cfs_rq), se->load.weight);
  534. if (entity_is_task(se)) {
  535. add_cfs_task_weight(cfs_rq, -se->load.weight);
  536. list_del_init(&se->group_node);
  537. }
  538. cfs_rq->nr_running--;
  539. se->on_rq = 0;
  540. }
  541. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  542. {
  543. #ifdef CONFIG_SCHEDSTATS
  544. struct task_struct *tsk = NULL;
  545. if (entity_is_task(se))
  546. tsk = task_of(se);
  547. if (se->sleep_start) {
  548. u64 delta = rq_of(cfs_rq)->clock - se->sleep_start;
  549. if ((s64)delta < 0)
  550. delta = 0;
  551. if (unlikely(delta > se->sleep_max))
  552. se->sleep_max = delta;
  553. se->sleep_start = 0;
  554. se->sum_sleep_runtime += delta;
  555. if (tsk) {
  556. account_scheduler_latency(tsk, delta >> 10, 1);
  557. trace_sched_stat_sleep(tsk, delta);
  558. }
  559. }
  560. if (se->block_start) {
  561. u64 delta = rq_of(cfs_rq)->clock - se->block_start;
  562. if ((s64)delta < 0)
  563. delta = 0;
  564. if (unlikely(delta > se->block_max))
  565. se->block_max = delta;
  566. se->block_start = 0;
  567. se->sum_sleep_runtime += delta;
  568. if (tsk) {
  569. if (tsk->in_iowait) {
  570. se->iowait_sum += delta;
  571. se->iowait_count++;
  572. trace_sched_stat_iowait(tsk, delta);
  573. }
  574. /*
  575. * Blocking time is in units of nanosecs, so shift by
  576. * 20 to get a milliseconds-range estimation of the
  577. * amount of time that the task spent sleeping:
  578. */
  579. if (unlikely(prof_on == SLEEP_PROFILING)) {
  580. profile_hits(SLEEP_PROFILING,
  581. (void *)get_wchan(tsk),
  582. delta >> 20);
  583. }
  584. account_scheduler_latency(tsk, delta >> 10, 0);
  585. }
  586. }
  587. #endif
  588. }
  589. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  590. {
  591. #ifdef CONFIG_SCHED_DEBUG
  592. s64 d = se->vruntime - cfs_rq->min_vruntime;
  593. if (d < 0)
  594. d = -d;
  595. if (d > 3*sysctl_sched_latency)
  596. schedstat_inc(cfs_rq, nr_spread_over);
  597. #endif
  598. }
  599. static void
  600. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  601. {
  602. u64 vruntime = cfs_rq->min_vruntime;
  603. /*
  604. * The 'current' period is already promised to the current tasks,
  605. * however the extra weight of the new task will slow them down a
  606. * little, place the new task so that it fits in the slot that
  607. * stays open at the end.
  608. */
  609. if (initial && sched_feat(START_DEBIT))
  610. vruntime += sched_vslice(cfs_rq, se);
  611. /* sleeps up to a single latency don't count. */
  612. if (!initial && sched_feat(FAIR_SLEEPERS)) {
  613. unsigned long thresh = sysctl_sched_latency;
  614. /*
  615. * Convert the sleeper threshold into virtual time.
  616. * SCHED_IDLE is a special sub-class. We care about
  617. * fairness only relative to other SCHED_IDLE tasks,
  618. * all of which have the same weight.
  619. */
  620. if (sched_feat(NORMALIZED_SLEEPER) && (!entity_is_task(se) ||
  621. task_of(se)->policy != SCHED_IDLE))
  622. thresh = calc_delta_fair(thresh, se);
  623. /*
  624. * Halve their sleep time's effect, to allow
  625. * for a gentler effect of sleepers:
  626. */
  627. if (sched_feat(GENTLE_FAIR_SLEEPERS))
  628. thresh >>= 1;
  629. vruntime -= thresh;
  630. }
  631. /* ensure we never gain time by being placed backwards. */
  632. vruntime = max_vruntime(se->vruntime, vruntime);
  633. se->vruntime = vruntime;
  634. }
  635. #define ENQUEUE_WAKEUP 1
  636. #define ENQUEUE_MIGRATE 2
  637. static void
  638. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
  639. {
  640. /*
  641. * Update the normalized vruntime before updating min_vruntime
  642. * through callig update_curr().
  643. */
  644. if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_MIGRATE))
  645. se->vruntime += cfs_rq->min_vruntime;
  646. /*
  647. * Update run-time statistics of the 'current'.
  648. */
  649. update_curr(cfs_rq);
  650. account_entity_enqueue(cfs_rq, se);
  651. if (flags & ENQUEUE_WAKEUP) {
  652. place_entity(cfs_rq, se, 0);
  653. enqueue_sleeper(cfs_rq, se);
  654. }
  655. update_stats_enqueue(cfs_rq, se);
  656. check_spread(cfs_rq, se);
  657. if (se != cfs_rq->curr)
  658. __enqueue_entity(cfs_rq, se);
  659. }
  660. static void __clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  661. {
  662. if (!se || cfs_rq->last == se)
  663. cfs_rq->last = NULL;
  664. if (!se || cfs_rq->next == se)
  665. cfs_rq->next = NULL;
  666. }
  667. static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  668. {
  669. for_each_sched_entity(se)
  670. __clear_buddies(cfs_rq_of(se), se);
  671. }
  672. static void
  673. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int sleep)
  674. {
  675. /*
  676. * Update run-time statistics of the 'current'.
  677. */
  678. update_curr(cfs_rq);
  679. update_stats_dequeue(cfs_rq, se);
  680. if (sleep) {
  681. #ifdef CONFIG_SCHEDSTATS
  682. if (entity_is_task(se)) {
  683. struct task_struct *tsk = task_of(se);
  684. if (tsk->state & TASK_INTERRUPTIBLE)
  685. se->sleep_start = rq_of(cfs_rq)->clock;
  686. if (tsk->state & TASK_UNINTERRUPTIBLE)
  687. se->block_start = rq_of(cfs_rq)->clock;
  688. }
  689. #endif
  690. }
  691. clear_buddies(cfs_rq, se);
  692. if (se != cfs_rq->curr)
  693. __dequeue_entity(cfs_rq, se);
  694. account_entity_dequeue(cfs_rq, se);
  695. update_min_vruntime(cfs_rq);
  696. /*
  697. * Normalize the entity after updating the min_vruntime because the
  698. * update can refer to the ->curr item and we need to reflect this
  699. * movement in our normalized position.
  700. */
  701. if (!sleep)
  702. se->vruntime -= cfs_rq->min_vruntime;
  703. }
  704. /*
  705. * Preempt the current task with a newly woken task if needed:
  706. */
  707. static void
  708. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  709. {
  710. unsigned long ideal_runtime, delta_exec;
  711. ideal_runtime = sched_slice(cfs_rq, curr);
  712. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  713. if (delta_exec > ideal_runtime) {
  714. resched_task(rq_of(cfs_rq)->curr);
  715. /*
  716. * The current task ran long enough, ensure it doesn't get
  717. * re-elected due to buddy favours.
  718. */
  719. clear_buddies(cfs_rq, curr);
  720. return;
  721. }
  722. /*
  723. * Ensure that a task that missed wakeup preemption by a
  724. * narrow margin doesn't have to wait for a full slice.
  725. * This also mitigates buddy induced latencies under load.
  726. */
  727. if (!sched_feat(WAKEUP_PREEMPT))
  728. return;
  729. if (delta_exec < sysctl_sched_min_granularity)
  730. return;
  731. if (cfs_rq->nr_running > 1) {
  732. struct sched_entity *se = __pick_next_entity(cfs_rq);
  733. s64 delta = curr->vruntime - se->vruntime;
  734. if (delta > ideal_runtime)
  735. resched_task(rq_of(cfs_rq)->curr);
  736. }
  737. }
  738. static void
  739. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  740. {
  741. /* 'current' is not kept within the tree. */
  742. if (se->on_rq) {
  743. /*
  744. * Any task has to be enqueued before it get to execute on
  745. * a CPU. So account for the time it spent waiting on the
  746. * runqueue.
  747. */
  748. update_stats_wait_end(cfs_rq, se);
  749. __dequeue_entity(cfs_rq, se);
  750. }
  751. update_stats_curr_start(cfs_rq, se);
  752. cfs_rq->curr = se;
  753. #ifdef CONFIG_SCHEDSTATS
  754. /*
  755. * Track our maximum slice length, if the CPU's load is at
  756. * least twice that of our own weight (i.e. dont track it
  757. * when there are only lesser-weight tasks around):
  758. */
  759. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  760. se->slice_max = max(se->slice_max,
  761. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  762. }
  763. #endif
  764. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  765. }
  766. static int
  767. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
  768. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  769. {
  770. struct sched_entity *se = __pick_next_entity(cfs_rq);
  771. struct sched_entity *left = se;
  772. if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
  773. se = cfs_rq->next;
  774. /*
  775. * Prefer last buddy, try to return the CPU to a preempted task.
  776. */
  777. if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
  778. se = cfs_rq->last;
  779. clear_buddies(cfs_rq, se);
  780. return se;
  781. }
  782. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  783. {
  784. /*
  785. * If still on the runqueue then deactivate_task()
  786. * was not called and update_curr() has to be done:
  787. */
  788. if (prev->on_rq)
  789. update_curr(cfs_rq);
  790. check_spread(cfs_rq, prev);
  791. if (prev->on_rq) {
  792. update_stats_wait_start(cfs_rq, prev);
  793. /* Put 'current' back into the tree. */
  794. __enqueue_entity(cfs_rq, prev);
  795. }
  796. cfs_rq->curr = NULL;
  797. }
  798. static void
  799. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  800. {
  801. /*
  802. * Update run-time statistics of the 'current'.
  803. */
  804. update_curr(cfs_rq);
  805. #ifdef CONFIG_SCHED_HRTICK
  806. /*
  807. * queued ticks are scheduled to match the slice, so don't bother
  808. * validating it and just reschedule.
  809. */
  810. if (queued) {
  811. resched_task(rq_of(cfs_rq)->curr);
  812. return;
  813. }
  814. /*
  815. * don't let the period tick interfere with the hrtick preemption
  816. */
  817. if (!sched_feat(DOUBLE_TICK) &&
  818. hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  819. return;
  820. #endif
  821. if (cfs_rq->nr_running > 1 || !sched_feat(WAKEUP_PREEMPT))
  822. check_preempt_tick(cfs_rq, curr);
  823. }
  824. /**************************************************
  825. * CFS operations on tasks:
  826. */
  827. #ifdef CONFIG_SCHED_HRTICK
  828. static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  829. {
  830. struct sched_entity *se = &p->se;
  831. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  832. WARN_ON(task_rq(p) != rq);
  833. if (hrtick_enabled(rq) && cfs_rq->nr_running > 1) {
  834. u64 slice = sched_slice(cfs_rq, se);
  835. u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
  836. s64 delta = slice - ran;
  837. if (delta < 0) {
  838. if (rq->curr == p)
  839. resched_task(p);
  840. return;
  841. }
  842. /*
  843. * Don't schedule slices shorter than 10000ns, that just
  844. * doesn't make sense. Rely on vruntime for fairness.
  845. */
  846. if (rq->curr != p)
  847. delta = max_t(s64, 10000LL, delta);
  848. hrtick_start(rq, delta);
  849. }
  850. }
  851. /*
  852. * called from enqueue/dequeue and updates the hrtick when the
  853. * current task is from our class and nr_running is low enough
  854. * to matter.
  855. */
  856. static void hrtick_update(struct rq *rq)
  857. {
  858. struct task_struct *curr = rq->curr;
  859. if (curr->sched_class != &fair_sched_class)
  860. return;
  861. if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
  862. hrtick_start_fair(rq, curr);
  863. }
  864. #else /* !CONFIG_SCHED_HRTICK */
  865. static inline void
  866. hrtick_start_fair(struct rq *rq, struct task_struct *p)
  867. {
  868. }
  869. static inline void hrtick_update(struct rq *rq)
  870. {
  871. }
  872. #endif
  873. /*
  874. * The enqueue_task method is called before nr_running is
  875. * increased. Here we update the fair scheduling stats and
  876. * then put the task into the rbtree:
  877. */
  878. static void enqueue_task_fair(struct rq *rq, struct task_struct *p, int wakeup)
  879. {
  880. struct cfs_rq *cfs_rq;
  881. struct sched_entity *se = &p->se;
  882. int flags = 0;
  883. if (wakeup)
  884. flags |= ENQUEUE_WAKEUP;
  885. if (p->state == TASK_WAKING)
  886. flags |= ENQUEUE_MIGRATE;
  887. for_each_sched_entity(se) {
  888. if (se->on_rq)
  889. break;
  890. cfs_rq = cfs_rq_of(se);
  891. enqueue_entity(cfs_rq, se, flags);
  892. flags = ENQUEUE_WAKEUP;
  893. }
  894. hrtick_update(rq);
  895. }
  896. /*
  897. * The dequeue_task method is called before nr_running is
  898. * decreased. We remove the task from the rbtree and
  899. * update the fair scheduling stats:
  900. */
  901. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int sleep)
  902. {
  903. struct cfs_rq *cfs_rq;
  904. struct sched_entity *se = &p->se;
  905. for_each_sched_entity(se) {
  906. cfs_rq = cfs_rq_of(se);
  907. dequeue_entity(cfs_rq, se, sleep);
  908. /* Don't dequeue parent if it has other entities besides us */
  909. if (cfs_rq->load.weight)
  910. break;
  911. sleep = 1;
  912. }
  913. hrtick_update(rq);
  914. }
  915. /*
  916. * sched_yield() support is very simple - we dequeue and enqueue.
  917. *
  918. * If compat_yield is turned on then we requeue to the end of the tree.
  919. */
  920. static void yield_task_fair(struct rq *rq)
  921. {
  922. struct task_struct *curr = rq->curr;
  923. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  924. struct sched_entity *rightmost, *se = &curr->se;
  925. /*
  926. * Are we the only task in the tree?
  927. */
  928. if (unlikely(cfs_rq->nr_running == 1))
  929. return;
  930. clear_buddies(cfs_rq, se);
  931. if (likely(!sysctl_sched_compat_yield) && curr->policy != SCHED_BATCH) {
  932. update_rq_clock(rq);
  933. /*
  934. * Update run-time statistics of the 'current'.
  935. */
  936. update_curr(cfs_rq);
  937. return;
  938. }
  939. /*
  940. * Find the rightmost entry in the rbtree:
  941. */
  942. rightmost = __pick_last_entity(cfs_rq);
  943. /*
  944. * Already in the rightmost position?
  945. */
  946. if (unlikely(!rightmost || entity_before(rightmost, se)))
  947. return;
  948. /*
  949. * Minimally necessary key value to be last in the tree:
  950. * Upon rescheduling, sched_class::put_prev_task() will place
  951. * 'current' within the tree based on its new key value.
  952. */
  953. se->vruntime = rightmost->vruntime + 1;
  954. }
  955. #ifdef CONFIG_SMP
  956. static void task_waking_fair(struct rq *rq, struct task_struct *p)
  957. {
  958. struct sched_entity *se = &p->se;
  959. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  960. se->vruntime -= cfs_rq->min_vruntime;
  961. }
  962. #ifdef CONFIG_FAIR_GROUP_SCHED
  963. /*
  964. * effective_load() calculates the load change as seen from the root_task_group
  965. *
  966. * Adding load to a group doesn't make a group heavier, but can cause movement
  967. * of group shares between cpus. Assuming the shares were perfectly aligned one
  968. * can calculate the shift in shares.
  969. *
  970. * The problem is that perfectly aligning the shares is rather expensive, hence
  971. * we try to avoid doing that too often - see update_shares(), which ratelimits
  972. * this change.
  973. *
  974. * We compensate this by not only taking the current delta into account, but
  975. * also considering the delta between when the shares were last adjusted and
  976. * now.
  977. *
  978. * We still saw a performance dip, some tracing learned us that between
  979. * cgroup:/ and cgroup:/foo balancing the number of affine wakeups increased
  980. * significantly. Therefore try to bias the error in direction of failing
  981. * the affine wakeup.
  982. *
  983. */
  984. static long effective_load(struct task_group *tg, int cpu,
  985. long wl, long wg)
  986. {
  987. struct sched_entity *se = tg->se[cpu];
  988. if (!tg->parent)
  989. return wl;
  990. /*
  991. * By not taking the decrease of shares on the other cpu into
  992. * account our error leans towards reducing the affine wakeups.
  993. */
  994. if (!wl && sched_feat(ASYM_EFF_LOAD))
  995. return wl;
  996. for_each_sched_entity(se) {
  997. long S, rw, s, a, b;
  998. long more_w;
  999. /*
  1000. * Instead of using this increment, also add the difference
  1001. * between when the shares were last updated and now.
  1002. */
  1003. more_w = se->my_q->load.weight - se->my_q->rq_weight;
  1004. wl += more_w;
  1005. wg += more_w;
  1006. S = se->my_q->tg->shares;
  1007. s = se->my_q->shares;
  1008. rw = se->my_q->rq_weight;
  1009. a = S*(rw + wl);
  1010. b = S*rw + s*wg;
  1011. wl = s*(a-b);
  1012. if (likely(b))
  1013. wl /= b;
  1014. /*
  1015. * Assume the group is already running and will
  1016. * thus already be accounted for in the weight.
  1017. *
  1018. * That is, moving shares between CPUs, does not
  1019. * alter the group weight.
  1020. */
  1021. wg = 0;
  1022. }
  1023. return wl;
  1024. }
  1025. #else
  1026. static inline unsigned long effective_load(struct task_group *tg, int cpu,
  1027. unsigned long wl, unsigned long wg)
  1028. {
  1029. return wl;
  1030. }
  1031. #endif
  1032. static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
  1033. {
  1034. struct task_struct *curr = current;
  1035. unsigned long this_load, load;
  1036. int idx, this_cpu, prev_cpu;
  1037. unsigned long tl_per_task;
  1038. unsigned int imbalance;
  1039. struct task_group *tg;
  1040. unsigned long weight;
  1041. int balanced;
  1042. idx = sd->wake_idx;
  1043. this_cpu = smp_processor_id();
  1044. prev_cpu = task_cpu(p);
  1045. load = source_load(prev_cpu, idx);
  1046. this_load = target_load(this_cpu, idx);
  1047. if (sync) {
  1048. if (sched_feat(SYNC_LESS) &&
  1049. (curr->se.avg_overlap > sysctl_sched_migration_cost ||
  1050. p->se.avg_overlap > sysctl_sched_migration_cost))
  1051. sync = 0;
  1052. } else {
  1053. if (sched_feat(SYNC_MORE) &&
  1054. (curr->se.avg_overlap < sysctl_sched_migration_cost &&
  1055. p->se.avg_overlap < sysctl_sched_migration_cost))
  1056. sync = 1;
  1057. }
  1058. /*
  1059. * If sync wakeup then subtract the (maximum possible)
  1060. * effect of the currently running task from the load
  1061. * of the current CPU:
  1062. */
  1063. if (sync) {
  1064. tg = task_group(current);
  1065. weight = current->se.load.weight;
  1066. this_load += effective_load(tg, this_cpu, -weight, -weight);
  1067. load += effective_load(tg, prev_cpu, 0, -weight);
  1068. }
  1069. tg = task_group(p);
  1070. weight = p->se.load.weight;
  1071. imbalance = 100 + (sd->imbalance_pct - 100) / 2;
  1072. /*
  1073. * In low-load situations, where prev_cpu is idle and this_cpu is idle
  1074. * due to the sync cause above having dropped this_load to 0, we'll
  1075. * always have an imbalance, but there's really nothing you can do
  1076. * about that, so that's good too.
  1077. *
  1078. * Otherwise check if either cpus are near enough in load to allow this
  1079. * task to be woken on this_cpu.
  1080. */
  1081. balanced = !this_load ||
  1082. 100*(this_load + effective_load(tg, this_cpu, weight, weight)) <=
  1083. imbalance*(load + effective_load(tg, prev_cpu, 0, weight));
  1084. /*
  1085. * If the currently running task will sleep within
  1086. * a reasonable amount of time then attract this newly
  1087. * woken task:
  1088. */
  1089. if (sync && balanced)
  1090. return 1;
  1091. schedstat_inc(p, se.nr_wakeups_affine_attempts);
  1092. tl_per_task = cpu_avg_load_per_task(this_cpu);
  1093. if (balanced ||
  1094. (this_load <= load &&
  1095. this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
  1096. /*
  1097. * This domain has SD_WAKE_AFFINE and
  1098. * p is cache cold in this domain, and
  1099. * there is no bad imbalance.
  1100. */
  1101. schedstat_inc(sd, ttwu_move_affine);
  1102. schedstat_inc(p, se.nr_wakeups_affine);
  1103. return 1;
  1104. }
  1105. return 0;
  1106. }
  1107. /*
  1108. * find_idlest_group finds and returns the least busy CPU group within the
  1109. * domain.
  1110. */
  1111. static struct sched_group *
  1112. find_idlest_group(struct sched_domain *sd, struct task_struct *p,
  1113. int this_cpu, int load_idx)
  1114. {
  1115. struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
  1116. unsigned long min_load = ULONG_MAX, this_load = 0;
  1117. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  1118. do {
  1119. unsigned long load, avg_load;
  1120. int local_group;
  1121. int i;
  1122. /* Skip over this group if it has no CPUs allowed */
  1123. if (!cpumask_intersects(sched_group_cpus(group),
  1124. &p->cpus_allowed))
  1125. continue;
  1126. local_group = cpumask_test_cpu(this_cpu,
  1127. sched_group_cpus(group));
  1128. /* Tally up the load of all CPUs in the group */
  1129. avg_load = 0;
  1130. for_each_cpu(i, sched_group_cpus(group)) {
  1131. /* Bias balancing toward cpus of our domain */
  1132. if (local_group)
  1133. load = source_load(i, load_idx);
  1134. else
  1135. load = target_load(i, load_idx);
  1136. avg_load += load;
  1137. }
  1138. /* Adjust by relative CPU power of the group */
  1139. avg_load = (avg_load * SCHED_LOAD_SCALE) / group->cpu_power;
  1140. if (local_group) {
  1141. this_load = avg_load;
  1142. this = group;
  1143. } else if (avg_load < min_load) {
  1144. min_load = avg_load;
  1145. idlest = group;
  1146. }
  1147. } while (group = group->next, group != sd->groups);
  1148. if (!idlest || 100*this_load < imbalance*min_load)
  1149. return NULL;
  1150. return idlest;
  1151. }
  1152. /*
  1153. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  1154. */
  1155. static int
  1156. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  1157. {
  1158. unsigned long load, min_load = ULONG_MAX;
  1159. int idlest = -1;
  1160. int i;
  1161. /* Traverse only the allowed CPUs */
  1162. for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
  1163. load = weighted_cpuload(i);
  1164. if (load < min_load || (load == min_load && i == this_cpu)) {
  1165. min_load = load;
  1166. idlest = i;
  1167. }
  1168. }
  1169. return idlest;
  1170. }
  1171. /*
  1172. * Try and locate an idle CPU in the sched_domain.
  1173. */
  1174. static int
  1175. select_idle_sibling(struct task_struct *p, struct sched_domain *sd, int target)
  1176. {
  1177. int cpu = smp_processor_id();
  1178. int prev_cpu = task_cpu(p);
  1179. int i;
  1180. /*
  1181. * If this domain spans both cpu and prev_cpu (see the SD_WAKE_AFFINE
  1182. * test in select_task_rq_fair) and the prev_cpu is idle then that's
  1183. * always a better target than the current cpu.
  1184. */
  1185. if (target == cpu && !cpu_rq(prev_cpu)->cfs.nr_running)
  1186. return prev_cpu;
  1187. /*
  1188. * Otherwise, iterate the domain and find an elegible idle cpu.
  1189. */
  1190. for_each_cpu_and(i, sched_domain_span(sd), &p->cpus_allowed) {
  1191. if (!cpu_rq(i)->cfs.nr_running) {
  1192. target = i;
  1193. break;
  1194. }
  1195. }
  1196. return target;
  1197. }
  1198. /*
  1199. * sched_balance_self: balance the current task (running on cpu) in domains
  1200. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  1201. * SD_BALANCE_EXEC.
  1202. *
  1203. * Balance, ie. select the least loaded group.
  1204. *
  1205. * Returns the target CPU number, or the same CPU if no balancing is needed.
  1206. *
  1207. * preempt must be disabled.
  1208. */
  1209. static int select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
  1210. {
  1211. struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
  1212. int cpu = smp_processor_id();
  1213. int prev_cpu = task_cpu(p);
  1214. int new_cpu = cpu;
  1215. int want_affine = 0;
  1216. int want_sd = 1;
  1217. int sync = wake_flags & WF_SYNC;
  1218. if (sd_flag & SD_BALANCE_WAKE) {
  1219. if (sched_feat(AFFINE_WAKEUPS) &&
  1220. cpumask_test_cpu(cpu, &p->cpus_allowed))
  1221. want_affine = 1;
  1222. new_cpu = prev_cpu;
  1223. }
  1224. for_each_domain(cpu, tmp) {
  1225. if (!(tmp->flags & SD_LOAD_BALANCE))
  1226. continue;
  1227. /*
  1228. * If power savings logic is enabled for a domain, see if we
  1229. * are not overloaded, if so, don't balance wider.
  1230. */
  1231. if (tmp->flags & (SD_POWERSAVINGS_BALANCE|SD_PREFER_LOCAL)) {
  1232. unsigned long power = 0;
  1233. unsigned long nr_running = 0;
  1234. unsigned long capacity;
  1235. int i;
  1236. for_each_cpu(i, sched_domain_span(tmp)) {
  1237. power += power_of(i);
  1238. nr_running += cpu_rq(i)->cfs.nr_running;
  1239. }
  1240. capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
  1241. if (tmp->flags & SD_POWERSAVINGS_BALANCE)
  1242. nr_running /= 2;
  1243. if (nr_running < capacity)
  1244. want_sd = 0;
  1245. }
  1246. /*
  1247. * While iterating the domains looking for a spanning
  1248. * WAKE_AFFINE domain, adjust the affine target to any idle cpu
  1249. * in cache sharing domains along the way.
  1250. */
  1251. if (want_affine) {
  1252. int target = -1;
  1253. /*
  1254. * If both cpu and prev_cpu are part of this domain,
  1255. * cpu is a valid SD_WAKE_AFFINE target.
  1256. */
  1257. if (cpumask_test_cpu(prev_cpu, sched_domain_span(tmp)))
  1258. target = cpu;
  1259. /*
  1260. * If there's an idle sibling in this domain, make that
  1261. * the wake_affine target instead of the current cpu.
  1262. */
  1263. if (tmp->flags & SD_PREFER_SIBLING)
  1264. target = select_idle_sibling(p, tmp, target);
  1265. if (target >= 0) {
  1266. if (tmp->flags & SD_WAKE_AFFINE) {
  1267. affine_sd = tmp;
  1268. want_affine = 0;
  1269. }
  1270. cpu = target;
  1271. }
  1272. }
  1273. if (!want_sd && !want_affine)
  1274. break;
  1275. if (!(tmp->flags & sd_flag))
  1276. continue;
  1277. if (want_sd)
  1278. sd = tmp;
  1279. }
  1280. if (sched_feat(LB_SHARES_UPDATE)) {
  1281. /*
  1282. * Pick the largest domain to update shares over
  1283. */
  1284. tmp = sd;
  1285. if (affine_sd && (!tmp ||
  1286. cpumask_weight(sched_domain_span(affine_sd)) >
  1287. cpumask_weight(sched_domain_span(sd))))
  1288. tmp = affine_sd;
  1289. if (tmp)
  1290. update_shares(tmp);
  1291. }
  1292. if (affine_sd && wake_affine(affine_sd, p, sync))
  1293. return cpu;
  1294. while (sd) {
  1295. int load_idx = sd->forkexec_idx;
  1296. struct sched_group *group;
  1297. int weight;
  1298. if (!(sd->flags & sd_flag)) {
  1299. sd = sd->child;
  1300. continue;
  1301. }
  1302. if (sd_flag & SD_BALANCE_WAKE)
  1303. load_idx = sd->wake_idx;
  1304. group = find_idlest_group(sd, p, cpu, load_idx);
  1305. if (!group) {
  1306. sd = sd->child;
  1307. continue;
  1308. }
  1309. new_cpu = find_idlest_cpu(group, p, cpu);
  1310. if (new_cpu == -1 || new_cpu == cpu) {
  1311. /* Now try balancing at a lower domain level of cpu */
  1312. sd = sd->child;
  1313. continue;
  1314. }
  1315. /* Now try balancing at a lower domain level of new_cpu */
  1316. cpu = new_cpu;
  1317. weight = cpumask_weight(sched_domain_span(sd));
  1318. sd = NULL;
  1319. for_each_domain(cpu, tmp) {
  1320. if (weight <= cpumask_weight(sched_domain_span(tmp)))
  1321. break;
  1322. if (tmp->flags & sd_flag)
  1323. sd = tmp;
  1324. }
  1325. /* while loop will break here if sd == NULL */
  1326. }
  1327. return new_cpu;
  1328. }
  1329. #endif /* CONFIG_SMP */
  1330. /*
  1331. * Adaptive granularity
  1332. *
  1333. * se->avg_wakeup gives the average time a task runs until it does a wakeup,
  1334. * with the limit of wakeup_gran -- when it never does a wakeup.
  1335. *
  1336. * So the smaller avg_wakeup is the faster we want this task to preempt,
  1337. * but we don't want to treat the preemptee unfairly and therefore allow it
  1338. * to run for at least the amount of time we'd like to run.
  1339. *
  1340. * NOTE: we use 2*avg_wakeup to increase the probability of actually doing one
  1341. *
  1342. * NOTE: we use *nr_running to scale with load, this nicely matches the
  1343. * degrading latency on load.
  1344. */
  1345. static unsigned long
  1346. adaptive_gran(struct sched_entity *curr, struct sched_entity *se)
  1347. {
  1348. u64 this_run = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  1349. u64 expected_wakeup = 2*se->avg_wakeup * cfs_rq_of(se)->nr_running;
  1350. u64 gran = 0;
  1351. if (this_run < expected_wakeup)
  1352. gran = expected_wakeup - this_run;
  1353. return min_t(s64, gran, sysctl_sched_wakeup_granularity);
  1354. }
  1355. static unsigned long
  1356. wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
  1357. {
  1358. unsigned long gran = sysctl_sched_wakeup_granularity;
  1359. if (cfs_rq_of(curr)->curr && sched_feat(ADAPTIVE_GRAN))
  1360. gran = adaptive_gran(curr, se);
  1361. /*
  1362. * Since its curr running now, convert the gran from real-time
  1363. * to virtual-time in his units.
  1364. */
  1365. if (sched_feat(ASYM_GRAN)) {
  1366. /*
  1367. * By using 'se' instead of 'curr' we penalize light tasks, so
  1368. * they get preempted easier. That is, if 'se' < 'curr' then
  1369. * the resulting gran will be larger, therefore penalizing the
  1370. * lighter, if otoh 'se' > 'curr' then the resulting gran will
  1371. * be smaller, again penalizing the lighter task.
  1372. *
  1373. * This is especially important for buddies when the leftmost
  1374. * task is higher priority than the buddy.
  1375. */
  1376. if (unlikely(se->load.weight != NICE_0_LOAD))
  1377. gran = calc_delta_fair(gran, se);
  1378. } else {
  1379. if (unlikely(curr->load.weight != NICE_0_LOAD))
  1380. gran = calc_delta_fair(gran, curr);
  1381. }
  1382. return gran;
  1383. }
  1384. /*
  1385. * Should 'se' preempt 'curr'.
  1386. *
  1387. * |s1
  1388. * |s2
  1389. * |s3
  1390. * g
  1391. * |<--->|c
  1392. *
  1393. * w(c, s1) = -1
  1394. * w(c, s2) = 0
  1395. * w(c, s3) = 1
  1396. *
  1397. */
  1398. static int
  1399. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
  1400. {
  1401. s64 gran, vdiff = curr->vruntime - se->vruntime;
  1402. if (vdiff <= 0)
  1403. return -1;
  1404. gran = wakeup_gran(curr, se);
  1405. if (vdiff > gran)
  1406. return 1;
  1407. return 0;
  1408. }
  1409. static void set_last_buddy(struct sched_entity *se)
  1410. {
  1411. if (likely(task_of(se)->policy != SCHED_IDLE)) {
  1412. for_each_sched_entity(se)
  1413. cfs_rq_of(se)->last = se;
  1414. }
  1415. }
  1416. static void set_next_buddy(struct sched_entity *se)
  1417. {
  1418. if (likely(task_of(se)->policy != SCHED_IDLE)) {
  1419. for_each_sched_entity(se)
  1420. cfs_rq_of(se)->next = se;
  1421. }
  1422. }
  1423. /*
  1424. * Preempt the current task with a newly woken task if needed:
  1425. */
  1426. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  1427. {
  1428. struct task_struct *curr = rq->curr;
  1429. struct sched_entity *se = &curr->se, *pse = &p->se;
  1430. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  1431. int sync = wake_flags & WF_SYNC;
  1432. int scale = cfs_rq->nr_running >= sched_nr_latency;
  1433. if (unlikely(rt_prio(p->prio)))
  1434. goto preempt;
  1435. if (unlikely(p->sched_class != &fair_sched_class))
  1436. return;
  1437. if (unlikely(se == pse))
  1438. return;
  1439. if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK))
  1440. set_next_buddy(pse);
  1441. /*
  1442. * We can come here with TIF_NEED_RESCHED already set from new task
  1443. * wake up path.
  1444. */
  1445. if (test_tsk_need_resched(curr))
  1446. return;
  1447. /*
  1448. * Batch and idle tasks do not preempt (their preemption is driven by
  1449. * the tick):
  1450. */
  1451. if (unlikely(p->policy != SCHED_NORMAL))
  1452. return;
  1453. /* Idle tasks are by definition preempted by everybody. */
  1454. if (unlikely(curr->policy == SCHED_IDLE))
  1455. goto preempt;
  1456. if (sched_feat(WAKEUP_SYNC) && sync)
  1457. goto preempt;
  1458. if (sched_feat(WAKEUP_OVERLAP) &&
  1459. se->avg_overlap < sysctl_sched_migration_cost &&
  1460. pse->avg_overlap < sysctl_sched_migration_cost)
  1461. goto preempt;
  1462. if (!sched_feat(WAKEUP_PREEMPT))
  1463. return;
  1464. update_curr(cfs_rq);
  1465. find_matching_se(&se, &pse);
  1466. BUG_ON(!pse);
  1467. if (wakeup_preempt_entity(se, pse) == 1)
  1468. goto preempt;
  1469. return;
  1470. preempt:
  1471. resched_task(curr);
  1472. /*
  1473. * Only set the backward buddy when the current task is still
  1474. * on the rq. This can happen when a wakeup gets interleaved
  1475. * with schedule on the ->pre_schedule() or idle_balance()
  1476. * point, either of which can * drop the rq lock.
  1477. *
  1478. * Also, during early boot the idle thread is in the fair class,
  1479. * for obvious reasons its a bad idea to schedule back to it.
  1480. */
  1481. if (unlikely(!se->on_rq || curr == rq->idle))
  1482. return;
  1483. if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
  1484. set_last_buddy(se);
  1485. }
  1486. static struct task_struct *pick_next_task_fair(struct rq *rq)
  1487. {
  1488. struct task_struct *p;
  1489. struct cfs_rq *cfs_rq = &rq->cfs;
  1490. struct sched_entity *se;
  1491. if (!cfs_rq->nr_running)
  1492. return NULL;
  1493. do {
  1494. se = pick_next_entity(cfs_rq);
  1495. set_next_entity(cfs_rq, se);
  1496. cfs_rq = group_cfs_rq(se);
  1497. } while (cfs_rq);
  1498. p = task_of(se);
  1499. hrtick_start_fair(rq, p);
  1500. return p;
  1501. }
  1502. /*
  1503. * Account for a descheduled task:
  1504. */
  1505. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  1506. {
  1507. struct sched_entity *se = &prev->se;
  1508. struct cfs_rq *cfs_rq;
  1509. for_each_sched_entity(se) {
  1510. cfs_rq = cfs_rq_of(se);
  1511. put_prev_entity(cfs_rq, se);
  1512. }
  1513. }
  1514. #ifdef CONFIG_SMP
  1515. /**************************************************
  1516. * Fair scheduling class load-balancing methods:
  1517. */
  1518. /*
  1519. * pull_task - move a task from a remote runqueue to the local runqueue.
  1520. * Both runqueues must be locked.
  1521. */
  1522. static void pull_task(struct rq *src_rq, struct task_struct *p,
  1523. struct rq *this_rq, int this_cpu)
  1524. {
  1525. deactivate_task(src_rq, p, 0);
  1526. set_task_cpu(p, this_cpu);
  1527. activate_task(this_rq, p, 0);
  1528. check_preempt_curr(this_rq, p, 0);
  1529. }
  1530. /*
  1531. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  1532. */
  1533. static
  1534. int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
  1535. struct sched_domain *sd, enum cpu_idle_type idle,
  1536. int *all_pinned)
  1537. {
  1538. int tsk_cache_hot = 0;
  1539. /*
  1540. * We do not migrate tasks that are:
  1541. * 1) running (obviously), or
  1542. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  1543. * 3) are cache-hot on their current CPU.
  1544. */
  1545. if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
  1546. schedstat_inc(p, se.nr_failed_migrations_affine);
  1547. return 0;
  1548. }
  1549. *all_pinned = 0;
  1550. if (task_running(rq, p)) {
  1551. schedstat_inc(p, se.nr_failed_migrations_running);
  1552. return 0;
  1553. }
  1554. /*
  1555. * Aggressive migration if:
  1556. * 1) task is cache cold, or
  1557. * 2) too many balance attempts have failed.
  1558. */
  1559. tsk_cache_hot = task_hot(p, rq->clock, sd);
  1560. if (!tsk_cache_hot ||
  1561. sd->nr_balance_failed > sd->cache_nice_tries) {
  1562. #ifdef CONFIG_SCHEDSTATS
  1563. if (tsk_cache_hot) {
  1564. schedstat_inc(sd, lb_hot_gained[idle]);
  1565. schedstat_inc(p, se.nr_forced_migrations);
  1566. }
  1567. #endif
  1568. return 1;
  1569. }
  1570. if (tsk_cache_hot) {
  1571. schedstat_inc(p, se.nr_failed_migrations_hot);
  1572. return 0;
  1573. }
  1574. return 1;
  1575. }
  1576. /*
  1577. * move_one_task tries to move exactly one task from busiest to this_rq, as
  1578. * part of active balancing operations within "domain".
  1579. * Returns 1 if successful and 0 otherwise.
  1580. *
  1581. * Called with both runqueues locked.
  1582. */
  1583. static int
  1584. move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1585. struct sched_domain *sd, enum cpu_idle_type idle)
  1586. {
  1587. struct task_struct *p, *n;
  1588. struct cfs_rq *cfs_rq;
  1589. int pinned = 0;
  1590. for_each_leaf_cfs_rq(busiest, cfs_rq) {
  1591. list_for_each_entry_safe(p, n, &cfs_rq->tasks, se.group_node) {
  1592. if (!can_migrate_task(p, busiest, this_cpu,
  1593. sd, idle, &pinned))
  1594. continue;
  1595. pull_task(busiest, p, this_rq, this_cpu);
  1596. /*
  1597. * Right now, this is only the second place pull_task()
  1598. * is called, so we can safely collect pull_task()
  1599. * stats here rather than inside pull_task().
  1600. */
  1601. schedstat_inc(sd, lb_gained[idle]);
  1602. return 1;
  1603. }
  1604. }
  1605. return 0;
  1606. }
  1607. static unsigned long
  1608. balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1609. unsigned long max_load_move, struct sched_domain *sd,
  1610. enum cpu_idle_type idle, int *all_pinned,
  1611. int *this_best_prio, struct cfs_rq *busiest_cfs_rq)
  1612. {
  1613. int loops = 0, pulled = 0, pinned = 0;
  1614. long rem_load_move = max_load_move;
  1615. struct task_struct *p, *n;
  1616. if (max_load_move == 0)
  1617. goto out;
  1618. pinned = 1;
  1619. list_for_each_entry_safe(p, n, &busiest_cfs_rq->tasks, se.group_node) {
  1620. if (loops++ > sysctl_sched_nr_migrate)
  1621. break;
  1622. if ((p->se.load.weight >> 1) > rem_load_move ||
  1623. !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned))
  1624. continue;
  1625. pull_task(busiest, p, this_rq, this_cpu);
  1626. pulled++;
  1627. rem_load_move -= p->se.load.weight;
  1628. #ifdef CONFIG_PREEMPT
  1629. /*
  1630. * NEWIDLE balancing is a source of latency, so preemptible
  1631. * kernels will stop after the first task is pulled to minimize
  1632. * the critical section.
  1633. */
  1634. if (idle == CPU_NEWLY_IDLE)
  1635. break;
  1636. #endif
  1637. /*
  1638. * We only want to steal up to the prescribed amount of
  1639. * weighted load.
  1640. */
  1641. if (rem_load_move <= 0)
  1642. break;
  1643. if (p->prio < *this_best_prio)
  1644. *this_best_prio = p->prio;
  1645. }
  1646. out:
  1647. /*
  1648. * Right now, this is one of only two places pull_task() is called,
  1649. * so we can safely collect pull_task() stats here rather than
  1650. * inside pull_task().
  1651. */
  1652. schedstat_add(sd, lb_gained[idle], pulled);
  1653. if (all_pinned)
  1654. *all_pinned = pinned;
  1655. return max_load_move - rem_load_move;
  1656. }
  1657. #ifdef CONFIG_FAIR_GROUP_SCHED
  1658. static unsigned long
  1659. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1660. unsigned long max_load_move,
  1661. struct sched_domain *sd, enum cpu_idle_type idle,
  1662. int *all_pinned, int *this_best_prio)
  1663. {
  1664. long rem_load_move = max_load_move;
  1665. int busiest_cpu = cpu_of(busiest);
  1666. struct task_group *tg;
  1667. rcu_read_lock();
  1668. update_h_load(busiest_cpu);
  1669. list_for_each_entry_rcu(tg, &task_groups, list) {
  1670. struct cfs_rq *busiest_cfs_rq = tg->cfs_rq[busiest_cpu];
  1671. unsigned long busiest_h_load = busiest_cfs_rq->h_load;
  1672. unsigned long busiest_weight = busiest_cfs_rq->load.weight;
  1673. u64 rem_load, moved_load;
  1674. /*
  1675. * empty group
  1676. */
  1677. if (!busiest_cfs_rq->task_weight)
  1678. continue;
  1679. rem_load = (u64)rem_load_move * busiest_weight;
  1680. rem_load = div_u64(rem_load, busiest_h_load + 1);
  1681. moved_load = balance_tasks(this_rq, this_cpu, busiest,
  1682. rem_load, sd, idle, all_pinned, this_best_prio,
  1683. busiest_cfs_rq);
  1684. if (!moved_load)
  1685. continue;
  1686. moved_load *= busiest_h_load;
  1687. moved_load = div_u64(moved_load, busiest_weight + 1);
  1688. rem_load_move -= moved_load;
  1689. if (rem_load_move < 0)
  1690. break;
  1691. }
  1692. rcu_read_unlock();
  1693. return max_load_move - rem_load_move;
  1694. }
  1695. #else
  1696. static unsigned long
  1697. load_balance_fair(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1698. unsigned long max_load_move,
  1699. struct sched_domain *sd, enum cpu_idle_type idle,
  1700. int *all_pinned, int *this_best_prio)
  1701. {
  1702. return balance_tasks(this_rq, this_cpu, busiest,
  1703. max_load_move, sd, idle, all_pinned,
  1704. this_best_prio, &busiest->cfs);
  1705. }
  1706. #endif
  1707. /*
  1708. * move_tasks tries to move up to max_load_move weighted load from busiest to
  1709. * this_rq, as part of a balancing operation within domain "sd".
  1710. * Returns 1 if successful and 0 otherwise.
  1711. *
  1712. * Called with both runqueues locked.
  1713. */
  1714. static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1715. unsigned long max_load_move,
  1716. struct sched_domain *sd, enum cpu_idle_type idle,
  1717. int *all_pinned)
  1718. {
  1719. unsigned long total_load_moved = 0, load_moved;
  1720. int this_best_prio = this_rq->curr->prio;
  1721. do {
  1722. load_moved = load_balance_fair(this_rq, this_cpu, busiest,
  1723. max_load_move - total_load_moved,
  1724. sd, idle, all_pinned, &this_best_prio);
  1725. total_load_moved += load_moved;
  1726. #ifdef CONFIG_PREEMPT
  1727. /*
  1728. * NEWIDLE balancing is a source of latency, so preemptible
  1729. * kernels will stop after the first task is pulled to minimize
  1730. * the critical section.
  1731. */
  1732. if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
  1733. break;
  1734. if (raw_spin_is_contended(&this_rq->lock) ||
  1735. raw_spin_is_contended(&busiest->lock))
  1736. break;
  1737. #endif
  1738. } while (load_moved && max_load_move > total_load_moved);
  1739. return total_load_moved > 0;
  1740. }
  1741. /********** Helpers for find_busiest_group ************************/
  1742. /*
  1743. * sd_lb_stats - Structure to store the statistics of a sched_domain
  1744. * during load balancing.
  1745. */
  1746. struct sd_lb_stats {
  1747. struct sched_group *busiest; /* Busiest group in this sd */
  1748. struct sched_group *this; /* Local group in this sd */
  1749. unsigned long total_load; /* Total load of all groups in sd */
  1750. unsigned long total_pwr; /* Total power of all groups in sd */
  1751. unsigned long avg_load; /* Average load across all groups in sd */
  1752. /** Statistics of this group */
  1753. unsigned long this_load;
  1754. unsigned long this_load_per_task;
  1755. unsigned long this_nr_running;
  1756. /* Statistics of the busiest group */
  1757. unsigned long max_load;
  1758. unsigned long busiest_load_per_task;
  1759. unsigned long busiest_nr_running;
  1760. int group_imb; /* Is there imbalance in this sd */
  1761. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  1762. int power_savings_balance; /* Is powersave balance needed for this sd */
  1763. struct sched_group *group_min; /* Least loaded group in sd */
  1764. struct sched_group *group_leader; /* Group which relieves group_min */
  1765. unsigned long min_load_per_task; /* load_per_task in group_min */
  1766. unsigned long leader_nr_running; /* Nr running of group_leader */
  1767. unsigned long min_nr_running; /* Nr running of group_min */
  1768. #endif
  1769. };
  1770. /*
  1771. * sg_lb_stats - stats of a sched_group required for load_balancing
  1772. */
  1773. struct sg_lb_stats {
  1774. unsigned long avg_load; /*Avg load across the CPUs of the group */
  1775. unsigned long group_load; /* Total load over the CPUs of the group */
  1776. unsigned long sum_nr_running; /* Nr tasks running in the group */
  1777. unsigned long sum_weighted_load; /* Weighted load of group's tasks */
  1778. unsigned long group_capacity;
  1779. int group_imb; /* Is there an imbalance in the group ? */
  1780. };
  1781. /**
  1782. * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
  1783. * @group: The group whose first cpu is to be returned.
  1784. */
  1785. static inline unsigned int group_first_cpu(struct sched_group *group)
  1786. {
  1787. return cpumask_first(sched_group_cpus(group));
  1788. }
  1789. /**
  1790. * get_sd_load_idx - Obtain the load index for a given sched domain.
  1791. * @sd: The sched_domain whose load_idx is to be obtained.
  1792. * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
  1793. */
  1794. static inline int get_sd_load_idx(struct sched_domain *sd,
  1795. enum cpu_idle_type idle)
  1796. {
  1797. int load_idx;
  1798. switch (idle) {
  1799. case CPU_NOT_IDLE:
  1800. load_idx = sd->busy_idx;
  1801. break;
  1802. case CPU_NEWLY_IDLE:
  1803. load_idx = sd->newidle_idx;
  1804. break;
  1805. default:
  1806. load_idx = sd->idle_idx;
  1807. break;
  1808. }
  1809. return load_idx;
  1810. }
  1811. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  1812. /**
  1813. * init_sd_power_savings_stats - Initialize power savings statistics for
  1814. * the given sched_domain, during load balancing.
  1815. *
  1816. * @sd: Sched domain whose power-savings statistics are to be initialized.
  1817. * @sds: Variable containing the statistics for sd.
  1818. * @idle: Idle status of the CPU at which we're performing load-balancing.
  1819. */
  1820. static inline void init_sd_power_savings_stats(struct sched_domain *sd,
  1821. struct sd_lb_stats *sds, enum cpu_idle_type idle)
  1822. {
  1823. /*
  1824. * Busy processors will not participate in power savings
  1825. * balance.
  1826. */
  1827. if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
  1828. sds->power_savings_balance = 0;
  1829. else {
  1830. sds->power_savings_balance = 1;
  1831. sds->min_nr_running = ULONG_MAX;
  1832. sds->leader_nr_running = 0;
  1833. }
  1834. }
  1835. /**
  1836. * update_sd_power_savings_stats - Update the power saving stats for a
  1837. * sched_domain while performing load balancing.
  1838. *
  1839. * @group: sched_group belonging to the sched_domain under consideration.
  1840. * @sds: Variable containing the statistics of the sched_domain
  1841. * @local_group: Does group contain the CPU for which we're performing
  1842. * load balancing ?
  1843. * @sgs: Variable containing the statistics of the group.
  1844. */
  1845. static inline void update_sd_power_savings_stats(struct sched_group *group,
  1846. struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
  1847. {
  1848. if (!sds->power_savings_balance)
  1849. return;
  1850. /*
  1851. * If the local group is idle or completely loaded
  1852. * no need to do power savings balance at this domain
  1853. */
  1854. if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
  1855. !sds->this_nr_running))
  1856. sds->power_savings_balance = 0;
  1857. /*
  1858. * If a group is already running at full capacity or idle,
  1859. * don't include that group in power savings calculations
  1860. */
  1861. if (!sds->power_savings_balance ||
  1862. sgs->sum_nr_running >= sgs->group_capacity ||
  1863. !sgs->sum_nr_running)
  1864. return;
  1865. /*
  1866. * Calculate the group which has the least non-idle load.
  1867. * This is the group from where we need to pick up the load
  1868. * for saving power
  1869. */
  1870. if ((sgs->sum_nr_running < sds->min_nr_running) ||
  1871. (sgs->sum_nr_running == sds->min_nr_running &&
  1872. group_first_cpu(group) > group_first_cpu(sds->group_min))) {
  1873. sds->group_min = group;
  1874. sds->min_nr_running = sgs->sum_nr_running;
  1875. sds->min_load_per_task = sgs->sum_weighted_load /
  1876. sgs->sum_nr_running;
  1877. }
  1878. /*
  1879. * Calculate the group which is almost near its
  1880. * capacity but still has some space to pick up some load
  1881. * from other group and save more power
  1882. */
  1883. if (sgs->sum_nr_running + 1 > sgs->group_capacity)
  1884. return;
  1885. if (sgs->sum_nr_running > sds->leader_nr_running ||
  1886. (sgs->sum_nr_running == sds->leader_nr_running &&
  1887. group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
  1888. sds->group_leader = group;
  1889. sds->leader_nr_running = sgs->sum_nr_running;
  1890. }
  1891. }
  1892. /**
  1893. * check_power_save_busiest_group - see if there is potential for some power-savings balance
  1894. * @sds: Variable containing the statistics of the sched_domain
  1895. * under consideration.
  1896. * @this_cpu: Cpu at which we're currently performing load-balancing.
  1897. * @imbalance: Variable to store the imbalance.
  1898. *
  1899. * Description:
  1900. * Check if we have potential to perform some power-savings balance.
  1901. * If yes, set the busiest group to be the least loaded group in the
  1902. * sched_domain, so that it's CPUs can be put to idle.
  1903. *
  1904. * Returns 1 if there is potential to perform power-savings balance.
  1905. * Else returns 0.
  1906. */
  1907. static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
  1908. int this_cpu, unsigned long *imbalance)
  1909. {
  1910. if (!sds->power_savings_balance)
  1911. return 0;
  1912. if (sds->this != sds->group_leader ||
  1913. sds->group_leader == sds->group_min)
  1914. return 0;
  1915. *imbalance = sds->min_load_per_task;
  1916. sds->busiest = sds->group_min;
  1917. return 1;
  1918. }
  1919. #else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
  1920. static inline void init_sd_power_savings_stats(struct sched_domain *sd,
  1921. struct sd_lb_stats *sds, enum cpu_idle_type idle)
  1922. {
  1923. return;
  1924. }
  1925. static inline void update_sd_power_savings_stats(struct sched_group *group,
  1926. struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
  1927. {
  1928. return;
  1929. }
  1930. static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
  1931. int this_cpu, unsigned long *imbalance)
  1932. {
  1933. return 0;
  1934. }
  1935. #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
  1936. unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
  1937. {
  1938. return SCHED_LOAD_SCALE;
  1939. }
  1940. unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
  1941. {
  1942. return default_scale_freq_power(sd, cpu);
  1943. }
  1944. unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
  1945. {
  1946. unsigned long weight = cpumask_weight(sched_domain_span(sd));
  1947. unsigned long smt_gain = sd->smt_gain;
  1948. smt_gain /= weight;
  1949. return smt_gain;
  1950. }
  1951. unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
  1952. {
  1953. return default_scale_smt_power(sd, cpu);
  1954. }
  1955. unsigned long scale_rt_power(int cpu)
  1956. {
  1957. struct rq *rq = cpu_rq(cpu);
  1958. u64 total, available;
  1959. sched_avg_update(rq);
  1960. total = sched_avg_period() + (rq->clock - rq->age_stamp);
  1961. available = total - rq->rt_avg;
  1962. if (unlikely((s64)total < SCHED_LOAD_SCALE))
  1963. total = SCHED_LOAD_SCALE;
  1964. total >>= SCHED_LOAD_SHIFT;
  1965. return div_u64(available, total);
  1966. }
  1967. static void update_cpu_power(struct sched_domain *sd, int cpu)
  1968. {
  1969. unsigned long weight = cpumask_weight(sched_domain_span(sd));
  1970. unsigned long power = SCHED_LOAD_SCALE;
  1971. struct sched_group *sdg = sd->groups;
  1972. if (sched_feat(ARCH_POWER))
  1973. power *= arch_scale_freq_power(sd, cpu);
  1974. else
  1975. power *= default_scale_freq_power(sd, cpu);
  1976. power >>= SCHED_LOAD_SHIFT;
  1977. if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
  1978. if (sched_feat(ARCH_POWER))
  1979. power *= arch_scale_smt_power(sd, cpu);
  1980. else
  1981. power *= default_scale_smt_power(sd, cpu);
  1982. power >>= SCHED_LOAD_SHIFT;
  1983. }
  1984. power *= scale_rt_power(cpu);
  1985. power >>= SCHED_LOAD_SHIFT;
  1986. if (!power)
  1987. power = 1;
  1988. sdg->cpu_power = power;
  1989. }
  1990. static void update_group_power(struct sched_domain *sd, int cpu)
  1991. {
  1992. struct sched_domain *child = sd->child;
  1993. struct sched_group *group, *sdg = sd->groups;
  1994. unsigned long power;
  1995. if (!child) {
  1996. update_cpu_power(sd, cpu);
  1997. return;
  1998. }
  1999. power = 0;
  2000. group = child->groups;
  2001. do {
  2002. power += group->cpu_power;
  2003. group = group->next;
  2004. } while (group != child->groups);
  2005. sdg->cpu_power = power;
  2006. }
  2007. /**
  2008. * update_sg_lb_stats - Update sched_group's statistics for load balancing.
  2009. * @sd: The sched_domain whose statistics are to be updated.
  2010. * @group: sched_group whose statistics are to be updated.
  2011. * @this_cpu: Cpu for which load balance is currently performed.
  2012. * @idle: Idle status of this_cpu
  2013. * @load_idx: Load index of sched_domain of this_cpu for load calc.
  2014. * @sd_idle: Idle status of the sched_domain containing group.
  2015. * @local_group: Does group contain this_cpu.
  2016. * @cpus: Set of cpus considered for load balancing.
  2017. * @balance: Should we balance.
  2018. * @sgs: variable to hold the statistics for this group.
  2019. */
  2020. static inline void update_sg_lb_stats(struct sched_domain *sd,
  2021. struct sched_group *group, int this_cpu,
  2022. enum cpu_idle_type idle, int load_idx, int *sd_idle,
  2023. int local_group, const struct cpumask *cpus,
  2024. int *balance, struct sg_lb_stats *sgs)
  2025. {
  2026. unsigned long load, max_cpu_load, min_cpu_load;
  2027. int i;
  2028. unsigned int balance_cpu = -1, first_idle_cpu = 0;
  2029. unsigned long sum_avg_load_per_task;
  2030. unsigned long avg_load_per_task;
  2031. if (local_group) {
  2032. balance_cpu = group_first_cpu(group);
  2033. if (balance_cpu == this_cpu)
  2034. update_group_power(sd, this_cpu);
  2035. }
  2036. /* Tally up the load of all CPUs in the group */
  2037. sum_avg_load_per_task = avg_load_per_task = 0;
  2038. max_cpu_load = 0;
  2039. min_cpu_load = ~0UL;
  2040. for_each_cpu_and(i, sched_group_cpus(group), cpus) {
  2041. struct rq *rq = cpu_rq(i);
  2042. if (*sd_idle && rq->nr_running)
  2043. *sd_idle = 0;
  2044. /* Bias balancing toward cpus of our domain */
  2045. if (local_group) {
  2046. if (idle_cpu(i) && !first_idle_cpu) {
  2047. first_idle_cpu = 1;
  2048. balance_cpu = i;
  2049. }
  2050. load = target_load(i, load_idx);
  2051. } else {
  2052. load = source_load(i, load_idx);
  2053. if (load > max_cpu_load)
  2054. max_cpu_load = load;
  2055. if (min_cpu_load > load)
  2056. min_cpu_load = load;
  2057. }
  2058. sgs->group_load += load;
  2059. sgs->sum_nr_running += rq->nr_running;
  2060. sgs->sum_weighted_load += weighted_cpuload(i);
  2061. sum_avg_load_per_task += cpu_avg_load_per_task(i);
  2062. }
  2063. /*
  2064. * First idle cpu or the first cpu(busiest) in this sched group
  2065. * is eligible for doing load balancing at this and above
  2066. * domains. In the newly idle case, we will allow all the cpu's
  2067. * to do the newly idle load balance.
  2068. */
  2069. if (idle != CPU_NEWLY_IDLE && local_group &&
  2070. balance_cpu != this_cpu && balance) {
  2071. *balance = 0;
  2072. return;
  2073. }
  2074. /* Adjust by relative CPU power of the group */
  2075. sgs->avg_load = (sgs->group_load * SCHED_LOAD_SCALE) / group->cpu_power;
  2076. /*
  2077. * Consider the group unbalanced when the imbalance is larger
  2078. * than the average weight of two tasks.
  2079. *
  2080. * APZ: with cgroup the avg task weight can vary wildly and
  2081. * might not be a suitable number - should we keep a
  2082. * normalized nr_running number somewhere that negates
  2083. * the hierarchy?
  2084. */
  2085. avg_load_per_task = (sum_avg_load_per_task * SCHED_LOAD_SCALE) /
  2086. group->cpu_power;
  2087. if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
  2088. sgs->group_imb = 1;
  2089. sgs->group_capacity =
  2090. DIV_ROUND_CLOSEST(group->cpu_power, SCHED_LOAD_SCALE);
  2091. }
  2092. /**
  2093. * update_sd_lb_stats - Update sched_group's statistics for load balancing.
  2094. * @sd: sched_domain whose statistics are to be updated.
  2095. * @this_cpu: Cpu for which load balance is currently performed.
  2096. * @idle: Idle status of this_cpu
  2097. * @sd_idle: Idle status of the sched_domain containing group.
  2098. * @cpus: Set of cpus considered for load balancing.
  2099. * @balance: Should we balance.
  2100. * @sds: variable to hold the statistics for this sched_domain.
  2101. */
  2102. static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
  2103. enum cpu_idle_type idle, int *sd_idle,
  2104. const struct cpumask *cpus, int *balance,
  2105. struct sd_lb_stats *sds)
  2106. {
  2107. struct sched_domain *child = sd->child;
  2108. struct sched_group *group = sd->groups;
  2109. struct sg_lb_stats sgs;
  2110. int load_idx, prefer_sibling = 0;
  2111. if (child && child->flags & SD_PREFER_SIBLING)
  2112. prefer_sibling = 1;
  2113. init_sd_power_savings_stats(sd, sds, idle);
  2114. load_idx = get_sd_load_idx(sd, idle);
  2115. do {
  2116. int local_group;
  2117. local_group = cpumask_test_cpu(this_cpu,
  2118. sched_group_cpus(group));
  2119. memset(&sgs, 0, sizeof(sgs));
  2120. update_sg_lb_stats(sd, group, this_cpu, idle, load_idx, sd_idle,
  2121. local_group, cpus, balance, &sgs);
  2122. if (local_group && balance && !(*balance))
  2123. return;
  2124. sds->total_load += sgs.group_load;
  2125. sds->total_pwr += group->cpu_power;
  2126. /*
  2127. * In case the child domain prefers tasks go to siblings
  2128. * first, lower the group capacity to one so that we'll try
  2129. * and move all the excess tasks away.
  2130. */
  2131. if (prefer_sibling)
  2132. sgs.group_capacity = min(sgs.group_capacity, 1UL);
  2133. if (local_group) {
  2134. sds->this_load = sgs.avg_load;
  2135. sds->this = group;
  2136. sds->this_nr_running = sgs.sum_nr_running;
  2137. sds->this_load_per_task = sgs.sum_weighted_load;
  2138. } else if (sgs.avg_load > sds->max_load &&
  2139. (sgs.sum_nr_running > sgs.group_capacity ||
  2140. sgs.group_imb)) {
  2141. sds->max_load = sgs.avg_load;
  2142. sds->busiest = group;
  2143. sds->busiest_nr_running = sgs.sum_nr_running;
  2144. sds->busiest_load_per_task = sgs.sum_weighted_load;
  2145. sds->group_imb = sgs.group_imb;
  2146. }
  2147. update_sd_power_savings_stats(group, sds, local_group, &sgs);
  2148. group = group->next;
  2149. } while (group != sd->groups);
  2150. }
  2151. /**
  2152. * fix_small_imbalance - Calculate the minor imbalance that exists
  2153. * amongst the groups of a sched_domain, during
  2154. * load balancing.
  2155. * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
  2156. * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
  2157. * @imbalance: Variable to store the imbalance.
  2158. */
  2159. static inline void fix_small_imbalance(struct sd_lb_stats *sds,
  2160. int this_cpu, unsigned long *imbalance)
  2161. {
  2162. unsigned long tmp, pwr_now = 0, pwr_move = 0;
  2163. unsigned int imbn = 2;
  2164. if (sds->this_nr_running) {
  2165. sds->this_load_per_task /= sds->this_nr_running;
  2166. if (sds->busiest_load_per_task >
  2167. sds->this_load_per_task)
  2168. imbn = 1;
  2169. } else
  2170. sds->this_load_per_task =
  2171. cpu_avg_load_per_task(this_cpu);
  2172. if (sds->max_load - sds->this_load + sds->busiest_load_per_task >=
  2173. sds->busiest_load_per_task * imbn) {
  2174. *imbalance = sds->busiest_load_per_task;
  2175. return;
  2176. }
  2177. /*
  2178. * OK, we don't have enough imbalance to justify moving tasks,
  2179. * however we may be able to increase total CPU power used by
  2180. * moving them.
  2181. */
  2182. pwr_now += sds->busiest->cpu_power *
  2183. min(sds->busiest_load_per_task, sds->max_load);
  2184. pwr_now += sds->this->cpu_power *
  2185. min(sds->this_load_per_task, sds->this_load);
  2186. pwr_now /= SCHED_LOAD_SCALE;
  2187. /* Amount of load we'd subtract */
  2188. tmp = (sds->busiest_load_per_task * SCHED_LOAD_SCALE) /
  2189. sds->busiest->cpu_power;
  2190. if (sds->max_load > tmp)
  2191. pwr_move += sds->busiest->cpu_power *
  2192. min(sds->busiest_load_per_task, sds->max_load - tmp);
  2193. /* Amount of load we'd add */
  2194. if (sds->max_load * sds->busiest->cpu_power <
  2195. sds->busiest_load_per_task * SCHED_LOAD_SCALE)
  2196. tmp = (sds->max_load * sds->busiest->cpu_power) /
  2197. sds->this->cpu_power;
  2198. else
  2199. tmp = (sds->busiest_load_per_task * SCHED_LOAD_SCALE) /
  2200. sds->this->cpu_power;
  2201. pwr_move += sds->this->cpu_power *
  2202. min(sds->this_load_per_task, sds->this_load + tmp);
  2203. pwr_move /= SCHED_LOAD_SCALE;
  2204. /* Move if we gain throughput */
  2205. if (pwr_move > pwr_now)
  2206. *imbalance = sds->busiest_load_per_task;
  2207. }
  2208. /**
  2209. * calculate_imbalance - Calculate the amount of imbalance present within the
  2210. * groups of a given sched_domain during load balance.
  2211. * @sds: statistics of the sched_domain whose imbalance is to be calculated.
  2212. * @this_cpu: Cpu for which currently load balance is being performed.
  2213. * @imbalance: The variable to store the imbalance.
  2214. */
  2215. static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
  2216. unsigned long *imbalance)
  2217. {
  2218. unsigned long max_pull;
  2219. /*
  2220. * In the presence of smp nice balancing, certain scenarios can have
  2221. * max load less than avg load(as we skip the groups at or below
  2222. * its cpu_power, while calculating max_load..)
  2223. */
  2224. if (sds->max_load < sds->avg_load) {
  2225. *imbalance = 0;
  2226. return fix_small_imbalance(sds, this_cpu, imbalance);
  2227. }
  2228. /* Don't want to pull so many tasks that a group would go idle */
  2229. max_pull = min(sds->max_load - sds->avg_load,
  2230. sds->max_load - sds->busiest_load_per_task);
  2231. /* How much load to actually move to equalise the imbalance */
  2232. *imbalance = min(max_pull * sds->busiest->cpu_power,
  2233. (sds->avg_load - sds->this_load) * sds->this->cpu_power)
  2234. / SCHED_LOAD_SCALE;
  2235. /*
  2236. * if *imbalance is less than the average load per runnable task
  2237. * there is no gaurantee that any tasks will be moved so we'll have
  2238. * a think about bumping its value to force at least one task to be
  2239. * moved
  2240. */
  2241. if (*imbalance < sds->busiest_load_per_task)
  2242. return fix_small_imbalance(sds, this_cpu, imbalance);
  2243. }
  2244. /******* find_busiest_group() helpers end here *********************/
  2245. /**
  2246. * find_busiest_group - Returns the busiest group within the sched_domain
  2247. * if there is an imbalance. If there isn't an imbalance, and
  2248. * the user has opted for power-savings, it returns a group whose
  2249. * CPUs can be put to idle by rebalancing those tasks elsewhere, if
  2250. * such a group exists.
  2251. *
  2252. * Also calculates the amount of weighted load which should be moved
  2253. * to restore balance.
  2254. *
  2255. * @sd: The sched_domain whose busiest group is to be returned.
  2256. * @this_cpu: The cpu for which load balancing is currently being performed.
  2257. * @imbalance: Variable which stores amount of weighted load which should
  2258. * be moved to restore balance/put a group to idle.
  2259. * @idle: The idle status of this_cpu.
  2260. * @sd_idle: The idleness of sd
  2261. * @cpus: The set of CPUs under consideration for load-balancing.
  2262. * @balance: Pointer to a variable indicating if this_cpu
  2263. * is the appropriate cpu to perform load balancing at this_level.
  2264. *
  2265. * Returns: - the busiest group if imbalance exists.
  2266. * - If no imbalance and user has opted for power-savings balance,
  2267. * return the least loaded group whose CPUs can be
  2268. * put to idle by rebalancing its tasks onto our group.
  2269. */
  2270. static struct sched_group *
  2271. find_busiest_group(struct sched_domain *sd, int this_cpu,
  2272. unsigned long *imbalance, enum cpu_idle_type idle,
  2273. int *sd_idle, const struct cpumask *cpus, int *balance)
  2274. {
  2275. struct sd_lb_stats sds;
  2276. memset(&sds, 0, sizeof(sds));
  2277. /*
  2278. * Compute the various statistics relavent for load balancing at
  2279. * this level.
  2280. */
  2281. update_sd_lb_stats(sd, this_cpu, idle, sd_idle, cpus,
  2282. balance, &sds);
  2283. /* Cases where imbalance does not exist from POV of this_cpu */
  2284. /* 1) this_cpu is not the appropriate cpu to perform load balancing
  2285. * at this level.
  2286. * 2) There is no busy sibling group to pull from.
  2287. * 3) This group is the busiest group.
  2288. * 4) This group is more busy than the avg busieness at this
  2289. * sched_domain.
  2290. * 5) The imbalance is within the specified limit.
  2291. * 6) Any rebalance would lead to ping-pong
  2292. */
  2293. if (balance && !(*balance))
  2294. goto ret;
  2295. if (!sds.busiest || sds.busiest_nr_running == 0)
  2296. goto out_balanced;
  2297. if (sds.this_load >= sds.max_load)
  2298. goto out_balanced;
  2299. sds.avg_load = (SCHED_LOAD_SCALE * sds.total_load) / sds.total_pwr;
  2300. if (sds.this_load >= sds.avg_load)
  2301. goto out_balanced;
  2302. if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
  2303. goto out_balanced;
  2304. sds.busiest_load_per_task /= sds.busiest_nr_running;
  2305. if (sds.group_imb)
  2306. sds.busiest_load_per_task =
  2307. min(sds.busiest_load_per_task, sds.avg_load);
  2308. /*
  2309. * We're trying to get all the cpus to the average_load, so we don't
  2310. * want to push ourselves above the average load, nor do we wish to
  2311. * reduce the max loaded cpu below the average load, as either of these
  2312. * actions would just result in more rebalancing later, and ping-pong
  2313. * tasks around. Thus we look for the minimum possible imbalance.
  2314. * Negative imbalances (*we* are more loaded than anyone else) will
  2315. * be counted as no imbalance for these purposes -- we can't fix that
  2316. * by pulling tasks to us. Be careful of negative numbers as they'll
  2317. * appear as very large values with unsigned longs.
  2318. */
  2319. if (sds.max_load <= sds.busiest_load_per_task)
  2320. goto out_balanced;
  2321. /* Looks like there is an imbalance. Compute it */
  2322. calculate_imbalance(&sds, this_cpu, imbalance);
  2323. return sds.busiest;
  2324. out_balanced:
  2325. /*
  2326. * There is no obvious imbalance. But check if we can do some balancing
  2327. * to save power.
  2328. */
  2329. if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
  2330. return sds.busiest;
  2331. ret:
  2332. *imbalance = 0;
  2333. return NULL;
  2334. }
  2335. /*
  2336. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  2337. */
  2338. static struct rq *
  2339. find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
  2340. unsigned long imbalance, const struct cpumask *cpus)
  2341. {
  2342. struct rq *busiest = NULL, *rq;
  2343. unsigned long max_load = 0;
  2344. int i;
  2345. for_each_cpu(i, sched_group_cpus(group)) {
  2346. unsigned long power = power_of(i);
  2347. unsigned long capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
  2348. unsigned long wl;
  2349. if (!cpumask_test_cpu(i, cpus))
  2350. continue;
  2351. rq = cpu_rq(i);
  2352. wl = weighted_cpuload(i) * SCHED_LOAD_SCALE;
  2353. wl /= power;
  2354. if (capacity && rq->nr_running == 1 && wl > imbalance)
  2355. continue;
  2356. if (wl > max_load) {
  2357. max_load = wl;
  2358. busiest = rq;
  2359. }
  2360. }
  2361. return busiest;
  2362. }
  2363. /*
  2364. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  2365. * so long as it is large enough.
  2366. */
  2367. #define MAX_PINNED_INTERVAL 512
  2368. /* Working cpumask for load_balance and load_balance_newidle. */
  2369. static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
  2370. static int need_active_balance(struct sched_domain *sd, int sd_idle, int idle)
  2371. {
  2372. if (idle == CPU_NEWLY_IDLE) {
  2373. /*
  2374. * The only task running in a non-idle cpu can be moved to this
  2375. * cpu in an attempt to completely freeup the other CPU
  2376. * package.
  2377. *
  2378. * The package power saving logic comes from
  2379. * find_busiest_group(). If there are no imbalance, then
  2380. * f_b_g() will return NULL. However when sched_mc={1,2} then
  2381. * f_b_g() will select a group from which a running task may be
  2382. * pulled to this cpu in order to make the other package idle.
  2383. * If there is no opportunity to make a package idle and if
  2384. * there are no imbalance, then f_b_g() will return NULL and no
  2385. * action will be taken in load_balance_newidle().
  2386. *
  2387. * Under normal task pull operation due to imbalance, there
  2388. * will be more than one task in the source run queue and
  2389. * move_tasks() will succeed. ld_moved will be true and this
  2390. * active balance code will not be triggered.
  2391. */
  2392. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2393. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2394. return 0;
  2395. if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
  2396. return 0;
  2397. }
  2398. return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
  2399. }
  2400. /*
  2401. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  2402. * tasks if there is an imbalance.
  2403. */
  2404. static int load_balance(int this_cpu, struct rq *this_rq,
  2405. struct sched_domain *sd, enum cpu_idle_type idle,
  2406. int *balance)
  2407. {
  2408. int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
  2409. struct sched_group *group;
  2410. unsigned long imbalance;
  2411. struct rq *busiest;
  2412. unsigned long flags;
  2413. struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
  2414. cpumask_copy(cpus, cpu_active_mask);
  2415. /*
  2416. * When power savings policy is enabled for the parent domain, idle
  2417. * sibling can pick up load irrespective of busy siblings. In this case,
  2418. * let the state of idle sibling percolate up as CPU_IDLE, instead of
  2419. * portraying it as CPU_NOT_IDLE.
  2420. */
  2421. if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
  2422. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2423. sd_idle = 1;
  2424. schedstat_inc(sd, lb_count[idle]);
  2425. redo:
  2426. update_shares(sd);
  2427. group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
  2428. cpus, balance);
  2429. if (*balance == 0)
  2430. goto out_balanced;
  2431. if (!group) {
  2432. schedstat_inc(sd, lb_nobusyg[idle]);
  2433. goto out_balanced;
  2434. }
  2435. busiest = find_busiest_queue(group, idle, imbalance, cpus);
  2436. if (!busiest) {
  2437. schedstat_inc(sd, lb_nobusyq[idle]);
  2438. goto out_balanced;
  2439. }
  2440. BUG_ON(busiest == this_rq);
  2441. schedstat_add(sd, lb_imbalance[idle], imbalance);
  2442. ld_moved = 0;
  2443. if (busiest->nr_running > 1) {
  2444. /*
  2445. * Attempt to move tasks. If find_busiest_group has found
  2446. * an imbalance but busiest->nr_running <= 1, the group is
  2447. * still unbalanced. ld_moved simply stays zero, so it is
  2448. * correctly treated as an imbalance.
  2449. */
  2450. local_irq_save(flags);
  2451. double_rq_lock(this_rq, busiest);
  2452. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  2453. imbalance, sd, idle, &all_pinned);
  2454. double_rq_unlock(this_rq, busiest);
  2455. local_irq_restore(flags);
  2456. /*
  2457. * some other cpu did the load balance for us.
  2458. */
  2459. if (ld_moved && this_cpu != smp_processor_id())
  2460. resched_cpu(this_cpu);
  2461. /* All tasks on this runqueue were pinned by CPU affinity */
  2462. if (unlikely(all_pinned)) {
  2463. cpumask_clear_cpu(cpu_of(busiest), cpus);
  2464. if (!cpumask_empty(cpus))
  2465. goto redo;
  2466. goto out_balanced;
  2467. }
  2468. }
  2469. if (!ld_moved) {
  2470. schedstat_inc(sd, lb_failed[idle]);
  2471. sd->nr_balance_failed++;
  2472. if (need_active_balance(sd, sd_idle, idle)) {
  2473. raw_spin_lock_irqsave(&busiest->lock, flags);
  2474. /* don't kick the migration_thread, if the curr
  2475. * task on busiest cpu can't be moved to this_cpu
  2476. */
  2477. if (!cpumask_test_cpu(this_cpu,
  2478. &busiest->curr->cpus_allowed)) {
  2479. raw_spin_unlock_irqrestore(&busiest->lock,
  2480. flags);
  2481. all_pinned = 1;
  2482. goto out_one_pinned;
  2483. }
  2484. if (!busiest->active_balance) {
  2485. busiest->active_balance = 1;
  2486. busiest->push_cpu = this_cpu;
  2487. active_balance = 1;
  2488. }
  2489. raw_spin_unlock_irqrestore(&busiest->lock, flags);
  2490. if (active_balance)
  2491. wake_up_process(busiest->migration_thread);
  2492. /*
  2493. * We've kicked active balancing, reset the failure
  2494. * counter.
  2495. */
  2496. sd->nr_balance_failed = sd->cache_nice_tries+1;
  2497. }
  2498. } else
  2499. sd->nr_balance_failed = 0;
  2500. if (likely(!active_balance)) {
  2501. /* We were unbalanced, so reset the balancing interval */
  2502. sd->balance_interval = sd->min_interval;
  2503. } else {
  2504. /*
  2505. * If we've begun active balancing, start to back off. This
  2506. * case may not be covered by the all_pinned logic if there
  2507. * is only 1 task on the busy runqueue (because we don't call
  2508. * move_tasks).
  2509. */
  2510. if (sd->balance_interval < sd->max_interval)
  2511. sd->balance_interval *= 2;
  2512. }
  2513. if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2514. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2515. ld_moved = -1;
  2516. goto out;
  2517. out_balanced:
  2518. schedstat_inc(sd, lb_balanced[idle]);
  2519. sd->nr_balance_failed = 0;
  2520. out_one_pinned:
  2521. /* tune up the balancing interval */
  2522. if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
  2523. (sd->balance_interval < sd->max_interval))
  2524. sd->balance_interval *= 2;
  2525. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2526. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2527. ld_moved = -1;
  2528. else
  2529. ld_moved = 0;
  2530. out:
  2531. if (ld_moved)
  2532. update_shares(sd);
  2533. return ld_moved;
  2534. }
  2535. /*
  2536. * idle_balance is called by schedule() if this_cpu is about to become
  2537. * idle. Attempts to pull tasks from other CPUs.
  2538. */
  2539. static void idle_balance(int this_cpu, struct rq *this_rq)
  2540. {
  2541. struct sched_domain *sd;
  2542. int pulled_task = 0;
  2543. unsigned long next_balance = jiffies + HZ;
  2544. this_rq->idle_stamp = this_rq->clock;
  2545. if (this_rq->avg_idle < sysctl_sched_migration_cost)
  2546. return;
  2547. /*
  2548. * Drop the rq->lock, but keep IRQ/preempt disabled.
  2549. */
  2550. raw_spin_unlock(&this_rq->lock);
  2551. for_each_domain(this_cpu, sd) {
  2552. unsigned long interval;
  2553. int balance = 1;
  2554. if (!(sd->flags & SD_LOAD_BALANCE))
  2555. continue;
  2556. if (sd->flags & SD_BALANCE_NEWIDLE) {
  2557. /* If we've pulled tasks over stop searching: */
  2558. pulled_task = load_balance(this_cpu, this_rq,
  2559. sd, CPU_NEWLY_IDLE, &balance);
  2560. }
  2561. interval = msecs_to_jiffies(sd->balance_interval);
  2562. if (time_after(next_balance, sd->last_balance + interval))
  2563. next_balance = sd->last_balance + interval;
  2564. if (pulled_task) {
  2565. this_rq->idle_stamp = 0;
  2566. break;
  2567. }
  2568. }
  2569. raw_spin_lock(&this_rq->lock);
  2570. if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
  2571. /*
  2572. * We are going idle. next_balance may be set based on
  2573. * a busy processor. So reset next_balance.
  2574. */
  2575. this_rq->next_balance = next_balance;
  2576. }
  2577. }
  2578. /*
  2579. * active_load_balance is run by migration threads. It pushes running tasks
  2580. * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
  2581. * running on each physical CPU where possible, and avoids physical /
  2582. * logical imbalances.
  2583. *
  2584. * Called with busiest_rq locked.
  2585. */
  2586. static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
  2587. {
  2588. int target_cpu = busiest_rq->push_cpu;
  2589. struct sched_domain *sd;
  2590. struct rq *target_rq;
  2591. /* Is there any task to move? */
  2592. if (busiest_rq->nr_running <= 1)
  2593. return;
  2594. target_rq = cpu_rq(target_cpu);
  2595. /*
  2596. * This condition is "impossible", if it occurs
  2597. * we need to fix it. Originally reported by
  2598. * Bjorn Helgaas on a 128-cpu setup.
  2599. */
  2600. BUG_ON(busiest_rq == target_rq);
  2601. /* move a task from busiest_rq to target_rq */
  2602. double_lock_balance(busiest_rq, target_rq);
  2603. update_rq_clock(busiest_rq);
  2604. update_rq_clock(target_rq);
  2605. /* Search for an sd spanning us and the target CPU. */
  2606. for_each_domain(target_cpu, sd) {
  2607. if ((sd->flags & SD_LOAD_BALANCE) &&
  2608. cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
  2609. break;
  2610. }
  2611. if (likely(sd)) {
  2612. schedstat_inc(sd, alb_count);
  2613. if (move_one_task(target_rq, target_cpu, busiest_rq,
  2614. sd, CPU_IDLE))
  2615. schedstat_inc(sd, alb_pushed);
  2616. else
  2617. schedstat_inc(sd, alb_failed);
  2618. }
  2619. double_unlock_balance(busiest_rq, target_rq);
  2620. }
  2621. #ifdef CONFIG_NO_HZ
  2622. static struct {
  2623. atomic_t load_balancer;
  2624. cpumask_var_t cpu_mask;
  2625. cpumask_var_t ilb_grp_nohz_mask;
  2626. } nohz ____cacheline_aligned = {
  2627. .load_balancer = ATOMIC_INIT(-1),
  2628. };
  2629. int get_nohz_load_balancer(void)
  2630. {
  2631. return atomic_read(&nohz.load_balancer);
  2632. }
  2633. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2634. /**
  2635. * lowest_flag_domain - Return lowest sched_domain containing flag.
  2636. * @cpu: The cpu whose lowest level of sched domain is to
  2637. * be returned.
  2638. * @flag: The flag to check for the lowest sched_domain
  2639. * for the given cpu.
  2640. *
  2641. * Returns the lowest sched_domain of a cpu which contains the given flag.
  2642. */
  2643. static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
  2644. {
  2645. struct sched_domain *sd;
  2646. for_each_domain(cpu, sd)
  2647. if (sd && (sd->flags & flag))
  2648. break;
  2649. return sd;
  2650. }
  2651. /**
  2652. * for_each_flag_domain - Iterates over sched_domains containing the flag.
  2653. * @cpu: The cpu whose domains we're iterating over.
  2654. * @sd: variable holding the value of the power_savings_sd
  2655. * for cpu.
  2656. * @flag: The flag to filter the sched_domains to be iterated.
  2657. *
  2658. * Iterates over all the scheduler domains for a given cpu that has the 'flag'
  2659. * set, starting from the lowest sched_domain to the highest.
  2660. */
  2661. #define for_each_flag_domain(cpu, sd, flag) \
  2662. for (sd = lowest_flag_domain(cpu, flag); \
  2663. (sd && (sd->flags & flag)); sd = sd->parent)
  2664. /**
  2665. * is_semi_idle_group - Checks if the given sched_group is semi-idle.
  2666. * @ilb_group: group to be checked for semi-idleness
  2667. *
  2668. * Returns: 1 if the group is semi-idle. 0 otherwise.
  2669. *
  2670. * We define a sched_group to be semi idle if it has atleast one idle-CPU
  2671. * and atleast one non-idle CPU. This helper function checks if the given
  2672. * sched_group is semi-idle or not.
  2673. */
  2674. static inline int is_semi_idle_group(struct sched_group *ilb_group)
  2675. {
  2676. cpumask_and(nohz.ilb_grp_nohz_mask, nohz.cpu_mask,
  2677. sched_group_cpus(ilb_group));
  2678. /*
  2679. * A sched_group is semi-idle when it has atleast one busy cpu
  2680. * and atleast one idle cpu.
  2681. */
  2682. if (cpumask_empty(nohz.ilb_grp_nohz_mask))
  2683. return 0;
  2684. if (cpumask_equal(nohz.ilb_grp_nohz_mask, sched_group_cpus(ilb_group)))
  2685. return 0;
  2686. return 1;
  2687. }
  2688. /**
  2689. * find_new_ilb - Finds the optimum idle load balancer for nomination.
  2690. * @cpu: The cpu which is nominating a new idle_load_balancer.
  2691. *
  2692. * Returns: Returns the id of the idle load balancer if it exists,
  2693. * Else, returns >= nr_cpu_ids.
  2694. *
  2695. * This algorithm picks the idle load balancer such that it belongs to a
  2696. * semi-idle powersavings sched_domain. The idea is to try and avoid
  2697. * completely idle packages/cores just for the purpose of idle load balancing
  2698. * when there are other idle cpu's which are better suited for that job.
  2699. */
  2700. static int find_new_ilb(int cpu)
  2701. {
  2702. struct sched_domain *sd;
  2703. struct sched_group *ilb_group;
  2704. /*
  2705. * Have idle load balancer selection from semi-idle packages only
  2706. * when power-aware load balancing is enabled
  2707. */
  2708. if (!(sched_smt_power_savings || sched_mc_power_savings))
  2709. goto out_done;
  2710. /*
  2711. * Optimize for the case when we have no idle CPUs or only one
  2712. * idle CPU. Don't walk the sched_domain hierarchy in such cases
  2713. */
  2714. if (cpumask_weight(nohz.cpu_mask) < 2)
  2715. goto out_done;
  2716. for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
  2717. ilb_group = sd->groups;
  2718. do {
  2719. if (is_semi_idle_group(ilb_group))
  2720. return cpumask_first(nohz.ilb_grp_nohz_mask);
  2721. ilb_group = ilb_group->next;
  2722. } while (ilb_group != sd->groups);
  2723. }
  2724. out_done:
  2725. return cpumask_first(nohz.cpu_mask);
  2726. }
  2727. #else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
  2728. static inline int find_new_ilb(int call_cpu)
  2729. {
  2730. return cpumask_first(nohz.cpu_mask);
  2731. }
  2732. #endif
  2733. /*
  2734. * This routine will try to nominate the ilb (idle load balancing)
  2735. * owner among the cpus whose ticks are stopped. ilb owner will do the idle
  2736. * load balancing on behalf of all those cpus. If all the cpus in the system
  2737. * go into this tickless mode, then there will be no ilb owner (as there is
  2738. * no need for one) and all the cpus will sleep till the next wakeup event
  2739. * arrives...
  2740. *
  2741. * For the ilb owner, tick is not stopped. And this tick will be used
  2742. * for idle load balancing. ilb owner will still be part of
  2743. * nohz.cpu_mask..
  2744. *
  2745. * While stopping the tick, this cpu will become the ilb owner if there
  2746. * is no other owner. And will be the owner till that cpu becomes busy
  2747. * or if all cpus in the system stop their ticks at which point
  2748. * there is no need for ilb owner.
  2749. *
  2750. * When the ilb owner becomes busy, it nominates another owner, during the
  2751. * next busy scheduler_tick()
  2752. */
  2753. int select_nohz_load_balancer(int stop_tick)
  2754. {
  2755. int cpu = smp_processor_id();
  2756. if (stop_tick) {
  2757. cpu_rq(cpu)->in_nohz_recently = 1;
  2758. if (!cpu_active(cpu)) {
  2759. if (atomic_read(&nohz.load_balancer) != cpu)
  2760. return 0;
  2761. /*
  2762. * If we are going offline and still the leader,
  2763. * give up!
  2764. */
  2765. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  2766. BUG();
  2767. return 0;
  2768. }
  2769. cpumask_set_cpu(cpu, nohz.cpu_mask);
  2770. /* time for ilb owner also to sleep */
  2771. if (cpumask_weight(nohz.cpu_mask) == num_active_cpus()) {
  2772. if (atomic_read(&nohz.load_balancer) == cpu)
  2773. atomic_set(&nohz.load_balancer, -1);
  2774. return 0;
  2775. }
  2776. if (atomic_read(&nohz.load_balancer) == -1) {
  2777. /* make me the ilb owner */
  2778. if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
  2779. return 1;
  2780. } else if (atomic_read(&nohz.load_balancer) == cpu) {
  2781. int new_ilb;
  2782. if (!(sched_smt_power_savings ||
  2783. sched_mc_power_savings))
  2784. return 1;
  2785. /*
  2786. * Check to see if there is a more power-efficient
  2787. * ilb.
  2788. */
  2789. new_ilb = find_new_ilb(cpu);
  2790. if (new_ilb < nr_cpu_ids && new_ilb != cpu) {
  2791. atomic_set(&nohz.load_balancer, -1);
  2792. resched_cpu(new_ilb);
  2793. return 0;
  2794. }
  2795. return 1;
  2796. }
  2797. } else {
  2798. if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
  2799. return 0;
  2800. cpumask_clear_cpu(cpu, nohz.cpu_mask);
  2801. if (atomic_read(&nohz.load_balancer) == cpu)
  2802. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  2803. BUG();
  2804. }
  2805. return 0;
  2806. }
  2807. #endif
  2808. static DEFINE_SPINLOCK(balancing);
  2809. /*
  2810. * It checks each scheduling domain to see if it is due to be balanced,
  2811. * and initiates a balancing operation if so.
  2812. *
  2813. * Balancing parameters are set up in arch_init_sched_domains.
  2814. */
  2815. static void rebalance_domains(int cpu, enum cpu_idle_type idle)
  2816. {
  2817. int balance = 1;
  2818. struct rq *rq = cpu_rq(cpu);
  2819. unsigned long interval;
  2820. struct sched_domain *sd;
  2821. /* Earliest time when we have to do rebalance again */
  2822. unsigned long next_balance = jiffies + 60*HZ;
  2823. int update_next_balance = 0;
  2824. int need_serialize;
  2825. for_each_domain(cpu, sd) {
  2826. if (!(sd->flags & SD_LOAD_BALANCE))
  2827. continue;
  2828. interval = sd->balance_interval;
  2829. if (idle != CPU_IDLE)
  2830. interval *= sd->busy_factor;
  2831. /* scale ms to jiffies */
  2832. interval = msecs_to_jiffies(interval);
  2833. if (unlikely(!interval))
  2834. interval = 1;
  2835. if (interval > HZ*NR_CPUS/10)
  2836. interval = HZ*NR_CPUS/10;
  2837. need_serialize = sd->flags & SD_SERIALIZE;
  2838. if (need_serialize) {
  2839. if (!spin_trylock(&balancing))
  2840. goto out;
  2841. }
  2842. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  2843. if (load_balance(cpu, rq, sd, idle, &balance)) {
  2844. /*
  2845. * We've pulled tasks over so either we're no
  2846. * longer idle, or one of our SMT siblings is
  2847. * not idle.
  2848. */
  2849. idle = CPU_NOT_IDLE;
  2850. }
  2851. sd->last_balance = jiffies;
  2852. }
  2853. if (need_serialize)
  2854. spin_unlock(&balancing);
  2855. out:
  2856. if (time_after(next_balance, sd->last_balance + interval)) {
  2857. next_balance = sd->last_balance + interval;
  2858. update_next_balance = 1;
  2859. }
  2860. /*
  2861. * Stop the load balance at this level. There is another
  2862. * CPU in our sched group which is doing load balancing more
  2863. * actively.
  2864. */
  2865. if (!balance)
  2866. break;
  2867. }
  2868. /*
  2869. * next_balance will be updated only when there is a need.
  2870. * When the cpu is attached to null domain for ex, it will not be
  2871. * updated.
  2872. */
  2873. if (likely(update_next_balance))
  2874. rq->next_balance = next_balance;
  2875. }
  2876. /*
  2877. * run_rebalance_domains is triggered when needed from the scheduler tick.
  2878. * In CONFIG_NO_HZ case, the idle load balance owner will do the
  2879. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  2880. */
  2881. static void run_rebalance_domains(struct softirq_action *h)
  2882. {
  2883. int this_cpu = smp_processor_id();
  2884. struct rq *this_rq = cpu_rq(this_cpu);
  2885. enum cpu_idle_type idle = this_rq->idle_at_tick ?
  2886. CPU_IDLE : CPU_NOT_IDLE;
  2887. rebalance_domains(this_cpu, idle);
  2888. #ifdef CONFIG_NO_HZ
  2889. /*
  2890. * If this cpu is the owner for idle load balancing, then do the
  2891. * balancing on behalf of the other idle cpus whose ticks are
  2892. * stopped.
  2893. */
  2894. if (this_rq->idle_at_tick &&
  2895. atomic_read(&nohz.load_balancer) == this_cpu) {
  2896. struct rq *rq;
  2897. int balance_cpu;
  2898. for_each_cpu(balance_cpu, nohz.cpu_mask) {
  2899. if (balance_cpu == this_cpu)
  2900. continue;
  2901. /*
  2902. * If this cpu gets work to do, stop the load balancing
  2903. * work being done for other cpus. Next load
  2904. * balancing owner will pick it up.
  2905. */
  2906. if (need_resched())
  2907. break;
  2908. rebalance_domains(balance_cpu, CPU_IDLE);
  2909. rq = cpu_rq(balance_cpu);
  2910. if (time_after(this_rq->next_balance, rq->next_balance))
  2911. this_rq->next_balance = rq->next_balance;
  2912. }
  2913. }
  2914. #endif
  2915. }
  2916. static inline int on_null_domain(int cpu)
  2917. {
  2918. return !rcu_dereference(cpu_rq(cpu)->sd);
  2919. }
  2920. /*
  2921. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  2922. *
  2923. * In case of CONFIG_NO_HZ, this is the place where we nominate a new
  2924. * idle load balancing owner or decide to stop the periodic load balancing,
  2925. * if the whole system is idle.
  2926. */
  2927. static inline void trigger_load_balance(struct rq *rq, int cpu)
  2928. {
  2929. #ifdef CONFIG_NO_HZ
  2930. /*
  2931. * If we were in the nohz mode recently and busy at the current
  2932. * scheduler tick, then check if we need to nominate new idle
  2933. * load balancer.
  2934. */
  2935. if (rq->in_nohz_recently && !rq->idle_at_tick) {
  2936. rq->in_nohz_recently = 0;
  2937. if (atomic_read(&nohz.load_balancer) == cpu) {
  2938. cpumask_clear_cpu(cpu, nohz.cpu_mask);
  2939. atomic_set(&nohz.load_balancer, -1);
  2940. }
  2941. if (atomic_read(&nohz.load_balancer) == -1) {
  2942. int ilb = find_new_ilb(cpu);
  2943. if (ilb < nr_cpu_ids)
  2944. resched_cpu(ilb);
  2945. }
  2946. }
  2947. /*
  2948. * If this cpu is idle and doing idle load balancing for all the
  2949. * cpus with ticks stopped, is it time for that to stop?
  2950. */
  2951. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
  2952. cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
  2953. resched_cpu(cpu);
  2954. return;
  2955. }
  2956. /*
  2957. * If this cpu is idle and the idle load balancing is done by
  2958. * someone else, then no need raise the SCHED_SOFTIRQ
  2959. */
  2960. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
  2961. cpumask_test_cpu(cpu, nohz.cpu_mask))
  2962. return;
  2963. #endif
  2964. /* Don't need to rebalance while attached to NULL domain */
  2965. if (time_after_eq(jiffies, rq->next_balance) &&
  2966. likely(!on_null_domain(cpu)))
  2967. raise_softirq(SCHED_SOFTIRQ);
  2968. }
  2969. static void rq_online_fair(struct rq *rq)
  2970. {
  2971. update_sysctl();
  2972. }
  2973. static void rq_offline_fair(struct rq *rq)
  2974. {
  2975. update_sysctl();
  2976. }
  2977. #else /* CONFIG_SMP */
  2978. /*
  2979. * on UP we do not need to balance between CPUs:
  2980. */
  2981. static inline void idle_balance(int cpu, struct rq *rq)
  2982. {
  2983. }
  2984. #endif /* CONFIG_SMP */
  2985. /*
  2986. * scheduler tick hitting a task of our scheduling class:
  2987. */
  2988. static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  2989. {
  2990. struct cfs_rq *cfs_rq;
  2991. struct sched_entity *se = &curr->se;
  2992. for_each_sched_entity(se) {
  2993. cfs_rq = cfs_rq_of(se);
  2994. entity_tick(cfs_rq, se, queued);
  2995. }
  2996. }
  2997. /*
  2998. * called on fork with the child task as argument from the parent's context
  2999. * - child not yet on the tasklist
  3000. * - preemption disabled
  3001. */
  3002. static void task_fork_fair(struct task_struct *p)
  3003. {
  3004. struct cfs_rq *cfs_rq = task_cfs_rq(current);
  3005. struct sched_entity *se = &p->se, *curr = cfs_rq->curr;
  3006. int this_cpu = smp_processor_id();
  3007. struct rq *rq = this_rq();
  3008. unsigned long flags;
  3009. raw_spin_lock_irqsave(&rq->lock, flags);
  3010. if (unlikely(task_cpu(p) != this_cpu))
  3011. __set_task_cpu(p, this_cpu);
  3012. update_curr(cfs_rq);
  3013. if (curr)
  3014. se->vruntime = curr->vruntime;
  3015. place_entity(cfs_rq, se, 1);
  3016. if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
  3017. /*
  3018. * Upon rescheduling, sched_class::put_prev_task() will place
  3019. * 'current' within the tree based on its new key value.
  3020. */
  3021. swap(curr->vruntime, se->vruntime);
  3022. resched_task(rq->curr);
  3023. }
  3024. se->vruntime -= cfs_rq->min_vruntime;
  3025. raw_spin_unlock_irqrestore(&rq->lock, flags);
  3026. }
  3027. /*
  3028. * Priority of the task has changed. Check to see if we preempt
  3029. * the current task.
  3030. */
  3031. static void prio_changed_fair(struct rq *rq, struct task_struct *p,
  3032. int oldprio, int running)
  3033. {
  3034. /*
  3035. * Reschedule if we are currently running on this runqueue and
  3036. * our priority decreased, or if we are not currently running on
  3037. * this runqueue and our priority is higher than the current's
  3038. */
  3039. if (running) {
  3040. if (p->prio > oldprio)
  3041. resched_task(rq->curr);
  3042. } else
  3043. check_preempt_curr(rq, p, 0);
  3044. }
  3045. /*
  3046. * We switched to the sched_fair class.
  3047. */
  3048. static void switched_to_fair(struct rq *rq, struct task_struct *p,
  3049. int running)
  3050. {
  3051. /*
  3052. * We were most likely switched from sched_rt, so
  3053. * kick off the schedule if running, otherwise just see
  3054. * if we can still preempt the current task.
  3055. */
  3056. if (running)
  3057. resched_task(rq->curr);
  3058. else
  3059. check_preempt_curr(rq, p, 0);
  3060. }
  3061. /* Account for a task changing its policy or group.
  3062. *
  3063. * This routine is mostly called to set cfs_rq->curr field when a task
  3064. * migrates between groups/classes.
  3065. */
  3066. static void set_curr_task_fair(struct rq *rq)
  3067. {
  3068. struct sched_entity *se = &rq->curr->se;
  3069. for_each_sched_entity(se)
  3070. set_next_entity(cfs_rq_of(se), se);
  3071. }
  3072. #ifdef CONFIG_FAIR_GROUP_SCHED
  3073. static void moved_group_fair(struct task_struct *p, int on_rq)
  3074. {
  3075. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  3076. update_curr(cfs_rq);
  3077. if (!on_rq)
  3078. place_entity(cfs_rq, &p->se, 1);
  3079. }
  3080. #endif
  3081. static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
  3082. {
  3083. struct sched_entity *se = &task->se;
  3084. unsigned int rr_interval = 0;
  3085. /*
  3086. * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
  3087. * idle runqueue:
  3088. */
  3089. if (rq->cfs.load.weight)
  3090. rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
  3091. return rr_interval;
  3092. }
  3093. /*
  3094. * All the scheduling class methods:
  3095. */
  3096. static const struct sched_class fair_sched_class = {
  3097. .next = &idle_sched_class,
  3098. .enqueue_task = enqueue_task_fair,
  3099. .dequeue_task = dequeue_task_fair,
  3100. .yield_task = yield_task_fair,
  3101. .check_preempt_curr = check_preempt_wakeup,
  3102. .pick_next_task = pick_next_task_fair,
  3103. .put_prev_task = put_prev_task_fair,
  3104. #ifdef CONFIG_SMP
  3105. .select_task_rq = select_task_rq_fair,
  3106. .rq_online = rq_online_fair,
  3107. .rq_offline = rq_offline_fair,
  3108. .task_waking = task_waking_fair,
  3109. #endif
  3110. .set_curr_task = set_curr_task_fair,
  3111. .task_tick = task_tick_fair,
  3112. .task_fork = task_fork_fair,
  3113. .prio_changed = prio_changed_fair,
  3114. .switched_to = switched_to_fair,
  3115. .get_rr_interval = get_rr_interval_fair,
  3116. #ifdef CONFIG_FAIR_GROUP_SCHED
  3117. .moved_group = moved_group_fair,
  3118. #endif
  3119. };
  3120. #ifdef CONFIG_SCHED_DEBUG
  3121. static void print_cfs_stats(struct seq_file *m, int cpu)
  3122. {
  3123. struct cfs_rq *cfs_rq;
  3124. rcu_read_lock();
  3125. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  3126. print_cfs_rq(m, cpu, cfs_rq);
  3127. rcu_read_unlock();
  3128. }
  3129. #endif