sched.c 223 KB

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