sched.c 271 KB

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