sched.c 219 KB

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