fair.c 148 KB

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