sched.c 231 KB

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