fair.c 137 KB

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