sched.c 175 KB

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