sched.c 198 KB

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