sched.c 266 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866
  1. /*
  2. * kernel/sched.c
  3. *
  4. * Kernel scheduler and related syscalls
  5. *
  6. * Copyright (C) 1991-2002 Linus Torvalds
  7. *
  8. * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
  9. * make semaphores SMP safe
  10. * 1998-11-19 Implemented schedule_timeout() and related stuff
  11. * by Andrea Arcangeli
  12. * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
  13. * hybrid priority-list and round-robin design with
  14. * an array-switch method of distributing timeslices
  15. * and per-CPU runqueues. Cleanups and useful suggestions
  16. * by Davide Libenzi, preemptible kernel bits by Robert Love.
  17. * 2003-09-03 Interactivity tuning by Con Kolivas.
  18. * 2004-04-02 Scheduler domains code by Nick Piggin
  19. * 2007-04-15 Work begun on replacing all interactivity tuning with a
  20. * fair scheduling design by Con Kolivas.
  21. * 2007-05-05 Load balancing (smp-nice) and other improvements
  22. * by Peter Williams
  23. * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
  24. * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
  25. * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
  26. * Thomas Gleixner, Mike Kravetz
  27. */
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/nmi.h>
  31. #include <linux/init.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/highmem.h>
  34. #include <linux/smp_lock.h>
  35. #include <asm/mmu_context.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/capability.h>
  38. #include <linux/completion.h>
  39. #include <linux/kernel_stat.h>
  40. #include <linux/debug_locks.h>
  41. #include <linux/perf_counter.h>
  42. #include <linux/security.h>
  43. #include <linux/notifier.h>
  44. #include <linux/profile.h>
  45. #include <linux/freezer.h>
  46. #include <linux/vmalloc.h>
  47. #include <linux/blkdev.h>
  48. #include <linux/delay.h>
  49. #include <linux/pid_namespace.h>
  50. #include <linux/smp.h>
  51. #include <linux/threads.h>
  52. #include <linux/timer.h>
  53. #include <linux/rcupdate.h>
  54. #include <linux/cpu.h>
  55. #include <linux/cpuset.h>
  56. #include <linux/percpu.h>
  57. #include <linux/kthread.h>
  58. #include <linux/proc_fs.h>
  59. #include <linux/seq_file.h>
  60. #include <linux/sysctl.h>
  61. #include <linux/syscalls.h>
  62. #include <linux/times.h>
  63. #include <linux/tsacct_kern.h>
  64. #include <linux/kprobes.h>
  65. #include <linux/delayacct.h>
  66. #include <linux/unistd.h>
  67. #include <linux/pagemap.h>
  68. #include <linux/hrtimer.h>
  69. #include <linux/tick.h>
  70. #include <linux/debugfs.h>
  71. #include <linux/ctype.h>
  72. #include <linux/ftrace.h>
  73. #include <asm/tlb.h>
  74. #include <asm/irq_regs.h>
  75. #include "sched_cpupri.h"
  76. #define CREATE_TRACE_POINTS
  77. #include <trace/events/sched.h>
  78. /*
  79. * Convert user-nice values [ -20 ... 0 ... 19 ]
  80. * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  81. * and back.
  82. */
  83. #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  84. #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  85. #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  86. /*
  87. * 'User priority' is the nice value converted to something we
  88. * can work with better when scaling various scheduler parameters,
  89. * it's a [ 0 ... 39 ] range.
  90. */
  91. #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
  92. #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  93. #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  94. /*
  95. * Helpers for converting nanosecond timing to jiffy resolution
  96. */
  97. #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
  98. #define NICE_0_LOAD SCHED_LOAD_SCALE
  99. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  100. /*
  101. * These are the 'tuning knobs' of the scheduler:
  102. *
  103. * default timeslice is 100 msecs (used only for SCHED_RR tasks).
  104. * Timeslices get refilled after they expire.
  105. */
  106. #define DEF_TIMESLICE (100 * HZ / 1000)
  107. /*
  108. * single value that denotes runtime == period, ie unlimited time.
  109. */
  110. #define RUNTIME_INF ((u64)~0ULL)
  111. static inline int rt_policy(int policy)
  112. {
  113. if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
  114. return 1;
  115. return 0;
  116. }
  117. static inline int task_has_rt_policy(struct task_struct *p)
  118. {
  119. return rt_policy(p->policy);
  120. }
  121. /*
  122. * This is the priority-queue data structure of the RT scheduling class:
  123. */
  124. struct rt_prio_array {
  125. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  126. struct list_head queue[MAX_RT_PRIO];
  127. };
  128. struct rt_bandwidth {
  129. /* nests inside the rq lock: */
  130. spinlock_t rt_runtime_lock;
  131. ktime_t rt_period;
  132. u64 rt_runtime;
  133. struct hrtimer rt_period_timer;
  134. };
  135. static struct rt_bandwidth def_rt_bandwidth;
  136. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
  137. static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
  138. {
  139. struct rt_bandwidth *rt_b =
  140. container_of(timer, struct rt_bandwidth, rt_period_timer);
  141. ktime_t now;
  142. int overrun;
  143. int idle = 0;
  144. for (;;) {
  145. now = hrtimer_cb_get_time(timer);
  146. overrun = hrtimer_forward(timer, now, rt_b->rt_period);
  147. if (!overrun)
  148. break;
  149. idle = do_sched_rt_period_timer(rt_b, overrun);
  150. }
  151. return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
  152. }
  153. static
  154. void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
  155. {
  156. rt_b->rt_period = ns_to_ktime(period);
  157. rt_b->rt_runtime = runtime;
  158. spin_lock_init(&rt_b->rt_runtime_lock);
  159. hrtimer_init(&rt_b->rt_period_timer,
  160. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  161. rt_b->rt_period_timer.function = sched_rt_period_timer;
  162. }
  163. static inline int rt_bandwidth_enabled(void)
  164. {
  165. return sysctl_sched_rt_runtime >= 0;
  166. }
  167. static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
  168. {
  169. ktime_t now;
  170. if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
  171. return;
  172. if (hrtimer_active(&rt_b->rt_period_timer))
  173. return;
  174. spin_lock(&rt_b->rt_runtime_lock);
  175. for (;;) {
  176. unsigned long delta;
  177. ktime_t soft, hard;
  178. if (hrtimer_active(&rt_b->rt_period_timer))
  179. break;
  180. now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
  181. hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
  182. soft = hrtimer_get_softexpires(&rt_b->rt_period_timer);
  183. hard = hrtimer_get_expires(&rt_b->rt_period_timer);
  184. delta = ktime_to_ns(ktime_sub(hard, soft));
  185. __hrtimer_start_range_ns(&rt_b->rt_period_timer, soft, delta,
  186. HRTIMER_MODE_ABS_PINNED, 0);
  187. }
  188. spin_unlock(&rt_b->rt_runtime_lock);
  189. }
  190. #ifdef CONFIG_RT_GROUP_SCHED
  191. static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
  192. {
  193. hrtimer_cancel(&rt_b->rt_period_timer);
  194. }
  195. #endif
  196. /*
  197. * sched_domains_mutex serializes calls to arch_init_sched_domains,
  198. * detach_destroy_domains and partition_sched_domains.
  199. */
  200. static DEFINE_MUTEX(sched_domains_mutex);
  201. #ifdef CONFIG_GROUP_SCHED
  202. #include <linux/cgroup.h>
  203. struct cfs_rq;
  204. static LIST_HEAD(task_groups);
  205. /* task group related information */
  206. struct task_group {
  207. #ifdef CONFIG_CGROUP_SCHED
  208. struct cgroup_subsys_state css;
  209. #endif
  210. #ifdef CONFIG_USER_SCHED
  211. uid_t uid;
  212. #endif
  213. #ifdef CONFIG_FAIR_GROUP_SCHED
  214. /* schedulable entities of this group on each cpu */
  215. struct sched_entity **se;
  216. /* runqueue "owned" by this group on each cpu */
  217. struct cfs_rq **cfs_rq;
  218. unsigned long shares;
  219. #endif
  220. #ifdef CONFIG_RT_GROUP_SCHED
  221. struct sched_rt_entity **rt_se;
  222. struct rt_rq **rt_rq;
  223. struct rt_bandwidth rt_bandwidth;
  224. #endif
  225. struct rcu_head rcu;
  226. struct list_head list;
  227. struct task_group *parent;
  228. struct list_head siblings;
  229. struct list_head children;
  230. };
  231. #ifdef CONFIG_USER_SCHED
  232. /* Helper function to pass uid information to create_sched_user() */
  233. void set_tg_uid(struct user_struct *user)
  234. {
  235. user->tg->uid = user->uid;
  236. }
  237. /*
  238. * Root task group.
  239. * Every UID task group (including init_task_group aka UID-0) will
  240. * be a child to this group.
  241. */
  242. struct task_group root_task_group;
  243. #ifdef CONFIG_FAIR_GROUP_SCHED
  244. /* Default task group's sched entity on each cpu */
  245. static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
  246. /* Default task group's cfs_rq on each cpu */
  247. static DEFINE_PER_CPU(struct cfs_rq, init_tg_cfs_rq) ____cacheline_aligned_in_smp;
  248. #endif /* CONFIG_FAIR_GROUP_SCHED */
  249. #ifdef CONFIG_RT_GROUP_SCHED
  250. static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
  251. static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
  252. #endif /* CONFIG_RT_GROUP_SCHED */
  253. #else /* !CONFIG_USER_SCHED */
  254. #define root_task_group init_task_group
  255. #endif /* CONFIG_USER_SCHED */
  256. /* task_group_lock serializes add/remove of task groups and also changes to
  257. * a task group's cpu shares.
  258. */
  259. static DEFINE_SPINLOCK(task_group_lock);
  260. #ifdef CONFIG_SMP
  261. static int root_task_group_empty(void)
  262. {
  263. return list_empty(&root_task_group.children);
  264. }
  265. #endif
  266. #ifdef CONFIG_FAIR_GROUP_SCHED
  267. #ifdef CONFIG_USER_SCHED
  268. # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
  269. #else /* !CONFIG_USER_SCHED */
  270. # define INIT_TASK_GROUP_LOAD NICE_0_LOAD
  271. #endif /* CONFIG_USER_SCHED */
  272. /*
  273. * A weight of 0 or 1 can cause arithmetics problems.
  274. * A weight of a cfs_rq is the sum of weights of which entities
  275. * are queued on this cfs_rq, so a weight of a entity should not be
  276. * too large, so as the shares value of a task group.
  277. * (The default weight is 1024 - so there's no practical
  278. * limitation from this.)
  279. */
  280. #define MIN_SHARES 2
  281. #define MAX_SHARES (1UL << 18)
  282. static int init_task_group_load = INIT_TASK_GROUP_LOAD;
  283. #endif
  284. /* Default task group.
  285. * Every task in system belong to this group at bootup.
  286. */
  287. struct task_group init_task_group;
  288. /* return group to which a task belongs */
  289. static inline struct task_group *task_group(struct task_struct *p)
  290. {
  291. struct task_group *tg;
  292. #ifdef CONFIG_USER_SCHED
  293. rcu_read_lock();
  294. tg = __task_cred(p)->user->tg;
  295. rcu_read_unlock();
  296. #elif defined(CONFIG_CGROUP_SCHED)
  297. tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
  298. struct task_group, css);
  299. #else
  300. tg = &init_task_group;
  301. #endif
  302. return tg;
  303. }
  304. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  305. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  306. {
  307. #ifdef CONFIG_FAIR_GROUP_SCHED
  308. p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
  309. p->se.parent = task_group(p)->se[cpu];
  310. #endif
  311. #ifdef CONFIG_RT_GROUP_SCHED
  312. p->rt.rt_rq = task_group(p)->rt_rq[cpu];
  313. p->rt.parent = task_group(p)->rt_se[cpu];
  314. #endif
  315. }
  316. #else
  317. #ifdef CONFIG_SMP
  318. static int root_task_group_empty(void)
  319. {
  320. return 1;
  321. }
  322. #endif
  323. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  324. static inline struct task_group *task_group(struct task_struct *p)
  325. {
  326. return NULL;
  327. }
  328. #endif /* CONFIG_GROUP_SCHED */
  329. /* CFS-related fields in a runqueue */
  330. struct cfs_rq {
  331. struct load_weight load;
  332. unsigned long nr_running;
  333. u64 exec_clock;
  334. u64 min_vruntime;
  335. struct rb_root tasks_timeline;
  336. struct rb_node *rb_leftmost;
  337. struct list_head tasks;
  338. struct list_head *balance_iterator;
  339. /*
  340. * 'curr' points to currently running entity on this cfs_rq.
  341. * It is set to NULL otherwise (i.e when none are currently running).
  342. */
  343. struct sched_entity *curr, *next, *last;
  344. unsigned int nr_spread_over;
  345. #ifdef CONFIG_FAIR_GROUP_SCHED
  346. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  347. /*
  348. * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  349. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  350. * (like users, containers etc.)
  351. *
  352. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  353. * list is used during load balance.
  354. */
  355. struct list_head leaf_cfs_rq_list;
  356. struct task_group *tg; /* group that "owns" this runqueue */
  357. #ifdef CONFIG_SMP
  358. /*
  359. * the part of load.weight contributed by tasks
  360. */
  361. unsigned long task_weight;
  362. /*
  363. * h_load = weight * f(tg)
  364. *
  365. * Where f(tg) is the recursive weight fraction assigned to
  366. * this group.
  367. */
  368. unsigned long h_load;
  369. /*
  370. * this cpu's part of tg->shares
  371. */
  372. unsigned long shares;
  373. /*
  374. * load.weight at the time we set shares
  375. */
  376. unsigned long rq_weight;
  377. #endif
  378. #endif
  379. };
  380. /* Real-Time classes' related field in a runqueue: */
  381. struct rt_rq {
  382. struct rt_prio_array active;
  383. unsigned long rt_nr_running;
  384. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  385. struct {
  386. int curr; /* highest queued rt task prio */
  387. #ifdef CONFIG_SMP
  388. int next; /* next highest */
  389. #endif
  390. } highest_prio;
  391. #endif
  392. #ifdef CONFIG_SMP
  393. unsigned long rt_nr_migratory;
  394. unsigned long rt_nr_total;
  395. int overloaded;
  396. struct plist_head pushable_tasks;
  397. #endif
  398. int rt_throttled;
  399. u64 rt_time;
  400. u64 rt_runtime;
  401. /* Nests inside the rq lock: */
  402. spinlock_t rt_runtime_lock;
  403. #ifdef CONFIG_RT_GROUP_SCHED
  404. unsigned long rt_nr_boosted;
  405. struct rq *rq;
  406. struct list_head leaf_rt_rq_list;
  407. struct task_group *tg;
  408. struct sched_rt_entity *rt_se;
  409. #endif
  410. };
  411. #ifdef CONFIG_SMP
  412. /*
  413. * We add the notion of a root-domain which will be used to define per-domain
  414. * variables. Each exclusive cpuset essentially defines an island domain by
  415. * fully partitioning the member cpus from any other cpuset. Whenever a new
  416. * exclusive cpuset is created, we also create and attach a new root-domain
  417. * object.
  418. *
  419. */
  420. struct root_domain {
  421. atomic_t refcount;
  422. cpumask_var_t span;
  423. cpumask_var_t online;
  424. /*
  425. * The "RT overload" flag: it gets set if a CPU has more than
  426. * one runnable RT task.
  427. */
  428. cpumask_var_t rto_mask;
  429. atomic_t rto_count;
  430. #ifdef CONFIG_SMP
  431. struct cpupri cpupri;
  432. #endif
  433. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  434. /*
  435. * Preferred wake up cpu nominated by sched_mc balance that will be
  436. * used when most cpus are idle in the system indicating overall very
  437. * low system utilisation. Triggered at POWERSAVINGS_BALANCE_WAKEUP(2)
  438. */
  439. unsigned int sched_mc_preferred_wakeup_cpu;
  440. #endif
  441. };
  442. /*
  443. * By default the system creates a single root-domain with all cpus as
  444. * members (mimicking the global state we have today).
  445. */
  446. static struct root_domain def_root_domain;
  447. #endif
  448. /*
  449. * This is the main, per-CPU runqueue data structure.
  450. *
  451. * Locking rule: those places that want to lock multiple runqueues
  452. * (such as the load balancing or the thread migration code), lock
  453. * acquire operations must be ordered by ascending &runqueue.
  454. */
  455. struct rq {
  456. /* runqueue lock: */
  457. spinlock_t lock;
  458. /*
  459. * nr_running and cpu_load should be in the same cacheline because
  460. * remote CPUs use both these fields when doing load calculation.
  461. */
  462. unsigned long nr_running;
  463. #define CPU_LOAD_IDX_MAX 5
  464. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  465. #ifdef CONFIG_NO_HZ
  466. unsigned long last_tick_seen;
  467. unsigned char in_nohz_recently;
  468. #endif
  469. /* capture load from *all* tasks on this cpu: */
  470. struct load_weight load;
  471. unsigned long nr_load_updates;
  472. u64 nr_switches;
  473. u64 nr_migrations_in;
  474. struct cfs_rq cfs;
  475. struct rt_rq rt;
  476. #ifdef CONFIG_FAIR_GROUP_SCHED
  477. /* list of leaf cfs_rq on this cpu: */
  478. struct list_head leaf_cfs_rq_list;
  479. #endif
  480. #ifdef CONFIG_RT_GROUP_SCHED
  481. struct list_head leaf_rt_rq_list;
  482. #endif
  483. /*
  484. * This is part of a global counter where only the total sum
  485. * over all CPUs matters. A task can increase this counter on
  486. * one CPU and if it got migrated afterwards it may decrease
  487. * it on another CPU. Always updated under the runqueue lock:
  488. */
  489. unsigned long nr_uninterruptible;
  490. struct task_struct *curr, *idle;
  491. unsigned long next_balance;
  492. struct mm_struct *prev_mm;
  493. u64 clock;
  494. atomic_t nr_iowait;
  495. #ifdef CONFIG_SMP
  496. struct root_domain *rd;
  497. struct sched_domain *sd;
  498. unsigned char idle_at_tick;
  499. /* For active balancing */
  500. int post_schedule;
  501. int active_balance;
  502. int push_cpu;
  503. /* cpu of this runqueue: */
  504. int cpu;
  505. int online;
  506. unsigned long avg_load_per_task;
  507. struct task_struct *migration_thread;
  508. struct list_head migration_queue;
  509. u64 rt_avg;
  510. u64 age_stamp;
  511. #endif
  512. /* calc_load related fields */
  513. unsigned long calc_load_update;
  514. long calc_load_active;
  515. #ifdef CONFIG_SCHED_HRTICK
  516. #ifdef CONFIG_SMP
  517. int hrtick_csd_pending;
  518. struct call_single_data hrtick_csd;
  519. #endif
  520. struct hrtimer hrtick_timer;
  521. #endif
  522. #ifdef CONFIG_SCHEDSTATS
  523. /* latency stats */
  524. struct sched_info rq_sched_info;
  525. unsigned long long rq_cpu_time;
  526. /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
  527. /* sys_sched_yield() stats */
  528. unsigned int yld_count;
  529. /* schedule() stats */
  530. unsigned int sched_switch;
  531. unsigned int sched_count;
  532. unsigned int sched_goidle;
  533. /* try_to_wake_up() stats */
  534. unsigned int ttwu_count;
  535. unsigned int ttwu_local;
  536. /* BKL stats */
  537. unsigned int bkl_count;
  538. #endif
  539. };
  540. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  541. static inline void check_preempt_curr(struct rq *rq, struct task_struct *p, int sync)
  542. {
  543. rq->curr->sched_class->check_preempt_curr(rq, p, sync);
  544. }
  545. static inline int cpu_of(struct rq *rq)
  546. {
  547. #ifdef CONFIG_SMP
  548. return rq->cpu;
  549. #else
  550. return 0;
  551. #endif
  552. }
  553. /*
  554. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  555. * See detach_destroy_domains: synchronize_sched for details.
  556. *
  557. * The domain tree of any CPU may only be accessed from within
  558. * preempt-disabled sections.
  559. */
  560. #define for_each_domain(cpu, __sd) \
  561. for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
  562. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  563. #define this_rq() (&__get_cpu_var(runqueues))
  564. #define task_rq(p) cpu_rq(task_cpu(p))
  565. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  566. #define raw_rq() (&__raw_get_cpu_var(runqueues))
  567. inline void update_rq_clock(struct rq *rq)
  568. {
  569. rq->clock = sched_clock_cpu(cpu_of(rq));
  570. }
  571. /*
  572. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  573. */
  574. #ifdef CONFIG_SCHED_DEBUG
  575. # define const_debug __read_mostly
  576. #else
  577. # define const_debug static const
  578. #endif
  579. /**
  580. * runqueue_is_locked
  581. *
  582. * Returns true if the current cpu runqueue is locked.
  583. * This interface allows printk to be called with the runqueue lock
  584. * held and know whether or not it is OK to wake up the klogd.
  585. */
  586. int runqueue_is_locked(void)
  587. {
  588. int cpu = get_cpu();
  589. struct rq *rq = cpu_rq(cpu);
  590. int ret;
  591. ret = spin_is_locked(&rq->lock);
  592. put_cpu();
  593. return ret;
  594. }
  595. /*
  596. * Debugging: various feature bits
  597. */
  598. #define SCHED_FEAT(name, enabled) \
  599. __SCHED_FEAT_##name ,
  600. enum {
  601. #include "sched_features.h"
  602. };
  603. #undef SCHED_FEAT
  604. #define SCHED_FEAT(name, enabled) \
  605. (1UL << __SCHED_FEAT_##name) * enabled |
  606. const_debug unsigned int sysctl_sched_features =
  607. #include "sched_features.h"
  608. 0;
  609. #undef SCHED_FEAT
  610. #ifdef CONFIG_SCHED_DEBUG
  611. #define SCHED_FEAT(name, enabled) \
  612. #name ,
  613. static __read_mostly char *sched_feat_names[] = {
  614. #include "sched_features.h"
  615. NULL
  616. };
  617. #undef SCHED_FEAT
  618. static int sched_feat_show(struct seq_file *m, void *v)
  619. {
  620. int i;
  621. for (i = 0; sched_feat_names[i]; i++) {
  622. if (!(sysctl_sched_features & (1UL << i)))
  623. seq_puts(m, "NO_");
  624. seq_printf(m, "%s ", sched_feat_names[i]);
  625. }
  626. seq_puts(m, "\n");
  627. return 0;
  628. }
  629. static ssize_t
  630. sched_feat_write(struct file *filp, const char __user *ubuf,
  631. size_t cnt, loff_t *ppos)
  632. {
  633. char buf[64];
  634. char *cmp = buf;
  635. int neg = 0;
  636. int i;
  637. if (cnt > 63)
  638. cnt = 63;
  639. if (copy_from_user(&buf, ubuf, cnt))
  640. return -EFAULT;
  641. buf[cnt] = 0;
  642. if (strncmp(buf, "NO_", 3) == 0) {
  643. neg = 1;
  644. cmp += 3;
  645. }
  646. for (i = 0; sched_feat_names[i]; i++) {
  647. int len = strlen(sched_feat_names[i]);
  648. if (strncmp(cmp, sched_feat_names[i], len) == 0) {
  649. if (neg)
  650. sysctl_sched_features &= ~(1UL << i);
  651. else
  652. sysctl_sched_features |= (1UL << i);
  653. break;
  654. }
  655. }
  656. if (!sched_feat_names[i])
  657. return -EINVAL;
  658. filp->f_pos += cnt;
  659. return cnt;
  660. }
  661. static int sched_feat_open(struct inode *inode, struct file *filp)
  662. {
  663. return single_open(filp, sched_feat_show, NULL);
  664. }
  665. static struct file_operations sched_feat_fops = {
  666. .open = sched_feat_open,
  667. .write = sched_feat_write,
  668. .read = seq_read,
  669. .llseek = seq_lseek,
  670. .release = single_release,
  671. };
  672. static __init int sched_init_debug(void)
  673. {
  674. debugfs_create_file("sched_features", 0644, NULL, NULL,
  675. &sched_feat_fops);
  676. return 0;
  677. }
  678. late_initcall(sched_init_debug);
  679. #endif
  680. #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  681. /*
  682. * Number of tasks to iterate in a single balance run.
  683. * Limited because this is done with IRQs disabled.
  684. */
  685. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  686. /*
  687. * ratelimit for updating the group shares.
  688. * default: 0.25ms
  689. */
  690. unsigned int sysctl_sched_shares_ratelimit = 250000;
  691. /*
  692. * Inject some fuzzyness into changing the per-cpu group shares
  693. * this avoids remote rq-locks at the expense of fairness.
  694. * default: 4
  695. */
  696. unsigned int sysctl_sched_shares_thresh = 4;
  697. /*
  698. * period over which we average the RT time consumption, measured
  699. * in ms.
  700. *
  701. * default: 1s
  702. */
  703. const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
  704. /*
  705. * period over which we measure -rt task cpu usage in us.
  706. * default: 1s
  707. */
  708. unsigned int sysctl_sched_rt_period = 1000000;
  709. static __read_mostly int scheduler_running;
  710. /*
  711. * part of the period that we allow rt tasks to run in us.
  712. * default: 0.95s
  713. */
  714. int sysctl_sched_rt_runtime = 950000;
  715. static inline u64 global_rt_period(void)
  716. {
  717. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  718. }
  719. static inline u64 global_rt_runtime(void)
  720. {
  721. if (sysctl_sched_rt_runtime < 0)
  722. return RUNTIME_INF;
  723. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  724. }
  725. #ifndef prepare_arch_switch
  726. # define prepare_arch_switch(next) do { } while (0)
  727. #endif
  728. #ifndef finish_arch_switch
  729. # define finish_arch_switch(prev) do { } while (0)
  730. #endif
  731. static inline int task_current(struct rq *rq, struct task_struct *p)
  732. {
  733. return rq->curr == p;
  734. }
  735. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  736. static inline int task_running(struct rq *rq, struct task_struct *p)
  737. {
  738. return task_current(rq, p);
  739. }
  740. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  741. {
  742. }
  743. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  744. {
  745. #ifdef CONFIG_DEBUG_SPINLOCK
  746. /* this is a valid case when another task releases the spinlock */
  747. rq->lock.owner = current;
  748. #endif
  749. /*
  750. * If we are tracking spinlock dependencies then we have to
  751. * fix up the runqueue lock - which gets 'carried over' from
  752. * prev into current:
  753. */
  754. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  755. spin_unlock_irq(&rq->lock);
  756. }
  757. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  758. static inline int task_running(struct rq *rq, struct task_struct *p)
  759. {
  760. #ifdef CONFIG_SMP
  761. return p->oncpu;
  762. #else
  763. return task_current(rq, p);
  764. #endif
  765. }
  766. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  767. {
  768. #ifdef CONFIG_SMP
  769. /*
  770. * We can optimise this out completely for !SMP, because the
  771. * SMP rebalancing from interrupt is the only thing that cares
  772. * here.
  773. */
  774. next->oncpu = 1;
  775. #endif
  776. #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  777. spin_unlock_irq(&rq->lock);
  778. #else
  779. spin_unlock(&rq->lock);
  780. #endif
  781. }
  782. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  783. {
  784. #ifdef CONFIG_SMP
  785. /*
  786. * After ->oncpu is cleared, the task can be moved to a different CPU.
  787. * We must ensure this doesn't happen until the switch is completely
  788. * finished.
  789. */
  790. smp_wmb();
  791. prev->oncpu = 0;
  792. #endif
  793. #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  794. local_irq_enable();
  795. #endif
  796. }
  797. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  798. /*
  799. * __task_rq_lock - lock the runqueue a given task resides on.
  800. * Must be called interrupts disabled.
  801. */
  802. static inline struct rq *__task_rq_lock(struct task_struct *p)
  803. __acquires(rq->lock)
  804. {
  805. for (;;) {
  806. struct rq *rq = task_rq(p);
  807. spin_lock(&rq->lock);
  808. if (likely(rq == task_rq(p)))
  809. return rq;
  810. spin_unlock(&rq->lock);
  811. }
  812. }
  813. /*
  814. * task_rq_lock - lock the runqueue a given task resides on and disable
  815. * interrupts. Note the ordering: we can safely lookup the task_rq without
  816. * explicitly disabling preemption.
  817. */
  818. static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
  819. __acquires(rq->lock)
  820. {
  821. struct rq *rq;
  822. for (;;) {
  823. local_irq_save(*flags);
  824. rq = task_rq(p);
  825. spin_lock(&rq->lock);
  826. if (likely(rq == task_rq(p)))
  827. return rq;
  828. spin_unlock_irqrestore(&rq->lock, *flags);
  829. }
  830. }
  831. void task_rq_unlock_wait(struct task_struct *p)
  832. {
  833. struct rq *rq = task_rq(p);
  834. smp_mb(); /* spin-unlock-wait is not a full memory barrier */
  835. spin_unlock_wait(&rq->lock);
  836. }
  837. static void __task_rq_unlock(struct rq *rq)
  838. __releases(rq->lock)
  839. {
  840. spin_unlock(&rq->lock);
  841. }
  842. static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
  843. __releases(rq->lock)
  844. {
  845. spin_unlock_irqrestore(&rq->lock, *flags);
  846. }
  847. /*
  848. * this_rq_lock - lock this runqueue and disable interrupts.
  849. */
  850. static struct rq *this_rq_lock(void)
  851. __acquires(rq->lock)
  852. {
  853. struct rq *rq;
  854. local_irq_disable();
  855. rq = this_rq();
  856. spin_lock(&rq->lock);
  857. return rq;
  858. }
  859. #ifdef CONFIG_SCHED_HRTICK
  860. /*
  861. * Use HR-timers to deliver accurate preemption points.
  862. *
  863. * Its all a bit involved since we cannot program an hrt while holding the
  864. * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
  865. * reschedule event.
  866. *
  867. * When we get rescheduled we reprogram the hrtick_timer outside of the
  868. * rq->lock.
  869. */
  870. /*
  871. * Use hrtick when:
  872. * - enabled by features
  873. * - hrtimer is actually high res
  874. */
  875. static inline int hrtick_enabled(struct rq *rq)
  876. {
  877. if (!sched_feat(HRTICK))
  878. return 0;
  879. if (!cpu_active(cpu_of(rq)))
  880. return 0;
  881. return hrtimer_is_hres_active(&rq->hrtick_timer);
  882. }
  883. static void hrtick_clear(struct rq *rq)
  884. {
  885. if (hrtimer_active(&rq->hrtick_timer))
  886. hrtimer_cancel(&rq->hrtick_timer);
  887. }
  888. /*
  889. * High-resolution timer tick.
  890. * Runs from hardirq context with interrupts disabled.
  891. */
  892. static enum hrtimer_restart hrtick(struct hrtimer *timer)
  893. {
  894. struct rq *rq = container_of(timer, struct rq, hrtick_timer);
  895. WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
  896. spin_lock(&rq->lock);
  897. update_rq_clock(rq);
  898. rq->curr->sched_class->task_tick(rq, rq->curr, 1);
  899. spin_unlock(&rq->lock);
  900. return HRTIMER_NORESTART;
  901. }
  902. #ifdef CONFIG_SMP
  903. /*
  904. * called from hardirq (IPI) context
  905. */
  906. static void __hrtick_start(void *arg)
  907. {
  908. struct rq *rq = arg;
  909. spin_lock(&rq->lock);
  910. hrtimer_restart(&rq->hrtick_timer);
  911. rq->hrtick_csd_pending = 0;
  912. spin_unlock(&rq->lock);
  913. }
  914. /*
  915. * Called to set the hrtick timer state.
  916. *
  917. * called with rq->lock held and irqs disabled
  918. */
  919. static void hrtick_start(struct rq *rq, u64 delay)
  920. {
  921. struct hrtimer *timer = &rq->hrtick_timer;
  922. ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
  923. hrtimer_set_expires(timer, time);
  924. if (rq == this_rq()) {
  925. hrtimer_restart(timer);
  926. } else if (!rq->hrtick_csd_pending) {
  927. __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
  928. rq->hrtick_csd_pending = 1;
  929. }
  930. }
  931. static int
  932. hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
  933. {
  934. int cpu = (int)(long)hcpu;
  935. switch (action) {
  936. case CPU_UP_CANCELED:
  937. case CPU_UP_CANCELED_FROZEN:
  938. case CPU_DOWN_PREPARE:
  939. case CPU_DOWN_PREPARE_FROZEN:
  940. case CPU_DEAD:
  941. case CPU_DEAD_FROZEN:
  942. hrtick_clear(cpu_rq(cpu));
  943. return NOTIFY_OK;
  944. }
  945. return NOTIFY_DONE;
  946. }
  947. static __init void init_hrtick(void)
  948. {
  949. hotcpu_notifier(hotplug_hrtick, 0);
  950. }
  951. #else
  952. /*
  953. * Called to set the hrtick timer state.
  954. *
  955. * called with rq->lock held and irqs disabled
  956. */
  957. static void hrtick_start(struct rq *rq, u64 delay)
  958. {
  959. __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
  960. HRTIMER_MODE_REL_PINNED, 0);
  961. }
  962. static inline void init_hrtick(void)
  963. {
  964. }
  965. #endif /* CONFIG_SMP */
  966. static void init_rq_hrtick(struct rq *rq)
  967. {
  968. #ifdef CONFIG_SMP
  969. rq->hrtick_csd_pending = 0;
  970. rq->hrtick_csd.flags = 0;
  971. rq->hrtick_csd.func = __hrtick_start;
  972. rq->hrtick_csd.info = rq;
  973. #endif
  974. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  975. rq->hrtick_timer.function = hrtick;
  976. }
  977. #else /* CONFIG_SCHED_HRTICK */
  978. static inline void hrtick_clear(struct rq *rq)
  979. {
  980. }
  981. static inline void init_rq_hrtick(struct rq *rq)
  982. {
  983. }
  984. static inline void init_hrtick(void)
  985. {
  986. }
  987. #endif /* CONFIG_SCHED_HRTICK */
  988. /*
  989. * resched_task - mark a task 'to be rescheduled now'.
  990. *
  991. * On UP this means the setting of the need_resched flag, on SMP it
  992. * might also involve a cross-CPU call to trigger the scheduler on
  993. * the target CPU.
  994. */
  995. #ifdef CONFIG_SMP
  996. #ifndef tsk_is_polling
  997. #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
  998. #endif
  999. static void resched_task(struct task_struct *p)
  1000. {
  1001. int cpu;
  1002. assert_spin_locked(&task_rq(p)->lock);
  1003. if (test_tsk_need_resched(p))
  1004. return;
  1005. set_tsk_need_resched(p);
  1006. cpu = task_cpu(p);
  1007. if (cpu == smp_processor_id())
  1008. return;
  1009. /* NEED_RESCHED must be visible before we test polling */
  1010. smp_mb();
  1011. if (!tsk_is_polling(p))
  1012. smp_send_reschedule(cpu);
  1013. }
  1014. static void resched_cpu(int cpu)
  1015. {
  1016. struct rq *rq = cpu_rq(cpu);
  1017. unsigned long flags;
  1018. if (!spin_trylock_irqsave(&rq->lock, flags))
  1019. return;
  1020. resched_task(cpu_curr(cpu));
  1021. spin_unlock_irqrestore(&rq->lock, flags);
  1022. }
  1023. #ifdef CONFIG_NO_HZ
  1024. /*
  1025. * When add_timer_on() enqueues a timer into the timer wheel of an
  1026. * idle CPU then this timer might expire before the next timer event
  1027. * which is scheduled to wake up that CPU. In case of a completely
  1028. * idle system the next event might even be infinite time into the
  1029. * future. wake_up_idle_cpu() ensures that the CPU is woken up and
  1030. * leaves the inner idle loop so the newly added timer is taken into
  1031. * account when the CPU goes back to idle and evaluates the timer
  1032. * wheel for the next timer event.
  1033. */
  1034. void wake_up_idle_cpu(int cpu)
  1035. {
  1036. struct rq *rq = cpu_rq(cpu);
  1037. if (cpu == smp_processor_id())
  1038. return;
  1039. /*
  1040. * This is safe, as this function is called with the timer
  1041. * wheel base lock of (cpu) held. When the CPU is on the way
  1042. * to idle and has not yet set rq->curr to idle then it will
  1043. * be serialized on the timer wheel base lock and take the new
  1044. * timer into account automatically.
  1045. */
  1046. if (rq->curr != rq->idle)
  1047. return;
  1048. /*
  1049. * We can set TIF_RESCHED on the idle task of the other CPU
  1050. * lockless. The worst case is that the other CPU runs the
  1051. * idle task through an additional NOOP schedule()
  1052. */
  1053. set_tsk_need_resched(rq->idle);
  1054. /* NEED_RESCHED must be visible before we test polling */
  1055. smp_mb();
  1056. if (!tsk_is_polling(rq->idle))
  1057. smp_send_reschedule(cpu);
  1058. }
  1059. #endif /* CONFIG_NO_HZ */
  1060. static u64 sched_avg_period(void)
  1061. {
  1062. return (u64)sysctl_sched_time_avg * NSEC_PER_MSEC / 2;
  1063. }
  1064. static void sched_avg_update(struct rq *rq)
  1065. {
  1066. s64 period = sched_avg_period();
  1067. while ((s64)(rq->clock - rq->age_stamp) > period) {
  1068. rq->age_stamp += period;
  1069. rq->rt_avg /= 2;
  1070. }
  1071. }
  1072. static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
  1073. {
  1074. rq->rt_avg += rt_delta;
  1075. sched_avg_update(rq);
  1076. }
  1077. #else /* !CONFIG_SMP */
  1078. static void resched_task(struct task_struct *p)
  1079. {
  1080. assert_spin_locked(&task_rq(p)->lock);
  1081. set_tsk_need_resched(p);
  1082. }
  1083. static void sched_rt_avg_update(struct rq *rq, u64 rt_delta)
  1084. {
  1085. }
  1086. #endif /* CONFIG_SMP */
  1087. #if BITS_PER_LONG == 32
  1088. # define WMULT_CONST (~0UL)
  1089. #else
  1090. # define WMULT_CONST (1UL << 32)
  1091. #endif
  1092. #define WMULT_SHIFT 32
  1093. /*
  1094. * Shift right and round:
  1095. */
  1096. #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
  1097. /*
  1098. * delta *= weight / lw
  1099. */
  1100. static unsigned long
  1101. calc_delta_mine(unsigned long delta_exec, unsigned long weight,
  1102. struct load_weight *lw)
  1103. {
  1104. u64 tmp;
  1105. if (!lw->inv_weight) {
  1106. if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
  1107. lw->inv_weight = 1;
  1108. else
  1109. lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
  1110. / (lw->weight+1);
  1111. }
  1112. tmp = (u64)delta_exec * weight;
  1113. /*
  1114. * Check whether we'd overflow the 64-bit multiplication:
  1115. */
  1116. if (unlikely(tmp > WMULT_CONST))
  1117. tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
  1118. WMULT_SHIFT/2);
  1119. else
  1120. tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
  1121. return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
  1122. }
  1123. static inline void update_load_add(struct load_weight *lw, unsigned long inc)
  1124. {
  1125. lw->weight += inc;
  1126. lw->inv_weight = 0;
  1127. }
  1128. static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
  1129. {
  1130. lw->weight -= dec;
  1131. lw->inv_weight = 0;
  1132. }
  1133. /*
  1134. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  1135. * of tasks with abnormal "nice" values across CPUs the contribution that
  1136. * each task makes to its run queue's load is weighted according to its
  1137. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  1138. * scaled version of the new time slice allocation that they receive on time
  1139. * slice expiry etc.
  1140. */
  1141. #define WEIGHT_IDLEPRIO 3
  1142. #define WMULT_IDLEPRIO 1431655765
  1143. /*
  1144. * Nice levels are multiplicative, with a gentle 10% change for every
  1145. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  1146. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  1147. * that remained on nice 0.
  1148. *
  1149. * The "10% effect" is relative and cumulative: from _any_ nice level,
  1150. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  1151. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  1152. * If a task goes up by ~10% and another task goes down by ~10% then
  1153. * the relative distance between them is ~25%.)
  1154. */
  1155. static const int prio_to_weight[40] = {
  1156. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  1157. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  1158. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  1159. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  1160. /* 0 */ 1024, 820, 655, 526, 423,
  1161. /* 5 */ 335, 272, 215, 172, 137,
  1162. /* 10 */ 110, 87, 70, 56, 45,
  1163. /* 15 */ 36, 29, 23, 18, 15,
  1164. };
  1165. /*
  1166. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  1167. *
  1168. * In cases where the weight does not change often, we can use the
  1169. * precalculated inverse to speed up arithmetics by turning divisions
  1170. * into multiplications:
  1171. */
  1172. static const u32 prio_to_wmult[40] = {
  1173. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  1174. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  1175. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  1176. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  1177. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  1178. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  1179. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  1180. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  1181. };
  1182. static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
  1183. /*
  1184. * runqueue iterator, to support SMP load-balancing between different
  1185. * scheduling classes, without having to expose their internal data
  1186. * structures to the load-balancing proper:
  1187. */
  1188. struct rq_iterator {
  1189. void *arg;
  1190. struct task_struct *(*start)(void *);
  1191. struct task_struct *(*next)(void *);
  1192. };
  1193. #ifdef CONFIG_SMP
  1194. static unsigned long
  1195. balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1196. unsigned long max_load_move, struct sched_domain *sd,
  1197. enum cpu_idle_type idle, int *all_pinned,
  1198. int *this_best_prio, struct rq_iterator *iterator);
  1199. static int
  1200. iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1201. struct sched_domain *sd, enum cpu_idle_type idle,
  1202. struct rq_iterator *iterator);
  1203. #endif
  1204. /* Time spent by the tasks of the cpu accounting group executing in ... */
  1205. enum cpuacct_stat_index {
  1206. CPUACCT_STAT_USER, /* ... user mode */
  1207. CPUACCT_STAT_SYSTEM, /* ... kernel mode */
  1208. CPUACCT_STAT_NSTATS,
  1209. };
  1210. #ifdef CONFIG_CGROUP_CPUACCT
  1211. static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
  1212. static void cpuacct_update_stats(struct task_struct *tsk,
  1213. enum cpuacct_stat_index idx, cputime_t val);
  1214. #else
  1215. static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
  1216. static inline void cpuacct_update_stats(struct task_struct *tsk,
  1217. enum cpuacct_stat_index idx, cputime_t val) {}
  1218. #endif
  1219. static inline void inc_cpu_load(struct rq *rq, unsigned long load)
  1220. {
  1221. update_load_add(&rq->load, load);
  1222. }
  1223. static inline void dec_cpu_load(struct rq *rq, unsigned long load)
  1224. {
  1225. update_load_sub(&rq->load, load);
  1226. }
  1227. #if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
  1228. typedef int (*tg_visitor)(struct task_group *, void *);
  1229. /*
  1230. * Iterate the full tree, calling @down when first entering a node and @up when
  1231. * leaving it for the final time.
  1232. */
  1233. static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
  1234. {
  1235. struct task_group *parent, *child;
  1236. int ret;
  1237. rcu_read_lock();
  1238. parent = &root_task_group;
  1239. down:
  1240. ret = (*down)(parent, data);
  1241. if (ret)
  1242. goto out_unlock;
  1243. list_for_each_entry_rcu(child, &parent->children, siblings) {
  1244. parent = child;
  1245. goto down;
  1246. up:
  1247. continue;
  1248. }
  1249. ret = (*up)(parent, data);
  1250. if (ret)
  1251. goto out_unlock;
  1252. child = parent;
  1253. parent = parent->parent;
  1254. if (parent)
  1255. goto up;
  1256. out_unlock:
  1257. rcu_read_unlock();
  1258. return ret;
  1259. }
  1260. static int tg_nop(struct task_group *tg, void *data)
  1261. {
  1262. return 0;
  1263. }
  1264. #endif
  1265. #ifdef CONFIG_SMP
  1266. /* Used instead of source_load when we know the type == 0 */
  1267. static unsigned long weighted_cpuload(const int cpu)
  1268. {
  1269. return cpu_rq(cpu)->load.weight;
  1270. }
  1271. /*
  1272. * Return a low guess at the load of a migration-source cpu weighted
  1273. * according to the scheduling class and "nice" value.
  1274. *
  1275. * We want to under-estimate the load of migration sources, to
  1276. * balance conservatively.
  1277. */
  1278. static unsigned long source_load(int cpu, int type)
  1279. {
  1280. struct rq *rq = cpu_rq(cpu);
  1281. unsigned long total = weighted_cpuload(cpu);
  1282. if (type == 0 || !sched_feat(LB_BIAS))
  1283. return total;
  1284. return min(rq->cpu_load[type-1], total);
  1285. }
  1286. /*
  1287. * Return a high guess at the load of a migration-target cpu weighted
  1288. * according to the scheduling class and "nice" value.
  1289. */
  1290. static unsigned long target_load(int cpu, int type)
  1291. {
  1292. struct rq *rq = cpu_rq(cpu);
  1293. unsigned long total = weighted_cpuload(cpu);
  1294. if (type == 0 || !sched_feat(LB_BIAS))
  1295. return total;
  1296. return max(rq->cpu_load[type-1], total);
  1297. }
  1298. static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
  1299. static unsigned long cpu_avg_load_per_task(int cpu)
  1300. {
  1301. struct rq *rq = cpu_rq(cpu);
  1302. unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
  1303. if (nr_running)
  1304. rq->avg_load_per_task = rq->load.weight / nr_running;
  1305. else
  1306. rq->avg_load_per_task = 0;
  1307. return rq->avg_load_per_task;
  1308. }
  1309. #ifdef CONFIG_FAIR_GROUP_SCHED
  1310. struct update_shares_data {
  1311. unsigned long rq_weight[NR_CPUS];
  1312. };
  1313. static DEFINE_PER_CPU(struct update_shares_data, update_shares_data);
  1314. static void __set_se_shares(struct sched_entity *se, unsigned long shares);
  1315. /*
  1316. * Calculate and set the cpu's group shares.
  1317. */
  1318. static void update_group_shares_cpu(struct task_group *tg, int cpu,
  1319. unsigned long sd_shares,
  1320. unsigned long sd_rq_weight,
  1321. struct update_shares_data *usd)
  1322. {
  1323. unsigned long shares, rq_weight;
  1324. int boost = 0;
  1325. rq_weight = usd->rq_weight[cpu];
  1326. if (!rq_weight) {
  1327. boost = 1;
  1328. rq_weight = NICE_0_LOAD;
  1329. }
  1330. /*
  1331. * \Sum_j shares_j * rq_weight_i
  1332. * shares_i = -----------------------------
  1333. * \Sum_j rq_weight_j
  1334. */
  1335. shares = (sd_shares * rq_weight) / sd_rq_weight;
  1336. shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
  1337. if (abs(shares - tg->se[cpu]->load.weight) >
  1338. sysctl_sched_shares_thresh) {
  1339. struct rq *rq = cpu_rq(cpu);
  1340. unsigned long flags;
  1341. spin_lock_irqsave(&rq->lock, flags);
  1342. tg->cfs_rq[cpu]->rq_weight = boost ? 0 : rq_weight;
  1343. tg->cfs_rq[cpu]->shares = boost ? 0 : shares;
  1344. __set_se_shares(tg->se[cpu], shares);
  1345. spin_unlock_irqrestore(&rq->lock, flags);
  1346. }
  1347. }
  1348. /*
  1349. * Re-compute the task group their per cpu shares over the given domain.
  1350. * This needs to be done in a bottom-up fashion because the rq weight of a
  1351. * parent group depends on the shares of its child groups.
  1352. */
  1353. static int tg_shares_up(struct task_group *tg, void *data)
  1354. {
  1355. unsigned long weight, rq_weight = 0, shares = 0;
  1356. struct update_shares_data *usd;
  1357. struct sched_domain *sd = data;
  1358. unsigned long flags;
  1359. int i;
  1360. if (!tg->se[0])
  1361. return 0;
  1362. local_irq_save(flags);
  1363. usd = &__get_cpu_var(update_shares_data);
  1364. for_each_cpu(i, sched_domain_span(sd)) {
  1365. weight = tg->cfs_rq[i]->load.weight;
  1366. usd->rq_weight[i] = weight;
  1367. /*
  1368. * If there are currently no tasks on the cpu pretend there
  1369. * is one of average load so that when a new task gets to
  1370. * run here it will not get delayed by group starvation.
  1371. */
  1372. if (!weight)
  1373. weight = NICE_0_LOAD;
  1374. rq_weight += weight;
  1375. shares += tg->cfs_rq[i]->shares;
  1376. }
  1377. if ((!shares && rq_weight) || shares > tg->shares)
  1378. shares = tg->shares;
  1379. if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
  1380. shares = tg->shares;
  1381. for_each_cpu(i, sched_domain_span(sd))
  1382. update_group_shares_cpu(tg, i, shares, rq_weight, usd);
  1383. local_irq_restore(flags);
  1384. return 0;
  1385. }
  1386. /*
  1387. * Compute the cpu's hierarchical load factor for each task group.
  1388. * This needs to be done in a top-down fashion because the load of a child
  1389. * group is a fraction of its parents load.
  1390. */
  1391. static int tg_load_down(struct task_group *tg, void *data)
  1392. {
  1393. unsigned long load;
  1394. long cpu = (long)data;
  1395. if (!tg->parent) {
  1396. load = cpu_rq(cpu)->load.weight;
  1397. } else {
  1398. load = tg->parent->cfs_rq[cpu]->h_load;
  1399. load *= tg->cfs_rq[cpu]->shares;
  1400. load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
  1401. }
  1402. tg->cfs_rq[cpu]->h_load = load;
  1403. return 0;
  1404. }
  1405. static void update_shares(struct sched_domain *sd)
  1406. {
  1407. s64 elapsed;
  1408. u64 now;
  1409. if (root_task_group_empty())
  1410. return;
  1411. now = cpu_clock(raw_smp_processor_id());
  1412. elapsed = now - sd->last_update;
  1413. if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
  1414. sd->last_update = now;
  1415. walk_tg_tree(tg_nop, tg_shares_up, sd);
  1416. }
  1417. }
  1418. static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
  1419. {
  1420. if (root_task_group_empty())
  1421. return;
  1422. spin_unlock(&rq->lock);
  1423. update_shares(sd);
  1424. spin_lock(&rq->lock);
  1425. }
  1426. static void update_h_load(long cpu)
  1427. {
  1428. if (root_task_group_empty())
  1429. return;
  1430. walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
  1431. }
  1432. #else
  1433. static inline void update_shares(struct sched_domain *sd)
  1434. {
  1435. }
  1436. static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
  1437. {
  1438. }
  1439. #endif
  1440. #ifdef CONFIG_PREEMPT
  1441. static void double_rq_lock(struct rq *rq1, struct rq *rq2);
  1442. /*
  1443. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  1444. * way at the expense of forcing extra atomic operations in all
  1445. * invocations. This assures that the double_lock is acquired using the
  1446. * same underlying policy as the spinlock_t on this architecture, which
  1447. * reduces latency compared to the unfair variant below. However, it
  1448. * also adds more overhead and therefore may reduce throughput.
  1449. */
  1450. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1451. __releases(this_rq->lock)
  1452. __acquires(busiest->lock)
  1453. __acquires(this_rq->lock)
  1454. {
  1455. spin_unlock(&this_rq->lock);
  1456. double_rq_lock(this_rq, busiest);
  1457. return 1;
  1458. }
  1459. #else
  1460. /*
  1461. * Unfair double_lock_balance: Optimizes throughput at the expense of
  1462. * latency by eliminating extra atomic operations when the locks are
  1463. * already in proper order on entry. This favors lower cpu-ids and will
  1464. * grant the double lock to lower cpus over higher ids under contention,
  1465. * regardless of entry order into the function.
  1466. */
  1467. static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1468. __releases(this_rq->lock)
  1469. __acquires(busiest->lock)
  1470. __acquires(this_rq->lock)
  1471. {
  1472. int ret = 0;
  1473. if (unlikely(!spin_trylock(&busiest->lock))) {
  1474. if (busiest < this_rq) {
  1475. spin_unlock(&this_rq->lock);
  1476. spin_lock(&busiest->lock);
  1477. spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
  1478. ret = 1;
  1479. } else
  1480. spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
  1481. }
  1482. return ret;
  1483. }
  1484. #endif /* CONFIG_PREEMPT */
  1485. /*
  1486. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1487. */
  1488. static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1489. {
  1490. if (unlikely(!irqs_disabled())) {
  1491. /* printk() doesn't work good under rq->lock */
  1492. spin_unlock(&this_rq->lock);
  1493. BUG_ON(1);
  1494. }
  1495. return _double_lock_balance(this_rq, busiest);
  1496. }
  1497. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  1498. __releases(busiest->lock)
  1499. {
  1500. spin_unlock(&busiest->lock);
  1501. lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
  1502. }
  1503. #endif
  1504. #ifdef CONFIG_FAIR_GROUP_SCHED
  1505. static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
  1506. {
  1507. #ifdef CONFIG_SMP
  1508. cfs_rq->shares = shares;
  1509. #endif
  1510. }
  1511. #endif
  1512. static void calc_load_account_active(struct rq *this_rq);
  1513. #include "sched_stats.h"
  1514. #include "sched_idletask.c"
  1515. #include "sched_fair.c"
  1516. #include "sched_rt.c"
  1517. #ifdef CONFIG_SCHED_DEBUG
  1518. # include "sched_debug.c"
  1519. #endif
  1520. #define sched_class_highest (&rt_sched_class)
  1521. #define for_each_class(class) \
  1522. for (class = sched_class_highest; class; class = class->next)
  1523. static void inc_nr_running(struct rq *rq)
  1524. {
  1525. rq->nr_running++;
  1526. }
  1527. static void dec_nr_running(struct rq *rq)
  1528. {
  1529. rq->nr_running--;
  1530. }
  1531. static void set_load_weight(struct task_struct *p)
  1532. {
  1533. if (task_has_rt_policy(p)) {
  1534. p->se.load.weight = prio_to_weight[0] * 2;
  1535. p->se.load.inv_weight = prio_to_wmult[0] >> 1;
  1536. return;
  1537. }
  1538. /*
  1539. * SCHED_IDLE tasks get minimal weight:
  1540. */
  1541. if (p->policy == SCHED_IDLE) {
  1542. p->se.load.weight = WEIGHT_IDLEPRIO;
  1543. p->se.load.inv_weight = WMULT_IDLEPRIO;
  1544. return;
  1545. }
  1546. p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
  1547. p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
  1548. }
  1549. static void update_avg(u64 *avg, u64 sample)
  1550. {
  1551. s64 diff = sample - *avg;
  1552. *avg += diff >> 3;
  1553. }
  1554. static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
  1555. {
  1556. if (wakeup)
  1557. p->se.start_runtime = p->se.sum_exec_runtime;
  1558. sched_info_queued(p);
  1559. p->sched_class->enqueue_task(rq, p, wakeup);
  1560. p->se.on_rq = 1;
  1561. }
  1562. static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
  1563. {
  1564. if (sleep) {
  1565. if (p->se.last_wakeup) {
  1566. update_avg(&p->se.avg_overlap,
  1567. p->se.sum_exec_runtime - p->se.last_wakeup);
  1568. p->se.last_wakeup = 0;
  1569. } else {
  1570. update_avg(&p->se.avg_wakeup,
  1571. sysctl_sched_wakeup_granularity);
  1572. }
  1573. }
  1574. sched_info_dequeued(p);
  1575. p->sched_class->dequeue_task(rq, p, sleep);
  1576. p->se.on_rq = 0;
  1577. }
  1578. /*
  1579. * __normal_prio - return the priority that is based on the static prio
  1580. */
  1581. static inline int __normal_prio(struct task_struct *p)
  1582. {
  1583. return p->static_prio;
  1584. }
  1585. /*
  1586. * Calculate the expected normal priority: i.e. priority
  1587. * without taking RT-inheritance into account. Might be
  1588. * boosted by interactivity modifiers. Changes upon fork,
  1589. * setprio syscalls, and whenever the interactivity
  1590. * estimator recalculates.
  1591. */
  1592. static inline int normal_prio(struct task_struct *p)
  1593. {
  1594. int prio;
  1595. if (task_has_rt_policy(p))
  1596. prio = MAX_RT_PRIO-1 - p->rt_priority;
  1597. else
  1598. prio = __normal_prio(p);
  1599. return prio;
  1600. }
  1601. /*
  1602. * Calculate the current priority, i.e. the priority
  1603. * taken into account by the scheduler. This value might
  1604. * be boosted by RT tasks, or might be boosted by
  1605. * interactivity modifiers. Will be RT if the task got
  1606. * RT-boosted. If not then it returns p->normal_prio.
  1607. */
  1608. static int effective_prio(struct task_struct *p)
  1609. {
  1610. p->normal_prio = normal_prio(p);
  1611. /*
  1612. * If we are RT tasks or we were boosted to RT priority,
  1613. * keep the priority unchanged. Otherwise, update priority
  1614. * to the normal priority:
  1615. */
  1616. if (!rt_prio(p->prio))
  1617. return p->normal_prio;
  1618. return p->prio;
  1619. }
  1620. /*
  1621. * activate_task - move a task to the runqueue.
  1622. */
  1623. static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
  1624. {
  1625. if (task_contributes_to_load(p))
  1626. rq->nr_uninterruptible--;
  1627. enqueue_task(rq, p, wakeup);
  1628. inc_nr_running(rq);
  1629. }
  1630. /*
  1631. * deactivate_task - remove a task from the runqueue.
  1632. */
  1633. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
  1634. {
  1635. if (task_contributes_to_load(p))
  1636. rq->nr_uninterruptible++;
  1637. dequeue_task(rq, p, sleep);
  1638. dec_nr_running(rq);
  1639. }
  1640. /**
  1641. * task_curr - is this task currently executing on a CPU?
  1642. * @p: the task in question.
  1643. */
  1644. inline int task_curr(const struct task_struct *p)
  1645. {
  1646. return cpu_curr(task_cpu(p)) == p;
  1647. }
  1648. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  1649. {
  1650. set_task_rq(p, cpu);
  1651. #ifdef CONFIG_SMP
  1652. /*
  1653. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  1654. * successfuly executed on another CPU. We must ensure that updates of
  1655. * per-task data have been completed by this moment.
  1656. */
  1657. smp_wmb();
  1658. task_thread_info(p)->cpu = cpu;
  1659. #endif
  1660. }
  1661. static inline void check_class_changed(struct rq *rq, struct task_struct *p,
  1662. const struct sched_class *prev_class,
  1663. int oldprio, int running)
  1664. {
  1665. if (prev_class != p->sched_class) {
  1666. if (prev_class->switched_from)
  1667. prev_class->switched_from(rq, p, running);
  1668. p->sched_class->switched_to(rq, p, running);
  1669. } else
  1670. p->sched_class->prio_changed(rq, p, oldprio, running);
  1671. }
  1672. #ifdef CONFIG_SMP
  1673. /*
  1674. * Is this task likely cache-hot:
  1675. */
  1676. static int
  1677. task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
  1678. {
  1679. s64 delta;
  1680. /*
  1681. * Buddy candidates are cache hot:
  1682. */
  1683. if (sched_feat(CACHE_HOT_BUDDY) &&
  1684. (&p->se == cfs_rq_of(&p->se)->next ||
  1685. &p->se == cfs_rq_of(&p->se)->last))
  1686. return 1;
  1687. if (p->sched_class != &fair_sched_class)
  1688. return 0;
  1689. if (sysctl_sched_migration_cost == -1)
  1690. return 1;
  1691. if (sysctl_sched_migration_cost == 0)
  1692. return 0;
  1693. delta = now - p->se.exec_start;
  1694. return delta < (s64)sysctl_sched_migration_cost;
  1695. }
  1696. void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
  1697. {
  1698. int old_cpu = task_cpu(p);
  1699. struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
  1700. struct cfs_rq *old_cfsrq = task_cfs_rq(p),
  1701. *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
  1702. u64 clock_offset;
  1703. clock_offset = old_rq->clock - new_rq->clock;
  1704. trace_sched_migrate_task(p, new_cpu);
  1705. #ifdef CONFIG_SCHEDSTATS
  1706. if (p->se.wait_start)
  1707. p->se.wait_start -= clock_offset;
  1708. if (p->se.sleep_start)
  1709. p->se.sleep_start -= clock_offset;
  1710. if (p->se.block_start)
  1711. p->se.block_start -= clock_offset;
  1712. #endif
  1713. if (old_cpu != new_cpu) {
  1714. p->se.nr_migrations++;
  1715. new_rq->nr_migrations_in++;
  1716. #ifdef CONFIG_SCHEDSTATS
  1717. if (task_hot(p, old_rq->clock, NULL))
  1718. schedstat_inc(p, se.nr_forced2_migrations);
  1719. #endif
  1720. perf_swcounter_event(PERF_COUNT_SW_CPU_MIGRATIONS,
  1721. 1, 1, NULL, 0);
  1722. }
  1723. p->se.vruntime -= old_cfsrq->min_vruntime -
  1724. new_cfsrq->min_vruntime;
  1725. __set_task_cpu(p, new_cpu);
  1726. }
  1727. struct migration_req {
  1728. struct list_head list;
  1729. struct task_struct *task;
  1730. int dest_cpu;
  1731. struct completion done;
  1732. };
  1733. /*
  1734. * The task's runqueue lock must be held.
  1735. * Returns true if you have to wait for migration thread.
  1736. */
  1737. static int
  1738. migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
  1739. {
  1740. struct rq *rq = task_rq(p);
  1741. /*
  1742. * If the task is not on a runqueue (and not running), then
  1743. * it is sufficient to simply update the task's cpu field.
  1744. */
  1745. if (!p->se.on_rq && !task_running(rq, p)) {
  1746. set_task_cpu(p, dest_cpu);
  1747. return 0;
  1748. }
  1749. init_completion(&req->done);
  1750. req->task = p;
  1751. req->dest_cpu = dest_cpu;
  1752. list_add(&req->list, &rq->migration_queue);
  1753. return 1;
  1754. }
  1755. /*
  1756. * wait_task_context_switch - wait for a thread to complete at least one
  1757. * context switch.
  1758. *
  1759. * @p must not be current.
  1760. */
  1761. void wait_task_context_switch(struct task_struct *p)
  1762. {
  1763. unsigned long nvcsw, nivcsw, flags;
  1764. int running;
  1765. struct rq *rq;
  1766. nvcsw = p->nvcsw;
  1767. nivcsw = p->nivcsw;
  1768. for (;;) {
  1769. /*
  1770. * The runqueue is assigned before the actual context
  1771. * switch. We need to take the runqueue lock.
  1772. *
  1773. * We could check initially without the lock but it is
  1774. * very likely that we need to take the lock in every
  1775. * iteration.
  1776. */
  1777. rq = task_rq_lock(p, &flags);
  1778. running = task_running(rq, p);
  1779. task_rq_unlock(rq, &flags);
  1780. if (likely(!running))
  1781. break;
  1782. /*
  1783. * The switch count is incremented before the actual
  1784. * context switch. We thus wait for two switches to be
  1785. * sure at least one completed.
  1786. */
  1787. if ((p->nvcsw - nvcsw) > 1)
  1788. break;
  1789. if ((p->nivcsw - nivcsw) > 1)
  1790. break;
  1791. cpu_relax();
  1792. }
  1793. }
  1794. /*
  1795. * wait_task_inactive - wait for a thread to unschedule.
  1796. *
  1797. * If @match_state is nonzero, it's the @p->state value just checked and
  1798. * not expected to change. If it changes, i.e. @p might have woken up,
  1799. * then return zero. When we succeed in waiting for @p to be off its CPU,
  1800. * we return a positive number (its total switch count). If a second call
  1801. * a short while later returns the same number, the caller can be sure that
  1802. * @p has remained unscheduled the whole time.
  1803. *
  1804. * The caller must ensure that the task *will* unschedule sometime soon,
  1805. * else this function might spin for a *long* time. This function can't
  1806. * be called with interrupts off, or it may introduce deadlock with
  1807. * smp_call_function() if an IPI is sent by the same process we are
  1808. * waiting to become inactive.
  1809. */
  1810. unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  1811. {
  1812. unsigned long flags;
  1813. int running, on_rq;
  1814. unsigned long ncsw;
  1815. struct rq *rq;
  1816. for (;;) {
  1817. /*
  1818. * We do the initial early heuristics without holding
  1819. * any task-queue locks at all. We'll only try to get
  1820. * the runqueue lock when things look like they will
  1821. * work out!
  1822. */
  1823. rq = task_rq(p);
  1824. /*
  1825. * If the task is actively running on another CPU
  1826. * still, just relax and busy-wait without holding
  1827. * any locks.
  1828. *
  1829. * NOTE! Since we don't hold any locks, it's not
  1830. * even sure that "rq" stays as the right runqueue!
  1831. * But we don't care, since "task_running()" will
  1832. * return false if the runqueue has changed and p
  1833. * is actually now running somewhere else!
  1834. */
  1835. while (task_running(rq, p)) {
  1836. if (match_state && unlikely(p->state != match_state))
  1837. return 0;
  1838. cpu_relax();
  1839. }
  1840. /*
  1841. * Ok, time to look more closely! We need the rq
  1842. * lock now, to be *sure*. If we're wrong, we'll
  1843. * just go back and repeat.
  1844. */
  1845. rq = task_rq_lock(p, &flags);
  1846. trace_sched_wait_task(rq, p);
  1847. running = task_running(rq, p);
  1848. on_rq = p->se.on_rq;
  1849. ncsw = 0;
  1850. if (!match_state || p->state == match_state)
  1851. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  1852. task_rq_unlock(rq, &flags);
  1853. /*
  1854. * If it changed from the expected state, bail out now.
  1855. */
  1856. if (unlikely(!ncsw))
  1857. break;
  1858. /*
  1859. * Was it really running after all now that we
  1860. * checked with the proper locks actually held?
  1861. *
  1862. * Oops. Go back and try again..
  1863. */
  1864. if (unlikely(running)) {
  1865. cpu_relax();
  1866. continue;
  1867. }
  1868. /*
  1869. * It's not enough that it's not actively running,
  1870. * it must be off the runqueue _entirely_, and not
  1871. * preempted!
  1872. *
  1873. * So if it was still runnable (but just not actively
  1874. * running right now), it's preempted, and we should
  1875. * yield - it could be a while.
  1876. */
  1877. if (unlikely(on_rq)) {
  1878. schedule_timeout_uninterruptible(1);
  1879. continue;
  1880. }
  1881. /*
  1882. * Ahh, all good. It wasn't running, and it wasn't
  1883. * runnable, which means that it will never become
  1884. * running in the future either. We're all done!
  1885. */
  1886. break;
  1887. }
  1888. return ncsw;
  1889. }
  1890. /***
  1891. * kick_process - kick a running thread to enter/exit the kernel
  1892. * @p: the to-be-kicked thread
  1893. *
  1894. * Cause a process which is running on another CPU to enter
  1895. * kernel-mode, without any delay. (to get signals handled.)
  1896. *
  1897. * NOTE: this function doesnt have to take the runqueue lock,
  1898. * because all it wants to ensure is that the remote task enters
  1899. * the kernel. If the IPI races and the task has been migrated
  1900. * to another CPU then no harm is done and the purpose has been
  1901. * achieved as well.
  1902. */
  1903. void kick_process(struct task_struct *p)
  1904. {
  1905. int cpu;
  1906. preempt_disable();
  1907. cpu = task_cpu(p);
  1908. if ((cpu != smp_processor_id()) && task_curr(p))
  1909. smp_send_reschedule(cpu);
  1910. preempt_enable();
  1911. }
  1912. EXPORT_SYMBOL_GPL(kick_process);
  1913. #endif /* CONFIG_SMP */
  1914. /**
  1915. * task_oncpu_function_call - call a function on the cpu on which a task runs
  1916. * @p: the task to evaluate
  1917. * @func: the function to be called
  1918. * @info: the function call argument
  1919. *
  1920. * Calls the function @func when the task is currently running. This might
  1921. * be on the current CPU, which just calls the function directly
  1922. */
  1923. void task_oncpu_function_call(struct task_struct *p,
  1924. void (*func) (void *info), void *info)
  1925. {
  1926. int cpu;
  1927. preempt_disable();
  1928. cpu = task_cpu(p);
  1929. if (task_curr(p))
  1930. smp_call_function_single(cpu, func, info, 1);
  1931. preempt_enable();
  1932. }
  1933. /***
  1934. * try_to_wake_up - wake up a thread
  1935. * @p: the to-be-woken-up thread
  1936. * @state: the mask of task states that can be woken
  1937. * @sync: do a synchronous wakeup?
  1938. *
  1939. * Put it on the run-queue if it's not already there. The "current"
  1940. * thread is always on the run-queue (except when the actual
  1941. * re-schedule is in progress), and as such you're allowed to do
  1942. * the simpler "current->state = TASK_RUNNING" to mark yourself
  1943. * runnable without the overhead of this.
  1944. *
  1945. * returns failure only if the task is already active.
  1946. */
  1947. static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
  1948. {
  1949. int cpu, orig_cpu, this_cpu, success = 0;
  1950. unsigned long flags;
  1951. long old_state;
  1952. struct rq *rq;
  1953. if (!sched_feat(SYNC_WAKEUPS))
  1954. sync = 0;
  1955. #ifdef CONFIG_SMP
  1956. if (sched_feat(LB_WAKEUP_UPDATE) && !root_task_group_empty()) {
  1957. struct sched_domain *sd;
  1958. this_cpu = raw_smp_processor_id();
  1959. cpu = task_cpu(p);
  1960. for_each_domain(this_cpu, sd) {
  1961. if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  1962. update_shares(sd);
  1963. break;
  1964. }
  1965. }
  1966. }
  1967. #endif
  1968. smp_wmb();
  1969. rq = task_rq_lock(p, &flags);
  1970. update_rq_clock(rq);
  1971. old_state = p->state;
  1972. if (!(old_state & state))
  1973. goto out;
  1974. if (p->se.on_rq)
  1975. goto out_running;
  1976. cpu = task_cpu(p);
  1977. orig_cpu = cpu;
  1978. this_cpu = smp_processor_id();
  1979. #ifdef CONFIG_SMP
  1980. if (unlikely(task_running(rq, p)))
  1981. goto out_activate;
  1982. cpu = p->sched_class->select_task_rq(p, sync);
  1983. if (cpu != orig_cpu) {
  1984. set_task_cpu(p, cpu);
  1985. task_rq_unlock(rq, &flags);
  1986. /* might preempt at this point */
  1987. rq = task_rq_lock(p, &flags);
  1988. old_state = p->state;
  1989. if (!(old_state & state))
  1990. goto out;
  1991. if (p->se.on_rq)
  1992. goto out_running;
  1993. this_cpu = smp_processor_id();
  1994. cpu = task_cpu(p);
  1995. }
  1996. #ifdef CONFIG_SCHEDSTATS
  1997. schedstat_inc(rq, ttwu_count);
  1998. if (cpu == this_cpu)
  1999. schedstat_inc(rq, ttwu_local);
  2000. else {
  2001. struct sched_domain *sd;
  2002. for_each_domain(this_cpu, sd) {
  2003. if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  2004. schedstat_inc(sd, ttwu_wake_remote);
  2005. break;
  2006. }
  2007. }
  2008. }
  2009. #endif /* CONFIG_SCHEDSTATS */
  2010. out_activate:
  2011. #endif /* CONFIG_SMP */
  2012. schedstat_inc(p, se.nr_wakeups);
  2013. if (sync)
  2014. schedstat_inc(p, se.nr_wakeups_sync);
  2015. if (orig_cpu != cpu)
  2016. schedstat_inc(p, se.nr_wakeups_migrate);
  2017. if (cpu == this_cpu)
  2018. schedstat_inc(p, se.nr_wakeups_local);
  2019. else
  2020. schedstat_inc(p, se.nr_wakeups_remote);
  2021. activate_task(rq, p, 1);
  2022. success = 1;
  2023. /*
  2024. * Only attribute actual wakeups done by this task.
  2025. */
  2026. if (!in_interrupt()) {
  2027. struct sched_entity *se = &current->se;
  2028. u64 sample = se->sum_exec_runtime;
  2029. if (se->last_wakeup)
  2030. sample -= se->last_wakeup;
  2031. else
  2032. sample -= se->start_runtime;
  2033. update_avg(&se->avg_wakeup, sample);
  2034. se->last_wakeup = se->sum_exec_runtime;
  2035. }
  2036. out_running:
  2037. trace_sched_wakeup(rq, p, success);
  2038. check_preempt_curr(rq, p, sync);
  2039. p->state = TASK_RUNNING;
  2040. #ifdef CONFIG_SMP
  2041. if (p->sched_class->task_wake_up)
  2042. p->sched_class->task_wake_up(rq, p);
  2043. #endif
  2044. out:
  2045. task_rq_unlock(rq, &flags);
  2046. return success;
  2047. }
  2048. /**
  2049. * wake_up_process - Wake up a specific process
  2050. * @p: The process to be woken up.
  2051. *
  2052. * Attempt to wake up the nominated process and move it to the set of runnable
  2053. * processes. Returns 1 if the process was woken up, 0 if it was already
  2054. * running.
  2055. *
  2056. * It may be assumed that this function implies a write memory barrier before
  2057. * changing the task state if and only if any tasks are woken up.
  2058. */
  2059. int wake_up_process(struct task_struct *p)
  2060. {
  2061. return try_to_wake_up(p, TASK_ALL, 0);
  2062. }
  2063. EXPORT_SYMBOL(wake_up_process);
  2064. int wake_up_state(struct task_struct *p, unsigned int state)
  2065. {
  2066. return try_to_wake_up(p, state, 0);
  2067. }
  2068. /*
  2069. * Perform scheduler related setup for a newly forked process p.
  2070. * p is forked by current.
  2071. *
  2072. * __sched_fork() is basic setup used by init_idle() too:
  2073. */
  2074. static void __sched_fork(struct task_struct *p)
  2075. {
  2076. p->se.exec_start = 0;
  2077. p->se.sum_exec_runtime = 0;
  2078. p->se.prev_sum_exec_runtime = 0;
  2079. p->se.nr_migrations = 0;
  2080. p->se.last_wakeup = 0;
  2081. p->se.avg_overlap = 0;
  2082. p->se.start_runtime = 0;
  2083. p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
  2084. #ifdef CONFIG_SCHEDSTATS
  2085. p->se.wait_start = 0;
  2086. p->se.wait_max = 0;
  2087. p->se.wait_count = 0;
  2088. p->se.wait_sum = 0;
  2089. p->se.sleep_start = 0;
  2090. p->se.sleep_max = 0;
  2091. p->se.sum_sleep_runtime = 0;
  2092. p->se.block_start = 0;
  2093. p->se.block_max = 0;
  2094. p->se.exec_max = 0;
  2095. p->se.slice_max = 0;
  2096. p->se.nr_migrations_cold = 0;
  2097. p->se.nr_failed_migrations_affine = 0;
  2098. p->se.nr_failed_migrations_running = 0;
  2099. p->se.nr_failed_migrations_hot = 0;
  2100. p->se.nr_forced_migrations = 0;
  2101. p->se.nr_forced2_migrations = 0;
  2102. p->se.nr_wakeups = 0;
  2103. p->se.nr_wakeups_sync = 0;
  2104. p->se.nr_wakeups_migrate = 0;
  2105. p->se.nr_wakeups_local = 0;
  2106. p->se.nr_wakeups_remote = 0;
  2107. p->se.nr_wakeups_affine = 0;
  2108. p->se.nr_wakeups_affine_attempts = 0;
  2109. p->se.nr_wakeups_passive = 0;
  2110. p->se.nr_wakeups_idle = 0;
  2111. #endif
  2112. INIT_LIST_HEAD(&p->rt.run_list);
  2113. p->se.on_rq = 0;
  2114. INIT_LIST_HEAD(&p->se.group_node);
  2115. #ifdef CONFIG_PREEMPT_NOTIFIERS
  2116. INIT_HLIST_HEAD(&p->preempt_notifiers);
  2117. #endif
  2118. /*
  2119. * We mark the process as running here, but have not actually
  2120. * inserted it onto the runqueue yet. This guarantees that
  2121. * nobody will actually run it, and a signal or other external
  2122. * event cannot wake it up and insert it on the runqueue either.
  2123. */
  2124. p->state = TASK_RUNNING;
  2125. }
  2126. /*
  2127. * fork()/clone()-time setup:
  2128. */
  2129. void sched_fork(struct task_struct *p, int clone_flags)
  2130. {
  2131. int cpu = get_cpu();
  2132. __sched_fork(p);
  2133. #ifdef CONFIG_SMP
  2134. cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
  2135. #endif
  2136. set_task_cpu(p, cpu);
  2137. /*
  2138. * Make sure we do not leak PI boosting priority to the child.
  2139. */
  2140. p->prio = current->normal_prio;
  2141. /*
  2142. * Revert to default priority/policy on fork if requested.
  2143. */
  2144. if (unlikely(p->sched_reset_on_fork)) {
  2145. if (p->policy == SCHED_FIFO || p->policy == SCHED_RR)
  2146. p->policy = SCHED_NORMAL;
  2147. if (p->normal_prio < DEFAULT_PRIO)
  2148. p->prio = DEFAULT_PRIO;
  2149. if (PRIO_TO_NICE(p->static_prio) < 0) {
  2150. p->static_prio = NICE_TO_PRIO(0);
  2151. set_load_weight(p);
  2152. }
  2153. /*
  2154. * We don't need the reset flag anymore after the fork. It has
  2155. * fulfilled its duty:
  2156. */
  2157. p->sched_reset_on_fork = 0;
  2158. }
  2159. if (!rt_prio(p->prio))
  2160. p->sched_class = &fair_sched_class;
  2161. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  2162. if (likely(sched_info_on()))
  2163. memset(&p->sched_info, 0, sizeof(p->sched_info));
  2164. #endif
  2165. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  2166. p->oncpu = 0;
  2167. #endif
  2168. #ifdef CONFIG_PREEMPT
  2169. /* Want to start with kernel preemption disabled. */
  2170. task_thread_info(p)->preempt_count = 1;
  2171. #endif
  2172. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  2173. put_cpu();
  2174. }
  2175. /*
  2176. * wake_up_new_task - wake up a newly created task for the first time.
  2177. *
  2178. * This function will do some initial scheduler statistics housekeeping
  2179. * that must be done for every newly created context, then puts the task
  2180. * on the runqueue and wakes it.
  2181. */
  2182. void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
  2183. {
  2184. unsigned long flags;
  2185. struct rq *rq;
  2186. rq = task_rq_lock(p, &flags);
  2187. BUG_ON(p->state != TASK_RUNNING);
  2188. update_rq_clock(rq);
  2189. p->prio = effective_prio(p);
  2190. if (!p->sched_class->task_new || !current->se.on_rq) {
  2191. activate_task(rq, p, 0);
  2192. } else {
  2193. /*
  2194. * Let the scheduling class do new task startup
  2195. * management (if any):
  2196. */
  2197. p->sched_class->task_new(rq, p);
  2198. inc_nr_running(rq);
  2199. }
  2200. trace_sched_wakeup_new(rq, p, 1);
  2201. check_preempt_curr(rq, p, 0);
  2202. #ifdef CONFIG_SMP
  2203. if (p->sched_class->task_wake_up)
  2204. p->sched_class->task_wake_up(rq, p);
  2205. #endif
  2206. task_rq_unlock(rq, &flags);
  2207. }
  2208. #ifdef CONFIG_PREEMPT_NOTIFIERS
  2209. /**
  2210. * preempt_notifier_register - tell me when current is being preempted & rescheduled
  2211. * @notifier: notifier struct to register
  2212. */
  2213. void preempt_notifier_register(struct preempt_notifier *notifier)
  2214. {
  2215. hlist_add_head(&notifier->link, &current->preempt_notifiers);
  2216. }
  2217. EXPORT_SYMBOL_GPL(preempt_notifier_register);
  2218. /**
  2219. * preempt_notifier_unregister - no longer interested in preemption notifications
  2220. * @notifier: notifier struct to unregister
  2221. *
  2222. * This is safe to call from within a preemption notifier.
  2223. */
  2224. void preempt_notifier_unregister(struct preempt_notifier *notifier)
  2225. {
  2226. hlist_del(&notifier->link);
  2227. }
  2228. EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
  2229. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  2230. {
  2231. struct preempt_notifier *notifier;
  2232. struct hlist_node *node;
  2233. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  2234. notifier->ops->sched_in(notifier, raw_smp_processor_id());
  2235. }
  2236. static void
  2237. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  2238. struct task_struct *next)
  2239. {
  2240. struct preempt_notifier *notifier;
  2241. struct hlist_node *node;
  2242. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  2243. notifier->ops->sched_out(notifier, next);
  2244. }
  2245. #else /* !CONFIG_PREEMPT_NOTIFIERS */
  2246. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  2247. {
  2248. }
  2249. static void
  2250. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  2251. struct task_struct *next)
  2252. {
  2253. }
  2254. #endif /* CONFIG_PREEMPT_NOTIFIERS */
  2255. /**
  2256. * prepare_task_switch - prepare to switch tasks
  2257. * @rq: the runqueue preparing to switch
  2258. * @prev: the current task that is being switched out
  2259. * @next: the task we are going to switch to.
  2260. *
  2261. * This is called with the rq lock held and interrupts off. It must
  2262. * be paired with a subsequent finish_task_switch after the context
  2263. * switch.
  2264. *
  2265. * prepare_task_switch sets up locking and calls architecture specific
  2266. * hooks.
  2267. */
  2268. static inline void
  2269. prepare_task_switch(struct rq *rq, struct task_struct *prev,
  2270. struct task_struct *next)
  2271. {
  2272. fire_sched_out_preempt_notifiers(prev, next);
  2273. prepare_lock_switch(rq, next);
  2274. prepare_arch_switch(next);
  2275. }
  2276. /**
  2277. * finish_task_switch - clean up after a task-switch
  2278. * @rq: runqueue associated with task-switch
  2279. * @prev: the thread we just switched away from.
  2280. *
  2281. * finish_task_switch must be called after the context switch, paired
  2282. * with a prepare_task_switch call before the context switch.
  2283. * finish_task_switch will reconcile locking set up by prepare_task_switch,
  2284. * and do any other architecture-specific cleanup actions.
  2285. *
  2286. * Note that we may have delayed dropping an mm in context_switch(). If
  2287. * so, we finish that here outside of the runqueue lock. (Doing it
  2288. * with the lock held can cause deadlocks; see schedule() for
  2289. * details.)
  2290. */
  2291. static void finish_task_switch(struct rq *rq, struct task_struct *prev)
  2292. __releases(rq->lock)
  2293. {
  2294. struct mm_struct *mm = rq->prev_mm;
  2295. long prev_state;
  2296. rq->prev_mm = NULL;
  2297. /*
  2298. * A task struct has one reference for the use as "current".
  2299. * If a task dies, then it sets TASK_DEAD in tsk->state and calls
  2300. * schedule one last time. The schedule call will never return, and
  2301. * the scheduled task must drop that reference.
  2302. * The test for TASK_DEAD must occur while the runqueue locks are
  2303. * still held, otherwise prev could be scheduled on another cpu, die
  2304. * there before we look at prev->state, and then the reference would
  2305. * be dropped twice.
  2306. * Manfred Spraul <manfred@colorfullife.com>
  2307. */
  2308. prev_state = prev->state;
  2309. finish_arch_switch(prev);
  2310. perf_counter_task_sched_in(current, cpu_of(rq));
  2311. finish_lock_switch(rq, prev);
  2312. fire_sched_in_preempt_notifiers(current);
  2313. if (mm)
  2314. mmdrop(mm);
  2315. if (unlikely(prev_state == TASK_DEAD)) {
  2316. /*
  2317. * Remove function-return probe instances associated with this
  2318. * task and put them back on the free list.
  2319. */
  2320. kprobe_flush_task(prev);
  2321. put_task_struct(prev);
  2322. }
  2323. }
  2324. #ifdef CONFIG_SMP
  2325. /* assumes rq->lock is held */
  2326. static inline void pre_schedule(struct rq *rq, struct task_struct *prev)
  2327. {
  2328. if (prev->sched_class->pre_schedule)
  2329. prev->sched_class->pre_schedule(rq, prev);
  2330. }
  2331. /* rq->lock is NOT held, but preemption is disabled */
  2332. static inline void post_schedule(struct rq *rq)
  2333. {
  2334. if (rq->post_schedule) {
  2335. unsigned long flags;
  2336. spin_lock_irqsave(&rq->lock, flags);
  2337. if (rq->curr->sched_class->post_schedule)
  2338. rq->curr->sched_class->post_schedule(rq);
  2339. spin_unlock_irqrestore(&rq->lock, flags);
  2340. rq->post_schedule = 0;
  2341. }
  2342. }
  2343. #else
  2344. static inline void pre_schedule(struct rq *rq, struct task_struct *p)
  2345. {
  2346. }
  2347. static inline void post_schedule(struct rq *rq)
  2348. {
  2349. }
  2350. #endif
  2351. /**
  2352. * schedule_tail - first thing a freshly forked thread must call.
  2353. * @prev: the thread we just switched away from.
  2354. */
  2355. asmlinkage void schedule_tail(struct task_struct *prev)
  2356. __releases(rq->lock)
  2357. {
  2358. struct rq *rq = this_rq();
  2359. finish_task_switch(rq, prev);
  2360. /*
  2361. * FIXME: do we need to worry about rq being invalidated by the
  2362. * task_switch?
  2363. */
  2364. post_schedule(rq);
  2365. #ifdef __ARCH_WANT_UNLOCKED_CTXSW
  2366. /* In this case, finish_task_switch does not reenable preemption */
  2367. preempt_enable();
  2368. #endif
  2369. if (current->set_child_tid)
  2370. put_user(task_pid_vnr(current), current->set_child_tid);
  2371. }
  2372. /*
  2373. * context_switch - switch to the new MM and the new
  2374. * thread's register state.
  2375. */
  2376. static inline void
  2377. context_switch(struct rq *rq, struct task_struct *prev,
  2378. struct task_struct *next)
  2379. {
  2380. struct mm_struct *mm, *oldmm;
  2381. prepare_task_switch(rq, prev, next);
  2382. trace_sched_switch(rq, prev, next);
  2383. mm = next->mm;
  2384. oldmm = prev->active_mm;
  2385. /*
  2386. * For paravirt, this is coupled with an exit in switch_to to
  2387. * combine the page table reload and the switch backend into
  2388. * one hypercall.
  2389. */
  2390. arch_start_context_switch(prev);
  2391. if (unlikely(!mm)) {
  2392. next->active_mm = oldmm;
  2393. atomic_inc(&oldmm->mm_count);
  2394. enter_lazy_tlb(oldmm, next);
  2395. } else
  2396. switch_mm(oldmm, mm, next);
  2397. if (unlikely(!prev->mm)) {
  2398. prev->active_mm = NULL;
  2399. rq->prev_mm = oldmm;
  2400. }
  2401. /*
  2402. * Since the runqueue lock will be released by the next
  2403. * task (which is an invalid locking op but in the case
  2404. * of the scheduler it's an obvious special-case), so we
  2405. * do an early lockdep release here:
  2406. */
  2407. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  2408. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  2409. #endif
  2410. /* Here we just switch the register state and the stack. */
  2411. switch_to(prev, next, prev);
  2412. barrier();
  2413. /*
  2414. * this_rq must be evaluated again because prev may have moved
  2415. * CPUs since it called schedule(), thus the 'rq' on its stack
  2416. * frame will be invalid.
  2417. */
  2418. finish_task_switch(this_rq(), prev);
  2419. }
  2420. /*
  2421. * nr_running, nr_uninterruptible and nr_context_switches:
  2422. *
  2423. * externally visible scheduler statistics: current number of runnable
  2424. * threads, current number of uninterruptible-sleeping threads, total
  2425. * number of context switches performed since bootup.
  2426. */
  2427. unsigned long nr_running(void)
  2428. {
  2429. unsigned long i, sum = 0;
  2430. for_each_online_cpu(i)
  2431. sum += cpu_rq(i)->nr_running;
  2432. return sum;
  2433. }
  2434. unsigned long nr_uninterruptible(void)
  2435. {
  2436. unsigned long i, sum = 0;
  2437. for_each_possible_cpu(i)
  2438. sum += cpu_rq(i)->nr_uninterruptible;
  2439. /*
  2440. * Since we read the counters lockless, it might be slightly
  2441. * inaccurate. Do not allow it to go below zero though:
  2442. */
  2443. if (unlikely((long)sum < 0))
  2444. sum = 0;
  2445. return sum;
  2446. }
  2447. unsigned long long nr_context_switches(void)
  2448. {
  2449. int i;
  2450. unsigned long long sum = 0;
  2451. for_each_possible_cpu(i)
  2452. sum += cpu_rq(i)->nr_switches;
  2453. return sum;
  2454. }
  2455. unsigned long nr_iowait(void)
  2456. {
  2457. unsigned long i, sum = 0;
  2458. for_each_possible_cpu(i)
  2459. sum += atomic_read(&cpu_rq(i)->nr_iowait);
  2460. return sum;
  2461. }
  2462. /* Variables and functions for calc_load */
  2463. static atomic_long_t calc_load_tasks;
  2464. static unsigned long calc_load_update;
  2465. unsigned long avenrun[3];
  2466. EXPORT_SYMBOL(avenrun);
  2467. /**
  2468. * get_avenrun - get the load average array
  2469. * @loads: pointer to dest load array
  2470. * @offset: offset to add
  2471. * @shift: shift count to shift the result left
  2472. *
  2473. * These values are estimates at best, so no need for locking.
  2474. */
  2475. void get_avenrun(unsigned long *loads, unsigned long offset, int shift)
  2476. {
  2477. loads[0] = (avenrun[0] + offset) << shift;
  2478. loads[1] = (avenrun[1] + offset) << shift;
  2479. loads[2] = (avenrun[2] + offset) << shift;
  2480. }
  2481. static unsigned long
  2482. calc_load(unsigned long load, unsigned long exp, unsigned long active)
  2483. {
  2484. load *= exp;
  2485. load += active * (FIXED_1 - exp);
  2486. return load >> FSHIFT;
  2487. }
  2488. /*
  2489. * calc_load - update the avenrun load estimates 10 ticks after the
  2490. * CPUs have updated calc_load_tasks.
  2491. */
  2492. void calc_global_load(void)
  2493. {
  2494. unsigned long upd = calc_load_update + 10;
  2495. long active;
  2496. if (time_before(jiffies, upd))
  2497. return;
  2498. active = atomic_long_read(&calc_load_tasks);
  2499. active = active > 0 ? active * FIXED_1 : 0;
  2500. avenrun[0] = calc_load(avenrun[0], EXP_1, active);
  2501. avenrun[1] = calc_load(avenrun[1], EXP_5, active);
  2502. avenrun[2] = calc_load(avenrun[2], EXP_15, active);
  2503. calc_load_update += LOAD_FREQ;
  2504. }
  2505. /*
  2506. * Either called from update_cpu_load() or from a cpu going idle
  2507. */
  2508. static void calc_load_account_active(struct rq *this_rq)
  2509. {
  2510. long nr_active, delta;
  2511. nr_active = this_rq->nr_running;
  2512. nr_active += (long) this_rq->nr_uninterruptible;
  2513. if (nr_active != this_rq->calc_load_active) {
  2514. delta = nr_active - this_rq->calc_load_active;
  2515. this_rq->calc_load_active = nr_active;
  2516. atomic_long_add(delta, &calc_load_tasks);
  2517. }
  2518. }
  2519. /*
  2520. * Externally visible per-cpu scheduler statistics:
  2521. * cpu_nr_migrations(cpu) - number of migrations into that cpu
  2522. */
  2523. u64 cpu_nr_migrations(int cpu)
  2524. {
  2525. return cpu_rq(cpu)->nr_migrations_in;
  2526. }
  2527. /*
  2528. * Update rq->cpu_load[] statistics. This function is usually called every
  2529. * scheduler tick (TICK_NSEC).
  2530. */
  2531. static void update_cpu_load(struct rq *this_rq)
  2532. {
  2533. unsigned long this_load = this_rq->load.weight;
  2534. int i, scale;
  2535. this_rq->nr_load_updates++;
  2536. /* Update our load: */
  2537. for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
  2538. unsigned long old_load, new_load;
  2539. /* scale is effectively 1 << i now, and >> i divides by scale */
  2540. old_load = this_rq->cpu_load[i];
  2541. new_load = this_load;
  2542. /*
  2543. * Round up the averaging division if load is increasing. This
  2544. * prevents us from getting stuck on 9 if the load is 10, for
  2545. * example.
  2546. */
  2547. if (new_load > old_load)
  2548. new_load += scale-1;
  2549. this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
  2550. }
  2551. if (time_after_eq(jiffies, this_rq->calc_load_update)) {
  2552. this_rq->calc_load_update += LOAD_FREQ;
  2553. calc_load_account_active(this_rq);
  2554. }
  2555. }
  2556. #ifdef CONFIG_SMP
  2557. /*
  2558. * double_rq_lock - safely lock two runqueues
  2559. *
  2560. * Note this does not disable interrupts like task_rq_lock,
  2561. * you need to do so manually before calling.
  2562. */
  2563. static void double_rq_lock(struct rq *rq1, struct rq *rq2)
  2564. __acquires(rq1->lock)
  2565. __acquires(rq2->lock)
  2566. {
  2567. BUG_ON(!irqs_disabled());
  2568. if (rq1 == rq2) {
  2569. spin_lock(&rq1->lock);
  2570. __acquire(rq2->lock); /* Fake it out ;) */
  2571. } else {
  2572. if (rq1 < rq2) {
  2573. spin_lock(&rq1->lock);
  2574. spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
  2575. } else {
  2576. spin_lock(&rq2->lock);
  2577. spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
  2578. }
  2579. }
  2580. update_rq_clock(rq1);
  2581. update_rq_clock(rq2);
  2582. }
  2583. /*
  2584. * double_rq_unlock - safely unlock two runqueues
  2585. *
  2586. * Note this does not restore interrupts like task_rq_unlock,
  2587. * you need to do so manually after calling.
  2588. */
  2589. static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  2590. __releases(rq1->lock)
  2591. __releases(rq2->lock)
  2592. {
  2593. spin_unlock(&rq1->lock);
  2594. if (rq1 != rq2)
  2595. spin_unlock(&rq2->lock);
  2596. else
  2597. __release(rq2->lock);
  2598. }
  2599. /*
  2600. * If dest_cpu is allowed for this process, migrate the task to it.
  2601. * This is accomplished by forcing the cpu_allowed mask to only
  2602. * allow dest_cpu, which will force the cpu onto dest_cpu. Then
  2603. * the cpu_allowed mask is restored.
  2604. */
  2605. static void sched_migrate_task(struct task_struct *p, int dest_cpu)
  2606. {
  2607. struct migration_req req;
  2608. unsigned long flags;
  2609. struct rq *rq;
  2610. rq = task_rq_lock(p, &flags);
  2611. if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
  2612. || unlikely(!cpu_active(dest_cpu)))
  2613. goto out;
  2614. /* force the process onto the specified CPU */
  2615. if (migrate_task(p, dest_cpu, &req)) {
  2616. /* Need to wait for migration thread (might exit: take ref). */
  2617. struct task_struct *mt = rq->migration_thread;
  2618. get_task_struct(mt);
  2619. task_rq_unlock(rq, &flags);
  2620. wake_up_process(mt);
  2621. put_task_struct(mt);
  2622. wait_for_completion(&req.done);
  2623. return;
  2624. }
  2625. out:
  2626. task_rq_unlock(rq, &flags);
  2627. }
  2628. /*
  2629. * sched_exec - execve() is a valuable balancing opportunity, because at
  2630. * this point the task has the smallest effective memory and cache footprint.
  2631. */
  2632. void sched_exec(void)
  2633. {
  2634. int new_cpu, this_cpu = get_cpu();
  2635. new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
  2636. put_cpu();
  2637. if (new_cpu != this_cpu)
  2638. sched_migrate_task(current, new_cpu);
  2639. }
  2640. /*
  2641. * pull_task - move a task from a remote runqueue to the local runqueue.
  2642. * Both runqueues must be locked.
  2643. */
  2644. static void pull_task(struct rq *src_rq, struct task_struct *p,
  2645. struct rq *this_rq, int this_cpu)
  2646. {
  2647. deactivate_task(src_rq, p, 0);
  2648. set_task_cpu(p, this_cpu);
  2649. activate_task(this_rq, p, 0);
  2650. /*
  2651. * Note that idle threads have a prio of MAX_PRIO, for this test
  2652. * to be always true for them.
  2653. */
  2654. check_preempt_curr(this_rq, p, 0);
  2655. }
  2656. /*
  2657. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  2658. */
  2659. static
  2660. int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
  2661. struct sched_domain *sd, enum cpu_idle_type idle,
  2662. int *all_pinned)
  2663. {
  2664. int tsk_cache_hot = 0;
  2665. /*
  2666. * We do not migrate tasks that are:
  2667. * 1) running (obviously), or
  2668. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  2669. * 3) are cache-hot on their current CPU.
  2670. */
  2671. if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
  2672. schedstat_inc(p, se.nr_failed_migrations_affine);
  2673. return 0;
  2674. }
  2675. *all_pinned = 0;
  2676. if (task_running(rq, p)) {
  2677. schedstat_inc(p, se.nr_failed_migrations_running);
  2678. return 0;
  2679. }
  2680. /*
  2681. * Aggressive migration if:
  2682. * 1) task is cache cold, or
  2683. * 2) too many balance attempts have failed.
  2684. */
  2685. tsk_cache_hot = task_hot(p, rq->clock, sd);
  2686. if (!tsk_cache_hot ||
  2687. sd->nr_balance_failed > sd->cache_nice_tries) {
  2688. #ifdef CONFIG_SCHEDSTATS
  2689. if (tsk_cache_hot) {
  2690. schedstat_inc(sd, lb_hot_gained[idle]);
  2691. schedstat_inc(p, se.nr_forced_migrations);
  2692. }
  2693. #endif
  2694. return 1;
  2695. }
  2696. if (tsk_cache_hot) {
  2697. schedstat_inc(p, se.nr_failed_migrations_hot);
  2698. return 0;
  2699. }
  2700. return 1;
  2701. }
  2702. static unsigned long
  2703. balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2704. unsigned long max_load_move, struct sched_domain *sd,
  2705. enum cpu_idle_type idle, int *all_pinned,
  2706. int *this_best_prio, struct rq_iterator *iterator)
  2707. {
  2708. int loops = 0, pulled = 0, pinned = 0;
  2709. struct task_struct *p;
  2710. long rem_load_move = max_load_move;
  2711. if (max_load_move == 0)
  2712. goto out;
  2713. pinned = 1;
  2714. /*
  2715. * Start the load-balancing iterator:
  2716. */
  2717. p = iterator->start(iterator->arg);
  2718. next:
  2719. if (!p || loops++ > sysctl_sched_nr_migrate)
  2720. goto out;
  2721. if ((p->se.load.weight >> 1) > rem_load_move ||
  2722. !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
  2723. p = iterator->next(iterator->arg);
  2724. goto next;
  2725. }
  2726. pull_task(busiest, p, this_rq, this_cpu);
  2727. pulled++;
  2728. rem_load_move -= p->se.load.weight;
  2729. #ifdef CONFIG_PREEMPT
  2730. /*
  2731. * NEWIDLE balancing is a source of latency, so preemptible kernels
  2732. * will stop after the first task is pulled to minimize the critical
  2733. * section.
  2734. */
  2735. if (idle == CPU_NEWLY_IDLE)
  2736. goto out;
  2737. #endif
  2738. /*
  2739. * We only want to steal up to the prescribed amount of weighted load.
  2740. */
  2741. if (rem_load_move > 0) {
  2742. if (p->prio < *this_best_prio)
  2743. *this_best_prio = p->prio;
  2744. p = iterator->next(iterator->arg);
  2745. goto next;
  2746. }
  2747. out:
  2748. /*
  2749. * Right now, this is one of only two places pull_task() is called,
  2750. * so we can safely collect pull_task() stats here rather than
  2751. * inside pull_task().
  2752. */
  2753. schedstat_add(sd, lb_gained[idle], pulled);
  2754. if (all_pinned)
  2755. *all_pinned = pinned;
  2756. return max_load_move - rem_load_move;
  2757. }
  2758. /*
  2759. * move_tasks tries to move up to max_load_move weighted load from busiest to
  2760. * this_rq, as part of a balancing operation within domain "sd".
  2761. * Returns 1 if successful and 0 otherwise.
  2762. *
  2763. * Called with both runqueues locked.
  2764. */
  2765. static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2766. unsigned long max_load_move,
  2767. struct sched_domain *sd, enum cpu_idle_type idle,
  2768. int *all_pinned)
  2769. {
  2770. const struct sched_class *class = sched_class_highest;
  2771. unsigned long total_load_moved = 0;
  2772. int this_best_prio = this_rq->curr->prio;
  2773. do {
  2774. total_load_moved +=
  2775. class->load_balance(this_rq, this_cpu, busiest,
  2776. max_load_move - total_load_moved,
  2777. sd, idle, all_pinned, &this_best_prio);
  2778. class = class->next;
  2779. #ifdef CONFIG_PREEMPT
  2780. /*
  2781. * NEWIDLE balancing is a source of latency, so preemptible
  2782. * kernels will stop after the first task is pulled to minimize
  2783. * the critical section.
  2784. */
  2785. if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
  2786. break;
  2787. #endif
  2788. } while (class && max_load_move > total_load_moved);
  2789. return total_load_moved > 0;
  2790. }
  2791. static int
  2792. iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2793. struct sched_domain *sd, enum cpu_idle_type idle,
  2794. struct rq_iterator *iterator)
  2795. {
  2796. struct task_struct *p = iterator->start(iterator->arg);
  2797. int pinned = 0;
  2798. while (p) {
  2799. if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
  2800. pull_task(busiest, p, this_rq, this_cpu);
  2801. /*
  2802. * Right now, this is only the second place pull_task()
  2803. * is called, so we can safely collect pull_task()
  2804. * stats here rather than inside pull_task().
  2805. */
  2806. schedstat_inc(sd, lb_gained[idle]);
  2807. return 1;
  2808. }
  2809. p = iterator->next(iterator->arg);
  2810. }
  2811. return 0;
  2812. }
  2813. /*
  2814. * move_one_task tries to move exactly one task from busiest to this_rq, as
  2815. * part of active balancing operations within "domain".
  2816. * Returns 1 if successful and 0 otherwise.
  2817. *
  2818. * Called with both runqueues locked.
  2819. */
  2820. static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2821. struct sched_domain *sd, enum cpu_idle_type idle)
  2822. {
  2823. const struct sched_class *class;
  2824. for_each_class(class) {
  2825. if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
  2826. return 1;
  2827. }
  2828. return 0;
  2829. }
  2830. /********** Helpers for find_busiest_group ************************/
  2831. /*
  2832. * sd_lb_stats - Structure to store the statistics of a sched_domain
  2833. * during load balancing.
  2834. */
  2835. struct sd_lb_stats {
  2836. struct sched_group *busiest; /* Busiest group in this sd */
  2837. struct sched_group *this; /* Local group in this sd */
  2838. unsigned long total_load; /* Total load of all groups in sd */
  2839. unsigned long total_pwr; /* Total power of all groups in sd */
  2840. unsigned long avg_load; /* Average load across all groups in sd */
  2841. /** Statistics of this group */
  2842. unsigned long this_load;
  2843. unsigned long this_load_per_task;
  2844. unsigned long this_nr_running;
  2845. /* Statistics of the busiest group */
  2846. unsigned long max_load;
  2847. unsigned long busiest_load_per_task;
  2848. unsigned long busiest_nr_running;
  2849. int group_imb; /* Is there imbalance in this sd */
  2850. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2851. int power_savings_balance; /* Is powersave balance needed for this sd */
  2852. struct sched_group *group_min; /* Least loaded group in sd */
  2853. struct sched_group *group_leader; /* Group which relieves group_min */
  2854. unsigned long min_load_per_task; /* load_per_task in group_min */
  2855. unsigned long leader_nr_running; /* Nr running of group_leader */
  2856. unsigned long min_nr_running; /* Nr running of group_min */
  2857. #endif
  2858. };
  2859. /*
  2860. * sg_lb_stats - stats of a sched_group required for load_balancing
  2861. */
  2862. struct sg_lb_stats {
  2863. unsigned long avg_load; /*Avg load across the CPUs of the group */
  2864. unsigned long group_load; /* Total load over the CPUs of the group */
  2865. unsigned long sum_nr_running; /* Nr tasks running in the group */
  2866. unsigned long sum_weighted_load; /* Weighted load of group's tasks */
  2867. unsigned long group_capacity;
  2868. int group_imb; /* Is there an imbalance in the group ? */
  2869. };
  2870. /**
  2871. * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
  2872. * @group: The group whose first cpu is to be returned.
  2873. */
  2874. static inline unsigned int group_first_cpu(struct sched_group *group)
  2875. {
  2876. return cpumask_first(sched_group_cpus(group));
  2877. }
  2878. /**
  2879. * get_sd_load_idx - Obtain the load index for a given sched domain.
  2880. * @sd: The sched_domain whose load_idx is to be obtained.
  2881. * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
  2882. */
  2883. static inline int get_sd_load_idx(struct sched_domain *sd,
  2884. enum cpu_idle_type idle)
  2885. {
  2886. int load_idx;
  2887. switch (idle) {
  2888. case CPU_NOT_IDLE:
  2889. load_idx = sd->busy_idx;
  2890. break;
  2891. case CPU_NEWLY_IDLE:
  2892. load_idx = sd->newidle_idx;
  2893. break;
  2894. default:
  2895. load_idx = sd->idle_idx;
  2896. break;
  2897. }
  2898. return load_idx;
  2899. }
  2900. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2901. /**
  2902. * init_sd_power_savings_stats - Initialize power savings statistics for
  2903. * the given sched_domain, during load balancing.
  2904. *
  2905. * @sd: Sched domain whose power-savings statistics are to be initialized.
  2906. * @sds: Variable containing the statistics for sd.
  2907. * @idle: Idle status of the CPU at which we're performing load-balancing.
  2908. */
  2909. static inline void init_sd_power_savings_stats(struct sched_domain *sd,
  2910. struct sd_lb_stats *sds, enum cpu_idle_type idle)
  2911. {
  2912. /*
  2913. * Busy processors will not participate in power savings
  2914. * balance.
  2915. */
  2916. if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
  2917. sds->power_savings_balance = 0;
  2918. else {
  2919. sds->power_savings_balance = 1;
  2920. sds->min_nr_running = ULONG_MAX;
  2921. sds->leader_nr_running = 0;
  2922. }
  2923. }
  2924. /**
  2925. * update_sd_power_savings_stats - Update the power saving stats for a
  2926. * sched_domain while performing load balancing.
  2927. *
  2928. * @group: sched_group belonging to the sched_domain under consideration.
  2929. * @sds: Variable containing the statistics of the sched_domain
  2930. * @local_group: Does group contain the CPU for which we're performing
  2931. * load balancing ?
  2932. * @sgs: Variable containing the statistics of the group.
  2933. */
  2934. static inline void update_sd_power_savings_stats(struct sched_group *group,
  2935. struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
  2936. {
  2937. if (!sds->power_savings_balance)
  2938. return;
  2939. /*
  2940. * If the local group is idle or completely loaded
  2941. * no need to do power savings balance at this domain
  2942. */
  2943. if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
  2944. !sds->this_nr_running))
  2945. sds->power_savings_balance = 0;
  2946. /*
  2947. * If a group is already running at full capacity or idle,
  2948. * don't include that group in power savings calculations
  2949. */
  2950. if (!sds->power_savings_balance ||
  2951. sgs->sum_nr_running >= sgs->group_capacity ||
  2952. !sgs->sum_nr_running)
  2953. return;
  2954. /*
  2955. * Calculate the group which has the least non-idle load.
  2956. * This is the group from where we need to pick up the load
  2957. * for saving power
  2958. */
  2959. if ((sgs->sum_nr_running < sds->min_nr_running) ||
  2960. (sgs->sum_nr_running == sds->min_nr_running &&
  2961. group_first_cpu(group) > group_first_cpu(sds->group_min))) {
  2962. sds->group_min = group;
  2963. sds->min_nr_running = sgs->sum_nr_running;
  2964. sds->min_load_per_task = sgs->sum_weighted_load /
  2965. sgs->sum_nr_running;
  2966. }
  2967. /*
  2968. * Calculate the group which is almost near its
  2969. * capacity but still has some space to pick up some load
  2970. * from other group and save more power
  2971. */
  2972. if (sgs->sum_nr_running + 1 > sgs->group_capacity)
  2973. return;
  2974. if (sgs->sum_nr_running > sds->leader_nr_running ||
  2975. (sgs->sum_nr_running == sds->leader_nr_running &&
  2976. group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
  2977. sds->group_leader = group;
  2978. sds->leader_nr_running = sgs->sum_nr_running;
  2979. }
  2980. }
  2981. /**
  2982. * check_power_save_busiest_group - see if there is potential for some power-savings balance
  2983. * @sds: Variable containing the statistics of the sched_domain
  2984. * under consideration.
  2985. * @this_cpu: Cpu at which we're currently performing load-balancing.
  2986. * @imbalance: Variable to store the imbalance.
  2987. *
  2988. * Description:
  2989. * Check if we have potential to perform some power-savings balance.
  2990. * If yes, set the busiest group to be the least loaded group in the
  2991. * sched_domain, so that it's CPUs can be put to idle.
  2992. *
  2993. * Returns 1 if there is potential to perform power-savings balance.
  2994. * Else returns 0.
  2995. */
  2996. static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
  2997. int this_cpu, unsigned long *imbalance)
  2998. {
  2999. if (!sds->power_savings_balance)
  3000. return 0;
  3001. if (sds->this != sds->group_leader ||
  3002. sds->group_leader == sds->group_min)
  3003. return 0;
  3004. *imbalance = sds->min_load_per_task;
  3005. sds->busiest = sds->group_min;
  3006. if (sched_mc_power_savings >= POWERSAVINGS_BALANCE_WAKEUP) {
  3007. cpu_rq(this_cpu)->rd->sched_mc_preferred_wakeup_cpu =
  3008. group_first_cpu(sds->group_leader);
  3009. }
  3010. return 1;
  3011. }
  3012. #else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
  3013. static inline void init_sd_power_savings_stats(struct sched_domain *sd,
  3014. struct sd_lb_stats *sds, enum cpu_idle_type idle)
  3015. {
  3016. return;
  3017. }
  3018. static inline void update_sd_power_savings_stats(struct sched_group *group,
  3019. struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
  3020. {
  3021. return;
  3022. }
  3023. static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
  3024. int this_cpu, unsigned long *imbalance)
  3025. {
  3026. return 0;
  3027. }
  3028. #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
  3029. unsigned long __weak arch_scale_smt_power(struct sched_domain *sd, int cpu)
  3030. {
  3031. unsigned long weight = cpumask_weight(sched_domain_span(sd));
  3032. unsigned long smt_gain = sd->smt_gain;
  3033. smt_gain /= weight;
  3034. return smt_gain;
  3035. }
  3036. unsigned long scale_rt_power(int cpu)
  3037. {
  3038. struct rq *rq = cpu_rq(cpu);
  3039. u64 total, available;
  3040. sched_avg_update(rq);
  3041. total = sched_avg_period() + (rq->clock - rq->age_stamp);
  3042. available = total - rq->rt_avg;
  3043. if (unlikely((s64)total < SCHED_LOAD_SCALE))
  3044. total = SCHED_LOAD_SCALE;
  3045. total >>= SCHED_LOAD_SHIFT;
  3046. return div_u64(available, total);
  3047. }
  3048. static void update_cpu_power(struct sched_domain *sd, int cpu)
  3049. {
  3050. unsigned long weight = cpumask_weight(sched_domain_span(sd));
  3051. unsigned long power = SCHED_LOAD_SCALE;
  3052. struct sched_group *sdg = sd->groups;
  3053. /* here we could scale based on cpufreq */
  3054. if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
  3055. power *= arch_scale_smt_power(sd, cpu);
  3056. power >>= SCHED_LOAD_SHIFT;
  3057. }
  3058. power *= scale_rt_power(cpu);
  3059. power >>= SCHED_LOAD_SHIFT;
  3060. if (!power)
  3061. power = 1;
  3062. sdg->cpu_power = power;
  3063. }
  3064. static void update_group_power(struct sched_domain *sd, int cpu)
  3065. {
  3066. struct sched_domain *child = sd->child;
  3067. struct sched_group *group, *sdg = sd->groups;
  3068. unsigned long power;
  3069. if (!child) {
  3070. update_cpu_power(sd, cpu);
  3071. return;
  3072. }
  3073. power = 0;
  3074. group = child->groups;
  3075. do {
  3076. power += group->cpu_power;
  3077. group = group->next;
  3078. } while (group != child->groups);
  3079. sdg->cpu_power = power;
  3080. }
  3081. /**
  3082. * update_sg_lb_stats - Update sched_group's statistics for load balancing.
  3083. * @group: sched_group whose statistics are to be updated.
  3084. * @this_cpu: Cpu for which load balance is currently performed.
  3085. * @idle: Idle status of this_cpu
  3086. * @load_idx: Load index of sched_domain of this_cpu for load calc.
  3087. * @sd_idle: Idle status of the sched_domain containing group.
  3088. * @local_group: Does group contain this_cpu.
  3089. * @cpus: Set of cpus considered for load balancing.
  3090. * @balance: Should we balance.
  3091. * @sgs: variable to hold the statistics for this group.
  3092. */
  3093. static inline void update_sg_lb_stats(struct sched_domain *sd,
  3094. struct sched_group *group, int this_cpu,
  3095. enum cpu_idle_type idle, int load_idx, int *sd_idle,
  3096. int local_group, const struct cpumask *cpus,
  3097. int *balance, struct sg_lb_stats *sgs)
  3098. {
  3099. unsigned long load, max_cpu_load, min_cpu_load;
  3100. int i;
  3101. unsigned int balance_cpu = -1, first_idle_cpu = 0;
  3102. unsigned long sum_avg_load_per_task;
  3103. unsigned long avg_load_per_task;
  3104. if (local_group) {
  3105. balance_cpu = group_first_cpu(group);
  3106. if (balance_cpu == this_cpu)
  3107. update_group_power(sd, this_cpu);
  3108. }
  3109. /* Tally up the load of all CPUs in the group */
  3110. sum_avg_load_per_task = avg_load_per_task = 0;
  3111. max_cpu_load = 0;
  3112. min_cpu_load = ~0UL;
  3113. for_each_cpu_and(i, sched_group_cpus(group), cpus) {
  3114. struct rq *rq = cpu_rq(i);
  3115. if (*sd_idle && rq->nr_running)
  3116. *sd_idle = 0;
  3117. /* Bias balancing toward cpus of our domain */
  3118. if (local_group) {
  3119. if (idle_cpu(i) && !first_idle_cpu) {
  3120. first_idle_cpu = 1;
  3121. balance_cpu = i;
  3122. }
  3123. load = target_load(i, load_idx);
  3124. } else {
  3125. load = source_load(i, load_idx);
  3126. if (load > max_cpu_load)
  3127. max_cpu_load = load;
  3128. if (min_cpu_load > load)
  3129. min_cpu_load = load;
  3130. }
  3131. sgs->group_load += load;
  3132. sgs->sum_nr_running += rq->nr_running;
  3133. sgs->sum_weighted_load += weighted_cpuload(i);
  3134. sum_avg_load_per_task += cpu_avg_load_per_task(i);
  3135. }
  3136. /*
  3137. * First idle cpu or the first cpu(busiest) in this sched group
  3138. * is eligible for doing load balancing at this and above
  3139. * domains. In the newly idle case, we will allow all the cpu's
  3140. * to do the newly idle load balance.
  3141. */
  3142. if (idle != CPU_NEWLY_IDLE && local_group &&
  3143. balance_cpu != this_cpu && balance) {
  3144. *balance = 0;
  3145. return;
  3146. }
  3147. /* Adjust by relative CPU power of the group */
  3148. sgs->avg_load = (sgs->group_load * SCHED_LOAD_SCALE) / group->cpu_power;
  3149. /*
  3150. * Consider the group unbalanced when the imbalance is larger
  3151. * than the average weight of two tasks.
  3152. *
  3153. * APZ: with cgroup the avg task weight can vary wildly and
  3154. * might not be a suitable number - should we keep a
  3155. * normalized nr_running number somewhere that negates
  3156. * the hierarchy?
  3157. */
  3158. avg_load_per_task = (sum_avg_load_per_task * SCHED_LOAD_SCALE) /
  3159. group->cpu_power;
  3160. if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
  3161. sgs->group_imb = 1;
  3162. sgs->group_capacity =
  3163. DIV_ROUND_CLOSEST(group->cpu_power, SCHED_LOAD_SCALE);
  3164. }
  3165. /**
  3166. * update_sd_lb_stats - Update sched_group's statistics for load balancing.
  3167. * @sd: sched_domain whose statistics are to be updated.
  3168. * @this_cpu: Cpu for which load balance is currently performed.
  3169. * @idle: Idle status of this_cpu
  3170. * @sd_idle: Idle status of the sched_domain containing group.
  3171. * @cpus: Set of cpus considered for load balancing.
  3172. * @balance: Should we balance.
  3173. * @sds: variable to hold the statistics for this sched_domain.
  3174. */
  3175. static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
  3176. enum cpu_idle_type idle, int *sd_idle,
  3177. const struct cpumask *cpus, int *balance,
  3178. struct sd_lb_stats *sds)
  3179. {
  3180. struct sched_domain *child = sd->child;
  3181. struct sched_group *group = sd->groups;
  3182. struct sg_lb_stats sgs;
  3183. int load_idx, prefer_sibling = 0;
  3184. if (child && child->flags & SD_PREFER_SIBLING)
  3185. prefer_sibling = 1;
  3186. init_sd_power_savings_stats(sd, sds, idle);
  3187. load_idx = get_sd_load_idx(sd, idle);
  3188. do {
  3189. int local_group;
  3190. local_group = cpumask_test_cpu(this_cpu,
  3191. sched_group_cpus(group));
  3192. memset(&sgs, 0, sizeof(sgs));
  3193. update_sg_lb_stats(sd, group, this_cpu, idle, load_idx, sd_idle,
  3194. local_group, cpus, balance, &sgs);
  3195. if (local_group && balance && !(*balance))
  3196. return;
  3197. sds->total_load += sgs.group_load;
  3198. sds->total_pwr += group->cpu_power;
  3199. /*
  3200. * In case the child domain prefers tasks go to siblings
  3201. * first, lower the group capacity to one so that we'll try
  3202. * and move all the excess tasks away.
  3203. */
  3204. if (prefer_sibling)
  3205. sgs.group_capacity = min(sgs.group_capacity, 1UL);
  3206. if (local_group) {
  3207. sds->this_load = sgs.avg_load;
  3208. sds->this = group;
  3209. sds->this_nr_running = sgs.sum_nr_running;
  3210. sds->this_load_per_task = sgs.sum_weighted_load;
  3211. } else if (sgs.avg_load > sds->max_load &&
  3212. (sgs.sum_nr_running > sgs.group_capacity ||
  3213. sgs.group_imb)) {
  3214. sds->max_load = sgs.avg_load;
  3215. sds->busiest = group;
  3216. sds->busiest_nr_running = sgs.sum_nr_running;
  3217. sds->busiest_load_per_task = sgs.sum_weighted_load;
  3218. sds->group_imb = sgs.group_imb;
  3219. }
  3220. update_sd_power_savings_stats(group, sds, local_group, &sgs);
  3221. group = group->next;
  3222. } while (group != sd->groups);
  3223. }
  3224. /**
  3225. * fix_small_imbalance - Calculate the minor imbalance that exists
  3226. * amongst the groups of a sched_domain, during
  3227. * load balancing.
  3228. * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
  3229. * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
  3230. * @imbalance: Variable to store the imbalance.
  3231. */
  3232. static inline void fix_small_imbalance(struct sd_lb_stats *sds,
  3233. int this_cpu, unsigned long *imbalance)
  3234. {
  3235. unsigned long tmp, pwr_now = 0, pwr_move = 0;
  3236. unsigned int imbn = 2;
  3237. if (sds->this_nr_running) {
  3238. sds->this_load_per_task /= sds->this_nr_running;
  3239. if (sds->busiest_load_per_task >
  3240. sds->this_load_per_task)
  3241. imbn = 1;
  3242. } else
  3243. sds->this_load_per_task =
  3244. cpu_avg_load_per_task(this_cpu);
  3245. if (sds->max_load - sds->this_load + sds->busiest_load_per_task >=
  3246. sds->busiest_load_per_task * imbn) {
  3247. *imbalance = sds->busiest_load_per_task;
  3248. return;
  3249. }
  3250. /*
  3251. * OK, we don't have enough imbalance to justify moving tasks,
  3252. * however we may be able to increase total CPU power used by
  3253. * moving them.
  3254. */
  3255. pwr_now += sds->busiest->cpu_power *
  3256. min(sds->busiest_load_per_task, sds->max_load);
  3257. pwr_now += sds->this->cpu_power *
  3258. min(sds->this_load_per_task, sds->this_load);
  3259. pwr_now /= SCHED_LOAD_SCALE;
  3260. /* Amount of load we'd subtract */
  3261. tmp = (sds->busiest_load_per_task * SCHED_LOAD_SCALE) /
  3262. sds->busiest->cpu_power;
  3263. if (sds->max_load > tmp)
  3264. pwr_move += sds->busiest->cpu_power *
  3265. min(sds->busiest_load_per_task, sds->max_load - tmp);
  3266. /* Amount of load we'd add */
  3267. if (sds->max_load * sds->busiest->cpu_power <
  3268. sds->busiest_load_per_task * SCHED_LOAD_SCALE)
  3269. tmp = (sds->max_load * sds->busiest->cpu_power) /
  3270. sds->this->cpu_power;
  3271. else
  3272. tmp = (sds->busiest_load_per_task * SCHED_LOAD_SCALE) /
  3273. sds->this->cpu_power;
  3274. pwr_move += sds->this->cpu_power *
  3275. min(sds->this_load_per_task, sds->this_load + tmp);
  3276. pwr_move /= SCHED_LOAD_SCALE;
  3277. /* Move if we gain throughput */
  3278. if (pwr_move > pwr_now)
  3279. *imbalance = sds->busiest_load_per_task;
  3280. }
  3281. /**
  3282. * calculate_imbalance - Calculate the amount of imbalance present within the
  3283. * groups of a given sched_domain during load balance.
  3284. * @sds: statistics of the sched_domain whose imbalance is to be calculated.
  3285. * @this_cpu: Cpu for which currently load balance is being performed.
  3286. * @imbalance: The variable to store the imbalance.
  3287. */
  3288. static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
  3289. unsigned long *imbalance)
  3290. {
  3291. unsigned long max_pull;
  3292. /*
  3293. * In the presence of smp nice balancing, certain scenarios can have
  3294. * max load less than avg load(as we skip the groups at or below
  3295. * its cpu_power, while calculating max_load..)
  3296. */
  3297. if (sds->max_load < sds->avg_load) {
  3298. *imbalance = 0;
  3299. return fix_small_imbalance(sds, this_cpu, imbalance);
  3300. }
  3301. /* Don't want to pull so many tasks that a group would go idle */
  3302. max_pull = min(sds->max_load - sds->avg_load,
  3303. sds->max_load - sds->busiest_load_per_task);
  3304. /* How much load to actually move to equalise the imbalance */
  3305. *imbalance = min(max_pull * sds->busiest->cpu_power,
  3306. (sds->avg_load - sds->this_load) * sds->this->cpu_power)
  3307. / SCHED_LOAD_SCALE;
  3308. /*
  3309. * if *imbalance is less than the average load per runnable task
  3310. * there is no gaurantee that any tasks will be moved so we'll have
  3311. * a think about bumping its value to force at least one task to be
  3312. * moved
  3313. */
  3314. if (*imbalance < sds->busiest_load_per_task)
  3315. return fix_small_imbalance(sds, this_cpu, imbalance);
  3316. }
  3317. /******* find_busiest_group() helpers end here *********************/
  3318. /**
  3319. * find_busiest_group - Returns the busiest group within the sched_domain
  3320. * if there is an imbalance. If there isn't an imbalance, and
  3321. * the user has opted for power-savings, it returns a group whose
  3322. * CPUs can be put to idle by rebalancing those tasks elsewhere, if
  3323. * such a group exists.
  3324. *
  3325. * Also calculates the amount of weighted load which should be moved
  3326. * to restore balance.
  3327. *
  3328. * @sd: The sched_domain whose busiest group is to be returned.
  3329. * @this_cpu: The cpu for which load balancing is currently being performed.
  3330. * @imbalance: Variable which stores amount of weighted load which should
  3331. * be moved to restore balance/put a group to idle.
  3332. * @idle: The idle status of this_cpu.
  3333. * @sd_idle: The idleness of sd
  3334. * @cpus: The set of CPUs under consideration for load-balancing.
  3335. * @balance: Pointer to a variable indicating if this_cpu
  3336. * is the appropriate cpu to perform load balancing at this_level.
  3337. *
  3338. * Returns: - the busiest group if imbalance exists.
  3339. * - If no imbalance and user has opted for power-savings balance,
  3340. * return the least loaded group whose CPUs can be
  3341. * put to idle by rebalancing its tasks onto our group.
  3342. */
  3343. static struct sched_group *
  3344. find_busiest_group(struct sched_domain *sd, int this_cpu,
  3345. unsigned long *imbalance, enum cpu_idle_type idle,
  3346. int *sd_idle, const struct cpumask *cpus, int *balance)
  3347. {
  3348. struct sd_lb_stats sds;
  3349. memset(&sds, 0, sizeof(sds));
  3350. /*
  3351. * Compute the various statistics relavent for load balancing at
  3352. * this level.
  3353. */
  3354. update_sd_lb_stats(sd, this_cpu, idle, sd_idle, cpus,
  3355. balance, &sds);
  3356. /* Cases where imbalance does not exist from POV of this_cpu */
  3357. /* 1) this_cpu is not the appropriate cpu to perform load balancing
  3358. * at this level.
  3359. * 2) There is no busy sibling group to pull from.
  3360. * 3) This group is the busiest group.
  3361. * 4) This group is more busy than the avg busieness at this
  3362. * sched_domain.
  3363. * 5) The imbalance is within the specified limit.
  3364. * 6) Any rebalance would lead to ping-pong
  3365. */
  3366. if (balance && !(*balance))
  3367. goto ret;
  3368. if (!sds.busiest || sds.busiest_nr_running == 0)
  3369. goto out_balanced;
  3370. if (sds.this_load >= sds.max_load)
  3371. goto out_balanced;
  3372. sds.avg_load = (SCHED_LOAD_SCALE * sds.total_load) / sds.total_pwr;
  3373. if (sds.this_load >= sds.avg_load)
  3374. goto out_balanced;
  3375. if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
  3376. goto out_balanced;
  3377. sds.busiest_load_per_task /= sds.busiest_nr_running;
  3378. if (sds.group_imb)
  3379. sds.busiest_load_per_task =
  3380. min(sds.busiest_load_per_task, sds.avg_load);
  3381. /*
  3382. * We're trying to get all the cpus to the average_load, so we don't
  3383. * want to push ourselves above the average load, nor do we wish to
  3384. * reduce the max loaded cpu below the average load, as either of these
  3385. * actions would just result in more rebalancing later, and ping-pong
  3386. * tasks around. Thus we look for the minimum possible imbalance.
  3387. * Negative imbalances (*we* are more loaded than anyone else) will
  3388. * be counted as no imbalance for these purposes -- we can't fix that
  3389. * by pulling tasks to us. Be careful of negative numbers as they'll
  3390. * appear as very large values with unsigned longs.
  3391. */
  3392. if (sds.max_load <= sds.busiest_load_per_task)
  3393. goto out_balanced;
  3394. /* Looks like there is an imbalance. Compute it */
  3395. calculate_imbalance(&sds, this_cpu, imbalance);
  3396. return sds.busiest;
  3397. out_balanced:
  3398. /*
  3399. * There is no obvious imbalance. But check if we can do some balancing
  3400. * to save power.
  3401. */
  3402. if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
  3403. return sds.busiest;
  3404. ret:
  3405. *imbalance = 0;
  3406. return NULL;
  3407. }
  3408. static struct sched_group *group_of(int cpu)
  3409. {
  3410. struct sched_domain *sd = rcu_dereference(cpu_rq(cpu)->sd);
  3411. if (!sd)
  3412. return NULL;
  3413. return sd->groups;
  3414. }
  3415. static unsigned long power_of(int cpu)
  3416. {
  3417. struct sched_group *group = group_of(cpu);
  3418. if (!group)
  3419. return SCHED_LOAD_SCALE;
  3420. return group->cpu_power;
  3421. }
  3422. /*
  3423. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  3424. */
  3425. static struct rq *
  3426. find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
  3427. unsigned long imbalance, const struct cpumask *cpus)
  3428. {
  3429. struct rq *busiest = NULL, *rq;
  3430. unsigned long max_load = 0;
  3431. int i;
  3432. for_each_cpu(i, sched_group_cpus(group)) {
  3433. unsigned long power = power_of(i);
  3434. unsigned long capacity = DIV_ROUND_CLOSEST(power, SCHED_LOAD_SCALE);
  3435. unsigned long wl;
  3436. if (!cpumask_test_cpu(i, cpus))
  3437. continue;
  3438. rq = cpu_rq(i);
  3439. wl = weighted_cpuload(i) * SCHED_LOAD_SCALE;
  3440. wl /= power;
  3441. if (capacity && rq->nr_running == 1 && wl > imbalance)
  3442. continue;
  3443. if (wl > max_load) {
  3444. max_load = wl;
  3445. busiest = rq;
  3446. }
  3447. }
  3448. return busiest;
  3449. }
  3450. /*
  3451. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  3452. * so long as it is large enough.
  3453. */
  3454. #define MAX_PINNED_INTERVAL 512
  3455. /* Working cpumask for load_balance and load_balance_newidle. */
  3456. static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
  3457. /*
  3458. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  3459. * tasks if there is an imbalance.
  3460. */
  3461. static int load_balance(int this_cpu, struct rq *this_rq,
  3462. struct sched_domain *sd, enum cpu_idle_type idle,
  3463. int *balance)
  3464. {
  3465. int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
  3466. struct sched_group *group;
  3467. unsigned long imbalance;
  3468. struct rq *busiest;
  3469. unsigned long flags;
  3470. struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
  3471. cpumask_setall(cpus);
  3472. /*
  3473. * When power savings policy is enabled for the parent domain, idle
  3474. * sibling can pick up load irrespective of busy siblings. In this case,
  3475. * let the state of idle sibling percolate up as CPU_IDLE, instead of
  3476. * portraying it as CPU_NOT_IDLE.
  3477. */
  3478. if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
  3479. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3480. sd_idle = 1;
  3481. schedstat_inc(sd, lb_count[idle]);
  3482. redo:
  3483. update_shares(sd);
  3484. group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
  3485. cpus, balance);
  3486. if (*balance == 0)
  3487. goto out_balanced;
  3488. if (!group) {
  3489. schedstat_inc(sd, lb_nobusyg[idle]);
  3490. goto out_balanced;
  3491. }
  3492. busiest = find_busiest_queue(group, idle, imbalance, cpus);
  3493. if (!busiest) {
  3494. schedstat_inc(sd, lb_nobusyq[idle]);
  3495. goto out_balanced;
  3496. }
  3497. BUG_ON(busiest == this_rq);
  3498. schedstat_add(sd, lb_imbalance[idle], imbalance);
  3499. ld_moved = 0;
  3500. if (busiest->nr_running > 1) {
  3501. /*
  3502. * Attempt to move tasks. If find_busiest_group has found
  3503. * an imbalance but busiest->nr_running <= 1, the group is
  3504. * still unbalanced. ld_moved simply stays zero, so it is
  3505. * correctly treated as an imbalance.
  3506. */
  3507. local_irq_save(flags);
  3508. double_rq_lock(this_rq, busiest);
  3509. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  3510. imbalance, sd, idle, &all_pinned);
  3511. double_rq_unlock(this_rq, busiest);
  3512. local_irq_restore(flags);
  3513. /*
  3514. * some other cpu did the load balance for us.
  3515. */
  3516. if (ld_moved && this_cpu != smp_processor_id())
  3517. resched_cpu(this_cpu);
  3518. /* All tasks on this runqueue were pinned by CPU affinity */
  3519. if (unlikely(all_pinned)) {
  3520. cpumask_clear_cpu(cpu_of(busiest), cpus);
  3521. if (!cpumask_empty(cpus))
  3522. goto redo;
  3523. goto out_balanced;
  3524. }
  3525. }
  3526. if (!ld_moved) {
  3527. schedstat_inc(sd, lb_failed[idle]);
  3528. sd->nr_balance_failed++;
  3529. if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
  3530. spin_lock_irqsave(&busiest->lock, flags);
  3531. /* don't kick the migration_thread, if the curr
  3532. * task on busiest cpu can't be moved to this_cpu
  3533. */
  3534. if (!cpumask_test_cpu(this_cpu,
  3535. &busiest->curr->cpus_allowed)) {
  3536. spin_unlock_irqrestore(&busiest->lock, flags);
  3537. all_pinned = 1;
  3538. goto out_one_pinned;
  3539. }
  3540. if (!busiest->active_balance) {
  3541. busiest->active_balance = 1;
  3542. busiest->push_cpu = this_cpu;
  3543. active_balance = 1;
  3544. }
  3545. spin_unlock_irqrestore(&busiest->lock, flags);
  3546. if (active_balance)
  3547. wake_up_process(busiest->migration_thread);
  3548. /*
  3549. * We've kicked active balancing, reset the failure
  3550. * counter.
  3551. */
  3552. sd->nr_balance_failed = sd->cache_nice_tries+1;
  3553. }
  3554. } else
  3555. sd->nr_balance_failed = 0;
  3556. if (likely(!active_balance)) {
  3557. /* We were unbalanced, so reset the balancing interval */
  3558. sd->balance_interval = sd->min_interval;
  3559. } else {
  3560. /*
  3561. * If we've begun active balancing, start to back off. This
  3562. * case may not be covered by the all_pinned logic if there
  3563. * is only 1 task on the busy runqueue (because we don't call
  3564. * move_tasks).
  3565. */
  3566. if (sd->balance_interval < sd->max_interval)
  3567. sd->balance_interval *= 2;
  3568. }
  3569. if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3570. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3571. ld_moved = -1;
  3572. goto out;
  3573. out_balanced:
  3574. schedstat_inc(sd, lb_balanced[idle]);
  3575. sd->nr_balance_failed = 0;
  3576. out_one_pinned:
  3577. /* tune up the balancing interval */
  3578. if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
  3579. (sd->balance_interval < sd->max_interval))
  3580. sd->balance_interval *= 2;
  3581. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3582. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3583. ld_moved = -1;
  3584. else
  3585. ld_moved = 0;
  3586. out:
  3587. if (ld_moved)
  3588. update_shares(sd);
  3589. return ld_moved;
  3590. }
  3591. /*
  3592. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  3593. * tasks if there is an imbalance.
  3594. *
  3595. * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
  3596. * this_rq is locked.
  3597. */
  3598. static int
  3599. load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
  3600. {
  3601. struct sched_group *group;
  3602. struct rq *busiest = NULL;
  3603. unsigned long imbalance;
  3604. int ld_moved = 0;
  3605. int sd_idle = 0;
  3606. int all_pinned = 0;
  3607. struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
  3608. cpumask_setall(cpus);
  3609. /*
  3610. * When power savings policy is enabled for the parent domain, idle
  3611. * sibling can pick up load irrespective of busy siblings. In this case,
  3612. * let the state of idle sibling percolate up as IDLE, instead of
  3613. * portraying it as CPU_NOT_IDLE.
  3614. */
  3615. if (sd->flags & SD_SHARE_CPUPOWER &&
  3616. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3617. sd_idle = 1;
  3618. schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
  3619. redo:
  3620. update_shares_locked(this_rq, sd);
  3621. group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
  3622. &sd_idle, cpus, NULL);
  3623. if (!group) {
  3624. schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
  3625. goto out_balanced;
  3626. }
  3627. busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
  3628. if (!busiest) {
  3629. schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
  3630. goto out_balanced;
  3631. }
  3632. BUG_ON(busiest == this_rq);
  3633. schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
  3634. ld_moved = 0;
  3635. if (busiest->nr_running > 1) {
  3636. /* Attempt to move tasks */
  3637. double_lock_balance(this_rq, busiest);
  3638. /* this_rq->clock is already updated */
  3639. update_rq_clock(busiest);
  3640. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  3641. imbalance, sd, CPU_NEWLY_IDLE,
  3642. &all_pinned);
  3643. double_unlock_balance(this_rq, busiest);
  3644. if (unlikely(all_pinned)) {
  3645. cpumask_clear_cpu(cpu_of(busiest), cpus);
  3646. if (!cpumask_empty(cpus))
  3647. goto redo;
  3648. }
  3649. }
  3650. if (!ld_moved) {
  3651. int active_balance = 0;
  3652. schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
  3653. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3654. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3655. return -1;
  3656. if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
  3657. return -1;
  3658. if (sd->nr_balance_failed++ < 2)
  3659. return -1;
  3660. /*
  3661. * The only task running in a non-idle cpu can be moved to this
  3662. * cpu in an attempt to completely freeup the other CPU
  3663. * package. The same method used to move task in load_balance()
  3664. * have been extended for load_balance_newidle() to speedup
  3665. * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2)
  3666. *
  3667. * The package power saving logic comes from
  3668. * find_busiest_group(). If there are no imbalance, then
  3669. * f_b_g() will return NULL. However when sched_mc={1,2} then
  3670. * f_b_g() will select a group from which a running task may be
  3671. * pulled to this cpu in order to make the other package idle.
  3672. * If there is no opportunity to make a package idle and if
  3673. * there are no imbalance, then f_b_g() will return NULL and no
  3674. * action will be taken in load_balance_newidle().
  3675. *
  3676. * Under normal task pull operation due to imbalance, there
  3677. * will be more than one task in the source run queue and
  3678. * move_tasks() will succeed. ld_moved will be true and this
  3679. * active balance code will not be triggered.
  3680. */
  3681. /* Lock busiest in correct order while this_rq is held */
  3682. double_lock_balance(this_rq, busiest);
  3683. /*
  3684. * don't kick the migration_thread, if the curr
  3685. * task on busiest cpu can't be moved to this_cpu
  3686. */
  3687. if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
  3688. double_unlock_balance(this_rq, busiest);
  3689. all_pinned = 1;
  3690. return ld_moved;
  3691. }
  3692. if (!busiest->active_balance) {
  3693. busiest->active_balance = 1;
  3694. busiest->push_cpu = this_cpu;
  3695. active_balance = 1;
  3696. }
  3697. double_unlock_balance(this_rq, busiest);
  3698. /*
  3699. * Should not call ttwu while holding a rq->lock
  3700. */
  3701. spin_unlock(&this_rq->lock);
  3702. if (active_balance)
  3703. wake_up_process(busiest->migration_thread);
  3704. spin_lock(&this_rq->lock);
  3705. } else
  3706. sd->nr_balance_failed = 0;
  3707. update_shares_locked(this_rq, sd);
  3708. return ld_moved;
  3709. out_balanced:
  3710. schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
  3711. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3712. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3713. return -1;
  3714. sd->nr_balance_failed = 0;
  3715. return 0;
  3716. }
  3717. /*
  3718. * idle_balance is called by schedule() if this_cpu is about to become
  3719. * idle. Attempts to pull tasks from other CPUs.
  3720. */
  3721. static void idle_balance(int this_cpu, struct rq *this_rq)
  3722. {
  3723. struct sched_domain *sd;
  3724. int pulled_task = 0;
  3725. unsigned long next_balance = jiffies + HZ;
  3726. for_each_domain(this_cpu, sd) {
  3727. unsigned long interval;
  3728. if (!(sd->flags & SD_LOAD_BALANCE))
  3729. continue;
  3730. if (sd->flags & SD_BALANCE_NEWIDLE)
  3731. /* If we've pulled tasks over stop searching: */
  3732. pulled_task = load_balance_newidle(this_cpu, this_rq,
  3733. sd);
  3734. interval = msecs_to_jiffies(sd->balance_interval);
  3735. if (time_after(next_balance, sd->last_balance + interval))
  3736. next_balance = sd->last_balance + interval;
  3737. if (pulled_task)
  3738. break;
  3739. }
  3740. if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
  3741. /*
  3742. * We are going idle. next_balance may be set based on
  3743. * a busy processor. So reset next_balance.
  3744. */
  3745. this_rq->next_balance = next_balance;
  3746. }
  3747. }
  3748. /*
  3749. * active_load_balance is run by migration threads. It pushes running tasks
  3750. * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
  3751. * running on each physical CPU where possible, and avoids physical /
  3752. * logical imbalances.
  3753. *
  3754. * Called with busiest_rq locked.
  3755. */
  3756. static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
  3757. {
  3758. int target_cpu = busiest_rq->push_cpu;
  3759. struct sched_domain *sd;
  3760. struct rq *target_rq;
  3761. /* Is there any task to move? */
  3762. if (busiest_rq->nr_running <= 1)
  3763. return;
  3764. target_rq = cpu_rq(target_cpu);
  3765. /*
  3766. * This condition is "impossible", if it occurs
  3767. * we need to fix it. Originally reported by
  3768. * Bjorn Helgaas on a 128-cpu setup.
  3769. */
  3770. BUG_ON(busiest_rq == target_rq);
  3771. /* move a task from busiest_rq to target_rq */
  3772. double_lock_balance(busiest_rq, target_rq);
  3773. update_rq_clock(busiest_rq);
  3774. update_rq_clock(target_rq);
  3775. /* Search for an sd spanning us and the target CPU. */
  3776. for_each_domain(target_cpu, sd) {
  3777. if ((sd->flags & SD_LOAD_BALANCE) &&
  3778. cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
  3779. break;
  3780. }
  3781. if (likely(sd)) {
  3782. schedstat_inc(sd, alb_count);
  3783. if (move_one_task(target_rq, target_cpu, busiest_rq,
  3784. sd, CPU_IDLE))
  3785. schedstat_inc(sd, alb_pushed);
  3786. else
  3787. schedstat_inc(sd, alb_failed);
  3788. }
  3789. double_unlock_balance(busiest_rq, target_rq);
  3790. }
  3791. #ifdef CONFIG_NO_HZ
  3792. static struct {
  3793. atomic_t load_balancer;
  3794. cpumask_var_t cpu_mask;
  3795. cpumask_var_t ilb_grp_nohz_mask;
  3796. } nohz ____cacheline_aligned = {
  3797. .load_balancer = ATOMIC_INIT(-1),
  3798. };
  3799. int get_nohz_load_balancer(void)
  3800. {
  3801. return atomic_read(&nohz.load_balancer);
  3802. }
  3803. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  3804. /**
  3805. * lowest_flag_domain - Return lowest sched_domain containing flag.
  3806. * @cpu: The cpu whose lowest level of sched domain is to
  3807. * be returned.
  3808. * @flag: The flag to check for the lowest sched_domain
  3809. * for the given cpu.
  3810. *
  3811. * Returns the lowest sched_domain of a cpu which contains the given flag.
  3812. */
  3813. static inline struct sched_domain *lowest_flag_domain(int cpu, int flag)
  3814. {
  3815. struct sched_domain *sd;
  3816. for_each_domain(cpu, sd)
  3817. if (sd && (sd->flags & flag))
  3818. break;
  3819. return sd;
  3820. }
  3821. /**
  3822. * for_each_flag_domain - Iterates over sched_domains containing the flag.
  3823. * @cpu: The cpu whose domains we're iterating over.
  3824. * @sd: variable holding the value of the power_savings_sd
  3825. * for cpu.
  3826. * @flag: The flag to filter the sched_domains to be iterated.
  3827. *
  3828. * Iterates over all the scheduler domains for a given cpu that has the 'flag'
  3829. * set, starting from the lowest sched_domain to the highest.
  3830. */
  3831. #define for_each_flag_domain(cpu, sd, flag) \
  3832. for (sd = lowest_flag_domain(cpu, flag); \
  3833. (sd && (sd->flags & flag)); sd = sd->parent)
  3834. /**
  3835. * is_semi_idle_group - Checks if the given sched_group is semi-idle.
  3836. * @ilb_group: group to be checked for semi-idleness
  3837. *
  3838. * Returns: 1 if the group is semi-idle. 0 otherwise.
  3839. *
  3840. * We define a sched_group to be semi idle if it has atleast one idle-CPU
  3841. * and atleast one non-idle CPU. This helper function checks if the given
  3842. * sched_group is semi-idle or not.
  3843. */
  3844. static inline int is_semi_idle_group(struct sched_group *ilb_group)
  3845. {
  3846. cpumask_and(nohz.ilb_grp_nohz_mask, nohz.cpu_mask,
  3847. sched_group_cpus(ilb_group));
  3848. /*
  3849. * A sched_group is semi-idle when it has atleast one busy cpu
  3850. * and atleast one idle cpu.
  3851. */
  3852. if (cpumask_empty(nohz.ilb_grp_nohz_mask))
  3853. return 0;
  3854. if (cpumask_equal(nohz.ilb_grp_nohz_mask, sched_group_cpus(ilb_group)))
  3855. return 0;
  3856. return 1;
  3857. }
  3858. /**
  3859. * find_new_ilb - Finds the optimum idle load balancer for nomination.
  3860. * @cpu: The cpu which is nominating a new idle_load_balancer.
  3861. *
  3862. * Returns: Returns the id of the idle load balancer if it exists,
  3863. * Else, returns >= nr_cpu_ids.
  3864. *
  3865. * This algorithm picks the idle load balancer such that it belongs to a
  3866. * semi-idle powersavings sched_domain. The idea is to try and avoid
  3867. * completely idle packages/cores just for the purpose of idle load balancing
  3868. * when there are other idle cpu's which are better suited for that job.
  3869. */
  3870. static int find_new_ilb(int cpu)
  3871. {
  3872. struct sched_domain *sd;
  3873. struct sched_group *ilb_group;
  3874. /*
  3875. * Have idle load balancer selection from semi-idle packages only
  3876. * when power-aware load balancing is enabled
  3877. */
  3878. if (!(sched_smt_power_savings || sched_mc_power_savings))
  3879. goto out_done;
  3880. /*
  3881. * Optimize for the case when we have no idle CPUs or only one
  3882. * idle CPU. Don't walk the sched_domain hierarchy in such cases
  3883. */
  3884. if (cpumask_weight(nohz.cpu_mask) < 2)
  3885. goto out_done;
  3886. for_each_flag_domain(cpu, sd, SD_POWERSAVINGS_BALANCE) {
  3887. ilb_group = sd->groups;
  3888. do {
  3889. if (is_semi_idle_group(ilb_group))
  3890. return cpumask_first(nohz.ilb_grp_nohz_mask);
  3891. ilb_group = ilb_group->next;
  3892. } while (ilb_group != sd->groups);
  3893. }
  3894. out_done:
  3895. return cpumask_first(nohz.cpu_mask);
  3896. }
  3897. #else /* (CONFIG_SCHED_MC || CONFIG_SCHED_SMT) */
  3898. static inline int find_new_ilb(int call_cpu)
  3899. {
  3900. return cpumask_first(nohz.cpu_mask);
  3901. }
  3902. #endif
  3903. /*
  3904. * This routine will try to nominate the ilb (idle load balancing)
  3905. * owner among the cpus whose ticks are stopped. ilb owner will do the idle
  3906. * load balancing on behalf of all those cpus. If all the cpus in the system
  3907. * go into this tickless mode, then there will be no ilb owner (as there is
  3908. * no need for one) and all the cpus will sleep till the next wakeup event
  3909. * arrives...
  3910. *
  3911. * For the ilb owner, tick is not stopped. And this tick will be used
  3912. * for idle load balancing. ilb owner will still be part of
  3913. * nohz.cpu_mask..
  3914. *
  3915. * While stopping the tick, this cpu will become the ilb owner if there
  3916. * is no other owner. And will be the owner till that cpu becomes busy
  3917. * or if all cpus in the system stop their ticks at which point
  3918. * there is no need for ilb owner.
  3919. *
  3920. * When the ilb owner becomes busy, it nominates another owner, during the
  3921. * next busy scheduler_tick()
  3922. */
  3923. int select_nohz_load_balancer(int stop_tick)
  3924. {
  3925. int cpu = smp_processor_id();
  3926. if (stop_tick) {
  3927. cpu_rq(cpu)->in_nohz_recently = 1;
  3928. if (!cpu_active(cpu)) {
  3929. if (atomic_read(&nohz.load_balancer) != cpu)
  3930. return 0;
  3931. /*
  3932. * If we are going offline and still the leader,
  3933. * give up!
  3934. */
  3935. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  3936. BUG();
  3937. return 0;
  3938. }
  3939. cpumask_set_cpu(cpu, nohz.cpu_mask);
  3940. /* time for ilb owner also to sleep */
  3941. if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
  3942. if (atomic_read(&nohz.load_balancer) == cpu)
  3943. atomic_set(&nohz.load_balancer, -1);
  3944. return 0;
  3945. }
  3946. if (atomic_read(&nohz.load_balancer) == -1) {
  3947. /* make me the ilb owner */
  3948. if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
  3949. return 1;
  3950. } else if (atomic_read(&nohz.load_balancer) == cpu) {
  3951. int new_ilb;
  3952. if (!(sched_smt_power_savings ||
  3953. sched_mc_power_savings))
  3954. return 1;
  3955. /*
  3956. * Check to see if there is a more power-efficient
  3957. * ilb.
  3958. */
  3959. new_ilb = find_new_ilb(cpu);
  3960. if (new_ilb < nr_cpu_ids && new_ilb != cpu) {
  3961. atomic_set(&nohz.load_balancer, -1);
  3962. resched_cpu(new_ilb);
  3963. return 0;
  3964. }
  3965. return 1;
  3966. }
  3967. } else {
  3968. if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
  3969. return 0;
  3970. cpumask_clear_cpu(cpu, nohz.cpu_mask);
  3971. if (atomic_read(&nohz.load_balancer) == cpu)
  3972. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  3973. BUG();
  3974. }
  3975. return 0;
  3976. }
  3977. #endif
  3978. static DEFINE_SPINLOCK(balancing);
  3979. /*
  3980. * It checks each scheduling domain to see if it is due to be balanced,
  3981. * and initiates a balancing operation if so.
  3982. *
  3983. * Balancing parameters are set up in arch_init_sched_domains.
  3984. */
  3985. static void rebalance_domains(int cpu, enum cpu_idle_type idle)
  3986. {
  3987. int balance = 1;
  3988. struct rq *rq = cpu_rq(cpu);
  3989. unsigned long interval;
  3990. struct sched_domain *sd;
  3991. /* Earliest time when we have to do rebalance again */
  3992. unsigned long next_balance = jiffies + 60*HZ;
  3993. int update_next_balance = 0;
  3994. int need_serialize;
  3995. for_each_domain(cpu, sd) {
  3996. if (!(sd->flags & SD_LOAD_BALANCE))
  3997. continue;
  3998. interval = sd->balance_interval;
  3999. if (idle != CPU_IDLE)
  4000. interval *= sd->busy_factor;
  4001. /* scale ms to jiffies */
  4002. interval = msecs_to_jiffies(interval);
  4003. if (unlikely(!interval))
  4004. interval = 1;
  4005. if (interval > HZ*NR_CPUS/10)
  4006. interval = HZ*NR_CPUS/10;
  4007. need_serialize = sd->flags & SD_SERIALIZE;
  4008. if (need_serialize) {
  4009. if (!spin_trylock(&balancing))
  4010. goto out;
  4011. }
  4012. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  4013. if (load_balance(cpu, rq, sd, idle, &balance)) {
  4014. /*
  4015. * We've pulled tasks over so either we're no
  4016. * longer idle, or one of our SMT siblings is
  4017. * not idle.
  4018. */
  4019. idle = CPU_NOT_IDLE;
  4020. }
  4021. sd->last_balance = jiffies;
  4022. }
  4023. if (need_serialize)
  4024. spin_unlock(&balancing);
  4025. out:
  4026. if (time_after(next_balance, sd->last_balance + interval)) {
  4027. next_balance = sd->last_balance + interval;
  4028. update_next_balance = 1;
  4029. }
  4030. /*
  4031. * Stop the load balance at this level. There is another
  4032. * CPU in our sched group which is doing load balancing more
  4033. * actively.
  4034. */
  4035. if (!balance)
  4036. break;
  4037. }
  4038. /*
  4039. * next_balance will be updated only when there is a need.
  4040. * When the cpu is attached to null domain for ex, it will not be
  4041. * updated.
  4042. */
  4043. if (likely(update_next_balance))
  4044. rq->next_balance = next_balance;
  4045. }
  4046. /*
  4047. * run_rebalance_domains is triggered when needed from the scheduler tick.
  4048. * In CONFIG_NO_HZ case, the idle load balance owner will do the
  4049. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  4050. */
  4051. static void run_rebalance_domains(struct softirq_action *h)
  4052. {
  4053. int this_cpu = smp_processor_id();
  4054. struct rq *this_rq = cpu_rq(this_cpu);
  4055. enum cpu_idle_type idle = this_rq->idle_at_tick ?
  4056. CPU_IDLE : CPU_NOT_IDLE;
  4057. rebalance_domains(this_cpu, idle);
  4058. #ifdef CONFIG_NO_HZ
  4059. /*
  4060. * If this cpu is the owner for idle load balancing, then do the
  4061. * balancing on behalf of the other idle cpus whose ticks are
  4062. * stopped.
  4063. */
  4064. if (this_rq->idle_at_tick &&
  4065. atomic_read(&nohz.load_balancer) == this_cpu) {
  4066. struct rq *rq;
  4067. int balance_cpu;
  4068. for_each_cpu(balance_cpu, nohz.cpu_mask) {
  4069. if (balance_cpu == this_cpu)
  4070. continue;
  4071. /*
  4072. * If this cpu gets work to do, stop the load balancing
  4073. * work being done for other cpus. Next load
  4074. * balancing owner will pick it up.
  4075. */
  4076. if (need_resched())
  4077. break;
  4078. rebalance_domains(balance_cpu, CPU_IDLE);
  4079. rq = cpu_rq(balance_cpu);
  4080. if (time_after(this_rq->next_balance, rq->next_balance))
  4081. this_rq->next_balance = rq->next_balance;
  4082. }
  4083. }
  4084. #endif
  4085. }
  4086. static inline int on_null_domain(int cpu)
  4087. {
  4088. return !rcu_dereference(cpu_rq(cpu)->sd);
  4089. }
  4090. /*
  4091. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  4092. *
  4093. * In case of CONFIG_NO_HZ, this is the place where we nominate a new
  4094. * idle load balancing owner or decide to stop the periodic load balancing,
  4095. * if the whole system is idle.
  4096. */
  4097. static inline void trigger_load_balance(struct rq *rq, int cpu)
  4098. {
  4099. #ifdef CONFIG_NO_HZ
  4100. /*
  4101. * If we were in the nohz mode recently and busy at the current
  4102. * scheduler tick, then check if we need to nominate new idle
  4103. * load balancer.
  4104. */
  4105. if (rq->in_nohz_recently && !rq->idle_at_tick) {
  4106. rq->in_nohz_recently = 0;
  4107. if (atomic_read(&nohz.load_balancer) == cpu) {
  4108. cpumask_clear_cpu(cpu, nohz.cpu_mask);
  4109. atomic_set(&nohz.load_balancer, -1);
  4110. }
  4111. if (atomic_read(&nohz.load_balancer) == -1) {
  4112. int ilb = find_new_ilb(cpu);
  4113. if (ilb < nr_cpu_ids)
  4114. resched_cpu(ilb);
  4115. }
  4116. }
  4117. /*
  4118. * If this cpu is idle and doing idle load balancing for all the
  4119. * cpus with ticks stopped, is it time for that to stop?
  4120. */
  4121. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
  4122. cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
  4123. resched_cpu(cpu);
  4124. return;
  4125. }
  4126. /*
  4127. * If this cpu is idle and the idle load balancing is done by
  4128. * someone else, then no need raise the SCHED_SOFTIRQ
  4129. */
  4130. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
  4131. cpumask_test_cpu(cpu, nohz.cpu_mask))
  4132. return;
  4133. #endif
  4134. /* Don't need to rebalance while attached to NULL domain */
  4135. if (time_after_eq(jiffies, rq->next_balance) &&
  4136. likely(!on_null_domain(cpu)))
  4137. raise_softirq(SCHED_SOFTIRQ);
  4138. }
  4139. #else /* CONFIG_SMP */
  4140. /*
  4141. * on UP we do not need to balance between CPUs:
  4142. */
  4143. static inline void idle_balance(int cpu, struct rq *rq)
  4144. {
  4145. }
  4146. #endif
  4147. DEFINE_PER_CPU(struct kernel_stat, kstat);
  4148. EXPORT_PER_CPU_SYMBOL(kstat);
  4149. /*
  4150. * Return any ns on the sched_clock that have not yet been accounted in
  4151. * @p in case that task is currently running.
  4152. *
  4153. * Called with task_rq_lock() held on @rq.
  4154. */
  4155. static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
  4156. {
  4157. u64 ns = 0;
  4158. if (task_current(rq, p)) {
  4159. update_rq_clock(rq);
  4160. ns = rq->clock - p->se.exec_start;
  4161. if ((s64)ns < 0)
  4162. ns = 0;
  4163. }
  4164. return ns;
  4165. }
  4166. unsigned long long task_delta_exec(struct task_struct *p)
  4167. {
  4168. unsigned long flags;
  4169. struct rq *rq;
  4170. u64 ns = 0;
  4171. rq = task_rq_lock(p, &flags);
  4172. ns = do_task_delta_exec(p, rq);
  4173. task_rq_unlock(rq, &flags);
  4174. return ns;
  4175. }
  4176. /*
  4177. * Return accounted runtime for the task.
  4178. * In case the task is currently running, return the runtime plus current's
  4179. * pending runtime that have not been accounted yet.
  4180. */
  4181. unsigned long long task_sched_runtime(struct task_struct *p)
  4182. {
  4183. unsigned long flags;
  4184. struct rq *rq;
  4185. u64 ns = 0;
  4186. rq = task_rq_lock(p, &flags);
  4187. ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
  4188. task_rq_unlock(rq, &flags);
  4189. return ns;
  4190. }
  4191. /*
  4192. * Return sum_exec_runtime for the thread group.
  4193. * In case the task is currently running, return the sum plus current's
  4194. * pending runtime that have not been accounted yet.
  4195. *
  4196. * Note that the thread group might have other running tasks as well,
  4197. * so the return value not includes other pending runtime that other
  4198. * running tasks might have.
  4199. */
  4200. unsigned long long thread_group_sched_runtime(struct task_struct *p)
  4201. {
  4202. struct task_cputime totals;
  4203. unsigned long flags;
  4204. struct rq *rq;
  4205. u64 ns;
  4206. rq = task_rq_lock(p, &flags);
  4207. thread_group_cputime(p, &totals);
  4208. ns = totals.sum_exec_runtime + do_task_delta_exec(p, rq);
  4209. task_rq_unlock(rq, &flags);
  4210. return ns;
  4211. }
  4212. /*
  4213. * Account user cpu time to a process.
  4214. * @p: the process that the cpu time gets accounted to
  4215. * @cputime: the cpu time spent in user space since the last update
  4216. * @cputime_scaled: cputime scaled by cpu frequency
  4217. */
  4218. void account_user_time(struct task_struct *p, cputime_t cputime,
  4219. cputime_t cputime_scaled)
  4220. {
  4221. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  4222. cputime64_t tmp;
  4223. /* Add user time to process. */
  4224. p->utime = cputime_add(p->utime, cputime);
  4225. p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
  4226. account_group_user_time(p, cputime);
  4227. /* Add user time to cpustat. */
  4228. tmp = cputime_to_cputime64(cputime);
  4229. if (TASK_NICE(p) > 0)
  4230. cpustat->nice = cputime64_add(cpustat->nice, tmp);
  4231. else
  4232. cpustat->user = cputime64_add(cpustat->user, tmp);
  4233. cpuacct_update_stats(p, CPUACCT_STAT_USER, cputime);
  4234. /* Account for user time used */
  4235. acct_update_integrals(p);
  4236. }
  4237. /*
  4238. * Account guest cpu time to a process.
  4239. * @p: the process that the cpu time gets accounted to
  4240. * @cputime: the cpu time spent in virtual machine since the last update
  4241. * @cputime_scaled: cputime scaled by cpu frequency
  4242. */
  4243. static void account_guest_time(struct task_struct *p, cputime_t cputime,
  4244. cputime_t cputime_scaled)
  4245. {
  4246. cputime64_t tmp;
  4247. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  4248. tmp = cputime_to_cputime64(cputime);
  4249. /* Add guest time to process. */
  4250. p->utime = cputime_add(p->utime, cputime);
  4251. p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
  4252. account_group_user_time(p, cputime);
  4253. p->gtime = cputime_add(p->gtime, cputime);
  4254. /* Add guest time to cpustat. */
  4255. cpustat->user = cputime64_add(cpustat->user, tmp);
  4256. cpustat->guest = cputime64_add(cpustat->guest, tmp);
  4257. }
  4258. /*
  4259. * Account system cpu time to a process.
  4260. * @p: the process that the cpu time gets accounted to
  4261. * @hardirq_offset: the offset to subtract from hardirq_count()
  4262. * @cputime: the cpu time spent in kernel space since the last update
  4263. * @cputime_scaled: cputime scaled by cpu frequency
  4264. */
  4265. void account_system_time(struct task_struct *p, int hardirq_offset,
  4266. cputime_t cputime, cputime_t cputime_scaled)
  4267. {
  4268. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  4269. cputime64_t tmp;
  4270. if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
  4271. account_guest_time(p, cputime, cputime_scaled);
  4272. return;
  4273. }
  4274. /* Add system time to process. */
  4275. p->stime = cputime_add(p->stime, cputime);
  4276. p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
  4277. account_group_system_time(p, cputime);
  4278. /* Add system time to cpustat. */
  4279. tmp = cputime_to_cputime64(cputime);
  4280. if (hardirq_count() - hardirq_offset)
  4281. cpustat->irq = cputime64_add(cpustat->irq, tmp);
  4282. else if (softirq_count())
  4283. cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
  4284. else
  4285. cpustat->system = cputime64_add(cpustat->system, tmp);
  4286. cpuacct_update_stats(p, CPUACCT_STAT_SYSTEM, cputime);
  4287. /* Account for system time used */
  4288. acct_update_integrals(p);
  4289. }
  4290. /*
  4291. * Account for involuntary wait time.
  4292. * @steal: the cpu time spent in involuntary wait
  4293. */
  4294. void account_steal_time(cputime_t cputime)
  4295. {
  4296. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  4297. cputime64_t cputime64 = cputime_to_cputime64(cputime);
  4298. cpustat->steal = cputime64_add(cpustat->steal, cputime64);
  4299. }
  4300. /*
  4301. * Account for idle time.
  4302. * @cputime: the cpu time spent in idle wait
  4303. */
  4304. void account_idle_time(cputime_t cputime)
  4305. {
  4306. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  4307. cputime64_t cputime64 = cputime_to_cputime64(cputime);
  4308. struct rq *rq = this_rq();
  4309. if (atomic_read(&rq->nr_iowait) > 0)
  4310. cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
  4311. else
  4312. cpustat->idle = cputime64_add(cpustat->idle, cputime64);
  4313. }
  4314. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  4315. /*
  4316. * Account a single tick of cpu time.
  4317. * @p: the process that the cpu time gets accounted to
  4318. * @user_tick: indicates if the tick is a user or a system tick
  4319. */
  4320. void account_process_tick(struct task_struct *p, int user_tick)
  4321. {
  4322. cputime_t one_jiffy = jiffies_to_cputime(1);
  4323. cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
  4324. struct rq *rq = this_rq();
  4325. if (user_tick)
  4326. account_user_time(p, one_jiffy, one_jiffy_scaled);
  4327. else if ((p != rq->idle) || (irq_count() != HARDIRQ_OFFSET))
  4328. account_system_time(p, HARDIRQ_OFFSET, one_jiffy,
  4329. one_jiffy_scaled);
  4330. else
  4331. account_idle_time(one_jiffy);
  4332. }
  4333. /*
  4334. * Account multiple ticks of steal time.
  4335. * @p: the process from which the cpu time has been stolen
  4336. * @ticks: number of stolen ticks
  4337. */
  4338. void account_steal_ticks(unsigned long ticks)
  4339. {
  4340. account_steal_time(jiffies_to_cputime(ticks));
  4341. }
  4342. /*
  4343. * Account multiple ticks of idle time.
  4344. * @ticks: number of stolen ticks
  4345. */
  4346. void account_idle_ticks(unsigned long ticks)
  4347. {
  4348. account_idle_time(jiffies_to_cputime(ticks));
  4349. }
  4350. #endif
  4351. /*
  4352. * Use precise platform statistics if available:
  4353. */
  4354. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  4355. cputime_t task_utime(struct task_struct *p)
  4356. {
  4357. return p->utime;
  4358. }
  4359. cputime_t task_stime(struct task_struct *p)
  4360. {
  4361. return p->stime;
  4362. }
  4363. #else
  4364. cputime_t task_utime(struct task_struct *p)
  4365. {
  4366. clock_t utime = cputime_to_clock_t(p->utime),
  4367. total = utime + cputime_to_clock_t(p->stime);
  4368. u64 temp;
  4369. /*
  4370. * Use CFS's precise accounting:
  4371. */
  4372. temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
  4373. if (total) {
  4374. temp *= utime;
  4375. do_div(temp, total);
  4376. }
  4377. utime = (clock_t)temp;
  4378. p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
  4379. return p->prev_utime;
  4380. }
  4381. cputime_t task_stime(struct task_struct *p)
  4382. {
  4383. clock_t stime;
  4384. /*
  4385. * Use CFS's precise accounting. (we subtract utime from
  4386. * the total, to make sure the total observed by userspace
  4387. * grows monotonically - apps rely on that):
  4388. */
  4389. stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
  4390. cputime_to_clock_t(task_utime(p));
  4391. if (stime >= 0)
  4392. p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
  4393. return p->prev_stime;
  4394. }
  4395. #endif
  4396. inline cputime_t task_gtime(struct task_struct *p)
  4397. {
  4398. return p->gtime;
  4399. }
  4400. /*
  4401. * This function gets called by the timer code, with HZ frequency.
  4402. * We call it with interrupts disabled.
  4403. *
  4404. * It also gets called by the fork code, when changing the parent's
  4405. * timeslices.
  4406. */
  4407. void scheduler_tick(void)
  4408. {
  4409. int cpu = smp_processor_id();
  4410. struct rq *rq = cpu_rq(cpu);
  4411. struct task_struct *curr = rq->curr;
  4412. sched_clock_tick();
  4413. spin_lock(&rq->lock);
  4414. update_rq_clock(rq);
  4415. update_cpu_load(rq);
  4416. curr->sched_class->task_tick(rq, curr, 0);
  4417. spin_unlock(&rq->lock);
  4418. perf_counter_task_tick(curr, cpu);
  4419. #ifdef CONFIG_SMP
  4420. rq->idle_at_tick = idle_cpu(cpu);
  4421. trigger_load_balance(rq, cpu);
  4422. #endif
  4423. }
  4424. notrace unsigned long get_parent_ip(unsigned long addr)
  4425. {
  4426. if (in_lock_functions(addr)) {
  4427. addr = CALLER_ADDR2;
  4428. if (in_lock_functions(addr))
  4429. addr = CALLER_ADDR3;
  4430. }
  4431. return addr;
  4432. }
  4433. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  4434. defined(CONFIG_PREEMPT_TRACER))
  4435. void __kprobes add_preempt_count(int val)
  4436. {
  4437. #ifdef CONFIG_DEBUG_PREEMPT
  4438. /*
  4439. * Underflow?
  4440. */
  4441. if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
  4442. return;
  4443. #endif
  4444. preempt_count() += val;
  4445. #ifdef CONFIG_DEBUG_PREEMPT
  4446. /*
  4447. * Spinlock count overflowing soon?
  4448. */
  4449. DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
  4450. PREEMPT_MASK - 10);
  4451. #endif
  4452. if (preempt_count() == val)
  4453. trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  4454. }
  4455. EXPORT_SYMBOL(add_preempt_count);
  4456. void __kprobes sub_preempt_count(int val)
  4457. {
  4458. #ifdef CONFIG_DEBUG_PREEMPT
  4459. /*
  4460. * Underflow?
  4461. */
  4462. if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
  4463. return;
  4464. /*
  4465. * Is the spinlock portion underflowing?
  4466. */
  4467. if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
  4468. !(preempt_count() & PREEMPT_MASK)))
  4469. return;
  4470. #endif
  4471. if (preempt_count() == val)
  4472. trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  4473. preempt_count() -= val;
  4474. }
  4475. EXPORT_SYMBOL(sub_preempt_count);
  4476. #endif
  4477. /*
  4478. * Print scheduling while atomic bug:
  4479. */
  4480. static noinline void __schedule_bug(struct task_struct *prev)
  4481. {
  4482. struct pt_regs *regs = get_irq_regs();
  4483. printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
  4484. prev->comm, prev->pid, preempt_count());
  4485. debug_show_held_locks(prev);
  4486. print_modules();
  4487. if (irqs_disabled())
  4488. print_irqtrace_events(prev);
  4489. if (regs)
  4490. show_regs(regs);
  4491. else
  4492. dump_stack();
  4493. }
  4494. /*
  4495. * Various schedule()-time debugging checks and statistics:
  4496. */
  4497. static inline void schedule_debug(struct task_struct *prev)
  4498. {
  4499. /*
  4500. * Test if we are atomic. Since do_exit() needs to call into
  4501. * schedule() atomically, we ignore that path for now.
  4502. * Otherwise, whine if we are scheduling when we should not be.
  4503. */
  4504. if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
  4505. __schedule_bug(prev);
  4506. profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  4507. schedstat_inc(this_rq(), sched_count);
  4508. #ifdef CONFIG_SCHEDSTATS
  4509. if (unlikely(prev->lock_depth >= 0)) {
  4510. schedstat_inc(this_rq(), bkl_count);
  4511. schedstat_inc(prev, sched_info.bkl_count);
  4512. }
  4513. #endif
  4514. }
  4515. static void put_prev_task(struct rq *rq, struct task_struct *prev)
  4516. {
  4517. if (prev->state == TASK_RUNNING) {
  4518. u64 runtime = prev->se.sum_exec_runtime;
  4519. runtime -= prev->se.prev_sum_exec_runtime;
  4520. runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
  4521. /*
  4522. * In order to avoid avg_overlap growing stale when we are
  4523. * indeed overlapping and hence not getting put to sleep, grow
  4524. * the avg_overlap on preemption.
  4525. *
  4526. * We use the average preemption runtime because that
  4527. * correlates to the amount of cache footprint a task can
  4528. * build up.
  4529. */
  4530. update_avg(&prev->se.avg_overlap, runtime);
  4531. }
  4532. prev->sched_class->put_prev_task(rq, prev);
  4533. }
  4534. /*
  4535. * Pick up the highest-prio task:
  4536. */
  4537. static inline struct task_struct *
  4538. pick_next_task(struct rq *rq)
  4539. {
  4540. const struct sched_class *class;
  4541. struct task_struct *p;
  4542. /*
  4543. * Optimization: we know that if all tasks are in
  4544. * the fair class we can call that function directly:
  4545. */
  4546. if (likely(rq->nr_running == rq->cfs.nr_running)) {
  4547. p = fair_sched_class.pick_next_task(rq);
  4548. if (likely(p))
  4549. return p;
  4550. }
  4551. class = sched_class_highest;
  4552. for ( ; ; ) {
  4553. p = class->pick_next_task(rq);
  4554. if (p)
  4555. return p;
  4556. /*
  4557. * Will never be NULL as the idle class always
  4558. * returns a non-NULL p:
  4559. */
  4560. class = class->next;
  4561. }
  4562. }
  4563. /*
  4564. * schedule() is the main scheduler function.
  4565. */
  4566. asmlinkage void __sched schedule(void)
  4567. {
  4568. struct task_struct *prev, *next;
  4569. unsigned long *switch_count;
  4570. struct rq *rq;
  4571. int cpu;
  4572. need_resched:
  4573. preempt_disable();
  4574. cpu = smp_processor_id();
  4575. rq = cpu_rq(cpu);
  4576. rcu_sched_qs(cpu);
  4577. prev = rq->curr;
  4578. switch_count = &prev->nivcsw;
  4579. release_kernel_lock(prev);
  4580. need_resched_nonpreemptible:
  4581. schedule_debug(prev);
  4582. if (sched_feat(HRTICK))
  4583. hrtick_clear(rq);
  4584. spin_lock_irq(&rq->lock);
  4585. update_rq_clock(rq);
  4586. clear_tsk_need_resched(prev);
  4587. if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  4588. if (unlikely(signal_pending_state(prev->state, prev)))
  4589. prev->state = TASK_RUNNING;
  4590. else
  4591. deactivate_task(rq, prev, 1);
  4592. switch_count = &prev->nvcsw;
  4593. }
  4594. pre_schedule(rq, prev);
  4595. if (unlikely(!rq->nr_running))
  4596. idle_balance(cpu, rq);
  4597. put_prev_task(rq, prev);
  4598. next = pick_next_task(rq);
  4599. if (likely(prev != next)) {
  4600. sched_info_switch(prev, next);
  4601. perf_counter_task_sched_out(prev, next, cpu);
  4602. rq->nr_switches++;
  4603. rq->curr = next;
  4604. ++*switch_count;
  4605. context_switch(rq, prev, next); /* unlocks the rq */
  4606. /*
  4607. * the context switch might have flipped the stack from under
  4608. * us, hence refresh the local variables.
  4609. */
  4610. cpu = smp_processor_id();
  4611. rq = cpu_rq(cpu);
  4612. } else
  4613. spin_unlock_irq(&rq->lock);
  4614. post_schedule(rq);
  4615. if (unlikely(reacquire_kernel_lock(current) < 0))
  4616. goto need_resched_nonpreemptible;
  4617. preempt_enable_no_resched();
  4618. if (need_resched())
  4619. goto need_resched;
  4620. }
  4621. EXPORT_SYMBOL(schedule);
  4622. #ifdef CONFIG_SMP
  4623. /*
  4624. * Look out! "owner" is an entirely speculative pointer
  4625. * access and not reliable.
  4626. */
  4627. int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
  4628. {
  4629. unsigned int cpu;
  4630. struct rq *rq;
  4631. if (!sched_feat(OWNER_SPIN))
  4632. return 0;
  4633. #ifdef CONFIG_DEBUG_PAGEALLOC
  4634. /*
  4635. * Need to access the cpu field knowing that
  4636. * DEBUG_PAGEALLOC could have unmapped it if
  4637. * the mutex owner just released it and exited.
  4638. */
  4639. if (probe_kernel_address(&owner->cpu, cpu))
  4640. goto out;
  4641. #else
  4642. cpu = owner->cpu;
  4643. #endif
  4644. /*
  4645. * Even if the access succeeded (likely case),
  4646. * the cpu field may no longer be valid.
  4647. */
  4648. if (cpu >= nr_cpumask_bits)
  4649. goto out;
  4650. /*
  4651. * We need to validate that we can do a
  4652. * get_cpu() and that we have the percpu area.
  4653. */
  4654. if (!cpu_online(cpu))
  4655. goto out;
  4656. rq = cpu_rq(cpu);
  4657. for (;;) {
  4658. /*
  4659. * Owner changed, break to re-assess state.
  4660. */
  4661. if (lock->owner != owner)
  4662. break;
  4663. /*
  4664. * Is that owner really running on that cpu?
  4665. */
  4666. if (task_thread_info(rq->curr) != owner || need_resched())
  4667. return 0;
  4668. cpu_relax();
  4669. }
  4670. out:
  4671. return 1;
  4672. }
  4673. #endif
  4674. #ifdef CONFIG_PREEMPT
  4675. /*
  4676. * this is the entry point to schedule() from in-kernel preemption
  4677. * off of preempt_enable. Kernel preemptions off return from interrupt
  4678. * occur there and call schedule directly.
  4679. */
  4680. asmlinkage void __sched preempt_schedule(void)
  4681. {
  4682. struct thread_info *ti = current_thread_info();
  4683. /*
  4684. * If there is a non-zero preempt_count or interrupts are disabled,
  4685. * we do not want to preempt the current task. Just return..
  4686. */
  4687. if (likely(ti->preempt_count || irqs_disabled()))
  4688. return;
  4689. do {
  4690. add_preempt_count(PREEMPT_ACTIVE);
  4691. schedule();
  4692. sub_preempt_count(PREEMPT_ACTIVE);
  4693. /*
  4694. * Check again in case we missed a preemption opportunity
  4695. * between schedule and now.
  4696. */
  4697. barrier();
  4698. } while (need_resched());
  4699. }
  4700. EXPORT_SYMBOL(preempt_schedule);
  4701. /*
  4702. * this is the entry point to schedule() from kernel preemption
  4703. * off of irq context.
  4704. * Note, that this is called and return with irqs disabled. This will
  4705. * protect us against recursive calling from irq.
  4706. */
  4707. asmlinkage void __sched preempt_schedule_irq(void)
  4708. {
  4709. struct thread_info *ti = current_thread_info();
  4710. /* Catch callers which need to be fixed */
  4711. BUG_ON(ti->preempt_count || !irqs_disabled());
  4712. do {
  4713. add_preempt_count(PREEMPT_ACTIVE);
  4714. local_irq_enable();
  4715. schedule();
  4716. local_irq_disable();
  4717. sub_preempt_count(PREEMPT_ACTIVE);
  4718. /*
  4719. * Check again in case we missed a preemption opportunity
  4720. * between schedule and now.
  4721. */
  4722. barrier();
  4723. } while (need_resched());
  4724. }
  4725. #endif /* CONFIG_PREEMPT */
  4726. int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
  4727. void *key)
  4728. {
  4729. return try_to_wake_up(curr->private, mode, sync);
  4730. }
  4731. EXPORT_SYMBOL(default_wake_function);
  4732. /*
  4733. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  4734. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  4735. * number) then we wake all the non-exclusive tasks and one exclusive task.
  4736. *
  4737. * There are circumstances in which we can try to wake a task which has already
  4738. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  4739. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  4740. */
  4741. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  4742. int nr_exclusive, int sync, void *key)
  4743. {
  4744. wait_queue_t *curr, *next;
  4745. list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
  4746. unsigned flags = curr->flags;
  4747. if (curr->func(curr, mode, sync, key) &&
  4748. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  4749. break;
  4750. }
  4751. }
  4752. /**
  4753. * __wake_up - wake up threads blocked on a waitqueue.
  4754. * @q: the waitqueue
  4755. * @mode: which threads
  4756. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  4757. * @key: is directly passed to the wakeup function
  4758. *
  4759. * It may be assumed that this function implies a write memory barrier before
  4760. * changing the task state if and only if any tasks are woken up.
  4761. */
  4762. void __wake_up(wait_queue_head_t *q, unsigned int mode,
  4763. int nr_exclusive, void *key)
  4764. {
  4765. unsigned long flags;
  4766. spin_lock_irqsave(&q->lock, flags);
  4767. __wake_up_common(q, mode, nr_exclusive, 0, key);
  4768. spin_unlock_irqrestore(&q->lock, flags);
  4769. }
  4770. EXPORT_SYMBOL(__wake_up);
  4771. /*
  4772. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  4773. */
  4774. void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
  4775. {
  4776. __wake_up_common(q, mode, 1, 0, NULL);
  4777. }
  4778. void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
  4779. {
  4780. __wake_up_common(q, mode, 1, 0, key);
  4781. }
  4782. /**
  4783. * __wake_up_sync_key - wake up threads blocked on a waitqueue.
  4784. * @q: the waitqueue
  4785. * @mode: which threads
  4786. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  4787. * @key: opaque value to be passed to wakeup targets
  4788. *
  4789. * The sync wakeup differs that the waker knows that it will schedule
  4790. * away soon, so while the target thread will be woken up, it will not
  4791. * be migrated to another CPU - ie. the two threads are 'synchronized'
  4792. * with each other. This can prevent needless bouncing between CPUs.
  4793. *
  4794. * On UP it can prevent extra preemption.
  4795. *
  4796. * It may be assumed that this function implies a write memory barrier before
  4797. * changing the task state if and only if any tasks are woken up.
  4798. */
  4799. void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
  4800. int nr_exclusive, void *key)
  4801. {
  4802. unsigned long flags;
  4803. int sync = 1;
  4804. if (unlikely(!q))
  4805. return;
  4806. if (unlikely(!nr_exclusive))
  4807. sync = 0;
  4808. spin_lock_irqsave(&q->lock, flags);
  4809. __wake_up_common(q, mode, nr_exclusive, sync, key);
  4810. spin_unlock_irqrestore(&q->lock, flags);
  4811. }
  4812. EXPORT_SYMBOL_GPL(__wake_up_sync_key);
  4813. /*
  4814. * __wake_up_sync - see __wake_up_sync_key()
  4815. */
  4816. void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  4817. {
  4818. __wake_up_sync_key(q, mode, nr_exclusive, NULL);
  4819. }
  4820. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  4821. /**
  4822. * complete: - signals a single thread waiting on this completion
  4823. * @x: holds the state of this particular completion
  4824. *
  4825. * This will wake up a single thread waiting on this completion. Threads will be
  4826. * awakened in the same order in which they were queued.
  4827. *
  4828. * See also complete_all(), wait_for_completion() and related routines.
  4829. *
  4830. * It may be assumed that this function implies a write memory barrier before
  4831. * changing the task state if and only if any tasks are woken up.
  4832. */
  4833. void complete(struct completion *x)
  4834. {
  4835. unsigned long flags;
  4836. spin_lock_irqsave(&x->wait.lock, flags);
  4837. x->done++;
  4838. __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
  4839. spin_unlock_irqrestore(&x->wait.lock, flags);
  4840. }
  4841. EXPORT_SYMBOL(complete);
  4842. /**
  4843. * complete_all: - signals all threads waiting on this completion
  4844. * @x: holds the state of this particular completion
  4845. *
  4846. * This will wake up all threads waiting on this particular completion event.
  4847. *
  4848. * It may be assumed that this function implies a write memory barrier before
  4849. * changing the task state if and only if any tasks are woken up.
  4850. */
  4851. void complete_all(struct completion *x)
  4852. {
  4853. unsigned long flags;
  4854. spin_lock_irqsave(&x->wait.lock, flags);
  4855. x->done += UINT_MAX/2;
  4856. __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
  4857. spin_unlock_irqrestore(&x->wait.lock, flags);
  4858. }
  4859. EXPORT_SYMBOL(complete_all);
  4860. static inline long __sched
  4861. do_wait_for_common(struct completion *x, long timeout, int state)
  4862. {
  4863. if (!x->done) {
  4864. DECLARE_WAITQUEUE(wait, current);
  4865. wait.flags |= WQ_FLAG_EXCLUSIVE;
  4866. __add_wait_queue_tail(&x->wait, &wait);
  4867. do {
  4868. if (signal_pending_state(state, current)) {
  4869. timeout = -ERESTARTSYS;
  4870. break;
  4871. }
  4872. __set_current_state(state);
  4873. spin_unlock_irq(&x->wait.lock);
  4874. timeout = schedule_timeout(timeout);
  4875. spin_lock_irq(&x->wait.lock);
  4876. } while (!x->done && timeout);
  4877. __remove_wait_queue(&x->wait, &wait);
  4878. if (!x->done)
  4879. return timeout;
  4880. }
  4881. x->done--;
  4882. return timeout ?: 1;
  4883. }
  4884. static long __sched
  4885. wait_for_common(struct completion *x, long timeout, int state)
  4886. {
  4887. might_sleep();
  4888. spin_lock_irq(&x->wait.lock);
  4889. timeout = do_wait_for_common(x, timeout, state);
  4890. spin_unlock_irq(&x->wait.lock);
  4891. return timeout;
  4892. }
  4893. /**
  4894. * wait_for_completion: - waits for completion of a task
  4895. * @x: holds the state of this particular completion
  4896. *
  4897. * This waits to be signaled for completion of a specific task. It is NOT
  4898. * interruptible and there is no timeout.
  4899. *
  4900. * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
  4901. * and interrupt capability. Also see complete().
  4902. */
  4903. void __sched wait_for_completion(struct completion *x)
  4904. {
  4905. wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
  4906. }
  4907. EXPORT_SYMBOL(wait_for_completion);
  4908. /**
  4909. * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
  4910. * @x: holds the state of this particular completion
  4911. * @timeout: timeout value in jiffies
  4912. *
  4913. * This waits for either a completion of a specific task to be signaled or for a
  4914. * specified timeout to expire. The timeout is in jiffies. It is not
  4915. * interruptible.
  4916. */
  4917. unsigned long __sched
  4918. wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  4919. {
  4920. return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
  4921. }
  4922. EXPORT_SYMBOL(wait_for_completion_timeout);
  4923. /**
  4924. * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
  4925. * @x: holds the state of this particular completion
  4926. *
  4927. * This waits for completion of a specific task to be signaled. It is
  4928. * interruptible.
  4929. */
  4930. int __sched wait_for_completion_interruptible(struct completion *x)
  4931. {
  4932. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
  4933. if (t == -ERESTARTSYS)
  4934. return t;
  4935. return 0;
  4936. }
  4937. EXPORT_SYMBOL(wait_for_completion_interruptible);
  4938. /**
  4939. * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
  4940. * @x: holds the state of this particular completion
  4941. * @timeout: timeout value in jiffies
  4942. *
  4943. * This waits for either a completion of a specific task to be signaled or for a
  4944. * specified timeout to expire. It is interruptible. The timeout is in jiffies.
  4945. */
  4946. unsigned long __sched
  4947. wait_for_completion_interruptible_timeout(struct completion *x,
  4948. unsigned long timeout)
  4949. {
  4950. return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
  4951. }
  4952. EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  4953. /**
  4954. * wait_for_completion_killable: - waits for completion of a task (killable)
  4955. * @x: holds the state of this particular completion
  4956. *
  4957. * This waits to be signaled for completion of a specific task. It can be
  4958. * interrupted by a kill signal.
  4959. */
  4960. int __sched wait_for_completion_killable(struct completion *x)
  4961. {
  4962. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
  4963. if (t == -ERESTARTSYS)
  4964. return t;
  4965. return 0;
  4966. }
  4967. EXPORT_SYMBOL(wait_for_completion_killable);
  4968. /**
  4969. * try_wait_for_completion - try to decrement a completion without blocking
  4970. * @x: completion structure
  4971. *
  4972. * Returns: 0 if a decrement cannot be done without blocking
  4973. * 1 if a decrement succeeded.
  4974. *
  4975. * If a completion is being used as a counting completion,
  4976. * attempt to decrement the counter without blocking. This
  4977. * enables us to avoid waiting if the resource the completion
  4978. * is protecting is not available.
  4979. */
  4980. bool try_wait_for_completion(struct completion *x)
  4981. {
  4982. int ret = 1;
  4983. spin_lock_irq(&x->wait.lock);
  4984. if (!x->done)
  4985. ret = 0;
  4986. else
  4987. x->done--;
  4988. spin_unlock_irq(&x->wait.lock);
  4989. return ret;
  4990. }
  4991. EXPORT_SYMBOL(try_wait_for_completion);
  4992. /**
  4993. * completion_done - Test to see if a completion has any waiters
  4994. * @x: completion structure
  4995. *
  4996. * Returns: 0 if there are waiters (wait_for_completion() in progress)
  4997. * 1 if there are no waiters.
  4998. *
  4999. */
  5000. bool completion_done(struct completion *x)
  5001. {
  5002. int ret = 1;
  5003. spin_lock_irq(&x->wait.lock);
  5004. if (!x->done)
  5005. ret = 0;
  5006. spin_unlock_irq(&x->wait.lock);
  5007. return ret;
  5008. }
  5009. EXPORT_SYMBOL(completion_done);
  5010. static long __sched
  5011. sleep_on_common(wait_queue_head_t *q, int state, long timeout)
  5012. {
  5013. unsigned long flags;
  5014. wait_queue_t wait;
  5015. init_waitqueue_entry(&wait, current);
  5016. __set_current_state(state);
  5017. spin_lock_irqsave(&q->lock, flags);
  5018. __add_wait_queue(q, &wait);
  5019. spin_unlock(&q->lock);
  5020. timeout = schedule_timeout(timeout);
  5021. spin_lock_irq(&q->lock);
  5022. __remove_wait_queue(q, &wait);
  5023. spin_unlock_irqrestore(&q->lock, flags);
  5024. return timeout;
  5025. }
  5026. void __sched interruptible_sleep_on(wait_queue_head_t *q)
  5027. {
  5028. sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  5029. }
  5030. EXPORT_SYMBOL(interruptible_sleep_on);
  5031. long __sched
  5032. interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
  5033. {
  5034. return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
  5035. }
  5036. EXPORT_SYMBOL(interruptible_sleep_on_timeout);
  5037. void __sched sleep_on(wait_queue_head_t *q)
  5038. {
  5039. sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  5040. }
  5041. EXPORT_SYMBOL(sleep_on);
  5042. long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
  5043. {
  5044. return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
  5045. }
  5046. EXPORT_SYMBOL(sleep_on_timeout);
  5047. #ifdef CONFIG_RT_MUTEXES
  5048. /*
  5049. * rt_mutex_setprio - set the current priority of a task
  5050. * @p: task
  5051. * @prio: prio value (kernel-internal form)
  5052. *
  5053. * This function changes the 'effective' priority of a task. It does
  5054. * not touch ->normal_prio like __setscheduler().
  5055. *
  5056. * Used by the rt_mutex code to implement priority inheritance logic.
  5057. */
  5058. void rt_mutex_setprio(struct task_struct *p, int prio)
  5059. {
  5060. unsigned long flags;
  5061. int oldprio, on_rq, running;
  5062. struct rq *rq;
  5063. const struct sched_class *prev_class = p->sched_class;
  5064. BUG_ON(prio < 0 || prio > MAX_PRIO);
  5065. rq = task_rq_lock(p, &flags);
  5066. update_rq_clock(rq);
  5067. oldprio = p->prio;
  5068. on_rq = p->se.on_rq;
  5069. running = task_current(rq, p);
  5070. if (on_rq)
  5071. dequeue_task(rq, p, 0);
  5072. if (running)
  5073. p->sched_class->put_prev_task(rq, p);
  5074. if (rt_prio(prio))
  5075. p->sched_class = &rt_sched_class;
  5076. else
  5077. p->sched_class = &fair_sched_class;
  5078. p->prio = prio;
  5079. if (running)
  5080. p->sched_class->set_curr_task(rq);
  5081. if (on_rq) {
  5082. enqueue_task(rq, p, 0);
  5083. check_class_changed(rq, p, prev_class, oldprio, running);
  5084. }
  5085. task_rq_unlock(rq, &flags);
  5086. }
  5087. #endif
  5088. void set_user_nice(struct task_struct *p, long nice)
  5089. {
  5090. int old_prio, delta, on_rq;
  5091. unsigned long flags;
  5092. struct rq *rq;
  5093. if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
  5094. return;
  5095. /*
  5096. * We have to be careful, if called from sys_setpriority(),
  5097. * the task might be in the middle of scheduling on another CPU.
  5098. */
  5099. rq = task_rq_lock(p, &flags);
  5100. update_rq_clock(rq);
  5101. /*
  5102. * The RT priorities are set via sched_setscheduler(), but we still
  5103. * allow the 'normal' nice value to be set - but as expected
  5104. * it wont have any effect on scheduling until the task is
  5105. * SCHED_FIFO/SCHED_RR:
  5106. */
  5107. if (task_has_rt_policy(p)) {
  5108. p->static_prio = NICE_TO_PRIO(nice);
  5109. goto out_unlock;
  5110. }
  5111. on_rq = p->se.on_rq;
  5112. if (on_rq)
  5113. dequeue_task(rq, p, 0);
  5114. p->static_prio = NICE_TO_PRIO(nice);
  5115. set_load_weight(p);
  5116. old_prio = p->prio;
  5117. p->prio = effective_prio(p);
  5118. delta = p->prio - old_prio;
  5119. if (on_rq) {
  5120. enqueue_task(rq, p, 0);
  5121. /*
  5122. * If the task increased its priority or is running and
  5123. * lowered its priority, then reschedule its CPU:
  5124. */
  5125. if (delta < 0 || (delta > 0 && task_running(rq, p)))
  5126. resched_task(rq->curr);
  5127. }
  5128. out_unlock:
  5129. task_rq_unlock(rq, &flags);
  5130. }
  5131. EXPORT_SYMBOL(set_user_nice);
  5132. /*
  5133. * can_nice - check if a task can reduce its nice value
  5134. * @p: task
  5135. * @nice: nice value
  5136. */
  5137. int can_nice(const struct task_struct *p, const int nice)
  5138. {
  5139. /* convert nice value [19,-20] to rlimit style value [1,40] */
  5140. int nice_rlim = 20 - nice;
  5141. return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
  5142. capable(CAP_SYS_NICE));
  5143. }
  5144. #ifdef __ARCH_WANT_SYS_NICE
  5145. /*
  5146. * sys_nice - change the priority of the current process.
  5147. * @increment: priority increment
  5148. *
  5149. * sys_setpriority is a more generic, but much slower function that
  5150. * does similar things.
  5151. */
  5152. SYSCALL_DEFINE1(nice, int, increment)
  5153. {
  5154. long nice, retval;
  5155. /*
  5156. * Setpriority might change our priority at the same moment.
  5157. * We don't have to worry. Conceptually one call occurs first
  5158. * and we have a single winner.
  5159. */
  5160. if (increment < -40)
  5161. increment = -40;
  5162. if (increment > 40)
  5163. increment = 40;
  5164. nice = TASK_NICE(current) + increment;
  5165. if (nice < -20)
  5166. nice = -20;
  5167. if (nice > 19)
  5168. nice = 19;
  5169. if (increment < 0 && !can_nice(current, nice))
  5170. return -EPERM;
  5171. retval = security_task_setnice(current, nice);
  5172. if (retval)
  5173. return retval;
  5174. set_user_nice(current, nice);
  5175. return 0;
  5176. }
  5177. #endif
  5178. /**
  5179. * task_prio - return the priority value of a given task.
  5180. * @p: the task in question.
  5181. *
  5182. * This is the priority value as seen by users in /proc.
  5183. * RT tasks are offset by -200. Normal tasks are centered
  5184. * around 0, value goes from -16 to +15.
  5185. */
  5186. int task_prio(const struct task_struct *p)
  5187. {
  5188. return p->prio - MAX_RT_PRIO;
  5189. }
  5190. /**
  5191. * task_nice - return the nice value of a given task.
  5192. * @p: the task in question.
  5193. */
  5194. int task_nice(const struct task_struct *p)
  5195. {
  5196. return TASK_NICE(p);
  5197. }
  5198. EXPORT_SYMBOL(task_nice);
  5199. /**
  5200. * idle_cpu - is a given cpu idle currently?
  5201. * @cpu: the processor in question.
  5202. */
  5203. int idle_cpu(int cpu)
  5204. {
  5205. return cpu_curr(cpu) == cpu_rq(cpu)->idle;
  5206. }
  5207. /**
  5208. * idle_task - return the idle task for a given cpu.
  5209. * @cpu: the processor in question.
  5210. */
  5211. struct task_struct *idle_task(int cpu)
  5212. {
  5213. return cpu_rq(cpu)->idle;
  5214. }
  5215. /**
  5216. * find_process_by_pid - find a process with a matching PID value.
  5217. * @pid: the pid in question.
  5218. */
  5219. static struct task_struct *find_process_by_pid(pid_t pid)
  5220. {
  5221. return pid ? find_task_by_vpid(pid) : current;
  5222. }
  5223. /* Actually do priority change: must hold rq lock. */
  5224. static void
  5225. __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
  5226. {
  5227. BUG_ON(p->se.on_rq);
  5228. p->policy = policy;
  5229. switch (p->policy) {
  5230. case SCHED_NORMAL:
  5231. case SCHED_BATCH:
  5232. case SCHED_IDLE:
  5233. p->sched_class = &fair_sched_class;
  5234. break;
  5235. case SCHED_FIFO:
  5236. case SCHED_RR:
  5237. p->sched_class = &rt_sched_class;
  5238. break;
  5239. }
  5240. p->rt_priority = prio;
  5241. p->normal_prio = normal_prio(p);
  5242. /* we are holding p->pi_lock already */
  5243. p->prio = rt_mutex_getprio(p);
  5244. set_load_weight(p);
  5245. }
  5246. /*
  5247. * check the target process has a UID that matches the current process's
  5248. */
  5249. static bool check_same_owner(struct task_struct *p)
  5250. {
  5251. const struct cred *cred = current_cred(), *pcred;
  5252. bool match;
  5253. rcu_read_lock();
  5254. pcred = __task_cred(p);
  5255. match = (cred->euid == pcred->euid ||
  5256. cred->euid == pcred->uid);
  5257. rcu_read_unlock();
  5258. return match;
  5259. }
  5260. static int __sched_setscheduler(struct task_struct *p, int policy,
  5261. struct sched_param *param, bool user)
  5262. {
  5263. int retval, oldprio, oldpolicy = -1, on_rq, running;
  5264. unsigned long flags;
  5265. const struct sched_class *prev_class = p->sched_class;
  5266. struct rq *rq;
  5267. int reset_on_fork;
  5268. /* may grab non-irq protected spin_locks */
  5269. BUG_ON(in_interrupt());
  5270. recheck:
  5271. /* double check policy once rq lock held */
  5272. if (policy < 0) {
  5273. reset_on_fork = p->sched_reset_on_fork;
  5274. policy = oldpolicy = p->policy;
  5275. } else {
  5276. reset_on_fork = !!(policy & SCHED_RESET_ON_FORK);
  5277. policy &= ~SCHED_RESET_ON_FORK;
  5278. if (policy != SCHED_FIFO && policy != SCHED_RR &&
  5279. policy != SCHED_NORMAL && policy != SCHED_BATCH &&
  5280. policy != SCHED_IDLE)
  5281. return -EINVAL;
  5282. }
  5283. /*
  5284. * Valid priorities for SCHED_FIFO and SCHED_RR are
  5285. * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
  5286. * SCHED_BATCH and SCHED_IDLE is 0.
  5287. */
  5288. if (param->sched_priority < 0 ||
  5289. (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
  5290. (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
  5291. return -EINVAL;
  5292. if (rt_policy(policy) != (param->sched_priority != 0))
  5293. return -EINVAL;
  5294. /*
  5295. * Allow unprivileged RT tasks to decrease priority:
  5296. */
  5297. if (user && !capable(CAP_SYS_NICE)) {
  5298. if (rt_policy(policy)) {
  5299. unsigned long rlim_rtprio;
  5300. if (!lock_task_sighand(p, &flags))
  5301. return -ESRCH;
  5302. rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
  5303. unlock_task_sighand(p, &flags);
  5304. /* can't set/change the rt policy */
  5305. if (policy != p->policy && !rlim_rtprio)
  5306. return -EPERM;
  5307. /* can't increase priority */
  5308. if (param->sched_priority > p->rt_priority &&
  5309. param->sched_priority > rlim_rtprio)
  5310. return -EPERM;
  5311. }
  5312. /*
  5313. * Like positive nice levels, dont allow tasks to
  5314. * move out of SCHED_IDLE either:
  5315. */
  5316. if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
  5317. return -EPERM;
  5318. /* can't change other user's priorities */
  5319. if (!check_same_owner(p))
  5320. return -EPERM;
  5321. /* Normal users shall not reset the sched_reset_on_fork flag */
  5322. if (p->sched_reset_on_fork && !reset_on_fork)
  5323. return -EPERM;
  5324. }
  5325. if (user) {
  5326. #ifdef CONFIG_RT_GROUP_SCHED
  5327. /*
  5328. * Do not allow realtime tasks into groups that have no runtime
  5329. * assigned.
  5330. */
  5331. if (rt_bandwidth_enabled() && rt_policy(policy) &&
  5332. task_group(p)->rt_bandwidth.rt_runtime == 0)
  5333. return -EPERM;
  5334. #endif
  5335. retval = security_task_setscheduler(p, policy, param);
  5336. if (retval)
  5337. return retval;
  5338. }
  5339. /*
  5340. * make sure no PI-waiters arrive (or leave) while we are
  5341. * changing the priority of the task:
  5342. */
  5343. spin_lock_irqsave(&p->pi_lock, flags);
  5344. /*
  5345. * To be able to change p->policy safely, the apropriate
  5346. * runqueue lock must be held.
  5347. */
  5348. rq = __task_rq_lock(p);
  5349. /* recheck policy now with rq lock held */
  5350. if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  5351. policy = oldpolicy = -1;
  5352. __task_rq_unlock(rq);
  5353. spin_unlock_irqrestore(&p->pi_lock, flags);
  5354. goto recheck;
  5355. }
  5356. update_rq_clock(rq);
  5357. on_rq = p->se.on_rq;
  5358. running = task_current(rq, p);
  5359. if (on_rq)
  5360. deactivate_task(rq, p, 0);
  5361. if (running)
  5362. p->sched_class->put_prev_task(rq, p);
  5363. p->sched_reset_on_fork = reset_on_fork;
  5364. oldprio = p->prio;
  5365. __setscheduler(rq, p, policy, param->sched_priority);
  5366. if (running)
  5367. p->sched_class->set_curr_task(rq);
  5368. if (on_rq) {
  5369. activate_task(rq, p, 0);
  5370. check_class_changed(rq, p, prev_class, oldprio, running);
  5371. }
  5372. __task_rq_unlock(rq);
  5373. spin_unlock_irqrestore(&p->pi_lock, flags);
  5374. rt_mutex_adjust_pi(p);
  5375. return 0;
  5376. }
  5377. /**
  5378. * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
  5379. * @p: the task in question.
  5380. * @policy: new policy.
  5381. * @param: structure containing the new RT priority.
  5382. *
  5383. * NOTE that the task may be already dead.
  5384. */
  5385. int sched_setscheduler(struct task_struct *p, int policy,
  5386. struct sched_param *param)
  5387. {
  5388. return __sched_setscheduler(p, policy, param, true);
  5389. }
  5390. EXPORT_SYMBOL_GPL(sched_setscheduler);
  5391. /**
  5392. * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
  5393. * @p: the task in question.
  5394. * @policy: new policy.
  5395. * @param: structure containing the new RT priority.
  5396. *
  5397. * Just like sched_setscheduler, only don't bother checking if the
  5398. * current context has permission. For example, this is needed in
  5399. * stop_machine(): we create temporary high priority worker threads,
  5400. * but our caller might not have that capability.
  5401. */
  5402. int sched_setscheduler_nocheck(struct task_struct *p, int policy,
  5403. struct sched_param *param)
  5404. {
  5405. return __sched_setscheduler(p, policy, param, false);
  5406. }
  5407. static int
  5408. do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  5409. {
  5410. struct sched_param lparam;
  5411. struct task_struct *p;
  5412. int retval;
  5413. if (!param || pid < 0)
  5414. return -EINVAL;
  5415. if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  5416. return -EFAULT;
  5417. rcu_read_lock();
  5418. retval = -ESRCH;
  5419. p = find_process_by_pid(pid);
  5420. if (p != NULL)
  5421. retval = sched_setscheduler(p, policy, &lparam);
  5422. rcu_read_unlock();
  5423. return retval;
  5424. }
  5425. /**
  5426. * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  5427. * @pid: the pid in question.
  5428. * @policy: new policy.
  5429. * @param: structure containing the new RT priority.
  5430. */
  5431. SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
  5432. struct sched_param __user *, param)
  5433. {
  5434. /* negative values for policy are not valid */
  5435. if (policy < 0)
  5436. return -EINVAL;
  5437. return do_sched_setscheduler(pid, policy, param);
  5438. }
  5439. /**
  5440. * sys_sched_setparam - set/change the RT priority of a thread
  5441. * @pid: the pid in question.
  5442. * @param: structure containing the new RT priority.
  5443. */
  5444. SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
  5445. {
  5446. return do_sched_setscheduler(pid, -1, param);
  5447. }
  5448. /**
  5449. * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  5450. * @pid: the pid in question.
  5451. */
  5452. SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
  5453. {
  5454. struct task_struct *p;
  5455. int retval;
  5456. if (pid < 0)
  5457. return -EINVAL;
  5458. retval = -ESRCH;
  5459. read_lock(&tasklist_lock);
  5460. p = find_process_by_pid(pid);
  5461. if (p) {
  5462. retval = security_task_getscheduler(p);
  5463. if (!retval)
  5464. retval = p->policy
  5465. | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
  5466. }
  5467. read_unlock(&tasklist_lock);
  5468. return retval;
  5469. }
  5470. /**
  5471. * sys_sched_getparam - get the RT priority of a thread
  5472. * @pid: the pid in question.
  5473. * @param: structure containing the RT priority.
  5474. */
  5475. SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
  5476. {
  5477. struct sched_param lp;
  5478. struct task_struct *p;
  5479. int retval;
  5480. if (!param || pid < 0)
  5481. return -EINVAL;
  5482. read_lock(&tasklist_lock);
  5483. p = find_process_by_pid(pid);
  5484. retval = -ESRCH;
  5485. if (!p)
  5486. goto out_unlock;
  5487. retval = security_task_getscheduler(p);
  5488. if (retval)
  5489. goto out_unlock;
  5490. lp.sched_priority = p->rt_priority;
  5491. read_unlock(&tasklist_lock);
  5492. /*
  5493. * This one might sleep, we cannot do it with a spinlock held ...
  5494. */
  5495. retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  5496. return retval;
  5497. out_unlock:
  5498. read_unlock(&tasklist_lock);
  5499. return retval;
  5500. }
  5501. long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
  5502. {
  5503. cpumask_var_t cpus_allowed, new_mask;
  5504. struct task_struct *p;
  5505. int retval;
  5506. get_online_cpus();
  5507. read_lock(&tasklist_lock);
  5508. p = find_process_by_pid(pid);
  5509. if (!p) {
  5510. read_unlock(&tasklist_lock);
  5511. put_online_cpus();
  5512. return -ESRCH;
  5513. }
  5514. /*
  5515. * It is not safe to call set_cpus_allowed with the
  5516. * tasklist_lock held. We will bump the task_struct's
  5517. * usage count and then drop tasklist_lock.
  5518. */
  5519. get_task_struct(p);
  5520. read_unlock(&tasklist_lock);
  5521. if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
  5522. retval = -ENOMEM;
  5523. goto out_put_task;
  5524. }
  5525. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
  5526. retval = -ENOMEM;
  5527. goto out_free_cpus_allowed;
  5528. }
  5529. retval = -EPERM;
  5530. if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
  5531. goto out_unlock;
  5532. retval = security_task_setscheduler(p, 0, NULL);
  5533. if (retval)
  5534. goto out_unlock;
  5535. cpuset_cpus_allowed(p, cpus_allowed);
  5536. cpumask_and(new_mask, in_mask, cpus_allowed);
  5537. again:
  5538. retval = set_cpus_allowed_ptr(p, new_mask);
  5539. if (!retval) {
  5540. cpuset_cpus_allowed(p, cpus_allowed);
  5541. if (!cpumask_subset(new_mask, cpus_allowed)) {
  5542. /*
  5543. * We must have raced with a concurrent cpuset
  5544. * update. Just reset the cpus_allowed to the
  5545. * cpuset's cpus_allowed
  5546. */
  5547. cpumask_copy(new_mask, cpus_allowed);
  5548. goto again;
  5549. }
  5550. }
  5551. out_unlock:
  5552. free_cpumask_var(new_mask);
  5553. out_free_cpus_allowed:
  5554. free_cpumask_var(cpus_allowed);
  5555. out_put_task:
  5556. put_task_struct(p);
  5557. put_online_cpus();
  5558. return retval;
  5559. }
  5560. static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  5561. struct cpumask *new_mask)
  5562. {
  5563. if (len < cpumask_size())
  5564. cpumask_clear(new_mask);
  5565. else if (len > cpumask_size())
  5566. len = cpumask_size();
  5567. return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  5568. }
  5569. /**
  5570. * sys_sched_setaffinity - set the cpu affinity of a process
  5571. * @pid: pid of the process
  5572. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  5573. * @user_mask_ptr: user-space pointer to the new cpu mask
  5574. */
  5575. SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
  5576. unsigned long __user *, user_mask_ptr)
  5577. {
  5578. cpumask_var_t new_mask;
  5579. int retval;
  5580. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  5581. return -ENOMEM;
  5582. retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
  5583. if (retval == 0)
  5584. retval = sched_setaffinity(pid, new_mask);
  5585. free_cpumask_var(new_mask);
  5586. return retval;
  5587. }
  5588. long sched_getaffinity(pid_t pid, struct cpumask *mask)
  5589. {
  5590. struct task_struct *p;
  5591. int retval;
  5592. get_online_cpus();
  5593. read_lock(&tasklist_lock);
  5594. retval = -ESRCH;
  5595. p = find_process_by_pid(pid);
  5596. if (!p)
  5597. goto out_unlock;
  5598. retval = security_task_getscheduler(p);
  5599. if (retval)
  5600. goto out_unlock;
  5601. cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
  5602. out_unlock:
  5603. read_unlock(&tasklist_lock);
  5604. put_online_cpus();
  5605. return retval;
  5606. }
  5607. /**
  5608. * sys_sched_getaffinity - get the cpu affinity of a process
  5609. * @pid: pid of the process
  5610. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  5611. * @user_mask_ptr: user-space pointer to hold the current cpu mask
  5612. */
  5613. SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
  5614. unsigned long __user *, user_mask_ptr)
  5615. {
  5616. int ret;
  5617. cpumask_var_t mask;
  5618. if (len < cpumask_size())
  5619. return -EINVAL;
  5620. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  5621. return -ENOMEM;
  5622. ret = sched_getaffinity(pid, mask);
  5623. if (ret == 0) {
  5624. if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
  5625. ret = -EFAULT;
  5626. else
  5627. ret = cpumask_size();
  5628. }
  5629. free_cpumask_var(mask);
  5630. return ret;
  5631. }
  5632. /**
  5633. * sys_sched_yield - yield the current processor to other threads.
  5634. *
  5635. * This function yields the current CPU to other tasks. If there are no
  5636. * other threads running on this CPU then this function will return.
  5637. */
  5638. SYSCALL_DEFINE0(sched_yield)
  5639. {
  5640. struct rq *rq = this_rq_lock();
  5641. schedstat_inc(rq, yld_count);
  5642. current->sched_class->yield_task(rq);
  5643. /*
  5644. * Since we are going to call schedule() anyway, there's
  5645. * no need to preempt or enable interrupts:
  5646. */
  5647. __release(rq->lock);
  5648. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  5649. _raw_spin_unlock(&rq->lock);
  5650. preempt_enable_no_resched();
  5651. schedule();
  5652. return 0;
  5653. }
  5654. static inline int should_resched(void)
  5655. {
  5656. return need_resched() && !(preempt_count() & PREEMPT_ACTIVE);
  5657. }
  5658. static void __cond_resched(void)
  5659. {
  5660. add_preempt_count(PREEMPT_ACTIVE);
  5661. schedule();
  5662. sub_preempt_count(PREEMPT_ACTIVE);
  5663. }
  5664. int __sched _cond_resched(void)
  5665. {
  5666. if (should_resched()) {
  5667. __cond_resched();
  5668. return 1;
  5669. }
  5670. return 0;
  5671. }
  5672. EXPORT_SYMBOL(_cond_resched);
  5673. /*
  5674. * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
  5675. * call schedule, and on return reacquire the lock.
  5676. *
  5677. * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  5678. * operations here to prevent schedule() from being called twice (once via
  5679. * spin_unlock(), once by hand).
  5680. */
  5681. int __cond_resched_lock(spinlock_t *lock)
  5682. {
  5683. int resched = should_resched();
  5684. int ret = 0;
  5685. lockdep_assert_held(lock);
  5686. if (spin_needbreak(lock) || resched) {
  5687. spin_unlock(lock);
  5688. if (resched)
  5689. __cond_resched();
  5690. else
  5691. cpu_relax();
  5692. ret = 1;
  5693. spin_lock(lock);
  5694. }
  5695. return ret;
  5696. }
  5697. EXPORT_SYMBOL(__cond_resched_lock);
  5698. int __sched __cond_resched_softirq(void)
  5699. {
  5700. BUG_ON(!in_softirq());
  5701. if (should_resched()) {
  5702. local_bh_enable();
  5703. __cond_resched();
  5704. local_bh_disable();
  5705. return 1;
  5706. }
  5707. return 0;
  5708. }
  5709. EXPORT_SYMBOL(__cond_resched_softirq);
  5710. /**
  5711. * yield - yield the current processor to other threads.
  5712. *
  5713. * This is a shortcut for kernel-space yielding - it marks the
  5714. * thread runnable and calls sys_sched_yield().
  5715. */
  5716. void __sched yield(void)
  5717. {
  5718. set_current_state(TASK_RUNNING);
  5719. sys_sched_yield();
  5720. }
  5721. EXPORT_SYMBOL(yield);
  5722. /*
  5723. * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  5724. * that process accounting knows that this is a task in IO wait state.
  5725. *
  5726. * But don't do that if it is a deliberate, throttling IO wait (this task
  5727. * has set its backing_dev_info: the queue against which it should throttle)
  5728. */
  5729. void __sched io_schedule(void)
  5730. {
  5731. struct rq *rq = raw_rq();
  5732. delayacct_blkio_start();
  5733. atomic_inc(&rq->nr_iowait);
  5734. current->in_iowait = 1;
  5735. schedule();
  5736. current->in_iowait = 0;
  5737. atomic_dec(&rq->nr_iowait);
  5738. delayacct_blkio_end();
  5739. }
  5740. EXPORT_SYMBOL(io_schedule);
  5741. long __sched io_schedule_timeout(long timeout)
  5742. {
  5743. struct rq *rq = raw_rq();
  5744. long ret;
  5745. delayacct_blkio_start();
  5746. atomic_inc(&rq->nr_iowait);
  5747. current->in_iowait = 1;
  5748. ret = schedule_timeout(timeout);
  5749. current->in_iowait = 0;
  5750. atomic_dec(&rq->nr_iowait);
  5751. delayacct_blkio_end();
  5752. return ret;
  5753. }
  5754. /**
  5755. * sys_sched_get_priority_max - return maximum RT priority.
  5756. * @policy: scheduling class.
  5757. *
  5758. * this syscall returns the maximum rt_priority that can be used
  5759. * by a given scheduling class.
  5760. */
  5761. SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
  5762. {
  5763. int ret = -EINVAL;
  5764. switch (policy) {
  5765. case SCHED_FIFO:
  5766. case SCHED_RR:
  5767. ret = MAX_USER_RT_PRIO-1;
  5768. break;
  5769. case SCHED_NORMAL:
  5770. case SCHED_BATCH:
  5771. case SCHED_IDLE:
  5772. ret = 0;
  5773. break;
  5774. }
  5775. return ret;
  5776. }
  5777. /**
  5778. * sys_sched_get_priority_min - return minimum RT priority.
  5779. * @policy: scheduling class.
  5780. *
  5781. * this syscall returns the minimum rt_priority that can be used
  5782. * by a given scheduling class.
  5783. */
  5784. SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
  5785. {
  5786. int ret = -EINVAL;
  5787. switch (policy) {
  5788. case SCHED_FIFO:
  5789. case SCHED_RR:
  5790. ret = 1;
  5791. break;
  5792. case SCHED_NORMAL:
  5793. case SCHED_BATCH:
  5794. case SCHED_IDLE:
  5795. ret = 0;
  5796. }
  5797. return ret;
  5798. }
  5799. /**
  5800. * sys_sched_rr_get_interval - return the default timeslice of a process.
  5801. * @pid: pid of the process.
  5802. * @interval: userspace pointer to the timeslice value.
  5803. *
  5804. * this syscall writes the default timeslice value of a given process
  5805. * into the user-space timespec buffer. A value of '0' means infinity.
  5806. */
  5807. SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
  5808. struct timespec __user *, interval)
  5809. {
  5810. struct task_struct *p;
  5811. unsigned int time_slice;
  5812. int retval;
  5813. struct timespec t;
  5814. if (pid < 0)
  5815. return -EINVAL;
  5816. retval = -ESRCH;
  5817. read_lock(&tasklist_lock);
  5818. p = find_process_by_pid(pid);
  5819. if (!p)
  5820. goto out_unlock;
  5821. retval = security_task_getscheduler(p);
  5822. if (retval)
  5823. goto out_unlock;
  5824. /*
  5825. * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
  5826. * tasks that are on an otherwise idle runqueue:
  5827. */
  5828. time_slice = 0;
  5829. if (p->policy == SCHED_RR) {
  5830. time_slice = DEF_TIMESLICE;
  5831. } else if (p->policy != SCHED_FIFO) {
  5832. struct sched_entity *se = &p->se;
  5833. unsigned long flags;
  5834. struct rq *rq;
  5835. rq = task_rq_lock(p, &flags);
  5836. if (rq->cfs.load.weight)
  5837. time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
  5838. task_rq_unlock(rq, &flags);
  5839. }
  5840. read_unlock(&tasklist_lock);
  5841. jiffies_to_timespec(time_slice, &t);
  5842. retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  5843. return retval;
  5844. out_unlock:
  5845. read_unlock(&tasklist_lock);
  5846. return retval;
  5847. }
  5848. static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
  5849. void sched_show_task(struct task_struct *p)
  5850. {
  5851. unsigned long free = 0;
  5852. unsigned state;
  5853. state = p->state ? __ffs(p->state) + 1 : 0;
  5854. printk(KERN_INFO "%-13.13s %c", p->comm,
  5855. state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
  5856. #if BITS_PER_LONG == 32
  5857. if (state == TASK_RUNNING)
  5858. printk(KERN_CONT " running ");
  5859. else
  5860. printk(KERN_CONT " %08lx ", thread_saved_pc(p));
  5861. #else
  5862. if (state == TASK_RUNNING)
  5863. printk(KERN_CONT " running task ");
  5864. else
  5865. printk(KERN_CONT " %016lx ", thread_saved_pc(p));
  5866. #endif
  5867. #ifdef CONFIG_DEBUG_STACK_USAGE
  5868. free = stack_not_used(p);
  5869. #endif
  5870. printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
  5871. task_pid_nr(p), task_pid_nr(p->real_parent),
  5872. (unsigned long)task_thread_info(p)->flags);
  5873. show_stack(p, NULL);
  5874. }
  5875. void show_state_filter(unsigned long state_filter)
  5876. {
  5877. struct task_struct *g, *p;
  5878. #if BITS_PER_LONG == 32
  5879. printk(KERN_INFO
  5880. " task PC stack pid father\n");
  5881. #else
  5882. printk(KERN_INFO
  5883. " task PC stack pid father\n");
  5884. #endif
  5885. read_lock(&tasklist_lock);
  5886. do_each_thread(g, p) {
  5887. /*
  5888. * reset the NMI-timeout, listing all files on a slow
  5889. * console might take alot of time:
  5890. */
  5891. touch_nmi_watchdog();
  5892. if (!state_filter || (p->state & state_filter))
  5893. sched_show_task(p);
  5894. } while_each_thread(g, p);
  5895. touch_all_softlockup_watchdogs();
  5896. #ifdef CONFIG_SCHED_DEBUG
  5897. sysrq_sched_debug_show();
  5898. #endif
  5899. read_unlock(&tasklist_lock);
  5900. /*
  5901. * Only show locks if all tasks are dumped:
  5902. */
  5903. if (state_filter == -1)
  5904. debug_show_all_locks();
  5905. }
  5906. void __cpuinit init_idle_bootup_task(struct task_struct *idle)
  5907. {
  5908. idle->sched_class = &idle_sched_class;
  5909. }
  5910. /**
  5911. * init_idle - set up an idle thread for a given CPU
  5912. * @idle: task in question
  5913. * @cpu: cpu the idle task belongs to
  5914. *
  5915. * NOTE: this function does not set the idle thread's NEED_RESCHED
  5916. * flag, to make booting more robust.
  5917. */
  5918. void __cpuinit init_idle(struct task_struct *idle, int cpu)
  5919. {
  5920. struct rq *rq = cpu_rq(cpu);
  5921. unsigned long flags;
  5922. spin_lock_irqsave(&rq->lock, flags);
  5923. __sched_fork(idle);
  5924. idle->se.exec_start = sched_clock();
  5925. idle->prio = idle->normal_prio = MAX_PRIO;
  5926. cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
  5927. __set_task_cpu(idle, cpu);
  5928. rq->curr = rq->idle = idle;
  5929. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  5930. idle->oncpu = 1;
  5931. #endif
  5932. spin_unlock_irqrestore(&rq->lock, flags);
  5933. /* Set the preempt count _outside_ the spinlocks! */
  5934. #if defined(CONFIG_PREEMPT)
  5935. task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
  5936. #else
  5937. task_thread_info(idle)->preempt_count = 0;
  5938. #endif
  5939. /*
  5940. * The idle tasks have their own, simple scheduling class:
  5941. */
  5942. idle->sched_class = &idle_sched_class;
  5943. ftrace_graph_init_task(idle);
  5944. }
  5945. /*
  5946. * In a system that switches off the HZ timer nohz_cpu_mask
  5947. * indicates which cpus entered this state. This is used
  5948. * in the rcu update to wait only for active cpus. For system
  5949. * which do not switch off the HZ timer nohz_cpu_mask should
  5950. * always be CPU_BITS_NONE.
  5951. */
  5952. cpumask_var_t nohz_cpu_mask;
  5953. /*
  5954. * Increase the granularity value when there are more CPUs,
  5955. * because with more CPUs the 'effective latency' as visible
  5956. * to users decreases. But the relationship is not linear,
  5957. * so pick a second-best guess by going with the log2 of the
  5958. * number of CPUs.
  5959. *
  5960. * This idea comes from the SD scheduler of Con Kolivas:
  5961. */
  5962. static inline void sched_init_granularity(void)
  5963. {
  5964. unsigned int factor = 1 + ilog2(num_online_cpus());
  5965. const unsigned long limit = 200000000;
  5966. sysctl_sched_min_granularity *= factor;
  5967. if (sysctl_sched_min_granularity > limit)
  5968. sysctl_sched_min_granularity = limit;
  5969. sysctl_sched_latency *= factor;
  5970. if (sysctl_sched_latency > limit)
  5971. sysctl_sched_latency = limit;
  5972. sysctl_sched_wakeup_granularity *= factor;
  5973. sysctl_sched_shares_ratelimit *= factor;
  5974. }
  5975. #ifdef CONFIG_SMP
  5976. /*
  5977. * This is how migration works:
  5978. *
  5979. * 1) we queue a struct migration_req structure in the source CPU's
  5980. * runqueue and wake up that CPU's migration thread.
  5981. * 2) we down() the locked semaphore => thread blocks.
  5982. * 3) migration thread wakes up (implicitly it forces the migrated
  5983. * thread off the CPU)
  5984. * 4) it gets the migration request and checks whether the migrated
  5985. * task is still in the wrong runqueue.
  5986. * 5) if it's in the wrong runqueue then the migration thread removes
  5987. * it and puts it into the right queue.
  5988. * 6) migration thread up()s the semaphore.
  5989. * 7) we wake up and the migration is done.
  5990. */
  5991. /*
  5992. * Change a given task's CPU affinity. Migrate the thread to a
  5993. * proper CPU and schedule it away if the CPU it's executing on
  5994. * is removed from the allowed bitmask.
  5995. *
  5996. * NOTE: the caller must have a valid reference to the task, the
  5997. * task must not exit() & deallocate itself prematurely. The
  5998. * call is not atomic; no spinlocks may be held.
  5999. */
  6000. int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
  6001. {
  6002. struct migration_req req;
  6003. unsigned long flags;
  6004. struct rq *rq;
  6005. int ret = 0;
  6006. rq = task_rq_lock(p, &flags);
  6007. if (!cpumask_intersects(new_mask, cpu_online_mask)) {
  6008. ret = -EINVAL;
  6009. goto out;
  6010. }
  6011. if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
  6012. !cpumask_equal(&p->cpus_allowed, new_mask))) {
  6013. ret = -EINVAL;
  6014. goto out;
  6015. }
  6016. if (p->sched_class->set_cpus_allowed)
  6017. p->sched_class->set_cpus_allowed(p, new_mask);
  6018. else {
  6019. cpumask_copy(&p->cpus_allowed, new_mask);
  6020. p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
  6021. }
  6022. /* Can the task run on the task's current CPU? If so, we're done */
  6023. if (cpumask_test_cpu(task_cpu(p), new_mask))
  6024. goto out;
  6025. if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) {
  6026. /* Need help from migration thread: drop lock and wait. */
  6027. struct task_struct *mt = rq->migration_thread;
  6028. get_task_struct(mt);
  6029. task_rq_unlock(rq, &flags);
  6030. wake_up_process(rq->migration_thread);
  6031. put_task_struct(mt);
  6032. wait_for_completion(&req.done);
  6033. tlb_migrate_finish(p->mm);
  6034. return 0;
  6035. }
  6036. out:
  6037. task_rq_unlock(rq, &flags);
  6038. return ret;
  6039. }
  6040. EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
  6041. /*
  6042. * Move (not current) task off this cpu, onto dest cpu. We're doing
  6043. * this because either it can't run here any more (set_cpus_allowed()
  6044. * away from this CPU, or CPU going down), or because we're
  6045. * attempting to rebalance this task on exec (sched_exec).
  6046. *
  6047. * So we race with normal scheduler movements, but that's OK, as long
  6048. * as the task is no longer on this CPU.
  6049. *
  6050. * Returns non-zero if task was successfully migrated.
  6051. */
  6052. static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
  6053. {
  6054. struct rq *rq_dest, *rq_src;
  6055. int ret = 0, on_rq;
  6056. if (unlikely(!cpu_active(dest_cpu)))
  6057. return ret;
  6058. rq_src = cpu_rq(src_cpu);
  6059. rq_dest = cpu_rq(dest_cpu);
  6060. double_rq_lock(rq_src, rq_dest);
  6061. /* Already moved. */
  6062. if (task_cpu(p) != src_cpu)
  6063. goto done;
  6064. /* Affinity changed (again). */
  6065. if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
  6066. goto fail;
  6067. on_rq = p->se.on_rq;
  6068. if (on_rq)
  6069. deactivate_task(rq_src, p, 0);
  6070. set_task_cpu(p, dest_cpu);
  6071. if (on_rq) {
  6072. activate_task(rq_dest, p, 0);
  6073. check_preempt_curr(rq_dest, p, 0);
  6074. }
  6075. done:
  6076. ret = 1;
  6077. fail:
  6078. double_rq_unlock(rq_src, rq_dest);
  6079. return ret;
  6080. }
  6081. #define RCU_MIGRATION_IDLE 0
  6082. #define RCU_MIGRATION_NEED_QS 1
  6083. #define RCU_MIGRATION_GOT_QS 2
  6084. #define RCU_MIGRATION_MUST_SYNC 3
  6085. /*
  6086. * migration_thread - this is a highprio system thread that performs
  6087. * thread migration by bumping thread off CPU then 'pushing' onto
  6088. * another runqueue.
  6089. */
  6090. static int migration_thread(void *data)
  6091. {
  6092. int badcpu;
  6093. int cpu = (long)data;
  6094. struct rq *rq;
  6095. rq = cpu_rq(cpu);
  6096. BUG_ON(rq->migration_thread != current);
  6097. set_current_state(TASK_INTERRUPTIBLE);
  6098. while (!kthread_should_stop()) {
  6099. struct migration_req *req;
  6100. struct list_head *head;
  6101. spin_lock_irq(&rq->lock);
  6102. if (cpu_is_offline(cpu)) {
  6103. spin_unlock_irq(&rq->lock);
  6104. break;
  6105. }
  6106. if (rq->active_balance) {
  6107. active_load_balance(rq, cpu);
  6108. rq->active_balance = 0;
  6109. }
  6110. head = &rq->migration_queue;
  6111. if (list_empty(head)) {
  6112. spin_unlock_irq(&rq->lock);
  6113. schedule();
  6114. set_current_state(TASK_INTERRUPTIBLE);
  6115. continue;
  6116. }
  6117. req = list_entry(head->next, struct migration_req, list);
  6118. list_del_init(head->next);
  6119. if (req->task != NULL) {
  6120. spin_unlock(&rq->lock);
  6121. __migrate_task(req->task, cpu, req->dest_cpu);
  6122. } else if (likely(cpu == (badcpu = smp_processor_id()))) {
  6123. req->dest_cpu = RCU_MIGRATION_GOT_QS;
  6124. spin_unlock(&rq->lock);
  6125. } else {
  6126. req->dest_cpu = RCU_MIGRATION_MUST_SYNC;
  6127. spin_unlock(&rq->lock);
  6128. WARN_ONCE(1, "migration_thread() on CPU %d, expected %d\n", badcpu, cpu);
  6129. }
  6130. local_irq_enable();
  6131. complete(&req->done);
  6132. }
  6133. __set_current_state(TASK_RUNNING);
  6134. return 0;
  6135. }
  6136. #ifdef CONFIG_HOTPLUG_CPU
  6137. static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
  6138. {
  6139. int ret;
  6140. local_irq_disable();
  6141. ret = __migrate_task(p, src_cpu, dest_cpu);
  6142. local_irq_enable();
  6143. return ret;
  6144. }
  6145. /*
  6146. * Figure out where task on dead CPU should go, use force if necessary.
  6147. */
  6148. static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
  6149. {
  6150. int dest_cpu;
  6151. const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(dead_cpu));
  6152. again:
  6153. /* Look for allowed, online CPU in same node. */
  6154. for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask)
  6155. if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
  6156. goto move;
  6157. /* Any allowed, online CPU? */
  6158. dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask);
  6159. if (dest_cpu < nr_cpu_ids)
  6160. goto move;
  6161. /* No more Mr. Nice Guy. */
  6162. if (dest_cpu >= nr_cpu_ids) {
  6163. cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
  6164. dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed);
  6165. /*
  6166. * Don't tell them about moving exiting tasks or
  6167. * kernel threads (both mm NULL), since they never
  6168. * leave kernel.
  6169. */
  6170. if (p->mm && printk_ratelimit()) {
  6171. printk(KERN_INFO "process %d (%s) no "
  6172. "longer affine to cpu%d\n",
  6173. task_pid_nr(p), p->comm, dead_cpu);
  6174. }
  6175. }
  6176. move:
  6177. /* It can have affinity changed while we were choosing. */
  6178. if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
  6179. goto again;
  6180. }
  6181. /*
  6182. * While a dead CPU has no uninterruptible tasks queued at this point,
  6183. * it might still have a nonzero ->nr_uninterruptible counter, because
  6184. * for performance reasons the counter is not stricly tracking tasks to
  6185. * their home CPUs. So we just add the counter to another CPU's counter,
  6186. * to keep the global sum constant after CPU-down:
  6187. */
  6188. static void migrate_nr_uninterruptible(struct rq *rq_src)
  6189. {
  6190. struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask));
  6191. unsigned long flags;
  6192. local_irq_save(flags);
  6193. double_rq_lock(rq_src, rq_dest);
  6194. rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
  6195. rq_src->nr_uninterruptible = 0;
  6196. double_rq_unlock(rq_src, rq_dest);
  6197. local_irq_restore(flags);
  6198. }
  6199. /* Run through task list and migrate tasks from the dead cpu. */
  6200. static void migrate_live_tasks(int src_cpu)
  6201. {
  6202. struct task_struct *p, *t;
  6203. read_lock(&tasklist_lock);
  6204. do_each_thread(t, p) {
  6205. if (p == current)
  6206. continue;
  6207. if (task_cpu(p) == src_cpu)
  6208. move_task_off_dead_cpu(src_cpu, p);
  6209. } while_each_thread(t, p);
  6210. read_unlock(&tasklist_lock);
  6211. }
  6212. /*
  6213. * Schedules idle task to be the next runnable task on current CPU.
  6214. * It does so by boosting its priority to highest possible.
  6215. * Used by CPU offline code.
  6216. */
  6217. void sched_idle_next(void)
  6218. {
  6219. int this_cpu = smp_processor_id();
  6220. struct rq *rq = cpu_rq(this_cpu);
  6221. struct task_struct *p = rq->idle;
  6222. unsigned long flags;
  6223. /* cpu has to be offline */
  6224. BUG_ON(cpu_online(this_cpu));
  6225. /*
  6226. * Strictly not necessary since rest of the CPUs are stopped by now
  6227. * and interrupts disabled on the current cpu.
  6228. */
  6229. spin_lock_irqsave(&rq->lock, flags);
  6230. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  6231. update_rq_clock(rq);
  6232. activate_task(rq, p, 0);
  6233. spin_unlock_irqrestore(&rq->lock, flags);
  6234. }
  6235. /*
  6236. * Ensures that the idle task is using init_mm right before its cpu goes
  6237. * offline.
  6238. */
  6239. void idle_task_exit(void)
  6240. {
  6241. struct mm_struct *mm = current->active_mm;
  6242. BUG_ON(cpu_online(smp_processor_id()));
  6243. if (mm != &init_mm)
  6244. switch_mm(mm, &init_mm, current);
  6245. mmdrop(mm);
  6246. }
  6247. /* called under rq->lock with disabled interrupts */
  6248. static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
  6249. {
  6250. struct rq *rq = cpu_rq(dead_cpu);
  6251. /* Must be exiting, otherwise would be on tasklist. */
  6252. BUG_ON(!p->exit_state);
  6253. /* Cannot have done final schedule yet: would have vanished. */
  6254. BUG_ON(p->state == TASK_DEAD);
  6255. get_task_struct(p);
  6256. /*
  6257. * Drop lock around migration; if someone else moves it,
  6258. * that's OK. No task can be added to this CPU, so iteration is
  6259. * fine.
  6260. */
  6261. spin_unlock_irq(&rq->lock);
  6262. move_task_off_dead_cpu(dead_cpu, p);
  6263. spin_lock_irq(&rq->lock);
  6264. put_task_struct(p);
  6265. }
  6266. /* release_task() removes task from tasklist, so we won't find dead tasks. */
  6267. static void migrate_dead_tasks(unsigned int dead_cpu)
  6268. {
  6269. struct rq *rq = cpu_rq(dead_cpu);
  6270. struct task_struct *next;
  6271. for ( ; ; ) {
  6272. if (!rq->nr_running)
  6273. break;
  6274. update_rq_clock(rq);
  6275. next = pick_next_task(rq);
  6276. if (!next)
  6277. break;
  6278. next->sched_class->put_prev_task(rq, next);
  6279. migrate_dead(dead_cpu, next);
  6280. }
  6281. }
  6282. /*
  6283. * remove the tasks which were accounted by rq from calc_load_tasks.
  6284. */
  6285. static void calc_global_load_remove(struct rq *rq)
  6286. {
  6287. atomic_long_sub(rq->calc_load_active, &calc_load_tasks);
  6288. rq->calc_load_active = 0;
  6289. }
  6290. #endif /* CONFIG_HOTPLUG_CPU */
  6291. #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
  6292. static struct ctl_table sd_ctl_dir[] = {
  6293. {
  6294. .procname = "sched_domain",
  6295. .mode = 0555,
  6296. },
  6297. {0, },
  6298. };
  6299. static struct ctl_table sd_ctl_root[] = {
  6300. {
  6301. .ctl_name = CTL_KERN,
  6302. .procname = "kernel",
  6303. .mode = 0555,
  6304. .child = sd_ctl_dir,
  6305. },
  6306. {0, },
  6307. };
  6308. static struct ctl_table *sd_alloc_ctl_entry(int n)
  6309. {
  6310. struct ctl_table *entry =
  6311. kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
  6312. return entry;
  6313. }
  6314. static void sd_free_ctl_entry(struct ctl_table **tablep)
  6315. {
  6316. struct ctl_table *entry;
  6317. /*
  6318. * In the intermediate directories, both the child directory and
  6319. * procname are dynamically allocated and could fail but the mode
  6320. * will always be set. In the lowest directory the names are
  6321. * static strings and all have proc handlers.
  6322. */
  6323. for (entry = *tablep; entry->mode; entry++) {
  6324. if (entry->child)
  6325. sd_free_ctl_entry(&entry->child);
  6326. if (entry->proc_handler == NULL)
  6327. kfree(entry->procname);
  6328. }
  6329. kfree(*tablep);
  6330. *tablep = NULL;
  6331. }
  6332. static void
  6333. set_table_entry(struct ctl_table *entry,
  6334. const char *procname, void *data, int maxlen,
  6335. mode_t mode, proc_handler *proc_handler)
  6336. {
  6337. entry->procname = procname;
  6338. entry->data = data;
  6339. entry->maxlen = maxlen;
  6340. entry->mode = mode;
  6341. entry->proc_handler = proc_handler;
  6342. }
  6343. static struct ctl_table *
  6344. sd_alloc_ctl_domain_table(struct sched_domain *sd)
  6345. {
  6346. struct ctl_table *table = sd_alloc_ctl_entry(13);
  6347. if (table == NULL)
  6348. return NULL;
  6349. set_table_entry(&table[0], "min_interval", &sd->min_interval,
  6350. sizeof(long), 0644, proc_doulongvec_minmax);
  6351. set_table_entry(&table[1], "max_interval", &sd->max_interval,
  6352. sizeof(long), 0644, proc_doulongvec_minmax);
  6353. set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
  6354. sizeof(int), 0644, proc_dointvec_minmax);
  6355. set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
  6356. sizeof(int), 0644, proc_dointvec_minmax);
  6357. set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
  6358. sizeof(int), 0644, proc_dointvec_minmax);
  6359. set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
  6360. sizeof(int), 0644, proc_dointvec_minmax);
  6361. set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
  6362. sizeof(int), 0644, proc_dointvec_minmax);
  6363. set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
  6364. sizeof(int), 0644, proc_dointvec_minmax);
  6365. set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
  6366. sizeof(int), 0644, proc_dointvec_minmax);
  6367. set_table_entry(&table[9], "cache_nice_tries",
  6368. &sd->cache_nice_tries,
  6369. sizeof(int), 0644, proc_dointvec_minmax);
  6370. set_table_entry(&table[10], "flags", &sd->flags,
  6371. sizeof(int), 0644, proc_dointvec_minmax);
  6372. set_table_entry(&table[11], "name", sd->name,
  6373. CORENAME_MAX_SIZE, 0444, proc_dostring);
  6374. /* &table[12] is terminator */
  6375. return table;
  6376. }
  6377. static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
  6378. {
  6379. struct ctl_table *entry, *table;
  6380. struct sched_domain *sd;
  6381. int domain_num = 0, i;
  6382. char buf[32];
  6383. for_each_domain(cpu, sd)
  6384. domain_num++;
  6385. entry = table = sd_alloc_ctl_entry(domain_num + 1);
  6386. if (table == NULL)
  6387. return NULL;
  6388. i = 0;
  6389. for_each_domain(cpu, sd) {
  6390. snprintf(buf, 32, "domain%d", i);
  6391. entry->procname = kstrdup(buf, GFP_KERNEL);
  6392. entry->mode = 0555;
  6393. entry->child = sd_alloc_ctl_domain_table(sd);
  6394. entry++;
  6395. i++;
  6396. }
  6397. return table;
  6398. }
  6399. static struct ctl_table_header *sd_sysctl_header;
  6400. static void register_sched_domain_sysctl(void)
  6401. {
  6402. int i, cpu_num = num_online_cpus();
  6403. struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
  6404. char buf[32];
  6405. WARN_ON(sd_ctl_dir[0].child);
  6406. sd_ctl_dir[0].child = entry;
  6407. if (entry == NULL)
  6408. return;
  6409. for_each_online_cpu(i) {
  6410. snprintf(buf, 32, "cpu%d", i);
  6411. entry->procname = kstrdup(buf, GFP_KERNEL);
  6412. entry->mode = 0555;
  6413. entry->child = sd_alloc_ctl_cpu_table(i);
  6414. entry++;
  6415. }
  6416. WARN_ON(sd_sysctl_header);
  6417. sd_sysctl_header = register_sysctl_table(sd_ctl_root);
  6418. }
  6419. /* may be called multiple times per register */
  6420. static void unregister_sched_domain_sysctl(void)
  6421. {
  6422. if (sd_sysctl_header)
  6423. unregister_sysctl_table(sd_sysctl_header);
  6424. sd_sysctl_header = NULL;
  6425. if (sd_ctl_dir[0].child)
  6426. sd_free_ctl_entry(&sd_ctl_dir[0].child);
  6427. }
  6428. #else
  6429. static void register_sched_domain_sysctl(void)
  6430. {
  6431. }
  6432. static void unregister_sched_domain_sysctl(void)
  6433. {
  6434. }
  6435. #endif
  6436. static void set_rq_online(struct rq *rq)
  6437. {
  6438. if (!rq->online) {
  6439. const struct sched_class *class;
  6440. cpumask_set_cpu(rq->cpu, rq->rd->online);
  6441. rq->online = 1;
  6442. for_each_class(class) {
  6443. if (class->rq_online)
  6444. class->rq_online(rq);
  6445. }
  6446. }
  6447. }
  6448. static void set_rq_offline(struct rq *rq)
  6449. {
  6450. if (rq->online) {
  6451. const struct sched_class *class;
  6452. for_each_class(class) {
  6453. if (class->rq_offline)
  6454. class->rq_offline(rq);
  6455. }
  6456. cpumask_clear_cpu(rq->cpu, rq->rd->online);
  6457. rq->online = 0;
  6458. }
  6459. }
  6460. /*
  6461. * migration_call - callback that gets triggered when a CPU is added.
  6462. * Here we can start up the necessary migration thread for the new CPU.
  6463. */
  6464. static int __cpuinit
  6465. migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  6466. {
  6467. struct task_struct *p;
  6468. int cpu = (long)hcpu;
  6469. unsigned long flags;
  6470. struct rq *rq;
  6471. switch (action) {
  6472. case CPU_UP_PREPARE:
  6473. case CPU_UP_PREPARE_FROZEN:
  6474. p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
  6475. if (IS_ERR(p))
  6476. return NOTIFY_BAD;
  6477. kthread_bind(p, cpu);
  6478. /* Must be high prio: stop_machine expects to yield to it. */
  6479. rq = task_rq_lock(p, &flags);
  6480. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  6481. task_rq_unlock(rq, &flags);
  6482. get_task_struct(p);
  6483. cpu_rq(cpu)->migration_thread = p;
  6484. rq->calc_load_update = calc_load_update;
  6485. break;
  6486. case CPU_ONLINE:
  6487. case CPU_ONLINE_FROZEN:
  6488. /* Strictly unnecessary, as first user will wake it. */
  6489. wake_up_process(cpu_rq(cpu)->migration_thread);
  6490. /* Update our root-domain */
  6491. rq = cpu_rq(cpu);
  6492. spin_lock_irqsave(&rq->lock, flags);
  6493. if (rq->rd) {
  6494. BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  6495. set_rq_online(rq);
  6496. }
  6497. spin_unlock_irqrestore(&rq->lock, flags);
  6498. break;
  6499. #ifdef CONFIG_HOTPLUG_CPU
  6500. case CPU_UP_CANCELED:
  6501. case CPU_UP_CANCELED_FROZEN:
  6502. if (!cpu_rq(cpu)->migration_thread)
  6503. break;
  6504. /* Unbind it from offline cpu so it can run. Fall thru. */
  6505. kthread_bind(cpu_rq(cpu)->migration_thread,
  6506. cpumask_any(cpu_online_mask));
  6507. kthread_stop(cpu_rq(cpu)->migration_thread);
  6508. put_task_struct(cpu_rq(cpu)->migration_thread);
  6509. cpu_rq(cpu)->migration_thread = NULL;
  6510. break;
  6511. case CPU_DEAD:
  6512. case CPU_DEAD_FROZEN:
  6513. cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
  6514. migrate_live_tasks(cpu);
  6515. rq = cpu_rq(cpu);
  6516. kthread_stop(rq->migration_thread);
  6517. put_task_struct(rq->migration_thread);
  6518. rq->migration_thread = NULL;
  6519. /* Idle task back to normal (off runqueue, low prio) */
  6520. spin_lock_irq(&rq->lock);
  6521. update_rq_clock(rq);
  6522. deactivate_task(rq, rq->idle, 0);
  6523. rq->idle->static_prio = MAX_PRIO;
  6524. __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
  6525. rq->idle->sched_class = &idle_sched_class;
  6526. migrate_dead_tasks(cpu);
  6527. spin_unlock_irq(&rq->lock);
  6528. cpuset_unlock();
  6529. migrate_nr_uninterruptible(rq);
  6530. BUG_ON(rq->nr_running != 0);
  6531. calc_global_load_remove(rq);
  6532. /*
  6533. * No need to migrate the tasks: it was best-effort if
  6534. * they didn't take sched_hotcpu_mutex. Just wake up
  6535. * the requestors.
  6536. */
  6537. spin_lock_irq(&rq->lock);
  6538. while (!list_empty(&rq->migration_queue)) {
  6539. struct migration_req *req;
  6540. req = list_entry(rq->migration_queue.next,
  6541. struct migration_req, list);
  6542. list_del_init(&req->list);
  6543. spin_unlock_irq(&rq->lock);
  6544. complete(&req->done);
  6545. spin_lock_irq(&rq->lock);
  6546. }
  6547. spin_unlock_irq(&rq->lock);
  6548. break;
  6549. case CPU_DYING:
  6550. case CPU_DYING_FROZEN:
  6551. /* Update our root-domain */
  6552. rq = cpu_rq(cpu);
  6553. spin_lock_irqsave(&rq->lock, flags);
  6554. if (rq->rd) {
  6555. BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  6556. set_rq_offline(rq);
  6557. }
  6558. spin_unlock_irqrestore(&rq->lock, flags);
  6559. break;
  6560. #endif
  6561. }
  6562. return NOTIFY_OK;
  6563. }
  6564. /*
  6565. * Register at high priority so that task migration (migrate_all_tasks)
  6566. * happens before everything else. This has to be lower priority than
  6567. * the notifier in the perf_counter subsystem, though.
  6568. */
  6569. static struct notifier_block __cpuinitdata migration_notifier = {
  6570. .notifier_call = migration_call,
  6571. .priority = 10
  6572. };
  6573. static int __init migration_init(void)
  6574. {
  6575. void *cpu = (void *)(long)smp_processor_id();
  6576. int err;
  6577. /* Start one for the boot CPU: */
  6578. err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  6579. BUG_ON(err == NOTIFY_BAD);
  6580. migration_call(&migration_notifier, CPU_ONLINE, cpu);
  6581. register_cpu_notifier(&migration_notifier);
  6582. return 0;
  6583. }
  6584. early_initcall(migration_init);
  6585. #endif
  6586. #ifdef CONFIG_SMP
  6587. #ifdef CONFIG_SCHED_DEBUG
  6588. static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
  6589. struct cpumask *groupmask)
  6590. {
  6591. struct sched_group *group = sd->groups;
  6592. char str[256];
  6593. cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
  6594. cpumask_clear(groupmask);
  6595. printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
  6596. if (!(sd->flags & SD_LOAD_BALANCE)) {
  6597. printk("does not load-balance\n");
  6598. if (sd->parent)
  6599. printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
  6600. " has parent");
  6601. return -1;
  6602. }
  6603. printk(KERN_CONT "span %s level %s\n", str, sd->name);
  6604. if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  6605. printk(KERN_ERR "ERROR: domain->span does not contain "
  6606. "CPU%d\n", cpu);
  6607. }
  6608. if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
  6609. printk(KERN_ERR "ERROR: domain->groups does not contain"
  6610. " CPU%d\n", cpu);
  6611. }
  6612. printk(KERN_DEBUG "%*s groups:", level + 1, "");
  6613. do {
  6614. if (!group) {
  6615. printk("\n");
  6616. printk(KERN_ERR "ERROR: group is NULL\n");
  6617. break;
  6618. }
  6619. if (!group->cpu_power) {
  6620. printk(KERN_CONT "\n");
  6621. printk(KERN_ERR "ERROR: domain->cpu_power not "
  6622. "set\n");
  6623. break;
  6624. }
  6625. if (!cpumask_weight(sched_group_cpus(group))) {
  6626. printk(KERN_CONT "\n");
  6627. printk(KERN_ERR "ERROR: empty group\n");
  6628. break;
  6629. }
  6630. if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
  6631. printk(KERN_CONT "\n");
  6632. printk(KERN_ERR "ERROR: repeated CPUs\n");
  6633. break;
  6634. }
  6635. cpumask_or(groupmask, groupmask, sched_group_cpus(group));
  6636. cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
  6637. printk(KERN_CONT " %s", str);
  6638. if (group->cpu_power != SCHED_LOAD_SCALE) {
  6639. printk(KERN_CONT " (cpu_power = %d)",
  6640. group->cpu_power);
  6641. }
  6642. group = group->next;
  6643. } while (group != sd->groups);
  6644. printk(KERN_CONT "\n");
  6645. if (!cpumask_equal(sched_domain_span(sd), groupmask))
  6646. printk(KERN_ERR "ERROR: groups don't span domain->span\n");
  6647. if (sd->parent &&
  6648. !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
  6649. printk(KERN_ERR "ERROR: parent span is not a superset "
  6650. "of domain->span\n");
  6651. return 0;
  6652. }
  6653. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  6654. {
  6655. cpumask_var_t groupmask;
  6656. int level = 0;
  6657. if (!sd) {
  6658. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  6659. return;
  6660. }
  6661. printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  6662. if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
  6663. printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
  6664. return;
  6665. }
  6666. for (;;) {
  6667. if (sched_domain_debug_one(sd, cpu, level, groupmask))
  6668. break;
  6669. level++;
  6670. sd = sd->parent;
  6671. if (!sd)
  6672. break;
  6673. }
  6674. free_cpumask_var(groupmask);
  6675. }
  6676. #else /* !CONFIG_SCHED_DEBUG */
  6677. # define sched_domain_debug(sd, cpu) do { } while (0)
  6678. #endif /* CONFIG_SCHED_DEBUG */
  6679. static int sd_degenerate(struct sched_domain *sd)
  6680. {
  6681. if (cpumask_weight(sched_domain_span(sd)) == 1)
  6682. return 1;
  6683. /* Following flags need at least 2 groups */
  6684. if (sd->flags & (SD_LOAD_BALANCE |
  6685. SD_BALANCE_NEWIDLE |
  6686. SD_BALANCE_FORK |
  6687. SD_BALANCE_EXEC |
  6688. SD_SHARE_CPUPOWER |
  6689. SD_SHARE_PKG_RESOURCES)) {
  6690. if (sd->groups != sd->groups->next)
  6691. return 0;
  6692. }
  6693. /* Following flags don't use groups */
  6694. if (sd->flags & (SD_WAKE_IDLE |
  6695. SD_WAKE_AFFINE |
  6696. SD_WAKE_BALANCE))
  6697. return 0;
  6698. return 1;
  6699. }
  6700. static int
  6701. sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  6702. {
  6703. unsigned long cflags = sd->flags, pflags = parent->flags;
  6704. if (sd_degenerate(parent))
  6705. return 1;
  6706. if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
  6707. return 0;
  6708. /* Does parent contain flags not in child? */
  6709. /* WAKE_BALANCE is a subset of WAKE_AFFINE */
  6710. if (cflags & SD_WAKE_AFFINE)
  6711. pflags &= ~SD_WAKE_BALANCE;
  6712. /* Flags needing groups don't count if only 1 group in parent */
  6713. if (parent->groups == parent->groups->next) {
  6714. pflags &= ~(SD_LOAD_BALANCE |
  6715. SD_BALANCE_NEWIDLE |
  6716. SD_BALANCE_FORK |
  6717. SD_BALANCE_EXEC |
  6718. SD_SHARE_CPUPOWER |
  6719. SD_SHARE_PKG_RESOURCES);
  6720. if (nr_node_ids == 1)
  6721. pflags &= ~SD_SERIALIZE;
  6722. }
  6723. if (~cflags & pflags)
  6724. return 0;
  6725. return 1;
  6726. }
  6727. static void free_rootdomain(struct root_domain *rd)
  6728. {
  6729. cpupri_cleanup(&rd->cpupri);
  6730. free_cpumask_var(rd->rto_mask);
  6731. free_cpumask_var(rd->online);
  6732. free_cpumask_var(rd->span);
  6733. kfree(rd);
  6734. }
  6735. static void rq_attach_root(struct rq *rq, struct root_domain *rd)
  6736. {
  6737. struct root_domain *old_rd = NULL;
  6738. unsigned long flags;
  6739. spin_lock_irqsave(&rq->lock, flags);
  6740. if (rq->rd) {
  6741. old_rd = rq->rd;
  6742. if (cpumask_test_cpu(rq->cpu, old_rd->online))
  6743. set_rq_offline(rq);
  6744. cpumask_clear_cpu(rq->cpu, old_rd->span);
  6745. /*
  6746. * If we dont want to free the old_rt yet then
  6747. * set old_rd to NULL to skip the freeing later
  6748. * in this function:
  6749. */
  6750. if (!atomic_dec_and_test(&old_rd->refcount))
  6751. old_rd = NULL;
  6752. }
  6753. atomic_inc(&rd->refcount);
  6754. rq->rd = rd;
  6755. cpumask_set_cpu(rq->cpu, rd->span);
  6756. if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
  6757. set_rq_online(rq);
  6758. spin_unlock_irqrestore(&rq->lock, flags);
  6759. if (old_rd)
  6760. free_rootdomain(old_rd);
  6761. }
  6762. static int init_rootdomain(struct root_domain *rd, bool bootmem)
  6763. {
  6764. gfp_t gfp = GFP_KERNEL;
  6765. memset(rd, 0, sizeof(*rd));
  6766. if (bootmem)
  6767. gfp = GFP_NOWAIT;
  6768. if (!alloc_cpumask_var(&rd->span, gfp))
  6769. goto out;
  6770. if (!alloc_cpumask_var(&rd->online, gfp))
  6771. goto free_span;
  6772. if (!alloc_cpumask_var(&rd->rto_mask, gfp))
  6773. goto free_online;
  6774. if (cpupri_init(&rd->cpupri, bootmem) != 0)
  6775. goto free_rto_mask;
  6776. return 0;
  6777. free_rto_mask:
  6778. free_cpumask_var(rd->rto_mask);
  6779. free_online:
  6780. free_cpumask_var(rd->online);
  6781. free_span:
  6782. free_cpumask_var(rd->span);
  6783. out:
  6784. return -ENOMEM;
  6785. }
  6786. static void init_defrootdomain(void)
  6787. {
  6788. init_rootdomain(&def_root_domain, true);
  6789. atomic_set(&def_root_domain.refcount, 1);
  6790. }
  6791. static struct root_domain *alloc_rootdomain(void)
  6792. {
  6793. struct root_domain *rd;
  6794. rd = kmalloc(sizeof(*rd), GFP_KERNEL);
  6795. if (!rd)
  6796. return NULL;
  6797. if (init_rootdomain(rd, false) != 0) {
  6798. kfree(rd);
  6799. return NULL;
  6800. }
  6801. return rd;
  6802. }
  6803. /*
  6804. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  6805. * hold the hotplug lock.
  6806. */
  6807. static void
  6808. cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
  6809. {
  6810. struct rq *rq = cpu_rq(cpu);
  6811. struct sched_domain *tmp;
  6812. /* Remove the sched domains which do not contribute to scheduling. */
  6813. for (tmp = sd; tmp; ) {
  6814. struct sched_domain *parent = tmp->parent;
  6815. if (!parent)
  6816. break;
  6817. if (sd_parent_degenerate(tmp, parent)) {
  6818. tmp->parent = parent->parent;
  6819. if (parent->parent)
  6820. parent->parent->child = tmp;
  6821. } else
  6822. tmp = tmp->parent;
  6823. }
  6824. if (sd && sd_degenerate(sd)) {
  6825. sd = sd->parent;
  6826. if (sd)
  6827. sd->child = NULL;
  6828. }
  6829. sched_domain_debug(sd, cpu);
  6830. rq_attach_root(rq, rd);
  6831. rcu_assign_pointer(rq->sd, sd);
  6832. }
  6833. /* cpus with isolated domains */
  6834. static cpumask_var_t cpu_isolated_map;
  6835. /* Setup the mask of cpus configured for isolated domains */
  6836. static int __init isolated_cpu_setup(char *str)
  6837. {
  6838. cpulist_parse(str, cpu_isolated_map);
  6839. return 1;
  6840. }
  6841. __setup("isolcpus=", isolated_cpu_setup);
  6842. /*
  6843. * init_sched_build_groups takes the cpumask we wish to span, and a pointer
  6844. * to a function which identifies what group(along with sched group) a CPU
  6845. * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
  6846. * (due to the fact that we keep track of groups covered with a struct cpumask).
  6847. *
  6848. * init_sched_build_groups will build a circular linked list of the groups
  6849. * covered by the given span, and will set each group's ->cpumask correctly,
  6850. * and ->cpu_power to 0.
  6851. */
  6852. static void
  6853. init_sched_build_groups(const struct cpumask *span,
  6854. const struct cpumask *cpu_map,
  6855. int (*group_fn)(int cpu, const struct cpumask *cpu_map,
  6856. struct sched_group **sg,
  6857. struct cpumask *tmpmask),
  6858. struct cpumask *covered, struct cpumask *tmpmask)
  6859. {
  6860. struct sched_group *first = NULL, *last = NULL;
  6861. int i;
  6862. cpumask_clear(covered);
  6863. for_each_cpu(i, span) {
  6864. struct sched_group *sg;
  6865. int group = group_fn(i, cpu_map, &sg, tmpmask);
  6866. int j;
  6867. if (cpumask_test_cpu(i, covered))
  6868. continue;
  6869. cpumask_clear(sched_group_cpus(sg));
  6870. sg->cpu_power = 0;
  6871. for_each_cpu(j, span) {
  6872. if (group_fn(j, cpu_map, NULL, tmpmask) != group)
  6873. continue;
  6874. cpumask_set_cpu(j, covered);
  6875. cpumask_set_cpu(j, sched_group_cpus(sg));
  6876. }
  6877. if (!first)
  6878. first = sg;
  6879. if (last)
  6880. last->next = sg;
  6881. last = sg;
  6882. }
  6883. last->next = first;
  6884. }
  6885. #define SD_NODES_PER_DOMAIN 16
  6886. #ifdef CONFIG_NUMA
  6887. /**
  6888. * find_next_best_node - find the next node to include in a sched_domain
  6889. * @node: node whose sched_domain we're building
  6890. * @used_nodes: nodes already in the sched_domain
  6891. *
  6892. * Find the next node to include in a given scheduling domain. Simply
  6893. * finds the closest node not already in the @used_nodes map.
  6894. *
  6895. * Should use nodemask_t.
  6896. */
  6897. static int find_next_best_node(int node, nodemask_t *used_nodes)
  6898. {
  6899. int i, n, val, min_val, best_node = 0;
  6900. min_val = INT_MAX;
  6901. for (i = 0; i < nr_node_ids; i++) {
  6902. /* Start at @node */
  6903. n = (node + i) % nr_node_ids;
  6904. if (!nr_cpus_node(n))
  6905. continue;
  6906. /* Skip already used nodes */
  6907. if (node_isset(n, *used_nodes))
  6908. continue;
  6909. /* Simple min distance search */
  6910. val = node_distance(node, n);
  6911. if (val < min_val) {
  6912. min_val = val;
  6913. best_node = n;
  6914. }
  6915. }
  6916. node_set(best_node, *used_nodes);
  6917. return best_node;
  6918. }
  6919. /**
  6920. * sched_domain_node_span - get a cpumask for a node's sched_domain
  6921. * @node: node whose cpumask we're constructing
  6922. * @span: resulting cpumask
  6923. *
  6924. * Given a node, construct a good cpumask for its sched_domain to span. It
  6925. * should be one that prevents unnecessary balancing, but also spreads tasks
  6926. * out optimally.
  6927. */
  6928. static void sched_domain_node_span(int node, struct cpumask *span)
  6929. {
  6930. nodemask_t used_nodes;
  6931. int i;
  6932. cpumask_clear(span);
  6933. nodes_clear(used_nodes);
  6934. cpumask_or(span, span, cpumask_of_node(node));
  6935. node_set(node, used_nodes);
  6936. for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
  6937. int next_node = find_next_best_node(node, &used_nodes);
  6938. cpumask_or(span, span, cpumask_of_node(next_node));
  6939. }
  6940. }
  6941. #endif /* CONFIG_NUMA */
  6942. int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
  6943. /*
  6944. * The cpus mask in sched_group and sched_domain hangs off the end.
  6945. *
  6946. * ( See the the comments in include/linux/sched.h:struct sched_group
  6947. * and struct sched_domain. )
  6948. */
  6949. struct static_sched_group {
  6950. struct sched_group sg;
  6951. DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
  6952. };
  6953. struct static_sched_domain {
  6954. struct sched_domain sd;
  6955. DECLARE_BITMAP(span, CONFIG_NR_CPUS);
  6956. };
  6957. struct s_data {
  6958. #ifdef CONFIG_NUMA
  6959. int sd_allnodes;
  6960. cpumask_var_t domainspan;
  6961. cpumask_var_t covered;
  6962. cpumask_var_t notcovered;
  6963. #endif
  6964. cpumask_var_t nodemask;
  6965. cpumask_var_t this_sibling_map;
  6966. cpumask_var_t this_core_map;
  6967. cpumask_var_t send_covered;
  6968. cpumask_var_t tmpmask;
  6969. struct sched_group **sched_group_nodes;
  6970. struct root_domain *rd;
  6971. };
  6972. enum s_alloc {
  6973. sa_sched_groups = 0,
  6974. sa_rootdomain,
  6975. sa_tmpmask,
  6976. sa_send_covered,
  6977. sa_this_core_map,
  6978. sa_this_sibling_map,
  6979. sa_nodemask,
  6980. sa_sched_group_nodes,
  6981. #ifdef CONFIG_NUMA
  6982. sa_notcovered,
  6983. sa_covered,
  6984. sa_domainspan,
  6985. #endif
  6986. sa_none,
  6987. };
  6988. /*
  6989. * SMT sched-domains:
  6990. */
  6991. #ifdef CONFIG_SCHED_SMT
  6992. static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
  6993. static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus);
  6994. static int
  6995. cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
  6996. struct sched_group **sg, struct cpumask *unused)
  6997. {
  6998. if (sg)
  6999. *sg = &per_cpu(sched_group_cpus, cpu).sg;
  7000. return cpu;
  7001. }
  7002. #endif /* CONFIG_SCHED_SMT */
  7003. /*
  7004. * multi-core sched-domains:
  7005. */
  7006. #ifdef CONFIG_SCHED_MC
  7007. static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
  7008. static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
  7009. #endif /* CONFIG_SCHED_MC */
  7010. #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
  7011. static int
  7012. cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
  7013. struct sched_group **sg, struct cpumask *mask)
  7014. {
  7015. int group;
  7016. cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
  7017. group = cpumask_first(mask);
  7018. if (sg)
  7019. *sg = &per_cpu(sched_group_core, group).sg;
  7020. return group;
  7021. }
  7022. #elif defined(CONFIG_SCHED_MC)
  7023. static int
  7024. cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
  7025. struct sched_group **sg, struct cpumask *unused)
  7026. {
  7027. if (sg)
  7028. *sg = &per_cpu(sched_group_core, cpu).sg;
  7029. return cpu;
  7030. }
  7031. #endif
  7032. static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
  7033. static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
  7034. static int
  7035. cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
  7036. struct sched_group **sg, struct cpumask *mask)
  7037. {
  7038. int group;
  7039. #ifdef CONFIG_SCHED_MC
  7040. cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
  7041. group = cpumask_first(mask);
  7042. #elif defined(CONFIG_SCHED_SMT)
  7043. cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
  7044. group = cpumask_first(mask);
  7045. #else
  7046. group = cpu;
  7047. #endif
  7048. if (sg)
  7049. *sg = &per_cpu(sched_group_phys, group).sg;
  7050. return group;
  7051. }
  7052. #ifdef CONFIG_NUMA
  7053. /*
  7054. * The init_sched_build_groups can't handle what we want to do with node
  7055. * groups, so roll our own. Now each node has its own list of groups which
  7056. * gets dynamically allocated.
  7057. */
  7058. static DEFINE_PER_CPU(struct static_sched_domain, node_domains);
  7059. static struct sched_group ***sched_group_nodes_bycpu;
  7060. static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains);
  7061. static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
  7062. static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
  7063. struct sched_group **sg,
  7064. struct cpumask *nodemask)
  7065. {
  7066. int group;
  7067. cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
  7068. group = cpumask_first(nodemask);
  7069. if (sg)
  7070. *sg = &per_cpu(sched_group_allnodes, group).sg;
  7071. return group;
  7072. }
  7073. static void init_numa_sched_groups_power(struct sched_group *group_head)
  7074. {
  7075. struct sched_group *sg = group_head;
  7076. int j;
  7077. if (!sg)
  7078. return;
  7079. do {
  7080. for_each_cpu(j, sched_group_cpus(sg)) {
  7081. struct sched_domain *sd;
  7082. sd = &per_cpu(phys_domains, j).sd;
  7083. if (j != group_first_cpu(sd->groups)) {
  7084. /*
  7085. * Only add "power" once for each
  7086. * physical package.
  7087. */
  7088. continue;
  7089. }
  7090. sg->cpu_power += sd->groups->cpu_power;
  7091. }
  7092. sg = sg->next;
  7093. } while (sg != group_head);
  7094. }
  7095. static int build_numa_sched_groups(struct s_data *d,
  7096. const struct cpumask *cpu_map, int num)
  7097. {
  7098. struct sched_domain *sd;
  7099. struct sched_group *sg, *prev;
  7100. int n, j;
  7101. cpumask_clear(d->covered);
  7102. cpumask_and(d->nodemask, cpumask_of_node(num), cpu_map);
  7103. if (cpumask_empty(d->nodemask)) {
  7104. d->sched_group_nodes[num] = NULL;
  7105. goto out;
  7106. }
  7107. sched_domain_node_span(num, d->domainspan);
  7108. cpumask_and(d->domainspan, d->domainspan, cpu_map);
  7109. sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
  7110. GFP_KERNEL, num);
  7111. if (!sg) {
  7112. printk(KERN_WARNING "Can not alloc domain group for node %d\n",
  7113. num);
  7114. return -ENOMEM;
  7115. }
  7116. d->sched_group_nodes[num] = sg;
  7117. for_each_cpu(j, d->nodemask) {
  7118. sd = &per_cpu(node_domains, j).sd;
  7119. sd->groups = sg;
  7120. }
  7121. sg->cpu_power = 0;
  7122. cpumask_copy(sched_group_cpus(sg), d->nodemask);
  7123. sg->next = sg;
  7124. cpumask_or(d->covered, d->covered, d->nodemask);
  7125. prev = sg;
  7126. for (j = 0; j < nr_node_ids; j++) {
  7127. n = (num + j) % nr_node_ids;
  7128. cpumask_complement(d->notcovered, d->covered);
  7129. cpumask_and(d->tmpmask, d->notcovered, cpu_map);
  7130. cpumask_and(d->tmpmask, d->tmpmask, d->domainspan);
  7131. if (cpumask_empty(d->tmpmask))
  7132. break;
  7133. cpumask_and(d->tmpmask, d->tmpmask, cpumask_of_node(n));
  7134. if (cpumask_empty(d->tmpmask))
  7135. continue;
  7136. sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
  7137. GFP_KERNEL, num);
  7138. if (!sg) {
  7139. printk(KERN_WARNING
  7140. "Can not alloc domain group for node %d\n", j);
  7141. return -ENOMEM;
  7142. }
  7143. sg->cpu_power = 0;
  7144. cpumask_copy(sched_group_cpus(sg), d->tmpmask);
  7145. sg->next = prev->next;
  7146. cpumask_or(d->covered, d->covered, d->tmpmask);
  7147. prev->next = sg;
  7148. prev = sg;
  7149. }
  7150. out:
  7151. return 0;
  7152. }
  7153. #endif /* CONFIG_NUMA */
  7154. #ifdef CONFIG_NUMA
  7155. /* Free memory allocated for various sched_group structures */
  7156. static void free_sched_groups(const struct cpumask *cpu_map,
  7157. struct cpumask *nodemask)
  7158. {
  7159. int cpu, i;
  7160. for_each_cpu(cpu, cpu_map) {
  7161. struct sched_group **sched_group_nodes
  7162. = sched_group_nodes_bycpu[cpu];
  7163. if (!sched_group_nodes)
  7164. continue;
  7165. for (i = 0; i < nr_node_ids; i++) {
  7166. struct sched_group *oldsg, *sg = sched_group_nodes[i];
  7167. cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
  7168. if (cpumask_empty(nodemask))
  7169. continue;
  7170. if (sg == NULL)
  7171. continue;
  7172. sg = sg->next;
  7173. next_sg:
  7174. oldsg = sg;
  7175. sg = sg->next;
  7176. kfree(oldsg);
  7177. if (oldsg != sched_group_nodes[i])
  7178. goto next_sg;
  7179. }
  7180. kfree(sched_group_nodes);
  7181. sched_group_nodes_bycpu[cpu] = NULL;
  7182. }
  7183. }
  7184. #else /* !CONFIG_NUMA */
  7185. static void free_sched_groups(const struct cpumask *cpu_map,
  7186. struct cpumask *nodemask)
  7187. {
  7188. }
  7189. #endif /* CONFIG_NUMA */
  7190. /*
  7191. * Initialize sched groups cpu_power.
  7192. *
  7193. * cpu_power indicates the capacity of sched group, which is used while
  7194. * distributing the load between different sched groups in a sched domain.
  7195. * Typically cpu_power for all the groups in a sched domain will be same unless
  7196. * there are asymmetries in the topology. If there are asymmetries, group
  7197. * having more cpu_power will pickup more load compared to the group having
  7198. * less cpu_power.
  7199. */
  7200. static void init_sched_groups_power(int cpu, struct sched_domain *sd)
  7201. {
  7202. struct sched_domain *child;
  7203. struct sched_group *group;
  7204. long power;
  7205. int weight;
  7206. WARN_ON(!sd || !sd->groups);
  7207. if (cpu != group_first_cpu(sd->groups))
  7208. return;
  7209. child = sd->child;
  7210. sd->groups->cpu_power = 0;
  7211. if (!child) {
  7212. power = SCHED_LOAD_SCALE;
  7213. weight = cpumask_weight(sched_domain_span(sd));
  7214. /*
  7215. * SMT siblings share the power of a single core.
  7216. * Usually multiple threads get a better yield out of
  7217. * that one core than a single thread would have,
  7218. * reflect that in sd->smt_gain.
  7219. */
  7220. if ((sd->flags & SD_SHARE_CPUPOWER) && weight > 1) {
  7221. power *= sd->smt_gain;
  7222. power /= weight;
  7223. power >>= SCHED_LOAD_SHIFT;
  7224. }
  7225. sd->groups->cpu_power += power;
  7226. return;
  7227. }
  7228. /*
  7229. * Add cpu_power of each child group to this groups cpu_power.
  7230. */
  7231. group = child->groups;
  7232. do {
  7233. sd->groups->cpu_power += group->cpu_power;
  7234. group = group->next;
  7235. } while (group != child->groups);
  7236. }
  7237. /*
  7238. * Initializers for schedule domains
  7239. * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
  7240. */
  7241. #ifdef CONFIG_SCHED_DEBUG
  7242. # define SD_INIT_NAME(sd, type) sd->name = #type
  7243. #else
  7244. # define SD_INIT_NAME(sd, type) do { } while (0)
  7245. #endif
  7246. #define SD_INIT(sd, type) sd_init_##type(sd)
  7247. #define SD_INIT_FUNC(type) \
  7248. static noinline void sd_init_##type(struct sched_domain *sd) \
  7249. { \
  7250. memset(sd, 0, sizeof(*sd)); \
  7251. *sd = SD_##type##_INIT; \
  7252. sd->level = SD_LV_##type; \
  7253. SD_INIT_NAME(sd, type); \
  7254. }
  7255. SD_INIT_FUNC(CPU)
  7256. #ifdef CONFIG_NUMA
  7257. SD_INIT_FUNC(ALLNODES)
  7258. SD_INIT_FUNC(NODE)
  7259. #endif
  7260. #ifdef CONFIG_SCHED_SMT
  7261. SD_INIT_FUNC(SIBLING)
  7262. #endif
  7263. #ifdef CONFIG_SCHED_MC
  7264. SD_INIT_FUNC(MC)
  7265. #endif
  7266. static int default_relax_domain_level = -1;
  7267. static int __init setup_relax_domain_level(char *str)
  7268. {
  7269. unsigned long val;
  7270. val = simple_strtoul(str, NULL, 0);
  7271. if (val < SD_LV_MAX)
  7272. default_relax_domain_level = val;
  7273. return 1;
  7274. }
  7275. __setup("relax_domain_level=", setup_relax_domain_level);
  7276. static void set_domain_attribute(struct sched_domain *sd,
  7277. struct sched_domain_attr *attr)
  7278. {
  7279. int request;
  7280. if (!attr || attr->relax_domain_level < 0) {
  7281. if (default_relax_domain_level < 0)
  7282. return;
  7283. else
  7284. request = default_relax_domain_level;
  7285. } else
  7286. request = attr->relax_domain_level;
  7287. if (request < sd->level) {
  7288. /* turn off idle balance on this domain */
  7289. sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
  7290. } else {
  7291. /* turn on idle balance on this domain */
  7292. sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
  7293. }
  7294. }
  7295. static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
  7296. const struct cpumask *cpu_map)
  7297. {
  7298. switch (what) {
  7299. case sa_sched_groups:
  7300. free_sched_groups(cpu_map, d->tmpmask); /* fall through */
  7301. d->sched_group_nodes = NULL;
  7302. case sa_rootdomain:
  7303. free_rootdomain(d->rd); /* fall through */
  7304. case sa_tmpmask:
  7305. free_cpumask_var(d->tmpmask); /* fall through */
  7306. case sa_send_covered:
  7307. free_cpumask_var(d->send_covered); /* fall through */
  7308. case sa_this_core_map:
  7309. free_cpumask_var(d->this_core_map); /* fall through */
  7310. case sa_this_sibling_map:
  7311. free_cpumask_var(d->this_sibling_map); /* fall through */
  7312. case sa_nodemask:
  7313. free_cpumask_var(d->nodemask); /* fall through */
  7314. case sa_sched_group_nodes:
  7315. #ifdef CONFIG_NUMA
  7316. kfree(d->sched_group_nodes); /* fall through */
  7317. case sa_notcovered:
  7318. free_cpumask_var(d->notcovered); /* fall through */
  7319. case sa_covered:
  7320. free_cpumask_var(d->covered); /* fall through */
  7321. case sa_domainspan:
  7322. free_cpumask_var(d->domainspan); /* fall through */
  7323. #endif
  7324. case sa_none:
  7325. break;
  7326. }
  7327. }
  7328. static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
  7329. const struct cpumask *cpu_map)
  7330. {
  7331. #ifdef CONFIG_NUMA
  7332. if (!alloc_cpumask_var(&d->domainspan, GFP_KERNEL))
  7333. return sa_none;
  7334. if (!alloc_cpumask_var(&d->covered, GFP_KERNEL))
  7335. return sa_domainspan;
  7336. if (!alloc_cpumask_var(&d->notcovered, GFP_KERNEL))
  7337. return sa_covered;
  7338. /* Allocate the per-node list of sched groups */
  7339. d->sched_group_nodes = kcalloc(nr_node_ids,
  7340. sizeof(struct sched_group *), GFP_KERNEL);
  7341. if (!d->sched_group_nodes) {
  7342. printk(KERN_WARNING "Can not alloc sched group node list\n");
  7343. return sa_notcovered;
  7344. }
  7345. sched_group_nodes_bycpu[cpumask_first(cpu_map)] = d->sched_group_nodes;
  7346. #endif
  7347. if (!alloc_cpumask_var(&d->nodemask, GFP_KERNEL))
  7348. return sa_sched_group_nodes;
  7349. if (!alloc_cpumask_var(&d->this_sibling_map, GFP_KERNEL))
  7350. return sa_nodemask;
  7351. if (!alloc_cpumask_var(&d->this_core_map, GFP_KERNEL))
  7352. return sa_this_sibling_map;
  7353. if (!alloc_cpumask_var(&d->send_covered, GFP_KERNEL))
  7354. return sa_this_core_map;
  7355. if (!alloc_cpumask_var(&d->tmpmask, GFP_KERNEL))
  7356. return sa_send_covered;
  7357. d->rd = alloc_rootdomain();
  7358. if (!d->rd) {
  7359. printk(KERN_WARNING "Cannot alloc root domain\n");
  7360. return sa_tmpmask;
  7361. }
  7362. return sa_rootdomain;
  7363. }
  7364. static struct sched_domain *__build_numa_sched_domains(struct s_data *d,
  7365. const struct cpumask *cpu_map, struct sched_domain_attr *attr, int i)
  7366. {
  7367. struct sched_domain *sd = NULL;
  7368. #ifdef CONFIG_NUMA
  7369. struct sched_domain *parent;
  7370. d->sd_allnodes = 0;
  7371. if (cpumask_weight(cpu_map) >
  7372. SD_NODES_PER_DOMAIN * cpumask_weight(d->nodemask)) {
  7373. sd = &per_cpu(allnodes_domains, i).sd;
  7374. SD_INIT(sd, ALLNODES);
  7375. set_domain_attribute(sd, attr);
  7376. cpumask_copy(sched_domain_span(sd), cpu_map);
  7377. cpu_to_allnodes_group(i, cpu_map, &sd->groups, d->tmpmask);
  7378. d->sd_allnodes = 1;
  7379. }
  7380. parent = sd;
  7381. sd = &per_cpu(node_domains, i).sd;
  7382. SD_INIT(sd, NODE);
  7383. set_domain_attribute(sd, attr);
  7384. sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
  7385. sd->parent = parent;
  7386. if (parent)
  7387. parent->child = sd;
  7388. cpumask_and(sched_domain_span(sd), sched_domain_span(sd), cpu_map);
  7389. #endif
  7390. return sd;
  7391. }
  7392. static struct sched_domain *__build_cpu_sched_domain(struct s_data *d,
  7393. const struct cpumask *cpu_map, struct sched_domain_attr *attr,
  7394. struct sched_domain *parent, int i)
  7395. {
  7396. struct sched_domain *sd;
  7397. sd = &per_cpu(phys_domains, i).sd;
  7398. SD_INIT(sd, CPU);
  7399. set_domain_attribute(sd, attr);
  7400. cpumask_copy(sched_domain_span(sd), d->nodemask);
  7401. sd->parent = parent;
  7402. if (parent)
  7403. parent->child = sd;
  7404. cpu_to_phys_group(i, cpu_map, &sd->groups, d->tmpmask);
  7405. return sd;
  7406. }
  7407. static struct sched_domain *__build_mc_sched_domain(struct s_data *d,
  7408. const struct cpumask *cpu_map, struct sched_domain_attr *attr,
  7409. struct sched_domain *parent, int i)
  7410. {
  7411. struct sched_domain *sd = parent;
  7412. #ifdef CONFIG_SCHED_MC
  7413. sd = &per_cpu(core_domains, i).sd;
  7414. SD_INIT(sd, MC);
  7415. set_domain_attribute(sd, attr);
  7416. cpumask_and(sched_domain_span(sd), cpu_map, cpu_coregroup_mask(i));
  7417. sd->parent = parent;
  7418. parent->child = sd;
  7419. cpu_to_core_group(i, cpu_map, &sd->groups, d->tmpmask);
  7420. #endif
  7421. return sd;
  7422. }
  7423. static struct sched_domain *__build_smt_sched_domain(struct s_data *d,
  7424. const struct cpumask *cpu_map, struct sched_domain_attr *attr,
  7425. struct sched_domain *parent, int i)
  7426. {
  7427. struct sched_domain *sd = parent;
  7428. #ifdef CONFIG_SCHED_SMT
  7429. sd = &per_cpu(cpu_domains, i).sd;
  7430. SD_INIT(sd, SIBLING);
  7431. set_domain_attribute(sd, attr);
  7432. cpumask_and(sched_domain_span(sd), cpu_map, topology_thread_cpumask(i));
  7433. sd->parent = parent;
  7434. parent->child = sd;
  7435. cpu_to_cpu_group(i, cpu_map, &sd->groups, d->tmpmask);
  7436. #endif
  7437. return sd;
  7438. }
  7439. static void build_sched_groups(struct s_data *d, enum sched_domain_level l,
  7440. const struct cpumask *cpu_map, int cpu)
  7441. {
  7442. switch (l) {
  7443. #ifdef CONFIG_SCHED_SMT
  7444. case SD_LV_SIBLING: /* set up CPU (sibling) groups */
  7445. cpumask_and(d->this_sibling_map, cpu_map,
  7446. topology_thread_cpumask(cpu));
  7447. if (cpu == cpumask_first(d->this_sibling_map))
  7448. init_sched_build_groups(d->this_sibling_map, cpu_map,
  7449. &cpu_to_cpu_group,
  7450. d->send_covered, d->tmpmask);
  7451. break;
  7452. #endif
  7453. #ifdef CONFIG_SCHED_MC
  7454. case SD_LV_MC: /* set up multi-core groups */
  7455. cpumask_and(d->this_core_map, cpu_map, cpu_coregroup_mask(cpu));
  7456. if (cpu == cpumask_first(d->this_core_map))
  7457. init_sched_build_groups(d->this_core_map, cpu_map,
  7458. &cpu_to_core_group,
  7459. d->send_covered, d->tmpmask);
  7460. break;
  7461. #endif
  7462. case SD_LV_CPU: /* set up physical groups */
  7463. cpumask_and(d->nodemask, cpumask_of_node(cpu), cpu_map);
  7464. if (!cpumask_empty(d->nodemask))
  7465. init_sched_build_groups(d->nodemask, cpu_map,
  7466. &cpu_to_phys_group,
  7467. d->send_covered, d->tmpmask);
  7468. break;
  7469. #ifdef CONFIG_NUMA
  7470. case SD_LV_ALLNODES:
  7471. init_sched_build_groups(cpu_map, cpu_map, &cpu_to_allnodes_group,
  7472. d->send_covered, d->tmpmask);
  7473. break;
  7474. #endif
  7475. default:
  7476. break;
  7477. }
  7478. }
  7479. /*
  7480. * Build sched domains for a given set of cpus and attach the sched domains
  7481. * to the individual cpus
  7482. */
  7483. static int __build_sched_domains(const struct cpumask *cpu_map,
  7484. struct sched_domain_attr *attr)
  7485. {
  7486. enum s_alloc alloc_state = sa_none;
  7487. struct s_data d;
  7488. struct sched_domain *sd;
  7489. int i;
  7490. #ifdef CONFIG_NUMA
  7491. d.sd_allnodes = 0;
  7492. #endif
  7493. alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
  7494. if (alloc_state != sa_rootdomain)
  7495. goto error;
  7496. alloc_state = sa_sched_groups;
  7497. /*
  7498. * Set up domains for cpus specified by the cpu_map.
  7499. */
  7500. for_each_cpu(i, cpu_map) {
  7501. cpumask_and(d.nodemask, cpumask_of_node(cpu_to_node(i)),
  7502. cpu_map);
  7503. sd = __build_numa_sched_domains(&d, cpu_map, attr, i);
  7504. sd = __build_cpu_sched_domain(&d, cpu_map, attr, sd, i);
  7505. sd = __build_mc_sched_domain(&d, cpu_map, attr, sd, i);
  7506. sd = __build_smt_sched_domain(&d, cpu_map, attr, sd, i);
  7507. }
  7508. for_each_cpu(i, cpu_map) {
  7509. build_sched_groups(&d, SD_LV_SIBLING, cpu_map, i);
  7510. build_sched_groups(&d, SD_LV_MC, cpu_map, i);
  7511. }
  7512. /* Set up physical groups */
  7513. for (i = 0; i < nr_node_ids; i++)
  7514. build_sched_groups(&d, SD_LV_CPU, cpu_map, i);
  7515. #ifdef CONFIG_NUMA
  7516. /* Set up node groups */
  7517. if (d.sd_allnodes)
  7518. build_sched_groups(&d, SD_LV_ALLNODES, cpu_map, 0);
  7519. for (i = 0; i < nr_node_ids; i++)
  7520. if (build_numa_sched_groups(&d, cpu_map, i))
  7521. goto error;
  7522. #endif
  7523. /* Calculate CPU power for physical packages and nodes */
  7524. #ifdef CONFIG_SCHED_SMT
  7525. for_each_cpu(i, cpu_map) {
  7526. sd = &per_cpu(cpu_domains, i).sd;
  7527. init_sched_groups_power(i, sd);
  7528. }
  7529. #endif
  7530. #ifdef CONFIG_SCHED_MC
  7531. for_each_cpu(i, cpu_map) {
  7532. sd = &per_cpu(core_domains, i).sd;
  7533. init_sched_groups_power(i, sd);
  7534. }
  7535. #endif
  7536. for_each_cpu(i, cpu_map) {
  7537. sd = &per_cpu(phys_domains, i).sd;
  7538. init_sched_groups_power(i, sd);
  7539. }
  7540. #ifdef CONFIG_NUMA
  7541. for (i = 0; i < nr_node_ids; i++)
  7542. init_numa_sched_groups_power(d.sched_group_nodes[i]);
  7543. if (d.sd_allnodes) {
  7544. struct sched_group *sg;
  7545. cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
  7546. d.tmpmask);
  7547. init_numa_sched_groups_power(sg);
  7548. }
  7549. #endif
  7550. /* Attach the domains */
  7551. for_each_cpu(i, cpu_map) {
  7552. #ifdef CONFIG_SCHED_SMT
  7553. sd = &per_cpu(cpu_domains, i).sd;
  7554. #elif defined(CONFIG_SCHED_MC)
  7555. sd = &per_cpu(core_domains, i).sd;
  7556. #else
  7557. sd = &per_cpu(phys_domains, i).sd;
  7558. #endif
  7559. cpu_attach_domain(sd, d.rd, i);
  7560. }
  7561. d.sched_group_nodes = NULL; /* don't free this we still need it */
  7562. __free_domain_allocs(&d, sa_tmpmask, cpu_map);
  7563. return 0;
  7564. error:
  7565. __free_domain_allocs(&d, alloc_state, cpu_map);
  7566. return -ENOMEM;
  7567. }
  7568. static int build_sched_domains(const struct cpumask *cpu_map)
  7569. {
  7570. return __build_sched_domains(cpu_map, NULL);
  7571. }
  7572. static struct cpumask *doms_cur; /* current sched domains */
  7573. static int ndoms_cur; /* number of sched domains in 'doms_cur' */
  7574. static struct sched_domain_attr *dattr_cur;
  7575. /* attribues of custom domains in 'doms_cur' */
  7576. /*
  7577. * Special case: If a kmalloc of a doms_cur partition (array of
  7578. * cpumask) fails, then fallback to a single sched domain,
  7579. * as determined by the single cpumask fallback_doms.
  7580. */
  7581. static cpumask_var_t fallback_doms;
  7582. /*
  7583. * arch_update_cpu_topology lets virtualized architectures update the
  7584. * cpu core maps. It is supposed to return 1 if the topology changed
  7585. * or 0 if it stayed the same.
  7586. */
  7587. int __attribute__((weak)) arch_update_cpu_topology(void)
  7588. {
  7589. return 0;
  7590. }
  7591. /*
  7592. * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  7593. * For now this just excludes isolated cpus, but could be used to
  7594. * exclude other special cases in the future.
  7595. */
  7596. static int arch_init_sched_domains(const struct cpumask *cpu_map)
  7597. {
  7598. int err;
  7599. arch_update_cpu_topology();
  7600. ndoms_cur = 1;
  7601. doms_cur = kmalloc(cpumask_size(), GFP_KERNEL);
  7602. if (!doms_cur)
  7603. doms_cur = fallback_doms;
  7604. cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map);
  7605. dattr_cur = NULL;
  7606. err = build_sched_domains(doms_cur);
  7607. register_sched_domain_sysctl();
  7608. return err;
  7609. }
  7610. static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
  7611. struct cpumask *tmpmask)
  7612. {
  7613. free_sched_groups(cpu_map, tmpmask);
  7614. }
  7615. /*
  7616. * Detach sched domains from a group of cpus specified in cpu_map
  7617. * These cpus will now be attached to the NULL domain
  7618. */
  7619. static void detach_destroy_domains(const struct cpumask *cpu_map)
  7620. {
  7621. /* Save because hotplug lock held. */
  7622. static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
  7623. int i;
  7624. for_each_cpu(i, cpu_map)
  7625. cpu_attach_domain(NULL, &def_root_domain, i);
  7626. synchronize_sched();
  7627. arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
  7628. }
  7629. /* handle null as "default" */
  7630. static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
  7631. struct sched_domain_attr *new, int idx_new)
  7632. {
  7633. struct sched_domain_attr tmp;
  7634. /* fast path */
  7635. if (!new && !cur)
  7636. return 1;
  7637. tmp = SD_ATTR_INIT;
  7638. return !memcmp(cur ? (cur + idx_cur) : &tmp,
  7639. new ? (new + idx_new) : &tmp,
  7640. sizeof(struct sched_domain_attr));
  7641. }
  7642. /*
  7643. * Partition sched domains as specified by the 'ndoms_new'
  7644. * cpumasks in the array doms_new[] of cpumasks. This compares
  7645. * doms_new[] to the current sched domain partitioning, doms_cur[].
  7646. * It destroys each deleted domain and builds each new domain.
  7647. *
  7648. * 'doms_new' is an array of cpumask's of length 'ndoms_new'.
  7649. * The masks don't intersect (don't overlap.) We should setup one
  7650. * sched domain for each mask. CPUs not in any of the cpumasks will
  7651. * not be load balanced. If the same cpumask appears both in the
  7652. * current 'doms_cur' domains and in the new 'doms_new', we can leave
  7653. * it as it is.
  7654. *
  7655. * The passed in 'doms_new' should be kmalloc'd. This routine takes
  7656. * ownership of it and will kfree it when done with it. If the caller
  7657. * failed the kmalloc call, then it can pass in doms_new == NULL &&
  7658. * ndoms_new == 1, and partition_sched_domains() will fallback to
  7659. * the single partition 'fallback_doms', it also forces the domains
  7660. * to be rebuilt.
  7661. *
  7662. * If doms_new == NULL it will be replaced with cpu_online_mask.
  7663. * ndoms_new == 0 is a special case for destroying existing domains,
  7664. * and it will not create the default domain.
  7665. *
  7666. * Call with hotplug lock held
  7667. */
  7668. /* FIXME: Change to struct cpumask *doms_new[] */
  7669. void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
  7670. struct sched_domain_attr *dattr_new)
  7671. {
  7672. int i, j, n;
  7673. int new_topology;
  7674. mutex_lock(&sched_domains_mutex);
  7675. /* always unregister in case we don't destroy any domains */
  7676. unregister_sched_domain_sysctl();
  7677. /* Let architecture update cpu core mappings. */
  7678. new_topology = arch_update_cpu_topology();
  7679. n = doms_new ? ndoms_new : 0;
  7680. /* Destroy deleted domains */
  7681. for (i = 0; i < ndoms_cur; i++) {
  7682. for (j = 0; j < n && !new_topology; j++) {
  7683. if (cpumask_equal(&doms_cur[i], &doms_new[j])
  7684. && dattrs_equal(dattr_cur, i, dattr_new, j))
  7685. goto match1;
  7686. }
  7687. /* no match - a current sched domain not in new doms_new[] */
  7688. detach_destroy_domains(doms_cur + i);
  7689. match1:
  7690. ;
  7691. }
  7692. if (doms_new == NULL) {
  7693. ndoms_cur = 0;
  7694. doms_new = fallback_doms;
  7695. cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map);
  7696. WARN_ON_ONCE(dattr_new);
  7697. }
  7698. /* Build new domains */
  7699. for (i = 0; i < ndoms_new; i++) {
  7700. for (j = 0; j < ndoms_cur && !new_topology; j++) {
  7701. if (cpumask_equal(&doms_new[i], &doms_cur[j])
  7702. && dattrs_equal(dattr_new, i, dattr_cur, j))
  7703. goto match2;
  7704. }
  7705. /* no match - add a new doms_new */
  7706. __build_sched_domains(doms_new + i,
  7707. dattr_new ? dattr_new + i : NULL);
  7708. match2:
  7709. ;
  7710. }
  7711. /* Remember the new sched domains */
  7712. if (doms_cur != fallback_doms)
  7713. kfree(doms_cur);
  7714. kfree(dattr_cur); /* kfree(NULL) is safe */
  7715. doms_cur = doms_new;
  7716. dattr_cur = dattr_new;
  7717. ndoms_cur = ndoms_new;
  7718. register_sched_domain_sysctl();
  7719. mutex_unlock(&sched_domains_mutex);
  7720. }
  7721. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  7722. static void arch_reinit_sched_domains(void)
  7723. {
  7724. get_online_cpus();
  7725. /* Destroy domains first to force the rebuild */
  7726. partition_sched_domains(0, NULL, NULL);
  7727. rebuild_sched_domains();
  7728. put_online_cpus();
  7729. }
  7730. static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
  7731. {
  7732. unsigned int level = 0;
  7733. if (sscanf(buf, "%u", &level) != 1)
  7734. return -EINVAL;
  7735. /*
  7736. * level is always be positive so don't check for
  7737. * level < POWERSAVINGS_BALANCE_NONE which is 0
  7738. * What happens on 0 or 1 byte write,
  7739. * need to check for count as well?
  7740. */
  7741. if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
  7742. return -EINVAL;
  7743. if (smt)
  7744. sched_smt_power_savings = level;
  7745. else
  7746. sched_mc_power_savings = level;
  7747. arch_reinit_sched_domains();
  7748. return count;
  7749. }
  7750. #ifdef CONFIG_SCHED_MC
  7751. static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
  7752. char *page)
  7753. {
  7754. return sprintf(page, "%u\n", sched_mc_power_savings);
  7755. }
  7756. static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
  7757. const char *buf, size_t count)
  7758. {
  7759. return sched_power_savings_store(buf, count, 0);
  7760. }
  7761. static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
  7762. sched_mc_power_savings_show,
  7763. sched_mc_power_savings_store);
  7764. #endif
  7765. #ifdef CONFIG_SCHED_SMT
  7766. static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
  7767. char *page)
  7768. {
  7769. return sprintf(page, "%u\n", sched_smt_power_savings);
  7770. }
  7771. static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
  7772. const char *buf, size_t count)
  7773. {
  7774. return sched_power_savings_store(buf, count, 1);
  7775. }
  7776. static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
  7777. sched_smt_power_savings_show,
  7778. sched_smt_power_savings_store);
  7779. #endif
  7780. int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
  7781. {
  7782. int err = 0;
  7783. #ifdef CONFIG_SCHED_SMT
  7784. if (smt_capable())
  7785. err = sysfs_create_file(&cls->kset.kobj,
  7786. &attr_sched_smt_power_savings.attr);
  7787. #endif
  7788. #ifdef CONFIG_SCHED_MC
  7789. if (!err && mc_capable())
  7790. err = sysfs_create_file(&cls->kset.kobj,
  7791. &attr_sched_mc_power_savings.attr);
  7792. #endif
  7793. return err;
  7794. }
  7795. #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
  7796. #ifndef CONFIG_CPUSETS
  7797. /*
  7798. * Add online and remove offline CPUs from the scheduler domains.
  7799. * When cpusets are enabled they take over this function.
  7800. */
  7801. static int update_sched_domains(struct notifier_block *nfb,
  7802. unsigned long action, void *hcpu)
  7803. {
  7804. switch (action) {
  7805. case CPU_ONLINE:
  7806. case CPU_ONLINE_FROZEN:
  7807. case CPU_DEAD:
  7808. case CPU_DEAD_FROZEN:
  7809. partition_sched_domains(1, NULL, NULL);
  7810. return NOTIFY_OK;
  7811. default:
  7812. return NOTIFY_DONE;
  7813. }
  7814. }
  7815. #endif
  7816. static int update_runtime(struct notifier_block *nfb,
  7817. unsigned long action, void *hcpu)
  7818. {
  7819. int cpu = (int)(long)hcpu;
  7820. switch (action) {
  7821. case CPU_DOWN_PREPARE:
  7822. case CPU_DOWN_PREPARE_FROZEN:
  7823. disable_runtime(cpu_rq(cpu));
  7824. return NOTIFY_OK;
  7825. case CPU_DOWN_FAILED:
  7826. case CPU_DOWN_FAILED_FROZEN:
  7827. case CPU_ONLINE:
  7828. case CPU_ONLINE_FROZEN:
  7829. enable_runtime(cpu_rq(cpu));
  7830. return NOTIFY_OK;
  7831. default:
  7832. return NOTIFY_DONE;
  7833. }
  7834. }
  7835. void __init sched_init_smp(void)
  7836. {
  7837. cpumask_var_t non_isolated_cpus;
  7838. alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
  7839. #if defined(CONFIG_NUMA)
  7840. sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
  7841. GFP_KERNEL);
  7842. BUG_ON(sched_group_nodes_bycpu == NULL);
  7843. #endif
  7844. get_online_cpus();
  7845. mutex_lock(&sched_domains_mutex);
  7846. arch_init_sched_domains(cpu_online_mask);
  7847. cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
  7848. if (cpumask_empty(non_isolated_cpus))
  7849. cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
  7850. mutex_unlock(&sched_domains_mutex);
  7851. put_online_cpus();
  7852. #ifndef CONFIG_CPUSETS
  7853. /* XXX: Theoretical race here - CPU may be hotplugged now */
  7854. hotcpu_notifier(update_sched_domains, 0);
  7855. #endif
  7856. /* RT runtime code needs to handle some hotplug events */
  7857. hotcpu_notifier(update_runtime, 0);
  7858. init_hrtick();
  7859. /* Move init over to a non-isolated CPU */
  7860. if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
  7861. BUG();
  7862. sched_init_granularity();
  7863. free_cpumask_var(non_isolated_cpus);
  7864. alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
  7865. init_sched_rt_class();
  7866. }
  7867. #else
  7868. void __init sched_init_smp(void)
  7869. {
  7870. sched_init_granularity();
  7871. }
  7872. #endif /* CONFIG_SMP */
  7873. const_debug unsigned int sysctl_timer_migration = 1;
  7874. int in_sched_functions(unsigned long addr)
  7875. {
  7876. return in_lock_functions(addr) ||
  7877. (addr >= (unsigned long)__sched_text_start
  7878. && addr < (unsigned long)__sched_text_end);
  7879. }
  7880. static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
  7881. {
  7882. cfs_rq->tasks_timeline = RB_ROOT;
  7883. INIT_LIST_HEAD(&cfs_rq->tasks);
  7884. #ifdef CONFIG_FAIR_GROUP_SCHED
  7885. cfs_rq->rq = rq;
  7886. #endif
  7887. cfs_rq->min_vruntime = (u64)(-(1LL << 20));
  7888. }
  7889. static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
  7890. {
  7891. struct rt_prio_array *array;
  7892. int i;
  7893. array = &rt_rq->active;
  7894. for (i = 0; i < MAX_RT_PRIO; i++) {
  7895. INIT_LIST_HEAD(array->queue + i);
  7896. __clear_bit(i, array->bitmap);
  7897. }
  7898. /* delimiter for bitsearch: */
  7899. __set_bit(MAX_RT_PRIO, array->bitmap);
  7900. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  7901. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  7902. #ifdef CONFIG_SMP
  7903. rt_rq->highest_prio.next = MAX_RT_PRIO;
  7904. #endif
  7905. #endif
  7906. #ifdef CONFIG_SMP
  7907. rt_rq->rt_nr_migratory = 0;
  7908. rt_rq->overloaded = 0;
  7909. plist_head_init(&rt_rq->pushable_tasks, &rq->lock);
  7910. #endif
  7911. rt_rq->rt_time = 0;
  7912. rt_rq->rt_throttled = 0;
  7913. rt_rq->rt_runtime = 0;
  7914. spin_lock_init(&rt_rq->rt_runtime_lock);
  7915. #ifdef CONFIG_RT_GROUP_SCHED
  7916. rt_rq->rt_nr_boosted = 0;
  7917. rt_rq->rq = rq;
  7918. #endif
  7919. }
  7920. #ifdef CONFIG_FAIR_GROUP_SCHED
  7921. static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  7922. struct sched_entity *se, int cpu, int add,
  7923. struct sched_entity *parent)
  7924. {
  7925. struct rq *rq = cpu_rq(cpu);
  7926. tg->cfs_rq[cpu] = cfs_rq;
  7927. init_cfs_rq(cfs_rq, rq);
  7928. cfs_rq->tg = tg;
  7929. if (add)
  7930. list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
  7931. tg->se[cpu] = se;
  7932. /* se could be NULL for init_task_group */
  7933. if (!se)
  7934. return;
  7935. if (!parent)
  7936. se->cfs_rq = &rq->cfs;
  7937. else
  7938. se->cfs_rq = parent->my_q;
  7939. se->my_q = cfs_rq;
  7940. se->load.weight = tg->shares;
  7941. se->load.inv_weight = 0;
  7942. se->parent = parent;
  7943. }
  7944. #endif
  7945. #ifdef CONFIG_RT_GROUP_SCHED
  7946. static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  7947. struct sched_rt_entity *rt_se, int cpu, int add,
  7948. struct sched_rt_entity *parent)
  7949. {
  7950. struct rq *rq = cpu_rq(cpu);
  7951. tg->rt_rq[cpu] = rt_rq;
  7952. init_rt_rq(rt_rq, rq);
  7953. rt_rq->tg = tg;
  7954. rt_rq->rt_se = rt_se;
  7955. rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
  7956. if (add)
  7957. list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
  7958. tg->rt_se[cpu] = rt_se;
  7959. if (!rt_se)
  7960. return;
  7961. if (!parent)
  7962. rt_se->rt_rq = &rq->rt;
  7963. else
  7964. rt_se->rt_rq = parent->my_q;
  7965. rt_se->my_q = rt_rq;
  7966. rt_se->parent = parent;
  7967. INIT_LIST_HEAD(&rt_se->run_list);
  7968. }
  7969. #endif
  7970. void __init sched_init(void)
  7971. {
  7972. int i, j;
  7973. unsigned long alloc_size = 0, ptr;
  7974. #ifdef CONFIG_FAIR_GROUP_SCHED
  7975. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  7976. #endif
  7977. #ifdef CONFIG_RT_GROUP_SCHED
  7978. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  7979. #endif
  7980. #ifdef CONFIG_USER_SCHED
  7981. alloc_size *= 2;
  7982. #endif
  7983. #ifdef CONFIG_CPUMASK_OFFSTACK
  7984. alloc_size += num_possible_cpus() * cpumask_size();
  7985. #endif
  7986. /*
  7987. * As sched_init() is called before page_alloc is setup,
  7988. * we use alloc_bootmem().
  7989. */
  7990. if (alloc_size) {
  7991. ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
  7992. #ifdef CONFIG_FAIR_GROUP_SCHED
  7993. init_task_group.se = (struct sched_entity **)ptr;
  7994. ptr += nr_cpu_ids * sizeof(void **);
  7995. init_task_group.cfs_rq = (struct cfs_rq **)ptr;
  7996. ptr += nr_cpu_ids * sizeof(void **);
  7997. #ifdef CONFIG_USER_SCHED
  7998. root_task_group.se = (struct sched_entity **)ptr;
  7999. ptr += nr_cpu_ids * sizeof(void **);
  8000. root_task_group.cfs_rq = (struct cfs_rq **)ptr;
  8001. ptr += nr_cpu_ids * sizeof(void **);
  8002. #endif /* CONFIG_USER_SCHED */
  8003. #endif /* CONFIG_FAIR_GROUP_SCHED */
  8004. #ifdef CONFIG_RT_GROUP_SCHED
  8005. init_task_group.rt_se = (struct sched_rt_entity **)ptr;
  8006. ptr += nr_cpu_ids * sizeof(void **);
  8007. init_task_group.rt_rq = (struct rt_rq **)ptr;
  8008. ptr += nr_cpu_ids * sizeof(void **);
  8009. #ifdef CONFIG_USER_SCHED
  8010. root_task_group.rt_se = (struct sched_rt_entity **)ptr;
  8011. ptr += nr_cpu_ids * sizeof(void **);
  8012. root_task_group.rt_rq = (struct rt_rq **)ptr;
  8013. ptr += nr_cpu_ids * sizeof(void **);
  8014. #endif /* CONFIG_USER_SCHED */
  8015. #endif /* CONFIG_RT_GROUP_SCHED */
  8016. #ifdef CONFIG_CPUMASK_OFFSTACK
  8017. for_each_possible_cpu(i) {
  8018. per_cpu(load_balance_tmpmask, i) = (void *)ptr;
  8019. ptr += cpumask_size();
  8020. }
  8021. #endif /* CONFIG_CPUMASK_OFFSTACK */
  8022. }
  8023. #ifdef CONFIG_SMP
  8024. init_defrootdomain();
  8025. #endif
  8026. init_rt_bandwidth(&def_rt_bandwidth,
  8027. global_rt_period(), global_rt_runtime());
  8028. #ifdef CONFIG_RT_GROUP_SCHED
  8029. init_rt_bandwidth(&init_task_group.rt_bandwidth,
  8030. global_rt_period(), global_rt_runtime());
  8031. #ifdef CONFIG_USER_SCHED
  8032. init_rt_bandwidth(&root_task_group.rt_bandwidth,
  8033. global_rt_period(), RUNTIME_INF);
  8034. #endif /* CONFIG_USER_SCHED */
  8035. #endif /* CONFIG_RT_GROUP_SCHED */
  8036. #ifdef CONFIG_GROUP_SCHED
  8037. list_add(&init_task_group.list, &task_groups);
  8038. INIT_LIST_HEAD(&init_task_group.children);
  8039. #ifdef CONFIG_USER_SCHED
  8040. INIT_LIST_HEAD(&root_task_group.children);
  8041. init_task_group.parent = &root_task_group;
  8042. list_add(&init_task_group.siblings, &root_task_group.children);
  8043. #endif /* CONFIG_USER_SCHED */
  8044. #endif /* CONFIG_GROUP_SCHED */
  8045. for_each_possible_cpu(i) {
  8046. struct rq *rq;
  8047. rq = cpu_rq(i);
  8048. spin_lock_init(&rq->lock);
  8049. rq->nr_running = 0;
  8050. rq->calc_load_active = 0;
  8051. rq->calc_load_update = jiffies + LOAD_FREQ;
  8052. init_cfs_rq(&rq->cfs, rq);
  8053. init_rt_rq(&rq->rt, rq);
  8054. #ifdef CONFIG_FAIR_GROUP_SCHED
  8055. init_task_group.shares = init_task_group_load;
  8056. INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
  8057. #ifdef CONFIG_CGROUP_SCHED
  8058. /*
  8059. * How much cpu bandwidth does init_task_group get?
  8060. *
  8061. * In case of task-groups formed thr' the cgroup filesystem, it
  8062. * gets 100% of the cpu resources in the system. This overall
  8063. * system cpu resource is divided among the tasks of
  8064. * init_task_group and its child task-groups in a fair manner,
  8065. * based on each entity's (task or task-group's) weight
  8066. * (se->load.weight).
  8067. *
  8068. * In other words, if init_task_group has 10 tasks of weight
  8069. * 1024) and two child groups A0 and A1 (of weight 1024 each),
  8070. * then A0's share of the cpu resource is:
  8071. *
  8072. * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
  8073. *
  8074. * We achieve this by letting init_task_group's tasks sit
  8075. * directly in rq->cfs (i.e init_task_group->se[] = NULL).
  8076. */
  8077. init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
  8078. #elif defined CONFIG_USER_SCHED
  8079. root_task_group.shares = NICE_0_LOAD;
  8080. init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
  8081. /*
  8082. * In case of task-groups formed thr' the user id of tasks,
  8083. * init_task_group represents tasks belonging to root user.
  8084. * Hence it forms a sibling of all subsequent groups formed.
  8085. * In this case, init_task_group gets only a fraction of overall
  8086. * system cpu resource, based on the weight assigned to root
  8087. * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
  8088. * by letting tasks of init_task_group sit in a separate cfs_rq
  8089. * (init_tg_cfs_rq) and having one entity represent this group of
  8090. * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
  8091. */
  8092. init_tg_cfs_entry(&init_task_group,
  8093. &per_cpu(init_tg_cfs_rq, i),
  8094. &per_cpu(init_sched_entity, i), i, 1,
  8095. root_task_group.se[i]);
  8096. #endif
  8097. #endif /* CONFIG_FAIR_GROUP_SCHED */
  8098. rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
  8099. #ifdef CONFIG_RT_GROUP_SCHED
  8100. INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
  8101. #ifdef CONFIG_CGROUP_SCHED
  8102. init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
  8103. #elif defined CONFIG_USER_SCHED
  8104. init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
  8105. init_tg_rt_entry(&init_task_group,
  8106. &per_cpu(init_rt_rq, i),
  8107. &per_cpu(init_sched_rt_entity, i), i, 1,
  8108. root_task_group.rt_se[i]);
  8109. #endif
  8110. #endif
  8111. for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
  8112. rq->cpu_load[j] = 0;
  8113. #ifdef CONFIG_SMP
  8114. rq->sd = NULL;
  8115. rq->rd = NULL;
  8116. rq->post_schedule = 0;
  8117. rq->active_balance = 0;
  8118. rq->next_balance = jiffies;
  8119. rq->push_cpu = 0;
  8120. rq->cpu = i;
  8121. rq->online = 0;
  8122. rq->migration_thread = NULL;
  8123. INIT_LIST_HEAD(&rq->migration_queue);
  8124. rq_attach_root(rq, &def_root_domain);
  8125. #endif
  8126. init_rq_hrtick(rq);
  8127. atomic_set(&rq->nr_iowait, 0);
  8128. }
  8129. set_load_weight(&init_task);
  8130. #ifdef CONFIG_PREEMPT_NOTIFIERS
  8131. INIT_HLIST_HEAD(&init_task.preempt_notifiers);
  8132. #endif
  8133. #ifdef CONFIG_SMP
  8134. open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
  8135. #endif
  8136. #ifdef CONFIG_RT_MUTEXES
  8137. plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
  8138. #endif
  8139. /*
  8140. * The boot idle thread does lazy MMU switching as well:
  8141. */
  8142. atomic_inc(&init_mm.mm_count);
  8143. enter_lazy_tlb(&init_mm, current);
  8144. /*
  8145. * Make us the idle thread. Technically, schedule() should not be
  8146. * called from this thread, however somewhere below it might be,
  8147. * but because we are the idle thread, we just pick up running again
  8148. * when this runqueue becomes "idle".
  8149. */
  8150. init_idle(current, smp_processor_id());
  8151. calc_load_update = jiffies + LOAD_FREQ;
  8152. /*
  8153. * During early bootup we pretend to be a normal task:
  8154. */
  8155. current->sched_class = &fair_sched_class;
  8156. /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
  8157. alloc_cpumask_var(&nohz_cpu_mask, GFP_NOWAIT);
  8158. #ifdef CONFIG_SMP
  8159. #ifdef CONFIG_NO_HZ
  8160. alloc_cpumask_var(&nohz.cpu_mask, GFP_NOWAIT);
  8161. alloc_cpumask_var(&nohz.ilb_grp_nohz_mask, GFP_NOWAIT);
  8162. #endif
  8163. alloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
  8164. #endif /* SMP */
  8165. perf_counter_init();
  8166. scheduler_running = 1;
  8167. }
  8168. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  8169. static inline int preempt_count_equals(int preempt_offset)
  8170. {
  8171. int nested = preempt_count() & ~PREEMPT_ACTIVE;
  8172. return (nested == PREEMPT_INATOMIC_BASE + preempt_offset);
  8173. }
  8174. void __might_sleep(char *file, int line, int preempt_offset)
  8175. {
  8176. #ifdef in_atomic
  8177. static unsigned long prev_jiffy; /* ratelimiting */
  8178. if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) ||
  8179. system_state != SYSTEM_RUNNING || oops_in_progress)
  8180. return;
  8181. if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  8182. return;
  8183. prev_jiffy = jiffies;
  8184. printk(KERN_ERR
  8185. "BUG: sleeping function called from invalid context at %s:%d\n",
  8186. file, line);
  8187. printk(KERN_ERR
  8188. "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
  8189. in_atomic(), irqs_disabled(),
  8190. current->pid, current->comm);
  8191. debug_show_held_locks(current);
  8192. if (irqs_disabled())
  8193. print_irqtrace_events(current);
  8194. dump_stack();
  8195. #endif
  8196. }
  8197. EXPORT_SYMBOL(__might_sleep);
  8198. #endif
  8199. #ifdef CONFIG_MAGIC_SYSRQ
  8200. static void normalize_task(struct rq *rq, struct task_struct *p)
  8201. {
  8202. int on_rq;
  8203. update_rq_clock(rq);
  8204. on_rq = p->se.on_rq;
  8205. if (on_rq)
  8206. deactivate_task(rq, p, 0);
  8207. __setscheduler(rq, p, SCHED_NORMAL, 0);
  8208. if (on_rq) {
  8209. activate_task(rq, p, 0);
  8210. resched_task(rq->curr);
  8211. }
  8212. }
  8213. void normalize_rt_tasks(void)
  8214. {
  8215. struct task_struct *g, *p;
  8216. unsigned long flags;
  8217. struct rq *rq;
  8218. read_lock_irqsave(&tasklist_lock, flags);
  8219. do_each_thread(g, p) {
  8220. /*
  8221. * Only normalize user tasks:
  8222. */
  8223. if (!p->mm)
  8224. continue;
  8225. p->se.exec_start = 0;
  8226. #ifdef CONFIG_SCHEDSTATS
  8227. p->se.wait_start = 0;
  8228. p->se.sleep_start = 0;
  8229. p->se.block_start = 0;
  8230. #endif
  8231. if (!rt_task(p)) {
  8232. /*
  8233. * Renice negative nice level userspace
  8234. * tasks back to 0:
  8235. */
  8236. if (TASK_NICE(p) < 0 && p->mm)
  8237. set_user_nice(p, 0);
  8238. continue;
  8239. }
  8240. spin_lock(&p->pi_lock);
  8241. rq = __task_rq_lock(p);
  8242. normalize_task(rq, p);
  8243. __task_rq_unlock(rq);
  8244. spin_unlock(&p->pi_lock);
  8245. } while_each_thread(g, p);
  8246. read_unlock_irqrestore(&tasklist_lock, flags);
  8247. }
  8248. #endif /* CONFIG_MAGIC_SYSRQ */
  8249. #ifdef CONFIG_IA64
  8250. /*
  8251. * These functions are only useful for the IA64 MCA handling.
  8252. *
  8253. * They can only be called when the whole system has been
  8254. * stopped - every CPU needs to be quiescent, and no scheduling
  8255. * activity can take place. Using them for anything else would
  8256. * be a serious bug, and as a result, they aren't even visible
  8257. * under any other configuration.
  8258. */
  8259. /**
  8260. * curr_task - return the current task for a given cpu.
  8261. * @cpu: the processor in question.
  8262. *
  8263. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  8264. */
  8265. struct task_struct *curr_task(int cpu)
  8266. {
  8267. return cpu_curr(cpu);
  8268. }
  8269. /**
  8270. * set_curr_task - set the current task for a given cpu.
  8271. * @cpu: the processor in question.
  8272. * @p: the task pointer to set.
  8273. *
  8274. * Description: This function must only be used when non-maskable interrupts
  8275. * are serviced on a separate stack. It allows the architecture to switch the
  8276. * notion of the current task on a cpu in a non-blocking manner. This function
  8277. * must be called with all CPU's synchronized, and interrupts disabled, the
  8278. * and caller must save the original value of the current task (see
  8279. * curr_task() above) and restore that value before reenabling interrupts and
  8280. * re-starting the system.
  8281. *
  8282. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  8283. */
  8284. void set_curr_task(int cpu, struct task_struct *p)
  8285. {
  8286. cpu_curr(cpu) = p;
  8287. }
  8288. #endif
  8289. #ifdef CONFIG_FAIR_GROUP_SCHED
  8290. static void free_fair_sched_group(struct task_group *tg)
  8291. {
  8292. int i;
  8293. for_each_possible_cpu(i) {
  8294. if (tg->cfs_rq)
  8295. kfree(tg->cfs_rq[i]);
  8296. if (tg->se)
  8297. kfree(tg->se[i]);
  8298. }
  8299. kfree(tg->cfs_rq);
  8300. kfree(tg->se);
  8301. }
  8302. static
  8303. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  8304. {
  8305. struct cfs_rq *cfs_rq;
  8306. struct sched_entity *se;
  8307. struct rq *rq;
  8308. int i;
  8309. tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
  8310. if (!tg->cfs_rq)
  8311. goto err;
  8312. tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
  8313. if (!tg->se)
  8314. goto err;
  8315. tg->shares = NICE_0_LOAD;
  8316. for_each_possible_cpu(i) {
  8317. rq = cpu_rq(i);
  8318. cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
  8319. GFP_KERNEL, cpu_to_node(i));
  8320. if (!cfs_rq)
  8321. goto err;
  8322. se = kzalloc_node(sizeof(struct sched_entity),
  8323. GFP_KERNEL, cpu_to_node(i));
  8324. if (!se)
  8325. goto err;
  8326. init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
  8327. }
  8328. return 1;
  8329. err:
  8330. return 0;
  8331. }
  8332. static inline void register_fair_sched_group(struct task_group *tg, int cpu)
  8333. {
  8334. list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
  8335. &cpu_rq(cpu)->leaf_cfs_rq_list);
  8336. }
  8337. static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
  8338. {
  8339. list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
  8340. }
  8341. #else /* !CONFG_FAIR_GROUP_SCHED */
  8342. static inline void free_fair_sched_group(struct task_group *tg)
  8343. {
  8344. }
  8345. static inline
  8346. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  8347. {
  8348. return 1;
  8349. }
  8350. static inline void register_fair_sched_group(struct task_group *tg, int cpu)
  8351. {
  8352. }
  8353. static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
  8354. {
  8355. }
  8356. #endif /* CONFIG_FAIR_GROUP_SCHED */
  8357. #ifdef CONFIG_RT_GROUP_SCHED
  8358. static void free_rt_sched_group(struct task_group *tg)
  8359. {
  8360. int i;
  8361. destroy_rt_bandwidth(&tg->rt_bandwidth);
  8362. for_each_possible_cpu(i) {
  8363. if (tg->rt_rq)
  8364. kfree(tg->rt_rq[i]);
  8365. if (tg->rt_se)
  8366. kfree(tg->rt_se[i]);
  8367. }
  8368. kfree(tg->rt_rq);
  8369. kfree(tg->rt_se);
  8370. }
  8371. static
  8372. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  8373. {
  8374. struct rt_rq *rt_rq;
  8375. struct sched_rt_entity *rt_se;
  8376. struct rq *rq;
  8377. int i;
  8378. tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
  8379. if (!tg->rt_rq)
  8380. goto err;
  8381. tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
  8382. if (!tg->rt_se)
  8383. goto err;
  8384. init_rt_bandwidth(&tg->rt_bandwidth,
  8385. ktime_to_ns(def_rt_bandwidth.rt_period), 0);
  8386. for_each_possible_cpu(i) {
  8387. rq = cpu_rq(i);
  8388. rt_rq = kzalloc_node(sizeof(struct rt_rq),
  8389. GFP_KERNEL, cpu_to_node(i));
  8390. if (!rt_rq)
  8391. goto err;
  8392. rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
  8393. GFP_KERNEL, cpu_to_node(i));
  8394. if (!rt_se)
  8395. goto err;
  8396. init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
  8397. }
  8398. return 1;
  8399. err:
  8400. return 0;
  8401. }
  8402. static inline void register_rt_sched_group(struct task_group *tg, int cpu)
  8403. {
  8404. list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
  8405. &cpu_rq(cpu)->leaf_rt_rq_list);
  8406. }
  8407. static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
  8408. {
  8409. list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
  8410. }
  8411. #else /* !CONFIG_RT_GROUP_SCHED */
  8412. static inline void free_rt_sched_group(struct task_group *tg)
  8413. {
  8414. }
  8415. static inline
  8416. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  8417. {
  8418. return 1;
  8419. }
  8420. static inline void register_rt_sched_group(struct task_group *tg, int cpu)
  8421. {
  8422. }
  8423. static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
  8424. {
  8425. }
  8426. #endif /* CONFIG_RT_GROUP_SCHED */
  8427. #ifdef CONFIG_GROUP_SCHED
  8428. static void free_sched_group(struct task_group *tg)
  8429. {
  8430. free_fair_sched_group(tg);
  8431. free_rt_sched_group(tg);
  8432. kfree(tg);
  8433. }
  8434. /* allocate runqueue etc for a new task group */
  8435. struct task_group *sched_create_group(struct task_group *parent)
  8436. {
  8437. struct task_group *tg;
  8438. unsigned long flags;
  8439. int i;
  8440. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  8441. if (!tg)
  8442. return ERR_PTR(-ENOMEM);
  8443. if (!alloc_fair_sched_group(tg, parent))
  8444. goto err;
  8445. if (!alloc_rt_sched_group(tg, parent))
  8446. goto err;
  8447. spin_lock_irqsave(&task_group_lock, flags);
  8448. for_each_possible_cpu(i) {
  8449. register_fair_sched_group(tg, i);
  8450. register_rt_sched_group(tg, i);
  8451. }
  8452. list_add_rcu(&tg->list, &task_groups);
  8453. WARN_ON(!parent); /* root should already exist */
  8454. tg->parent = parent;
  8455. INIT_LIST_HEAD(&tg->children);
  8456. list_add_rcu(&tg->siblings, &parent->children);
  8457. spin_unlock_irqrestore(&task_group_lock, flags);
  8458. return tg;
  8459. err:
  8460. free_sched_group(tg);
  8461. return ERR_PTR(-ENOMEM);
  8462. }
  8463. /* rcu callback to free various structures associated with a task group */
  8464. static void free_sched_group_rcu(struct rcu_head *rhp)
  8465. {
  8466. /* now it should be safe to free those cfs_rqs */
  8467. free_sched_group(container_of(rhp, struct task_group, rcu));
  8468. }
  8469. /* Destroy runqueue etc associated with a task group */
  8470. void sched_destroy_group(struct task_group *tg)
  8471. {
  8472. unsigned long flags;
  8473. int i;
  8474. spin_lock_irqsave(&task_group_lock, flags);
  8475. for_each_possible_cpu(i) {
  8476. unregister_fair_sched_group(tg, i);
  8477. unregister_rt_sched_group(tg, i);
  8478. }
  8479. list_del_rcu(&tg->list);
  8480. list_del_rcu(&tg->siblings);
  8481. spin_unlock_irqrestore(&task_group_lock, flags);
  8482. /* wait for possible concurrent references to cfs_rqs complete */
  8483. call_rcu(&tg->rcu, free_sched_group_rcu);
  8484. }
  8485. /* change task's runqueue when it moves between groups.
  8486. * The caller of this function should have put the task in its new group
  8487. * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
  8488. * reflect its new group.
  8489. */
  8490. void sched_move_task(struct task_struct *tsk)
  8491. {
  8492. int on_rq, running;
  8493. unsigned long flags;
  8494. struct rq *rq;
  8495. rq = task_rq_lock(tsk, &flags);
  8496. update_rq_clock(rq);
  8497. running = task_current(rq, tsk);
  8498. on_rq = tsk->se.on_rq;
  8499. if (on_rq)
  8500. dequeue_task(rq, tsk, 0);
  8501. if (unlikely(running))
  8502. tsk->sched_class->put_prev_task(rq, tsk);
  8503. set_task_rq(tsk, task_cpu(tsk));
  8504. #ifdef CONFIG_FAIR_GROUP_SCHED
  8505. if (tsk->sched_class->moved_group)
  8506. tsk->sched_class->moved_group(tsk);
  8507. #endif
  8508. if (unlikely(running))
  8509. tsk->sched_class->set_curr_task(rq);
  8510. if (on_rq)
  8511. enqueue_task(rq, tsk, 0);
  8512. task_rq_unlock(rq, &flags);
  8513. }
  8514. #endif /* CONFIG_GROUP_SCHED */
  8515. #ifdef CONFIG_FAIR_GROUP_SCHED
  8516. static void __set_se_shares(struct sched_entity *se, unsigned long shares)
  8517. {
  8518. struct cfs_rq *cfs_rq = se->cfs_rq;
  8519. int on_rq;
  8520. on_rq = se->on_rq;
  8521. if (on_rq)
  8522. dequeue_entity(cfs_rq, se, 0);
  8523. se->load.weight = shares;
  8524. se->load.inv_weight = 0;
  8525. if (on_rq)
  8526. enqueue_entity(cfs_rq, se, 0);
  8527. }
  8528. static void set_se_shares(struct sched_entity *se, unsigned long shares)
  8529. {
  8530. struct cfs_rq *cfs_rq = se->cfs_rq;
  8531. struct rq *rq = cfs_rq->rq;
  8532. unsigned long flags;
  8533. spin_lock_irqsave(&rq->lock, flags);
  8534. __set_se_shares(se, shares);
  8535. spin_unlock_irqrestore(&rq->lock, flags);
  8536. }
  8537. static DEFINE_MUTEX(shares_mutex);
  8538. int sched_group_set_shares(struct task_group *tg, unsigned long shares)
  8539. {
  8540. int i;
  8541. unsigned long flags;
  8542. /*
  8543. * We can't change the weight of the root cgroup.
  8544. */
  8545. if (!tg->se[0])
  8546. return -EINVAL;
  8547. if (shares < MIN_SHARES)
  8548. shares = MIN_SHARES;
  8549. else if (shares > MAX_SHARES)
  8550. shares = MAX_SHARES;
  8551. mutex_lock(&shares_mutex);
  8552. if (tg->shares == shares)
  8553. goto done;
  8554. spin_lock_irqsave(&task_group_lock, flags);
  8555. for_each_possible_cpu(i)
  8556. unregister_fair_sched_group(tg, i);
  8557. list_del_rcu(&tg->siblings);
  8558. spin_unlock_irqrestore(&task_group_lock, flags);
  8559. /* wait for any ongoing reference to this group to finish */
  8560. synchronize_sched();
  8561. /*
  8562. * Now we are free to modify the group's share on each cpu
  8563. * w/o tripping rebalance_share or load_balance_fair.
  8564. */
  8565. tg->shares = shares;
  8566. for_each_possible_cpu(i) {
  8567. /*
  8568. * force a rebalance
  8569. */
  8570. cfs_rq_set_shares(tg->cfs_rq[i], 0);
  8571. set_se_shares(tg->se[i], shares);
  8572. }
  8573. /*
  8574. * Enable load balance activity on this group, by inserting it back on
  8575. * each cpu's rq->leaf_cfs_rq_list.
  8576. */
  8577. spin_lock_irqsave(&task_group_lock, flags);
  8578. for_each_possible_cpu(i)
  8579. register_fair_sched_group(tg, i);
  8580. list_add_rcu(&tg->siblings, &tg->parent->children);
  8581. spin_unlock_irqrestore(&task_group_lock, flags);
  8582. done:
  8583. mutex_unlock(&shares_mutex);
  8584. return 0;
  8585. }
  8586. unsigned long sched_group_shares(struct task_group *tg)
  8587. {
  8588. return tg->shares;
  8589. }
  8590. #endif
  8591. #ifdef CONFIG_RT_GROUP_SCHED
  8592. /*
  8593. * Ensure that the real time constraints are schedulable.
  8594. */
  8595. static DEFINE_MUTEX(rt_constraints_mutex);
  8596. static unsigned long to_ratio(u64 period, u64 runtime)
  8597. {
  8598. if (runtime == RUNTIME_INF)
  8599. return 1ULL << 20;
  8600. return div64_u64(runtime << 20, period);
  8601. }
  8602. /* Must be called with tasklist_lock held */
  8603. static inline int tg_has_rt_tasks(struct task_group *tg)
  8604. {
  8605. struct task_struct *g, *p;
  8606. do_each_thread(g, p) {
  8607. if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
  8608. return 1;
  8609. } while_each_thread(g, p);
  8610. return 0;
  8611. }
  8612. struct rt_schedulable_data {
  8613. struct task_group *tg;
  8614. u64 rt_period;
  8615. u64 rt_runtime;
  8616. };
  8617. static int tg_schedulable(struct task_group *tg, void *data)
  8618. {
  8619. struct rt_schedulable_data *d = data;
  8620. struct task_group *child;
  8621. unsigned long total, sum = 0;
  8622. u64 period, runtime;
  8623. period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  8624. runtime = tg->rt_bandwidth.rt_runtime;
  8625. if (tg == d->tg) {
  8626. period = d->rt_period;
  8627. runtime = d->rt_runtime;
  8628. }
  8629. #ifdef CONFIG_USER_SCHED
  8630. if (tg == &root_task_group) {
  8631. period = global_rt_period();
  8632. runtime = global_rt_runtime();
  8633. }
  8634. #endif
  8635. /*
  8636. * Cannot have more runtime than the period.
  8637. */
  8638. if (runtime > period && runtime != RUNTIME_INF)
  8639. return -EINVAL;
  8640. /*
  8641. * Ensure we don't starve existing RT tasks.
  8642. */
  8643. if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
  8644. return -EBUSY;
  8645. total = to_ratio(period, runtime);
  8646. /*
  8647. * Nobody can have more than the global setting allows.
  8648. */
  8649. if (total > to_ratio(global_rt_period(), global_rt_runtime()))
  8650. return -EINVAL;
  8651. /*
  8652. * The sum of our children's runtime should not exceed our own.
  8653. */
  8654. list_for_each_entry_rcu(child, &tg->children, siblings) {
  8655. period = ktime_to_ns(child->rt_bandwidth.rt_period);
  8656. runtime = child->rt_bandwidth.rt_runtime;
  8657. if (child == d->tg) {
  8658. period = d->rt_period;
  8659. runtime = d->rt_runtime;
  8660. }
  8661. sum += to_ratio(period, runtime);
  8662. }
  8663. if (sum > total)
  8664. return -EINVAL;
  8665. return 0;
  8666. }
  8667. static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
  8668. {
  8669. struct rt_schedulable_data data = {
  8670. .tg = tg,
  8671. .rt_period = period,
  8672. .rt_runtime = runtime,
  8673. };
  8674. return walk_tg_tree(tg_schedulable, tg_nop, &data);
  8675. }
  8676. static int tg_set_bandwidth(struct task_group *tg,
  8677. u64 rt_period, u64 rt_runtime)
  8678. {
  8679. int i, err = 0;
  8680. mutex_lock(&rt_constraints_mutex);
  8681. read_lock(&tasklist_lock);
  8682. err = __rt_schedulable(tg, rt_period, rt_runtime);
  8683. if (err)
  8684. goto unlock;
  8685. spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  8686. tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
  8687. tg->rt_bandwidth.rt_runtime = rt_runtime;
  8688. for_each_possible_cpu(i) {
  8689. struct rt_rq *rt_rq = tg->rt_rq[i];
  8690. spin_lock(&rt_rq->rt_runtime_lock);
  8691. rt_rq->rt_runtime = rt_runtime;
  8692. spin_unlock(&rt_rq->rt_runtime_lock);
  8693. }
  8694. spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  8695. unlock:
  8696. read_unlock(&tasklist_lock);
  8697. mutex_unlock(&rt_constraints_mutex);
  8698. return err;
  8699. }
  8700. int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
  8701. {
  8702. u64 rt_runtime, rt_period;
  8703. rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  8704. rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
  8705. if (rt_runtime_us < 0)
  8706. rt_runtime = RUNTIME_INF;
  8707. return tg_set_bandwidth(tg, rt_period, rt_runtime);
  8708. }
  8709. long sched_group_rt_runtime(struct task_group *tg)
  8710. {
  8711. u64 rt_runtime_us;
  8712. if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
  8713. return -1;
  8714. rt_runtime_us = tg->rt_bandwidth.rt_runtime;
  8715. do_div(rt_runtime_us, NSEC_PER_USEC);
  8716. return rt_runtime_us;
  8717. }
  8718. int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
  8719. {
  8720. u64 rt_runtime, rt_period;
  8721. rt_period = (u64)rt_period_us * NSEC_PER_USEC;
  8722. rt_runtime = tg->rt_bandwidth.rt_runtime;
  8723. if (rt_period == 0)
  8724. return -EINVAL;
  8725. return tg_set_bandwidth(tg, rt_period, rt_runtime);
  8726. }
  8727. long sched_group_rt_period(struct task_group *tg)
  8728. {
  8729. u64 rt_period_us;
  8730. rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
  8731. do_div(rt_period_us, NSEC_PER_USEC);
  8732. return rt_period_us;
  8733. }
  8734. static int sched_rt_global_constraints(void)
  8735. {
  8736. u64 runtime, period;
  8737. int ret = 0;
  8738. if (sysctl_sched_rt_period <= 0)
  8739. return -EINVAL;
  8740. runtime = global_rt_runtime();
  8741. period = global_rt_period();
  8742. /*
  8743. * Sanity check on the sysctl variables.
  8744. */
  8745. if (runtime > period && runtime != RUNTIME_INF)
  8746. return -EINVAL;
  8747. mutex_lock(&rt_constraints_mutex);
  8748. read_lock(&tasklist_lock);
  8749. ret = __rt_schedulable(NULL, 0, 0);
  8750. read_unlock(&tasklist_lock);
  8751. mutex_unlock(&rt_constraints_mutex);
  8752. return ret;
  8753. }
  8754. int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
  8755. {
  8756. /* Don't accept realtime tasks when there is no way for them to run */
  8757. if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
  8758. return 0;
  8759. return 1;
  8760. }
  8761. #else /* !CONFIG_RT_GROUP_SCHED */
  8762. static int sched_rt_global_constraints(void)
  8763. {
  8764. unsigned long flags;
  8765. int i;
  8766. if (sysctl_sched_rt_period <= 0)
  8767. return -EINVAL;
  8768. /*
  8769. * There's always some RT tasks in the root group
  8770. * -- migration, kstopmachine etc..
  8771. */
  8772. if (sysctl_sched_rt_runtime == 0)
  8773. return -EBUSY;
  8774. spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
  8775. for_each_possible_cpu(i) {
  8776. struct rt_rq *rt_rq = &cpu_rq(i)->rt;
  8777. spin_lock(&rt_rq->rt_runtime_lock);
  8778. rt_rq->rt_runtime = global_rt_runtime();
  8779. spin_unlock(&rt_rq->rt_runtime_lock);
  8780. }
  8781. spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
  8782. return 0;
  8783. }
  8784. #endif /* CONFIG_RT_GROUP_SCHED */
  8785. int sched_rt_handler(struct ctl_table *table, int write,
  8786. struct file *filp, void __user *buffer, size_t *lenp,
  8787. loff_t *ppos)
  8788. {
  8789. int ret;
  8790. int old_period, old_runtime;
  8791. static DEFINE_MUTEX(mutex);
  8792. mutex_lock(&mutex);
  8793. old_period = sysctl_sched_rt_period;
  8794. old_runtime = sysctl_sched_rt_runtime;
  8795. ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  8796. if (!ret && write) {
  8797. ret = sched_rt_global_constraints();
  8798. if (ret) {
  8799. sysctl_sched_rt_period = old_period;
  8800. sysctl_sched_rt_runtime = old_runtime;
  8801. } else {
  8802. def_rt_bandwidth.rt_runtime = global_rt_runtime();
  8803. def_rt_bandwidth.rt_period =
  8804. ns_to_ktime(global_rt_period());
  8805. }
  8806. }
  8807. mutex_unlock(&mutex);
  8808. return ret;
  8809. }
  8810. #ifdef CONFIG_CGROUP_SCHED
  8811. /* return corresponding task_group object of a cgroup */
  8812. static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
  8813. {
  8814. return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
  8815. struct task_group, css);
  8816. }
  8817. static struct cgroup_subsys_state *
  8818. cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8819. {
  8820. struct task_group *tg, *parent;
  8821. if (!cgrp->parent) {
  8822. /* This is early initialization for the top cgroup */
  8823. return &init_task_group.css;
  8824. }
  8825. parent = cgroup_tg(cgrp->parent);
  8826. tg = sched_create_group(parent);
  8827. if (IS_ERR(tg))
  8828. return ERR_PTR(-ENOMEM);
  8829. return &tg->css;
  8830. }
  8831. static void
  8832. cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8833. {
  8834. struct task_group *tg = cgroup_tg(cgrp);
  8835. sched_destroy_group(tg);
  8836. }
  8837. static int
  8838. cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  8839. struct task_struct *tsk)
  8840. {
  8841. #ifdef CONFIG_RT_GROUP_SCHED
  8842. if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
  8843. return -EINVAL;
  8844. #else
  8845. /* We don't support RT-tasks being in separate groups */
  8846. if (tsk->sched_class != &fair_sched_class)
  8847. return -EINVAL;
  8848. #endif
  8849. return 0;
  8850. }
  8851. static void
  8852. cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  8853. struct cgroup *old_cont, struct task_struct *tsk)
  8854. {
  8855. sched_move_task(tsk);
  8856. }
  8857. #ifdef CONFIG_FAIR_GROUP_SCHED
  8858. static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
  8859. u64 shareval)
  8860. {
  8861. return sched_group_set_shares(cgroup_tg(cgrp), shareval);
  8862. }
  8863. static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
  8864. {
  8865. struct task_group *tg = cgroup_tg(cgrp);
  8866. return (u64) tg->shares;
  8867. }
  8868. #endif /* CONFIG_FAIR_GROUP_SCHED */
  8869. #ifdef CONFIG_RT_GROUP_SCHED
  8870. static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
  8871. s64 val)
  8872. {
  8873. return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
  8874. }
  8875. static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
  8876. {
  8877. return sched_group_rt_runtime(cgroup_tg(cgrp));
  8878. }
  8879. static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
  8880. u64 rt_period_us)
  8881. {
  8882. return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
  8883. }
  8884. static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
  8885. {
  8886. return sched_group_rt_period(cgroup_tg(cgrp));
  8887. }
  8888. #endif /* CONFIG_RT_GROUP_SCHED */
  8889. static struct cftype cpu_files[] = {
  8890. #ifdef CONFIG_FAIR_GROUP_SCHED
  8891. {
  8892. .name = "shares",
  8893. .read_u64 = cpu_shares_read_u64,
  8894. .write_u64 = cpu_shares_write_u64,
  8895. },
  8896. #endif
  8897. #ifdef CONFIG_RT_GROUP_SCHED
  8898. {
  8899. .name = "rt_runtime_us",
  8900. .read_s64 = cpu_rt_runtime_read,
  8901. .write_s64 = cpu_rt_runtime_write,
  8902. },
  8903. {
  8904. .name = "rt_period_us",
  8905. .read_u64 = cpu_rt_period_read_uint,
  8906. .write_u64 = cpu_rt_period_write_uint,
  8907. },
  8908. #endif
  8909. };
  8910. static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  8911. {
  8912. return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
  8913. }
  8914. struct cgroup_subsys cpu_cgroup_subsys = {
  8915. .name = "cpu",
  8916. .create = cpu_cgroup_create,
  8917. .destroy = cpu_cgroup_destroy,
  8918. .can_attach = cpu_cgroup_can_attach,
  8919. .attach = cpu_cgroup_attach,
  8920. .populate = cpu_cgroup_populate,
  8921. .subsys_id = cpu_cgroup_subsys_id,
  8922. .early_init = 1,
  8923. };
  8924. #endif /* CONFIG_CGROUP_SCHED */
  8925. #ifdef CONFIG_CGROUP_CPUACCT
  8926. /*
  8927. * CPU accounting code for task groups.
  8928. *
  8929. * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
  8930. * (balbir@in.ibm.com).
  8931. */
  8932. /* track cpu usage of a group of tasks and its child groups */
  8933. struct cpuacct {
  8934. struct cgroup_subsys_state css;
  8935. /* cpuusage holds pointer to a u64-type object on every cpu */
  8936. u64 *cpuusage;
  8937. struct percpu_counter cpustat[CPUACCT_STAT_NSTATS];
  8938. struct cpuacct *parent;
  8939. };
  8940. struct cgroup_subsys cpuacct_subsys;
  8941. /* return cpu accounting group corresponding to this container */
  8942. static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
  8943. {
  8944. return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
  8945. struct cpuacct, css);
  8946. }
  8947. /* return cpu accounting group to which this task belongs */
  8948. static inline struct cpuacct *task_ca(struct task_struct *tsk)
  8949. {
  8950. return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
  8951. struct cpuacct, css);
  8952. }
  8953. /* create a new cpu accounting group */
  8954. static struct cgroup_subsys_state *cpuacct_create(
  8955. struct cgroup_subsys *ss, struct cgroup *cgrp)
  8956. {
  8957. struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  8958. int i;
  8959. if (!ca)
  8960. goto out;
  8961. ca->cpuusage = alloc_percpu(u64);
  8962. if (!ca->cpuusage)
  8963. goto out_free_ca;
  8964. for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
  8965. if (percpu_counter_init(&ca->cpustat[i], 0))
  8966. goto out_free_counters;
  8967. if (cgrp->parent)
  8968. ca->parent = cgroup_ca(cgrp->parent);
  8969. return &ca->css;
  8970. out_free_counters:
  8971. while (--i >= 0)
  8972. percpu_counter_destroy(&ca->cpustat[i]);
  8973. free_percpu(ca->cpuusage);
  8974. out_free_ca:
  8975. kfree(ca);
  8976. out:
  8977. return ERR_PTR(-ENOMEM);
  8978. }
  8979. /* destroy an existing cpu accounting group */
  8980. static void
  8981. cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8982. {
  8983. struct cpuacct *ca = cgroup_ca(cgrp);
  8984. int i;
  8985. for (i = 0; i < CPUACCT_STAT_NSTATS; i++)
  8986. percpu_counter_destroy(&ca->cpustat[i]);
  8987. free_percpu(ca->cpuusage);
  8988. kfree(ca);
  8989. }
  8990. static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
  8991. {
  8992. u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  8993. u64 data;
  8994. #ifndef CONFIG_64BIT
  8995. /*
  8996. * Take rq->lock to make 64-bit read safe on 32-bit platforms.
  8997. */
  8998. spin_lock_irq(&cpu_rq(cpu)->lock);
  8999. data = *cpuusage;
  9000. spin_unlock_irq(&cpu_rq(cpu)->lock);
  9001. #else
  9002. data = *cpuusage;
  9003. #endif
  9004. return data;
  9005. }
  9006. static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
  9007. {
  9008. u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  9009. #ifndef CONFIG_64BIT
  9010. /*
  9011. * Take rq->lock to make 64-bit write safe on 32-bit platforms.
  9012. */
  9013. spin_lock_irq(&cpu_rq(cpu)->lock);
  9014. *cpuusage = val;
  9015. spin_unlock_irq(&cpu_rq(cpu)->lock);
  9016. #else
  9017. *cpuusage = val;
  9018. #endif
  9019. }
  9020. /* return total cpu usage (in nanoseconds) of a group */
  9021. static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
  9022. {
  9023. struct cpuacct *ca = cgroup_ca(cgrp);
  9024. u64 totalcpuusage = 0;
  9025. int i;
  9026. for_each_present_cpu(i)
  9027. totalcpuusage += cpuacct_cpuusage_read(ca, i);
  9028. return totalcpuusage;
  9029. }
  9030. static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
  9031. u64 reset)
  9032. {
  9033. struct cpuacct *ca = cgroup_ca(cgrp);
  9034. int err = 0;
  9035. int i;
  9036. if (reset) {
  9037. err = -EINVAL;
  9038. goto out;
  9039. }
  9040. for_each_present_cpu(i)
  9041. cpuacct_cpuusage_write(ca, i, 0);
  9042. out:
  9043. return err;
  9044. }
  9045. static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
  9046. struct seq_file *m)
  9047. {
  9048. struct cpuacct *ca = cgroup_ca(cgroup);
  9049. u64 percpu;
  9050. int i;
  9051. for_each_present_cpu(i) {
  9052. percpu = cpuacct_cpuusage_read(ca, i);
  9053. seq_printf(m, "%llu ", (unsigned long long) percpu);
  9054. }
  9055. seq_printf(m, "\n");
  9056. return 0;
  9057. }
  9058. static const char *cpuacct_stat_desc[] = {
  9059. [CPUACCT_STAT_USER] = "user",
  9060. [CPUACCT_STAT_SYSTEM] = "system",
  9061. };
  9062. static int cpuacct_stats_show(struct cgroup *cgrp, struct cftype *cft,
  9063. struct cgroup_map_cb *cb)
  9064. {
  9065. struct cpuacct *ca = cgroup_ca(cgrp);
  9066. int i;
  9067. for (i = 0; i < CPUACCT_STAT_NSTATS; i++) {
  9068. s64 val = percpu_counter_read(&ca->cpustat[i]);
  9069. val = cputime64_to_clock_t(val);
  9070. cb->fill(cb, cpuacct_stat_desc[i], val);
  9071. }
  9072. return 0;
  9073. }
  9074. static struct cftype files[] = {
  9075. {
  9076. .name = "usage",
  9077. .read_u64 = cpuusage_read,
  9078. .write_u64 = cpuusage_write,
  9079. },
  9080. {
  9081. .name = "usage_percpu",
  9082. .read_seq_string = cpuacct_percpu_seq_read,
  9083. },
  9084. {
  9085. .name = "stat",
  9086. .read_map = cpuacct_stats_show,
  9087. },
  9088. };
  9089. static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
  9090. {
  9091. return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
  9092. }
  9093. /*
  9094. * charge this task's execution time to its accounting group.
  9095. *
  9096. * called with rq->lock held.
  9097. */
  9098. static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
  9099. {
  9100. struct cpuacct *ca;
  9101. int cpu;
  9102. if (unlikely(!cpuacct_subsys.active))
  9103. return;
  9104. cpu = task_cpu(tsk);
  9105. rcu_read_lock();
  9106. ca = task_ca(tsk);
  9107. for (; ca; ca = ca->parent) {
  9108. u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  9109. *cpuusage += cputime;
  9110. }
  9111. rcu_read_unlock();
  9112. }
  9113. /*
  9114. * Charge the system/user time to the task's accounting group.
  9115. */
  9116. static void cpuacct_update_stats(struct task_struct *tsk,
  9117. enum cpuacct_stat_index idx, cputime_t val)
  9118. {
  9119. struct cpuacct *ca;
  9120. if (unlikely(!cpuacct_subsys.active))
  9121. return;
  9122. rcu_read_lock();
  9123. ca = task_ca(tsk);
  9124. do {
  9125. percpu_counter_add(&ca->cpustat[idx], val);
  9126. ca = ca->parent;
  9127. } while (ca);
  9128. rcu_read_unlock();
  9129. }
  9130. struct cgroup_subsys cpuacct_subsys = {
  9131. .name = "cpuacct",
  9132. .create = cpuacct_create,
  9133. .destroy = cpuacct_destroy,
  9134. .populate = cpuacct_populate,
  9135. .subsys_id = cpuacct_subsys_id,
  9136. };
  9137. #endif /* CONFIG_CGROUP_CPUACCT */
  9138. #ifndef CONFIG_SMP
  9139. int rcu_expedited_torture_stats(char *page)
  9140. {
  9141. return 0;
  9142. }
  9143. EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
  9144. void synchronize_sched_expedited(void)
  9145. {
  9146. }
  9147. EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
  9148. #else /* #ifndef CONFIG_SMP */
  9149. static DEFINE_PER_CPU(struct migration_req, rcu_migration_req);
  9150. static DEFINE_MUTEX(rcu_sched_expedited_mutex);
  9151. #define RCU_EXPEDITED_STATE_POST -2
  9152. #define RCU_EXPEDITED_STATE_IDLE -1
  9153. static int rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
  9154. int rcu_expedited_torture_stats(char *page)
  9155. {
  9156. int cnt = 0;
  9157. int cpu;
  9158. cnt += sprintf(&page[cnt], "state: %d /", rcu_expedited_state);
  9159. for_each_online_cpu(cpu) {
  9160. cnt += sprintf(&page[cnt], " %d:%d",
  9161. cpu, per_cpu(rcu_migration_req, cpu).dest_cpu);
  9162. }
  9163. cnt += sprintf(&page[cnt], "\n");
  9164. return cnt;
  9165. }
  9166. EXPORT_SYMBOL_GPL(rcu_expedited_torture_stats);
  9167. static long synchronize_sched_expedited_count;
  9168. /*
  9169. * Wait for an rcu-sched grace period to elapse, but use "big hammer"
  9170. * approach to force grace period to end quickly. This consumes
  9171. * significant time on all CPUs, and is thus not recommended for
  9172. * any sort of common-case code.
  9173. *
  9174. * Note that it is illegal to call this function while holding any
  9175. * lock that is acquired by a CPU-hotplug notifier. Failing to
  9176. * observe this restriction will result in deadlock.
  9177. */
  9178. void synchronize_sched_expedited(void)
  9179. {
  9180. int cpu;
  9181. unsigned long flags;
  9182. bool need_full_sync = 0;
  9183. struct rq *rq;
  9184. struct migration_req *req;
  9185. long snap;
  9186. int trycount = 0;
  9187. smp_mb(); /* ensure prior mod happens before capturing snap. */
  9188. snap = ACCESS_ONCE(synchronize_sched_expedited_count) + 1;
  9189. get_online_cpus();
  9190. while (!mutex_trylock(&rcu_sched_expedited_mutex)) {
  9191. put_online_cpus();
  9192. if (trycount++ < 10)
  9193. udelay(trycount * num_online_cpus());
  9194. else {
  9195. synchronize_sched();
  9196. return;
  9197. }
  9198. if (ACCESS_ONCE(synchronize_sched_expedited_count) - snap > 0) {
  9199. smp_mb(); /* ensure test happens before caller kfree */
  9200. return;
  9201. }
  9202. get_online_cpus();
  9203. }
  9204. rcu_expedited_state = RCU_EXPEDITED_STATE_POST;
  9205. for_each_online_cpu(cpu) {
  9206. rq = cpu_rq(cpu);
  9207. req = &per_cpu(rcu_migration_req, cpu);
  9208. init_completion(&req->done);
  9209. req->task = NULL;
  9210. req->dest_cpu = RCU_MIGRATION_NEED_QS;
  9211. spin_lock_irqsave(&rq->lock, flags);
  9212. list_add(&req->list, &rq->migration_queue);
  9213. spin_unlock_irqrestore(&rq->lock, flags);
  9214. wake_up_process(rq->migration_thread);
  9215. }
  9216. for_each_online_cpu(cpu) {
  9217. rcu_expedited_state = cpu;
  9218. req = &per_cpu(rcu_migration_req, cpu);
  9219. rq = cpu_rq(cpu);
  9220. wait_for_completion(&req->done);
  9221. spin_lock_irqsave(&rq->lock, flags);
  9222. if (unlikely(req->dest_cpu == RCU_MIGRATION_MUST_SYNC))
  9223. need_full_sync = 1;
  9224. req->dest_cpu = RCU_MIGRATION_IDLE;
  9225. spin_unlock_irqrestore(&rq->lock, flags);
  9226. }
  9227. rcu_expedited_state = RCU_EXPEDITED_STATE_IDLE;
  9228. mutex_unlock(&rcu_sched_expedited_mutex);
  9229. put_online_cpus();
  9230. if (need_full_sync)
  9231. synchronize_sched();
  9232. }
  9233. EXPORT_SYMBOL_GPL(synchronize_sched_expedited);
  9234. #endif /* #else #ifndef CONFIG_SMP */