sched.c 231 KB

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