sched.c 220 KB

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