sched.c 218 KB

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