sched.c 233 KB

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