sched.c 173 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083
  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. */
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/nmi.h>
  29. #include <linux/init.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/highmem.h>
  32. #include <linux/smp_lock.h>
  33. #include <asm/mmu_context.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/capability.h>
  36. #include <linux/completion.h>
  37. #include <linux/kernel_stat.h>
  38. #include <linux/debug_locks.h>
  39. #include <linux/security.h>
  40. #include <linux/notifier.h>
  41. #include <linux/profile.h>
  42. #include <linux/freezer.h>
  43. #include <linux/vmalloc.h>
  44. #include <linux/blkdev.h>
  45. #include <linux/delay.h>
  46. #include <linux/pid_namespace.h>
  47. #include <linux/smp.h>
  48. #include <linux/threads.h>
  49. #include <linux/timer.h>
  50. #include <linux/rcupdate.h>
  51. #include <linux/cpu.h>
  52. #include <linux/cpuset.h>
  53. #include <linux/percpu.h>
  54. #include <linux/cpu_acct.h>
  55. #include <linux/kthread.h>
  56. #include <linux/seq_file.h>
  57. #include <linux/sysctl.h>
  58. #include <linux/syscalls.h>
  59. #include <linux/times.h>
  60. #include <linux/tsacct_kern.h>
  61. #include <linux/kprobes.h>
  62. #include <linux/delayacct.h>
  63. #include <linux/reciprocal_div.h>
  64. #include <linux/unistd.h>
  65. #include <linux/pagemap.h>
  66. #include <asm/tlb.h>
  67. /*
  68. * Scheduler clock - returns current time in nanosec units.
  69. * This is default implementation.
  70. * Architectures and sub-architectures can override this.
  71. */
  72. unsigned long long __attribute__((weak)) sched_clock(void)
  73. {
  74. return (unsigned long long)jiffies * (1000000000 / HZ);
  75. }
  76. /*
  77. * Convert user-nice values [ -20 ... 0 ... 19 ]
  78. * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  79. * and back.
  80. */
  81. #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  82. #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  83. #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  84. /*
  85. * 'User priority' is the nice value converted to something we
  86. * can work with better when scaling various scheduler parameters,
  87. * it's a [ 0 ... 39 ] range.
  88. */
  89. #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
  90. #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  91. #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  92. /*
  93. * Some helpers for converting nanosecond timing to jiffy resolution
  94. */
  95. #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (1000000000 / HZ))
  96. #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
  97. #define NICE_0_LOAD SCHED_LOAD_SCALE
  98. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  99. /*
  100. * These are the 'tuning knobs' of the scheduler:
  101. *
  102. * default timeslice is 100 msecs (used only for SCHED_RR tasks).
  103. * Timeslices get refilled after they expire.
  104. */
  105. #define DEF_TIMESLICE (100 * HZ / 1000)
  106. #ifdef CONFIG_SMP
  107. /*
  108. * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
  109. * Since cpu_power is a 'constant', we can use a reciprocal divide.
  110. */
  111. static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
  112. {
  113. return reciprocal_divide(load, sg->reciprocal_cpu_power);
  114. }
  115. /*
  116. * Each time a sched group cpu_power is changed,
  117. * we must compute its reciprocal value
  118. */
  119. static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
  120. {
  121. sg->__cpu_power += val;
  122. sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
  123. }
  124. #endif
  125. static inline int rt_policy(int policy)
  126. {
  127. if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
  128. return 1;
  129. return 0;
  130. }
  131. static inline int task_has_rt_policy(struct task_struct *p)
  132. {
  133. return rt_policy(p->policy);
  134. }
  135. /*
  136. * This is the priority-queue data structure of the RT scheduling class:
  137. */
  138. struct rt_prio_array {
  139. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  140. struct list_head queue[MAX_RT_PRIO];
  141. };
  142. #ifdef CONFIG_FAIR_GROUP_SCHED
  143. struct cfs_rq;
  144. /* task group related information */
  145. struct task_group {
  146. /* schedulable entities of this group on each cpu */
  147. struct sched_entity **se;
  148. /* runqueue "owned" by this group on each cpu */
  149. struct cfs_rq **cfs_rq;
  150. unsigned long shares;
  151. /* spinlock to serialize modification to shares */
  152. spinlock_t lock;
  153. };
  154. /* Default task group's sched entity on each cpu */
  155. static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
  156. /* Default task group's cfs_rq on each cpu */
  157. static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
  158. static struct sched_entity *init_sched_entity_p[NR_CPUS];
  159. static struct cfs_rq *init_cfs_rq_p[NR_CPUS];
  160. /* Default task group.
  161. * Every task in system belong to this group at bootup.
  162. */
  163. struct task_group init_task_group = {
  164. .se = init_sched_entity_p,
  165. .cfs_rq = init_cfs_rq_p,
  166. };
  167. #ifdef CONFIG_FAIR_USER_SCHED
  168. # define INIT_TASK_GRP_LOAD 2*NICE_0_LOAD
  169. #else
  170. # define INIT_TASK_GRP_LOAD NICE_0_LOAD
  171. #endif
  172. static int init_task_group_load = INIT_TASK_GRP_LOAD;
  173. /* return group to which a task belongs */
  174. static inline struct task_group *task_group(struct task_struct *p)
  175. {
  176. struct task_group *tg;
  177. #ifdef CONFIG_FAIR_USER_SCHED
  178. tg = p->user->tg;
  179. #else
  180. tg = &init_task_group;
  181. #endif
  182. return tg;
  183. }
  184. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  185. static inline void set_task_cfs_rq(struct task_struct *p)
  186. {
  187. p->se.cfs_rq = task_group(p)->cfs_rq[task_cpu(p)];
  188. p->se.parent = task_group(p)->se[task_cpu(p)];
  189. }
  190. #else
  191. static inline void set_task_cfs_rq(struct task_struct *p) { }
  192. #endif /* CONFIG_FAIR_GROUP_SCHED */
  193. /* CFS-related fields in a runqueue */
  194. struct cfs_rq {
  195. struct load_weight load;
  196. unsigned long nr_running;
  197. u64 exec_clock;
  198. u64 min_vruntime;
  199. struct rb_root tasks_timeline;
  200. struct rb_node *rb_leftmost;
  201. struct rb_node *rb_load_balance_curr;
  202. /* 'curr' points to currently running entity on this cfs_rq.
  203. * It is set to NULL otherwise (i.e when none are currently running).
  204. */
  205. struct sched_entity *curr;
  206. unsigned long nr_spread_over;
  207. #ifdef CONFIG_FAIR_GROUP_SCHED
  208. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  209. /* leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  210. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  211. * (like users, containers etc.)
  212. *
  213. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  214. * list is used during load balance.
  215. */
  216. struct list_head leaf_cfs_rq_list; /* Better name : task_cfs_rq_list? */
  217. struct task_group *tg; /* group that "owns" this runqueue */
  218. struct rcu_head rcu;
  219. #endif
  220. };
  221. /* Real-Time classes' related field in a runqueue: */
  222. struct rt_rq {
  223. struct rt_prio_array active;
  224. int rt_load_balance_idx;
  225. struct list_head *rt_load_balance_head, *rt_load_balance_curr;
  226. };
  227. /*
  228. * This is the main, per-CPU runqueue data structure.
  229. *
  230. * Locking rule: those places that want to lock multiple runqueues
  231. * (such as the load balancing or the thread migration code), lock
  232. * acquire operations must be ordered by ascending &runqueue.
  233. */
  234. struct rq {
  235. /* runqueue lock: */
  236. spinlock_t lock;
  237. /*
  238. * nr_running and cpu_load should be in the same cacheline because
  239. * remote CPUs use both these fields when doing load calculation.
  240. */
  241. unsigned long nr_running;
  242. #define CPU_LOAD_IDX_MAX 5
  243. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  244. unsigned char idle_at_tick;
  245. #ifdef CONFIG_NO_HZ
  246. unsigned char in_nohz_recently;
  247. #endif
  248. /* capture load from *all* tasks on this cpu: */
  249. struct load_weight load;
  250. unsigned long nr_load_updates;
  251. u64 nr_switches;
  252. struct cfs_rq cfs;
  253. #ifdef CONFIG_FAIR_GROUP_SCHED
  254. /* list of leaf cfs_rq on this cpu: */
  255. struct list_head leaf_cfs_rq_list;
  256. #endif
  257. struct rt_rq rt;
  258. /*
  259. * This is part of a global counter where only the total sum
  260. * over all CPUs matters. A task can increase this counter on
  261. * one CPU and if it got migrated afterwards it may decrease
  262. * it on another CPU. Always updated under the runqueue lock:
  263. */
  264. unsigned long nr_uninterruptible;
  265. struct task_struct *curr, *idle;
  266. unsigned long next_balance;
  267. struct mm_struct *prev_mm;
  268. u64 clock, prev_clock_raw;
  269. s64 clock_max_delta;
  270. unsigned int clock_warps, clock_overflows;
  271. u64 idle_clock;
  272. unsigned int clock_deep_idle_events;
  273. u64 tick_timestamp;
  274. atomic_t nr_iowait;
  275. #ifdef CONFIG_SMP
  276. struct sched_domain *sd;
  277. /* For active balancing */
  278. int active_balance;
  279. int push_cpu;
  280. /* cpu of this runqueue: */
  281. int cpu;
  282. struct task_struct *migration_thread;
  283. struct list_head migration_queue;
  284. #endif
  285. #ifdef CONFIG_SCHEDSTATS
  286. /* latency stats */
  287. struct sched_info rq_sched_info;
  288. /* sys_sched_yield() stats */
  289. unsigned int yld_exp_empty;
  290. unsigned int yld_act_empty;
  291. unsigned int yld_both_empty;
  292. unsigned int yld_count;
  293. /* schedule() stats */
  294. unsigned int sched_switch;
  295. unsigned int sched_count;
  296. unsigned int sched_goidle;
  297. /* try_to_wake_up() stats */
  298. unsigned int ttwu_count;
  299. unsigned int ttwu_local;
  300. /* BKL stats */
  301. unsigned int bkl_count;
  302. #endif
  303. struct lock_class_key rq_lock_key;
  304. };
  305. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  306. static DEFINE_MUTEX(sched_hotcpu_mutex);
  307. static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
  308. {
  309. rq->curr->sched_class->check_preempt_curr(rq, p);
  310. }
  311. static inline int cpu_of(struct rq *rq)
  312. {
  313. #ifdef CONFIG_SMP
  314. return rq->cpu;
  315. #else
  316. return 0;
  317. #endif
  318. }
  319. /*
  320. * Update the per-runqueue clock, as finegrained as the platform can give
  321. * us, but without assuming monotonicity, etc.:
  322. */
  323. static void __update_rq_clock(struct rq *rq)
  324. {
  325. u64 prev_raw = rq->prev_clock_raw;
  326. u64 now = sched_clock();
  327. s64 delta = now - prev_raw;
  328. u64 clock = rq->clock;
  329. #ifdef CONFIG_SCHED_DEBUG
  330. WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
  331. #endif
  332. /*
  333. * Protect against sched_clock() occasionally going backwards:
  334. */
  335. if (unlikely(delta < 0)) {
  336. clock++;
  337. rq->clock_warps++;
  338. } else {
  339. /*
  340. * Catch too large forward jumps too:
  341. */
  342. if (unlikely(clock + delta > rq->tick_timestamp + TICK_NSEC)) {
  343. if (clock < rq->tick_timestamp + TICK_NSEC)
  344. clock = rq->tick_timestamp + TICK_NSEC;
  345. else
  346. clock++;
  347. rq->clock_overflows++;
  348. } else {
  349. if (unlikely(delta > rq->clock_max_delta))
  350. rq->clock_max_delta = delta;
  351. clock += delta;
  352. }
  353. }
  354. rq->prev_clock_raw = now;
  355. rq->clock = clock;
  356. }
  357. static void update_rq_clock(struct rq *rq)
  358. {
  359. if (likely(smp_processor_id() == cpu_of(rq)))
  360. __update_rq_clock(rq);
  361. }
  362. /*
  363. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  364. * See detach_destroy_domains: synchronize_sched for details.
  365. *
  366. * The domain tree of any CPU may only be accessed from within
  367. * preempt-disabled sections.
  368. */
  369. #define for_each_domain(cpu, __sd) \
  370. for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
  371. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  372. #define this_rq() (&__get_cpu_var(runqueues))
  373. #define task_rq(p) cpu_rq(task_cpu(p))
  374. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  375. /*
  376. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  377. */
  378. #ifdef CONFIG_SCHED_DEBUG
  379. # define const_debug __read_mostly
  380. #else
  381. # define const_debug static const
  382. #endif
  383. /*
  384. * Debugging: various feature bits
  385. */
  386. enum {
  387. SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
  388. SCHED_FEAT_START_DEBIT = 2,
  389. SCHED_FEAT_TREE_AVG = 4,
  390. SCHED_FEAT_APPROX_AVG = 8,
  391. SCHED_FEAT_WAKEUP_PREEMPT = 16,
  392. SCHED_FEAT_PREEMPT_RESTRICT = 32,
  393. };
  394. const_debug unsigned int sysctl_sched_features =
  395. SCHED_FEAT_NEW_FAIR_SLEEPERS * 1 |
  396. SCHED_FEAT_START_DEBIT * 1 |
  397. SCHED_FEAT_TREE_AVG * 0 |
  398. SCHED_FEAT_APPROX_AVG * 0 |
  399. SCHED_FEAT_WAKEUP_PREEMPT * 1 |
  400. SCHED_FEAT_PREEMPT_RESTRICT * 1;
  401. #define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
  402. /*
  403. * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
  404. * clock constructed from sched_clock():
  405. */
  406. unsigned long long cpu_clock(int cpu)
  407. {
  408. unsigned long long now;
  409. unsigned long flags;
  410. struct rq *rq;
  411. local_irq_save(flags);
  412. rq = cpu_rq(cpu);
  413. update_rq_clock(rq);
  414. now = rq->clock;
  415. local_irq_restore(flags);
  416. return now;
  417. }
  418. EXPORT_SYMBOL_GPL(cpu_clock);
  419. #ifndef prepare_arch_switch
  420. # define prepare_arch_switch(next) do { } while (0)
  421. #endif
  422. #ifndef finish_arch_switch
  423. # define finish_arch_switch(prev) do { } while (0)
  424. #endif
  425. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  426. static inline int task_running(struct rq *rq, struct task_struct *p)
  427. {
  428. return rq->curr == p;
  429. }
  430. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  431. {
  432. }
  433. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  434. {
  435. #ifdef CONFIG_DEBUG_SPINLOCK
  436. /* this is a valid case when another task releases the spinlock */
  437. rq->lock.owner = current;
  438. #endif
  439. /*
  440. * If we are tracking spinlock dependencies then we have to
  441. * fix up the runqueue lock - which gets 'carried over' from
  442. * prev into current:
  443. */
  444. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  445. spin_unlock_irq(&rq->lock);
  446. }
  447. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  448. static inline int task_running(struct rq *rq, struct task_struct *p)
  449. {
  450. #ifdef CONFIG_SMP
  451. return p->oncpu;
  452. #else
  453. return rq->curr == p;
  454. #endif
  455. }
  456. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  457. {
  458. #ifdef CONFIG_SMP
  459. /*
  460. * We can optimise this out completely for !SMP, because the
  461. * SMP rebalancing from interrupt is the only thing that cares
  462. * here.
  463. */
  464. next->oncpu = 1;
  465. #endif
  466. #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  467. spin_unlock_irq(&rq->lock);
  468. #else
  469. spin_unlock(&rq->lock);
  470. #endif
  471. }
  472. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  473. {
  474. #ifdef CONFIG_SMP
  475. /*
  476. * After ->oncpu is cleared, the task can be moved to a different CPU.
  477. * We must ensure this doesn't happen until the switch is completely
  478. * finished.
  479. */
  480. smp_wmb();
  481. prev->oncpu = 0;
  482. #endif
  483. #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  484. local_irq_enable();
  485. #endif
  486. }
  487. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  488. /*
  489. * __task_rq_lock - lock the runqueue a given task resides on.
  490. * Must be called interrupts disabled.
  491. */
  492. static inline struct rq *__task_rq_lock(struct task_struct *p)
  493. __acquires(rq->lock)
  494. {
  495. for (;;) {
  496. struct rq *rq = task_rq(p);
  497. spin_lock(&rq->lock);
  498. if (likely(rq == task_rq(p)))
  499. return rq;
  500. spin_unlock(&rq->lock);
  501. }
  502. }
  503. /*
  504. * task_rq_lock - lock the runqueue a given task resides on and disable
  505. * interrupts. Note the ordering: we can safely lookup the task_rq without
  506. * explicitly disabling preemption.
  507. */
  508. static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
  509. __acquires(rq->lock)
  510. {
  511. struct rq *rq;
  512. for (;;) {
  513. local_irq_save(*flags);
  514. rq = task_rq(p);
  515. spin_lock(&rq->lock);
  516. if (likely(rq == task_rq(p)))
  517. return rq;
  518. spin_unlock_irqrestore(&rq->lock, *flags);
  519. }
  520. }
  521. static void __task_rq_unlock(struct rq *rq)
  522. __releases(rq->lock)
  523. {
  524. spin_unlock(&rq->lock);
  525. }
  526. static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
  527. __releases(rq->lock)
  528. {
  529. spin_unlock_irqrestore(&rq->lock, *flags);
  530. }
  531. /*
  532. * this_rq_lock - lock this runqueue and disable interrupts.
  533. */
  534. static struct rq *this_rq_lock(void)
  535. __acquires(rq->lock)
  536. {
  537. struct rq *rq;
  538. local_irq_disable();
  539. rq = this_rq();
  540. spin_lock(&rq->lock);
  541. return rq;
  542. }
  543. /*
  544. * We are going deep-idle (irqs are disabled):
  545. */
  546. void sched_clock_idle_sleep_event(void)
  547. {
  548. struct rq *rq = cpu_rq(smp_processor_id());
  549. spin_lock(&rq->lock);
  550. __update_rq_clock(rq);
  551. spin_unlock(&rq->lock);
  552. rq->clock_deep_idle_events++;
  553. }
  554. EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
  555. /*
  556. * We just idled delta nanoseconds (called with irqs disabled):
  557. */
  558. void sched_clock_idle_wakeup_event(u64 delta_ns)
  559. {
  560. struct rq *rq = cpu_rq(smp_processor_id());
  561. u64 now = sched_clock();
  562. rq->idle_clock += delta_ns;
  563. /*
  564. * Override the previous timestamp and ignore all
  565. * sched_clock() deltas that occured while we idled,
  566. * and use the PM-provided delta_ns to advance the
  567. * rq clock:
  568. */
  569. spin_lock(&rq->lock);
  570. rq->prev_clock_raw = now;
  571. rq->clock += delta_ns;
  572. spin_unlock(&rq->lock);
  573. }
  574. EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
  575. /*
  576. * resched_task - mark a task 'to be rescheduled now'.
  577. *
  578. * On UP this means the setting of the need_resched flag, on SMP it
  579. * might also involve a cross-CPU call to trigger the scheduler on
  580. * the target CPU.
  581. */
  582. #ifdef CONFIG_SMP
  583. #ifndef tsk_is_polling
  584. #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
  585. #endif
  586. static void resched_task(struct task_struct *p)
  587. {
  588. int cpu;
  589. assert_spin_locked(&task_rq(p)->lock);
  590. if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
  591. return;
  592. set_tsk_thread_flag(p, TIF_NEED_RESCHED);
  593. cpu = task_cpu(p);
  594. if (cpu == smp_processor_id())
  595. return;
  596. /* NEED_RESCHED must be visible before we test polling */
  597. smp_mb();
  598. if (!tsk_is_polling(p))
  599. smp_send_reschedule(cpu);
  600. }
  601. static void resched_cpu(int cpu)
  602. {
  603. struct rq *rq = cpu_rq(cpu);
  604. unsigned long flags;
  605. if (!spin_trylock_irqsave(&rq->lock, flags))
  606. return;
  607. resched_task(cpu_curr(cpu));
  608. spin_unlock_irqrestore(&rq->lock, flags);
  609. }
  610. #else
  611. static inline void resched_task(struct task_struct *p)
  612. {
  613. assert_spin_locked(&task_rq(p)->lock);
  614. set_tsk_need_resched(p);
  615. }
  616. #endif
  617. #if BITS_PER_LONG == 32
  618. # define WMULT_CONST (~0UL)
  619. #else
  620. # define WMULT_CONST (1UL << 32)
  621. #endif
  622. #define WMULT_SHIFT 32
  623. /*
  624. * Shift right and round:
  625. */
  626. #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
  627. static unsigned long
  628. calc_delta_mine(unsigned long delta_exec, unsigned long weight,
  629. struct load_weight *lw)
  630. {
  631. u64 tmp;
  632. if (unlikely(!lw->inv_weight))
  633. lw->inv_weight = (WMULT_CONST - lw->weight/2) / lw->weight + 1;
  634. tmp = (u64)delta_exec * weight;
  635. /*
  636. * Check whether we'd overflow the 64-bit multiplication:
  637. */
  638. if (unlikely(tmp > WMULT_CONST))
  639. tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
  640. WMULT_SHIFT/2);
  641. else
  642. tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
  643. return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
  644. }
  645. static inline unsigned long
  646. calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
  647. {
  648. return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
  649. }
  650. static inline void update_load_add(struct load_weight *lw, unsigned long inc)
  651. {
  652. lw->weight += inc;
  653. }
  654. static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
  655. {
  656. lw->weight -= dec;
  657. }
  658. /*
  659. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  660. * of tasks with abnormal "nice" values across CPUs the contribution that
  661. * each task makes to its run queue's load is weighted according to its
  662. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  663. * scaled version of the new time slice allocation that they receive on time
  664. * slice expiry etc.
  665. */
  666. #define WEIGHT_IDLEPRIO 2
  667. #define WMULT_IDLEPRIO (1 << 31)
  668. /*
  669. * Nice levels are multiplicative, with a gentle 10% change for every
  670. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  671. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  672. * that remained on nice 0.
  673. *
  674. * The "10% effect" is relative and cumulative: from _any_ nice level,
  675. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  676. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  677. * If a task goes up by ~10% and another task goes down by ~10% then
  678. * the relative distance between them is ~25%.)
  679. */
  680. static const int prio_to_weight[40] = {
  681. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  682. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  683. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  684. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  685. /* 0 */ 1024, 820, 655, 526, 423,
  686. /* 5 */ 335, 272, 215, 172, 137,
  687. /* 10 */ 110, 87, 70, 56, 45,
  688. /* 15 */ 36, 29, 23, 18, 15,
  689. };
  690. /*
  691. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  692. *
  693. * In cases where the weight does not change often, we can use the
  694. * precalculated inverse to speed up arithmetics by turning divisions
  695. * into multiplications:
  696. */
  697. static const u32 prio_to_wmult[40] = {
  698. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  699. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  700. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  701. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  702. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  703. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  704. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  705. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  706. };
  707. static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
  708. /*
  709. * runqueue iterator, to support SMP load-balancing between different
  710. * scheduling classes, without having to expose their internal data
  711. * structures to the load-balancing proper:
  712. */
  713. struct rq_iterator {
  714. void *arg;
  715. struct task_struct *(*start)(void *);
  716. struct task_struct *(*next)(void *);
  717. };
  718. static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  719. unsigned long max_nr_move, unsigned long max_load_move,
  720. struct sched_domain *sd, enum cpu_idle_type idle,
  721. int *all_pinned, unsigned long *load_moved,
  722. int *this_best_prio, struct rq_iterator *iterator);
  723. #include "sched_stats.h"
  724. #include "sched_idletask.c"
  725. #include "sched_fair.c"
  726. #include "sched_rt.c"
  727. #ifdef CONFIG_SCHED_DEBUG
  728. # include "sched_debug.c"
  729. #endif
  730. #define sched_class_highest (&rt_sched_class)
  731. /*
  732. * Update delta_exec, delta_fair fields for rq.
  733. *
  734. * delta_fair clock advances at a rate inversely proportional to
  735. * total load (rq->load.weight) on the runqueue, while
  736. * delta_exec advances at the same rate as wall-clock (provided
  737. * cpu is not idle).
  738. *
  739. * delta_exec / delta_fair is a measure of the (smoothened) load on this
  740. * runqueue over any given interval. This (smoothened) load is used
  741. * during load balance.
  742. *
  743. * This function is called /before/ updating rq->load
  744. * and when switching tasks.
  745. */
  746. static inline void inc_load(struct rq *rq, const struct task_struct *p)
  747. {
  748. update_load_add(&rq->load, p->se.load.weight);
  749. }
  750. static inline void dec_load(struct rq *rq, const struct task_struct *p)
  751. {
  752. update_load_sub(&rq->load, p->se.load.weight);
  753. }
  754. static void inc_nr_running(struct task_struct *p, struct rq *rq)
  755. {
  756. rq->nr_running++;
  757. inc_load(rq, p);
  758. }
  759. static void dec_nr_running(struct task_struct *p, struct rq *rq)
  760. {
  761. rq->nr_running--;
  762. dec_load(rq, p);
  763. }
  764. static void set_load_weight(struct task_struct *p)
  765. {
  766. if (task_has_rt_policy(p)) {
  767. p->se.load.weight = prio_to_weight[0] * 2;
  768. p->se.load.inv_weight = prio_to_wmult[0] >> 1;
  769. return;
  770. }
  771. /*
  772. * SCHED_IDLE tasks get minimal weight:
  773. */
  774. if (p->policy == SCHED_IDLE) {
  775. p->se.load.weight = WEIGHT_IDLEPRIO;
  776. p->se.load.inv_weight = WMULT_IDLEPRIO;
  777. return;
  778. }
  779. p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
  780. p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
  781. }
  782. static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
  783. {
  784. sched_info_queued(p);
  785. p->sched_class->enqueue_task(rq, p, wakeup);
  786. p->se.on_rq = 1;
  787. }
  788. static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
  789. {
  790. p->sched_class->dequeue_task(rq, p, sleep);
  791. p->se.on_rq = 0;
  792. }
  793. /*
  794. * __normal_prio - return the priority that is based on the static prio
  795. */
  796. static inline int __normal_prio(struct task_struct *p)
  797. {
  798. return p->static_prio;
  799. }
  800. /*
  801. * Calculate the expected normal priority: i.e. priority
  802. * without taking RT-inheritance into account. Might be
  803. * boosted by interactivity modifiers. Changes upon fork,
  804. * setprio syscalls, and whenever the interactivity
  805. * estimator recalculates.
  806. */
  807. static inline int normal_prio(struct task_struct *p)
  808. {
  809. int prio;
  810. if (task_has_rt_policy(p))
  811. prio = MAX_RT_PRIO-1 - p->rt_priority;
  812. else
  813. prio = __normal_prio(p);
  814. return prio;
  815. }
  816. /*
  817. * Calculate the current priority, i.e. the priority
  818. * taken into account by the scheduler. This value might
  819. * be boosted by RT tasks, or might be boosted by
  820. * interactivity modifiers. Will be RT if the task got
  821. * RT-boosted. If not then it returns p->normal_prio.
  822. */
  823. static int effective_prio(struct task_struct *p)
  824. {
  825. p->normal_prio = normal_prio(p);
  826. /*
  827. * If we are RT tasks or we were boosted to RT priority,
  828. * keep the priority unchanged. Otherwise, update priority
  829. * to the normal priority:
  830. */
  831. if (!rt_prio(p->prio))
  832. return p->normal_prio;
  833. return p->prio;
  834. }
  835. /*
  836. * activate_task - move a task to the runqueue.
  837. */
  838. static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
  839. {
  840. if (p->state == TASK_UNINTERRUPTIBLE)
  841. rq->nr_uninterruptible--;
  842. enqueue_task(rq, p, wakeup);
  843. inc_nr_running(p, rq);
  844. }
  845. /*
  846. * deactivate_task - remove a task from the runqueue.
  847. */
  848. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
  849. {
  850. if (p->state == TASK_UNINTERRUPTIBLE)
  851. rq->nr_uninterruptible++;
  852. dequeue_task(rq, p, sleep);
  853. dec_nr_running(p, rq);
  854. }
  855. /**
  856. * task_curr - is this task currently executing on a CPU?
  857. * @p: the task in question.
  858. */
  859. inline int task_curr(const struct task_struct *p)
  860. {
  861. return cpu_curr(task_cpu(p)) == p;
  862. }
  863. /* Used instead of source_load when we know the type == 0 */
  864. unsigned long weighted_cpuload(const int cpu)
  865. {
  866. return cpu_rq(cpu)->load.weight;
  867. }
  868. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  869. {
  870. #ifdef CONFIG_SMP
  871. task_thread_info(p)->cpu = cpu;
  872. #endif
  873. set_task_cfs_rq(p);
  874. }
  875. #ifdef CONFIG_SMP
  876. /*
  877. * Is this task likely cache-hot:
  878. */
  879. static inline int
  880. task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
  881. {
  882. s64 delta;
  883. if (p->sched_class != &fair_sched_class)
  884. return 0;
  885. if (sysctl_sched_migration_cost == -1)
  886. return 1;
  887. if (sysctl_sched_migration_cost == 0)
  888. return 0;
  889. delta = now - p->se.exec_start;
  890. return delta < (s64)sysctl_sched_migration_cost;
  891. }
  892. void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
  893. {
  894. int old_cpu = task_cpu(p);
  895. struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
  896. struct cfs_rq *old_cfsrq = task_cfs_rq(p),
  897. *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
  898. u64 clock_offset;
  899. clock_offset = old_rq->clock - new_rq->clock;
  900. #ifdef CONFIG_SCHEDSTATS
  901. if (p->se.wait_start)
  902. p->se.wait_start -= clock_offset;
  903. if (p->se.sleep_start)
  904. p->se.sleep_start -= clock_offset;
  905. if (p->se.block_start)
  906. p->se.block_start -= clock_offset;
  907. if (old_cpu != new_cpu) {
  908. schedstat_inc(p, se.nr_migrations);
  909. if (task_hot(p, old_rq->clock, NULL))
  910. schedstat_inc(p, se.nr_forced2_migrations);
  911. }
  912. #endif
  913. p->se.vruntime -= old_cfsrq->min_vruntime -
  914. new_cfsrq->min_vruntime;
  915. __set_task_cpu(p, new_cpu);
  916. }
  917. struct migration_req {
  918. struct list_head list;
  919. struct task_struct *task;
  920. int dest_cpu;
  921. struct completion done;
  922. };
  923. /*
  924. * The task's runqueue lock must be held.
  925. * Returns true if you have to wait for migration thread.
  926. */
  927. static int
  928. migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
  929. {
  930. struct rq *rq = task_rq(p);
  931. /*
  932. * If the task is not on a runqueue (and not running), then
  933. * it is sufficient to simply update the task's cpu field.
  934. */
  935. if (!p->se.on_rq && !task_running(rq, p)) {
  936. set_task_cpu(p, dest_cpu);
  937. return 0;
  938. }
  939. init_completion(&req->done);
  940. req->task = p;
  941. req->dest_cpu = dest_cpu;
  942. list_add(&req->list, &rq->migration_queue);
  943. return 1;
  944. }
  945. /*
  946. * wait_task_inactive - wait for a thread to unschedule.
  947. *
  948. * The caller must ensure that the task *will* unschedule sometime soon,
  949. * else this function might spin for a *long* time. This function can't
  950. * be called with interrupts off, or it may introduce deadlock with
  951. * smp_call_function() if an IPI is sent by the same process we are
  952. * waiting to become inactive.
  953. */
  954. void wait_task_inactive(struct task_struct *p)
  955. {
  956. unsigned long flags;
  957. int running, on_rq;
  958. struct rq *rq;
  959. for (;;) {
  960. /*
  961. * We do the initial early heuristics without holding
  962. * any task-queue locks at all. We'll only try to get
  963. * the runqueue lock when things look like they will
  964. * work out!
  965. */
  966. rq = task_rq(p);
  967. /*
  968. * If the task is actively running on another CPU
  969. * still, just relax and busy-wait without holding
  970. * any locks.
  971. *
  972. * NOTE! Since we don't hold any locks, it's not
  973. * even sure that "rq" stays as the right runqueue!
  974. * But we don't care, since "task_running()" will
  975. * return false if the runqueue has changed and p
  976. * is actually now running somewhere else!
  977. */
  978. while (task_running(rq, p))
  979. cpu_relax();
  980. /*
  981. * Ok, time to look more closely! We need the rq
  982. * lock now, to be *sure*. If we're wrong, we'll
  983. * just go back and repeat.
  984. */
  985. rq = task_rq_lock(p, &flags);
  986. running = task_running(rq, p);
  987. on_rq = p->se.on_rq;
  988. task_rq_unlock(rq, &flags);
  989. /*
  990. * Was it really running after all now that we
  991. * checked with the proper locks actually held?
  992. *
  993. * Oops. Go back and try again..
  994. */
  995. if (unlikely(running)) {
  996. cpu_relax();
  997. continue;
  998. }
  999. /*
  1000. * It's not enough that it's not actively running,
  1001. * it must be off the runqueue _entirely_, and not
  1002. * preempted!
  1003. *
  1004. * So if it wa still runnable (but just not actively
  1005. * running right now), it's preempted, and we should
  1006. * yield - it could be a while.
  1007. */
  1008. if (unlikely(on_rq)) {
  1009. schedule_timeout_uninterruptible(1);
  1010. continue;
  1011. }
  1012. /*
  1013. * Ahh, all good. It wasn't running, and it wasn't
  1014. * runnable, which means that it will never become
  1015. * running in the future either. We're all done!
  1016. */
  1017. break;
  1018. }
  1019. }
  1020. /***
  1021. * kick_process - kick a running thread to enter/exit the kernel
  1022. * @p: the to-be-kicked thread
  1023. *
  1024. * Cause a process which is running on another CPU to enter
  1025. * kernel-mode, without any delay. (to get signals handled.)
  1026. *
  1027. * NOTE: this function doesnt have to take the runqueue lock,
  1028. * because all it wants to ensure is that the remote task enters
  1029. * the kernel. If the IPI races and the task has been migrated
  1030. * to another CPU then no harm is done and the purpose has been
  1031. * achieved as well.
  1032. */
  1033. void kick_process(struct task_struct *p)
  1034. {
  1035. int cpu;
  1036. preempt_disable();
  1037. cpu = task_cpu(p);
  1038. if ((cpu != smp_processor_id()) && task_curr(p))
  1039. smp_send_reschedule(cpu);
  1040. preempt_enable();
  1041. }
  1042. /*
  1043. * Return a low guess at the load of a migration-source cpu weighted
  1044. * according to the scheduling class and "nice" value.
  1045. *
  1046. * We want to under-estimate the load of migration sources, to
  1047. * balance conservatively.
  1048. */
  1049. static unsigned long source_load(int cpu, int type)
  1050. {
  1051. struct rq *rq = cpu_rq(cpu);
  1052. unsigned long total = weighted_cpuload(cpu);
  1053. if (type == 0)
  1054. return total;
  1055. return min(rq->cpu_load[type-1], total);
  1056. }
  1057. /*
  1058. * Return a high guess at the load of a migration-target cpu weighted
  1059. * according to the scheduling class and "nice" value.
  1060. */
  1061. static unsigned long target_load(int cpu, int type)
  1062. {
  1063. struct rq *rq = cpu_rq(cpu);
  1064. unsigned long total = weighted_cpuload(cpu);
  1065. if (type == 0)
  1066. return total;
  1067. return max(rq->cpu_load[type-1], total);
  1068. }
  1069. /*
  1070. * Return the average load per task on the cpu's run queue
  1071. */
  1072. static inline unsigned long cpu_avg_load_per_task(int cpu)
  1073. {
  1074. struct rq *rq = cpu_rq(cpu);
  1075. unsigned long total = weighted_cpuload(cpu);
  1076. unsigned long n = rq->nr_running;
  1077. return n ? total / n : SCHED_LOAD_SCALE;
  1078. }
  1079. /*
  1080. * find_idlest_group finds and returns the least busy CPU group within the
  1081. * domain.
  1082. */
  1083. static struct sched_group *
  1084. find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
  1085. {
  1086. struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
  1087. unsigned long min_load = ULONG_MAX, this_load = 0;
  1088. int load_idx = sd->forkexec_idx;
  1089. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  1090. do {
  1091. unsigned long load, avg_load;
  1092. int local_group;
  1093. int i;
  1094. /* Skip over this group if it has no CPUs allowed */
  1095. if (!cpus_intersects(group->cpumask, p->cpus_allowed))
  1096. continue;
  1097. local_group = cpu_isset(this_cpu, group->cpumask);
  1098. /* Tally up the load of all CPUs in the group */
  1099. avg_load = 0;
  1100. for_each_cpu_mask(i, group->cpumask) {
  1101. /* Bias balancing toward cpus of our domain */
  1102. if (local_group)
  1103. load = source_load(i, load_idx);
  1104. else
  1105. load = target_load(i, load_idx);
  1106. avg_load += load;
  1107. }
  1108. /* Adjust by relative CPU power of the group */
  1109. avg_load = sg_div_cpu_power(group,
  1110. avg_load * SCHED_LOAD_SCALE);
  1111. if (local_group) {
  1112. this_load = avg_load;
  1113. this = group;
  1114. } else if (avg_load < min_load) {
  1115. min_load = avg_load;
  1116. idlest = group;
  1117. }
  1118. } while (group = group->next, group != sd->groups);
  1119. if (!idlest || 100*this_load < imbalance*min_load)
  1120. return NULL;
  1121. return idlest;
  1122. }
  1123. /*
  1124. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  1125. */
  1126. static int
  1127. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  1128. {
  1129. cpumask_t tmp;
  1130. unsigned long load, min_load = ULONG_MAX;
  1131. int idlest = -1;
  1132. int i;
  1133. /* Traverse only the allowed CPUs */
  1134. cpus_and(tmp, group->cpumask, p->cpus_allowed);
  1135. for_each_cpu_mask(i, tmp) {
  1136. load = weighted_cpuload(i);
  1137. if (load < min_load || (load == min_load && i == this_cpu)) {
  1138. min_load = load;
  1139. idlest = i;
  1140. }
  1141. }
  1142. return idlest;
  1143. }
  1144. /*
  1145. * sched_balance_self: balance the current task (running on cpu) in domains
  1146. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  1147. * SD_BALANCE_EXEC.
  1148. *
  1149. * Balance, ie. select the least loaded group.
  1150. *
  1151. * Returns the target CPU number, or the same CPU if no balancing is needed.
  1152. *
  1153. * preempt must be disabled.
  1154. */
  1155. static int sched_balance_self(int cpu, int flag)
  1156. {
  1157. struct task_struct *t = current;
  1158. struct sched_domain *tmp, *sd = NULL;
  1159. for_each_domain(cpu, tmp) {
  1160. /*
  1161. * If power savings logic is enabled for a domain, stop there.
  1162. */
  1163. if (tmp->flags & SD_POWERSAVINGS_BALANCE)
  1164. break;
  1165. if (tmp->flags & flag)
  1166. sd = tmp;
  1167. }
  1168. while (sd) {
  1169. cpumask_t span;
  1170. struct sched_group *group;
  1171. int new_cpu, weight;
  1172. if (!(sd->flags & flag)) {
  1173. sd = sd->child;
  1174. continue;
  1175. }
  1176. span = sd->span;
  1177. group = find_idlest_group(sd, t, cpu);
  1178. if (!group) {
  1179. sd = sd->child;
  1180. continue;
  1181. }
  1182. new_cpu = find_idlest_cpu(group, t, cpu);
  1183. if (new_cpu == -1 || new_cpu == cpu) {
  1184. /* Now try balancing at a lower domain level of cpu */
  1185. sd = sd->child;
  1186. continue;
  1187. }
  1188. /* Now try balancing at a lower domain level of new_cpu */
  1189. cpu = new_cpu;
  1190. sd = NULL;
  1191. weight = cpus_weight(span);
  1192. for_each_domain(cpu, tmp) {
  1193. if (weight <= cpus_weight(tmp->span))
  1194. break;
  1195. if (tmp->flags & flag)
  1196. sd = tmp;
  1197. }
  1198. /* while loop will break here if sd == NULL */
  1199. }
  1200. return cpu;
  1201. }
  1202. #endif /* CONFIG_SMP */
  1203. /*
  1204. * wake_idle() will wake a task on an idle cpu if task->cpu is
  1205. * not idle and an idle cpu is available. The span of cpus to
  1206. * search starts with cpus closest then further out as needed,
  1207. * so we always favor a closer, idle cpu.
  1208. *
  1209. * Returns the CPU we should wake onto.
  1210. */
  1211. #if defined(ARCH_HAS_SCHED_WAKE_IDLE)
  1212. static int wake_idle(int cpu, struct task_struct *p)
  1213. {
  1214. cpumask_t tmp;
  1215. struct sched_domain *sd;
  1216. int i;
  1217. /*
  1218. * If it is idle, then it is the best cpu to run this task.
  1219. *
  1220. * This cpu is also the best, if it has more than one task already.
  1221. * Siblings must be also busy(in most cases) as they didn't already
  1222. * pickup the extra load from this cpu and hence we need not check
  1223. * sibling runqueue info. This will avoid the checks and cache miss
  1224. * penalities associated with that.
  1225. */
  1226. if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1)
  1227. return cpu;
  1228. for_each_domain(cpu, sd) {
  1229. if (sd->flags & SD_WAKE_IDLE) {
  1230. cpus_and(tmp, sd->span, p->cpus_allowed);
  1231. for_each_cpu_mask(i, tmp) {
  1232. if (idle_cpu(i)) {
  1233. if (i != task_cpu(p)) {
  1234. schedstat_inc(p,
  1235. se.nr_wakeups_idle);
  1236. }
  1237. return i;
  1238. }
  1239. }
  1240. } else {
  1241. break;
  1242. }
  1243. }
  1244. return cpu;
  1245. }
  1246. #else
  1247. static inline int wake_idle(int cpu, struct task_struct *p)
  1248. {
  1249. return cpu;
  1250. }
  1251. #endif
  1252. /***
  1253. * try_to_wake_up - wake up a thread
  1254. * @p: the to-be-woken-up thread
  1255. * @state: the mask of task states that can be woken
  1256. * @sync: do a synchronous wakeup?
  1257. *
  1258. * Put it on the run-queue if it's not already there. The "current"
  1259. * thread is always on the run-queue (except when the actual
  1260. * re-schedule is in progress), and as such you're allowed to do
  1261. * the simpler "current->state = TASK_RUNNING" to mark yourself
  1262. * runnable without the overhead of this.
  1263. *
  1264. * returns failure only if the task is already active.
  1265. */
  1266. static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
  1267. {
  1268. int cpu, orig_cpu, this_cpu, success = 0;
  1269. unsigned long flags;
  1270. long old_state;
  1271. struct rq *rq;
  1272. #ifdef CONFIG_SMP
  1273. struct sched_domain *sd, *this_sd = NULL;
  1274. unsigned long load, this_load;
  1275. int new_cpu;
  1276. #endif
  1277. rq = task_rq_lock(p, &flags);
  1278. old_state = p->state;
  1279. if (!(old_state & state))
  1280. goto out;
  1281. if (p->se.on_rq)
  1282. goto out_running;
  1283. cpu = task_cpu(p);
  1284. orig_cpu = cpu;
  1285. this_cpu = smp_processor_id();
  1286. #ifdef CONFIG_SMP
  1287. if (unlikely(task_running(rq, p)))
  1288. goto out_activate;
  1289. new_cpu = cpu;
  1290. schedstat_inc(rq, ttwu_count);
  1291. if (cpu == this_cpu) {
  1292. schedstat_inc(rq, ttwu_local);
  1293. goto out_set_cpu;
  1294. }
  1295. for_each_domain(this_cpu, sd) {
  1296. if (cpu_isset(cpu, sd->span)) {
  1297. schedstat_inc(sd, ttwu_wake_remote);
  1298. this_sd = sd;
  1299. break;
  1300. }
  1301. }
  1302. if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
  1303. goto out_set_cpu;
  1304. /*
  1305. * Check for affine wakeup and passive balancing possibilities.
  1306. */
  1307. if (this_sd) {
  1308. int idx = this_sd->wake_idx;
  1309. unsigned int imbalance;
  1310. imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;
  1311. load = source_load(cpu, idx);
  1312. this_load = target_load(this_cpu, idx);
  1313. new_cpu = this_cpu; /* Wake to this CPU if we can */
  1314. if (this_sd->flags & SD_WAKE_AFFINE) {
  1315. unsigned long tl = this_load;
  1316. unsigned long tl_per_task;
  1317. /*
  1318. * Attract cache-cold tasks on sync wakeups:
  1319. */
  1320. if (sync && !task_hot(p, rq->clock, this_sd))
  1321. goto out_set_cpu;
  1322. schedstat_inc(p, se.nr_wakeups_affine_attempts);
  1323. tl_per_task = cpu_avg_load_per_task(this_cpu);
  1324. /*
  1325. * If sync wakeup then subtract the (maximum possible)
  1326. * effect of the currently running task from the load
  1327. * of the current CPU:
  1328. */
  1329. if (sync)
  1330. tl -= current->se.load.weight;
  1331. if ((tl <= load &&
  1332. tl + target_load(cpu, idx) <= tl_per_task) ||
  1333. 100*(tl + p->se.load.weight) <= imbalance*load) {
  1334. /*
  1335. * This domain has SD_WAKE_AFFINE and
  1336. * p is cache cold in this domain, and
  1337. * there is no bad imbalance.
  1338. */
  1339. schedstat_inc(this_sd, ttwu_move_affine);
  1340. schedstat_inc(p, se.nr_wakeups_affine);
  1341. goto out_set_cpu;
  1342. }
  1343. }
  1344. /*
  1345. * Start passive balancing when half the imbalance_pct
  1346. * limit is reached.
  1347. */
  1348. if (this_sd->flags & SD_WAKE_BALANCE) {
  1349. if (imbalance*this_load <= 100*load) {
  1350. schedstat_inc(this_sd, ttwu_move_balance);
  1351. schedstat_inc(p, se.nr_wakeups_passive);
  1352. goto out_set_cpu;
  1353. }
  1354. }
  1355. }
  1356. new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */
  1357. out_set_cpu:
  1358. new_cpu = wake_idle(new_cpu, p);
  1359. if (new_cpu != cpu) {
  1360. set_task_cpu(p, new_cpu);
  1361. task_rq_unlock(rq, &flags);
  1362. /* might preempt at this point */
  1363. rq = task_rq_lock(p, &flags);
  1364. old_state = p->state;
  1365. if (!(old_state & state))
  1366. goto out;
  1367. if (p->se.on_rq)
  1368. goto out_running;
  1369. this_cpu = smp_processor_id();
  1370. cpu = task_cpu(p);
  1371. }
  1372. out_activate:
  1373. #endif /* CONFIG_SMP */
  1374. schedstat_inc(p, se.nr_wakeups);
  1375. if (sync)
  1376. schedstat_inc(p, se.nr_wakeups_sync);
  1377. if (orig_cpu != cpu)
  1378. schedstat_inc(p, se.nr_wakeups_migrate);
  1379. if (cpu == this_cpu)
  1380. schedstat_inc(p, se.nr_wakeups_local);
  1381. else
  1382. schedstat_inc(p, se.nr_wakeups_remote);
  1383. update_rq_clock(rq);
  1384. activate_task(rq, p, 1);
  1385. check_preempt_curr(rq, p);
  1386. success = 1;
  1387. out_running:
  1388. p->state = TASK_RUNNING;
  1389. out:
  1390. task_rq_unlock(rq, &flags);
  1391. return success;
  1392. }
  1393. int fastcall wake_up_process(struct task_struct *p)
  1394. {
  1395. return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED |
  1396. TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0);
  1397. }
  1398. EXPORT_SYMBOL(wake_up_process);
  1399. int fastcall wake_up_state(struct task_struct *p, unsigned int state)
  1400. {
  1401. return try_to_wake_up(p, state, 0);
  1402. }
  1403. /*
  1404. * Perform scheduler related setup for a newly forked process p.
  1405. * p is forked by current.
  1406. *
  1407. * __sched_fork() is basic setup used by init_idle() too:
  1408. */
  1409. static void __sched_fork(struct task_struct *p)
  1410. {
  1411. p->se.exec_start = 0;
  1412. p->se.sum_exec_runtime = 0;
  1413. p->se.prev_sum_exec_runtime = 0;
  1414. #ifdef CONFIG_SCHEDSTATS
  1415. p->se.wait_start = 0;
  1416. p->se.sum_sleep_runtime = 0;
  1417. p->se.sleep_start = 0;
  1418. p->se.block_start = 0;
  1419. p->se.sleep_max = 0;
  1420. p->se.block_max = 0;
  1421. p->se.exec_max = 0;
  1422. p->se.slice_max = 0;
  1423. p->se.wait_max = 0;
  1424. #endif
  1425. INIT_LIST_HEAD(&p->run_list);
  1426. p->se.on_rq = 0;
  1427. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1428. INIT_HLIST_HEAD(&p->preempt_notifiers);
  1429. #endif
  1430. /*
  1431. * We mark the process as running here, but have not actually
  1432. * inserted it onto the runqueue yet. This guarantees that
  1433. * nobody will actually run it, and a signal or other external
  1434. * event cannot wake it up and insert it on the runqueue either.
  1435. */
  1436. p->state = TASK_RUNNING;
  1437. }
  1438. /*
  1439. * fork()/clone()-time setup:
  1440. */
  1441. void sched_fork(struct task_struct *p, int clone_flags)
  1442. {
  1443. int cpu = get_cpu();
  1444. __sched_fork(p);
  1445. #ifdef CONFIG_SMP
  1446. cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
  1447. #endif
  1448. set_task_cpu(p, cpu);
  1449. /*
  1450. * Make sure we do not leak PI boosting priority to the child:
  1451. */
  1452. p->prio = current->normal_prio;
  1453. if (!rt_prio(p->prio))
  1454. p->sched_class = &fair_sched_class;
  1455. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  1456. if (likely(sched_info_on()))
  1457. memset(&p->sched_info, 0, sizeof(p->sched_info));
  1458. #endif
  1459. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  1460. p->oncpu = 0;
  1461. #endif
  1462. #ifdef CONFIG_PREEMPT
  1463. /* Want to start with kernel preemption disabled. */
  1464. task_thread_info(p)->preempt_count = 1;
  1465. #endif
  1466. put_cpu();
  1467. }
  1468. /*
  1469. * wake_up_new_task - wake up a newly created task for the first time.
  1470. *
  1471. * This function will do some initial scheduler statistics housekeeping
  1472. * that must be done for every newly created context, then puts the task
  1473. * on the runqueue and wakes it.
  1474. */
  1475. void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
  1476. {
  1477. unsigned long flags;
  1478. struct rq *rq;
  1479. rq = task_rq_lock(p, &flags);
  1480. BUG_ON(p->state != TASK_RUNNING);
  1481. update_rq_clock(rq);
  1482. p->prio = effective_prio(p);
  1483. if (!p->sched_class->task_new || !current->se.on_rq) {
  1484. activate_task(rq, p, 0);
  1485. } else {
  1486. /*
  1487. * Let the scheduling class do new task startup
  1488. * management (if any):
  1489. */
  1490. p->sched_class->task_new(rq, p);
  1491. inc_nr_running(p, rq);
  1492. }
  1493. check_preempt_curr(rq, p);
  1494. task_rq_unlock(rq, &flags);
  1495. }
  1496. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1497. /**
  1498. * preempt_notifier_register - tell me when current is being being preempted & rescheduled
  1499. * @notifier: notifier struct to register
  1500. */
  1501. void preempt_notifier_register(struct preempt_notifier *notifier)
  1502. {
  1503. hlist_add_head(&notifier->link, &current->preempt_notifiers);
  1504. }
  1505. EXPORT_SYMBOL_GPL(preempt_notifier_register);
  1506. /**
  1507. * preempt_notifier_unregister - no longer interested in preemption notifications
  1508. * @notifier: notifier struct to unregister
  1509. *
  1510. * This is safe to call from within a preemption notifier.
  1511. */
  1512. void preempt_notifier_unregister(struct preempt_notifier *notifier)
  1513. {
  1514. hlist_del(&notifier->link);
  1515. }
  1516. EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
  1517. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1518. {
  1519. struct preempt_notifier *notifier;
  1520. struct hlist_node *node;
  1521. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  1522. notifier->ops->sched_in(notifier, raw_smp_processor_id());
  1523. }
  1524. static void
  1525. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1526. struct task_struct *next)
  1527. {
  1528. struct preempt_notifier *notifier;
  1529. struct hlist_node *node;
  1530. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  1531. notifier->ops->sched_out(notifier, next);
  1532. }
  1533. #else
  1534. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1535. {
  1536. }
  1537. static void
  1538. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1539. struct task_struct *next)
  1540. {
  1541. }
  1542. #endif
  1543. /**
  1544. * prepare_task_switch - prepare to switch tasks
  1545. * @rq: the runqueue preparing to switch
  1546. * @prev: the current task that is being switched out
  1547. * @next: the task we are going to switch to.
  1548. *
  1549. * This is called with the rq lock held and interrupts off. It must
  1550. * be paired with a subsequent finish_task_switch after the context
  1551. * switch.
  1552. *
  1553. * prepare_task_switch sets up locking and calls architecture specific
  1554. * hooks.
  1555. */
  1556. static inline void
  1557. prepare_task_switch(struct rq *rq, struct task_struct *prev,
  1558. struct task_struct *next)
  1559. {
  1560. fire_sched_out_preempt_notifiers(prev, next);
  1561. prepare_lock_switch(rq, next);
  1562. prepare_arch_switch(next);
  1563. }
  1564. /**
  1565. * finish_task_switch - clean up after a task-switch
  1566. * @rq: runqueue associated with task-switch
  1567. * @prev: the thread we just switched away from.
  1568. *
  1569. * finish_task_switch must be called after the context switch, paired
  1570. * with a prepare_task_switch call before the context switch.
  1571. * finish_task_switch will reconcile locking set up by prepare_task_switch,
  1572. * and do any other architecture-specific cleanup actions.
  1573. *
  1574. * Note that we may have delayed dropping an mm in context_switch(). If
  1575. * so, we finish that here outside of the runqueue lock. (Doing it
  1576. * with the lock held can cause deadlocks; see schedule() for
  1577. * details.)
  1578. */
  1579. static void finish_task_switch(struct rq *rq, struct task_struct *prev)
  1580. __releases(rq->lock)
  1581. {
  1582. struct mm_struct *mm = rq->prev_mm;
  1583. long prev_state;
  1584. rq->prev_mm = NULL;
  1585. /*
  1586. * A task struct has one reference for the use as "current".
  1587. * If a task dies, then it sets TASK_DEAD in tsk->state and calls
  1588. * schedule one last time. The schedule call will never return, and
  1589. * the scheduled task must drop that reference.
  1590. * The test for TASK_DEAD must occur while the runqueue locks are
  1591. * still held, otherwise prev could be scheduled on another cpu, die
  1592. * there before we look at prev->state, and then the reference would
  1593. * be dropped twice.
  1594. * Manfred Spraul <manfred@colorfullife.com>
  1595. */
  1596. prev_state = prev->state;
  1597. finish_arch_switch(prev);
  1598. finish_lock_switch(rq, prev);
  1599. fire_sched_in_preempt_notifiers(current);
  1600. if (mm)
  1601. mmdrop(mm);
  1602. if (unlikely(prev_state == TASK_DEAD)) {
  1603. /*
  1604. * Remove function-return probe instances associated with this
  1605. * task and put them back on the free list.
  1606. */
  1607. kprobe_flush_task(prev);
  1608. put_task_struct(prev);
  1609. }
  1610. }
  1611. /**
  1612. * schedule_tail - first thing a freshly forked thread must call.
  1613. * @prev: the thread we just switched away from.
  1614. */
  1615. asmlinkage void schedule_tail(struct task_struct *prev)
  1616. __releases(rq->lock)
  1617. {
  1618. struct rq *rq = this_rq();
  1619. finish_task_switch(rq, prev);
  1620. #ifdef __ARCH_WANT_UNLOCKED_CTXSW
  1621. /* In this case, finish_task_switch does not reenable preemption */
  1622. preempt_enable();
  1623. #endif
  1624. if (current->set_child_tid)
  1625. put_user(task_pid_vnr(current), current->set_child_tid);
  1626. }
  1627. /*
  1628. * context_switch - switch to the new MM and the new
  1629. * thread's register state.
  1630. */
  1631. static inline void
  1632. context_switch(struct rq *rq, struct task_struct *prev,
  1633. struct task_struct *next)
  1634. {
  1635. struct mm_struct *mm, *oldmm;
  1636. prepare_task_switch(rq, prev, next);
  1637. mm = next->mm;
  1638. oldmm = prev->active_mm;
  1639. /*
  1640. * For paravirt, this is coupled with an exit in switch_to to
  1641. * combine the page table reload and the switch backend into
  1642. * one hypercall.
  1643. */
  1644. arch_enter_lazy_cpu_mode();
  1645. if (unlikely(!mm)) {
  1646. next->active_mm = oldmm;
  1647. atomic_inc(&oldmm->mm_count);
  1648. enter_lazy_tlb(oldmm, next);
  1649. } else
  1650. switch_mm(oldmm, mm, next);
  1651. if (unlikely(!prev->mm)) {
  1652. prev->active_mm = NULL;
  1653. rq->prev_mm = oldmm;
  1654. }
  1655. /*
  1656. * Since the runqueue lock will be released by the next
  1657. * task (which is an invalid locking op but in the case
  1658. * of the scheduler it's an obvious special-case), so we
  1659. * do an early lockdep release here:
  1660. */
  1661. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  1662. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  1663. #endif
  1664. /* Here we just switch the register state and the stack. */
  1665. switch_to(prev, next, prev);
  1666. barrier();
  1667. /*
  1668. * this_rq must be evaluated again because prev may have moved
  1669. * CPUs since it called schedule(), thus the 'rq' on its stack
  1670. * frame will be invalid.
  1671. */
  1672. finish_task_switch(this_rq(), prev);
  1673. }
  1674. /*
  1675. * nr_running, nr_uninterruptible and nr_context_switches:
  1676. *
  1677. * externally visible scheduler statistics: current number of runnable
  1678. * threads, current number of uninterruptible-sleeping threads, total
  1679. * number of context switches performed since bootup.
  1680. */
  1681. unsigned long nr_running(void)
  1682. {
  1683. unsigned long i, sum = 0;
  1684. for_each_online_cpu(i)
  1685. sum += cpu_rq(i)->nr_running;
  1686. return sum;
  1687. }
  1688. unsigned long nr_uninterruptible(void)
  1689. {
  1690. unsigned long i, sum = 0;
  1691. for_each_possible_cpu(i)
  1692. sum += cpu_rq(i)->nr_uninterruptible;
  1693. /*
  1694. * Since we read the counters lockless, it might be slightly
  1695. * inaccurate. Do not allow it to go below zero though:
  1696. */
  1697. if (unlikely((long)sum < 0))
  1698. sum = 0;
  1699. return sum;
  1700. }
  1701. unsigned long long nr_context_switches(void)
  1702. {
  1703. int i;
  1704. unsigned long long sum = 0;
  1705. for_each_possible_cpu(i)
  1706. sum += cpu_rq(i)->nr_switches;
  1707. return sum;
  1708. }
  1709. unsigned long nr_iowait(void)
  1710. {
  1711. unsigned long i, sum = 0;
  1712. for_each_possible_cpu(i)
  1713. sum += atomic_read(&cpu_rq(i)->nr_iowait);
  1714. return sum;
  1715. }
  1716. unsigned long nr_active(void)
  1717. {
  1718. unsigned long i, running = 0, uninterruptible = 0;
  1719. for_each_online_cpu(i) {
  1720. running += cpu_rq(i)->nr_running;
  1721. uninterruptible += cpu_rq(i)->nr_uninterruptible;
  1722. }
  1723. if (unlikely((long)uninterruptible < 0))
  1724. uninterruptible = 0;
  1725. return running + uninterruptible;
  1726. }
  1727. /*
  1728. * Update rq->cpu_load[] statistics. This function is usually called every
  1729. * scheduler tick (TICK_NSEC).
  1730. */
  1731. static void update_cpu_load(struct rq *this_rq)
  1732. {
  1733. unsigned long this_load = this_rq->load.weight;
  1734. int i, scale;
  1735. this_rq->nr_load_updates++;
  1736. /* Update our load: */
  1737. for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
  1738. unsigned long old_load, new_load;
  1739. /* scale is effectively 1 << i now, and >> i divides by scale */
  1740. old_load = this_rq->cpu_load[i];
  1741. new_load = this_load;
  1742. /*
  1743. * Round up the averaging division if load is increasing. This
  1744. * prevents us from getting stuck on 9 if the load is 10, for
  1745. * example.
  1746. */
  1747. if (new_load > old_load)
  1748. new_load += scale-1;
  1749. this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
  1750. }
  1751. }
  1752. #ifdef CONFIG_SMP
  1753. /*
  1754. * double_rq_lock - safely lock two runqueues
  1755. *
  1756. * Note this does not disable interrupts like task_rq_lock,
  1757. * you need to do so manually before calling.
  1758. */
  1759. static void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1760. __acquires(rq1->lock)
  1761. __acquires(rq2->lock)
  1762. {
  1763. BUG_ON(!irqs_disabled());
  1764. if (rq1 == rq2) {
  1765. spin_lock(&rq1->lock);
  1766. __acquire(rq2->lock); /* Fake it out ;) */
  1767. } else {
  1768. if (rq1 < rq2) {
  1769. spin_lock(&rq1->lock);
  1770. spin_lock(&rq2->lock);
  1771. } else {
  1772. spin_lock(&rq2->lock);
  1773. spin_lock(&rq1->lock);
  1774. }
  1775. }
  1776. update_rq_clock(rq1);
  1777. update_rq_clock(rq2);
  1778. }
  1779. /*
  1780. * double_rq_unlock - safely unlock two runqueues
  1781. *
  1782. * Note this does not restore interrupts like task_rq_unlock,
  1783. * you need to do so manually after calling.
  1784. */
  1785. static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1786. __releases(rq1->lock)
  1787. __releases(rq2->lock)
  1788. {
  1789. spin_unlock(&rq1->lock);
  1790. if (rq1 != rq2)
  1791. spin_unlock(&rq2->lock);
  1792. else
  1793. __release(rq2->lock);
  1794. }
  1795. /*
  1796. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1797. */
  1798. static void double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1799. __releases(this_rq->lock)
  1800. __acquires(busiest->lock)
  1801. __acquires(this_rq->lock)
  1802. {
  1803. if (unlikely(!irqs_disabled())) {
  1804. /* printk() doesn't work good under rq->lock */
  1805. spin_unlock(&this_rq->lock);
  1806. BUG_ON(1);
  1807. }
  1808. if (unlikely(!spin_trylock(&busiest->lock))) {
  1809. if (busiest < this_rq) {
  1810. spin_unlock(&this_rq->lock);
  1811. spin_lock(&busiest->lock);
  1812. spin_lock(&this_rq->lock);
  1813. } else
  1814. spin_lock(&busiest->lock);
  1815. }
  1816. }
  1817. /*
  1818. * If dest_cpu is allowed for this process, migrate the task to it.
  1819. * This is accomplished by forcing the cpu_allowed mask to only
  1820. * allow dest_cpu, which will force the cpu onto dest_cpu. Then
  1821. * the cpu_allowed mask is restored.
  1822. */
  1823. static void sched_migrate_task(struct task_struct *p, int dest_cpu)
  1824. {
  1825. struct migration_req req;
  1826. unsigned long flags;
  1827. struct rq *rq;
  1828. rq = task_rq_lock(p, &flags);
  1829. if (!cpu_isset(dest_cpu, p->cpus_allowed)
  1830. || unlikely(cpu_is_offline(dest_cpu)))
  1831. goto out;
  1832. /* force the process onto the specified CPU */
  1833. if (migrate_task(p, dest_cpu, &req)) {
  1834. /* Need to wait for migration thread (might exit: take ref). */
  1835. struct task_struct *mt = rq->migration_thread;
  1836. get_task_struct(mt);
  1837. task_rq_unlock(rq, &flags);
  1838. wake_up_process(mt);
  1839. put_task_struct(mt);
  1840. wait_for_completion(&req.done);
  1841. return;
  1842. }
  1843. out:
  1844. task_rq_unlock(rq, &flags);
  1845. }
  1846. /*
  1847. * sched_exec - execve() is a valuable balancing opportunity, because at
  1848. * this point the task has the smallest effective memory and cache footprint.
  1849. */
  1850. void sched_exec(void)
  1851. {
  1852. int new_cpu, this_cpu = get_cpu();
  1853. new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
  1854. put_cpu();
  1855. if (new_cpu != this_cpu)
  1856. sched_migrate_task(current, new_cpu);
  1857. }
  1858. /*
  1859. * pull_task - move a task from a remote runqueue to the local runqueue.
  1860. * Both runqueues must be locked.
  1861. */
  1862. static void pull_task(struct rq *src_rq, struct task_struct *p,
  1863. struct rq *this_rq, int this_cpu)
  1864. {
  1865. deactivate_task(src_rq, p, 0);
  1866. set_task_cpu(p, this_cpu);
  1867. activate_task(this_rq, p, 0);
  1868. /*
  1869. * Note that idle threads have a prio of MAX_PRIO, for this test
  1870. * to be always true for them.
  1871. */
  1872. check_preempt_curr(this_rq, p);
  1873. }
  1874. /*
  1875. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  1876. */
  1877. static
  1878. int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
  1879. struct sched_domain *sd, enum cpu_idle_type idle,
  1880. int *all_pinned)
  1881. {
  1882. /*
  1883. * We do not migrate tasks that are:
  1884. * 1) running (obviously), or
  1885. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  1886. * 3) are cache-hot on their current CPU.
  1887. */
  1888. if (!cpu_isset(this_cpu, p->cpus_allowed)) {
  1889. schedstat_inc(p, se.nr_failed_migrations_affine);
  1890. return 0;
  1891. }
  1892. *all_pinned = 0;
  1893. if (task_running(rq, p)) {
  1894. schedstat_inc(p, se.nr_failed_migrations_running);
  1895. return 0;
  1896. }
  1897. /*
  1898. * Aggressive migration if:
  1899. * 1) task is cache cold, or
  1900. * 2) too many balance attempts have failed.
  1901. */
  1902. if (!task_hot(p, rq->clock, sd) ||
  1903. sd->nr_balance_failed > sd->cache_nice_tries) {
  1904. #ifdef CONFIG_SCHEDSTATS
  1905. if (task_hot(p, rq->clock, sd)) {
  1906. schedstat_inc(sd, lb_hot_gained[idle]);
  1907. schedstat_inc(p, se.nr_forced_migrations);
  1908. }
  1909. #endif
  1910. return 1;
  1911. }
  1912. if (task_hot(p, rq->clock, sd)) {
  1913. schedstat_inc(p, se.nr_failed_migrations_hot);
  1914. return 0;
  1915. }
  1916. return 1;
  1917. }
  1918. static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1919. unsigned long max_nr_move, unsigned long max_load_move,
  1920. struct sched_domain *sd, enum cpu_idle_type idle,
  1921. int *all_pinned, unsigned long *load_moved,
  1922. int *this_best_prio, struct rq_iterator *iterator)
  1923. {
  1924. int pulled = 0, pinned = 0, skip_for_load;
  1925. struct task_struct *p;
  1926. long rem_load_move = max_load_move;
  1927. if (max_nr_move == 0 || max_load_move == 0)
  1928. goto out;
  1929. pinned = 1;
  1930. /*
  1931. * Start the load-balancing iterator:
  1932. */
  1933. p = iterator->start(iterator->arg);
  1934. next:
  1935. if (!p)
  1936. goto out;
  1937. /*
  1938. * To help distribute high priority tasks accross CPUs we don't
  1939. * skip a task if it will be the highest priority task (i.e. smallest
  1940. * prio value) on its new queue regardless of its load weight
  1941. */
  1942. skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
  1943. SCHED_LOAD_SCALE_FUZZ;
  1944. if ((skip_for_load && p->prio >= *this_best_prio) ||
  1945. !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
  1946. p = iterator->next(iterator->arg);
  1947. goto next;
  1948. }
  1949. pull_task(busiest, p, this_rq, this_cpu);
  1950. pulled++;
  1951. rem_load_move -= p->se.load.weight;
  1952. /*
  1953. * We only want to steal up to the prescribed number of tasks
  1954. * and the prescribed amount of weighted load.
  1955. */
  1956. if (pulled < max_nr_move && rem_load_move > 0) {
  1957. if (p->prio < *this_best_prio)
  1958. *this_best_prio = p->prio;
  1959. p = iterator->next(iterator->arg);
  1960. goto next;
  1961. }
  1962. out:
  1963. /*
  1964. * Right now, this is the only place pull_task() is called,
  1965. * so we can safely collect pull_task() stats here rather than
  1966. * inside pull_task().
  1967. */
  1968. schedstat_add(sd, lb_gained[idle], pulled);
  1969. if (all_pinned)
  1970. *all_pinned = pinned;
  1971. *load_moved = max_load_move - rem_load_move;
  1972. return pulled;
  1973. }
  1974. /*
  1975. * move_tasks tries to move up to max_load_move weighted load from busiest to
  1976. * this_rq, as part of a balancing operation within domain "sd".
  1977. * Returns 1 if successful and 0 otherwise.
  1978. *
  1979. * Called with both runqueues locked.
  1980. */
  1981. static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1982. unsigned long max_load_move,
  1983. struct sched_domain *sd, enum cpu_idle_type idle,
  1984. int *all_pinned)
  1985. {
  1986. const struct sched_class *class = sched_class_highest;
  1987. unsigned long total_load_moved = 0;
  1988. int this_best_prio = this_rq->curr->prio;
  1989. do {
  1990. total_load_moved +=
  1991. class->load_balance(this_rq, this_cpu, busiest,
  1992. ULONG_MAX, max_load_move - total_load_moved,
  1993. sd, idle, all_pinned, &this_best_prio);
  1994. class = class->next;
  1995. } while (class && max_load_move > total_load_moved);
  1996. return total_load_moved > 0;
  1997. }
  1998. /*
  1999. * move_one_task tries to move exactly one task from busiest to this_rq, as
  2000. * part of active balancing operations within "domain".
  2001. * Returns 1 if successful and 0 otherwise.
  2002. *
  2003. * Called with both runqueues locked.
  2004. */
  2005. static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2006. struct sched_domain *sd, enum cpu_idle_type idle)
  2007. {
  2008. const struct sched_class *class;
  2009. int this_best_prio = MAX_PRIO;
  2010. for (class = sched_class_highest; class; class = class->next)
  2011. if (class->load_balance(this_rq, this_cpu, busiest,
  2012. 1, ULONG_MAX, sd, idle, NULL,
  2013. &this_best_prio))
  2014. return 1;
  2015. return 0;
  2016. }
  2017. /*
  2018. * find_busiest_group finds and returns the busiest CPU group within the
  2019. * domain. It calculates and returns the amount of weighted load which
  2020. * should be moved to restore balance via the imbalance parameter.
  2021. */
  2022. static struct sched_group *
  2023. find_busiest_group(struct sched_domain *sd, int this_cpu,
  2024. unsigned long *imbalance, enum cpu_idle_type idle,
  2025. int *sd_idle, cpumask_t *cpus, int *balance)
  2026. {
  2027. struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
  2028. unsigned long max_load, avg_load, total_load, this_load, total_pwr;
  2029. unsigned long max_pull;
  2030. unsigned long busiest_load_per_task, busiest_nr_running;
  2031. unsigned long this_load_per_task, this_nr_running;
  2032. int load_idx, group_imb = 0;
  2033. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2034. int power_savings_balance = 1;
  2035. unsigned long leader_nr_running = 0, min_load_per_task = 0;
  2036. unsigned long min_nr_running = ULONG_MAX;
  2037. struct sched_group *group_min = NULL, *group_leader = NULL;
  2038. #endif
  2039. max_load = this_load = total_load = total_pwr = 0;
  2040. busiest_load_per_task = busiest_nr_running = 0;
  2041. this_load_per_task = this_nr_running = 0;
  2042. if (idle == CPU_NOT_IDLE)
  2043. load_idx = sd->busy_idx;
  2044. else if (idle == CPU_NEWLY_IDLE)
  2045. load_idx = sd->newidle_idx;
  2046. else
  2047. load_idx = sd->idle_idx;
  2048. do {
  2049. unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
  2050. int local_group;
  2051. int i;
  2052. int __group_imb = 0;
  2053. unsigned int balance_cpu = -1, first_idle_cpu = 0;
  2054. unsigned long sum_nr_running, sum_weighted_load;
  2055. local_group = cpu_isset(this_cpu, group->cpumask);
  2056. if (local_group)
  2057. balance_cpu = first_cpu(group->cpumask);
  2058. /* Tally up the load of all CPUs in the group */
  2059. sum_weighted_load = sum_nr_running = avg_load = 0;
  2060. max_cpu_load = 0;
  2061. min_cpu_load = ~0UL;
  2062. for_each_cpu_mask(i, group->cpumask) {
  2063. struct rq *rq;
  2064. if (!cpu_isset(i, *cpus))
  2065. continue;
  2066. rq = cpu_rq(i);
  2067. if (*sd_idle && rq->nr_running)
  2068. *sd_idle = 0;
  2069. /* Bias balancing toward cpus of our domain */
  2070. if (local_group) {
  2071. if (idle_cpu(i) && !first_idle_cpu) {
  2072. first_idle_cpu = 1;
  2073. balance_cpu = i;
  2074. }
  2075. load = target_load(i, load_idx);
  2076. } else {
  2077. load = source_load(i, load_idx);
  2078. if (load > max_cpu_load)
  2079. max_cpu_load = load;
  2080. if (min_cpu_load > load)
  2081. min_cpu_load = load;
  2082. }
  2083. avg_load += load;
  2084. sum_nr_running += rq->nr_running;
  2085. sum_weighted_load += weighted_cpuload(i);
  2086. }
  2087. /*
  2088. * First idle cpu or the first cpu(busiest) in this sched group
  2089. * is eligible for doing load balancing at this and above
  2090. * domains. In the newly idle case, we will allow all the cpu's
  2091. * to do the newly idle load balance.
  2092. */
  2093. if (idle != CPU_NEWLY_IDLE && local_group &&
  2094. balance_cpu != this_cpu && balance) {
  2095. *balance = 0;
  2096. goto ret;
  2097. }
  2098. total_load += avg_load;
  2099. total_pwr += group->__cpu_power;
  2100. /* Adjust by relative CPU power of the group */
  2101. avg_load = sg_div_cpu_power(group,
  2102. avg_load * SCHED_LOAD_SCALE);
  2103. if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
  2104. __group_imb = 1;
  2105. group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
  2106. if (local_group) {
  2107. this_load = avg_load;
  2108. this = group;
  2109. this_nr_running = sum_nr_running;
  2110. this_load_per_task = sum_weighted_load;
  2111. } else if (avg_load > max_load &&
  2112. (sum_nr_running > group_capacity || __group_imb)) {
  2113. max_load = avg_load;
  2114. busiest = group;
  2115. busiest_nr_running = sum_nr_running;
  2116. busiest_load_per_task = sum_weighted_load;
  2117. group_imb = __group_imb;
  2118. }
  2119. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2120. /*
  2121. * Busy processors will not participate in power savings
  2122. * balance.
  2123. */
  2124. if (idle == CPU_NOT_IDLE ||
  2125. !(sd->flags & SD_POWERSAVINGS_BALANCE))
  2126. goto group_next;
  2127. /*
  2128. * If the local group is idle or completely loaded
  2129. * no need to do power savings balance at this domain
  2130. */
  2131. if (local_group && (this_nr_running >= group_capacity ||
  2132. !this_nr_running))
  2133. power_savings_balance = 0;
  2134. /*
  2135. * If a group is already running at full capacity or idle,
  2136. * don't include that group in power savings calculations
  2137. */
  2138. if (!power_savings_balance || sum_nr_running >= group_capacity
  2139. || !sum_nr_running)
  2140. goto group_next;
  2141. /*
  2142. * Calculate the group which has the least non-idle load.
  2143. * This is the group from where we need to pick up the load
  2144. * for saving power
  2145. */
  2146. if ((sum_nr_running < min_nr_running) ||
  2147. (sum_nr_running == min_nr_running &&
  2148. first_cpu(group->cpumask) <
  2149. first_cpu(group_min->cpumask))) {
  2150. group_min = group;
  2151. min_nr_running = sum_nr_running;
  2152. min_load_per_task = sum_weighted_load /
  2153. sum_nr_running;
  2154. }
  2155. /*
  2156. * Calculate the group which is almost near its
  2157. * capacity but still has some space to pick up some load
  2158. * from other group and save more power
  2159. */
  2160. if (sum_nr_running <= group_capacity - 1) {
  2161. if (sum_nr_running > leader_nr_running ||
  2162. (sum_nr_running == leader_nr_running &&
  2163. first_cpu(group->cpumask) >
  2164. first_cpu(group_leader->cpumask))) {
  2165. group_leader = group;
  2166. leader_nr_running = sum_nr_running;
  2167. }
  2168. }
  2169. group_next:
  2170. #endif
  2171. group = group->next;
  2172. } while (group != sd->groups);
  2173. if (!busiest || this_load >= max_load || busiest_nr_running == 0)
  2174. goto out_balanced;
  2175. avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
  2176. if (this_load >= avg_load ||
  2177. 100*max_load <= sd->imbalance_pct*this_load)
  2178. goto out_balanced;
  2179. busiest_load_per_task /= busiest_nr_running;
  2180. if (group_imb)
  2181. busiest_load_per_task = min(busiest_load_per_task, avg_load);
  2182. /*
  2183. * We're trying to get all the cpus to the average_load, so we don't
  2184. * want to push ourselves above the average load, nor do we wish to
  2185. * reduce the max loaded cpu below the average load, as either of these
  2186. * actions would just result in more rebalancing later, and ping-pong
  2187. * tasks around. Thus we look for the minimum possible imbalance.
  2188. * Negative imbalances (*we* are more loaded than anyone else) will
  2189. * be counted as no imbalance for these purposes -- we can't fix that
  2190. * by pulling tasks to us. Be careful of negative numbers as they'll
  2191. * appear as very large values with unsigned longs.
  2192. */
  2193. if (max_load <= busiest_load_per_task)
  2194. goto out_balanced;
  2195. /*
  2196. * In the presence of smp nice balancing, certain scenarios can have
  2197. * max load less than avg load(as we skip the groups at or below
  2198. * its cpu_power, while calculating max_load..)
  2199. */
  2200. if (max_load < avg_load) {
  2201. *imbalance = 0;
  2202. goto small_imbalance;
  2203. }
  2204. /* Don't want to pull so many tasks that a group would go idle */
  2205. max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
  2206. /* How much load to actually move to equalise the imbalance */
  2207. *imbalance = min(max_pull * busiest->__cpu_power,
  2208. (avg_load - this_load) * this->__cpu_power)
  2209. / SCHED_LOAD_SCALE;
  2210. /*
  2211. * if *imbalance is less than the average load per runnable task
  2212. * there is no gaurantee that any tasks will be moved so we'll have
  2213. * a think about bumping its value to force at least one task to be
  2214. * moved
  2215. */
  2216. if (*imbalance < busiest_load_per_task) {
  2217. unsigned long tmp, pwr_now, pwr_move;
  2218. unsigned int imbn;
  2219. small_imbalance:
  2220. pwr_move = pwr_now = 0;
  2221. imbn = 2;
  2222. if (this_nr_running) {
  2223. this_load_per_task /= this_nr_running;
  2224. if (busiest_load_per_task > this_load_per_task)
  2225. imbn = 1;
  2226. } else
  2227. this_load_per_task = SCHED_LOAD_SCALE;
  2228. if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
  2229. busiest_load_per_task * imbn) {
  2230. *imbalance = busiest_load_per_task;
  2231. return busiest;
  2232. }
  2233. /*
  2234. * OK, we don't have enough imbalance to justify moving tasks,
  2235. * however we may be able to increase total CPU power used by
  2236. * moving them.
  2237. */
  2238. pwr_now += busiest->__cpu_power *
  2239. min(busiest_load_per_task, max_load);
  2240. pwr_now += this->__cpu_power *
  2241. min(this_load_per_task, this_load);
  2242. pwr_now /= SCHED_LOAD_SCALE;
  2243. /* Amount of load we'd subtract */
  2244. tmp = sg_div_cpu_power(busiest,
  2245. busiest_load_per_task * SCHED_LOAD_SCALE);
  2246. if (max_load > tmp)
  2247. pwr_move += busiest->__cpu_power *
  2248. min(busiest_load_per_task, max_load - tmp);
  2249. /* Amount of load we'd add */
  2250. if (max_load * busiest->__cpu_power <
  2251. busiest_load_per_task * SCHED_LOAD_SCALE)
  2252. tmp = sg_div_cpu_power(this,
  2253. max_load * busiest->__cpu_power);
  2254. else
  2255. tmp = sg_div_cpu_power(this,
  2256. busiest_load_per_task * SCHED_LOAD_SCALE);
  2257. pwr_move += this->__cpu_power *
  2258. min(this_load_per_task, this_load + tmp);
  2259. pwr_move /= SCHED_LOAD_SCALE;
  2260. /* Move if we gain throughput */
  2261. if (pwr_move > pwr_now)
  2262. *imbalance = busiest_load_per_task;
  2263. }
  2264. return busiest;
  2265. out_balanced:
  2266. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2267. if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
  2268. goto ret;
  2269. if (this == group_leader && group_leader != group_min) {
  2270. *imbalance = min_load_per_task;
  2271. return group_min;
  2272. }
  2273. #endif
  2274. ret:
  2275. *imbalance = 0;
  2276. return NULL;
  2277. }
  2278. /*
  2279. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  2280. */
  2281. static struct rq *
  2282. find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
  2283. unsigned long imbalance, cpumask_t *cpus)
  2284. {
  2285. struct rq *busiest = NULL, *rq;
  2286. unsigned long max_load = 0;
  2287. int i;
  2288. for_each_cpu_mask(i, group->cpumask) {
  2289. unsigned long wl;
  2290. if (!cpu_isset(i, *cpus))
  2291. continue;
  2292. rq = cpu_rq(i);
  2293. wl = weighted_cpuload(i);
  2294. if (rq->nr_running == 1 && wl > imbalance)
  2295. continue;
  2296. if (wl > max_load) {
  2297. max_load = wl;
  2298. busiest = rq;
  2299. }
  2300. }
  2301. return busiest;
  2302. }
  2303. /*
  2304. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  2305. * so long as it is large enough.
  2306. */
  2307. #define MAX_PINNED_INTERVAL 512
  2308. /*
  2309. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  2310. * tasks if there is an imbalance.
  2311. */
  2312. static int load_balance(int this_cpu, struct rq *this_rq,
  2313. struct sched_domain *sd, enum cpu_idle_type idle,
  2314. int *balance)
  2315. {
  2316. int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
  2317. struct sched_group *group;
  2318. unsigned long imbalance;
  2319. struct rq *busiest;
  2320. cpumask_t cpus = CPU_MASK_ALL;
  2321. unsigned long flags;
  2322. /*
  2323. * When power savings policy is enabled for the parent domain, idle
  2324. * sibling can pick up load irrespective of busy siblings. In this case,
  2325. * let the state of idle sibling percolate up as CPU_IDLE, instead of
  2326. * portraying it as CPU_NOT_IDLE.
  2327. */
  2328. if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
  2329. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2330. sd_idle = 1;
  2331. schedstat_inc(sd, lb_count[idle]);
  2332. redo:
  2333. group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
  2334. &cpus, balance);
  2335. if (*balance == 0)
  2336. goto out_balanced;
  2337. if (!group) {
  2338. schedstat_inc(sd, lb_nobusyg[idle]);
  2339. goto out_balanced;
  2340. }
  2341. busiest = find_busiest_queue(group, idle, imbalance, &cpus);
  2342. if (!busiest) {
  2343. schedstat_inc(sd, lb_nobusyq[idle]);
  2344. goto out_balanced;
  2345. }
  2346. BUG_ON(busiest == this_rq);
  2347. schedstat_add(sd, lb_imbalance[idle], imbalance);
  2348. ld_moved = 0;
  2349. if (busiest->nr_running > 1) {
  2350. /*
  2351. * Attempt to move tasks. If find_busiest_group has found
  2352. * an imbalance but busiest->nr_running <= 1, the group is
  2353. * still unbalanced. ld_moved simply stays zero, so it is
  2354. * correctly treated as an imbalance.
  2355. */
  2356. local_irq_save(flags);
  2357. double_rq_lock(this_rq, busiest);
  2358. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  2359. imbalance, sd, idle, &all_pinned);
  2360. double_rq_unlock(this_rq, busiest);
  2361. local_irq_restore(flags);
  2362. /*
  2363. * some other cpu did the load balance for us.
  2364. */
  2365. if (ld_moved && this_cpu != smp_processor_id())
  2366. resched_cpu(this_cpu);
  2367. /* All tasks on this runqueue were pinned by CPU affinity */
  2368. if (unlikely(all_pinned)) {
  2369. cpu_clear(cpu_of(busiest), cpus);
  2370. if (!cpus_empty(cpus))
  2371. goto redo;
  2372. goto out_balanced;
  2373. }
  2374. }
  2375. if (!ld_moved) {
  2376. schedstat_inc(sd, lb_failed[idle]);
  2377. sd->nr_balance_failed++;
  2378. if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
  2379. spin_lock_irqsave(&busiest->lock, flags);
  2380. /* don't kick the migration_thread, if the curr
  2381. * task on busiest cpu can't be moved to this_cpu
  2382. */
  2383. if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
  2384. spin_unlock_irqrestore(&busiest->lock, flags);
  2385. all_pinned = 1;
  2386. goto out_one_pinned;
  2387. }
  2388. if (!busiest->active_balance) {
  2389. busiest->active_balance = 1;
  2390. busiest->push_cpu = this_cpu;
  2391. active_balance = 1;
  2392. }
  2393. spin_unlock_irqrestore(&busiest->lock, flags);
  2394. if (active_balance)
  2395. wake_up_process(busiest->migration_thread);
  2396. /*
  2397. * We've kicked active balancing, reset the failure
  2398. * counter.
  2399. */
  2400. sd->nr_balance_failed = sd->cache_nice_tries+1;
  2401. }
  2402. } else
  2403. sd->nr_balance_failed = 0;
  2404. if (likely(!active_balance)) {
  2405. /* We were unbalanced, so reset the balancing interval */
  2406. sd->balance_interval = sd->min_interval;
  2407. } else {
  2408. /*
  2409. * If we've begun active balancing, start to back off. This
  2410. * case may not be covered by the all_pinned logic if there
  2411. * is only 1 task on the busy runqueue (because we don't call
  2412. * move_tasks).
  2413. */
  2414. if (sd->balance_interval < sd->max_interval)
  2415. sd->balance_interval *= 2;
  2416. }
  2417. if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2418. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2419. return -1;
  2420. return ld_moved;
  2421. out_balanced:
  2422. schedstat_inc(sd, lb_balanced[idle]);
  2423. sd->nr_balance_failed = 0;
  2424. out_one_pinned:
  2425. /* tune up the balancing interval */
  2426. if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
  2427. (sd->balance_interval < sd->max_interval))
  2428. sd->balance_interval *= 2;
  2429. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2430. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2431. return -1;
  2432. return 0;
  2433. }
  2434. /*
  2435. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  2436. * tasks if there is an imbalance.
  2437. *
  2438. * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
  2439. * this_rq is locked.
  2440. */
  2441. static int
  2442. load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
  2443. {
  2444. struct sched_group *group;
  2445. struct rq *busiest = NULL;
  2446. unsigned long imbalance;
  2447. int ld_moved = 0;
  2448. int sd_idle = 0;
  2449. int all_pinned = 0;
  2450. cpumask_t cpus = CPU_MASK_ALL;
  2451. /*
  2452. * When power savings policy is enabled for the parent domain, idle
  2453. * sibling can pick up load irrespective of busy siblings. In this case,
  2454. * let the state of idle sibling percolate up as IDLE, instead of
  2455. * portraying it as CPU_NOT_IDLE.
  2456. */
  2457. if (sd->flags & SD_SHARE_CPUPOWER &&
  2458. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2459. sd_idle = 1;
  2460. schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
  2461. redo:
  2462. group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
  2463. &sd_idle, &cpus, NULL);
  2464. if (!group) {
  2465. schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
  2466. goto out_balanced;
  2467. }
  2468. busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
  2469. &cpus);
  2470. if (!busiest) {
  2471. schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
  2472. goto out_balanced;
  2473. }
  2474. BUG_ON(busiest == this_rq);
  2475. schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
  2476. ld_moved = 0;
  2477. if (busiest->nr_running > 1) {
  2478. /* Attempt to move tasks */
  2479. double_lock_balance(this_rq, busiest);
  2480. /* this_rq->clock is already updated */
  2481. update_rq_clock(busiest);
  2482. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  2483. imbalance, sd, CPU_NEWLY_IDLE,
  2484. &all_pinned);
  2485. spin_unlock(&busiest->lock);
  2486. if (unlikely(all_pinned)) {
  2487. cpu_clear(cpu_of(busiest), cpus);
  2488. if (!cpus_empty(cpus))
  2489. goto redo;
  2490. }
  2491. }
  2492. if (!ld_moved) {
  2493. schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
  2494. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2495. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2496. return -1;
  2497. } else
  2498. sd->nr_balance_failed = 0;
  2499. return ld_moved;
  2500. out_balanced:
  2501. schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
  2502. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2503. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2504. return -1;
  2505. sd->nr_balance_failed = 0;
  2506. return 0;
  2507. }
  2508. /*
  2509. * idle_balance is called by schedule() if this_cpu is about to become
  2510. * idle. Attempts to pull tasks from other CPUs.
  2511. */
  2512. static void idle_balance(int this_cpu, struct rq *this_rq)
  2513. {
  2514. struct sched_domain *sd;
  2515. int pulled_task = -1;
  2516. unsigned long next_balance = jiffies + HZ;
  2517. for_each_domain(this_cpu, sd) {
  2518. unsigned long interval;
  2519. if (!(sd->flags & SD_LOAD_BALANCE))
  2520. continue;
  2521. if (sd->flags & SD_BALANCE_NEWIDLE)
  2522. /* If we've pulled tasks over stop searching: */
  2523. pulled_task = load_balance_newidle(this_cpu,
  2524. this_rq, sd);
  2525. interval = msecs_to_jiffies(sd->balance_interval);
  2526. if (time_after(next_balance, sd->last_balance + interval))
  2527. next_balance = sd->last_balance + interval;
  2528. if (pulled_task)
  2529. break;
  2530. }
  2531. if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
  2532. /*
  2533. * We are going idle. next_balance may be set based on
  2534. * a busy processor. So reset next_balance.
  2535. */
  2536. this_rq->next_balance = next_balance;
  2537. }
  2538. }
  2539. /*
  2540. * active_load_balance is run by migration threads. It pushes running tasks
  2541. * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
  2542. * running on each physical CPU where possible, and avoids physical /
  2543. * logical imbalances.
  2544. *
  2545. * Called with busiest_rq locked.
  2546. */
  2547. static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
  2548. {
  2549. int target_cpu = busiest_rq->push_cpu;
  2550. struct sched_domain *sd;
  2551. struct rq *target_rq;
  2552. /* Is there any task to move? */
  2553. if (busiest_rq->nr_running <= 1)
  2554. return;
  2555. target_rq = cpu_rq(target_cpu);
  2556. /*
  2557. * This condition is "impossible", if it occurs
  2558. * we need to fix it. Originally reported by
  2559. * Bjorn Helgaas on a 128-cpu setup.
  2560. */
  2561. BUG_ON(busiest_rq == target_rq);
  2562. /* move a task from busiest_rq to target_rq */
  2563. double_lock_balance(busiest_rq, target_rq);
  2564. update_rq_clock(busiest_rq);
  2565. update_rq_clock(target_rq);
  2566. /* Search for an sd spanning us and the target CPU. */
  2567. for_each_domain(target_cpu, sd) {
  2568. if ((sd->flags & SD_LOAD_BALANCE) &&
  2569. cpu_isset(busiest_cpu, sd->span))
  2570. break;
  2571. }
  2572. if (likely(sd)) {
  2573. schedstat_inc(sd, alb_count);
  2574. if (move_one_task(target_rq, target_cpu, busiest_rq,
  2575. sd, CPU_IDLE))
  2576. schedstat_inc(sd, alb_pushed);
  2577. else
  2578. schedstat_inc(sd, alb_failed);
  2579. }
  2580. spin_unlock(&target_rq->lock);
  2581. }
  2582. #ifdef CONFIG_NO_HZ
  2583. static struct {
  2584. atomic_t load_balancer;
  2585. cpumask_t cpu_mask;
  2586. } nohz ____cacheline_aligned = {
  2587. .load_balancer = ATOMIC_INIT(-1),
  2588. .cpu_mask = CPU_MASK_NONE,
  2589. };
  2590. /*
  2591. * This routine will try to nominate the ilb (idle load balancing)
  2592. * owner among the cpus whose ticks are stopped. ilb owner will do the idle
  2593. * load balancing on behalf of all those cpus. If all the cpus in the system
  2594. * go into this tickless mode, then there will be no ilb owner (as there is
  2595. * no need for one) and all the cpus will sleep till the next wakeup event
  2596. * arrives...
  2597. *
  2598. * For the ilb owner, tick is not stopped. And this tick will be used
  2599. * for idle load balancing. ilb owner will still be part of
  2600. * nohz.cpu_mask..
  2601. *
  2602. * While stopping the tick, this cpu will become the ilb owner if there
  2603. * is no other owner. And will be the owner till that cpu becomes busy
  2604. * or if all cpus in the system stop their ticks at which point
  2605. * there is no need for ilb owner.
  2606. *
  2607. * When the ilb owner becomes busy, it nominates another owner, during the
  2608. * next busy scheduler_tick()
  2609. */
  2610. int select_nohz_load_balancer(int stop_tick)
  2611. {
  2612. int cpu = smp_processor_id();
  2613. if (stop_tick) {
  2614. cpu_set(cpu, nohz.cpu_mask);
  2615. cpu_rq(cpu)->in_nohz_recently = 1;
  2616. /*
  2617. * If we are going offline and still the leader, give up!
  2618. */
  2619. if (cpu_is_offline(cpu) &&
  2620. atomic_read(&nohz.load_balancer) == cpu) {
  2621. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  2622. BUG();
  2623. return 0;
  2624. }
  2625. /* time for ilb owner also to sleep */
  2626. if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
  2627. if (atomic_read(&nohz.load_balancer) == cpu)
  2628. atomic_set(&nohz.load_balancer, -1);
  2629. return 0;
  2630. }
  2631. if (atomic_read(&nohz.load_balancer) == -1) {
  2632. /* make me the ilb owner */
  2633. if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
  2634. return 1;
  2635. } else if (atomic_read(&nohz.load_balancer) == cpu)
  2636. return 1;
  2637. } else {
  2638. if (!cpu_isset(cpu, nohz.cpu_mask))
  2639. return 0;
  2640. cpu_clear(cpu, nohz.cpu_mask);
  2641. if (atomic_read(&nohz.load_balancer) == cpu)
  2642. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  2643. BUG();
  2644. }
  2645. return 0;
  2646. }
  2647. #endif
  2648. static DEFINE_SPINLOCK(balancing);
  2649. /*
  2650. * It checks each scheduling domain to see if it is due to be balanced,
  2651. * and initiates a balancing operation if so.
  2652. *
  2653. * Balancing parameters are set up in arch_init_sched_domains.
  2654. */
  2655. static void rebalance_domains(int cpu, enum cpu_idle_type idle)
  2656. {
  2657. int balance = 1;
  2658. struct rq *rq = cpu_rq(cpu);
  2659. unsigned long interval;
  2660. struct sched_domain *sd;
  2661. /* Earliest time when we have to do rebalance again */
  2662. unsigned long next_balance = jiffies + 60*HZ;
  2663. int update_next_balance = 0;
  2664. for_each_domain(cpu, sd) {
  2665. if (!(sd->flags & SD_LOAD_BALANCE))
  2666. continue;
  2667. interval = sd->balance_interval;
  2668. if (idle != CPU_IDLE)
  2669. interval *= sd->busy_factor;
  2670. /* scale ms to jiffies */
  2671. interval = msecs_to_jiffies(interval);
  2672. if (unlikely(!interval))
  2673. interval = 1;
  2674. if (interval > HZ*NR_CPUS/10)
  2675. interval = HZ*NR_CPUS/10;
  2676. if (sd->flags & SD_SERIALIZE) {
  2677. if (!spin_trylock(&balancing))
  2678. goto out;
  2679. }
  2680. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  2681. if (load_balance(cpu, rq, sd, idle, &balance)) {
  2682. /*
  2683. * We've pulled tasks over so either we're no
  2684. * longer idle, or one of our SMT siblings is
  2685. * not idle.
  2686. */
  2687. idle = CPU_NOT_IDLE;
  2688. }
  2689. sd->last_balance = jiffies;
  2690. }
  2691. if (sd->flags & SD_SERIALIZE)
  2692. spin_unlock(&balancing);
  2693. out:
  2694. if (time_after(next_balance, sd->last_balance + interval)) {
  2695. next_balance = sd->last_balance + interval;
  2696. update_next_balance = 1;
  2697. }
  2698. /*
  2699. * Stop the load balance at this level. There is another
  2700. * CPU in our sched group which is doing load balancing more
  2701. * actively.
  2702. */
  2703. if (!balance)
  2704. break;
  2705. }
  2706. /*
  2707. * next_balance will be updated only when there is a need.
  2708. * When the cpu is attached to null domain for ex, it will not be
  2709. * updated.
  2710. */
  2711. if (likely(update_next_balance))
  2712. rq->next_balance = next_balance;
  2713. }
  2714. /*
  2715. * run_rebalance_domains is triggered when needed from the scheduler tick.
  2716. * In CONFIG_NO_HZ case, the idle load balance owner will do the
  2717. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  2718. */
  2719. static void run_rebalance_domains(struct softirq_action *h)
  2720. {
  2721. int this_cpu = smp_processor_id();
  2722. struct rq *this_rq = cpu_rq(this_cpu);
  2723. enum cpu_idle_type idle = this_rq->idle_at_tick ?
  2724. CPU_IDLE : CPU_NOT_IDLE;
  2725. rebalance_domains(this_cpu, idle);
  2726. #ifdef CONFIG_NO_HZ
  2727. /*
  2728. * If this cpu is the owner for idle load balancing, then do the
  2729. * balancing on behalf of the other idle cpus whose ticks are
  2730. * stopped.
  2731. */
  2732. if (this_rq->idle_at_tick &&
  2733. atomic_read(&nohz.load_balancer) == this_cpu) {
  2734. cpumask_t cpus = nohz.cpu_mask;
  2735. struct rq *rq;
  2736. int balance_cpu;
  2737. cpu_clear(this_cpu, cpus);
  2738. for_each_cpu_mask(balance_cpu, cpus) {
  2739. /*
  2740. * If this cpu gets work to do, stop the load balancing
  2741. * work being done for other cpus. Next load
  2742. * balancing owner will pick it up.
  2743. */
  2744. if (need_resched())
  2745. break;
  2746. rebalance_domains(balance_cpu, CPU_IDLE);
  2747. rq = cpu_rq(balance_cpu);
  2748. if (time_after(this_rq->next_balance, rq->next_balance))
  2749. this_rq->next_balance = rq->next_balance;
  2750. }
  2751. }
  2752. #endif
  2753. }
  2754. /*
  2755. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  2756. *
  2757. * In case of CONFIG_NO_HZ, this is the place where we nominate a new
  2758. * idle load balancing owner or decide to stop the periodic load balancing,
  2759. * if the whole system is idle.
  2760. */
  2761. static inline void trigger_load_balance(struct rq *rq, int cpu)
  2762. {
  2763. #ifdef CONFIG_NO_HZ
  2764. /*
  2765. * If we were in the nohz mode recently and busy at the current
  2766. * scheduler tick, then check if we need to nominate new idle
  2767. * load balancer.
  2768. */
  2769. if (rq->in_nohz_recently && !rq->idle_at_tick) {
  2770. rq->in_nohz_recently = 0;
  2771. if (atomic_read(&nohz.load_balancer) == cpu) {
  2772. cpu_clear(cpu, nohz.cpu_mask);
  2773. atomic_set(&nohz.load_balancer, -1);
  2774. }
  2775. if (atomic_read(&nohz.load_balancer) == -1) {
  2776. /*
  2777. * simple selection for now: Nominate the
  2778. * first cpu in the nohz list to be the next
  2779. * ilb owner.
  2780. *
  2781. * TBD: Traverse the sched domains and nominate
  2782. * the nearest cpu in the nohz.cpu_mask.
  2783. */
  2784. int ilb = first_cpu(nohz.cpu_mask);
  2785. if (ilb != NR_CPUS)
  2786. resched_cpu(ilb);
  2787. }
  2788. }
  2789. /*
  2790. * If this cpu is idle and doing idle load balancing for all the
  2791. * cpus with ticks stopped, is it time for that to stop?
  2792. */
  2793. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
  2794. cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
  2795. resched_cpu(cpu);
  2796. return;
  2797. }
  2798. /*
  2799. * If this cpu is idle and the idle load balancing is done by
  2800. * someone else, then no need raise the SCHED_SOFTIRQ
  2801. */
  2802. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
  2803. cpu_isset(cpu, nohz.cpu_mask))
  2804. return;
  2805. #endif
  2806. if (time_after_eq(jiffies, rq->next_balance))
  2807. raise_softirq(SCHED_SOFTIRQ);
  2808. }
  2809. #else /* CONFIG_SMP */
  2810. /*
  2811. * on UP we do not need to balance between CPUs:
  2812. */
  2813. static inline void idle_balance(int cpu, struct rq *rq)
  2814. {
  2815. }
  2816. /* Avoid "used but not defined" warning on UP */
  2817. static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2818. unsigned long max_nr_move, unsigned long max_load_move,
  2819. struct sched_domain *sd, enum cpu_idle_type idle,
  2820. int *all_pinned, unsigned long *load_moved,
  2821. int *this_best_prio, struct rq_iterator *iterator)
  2822. {
  2823. *load_moved = 0;
  2824. return 0;
  2825. }
  2826. #endif
  2827. DEFINE_PER_CPU(struct kernel_stat, kstat);
  2828. EXPORT_PER_CPU_SYMBOL(kstat);
  2829. /*
  2830. * Return p->sum_exec_runtime plus any more ns on the sched_clock
  2831. * that have not yet been banked in case the task is currently running.
  2832. */
  2833. unsigned long long task_sched_runtime(struct task_struct *p)
  2834. {
  2835. unsigned long flags;
  2836. u64 ns, delta_exec;
  2837. struct rq *rq;
  2838. rq = task_rq_lock(p, &flags);
  2839. ns = p->se.sum_exec_runtime;
  2840. if (rq->curr == p) {
  2841. update_rq_clock(rq);
  2842. delta_exec = rq->clock - p->se.exec_start;
  2843. if ((s64)delta_exec > 0)
  2844. ns += delta_exec;
  2845. }
  2846. task_rq_unlock(rq, &flags);
  2847. return ns;
  2848. }
  2849. /*
  2850. * Account user cpu time to a process.
  2851. * @p: the process that the cpu time gets accounted to
  2852. * @hardirq_offset: the offset to subtract from hardirq_count()
  2853. * @cputime: the cpu time spent in user space since the last update
  2854. */
  2855. void account_user_time(struct task_struct *p, cputime_t cputime)
  2856. {
  2857. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2858. cputime64_t tmp;
  2859. struct rq *rq = this_rq();
  2860. p->utime = cputime_add(p->utime, cputime);
  2861. if (p != rq->idle)
  2862. cpuacct_charge(p, cputime);
  2863. /* Add user time to cpustat. */
  2864. tmp = cputime_to_cputime64(cputime);
  2865. if (TASK_NICE(p) > 0)
  2866. cpustat->nice = cputime64_add(cpustat->nice, tmp);
  2867. else
  2868. cpustat->user = cputime64_add(cpustat->user, tmp);
  2869. }
  2870. /*
  2871. * Account guest cpu time to a process.
  2872. * @p: the process that the cpu time gets accounted to
  2873. * @cputime: the cpu time spent in virtual machine since the last update
  2874. */
  2875. void account_guest_time(struct task_struct *p, cputime_t cputime)
  2876. {
  2877. cputime64_t tmp;
  2878. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2879. tmp = cputime_to_cputime64(cputime);
  2880. p->utime = cputime_add(p->utime, cputime);
  2881. p->gtime = cputime_add(p->gtime, cputime);
  2882. cpustat->user = cputime64_add(cpustat->user, tmp);
  2883. cpustat->guest = cputime64_add(cpustat->guest, tmp);
  2884. }
  2885. /*
  2886. * Account scaled user cpu time to a process.
  2887. * @p: the process that the cpu time gets accounted to
  2888. * @cputime: the cpu time spent in user space since the last update
  2889. */
  2890. void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
  2891. {
  2892. p->utimescaled = cputime_add(p->utimescaled, cputime);
  2893. }
  2894. /*
  2895. * Account system cpu time to a process.
  2896. * @p: the process that the cpu time gets accounted to
  2897. * @hardirq_offset: the offset to subtract from hardirq_count()
  2898. * @cputime: the cpu time spent in kernel space since the last update
  2899. */
  2900. void account_system_time(struct task_struct *p, int hardirq_offset,
  2901. cputime_t cputime)
  2902. {
  2903. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2904. struct rq *rq = this_rq();
  2905. cputime64_t tmp;
  2906. if (p->flags & PF_VCPU) {
  2907. account_guest_time(p, cputime);
  2908. p->flags &= ~PF_VCPU;
  2909. return;
  2910. }
  2911. p->stime = cputime_add(p->stime, cputime);
  2912. /* Add system time to cpustat. */
  2913. tmp = cputime_to_cputime64(cputime);
  2914. if (hardirq_count() - hardirq_offset)
  2915. cpustat->irq = cputime64_add(cpustat->irq, tmp);
  2916. else if (softirq_count())
  2917. cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
  2918. else if (p != rq->idle) {
  2919. cpustat->system = cputime64_add(cpustat->system, tmp);
  2920. cpuacct_charge(p, cputime);
  2921. } else if (atomic_read(&rq->nr_iowait) > 0)
  2922. cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
  2923. else
  2924. cpustat->idle = cputime64_add(cpustat->idle, tmp);
  2925. /* Account for system time used */
  2926. acct_update_integrals(p);
  2927. }
  2928. /*
  2929. * Account scaled system cpu time to a process.
  2930. * @p: the process that the cpu time gets accounted to
  2931. * @hardirq_offset: the offset to subtract from hardirq_count()
  2932. * @cputime: the cpu time spent in kernel space since the last update
  2933. */
  2934. void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
  2935. {
  2936. p->stimescaled = cputime_add(p->stimescaled, cputime);
  2937. }
  2938. /*
  2939. * Account for involuntary wait time.
  2940. * @p: the process from which the cpu time has been stolen
  2941. * @steal: the cpu time spent in involuntary wait
  2942. */
  2943. void account_steal_time(struct task_struct *p, cputime_t steal)
  2944. {
  2945. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2946. cputime64_t tmp = cputime_to_cputime64(steal);
  2947. struct rq *rq = this_rq();
  2948. if (p == rq->idle) {
  2949. p->stime = cputime_add(p->stime, steal);
  2950. if (atomic_read(&rq->nr_iowait) > 0)
  2951. cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
  2952. else
  2953. cpustat->idle = cputime64_add(cpustat->idle, tmp);
  2954. } else {
  2955. cpustat->steal = cputime64_add(cpustat->steal, tmp);
  2956. cpuacct_charge(p, -tmp);
  2957. }
  2958. }
  2959. /*
  2960. * This function gets called by the timer code, with HZ frequency.
  2961. * We call it with interrupts disabled.
  2962. *
  2963. * It also gets called by the fork code, when changing the parent's
  2964. * timeslices.
  2965. */
  2966. void scheduler_tick(void)
  2967. {
  2968. int cpu = smp_processor_id();
  2969. struct rq *rq = cpu_rq(cpu);
  2970. struct task_struct *curr = rq->curr;
  2971. u64 next_tick = rq->tick_timestamp + TICK_NSEC;
  2972. spin_lock(&rq->lock);
  2973. __update_rq_clock(rq);
  2974. /*
  2975. * Let rq->clock advance by at least TICK_NSEC:
  2976. */
  2977. if (unlikely(rq->clock < next_tick))
  2978. rq->clock = next_tick;
  2979. rq->tick_timestamp = rq->clock;
  2980. update_cpu_load(rq);
  2981. if (curr != rq->idle) /* FIXME: needed? */
  2982. curr->sched_class->task_tick(rq, curr);
  2983. spin_unlock(&rq->lock);
  2984. #ifdef CONFIG_SMP
  2985. rq->idle_at_tick = idle_cpu(cpu);
  2986. trigger_load_balance(rq, cpu);
  2987. #endif
  2988. }
  2989. #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
  2990. void fastcall add_preempt_count(int val)
  2991. {
  2992. /*
  2993. * Underflow?
  2994. */
  2995. if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
  2996. return;
  2997. preempt_count() += val;
  2998. /*
  2999. * Spinlock count overflowing soon?
  3000. */
  3001. DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
  3002. PREEMPT_MASK - 10);
  3003. }
  3004. EXPORT_SYMBOL(add_preempt_count);
  3005. void fastcall sub_preempt_count(int val)
  3006. {
  3007. /*
  3008. * Underflow?
  3009. */
  3010. if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
  3011. return;
  3012. /*
  3013. * Is the spinlock portion underflowing?
  3014. */
  3015. if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
  3016. !(preempt_count() & PREEMPT_MASK)))
  3017. return;
  3018. preempt_count() -= val;
  3019. }
  3020. EXPORT_SYMBOL(sub_preempt_count);
  3021. #endif
  3022. /*
  3023. * Print scheduling while atomic bug:
  3024. */
  3025. static noinline void __schedule_bug(struct task_struct *prev)
  3026. {
  3027. printk(KERN_ERR "BUG: scheduling while atomic: %s/0x%08x/%d\n",
  3028. prev->comm, preempt_count(), task_pid_nr(prev));
  3029. debug_show_held_locks(prev);
  3030. if (irqs_disabled())
  3031. print_irqtrace_events(prev);
  3032. dump_stack();
  3033. }
  3034. /*
  3035. * Various schedule()-time debugging checks and statistics:
  3036. */
  3037. static inline void schedule_debug(struct task_struct *prev)
  3038. {
  3039. /*
  3040. * Test if we are atomic. Since do_exit() needs to call into
  3041. * schedule() atomically, we ignore that path for now.
  3042. * Otherwise, whine if we are scheduling when we should not be.
  3043. */
  3044. if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
  3045. __schedule_bug(prev);
  3046. profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  3047. schedstat_inc(this_rq(), sched_count);
  3048. #ifdef CONFIG_SCHEDSTATS
  3049. if (unlikely(prev->lock_depth >= 0)) {
  3050. schedstat_inc(this_rq(), bkl_count);
  3051. schedstat_inc(prev, sched_info.bkl_count);
  3052. }
  3053. #endif
  3054. }
  3055. /*
  3056. * Pick up the highest-prio task:
  3057. */
  3058. static inline struct task_struct *
  3059. pick_next_task(struct rq *rq, struct task_struct *prev)
  3060. {
  3061. const struct sched_class *class;
  3062. struct task_struct *p;
  3063. /*
  3064. * Optimization: we know that if all tasks are in
  3065. * the fair class we can call that function directly:
  3066. */
  3067. if (likely(rq->nr_running == rq->cfs.nr_running)) {
  3068. p = fair_sched_class.pick_next_task(rq);
  3069. if (likely(p))
  3070. return p;
  3071. }
  3072. class = sched_class_highest;
  3073. for ( ; ; ) {
  3074. p = class->pick_next_task(rq);
  3075. if (p)
  3076. return p;
  3077. /*
  3078. * Will never be NULL as the idle class always
  3079. * returns a non-NULL p:
  3080. */
  3081. class = class->next;
  3082. }
  3083. }
  3084. /*
  3085. * schedule() is the main scheduler function.
  3086. */
  3087. asmlinkage void __sched schedule(void)
  3088. {
  3089. struct task_struct *prev, *next;
  3090. long *switch_count;
  3091. struct rq *rq;
  3092. int cpu;
  3093. need_resched:
  3094. preempt_disable();
  3095. cpu = smp_processor_id();
  3096. rq = cpu_rq(cpu);
  3097. rcu_qsctr_inc(cpu);
  3098. prev = rq->curr;
  3099. switch_count = &prev->nivcsw;
  3100. release_kernel_lock(prev);
  3101. need_resched_nonpreemptible:
  3102. schedule_debug(prev);
  3103. /*
  3104. * Do the rq-clock update outside the rq lock:
  3105. */
  3106. local_irq_disable();
  3107. __update_rq_clock(rq);
  3108. spin_lock(&rq->lock);
  3109. clear_tsk_need_resched(prev);
  3110. if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  3111. if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
  3112. unlikely(signal_pending(prev)))) {
  3113. prev->state = TASK_RUNNING;
  3114. } else {
  3115. deactivate_task(rq, prev, 1);
  3116. }
  3117. switch_count = &prev->nvcsw;
  3118. }
  3119. if (unlikely(!rq->nr_running))
  3120. idle_balance(cpu, rq);
  3121. prev->sched_class->put_prev_task(rq, prev);
  3122. next = pick_next_task(rq, prev);
  3123. sched_info_switch(prev, next);
  3124. if (likely(prev != next)) {
  3125. rq->nr_switches++;
  3126. rq->curr = next;
  3127. ++*switch_count;
  3128. context_switch(rq, prev, next); /* unlocks the rq */
  3129. } else
  3130. spin_unlock_irq(&rq->lock);
  3131. if (unlikely(reacquire_kernel_lock(current) < 0)) {
  3132. cpu = smp_processor_id();
  3133. rq = cpu_rq(cpu);
  3134. goto need_resched_nonpreemptible;
  3135. }
  3136. preempt_enable_no_resched();
  3137. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  3138. goto need_resched;
  3139. }
  3140. EXPORT_SYMBOL(schedule);
  3141. #ifdef CONFIG_PREEMPT
  3142. /*
  3143. * this is the entry point to schedule() from in-kernel preemption
  3144. * off of preempt_enable. Kernel preemptions off return from interrupt
  3145. * occur there and call schedule directly.
  3146. */
  3147. asmlinkage void __sched preempt_schedule(void)
  3148. {
  3149. struct thread_info *ti = current_thread_info();
  3150. #ifdef CONFIG_PREEMPT_BKL
  3151. struct task_struct *task = current;
  3152. int saved_lock_depth;
  3153. #endif
  3154. /*
  3155. * If there is a non-zero preempt_count or interrupts are disabled,
  3156. * we do not want to preempt the current task. Just return..
  3157. */
  3158. if (likely(ti->preempt_count || irqs_disabled()))
  3159. return;
  3160. do {
  3161. add_preempt_count(PREEMPT_ACTIVE);
  3162. /*
  3163. * We keep the big kernel semaphore locked, but we
  3164. * clear ->lock_depth so that schedule() doesnt
  3165. * auto-release the semaphore:
  3166. */
  3167. #ifdef CONFIG_PREEMPT_BKL
  3168. saved_lock_depth = task->lock_depth;
  3169. task->lock_depth = -1;
  3170. #endif
  3171. schedule();
  3172. #ifdef CONFIG_PREEMPT_BKL
  3173. task->lock_depth = saved_lock_depth;
  3174. #endif
  3175. sub_preempt_count(PREEMPT_ACTIVE);
  3176. /*
  3177. * Check again in case we missed a preemption opportunity
  3178. * between schedule and now.
  3179. */
  3180. barrier();
  3181. } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
  3182. }
  3183. EXPORT_SYMBOL(preempt_schedule);
  3184. /*
  3185. * this is the entry point to schedule() from kernel preemption
  3186. * off of irq context.
  3187. * Note, that this is called and return with irqs disabled. This will
  3188. * protect us against recursive calling from irq.
  3189. */
  3190. asmlinkage void __sched preempt_schedule_irq(void)
  3191. {
  3192. struct thread_info *ti = current_thread_info();
  3193. #ifdef CONFIG_PREEMPT_BKL
  3194. struct task_struct *task = current;
  3195. int saved_lock_depth;
  3196. #endif
  3197. /* Catch callers which need to be fixed */
  3198. BUG_ON(ti->preempt_count || !irqs_disabled());
  3199. do {
  3200. add_preempt_count(PREEMPT_ACTIVE);
  3201. /*
  3202. * We keep the big kernel semaphore locked, but we
  3203. * clear ->lock_depth so that schedule() doesnt
  3204. * auto-release the semaphore:
  3205. */
  3206. #ifdef CONFIG_PREEMPT_BKL
  3207. saved_lock_depth = task->lock_depth;
  3208. task->lock_depth = -1;
  3209. #endif
  3210. local_irq_enable();
  3211. schedule();
  3212. local_irq_disable();
  3213. #ifdef CONFIG_PREEMPT_BKL
  3214. task->lock_depth = saved_lock_depth;
  3215. #endif
  3216. sub_preempt_count(PREEMPT_ACTIVE);
  3217. /*
  3218. * Check again in case we missed a preemption opportunity
  3219. * between schedule and now.
  3220. */
  3221. barrier();
  3222. } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
  3223. }
  3224. #endif /* CONFIG_PREEMPT */
  3225. int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
  3226. void *key)
  3227. {
  3228. return try_to_wake_up(curr->private, mode, sync);
  3229. }
  3230. EXPORT_SYMBOL(default_wake_function);
  3231. /*
  3232. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  3233. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  3234. * number) then we wake all the non-exclusive tasks and one exclusive task.
  3235. *
  3236. * There are circumstances in which we can try to wake a task which has already
  3237. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  3238. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  3239. */
  3240. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  3241. int nr_exclusive, int sync, void *key)
  3242. {
  3243. wait_queue_t *curr, *next;
  3244. list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
  3245. unsigned flags = curr->flags;
  3246. if (curr->func(curr, mode, sync, key) &&
  3247. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  3248. break;
  3249. }
  3250. }
  3251. /**
  3252. * __wake_up - wake up threads blocked on a waitqueue.
  3253. * @q: the waitqueue
  3254. * @mode: which threads
  3255. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  3256. * @key: is directly passed to the wakeup function
  3257. */
  3258. void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
  3259. int nr_exclusive, void *key)
  3260. {
  3261. unsigned long flags;
  3262. spin_lock_irqsave(&q->lock, flags);
  3263. __wake_up_common(q, mode, nr_exclusive, 0, key);
  3264. spin_unlock_irqrestore(&q->lock, flags);
  3265. }
  3266. EXPORT_SYMBOL(__wake_up);
  3267. /*
  3268. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  3269. */
  3270. void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
  3271. {
  3272. __wake_up_common(q, mode, 1, 0, NULL);
  3273. }
  3274. /**
  3275. * __wake_up_sync - wake up threads blocked on a waitqueue.
  3276. * @q: the waitqueue
  3277. * @mode: which threads
  3278. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  3279. *
  3280. * The sync wakeup differs that the waker knows that it will schedule
  3281. * away soon, so while the target thread will be woken up, it will not
  3282. * be migrated to another CPU - ie. the two threads are 'synchronized'
  3283. * with each other. This can prevent needless bouncing between CPUs.
  3284. *
  3285. * On UP it can prevent extra preemption.
  3286. */
  3287. void fastcall
  3288. __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  3289. {
  3290. unsigned long flags;
  3291. int sync = 1;
  3292. if (unlikely(!q))
  3293. return;
  3294. if (unlikely(!nr_exclusive))
  3295. sync = 0;
  3296. spin_lock_irqsave(&q->lock, flags);
  3297. __wake_up_common(q, mode, nr_exclusive, sync, NULL);
  3298. spin_unlock_irqrestore(&q->lock, flags);
  3299. }
  3300. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  3301. void fastcall complete(struct completion *x)
  3302. {
  3303. unsigned long flags;
  3304. spin_lock_irqsave(&x->wait.lock, flags);
  3305. x->done++;
  3306. __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
  3307. 1, 0, NULL);
  3308. spin_unlock_irqrestore(&x->wait.lock, flags);
  3309. }
  3310. EXPORT_SYMBOL(complete);
  3311. void fastcall complete_all(struct completion *x)
  3312. {
  3313. unsigned long flags;
  3314. spin_lock_irqsave(&x->wait.lock, flags);
  3315. x->done += UINT_MAX/2;
  3316. __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
  3317. 0, 0, NULL);
  3318. spin_unlock_irqrestore(&x->wait.lock, flags);
  3319. }
  3320. EXPORT_SYMBOL(complete_all);
  3321. static inline long __sched
  3322. do_wait_for_common(struct completion *x, long timeout, int state)
  3323. {
  3324. if (!x->done) {
  3325. DECLARE_WAITQUEUE(wait, current);
  3326. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3327. __add_wait_queue_tail(&x->wait, &wait);
  3328. do {
  3329. if (state == TASK_INTERRUPTIBLE &&
  3330. signal_pending(current)) {
  3331. __remove_wait_queue(&x->wait, &wait);
  3332. return -ERESTARTSYS;
  3333. }
  3334. __set_current_state(state);
  3335. spin_unlock_irq(&x->wait.lock);
  3336. timeout = schedule_timeout(timeout);
  3337. spin_lock_irq(&x->wait.lock);
  3338. if (!timeout) {
  3339. __remove_wait_queue(&x->wait, &wait);
  3340. return timeout;
  3341. }
  3342. } while (!x->done);
  3343. __remove_wait_queue(&x->wait, &wait);
  3344. }
  3345. x->done--;
  3346. return timeout;
  3347. }
  3348. static long __sched
  3349. wait_for_common(struct completion *x, long timeout, int state)
  3350. {
  3351. might_sleep();
  3352. spin_lock_irq(&x->wait.lock);
  3353. timeout = do_wait_for_common(x, timeout, state);
  3354. spin_unlock_irq(&x->wait.lock);
  3355. return timeout;
  3356. }
  3357. void fastcall __sched wait_for_completion(struct completion *x)
  3358. {
  3359. wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
  3360. }
  3361. EXPORT_SYMBOL(wait_for_completion);
  3362. unsigned long fastcall __sched
  3363. wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  3364. {
  3365. return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
  3366. }
  3367. EXPORT_SYMBOL(wait_for_completion_timeout);
  3368. int __sched wait_for_completion_interruptible(struct completion *x)
  3369. {
  3370. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
  3371. if (t == -ERESTARTSYS)
  3372. return t;
  3373. return 0;
  3374. }
  3375. EXPORT_SYMBOL(wait_for_completion_interruptible);
  3376. unsigned long fastcall __sched
  3377. wait_for_completion_interruptible_timeout(struct completion *x,
  3378. unsigned long timeout)
  3379. {
  3380. return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
  3381. }
  3382. EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  3383. static long __sched
  3384. sleep_on_common(wait_queue_head_t *q, int state, long timeout)
  3385. {
  3386. unsigned long flags;
  3387. wait_queue_t wait;
  3388. init_waitqueue_entry(&wait, current);
  3389. __set_current_state(state);
  3390. spin_lock_irqsave(&q->lock, flags);
  3391. __add_wait_queue(q, &wait);
  3392. spin_unlock(&q->lock);
  3393. timeout = schedule_timeout(timeout);
  3394. spin_lock_irq(&q->lock);
  3395. __remove_wait_queue(q, &wait);
  3396. spin_unlock_irqrestore(&q->lock, flags);
  3397. return timeout;
  3398. }
  3399. void __sched interruptible_sleep_on(wait_queue_head_t *q)
  3400. {
  3401. sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  3402. }
  3403. EXPORT_SYMBOL(interruptible_sleep_on);
  3404. long __sched
  3405. interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
  3406. {
  3407. return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
  3408. }
  3409. EXPORT_SYMBOL(interruptible_sleep_on_timeout);
  3410. void __sched sleep_on(wait_queue_head_t *q)
  3411. {
  3412. sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  3413. }
  3414. EXPORT_SYMBOL(sleep_on);
  3415. long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
  3416. {
  3417. return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
  3418. }
  3419. EXPORT_SYMBOL(sleep_on_timeout);
  3420. #ifdef CONFIG_RT_MUTEXES
  3421. /*
  3422. * rt_mutex_setprio - set the current priority of a task
  3423. * @p: task
  3424. * @prio: prio value (kernel-internal form)
  3425. *
  3426. * This function changes the 'effective' priority of a task. It does
  3427. * not touch ->normal_prio like __setscheduler().
  3428. *
  3429. * Used by the rt_mutex code to implement priority inheritance logic.
  3430. */
  3431. void rt_mutex_setprio(struct task_struct *p, int prio)
  3432. {
  3433. unsigned long flags;
  3434. int oldprio, on_rq, running;
  3435. struct rq *rq;
  3436. BUG_ON(prio < 0 || prio > MAX_PRIO);
  3437. rq = task_rq_lock(p, &flags);
  3438. update_rq_clock(rq);
  3439. oldprio = p->prio;
  3440. on_rq = p->se.on_rq;
  3441. running = task_running(rq, p);
  3442. if (on_rq) {
  3443. dequeue_task(rq, p, 0);
  3444. if (running)
  3445. p->sched_class->put_prev_task(rq, p);
  3446. }
  3447. if (rt_prio(prio))
  3448. p->sched_class = &rt_sched_class;
  3449. else
  3450. p->sched_class = &fair_sched_class;
  3451. p->prio = prio;
  3452. if (on_rq) {
  3453. if (running)
  3454. p->sched_class->set_curr_task(rq);
  3455. enqueue_task(rq, p, 0);
  3456. /*
  3457. * Reschedule if we are currently running on this runqueue and
  3458. * our priority decreased, or if we are not currently running on
  3459. * this runqueue and our priority is higher than the current's
  3460. */
  3461. if (running) {
  3462. if (p->prio > oldprio)
  3463. resched_task(rq->curr);
  3464. } else {
  3465. check_preempt_curr(rq, p);
  3466. }
  3467. }
  3468. task_rq_unlock(rq, &flags);
  3469. }
  3470. #endif
  3471. void set_user_nice(struct task_struct *p, long nice)
  3472. {
  3473. int old_prio, delta, on_rq;
  3474. unsigned long flags;
  3475. struct rq *rq;
  3476. if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
  3477. return;
  3478. /*
  3479. * We have to be careful, if called from sys_setpriority(),
  3480. * the task might be in the middle of scheduling on another CPU.
  3481. */
  3482. rq = task_rq_lock(p, &flags);
  3483. update_rq_clock(rq);
  3484. /*
  3485. * The RT priorities are set via sched_setscheduler(), but we still
  3486. * allow the 'normal' nice value to be set - but as expected
  3487. * it wont have any effect on scheduling until the task is
  3488. * SCHED_FIFO/SCHED_RR:
  3489. */
  3490. if (task_has_rt_policy(p)) {
  3491. p->static_prio = NICE_TO_PRIO(nice);
  3492. goto out_unlock;
  3493. }
  3494. on_rq = p->se.on_rq;
  3495. if (on_rq) {
  3496. dequeue_task(rq, p, 0);
  3497. dec_load(rq, p);
  3498. }
  3499. p->static_prio = NICE_TO_PRIO(nice);
  3500. set_load_weight(p);
  3501. old_prio = p->prio;
  3502. p->prio = effective_prio(p);
  3503. delta = p->prio - old_prio;
  3504. if (on_rq) {
  3505. enqueue_task(rq, p, 0);
  3506. inc_load(rq, p);
  3507. /*
  3508. * If the task increased its priority or is running and
  3509. * lowered its priority, then reschedule its CPU:
  3510. */
  3511. if (delta < 0 || (delta > 0 && task_running(rq, p)))
  3512. resched_task(rq->curr);
  3513. }
  3514. out_unlock:
  3515. task_rq_unlock(rq, &flags);
  3516. }
  3517. EXPORT_SYMBOL(set_user_nice);
  3518. /*
  3519. * can_nice - check if a task can reduce its nice value
  3520. * @p: task
  3521. * @nice: nice value
  3522. */
  3523. int can_nice(const struct task_struct *p, const int nice)
  3524. {
  3525. /* convert nice value [19,-20] to rlimit style value [1,40] */
  3526. int nice_rlim = 20 - nice;
  3527. return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
  3528. capable(CAP_SYS_NICE));
  3529. }
  3530. #ifdef __ARCH_WANT_SYS_NICE
  3531. /*
  3532. * sys_nice - change the priority of the current process.
  3533. * @increment: priority increment
  3534. *
  3535. * sys_setpriority is a more generic, but much slower function that
  3536. * does similar things.
  3537. */
  3538. asmlinkage long sys_nice(int increment)
  3539. {
  3540. long nice, retval;
  3541. /*
  3542. * Setpriority might change our priority at the same moment.
  3543. * We don't have to worry. Conceptually one call occurs first
  3544. * and we have a single winner.
  3545. */
  3546. if (increment < -40)
  3547. increment = -40;
  3548. if (increment > 40)
  3549. increment = 40;
  3550. nice = PRIO_TO_NICE(current->static_prio) + increment;
  3551. if (nice < -20)
  3552. nice = -20;
  3553. if (nice > 19)
  3554. nice = 19;
  3555. if (increment < 0 && !can_nice(current, nice))
  3556. return -EPERM;
  3557. retval = security_task_setnice(current, nice);
  3558. if (retval)
  3559. return retval;
  3560. set_user_nice(current, nice);
  3561. return 0;
  3562. }
  3563. #endif
  3564. /**
  3565. * task_prio - return the priority value of a given task.
  3566. * @p: the task in question.
  3567. *
  3568. * This is the priority value as seen by users in /proc.
  3569. * RT tasks are offset by -200. Normal tasks are centered
  3570. * around 0, value goes from -16 to +15.
  3571. */
  3572. int task_prio(const struct task_struct *p)
  3573. {
  3574. return p->prio - MAX_RT_PRIO;
  3575. }
  3576. /**
  3577. * task_nice - return the nice value of a given task.
  3578. * @p: the task in question.
  3579. */
  3580. int task_nice(const struct task_struct *p)
  3581. {
  3582. return TASK_NICE(p);
  3583. }
  3584. EXPORT_SYMBOL_GPL(task_nice);
  3585. /**
  3586. * idle_cpu - is a given cpu idle currently?
  3587. * @cpu: the processor in question.
  3588. */
  3589. int idle_cpu(int cpu)
  3590. {
  3591. return cpu_curr(cpu) == cpu_rq(cpu)->idle;
  3592. }
  3593. /**
  3594. * idle_task - return the idle task for a given cpu.
  3595. * @cpu: the processor in question.
  3596. */
  3597. struct task_struct *idle_task(int cpu)
  3598. {
  3599. return cpu_rq(cpu)->idle;
  3600. }
  3601. /**
  3602. * find_process_by_pid - find a process with a matching PID value.
  3603. * @pid: the pid in question.
  3604. */
  3605. static struct task_struct *find_process_by_pid(pid_t pid)
  3606. {
  3607. return pid ? find_task_by_vpid(pid) : current;
  3608. }
  3609. /* Actually do priority change: must hold rq lock. */
  3610. static void
  3611. __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
  3612. {
  3613. BUG_ON(p->se.on_rq);
  3614. p->policy = policy;
  3615. switch (p->policy) {
  3616. case SCHED_NORMAL:
  3617. case SCHED_BATCH:
  3618. case SCHED_IDLE:
  3619. p->sched_class = &fair_sched_class;
  3620. break;
  3621. case SCHED_FIFO:
  3622. case SCHED_RR:
  3623. p->sched_class = &rt_sched_class;
  3624. break;
  3625. }
  3626. p->rt_priority = prio;
  3627. p->normal_prio = normal_prio(p);
  3628. /* we are holding p->pi_lock already */
  3629. p->prio = rt_mutex_getprio(p);
  3630. set_load_weight(p);
  3631. }
  3632. /**
  3633. * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
  3634. * @p: the task in question.
  3635. * @policy: new policy.
  3636. * @param: structure containing the new RT priority.
  3637. *
  3638. * NOTE that the task may be already dead.
  3639. */
  3640. int sched_setscheduler(struct task_struct *p, int policy,
  3641. struct sched_param *param)
  3642. {
  3643. int retval, oldprio, oldpolicy = -1, on_rq, running;
  3644. unsigned long flags;
  3645. struct rq *rq;
  3646. /* may grab non-irq protected spin_locks */
  3647. BUG_ON(in_interrupt());
  3648. recheck:
  3649. /* double check policy once rq lock held */
  3650. if (policy < 0)
  3651. policy = oldpolicy = p->policy;
  3652. else if (policy != SCHED_FIFO && policy != SCHED_RR &&
  3653. policy != SCHED_NORMAL && policy != SCHED_BATCH &&
  3654. policy != SCHED_IDLE)
  3655. return -EINVAL;
  3656. /*
  3657. * Valid priorities for SCHED_FIFO and SCHED_RR are
  3658. * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
  3659. * SCHED_BATCH and SCHED_IDLE is 0.
  3660. */
  3661. if (param->sched_priority < 0 ||
  3662. (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
  3663. (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
  3664. return -EINVAL;
  3665. if (rt_policy(policy) != (param->sched_priority != 0))
  3666. return -EINVAL;
  3667. /*
  3668. * Allow unprivileged RT tasks to decrease priority:
  3669. */
  3670. if (!capable(CAP_SYS_NICE)) {
  3671. if (rt_policy(policy)) {
  3672. unsigned long rlim_rtprio;
  3673. if (!lock_task_sighand(p, &flags))
  3674. return -ESRCH;
  3675. rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
  3676. unlock_task_sighand(p, &flags);
  3677. /* can't set/change the rt policy */
  3678. if (policy != p->policy && !rlim_rtprio)
  3679. return -EPERM;
  3680. /* can't increase priority */
  3681. if (param->sched_priority > p->rt_priority &&
  3682. param->sched_priority > rlim_rtprio)
  3683. return -EPERM;
  3684. }
  3685. /*
  3686. * Like positive nice levels, dont allow tasks to
  3687. * move out of SCHED_IDLE either:
  3688. */
  3689. if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
  3690. return -EPERM;
  3691. /* can't change other user's priorities */
  3692. if ((current->euid != p->euid) &&
  3693. (current->euid != p->uid))
  3694. return -EPERM;
  3695. }
  3696. retval = security_task_setscheduler(p, policy, param);
  3697. if (retval)
  3698. return retval;
  3699. /*
  3700. * make sure no PI-waiters arrive (or leave) while we are
  3701. * changing the priority of the task:
  3702. */
  3703. spin_lock_irqsave(&p->pi_lock, flags);
  3704. /*
  3705. * To be able to change p->policy safely, the apropriate
  3706. * runqueue lock must be held.
  3707. */
  3708. rq = __task_rq_lock(p);
  3709. /* recheck policy now with rq lock held */
  3710. if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  3711. policy = oldpolicy = -1;
  3712. __task_rq_unlock(rq);
  3713. spin_unlock_irqrestore(&p->pi_lock, flags);
  3714. goto recheck;
  3715. }
  3716. update_rq_clock(rq);
  3717. on_rq = p->se.on_rq;
  3718. running = task_running(rq, p);
  3719. if (on_rq) {
  3720. deactivate_task(rq, p, 0);
  3721. if (running)
  3722. p->sched_class->put_prev_task(rq, p);
  3723. }
  3724. oldprio = p->prio;
  3725. __setscheduler(rq, p, policy, param->sched_priority);
  3726. if (on_rq) {
  3727. if (running)
  3728. p->sched_class->set_curr_task(rq);
  3729. activate_task(rq, p, 0);
  3730. /*
  3731. * Reschedule if we are currently running on this runqueue and
  3732. * our priority decreased, or if we are not currently running on
  3733. * this runqueue and our priority is higher than the current's
  3734. */
  3735. if (running) {
  3736. if (p->prio > oldprio)
  3737. resched_task(rq->curr);
  3738. } else {
  3739. check_preempt_curr(rq, p);
  3740. }
  3741. }
  3742. __task_rq_unlock(rq);
  3743. spin_unlock_irqrestore(&p->pi_lock, flags);
  3744. rt_mutex_adjust_pi(p);
  3745. return 0;
  3746. }
  3747. EXPORT_SYMBOL_GPL(sched_setscheduler);
  3748. static int
  3749. do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  3750. {
  3751. struct sched_param lparam;
  3752. struct task_struct *p;
  3753. int retval;
  3754. if (!param || pid < 0)
  3755. return -EINVAL;
  3756. if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  3757. return -EFAULT;
  3758. rcu_read_lock();
  3759. retval = -ESRCH;
  3760. p = find_process_by_pid(pid);
  3761. if (p != NULL)
  3762. retval = sched_setscheduler(p, policy, &lparam);
  3763. rcu_read_unlock();
  3764. return retval;
  3765. }
  3766. /**
  3767. * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  3768. * @pid: the pid in question.
  3769. * @policy: new policy.
  3770. * @param: structure containing the new RT priority.
  3771. */
  3772. asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
  3773. struct sched_param __user *param)
  3774. {
  3775. /* negative values for policy are not valid */
  3776. if (policy < 0)
  3777. return -EINVAL;
  3778. return do_sched_setscheduler(pid, policy, param);
  3779. }
  3780. /**
  3781. * sys_sched_setparam - set/change the RT priority of a thread
  3782. * @pid: the pid in question.
  3783. * @param: structure containing the new RT priority.
  3784. */
  3785. asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
  3786. {
  3787. return do_sched_setscheduler(pid, -1, param);
  3788. }
  3789. /**
  3790. * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  3791. * @pid: the pid in question.
  3792. */
  3793. asmlinkage long sys_sched_getscheduler(pid_t pid)
  3794. {
  3795. struct task_struct *p;
  3796. int retval;
  3797. if (pid < 0)
  3798. return -EINVAL;
  3799. retval = -ESRCH;
  3800. read_lock(&tasklist_lock);
  3801. p = find_process_by_pid(pid);
  3802. if (p) {
  3803. retval = security_task_getscheduler(p);
  3804. if (!retval)
  3805. retval = p->policy;
  3806. }
  3807. read_unlock(&tasklist_lock);
  3808. return retval;
  3809. }
  3810. /**
  3811. * sys_sched_getscheduler - get the RT priority of a thread
  3812. * @pid: the pid in question.
  3813. * @param: structure containing the RT priority.
  3814. */
  3815. asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
  3816. {
  3817. struct sched_param lp;
  3818. struct task_struct *p;
  3819. int retval;
  3820. if (!param || pid < 0)
  3821. return -EINVAL;
  3822. read_lock(&tasklist_lock);
  3823. p = find_process_by_pid(pid);
  3824. retval = -ESRCH;
  3825. if (!p)
  3826. goto out_unlock;
  3827. retval = security_task_getscheduler(p);
  3828. if (retval)
  3829. goto out_unlock;
  3830. lp.sched_priority = p->rt_priority;
  3831. read_unlock(&tasklist_lock);
  3832. /*
  3833. * This one might sleep, we cannot do it with a spinlock held ...
  3834. */
  3835. retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  3836. return retval;
  3837. out_unlock:
  3838. read_unlock(&tasklist_lock);
  3839. return retval;
  3840. }
  3841. long sched_setaffinity(pid_t pid, cpumask_t new_mask)
  3842. {
  3843. cpumask_t cpus_allowed;
  3844. struct task_struct *p;
  3845. int retval;
  3846. mutex_lock(&sched_hotcpu_mutex);
  3847. read_lock(&tasklist_lock);
  3848. p = find_process_by_pid(pid);
  3849. if (!p) {
  3850. read_unlock(&tasklist_lock);
  3851. mutex_unlock(&sched_hotcpu_mutex);
  3852. return -ESRCH;
  3853. }
  3854. /*
  3855. * It is not safe to call set_cpus_allowed with the
  3856. * tasklist_lock held. We will bump the task_struct's
  3857. * usage count and then drop tasklist_lock.
  3858. */
  3859. get_task_struct(p);
  3860. read_unlock(&tasklist_lock);
  3861. retval = -EPERM;
  3862. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  3863. !capable(CAP_SYS_NICE))
  3864. goto out_unlock;
  3865. retval = security_task_setscheduler(p, 0, NULL);
  3866. if (retval)
  3867. goto out_unlock;
  3868. cpus_allowed = cpuset_cpus_allowed(p);
  3869. cpus_and(new_mask, new_mask, cpus_allowed);
  3870. again:
  3871. retval = set_cpus_allowed(p, new_mask);
  3872. if (!retval) {
  3873. cpus_allowed = cpuset_cpus_allowed(p);
  3874. if (!cpus_subset(new_mask, cpus_allowed)) {
  3875. /*
  3876. * We must have raced with a concurrent cpuset
  3877. * update. Just reset the cpus_allowed to the
  3878. * cpuset's cpus_allowed
  3879. */
  3880. new_mask = cpus_allowed;
  3881. goto again;
  3882. }
  3883. }
  3884. out_unlock:
  3885. put_task_struct(p);
  3886. mutex_unlock(&sched_hotcpu_mutex);
  3887. return retval;
  3888. }
  3889. static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  3890. cpumask_t *new_mask)
  3891. {
  3892. if (len < sizeof(cpumask_t)) {
  3893. memset(new_mask, 0, sizeof(cpumask_t));
  3894. } else if (len > sizeof(cpumask_t)) {
  3895. len = sizeof(cpumask_t);
  3896. }
  3897. return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  3898. }
  3899. /**
  3900. * sys_sched_setaffinity - set the cpu affinity of a process
  3901. * @pid: pid of the process
  3902. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3903. * @user_mask_ptr: user-space pointer to the new cpu mask
  3904. */
  3905. asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
  3906. unsigned long __user *user_mask_ptr)
  3907. {
  3908. cpumask_t new_mask;
  3909. int retval;
  3910. retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
  3911. if (retval)
  3912. return retval;
  3913. return sched_setaffinity(pid, new_mask);
  3914. }
  3915. /*
  3916. * Represents all cpu's present in the system
  3917. * In systems capable of hotplug, this map could dynamically grow
  3918. * as new cpu's are detected in the system via any platform specific
  3919. * method, such as ACPI for e.g.
  3920. */
  3921. cpumask_t cpu_present_map __read_mostly;
  3922. EXPORT_SYMBOL(cpu_present_map);
  3923. #ifndef CONFIG_SMP
  3924. cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
  3925. EXPORT_SYMBOL(cpu_online_map);
  3926. cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
  3927. EXPORT_SYMBOL(cpu_possible_map);
  3928. #endif
  3929. long sched_getaffinity(pid_t pid, cpumask_t *mask)
  3930. {
  3931. struct task_struct *p;
  3932. int retval;
  3933. mutex_lock(&sched_hotcpu_mutex);
  3934. read_lock(&tasklist_lock);
  3935. retval = -ESRCH;
  3936. p = find_process_by_pid(pid);
  3937. if (!p)
  3938. goto out_unlock;
  3939. retval = security_task_getscheduler(p);
  3940. if (retval)
  3941. goto out_unlock;
  3942. cpus_and(*mask, p->cpus_allowed, cpu_online_map);
  3943. out_unlock:
  3944. read_unlock(&tasklist_lock);
  3945. mutex_unlock(&sched_hotcpu_mutex);
  3946. return retval;
  3947. }
  3948. /**
  3949. * sys_sched_getaffinity - get the cpu affinity of a process
  3950. * @pid: pid of the process
  3951. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3952. * @user_mask_ptr: user-space pointer to hold the current cpu mask
  3953. */
  3954. asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
  3955. unsigned long __user *user_mask_ptr)
  3956. {
  3957. int ret;
  3958. cpumask_t mask;
  3959. if (len < sizeof(cpumask_t))
  3960. return -EINVAL;
  3961. ret = sched_getaffinity(pid, &mask);
  3962. if (ret < 0)
  3963. return ret;
  3964. if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
  3965. return -EFAULT;
  3966. return sizeof(cpumask_t);
  3967. }
  3968. /**
  3969. * sys_sched_yield - yield the current processor to other threads.
  3970. *
  3971. * This function yields the current CPU to other tasks. If there are no
  3972. * other threads running on this CPU then this function will return.
  3973. */
  3974. asmlinkage long sys_sched_yield(void)
  3975. {
  3976. struct rq *rq = this_rq_lock();
  3977. schedstat_inc(rq, yld_count);
  3978. current->sched_class->yield_task(rq);
  3979. /*
  3980. * Since we are going to call schedule() anyway, there's
  3981. * no need to preempt or enable interrupts:
  3982. */
  3983. __release(rq->lock);
  3984. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  3985. _raw_spin_unlock(&rq->lock);
  3986. preempt_enable_no_resched();
  3987. schedule();
  3988. return 0;
  3989. }
  3990. static void __cond_resched(void)
  3991. {
  3992. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  3993. __might_sleep(__FILE__, __LINE__);
  3994. #endif
  3995. /*
  3996. * The BKS might be reacquired before we have dropped
  3997. * PREEMPT_ACTIVE, which could trigger a second
  3998. * cond_resched() call.
  3999. */
  4000. do {
  4001. add_preempt_count(PREEMPT_ACTIVE);
  4002. schedule();
  4003. sub_preempt_count(PREEMPT_ACTIVE);
  4004. } while (need_resched());
  4005. }
  4006. int __sched cond_resched(void)
  4007. {
  4008. if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
  4009. system_state == SYSTEM_RUNNING) {
  4010. __cond_resched();
  4011. return 1;
  4012. }
  4013. return 0;
  4014. }
  4015. EXPORT_SYMBOL(cond_resched);
  4016. /*
  4017. * cond_resched_lock() - if a reschedule is pending, drop the given lock,
  4018. * call schedule, and on return reacquire the lock.
  4019. *
  4020. * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  4021. * operations here to prevent schedule() from being called twice (once via
  4022. * spin_unlock(), once by hand).
  4023. */
  4024. int cond_resched_lock(spinlock_t *lock)
  4025. {
  4026. int ret = 0;
  4027. if (need_lockbreak(lock)) {
  4028. spin_unlock(lock);
  4029. cpu_relax();
  4030. ret = 1;
  4031. spin_lock(lock);
  4032. }
  4033. if (need_resched() && system_state == SYSTEM_RUNNING) {
  4034. spin_release(&lock->dep_map, 1, _THIS_IP_);
  4035. _raw_spin_unlock(lock);
  4036. preempt_enable_no_resched();
  4037. __cond_resched();
  4038. ret = 1;
  4039. spin_lock(lock);
  4040. }
  4041. return ret;
  4042. }
  4043. EXPORT_SYMBOL(cond_resched_lock);
  4044. int __sched cond_resched_softirq(void)
  4045. {
  4046. BUG_ON(!in_softirq());
  4047. if (need_resched() && system_state == SYSTEM_RUNNING) {
  4048. local_bh_enable();
  4049. __cond_resched();
  4050. local_bh_disable();
  4051. return 1;
  4052. }
  4053. return 0;
  4054. }
  4055. EXPORT_SYMBOL(cond_resched_softirq);
  4056. /**
  4057. * yield - yield the current processor to other threads.
  4058. *
  4059. * This is a shortcut for kernel-space yielding - it marks the
  4060. * thread runnable and calls sys_sched_yield().
  4061. */
  4062. void __sched yield(void)
  4063. {
  4064. set_current_state(TASK_RUNNING);
  4065. sys_sched_yield();
  4066. }
  4067. EXPORT_SYMBOL(yield);
  4068. /*
  4069. * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  4070. * that process accounting knows that this is a task in IO wait state.
  4071. *
  4072. * But don't do that if it is a deliberate, throttling IO wait (this task
  4073. * has set its backing_dev_info: the queue against which it should throttle)
  4074. */
  4075. void __sched io_schedule(void)
  4076. {
  4077. struct rq *rq = &__raw_get_cpu_var(runqueues);
  4078. delayacct_blkio_start();
  4079. atomic_inc(&rq->nr_iowait);
  4080. schedule();
  4081. atomic_dec(&rq->nr_iowait);
  4082. delayacct_blkio_end();
  4083. }
  4084. EXPORT_SYMBOL(io_schedule);
  4085. long __sched io_schedule_timeout(long timeout)
  4086. {
  4087. struct rq *rq = &__raw_get_cpu_var(runqueues);
  4088. long ret;
  4089. delayacct_blkio_start();
  4090. atomic_inc(&rq->nr_iowait);
  4091. ret = schedule_timeout(timeout);
  4092. atomic_dec(&rq->nr_iowait);
  4093. delayacct_blkio_end();
  4094. return ret;
  4095. }
  4096. /**
  4097. * sys_sched_get_priority_max - return maximum RT priority.
  4098. * @policy: scheduling class.
  4099. *
  4100. * this syscall returns the maximum rt_priority that can be used
  4101. * by a given scheduling class.
  4102. */
  4103. asmlinkage long sys_sched_get_priority_max(int policy)
  4104. {
  4105. int ret = -EINVAL;
  4106. switch (policy) {
  4107. case SCHED_FIFO:
  4108. case SCHED_RR:
  4109. ret = MAX_USER_RT_PRIO-1;
  4110. break;
  4111. case SCHED_NORMAL:
  4112. case SCHED_BATCH:
  4113. case SCHED_IDLE:
  4114. ret = 0;
  4115. break;
  4116. }
  4117. return ret;
  4118. }
  4119. /**
  4120. * sys_sched_get_priority_min - return minimum RT priority.
  4121. * @policy: scheduling class.
  4122. *
  4123. * this syscall returns the minimum rt_priority that can be used
  4124. * by a given scheduling class.
  4125. */
  4126. asmlinkage long sys_sched_get_priority_min(int policy)
  4127. {
  4128. int ret = -EINVAL;
  4129. switch (policy) {
  4130. case SCHED_FIFO:
  4131. case SCHED_RR:
  4132. ret = 1;
  4133. break;
  4134. case SCHED_NORMAL:
  4135. case SCHED_BATCH:
  4136. case SCHED_IDLE:
  4137. ret = 0;
  4138. }
  4139. return ret;
  4140. }
  4141. /**
  4142. * sys_sched_rr_get_interval - return the default timeslice of a process.
  4143. * @pid: pid of the process.
  4144. * @interval: userspace pointer to the timeslice value.
  4145. *
  4146. * this syscall writes the default timeslice value of a given process
  4147. * into the user-space timespec buffer. A value of '0' means infinity.
  4148. */
  4149. asmlinkage
  4150. long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
  4151. {
  4152. struct task_struct *p;
  4153. unsigned int time_slice;
  4154. int retval;
  4155. struct timespec t;
  4156. if (pid < 0)
  4157. return -EINVAL;
  4158. retval = -ESRCH;
  4159. read_lock(&tasklist_lock);
  4160. p = find_process_by_pid(pid);
  4161. if (!p)
  4162. goto out_unlock;
  4163. retval = security_task_getscheduler(p);
  4164. if (retval)
  4165. goto out_unlock;
  4166. if (p->policy == SCHED_FIFO)
  4167. time_slice = 0;
  4168. else if (p->policy == SCHED_RR)
  4169. time_slice = DEF_TIMESLICE;
  4170. else {
  4171. struct sched_entity *se = &p->se;
  4172. unsigned long flags;
  4173. struct rq *rq;
  4174. rq = task_rq_lock(p, &flags);
  4175. time_slice = NS_TO_JIFFIES(sched_slice(cfs_rq_of(se), se));
  4176. task_rq_unlock(rq, &flags);
  4177. }
  4178. read_unlock(&tasklist_lock);
  4179. jiffies_to_timespec(time_slice, &t);
  4180. retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  4181. return retval;
  4182. out_unlock:
  4183. read_unlock(&tasklist_lock);
  4184. return retval;
  4185. }
  4186. static const char stat_nam[] = "RSDTtZX";
  4187. static void show_task(struct task_struct *p)
  4188. {
  4189. unsigned long free = 0;
  4190. unsigned state;
  4191. state = p->state ? __ffs(p->state) + 1 : 0;
  4192. printk(KERN_INFO "%-13.13s %c", p->comm,
  4193. state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
  4194. #if BITS_PER_LONG == 32
  4195. if (state == TASK_RUNNING)
  4196. printk(KERN_CONT " running ");
  4197. else
  4198. printk(KERN_CONT " %08lx ", thread_saved_pc(p));
  4199. #else
  4200. if (state == TASK_RUNNING)
  4201. printk(KERN_CONT " running task ");
  4202. else
  4203. printk(KERN_CONT " %016lx ", thread_saved_pc(p));
  4204. #endif
  4205. #ifdef CONFIG_DEBUG_STACK_USAGE
  4206. {
  4207. unsigned long *n = end_of_stack(p);
  4208. while (!*n)
  4209. n++;
  4210. free = (unsigned long)n - (unsigned long)end_of_stack(p);
  4211. }
  4212. #endif
  4213. printk(KERN_CONT "%5lu %5d %6d\n", free,
  4214. task_pid_nr(p), task_pid_nr(p->parent));
  4215. if (state != TASK_RUNNING)
  4216. show_stack(p, NULL);
  4217. }
  4218. void show_state_filter(unsigned long state_filter)
  4219. {
  4220. struct task_struct *g, *p;
  4221. #if BITS_PER_LONG == 32
  4222. printk(KERN_INFO
  4223. " task PC stack pid father\n");
  4224. #else
  4225. printk(KERN_INFO
  4226. " task PC stack pid father\n");
  4227. #endif
  4228. read_lock(&tasklist_lock);
  4229. do_each_thread(g, p) {
  4230. /*
  4231. * reset the NMI-timeout, listing all files on a slow
  4232. * console might take alot of time:
  4233. */
  4234. touch_nmi_watchdog();
  4235. if (!state_filter || (p->state & state_filter))
  4236. show_task(p);
  4237. } while_each_thread(g, p);
  4238. touch_all_softlockup_watchdogs();
  4239. #ifdef CONFIG_SCHED_DEBUG
  4240. sysrq_sched_debug_show();
  4241. #endif
  4242. read_unlock(&tasklist_lock);
  4243. /*
  4244. * Only show locks if all tasks are dumped:
  4245. */
  4246. if (state_filter == -1)
  4247. debug_show_all_locks();
  4248. }
  4249. void __cpuinit init_idle_bootup_task(struct task_struct *idle)
  4250. {
  4251. idle->sched_class = &idle_sched_class;
  4252. }
  4253. /**
  4254. * init_idle - set up an idle thread for a given CPU
  4255. * @idle: task in question
  4256. * @cpu: cpu the idle task belongs to
  4257. *
  4258. * NOTE: this function does not set the idle thread's NEED_RESCHED
  4259. * flag, to make booting more robust.
  4260. */
  4261. void __cpuinit init_idle(struct task_struct *idle, int cpu)
  4262. {
  4263. struct rq *rq = cpu_rq(cpu);
  4264. unsigned long flags;
  4265. __sched_fork(idle);
  4266. idle->se.exec_start = sched_clock();
  4267. idle->prio = idle->normal_prio = MAX_PRIO;
  4268. idle->cpus_allowed = cpumask_of_cpu(cpu);
  4269. __set_task_cpu(idle, cpu);
  4270. spin_lock_irqsave(&rq->lock, flags);
  4271. rq->curr = rq->idle = idle;
  4272. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  4273. idle->oncpu = 1;
  4274. #endif
  4275. spin_unlock_irqrestore(&rq->lock, flags);
  4276. /* Set the preempt count _outside_ the spinlocks! */
  4277. #if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL)
  4278. task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
  4279. #else
  4280. task_thread_info(idle)->preempt_count = 0;
  4281. #endif
  4282. /*
  4283. * The idle tasks have their own, simple scheduling class:
  4284. */
  4285. idle->sched_class = &idle_sched_class;
  4286. }
  4287. /*
  4288. * In a system that switches off the HZ timer nohz_cpu_mask
  4289. * indicates which cpus entered this state. This is used
  4290. * in the rcu update to wait only for active cpus. For system
  4291. * which do not switch off the HZ timer nohz_cpu_mask should
  4292. * always be CPU_MASK_NONE.
  4293. */
  4294. cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
  4295. #ifdef CONFIG_SMP
  4296. /*
  4297. * This is how migration works:
  4298. *
  4299. * 1) we queue a struct migration_req structure in the source CPU's
  4300. * runqueue and wake up that CPU's migration thread.
  4301. * 2) we down() the locked semaphore => thread blocks.
  4302. * 3) migration thread wakes up (implicitly it forces the migrated
  4303. * thread off the CPU)
  4304. * 4) it gets the migration request and checks whether the migrated
  4305. * task is still in the wrong runqueue.
  4306. * 5) if it's in the wrong runqueue then the migration thread removes
  4307. * it and puts it into the right queue.
  4308. * 6) migration thread up()s the semaphore.
  4309. * 7) we wake up and the migration is done.
  4310. */
  4311. /*
  4312. * Change a given task's CPU affinity. Migrate the thread to a
  4313. * proper CPU and schedule it away if the CPU it's executing on
  4314. * is removed from the allowed bitmask.
  4315. *
  4316. * NOTE: the caller must have a valid reference to the task, the
  4317. * task must not exit() & deallocate itself prematurely. The
  4318. * call is not atomic; no spinlocks may be held.
  4319. */
  4320. int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
  4321. {
  4322. struct migration_req req;
  4323. unsigned long flags;
  4324. struct rq *rq;
  4325. int ret = 0;
  4326. rq = task_rq_lock(p, &flags);
  4327. if (!cpus_intersects(new_mask, cpu_online_map)) {
  4328. ret = -EINVAL;
  4329. goto out;
  4330. }
  4331. p->cpus_allowed = new_mask;
  4332. /* Can the task run on the task's current CPU? If so, we're done */
  4333. if (cpu_isset(task_cpu(p), new_mask))
  4334. goto out;
  4335. if (migrate_task(p, any_online_cpu(new_mask), &req)) {
  4336. /* Need help from migration thread: drop lock and wait. */
  4337. task_rq_unlock(rq, &flags);
  4338. wake_up_process(rq->migration_thread);
  4339. wait_for_completion(&req.done);
  4340. tlb_migrate_finish(p->mm);
  4341. return 0;
  4342. }
  4343. out:
  4344. task_rq_unlock(rq, &flags);
  4345. return ret;
  4346. }
  4347. EXPORT_SYMBOL_GPL(set_cpus_allowed);
  4348. /*
  4349. * Move (not current) task off this cpu, onto dest cpu. We're doing
  4350. * this because either it can't run here any more (set_cpus_allowed()
  4351. * away from this CPU, or CPU going down), or because we're
  4352. * attempting to rebalance this task on exec (sched_exec).
  4353. *
  4354. * So we race with normal scheduler movements, but that's OK, as long
  4355. * as the task is no longer on this CPU.
  4356. *
  4357. * Returns non-zero if task was successfully migrated.
  4358. */
  4359. static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
  4360. {
  4361. struct rq *rq_dest, *rq_src;
  4362. int ret = 0, on_rq;
  4363. if (unlikely(cpu_is_offline(dest_cpu)))
  4364. return ret;
  4365. rq_src = cpu_rq(src_cpu);
  4366. rq_dest = cpu_rq(dest_cpu);
  4367. double_rq_lock(rq_src, rq_dest);
  4368. /* Already moved. */
  4369. if (task_cpu(p) != src_cpu)
  4370. goto out;
  4371. /* Affinity changed (again). */
  4372. if (!cpu_isset(dest_cpu, p->cpus_allowed))
  4373. goto out;
  4374. on_rq = p->se.on_rq;
  4375. if (on_rq)
  4376. deactivate_task(rq_src, p, 0);
  4377. set_task_cpu(p, dest_cpu);
  4378. if (on_rq) {
  4379. activate_task(rq_dest, p, 0);
  4380. check_preempt_curr(rq_dest, p);
  4381. }
  4382. ret = 1;
  4383. out:
  4384. double_rq_unlock(rq_src, rq_dest);
  4385. return ret;
  4386. }
  4387. /*
  4388. * migration_thread - this is a highprio system thread that performs
  4389. * thread migration by bumping thread off CPU then 'pushing' onto
  4390. * another runqueue.
  4391. */
  4392. static int migration_thread(void *data)
  4393. {
  4394. int cpu = (long)data;
  4395. struct rq *rq;
  4396. rq = cpu_rq(cpu);
  4397. BUG_ON(rq->migration_thread != current);
  4398. set_current_state(TASK_INTERRUPTIBLE);
  4399. while (!kthread_should_stop()) {
  4400. struct migration_req *req;
  4401. struct list_head *head;
  4402. spin_lock_irq(&rq->lock);
  4403. if (cpu_is_offline(cpu)) {
  4404. spin_unlock_irq(&rq->lock);
  4405. goto wait_to_die;
  4406. }
  4407. if (rq->active_balance) {
  4408. active_load_balance(rq, cpu);
  4409. rq->active_balance = 0;
  4410. }
  4411. head = &rq->migration_queue;
  4412. if (list_empty(head)) {
  4413. spin_unlock_irq(&rq->lock);
  4414. schedule();
  4415. set_current_state(TASK_INTERRUPTIBLE);
  4416. continue;
  4417. }
  4418. req = list_entry(head->next, struct migration_req, list);
  4419. list_del_init(head->next);
  4420. spin_unlock(&rq->lock);
  4421. __migrate_task(req->task, cpu, req->dest_cpu);
  4422. local_irq_enable();
  4423. complete(&req->done);
  4424. }
  4425. __set_current_state(TASK_RUNNING);
  4426. return 0;
  4427. wait_to_die:
  4428. /* Wait for kthread_stop */
  4429. set_current_state(TASK_INTERRUPTIBLE);
  4430. while (!kthread_should_stop()) {
  4431. schedule();
  4432. set_current_state(TASK_INTERRUPTIBLE);
  4433. }
  4434. __set_current_state(TASK_RUNNING);
  4435. return 0;
  4436. }
  4437. #ifdef CONFIG_HOTPLUG_CPU
  4438. static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
  4439. {
  4440. int ret;
  4441. local_irq_disable();
  4442. ret = __migrate_task(p, src_cpu, dest_cpu);
  4443. local_irq_enable();
  4444. return ret;
  4445. }
  4446. /*
  4447. * Figure out where task on dead CPU should go, use force if neccessary.
  4448. * NOTE: interrupts should be disabled by the caller
  4449. */
  4450. static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
  4451. {
  4452. unsigned long flags;
  4453. cpumask_t mask;
  4454. struct rq *rq;
  4455. int dest_cpu;
  4456. do {
  4457. /* On same node? */
  4458. mask = node_to_cpumask(cpu_to_node(dead_cpu));
  4459. cpus_and(mask, mask, p->cpus_allowed);
  4460. dest_cpu = any_online_cpu(mask);
  4461. /* On any allowed CPU? */
  4462. if (dest_cpu == NR_CPUS)
  4463. dest_cpu = any_online_cpu(p->cpus_allowed);
  4464. /* No more Mr. Nice Guy. */
  4465. if (dest_cpu == NR_CPUS) {
  4466. rq = task_rq_lock(p, &flags);
  4467. cpus_setall(p->cpus_allowed);
  4468. dest_cpu = any_online_cpu(p->cpus_allowed);
  4469. task_rq_unlock(rq, &flags);
  4470. /*
  4471. * Don't tell them about moving exiting tasks or
  4472. * kernel threads (both mm NULL), since they never
  4473. * leave kernel.
  4474. */
  4475. if (p->mm && printk_ratelimit())
  4476. printk(KERN_INFO "process %d (%s) no "
  4477. "longer affine to cpu%d\n",
  4478. task_pid_nr(p), p->comm, dead_cpu);
  4479. }
  4480. } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
  4481. }
  4482. /*
  4483. * While a dead CPU has no uninterruptible tasks queued at this point,
  4484. * it might still have a nonzero ->nr_uninterruptible counter, because
  4485. * for performance reasons the counter is not stricly tracking tasks to
  4486. * their home CPUs. So we just add the counter to another CPU's counter,
  4487. * to keep the global sum constant after CPU-down:
  4488. */
  4489. static void migrate_nr_uninterruptible(struct rq *rq_src)
  4490. {
  4491. struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
  4492. unsigned long flags;
  4493. local_irq_save(flags);
  4494. double_rq_lock(rq_src, rq_dest);
  4495. rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
  4496. rq_src->nr_uninterruptible = 0;
  4497. double_rq_unlock(rq_src, rq_dest);
  4498. local_irq_restore(flags);
  4499. }
  4500. /* Run through task list and migrate tasks from the dead cpu. */
  4501. static void migrate_live_tasks(int src_cpu)
  4502. {
  4503. struct task_struct *p, *t;
  4504. read_lock(&tasklist_lock);
  4505. do_each_thread(t, p) {
  4506. if (p == current)
  4507. continue;
  4508. if (task_cpu(p) == src_cpu)
  4509. move_task_off_dead_cpu(src_cpu, p);
  4510. } while_each_thread(t, p);
  4511. read_unlock(&tasklist_lock);
  4512. }
  4513. /*
  4514. * activate_idle_task - move idle task to the _front_ of runqueue.
  4515. */
  4516. static void activate_idle_task(struct task_struct *p, struct rq *rq)
  4517. {
  4518. update_rq_clock(rq);
  4519. if (p->state == TASK_UNINTERRUPTIBLE)
  4520. rq->nr_uninterruptible--;
  4521. enqueue_task(rq, p, 0);
  4522. inc_nr_running(p, rq);
  4523. }
  4524. /*
  4525. * Schedules idle task to be the next runnable task on current CPU.
  4526. * It does so by boosting its priority to highest possible and adding it to
  4527. * the _front_ of the runqueue. Used by CPU offline code.
  4528. */
  4529. void sched_idle_next(void)
  4530. {
  4531. int this_cpu = smp_processor_id();
  4532. struct rq *rq = cpu_rq(this_cpu);
  4533. struct task_struct *p = rq->idle;
  4534. unsigned long flags;
  4535. /* cpu has to be offline */
  4536. BUG_ON(cpu_online(this_cpu));
  4537. /*
  4538. * Strictly not necessary since rest of the CPUs are stopped by now
  4539. * and interrupts disabled on the current cpu.
  4540. */
  4541. spin_lock_irqsave(&rq->lock, flags);
  4542. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  4543. /* Add idle task to the _front_ of its priority queue: */
  4544. activate_idle_task(p, rq);
  4545. spin_unlock_irqrestore(&rq->lock, flags);
  4546. }
  4547. /*
  4548. * Ensures that the idle task is using init_mm right before its cpu goes
  4549. * offline.
  4550. */
  4551. void idle_task_exit(void)
  4552. {
  4553. struct mm_struct *mm = current->active_mm;
  4554. BUG_ON(cpu_online(smp_processor_id()));
  4555. if (mm != &init_mm)
  4556. switch_mm(mm, &init_mm, current);
  4557. mmdrop(mm);
  4558. }
  4559. /* called under rq->lock with disabled interrupts */
  4560. static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
  4561. {
  4562. struct rq *rq = cpu_rq(dead_cpu);
  4563. /* Must be exiting, otherwise would be on tasklist. */
  4564. BUG_ON(!p->exit_state);
  4565. /* Cannot have done final schedule yet: would have vanished. */
  4566. BUG_ON(p->state == TASK_DEAD);
  4567. get_task_struct(p);
  4568. /*
  4569. * Drop lock around migration; if someone else moves it,
  4570. * that's OK. No task can be added to this CPU, so iteration is
  4571. * fine.
  4572. */
  4573. spin_unlock_irq(&rq->lock);
  4574. move_task_off_dead_cpu(dead_cpu, p);
  4575. spin_lock_irq(&rq->lock);
  4576. put_task_struct(p);
  4577. }
  4578. /* release_task() removes task from tasklist, so we won't find dead tasks. */
  4579. static void migrate_dead_tasks(unsigned int dead_cpu)
  4580. {
  4581. struct rq *rq = cpu_rq(dead_cpu);
  4582. struct task_struct *next;
  4583. for ( ; ; ) {
  4584. if (!rq->nr_running)
  4585. break;
  4586. update_rq_clock(rq);
  4587. next = pick_next_task(rq, rq->curr);
  4588. if (!next)
  4589. break;
  4590. migrate_dead(dead_cpu, next);
  4591. }
  4592. }
  4593. #endif /* CONFIG_HOTPLUG_CPU */
  4594. #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
  4595. static struct ctl_table sd_ctl_dir[] = {
  4596. {
  4597. .procname = "sched_domain",
  4598. .mode = 0555,
  4599. },
  4600. {0,},
  4601. };
  4602. static struct ctl_table sd_ctl_root[] = {
  4603. {
  4604. .ctl_name = CTL_KERN,
  4605. .procname = "kernel",
  4606. .mode = 0555,
  4607. .child = sd_ctl_dir,
  4608. },
  4609. {0,},
  4610. };
  4611. static struct ctl_table *sd_alloc_ctl_entry(int n)
  4612. {
  4613. struct ctl_table *entry =
  4614. kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
  4615. return entry;
  4616. }
  4617. static void sd_free_ctl_entry(struct ctl_table **tablep)
  4618. {
  4619. struct ctl_table *entry;
  4620. /*
  4621. * In the intermediate directories, both the child directory and
  4622. * procname are dynamically allocated and could fail but the mode
  4623. * will always be set. In the lowest directory the names are
  4624. * static strings and all have proc handlers.
  4625. */
  4626. for (entry = *tablep; entry->mode; entry++) {
  4627. if (entry->child)
  4628. sd_free_ctl_entry(&entry->child);
  4629. if (entry->proc_handler == NULL)
  4630. kfree(entry->procname);
  4631. }
  4632. kfree(*tablep);
  4633. *tablep = NULL;
  4634. }
  4635. static void
  4636. set_table_entry(struct ctl_table *entry,
  4637. const char *procname, void *data, int maxlen,
  4638. mode_t mode, proc_handler *proc_handler)
  4639. {
  4640. entry->procname = procname;
  4641. entry->data = data;
  4642. entry->maxlen = maxlen;
  4643. entry->mode = mode;
  4644. entry->proc_handler = proc_handler;
  4645. }
  4646. static struct ctl_table *
  4647. sd_alloc_ctl_domain_table(struct sched_domain *sd)
  4648. {
  4649. struct ctl_table *table = sd_alloc_ctl_entry(12);
  4650. if (table == NULL)
  4651. return NULL;
  4652. set_table_entry(&table[0], "min_interval", &sd->min_interval,
  4653. sizeof(long), 0644, proc_doulongvec_minmax);
  4654. set_table_entry(&table[1], "max_interval", &sd->max_interval,
  4655. sizeof(long), 0644, proc_doulongvec_minmax);
  4656. set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
  4657. sizeof(int), 0644, proc_dointvec_minmax);
  4658. set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
  4659. sizeof(int), 0644, proc_dointvec_minmax);
  4660. set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
  4661. sizeof(int), 0644, proc_dointvec_minmax);
  4662. set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
  4663. sizeof(int), 0644, proc_dointvec_minmax);
  4664. set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
  4665. sizeof(int), 0644, proc_dointvec_minmax);
  4666. set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
  4667. sizeof(int), 0644, proc_dointvec_minmax);
  4668. set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
  4669. sizeof(int), 0644, proc_dointvec_minmax);
  4670. set_table_entry(&table[9], "cache_nice_tries",
  4671. &sd->cache_nice_tries,
  4672. sizeof(int), 0644, proc_dointvec_minmax);
  4673. set_table_entry(&table[10], "flags", &sd->flags,
  4674. sizeof(int), 0644, proc_dointvec_minmax);
  4675. /* &table[11] is terminator */
  4676. return table;
  4677. }
  4678. static ctl_table * sd_alloc_ctl_cpu_table(int cpu)
  4679. {
  4680. struct ctl_table *entry, *table;
  4681. struct sched_domain *sd;
  4682. int domain_num = 0, i;
  4683. char buf[32];
  4684. for_each_domain(cpu, sd)
  4685. domain_num++;
  4686. entry = table = sd_alloc_ctl_entry(domain_num + 1);
  4687. if (table == NULL)
  4688. return NULL;
  4689. i = 0;
  4690. for_each_domain(cpu, sd) {
  4691. snprintf(buf, 32, "domain%d", i);
  4692. entry->procname = kstrdup(buf, GFP_KERNEL);
  4693. entry->mode = 0555;
  4694. entry->child = sd_alloc_ctl_domain_table(sd);
  4695. entry++;
  4696. i++;
  4697. }
  4698. return table;
  4699. }
  4700. static struct ctl_table_header *sd_sysctl_header;
  4701. static void register_sched_domain_sysctl(void)
  4702. {
  4703. int i, cpu_num = num_online_cpus();
  4704. struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
  4705. char buf[32];
  4706. if (entry == NULL)
  4707. return;
  4708. sd_ctl_dir[0].child = entry;
  4709. for_each_online_cpu(i) {
  4710. snprintf(buf, 32, "cpu%d", i);
  4711. entry->procname = kstrdup(buf, GFP_KERNEL);
  4712. entry->mode = 0555;
  4713. entry->child = sd_alloc_ctl_cpu_table(i);
  4714. entry++;
  4715. }
  4716. sd_sysctl_header = register_sysctl_table(sd_ctl_root);
  4717. }
  4718. static void unregister_sched_domain_sysctl(void)
  4719. {
  4720. unregister_sysctl_table(sd_sysctl_header);
  4721. sd_sysctl_header = NULL;
  4722. sd_free_ctl_entry(&sd_ctl_dir[0].child);
  4723. }
  4724. #else
  4725. static void register_sched_domain_sysctl(void)
  4726. {
  4727. }
  4728. static void unregister_sched_domain_sysctl(void)
  4729. {
  4730. }
  4731. #endif
  4732. /*
  4733. * migration_call - callback that gets triggered when a CPU is added.
  4734. * Here we can start up the necessary migration thread for the new CPU.
  4735. */
  4736. static int __cpuinit
  4737. migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  4738. {
  4739. struct task_struct *p;
  4740. int cpu = (long)hcpu;
  4741. unsigned long flags;
  4742. struct rq *rq;
  4743. switch (action) {
  4744. case CPU_LOCK_ACQUIRE:
  4745. mutex_lock(&sched_hotcpu_mutex);
  4746. break;
  4747. case CPU_UP_PREPARE:
  4748. case CPU_UP_PREPARE_FROZEN:
  4749. p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
  4750. if (IS_ERR(p))
  4751. return NOTIFY_BAD;
  4752. kthread_bind(p, cpu);
  4753. /* Must be high prio: stop_machine expects to yield to it. */
  4754. rq = task_rq_lock(p, &flags);
  4755. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  4756. task_rq_unlock(rq, &flags);
  4757. cpu_rq(cpu)->migration_thread = p;
  4758. break;
  4759. case CPU_ONLINE:
  4760. case CPU_ONLINE_FROZEN:
  4761. /* Strictly unneccessary, as first user will wake it. */
  4762. wake_up_process(cpu_rq(cpu)->migration_thread);
  4763. break;
  4764. #ifdef CONFIG_HOTPLUG_CPU
  4765. case CPU_UP_CANCELED:
  4766. case CPU_UP_CANCELED_FROZEN:
  4767. if (!cpu_rq(cpu)->migration_thread)
  4768. break;
  4769. /* Unbind it from offline cpu so it can run. Fall thru. */
  4770. kthread_bind(cpu_rq(cpu)->migration_thread,
  4771. any_online_cpu(cpu_online_map));
  4772. kthread_stop(cpu_rq(cpu)->migration_thread);
  4773. cpu_rq(cpu)->migration_thread = NULL;
  4774. break;
  4775. case CPU_DEAD:
  4776. case CPU_DEAD_FROZEN:
  4777. migrate_live_tasks(cpu);
  4778. rq = cpu_rq(cpu);
  4779. kthread_stop(rq->migration_thread);
  4780. rq->migration_thread = NULL;
  4781. /* Idle task back to normal (off runqueue, low prio) */
  4782. spin_lock_irq(&rq->lock);
  4783. update_rq_clock(rq);
  4784. deactivate_task(rq, rq->idle, 0);
  4785. rq->idle->static_prio = MAX_PRIO;
  4786. __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
  4787. rq->idle->sched_class = &idle_sched_class;
  4788. migrate_dead_tasks(cpu);
  4789. spin_unlock_irq(&rq->lock);
  4790. migrate_nr_uninterruptible(rq);
  4791. BUG_ON(rq->nr_running != 0);
  4792. /* No need to migrate the tasks: it was best-effort if
  4793. * they didn't take sched_hotcpu_mutex. Just wake up
  4794. * the requestors. */
  4795. spin_lock_irq(&rq->lock);
  4796. while (!list_empty(&rq->migration_queue)) {
  4797. struct migration_req *req;
  4798. req = list_entry(rq->migration_queue.next,
  4799. struct migration_req, list);
  4800. list_del_init(&req->list);
  4801. complete(&req->done);
  4802. }
  4803. spin_unlock_irq(&rq->lock);
  4804. break;
  4805. #endif
  4806. case CPU_LOCK_RELEASE:
  4807. mutex_unlock(&sched_hotcpu_mutex);
  4808. break;
  4809. }
  4810. return NOTIFY_OK;
  4811. }
  4812. /* Register at highest priority so that task migration (migrate_all_tasks)
  4813. * happens before everything else.
  4814. */
  4815. static struct notifier_block __cpuinitdata migration_notifier = {
  4816. .notifier_call = migration_call,
  4817. .priority = 10
  4818. };
  4819. int __init migration_init(void)
  4820. {
  4821. void *cpu = (void *)(long)smp_processor_id();
  4822. int err;
  4823. /* Start one for the boot CPU: */
  4824. err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  4825. BUG_ON(err == NOTIFY_BAD);
  4826. migration_call(&migration_notifier, CPU_ONLINE, cpu);
  4827. register_cpu_notifier(&migration_notifier);
  4828. return 0;
  4829. }
  4830. #endif
  4831. #ifdef CONFIG_SMP
  4832. /* Number of possible processor ids */
  4833. int nr_cpu_ids __read_mostly = NR_CPUS;
  4834. EXPORT_SYMBOL(nr_cpu_ids);
  4835. #ifdef CONFIG_SCHED_DEBUG
  4836. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  4837. {
  4838. int level = 0;
  4839. if (!sd) {
  4840. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  4841. return;
  4842. }
  4843. printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  4844. do {
  4845. int i;
  4846. char str[NR_CPUS];
  4847. struct sched_group *group = sd->groups;
  4848. cpumask_t groupmask;
  4849. cpumask_scnprintf(str, NR_CPUS, sd->span);
  4850. cpus_clear(groupmask);
  4851. printk(KERN_DEBUG);
  4852. for (i = 0; i < level + 1; i++)
  4853. printk(" ");
  4854. printk("domain %d: ", level);
  4855. if (!(sd->flags & SD_LOAD_BALANCE)) {
  4856. printk("does not load-balance\n");
  4857. if (sd->parent)
  4858. printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
  4859. " has parent");
  4860. break;
  4861. }
  4862. printk("span %s\n", str);
  4863. if (!cpu_isset(cpu, sd->span))
  4864. printk(KERN_ERR "ERROR: domain->span does not contain "
  4865. "CPU%d\n", cpu);
  4866. if (!cpu_isset(cpu, group->cpumask))
  4867. printk(KERN_ERR "ERROR: domain->groups does not contain"
  4868. " CPU%d\n", cpu);
  4869. printk(KERN_DEBUG);
  4870. for (i = 0; i < level + 2; i++)
  4871. printk(" ");
  4872. printk("groups:");
  4873. do {
  4874. if (!group) {
  4875. printk("\n");
  4876. printk(KERN_ERR "ERROR: group is NULL\n");
  4877. break;
  4878. }
  4879. if (!group->__cpu_power) {
  4880. printk(KERN_CONT "\n");
  4881. printk(KERN_ERR "ERROR: domain->cpu_power not "
  4882. "set\n");
  4883. break;
  4884. }
  4885. if (!cpus_weight(group->cpumask)) {
  4886. printk(KERN_CONT "\n");
  4887. printk(KERN_ERR "ERROR: empty group\n");
  4888. break;
  4889. }
  4890. if (cpus_intersects(groupmask, group->cpumask)) {
  4891. printk(KERN_CONT "\n");
  4892. printk(KERN_ERR "ERROR: repeated CPUs\n");
  4893. break;
  4894. }
  4895. cpus_or(groupmask, groupmask, group->cpumask);
  4896. cpumask_scnprintf(str, NR_CPUS, group->cpumask);
  4897. printk(KERN_CONT " %s", str);
  4898. group = group->next;
  4899. } while (group != sd->groups);
  4900. printk(KERN_CONT "\n");
  4901. if (!cpus_equal(sd->span, groupmask))
  4902. printk(KERN_ERR "ERROR: groups don't span "
  4903. "domain->span\n");
  4904. level++;
  4905. sd = sd->parent;
  4906. if (!sd)
  4907. continue;
  4908. if (!cpus_subset(groupmask, sd->span))
  4909. printk(KERN_ERR "ERROR: parent span is not a superset "
  4910. "of domain->span\n");
  4911. } while (sd);
  4912. }
  4913. #else
  4914. # define sched_domain_debug(sd, cpu) do { } while (0)
  4915. #endif
  4916. static int sd_degenerate(struct sched_domain *sd)
  4917. {
  4918. if (cpus_weight(sd->span) == 1)
  4919. return 1;
  4920. /* Following flags need at least 2 groups */
  4921. if (sd->flags & (SD_LOAD_BALANCE |
  4922. SD_BALANCE_NEWIDLE |
  4923. SD_BALANCE_FORK |
  4924. SD_BALANCE_EXEC |
  4925. SD_SHARE_CPUPOWER |
  4926. SD_SHARE_PKG_RESOURCES)) {
  4927. if (sd->groups != sd->groups->next)
  4928. return 0;
  4929. }
  4930. /* Following flags don't use groups */
  4931. if (sd->flags & (SD_WAKE_IDLE |
  4932. SD_WAKE_AFFINE |
  4933. SD_WAKE_BALANCE))
  4934. return 0;
  4935. return 1;
  4936. }
  4937. static int
  4938. sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  4939. {
  4940. unsigned long cflags = sd->flags, pflags = parent->flags;
  4941. if (sd_degenerate(parent))
  4942. return 1;
  4943. if (!cpus_equal(sd->span, parent->span))
  4944. return 0;
  4945. /* Does parent contain flags not in child? */
  4946. /* WAKE_BALANCE is a subset of WAKE_AFFINE */
  4947. if (cflags & SD_WAKE_AFFINE)
  4948. pflags &= ~SD_WAKE_BALANCE;
  4949. /* Flags needing groups don't count if only 1 group in parent */
  4950. if (parent->groups == parent->groups->next) {
  4951. pflags &= ~(SD_LOAD_BALANCE |
  4952. SD_BALANCE_NEWIDLE |
  4953. SD_BALANCE_FORK |
  4954. SD_BALANCE_EXEC |
  4955. SD_SHARE_CPUPOWER |
  4956. SD_SHARE_PKG_RESOURCES);
  4957. }
  4958. if (~cflags & pflags)
  4959. return 0;
  4960. return 1;
  4961. }
  4962. /*
  4963. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  4964. * hold the hotplug lock.
  4965. */
  4966. static void cpu_attach_domain(struct sched_domain *sd, int cpu)
  4967. {
  4968. struct rq *rq = cpu_rq(cpu);
  4969. struct sched_domain *tmp;
  4970. /* Remove the sched domains which do not contribute to scheduling. */
  4971. for (tmp = sd; tmp; tmp = tmp->parent) {
  4972. struct sched_domain *parent = tmp->parent;
  4973. if (!parent)
  4974. break;
  4975. if (sd_parent_degenerate(tmp, parent)) {
  4976. tmp->parent = parent->parent;
  4977. if (parent->parent)
  4978. parent->parent->child = tmp;
  4979. }
  4980. }
  4981. if (sd && sd_degenerate(sd)) {
  4982. sd = sd->parent;
  4983. if (sd)
  4984. sd->child = NULL;
  4985. }
  4986. sched_domain_debug(sd, cpu);
  4987. rcu_assign_pointer(rq->sd, sd);
  4988. }
  4989. /* cpus with isolated domains */
  4990. static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
  4991. /* Setup the mask of cpus configured for isolated domains */
  4992. static int __init isolated_cpu_setup(char *str)
  4993. {
  4994. int ints[NR_CPUS], i;
  4995. str = get_options(str, ARRAY_SIZE(ints), ints);
  4996. cpus_clear(cpu_isolated_map);
  4997. for (i = 1; i <= ints[0]; i++)
  4998. if (ints[i] < NR_CPUS)
  4999. cpu_set(ints[i], cpu_isolated_map);
  5000. return 1;
  5001. }
  5002. __setup("isolcpus=", isolated_cpu_setup);
  5003. /*
  5004. * init_sched_build_groups takes the cpumask we wish to span, and a pointer
  5005. * to a function which identifies what group(along with sched group) a CPU
  5006. * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
  5007. * (due to the fact that we keep track of groups covered with a cpumask_t).
  5008. *
  5009. * init_sched_build_groups will build a circular linked list of the groups
  5010. * covered by the given span, and will set each group's ->cpumask correctly,
  5011. * and ->cpu_power to 0.
  5012. */
  5013. static void
  5014. init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
  5015. int (*group_fn)(int cpu, const cpumask_t *cpu_map,
  5016. struct sched_group **sg))
  5017. {
  5018. struct sched_group *first = NULL, *last = NULL;
  5019. cpumask_t covered = CPU_MASK_NONE;
  5020. int i;
  5021. for_each_cpu_mask(i, span) {
  5022. struct sched_group *sg;
  5023. int group = group_fn(i, cpu_map, &sg);
  5024. int j;
  5025. if (cpu_isset(i, covered))
  5026. continue;
  5027. sg->cpumask = CPU_MASK_NONE;
  5028. sg->__cpu_power = 0;
  5029. for_each_cpu_mask(j, span) {
  5030. if (group_fn(j, cpu_map, NULL) != group)
  5031. continue;
  5032. cpu_set(j, covered);
  5033. cpu_set(j, sg->cpumask);
  5034. }
  5035. if (!first)
  5036. first = sg;
  5037. if (last)
  5038. last->next = sg;
  5039. last = sg;
  5040. }
  5041. last->next = first;
  5042. }
  5043. #define SD_NODES_PER_DOMAIN 16
  5044. #ifdef CONFIG_NUMA
  5045. /**
  5046. * find_next_best_node - find the next node to include in a sched_domain
  5047. * @node: node whose sched_domain we're building
  5048. * @used_nodes: nodes already in the sched_domain
  5049. *
  5050. * Find the next node to include in a given scheduling domain. Simply
  5051. * finds the closest node not already in the @used_nodes map.
  5052. *
  5053. * Should use nodemask_t.
  5054. */
  5055. static int find_next_best_node(int node, unsigned long *used_nodes)
  5056. {
  5057. int i, n, val, min_val, best_node = 0;
  5058. min_val = INT_MAX;
  5059. for (i = 0; i < MAX_NUMNODES; i++) {
  5060. /* Start at @node */
  5061. n = (node + i) % MAX_NUMNODES;
  5062. if (!nr_cpus_node(n))
  5063. continue;
  5064. /* Skip already used nodes */
  5065. if (test_bit(n, used_nodes))
  5066. continue;
  5067. /* Simple min distance search */
  5068. val = node_distance(node, n);
  5069. if (val < min_val) {
  5070. min_val = val;
  5071. best_node = n;
  5072. }
  5073. }
  5074. set_bit(best_node, used_nodes);
  5075. return best_node;
  5076. }
  5077. /**
  5078. * sched_domain_node_span - get a cpumask for a node's sched_domain
  5079. * @node: node whose cpumask we're constructing
  5080. * @size: number of nodes to include in this span
  5081. *
  5082. * Given a node, construct a good cpumask for its sched_domain to span. It
  5083. * should be one that prevents unnecessary balancing, but also spreads tasks
  5084. * out optimally.
  5085. */
  5086. static cpumask_t sched_domain_node_span(int node)
  5087. {
  5088. DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
  5089. cpumask_t span, nodemask;
  5090. int i;
  5091. cpus_clear(span);
  5092. bitmap_zero(used_nodes, MAX_NUMNODES);
  5093. nodemask = node_to_cpumask(node);
  5094. cpus_or(span, span, nodemask);
  5095. set_bit(node, used_nodes);
  5096. for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
  5097. int next_node = find_next_best_node(node, used_nodes);
  5098. nodemask = node_to_cpumask(next_node);
  5099. cpus_or(span, span, nodemask);
  5100. }
  5101. return span;
  5102. }
  5103. #endif
  5104. int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
  5105. /*
  5106. * SMT sched-domains:
  5107. */
  5108. #ifdef CONFIG_SCHED_SMT
  5109. static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
  5110. static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
  5111. static int cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map,
  5112. struct sched_group **sg)
  5113. {
  5114. if (sg)
  5115. *sg = &per_cpu(sched_group_cpus, cpu);
  5116. return cpu;
  5117. }
  5118. #endif
  5119. /*
  5120. * multi-core sched-domains:
  5121. */
  5122. #ifdef CONFIG_SCHED_MC
  5123. static DEFINE_PER_CPU(struct sched_domain, core_domains);
  5124. static DEFINE_PER_CPU(struct sched_group, sched_group_core);
  5125. #endif
  5126. #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
  5127. static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
  5128. struct sched_group **sg)
  5129. {
  5130. int group;
  5131. cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
  5132. cpus_and(mask, mask, *cpu_map);
  5133. group = first_cpu(mask);
  5134. if (sg)
  5135. *sg = &per_cpu(sched_group_core, group);
  5136. return group;
  5137. }
  5138. #elif defined(CONFIG_SCHED_MC)
  5139. static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
  5140. struct sched_group **sg)
  5141. {
  5142. if (sg)
  5143. *sg = &per_cpu(sched_group_core, cpu);
  5144. return cpu;
  5145. }
  5146. #endif
  5147. static DEFINE_PER_CPU(struct sched_domain, phys_domains);
  5148. static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
  5149. static int cpu_to_phys_group(int cpu, const cpumask_t *cpu_map,
  5150. struct sched_group **sg)
  5151. {
  5152. int group;
  5153. #ifdef CONFIG_SCHED_MC
  5154. cpumask_t mask = cpu_coregroup_map(cpu);
  5155. cpus_and(mask, mask, *cpu_map);
  5156. group = first_cpu(mask);
  5157. #elif defined(CONFIG_SCHED_SMT)
  5158. cpumask_t mask = per_cpu(cpu_sibling_map, cpu);
  5159. cpus_and(mask, mask, *cpu_map);
  5160. group = first_cpu(mask);
  5161. #else
  5162. group = cpu;
  5163. #endif
  5164. if (sg)
  5165. *sg = &per_cpu(sched_group_phys, group);
  5166. return group;
  5167. }
  5168. #ifdef CONFIG_NUMA
  5169. /*
  5170. * The init_sched_build_groups can't handle what we want to do with node
  5171. * groups, so roll our own. Now each node has its own list of groups which
  5172. * gets dynamically allocated.
  5173. */
  5174. static DEFINE_PER_CPU(struct sched_domain, node_domains);
  5175. static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
  5176. static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
  5177. static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
  5178. static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
  5179. struct sched_group **sg)
  5180. {
  5181. cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
  5182. int group;
  5183. cpus_and(nodemask, nodemask, *cpu_map);
  5184. group = first_cpu(nodemask);
  5185. if (sg)
  5186. *sg = &per_cpu(sched_group_allnodes, group);
  5187. return group;
  5188. }
  5189. static void init_numa_sched_groups_power(struct sched_group *group_head)
  5190. {
  5191. struct sched_group *sg = group_head;
  5192. int j;
  5193. if (!sg)
  5194. return;
  5195. do {
  5196. for_each_cpu_mask(j, sg->cpumask) {
  5197. struct sched_domain *sd;
  5198. sd = &per_cpu(phys_domains, j);
  5199. if (j != first_cpu(sd->groups->cpumask)) {
  5200. /*
  5201. * Only add "power" once for each
  5202. * physical package.
  5203. */
  5204. continue;
  5205. }
  5206. sg_inc_cpu_power(sg, sd->groups->__cpu_power);
  5207. }
  5208. sg = sg->next;
  5209. } while (sg != group_head);
  5210. }
  5211. #endif
  5212. #ifdef CONFIG_NUMA
  5213. /* Free memory allocated for various sched_group structures */
  5214. static void free_sched_groups(const cpumask_t *cpu_map)
  5215. {
  5216. int cpu, i;
  5217. for_each_cpu_mask(cpu, *cpu_map) {
  5218. struct sched_group **sched_group_nodes
  5219. = sched_group_nodes_bycpu[cpu];
  5220. if (!sched_group_nodes)
  5221. continue;
  5222. for (i = 0; i < MAX_NUMNODES; i++) {
  5223. cpumask_t nodemask = node_to_cpumask(i);
  5224. struct sched_group *oldsg, *sg = sched_group_nodes[i];
  5225. cpus_and(nodemask, nodemask, *cpu_map);
  5226. if (cpus_empty(nodemask))
  5227. continue;
  5228. if (sg == NULL)
  5229. continue;
  5230. sg = sg->next;
  5231. next_sg:
  5232. oldsg = sg;
  5233. sg = sg->next;
  5234. kfree(oldsg);
  5235. if (oldsg != sched_group_nodes[i])
  5236. goto next_sg;
  5237. }
  5238. kfree(sched_group_nodes);
  5239. sched_group_nodes_bycpu[cpu] = NULL;
  5240. }
  5241. }
  5242. #else
  5243. static void free_sched_groups(const cpumask_t *cpu_map)
  5244. {
  5245. }
  5246. #endif
  5247. /*
  5248. * Initialize sched groups cpu_power.
  5249. *
  5250. * cpu_power indicates the capacity of sched group, which is used while
  5251. * distributing the load between different sched groups in a sched domain.
  5252. * Typically cpu_power for all the groups in a sched domain will be same unless
  5253. * there are asymmetries in the topology. If there are asymmetries, group
  5254. * having more cpu_power will pickup more load compared to the group having
  5255. * less cpu_power.
  5256. *
  5257. * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
  5258. * the maximum number of tasks a group can handle in the presence of other idle
  5259. * or lightly loaded groups in the same sched domain.
  5260. */
  5261. static void init_sched_groups_power(int cpu, struct sched_domain *sd)
  5262. {
  5263. struct sched_domain *child;
  5264. struct sched_group *group;
  5265. WARN_ON(!sd || !sd->groups);
  5266. if (cpu != first_cpu(sd->groups->cpumask))
  5267. return;
  5268. child = sd->child;
  5269. sd->groups->__cpu_power = 0;
  5270. /*
  5271. * For perf policy, if the groups in child domain share resources
  5272. * (for example cores sharing some portions of the cache hierarchy
  5273. * or SMT), then set this domain groups cpu_power such that each group
  5274. * can handle only one task, when there are other idle groups in the
  5275. * same sched domain.
  5276. */
  5277. if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
  5278. (child->flags &
  5279. (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
  5280. sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
  5281. return;
  5282. }
  5283. /*
  5284. * add cpu_power of each child group to this groups cpu_power
  5285. */
  5286. group = child->groups;
  5287. do {
  5288. sg_inc_cpu_power(sd->groups, group->__cpu_power);
  5289. group = group->next;
  5290. } while (group != child->groups);
  5291. }
  5292. /*
  5293. * Build sched domains for a given set of cpus and attach the sched domains
  5294. * to the individual cpus
  5295. */
  5296. static int build_sched_domains(const cpumask_t *cpu_map)
  5297. {
  5298. int i;
  5299. #ifdef CONFIG_NUMA
  5300. struct sched_group **sched_group_nodes = NULL;
  5301. int sd_allnodes = 0;
  5302. /*
  5303. * Allocate the per-node list of sched groups
  5304. */
  5305. sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
  5306. GFP_KERNEL);
  5307. if (!sched_group_nodes) {
  5308. printk(KERN_WARNING "Can not alloc sched group node list\n");
  5309. return -ENOMEM;
  5310. }
  5311. sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
  5312. #endif
  5313. /*
  5314. * Set up domains for cpus specified by the cpu_map.
  5315. */
  5316. for_each_cpu_mask(i, *cpu_map) {
  5317. struct sched_domain *sd = NULL, *p;
  5318. cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
  5319. cpus_and(nodemask, nodemask, *cpu_map);
  5320. #ifdef CONFIG_NUMA
  5321. if (cpus_weight(*cpu_map) >
  5322. SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
  5323. sd = &per_cpu(allnodes_domains, i);
  5324. *sd = SD_ALLNODES_INIT;
  5325. sd->span = *cpu_map;
  5326. cpu_to_allnodes_group(i, cpu_map, &sd->groups);
  5327. p = sd;
  5328. sd_allnodes = 1;
  5329. } else
  5330. p = NULL;
  5331. sd = &per_cpu(node_domains, i);
  5332. *sd = SD_NODE_INIT;
  5333. sd->span = sched_domain_node_span(cpu_to_node(i));
  5334. sd->parent = p;
  5335. if (p)
  5336. p->child = sd;
  5337. cpus_and(sd->span, sd->span, *cpu_map);
  5338. #endif
  5339. p = sd;
  5340. sd = &per_cpu(phys_domains, i);
  5341. *sd = SD_CPU_INIT;
  5342. sd->span = nodemask;
  5343. sd->parent = p;
  5344. if (p)
  5345. p->child = sd;
  5346. cpu_to_phys_group(i, cpu_map, &sd->groups);
  5347. #ifdef CONFIG_SCHED_MC
  5348. p = sd;
  5349. sd = &per_cpu(core_domains, i);
  5350. *sd = SD_MC_INIT;
  5351. sd->span = cpu_coregroup_map(i);
  5352. cpus_and(sd->span, sd->span, *cpu_map);
  5353. sd->parent = p;
  5354. p->child = sd;
  5355. cpu_to_core_group(i, cpu_map, &sd->groups);
  5356. #endif
  5357. #ifdef CONFIG_SCHED_SMT
  5358. p = sd;
  5359. sd = &per_cpu(cpu_domains, i);
  5360. *sd = SD_SIBLING_INIT;
  5361. sd->span = per_cpu(cpu_sibling_map, i);
  5362. cpus_and(sd->span, sd->span, *cpu_map);
  5363. sd->parent = p;
  5364. p->child = sd;
  5365. cpu_to_cpu_group(i, cpu_map, &sd->groups);
  5366. #endif
  5367. }
  5368. #ifdef CONFIG_SCHED_SMT
  5369. /* Set up CPU (sibling) groups */
  5370. for_each_cpu_mask(i, *cpu_map) {
  5371. cpumask_t this_sibling_map = per_cpu(cpu_sibling_map, i);
  5372. cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
  5373. if (i != first_cpu(this_sibling_map))
  5374. continue;
  5375. init_sched_build_groups(this_sibling_map, cpu_map,
  5376. &cpu_to_cpu_group);
  5377. }
  5378. #endif
  5379. #ifdef CONFIG_SCHED_MC
  5380. /* Set up multi-core groups */
  5381. for_each_cpu_mask(i, *cpu_map) {
  5382. cpumask_t this_core_map = cpu_coregroup_map(i);
  5383. cpus_and(this_core_map, this_core_map, *cpu_map);
  5384. if (i != first_cpu(this_core_map))
  5385. continue;
  5386. init_sched_build_groups(this_core_map, cpu_map,
  5387. &cpu_to_core_group);
  5388. }
  5389. #endif
  5390. /* Set up physical groups */
  5391. for (i = 0; i < MAX_NUMNODES; i++) {
  5392. cpumask_t nodemask = node_to_cpumask(i);
  5393. cpus_and(nodemask, nodemask, *cpu_map);
  5394. if (cpus_empty(nodemask))
  5395. continue;
  5396. init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
  5397. }
  5398. #ifdef CONFIG_NUMA
  5399. /* Set up node groups */
  5400. if (sd_allnodes)
  5401. init_sched_build_groups(*cpu_map, cpu_map,
  5402. &cpu_to_allnodes_group);
  5403. for (i = 0; i < MAX_NUMNODES; i++) {
  5404. /* Set up node groups */
  5405. struct sched_group *sg, *prev;
  5406. cpumask_t nodemask = node_to_cpumask(i);
  5407. cpumask_t domainspan;
  5408. cpumask_t covered = CPU_MASK_NONE;
  5409. int j;
  5410. cpus_and(nodemask, nodemask, *cpu_map);
  5411. if (cpus_empty(nodemask)) {
  5412. sched_group_nodes[i] = NULL;
  5413. continue;
  5414. }
  5415. domainspan = sched_domain_node_span(i);
  5416. cpus_and(domainspan, domainspan, *cpu_map);
  5417. sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
  5418. if (!sg) {
  5419. printk(KERN_WARNING "Can not alloc domain group for "
  5420. "node %d\n", i);
  5421. goto error;
  5422. }
  5423. sched_group_nodes[i] = sg;
  5424. for_each_cpu_mask(j, nodemask) {
  5425. struct sched_domain *sd;
  5426. sd = &per_cpu(node_domains, j);
  5427. sd->groups = sg;
  5428. }
  5429. sg->__cpu_power = 0;
  5430. sg->cpumask = nodemask;
  5431. sg->next = sg;
  5432. cpus_or(covered, covered, nodemask);
  5433. prev = sg;
  5434. for (j = 0; j < MAX_NUMNODES; j++) {
  5435. cpumask_t tmp, notcovered;
  5436. int n = (i + j) % MAX_NUMNODES;
  5437. cpus_complement(notcovered, covered);
  5438. cpus_and(tmp, notcovered, *cpu_map);
  5439. cpus_and(tmp, tmp, domainspan);
  5440. if (cpus_empty(tmp))
  5441. break;
  5442. nodemask = node_to_cpumask(n);
  5443. cpus_and(tmp, tmp, nodemask);
  5444. if (cpus_empty(tmp))
  5445. continue;
  5446. sg = kmalloc_node(sizeof(struct sched_group),
  5447. GFP_KERNEL, i);
  5448. if (!sg) {
  5449. printk(KERN_WARNING
  5450. "Can not alloc domain group for node %d\n", j);
  5451. goto error;
  5452. }
  5453. sg->__cpu_power = 0;
  5454. sg->cpumask = tmp;
  5455. sg->next = prev->next;
  5456. cpus_or(covered, covered, tmp);
  5457. prev->next = sg;
  5458. prev = sg;
  5459. }
  5460. }
  5461. #endif
  5462. /* Calculate CPU power for physical packages and nodes */
  5463. #ifdef CONFIG_SCHED_SMT
  5464. for_each_cpu_mask(i, *cpu_map) {
  5465. struct sched_domain *sd = &per_cpu(cpu_domains, i);
  5466. init_sched_groups_power(i, sd);
  5467. }
  5468. #endif
  5469. #ifdef CONFIG_SCHED_MC
  5470. for_each_cpu_mask(i, *cpu_map) {
  5471. struct sched_domain *sd = &per_cpu(core_domains, i);
  5472. init_sched_groups_power(i, sd);
  5473. }
  5474. #endif
  5475. for_each_cpu_mask(i, *cpu_map) {
  5476. struct sched_domain *sd = &per_cpu(phys_domains, i);
  5477. init_sched_groups_power(i, sd);
  5478. }
  5479. #ifdef CONFIG_NUMA
  5480. for (i = 0; i < MAX_NUMNODES; i++)
  5481. init_numa_sched_groups_power(sched_group_nodes[i]);
  5482. if (sd_allnodes) {
  5483. struct sched_group *sg;
  5484. cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
  5485. init_numa_sched_groups_power(sg);
  5486. }
  5487. #endif
  5488. /* Attach the domains */
  5489. for_each_cpu_mask(i, *cpu_map) {
  5490. struct sched_domain *sd;
  5491. #ifdef CONFIG_SCHED_SMT
  5492. sd = &per_cpu(cpu_domains, i);
  5493. #elif defined(CONFIG_SCHED_MC)
  5494. sd = &per_cpu(core_domains, i);
  5495. #else
  5496. sd = &per_cpu(phys_domains, i);
  5497. #endif
  5498. cpu_attach_domain(sd, i);
  5499. }
  5500. return 0;
  5501. #ifdef CONFIG_NUMA
  5502. error:
  5503. free_sched_groups(cpu_map);
  5504. return -ENOMEM;
  5505. #endif
  5506. }
  5507. static cpumask_t *doms_cur; /* current sched domains */
  5508. static int ndoms_cur; /* number of sched domains in 'doms_cur' */
  5509. /*
  5510. * Special case: If a kmalloc of a doms_cur partition (array of
  5511. * cpumask_t) fails, then fallback to a single sched domain,
  5512. * as determined by the single cpumask_t fallback_doms.
  5513. */
  5514. static cpumask_t fallback_doms;
  5515. /*
  5516. * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  5517. * For now this just excludes isolated cpus, but could be used to
  5518. * exclude other special cases in the future.
  5519. */
  5520. static int arch_init_sched_domains(const cpumask_t *cpu_map)
  5521. {
  5522. ndoms_cur = 1;
  5523. doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
  5524. if (!doms_cur)
  5525. doms_cur = &fallback_doms;
  5526. cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
  5527. register_sched_domain_sysctl();
  5528. return build_sched_domains(doms_cur);
  5529. }
  5530. static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
  5531. {
  5532. free_sched_groups(cpu_map);
  5533. }
  5534. /*
  5535. * Detach sched domains from a group of cpus specified in cpu_map
  5536. * These cpus will now be attached to the NULL domain
  5537. */
  5538. static void detach_destroy_domains(const cpumask_t *cpu_map)
  5539. {
  5540. int i;
  5541. unregister_sched_domain_sysctl();
  5542. for_each_cpu_mask(i, *cpu_map)
  5543. cpu_attach_domain(NULL, i);
  5544. synchronize_sched();
  5545. arch_destroy_sched_domains(cpu_map);
  5546. }
  5547. /*
  5548. * Partition sched domains as specified by the 'ndoms_new'
  5549. * cpumasks in the array doms_new[] of cpumasks. This compares
  5550. * doms_new[] to the current sched domain partitioning, doms_cur[].
  5551. * It destroys each deleted domain and builds each new domain.
  5552. *
  5553. * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
  5554. * The masks don't intersect (don't overlap.) We should setup one
  5555. * sched domain for each mask. CPUs not in any of the cpumasks will
  5556. * not be load balanced. If the same cpumask appears both in the
  5557. * current 'doms_cur' domains and in the new 'doms_new', we can leave
  5558. * it as it is.
  5559. *
  5560. * The passed in 'doms_new' should be kmalloc'd. This routine takes
  5561. * ownership of it and will kfree it when done with it. If the caller
  5562. * failed the kmalloc call, then it can pass in doms_new == NULL,
  5563. * and partition_sched_domains() will fallback to the single partition
  5564. * 'fallback_doms'.
  5565. *
  5566. * Call with hotplug lock held
  5567. */
  5568. void partition_sched_domains(int ndoms_new, cpumask_t *doms_new)
  5569. {
  5570. int i, j;
  5571. if (doms_new == NULL) {
  5572. ndoms_new = 1;
  5573. doms_new = &fallback_doms;
  5574. cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
  5575. }
  5576. /* Destroy deleted domains */
  5577. for (i = 0; i < ndoms_cur; i++) {
  5578. for (j = 0; j < ndoms_new; j++) {
  5579. if (cpus_equal(doms_cur[i], doms_new[j]))
  5580. goto match1;
  5581. }
  5582. /* no match - a current sched domain not in new doms_new[] */
  5583. detach_destroy_domains(doms_cur + i);
  5584. match1:
  5585. ;
  5586. }
  5587. /* Build new domains */
  5588. for (i = 0; i < ndoms_new; i++) {
  5589. for (j = 0; j < ndoms_cur; j++) {
  5590. if (cpus_equal(doms_new[i], doms_cur[j]))
  5591. goto match2;
  5592. }
  5593. /* no match - add a new doms_new */
  5594. build_sched_domains(doms_new + i);
  5595. match2:
  5596. ;
  5597. }
  5598. /* Remember the new sched domains */
  5599. if (doms_cur != &fallback_doms)
  5600. kfree(doms_cur);
  5601. doms_cur = doms_new;
  5602. ndoms_cur = ndoms_new;
  5603. }
  5604. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  5605. static int arch_reinit_sched_domains(void)
  5606. {
  5607. int err;
  5608. mutex_lock(&sched_hotcpu_mutex);
  5609. detach_destroy_domains(&cpu_online_map);
  5610. err = arch_init_sched_domains(&cpu_online_map);
  5611. mutex_unlock(&sched_hotcpu_mutex);
  5612. return err;
  5613. }
  5614. static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
  5615. {
  5616. int ret;
  5617. if (buf[0] != '0' && buf[0] != '1')
  5618. return -EINVAL;
  5619. if (smt)
  5620. sched_smt_power_savings = (buf[0] == '1');
  5621. else
  5622. sched_mc_power_savings = (buf[0] == '1');
  5623. ret = arch_reinit_sched_domains();
  5624. return ret ? ret : count;
  5625. }
  5626. #ifdef CONFIG_SCHED_MC
  5627. static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
  5628. {
  5629. return sprintf(page, "%u\n", sched_mc_power_savings);
  5630. }
  5631. static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
  5632. const char *buf, size_t count)
  5633. {
  5634. return sched_power_savings_store(buf, count, 0);
  5635. }
  5636. static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
  5637. sched_mc_power_savings_store);
  5638. #endif
  5639. #ifdef CONFIG_SCHED_SMT
  5640. static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
  5641. {
  5642. return sprintf(page, "%u\n", sched_smt_power_savings);
  5643. }
  5644. static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
  5645. const char *buf, size_t count)
  5646. {
  5647. return sched_power_savings_store(buf, count, 1);
  5648. }
  5649. static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
  5650. sched_smt_power_savings_store);
  5651. #endif
  5652. int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
  5653. {
  5654. int err = 0;
  5655. #ifdef CONFIG_SCHED_SMT
  5656. if (smt_capable())
  5657. err = sysfs_create_file(&cls->kset.kobj,
  5658. &attr_sched_smt_power_savings.attr);
  5659. #endif
  5660. #ifdef CONFIG_SCHED_MC
  5661. if (!err && mc_capable())
  5662. err = sysfs_create_file(&cls->kset.kobj,
  5663. &attr_sched_mc_power_savings.attr);
  5664. #endif
  5665. return err;
  5666. }
  5667. #endif
  5668. /*
  5669. * Force a reinitialization of the sched domains hierarchy. The domains
  5670. * and groups cannot be updated in place without racing with the balancing
  5671. * code, so we temporarily attach all running cpus to the NULL domain
  5672. * which will prevent rebalancing while the sched domains are recalculated.
  5673. */
  5674. static int update_sched_domains(struct notifier_block *nfb,
  5675. unsigned long action, void *hcpu)
  5676. {
  5677. switch (action) {
  5678. case CPU_UP_PREPARE:
  5679. case CPU_UP_PREPARE_FROZEN:
  5680. case CPU_DOWN_PREPARE:
  5681. case CPU_DOWN_PREPARE_FROZEN:
  5682. detach_destroy_domains(&cpu_online_map);
  5683. return NOTIFY_OK;
  5684. case CPU_UP_CANCELED:
  5685. case CPU_UP_CANCELED_FROZEN:
  5686. case CPU_DOWN_FAILED:
  5687. case CPU_DOWN_FAILED_FROZEN:
  5688. case CPU_ONLINE:
  5689. case CPU_ONLINE_FROZEN:
  5690. case CPU_DEAD:
  5691. case CPU_DEAD_FROZEN:
  5692. /*
  5693. * Fall through and re-initialise the domains.
  5694. */
  5695. break;
  5696. default:
  5697. return NOTIFY_DONE;
  5698. }
  5699. /* The hotplug lock is already held by cpu_up/cpu_down */
  5700. arch_init_sched_domains(&cpu_online_map);
  5701. return NOTIFY_OK;
  5702. }
  5703. void __init sched_init_smp(void)
  5704. {
  5705. cpumask_t non_isolated_cpus;
  5706. mutex_lock(&sched_hotcpu_mutex);
  5707. arch_init_sched_domains(&cpu_online_map);
  5708. cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
  5709. if (cpus_empty(non_isolated_cpus))
  5710. cpu_set(smp_processor_id(), non_isolated_cpus);
  5711. mutex_unlock(&sched_hotcpu_mutex);
  5712. /* XXX: Theoretical race here - CPU may be hotplugged now */
  5713. hotcpu_notifier(update_sched_domains, 0);
  5714. /* Move init over to a non-isolated CPU */
  5715. if (set_cpus_allowed(current, non_isolated_cpus) < 0)
  5716. BUG();
  5717. }
  5718. #else
  5719. void __init sched_init_smp(void)
  5720. {
  5721. }
  5722. #endif /* CONFIG_SMP */
  5723. int in_sched_functions(unsigned long addr)
  5724. {
  5725. /* Linker adds these: start and end of __sched functions */
  5726. extern char __sched_text_start[], __sched_text_end[];
  5727. return in_lock_functions(addr) ||
  5728. (addr >= (unsigned long)__sched_text_start
  5729. && addr < (unsigned long)__sched_text_end);
  5730. }
  5731. static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
  5732. {
  5733. cfs_rq->tasks_timeline = RB_ROOT;
  5734. #ifdef CONFIG_FAIR_GROUP_SCHED
  5735. cfs_rq->rq = rq;
  5736. #endif
  5737. cfs_rq->min_vruntime = (u64)(-(1LL << 20));
  5738. }
  5739. void __init sched_init(void)
  5740. {
  5741. int highest_cpu = 0;
  5742. int i, j;
  5743. for_each_possible_cpu(i) {
  5744. struct rt_prio_array *array;
  5745. struct rq *rq;
  5746. rq = cpu_rq(i);
  5747. spin_lock_init(&rq->lock);
  5748. lockdep_set_class(&rq->lock, &rq->rq_lock_key);
  5749. rq->nr_running = 0;
  5750. rq->clock = 1;
  5751. init_cfs_rq(&rq->cfs, rq);
  5752. #ifdef CONFIG_FAIR_GROUP_SCHED
  5753. INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
  5754. {
  5755. struct cfs_rq *cfs_rq = &per_cpu(init_cfs_rq, i);
  5756. struct sched_entity *se =
  5757. &per_cpu(init_sched_entity, i);
  5758. init_cfs_rq_p[i] = cfs_rq;
  5759. init_cfs_rq(cfs_rq, rq);
  5760. cfs_rq->tg = &init_task_group;
  5761. list_add(&cfs_rq->leaf_cfs_rq_list,
  5762. &rq->leaf_cfs_rq_list);
  5763. init_sched_entity_p[i] = se;
  5764. se->cfs_rq = &rq->cfs;
  5765. se->my_q = cfs_rq;
  5766. se->load.weight = init_task_group_load;
  5767. se->load.inv_weight =
  5768. div64_64(1ULL<<32, init_task_group_load);
  5769. se->parent = NULL;
  5770. }
  5771. init_task_group.shares = init_task_group_load;
  5772. spin_lock_init(&init_task_group.lock);
  5773. #endif
  5774. for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
  5775. rq->cpu_load[j] = 0;
  5776. #ifdef CONFIG_SMP
  5777. rq->sd = NULL;
  5778. rq->active_balance = 0;
  5779. rq->next_balance = jiffies;
  5780. rq->push_cpu = 0;
  5781. rq->cpu = i;
  5782. rq->migration_thread = NULL;
  5783. INIT_LIST_HEAD(&rq->migration_queue);
  5784. #endif
  5785. atomic_set(&rq->nr_iowait, 0);
  5786. array = &rq->rt.active;
  5787. for (j = 0; j < MAX_RT_PRIO; j++) {
  5788. INIT_LIST_HEAD(array->queue + j);
  5789. __clear_bit(j, array->bitmap);
  5790. }
  5791. highest_cpu = i;
  5792. /* delimiter for bitsearch: */
  5793. __set_bit(MAX_RT_PRIO, array->bitmap);
  5794. }
  5795. set_load_weight(&init_task);
  5796. #ifdef CONFIG_PREEMPT_NOTIFIERS
  5797. INIT_HLIST_HEAD(&init_task.preempt_notifiers);
  5798. #endif
  5799. #ifdef CONFIG_SMP
  5800. nr_cpu_ids = highest_cpu + 1;
  5801. open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
  5802. #endif
  5803. #ifdef CONFIG_RT_MUTEXES
  5804. plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
  5805. #endif
  5806. /*
  5807. * The boot idle thread does lazy MMU switching as well:
  5808. */
  5809. atomic_inc(&init_mm.mm_count);
  5810. enter_lazy_tlb(&init_mm, current);
  5811. /*
  5812. * Make us the idle thread. Technically, schedule() should not be
  5813. * called from this thread, however somewhere below it might be,
  5814. * but because we are the idle thread, we just pick up running again
  5815. * when this runqueue becomes "idle".
  5816. */
  5817. init_idle(current, smp_processor_id());
  5818. /*
  5819. * During early bootup we pretend to be a normal task:
  5820. */
  5821. current->sched_class = &fair_sched_class;
  5822. }
  5823. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  5824. void __might_sleep(char *file, int line)
  5825. {
  5826. #ifdef in_atomic
  5827. static unsigned long prev_jiffy; /* ratelimiting */
  5828. if ((in_atomic() || irqs_disabled()) &&
  5829. system_state == SYSTEM_RUNNING && !oops_in_progress) {
  5830. if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  5831. return;
  5832. prev_jiffy = jiffies;
  5833. printk(KERN_ERR "BUG: sleeping function called from invalid"
  5834. " context at %s:%d\n", file, line);
  5835. printk("in_atomic():%d, irqs_disabled():%d\n",
  5836. in_atomic(), irqs_disabled());
  5837. debug_show_held_locks(current);
  5838. if (irqs_disabled())
  5839. print_irqtrace_events(current);
  5840. dump_stack();
  5841. }
  5842. #endif
  5843. }
  5844. EXPORT_SYMBOL(__might_sleep);
  5845. #endif
  5846. #ifdef CONFIG_MAGIC_SYSRQ
  5847. static void normalize_task(struct rq *rq, struct task_struct *p)
  5848. {
  5849. int on_rq;
  5850. update_rq_clock(rq);
  5851. on_rq = p->se.on_rq;
  5852. if (on_rq)
  5853. deactivate_task(rq, p, 0);
  5854. __setscheduler(rq, p, SCHED_NORMAL, 0);
  5855. if (on_rq) {
  5856. activate_task(rq, p, 0);
  5857. resched_task(rq->curr);
  5858. }
  5859. }
  5860. void normalize_rt_tasks(void)
  5861. {
  5862. struct task_struct *g, *p;
  5863. unsigned long flags;
  5864. struct rq *rq;
  5865. read_lock_irq(&tasklist_lock);
  5866. do_each_thread(g, p) {
  5867. /*
  5868. * Only normalize user tasks:
  5869. */
  5870. if (!p->mm)
  5871. continue;
  5872. p->se.exec_start = 0;
  5873. #ifdef CONFIG_SCHEDSTATS
  5874. p->se.wait_start = 0;
  5875. p->se.sleep_start = 0;
  5876. p->se.block_start = 0;
  5877. #endif
  5878. task_rq(p)->clock = 0;
  5879. if (!rt_task(p)) {
  5880. /*
  5881. * Renice negative nice level userspace
  5882. * tasks back to 0:
  5883. */
  5884. if (TASK_NICE(p) < 0 && p->mm)
  5885. set_user_nice(p, 0);
  5886. continue;
  5887. }
  5888. spin_lock_irqsave(&p->pi_lock, flags);
  5889. rq = __task_rq_lock(p);
  5890. normalize_task(rq, p);
  5891. __task_rq_unlock(rq);
  5892. spin_unlock_irqrestore(&p->pi_lock, flags);
  5893. } while_each_thread(g, p);
  5894. read_unlock_irq(&tasklist_lock);
  5895. }
  5896. #endif /* CONFIG_MAGIC_SYSRQ */
  5897. #ifdef CONFIG_IA64
  5898. /*
  5899. * These functions are only useful for the IA64 MCA handling.
  5900. *
  5901. * They can only be called when the whole system has been
  5902. * stopped - every CPU needs to be quiescent, and no scheduling
  5903. * activity can take place. Using them for anything else would
  5904. * be a serious bug, and as a result, they aren't even visible
  5905. * under any other configuration.
  5906. */
  5907. /**
  5908. * curr_task - return the current task for a given cpu.
  5909. * @cpu: the processor in question.
  5910. *
  5911. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  5912. */
  5913. struct task_struct *curr_task(int cpu)
  5914. {
  5915. return cpu_curr(cpu);
  5916. }
  5917. /**
  5918. * set_curr_task - set the current task for a given cpu.
  5919. * @cpu: the processor in question.
  5920. * @p: the task pointer to set.
  5921. *
  5922. * Description: This function must only be used when non-maskable interrupts
  5923. * are serviced on a separate stack. It allows the architecture to switch the
  5924. * notion of the current task on a cpu in a non-blocking manner. This function
  5925. * must be called with all CPU's synchronized, and interrupts disabled, the
  5926. * and caller must save the original value of the current task (see
  5927. * curr_task() above) and restore that value before reenabling interrupts and
  5928. * re-starting the system.
  5929. *
  5930. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  5931. */
  5932. void set_curr_task(int cpu, struct task_struct *p)
  5933. {
  5934. cpu_curr(cpu) = p;
  5935. }
  5936. #endif
  5937. #ifdef CONFIG_FAIR_GROUP_SCHED
  5938. /* allocate runqueue etc for a new task group */
  5939. struct task_group *sched_create_group(void)
  5940. {
  5941. struct task_group *tg;
  5942. struct cfs_rq *cfs_rq;
  5943. struct sched_entity *se;
  5944. struct rq *rq;
  5945. int i;
  5946. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  5947. if (!tg)
  5948. return ERR_PTR(-ENOMEM);
  5949. tg->cfs_rq = kzalloc(sizeof(cfs_rq) * NR_CPUS, GFP_KERNEL);
  5950. if (!tg->cfs_rq)
  5951. goto err;
  5952. tg->se = kzalloc(sizeof(se) * NR_CPUS, GFP_KERNEL);
  5953. if (!tg->se)
  5954. goto err;
  5955. for_each_possible_cpu(i) {
  5956. rq = cpu_rq(i);
  5957. cfs_rq = kmalloc_node(sizeof(struct cfs_rq), GFP_KERNEL,
  5958. cpu_to_node(i));
  5959. if (!cfs_rq)
  5960. goto err;
  5961. se = kmalloc_node(sizeof(struct sched_entity), GFP_KERNEL,
  5962. cpu_to_node(i));
  5963. if (!se)
  5964. goto err;
  5965. memset(cfs_rq, 0, sizeof(struct cfs_rq));
  5966. memset(se, 0, sizeof(struct sched_entity));
  5967. tg->cfs_rq[i] = cfs_rq;
  5968. init_cfs_rq(cfs_rq, rq);
  5969. cfs_rq->tg = tg;
  5970. tg->se[i] = se;
  5971. se->cfs_rq = &rq->cfs;
  5972. se->my_q = cfs_rq;
  5973. se->load.weight = NICE_0_LOAD;
  5974. se->load.inv_weight = div64_64(1ULL<<32, NICE_0_LOAD);
  5975. se->parent = NULL;
  5976. }
  5977. for_each_possible_cpu(i) {
  5978. rq = cpu_rq(i);
  5979. cfs_rq = tg->cfs_rq[i];
  5980. list_add_rcu(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
  5981. }
  5982. tg->shares = NICE_0_LOAD;
  5983. spin_lock_init(&tg->lock);
  5984. return tg;
  5985. err:
  5986. for_each_possible_cpu(i) {
  5987. if (tg->cfs_rq)
  5988. kfree(tg->cfs_rq[i]);
  5989. if (tg->se)
  5990. kfree(tg->se[i]);
  5991. }
  5992. kfree(tg->cfs_rq);
  5993. kfree(tg->se);
  5994. kfree(tg);
  5995. return ERR_PTR(-ENOMEM);
  5996. }
  5997. /* rcu callback to free various structures associated with a task group */
  5998. static void free_sched_group(struct rcu_head *rhp)
  5999. {
  6000. struct cfs_rq *cfs_rq = container_of(rhp, struct cfs_rq, rcu);
  6001. struct task_group *tg = cfs_rq->tg;
  6002. struct sched_entity *se;
  6003. int i;
  6004. /* now it should be safe to free those cfs_rqs */
  6005. for_each_possible_cpu(i) {
  6006. cfs_rq = tg->cfs_rq[i];
  6007. kfree(cfs_rq);
  6008. se = tg->se[i];
  6009. kfree(se);
  6010. }
  6011. kfree(tg->cfs_rq);
  6012. kfree(tg->se);
  6013. kfree(tg);
  6014. }
  6015. /* Destroy runqueue etc associated with a task group */
  6016. void sched_destroy_group(struct task_group *tg)
  6017. {
  6018. struct cfs_rq *cfs_rq;
  6019. int i;
  6020. for_each_possible_cpu(i) {
  6021. cfs_rq = tg->cfs_rq[i];
  6022. list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
  6023. }
  6024. cfs_rq = tg->cfs_rq[0];
  6025. /* wait for possible concurrent references to cfs_rqs complete */
  6026. call_rcu(&cfs_rq->rcu, free_sched_group);
  6027. }
  6028. /* change task's runqueue when it moves between groups.
  6029. * The caller of this function should have put the task in its new group
  6030. * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
  6031. * reflect its new group.
  6032. */
  6033. void sched_move_task(struct task_struct *tsk)
  6034. {
  6035. int on_rq, running;
  6036. unsigned long flags;
  6037. struct rq *rq;
  6038. rq = task_rq_lock(tsk, &flags);
  6039. if (tsk->sched_class != &fair_sched_class)
  6040. goto done;
  6041. update_rq_clock(rq);
  6042. running = task_running(rq, tsk);
  6043. on_rq = tsk->se.on_rq;
  6044. if (on_rq) {
  6045. dequeue_task(rq, tsk, 0);
  6046. if (unlikely(running))
  6047. tsk->sched_class->put_prev_task(rq, tsk);
  6048. }
  6049. set_task_cfs_rq(tsk);
  6050. if (on_rq) {
  6051. if (unlikely(running))
  6052. tsk->sched_class->set_curr_task(rq);
  6053. enqueue_task(rq, tsk, 0);
  6054. }
  6055. done:
  6056. task_rq_unlock(rq, &flags);
  6057. }
  6058. static void set_se_shares(struct sched_entity *se, unsigned long shares)
  6059. {
  6060. struct cfs_rq *cfs_rq = se->cfs_rq;
  6061. struct rq *rq = cfs_rq->rq;
  6062. int on_rq;
  6063. spin_lock_irq(&rq->lock);
  6064. on_rq = se->on_rq;
  6065. if (on_rq)
  6066. dequeue_entity(cfs_rq, se, 0);
  6067. se->load.weight = shares;
  6068. se->load.inv_weight = div64_64((1ULL<<32), shares);
  6069. if (on_rq)
  6070. enqueue_entity(cfs_rq, se, 0);
  6071. spin_unlock_irq(&rq->lock);
  6072. }
  6073. int sched_group_set_shares(struct task_group *tg, unsigned long shares)
  6074. {
  6075. int i;
  6076. spin_lock(&tg->lock);
  6077. if (tg->shares == shares)
  6078. goto done;
  6079. tg->shares = shares;
  6080. for_each_possible_cpu(i)
  6081. set_se_shares(tg->se[i], shares);
  6082. done:
  6083. spin_unlock(&tg->lock);
  6084. return 0;
  6085. }
  6086. unsigned long sched_group_shares(struct task_group *tg)
  6087. {
  6088. return tg->shares;
  6089. }
  6090. #endif /* CONFIG_FAIR_GROUP_SCHED */