sched.c 159 KB

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