sched.c 164 KB

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