fair.c 163 KB

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