sched.c 170 KB

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