sched.c 221 KB

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