fair.c 159 KB

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