sched.c 141 KB

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