fair.c 147 KB

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