sched.c 159 KB

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