sched.c 140 KB

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