sched.c 196 KB

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