fair.c 144 KB

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