sched.c 225 KB

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