sched.c 213 KB

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