sched.c 229 KB

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