fair.c 159 KB

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