sched.c 222 KB

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