sched.c 170 KB

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