sched.c 164 KB

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