sched.c 171 KB

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