sched.c 155 KB

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