sched.c 180 KB

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