sched.c 222 KB

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