sched.c 221 KB

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