fair.c 145 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748
  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. #include <linux/cpumask.h>
  25. #include <linux/slab.h>
  26. #include <linux/profile.h>
  27. #include <linux/interrupt.h>
  28. #include <trace/events/sched.h>
  29. #include "sched.h"
  30. /*
  31. * Targeted preemption latency for CPU-bound tasks:
  32. * (default: 6ms * (1 + ilog(ncpus)), units: nanoseconds)
  33. *
  34. * NOTE: this latency value is not the same as the concept of
  35. * 'timeslice length' - timeslices in CFS are of variable length
  36. * and have no persistent notion like in traditional, time-slice
  37. * based scheduling concepts.
  38. *
  39. * (to see the precise effective timeslice length of your workload,
  40. * run vmstat and monitor the context-switches (cs) field)
  41. */
  42. unsigned int sysctl_sched_latency = 6000000ULL;
  43. unsigned int normalized_sysctl_sched_latency = 6000000ULL;
  44. /*
  45. * The initial- and re-scaling of tunables is configurable
  46. * (default SCHED_TUNABLESCALING_LOG = *(1+ilog(ncpus))
  47. *
  48. * Options are:
  49. * SCHED_TUNABLESCALING_NONE - unscaled, always *1
  50. * SCHED_TUNABLESCALING_LOG - scaled logarithmical, *1+ilog(ncpus)
  51. * SCHED_TUNABLESCALING_LINEAR - scaled linear, *ncpus
  52. */
  53. enum sched_tunable_scaling sysctl_sched_tunable_scaling
  54. = SCHED_TUNABLESCALING_LOG;
  55. /*
  56. * Minimal preemption granularity for CPU-bound tasks:
  57. * (default: 0.75 msec * (1 + ilog(ncpus)), units: nanoseconds)
  58. */
  59. unsigned int sysctl_sched_min_granularity = 750000ULL;
  60. unsigned int normalized_sysctl_sched_min_granularity = 750000ULL;
  61. /*
  62. * is kept at sysctl_sched_latency / sysctl_sched_min_granularity
  63. */
  64. static unsigned int sched_nr_latency = 8;
  65. /*
  66. * After fork, child runs first. If set to 0 (default) then
  67. * parent will (try to) run first.
  68. */
  69. unsigned int sysctl_sched_child_runs_first __read_mostly;
  70. /*
  71. * SCHED_OTHER wake-up granularity.
  72. * (default: 1 msec * (1 + ilog(ncpus)), units: nanoseconds)
  73. *
  74. * This option delays the preemption effects of decoupled workloads
  75. * and reduces their over-scheduling. Synchronous workloads will still
  76. * have immediate wakeup/sleep latencies.
  77. */
  78. unsigned int sysctl_sched_wakeup_granularity = 1000000UL;
  79. unsigned int normalized_sysctl_sched_wakeup_granularity = 1000000UL;
  80. const_debug unsigned int sysctl_sched_migration_cost = 500000UL;
  81. /*
  82. * The exponential sliding window over which load is averaged for shares
  83. * distribution.
  84. * (default: 10msec)
  85. */
  86. unsigned int __read_mostly sysctl_sched_shares_window = 10000000UL;
  87. #ifdef CONFIG_CFS_BANDWIDTH
  88. /*
  89. * Amount of runtime to allocate from global (tg) to local (per-cfs_rq) pool
  90. * each time a cfs_rq requests quota.
  91. *
  92. * Note: in the case that the slice exceeds the runtime remaining (either due
  93. * to consumption or the quota being specified to be smaller than the slice)
  94. * we will always only issue the remaining available time.
  95. *
  96. * default: 5 msec, units: microseconds
  97. */
  98. unsigned int sysctl_sched_cfs_bandwidth_slice = 5000UL;
  99. #endif
  100. /*
  101. * Increase the granularity value when there are more CPUs,
  102. * because with more CPUs the 'effective latency' as visible
  103. * to users decreases. But the relationship is not linear,
  104. * so pick a second-best guess by going with the log2 of the
  105. * number of CPUs.
  106. *
  107. * This idea comes from the SD scheduler of Con Kolivas:
  108. */
  109. static int get_update_sysctl_factor(void)
  110. {
  111. unsigned int cpus = min_t(int, num_online_cpus(), 8);
  112. unsigned int factor;
  113. switch (sysctl_sched_tunable_scaling) {
  114. case SCHED_TUNABLESCALING_NONE:
  115. factor = 1;
  116. break;
  117. case SCHED_TUNABLESCALING_LINEAR:
  118. factor = cpus;
  119. break;
  120. case SCHED_TUNABLESCALING_LOG:
  121. default:
  122. factor = 1 + ilog2(cpus);
  123. break;
  124. }
  125. return factor;
  126. }
  127. static void update_sysctl(void)
  128. {
  129. unsigned int factor = get_update_sysctl_factor();
  130. #define SET_SYSCTL(name) \
  131. (sysctl_##name = (factor) * normalized_sysctl_##name)
  132. SET_SYSCTL(sched_min_granularity);
  133. SET_SYSCTL(sched_latency);
  134. SET_SYSCTL(sched_wakeup_granularity);
  135. #undef SET_SYSCTL
  136. }
  137. void sched_init_granularity(void)
  138. {
  139. update_sysctl();
  140. }
  141. #if BITS_PER_LONG == 32
  142. # define WMULT_CONST (~0UL)
  143. #else
  144. # define WMULT_CONST (1UL << 32)
  145. #endif
  146. #define WMULT_SHIFT 32
  147. /*
  148. * Shift right and round:
  149. */
  150. #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
  151. /*
  152. * delta *= weight / lw
  153. */
  154. static unsigned long
  155. calc_delta_mine(unsigned long delta_exec, unsigned long weight,
  156. struct load_weight *lw)
  157. {
  158. u64 tmp;
  159. /*
  160. * weight can be less than 2^SCHED_LOAD_RESOLUTION for task group sched
  161. * entities since MIN_SHARES = 2. Treat weight as 1 if less than
  162. * 2^SCHED_LOAD_RESOLUTION.
  163. */
  164. if (likely(weight > (1UL << SCHED_LOAD_RESOLUTION)))
  165. tmp = (u64)delta_exec * scale_load_down(weight);
  166. else
  167. tmp = (u64)delta_exec;
  168. if (!lw->inv_weight) {
  169. unsigned long w = scale_load_down(lw->weight);
  170. if (BITS_PER_LONG > 32 && unlikely(w >= WMULT_CONST))
  171. lw->inv_weight = 1;
  172. else if (unlikely(!w))
  173. lw->inv_weight = WMULT_CONST;
  174. else
  175. lw->inv_weight = WMULT_CONST / w;
  176. }
  177. /*
  178. * Check whether we'd overflow the 64-bit multiplication:
  179. */
  180. if (unlikely(tmp > WMULT_CONST))
  181. tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
  182. WMULT_SHIFT/2);
  183. else
  184. tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
  185. return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
  186. }
  187. const struct sched_class fair_sched_class;
  188. /**************************************************************
  189. * CFS operations on generic schedulable entities:
  190. */
  191. #ifdef CONFIG_FAIR_GROUP_SCHED
  192. /* cpu runqueue to which this cfs_rq is attached */
  193. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  194. {
  195. return cfs_rq->rq;
  196. }
  197. /* An entity is a task if it doesn't "own" a runqueue */
  198. #define entity_is_task(se) (!se->my_q)
  199. static inline struct task_struct *task_of(struct sched_entity *se)
  200. {
  201. #ifdef CONFIG_SCHED_DEBUG
  202. WARN_ON_ONCE(!entity_is_task(se));
  203. #endif
  204. return container_of(se, struct task_struct, se);
  205. }
  206. /* Walk up scheduling entities hierarchy */
  207. #define for_each_sched_entity(se) \
  208. for (; se; se = se->parent)
  209. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  210. {
  211. return p->se.cfs_rq;
  212. }
  213. /* runqueue on which this entity is (to be) queued */
  214. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  215. {
  216. return se->cfs_rq;
  217. }
  218. /* runqueue "owned" by this group */
  219. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  220. {
  221. return grp->my_q;
  222. }
  223. static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
  224. int force_update);
  225. static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  226. {
  227. if (!cfs_rq->on_list) {
  228. /*
  229. * Ensure we either appear before our parent (if already
  230. * enqueued) or force our parent to appear after us when it is
  231. * enqueued. The fact that we always enqueue bottom-up
  232. * reduces this to two cases.
  233. */
  234. if (cfs_rq->tg->parent &&
  235. cfs_rq->tg->parent->cfs_rq[cpu_of(rq_of(cfs_rq))]->on_list) {
  236. list_add_rcu(&cfs_rq->leaf_cfs_rq_list,
  237. &rq_of(cfs_rq)->leaf_cfs_rq_list);
  238. } else {
  239. list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list,
  240. &rq_of(cfs_rq)->leaf_cfs_rq_list);
  241. }
  242. cfs_rq->on_list = 1;
  243. /* We should have no load, but we need to update last_decay. */
  244. update_cfs_rq_blocked_load(cfs_rq, 0);
  245. }
  246. }
  247. static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  248. {
  249. if (cfs_rq->on_list) {
  250. list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
  251. cfs_rq->on_list = 0;
  252. }
  253. }
  254. /* Iterate thr' all leaf cfs_rq's on a runqueue */
  255. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  256. list_for_each_entry_rcu(cfs_rq, &rq->leaf_cfs_rq_list, leaf_cfs_rq_list)
  257. /* Do the two (enqueued) entities belong to the same group ? */
  258. static inline int
  259. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  260. {
  261. if (se->cfs_rq == pse->cfs_rq)
  262. return 1;
  263. return 0;
  264. }
  265. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  266. {
  267. return se->parent;
  268. }
  269. /* return depth at which a sched entity is present in the hierarchy */
  270. static inline int depth_se(struct sched_entity *se)
  271. {
  272. int depth = 0;
  273. for_each_sched_entity(se)
  274. depth++;
  275. return depth;
  276. }
  277. static void
  278. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  279. {
  280. int se_depth, pse_depth;
  281. /*
  282. * preemption test can be made between sibling entities who are in the
  283. * same cfs_rq i.e who have a common parent. Walk up the hierarchy of
  284. * both tasks until we find their ancestors who are siblings of common
  285. * parent.
  286. */
  287. /* First walk up until both entities are at same depth */
  288. se_depth = depth_se(*se);
  289. pse_depth = depth_se(*pse);
  290. while (se_depth > pse_depth) {
  291. se_depth--;
  292. *se = parent_entity(*se);
  293. }
  294. while (pse_depth > se_depth) {
  295. pse_depth--;
  296. *pse = parent_entity(*pse);
  297. }
  298. while (!is_same_group(*se, *pse)) {
  299. *se = parent_entity(*se);
  300. *pse = parent_entity(*pse);
  301. }
  302. }
  303. #else /* !CONFIG_FAIR_GROUP_SCHED */
  304. static inline struct task_struct *task_of(struct sched_entity *se)
  305. {
  306. return container_of(se, struct task_struct, se);
  307. }
  308. static inline struct rq *rq_of(struct cfs_rq *cfs_rq)
  309. {
  310. return container_of(cfs_rq, struct rq, cfs);
  311. }
  312. #define entity_is_task(se) 1
  313. #define for_each_sched_entity(se) \
  314. for (; se; se = NULL)
  315. static inline struct cfs_rq *task_cfs_rq(struct task_struct *p)
  316. {
  317. return &task_rq(p)->cfs;
  318. }
  319. static inline struct cfs_rq *cfs_rq_of(struct sched_entity *se)
  320. {
  321. struct task_struct *p = task_of(se);
  322. struct rq *rq = task_rq(p);
  323. return &rq->cfs;
  324. }
  325. /* runqueue "owned" by this group */
  326. static inline struct cfs_rq *group_cfs_rq(struct sched_entity *grp)
  327. {
  328. return NULL;
  329. }
  330. static inline void list_add_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  331. {
  332. }
  333. static inline void list_del_leaf_cfs_rq(struct cfs_rq *cfs_rq)
  334. {
  335. }
  336. #define for_each_leaf_cfs_rq(rq, cfs_rq) \
  337. for (cfs_rq = &rq->cfs; cfs_rq; cfs_rq = NULL)
  338. static inline int
  339. is_same_group(struct sched_entity *se, struct sched_entity *pse)
  340. {
  341. return 1;
  342. }
  343. static inline struct sched_entity *parent_entity(struct sched_entity *se)
  344. {
  345. return NULL;
  346. }
  347. static inline void
  348. find_matching_se(struct sched_entity **se, struct sched_entity **pse)
  349. {
  350. }
  351. #endif /* CONFIG_FAIR_GROUP_SCHED */
  352. static __always_inline
  353. void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec);
  354. /**************************************************************
  355. * Scheduling class tree data structure manipulation methods:
  356. */
  357. static inline u64 max_vruntime(u64 min_vruntime, u64 vruntime)
  358. {
  359. s64 delta = (s64)(vruntime - min_vruntime);
  360. if (delta > 0)
  361. min_vruntime = vruntime;
  362. return min_vruntime;
  363. }
  364. static inline u64 min_vruntime(u64 min_vruntime, u64 vruntime)
  365. {
  366. s64 delta = (s64)(vruntime - min_vruntime);
  367. if (delta < 0)
  368. min_vruntime = vruntime;
  369. return min_vruntime;
  370. }
  371. static inline int entity_before(struct sched_entity *a,
  372. struct sched_entity *b)
  373. {
  374. return (s64)(a->vruntime - b->vruntime) < 0;
  375. }
  376. static void update_min_vruntime(struct cfs_rq *cfs_rq)
  377. {
  378. u64 vruntime = cfs_rq->min_vruntime;
  379. if (cfs_rq->curr)
  380. vruntime = cfs_rq->curr->vruntime;
  381. if (cfs_rq->rb_leftmost) {
  382. struct sched_entity *se = rb_entry(cfs_rq->rb_leftmost,
  383. struct sched_entity,
  384. run_node);
  385. if (!cfs_rq->curr)
  386. vruntime = se->vruntime;
  387. else
  388. vruntime = min_vruntime(vruntime, se->vruntime);
  389. }
  390. cfs_rq->min_vruntime = max_vruntime(cfs_rq->min_vruntime, vruntime);
  391. #ifndef CONFIG_64BIT
  392. smp_wmb();
  393. cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
  394. #endif
  395. }
  396. /*
  397. * Enqueue an entity into the rb-tree:
  398. */
  399. static void __enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  400. {
  401. struct rb_node **link = &cfs_rq->tasks_timeline.rb_node;
  402. struct rb_node *parent = NULL;
  403. struct sched_entity *entry;
  404. int leftmost = 1;
  405. /*
  406. * Find the right place in the rbtree:
  407. */
  408. while (*link) {
  409. parent = *link;
  410. entry = rb_entry(parent, struct sched_entity, run_node);
  411. /*
  412. * We dont care about collisions. Nodes with
  413. * the same key stay together.
  414. */
  415. if (entity_before(se, entry)) {
  416. link = &parent->rb_left;
  417. } else {
  418. link = &parent->rb_right;
  419. leftmost = 0;
  420. }
  421. }
  422. /*
  423. * Maintain a cache of leftmost tree entries (it is frequently
  424. * used):
  425. */
  426. if (leftmost)
  427. cfs_rq->rb_leftmost = &se->run_node;
  428. rb_link_node(&se->run_node, parent, link);
  429. rb_insert_color(&se->run_node, &cfs_rq->tasks_timeline);
  430. }
  431. static void __dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  432. {
  433. if (cfs_rq->rb_leftmost == &se->run_node) {
  434. struct rb_node *next_node;
  435. next_node = rb_next(&se->run_node);
  436. cfs_rq->rb_leftmost = next_node;
  437. }
  438. rb_erase(&se->run_node, &cfs_rq->tasks_timeline);
  439. }
  440. struct sched_entity *__pick_first_entity(struct cfs_rq *cfs_rq)
  441. {
  442. struct rb_node *left = cfs_rq->rb_leftmost;
  443. if (!left)
  444. return NULL;
  445. return rb_entry(left, struct sched_entity, run_node);
  446. }
  447. static struct sched_entity *__pick_next_entity(struct sched_entity *se)
  448. {
  449. struct rb_node *next = rb_next(&se->run_node);
  450. if (!next)
  451. return NULL;
  452. return rb_entry(next, struct sched_entity, run_node);
  453. }
  454. #ifdef CONFIG_SCHED_DEBUG
  455. struct sched_entity *__pick_last_entity(struct cfs_rq *cfs_rq)
  456. {
  457. struct rb_node *last = rb_last(&cfs_rq->tasks_timeline);
  458. if (!last)
  459. return NULL;
  460. return rb_entry(last, struct sched_entity, run_node);
  461. }
  462. /**************************************************************
  463. * Scheduling class statistics methods:
  464. */
  465. int sched_proc_update_handler(struct ctl_table *table, int write,
  466. void __user *buffer, size_t *lenp,
  467. loff_t *ppos)
  468. {
  469. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  470. int factor = get_update_sysctl_factor();
  471. if (ret || !write)
  472. return ret;
  473. sched_nr_latency = DIV_ROUND_UP(sysctl_sched_latency,
  474. sysctl_sched_min_granularity);
  475. #define WRT_SYSCTL(name) \
  476. (normalized_sysctl_##name = sysctl_##name / (factor))
  477. WRT_SYSCTL(sched_min_granularity);
  478. WRT_SYSCTL(sched_latency);
  479. WRT_SYSCTL(sched_wakeup_granularity);
  480. #undef WRT_SYSCTL
  481. return 0;
  482. }
  483. #endif
  484. /*
  485. * delta /= w
  486. */
  487. static inline unsigned long
  488. calc_delta_fair(unsigned long delta, struct sched_entity *se)
  489. {
  490. if (unlikely(se->load.weight != NICE_0_LOAD))
  491. delta = calc_delta_mine(delta, NICE_0_LOAD, &se->load);
  492. return delta;
  493. }
  494. /*
  495. * The idea is to set a period in which each task runs once.
  496. *
  497. * When there are too many tasks (sched_nr_latency) we have to stretch
  498. * this period because otherwise the slices get too small.
  499. *
  500. * p = (nr <= nl) ? l : l*nr/nl
  501. */
  502. static u64 __sched_period(unsigned long nr_running)
  503. {
  504. u64 period = sysctl_sched_latency;
  505. unsigned long nr_latency = sched_nr_latency;
  506. if (unlikely(nr_running > nr_latency)) {
  507. period = sysctl_sched_min_granularity;
  508. period *= nr_running;
  509. }
  510. return period;
  511. }
  512. /*
  513. * We calculate the wall-time slice from the period by taking a part
  514. * proportional to the weight.
  515. *
  516. * s = p*P[w/rw]
  517. */
  518. static u64 sched_slice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  519. {
  520. u64 slice = __sched_period(cfs_rq->nr_running + !se->on_rq);
  521. for_each_sched_entity(se) {
  522. struct load_weight *load;
  523. struct load_weight lw;
  524. cfs_rq = cfs_rq_of(se);
  525. load = &cfs_rq->load;
  526. if (unlikely(!se->on_rq)) {
  527. lw = cfs_rq->load;
  528. update_load_add(&lw, se->load.weight);
  529. load = &lw;
  530. }
  531. slice = calc_delta_mine(slice, se->load.weight, load);
  532. }
  533. return slice;
  534. }
  535. /*
  536. * We calculate the vruntime slice of a to be inserted task
  537. *
  538. * vs = s/w
  539. */
  540. static u64 sched_vslice(struct cfs_rq *cfs_rq, struct sched_entity *se)
  541. {
  542. return calc_delta_fair(sched_slice(cfs_rq, se), se);
  543. }
  544. /*
  545. * Update the current task's runtime statistics. Skip current tasks that
  546. * are not in our scheduling class.
  547. */
  548. static inline void
  549. __update_curr(struct cfs_rq *cfs_rq, struct sched_entity *curr,
  550. unsigned long delta_exec)
  551. {
  552. unsigned long delta_exec_weighted;
  553. schedstat_set(curr->statistics.exec_max,
  554. max((u64)delta_exec, curr->statistics.exec_max));
  555. curr->sum_exec_runtime += delta_exec;
  556. schedstat_add(cfs_rq, exec_clock, delta_exec);
  557. delta_exec_weighted = calc_delta_fair(delta_exec, curr);
  558. curr->vruntime += delta_exec_weighted;
  559. update_min_vruntime(cfs_rq);
  560. }
  561. static void update_curr(struct cfs_rq *cfs_rq)
  562. {
  563. struct sched_entity *curr = cfs_rq->curr;
  564. u64 now = rq_of(cfs_rq)->clock_task;
  565. unsigned long delta_exec;
  566. if (unlikely(!curr))
  567. return;
  568. /*
  569. * Get the amount of time the current task was running
  570. * since the last time we changed load (this cannot
  571. * overflow on 32 bits):
  572. */
  573. delta_exec = (unsigned long)(now - curr->exec_start);
  574. if (!delta_exec)
  575. return;
  576. __update_curr(cfs_rq, curr, delta_exec);
  577. curr->exec_start = now;
  578. if (entity_is_task(curr)) {
  579. struct task_struct *curtask = task_of(curr);
  580. trace_sched_stat_runtime(curtask, delta_exec, curr->vruntime);
  581. cpuacct_charge(curtask, delta_exec);
  582. account_group_exec_runtime(curtask, delta_exec);
  583. }
  584. account_cfs_rq_runtime(cfs_rq, delta_exec);
  585. }
  586. static inline void
  587. update_stats_wait_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  588. {
  589. schedstat_set(se->statistics.wait_start, rq_of(cfs_rq)->clock);
  590. }
  591. /*
  592. * Task is being enqueued - update stats:
  593. */
  594. static void update_stats_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  595. {
  596. /*
  597. * Are we enqueueing a waiting task? (for current tasks
  598. * a dequeue/enqueue event is a NOP)
  599. */
  600. if (se != cfs_rq->curr)
  601. update_stats_wait_start(cfs_rq, se);
  602. }
  603. static void
  604. update_stats_wait_end(struct cfs_rq *cfs_rq, struct sched_entity *se)
  605. {
  606. schedstat_set(se->statistics.wait_max, max(se->statistics.wait_max,
  607. rq_of(cfs_rq)->clock - se->statistics.wait_start));
  608. schedstat_set(se->statistics.wait_count, se->statistics.wait_count + 1);
  609. schedstat_set(se->statistics.wait_sum, se->statistics.wait_sum +
  610. rq_of(cfs_rq)->clock - se->statistics.wait_start);
  611. #ifdef CONFIG_SCHEDSTATS
  612. if (entity_is_task(se)) {
  613. trace_sched_stat_wait(task_of(se),
  614. rq_of(cfs_rq)->clock - se->statistics.wait_start);
  615. }
  616. #endif
  617. schedstat_set(se->statistics.wait_start, 0);
  618. }
  619. static inline void
  620. update_stats_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  621. {
  622. /*
  623. * Mark the end of the wait period if dequeueing a
  624. * waiting task:
  625. */
  626. if (se != cfs_rq->curr)
  627. update_stats_wait_end(cfs_rq, se);
  628. }
  629. /*
  630. * We are picking a new current task - update its stats:
  631. */
  632. static inline void
  633. update_stats_curr_start(struct cfs_rq *cfs_rq, struct sched_entity *se)
  634. {
  635. /*
  636. * We are starting a new run period:
  637. */
  638. se->exec_start = rq_of(cfs_rq)->clock_task;
  639. }
  640. /**************************************************
  641. * Scheduling class queueing methods:
  642. */
  643. static void
  644. account_entity_enqueue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  645. {
  646. update_load_add(&cfs_rq->load, se->load.weight);
  647. if (!parent_entity(se))
  648. update_load_add(&rq_of(cfs_rq)->load, se->load.weight);
  649. #ifdef CONFIG_SMP
  650. if (entity_is_task(se))
  651. list_add(&se->group_node, &rq_of(cfs_rq)->cfs_tasks);
  652. #endif
  653. cfs_rq->nr_running++;
  654. }
  655. static void
  656. account_entity_dequeue(struct cfs_rq *cfs_rq, struct sched_entity *se)
  657. {
  658. update_load_sub(&cfs_rq->load, se->load.weight);
  659. if (!parent_entity(se))
  660. update_load_sub(&rq_of(cfs_rq)->load, se->load.weight);
  661. if (entity_is_task(se))
  662. list_del_init(&se->group_node);
  663. cfs_rq->nr_running--;
  664. }
  665. #ifdef CONFIG_FAIR_GROUP_SCHED
  666. # ifdef CONFIG_SMP
  667. static inline long calc_tg_weight(struct task_group *tg, struct cfs_rq *cfs_rq)
  668. {
  669. long tg_weight;
  670. /*
  671. * Use this CPU's actual weight instead of the last load_contribution
  672. * to gain a more accurate current total weight. See
  673. * update_cfs_rq_load_contribution().
  674. */
  675. tg_weight = atomic64_read(&tg->load_avg);
  676. tg_weight -= cfs_rq->tg_load_contrib;
  677. tg_weight += cfs_rq->load.weight;
  678. return tg_weight;
  679. }
  680. static long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
  681. {
  682. long tg_weight, load, shares;
  683. tg_weight = calc_tg_weight(tg, cfs_rq);
  684. load = cfs_rq->load.weight;
  685. shares = (tg->shares * load);
  686. if (tg_weight)
  687. shares /= tg_weight;
  688. if (shares < MIN_SHARES)
  689. shares = MIN_SHARES;
  690. if (shares > tg->shares)
  691. shares = tg->shares;
  692. return shares;
  693. }
  694. # else /* CONFIG_SMP */
  695. static inline long calc_cfs_shares(struct cfs_rq *cfs_rq, struct task_group *tg)
  696. {
  697. return tg->shares;
  698. }
  699. # endif /* CONFIG_SMP */
  700. static void reweight_entity(struct cfs_rq *cfs_rq, struct sched_entity *se,
  701. unsigned long weight)
  702. {
  703. if (se->on_rq) {
  704. /* commit outstanding execution time */
  705. if (cfs_rq->curr == se)
  706. update_curr(cfs_rq);
  707. account_entity_dequeue(cfs_rq, se);
  708. }
  709. update_load_set(&se->load, weight);
  710. if (se->on_rq)
  711. account_entity_enqueue(cfs_rq, se);
  712. }
  713. static inline int throttled_hierarchy(struct cfs_rq *cfs_rq);
  714. static void update_cfs_shares(struct cfs_rq *cfs_rq)
  715. {
  716. struct task_group *tg;
  717. struct sched_entity *se;
  718. long shares;
  719. tg = cfs_rq->tg;
  720. se = tg->se[cpu_of(rq_of(cfs_rq))];
  721. if (!se || throttled_hierarchy(cfs_rq))
  722. return;
  723. #ifndef CONFIG_SMP
  724. if (likely(se->load.weight == tg->shares))
  725. return;
  726. #endif
  727. shares = calc_cfs_shares(cfs_rq, tg);
  728. reweight_entity(cfs_rq_of(se), se, shares);
  729. }
  730. #else /* CONFIG_FAIR_GROUP_SCHED */
  731. static inline void update_cfs_shares(struct cfs_rq *cfs_rq)
  732. {
  733. }
  734. #endif /* CONFIG_FAIR_GROUP_SCHED */
  735. #ifdef CONFIG_SMP
  736. /*
  737. * Approximate:
  738. * val * y^n, where y^32 ~= 0.5 (~1 scheduling period)
  739. */
  740. static __always_inline u64 decay_load(u64 val, u64 n)
  741. {
  742. for (; n && val; n--) {
  743. val *= 4008;
  744. val >>= 12;
  745. }
  746. return val;
  747. }
  748. /*
  749. * We can represent the historical contribution to runnable average as the
  750. * coefficients of a geometric series. To do this we sub-divide our runnable
  751. * history into segments of approximately 1ms (1024us); label the segment that
  752. * occurred N-ms ago p_N, with p_0 corresponding to the current period, e.g.
  753. *
  754. * [<- 1024us ->|<- 1024us ->|<- 1024us ->| ...
  755. * p0 p1 p2
  756. * (now) (~1ms ago) (~2ms ago)
  757. *
  758. * Let u_i denote the fraction of p_i that the entity was runnable.
  759. *
  760. * We then designate the fractions u_i as our co-efficients, yielding the
  761. * following representation of historical load:
  762. * u_0 + u_1*y + u_2*y^2 + u_3*y^3 + ...
  763. *
  764. * We choose y based on the with of a reasonably scheduling period, fixing:
  765. * y^32 = 0.5
  766. *
  767. * This means that the contribution to load ~32ms ago (u_32) will be weighted
  768. * approximately half as much as the contribution to load within the last ms
  769. * (u_0).
  770. *
  771. * When a period "rolls over" and we have new u_0`, multiplying the previous
  772. * sum again by y is sufficient to update:
  773. * load_avg = u_0` + y*(u_0 + u_1*y + u_2*y^2 + ... )
  774. * = u_0 + u_1*y + u_2*y^2 + ... [re-labeling u_i --> u_{i+1}]
  775. */
  776. static __always_inline int __update_entity_runnable_avg(u64 now,
  777. struct sched_avg *sa,
  778. int runnable)
  779. {
  780. u64 delta;
  781. int delta_w, decayed = 0;
  782. delta = now - sa->last_runnable_update;
  783. /*
  784. * This should only happen when time goes backwards, which it
  785. * unfortunately does during sched clock init when we swap over to TSC.
  786. */
  787. if ((s64)delta < 0) {
  788. sa->last_runnable_update = now;
  789. return 0;
  790. }
  791. /*
  792. * Use 1024ns as the unit of measurement since it's a reasonable
  793. * approximation of 1us and fast to compute.
  794. */
  795. delta >>= 10;
  796. if (!delta)
  797. return 0;
  798. sa->last_runnable_update = now;
  799. /* delta_w is the amount already accumulated against our next period */
  800. delta_w = sa->runnable_avg_period % 1024;
  801. if (delta + delta_w >= 1024) {
  802. /* period roll-over */
  803. decayed = 1;
  804. /*
  805. * Now that we know we're crossing a period boundary, figure
  806. * out how much from delta we need to complete the current
  807. * period and accrue it.
  808. */
  809. delta_w = 1024 - delta_w;
  810. BUG_ON(delta_w > delta);
  811. do {
  812. if (runnable)
  813. sa->runnable_avg_sum += delta_w;
  814. sa->runnable_avg_period += delta_w;
  815. /*
  816. * Remainder of delta initiates a new period, roll over
  817. * the previous.
  818. */
  819. sa->runnable_avg_sum =
  820. decay_load(sa->runnable_avg_sum, 1);
  821. sa->runnable_avg_period =
  822. decay_load(sa->runnable_avg_period, 1);
  823. delta -= delta_w;
  824. /* New period is empty */
  825. delta_w = 1024;
  826. } while (delta >= 1024);
  827. }
  828. /* Remainder of delta accrued against u_0` */
  829. if (runnable)
  830. sa->runnable_avg_sum += delta;
  831. sa->runnable_avg_period += delta;
  832. return decayed;
  833. }
  834. /* Synchronize an entity's decay with its parenting cfs_rq.*/
  835. static inline u64 __synchronize_entity_decay(struct sched_entity *se)
  836. {
  837. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  838. u64 decays = atomic64_read(&cfs_rq->decay_counter);
  839. decays -= se->avg.decay_count;
  840. if (!decays)
  841. return 0;
  842. se->avg.load_avg_contrib = decay_load(se->avg.load_avg_contrib, decays);
  843. se->avg.decay_count = 0;
  844. return decays;
  845. }
  846. #ifdef CONFIG_FAIR_GROUP_SCHED
  847. static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
  848. int force_update)
  849. {
  850. struct task_group *tg = cfs_rq->tg;
  851. s64 tg_contrib;
  852. tg_contrib = cfs_rq->runnable_load_avg + cfs_rq->blocked_load_avg;
  853. tg_contrib -= cfs_rq->tg_load_contrib;
  854. if (force_update || abs64(tg_contrib) > cfs_rq->tg_load_contrib / 8) {
  855. atomic64_add(tg_contrib, &tg->load_avg);
  856. cfs_rq->tg_load_contrib += tg_contrib;
  857. }
  858. }
  859. /*
  860. * Aggregate cfs_rq runnable averages into an equivalent task_group
  861. * representation for computing load contributions.
  862. */
  863. static inline void __update_tg_runnable_avg(struct sched_avg *sa,
  864. struct cfs_rq *cfs_rq)
  865. {
  866. struct task_group *tg = cfs_rq->tg;
  867. long contrib;
  868. /* The fraction of a cpu used by this cfs_rq */
  869. contrib = div_u64(sa->runnable_avg_sum << NICE_0_SHIFT,
  870. sa->runnable_avg_period + 1);
  871. contrib -= cfs_rq->tg_runnable_contrib;
  872. if (abs(contrib) > cfs_rq->tg_runnable_contrib / 64) {
  873. atomic_add(contrib, &tg->runnable_avg);
  874. cfs_rq->tg_runnable_contrib += contrib;
  875. }
  876. }
  877. static inline void __update_group_entity_contrib(struct sched_entity *se)
  878. {
  879. struct cfs_rq *cfs_rq = group_cfs_rq(se);
  880. struct task_group *tg = cfs_rq->tg;
  881. int runnable_avg;
  882. u64 contrib;
  883. contrib = cfs_rq->tg_load_contrib * tg->shares;
  884. se->avg.load_avg_contrib = div64_u64(contrib,
  885. atomic64_read(&tg->load_avg) + 1);
  886. /*
  887. * For group entities we need to compute a correction term in the case
  888. * that they are consuming <1 cpu so that we would contribute the same
  889. * load as a task of equal weight.
  890. *
  891. * Explicitly co-ordinating this measurement would be expensive, but
  892. * fortunately the sum of each cpus contribution forms a usable
  893. * lower-bound on the true value.
  894. *
  895. * Consider the aggregate of 2 contributions. Either they are disjoint
  896. * (and the sum represents true value) or they are disjoint and we are
  897. * understating by the aggregate of their overlap.
  898. *
  899. * Extending this to N cpus, for a given overlap, the maximum amount we
  900. * understand is then n_i(n_i+1)/2 * w_i where n_i is the number of
  901. * cpus that overlap for this interval and w_i is the interval width.
  902. *
  903. * On a small machine; the first term is well-bounded which bounds the
  904. * total error since w_i is a subset of the period. Whereas on a
  905. * larger machine, while this first term can be larger, if w_i is the
  906. * of consequential size guaranteed to see n_i*w_i quickly converge to
  907. * our upper bound of 1-cpu.
  908. */
  909. runnable_avg = atomic_read(&tg->runnable_avg);
  910. if (runnable_avg < NICE_0_LOAD) {
  911. se->avg.load_avg_contrib *= runnable_avg;
  912. se->avg.load_avg_contrib >>= NICE_0_SHIFT;
  913. }
  914. }
  915. #else
  916. static inline void __update_cfs_rq_tg_load_contrib(struct cfs_rq *cfs_rq,
  917. int force_update) {}
  918. static inline void __update_tg_runnable_avg(struct sched_avg *sa,
  919. struct cfs_rq *cfs_rq) {}
  920. static inline void __update_group_entity_contrib(struct sched_entity *se) {}
  921. #endif
  922. static inline void __update_task_entity_contrib(struct sched_entity *se)
  923. {
  924. u32 contrib;
  925. /* avoid overflowing a 32-bit type w/ SCHED_LOAD_SCALE */
  926. contrib = se->avg.runnable_avg_sum * scale_load_down(se->load.weight);
  927. contrib /= (se->avg.runnable_avg_period + 1);
  928. se->avg.load_avg_contrib = scale_load(contrib);
  929. }
  930. /* Compute the current contribution to load_avg by se, return any delta */
  931. static long __update_entity_load_avg_contrib(struct sched_entity *se)
  932. {
  933. long old_contrib = se->avg.load_avg_contrib;
  934. if (entity_is_task(se)) {
  935. __update_task_entity_contrib(se);
  936. } else {
  937. __update_tg_runnable_avg(&se->avg, group_cfs_rq(se));
  938. __update_group_entity_contrib(se);
  939. }
  940. return se->avg.load_avg_contrib - old_contrib;
  941. }
  942. static inline void subtract_blocked_load_contrib(struct cfs_rq *cfs_rq,
  943. long load_contrib)
  944. {
  945. if (likely(load_contrib < cfs_rq->blocked_load_avg))
  946. cfs_rq->blocked_load_avg -= load_contrib;
  947. else
  948. cfs_rq->blocked_load_avg = 0;
  949. }
  950. static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq);
  951. /* Update a sched_entity's runnable average */
  952. static inline void update_entity_load_avg(struct sched_entity *se,
  953. int update_cfs_rq)
  954. {
  955. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  956. long contrib_delta;
  957. u64 now;
  958. /*
  959. * For a group entity we need to use their owned cfs_rq_clock_task() in
  960. * case they are the parent of a throttled hierarchy.
  961. */
  962. if (entity_is_task(se))
  963. now = cfs_rq_clock_task(cfs_rq);
  964. else
  965. now = cfs_rq_clock_task(group_cfs_rq(se));
  966. if (!__update_entity_runnable_avg(now, &se->avg, se->on_rq))
  967. return;
  968. contrib_delta = __update_entity_load_avg_contrib(se);
  969. if (!update_cfs_rq)
  970. return;
  971. if (se->on_rq)
  972. cfs_rq->runnable_load_avg += contrib_delta;
  973. else
  974. subtract_blocked_load_contrib(cfs_rq, -contrib_delta);
  975. }
  976. /*
  977. * Decay the load contributed by all blocked children and account this so that
  978. * their contribution may appropriately discounted when they wake up.
  979. */
  980. static void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq, int force_update)
  981. {
  982. u64 now = cfs_rq_clock_task(cfs_rq) >> 20;
  983. u64 decays;
  984. decays = now - cfs_rq->last_decay;
  985. if (!decays && !force_update)
  986. return;
  987. if (atomic64_read(&cfs_rq->removed_load)) {
  988. u64 removed_load = atomic64_xchg(&cfs_rq->removed_load, 0);
  989. subtract_blocked_load_contrib(cfs_rq, removed_load);
  990. }
  991. if (decays) {
  992. cfs_rq->blocked_load_avg = decay_load(cfs_rq->blocked_load_avg,
  993. decays);
  994. atomic64_add(decays, &cfs_rq->decay_counter);
  995. cfs_rq->last_decay = now;
  996. }
  997. __update_cfs_rq_tg_load_contrib(cfs_rq, force_update);
  998. }
  999. static inline void update_rq_runnable_avg(struct rq *rq, int runnable)
  1000. {
  1001. __update_entity_runnable_avg(rq->clock_task, &rq->avg, runnable);
  1002. __update_tg_runnable_avg(&rq->avg, &rq->cfs);
  1003. }
  1004. /* Add the load generated by se into cfs_rq's child load-average */
  1005. static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
  1006. struct sched_entity *se,
  1007. int wakeup)
  1008. {
  1009. /*
  1010. * We track migrations using entity decay_count <= 0, on a wake-up
  1011. * migration we use a negative decay count to track the remote decays
  1012. * accumulated while sleeping.
  1013. */
  1014. if (unlikely(se->avg.decay_count <= 0)) {
  1015. se->avg.last_runnable_update = rq_of(cfs_rq)->clock_task;
  1016. if (se->avg.decay_count) {
  1017. /*
  1018. * In a wake-up migration we have to approximate the
  1019. * time sleeping. This is because we can't synchronize
  1020. * clock_task between the two cpus, and it is not
  1021. * guaranteed to be read-safe. Instead, we can
  1022. * approximate this using our carried decays, which are
  1023. * explicitly atomically readable.
  1024. */
  1025. se->avg.last_runnable_update -= (-se->avg.decay_count)
  1026. << 20;
  1027. update_entity_load_avg(se, 0);
  1028. /* Indicate that we're now synchronized and on-rq */
  1029. se->avg.decay_count = 0;
  1030. }
  1031. wakeup = 0;
  1032. } else {
  1033. __synchronize_entity_decay(se);
  1034. }
  1035. /* migrated tasks did not contribute to our blocked load */
  1036. if (wakeup) {
  1037. subtract_blocked_load_contrib(cfs_rq, se->avg.load_avg_contrib);
  1038. update_entity_load_avg(se, 0);
  1039. }
  1040. cfs_rq->runnable_load_avg += se->avg.load_avg_contrib;
  1041. /* we force update consideration on load-balancer moves */
  1042. update_cfs_rq_blocked_load(cfs_rq, !wakeup);
  1043. }
  1044. /*
  1045. * Remove se's load from this cfs_rq child load-average, if the entity is
  1046. * transitioning to a blocked state we track its projected decay using
  1047. * blocked_load_avg.
  1048. */
  1049. static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
  1050. struct sched_entity *se,
  1051. int sleep)
  1052. {
  1053. update_entity_load_avg(se, 1);
  1054. /* we force update consideration on load-balancer moves */
  1055. update_cfs_rq_blocked_load(cfs_rq, !sleep);
  1056. cfs_rq->runnable_load_avg -= se->avg.load_avg_contrib;
  1057. if (sleep) {
  1058. cfs_rq->blocked_load_avg += se->avg.load_avg_contrib;
  1059. se->avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
  1060. } /* migrations, e.g. sleep=0 leave decay_count == 0 */
  1061. }
  1062. #else
  1063. static inline void update_entity_load_avg(struct sched_entity *se,
  1064. int update_cfs_rq) {}
  1065. static inline void update_rq_runnable_avg(struct rq *rq, int runnable) {}
  1066. static inline void enqueue_entity_load_avg(struct cfs_rq *cfs_rq,
  1067. struct sched_entity *se,
  1068. int wakeup) {}
  1069. static inline void dequeue_entity_load_avg(struct cfs_rq *cfs_rq,
  1070. struct sched_entity *se,
  1071. int sleep) {}
  1072. static inline void update_cfs_rq_blocked_load(struct cfs_rq *cfs_rq,
  1073. int force_update) {}
  1074. #endif
  1075. static void enqueue_sleeper(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1076. {
  1077. #ifdef CONFIG_SCHEDSTATS
  1078. struct task_struct *tsk = NULL;
  1079. if (entity_is_task(se))
  1080. tsk = task_of(se);
  1081. if (se->statistics.sleep_start) {
  1082. u64 delta = rq_of(cfs_rq)->clock - se->statistics.sleep_start;
  1083. if ((s64)delta < 0)
  1084. delta = 0;
  1085. if (unlikely(delta > se->statistics.sleep_max))
  1086. se->statistics.sleep_max = delta;
  1087. se->statistics.sleep_start = 0;
  1088. se->statistics.sum_sleep_runtime += delta;
  1089. if (tsk) {
  1090. account_scheduler_latency(tsk, delta >> 10, 1);
  1091. trace_sched_stat_sleep(tsk, delta);
  1092. }
  1093. }
  1094. if (se->statistics.block_start) {
  1095. u64 delta = rq_of(cfs_rq)->clock - se->statistics.block_start;
  1096. if ((s64)delta < 0)
  1097. delta = 0;
  1098. if (unlikely(delta > se->statistics.block_max))
  1099. se->statistics.block_max = delta;
  1100. se->statistics.block_start = 0;
  1101. se->statistics.sum_sleep_runtime += delta;
  1102. if (tsk) {
  1103. if (tsk->in_iowait) {
  1104. se->statistics.iowait_sum += delta;
  1105. se->statistics.iowait_count++;
  1106. trace_sched_stat_iowait(tsk, delta);
  1107. }
  1108. trace_sched_stat_blocked(tsk, delta);
  1109. /*
  1110. * Blocking time is in units of nanosecs, so shift by
  1111. * 20 to get a milliseconds-range estimation of the
  1112. * amount of time that the task spent sleeping:
  1113. */
  1114. if (unlikely(prof_on == SLEEP_PROFILING)) {
  1115. profile_hits(SLEEP_PROFILING,
  1116. (void *)get_wchan(tsk),
  1117. delta >> 20);
  1118. }
  1119. account_scheduler_latency(tsk, delta >> 10, 0);
  1120. }
  1121. }
  1122. #endif
  1123. }
  1124. static void check_spread(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1125. {
  1126. #ifdef CONFIG_SCHED_DEBUG
  1127. s64 d = se->vruntime - cfs_rq->min_vruntime;
  1128. if (d < 0)
  1129. d = -d;
  1130. if (d > 3*sysctl_sched_latency)
  1131. schedstat_inc(cfs_rq, nr_spread_over);
  1132. #endif
  1133. }
  1134. static void
  1135. place_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int initial)
  1136. {
  1137. u64 vruntime = cfs_rq->min_vruntime;
  1138. /*
  1139. * The 'current' period is already promised to the current tasks,
  1140. * however the extra weight of the new task will slow them down a
  1141. * little, place the new task so that it fits in the slot that
  1142. * stays open at the end.
  1143. */
  1144. if (initial && sched_feat(START_DEBIT))
  1145. vruntime += sched_vslice(cfs_rq, se);
  1146. /* sleeps up to a single latency don't count. */
  1147. if (!initial) {
  1148. unsigned long thresh = sysctl_sched_latency;
  1149. /*
  1150. * Halve their sleep time's effect, to allow
  1151. * for a gentler effect of sleepers:
  1152. */
  1153. if (sched_feat(GENTLE_FAIR_SLEEPERS))
  1154. thresh >>= 1;
  1155. vruntime -= thresh;
  1156. }
  1157. /* ensure we never gain time by being placed backwards. */
  1158. vruntime = max_vruntime(se->vruntime, vruntime);
  1159. se->vruntime = vruntime;
  1160. }
  1161. static void check_enqueue_throttle(struct cfs_rq *cfs_rq);
  1162. static void
  1163. enqueue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
  1164. {
  1165. /*
  1166. * Update the normalized vruntime before updating min_vruntime
  1167. * through callig update_curr().
  1168. */
  1169. if (!(flags & ENQUEUE_WAKEUP) || (flags & ENQUEUE_WAKING))
  1170. se->vruntime += cfs_rq->min_vruntime;
  1171. /*
  1172. * Update run-time statistics of the 'current'.
  1173. */
  1174. update_curr(cfs_rq);
  1175. enqueue_entity_load_avg(cfs_rq, se, flags & ENQUEUE_WAKEUP);
  1176. account_entity_enqueue(cfs_rq, se);
  1177. update_cfs_shares(cfs_rq);
  1178. if (flags & ENQUEUE_WAKEUP) {
  1179. place_entity(cfs_rq, se, 0);
  1180. enqueue_sleeper(cfs_rq, se);
  1181. }
  1182. update_stats_enqueue(cfs_rq, se);
  1183. check_spread(cfs_rq, se);
  1184. if (se != cfs_rq->curr)
  1185. __enqueue_entity(cfs_rq, se);
  1186. se->on_rq = 1;
  1187. if (cfs_rq->nr_running == 1) {
  1188. list_add_leaf_cfs_rq(cfs_rq);
  1189. check_enqueue_throttle(cfs_rq);
  1190. }
  1191. }
  1192. static void __clear_buddies_last(struct sched_entity *se)
  1193. {
  1194. for_each_sched_entity(se) {
  1195. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1196. if (cfs_rq->last == se)
  1197. cfs_rq->last = NULL;
  1198. else
  1199. break;
  1200. }
  1201. }
  1202. static void __clear_buddies_next(struct sched_entity *se)
  1203. {
  1204. for_each_sched_entity(se) {
  1205. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1206. if (cfs_rq->next == se)
  1207. cfs_rq->next = NULL;
  1208. else
  1209. break;
  1210. }
  1211. }
  1212. static void __clear_buddies_skip(struct sched_entity *se)
  1213. {
  1214. for_each_sched_entity(se) {
  1215. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  1216. if (cfs_rq->skip == se)
  1217. cfs_rq->skip = NULL;
  1218. else
  1219. break;
  1220. }
  1221. }
  1222. static void clear_buddies(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1223. {
  1224. if (cfs_rq->last == se)
  1225. __clear_buddies_last(se);
  1226. if (cfs_rq->next == se)
  1227. __clear_buddies_next(se);
  1228. if (cfs_rq->skip == se)
  1229. __clear_buddies_skip(se);
  1230. }
  1231. static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq);
  1232. static void
  1233. dequeue_entity(struct cfs_rq *cfs_rq, struct sched_entity *se, int flags)
  1234. {
  1235. /*
  1236. * Update run-time statistics of the 'current'.
  1237. */
  1238. update_curr(cfs_rq);
  1239. dequeue_entity_load_avg(cfs_rq, se, flags & DEQUEUE_SLEEP);
  1240. update_stats_dequeue(cfs_rq, se);
  1241. if (flags & DEQUEUE_SLEEP) {
  1242. #ifdef CONFIG_SCHEDSTATS
  1243. if (entity_is_task(se)) {
  1244. struct task_struct *tsk = task_of(se);
  1245. if (tsk->state & TASK_INTERRUPTIBLE)
  1246. se->statistics.sleep_start = rq_of(cfs_rq)->clock;
  1247. if (tsk->state & TASK_UNINTERRUPTIBLE)
  1248. se->statistics.block_start = rq_of(cfs_rq)->clock;
  1249. }
  1250. #endif
  1251. }
  1252. clear_buddies(cfs_rq, se);
  1253. if (se != cfs_rq->curr)
  1254. __dequeue_entity(cfs_rq, se);
  1255. se->on_rq = 0;
  1256. account_entity_dequeue(cfs_rq, se);
  1257. /*
  1258. * Normalize the entity after updating the min_vruntime because the
  1259. * update can refer to the ->curr item and we need to reflect this
  1260. * movement in our normalized position.
  1261. */
  1262. if (!(flags & DEQUEUE_SLEEP))
  1263. se->vruntime -= cfs_rq->min_vruntime;
  1264. /* return excess runtime on last dequeue */
  1265. return_cfs_rq_runtime(cfs_rq);
  1266. update_min_vruntime(cfs_rq);
  1267. update_cfs_shares(cfs_rq);
  1268. }
  1269. /*
  1270. * Preempt the current task with a newly woken task if needed:
  1271. */
  1272. static void
  1273. check_preempt_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr)
  1274. {
  1275. unsigned long ideal_runtime, delta_exec;
  1276. struct sched_entity *se;
  1277. s64 delta;
  1278. ideal_runtime = sched_slice(cfs_rq, curr);
  1279. delta_exec = curr->sum_exec_runtime - curr->prev_sum_exec_runtime;
  1280. if (delta_exec > ideal_runtime) {
  1281. resched_task(rq_of(cfs_rq)->curr);
  1282. /*
  1283. * The current task ran long enough, ensure it doesn't get
  1284. * re-elected due to buddy favours.
  1285. */
  1286. clear_buddies(cfs_rq, curr);
  1287. return;
  1288. }
  1289. /*
  1290. * Ensure that a task that missed wakeup preemption by a
  1291. * narrow margin doesn't have to wait for a full slice.
  1292. * This also mitigates buddy induced latencies under load.
  1293. */
  1294. if (delta_exec < sysctl_sched_min_granularity)
  1295. return;
  1296. se = __pick_first_entity(cfs_rq);
  1297. delta = curr->vruntime - se->vruntime;
  1298. if (delta < 0)
  1299. return;
  1300. if (delta > ideal_runtime)
  1301. resched_task(rq_of(cfs_rq)->curr);
  1302. }
  1303. static void
  1304. set_next_entity(struct cfs_rq *cfs_rq, struct sched_entity *se)
  1305. {
  1306. /* 'current' is not kept within the tree. */
  1307. if (se->on_rq) {
  1308. /*
  1309. * Any task has to be enqueued before it get to execute on
  1310. * a CPU. So account for the time it spent waiting on the
  1311. * runqueue.
  1312. */
  1313. update_stats_wait_end(cfs_rq, se);
  1314. __dequeue_entity(cfs_rq, se);
  1315. }
  1316. update_stats_curr_start(cfs_rq, se);
  1317. cfs_rq->curr = se;
  1318. #ifdef CONFIG_SCHEDSTATS
  1319. /*
  1320. * Track our maximum slice length, if the CPU's load is at
  1321. * least twice that of our own weight (i.e. dont track it
  1322. * when there are only lesser-weight tasks around):
  1323. */
  1324. if (rq_of(cfs_rq)->load.weight >= 2*se->load.weight) {
  1325. se->statistics.slice_max = max(se->statistics.slice_max,
  1326. se->sum_exec_runtime - se->prev_sum_exec_runtime);
  1327. }
  1328. #endif
  1329. se->prev_sum_exec_runtime = se->sum_exec_runtime;
  1330. }
  1331. static int
  1332. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se);
  1333. /*
  1334. * Pick the next process, keeping these things in mind, in this order:
  1335. * 1) keep things fair between processes/task groups
  1336. * 2) pick the "next" process, since someone really wants that to run
  1337. * 3) pick the "last" process, for cache locality
  1338. * 4) do not run the "skip" process, if something else is available
  1339. */
  1340. static struct sched_entity *pick_next_entity(struct cfs_rq *cfs_rq)
  1341. {
  1342. struct sched_entity *se = __pick_first_entity(cfs_rq);
  1343. struct sched_entity *left = se;
  1344. /*
  1345. * Avoid running the skip buddy, if running something else can
  1346. * be done without getting too unfair.
  1347. */
  1348. if (cfs_rq->skip == se) {
  1349. struct sched_entity *second = __pick_next_entity(se);
  1350. if (second && wakeup_preempt_entity(second, left) < 1)
  1351. se = second;
  1352. }
  1353. /*
  1354. * Prefer last buddy, try to return the CPU to a preempted task.
  1355. */
  1356. if (cfs_rq->last && wakeup_preempt_entity(cfs_rq->last, left) < 1)
  1357. se = cfs_rq->last;
  1358. /*
  1359. * Someone really wants this to run. If it's not unfair, run it.
  1360. */
  1361. if (cfs_rq->next && wakeup_preempt_entity(cfs_rq->next, left) < 1)
  1362. se = cfs_rq->next;
  1363. clear_buddies(cfs_rq, se);
  1364. return se;
  1365. }
  1366. static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq);
  1367. static void put_prev_entity(struct cfs_rq *cfs_rq, struct sched_entity *prev)
  1368. {
  1369. /*
  1370. * If still on the runqueue then deactivate_task()
  1371. * was not called and update_curr() has to be done:
  1372. */
  1373. if (prev->on_rq)
  1374. update_curr(cfs_rq);
  1375. /* throttle cfs_rqs exceeding runtime */
  1376. check_cfs_rq_runtime(cfs_rq);
  1377. check_spread(cfs_rq, prev);
  1378. if (prev->on_rq) {
  1379. update_stats_wait_start(cfs_rq, prev);
  1380. /* Put 'current' back into the tree. */
  1381. __enqueue_entity(cfs_rq, prev);
  1382. /* in !on_rq case, update occurred at dequeue */
  1383. update_entity_load_avg(prev, 1);
  1384. }
  1385. cfs_rq->curr = NULL;
  1386. }
  1387. static void
  1388. entity_tick(struct cfs_rq *cfs_rq, struct sched_entity *curr, int queued)
  1389. {
  1390. /*
  1391. * Update run-time statistics of the 'current'.
  1392. */
  1393. update_curr(cfs_rq);
  1394. /*
  1395. * Ensure that runnable average is periodically updated.
  1396. */
  1397. update_entity_load_avg(curr, 1);
  1398. update_cfs_rq_blocked_load(cfs_rq, 1);
  1399. #ifdef CONFIG_SCHED_HRTICK
  1400. /*
  1401. * queued ticks are scheduled to match the slice, so don't bother
  1402. * validating it and just reschedule.
  1403. */
  1404. if (queued) {
  1405. resched_task(rq_of(cfs_rq)->curr);
  1406. return;
  1407. }
  1408. /*
  1409. * don't let the period tick interfere with the hrtick preemption
  1410. */
  1411. if (!sched_feat(DOUBLE_TICK) &&
  1412. hrtimer_active(&rq_of(cfs_rq)->hrtick_timer))
  1413. return;
  1414. #endif
  1415. if (cfs_rq->nr_running > 1)
  1416. check_preempt_tick(cfs_rq, curr);
  1417. }
  1418. /**************************************************
  1419. * CFS bandwidth control machinery
  1420. */
  1421. #ifdef CONFIG_CFS_BANDWIDTH
  1422. #ifdef HAVE_JUMP_LABEL
  1423. static struct static_key __cfs_bandwidth_used;
  1424. static inline bool cfs_bandwidth_used(void)
  1425. {
  1426. return static_key_false(&__cfs_bandwidth_used);
  1427. }
  1428. void account_cfs_bandwidth_used(int enabled, int was_enabled)
  1429. {
  1430. /* only need to count groups transitioning between enabled/!enabled */
  1431. if (enabled && !was_enabled)
  1432. static_key_slow_inc(&__cfs_bandwidth_used);
  1433. else if (!enabled && was_enabled)
  1434. static_key_slow_dec(&__cfs_bandwidth_used);
  1435. }
  1436. #else /* HAVE_JUMP_LABEL */
  1437. static bool cfs_bandwidth_used(void)
  1438. {
  1439. return true;
  1440. }
  1441. void account_cfs_bandwidth_used(int enabled, int was_enabled) {}
  1442. #endif /* HAVE_JUMP_LABEL */
  1443. /*
  1444. * default period for cfs group bandwidth.
  1445. * default: 0.1s, units: nanoseconds
  1446. */
  1447. static inline u64 default_cfs_period(void)
  1448. {
  1449. return 100000000ULL;
  1450. }
  1451. static inline u64 sched_cfs_bandwidth_slice(void)
  1452. {
  1453. return (u64)sysctl_sched_cfs_bandwidth_slice * NSEC_PER_USEC;
  1454. }
  1455. /*
  1456. * Replenish runtime according to assigned quota and update expiration time.
  1457. * We use sched_clock_cpu directly instead of rq->clock to avoid adding
  1458. * additional synchronization around rq->lock.
  1459. *
  1460. * requires cfs_b->lock
  1461. */
  1462. void __refill_cfs_bandwidth_runtime(struct cfs_bandwidth *cfs_b)
  1463. {
  1464. u64 now;
  1465. if (cfs_b->quota == RUNTIME_INF)
  1466. return;
  1467. now = sched_clock_cpu(smp_processor_id());
  1468. cfs_b->runtime = cfs_b->quota;
  1469. cfs_b->runtime_expires = now + ktime_to_ns(cfs_b->period);
  1470. }
  1471. static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
  1472. {
  1473. return &tg->cfs_bandwidth;
  1474. }
  1475. /* rq->task_clock normalized against any time this cfs_rq has spent throttled */
  1476. static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
  1477. {
  1478. if (unlikely(cfs_rq->throttle_count))
  1479. return cfs_rq->throttled_clock_task;
  1480. return rq_of(cfs_rq)->clock_task - cfs_rq->throttled_clock_task_time;
  1481. }
  1482. /* returns 0 on failure to allocate runtime */
  1483. static int assign_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1484. {
  1485. struct task_group *tg = cfs_rq->tg;
  1486. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(tg);
  1487. u64 amount = 0, min_amount, expires;
  1488. /* note: this is a positive sum as runtime_remaining <= 0 */
  1489. min_amount = sched_cfs_bandwidth_slice() - cfs_rq->runtime_remaining;
  1490. raw_spin_lock(&cfs_b->lock);
  1491. if (cfs_b->quota == RUNTIME_INF)
  1492. amount = min_amount;
  1493. else {
  1494. /*
  1495. * If the bandwidth pool has become inactive, then at least one
  1496. * period must have elapsed since the last consumption.
  1497. * Refresh the global state and ensure bandwidth timer becomes
  1498. * active.
  1499. */
  1500. if (!cfs_b->timer_active) {
  1501. __refill_cfs_bandwidth_runtime(cfs_b);
  1502. __start_cfs_bandwidth(cfs_b);
  1503. }
  1504. if (cfs_b->runtime > 0) {
  1505. amount = min(cfs_b->runtime, min_amount);
  1506. cfs_b->runtime -= amount;
  1507. cfs_b->idle = 0;
  1508. }
  1509. }
  1510. expires = cfs_b->runtime_expires;
  1511. raw_spin_unlock(&cfs_b->lock);
  1512. cfs_rq->runtime_remaining += amount;
  1513. /*
  1514. * we may have advanced our local expiration to account for allowed
  1515. * spread between our sched_clock and the one on which runtime was
  1516. * issued.
  1517. */
  1518. if ((s64)(expires - cfs_rq->runtime_expires) > 0)
  1519. cfs_rq->runtime_expires = expires;
  1520. return cfs_rq->runtime_remaining > 0;
  1521. }
  1522. /*
  1523. * Note: This depends on the synchronization provided by sched_clock and the
  1524. * fact that rq->clock snapshots this value.
  1525. */
  1526. static void expire_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1527. {
  1528. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1529. struct rq *rq = rq_of(cfs_rq);
  1530. /* if the deadline is ahead of our clock, nothing to do */
  1531. if (likely((s64)(rq->clock - cfs_rq->runtime_expires) < 0))
  1532. return;
  1533. if (cfs_rq->runtime_remaining < 0)
  1534. return;
  1535. /*
  1536. * If the local deadline has passed we have to consider the
  1537. * possibility that our sched_clock is 'fast' and the global deadline
  1538. * has not truly expired.
  1539. *
  1540. * Fortunately we can check determine whether this the case by checking
  1541. * whether the global deadline has advanced.
  1542. */
  1543. if ((s64)(cfs_rq->runtime_expires - cfs_b->runtime_expires) >= 0) {
  1544. /* extend local deadline, drift is bounded above by 2 ticks */
  1545. cfs_rq->runtime_expires += TICK_NSEC;
  1546. } else {
  1547. /* global deadline is ahead, expiration has passed */
  1548. cfs_rq->runtime_remaining = 0;
  1549. }
  1550. }
  1551. static void __account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
  1552. unsigned long delta_exec)
  1553. {
  1554. /* dock delta_exec before expiring quota (as it could span periods) */
  1555. cfs_rq->runtime_remaining -= delta_exec;
  1556. expire_cfs_rq_runtime(cfs_rq);
  1557. if (likely(cfs_rq->runtime_remaining > 0))
  1558. return;
  1559. /*
  1560. * if we're unable to extend our runtime we resched so that the active
  1561. * hierarchy can be throttled
  1562. */
  1563. if (!assign_cfs_rq_runtime(cfs_rq) && likely(cfs_rq->curr))
  1564. resched_task(rq_of(cfs_rq)->curr);
  1565. }
  1566. static __always_inline
  1567. void account_cfs_rq_runtime(struct cfs_rq *cfs_rq, unsigned long delta_exec)
  1568. {
  1569. if (!cfs_bandwidth_used() || !cfs_rq->runtime_enabled)
  1570. return;
  1571. __account_cfs_rq_runtime(cfs_rq, delta_exec);
  1572. }
  1573. static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
  1574. {
  1575. return cfs_bandwidth_used() && cfs_rq->throttled;
  1576. }
  1577. /* check whether cfs_rq, or any parent, is throttled */
  1578. static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
  1579. {
  1580. return cfs_bandwidth_used() && cfs_rq->throttle_count;
  1581. }
  1582. /*
  1583. * Ensure that neither of the group entities corresponding to src_cpu or
  1584. * dest_cpu are members of a throttled hierarchy when performing group
  1585. * load-balance operations.
  1586. */
  1587. static inline int throttled_lb_pair(struct task_group *tg,
  1588. int src_cpu, int dest_cpu)
  1589. {
  1590. struct cfs_rq *src_cfs_rq, *dest_cfs_rq;
  1591. src_cfs_rq = tg->cfs_rq[src_cpu];
  1592. dest_cfs_rq = tg->cfs_rq[dest_cpu];
  1593. return throttled_hierarchy(src_cfs_rq) ||
  1594. throttled_hierarchy(dest_cfs_rq);
  1595. }
  1596. /* updated child weight may affect parent so we have to do this bottom up */
  1597. static int tg_unthrottle_up(struct task_group *tg, void *data)
  1598. {
  1599. struct rq *rq = data;
  1600. struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
  1601. cfs_rq->throttle_count--;
  1602. #ifdef CONFIG_SMP
  1603. if (!cfs_rq->throttle_count) {
  1604. /* adjust cfs_rq_clock_task() */
  1605. cfs_rq->throttled_clock_task_time += rq->clock_task -
  1606. cfs_rq->throttled_clock_task;
  1607. }
  1608. #endif
  1609. return 0;
  1610. }
  1611. static int tg_throttle_down(struct task_group *tg, void *data)
  1612. {
  1613. struct rq *rq = data;
  1614. struct cfs_rq *cfs_rq = tg->cfs_rq[cpu_of(rq)];
  1615. /* group is entering throttled state, stop time */
  1616. if (!cfs_rq->throttle_count)
  1617. cfs_rq->throttled_clock_task = rq->clock_task;
  1618. cfs_rq->throttle_count++;
  1619. return 0;
  1620. }
  1621. static void throttle_cfs_rq(struct cfs_rq *cfs_rq)
  1622. {
  1623. struct rq *rq = rq_of(cfs_rq);
  1624. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1625. struct sched_entity *se;
  1626. long task_delta, dequeue = 1;
  1627. se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
  1628. /* freeze hierarchy runnable averages while throttled */
  1629. rcu_read_lock();
  1630. walk_tg_tree_from(cfs_rq->tg, tg_throttle_down, tg_nop, (void *)rq);
  1631. rcu_read_unlock();
  1632. task_delta = cfs_rq->h_nr_running;
  1633. for_each_sched_entity(se) {
  1634. struct cfs_rq *qcfs_rq = cfs_rq_of(se);
  1635. /* throttled entity or throttle-on-deactivate */
  1636. if (!se->on_rq)
  1637. break;
  1638. if (dequeue)
  1639. dequeue_entity(qcfs_rq, se, DEQUEUE_SLEEP);
  1640. qcfs_rq->h_nr_running -= task_delta;
  1641. if (qcfs_rq->load.weight)
  1642. dequeue = 0;
  1643. }
  1644. if (!se)
  1645. rq->nr_running -= task_delta;
  1646. cfs_rq->throttled = 1;
  1647. cfs_rq->throttled_clock = rq->clock;
  1648. raw_spin_lock(&cfs_b->lock);
  1649. list_add_tail_rcu(&cfs_rq->throttled_list, &cfs_b->throttled_cfs_rq);
  1650. raw_spin_unlock(&cfs_b->lock);
  1651. }
  1652. void unthrottle_cfs_rq(struct cfs_rq *cfs_rq)
  1653. {
  1654. struct rq *rq = rq_of(cfs_rq);
  1655. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1656. struct sched_entity *se;
  1657. int enqueue = 1;
  1658. long task_delta;
  1659. se = cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))];
  1660. cfs_rq->throttled = 0;
  1661. raw_spin_lock(&cfs_b->lock);
  1662. cfs_b->throttled_time += rq->clock - cfs_rq->throttled_clock;
  1663. list_del_rcu(&cfs_rq->throttled_list);
  1664. raw_spin_unlock(&cfs_b->lock);
  1665. update_rq_clock(rq);
  1666. /* update hierarchical throttle state */
  1667. walk_tg_tree_from(cfs_rq->tg, tg_nop, tg_unthrottle_up, (void *)rq);
  1668. if (!cfs_rq->load.weight)
  1669. return;
  1670. task_delta = cfs_rq->h_nr_running;
  1671. for_each_sched_entity(se) {
  1672. if (se->on_rq)
  1673. enqueue = 0;
  1674. cfs_rq = cfs_rq_of(se);
  1675. if (enqueue)
  1676. enqueue_entity(cfs_rq, se, ENQUEUE_WAKEUP);
  1677. cfs_rq->h_nr_running += task_delta;
  1678. if (cfs_rq_throttled(cfs_rq))
  1679. break;
  1680. }
  1681. if (!se)
  1682. rq->nr_running += task_delta;
  1683. /* determine whether we need to wake up potentially idle cpu */
  1684. if (rq->curr == rq->idle && rq->cfs.nr_running)
  1685. resched_task(rq->curr);
  1686. }
  1687. static u64 distribute_cfs_runtime(struct cfs_bandwidth *cfs_b,
  1688. u64 remaining, u64 expires)
  1689. {
  1690. struct cfs_rq *cfs_rq;
  1691. u64 runtime = remaining;
  1692. rcu_read_lock();
  1693. list_for_each_entry_rcu(cfs_rq, &cfs_b->throttled_cfs_rq,
  1694. throttled_list) {
  1695. struct rq *rq = rq_of(cfs_rq);
  1696. raw_spin_lock(&rq->lock);
  1697. if (!cfs_rq_throttled(cfs_rq))
  1698. goto next;
  1699. runtime = -cfs_rq->runtime_remaining + 1;
  1700. if (runtime > remaining)
  1701. runtime = remaining;
  1702. remaining -= runtime;
  1703. cfs_rq->runtime_remaining += runtime;
  1704. cfs_rq->runtime_expires = expires;
  1705. /* we check whether we're throttled above */
  1706. if (cfs_rq->runtime_remaining > 0)
  1707. unthrottle_cfs_rq(cfs_rq);
  1708. next:
  1709. raw_spin_unlock(&rq->lock);
  1710. if (!remaining)
  1711. break;
  1712. }
  1713. rcu_read_unlock();
  1714. return remaining;
  1715. }
  1716. /*
  1717. * Responsible for refilling a task_group's bandwidth and unthrottling its
  1718. * cfs_rqs as appropriate. If there has been no activity within the last
  1719. * period the timer is deactivated until scheduling resumes; cfs_b->idle is
  1720. * used to track this state.
  1721. */
  1722. static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun)
  1723. {
  1724. u64 runtime, runtime_expires;
  1725. int idle = 1, throttled;
  1726. raw_spin_lock(&cfs_b->lock);
  1727. /* no need to continue the timer with no bandwidth constraint */
  1728. if (cfs_b->quota == RUNTIME_INF)
  1729. goto out_unlock;
  1730. throttled = !list_empty(&cfs_b->throttled_cfs_rq);
  1731. /* idle depends on !throttled (for the case of a large deficit) */
  1732. idle = cfs_b->idle && !throttled;
  1733. cfs_b->nr_periods += overrun;
  1734. /* if we're going inactive then everything else can be deferred */
  1735. if (idle)
  1736. goto out_unlock;
  1737. __refill_cfs_bandwidth_runtime(cfs_b);
  1738. if (!throttled) {
  1739. /* mark as potentially idle for the upcoming period */
  1740. cfs_b->idle = 1;
  1741. goto out_unlock;
  1742. }
  1743. /* account preceding periods in which throttling occurred */
  1744. cfs_b->nr_throttled += overrun;
  1745. /*
  1746. * There are throttled entities so we must first use the new bandwidth
  1747. * to unthrottle them before making it generally available. This
  1748. * ensures that all existing debts will be paid before a new cfs_rq is
  1749. * allowed to run.
  1750. */
  1751. runtime = cfs_b->runtime;
  1752. runtime_expires = cfs_b->runtime_expires;
  1753. cfs_b->runtime = 0;
  1754. /*
  1755. * This check is repeated as we are holding onto the new bandwidth
  1756. * while we unthrottle. This can potentially race with an unthrottled
  1757. * group trying to acquire new bandwidth from the global pool.
  1758. */
  1759. while (throttled && runtime > 0) {
  1760. raw_spin_unlock(&cfs_b->lock);
  1761. /* we can't nest cfs_b->lock while distributing bandwidth */
  1762. runtime = distribute_cfs_runtime(cfs_b, runtime,
  1763. runtime_expires);
  1764. raw_spin_lock(&cfs_b->lock);
  1765. throttled = !list_empty(&cfs_b->throttled_cfs_rq);
  1766. }
  1767. /* return (any) remaining runtime */
  1768. cfs_b->runtime = runtime;
  1769. /*
  1770. * While we are ensured activity in the period following an
  1771. * unthrottle, this also covers the case in which the new bandwidth is
  1772. * insufficient to cover the existing bandwidth deficit. (Forcing the
  1773. * timer to remain active while there are any throttled entities.)
  1774. */
  1775. cfs_b->idle = 0;
  1776. out_unlock:
  1777. if (idle)
  1778. cfs_b->timer_active = 0;
  1779. raw_spin_unlock(&cfs_b->lock);
  1780. return idle;
  1781. }
  1782. /* a cfs_rq won't donate quota below this amount */
  1783. static const u64 min_cfs_rq_runtime = 1 * NSEC_PER_MSEC;
  1784. /* minimum remaining period time to redistribute slack quota */
  1785. static const u64 min_bandwidth_expiration = 2 * NSEC_PER_MSEC;
  1786. /* how long we wait to gather additional slack before distributing */
  1787. static const u64 cfs_bandwidth_slack_period = 5 * NSEC_PER_MSEC;
  1788. /* are we near the end of the current quota period? */
  1789. static int runtime_refresh_within(struct cfs_bandwidth *cfs_b, u64 min_expire)
  1790. {
  1791. struct hrtimer *refresh_timer = &cfs_b->period_timer;
  1792. u64 remaining;
  1793. /* if the call-back is running a quota refresh is already occurring */
  1794. if (hrtimer_callback_running(refresh_timer))
  1795. return 1;
  1796. /* is a quota refresh about to occur? */
  1797. remaining = ktime_to_ns(hrtimer_expires_remaining(refresh_timer));
  1798. if (remaining < min_expire)
  1799. return 1;
  1800. return 0;
  1801. }
  1802. static void start_cfs_slack_bandwidth(struct cfs_bandwidth *cfs_b)
  1803. {
  1804. u64 min_left = cfs_bandwidth_slack_period + min_bandwidth_expiration;
  1805. /* if there's a quota refresh soon don't bother with slack */
  1806. if (runtime_refresh_within(cfs_b, min_left))
  1807. return;
  1808. start_bandwidth_timer(&cfs_b->slack_timer,
  1809. ns_to_ktime(cfs_bandwidth_slack_period));
  1810. }
  1811. /* we know any runtime found here is valid as update_curr() precedes return */
  1812. static void __return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1813. {
  1814. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1815. s64 slack_runtime = cfs_rq->runtime_remaining - min_cfs_rq_runtime;
  1816. if (slack_runtime <= 0)
  1817. return;
  1818. raw_spin_lock(&cfs_b->lock);
  1819. if (cfs_b->quota != RUNTIME_INF &&
  1820. cfs_rq->runtime_expires == cfs_b->runtime_expires) {
  1821. cfs_b->runtime += slack_runtime;
  1822. /* we are under rq->lock, defer unthrottling using a timer */
  1823. if (cfs_b->runtime > sched_cfs_bandwidth_slice() &&
  1824. !list_empty(&cfs_b->throttled_cfs_rq))
  1825. start_cfs_slack_bandwidth(cfs_b);
  1826. }
  1827. raw_spin_unlock(&cfs_b->lock);
  1828. /* even if it's not valid for return we don't want to try again */
  1829. cfs_rq->runtime_remaining -= slack_runtime;
  1830. }
  1831. static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1832. {
  1833. if (!cfs_bandwidth_used())
  1834. return;
  1835. if (!cfs_rq->runtime_enabled || cfs_rq->nr_running)
  1836. return;
  1837. __return_cfs_rq_runtime(cfs_rq);
  1838. }
  1839. /*
  1840. * This is done with a timer (instead of inline with bandwidth return) since
  1841. * it's necessary to juggle rq->locks to unthrottle their respective cfs_rqs.
  1842. */
  1843. static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b)
  1844. {
  1845. u64 runtime = 0, slice = sched_cfs_bandwidth_slice();
  1846. u64 expires;
  1847. /* confirm we're still not at a refresh boundary */
  1848. if (runtime_refresh_within(cfs_b, min_bandwidth_expiration))
  1849. return;
  1850. raw_spin_lock(&cfs_b->lock);
  1851. if (cfs_b->quota != RUNTIME_INF && cfs_b->runtime > slice) {
  1852. runtime = cfs_b->runtime;
  1853. cfs_b->runtime = 0;
  1854. }
  1855. expires = cfs_b->runtime_expires;
  1856. raw_spin_unlock(&cfs_b->lock);
  1857. if (!runtime)
  1858. return;
  1859. runtime = distribute_cfs_runtime(cfs_b, runtime, expires);
  1860. raw_spin_lock(&cfs_b->lock);
  1861. if (expires == cfs_b->runtime_expires)
  1862. cfs_b->runtime = runtime;
  1863. raw_spin_unlock(&cfs_b->lock);
  1864. }
  1865. /*
  1866. * When a group wakes up we want to make sure that its quota is not already
  1867. * expired/exceeded, otherwise it may be allowed to steal additional ticks of
  1868. * runtime as update_curr() throttling can not not trigger until it's on-rq.
  1869. */
  1870. static void check_enqueue_throttle(struct cfs_rq *cfs_rq)
  1871. {
  1872. if (!cfs_bandwidth_used())
  1873. return;
  1874. /* an active group must be handled by the update_curr()->put() path */
  1875. if (!cfs_rq->runtime_enabled || cfs_rq->curr)
  1876. return;
  1877. /* ensure the group is not already throttled */
  1878. if (cfs_rq_throttled(cfs_rq))
  1879. return;
  1880. /* update runtime allocation */
  1881. account_cfs_rq_runtime(cfs_rq, 0);
  1882. if (cfs_rq->runtime_remaining <= 0)
  1883. throttle_cfs_rq(cfs_rq);
  1884. }
  1885. /* conditionally throttle active cfs_rq's from put_prev_entity() */
  1886. static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1887. {
  1888. if (!cfs_bandwidth_used())
  1889. return;
  1890. if (likely(!cfs_rq->runtime_enabled || cfs_rq->runtime_remaining > 0))
  1891. return;
  1892. /*
  1893. * it's possible for a throttled entity to be forced into a running
  1894. * state (e.g. set_curr_task), in this case we're finished.
  1895. */
  1896. if (cfs_rq_throttled(cfs_rq))
  1897. return;
  1898. throttle_cfs_rq(cfs_rq);
  1899. }
  1900. static inline u64 default_cfs_period(void);
  1901. static int do_sched_cfs_period_timer(struct cfs_bandwidth *cfs_b, int overrun);
  1902. static void do_sched_cfs_slack_timer(struct cfs_bandwidth *cfs_b);
  1903. static enum hrtimer_restart sched_cfs_slack_timer(struct hrtimer *timer)
  1904. {
  1905. struct cfs_bandwidth *cfs_b =
  1906. container_of(timer, struct cfs_bandwidth, slack_timer);
  1907. do_sched_cfs_slack_timer(cfs_b);
  1908. return HRTIMER_NORESTART;
  1909. }
  1910. static enum hrtimer_restart sched_cfs_period_timer(struct hrtimer *timer)
  1911. {
  1912. struct cfs_bandwidth *cfs_b =
  1913. container_of(timer, struct cfs_bandwidth, period_timer);
  1914. ktime_t now;
  1915. int overrun;
  1916. int idle = 0;
  1917. for (;;) {
  1918. now = hrtimer_cb_get_time(timer);
  1919. overrun = hrtimer_forward(timer, now, cfs_b->period);
  1920. if (!overrun)
  1921. break;
  1922. idle = do_sched_cfs_period_timer(cfs_b, overrun);
  1923. }
  1924. return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
  1925. }
  1926. void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
  1927. {
  1928. raw_spin_lock_init(&cfs_b->lock);
  1929. cfs_b->runtime = 0;
  1930. cfs_b->quota = RUNTIME_INF;
  1931. cfs_b->period = ns_to_ktime(default_cfs_period());
  1932. INIT_LIST_HEAD(&cfs_b->throttled_cfs_rq);
  1933. hrtimer_init(&cfs_b->period_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1934. cfs_b->period_timer.function = sched_cfs_period_timer;
  1935. hrtimer_init(&cfs_b->slack_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1936. cfs_b->slack_timer.function = sched_cfs_slack_timer;
  1937. }
  1938. static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq)
  1939. {
  1940. cfs_rq->runtime_enabled = 0;
  1941. INIT_LIST_HEAD(&cfs_rq->throttled_list);
  1942. }
  1943. /* requires cfs_b->lock, may release to reprogram timer */
  1944. void __start_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
  1945. {
  1946. /*
  1947. * The timer may be active because we're trying to set a new bandwidth
  1948. * period or because we're racing with the tear-down path
  1949. * (timer_active==0 becomes visible before the hrtimer call-back
  1950. * terminates). In either case we ensure that it's re-programmed
  1951. */
  1952. while (unlikely(hrtimer_active(&cfs_b->period_timer))) {
  1953. raw_spin_unlock(&cfs_b->lock);
  1954. /* ensure cfs_b->lock is available while we wait */
  1955. hrtimer_cancel(&cfs_b->period_timer);
  1956. raw_spin_lock(&cfs_b->lock);
  1957. /* if someone else restarted the timer then we're done */
  1958. if (cfs_b->timer_active)
  1959. return;
  1960. }
  1961. cfs_b->timer_active = 1;
  1962. start_bandwidth_timer(&cfs_b->period_timer, cfs_b->period);
  1963. }
  1964. static void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b)
  1965. {
  1966. hrtimer_cancel(&cfs_b->period_timer);
  1967. hrtimer_cancel(&cfs_b->slack_timer);
  1968. }
  1969. static void unthrottle_offline_cfs_rqs(struct rq *rq)
  1970. {
  1971. struct cfs_rq *cfs_rq;
  1972. for_each_leaf_cfs_rq(rq, cfs_rq) {
  1973. struct cfs_bandwidth *cfs_b = tg_cfs_bandwidth(cfs_rq->tg);
  1974. if (!cfs_rq->runtime_enabled)
  1975. continue;
  1976. /*
  1977. * clock_task is not advancing so we just need to make sure
  1978. * there's some valid quota amount
  1979. */
  1980. cfs_rq->runtime_remaining = cfs_b->quota;
  1981. if (cfs_rq_throttled(cfs_rq))
  1982. unthrottle_cfs_rq(cfs_rq);
  1983. }
  1984. }
  1985. #else /* CONFIG_CFS_BANDWIDTH */
  1986. static inline u64 cfs_rq_clock_task(struct cfs_rq *cfs_rq)
  1987. {
  1988. return rq_of(cfs_rq)->clock_task;
  1989. }
  1990. static void account_cfs_rq_runtime(struct cfs_rq *cfs_rq,
  1991. unsigned long delta_exec) {}
  1992. static void check_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
  1993. static void check_enqueue_throttle(struct cfs_rq *cfs_rq) {}
  1994. static __always_inline void return_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
  1995. static inline int cfs_rq_throttled(struct cfs_rq *cfs_rq)
  1996. {
  1997. return 0;
  1998. }
  1999. static inline int throttled_hierarchy(struct cfs_rq *cfs_rq)
  2000. {
  2001. return 0;
  2002. }
  2003. static inline int throttled_lb_pair(struct task_group *tg,
  2004. int src_cpu, int dest_cpu)
  2005. {
  2006. return 0;
  2007. }
  2008. void init_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
  2009. #ifdef CONFIG_FAIR_GROUP_SCHED
  2010. static void init_cfs_rq_runtime(struct cfs_rq *cfs_rq) {}
  2011. #endif
  2012. static inline struct cfs_bandwidth *tg_cfs_bandwidth(struct task_group *tg)
  2013. {
  2014. return NULL;
  2015. }
  2016. static inline void destroy_cfs_bandwidth(struct cfs_bandwidth *cfs_b) {}
  2017. static inline void unthrottle_offline_cfs_rqs(struct rq *rq) {}
  2018. #endif /* CONFIG_CFS_BANDWIDTH */
  2019. /**************************************************
  2020. * CFS operations on tasks:
  2021. */
  2022. #ifdef CONFIG_SCHED_HRTICK
  2023. static void hrtick_start_fair(struct rq *rq, struct task_struct *p)
  2024. {
  2025. struct sched_entity *se = &p->se;
  2026. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  2027. WARN_ON(task_rq(p) != rq);
  2028. if (cfs_rq->nr_running > 1) {
  2029. u64 slice = sched_slice(cfs_rq, se);
  2030. u64 ran = se->sum_exec_runtime - se->prev_sum_exec_runtime;
  2031. s64 delta = slice - ran;
  2032. if (delta < 0) {
  2033. if (rq->curr == p)
  2034. resched_task(p);
  2035. return;
  2036. }
  2037. /*
  2038. * Don't schedule slices shorter than 10000ns, that just
  2039. * doesn't make sense. Rely on vruntime for fairness.
  2040. */
  2041. if (rq->curr != p)
  2042. delta = max_t(s64, 10000LL, delta);
  2043. hrtick_start(rq, delta);
  2044. }
  2045. }
  2046. /*
  2047. * called from enqueue/dequeue and updates the hrtick when the
  2048. * current task is from our class and nr_running is low enough
  2049. * to matter.
  2050. */
  2051. static void hrtick_update(struct rq *rq)
  2052. {
  2053. struct task_struct *curr = rq->curr;
  2054. if (!hrtick_enabled(rq) || curr->sched_class != &fair_sched_class)
  2055. return;
  2056. if (cfs_rq_of(&curr->se)->nr_running < sched_nr_latency)
  2057. hrtick_start_fair(rq, curr);
  2058. }
  2059. #else /* !CONFIG_SCHED_HRTICK */
  2060. static inline void
  2061. hrtick_start_fair(struct rq *rq, struct task_struct *p)
  2062. {
  2063. }
  2064. static inline void hrtick_update(struct rq *rq)
  2065. {
  2066. }
  2067. #endif
  2068. /*
  2069. * The enqueue_task method is called before nr_running is
  2070. * increased. Here we update the fair scheduling stats and
  2071. * then put the task into the rbtree:
  2072. */
  2073. static void
  2074. enqueue_task_fair(struct rq *rq, struct task_struct *p, int flags)
  2075. {
  2076. struct cfs_rq *cfs_rq;
  2077. struct sched_entity *se = &p->se;
  2078. for_each_sched_entity(se) {
  2079. if (se->on_rq)
  2080. break;
  2081. cfs_rq = cfs_rq_of(se);
  2082. enqueue_entity(cfs_rq, se, flags);
  2083. /*
  2084. * end evaluation on encountering a throttled cfs_rq
  2085. *
  2086. * note: in the case of encountering a throttled cfs_rq we will
  2087. * post the final h_nr_running increment below.
  2088. */
  2089. if (cfs_rq_throttled(cfs_rq))
  2090. break;
  2091. cfs_rq->h_nr_running++;
  2092. flags = ENQUEUE_WAKEUP;
  2093. }
  2094. for_each_sched_entity(se) {
  2095. cfs_rq = cfs_rq_of(se);
  2096. cfs_rq->h_nr_running++;
  2097. if (cfs_rq_throttled(cfs_rq))
  2098. break;
  2099. update_cfs_shares(cfs_rq);
  2100. update_entity_load_avg(se, 1);
  2101. }
  2102. if (!se) {
  2103. update_rq_runnable_avg(rq, rq->nr_running);
  2104. inc_nr_running(rq);
  2105. }
  2106. hrtick_update(rq);
  2107. }
  2108. static void set_next_buddy(struct sched_entity *se);
  2109. /*
  2110. * The dequeue_task method is called before nr_running is
  2111. * decreased. We remove the task from the rbtree and
  2112. * update the fair scheduling stats:
  2113. */
  2114. static void dequeue_task_fair(struct rq *rq, struct task_struct *p, int flags)
  2115. {
  2116. struct cfs_rq *cfs_rq;
  2117. struct sched_entity *se = &p->se;
  2118. int task_sleep = flags & DEQUEUE_SLEEP;
  2119. for_each_sched_entity(se) {
  2120. cfs_rq = cfs_rq_of(se);
  2121. dequeue_entity(cfs_rq, se, flags);
  2122. /*
  2123. * end evaluation on encountering a throttled cfs_rq
  2124. *
  2125. * note: in the case of encountering a throttled cfs_rq we will
  2126. * post the final h_nr_running decrement below.
  2127. */
  2128. if (cfs_rq_throttled(cfs_rq))
  2129. break;
  2130. cfs_rq->h_nr_running--;
  2131. /* Don't dequeue parent if it has other entities besides us */
  2132. if (cfs_rq->load.weight) {
  2133. /*
  2134. * Bias pick_next to pick a task from this cfs_rq, as
  2135. * p is sleeping when it is within its sched_slice.
  2136. */
  2137. if (task_sleep && parent_entity(se))
  2138. set_next_buddy(parent_entity(se));
  2139. /* avoid re-evaluating load for this entity */
  2140. se = parent_entity(se);
  2141. break;
  2142. }
  2143. flags |= DEQUEUE_SLEEP;
  2144. }
  2145. for_each_sched_entity(se) {
  2146. cfs_rq = cfs_rq_of(se);
  2147. cfs_rq->h_nr_running--;
  2148. if (cfs_rq_throttled(cfs_rq))
  2149. break;
  2150. update_cfs_shares(cfs_rq);
  2151. update_entity_load_avg(se, 1);
  2152. }
  2153. if (!se) {
  2154. dec_nr_running(rq);
  2155. update_rq_runnable_avg(rq, 1);
  2156. }
  2157. hrtick_update(rq);
  2158. }
  2159. #ifdef CONFIG_SMP
  2160. /* Used instead of source_load when we know the type == 0 */
  2161. static unsigned long weighted_cpuload(const int cpu)
  2162. {
  2163. return cpu_rq(cpu)->load.weight;
  2164. }
  2165. /*
  2166. * Return a low guess at the load of a migration-source cpu weighted
  2167. * according to the scheduling class and "nice" value.
  2168. *
  2169. * We want to under-estimate the load of migration sources, to
  2170. * balance conservatively.
  2171. */
  2172. static unsigned long source_load(int cpu, int type)
  2173. {
  2174. struct rq *rq = cpu_rq(cpu);
  2175. unsigned long total = weighted_cpuload(cpu);
  2176. if (type == 0 || !sched_feat(LB_BIAS))
  2177. return total;
  2178. return min(rq->cpu_load[type-1], total);
  2179. }
  2180. /*
  2181. * Return a high guess at the load of a migration-target cpu weighted
  2182. * according to the scheduling class and "nice" value.
  2183. */
  2184. static unsigned long target_load(int cpu, int type)
  2185. {
  2186. struct rq *rq = cpu_rq(cpu);
  2187. unsigned long total = weighted_cpuload(cpu);
  2188. if (type == 0 || !sched_feat(LB_BIAS))
  2189. return total;
  2190. return max(rq->cpu_load[type-1], total);
  2191. }
  2192. static unsigned long power_of(int cpu)
  2193. {
  2194. return cpu_rq(cpu)->cpu_power;
  2195. }
  2196. static unsigned long cpu_avg_load_per_task(int cpu)
  2197. {
  2198. struct rq *rq = cpu_rq(cpu);
  2199. unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
  2200. if (nr_running)
  2201. return rq->load.weight / nr_running;
  2202. return 0;
  2203. }
  2204. static void task_waking_fair(struct task_struct *p)
  2205. {
  2206. struct sched_entity *se = &p->se;
  2207. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  2208. u64 min_vruntime;
  2209. #ifndef CONFIG_64BIT
  2210. u64 min_vruntime_copy;
  2211. do {
  2212. min_vruntime_copy = cfs_rq->min_vruntime_copy;
  2213. smp_rmb();
  2214. min_vruntime = cfs_rq->min_vruntime;
  2215. } while (min_vruntime != min_vruntime_copy);
  2216. #else
  2217. min_vruntime = cfs_rq->min_vruntime;
  2218. #endif
  2219. se->vruntime -= min_vruntime;
  2220. }
  2221. #ifdef CONFIG_FAIR_GROUP_SCHED
  2222. /*
  2223. * effective_load() calculates the load change as seen from the root_task_group
  2224. *
  2225. * Adding load to a group doesn't make a group heavier, but can cause movement
  2226. * of group shares between cpus. Assuming the shares were perfectly aligned one
  2227. * can calculate the shift in shares.
  2228. *
  2229. * Calculate the effective load difference if @wl is added (subtracted) to @tg
  2230. * on this @cpu and results in a total addition (subtraction) of @wg to the
  2231. * total group weight.
  2232. *
  2233. * Given a runqueue weight distribution (rw_i) we can compute a shares
  2234. * distribution (s_i) using:
  2235. *
  2236. * s_i = rw_i / \Sum rw_j (1)
  2237. *
  2238. * Suppose we have 4 CPUs and our @tg is a direct child of the root group and
  2239. * has 7 equal weight tasks, distributed as below (rw_i), with the resulting
  2240. * shares distribution (s_i):
  2241. *
  2242. * rw_i = { 2, 4, 1, 0 }
  2243. * s_i = { 2/7, 4/7, 1/7, 0 }
  2244. *
  2245. * As per wake_affine() we're interested in the load of two CPUs (the CPU the
  2246. * task used to run on and the CPU the waker is running on), we need to
  2247. * compute the effect of waking a task on either CPU and, in case of a sync
  2248. * wakeup, compute the effect of the current task going to sleep.
  2249. *
  2250. * So for a change of @wl to the local @cpu with an overall group weight change
  2251. * of @wl we can compute the new shares distribution (s'_i) using:
  2252. *
  2253. * s'_i = (rw_i + @wl) / (@wg + \Sum rw_j) (2)
  2254. *
  2255. * Suppose we're interested in CPUs 0 and 1, and want to compute the load
  2256. * differences in waking a task to CPU 0. The additional task changes the
  2257. * weight and shares distributions like:
  2258. *
  2259. * rw'_i = { 3, 4, 1, 0 }
  2260. * s'_i = { 3/8, 4/8, 1/8, 0 }
  2261. *
  2262. * We can then compute the difference in effective weight by using:
  2263. *
  2264. * dw_i = S * (s'_i - s_i) (3)
  2265. *
  2266. * Where 'S' is the group weight as seen by its parent.
  2267. *
  2268. * Therefore the effective change in loads on CPU 0 would be 5/56 (3/8 - 2/7)
  2269. * times the weight of the group. The effect on CPU 1 would be -4/56 (4/8 -
  2270. * 4/7) times the weight of the group.
  2271. */
  2272. static long effective_load(struct task_group *tg, int cpu, long wl, long wg)
  2273. {
  2274. struct sched_entity *se = tg->se[cpu];
  2275. if (!tg->parent) /* the trivial, non-cgroup case */
  2276. return wl;
  2277. for_each_sched_entity(se) {
  2278. long w, W;
  2279. tg = se->my_q->tg;
  2280. /*
  2281. * W = @wg + \Sum rw_j
  2282. */
  2283. W = wg + calc_tg_weight(tg, se->my_q);
  2284. /*
  2285. * w = rw_i + @wl
  2286. */
  2287. w = se->my_q->load.weight + wl;
  2288. /*
  2289. * wl = S * s'_i; see (2)
  2290. */
  2291. if (W > 0 && w < W)
  2292. wl = (w * tg->shares) / W;
  2293. else
  2294. wl = tg->shares;
  2295. /*
  2296. * Per the above, wl is the new se->load.weight value; since
  2297. * those are clipped to [MIN_SHARES, ...) do so now. See
  2298. * calc_cfs_shares().
  2299. */
  2300. if (wl < MIN_SHARES)
  2301. wl = MIN_SHARES;
  2302. /*
  2303. * wl = dw_i = S * (s'_i - s_i); see (3)
  2304. */
  2305. wl -= se->load.weight;
  2306. /*
  2307. * Recursively apply this logic to all parent groups to compute
  2308. * the final effective load change on the root group. Since
  2309. * only the @tg group gets extra weight, all parent groups can
  2310. * only redistribute existing shares. @wl is the shift in shares
  2311. * resulting from this level per the above.
  2312. */
  2313. wg = 0;
  2314. }
  2315. return wl;
  2316. }
  2317. #else
  2318. static inline unsigned long effective_load(struct task_group *tg, int cpu,
  2319. unsigned long wl, unsigned long wg)
  2320. {
  2321. return wl;
  2322. }
  2323. #endif
  2324. static int wake_affine(struct sched_domain *sd, struct task_struct *p, int sync)
  2325. {
  2326. s64 this_load, load;
  2327. int idx, this_cpu, prev_cpu;
  2328. unsigned long tl_per_task;
  2329. struct task_group *tg;
  2330. unsigned long weight;
  2331. int balanced;
  2332. idx = sd->wake_idx;
  2333. this_cpu = smp_processor_id();
  2334. prev_cpu = task_cpu(p);
  2335. load = source_load(prev_cpu, idx);
  2336. this_load = target_load(this_cpu, idx);
  2337. /*
  2338. * If sync wakeup then subtract the (maximum possible)
  2339. * effect of the currently running task from the load
  2340. * of the current CPU:
  2341. */
  2342. if (sync) {
  2343. tg = task_group(current);
  2344. weight = current->se.load.weight;
  2345. this_load += effective_load(tg, this_cpu, -weight, -weight);
  2346. load += effective_load(tg, prev_cpu, 0, -weight);
  2347. }
  2348. tg = task_group(p);
  2349. weight = p->se.load.weight;
  2350. /*
  2351. * In low-load situations, where prev_cpu is idle and this_cpu is idle
  2352. * due to the sync cause above having dropped this_load to 0, we'll
  2353. * always have an imbalance, but there's really nothing you can do
  2354. * about that, so that's good too.
  2355. *
  2356. * Otherwise check if either cpus are near enough in load to allow this
  2357. * task to be woken on this_cpu.
  2358. */
  2359. if (this_load > 0) {
  2360. s64 this_eff_load, prev_eff_load;
  2361. this_eff_load = 100;
  2362. this_eff_load *= power_of(prev_cpu);
  2363. this_eff_load *= this_load +
  2364. effective_load(tg, this_cpu, weight, weight);
  2365. prev_eff_load = 100 + (sd->imbalance_pct - 100) / 2;
  2366. prev_eff_load *= power_of(this_cpu);
  2367. prev_eff_load *= load + effective_load(tg, prev_cpu, 0, weight);
  2368. balanced = this_eff_load <= prev_eff_load;
  2369. } else
  2370. balanced = true;
  2371. /*
  2372. * If the currently running task will sleep within
  2373. * a reasonable amount of time then attract this newly
  2374. * woken task:
  2375. */
  2376. if (sync && balanced)
  2377. return 1;
  2378. schedstat_inc(p, se.statistics.nr_wakeups_affine_attempts);
  2379. tl_per_task = cpu_avg_load_per_task(this_cpu);
  2380. if (balanced ||
  2381. (this_load <= load &&
  2382. this_load + target_load(prev_cpu, idx) <= tl_per_task)) {
  2383. /*
  2384. * This domain has SD_WAKE_AFFINE and
  2385. * p is cache cold in this domain, and
  2386. * there is no bad imbalance.
  2387. */
  2388. schedstat_inc(sd, ttwu_move_affine);
  2389. schedstat_inc(p, se.statistics.nr_wakeups_affine);
  2390. return 1;
  2391. }
  2392. return 0;
  2393. }
  2394. /*
  2395. * find_idlest_group finds and returns the least busy CPU group within the
  2396. * domain.
  2397. */
  2398. static struct sched_group *
  2399. find_idlest_group(struct sched_domain *sd, struct task_struct *p,
  2400. int this_cpu, int load_idx)
  2401. {
  2402. struct sched_group *idlest = NULL, *group = sd->groups;
  2403. unsigned long min_load = ULONG_MAX, this_load = 0;
  2404. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  2405. do {
  2406. unsigned long load, avg_load;
  2407. int local_group;
  2408. int i;
  2409. /* Skip over this group if it has no CPUs allowed */
  2410. if (!cpumask_intersects(sched_group_cpus(group),
  2411. tsk_cpus_allowed(p)))
  2412. continue;
  2413. local_group = cpumask_test_cpu(this_cpu,
  2414. sched_group_cpus(group));
  2415. /* Tally up the load of all CPUs in the group */
  2416. avg_load = 0;
  2417. for_each_cpu(i, sched_group_cpus(group)) {
  2418. /* Bias balancing toward cpus of our domain */
  2419. if (local_group)
  2420. load = source_load(i, load_idx);
  2421. else
  2422. load = target_load(i, load_idx);
  2423. avg_load += load;
  2424. }
  2425. /* Adjust by relative CPU power of the group */
  2426. avg_load = (avg_load * SCHED_POWER_SCALE) / group->sgp->power;
  2427. if (local_group) {
  2428. this_load = avg_load;
  2429. } else if (avg_load < min_load) {
  2430. min_load = avg_load;
  2431. idlest = group;
  2432. }
  2433. } while (group = group->next, group != sd->groups);
  2434. if (!idlest || 100*this_load < imbalance*min_load)
  2435. return NULL;
  2436. return idlest;
  2437. }
  2438. /*
  2439. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  2440. */
  2441. static int
  2442. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  2443. {
  2444. unsigned long load, min_load = ULONG_MAX;
  2445. int idlest = -1;
  2446. int i;
  2447. /* Traverse only the allowed CPUs */
  2448. for_each_cpu_and(i, sched_group_cpus(group), tsk_cpus_allowed(p)) {
  2449. load = weighted_cpuload(i);
  2450. if (load < min_load || (load == min_load && i == this_cpu)) {
  2451. min_load = load;
  2452. idlest = i;
  2453. }
  2454. }
  2455. return idlest;
  2456. }
  2457. /*
  2458. * Try and locate an idle CPU in the sched_domain.
  2459. */
  2460. static int select_idle_sibling(struct task_struct *p, int target)
  2461. {
  2462. int cpu = smp_processor_id();
  2463. int prev_cpu = task_cpu(p);
  2464. struct sched_domain *sd;
  2465. struct sched_group *sg;
  2466. int i;
  2467. /*
  2468. * If the task is going to be woken-up on this cpu and if it is
  2469. * already idle, then it is the right target.
  2470. */
  2471. if (target == cpu && idle_cpu(cpu))
  2472. return cpu;
  2473. /*
  2474. * If the task is going to be woken-up on the cpu where it previously
  2475. * ran and if it is currently idle, then it the right target.
  2476. */
  2477. if (target == prev_cpu && idle_cpu(prev_cpu))
  2478. return prev_cpu;
  2479. /*
  2480. * Otherwise, iterate the domains and find an elegible idle cpu.
  2481. */
  2482. sd = rcu_dereference(per_cpu(sd_llc, target));
  2483. for_each_lower_domain(sd) {
  2484. sg = sd->groups;
  2485. do {
  2486. if (!cpumask_intersects(sched_group_cpus(sg),
  2487. tsk_cpus_allowed(p)))
  2488. goto next;
  2489. for_each_cpu(i, sched_group_cpus(sg)) {
  2490. if (!idle_cpu(i))
  2491. goto next;
  2492. }
  2493. target = cpumask_first_and(sched_group_cpus(sg),
  2494. tsk_cpus_allowed(p));
  2495. goto done;
  2496. next:
  2497. sg = sg->next;
  2498. } while (sg != sd->groups);
  2499. }
  2500. done:
  2501. return target;
  2502. }
  2503. /*
  2504. * sched_balance_self: balance the current task (running on cpu) in domains
  2505. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  2506. * SD_BALANCE_EXEC.
  2507. *
  2508. * Balance, ie. select the least loaded group.
  2509. *
  2510. * Returns the target CPU number, or the same CPU if no balancing is needed.
  2511. *
  2512. * preempt must be disabled.
  2513. */
  2514. static int
  2515. select_task_rq_fair(struct task_struct *p, int sd_flag, int wake_flags)
  2516. {
  2517. struct sched_domain *tmp, *affine_sd = NULL, *sd = NULL;
  2518. int cpu = smp_processor_id();
  2519. int prev_cpu = task_cpu(p);
  2520. int new_cpu = cpu;
  2521. int want_affine = 0;
  2522. int sync = wake_flags & WF_SYNC;
  2523. if (p->nr_cpus_allowed == 1)
  2524. return prev_cpu;
  2525. if (sd_flag & SD_BALANCE_WAKE) {
  2526. if (cpumask_test_cpu(cpu, tsk_cpus_allowed(p)))
  2527. want_affine = 1;
  2528. new_cpu = prev_cpu;
  2529. }
  2530. rcu_read_lock();
  2531. for_each_domain(cpu, tmp) {
  2532. if (!(tmp->flags & SD_LOAD_BALANCE))
  2533. continue;
  2534. /*
  2535. * If both cpu and prev_cpu are part of this domain,
  2536. * cpu is a valid SD_WAKE_AFFINE target.
  2537. */
  2538. if (want_affine && (tmp->flags & SD_WAKE_AFFINE) &&
  2539. cpumask_test_cpu(prev_cpu, sched_domain_span(tmp))) {
  2540. affine_sd = tmp;
  2541. break;
  2542. }
  2543. if (tmp->flags & sd_flag)
  2544. sd = tmp;
  2545. }
  2546. if (affine_sd) {
  2547. if (cpu != prev_cpu && wake_affine(affine_sd, p, sync))
  2548. prev_cpu = cpu;
  2549. new_cpu = select_idle_sibling(p, prev_cpu);
  2550. goto unlock;
  2551. }
  2552. while (sd) {
  2553. int load_idx = sd->forkexec_idx;
  2554. struct sched_group *group;
  2555. int weight;
  2556. if (!(sd->flags & sd_flag)) {
  2557. sd = sd->child;
  2558. continue;
  2559. }
  2560. if (sd_flag & SD_BALANCE_WAKE)
  2561. load_idx = sd->wake_idx;
  2562. group = find_idlest_group(sd, p, cpu, load_idx);
  2563. if (!group) {
  2564. sd = sd->child;
  2565. continue;
  2566. }
  2567. new_cpu = find_idlest_cpu(group, p, cpu);
  2568. if (new_cpu == -1 || new_cpu == cpu) {
  2569. /* Now try balancing at a lower domain level of cpu */
  2570. sd = sd->child;
  2571. continue;
  2572. }
  2573. /* Now try balancing at a lower domain level of new_cpu */
  2574. cpu = new_cpu;
  2575. weight = sd->span_weight;
  2576. sd = NULL;
  2577. for_each_domain(cpu, tmp) {
  2578. if (weight <= tmp->span_weight)
  2579. break;
  2580. if (tmp->flags & sd_flag)
  2581. sd = tmp;
  2582. }
  2583. /* while loop will break here if sd == NULL */
  2584. }
  2585. unlock:
  2586. rcu_read_unlock();
  2587. return new_cpu;
  2588. }
  2589. /*
  2590. * Called immediately before a task is migrated to a new cpu; task_cpu(p) and
  2591. * cfs_rq_of(p) references at time of call are still valid and identify the
  2592. * previous cpu. However, the caller only guarantees p->pi_lock is held; no
  2593. * other assumptions, including the state of rq->lock, should be made.
  2594. */
  2595. static void
  2596. migrate_task_rq_fair(struct task_struct *p, int next_cpu)
  2597. {
  2598. struct sched_entity *se = &p->se;
  2599. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  2600. /*
  2601. * Load tracking: accumulate removed load so that it can be processed
  2602. * when we next update owning cfs_rq under rq->lock. Tasks contribute
  2603. * to blocked load iff they have a positive decay-count. It can never
  2604. * be negative here since on-rq tasks have decay-count == 0.
  2605. */
  2606. if (se->avg.decay_count) {
  2607. se->avg.decay_count = -__synchronize_entity_decay(se);
  2608. atomic64_add(se->avg.load_avg_contrib, &cfs_rq->removed_load);
  2609. }
  2610. }
  2611. #endif /* CONFIG_SMP */
  2612. static unsigned long
  2613. wakeup_gran(struct sched_entity *curr, struct sched_entity *se)
  2614. {
  2615. unsigned long gran = sysctl_sched_wakeup_granularity;
  2616. /*
  2617. * Since its curr running now, convert the gran from real-time
  2618. * to virtual-time in his units.
  2619. *
  2620. * By using 'se' instead of 'curr' we penalize light tasks, so
  2621. * they get preempted easier. That is, if 'se' < 'curr' then
  2622. * the resulting gran will be larger, therefore penalizing the
  2623. * lighter, if otoh 'se' > 'curr' then the resulting gran will
  2624. * be smaller, again penalizing the lighter task.
  2625. *
  2626. * This is especially important for buddies when the leftmost
  2627. * task is higher priority than the buddy.
  2628. */
  2629. return calc_delta_fair(gran, se);
  2630. }
  2631. /*
  2632. * Should 'se' preempt 'curr'.
  2633. *
  2634. * |s1
  2635. * |s2
  2636. * |s3
  2637. * g
  2638. * |<--->|c
  2639. *
  2640. * w(c, s1) = -1
  2641. * w(c, s2) = 0
  2642. * w(c, s3) = 1
  2643. *
  2644. */
  2645. static int
  2646. wakeup_preempt_entity(struct sched_entity *curr, struct sched_entity *se)
  2647. {
  2648. s64 gran, vdiff = curr->vruntime - se->vruntime;
  2649. if (vdiff <= 0)
  2650. return -1;
  2651. gran = wakeup_gran(curr, se);
  2652. if (vdiff > gran)
  2653. return 1;
  2654. return 0;
  2655. }
  2656. static void set_last_buddy(struct sched_entity *se)
  2657. {
  2658. if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
  2659. return;
  2660. for_each_sched_entity(se)
  2661. cfs_rq_of(se)->last = se;
  2662. }
  2663. static void set_next_buddy(struct sched_entity *se)
  2664. {
  2665. if (entity_is_task(se) && unlikely(task_of(se)->policy == SCHED_IDLE))
  2666. return;
  2667. for_each_sched_entity(se)
  2668. cfs_rq_of(se)->next = se;
  2669. }
  2670. static void set_skip_buddy(struct sched_entity *se)
  2671. {
  2672. for_each_sched_entity(se)
  2673. cfs_rq_of(se)->skip = se;
  2674. }
  2675. /*
  2676. * Preempt the current task with a newly woken task if needed:
  2677. */
  2678. static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  2679. {
  2680. struct task_struct *curr = rq->curr;
  2681. struct sched_entity *se = &curr->se, *pse = &p->se;
  2682. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  2683. int scale = cfs_rq->nr_running >= sched_nr_latency;
  2684. int next_buddy_marked = 0;
  2685. if (unlikely(se == pse))
  2686. return;
  2687. /*
  2688. * This is possible from callers such as move_task(), in which we
  2689. * unconditionally check_prempt_curr() after an enqueue (which may have
  2690. * lead to a throttle). This both saves work and prevents false
  2691. * next-buddy nomination below.
  2692. */
  2693. if (unlikely(throttled_hierarchy(cfs_rq_of(pse))))
  2694. return;
  2695. if (sched_feat(NEXT_BUDDY) && scale && !(wake_flags & WF_FORK)) {
  2696. set_next_buddy(pse);
  2697. next_buddy_marked = 1;
  2698. }
  2699. /*
  2700. * We can come here with TIF_NEED_RESCHED already set from new task
  2701. * wake up path.
  2702. *
  2703. * Note: this also catches the edge-case of curr being in a throttled
  2704. * group (e.g. via set_curr_task), since update_curr() (in the
  2705. * enqueue of curr) will have resulted in resched being set. This
  2706. * prevents us from potentially nominating it as a false LAST_BUDDY
  2707. * below.
  2708. */
  2709. if (test_tsk_need_resched(curr))
  2710. return;
  2711. /* Idle tasks are by definition preempted by non-idle tasks. */
  2712. if (unlikely(curr->policy == SCHED_IDLE) &&
  2713. likely(p->policy != SCHED_IDLE))
  2714. goto preempt;
  2715. /*
  2716. * Batch and idle tasks do not preempt non-idle tasks (their preemption
  2717. * is driven by the tick):
  2718. */
  2719. if (unlikely(p->policy != SCHED_NORMAL))
  2720. return;
  2721. find_matching_se(&se, &pse);
  2722. update_curr(cfs_rq_of(se));
  2723. BUG_ON(!pse);
  2724. if (wakeup_preempt_entity(se, pse) == 1) {
  2725. /*
  2726. * Bias pick_next to pick the sched entity that is
  2727. * triggering this preemption.
  2728. */
  2729. if (!next_buddy_marked)
  2730. set_next_buddy(pse);
  2731. goto preempt;
  2732. }
  2733. return;
  2734. preempt:
  2735. resched_task(curr);
  2736. /*
  2737. * Only set the backward buddy when the current task is still
  2738. * on the rq. This can happen when a wakeup gets interleaved
  2739. * with schedule on the ->pre_schedule() or idle_balance()
  2740. * point, either of which can * drop the rq lock.
  2741. *
  2742. * Also, during early boot the idle thread is in the fair class,
  2743. * for obvious reasons its a bad idea to schedule back to it.
  2744. */
  2745. if (unlikely(!se->on_rq || curr == rq->idle))
  2746. return;
  2747. if (sched_feat(LAST_BUDDY) && scale && entity_is_task(se))
  2748. set_last_buddy(se);
  2749. }
  2750. static struct task_struct *pick_next_task_fair(struct rq *rq)
  2751. {
  2752. struct task_struct *p;
  2753. struct cfs_rq *cfs_rq = &rq->cfs;
  2754. struct sched_entity *se;
  2755. if (!cfs_rq->nr_running)
  2756. return NULL;
  2757. do {
  2758. se = pick_next_entity(cfs_rq);
  2759. set_next_entity(cfs_rq, se);
  2760. cfs_rq = group_cfs_rq(se);
  2761. } while (cfs_rq);
  2762. p = task_of(se);
  2763. if (hrtick_enabled(rq))
  2764. hrtick_start_fair(rq, p);
  2765. return p;
  2766. }
  2767. /*
  2768. * Account for a descheduled task:
  2769. */
  2770. static void put_prev_task_fair(struct rq *rq, struct task_struct *prev)
  2771. {
  2772. struct sched_entity *se = &prev->se;
  2773. struct cfs_rq *cfs_rq;
  2774. for_each_sched_entity(se) {
  2775. cfs_rq = cfs_rq_of(se);
  2776. put_prev_entity(cfs_rq, se);
  2777. }
  2778. }
  2779. /*
  2780. * sched_yield() is very simple
  2781. *
  2782. * The magic of dealing with the ->skip buddy is in pick_next_entity.
  2783. */
  2784. static void yield_task_fair(struct rq *rq)
  2785. {
  2786. struct task_struct *curr = rq->curr;
  2787. struct cfs_rq *cfs_rq = task_cfs_rq(curr);
  2788. struct sched_entity *se = &curr->se;
  2789. /*
  2790. * Are we the only task in the tree?
  2791. */
  2792. if (unlikely(rq->nr_running == 1))
  2793. return;
  2794. clear_buddies(cfs_rq, se);
  2795. if (curr->policy != SCHED_BATCH) {
  2796. update_rq_clock(rq);
  2797. /*
  2798. * Update run-time statistics of the 'current'.
  2799. */
  2800. update_curr(cfs_rq);
  2801. /*
  2802. * Tell update_rq_clock() that we've just updated,
  2803. * so we don't do microscopic update in schedule()
  2804. * and double the fastpath cost.
  2805. */
  2806. rq->skip_clock_update = 1;
  2807. }
  2808. set_skip_buddy(se);
  2809. }
  2810. static bool yield_to_task_fair(struct rq *rq, struct task_struct *p, bool preempt)
  2811. {
  2812. struct sched_entity *se = &p->se;
  2813. /* throttled hierarchies are not runnable */
  2814. if (!se->on_rq || throttled_hierarchy(cfs_rq_of(se)))
  2815. return false;
  2816. /* Tell the scheduler that we'd really like pse to run next. */
  2817. set_next_buddy(se);
  2818. yield_task_fair(rq);
  2819. return true;
  2820. }
  2821. #ifdef CONFIG_SMP
  2822. /**************************************************
  2823. * Fair scheduling class load-balancing methods:
  2824. */
  2825. static unsigned long __read_mostly max_load_balance_interval = HZ/10;
  2826. #define LBF_ALL_PINNED 0x01
  2827. #define LBF_NEED_BREAK 0x02
  2828. #define LBF_SOME_PINNED 0x04
  2829. struct lb_env {
  2830. struct sched_domain *sd;
  2831. struct rq *src_rq;
  2832. int src_cpu;
  2833. int dst_cpu;
  2834. struct rq *dst_rq;
  2835. struct cpumask *dst_grpmask;
  2836. int new_dst_cpu;
  2837. enum cpu_idle_type idle;
  2838. long imbalance;
  2839. /* The set of CPUs under consideration for load-balancing */
  2840. struct cpumask *cpus;
  2841. unsigned int flags;
  2842. unsigned int loop;
  2843. unsigned int loop_break;
  2844. unsigned int loop_max;
  2845. };
  2846. /*
  2847. * move_task - move a task from one runqueue to another runqueue.
  2848. * Both runqueues must be locked.
  2849. */
  2850. static void move_task(struct task_struct *p, struct lb_env *env)
  2851. {
  2852. deactivate_task(env->src_rq, p, 0);
  2853. set_task_cpu(p, env->dst_cpu);
  2854. activate_task(env->dst_rq, p, 0);
  2855. check_preempt_curr(env->dst_rq, p, 0);
  2856. }
  2857. /*
  2858. * Is this task likely cache-hot:
  2859. */
  2860. static int
  2861. task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
  2862. {
  2863. s64 delta;
  2864. if (p->sched_class != &fair_sched_class)
  2865. return 0;
  2866. if (unlikely(p->policy == SCHED_IDLE))
  2867. return 0;
  2868. /*
  2869. * Buddy candidates are cache hot:
  2870. */
  2871. if (sched_feat(CACHE_HOT_BUDDY) && this_rq()->nr_running &&
  2872. (&p->se == cfs_rq_of(&p->se)->next ||
  2873. &p->se == cfs_rq_of(&p->se)->last))
  2874. return 1;
  2875. if (sysctl_sched_migration_cost == -1)
  2876. return 1;
  2877. if (sysctl_sched_migration_cost == 0)
  2878. return 0;
  2879. delta = now - p->se.exec_start;
  2880. return delta < (s64)sysctl_sched_migration_cost;
  2881. }
  2882. /*
  2883. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  2884. */
  2885. static
  2886. int can_migrate_task(struct task_struct *p, struct lb_env *env)
  2887. {
  2888. int tsk_cache_hot = 0;
  2889. /*
  2890. * We do not migrate tasks that are:
  2891. * 1) running (obviously), or
  2892. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  2893. * 3) are cache-hot on their current CPU.
  2894. */
  2895. if (!cpumask_test_cpu(env->dst_cpu, tsk_cpus_allowed(p))) {
  2896. int new_dst_cpu;
  2897. schedstat_inc(p, se.statistics.nr_failed_migrations_affine);
  2898. /*
  2899. * Remember if this task can be migrated to any other cpu in
  2900. * our sched_group. We may want to revisit it if we couldn't
  2901. * meet load balance goals by pulling other tasks on src_cpu.
  2902. *
  2903. * Also avoid computing new_dst_cpu if we have already computed
  2904. * one in current iteration.
  2905. */
  2906. if (!env->dst_grpmask || (env->flags & LBF_SOME_PINNED))
  2907. return 0;
  2908. new_dst_cpu = cpumask_first_and(env->dst_grpmask,
  2909. tsk_cpus_allowed(p));
  2910. if (new_dst_cpu < nr_cpu_ids) {
  2911. env->flags |= LBF_SOME_PINNED;
  2912. env->new_dst_cpu = new_dst_cpu;
  2913. }
  2914. return 0;
  2915. }
  2916. /* Record that we found atleast one task that could run on dst_cpu */
  2917. env->flags &= ~LBF_ALL_PINNED;
  2918. if (task_running(env->src_rq, p)) {
  2919. schedstat_inc(p, se.statistics.nr_failed_migrations_running);
  2920. return 0;
  2921. }
  2922. /*
  2923. * Aggressive migration if:
  2924. * 1) task is cache cold, or
  2925. * 2) too many balance attempts have failed.
  2926. */
  2927. tsk_cache_hot = task_hot(p, env->src_rq->clock_task, env->sd);
  2928. if (!tsk_cache_hot ||
  2929. env->sd->nr_balance_failed > env->sd->cache_nice_tries) {
  2930. #ifdef CONFIG_SCHEDSTATS
  2931. if (tsk_cache_hot) {
  2932. schedstat_inc(env->sd, lb_hot_gained[env->idle]);
  2933. schedstat_inc(p, se.statistics.nr_forced_migrations);
  2934. }
  2935. #endif
  2936. return 1;
  2937. }
  2938. if (tsk_cache_hot) {
  2939. schedstat_inc(p, se.statistics.nr_failed_migrations_hot);
  2940. return 0;
  2941. }
  2942. return 1;
  2943. }
  2944. /*
  2945. * move_one_task tries to move exactly one task from busiest to this_rq, as
  2946. * part of active balancing operations within "domain".
  2947. * Returns 1 if successful and 0 otherwise.
  2948. *
  2949. * Called with both runqueues locked.
  2950. */
  2951. static int move_one_task(struct lb_env *env)
  2952. {
  2953. struct task_struct *p, *n;
  2954. list_for_each_entry_safe(p, n, &env->src_rq->cfs_tasks, se.group_node) {
  2955. if (throttled_lb_pair(task_group(p), env->src_rq->cpu, env->dst_cpu))
  2956. continue;
  2957. if (!can_migrate_task(p, env))
  2958. continue;
  2959. move_task(p, env);
  2960. /*
  2961. * Right now, this is only the second place move_task()
  2962. * is called, so we can safely collect move_task()
  2963. * stats here rather than inside move_task().
  2964. */
  2965. schedstat_inc(env->sd, lb_gained[env->idle]);
  2966. return 1;
  2967. }
  2968. return 0;
  2969. }
  2970. static unsigned long task_h_load(struct task_struct *p);
  2971. static const unsigned int sched_nr_migrate_break = 32;
  2972. /*
  2973. * move_tasks tries to move up to imbalance weighted load from busiest to
  2974. * this_rq, as part of a balancing operation within domain "sd".
  2975. * Returns 1 if successful and 0 otherwise.
  2976. *
  2977. * Called with both runqueues locked.
  2978. */
  2979. static int move_tasks(struct lb_env *env)
  2980. {
  2981. struct list_head *tasks = &env->src_rq->cfs_tasks;
  2982. struct task_struct *p;
  2983. unsigned long load;
  2984. int pulled = 0;
  2985. if (env->imbalance <= 0)
  2986. return 0;
  2987. while (!list_empty(tasks)) {
  2988. p = list_first_entry(tasks, struct task_struct, se.group_node);
  2989. env->loop++;
  2990. /* We've more or less seen every task there is, call it quits */
  2991. if (env->loop > env->loop_max)
  2992. break;
  2993. /* take a breather every nr_migrate tasks */
  2994. if (env->loop > env->loop_break) {
  2995. env->loop_break += sched_nr_migrate_break;
  2996. env->flags |= LBF_NEED_BREAK;
  2997. break;
  2998. }
  2999. if (throttled_lb_pair(task_group(p), env->src_cpu, env->dst_cpu))
  3000. goto next;
  3001. load = task_h_load(p);
  3002. if (sched_feat(LB_MIN) && load < 16 && !env->sd->nr_balance_failed)
  3003. goto next;
  3004. if ((load / 2) > env->imbalance)
  3005. goto next;
  3006. if (!can_migrate_task(p, env))
  3007. goto next;
  3008. move_task(p, env);
  3009. pulled++;
  3010. env->imbalance -= load;
  3011. #ifdef CONFIG_PREEMPT
  3012. /*
  3013. * NEWIDLE balancing is a source of latency, so preemptible
  3014. * kernels will stop after the first task is pulled to minimize
  3015. * the critical section.
  3016. */
  3017. if (env->idle == CPU_NEWLY_IDLE)
  3018. break;
  3019. #endif
  3020. /*
  3021. * We only want to steal up to the prescribed amount of
  3022. * weighted load.
  3023. */
  3024. if (env->imbalance <= 0)
  3025. break;
  3026. continue;
  3027. next:
  3028. list_move_tail(&p->se.group_node, tasks);
  3029. }
  3030. /*
  3031. * Right now, this is one of only two places move_task() is called,
  3032. * so we can safely collect move_task() stats here rather than
  3033. * inside move_task().
  3034. */
  3035. schedstat_add(env->sd, lb_gained[env->idle], pulled);
  3036. return pulled;
  3037. }
  3038. #ifdef CONFIG_FAIR_GROUP_SCHED
  3039. /*
  3040. * update tg->load_weight by folding this cpu's load_avg
  3041. */
  3042. static int update_shares_cpu(struct task_group *tg, int cpu)
  3043. {
  3044. struct sched_entity *se;
  3045. struct cfs_rq *cfs_rq;
  3046. unsigned long flags;
  3047. struct rq *rq;
  3048. rq = cpu_rq(cpu);
  3049. se = tg->se[cpu];
  3050. cfs_rq = tg->cfs_rq[cpu];
  3051. raw_spin_lock_irqsave(&rq->lock, flags);
  3052. update_rq_clock(rq);
  3053. update_cfs_rq_blocked_load(cfs_rq, 1);
  3054. if (se) {
  3055. update_entity_load_avg(se, 1);
  3056. /*
  3057. * We pivot on our runnable average having decayed to zero for
  3058. * list removal. This generally implies that all our children
  3059. * have also been removed (modulo rounding error or bandwidth
  3060. * control); however, such cases are rare and we can fix these
  3061. * at enqueue.
  3062. *
  3063. * TODO: fix up out-of-order children on enqueue.
  3064. */
  3065. if (!se->avg.runnable_avg_sum && !cfs_rq->nr_running)
  3066. list_del_leaf_cfs_rq(cfs_rq);
  3067. } else {
  3068. update_rq_runnable_avg(rq, rq->nr_running);
  3069. }
  3070. raw_spin_unlock_irqrestore(&rq->lock, flags);
  3071. return 0;
  3072. }
  3073. static void update_shares(int cpu)
  3074. {
  3075. struct cfs_rq *cfs_rq;
  3076. struct rq *rq = cpu_rq(cpu);
  3077. rcu_read_lock();
  3078. /*
  3079. * Iterates the task_group tree in a bottom up fashion, see
  3080. * list_add_leaf_cfs_rq() for details.
  3081. */
  3082. for_each_leaf_cfs_rq(rq, cfs_rq) {
  3083. /* throttled entities do not contribute to load */
  3084. if (throttled_hierarchy(cfs_rq))
  3085. continue;
  3086. update_shares_cpu(cfs_rq->tg, cpu);
  3087. }
  3088. rcu_read_unlock();
  3089. }
  3090. /*
  3091. * Compute the cpu's hierarchical load factor for each task group.
  3092. * This needs to be done in a top-down fashion because the load of a child
  3093. * group is a fraction of its parents load.
  3094. */
  3095. static int tg_load_down(struct task_group *tg, void *data)
  3096. {
  3097. unsigned long load;
  3098. long cpu = (long)data;
  3099. if (!tg->parent) {
  3100. load = cpu_rq(cpu)->load.weight;
  3101. } else {
  3102. load = tg->parent->cfs_rq[cpu]->h_load;
  3103. load *= tg->se[cpu]->load.weight;
  3104. load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
  3105. }
  3106. tg->cfs_rq[cpu]->h_load = load;
  3107. return 0;
  3108. }
  3109. static void update_h_load(long cpu)
  3110. {
  3111. struct rq *rq = cpu_rq(cpu);
  3112. unsigned long now = jiffies;
  3113. if (rq->h_load_throttle == now)
  3114. return;
  3115. rq->h_load_throttle = now;
  3116. rcu_read_lock();
  3117. walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
  3118. rcu_read_unlock();
  3119. }
  3120. static unsigned long task_h_load(struct task_struct *p)
  3121. {
  3122. struct cfs_rq *cfs_rq = task_cfs_rq(p);
  3123. unsigned long load;
  3124. load = p->se.load.weight;
  3125. load = div_u64(load * cfs_rq->h_load, cfs_rq->load.weight + 1);
  3126. return load;
  3127. }
  3128. #else
  3129. static inline void update_shares(int cpu)
  3130. {
  3131. }
  3132. static inline void update_h_load(long cpu)
  3133. {
  3134. }
  3135. static unsigned long task_h_load(struct task_struct *p)
  3136. {
  3137. return p->se.load.weight;
  3138. }
  3139. #endif
  3140. /********** Helpers for find_busiest_group ************************/
  3141. /*
  3142. * sd_lb_stats - Structure to store the statistics of a sched_domain
  3143. * during load balancing.
  3144. */
  3145. struct sd_lb_stats {
  3146. struct sched_group *busiest; /* Busiest group in this sd */
  3147. struct sched_group *this; /* Local group in this sd */
  3148. unsigned long total_load; /* Total load of all groups in sd */
  3149. unsigned long total_pwr; /* Total power of all groups in sd */
  3150. unsigned long avg_load; /* Average load across all groups in sd */
  3151. /** Statistics of this group */
  3152. unsigned long this_load;
  3153. unsigned long this_load_per_task;
  3154. unsigned long this_nr_running;
  3155. unsigned long this_has_capacity;
  3156. unsigned int this_idle_cpus;
  3157. /* Statistics of the busiest group */
  3158. unsigned int busiest_idle_cpus;
  3159. unsigned long max_load;
  3160. unsigned long busiest_load_per_task;
  3161. unsigned long busiest_nr_running;
  3162. unsigned long busiest_group_capacity;
  3163. unsigned long busiest_has_capacity;
  3164. unsigned int busiest_group_weight;
  3165. int group_imb; /* Is there imbalance in this sd */
  3166. };
  3167. /*
  3168. * sg_lb_stats - stats of a sched_group required for load_balancing
  3169. */
  3170. struct sg_lb_stats {
  3171. unsigned long avg_load; /*Avg load across the CPUs of the group */
  3172. unsigned long group_load; /* Total load over the CPUs of the group */
  3173. unsigned long sum_nr_running; /* Nr tasks running in the group */
  3174. unsigned long sum_weighted_load; /* Weighted load of group's tasks */
  3175. unsigned long group_capacity;
  3176. unsigned long idle_cpus;
  3177. unsigned long group_weight;
  3178. int group_imb; /* Is there an imbalance in the group ? */
  3179. int group_has_capacity; /* Is there extra capacity in the group? */
  3180. };
  3181. /**
  3182. * get_sd_load_idx - Obtain the load index for a given sched domain.
  3183. * @sd: The sched_domain whose load_idx is to be obtained.
  3184. * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
  3185. */
  3186. static inline int get_sd_load_idx(struct sched_domain *sd,
  3187. enum cpu_idle_type idle)
  3188. {
  3189. int load_idx;
  3190. switch (idle) {
  3191. case CPU_NOT_IDLE:
  3192. load_idx = sd->busy_idx;
  3193. break;
  3194. case CPU_NEWLY_IDLE:
  3195. load_idx = sd->newidle_idx;
  3196. break;
  3197. default:
  3198. load_idx = sd->idle_idx;
  3199. break;
  3200. }
  3201. return load_idx;
  3202. }
  3203. unsigned long default_scale_freq_power(struct sched_domain *sd, int cpu)
  3204. {
  3205. return SCHED_POWER_SCALE;
  3206. }
  3207. unsigned long __weak arch_scale_freq_power(struct sched_domain *sd, int cpu)
  3208. {
  3209. return default_scale_freq_power(sd, cpu);
  3210. }
  3211. unsigned long default_scale_smt_power(struct sched_domain *sd, int cpu)
  3212. {
  3213. unsigned long weight = sd->span_weight;
  3214. unsigned long smt_gain = sd->smt_gain;
  3215. smt_gain /= weight;
  3216. return smt_gain;
  3217. }
  3218. unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
  3219. {
  3220. return default_scale_smt_power(sd, cpu);
  3221. }
  3222. unsigned long scale_rt_power(int cpu)
  3223. {
  3224. struct rq *rq = cpu_rq(cpu);
  3225. u64 total, available, age_stamp, avg;
  3226. /*
  3227. * Since we're reading these variables without serialization make sure
  3228. * we read them once before doing sanity checks on them.
  3229. */
  3230. age_stamp = ACCESS_ONCE(rq->age_stamp);
  3231. avg = ACCESS_ONCE(rq->rt_avg);
  3232. total = sched_avg_period() + (rq->clock - age_stamp);
  3233. if (unlikely(total < avg)) {
  3234. /* Ensures that power won't end up being negative */
  3235. available = 0;
  3236. } else {
  3237. available = total - avg;
  3238. }
  3239. if (unlikely((s64)total < SCHED_POWER_SCALE))
  3240. total = SCHED_POWER_SCALE;
  3241. total >>= SCHED_POWER_SHIFT;
  3242. return div_u64(available, total);
  3243. }
  3244. static void update_cpu_power(struct sched_domain *sd, int cpu)
  3245. {
  3246. unsigned long weight = sd->span_weight;
  3247. unsigned long power = SCHED_POWER_SCALE;
  3248. struct sched_group *sdg = sd->groups;
  3249. if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
  3250. if (sched_feat(ARCH_POWER))
  3251. power *= arch_scale_smt_power(sd, cpu);
  3252. else
  3253. power *= default_scale_smt_power(sd, cpu);
  3254. power >>= SCHED_POWER_SHIFT;
  3255. }
  3256. sdg->sgp->power_orig = power;
  3257. if (sched_feat(ARCH_POWER))
  3258. power *= arch_scale_freq_power(sd, cpu);
  3259. else
  3260. power *= default_scale_freq_power(sd, cpu);
  3261. power >>= SCHED_POWER_SHIFT;
  3262. power *= scale_rt_power(cpu);
  3263. power >>= SCHED_POWER_SHIFT;
  3264. if (!power)
  3265. power = 1;
  3266. cpu_rq(cpu)->cpu_power = power;
  3267. sdg->sgp->power = power;
  3268. }
  3269. void update_group_power(struct sched_domain *sd, int cpu)
  3270. {
  3271. struct sched_domain *child = sd->child;
  3272. struct sched_group *group, *sdg = sd->groups;
  3273. unsigned long power;
  3274. unsigned long interval;
  3275. interval = msecs_to_jiffies(sd->balance_interval);
  3276. interval = clamp(interval, 1UL, max_load_balance_interval);
  3277. sdg->sgp->next_update = jiffies + interval;
  3278. if (!child) {
  3279. update_cpu_power(sd, cpu);
  3280. return;
  3281. }
  3282. power = 0;
  3283. if (child->flags & SD_OVERLAP) {
  3284. /*
  3285. * SD_OVERLAP domains cannot assume that child groups
  3286. * span the current group.
  3287. */
  3288. for_each_cpu(cpu, sched_group_cpus(sdg))
  3289. power += power_of(cpu);
  3290. } else {
  3291. /*
  3292. * !SD_OVERLAP domains can assume that child groups
  3293. * span the current group.
  3294. */
  3295. group = child->groups;
  3296. do {
  3297. power += group->sgp->power;
  3298. group = group->next;
  3299. } while (group != child->groups);
  3300. }
  3301. sdg->sgp->power_orig = sdg->sgp->power = power;
  3302. }
  3303. /*
  3304. * Try and fix up capacity for tiny siblings, this is needed when
  3305. * things like SD_ASYM_PACKING need f_b_g to select another sibling
  3306. * which on its own isn't powerful enough.
  3307. *
  3308. * See update_sd_pick_busiest() and check_asym_packing().
  3309. */
  3310. static inline int
  3311. fix_small_capacity(struct sched_domain *sd, struct sched_group *group)
  3312. {
  3313. /*
  3314. * Only siblings can have significantly less than SCHED_POWER_SCALE
  3315. */
  3316. if (!(sd->flags & SD_SHARE_CPUPOWER))
  3317. return 0;
  3318. /*
  3319. * If ~90% of the cpu_power is still there, we're good.
  3320. */
  3321. if (group->sgp->power * 32 > group->sgp->power_orig * 29)
  3322. return 1;
  3323. return 0;
  3324. }
  3325. /**
  3326. * update_sg_lb_stats - Update sched_group's statistics for load balancing.
  3327. * @env: The load balancing environment.
  3328. * @group: sched_group whose statistics are to be updated.
  3329. * @load_idx: Load index of sched_domain of this_cpu for load calc.
  3330. * @local_group: Does group contain this_cpu.
  3331. * @balance: Should we balance.
  3332. * @sgs: variable to hold the statistics for this group.
  3333. */
  3334. static inline void update_sg_lb_stats(struct lb_env *env,
  3335. struct sched_group *group, int load_idx,
  3336. int local_group, int *balance, struct sg_lb_stats *sgs)
  3337. {
  3338. unsigned long nr_running, max_nr_running, min_nr_running;
  3339. unsigned long load, max_cpu_load, min_cpu_load;
  3340. unsigned int balance_cpu = -1, first_idle_cpu = 0;
  3341. unsigned long avg_load_per_task = 0;
  3342. int i;
  3343. if (local_group)
  3344. balance_cpu = group_balance_cpu(group);
  3345. /* Tally up the load of all CPUs in the group */
  3346. max_cpu_load = 0;
  3347. min_cpu_load = ~0UL;
  3348. max_nr_running = 0;
  3349. min_nr_running = ~0UL;
  3350. for_each_cpu_and(i, sched_group_cpus(group), env->cpus) {
  3351. struct rq *rq = cpu_rq(i);
  3352. nr_running = rq->nr_running;
  3353. /* Bias balancing toward cpus of our domain */
  3354. if (local_group) {
  3355. if (idle_cpu(i) && !first_idle_cpu &&
  3356. cpumask_test_cpu(i, sched_group_mask(group))) {
  3357. first_idle_cpu = 1;
  3358. balance_cpu = i;
  3359. }
  3360. load = target_load(i, load_idx);
  3361. } else {
  3362. load = source_load(i, load_idx);
  3363. if (load > max_cpu_load)
  3364. max_cpu_load = load;
  3365. if (min_cpu_load > load)
  3366. min_cpu_load = load;
  3367. if (nr_running > max_nr_running)
  3368. max_nr_running = nr_running;
  3369. if (min_nr_running > nr_running)
  3370. min_nr_running = nr_running;
  3371. }
  3372. sgs->group_load += load;
  3373. sgs->sum_nr_running += nr_running;
  3374. sgs->sum_weighted_load += weighted_cpuload(i);
  3375. if (idle_cpu(i))
  3376. sgs->idle_cpus++;
  3377. }
  3378. /*
  3379. * First idle cpu or the first cpu(busiest) in this sched group
  3380. * is eligible for doing load balancing at this and above
  3381. * domains. In the newly idle case, we will allow all the cpu's
  3382. * to do the newly idle load balance.
  3383. */
  3384. if (local_group) {
  3385. if (env->idle != CPU_NEWLY_IDLE) {
  3386. if (balance_cpu != env->dst_cpu) {
  3387. *balance = 0;
  3388. return;
  3389. }
  3390. update_group_power(env->sd, env->dst_cpu);
  3391. } else if (time_after_eq(jiffies, group->sgp->next_update))
  3392. update_group_power(env->sd, env->dst_cpu);
  3393. }
  3394. /* Adjust by relative CPU power of the group */
  3395. sgs->avg_load = (sgs->group_load*SCHED_POWER_SCALE) / group->sgp->power;
  3396. /*
  3397. * Consider the group unbalanced when the imbalance is larger
  3398. * than the average weight of a task.
  3399. *
  3400. * APZ: with cgroup the avg task weight can vary wildly and
  3401. * might not be a suitable number - should we keep a
  3402. * normalized nr_running number somewhere that negates
  3403. * the hierarchy?
  3404. */
  3405. if (sgs->sum_nr_running)
  3406. avg_load_per_task = sgs->sum_weighted_load / sgs->sum_nr_running;
  3407. if ((max_cpu_load - min_cpu_load) >= avg_load_per_task &&
  3408. (max_nr_running - min_nr_running) > 1)
  3409. sgs->group_imb = 1;
  3410. sgs->group_capacity = DIV_ROUND_CLOSEST(group->sgp->power,
  3411. SCHED_POWER_SCALE);
  3412. if (!sgs->group_capacity)
  3413. sgs->group_capacity = fix_small_capacity(env->sd, group);
  3414. sgs->group_weight = group->group_weight;
  3415. if (sgs->group_capacity > sgs->sum_nr_running)
  3416. sgs->group_has_capacity = 1;
  3417. }
  3418. /**
  3419. * update_sd_pick_busiest - return 1 on busiest group
  3420. * @env: The load balancing environment.
  3421. * @sds: sched_domain statistics
  3422. * @sg: sched_group candidate to be checked for being the busiest
  3423. * @sgs: sched_group statistics
  3424. *
  3425. * Determine if @sg is a busier group than the previously selected
  3426. * busiest group.
  3427. */
  3428. static bool update_sd_pick_busiest(struct lb_env *env,
  3429. struct sd_lb_stats *sds,
  3430. struct sched_group *sg,
  3431. struct sg_lb_stats *sgs)
  3432. {
  3433. if (sgs->avg_load <= sds->max_load)
  3434. return false;
  3435. if (sgs->sum_nr_running > sgs->group_capacity)
  3436. return true;
  3437. if (sgs->group_imb)
  3438. return true;
  3439. /*
  3440. * ASYM_PACKING needs to move all the work to the lowest
  3441. * numbered CPUs in the group, therefore mark all groups
  3442. * higher than ourself as busy.
  3443. */
  3444. if ((env->sd->flags & SD_ASYM_PACKING) && sgs->sum_nr_running &&
  3445. env->dst_cpu < group_first_cpu(sg)) {
  3446. if (!sds->busiest)
  3447. return true;
  3448. if (group_first_cpu(sds->busiest) > group_first_cpu(sg))
  3449. return true;
  3450. }
  3451. return false;
  3452. }
  3453. /**
  3454. * update_sd_lb_stats - Update sched_domain's statistics for load balancing.
  3455. * @env: The load balancing environment.
  3456. * @balance: Should we balance.
  3457. * @sds: variable to hold the statistics for this sched_domain.
  3458. */
  3459. static inline void update_sd_lb_stats(struct lb_env *env,
  3460. int *balance, struct sd_lb_stats *sds)
  3461. {
  3462. struct sched_domain *child = env->sd->child;
  3463. struct sched_group *sg = env->sd->groups;
  3464. struct sg_lb_stats sgs;
  3465. int load_idx, prefer_sibling = 0;
  3466. if (child && child->flags & SD_PREFER_SIBLING)
  3467. prefer_sibling = 1;
  3468. load_idx = get_sd_load_idx(env->sd, env->idle);
  3469. do {
  3470. int local_group;
  3471. local_group = cpumask_test_cpu(env->dst_cpu, sched_group_cpus(sg));
  3472. memset(&sgs, 0, sizeof(sgs));
  3473. update_sg_lb_stats(env, sg, load_idx, local_group, balance, &sgs);
  3474. if (local_group && !(*balance))
  3475. return;
  3476. sds->total_load += sgs.group_load;
  3477. sds->total_pwr += sg->sgp->power;
  3478. /*
  3479. * In case the child domain prefers tasks go to siblings
  3480. * first, lower the sg capacity to one so that we'll try
  3481. * and move all the excess tasks away. We lower the capacity
  3482. * of a group only if the local group has the capacity to fit
  3483. * these excess tasks, i.e. nr_running < group_capacity. The
  3484. * extra check prevents the case where you always pull from the
  3485. * heaviest group when it is already under-utilized (possible
  3486. * with a large weight task outweighs the tasks on the system).
  3487. */
  3488. if (prefer_sibling && !local_group && sds->this_has_capacity)
  3489. sgs.group_capacity = min(sgs.group_capacity, 1UL);
  3490. if (local_group) {
  3491. sds->this_load = sgs.avg_load;
  3492. sds->this = sg;
  3493. sds->this_nr_running = sgs.sum_nr_running;
  3494. sds->this_load_per_task = sgs.sum_weighted_load;
  3495. sds->this_has_capacity = sgs.group_has_capacity;
  3496. sds->this_idle_cpus = sgs.idle_cpus;
  3497. } else if (update_sd_pick_busiest(env, sds, sg, &sgs)) {
  3498. sds->max_load = sgs.avg_load;
  3499. sds->busiest = sg;
  3500. sds->busiest_nr_running = sgs.sum_nr_running;
  3501. sds->busiest_idle_cpus = sgs.idle_cpus;
  3502. sds->busiest_group_capacity = sgs.group_capacity;
  3503. sds->busiest_load_per_task = sgs.sum_weighted_load;
  3504. sds->busiest_has_capacity = sgs.group_has_capacity;
  3505. sds->busiest_group_weight = sgs.group_weight;
  3506. sds->group_imb = sgs.group_imb;
  3507. }
  3508. sg = sg->next;
  3509. } while (sg != env->sd->groups);
  3510. }
  3511. /**
  3512. * check_asym_packing - Check to see if the group is packed into the
  3513. * sched doman.
  3514. *
  3515. * This is primarily intended to used at the sibling level. Some
  3516. * cores like POWER7 prefer to use lower numbered SMT threads. In the
  3517. * case of POWER7, it can move to lower SMT modes only when higher
  3518. * threads are idle. When in lower SMT modes, the threads will
  3519. * perform better since they share less core resources. Hence when we
  3520. * have idle threads, we want them to be the higher ones.
  3521. *
  3522. * This packing function is run on idle threads. It checks to see if
  3523. * the busiest CPU in this domain (core in the P7 case) has a higher
  3524. * CPU number than the packing function is being run on. Here we are
  3525. * assuming lower CPU number will be equivalent to lower a SMT thread
  3526. * number.
  3527. *
  3528. * Returns 1 when packing is required and a task should be moved to
  3529. * this CPU. The amount of the imbalance is returned in *imbalance.
  3530. *
  3531. * @env: The load balancing environment.
  3532. * @sds: Statistics of the sched_domain which is to be packed
  3533. */
  3534. static int check_asym_packing(struct lb_env *env, struct sd_lb_stats *sds)
  3535. {
  3536. int busiest_cpu;
  3537. if (!(env->sd->flags & SD_ASYM_PACKING))
  3538. return 0;
  3539. if (!sds->busiest)
  3540. return 0;
  3541. busiest_cpu = group_first_cpu(sds->busiest);
  3542. if (env->dst_cpu > busiest_cpu)
  3543. return 0;
  3544. env->imbalance = DIV_ROUND_CLOSEST(
  3545. sds->max_load * sds->busiest->sgp->power, SCHED_POWER_SCALE);
  3546. return 1;
  3547. }
  3548. /**
  3549. * fix_small_imbalance - Calculate the minor imbalance that exists
  3550. * amongst the groups of a sched_domain, during
  3551. * load balancing.
  3552. * @env: The load balancing environment.
  3553. * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
  3554. */
  3555. static inline
  3556. void fix_small_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
  3557. {
  3558. unsigned long tmp, pwr_now = 0, pwr_move = 0;
  3559. unsigned int imbn = 2;
  3560. unsigned long scaled_busy_load_per_task;
  3561. if (sds->this_nr_running) {
  3562. sds->this_load_per_task /= sds->this_nr_running;
  3563. if (sds->busiest_load_per_task >
  3564. sds->this_load_per_task)
  3565. imbn = 1;
  3566. } else {
  3567. sds->this_load_per_task =
  3568. cpu_avg_load_per_task(env->dst_cpu);
  3569. }
  3570. scaled_busy_load_per_task = sds->busiest_load_per_task
  3571. * SCHED_POWER_SCALE;
  3572. scaled_busy_load_per_task /= sds->busiest->sgp->power;
  3573. if (sds->max_load - sds->this_load + scaled_busy_load_per_task >=
  3574. (scaled_busy_load_per_task * imbn)) {
  3575. env->imbalance = sds->busiest_load_per_task;
  3576. return;
  3577. }
  3578. /*
  3579. * OK, we don't have enough imbalance to justify moving tasks,
  3580. * however we may be able to increase total CPU power used by
  3581. * moving them.
  3582. */
  3583. pwr_now += sds->busiest->sgp->power *
  3584. min(sds->busiest_load_per_task, sds->max_load);
  3585. pwr_now += sds->this->sgp->power *
  3586. min(sds->this_load_per_task, sds->this_load);
  3587. pwr_now /= SCHED_POWER_SCALE;
  3588. /* Amount of load we'd subtract */
  3589. tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
  3590. sds->busiest->sgp->power;
  3591. if (sds->max_load > tmp)
  3592. pwr_move += sds->busiest->sgp->power *
  3593. min(sds->busiest_load_per_task, sds->max_load - tmp);
  3594. /* Amount of load we'd add */
  3595. if (sds->max_load * sds->busiest->sgp->power <
  3596. sds->busiest_load_per_task * SCHED_POWER_SCALE)
  3597. tmp = (sds->max_load * sds->busiest->sgp->power) /
  3598. sds->this->sgp->power;
  3599. else
  3600. tmp = (sds->busiest_load_per_task * SCHED_POWER_SCALE) /
  3601. sds->this->sgp->power;
  3602. pwr_move += sds->this->sgp->power *
  3603. min(sds->this_load_per_task, sds->this_load + tmp);
  3604. pwr_move /= SCHED_POWER_SCALE;
  3605. /* Move if we gain throughput */
  3606. if (pwr_move > pwr_now)
  3607. env->imbalance = sds->busiest_load_per_task;
  3608. }
  3609. /**
  3610. * calculate_imbalance - Calculate the amount of imbalance present within the
  3611. * groups of a given sched_domain during load balance.
  3612. * @env: load balance environment
  3613. * @sds: statistics of the sched_domain whose imbalance is to be calculated.
  3614. */
  3615. static inline void calculate_imbalance(struct lb_env *env, struct sd_lb_stats *sds)
  3616. {
  3617. unsigned long max_pull, load_above_capacity = ~0UL;
  3618. sds->busiest_load_per_task /= sds->busiest_nr_running;
  3619. if (sds->group_imb) {
  3620. sds->busiest_load_per_task =
  3621. min(sds->busiest_load_per_task, sds->avg_load);
  3622. }
  3623. /*
  3624. * In the presence of smp nice balancing, certain scenarios can have
  3625. * max load less than avg load(as we skip the groups at or below
  3626. * its cpu_power, while calculating max_load..)
  3627. */
  3628. if (sds->max_load < sds->avg_load) {
  3629. env->imbalance = 0;
  3630. return fix_small_imbalance(env, sds);
  3631. }
  3632. if (!sds->group_imb) {
  3633. /*
  3634. * Don't want to pull so many tasks that a group would go idle.
  3635. */
  3636. load_above_capacity = (sds->busiest_nr_running -
  3637. sds->busiest_group_capacity);
  3638. load_above_capacity *= (SCHED_LOAD_SCALE * SCHED_POWER_SCALE);
  3639. load_above_capacity /= sds->busiest->sgp->power;
  3640. }
  3641. /*
  3642. * We're trying to get all the cpus to the average_load, so we don't
  3643. * want to push ourselves above the average load, nor do we wish to
  3644. * reduce the max loaded cpu below the average load. At the same time,
  3645. * we also don't want to reduce the group load below the group capacity
  3646. * (so that we can implement power-savings policies etc). Thus we look
  3647. * for the minimum possible imbalance.
  3648. * Be careful of negative numbers as they'll appear as very large values
  3649. * with unsigned longs.
  3650. */
  3651. max_pull = min(sds->max_load - sds->avg_load, load_above_capacity);
  3652. /* How much load to actually move to equalise the imbalance */
  3653. env->imbalance = min(max_pull * sds->busiest->sgp->power,
  3654. (sds->avg_load - sds->this_load) * sds->this->sgp->power)
  3655. / SCHED_POWER_SCALE;
  3656. /*
  3657. * if *imbalance is less than the average load per runnable task
  3658. * there is no guarantee that any tasks will be moved so we'll have
  3659. * a think about bumping its value to force at least one task to be
  3660. * moved
  3661. */
  3662. if (env->imbalance < sds->busiest_load_per_task)
  3663. return fix_small_imbalance(env, sds);
  3664. }
  3665. /******* find_busiest_group() helpers end here *********************/
  3666. /**
  3667. * find_busiest_group - Returns the busiest group within the sched_domain
  3668. * if there is an imbalance. If there isn't an imbalance, and
  3669. * the user has opted for power-savings, it returns a group whose
  3670. * CPUs can be put to idle by rebalancing those tasks elsewhere, if
  3671. * such a group exists.
  3672. *
  3673. * Also calculates the amount of weighted load which should be moved
  3674. * to restore balance.
  3675. *
  3676. * @env: The load balancing environment.
  3677. * @balance: Pointer to a variable indicating if this_cpu
  3678. * is the appropriate cpu to perform load balancing at this_level.
  3679. *
  3680. * Returns: - the busiest group if imbalance exists.
  3681. * - If no imbalance and user has opted for power-savings balance,
  3682. * return the least loaded group whose CPUs can be
  3683. * put to idle by rebalancing its tasks onto our group.
  3684. */
  3685. static struct sched_group *
  3686. find_busiest_group(struct lb_env *env, int *balance)
  3687. {
  3688. struct sd_lb_stats sds;
  3689. memset(&sds, 0, sizeof(sds));
  3690. /*
  3691. * Compute the various statistics relavent for load balancing at
  3692. * this level.
  3693. */
  3694. update_sd_lb_stats(env, balance, &sds);
  3695. /*
  3696. * this_cpu is not the appropriate cpu to perform load balancing at
  3697. * this level.
  3698. */
  3699. if (!(*balance))
  3700. goto ret;
  3701. if ((env->idle == CPU_IDLE || env->idle == CPU_NEWLY_IDLE) &&
  3702. check_asym_packing(env, &sds))
  3703. return sds.busiest;
  3704. /* There is no busy sibling group to pull tasks from */
  3705. if (!sds.busiest || sds.busiest_nr_running == 0)
  3706. goto out_balanced;
  3707. sds.avg_load = (SCHED_POWER_SCALE * sds.total_load) / sds.total_pwr;
  3708. /*
  3709. * If the busiest group is imbalanced the below checks don't
  3710. * work because they assumes all things are equal, which typically
  3711. * isn't true due to cpus_allowed constraints and the like.
  3712. */
  3713. if (sds.group_imb)
  3714. goto force_balance;
  3715. /* SD_BALANCE_NEWIDLE trumps SMP nice when underutilized */
  3716. if (env->idle == CPU_NEWLY_IDLE && sds.this_has_capacity &&
  3717. !sds.busiest_has_capacity)
  3718. goto force_balance;
  3719. /*
  3720. * If the local group is more busy than the selected busiest group
  3721. * don't try and pull any tasks.
  3722. */
  3723. if (sds.this_load >= sds.max_load)
  3724. goto out_balanced;
  3725. /*
  3726. * Don't pull any tasks if this group is already above the domain
  3727. * average load.
  3728. */
  3729. if (sds.this_load >= sds.avg_load)
  3730. goto out_balanced;
  3731. if (env->idle == CPU_IDLE) {
  3732. /*
  3733. * This cpu is idle. If the busiest group load doesn't
  3734. * have more tasks than the number of available cpu's and
  3735. * there is no imbalance between this and busiest group
  3736. * wrt to idle cpu's, it is balanced.
  3737. */
  3738. if ((sds.this_idle_cpus <= sds.busiest_idle_cpus + 1) &&
  3739. sds.busiest_nr_running <= sds.busiest_group_weight)
  3740. goto out_balanced;
  3741. } else {
  3742. /*
  3743. * In the CPU_NEWLY_IDLE, CPU_NOT_IDLE cases, use
  3744. * imbalance_pct to be conservative.
  3745. */
  3746. if (100 * sds.max_load <= env->sd->imbalance_pct * sds.this_load)
  3747. goto out_balanced;
  3748. }
  3749. force_balance:
  3750. /* Looks like there is an imbalance. Compute it */
  3751. calculate_imbalance(env, &sds);
  3752. return sds.busiest;
  3753. out_balanced:
  3754. ret:
  3755. env->imbalance = 0;
  3756. return NULL;
  3757. }
  3758. /*
  3759. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  3760. */
  3761. static struct rq *find_busiest_queue(struct lb_env *env,
  3762. struct sched_group *group)
  3763. {
  3764. struct rq *busiest = NULL, *rq;
  3765. unsigned long max_load = 0;
  3766. int i;
  3767. for_each_cpu(i, sched_group_cpus(group)) {
  3768. unsigned long power = power_of(i);
  3769. unsigned long capacity = DIV_ROUND_CLOSEST(power,
  3770. SCHED_POWER_SCALE);
  3771. unsigned long wl;
  3772. if (!capacity)
  3773. capacity = fix_small_capacity(env->sd, group);
  3774. if (!cpumask_test_cpu(i, env->cpus))
  3775. continue;
  3776. rq = cpu_rq(i);
  3777. wl = weighted_cpuload(i);
  3778. /*
  3779. * When comparing with imbalance, use weighted_cpuload()
  3780. * which is not scaled with the cpu power.
  3781. */
  3782. if (capacity && rq->nr_running == 1 && wl > env->imbalance)
  3783. continue;
  3784. /*
  3785. * For the load comparisons with the other cpu's, consider
  3786. * the weighted_cpuload() scaled with the cpu power, so that
  3787. * the load can be moved away from the cpu that is potentially
  3788. * running at a lower capacity.
  3789. */
  3790. wl = (wl * SCHED_POWER_SCALE) / power;
  3791. if (wl > max_load) {
  3792. max_load = wl;
  3793. busiest = rq;
  3794. }
  3795. }
  3796. return busiest;
  3797. }
  3798. /*
  3799. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  3800. * so long as it is large enough.
  3801. */
  3802. #define MAX_PINNED_INTERVAL 512
  3803. /* Working cpumask for load_balance and load_balance_newidle. */
  3804. DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
  3805. static int need_active_balance(struct lb_env *env)
  3806. {
  3807. struct sched_domain *sd = env->sd;
  3808. if (env->idle == CPU_NEWLY_IDLE) {
  3809. /*
  3810. * ASYM_PACKING needs to force migrate tasks from busy but
  3811. * higher numbered CPUs in order to pack all tasks in the
  3812. * lowest numbered CPUs.
  3813. */
  3814. if ((sd->flags & SD_ASYM_PACKING) && env->src_cpu > env->dst_cpu)
  3815. return 1;
  3816. }
  3817. return unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2);
  3818. }
  3819. static int active_load_balance_cpu_stop(void *data);
  3820. /*
  3821. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  3822. * tasks if there is an imbalance.
  3823. */
  3824. static int load_balance(int this_cpu, struct rq *this_rq,
  3825. struct sched_domain *sd, enum cpu_idle_type idle,
  3826. int *balance)
  3827. {
  3828. int ld_moved, cur_ld_moved, active_balance = 0;
  3829. int lb_iterations, max_lb_iterations;
  3830. struct sched_group *group;
  3831. struct rq *busiest;
  3832. unsigned long flags;
  3833. struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
  3834. struct lb_env env = {
  3835. .sd = sd,
  3836. .dst_cpu = this_cpu,
  3837. .dst_rq = this_rq,
  3838. .dst_grpmask = sched_group_cpus(sd->groups),
  3839. .idle = idle,
  3840. .loop_break = sched_nr_migrate_break,
  3841. .cpus = cpus,
  3842. };
  3843. cpumask_copy(cpus, cpu_active_mask);
  3844. max_lb_iterations = cpumask_weight(env.dst_grpmask);
  3845. schedstat_inc(sd, lb_count[idle]);
  3846. redo:
  3847. group = find_busiest_group(&env, balance);
  3848. if (*balance == 0)
  3849. goto out_balanced;
  3850. if (!group) {
  3851. schedstat_inc(sd, lb_nobusyg[idle]);
  3852. goto out_balanced;
  3853. }
  3854. busiest = find_busiest_queue(&env, group);
  3855. if (!busiest) {
  3856. schedstat_inc(sd, lb_nobusyq[idle]);
  3857. goto out_balanced;
  3858. }
  3859. BUG_ON(busiest == env.dst_rq);
  3860. schedstat_add(sd, lb_imbalance[idle], env.imbalance);
  3861. ld_moved = 0;
  3862. lb_iterations = 1;
  3863. if (busiest->nr_running > 1) {
  3864. /*
  3865. * Attempt to move tasks. If find_busiest_group has found
  3866. * an imbalance but busiest->nr_running <= 1, the group is
  3867. * still unbalanced. ld_moved simply stays zero, so it is
  3868. * correctly treated as an imbalance.
  3869. */
  3870. env.flags |= LBF_ALL_PINNED;
  3871. env.src_cpu = busiest->cpu;
  3872. env.src_rq = busiest;
  3873. env.loop_max = min(sysctl_sched_nr_migrate, busiest->nr_running);
  3874. update_h_load(env.src_cpu);
  3875. more_balance:
  3876. local_irq_save(flags);
  3877. double_rq_lock(env.dst_rq, busiest);
  3878. /*
  3879. * cur_ld_moved - load moved in current iteration
  3880. * ld_moved - cumulative load moved across iterations
  3881. */
  3882. cur_ld_moved = move_tasks(&env);
  3883. ld_moved += cur_ld_moved;
  3884. double_rq_unlock(env.dst_rq, busiest);
  3885. local_irq_restore(flags);
  3886. if (env.flags & LBF_NEED_BREAK) {
  3887. env.flags &= ~LBF_NEED_BREAK;
  3888. goto more_balance;
  3889. }
  3890. /*
  3891. * some other cpu did the load balance for us.
  3892. */
  3893. if (cur_ld_moved && env.dst_cpu != smp_processor_id())
  3894. resched_cpu(env.dst_cpu);
  3895. /*
  3896. * Revisit (affine) tasks on src_cpu that couldn't be moved to
  3897. * us and move them to an alternate dst_cpu in our sched_group
  3898. * where they can run. The upper limit on how many times we
  3899. * iterate on same src_cpu is dependent on number of cpus in our
  3900. * sched_group.
  3901. *
  3902. * This changes load balance semantics a bit on who can move
  3903. * load to a given_cpu. In addition to the given_cpu itself
  3904. * (or a ilb_cpu acting on its behalf where given_cpu is
  3905. * nohz-idle), we now have balance_cpu in a position to move
  3906. * load to given_cpu. In rare situations, this may cause
  3907. * conflicts (balance_cpu and given_cpu/ilb_cpu deciding
  3908. * _independently_ and at _same_ time to move some load to
  3909. * given_cpu) causing exceess load to be moved to given_cpu.
  3910. * This however should not happen so much in practice and
  3911. * moreover subsequent load balance cycles should correct the
  3912. * excess load moved.
  3913. */
  3914. if ((env.flags & LBF_SOME_PINNED) && env.imbalance > 0 &&
  3915. lb_iterations++ < max_lb_iterations) {
  3916. env.dst_rq = cpu_rq(env.new_dst_cpu);
  3917. env.dst_cpu = env.new_dst_cpu;
  3918. env.flags &= ~LBF_SOME_PINNED;
  3919. env.loop = 0;
  3920. env.loop_break = sched_nr_migrate_break;
  3921. /*
  3922. * Go back to "more_balance" rather than "redo" since we
  3923. * need to continue with same src_cpu.
  3924. */
  3925. goto more_balance;
  3926. }
  3927. /* All tasks on this runqueue were pinned by CPU affinity */
  3928. if (unlikely(env.flags & LBF_ALL_PINNED)) {
  3929. cpumask_clear_cpu(cpu_of(busiest), cpus);
  3930. if (!cpumask_empty(cpus)) {
  3931. env.loop = 0;
  3932. env.loop_break = sched_nr_migrate_break;
  3933. goto redo;
  3934. }
  3935. goto out_balanced;
  3936. }
  3937. }
  3938. if (!ld_moved) {
  3939. schedstat_inc(sd, lb_failed[idle]);
  3940. /*
  3941. * Increment the failure counter only on periodic balance.
  3942. * We do not want newidle balance, which can be very
  3943. * frequent, pollute the failure counter causing
  3944. * excessive cache_hot migrations and active balances.
  3945. */
  3946. if (idle != CPU_NEWLY_IDLE)
  3947. sd->nr_balance_failed++;
  3948. if (need_active_balance(&env)) {
  3949. raw_spin_lock_irqsave(&busiest->lock, flags);
  3950. /* don't kick the active_load_balance_cpu_stop,
  3951. * if the curr task on busiest cpu can't be
  3952. * moved to this_cpu
  3953. */
  3954. if (!cpumask_test_cpu(this_cpu,
  3955. tsk_cpus_allowed(busiest->curr))) {
  3956. raw_spin_unlock_irqrestore(&busiest->lock,
  3957. flags);
  3958. env.flags |= LBF_ALL_PINNED;
  3959. goto out_one_pinned;
  3960. }
  3961. /*
  3962. * ->active_balance synchronizes accesses to
  3963. * ->active_balance_work. Once set, it's cleared
  3964. * only after active load balance is finished.
  3965. */
  3966. if (!busiest->active_balance) {
  3967. busiest->active_balance = 1;
  3968. busiest->push_cpu = this_cpu;
  3969. active_balance = 1;
  3970. }
  3971. raw_spin_unlock_irqrestore(&busiest->lock, flags);
  3972. if (active_balance) {
  3973. stop_one_cpu_nowait(cpu_of(busiest),
  3974. active_load_balance_cpu_stop, busiest,
  3975. &busiest->active_balance_work);
  3976. }
  3977. /*
  3978. * We've kicked active balancing, reset the failure
  3979. * counter.
  3980. */
  3981. sd->nr_balance_failed = sd->cache_nice_tries+1;
  3982. }
  3983. } else
  3984. sd->nr_balance_failed = 0;
  3985. if (likely(!active_balance)) {
  3986. /* We were unbalanced, so reset the balancing interval */
  3987. sd->balance_interval = sd->min_interval;
  3988. } else {
  3989. /*
  3990. * If we've begun active balancing, start to back off. This
  3991. * case may not be covered by the all_pinned logic if there
  3992. * is only 1 task on the busy runqueue (because we don't call
  3993. * move_tasks).
  3994. */
  3995. if (sd->balance_interval < sd->max_interval)
  3996. sd->balance_interval *= 2;
  3997. }
  3998. goto out;
  3999. out_balanced:
  4000. schedstat_inc(sd, lb_balanced[idle]);
  4001. sd->nr_balance_failed = 0;
  4002. out_one_pinned:
  4003. /* tune up the balancing interval */
  4004. if (((env.flags & LBF_ALL_PINNED) &&
  4005. sd->balance_interval < MAX_PINNED_INTERVAL) ||
  4006. (sd->balance_interval < sd->max_interval))
  4007. sd->balance_interval *= 2;
  4008. ld_moved = 0;
  4009. out:
  4010. return ld_moved;
  4011. }
  4012. /*
  4013. * idle_balance is called by schedule() if this_cpu is about to become
  4014. * idle. Attempts to pull tasks from other CPUs.
  4015. */
  4016. void idle_balance(int this_cpu, struct rq *this_rq)
  4017. {
  4018. struct sched_domain *sd;
  4019. int pulled_task = 0;
  4020. unsigned long next_balance = jiffies + HZ;
  4021. this_rq->idle_stamp = this_rq->clock;
  4022. if (this_rq->avg_idle < sysctl_sched_migration_cost)
  4023. return;
  4024. update_rq_runnable_avg(this_rq, 1);
  4025. /*
  4026. * Drop the rq->lock, but keep IRQ/preempt disabled.
  4027. */
  4028. raw_spin_unlock(&this_rq->lock);
  4029. update_shares(this_cpu);
  4030. rcu_read_lock();
  4031. for_each_domain(this_cpu, sd) {
  4032. unsigned long interval;
  4033. int balance = 1;
  4034. if (!(sd->flags & SD_LOAD_BALANCE))
  4035. continue;
  4036. if (sd->flags & SD_BALANCE_NEWIDLE) {
  4037. /* If we've pulled tasks over stop searching: */
  4038. pulled_task = load_balance(this_cpu, this_rq,
  4039. sd, CPU_NEWLY_IDLE, &balance);
  4040. }
  4041. interval = msecs_to_jiffies(sd->balance_interval);
  4042. if (time_after(next_balance, sd->last_balance + interval))
  4043. next_balance = sd->last_balance + interval;
  4044. if (pulled_task) {
  4045. this_rq->idle_stamp = 0;
  4046. break;
  4047. }
  4048. }
  4049. rcu_read_unlock();
  4050. raw_spin_lock(&this_rq->lock);
  4051. if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
  4052. /*
  4053. * We are going idle. next_balance may be set based on
  4054. * a busy processor. So reset next_balance.
  4055. */
  4056. this_rq->next_balance = next_balance;
  4057. }
  4058. }
  4059. /*
  4060. * active_load_balance_cpu_stop is run by cpu stopper. It pushes
  4061. * running tasks off the busiest CPU onto idle CPUs. It requires at
  4062. * least 1 task to be running on each physical CPU where possible, and
  4063. * avoids physical / logical imbalances.
  4064. */
  4065. static int active_load_balance_cpu_stop(void *data)
  4066. {
  4067. struct rq *busiest_rq = data;
  4068. int busiest_cpu = cpu_of(busiest_rq);
  4069. int target_cpu = busiest_rq->push_cpu;
  4070. struct rq *target_rq = cpu_rq(target_cpu);
  4071. struct sched_domain *sd;
  4072. raw_spin_lock_irq(&busiest_rq->lock);
  4073. /* make sure the requested cpu hasn't gone down in the meantime */
  4074. if (unlikely(busiest_cpu != smp_processor_id() ||
  4075. !busiest_rq->active_balance))
  4076. goto out_unlock;
  4077. /* Is there any task to move? */
  4078. if (busiest_rq->nr_running <= 1)
  4079. goto out_unlock;
  4080. /*
  4081. * This condition is "impossible", if it occurs
  4082. * we need to fix it. Originally reported by
  4083. * Bjorn Helgaas on a 128-cpu setup.
  4084. */
  4085. BUG_ON(busiest_rq == target_rq);
  4086. /* move a task from busiest_rq to target_rq */
  4087. double_lock_balance(busiest_rq, target_rq);
  4088. /* Search for an sd spanning us and the target CPU. */
  4089. rcu_read_lock();
  4090. for_each_domain(target_cpu, sd) {
  4091. if ((sd->flags & SD_LOAD_BALANCE) &&
  4092. cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
  4093. break;
  4094. }
  4095. if (likely(sd)) {
  4096. struct lb_env env = {
  4097. .sd = sd,
  4098. .dst_cpu = target_cpu,
  4099. .dst_rq = target_rq,
  4100. .src_cpu = busiest_rq->cpu,
  4101. .src_rq = busiest_rq,
  4102. .idle = CPU_IDLE,
  4103. };
  4104. schedstat_inc(sd, alb_count);
  4105. if (move_one_task(&env))
  4106. schedstat_inc(sd, alb_pushed);
  4107. else
  4108. schedstat_inc(sd, alb_failed);
  4109. }
  4110. rcu_read_unlock();
  4111. double_unlock_balance(busiest_rq, target_rq);
  4112. out_unlock:
  4113. busiest_rq->active_balance = 0;
  4114. raw_spin_unlock_irq(&busiest_rq->lock);
  4115. return 0;
  4116. }
  4117. #ifdef CONFIG_NO_HZ
  4118. /*
  4119. * idle load balancing details
  4120. * - When one of the busy CPUs notice that there may be an idle rebalancing
  4121. * needed, they will kick the idle load balancer, which then does idle
  4122. * load balancing for all the idle CPUs.
  4123. */
  4124. static struct {
  4125. cpumask_var_t idle_cpus_mask;
  4126. atomic_t nr_cpus;
  4127. unsigned long next_balance; /* in jiffy units */
  4128. } nohz ____cacheline_aligned;
  4129. static inline int find_new_ilb(int call_cpu)
  4130. {
  4131. int ilb = cpumask_first(nohz.idle_cpus_mask);
  4132. if (ilb < nr_cpu_ids && idle_cpu(ilb))
  4133. return ilb;
  4134. return nr_cpu_ids;
  4135. }
  4136. /*
  4137. * Kick a CPU to do the nohz balancing, if it is time for it. We pick the
  4138. * nohz_load_balancer CPU (if there is one) otherwise fallback to any idle
  4139. * CPU (if there is one).
  4140. */
  4141. static void nohz_balancer_kick(int cpu)
  4142. {
  4143. int ilb_cpu;
  4144. nohz.next_balance++;
  4145. ilb_cpu = find_new_ilb(cpu);
  4146. if (ilb_cpu >= nr_cpu_ids)
  4147. return;
  4148. if (test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(ilb_cpu)))
  4149. return;
  4150. /*
  4151. * Use smp_send_reschedule() instead of resched_cpu().
  4152. * This way we generate a sched IPI on the target cpu which
  4153. * is idle. And the softirq performing nohz idle load balance
  4154. * will be run before returning from the IPI.
  4155. */
  4156. smp_send_reschedule(ilb_cpu);
  4157. return;
  4158. }
  4159. static inline void nohz_balance_exit_idle(int cpu)
  4160. {
  4161. if (unlikely(test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))) {
  4162. cpumask_clear_cpu(cpu, nohz.idle_cpus_mask);
  4163. atomic_dec(&nohz.nr_cpus);
  4164. clear_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
  4165. }
  4166. }
  4167. static inline void set_cpu_sd_state_busy(void)
  4168. {
  4169. struct sched_domain *sd;
  4170. int cpu = smp_processor_id();
  4171. if (!test_bit(NOHZ_IDLE, nohz_flags(cpu)))
  4172. return;
  4173. clear_bit(NOHZ_IDLE, nohz_flags(cpu));
  4174. rcu_read_lock();
  4175. for_each_domain(cpu, sd)
  4176. atomic_inc(&sd->groups->sgp->nr_busy_cpus);
  4177. rcu_read_unlock();
  4178. }
  4179. void set_cpu_sd_state_idle(void)
  4180. {
  4181. struct sched_domain *sd;
  4182. int cpu = smp_processor_id();
  4183. if (test_bit(NOHZ_IDLE, nohz_flags(cpu)))
  4184. return;
  4185. set_bit(NOHZ_IDLE, nohz_flags(cpu));
  4186. rcu_read_lock();
  4187. for_each_domain(cpu, sd)
  4188. atomic_dec(&sd->groups->sgp->nr_busy_cpus);
  4189. rcu_read_unlock();
  4190. }
  4191. /*
  4192. * This routine will record that the cpu is going idle with tick stopped.
  4193. * This info will be used in performing idle load balancing in the future.
  4194. */
  4195. void nohz_balance_enter_idle(int cpu)
  4196. {
  4197. /*
  4198. * If this cpu is going down, then nothing needs to be done.
  4199. */
  4200. if (!cpu_active(cpu))
  4201. return;
  4202. if (test_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu)))
  4203. return;
  4204. cpumask_set_cpu(cpu, nohz.idle_cpus_mask);
  4205. atomic_inc(&nohz.nr_cpus);
  4206. set_bit(NOHZ_TICK_STOPPED, nohz_flags(cpu));
  4207. }
  4208. static int __cpuinit sched_ilb_notifier(struct notifier_block *nfb,
  4209. unsigned long action, void *hcpu)
  4210. {
  4211. switch (action & ~CPU_TASKS_FROZEN) {
  4212. case CPU_DYING:
  4213. nohz_balance_exit_idle(smp_processor_id());
  4214. return NOTIFY_OK;
  4215. default:
  4216. return NOTIFY_DONE;
  4217. }
  4218. }
  4219. #endif
  4220. static DEFINE_SPINLOCK(balancing);
  4221. /*
  4222. * Scale the max load_balance interval with the number of CPUs in the system.
  4223. * This trades load-balance latency on larger machines for less cross talk.
  4224. */
  4225. void update_max_interval(void)
  4226. {
  4227. max_load_balance_interval = HZ*num_online_cpus()/10;
  4228. }
  4229. /*
  4230. * It checks each scheduling domain to see if it is due to be balanced,
  4231. * and initiates a balancing operation if so.
  4232. *
  4233. * Balancing parameters are set up in arch_init_sched_domains.
  4234. */
  4235. static void rebalance_domains(int cpu, enum cpu_idle_type idle)
  4236. {
  4237. int balance = 1;
  4238. struct rq *rq = cpu_rq(cpu);
  4239. unsigned long interval;
  4240. struct sched_domain *sd;
  4241. /* Earliest time when we have to do rebalance again */
  4242. unsigned long next_balance = jiffies + 60*HZ;
  4243. int update_next_balance = 0;
  4244. int need_serialize;
  4245. update_shares(cpu);
  4246. rcu_read_lock();
  4247. for_each_domain(cpu, sd) {
  4248. if (!(sd->flags & SD_LOAD_BALANCE))
  4249. continue;
  4250. interval = sd->balance_interval;
  4251. if (idle != CPU_IDLE)
  4252. interval *= sd->busy_factor;
  4253. /* scale ms to jiffies */
  4254. interval = msecs_to_jiffies(interval);
  4255. interval = clamp(interval, 1UL, max_load_balance_interval);
  4256. need_serialize = sd->flags & SD_SERIALIZE;
  4257. if (need_serialize) {
  4258. if (!spin_trylock(&balancing))
  4259. goto out;
  4260. }
  4261. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  4262. if (load_balance(cpu, rq, sd, idle, &balance)) {
  4263. /*
  4264. * We've pulled tasks over so either we're no
  4265. * longer idle.
  4266. */
  4267. idle = CPU_NOT_IDLE;
  4268. }
  4269. sd->last_balance = jiffies;
  4270. }
  4271. if (need_serialize)
  4272. spin_unlock(&balancing);
  4273. out:
  4274. if (time_after(next_balance, sd->last_balance + interval)) {
  4275. next_balance = sd->last_balance + interval;
  4276. update_next_balance = 1;
  4277. }
  4278. /*
  4279. * Stop the load balance at this level. There is another
  4280. * CPU in our sched group which is doing load balancing more
  4281. * actively.
  4282. */
  4283. if (!balance)
  4284. break;
  4285. }
  4286. rcu_read_unlock();
  4287. /*
  4288. * next_balance will be updated only when there is a need.
  4289. * When the cpu is attached to null domain for ex, it will not be
  4290. * updated.
  4291. */
  4292. if (likely(update_next_balance))
  4293. rq->next_balance = next_balance;
  4294. }
  4295. #ifdef CONFIG_NO_HZ
  4296. /*
  4297. * In CONFIG_NO_HZ case, the idle balance kickee will do the
  4298. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  4299. */
  4300. static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle)
  4301. {
  4302. struct rq *this_rq = cpu_rq(this_cpu);
  4303. struct rq *rq;
  4304. int balance_cpu;
  4305. if (idle != CPU_IDLE ||
  4306. !test_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu)))
  4307. goto end;
  4308. for_each_cpu(balance_cpu, nohz.idle_cpus_mask) {
  4309. if (balance_cpu == this_cpu || !idle_cpu(balance_cpu))
  4310. continue;
  4311. /*
  4312. * If this cpu gets work to do, stop the load balancing
  4313. * work being done for other cpus. Next load
  4314. * balancing owner will pick it up.
  4315. */
  4316. if (need_resched())
  4317. break;
  4318. rq = cpu_rq(balance_cpu);
  4319. raw_spin_lock_irq(&rq->lock);
  4320. update_rq_clock(rq);
  4321. update_idle_cpu_load(rq);
  4322. raw_spin_unlock_irq(&rq->lock);
  4323. rebalance_domains(balance_cpu, CPU_IDLE);
  4324. if (time_after(this_rq->next_balance, rq->next_balance))
  4325. this_rq->next_balance = rq->next_balance;
  4326. }
  4327. nohz.next_balance = this_rq->next_balance;
  4328. end:
  4329. clear_bit(NOHZ_BALANCE_KICK, nohz_flags(this_cpu));
  4330. }
  4331. /*
  4332. * Current heuristic for kicking the idle load balancer in the presence
  4333. * of an idle cpu is the system.
  4334. * - This rq has more than one task.
  4335. * - At any scheduler domain level, this cpu's scheduler group has multiple
  4336. * busy cpu's exceeding the group's power.
  4337. * - For SD_ASYM_PACKING, if the lower numbered cpu's in the scheduler
  4338. * domain span are idle.
  4339. */
  4340. static inline int nohz_kick_needed(struct rq *rq, int cpu)
  4341. {
  4342. unsigned long now = jiffies;
  4343. struct sched_domain *sd;
  4344. if (unlikely(idle_cpu(cpu)))
  4345. return 0;
  4346. /*
  4347. * We may be recently in ticked or tickless idle mode. At the first
  4348. * busy tick after returning from idle, we will update the busy stats.
  4349. */
  4350. set_cpu_sd_state_busy();
  4351. nohz_balance_exit_idle(cpu);
  4352. /*
  4353. * None are in tickless mode and hence no need for NOHZ idle load
  4354. * balancing.
  4355. */
  4356. if (likely(!atomic_read(&nohz.nr_cpus)))
  4357. return 0;
  4358. if (time_before(now, nohz.next_balance))
  4359. return 0;
  4360. if (rq->nr_running >= 2)
  4361. goto need_kick;
  4362. rcu_read_lock();
  4363. for_each_domain(cpu, sd) {
  4364. struct sched_group *sg = sd->groups;
  4365. struct sched_group_power *sgp = sg->sgp;
  4366. int nr_busy = atomic_read(&sgp->nr_busy_cpus);
  4367. if (sd->flags & SD_SHARE_PKG_RESOURCES && nr_busy > 1)
  4368. goto need_kick_unlock;
  4369. if (sd->flags & SD_ASYM_PACKING && nr_busy != sg->group_weight
  4370. && (cpumask_first_and(nohz.idle_cpus_mask,
  4371. sched_domain_span(sd)) < cpu))
  4372. goto need_kick_unlock;
  4373. if (!(sd->flags & (SD_SHARE_PKG_RESOURCES | SD_ASYM_PACKING)))
  4374. break;
  4375. }
  4376. rcu_read_unlock();
  4377. return 0;
  4378. need_kick_unlock:
  4379. rcu_read_unlock();
  4380. need_kick:
  4381. return 1;
  4382. }
  4383. #else
  4384. static void nohz_idle_balance(int this_cpu, enum cpu_idle_type idle) { }
  4385. #endif
  4386. /*
  4387. * run_rebalance_domains is triggered when needed from the scheduler tick.
  4388. * Also triggered for nohz idle balancing (with nohz_balancing_kick set).
  4389. */
  4390. static void run_rebalance_domains(struct softirq_action *h)
  4391. {
  4392. int this_cpu = smp_processor_id();
  4393. struct rq *this_rq = cpu_rq(this_cpu);
  4394. enum cpu_idle_type idle = this_rq->idle_balance ?
  4395. CPU_IDLE : CPU_NOT_IDLE;
  4396. rebalance_domains(this_cpu, idle);
  4397. /*
  4398. * If this cpu has a pending nohz_balance_kick, then do the
  4399. * balancing on behalf of the other idle cpus whose ticks are
  4400. * stopped.
  4401. */
  4402. nohz_idle_balance(this_cpu, idle);
  4403. }
  4404. static inline int on_null_domain(int cpu)
  4405. {
  4406. return !rcu_dereference_sched(cpu_rq(cpu)->sd);
  4407. }
  4408. /*
  4409. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  4410. */
  4411. void trigger_load_balance(struct rq *rq, int cpu)
  4412. {
  4413. /* Don't need to rebalance while attached to NULL domain */
  4414. if (time_after_eq(jiffies, rq->next_balance) &&
  4415. likely(!on_null_domain(cpu)))
  4416. raise_softirq(SCHED_SOFTIRQ);
  4417. #ifdef CONFIG_NO_HZ
  4418. if (nohz_kick_needed(rq, cpu) && likely(!on_null_domain(cpu)))
  4419. nohz_balancer_kick(cpu);
  4420. #endif
  4421. }
  4422. static void rq_online_fair(struct rq *rq)
  4423. {
  4424. update_sysctl();
  4425. }
  4426. static void rq_offline_fair(struct rq *rq)
  4427. {
  4428. update_sysctl();
  4429. /* Ensure any throttled groups are reachable by pick_next_task */
  4430. unthrottle_offline_cfs_rqs(rq);
  4431. }
  4432. #endif /* CONFIG_SMP */
  4433. /*
  4434. * scheduler tick hitting a task of our scheduling class:
  4435. */
  4436. static void task_tick_fair(struct rq *rq, struct task_struct *curr, int queued)
  4437. {
  4438. struct cfs_rq *cfs_rq;
  4439. struct sched_entity *se = &curr->se;
  4440. for_each_sched_entity(se) {
  4441. cfs_rq = cfs_rq_of(se);
  4442. entity_tick(cfs_rq, se, queued);
  4443. }
  4444. update_rq_runnable_avg(rq, 1);
  4445. }
  4446. /*
  4447. * called on fork with the child task as argument from the parent's context
  4448. * - child not yet on the tasklist
  4449. * - preemption disabled
  4450. */
  4451. static void task_fork_fair(struct task_struct *p)
  4452. {
  4453. struct cfs_rq *cfs_rq;
  4454. struct sched_entity *se = &p->se, *curr;
  4455. int this_cpu = smp_processor_id();
  4456. struct rq *rq = this_rq();
  4457. unsigned long flags;
  4458. raw_spin_lock_irqsave(&rq->lock, flags);
  4459. update_rq_clock(rq);
  4460. cfs_rq = task_cfs_rq(current);
  4461. curr = cfs_rq->curr;
  4462. if (unlikely(task_cpu(p) != this_cpu)) {
  4463. rcu_read_lock();
  4464. __set_task_cpu(p, this_cpu);
  4465. rcu_read_unlock();
  4466. }
  4467. update_curr(cfs_rq);
  4468. if (curr)
  4469. se->vruntime = curr->vruntime;
  4470. place_entity(cfs_rq, se, 1);
  4471. if (sysctl_sched_child_runs_first && curr && entity_before(curr, se)) {
  4472. /*
  4473. * Upon rescheduling, sched_class::put_prev_task() will place
  4474. * 'current' within the tree based on its new key value.
  4475. */
  4476. swap(curr->vruntime, se->vruntime);
  4477. resched_task(rq->curr);
  4478. }
  4479. se->vruntime -= cfs_rq->min_vruntime;
  4480. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4481. }
  4482. /*
  4483. * Priority of the task has changed. Check to see if we preempt
  4484. * the current task.
  4485. */
  4486. static void
  4487. prio_changed_fair(struct rq *rq, struct task_struct *p, int oldprio)
  4488. {
  4489. if (!p->se.on_rq)
  4490. return;
  4491. /*
  4492. * Reschedule if we are currently running on this runqueue and
  4493. * our priority decreased, or if we are not currently running on
  4494. * this runqueue and our priority is higher than the current's
  4495. */
  4496. if (rq->curr == p) {
  4497. if (p->prio > oldprio)
  4498. resched_task(rq->curr);
  4499. } else
  4500. check_preempt_curr(rq, p, 0);
  4501. }
  4502. static void switched_from_fair(struct rq *rq, struct task_struct *p)
  4503. {
  4504. struct sched_entity *se = &p->se;
  4505. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  4506. /*
  4507. * Ensure the task's vruntime is normalized, so that when its
  4508. * switched back to the fair class the enqueue_entity(.flags=0) will
  4509. * do the right thing.
  4510. *
  4511. * If it was on_rq, then the dequeue_entity(.flags=0) will already
  4512. * have normalized the vruntime, if it was !on_rq, then only when
  4513. * the task is sleeping will it still have non-normalized vruntime.
  4514. */
  4515. if (!se->on_rq && p->state != TASK_RUNNING) {
  4516. /*
  4517. * Fix up our vruntime so that the current sleep doesn't
  4518. * cause 'unlimited' sleep bonus.
  4519. */
  4520. place_entity(cfs_rq, se, 0);
  4521. se->vruntime -= cfs_rq->min_vruntime;
  4522. }
  4523. #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
  4524. /*
  4525. * Remove our load from contribution when we leave sched_fair
  4526. * and ensure we don't carry in an old decay_count if we
  4527. * switch back.
  4528. */
  4529. if (p->se.avg.decay_count) {
  4530. struct cfs_rq *cfs_rq = cfs_rq_of(&p->se);
  4531. __synchronize_entity_decay(&p->se);
  4532. subtract_blocked_load_contrib(cfs_rq,
  4533. p->se.avg.load_avg_contrib);
  4534. }
  4535. #endif
  4536. }
  4537. /*
  4538. * We switched to the sched_fair class.
  4539. */
  4540. static void switched_to_fair(struct rq *rq, struct task_struct *p)
  4541. {
  4542. if (!p->se.on_rq)
  4543. return;
  4544. /*
  4545. * We were most likely switched from sched_rt, so
  4546. * kick off the schedule if running, otherwise just see
  4547. * if we can still preempt the current task.
  4548. */
  4549. if (rq->curr == p)
  4550. resched_task(rq->curr);
  4551. else
  4552. check_preempt_curr(rq, p, 0);
  4553. }
  4554. /* Account for a task changing its policy or group.
  4555. *
  4556. * This routine is mostly called to set cfs_rq->curr field when a task
  4557. * migrates between groups/classes.
  4558. */
  4559. static void set_curr_task_fair(struct rq *rq)
  4560. {
  4561. struct sched_entity *se = &rq->curr->se;
  4562. for_each_sched_entity(se) {
  4563. struct cfs_rq *cfs_rq = cfs_rq_of(se);
  4564. set_next_entity(cfs_rq, se);
  4565. /* ensure bandwidth has been allocated on our new cfs_rq */
  4566. account_cfs_rq_runtime(cfs_rq, 0);
  4567. }
  4568. }
  4569. void init_cfs_rq(struct cfs_rq *cfs_rq)
  4570. {
  4571. cfs_rq->tasks_timeline = RB_ROOT;
  4572. cfs_rq->min_vruntime = (u64)(-(1LL << 20));
  4573. #ifndef CONFIG_64BIT
  4574. cfs_rq->min_vruntime_copy = cfs_rq->min_vruntime;
  4575. #endif
  4576. #if defined(CONFIG_FAIR_GROUP_SCHED) && defined(CONFIG_SMP)
  4577. atomic64_set(&cfs_rq->decay_counter, 1);
  4578. atomic64_set(&cfs_rq->removed_load, 0);
  4579. #endif
  4580. }
  4581. #ifdef CONFIG_FAIR_GROUP_SCHED
  4582. static void task_move_group_fair(struct task_struct *p, int on_rq)
  4583. {
  4584. struct cfs_rq *cfs_rq;
  4585. /*
  4586. * If the task was not on the rq at the time of this cgroup movement
  4587. * it must have been asleep, sleeping tasks keep their ->vruntime
  4588. * absolute on their old rq until wakeup (needed for the fair sleeper
  4589. * bonus in place_entity()).
  4590. *
  4591. * If it was on the rq, we've just 'preempted' it, which does convert
  4592. * ->vruntime to a relative base.
  4593. *
  4594. * Make sure both cases convert their relative position when migrating
  4595. * to another cgroup's rq. This does somewhat interfere with the
  4596. * fair sleeper stuff for the first placement, but who cares.
  4597. */
  4598. /*
  4599. * When !on_rq, vruntime of the task has usually NOT been normalized.
  4600. * But there are some cases where it has already been normalized:
  4601. *
  4602. * - Moving a forked child which is waiting for being woken up by
  4603. * wake_up_new_task().
  4604. * - Moving a task which has been woken up by try_to_wake_up() and
  4605. * waiting for actually being woken up by sched_ttwu_pending().
  4606. *
  4607. * To prevent boost or penalty in the new cfs_rq caused by delta
  4608. * min_vruntime between the two cfs_rqs, we skip vruntime adjustment.
  4609. */
  4610. if (!on_rq && (!p->se.sum_exec_runtime || p->state == TASK_WAKING))
  4611. on_rq = 1;
  4612. if (!on_rq)
  4613. p->se.vruntime -= cfs_rq_of(&p->se)->min_vruntime;
  4614. set_task_rq(p, task_cpu(p));
  4615. if (!on_rq) {
  4616. cfs_rq = cfs_rq_of(&p->se);
  4617. p->se.vruntime += cfs_rq->min_vruntime;
  4618. #ifdef CONFIG_SMP
  4619. /*
  4620. * migrate_task_rq_fair() will have removed our previous
  4621. * contribution, but we must synchronize for ongoing future
  4622. * decay.
  4623. */
  4624. p->se.avg.decay_count = atomic64_read(&cfs_rq->decay_counter);
  4625. cfs_rq->blocked_load_avg += p->se.avg.load_avg_contrib;
  4626. #endif
  4627. }
  4628. }
  4629. void free_fair_sched_group(struct task_group *tg)
  4630. {
  4631. int i;
  4632. destroy_cfs_bandwidth(tg_cfs_bandwidth(tg));
  4633. for_each_possible_cpu(i) {
  4634. if (tg->cfs_rq)
  4635. kfree(tg->cfs_rq[i]);
  4636. if (tg->se)
  4637. kfree(tg->se[i]);
  4638. }
  4639. kfree(tg->cfs_rq);
  4640. kfree(tg->se);
  4641. }
  4642. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  4643. {
  4644. struct cfs_rq *cfs_rq;
  4645. struct sched_entity *se;
  4646. int i;
  4647. tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
  4648. if (!tg->cfs_rq)
  4649. goto err;
  4650. tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
  4651. if (!tg->se)
  4652. goto err;
  4653. tg->shares = NICE_0_LOAD;
  4654. init_cfs_bandwidth(tg_cfs_bandwidth(tg));
  4655. for_each_possible_cpu(i) {
  4656. cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
  4657. GFP_KERNEL, cpu_to_node(i));
  4658. if (!cfs_rq)
  4659. goto err;
  4660. se = kzalloc_node(sizeof(struct sched_entity),
  4661. GFP_KERNEL, cpu_to_node(i));
  4662. if (!se)
  4663. goto err_free_rq;
  4664. init_cfs_rq(cfs_rq);
  4665. init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]);
  4666. }
  4667. return 1;
  4668. err_free_rq:
  4669. kfree(cfs_rq);
  4670. err:
  4671. return 0;
  4672. }
  4673. void unregister_fair_sched_group(struct task_group *tg, int cpu)
  4674. {
  4675. struct rq *rq = cpu_rq(cpu);
  4676. unsigned long flags;
  4677. /*
  4678. * Only empty task groups can be destroyed; so we can speculatively
  4679. * check on_list without danger of it being re-added.
  4680. */
  4681. if (!tg->cfs_rq[cpu]->on_list)
  4682. return;
  4683. raw_spin_lock_irqsave(&rq->lock, flags);
  4684. list_del_leaf_cfs_rq(tg->cfs_rq[cpu]);
  4685. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4686. }
  4687. void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  4688. struct sched_entity *se, int cpu,
  4689. struct sched_entity *parent)
  4690. {
  4691. struct rq *rq = cpu_rq(cpu);
  4692. cfs_rq->tg = tg;
  4693. cfs_rq->rq = rq;
  4694. init_cfs_rq_runtime(cfs_rq);
  4695. tg->cfs_rq[cpu] = cfs_rq;
  4696. tg->se[cpu] = se;
  4697. /* se could be NULL for root_task_group */
  4698. if (!se)
  4699. return;
  4700. if (!parent)
  4701. se->cfs_rq = &rq->cfs;
  4702. else
  4703. se->cfs_rq = parent->my_q;
  4704. se->my_q = cfs_rq;
  4705. update_load_set(&se->load, 0);
  4706. se->parent = parent;
  4707. }
  4708. static DEFINE_MUTEX(shares_mutex);
  4709. int sched_group_set_shares(struct task_group *tg, unsigned long shares)
  4710. {
  4711. int i;
  4712. unsigned long flags;
  4713. /*
  4714. * We can't change the weight of the root cgroup.
  4715. */
  4716. if (!tg->se[0])
  4717. return -EINVAL;
  4718. shares = clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES));
  4719. mutex_lock(&shares_mutex);
  4720. if (tg->shares == shares)
  4721. goto done;
  4722. tg->shares = shares;
  4723. for_each_possible_cpu(i) {
  4724. struct rq *rq = cpu_rq(i);
  4725. struct sched_entity *se;
  4726. se = tg->se[i];
  4727. /* Propagate contribution to hierarchy */
  4728. raw_spin_lock_irqsave(&rq->lock, flags);
  4729. for_each_sched_entity(se)
  4730. update_cfs_shares(group_cfs_rq(se));
  4731. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4732. }
  4733. done:
  4734. mutex_unlock(&shares_mutex);
  4735. return 0;
  4736. }
  4737. #else /* CONFIG_FAIR_GROUP_SCHED */
  4738. void free_fair_sched_group(struct task_group *tg) { }
  4739. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  4740. {
  4741. return 1;
  4742. }
  4743. void unregister_fair_sched_group(struct task_group *tg, int cpu) { }
  4744. #endif /* CONFIG_FAIR_GROUP_SCHED */
  4745. static unsigned int get_rr_interval_fair(struct rq *rq, struct task_struct *task)
  4746. {
  4747. struct sched_entity *se = &task->se;
  4748. unsigned int rr_interval = 0;
  4749. /*
  4750. * Time slice is 0 for SCHED_OTHER tasks that are on an otherwise
  4751. * idle runqueue:
  4752. */
  4753. if (rq->cfs.load.weight)
  4754. rr_interval = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
  4755. return rr_interval;
  4756. }
  4757. /*
  4758. * All the scheduling class methods:
  4759. */
  4760. const struct sched_class fair_sched_class = {
  4761. .next = &idle_sched_class,
  4762. .enqueue_task = enqueue_task_fair,
  4763. .dequeue_task = dequeue_task_fair,
  4764. .yield_task = yield_task_fair,
  4765. .yield_to_task = yield_to_task_fair,
  4766. .check_preempt_curr = check_preempt_wakeup,
  4767. .pick_next_task = pick_next_task_fair,
  4768. .put_prev_task = put_prev_task_fair,
  4769. #ifdef CONFIG_SMP
  4770. .select_task_rq = select_task_rq_fair,
  4771. .migrate_task_rq = migrate_task_rq_fair,
  4772. .rq_online = rq_online_fair,
  4773. .rq_offline = rq_offline_fair,
  4774. .task_waking = task_waking_fair,
  4775. #endif
  4776. .set_curr_task = set_curr_task_fair,
  4777. .task_tick = task_tick_fair,
  4778. .task_fork = task_fork_fair,
  4779. .prio_changed = prio_changed_fair,
  4780. .switched_from = switched_from_fair,
  4781. .switched_to = switched_to_fair,
  4782. .get_rr_interval = get_rr_interval_fair,
  4783. #ifdef CONFIG_FAIR_GROUP_SCHED
  4784. .task_move_group = task_move_group_fair,
  4785. #endif
  4786. };
  4787. #ifdef CONFIG_SCHED_DEBUG
  4788. void print_cfs_stats(struct seq_file *m, int cpu)
  4789. {
  4790. struct cfs_rq *cfs_rq;
  4791. rcu_read_lock();
  4792. for_each_leaf_cfs_rq(cpu_rq(cpu), cfs_rq)
  4793. print_cfs_rq(m, cpu, cfs_rq);
  4794. rcu_read_unlock();
  4795. }
  4796. #endif
  4797. __init void init_sched_fair_class(void)
  4798. {
  4799. #ifdef CONFIG_SMP
  4800. open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
  4801. #ifdef CONFIG_NO_HZ
  4802. nohz.next_balance = jiffies;
  4803. zalloc_cpumask_var(&nohz.idle_cpus_mask, GFP_NOWAIT);
  4804. cpu_notifier(sched_ilb_notifier, 0);
  4805. #endif
  4806. #endif /* SMP */
  4807. }