fair.c 159 KB

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