sched.c 233 KB

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