sched.c 212 KB

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