sched.c 164 KB

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