sched.c 170 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951
  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. */
  26. #include <linux/mm.h>
  27. #include <linux/module.h>
  28. #include <linux/nmi.h>
  29. #include <linux/init.h>
  30. #include <linux/uaccess.h>
  31. #include <linux/highmem.h>
  32. #include <linux/smp_lock.h>
  33. #include <asm/mmu_context.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/capability.h>
  36. #include <linux/completion.h>
  37. #include <linux/kernel_stat.h>
  38. #include <linux/debug_locks.h>
  39. #include <linux/security.h>
  40. #include <linux/notifier.h>
  41. #include <linux/profile.h>
  42. #include <linux/freezer.h>
  43. #include <linux/vmalloc.h>
  44. #include <linux/blkdev.h>
  45. #include <linux/delay.h>
  46. #include <linux/smp.h>
  47. #include <linux/threads.h>
  48. #include <linux/timer.h>
  49. #include <linux/rcupdate.h>
  50. #include <linux/cpu.h>
  51. #include <linux/cpuset.h>
  52. #include <linux/percpu.h>
  53. #include <linux/kthread.h>
  54. #include <linux/seq_file.h>
  55. #include <linux/sysctl.h>
  56. #include <linux/syscalls.h>
  57. #include <linux/times.h>
  58. #include <linux/tsacct_kern.h>
  59. #include <linux/kprobes.h>
  60. #include <linux/delayacct.h>
  61. #include <linux/reciprocal_div.h>
  62. #include <linux/unistd.h>
  63. #include <linux/pagemap.h>
  64. #include <asm/tlb.h>
  65. /*
  66. * Scheduler clock - returns current time in nanosec units.
  67. * This is default implementation.
  68. * Architectures and sub-architectures can override this.
  69. */
  70. unsigned long long __attribute__((weak)) sched_clock(void)
  71. {
  72. return (unsigned long long)jiffies * (1000000000 / HZ);
  73. }
  74. /*
  75. * Convert user-nice values [ -20 ... 0 ... 19 ]
  76. * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  77. * and back.
  78. */
  79. #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  80. #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  81. #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  82. /*
  83. * 'User priority' is the nice value converted to something we
  84. * can work with better when scaling various scheduler parameters,
  85. * it's a [ 0 ... 39 ] range.
  86. */
  87. #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
  88. #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  89. #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  90. /*
  91. * Some helpers for converting nanosecond timing to jiffy resolution
  92. */
  93. #define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ))
  94. #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
  95. #define NICE_0_LOAD SCHED_LOAD_SCALE
  96. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  97. /*
  98. * These are the 'tuning knobs' of the scheduler:
  99. *
  100. * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger),
  101. * default timeslice is 100 msecs, maximum timeslice is 800 msecs.
  102. * Timeslices get refilled after they expire.
  103. */
  104. #define MIN_TIMESLICE max(5 * HZ / 1000, 1)
  105. #define DEF_TIMESLICE (100 * HZ / 1000)
  106. #ifdef CONFIG_SMP
  107. /*
  108. * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
  109. * Since cpu_power is a 'constant', we can use a reciprocal divide.
  110. */
  111. static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
  112. {
  113. return reciprocal_divide(load, sg->reciprocal_cpu_power);
  114. }
  115. /*
  116. * Each time a sched group cpu_power is changed,
  117. * we must compute its reciprocal value
  118. */
  119. static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
  120. {
  121. sg->__cpu_power += val;
  122. sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
  123. }
  124. #endif
  125. #define SCALE_PRIO(x, prio) \
  126. max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_TIMESLICE)
  127. /*
  128. * static_prio_timeslice() scales user-nice values [ -20 ... 0 ... 19 ]
  129. * to time slice values: [800ms ... 100ms ... 5ms]
  130. */
  131. static unsigned int static_prio_timeslice(int static_prio)
  132. {
  133. if (static_prio == NICE_TO_PRIO(19))
  134. return 1;
  135. if (static_prio < NICE_TO_PRIO(0))
  136. return SCALE_PRIO(DEF_TIMESLICE * 4, static_prio);
  137. else
  138. return SCALE_PRIO(DEF_TIMESLICE, static_prio);
  139. }
  140. static inline int rt_policy(int policy)
  141. {
  142. if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
  143. return 1;
  144. return 0;
  145. }
  146. static inline int task_has_rt_policy(struct task_struct *p)
  147. {
  148. return rt_policy(p->policy);
  149. }
  150. /*
  151. * This is the priority-queue data structure of the RT scheduling class:
  152. */
  153. struct rt_prio_array {
  154. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  155. struct list_head queue[MAX_RT_PRIO];
  156. };
  157. #ifdef CONFIG_FAIR_GROUP_SCHED
  158. #include <linux/container.h>
  159. struct cfs_rq;
  160. /* task group related information */
  161. struct task_grp {
  162. struct container_subsys_state css;
  163. /* schedulable entities of this group on each cpu */
  164. struct sched_entity **se;
  165. /* runqueue "owned" by this group on each cpu */
  166. struct cfs_rq **cfs_rq;
  167. unsigned long shares;
  168. };
  169. /* Default task group's sched entity on each cpu */
  170. static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
  171. /* Default task group's cfs_rq on each cpu */
  172. static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
  173. static struct sched_entity *init_sched_entity_p[CONFIG_NR_CPUS];
  174. static struct cfs_rq *init_cfs_rq_p[CONFIG_NR_CPUS];
  175. /* Default task group.
  176. * Every task in system belong to this group at bootup.
  177. */
  178. static struct task_grp init_task_grp = {
  179. .se = init_sched_entity_p,
  180. .cfs_rq = init_cfs_rq_p,
  181. };
  182. /* return group to which a task belongs */
  183. static inline struct task_grp *task_grp(struct task_struct *p)
  184. {
  185. return container_of(task_subsys_state(p, cpu_subsys_id),
  186. struct task_grp, css);
  187. }
  188. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  189. static inline void set_task_cfs_rq(struct task_struct *p)
  190. {
  191. p->se.cfs_rq = task_grp(p)->cfs_rq[task_cpu(p)];
  192. p->se.parent = task_grp(p)->se[task_cpu(p)];
  193. }
  194. #else
  195. static inline void set_task_cfs_rq(struct task_struct *p) { }
  196. #endif /* CONFIG_FAIR_GROUP_SCHED */
  197. /* CFS-related fields in a runqueue */
  198. struct cfs_rq {
  199. struct load_weight load;
  200. unsigned long nr_running;
  201. u64 exec_clock;
  202. u64 min_vruntime;
  203. struct rb_root tasks_timeline;
  204. struct rb_node *rb_leftmost;
  205. struct rb_node *rb_load_balance_curr;
  206. /* 'curr' points to currently running entity on this cfs_rq.
  207. * It is set to NULL otherwise (i.e when none are currently running).
  208. */
  209. struct sched_entity *curr;
  210. #ifdef CONFIG_FAIR_GROUP_SCHED
  211. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  212. /* leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  213. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  214. * (like users, containers etc.)
  215. *
  216. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  217. * list is used during load balance.
  218. */
  219. struct list_head leaf_cfs_rq_list; /* Better name : task_cfs_rq_list? */
  220. struct task_grp *tg; /* group that "owns" this runqueue */
  221. #endif
  222. };
  223. /* Real-Time classes' related field in a runqueue: */
  224. struct rt_rq {
  225. struct rt_prio_array active;
  226. int rt_load_balance_idx;
  227. struct list_head *rt_load_balance_head, *rt_load_balance_curr;
  228. };
  229. /*
  230. * This is the main, per-CPU runqueue data structure.
  231. *
  232. * Locking rule: those places that want to lock multiple runqueues
  233. * (such as the load balancing or the thread migration code), lock
  234. * acquire operations must be ordered by ascending &runqueue.
  235. */
  236. struct rq {
  237. spinlock_t lock; /* runqueue lock */
  238. /*
  239. * nr_running and cpu_load should be in the same cacheline because
  240. * remote CPUs use both these fields when doing load calculation.
  241. */
  242. unsigned long nr_running;
  243. #define CPU_LOAD_IDX_MAX 5
  244. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  245. unsigned char idle_at_tick;
  246. #ifdef CONFIG_NO_HZ
  247. unsigned char in_nohz_recently;
  248. #endif
  249. struct load_weight load; /* capture load from *all* tasks on this cpu */
  250. unsigned long nr_load_updates;
  251. u64 nr_switches;
  252. struct cfs_rq cfs;
  253. #ifdef CONFIG_FAIR_GROUP_SCHED
  254. struct list_head leaf_cfs_rq_list; /* list of leaf cfs_rq on this cpu */
  255. #endif
  256. struct rt_rq rt;
  257. /*
  258. * This is part of a global counter where only the total sum
  259. * over all CPUs matters. A task can increase this counter on
  260. * one CPU and if it got migrated afterwards it may decrease
  261. * it on another CPU. Always updated under the runqueue lock:
  262. */
  263. unsigned long nr_uninterruptible;
  264. struct task_struct *curr, *idle;
  265. unsigned long next_balance;
  266. struct mm_struct *prev_mm;
  267. u64 clock, prev_clock_raw;
  268. s64 clock_max_delta;
  269. unsigned int clock_warps, clock_overflows;
  270. u64 idle_clock;
  271. unsigned int clock_deep_idle_events;
  272. u64 tick_timestamp;
  273. atomic_t nr_iowait;
  274. #ifdef CONFIG_SMP
  275. struct sched_domain *sd;
  276. /* For active balancing */
  277. int active_balance;
  278. int push_cpu;
  279. int cpu; /* cpu of this runqueue */
  280. struct task_struct *migration_thread;
  281. struct list_head migration_queue;
  282. #endif
  283. #ifdef CONFIG_SCHEDSTATS
  284. /* latency stats */
  285. struct sched_info rq_sched_info;
  286. /* sys_sched_yield() stats */
  287. unsigned long yld_exp_empty;
  288. unsigned long yld_act_empty;
  289. unsigned long yld_both_empty;
  290. unsigned long yld_cnt;
  291. /* schedule() stats */
  292. unsigned long sched_switch;
  293. unsigned long sched_cnt;
  294. unsigned long sched_goidle;
  295. /* try_to_wake_up() stats */
  296. unsigned long ttwu_cnt;
  297. unsigned long ttwu_local;
  298. #endif
  299. struct lock_class_key rq_lock_key;
  300. };
  301. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  302. static DEFINE_MUTEX(sched_hotcpu_mutex);
  303. static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
  304. {
  305. rq->curr->sched_class->check_preempt_curr(rq, p);
  306. }
  307. static inline int cpu_of(struct rq *rq)
  308. {
  309. #ifdef CONFIG_SMP
  310. return rq->cpu;
  311. #else
  312. return 0;
  313. #endif
  314. }
  315. /*
  316. * Update the per-runqueue clock, as finegrained as the platform can give
  317. * us, but without assuming monotonicity, etc.:
  318. */
  319. static void __update_rq_clock(struct rq *rq)
  320. {
  321. u64 prev_raw = rq->prev_clock_raw;
  322. u64 now = sched_clock();
  323. s64 delta = now - prev_raw;
  324. u64 clock = rq->clock;
  325. #ifdef CONFIG_SCHED_DEBUG
  326. WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
  327. #endif
  328. /*
  329. * Protect against sched_clock() occasionally going backwards:
  330. */
  331. if (unlikely(delta < 0)) {
  332. clock++;
  333. rq->clock_warps++;
  334. } else {
  335. /*
  336. * Catch too large forward jumps too:
  337. */
  338. if (unlikely(clock + delta > rq->tick_timestamp + TICK_NSEC)) {
  339. if (clock < rq->tick_timestamp + TICK_NSEC)
  340. clock = rq->tick_timestamp + TICK_NSEC;
  341. else
  342. clock++;
  343. rq->clock_overflows++;
  344. } else {
  345. if (unlikely(delta > rq->clock_max_delta))
  346. rq->clock_max_delta = delta;
  347. clock += delta;
  348. }
  349. }
  350. rq->prev_clock_raw = now;
  351. rq->clock = clock;
  352. }
  353. static void update_rq_clock(struct rq *rq)
  354. {
  355. if (likely(smp_processor_id() == cpu_of(rq)))
  356. __update_rq_clock(rq);
  357. }
  358. /*
  359. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  360. * See detach_destroy_domains: synchronize_sched for details.
  361. *
  362. * The domain tree of any CPU may only be accessed from within
  363. * preempt-disabled sections.
  364. */
  365. #define for_each_domain(cpu, __sd) \
  366. for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
  367. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  368. #define this_rq() (&__get_cpu_var(runqueues))
  369. #define task_rq(p) cpu_rq(task_cpu(p))
  370. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  371. /*
  372. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  373. */
  374. #ifdef CONFIG_SCHED_DEBUG
  375. # define const_debug __read_mostly
  376. #else
  377. # define const_debug static const
  378. #endif
  379. /*
  380. * Debugging: various feature bits
  381. */
  382. enum {
  383. SCHED_FEAT_NEW_FAIR_SLEEPERS = 1,
  384. SCHED_FEAT_START_DEBIT = 2,
  385. SCHED_FEAT_USE_TREE_AVG = 4,
  386. SCHED_FEAT_APPROX_AVG = 8,
  387. };
  388. const_debug unsigned int sysctl_sched_features =
  389. SCHED_FEAT_NEW_FAIR_SLEEPERS *1 |
  390. SCHED_FEAT_START_DEBIT *1 |
  391. SCHED_FEAT_USE_TREE_AVG *0 |
  392. SCHED_FEAT_APPROX_AVG *0;
  393. #define sched_feat(x) (sysctl_sched_features & SCHED_FEAT_##x)
  394. /*
  395. * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
  396. * clock constructed from sched_clock():
  397. */
  398. unsigned long long cpu_clock(int cpu)
  399. {
  400. unsigned long long now;
  401. unsigned long flags;
  402. struct rq *rq;
  403. local_irq_save(flags);
  404. rq = cpu_rq(cpu);
  405. update_rq_clock(rq);
  406. now = rq->clock;
  407. local_irq_restore(flags);
  408. return now;
  409. }
  410. #ifndef prepare_arch_switch
  411. # define prepare_arch_switch(next) do { } while (0)
  412. #endif
  413. #ifndef finish_arch_switch
  414. # define finish_arch_switch(prev) do { } while (0)
  415. #endif
  416. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  417. static inline int task_running(struct rq *rq, struct task_struct *p)
  418. {
  419. return rq->curr == p;
  420. }
  421. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  422. {
  423. }
  424. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  425. {
  426. #ifdef CONFIG_DEBUG_SPINLOCK
  427. /* this is a valid case when another task releases the spinlock */
  428. rq->lock.owner = current;
  429. #endif
  430. /*
  431. * If we are tracking spinlock dependencies then we have to
  432. * fix up the runqueue lock - which gets 'carried over' from
  433. * prev into current:
  434. */
  435. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  436. spin_unlock_irq(&rq->lock);
  437. }
  438. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  439. static inline int task_running(struct rq *rq, struct task_struct *p)
  440. {
  441. #ifdef CONFIG_SMP
  442. return p->oncpu;
  443. #else
  444. return rq->curr == p;
  445. #endif
  446. }
  447. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  448. {
  449. #ifdef CONFIG_SMP
  450. /*
  451. * We can optimise this out completely for !SMP, because the
  452. * SMP rebalancing from interrupt is the only thing that cares
  453. * here.
  454. */
  455. next->oncpu = 1;
  456. #endif
  457. #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  458. spin_unlock_irq(&rq->lock);
  459. #else
  460. spin_unlock(&rq->lock);
  461. #endif
  462. }
  463. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  464. {
  465. #ifdef CONFIG_SMP
  466. /*
  467. * After ->oncpu is cleared, the task can be moved to a different CPU.
  468. * We must ensure this doesn't happen until the switch is completely
  469. * finished.
  470. */
  471. smp_wmb();
  472. prev->oncpu = 0;
  473. #endif
  474. #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  475. local_irq_enable();
  476. #endif
  477. }
  478. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  479. /*
  480. * __task_rq_lock - lock the runqueue a given task resides on.
  481. * Must be called interrupts disabled.
  482. */
  483. static inline struct rq *__task_rq_lock(struct task_struct *p)
  484. __acquires(rq->lock)
  485. {
  486. struct rq *rq;
  487. repeat_lock_task:
  488. rq = task_rq(p);
  489. spin_lock(&rq->lock);
  490. if (unlikely(rq != task_rq(p))) {
  491. spin_unlock(&rq->lock);
  492. goto repeat_lock_task;
  493. }
  494. return rq;
  495. }
  496. /*
  497. * task_rq_lock - lock the runqueue a given task resides on and disable
  498. * interrupts. Note the ordering: we can safely lookup the task_rq without
  499. * explicitly disabling preemption.
  500. */
  501. static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
  502. __acquires(rq->lock)
  503. {
  504. struct rq *rq;
  505. repeat_lock_task:
  506. local_irq_save(*flags);
  507. rq = task_rq(p);
  508. spin_lock(&rq->lock);
  509. if (unlikely(rq != task_rq(p))) {
  510. spin_unlock_irqrestore(&rq->lock, *flags);
  511. goto repeat_lock_task;
  512. }
  513. return rq;
  514. }
  515. static inline void __task_rq_unlock(struct rq *rq)
  516. __releases(rq->lock)
  517. {
  518. spin_unlock(&rq->lock);
  519. }
  520. static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
  521. __releases(rq->lock)
  522. {
  523. spin_unlock_irqrestore(&rq->lock, *flags);
  524. }
  525. /*
  526. * this_rq_lock - lock this runqueue and disable interrupts.
  527. */
  528. static inline struct rq *this_rq_lock(void)
  529. __acquires(rq->lock)
  530. {
  531. struct rq *rq;
  532. local_irq_disable();
  533. rq = this_rq();
  534. spin_lock(&rq->lock);
  535. return rq;
  536. }
  537. /*
  538. * We are going deep-idle (irqs are disabled):
  539. */
  540. void sched_clock_idle_sleep_event(void)
  541. {
  542. struct rq *rq = cpu_rq(smp_processor_id());
  543. spin_lock(&rq->lock);
  544. __update_rq_clock(rq);
  545. spin_unlock(&rq->lock);
  546. rq->clock_deep_idle_events++;
  547. }
  548. EXPORT_SYMBOL_GPL(sched_clock_idle_sleep_event);
  549. /*
  550. * We just idled delta nanoseconds (called with irqs disabled):
  551. */
  552. void sched_clock_idle_wakeup_event(u64 delta_ns)
  553. {
  554. struct rq *rq = cpu_rq(smp_processor_id());
  555. u64 now = sched_clock();
  556. rq->idle_clock += delta_ns;
  557. /*
  558. * Override the previous timestamp and ignore all
  559. * sched_clock() deltas that occured while we idled,
  560. * and use the PM-provided delta_ns to advance the
  561. * rq clock:
  562. */
  563. spin_lock(&rq->lock);
  564. rq->prev_clock_raw = now;
  565. rq->clock += delta_ns;
  566. spin_unlock(&rq->lock);
  567. }
  568. EXPORT_SYMBOL_GPL(sched_clock_idle_wakeup_event);
  569. /*
  570. * resched_task - mark a task 'to be rescheduled now'.
  571. *
  572. * On UP this means the setting of the need_resched flag, on SMP it
  573. * might also involve a cross-CPU call to trigger the scheduler on
  574. * the target CPU.
  575. */
  576. #ifdef CONFIG_SMP
  577. #ifndef tsk_is_polling
  578. #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
  579. #endif
  580. static void resched_task(struct task_struct *p)
  581. {
  582. int cpu;
  583. assert_spin_locked(&task_rq(p)->lock);
  584. if (unlikely(test_tsk_thread_flag(p, TIF_NEED_RESCHED)))
  585. return;
  586. set_tsk_thread_flag(p, TIF_NEED_RESCHED);
  587. cpu = task_cpu(p);
  588. if (cpu == smp_processor_id())
  589. return;
  590. /* NEED_RESCHED must be visible before we test polling */
  591. smp_mb();
  592. if (!tsk_is_polling(p))
  593. smp_send_reschedule(cpu);
  594. }
  595. static void resched_cpu(int cpu)
  596. {
  597. struct rq *rq = cpu_rq(cpu);
  598. unsigned long flags;
  599. if (!spin_trylock_irqsave(&rq->lock, flags))
  600. return;
  601. resched_task(cpu_curr(cpu));
  602. spin_unlock_irqrestore(&rq->lock, flags);
  603. }
  604. #else
  605. static inline void resched_task(struct task_struct *p)
  606. {
  607. assert_spin_locked(&task_rq(p)->lock);
  608. set_tsk_need_resched(p);
  609. }
  610. #endif
  611. #if BITS_PER_LONG == 32
  612. # define WMULT_CONST (~0UL)
  613. #else
  614. # define WMULT_CONST (1UL << 32)
  615. #endif
  616. #define WMULT_SHIFT 32
  617. /*
  618. * Shift right and round:
  619. */
  620. #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
  621. static unsigned long
  622. calc_delta_mine(unsigned long delta_exec, unsigned long weight,
  623. struct load_weight *lw)
  624. {
  625. u64 tmp;
  626. if (unlikely(!lw->inv_weight))
  627. lw->inv_weight = (WMULT_CONST - lw->weight/2) / lw->weight + 1;
  628. tmp = (u64)delta_exec * weight;
  629. /*
  630. * Check whether we'd overflow the 64-bit multiplication:
  631. */
  632. if (unlikely(tmp > WMULT_CONST))
  633. tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
  634. WMULT_SHIFT/2);
  635. else
  636. tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
  637. return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
  638. }
  639. static inline unsigned long
  640. calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
  641. {
  642. return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
  643. }
  644. static inline void update_load_add(struct load_weight *lw, unsigned long inc)
  645. {
  646. lw->weight += inc;
  647. }
  648. static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
  649. {
  650. lw->weight -= dec;
  651. }
  652. /*
  653. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  654. * of tasks with abnormal "nice" values across CPUs the contribution that
  655. * each task makes to its run queue's load is weighted according to its
  656. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  657. * scaled version of the new time slice allocation that they receive on time
  658. * slice expiry etc.
  659. */
  660. #define WEIGHT_IDLEPRIO 2
  661. #define WMULT_IDLEPRIO (1 << 31)
  662. /*
  663. * Nice levels are multiplicative, with a gentle 10% change for every
  664. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  665. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  666. * that remained on nice 0.
  667. *
  668. * The "10% effect" is relative and cumulative: from _any_ nice level,
  669. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  670. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  671. * If a task goes up by ~10% and another task goes down by ~10% then
  672. * the relative distance between them is ~25%.)
  673. */
  674. static const int prio_to_weight[40] = {
  675. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  676. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  677. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  678. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  679. /* 0 */ 1024, 820, 655, 526, 423,
  680. /* 5 */ 335, 272, 215, 172, 137,
  681. /* 10 */ 110, 87, 70, 56, 45,
  682. /* 15 */ 36, 29, 23, 18, 15,
  683. };
  684. /*
  685. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  686. *
  687. * In cases where the weight does not change often, we can use the
  688. * precalculated inverse to speed up arithmetics by turning divisions
  689. * into multiplications:
  690. */
  691. static const u32 prio_to_wmult[40] = {
  692. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  693. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  694. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  695. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  696. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  697. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  698. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  699. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  700. };
  701. static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
  702. /*
  703. * runqueue iterator, to support SMP load-balancing between different
  704. * scheduling classes, without having to expose their internal data
  705. * structures to the load-balancing proper:
  706. */
  707. struct rq_iterator {
  708. void *arg;
  709. struct task_struct *(*start)(void *);
  710. struct task_struct *(*next)(void *);
  711. };
  712. static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  713. unsigned long max_nr_move, unsigned long max_load_move,
  714. struct sched_domain *sd, enum cpu_idle_type idle,
  715. int *all_pinned, unsigned long *load_moved,
  716. int *this_best_prio, struct rq_iterator *iterator);
  717. #include "sched_stats.h"
  718. #include "sched_rt.c"
  719. #include "sched_fair.c"
  720. #include "sched_idletask.c"
  721. #ifdef CONFIG_SCHED_DEBUG
  722. # include "sched_debug.c"
  723. #endif
  724. #define sched_class_highest (&rt_sched_class)
  725. /*
  726. * Update delta_exec, delta_fair fields for rq.
  727. *
  728. * delta_fair clock advances at a rate inversely proportional to
  729. * total load (rq->load.weight) on the runqueue, while
  730. * delta_exec advances at the same rate as wall-clock (provided
  731. * cpu is not idle).
  732. *
  733. * delta_exec / delta_fair is a measure of the (smoothened) load on this
  734. * runqueue over any given interval. This (smoothened) load is used
  735. * during load balance.
  736. *
  737. * This function is called /before/ updating rq->load
  738. * and when switching tasks.
  739. */
  740. static inline void inc_load(struct rq *rq, const struct task_struct *p)
  741. {
  742. update_load_add(&rq->load, p->se.load.weight);
  743. }
  744. static inline void dec_load(struct rq *rq, const struct task_struct *p)
  745. {
  746. update_load_sub(&rq->load, p->se.load.weight);
  747. }
  748. static void inc_nr_running(struct task_struct *p, struct rq *rq)
  749. {
  750. rq->nr_running++;
  751. inc_load(rq, p);
  752. }
  753. static void dec_nr_running(struct task_struct *p, struct rq *rq)
  754. {
  755. rq->nr_running--;
  756. dec_load(rq, p);
  757. }
  758. static void set_load_weight(struct task_struct *p)
  759. {
  760. if (task_has_rt_policy(p)) {
  761. p->se.load.weight = prio_to_weight[0] * 2;
  762. p->se.load.inv_weight = prio_to_wmult[0] >> 1;
  763. return;
  764. }
  765. /*
  766. * SCHED_IDLE tasks get minimal weight:
  767. */
  768. if (p->policy == SCHED_IDLE) {
  769. p->se.load.weight = WEIGHT_IDLEPRIO;
  770. p->se.load.inv_weight = WMULT_IDLEPRIO;
  771. return;
  772. }
  773. p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
  774. p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
  775. }
  776. static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
  777. {
  778. sched_info_queued(p);
  779. p->sched_class->enqueue_task(rq, p, wakeup);
  780. p->se.on_rq = 1;
  781. }
  782. static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
  783. {
  784. p->sched_class->dequeue_task(rq, p, sleep);
  785. p->se.on_rq = 0;
  786. }
  787. /*
  788. * __normal_prio - return the priority that is based on the static prio
  789. */
  790. static inline int __normal_prio(struct task_struct *p)
  791. {
  792. return p->static_prio;
  793. }
  794. /*
  795. * Calculate the expected normal priority: i.e. priority
  796. * without taking RT-inheritance into account. Might be
  797. * boosted by interactivity modifiers. Changes upon fork,
  798. * setprio syscalls, and whenever the interactivity
  799. * estimator recalculates.
  800. */
  801. static inline int normal_prio(struct task_struct *p)
  802. {
  803. int prio;
  804. if (task_has_rt_policy(p))
  805. prio = MAX_RT_PRIO-1 - p->rt_priority;
  806. else
  807. prio = __normal_prio(p);
  808. return prio;
  809. }
  810. /*
  811. * Calculate the current priority, i.e. the priority
  812. * taken into account by the scheduler. This value might
  813. * be boosted by RT tasks, or might be boosted by
  814. * interactivity modifiers. Will be RT if the task got
  815. * RT-boosted. If not then it returns p->normal_prio.
  816. */
  817. static int effective_prio(struct task_struct *p)
  818. {
  819. p->normal_prio = normal_prio(p);
  820. /*
  821. * If we are RT tasks or we were boosted to RT priority,
  822. * keep the priority unchanged. Otherwise, update priority
  823. * to the normal priority:
  824. */
  825. if (!rt_prio(p->prio))
  826. return p->normal_prio;
  827. return p->prio;
  828. }
  829. /*
  830. * activate_task - move a task to the runqueue.
  831. */
  832. static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
  833. {
  834. if (p->state == TASK_UNINTERRUPTIBLE)
  835. rq->nr_uninterruptible--;
  836. enqueue_task(rq, p, wakeup);
  837. inc_nr_running(p, rq);
  838. }
  839. /*
  840. * activate_idle_task - move idle task to the _front_ of runqueue.
  841. */
  842. static inline void activate_idle_task(struct task_struct *p, struct rq *rq)
  843. {
  844. update_rq_clock(rq);
  845. if (p->state == TASK_UNINTERRUPTIBLE)
  846. rq->nr_uninterruptible--;
  847. enqueue_task(rq, p, 0);
  848. inc_nr_running(p, rq);
  849. }
  850. /*
  851. * deactivate_task - remove a task from the runqueue.
  852. */
  853. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
  854. {
  855. if (p->state == TASK_UNINTERRUPTIBLE)
  856. rq->nr_uninterruptible++;
  857. dequeue_task(rq, p, sleep);
  858. dec_nr_running(p, rq);
  859. }
  860. /**
  861. * task_curr - is this task currently executing on a CPU?
  862. * @p: the task in question.
  863. */
  864. inline int task_curr(const struct task_struct *p)
  865. {
  866. return cpu_curr(task_cpu(p)) == p;
  867. }
  868. /* Used instead of source_load when we know the type == 0 */
  869. unsigned long weighted_cpuload(const int cpu)
  870. {
  871. return cpu_rq(cpu)->load.weight;
  872. }
  873. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  874. {
  875. #ifdef CONFIG_SMP
  876. task_thread_info(p)->cpu = cpu;
  877. #endif
  878. set_task_cfs_rq(p);
  879. }
  880. #ifdef CONFIG_SMP
  881. void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
  882. {
  883. int old_cpu = task_cpu(p);
  884. struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
  885. u64 clock_offset;
  886. clock_offset = old_rq->clock - new_rq->clock;
  887. #ifdef CONFIG_SCHEDSTATS
  888. if (p->se.wait_start)
  889. p->se.wait_start -= clock_offset;
  890. if (p->se.sleep_start)
  891. p->se.sleep_start -= clock_offset;
  892. if (p->se.block_start)
  893. p->se.block_start -= clock_offset;
  894. #endif
  895. if (likely(new_rq->cfs.min_vruntime))
  896. p->se.vruntime -= old_rq->cfs.min_vruntime -
  897. new_rq->cfs.min_vruntime;
  898. __set_task_cpu(p, new_cpu);
  899. }
  900. struct migration_req {
  901. struct list_head list;
  902. struct task_struct *task;
  903. int dest_cpu;
  904. struct completion done;
  905. };
  906. /*
  907. * The task's runqueue lock must be held.
  908. * Returns true if you have to wait for migration thread.
  909. */
  910. static int
  911. migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
  912. {
  913. struct rq *rq = task_rq(p);
  914. /*
  915. * If the task is not on a runqueue (and not running), then
  916. * it is sufficient to simply update the task's cpu field.
  917. */
  918. if (!p->se.on_rq && !task_running(rq, p)) {
  919. set_task_cpu(p, dest_cpu);
  920. return 0;
  921. }
  922. init_completion(&req->done);
  923. req->task = p;
  924. req->dest_cpu = dest_cpu;
  925. list_add(&req->list, &rq->migration_queue);
  926. return 1;
  927. }
  928. /*
  929. * wait_task_inactive - wait for a thread to unschedule.
  930. *
  931. * The caller must ensure that the task *will* unschedule sometime soon,
  932. * else this function might spin for a *long* time. This function can't
  933. * be called with interrupts off, or it may introduce deadlock with
  934. * smp_call_function() if an IPI is sent by the same process we are
  935. * waiting to become inactive.
  936. */
  937. void wait_task_inactive(struct task_struct *p)
  938. {
  939. unsigned long flags;
  940. int running, on_rq;
  941. struct rq *rq;
  942. repeat:
  943. /*
  944. * We do the initial early heuristics without holding
  945. * any task-queue locks at all. We'll only try to get
  946. * the runqueue lock when things look like they will
  947. * work out!
  948. */
  949. rq = task_rq(p);
  950. /*
  951. * If the task is actively running on another CPU
  952. * still, just relax and busy-wait without holding
  953. * any locks.
  954. *
  955. * NOTE! Since we don't hold any locks, it's not
  956. * even sure that "rq" stays as the right runqueue!
  957. * But we don't care, since "task_running()" will
  958. * return false if the runqueue has changed and p
  959. * is actually now running somewhere else!
  960. */
  961. while (task_running(rq, p))
  962. cpu_relax();
  963. /*
  964. * Ok, time to look more closely! We need the rq
  965. * lock now, to be *sure*. If we're wrong, we'll
  966. * just go back and repeat.
  967. */
  968. rq = task_rq_lock(p, &flags);
  969. running = task_running(rq, p);
  970. on_rq = p->se.on_rq;
  971. task_rq_unlock(rq, &flags);
  972. /*
  973. * Was it really running after all now that we
  974. * checked with the proper locks actually held?
  975. *
  976. * Oops. Go back and try again..
  977. */
  978. if (unlikely(running)) {
  979. cpu_relax();
  980. goto repeat;
  981. }
  982. /*
  983. * It's not enough that it's not actively running,
  984. * it must be off the runqueue _entirely_, and not
  985. * preempted!
  986. *
  987. * So if it wa still runnable (but just not actively
  988. * running right now), it's preempted, and we should
  989. * yield - it could be a while.
  990. */
  991. if (unlikely(on_rq)) {
  992. yield();
  993. goto repeat;
  994. }
  995. /*
  996. * Ahh, all good. It wasn't running, and it wasn't
  997. * runnable, which means that it will never become
  998. * running in the future either. We're all done!
  999. */
  1000. }
  1001. /***
  1002. * kick_process - kick a running thread to enter/exit the kernel
  1003. * @p: the to-be-kicked thread
  1004. *
  1005. * Cause a process which is running on another CPU to enter
  1006. * kernel-mode, without any delay. (to get signals handled.)
  1007. *
  1008. * NOTE: this function doesnt have to take the runqueue lock,
  1009. * because all it wants to ensure is that the remote task enters
  1010. * the kernel. If the IPI races and the task has been migrated
  1011. * to another CPU then no harm is done and the purpose has been
  1012. * achieved as well.
  1013. */
  1014. void kick_process(struct task_struct *p)
  1015. {
  1016. int cpu;
  1017. preempt_disable();
  1018. cpu = task_cpu(p);
  1019. if ((cpu != smp_processor_id()) && task_curr(p))
  1020. smp_send_reschedule(cpu);
  1021. preempt_enable();
  1022. }
  1023. /*
  1024. * Return a low guess at the load of a migration-source cpu weighted
  1025. * according to the scheduling class and "nice" value.
  1026. *
  1027. * We want to under-estimate the load of migration sources, to
  1028. * balance conservatively.
  1029. */
  1030. static inline unsigned long source_load(int cpu, int type)
  1031. {
  1032. struct rq *rq = cpu_rq(cpu);
  1033. unsigned long total = weighted_cpuload(cpu);
  1034. if (type == 0)
  1035. return total;
  1036. return min(rq->cpu_load[type-1], total);
  1037. }
  1038. /*
  1039. * Return a high guess at the load of a migration-target cpu weighted
  1040. * according to the scheduling class and "nice" value.
  1041. */
  1042. static inline unsigned long target_load(int cpu, int type)
  1043. {
  1044. struct rq *rq = cpu_rq(cpu);
  1045. unsigned long total = weighted_cpuload(cpu);
  1046. if (type == 0)
  1047. return total;
  1048. return max(rq->cpu_load[type-1], total);
  1049. }
  1050. /*
  1051. * Return the average load per task on the cpu's run queue
  1052. */
  1053. static inline unsigned long cpu_avg_load_per_task(int cpu)
  1054. {
  1055. struct rq *rq = cpu_rq(cpu);
  1056. unsigned long total = weighted_cpuload(cpu);
  1057. unsigned long n = rq->nr_running;
  1058. return n ? total / n : SCHED_LOAD_SCALE;
  1059. }
  1060. /*
  1061. * find_idlest_group finds and returns the least busy CPU group within the
  1062. * domain.
  1063. */
  1064. static struct sched_group *
  1065. find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
  1066. {
  1067. struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
  1068. unsigned long min_load = ULONG_MAX, this_load = 0;
  1069. int load_idx = sd->forkexec_idx;
  1070. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  1071. do {
  1072. unsigned long load, avg_load;
  1073. int local_group;
  1074. int i;
  1075. /* Skip over this group if it has no CPUs allowed */
  1076. if (!cpus_intersects(group->cpumask, p->cpus_allowed))
  1077. goto nextgroup;
  1078. local_group = cpu_isset(this_cpu, group->cpumask);
  1079. /* Tally up the load of all CPUs in the group */
  1080. avg_load = 0;
  1081. for_each_cpu_mask(i, group->cpumask) {
  1082. /* Bias balancing toward cpus of our domain */
  1083. if (local_group)
  1084. load = source_load(i, load_idx);
  1085. else
  1086. load = target_load(i, load_idx);
  1087. avg_load += load;
  1088. }
  1089. /* Adjust by relative CPU power of the group */
  1090. avg_load = sg_div_cpu_power(group,
  1091. avg_load * SCHED_LOAD_SCALE);
  1092. if (local_group) {
  1093. this_load = avg_load;
  1094. this = group;
  1095. } else if (avg_load < min_load) {
  1096. min_load = avg_load;
  1097. idlest = group;
  1098. }
  1099. nextgroup:
  1100. group = group->next;
  1101. } while (group != sd->groups);
  1102. if (!idlest || 100*this_load < imbalance*min_load)
  1103. return NULL;
  1104. return idlest;
  1105. }
  1106. /*
  1107. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  1108. */
  1109. static int
  1110. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  1111. {
  1112. cpumask_t tmp;
  1113. unsigned long load, min_load = ULONG_MAX;
  1114. int idlest = -1;
  1115. int i;
  1116. /* Traverse only the allowed CPUs */
  1117. cpus_and(tmp, group->cpumask, p->cpus_allowed);
  1118. for_each_cpu_mask(i, tmp) {
  1119. load = weighted_cpuload(i);
  1120. if (load < min_load || (load == min_load && i == this_cpu)) {
  1121. min_load = load;
  1122. idlest = i;
  1123. }
  1124. }
  1125. return idlest;
  1126. }
  1127. /*
  1128. * sched_balance_self: balance the current task (running on cpu) in domains
  1129. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  1130. * SD_BALANCE_EXEC.
  1131. *
  1132. * Balance, ie. select the least loaded group.
  1133. *
  1134. * Returns the target CPU number, or the same CPU if no balancing is needed.
  1135. *
  1136. * preempt must be disabled.
  1137. */
  1138. static int sched_balance_self(int cpu, int flag)
  1139. {
  1140. struct task_struct *t = current;
  1141. struct sched_domain *tmp, *sd = NULL;
  1142. for_each_domain(cpu, tmp) {
  1143. /*
  1144. * If power savings logic is enabled for a domain, stop there.
  1145. */
  1146. if (tmp->flags & SD_POWERSAVINGS_BALANCE)
  1147. break;
  1148. if (tmp->flags & flag)
  1149. sd = tmp;
  1150. }
  1151. while (sd) {
  1152. cpumask_t span;
  1153. struct sched_group *group;
  1154. int new_cpu, weight;
  1155. if (!(sd->flags & flag)) {
  1156. sd = sd->child;
  1157. continue;
  1158. }
  1159. span = sd->span;
  1160. group = find_idlest_group(sd, t, cpu);
  1161. if (!group) {
  1162. sd = sd->child;
  1163. continue;
  1164. }
  1165. new_cpu = find_idlest_cpu(group, t, cpu);
  1166. if (new_cpu == -1 || new_cpu == cpu) {
  1167. /* Now try balancing at a lower domain level of cpu */
  1168. sd = sd->child;
  1169. continue;
  1170. }
  1171. /* Now try balancing at a lower domain level of new_cpu */
  1172. cpu = new_cpu;
  1173. sd = NULL;
  1174. weight = cpus_weight(span);
  1175. for_each_domain(cpu, tmp) {
  1176. if (weight <= cpus_weight(tmp->span))
  1177. break;
  1178. if (tmp->flags & flag)
  1179. sd = tmp;
  1180. }
  1181. /* while loop will break here if sd == NULL */
  1182. }
  1183. return cpu;
  1184. }
  1185. #endif /* CONFIG_SMP */
  1186. /*
  1187. * wake_idle() will wake a task on an idle cpu if task->cpu is
  1188. * not idle and an idle cpu is available. The span of cpus to
  1189. * search starts with cpus closest then further out as needed,
  1190. * so we always favor a closer, idle cpu.
  1191. *
  1192. * Returns the CPU we should wake onto.
  1193. */
  1194. #if defined(ARCH_HAS_SCHED_WAKE_IDLE)
  1195. static int wake_idle(int cpu, struct task_struct *p)
  1196. {
  1197. cpumask_t tmp;
  1198. struct sched_domain *sd;
  1199. int i;
  1200. /*
  1201. * If it is idle, then it is the best cpu to run this task.
  1202. *
  1203. * This cpu is also the best, if it has more than one task already.
  1204. * Siblings must be also busy(in most cases) as they didn't already
  1205. * pickup the extra load from this cpu and hence we need not check
  1206. * sibling runqueue info. This will avoid the checks and cache miss
  1207. * penalities associated with that.
  1208. */
  1209. if (idle_cpu(cpu) || cpu_rq(cpu)->nr_running > 1)
  1210. return cpu;
  1211. for_each_domain(cpu, sd) {
  1212. if (sd->flags & SD_WAKE_IDLE) {
  1213. cpus_and(tmp, sd->span, p->cpus_allowed);
  1214. for_each_cpu_mask(i, tmp) {
  1215. if (idle_cpu(i))
  1216. return i;
  1217. }
  1218. } else {
  1219. break;
  1220. }
  1221. }
  1222. return cpu;
  1223. }
  1224. #else
  1225. static inline int wake_idle(int cpu, struct task_struct *p)
  1226. {
  1227. return cpu;
  1228. }
  1229. #endif
  1230. /***
  1231. * try_to_wake_up - wake up a thread
  1232. * @p: the to-be-woken-up thread
  1233. * @state: the mask of task states that can be woken
  1234. * @sync: do a synchronous wakeup?
  1235. *
  1236. * Put it on the run-queue if it's not already there. The "current"
  1237. * thread is always on the run-queue (except when the actual
  1238. * re-schedule is in progress), and as such you're allowed to do
  1239. * the simpler "current->state = TASK_RUNNING" to mark yourself
  1240. * runnable without the overhead of this.
  1241. *
  1242. * returns failure only if the task is already active.
  1243. */
  1244. static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
  1245. {
  1246. int cpu, this_cpu, success = 0;
  1247. unsigned long flags;
  1248. long old_state;
  1249. struct rq *rq;
  1250. #ifdef CONFIG_SMP
  1251. struct sched_domain *sd, *this_sd = NULL;
  1252. unsigned long load, this_load;
  1253. int new_cpu;
  1254. #endif
  1255. rq = task_rq_lock(p, &flags);
  1256. old_state = p->state;
  1257. if (!(old_state & state))
  1258. goto out;
  1259. if (p->se.on_rq)
  1260. goto out_running;
  1261. cpu = task_cpu(p);
  1262. this_cpu = smp_processor_id();
  1263. #ifdef CONFIG_SMP
  1264. if (unlikely(task_running(rq, p)))
  1265. goto out_activate;
  1266. new_cpu = cpu;
  1267. schedstat_inc(rq, ttwu_cnt);
  1268. if (cpu == this_cpu) {
  1269. schedstat_inc(rq, ttwu_local);
  1270. goto out_set_cpu;
  1271. }
  1272. for_each_domain(this_cpu, sd) {
  1273. if (cpu_isset(cpu, sd->span)) {
  1274. schedstat_inc(sd, ttwu_wake_remote);
  1275. this_sd = sd;
  1276. break;
  1277. }
  1278. }
  1279. if (unlikely(!cpu_isset(this_cpu, p->cpus_allowed)))
  1280. goto out_set_cpu;
  1281. /*
  1282. * Check for affine wakeup and passive balancing possibilities.
  1283. */
  1284. if (this_sd) {
  1285. int idx = this_sd->wake_idx;
  1286. unsigned int imbalance;
  1287. imbalance = 100 + (this_sd->imbalance_pct - 100) / 2;
  1288. load = source_load(cpu, idx);
  1289. this_load = target_load(this_cpu, idx);
  1290. new_cpu = this_cpu; /* Wake to this CPU if we can */
  1291. if (this_sd->flags & SD_WAKE_AFFINE) {
  1292. unsigned long tl = this_load;
  1293. unsigned long tl_per_task;
  1294. tl_per_task = cpu_avg_load_per_task(this_cpu);
  1295. /*
  1296. * If sync wakeup then subtract the (maximum possible)
  1297. * effect of the currently running task from the load
  1298. * of the current CPU:
  1299. */
  1300. if (sync)
  1301. tl -= current->se.load.weight;
  1302. if ((tl <= load &&
  1303. tl + target_load(cpu, idx) <= tl_per_task) ||
  1304. 100*(tl + p->se.load.weight) <= imbalance*load) {
  1305. /*
  1306. * This domain has SD_WAKE_AFFINE and
  1307. * p is cache cold in this domain, and
  1308. * there is no bad imbalance.
  1309. */
  1310. schedstat_inc(this_sd, ttwu_move_affine);
  1311. goto out_set_cpu;
  1312. }
  1313. }
  1314. /*
  1315. * Start passive balancing when half the imbalance_pct
  1316. * limit is reached.
  1317. */
  1318. if (this_sd->flags & SD_WAKE_BALANCE) {
  1319. if (imbalance*this_load <= 100*load) {
  1320. schedstat_inc(this_sd, ttwu_move_balance);
  1321. goto out_set_cpu;
  1322. }
  1323. }
  1324. }
  1325. new_cpu = cpu; /* Could not wake to this_cpu. Wake to cpu instead */
  1326. out_set_cpu:
  1327. new_cpu = wake_idle(new_cpu, p);
  1328. if (new_cpu != cpu) {
  1329. set_task_cpu(p, new_cpu);
  1330. task_rq_unlock(rq, &flags);
  1331. /* might preempt at this point */
  1332. rq = task_rq_lock(p, &flags);
  1333. old_state = p->state;
  1334. if (!(old_state & state))
  1335. goto out;
  1336. if (p->se.on_rq)
  1337. goto out_running;
  1338. this_cpu = smp_processor_id();
  1339. cpu = task_cpu(p);
  1340. }
  1341. out_activate:
  1342. #endif /* CONFIG_SMP */
  1343. update_rq_clock(rq);
  1344. activate_task(rq, p, 1);
  1345. /*
  1346. * Sync wakeups (i.e. those types of wakeups where the waker
  1347. * has indicated that it will leave the CPU in short order)
  1348. * don't trigger a preemption, if the woken up task will run on
  1349. * this cpu. (in this case the 'I will reschedule' promise of
  1350. * the waker guarantees that the freshly woken up task is going
  1351. * to be considered on this CPU.)
  1352. */
  1353. if (!sync || cpu != this_cpu)
  1354. check_preempt_curr(rq, p);
  1355. success = 1;
  1356. out_running:
  1357. p->state = TASK_RUNNING;
  1358. out:
  1359. task_rq_unlock(rq, &flags);
  1360. return success;
  1361. }
  1362. int fastcall wake_up_process(struct task_struct *p)
  1363. {
  1364. return try_to_wake_up(p, TASK_STOPPED | TASK_TRACED |
  1365. TASK_INTERRUPTIBLE | TASK_UNINTERRUPTIBLE, 0);
  1366. }
  1367. EXPORT_SYMBOL(wake_up_process);
  1368. int fastcall wake_up_state(struct task_struct *p, unsigned int state)
  1369. {
  1370. return try_to_wake_up(p, state, 0);
  1371. }
  1372. /*
  1373. * Perform scheduler related setup for a newly forked process p.
  1374. * p is forked by current.
  1375. *
  1376. * __sched_fork() is basic setup used by init_idle() too:
  1377. */
  1378. static void __sched_fork(struct task_struct *p)
  1379. {
  1380. p->se.exec_start = 0;
  1381. p->se.sum_exec_runtime = 0;
  1382. p->se.prev_sum_exec_runtime = 0;
  1383. #ifdef CONFIG_SCHEDSTATS
  1384. p->se.wait_start = 0;
  1385. p->se.sum_sleep_runtime = 0;
  1386. p->se.sleep_start = 0;
  1387. p->se.block_start = 0;
  1388. p->se.sleep_max = 0;
  1389. p->se.block_max = 0;
  1390. p->se.exec_max = 0;
  1391. p->se.slice_max = 0;
  1392. p->se.wait_max = 0;
  1393. #endif
  1394. INIT_LIST_HEAD(&p->run_list);
  1395. p->se.on_rq = 0;
  1396. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1397. INIT_HLIST_HEAD(&p->preempt_notifiers);
  1398. #endif
  1399. /*
  1400. * We mark the process as running here, but have not actually
  1401. * inserted it onto the runqueue yet. This guarantees that
  1402. * nobody will actually run it, and a signal or other external
  1403. * event cannot wake it up and insert it on the runqueue either.
  1404. */
  1405. p->state = TASK_RUNNING;
  1406. }
  1407. /*
  1408. * fork()/clone()-time setup:
  1409. */
  1410. void sched_fork(struct task_struct *p, int clone_flags)
  1411. {
  1412. int cpu = get_cpu();
  1413. __sched_fork(p);
  1414. #ifdef CONFIG_SMP
  1415. cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
  1416. #endif
  1417. __set_task_cpu(p, cpu);
  1418. /*
  1419. * Make sure we do not leak PI boosting priority to the child:
  1420. */
  1421. p->prio = current->normal_prio;
  1422. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  1423. if (likely(sched_info_on()))
  1424. memset(&p->sched_info, 0, sizeof(p->sched_info));
  1425. #endif
  1426. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  1427. p->oncpu = 0;
  1428. #endif
  1429. #ifdef CONFIG_PREEMPT
  1430. /* Want to start with kernel preemption disabled. */
  1431. task_thread_info(p)->preempt_count = 1;
  1432. #endif
  1433. put_cpu();
  1434. }
  1435. /*
  1436. * wake_up_new_task - wake up a newly created task for the first time.
  1437. *
  1438. * This function will do some initial scheduler statistics housekeeping
  1439. * that must be done for every newly created context, then puts the task
  1440. * on the runqueue and wakes it.
  1441. */
  1442. void fastcall wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
  1443. {
  1444. unsigned long flags;
  1445. struct rq *rq;
  1446. int this_cpu;
  1447. rq = task_rq_lock(p, &flags);
  1448. BUG_ON(p->state != TASK_RUNNING);
  1449. this_cpu = smp_processor_id(); /* parent's CPU */
  1450. update_rq_clock(rq);
  1451. p->prio = effective_prio(p);
  1452. if (rt_prio(p->prio))
  1453. p->sched_class = &rt_sched_class;
  1454. else
  1455. p->sched_class = &fair_sched_class;
  1456. if (task_cpu(p) != this_cpu || !p->sched_class->task_new ||
  1457. !current->se.on_rq) {
  1458. activate_task(rq, p, 0);
  1459. } else {
  1460. /*
  1461. * Let the scheduling class do new task startup
  1462. * management (if any):
  1463. */
  1464. p->sched_class->task_new(rq, p);
  1465. inc_nr_running(p, rq);
  1466. }
  1467. check_preempt_curr(rq, p);
  1468. task_rq_unlock(rq, &flags);
  1469. }
  1470. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1471. /**
  1472. * preempt_notifier_register - tell me when current is being being preempted & rescheduled
  1473. * @notifier: notifier struct to register
  1474. */
  1475. void preempt_notifier_register(struct preempt_notifier *notifier)
  1476. {
  1477. hlist_add_head(&notifier->link, &current->preempt_notifiers);
  1478. }
  1479. EXPORT_SYMBOL_GPL(preempt_notifier_register);
  1480. /**
  1481. * preempt_notifier_unregister - no longer interested in preemption notifications
  1482. * @notifier: notifier struct to unregister
  1483. *
  1484. * This is safe to call from within a preemption notifier.
  1485. */
  1486. void preempt_notifier_unregister(struct preempt_notifier *notifier)
  1487. {
  1488. hlist_del(&notifier->link);
  1489. }
  1490. EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
  1491. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1492. {
  1493. struct preempt_notifier *notifier;
  1494. struct hlist_node *node;
  1495. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  1496. notifier->ops->sched_in(notifier, raw_smp_processor_id());
  1497. }
  1498. static void
  1499. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1500. struct task_struct *next)
  1501. {
  1502. struct preempt_notifier *notifier;
  1503. struct hlist_node *node;
  1504. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  1505. notifier->ops->sched_out(notifier, next);
  1506. }
  1507. #else
  1508. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1509. {
  1510. }
  1511. static void
  1512. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1513. struct task_struct *next)
  1514. {
  1515. }
  1516. #endif
  1517. /**
  1518. * prepare_task_switch - prepare to switch tasks
  1519. * @rq: the runqueue preparing to switch
  1520. * @prev: the current task that is being switched out
  1521. * @next: the task we are going to switch to.
  1522. *
  1523. * This is called with the rq lock held and interrupts off. It must
  1524. * be paired with a subsequent finish_task_switch after the context
  1525. * switch.
  1526. *
  1527. * prepare_task_switch sets up locking and calls architecture specific
  1528. * hooks.
  1529. */
  1530. static inline void
  1531. prepare_task_switch(struct rq *rq, struct task_struct *prev,
  1532. struct task_struct *next)
  1533. {
  1534. fire_sched_out_preempt_notifiers(prev, next);
  1535. prepare_lock_switch(rq, next);
  1536. prepare_arch_switch(next);
  1537. }
  1538. /**
  1539. * finish_task_switch - clean up after a task-switch
  1540. * @rq: runqueue associated with task-switch
  1541. * @prev: the thread we just switched away from.
  1542. *
  1543. * finish_task_switch must be called after the context switch, paired
  1544. * with a prepare_task_switch call before the context switch.
  1545. * finish_task_switch will reconcile locking set up by prepare_task_switch,
  1546. * and do any other architecture-specific cleanup actions.
  1547. *
  1548. * Note that we may have delayed dropping an mm in context_switch(). If
  1549. * so, we finish that here outside of the runqueue lock. (Doing it
  1550. * with the lock held can cause deadlocks; see schedule() for
  1551. * details.)
  1552. */
  1553. static inline void finish_task_switch(struct rq *rq, struct task_struct *prev)
  1554. __releases(rq->lock)
  1555. {
  1556. struct mm_struct *mm = rq->prev_mm;
  1557. long prev_state;
  1558. rq->prev_mm = NULL;
  1559. /*
  1560. * A task struct has one reference for the use as "current".
  1561. * If a task dies, then it sets TASK_DEAD in tsk->state and calls
  1562. * schedule one last time. The schedule call will never return, and
  1563. * the scheduled task must drop that reference.
  1564. * The test for TASK_DEAD must occur while the runqueue locks are
  1565. * still held, otherwise prev could be scheduled on another cpu, die
  1566. * there before we look at prev->state, and then the reference would
  1567. * be dropped twice.
  1568. * Manfred Spraul <manfred@colorfullife.com>
  1569. */
  1570. prev_state = prev->state;
  1571. finish_arch_switch(prev);
  1572. finish_lock_switch(rq, prev);
  1573. fire_sched_in_preempt_notifiers(current);
  1574. if (mm)
  1575. mmdrop(mm);
  1576. if (unlikely(prev_state == TASK_DEAD)) {
  1577. /*
  1578. * Remove function-return probe instances associated with this
  1579. * task and put them back on the free list.
  1580. */
  1581. kprobe_flush_task(prev);
  1582. put_task_struct(prev);
  1583. }
  1584. }
  1585. /**
  1586. * schedule_tail - first thing a freshly forked thread must call.
  1587. * @prev: the thread we just switched away from.
  1588. */
  1589. asmlinkage void schedule_tail(struct task_struct *prev)
  1590. __releases(rq->lock)
  1591. {
  1592. struct rq *rq = this_rq();
  1593. finish_task_switch(rq, prev);
  1594. #ifdef __ARCH_WANT_UNLOCKED_CTXSW
  1595. /* In this case, finish_task_switch does not reenable preemption */
  1596. preempt_enable();
  1597. #endif
  1598. if (current->set_child_tid)
  1599. put_user(current->pid, current->set_child_tid);
  1600. }
  1601. /*
  1602. * context_switch - switch to the new MM and the new
  1603. * thread's register state.
  1604. */
  1605. static inline void
  1606. context_switch(struct rq *rq, struct task_struct *prev,
  1607. struct task_struct *next)
  1608. {
  1609. struct mm_struct *mm, *oldmm;
  1610. prepare_task_switch(rq, prev, next);
  1611. mm = next->mm;
  1612. oldmm = prev->active_mm;
  1613. /*
  1614. * For paravirt, this is coupled with an exit in switch_to to
  1615. * combine the page table reload and the switch backend into
  1616. * one hypercall.
  1617. */
  1618. arch_enter_lazy_cpu_mode();
  1619. if (unlikely(!mm)) {
  1620. next->active_mm = oldmm;
  1621. atomic_inc(&oldmm->mm_count);
  1622. enter_lazy_tlb(oldmm, next);
  1623. } else
  1624. switch_mm(oldmm, mm, next);
  1625. if (unlikely(!prev->mm)) {
  1626. prev->active_mm = NULL;
  1627. rq->prev_mm = oldmm;
  1628. }
  1629. /*
  1630. * Since the runqueue lock will be released by the next
  1631. * task (which is an invalid locking op but in the case
  1632. * of the scheduler it's an obvious special-case), so we
  1633. * do an early lockdep release here:
  1634. */
  1635. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  1636. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  1637. #endif
  1638. /* Here we just switch the register state and the stack. */
  1639. switch_to(prev, next, prev);
  1640. barrier();
  1641. /*
  1642. * this_rq must be evaluated again because prev may have moved
  1643. * CPUs since it called schedule(), thus the 'rq' on its stack
  1644. * frame will be invalid.
  1645. */
  1646. finish_task_switch(this_rq(), prev);
  1647. }
  1648. /*
  1649. * nr_running, nr_uninterruptible and nr_context_switches:
  1650. *
  1651. * externally visible scheduler statistics: current number of runnable
  1652. * threads, current number of uninterruptible-sleeping threads, total
  1653. * number of context switches performed since bootup.
  1654. */
  1655. unsigned long nr_running(void)
  1656. {
  1657. unsigned long i, sum = 0;
  1658. for_each_online_cpu(i)
  1659. sum += cpu_rq(i)->nr_running;
  1660. return sum;
  1661. }
  1662. unsigned long nr_uninterruptible(void)
  1663. {
  1664. unsigned long i, sum = 0;
  1665. for_each_possible_cpu(i)
  1666. sum += cpu_rq(i)->nr_uninterruptible;
  1667. /*
  1668. * Since we read the counters lockless, it might be slightly
  1669. * inaccurate. Do not allow it to go below zero though:
  1670. */
  1671. if (unlikely((long)sum < 0))
  1672. sum = 0;
  1673. return sum;
  1674. }
  1675. unsigned long long nr_context_switches(void)
  1676. {
  1677. int i;
  1678. unsigned long long sum = 0;
  1679. for_each_possible_cpu(i)
  1680. sum += cpu_rq(i)->nr_switches;
  1681. return sum;
  1682. }
  1683. unsigned long nr_iowait(void)
  1684. {
  1685. unsigned long i, sum = 0;
  1686. for_each_possible_cpu(i)
  1687. sum += atomic_read(&cpu_rq(i)->nr_iowait);
  1688. return sum;
  1689. }
  1690. unsigned long nr_active(void)
  1691. {
  1692. unsigned long i, running = 0, uninterruptible = 0;
  1693. for_each_online_cpu(i) {
  1694. running += cpu_rq(i)->nr_running;
  1695. uninterruptible += cpu_rq(i)->nr_uninterruptible;
  1696. }
  1697. if (unlikely((long)uninterruptible < 0))
  1698. uninterruptible = 0;
  1699. return running + uninterruptible;
  1700. }
  1701. /*
  1702. * Update rq->cpu_load[] statistics. This function is usually called every
  1703. * scheduler tick (TICK_NSEC).
  1704. */
  1705. static void update_cpu_load(struct rq *this_rq)
  1706. {
  1707. unsigned long this_load = this_rq->load.weight;
  1708. int i, scale;
  1709. this_rq->nr_load_updates++;
  1710. /* Update our load: */
  1711. for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
  1712. unsigned long old_load, new_load;
  1713. /* scale is effectively 1 << i now, and >> i divides by scale */
  1714. old_load = this_rq->cpu_load[i];
  1715. new_load = this_load;
  1716. /*
  1717. * Round up the averaging division if load is increasing. This
  1718. * prevents us from getting stuck on 9 if the load is 10, for
  1719. * example.
  1720. */
  1721. if (new_load > old_load)
  1722. new_load += scale-1;
  1723. this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
  1724. }
  1725. }
  1726. #ifdef CONFIG_SMP
  1727. /*
  1728. * double_rq_lock - safely lock two runqueues
  1729. *
  1730. * Note this does not disable interrupts like task_rq_lock,
  1731. * you need to do so manually before calling.
  1732. */
  1733. static void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1734. __acquires(rq1->lock)
  1735. __acquires(rq2->lock)
  1736. {
  1737. BUG_ON(!irqs_disabled());
  1738. if (rq1 == rq2) {
  1739. spin_lock(&rq1->lock);
  1740. __acquire(rq2->lock); /* Fake it out ;) */
  1741. } else {
  1742. if (rq1 < rq2) {
  1743. spin_lock(&rq1->lock);
  1744. spin_lock(&rq2->lock);
  1745. } else {
  1746. spin_lock(&rq2->lock);
  1747. spin_lock(&rq1->lock);
  1748. }
  1749. }
  1750. update_rq_clock(rq1);
  1751. update_rq_clock(rq2);
  1752. }
  1753. /*
  1754. * double_rq_unlock - safely unlock two runqueues
  1755. *
  1756. * Note this does not restore interrupts like task_rq_unlock,
  1757. * you need to do so manually after calling.
  1758. */
  1759. static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1760. __releases(rq1->lock)
  1761. __releases(rq2->lock)
  1762. {
  1763. spin_unlock(&rq1->lock);
  1764. if (rq1 != rq2)
  1765. spin_unlock(&rq2->lock);
  1766. else
  1767. __release(rq2->lock);
  1768. }
  1769. /*
  1770. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1771. */
  1772. static void double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1773. __releases(this_rq->lock)
  1774. __acquires(busiest->lock)
  1775. __acquires(this_rq->lock)
  1776. {
  1777. if (unlikely(!irqs_disabled())) {
  1778. /* printk() doesn't work good under rq->lock */
  1779. spin_unlock(&this_rq->lock);
  1780. BUG_ON(1);
  1781. }
  1782. if (unlikely(!spin_trylock(&busiest->lock))) {
  1783. if (busiest < this_rq) {
  1784. spin_unlock(&this_rq->lock);
  1785. spin_lock(&busiest->lock);
  1786. spin_lock(&this_rq->lock);
  1787. } else
  1788. spin_lock(&busiest->lock);
  1789. }
  1790. }
  1791. /*
  1792. * If dest_cpu is allowed for this process, migrate the task to it.
  1793. * This is accomplished by forcing the cpu_allowed mask to only
  1794. * allow dest_cpu, which will force the cpu onto dest_cpu. Then
  1795. * the cpu_allowed mask is restored.
  1796. */
  1797. static void sched_migrate_task(struct task_struct *p, int dest_cpu)
  1798. {
  1799. struct migration_req req;
  1800. unsigned long flags;
  1801. struct rq *rq;
  1802. rq = task_rq_lock(p, &flags);
  1803. if (!cpu_isset(dest_cpu, p->cpus_allowed)
  1804. || unlikely(cpu_is_offline(dest_cpu)))
  1805. goto out;
  1806. /* force the process onto the specified CPU */
  1807. if (migrate_task(p, dest_cpu, &req)) {
  1808. /* Need to wait for migration thread (might exit: take ref). */
  1809. struct task_struct *mt = rq->migration_thread;
  1810. get_task_struct(mt);
  1811. task_rq_unlock(rq, &flags);
  1812. wake_up_process(mt);
  1813. put_task_struct(mt);
  1814. wait_for_completion(&req.done);
  1815. return;
  1816. }
  1817. out:
  1818. task_rq_unlock(rq, &flags);
  1819. }
  1820. /*
  1821. * sched_exec - execve() is a valuable balancing opportunity, because at
  1822. * this point the task has the smallest effective memory and cache footprint.
  1823. */
  1824. void sched_exec(void)
  1825. {
  1826. int new_cpu, this_cpu = get_cpu();
  1827. new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
  1828. put_cpu();
  1829. if (new_cpu != this_cpu)
  1830. sched_migrate_task(current, new_cpu);
  1831. }
  1832. /*
  1833. * pull_task - move a task from a remote runqueue to the local runqueue.
  1834. * Both runqueues must be locked.
  1835. */
  1836. static void pull_task(struct rq *src_rq, struct task_struct *p,
  1837. struct rq *this_rq, int this_cpu)
  1838. {
  1839. deactivate_task(src_rq, p, 0);
  1840. set_task_cpu(p, this_cpu);
  1841. activate_task(this_rq, p, 0);
  1842. /*
  1843. * Note that idle threads have a prio of MAX_PRIO, for this test
  1844. * to be always true for them.
  1845. */
  1846. check_preempt_curr(this_rq, p);
  1847. }
  1848. /*
  1849. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  1850. */
  1851. static
  1852. int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
  1853. struct sched_domain *sd, enum cpu_idle_type idle,
  1854. int *all_pinned)
  1855. {
  1856. /*
  1857. * We do not migrate tasks that are:
  1858. * 1) running (obviously), or
  1859. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  1860. * 3) are cache-hot on their current CPU.
  1861. */
  1862. if (!cpu_isset(this_cpu, p->cpus_allowed))
  1863. return 0;
  1864. *all_pinned = 0;
  1865. if (task_running(rq, p))
  1866. return 0;
  1867. return 1;
  1868. }
  1869. static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1870. unsigned long max_nr_move, unsigned long max_load_move,
  1871. struct sched_domain *sd, enum cpu_idle_type idle,
  1872. int *all_pinned, unsigned long *load_moved,
  1873. int *this_best_prio, struct rq_iterator *iterator)
  1874. {
  1875. int pulled = 0, pinned = 0, skip_for_load;
  1876. struct task_struct *p;
  1877. long rem_load_move = max_load_move;
  1878. if (max_nr_move == 0 || max_load_move == 0)
  1879. goto out;
  1880. pinned = 1;
  1881. /*
  1882. * Start the load-balancing iterator:
  1883. */
  1884. p = iterator->start(iterator->arg);
  1885. next:
  1886. if (!p)
  1887. goto out;
  1888. /*
  1889. * To help distribute high priority tasks accross CPUs we don't
  1890. * skip a task if it will be the highest priority task (i.e. smallest
  1891. * prio value) on its new queue regardless of its load weight
  1892. */
  1893. skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
  1894. SCHED_LOAD_SCALE_FUZZ;
  1895. if ((skip_for_load && p->prio >= *this_best_prio) ||
  1896. !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
  1897. p = iterator->next(iterator->arg);
  1898. goto next;
  1899. }
  1900. pull_task(busiest, p, this_rq, this_cpu);
  1901. pulled++;
  1902. rem_load_move -= p->se.load.weight;
  1903. /*
  1904. * We only want to steal up to the prescribed number of tasks
  1905. * and the prescribed amount of weighted load.
  1906. */
  1907. if (pulled < max_nr_move && rem_load_move > 0) {
  1908. if (p->prio < *this_best_prio)
  1909. *this_best_prio = p->prio;
  1910. p = iterator->next(iterator->arg);
  1911. goto next;
  1912. }
  1913. out:
  1914. /*
  1915. * Right now, this is the only place pull_task() is called,
  1916. * so we can safely collect pull_task() stats here rather than
  1917. * inside pull_task().
  1918. */
  1919. schedstat_add(sd, lb_gained[idle], pulled);
  1920. if (all_pinned)
  1921. *all_pinned = pinned;
  1922. *load_moved = max_load_move - rem_load_move;
  1923. return pulled;
  1924. }
  1925. /*
  1926. * move_tasks tries to move up to max_load_move weighted load from busiest to
  1927. * this_rq, as part of a balancing operation within domain "sd".
  1928. * Returns 1 if successful and 0 otherwise.
  1929. *
  1930. * Called with both runqueues locked.
  1931. */
  1932. static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1933. unsigned long max_load_move,
  1934. struct sched_domain *sd, enum cpu_idle_type idle,
  1935. int *all_pinned)
  1936. {
  1937. struct sched_class *class = sched_class_highest;
  1938. unsigned long total_load_moved = 0;
  1939. int this_best_prio = this_rq->curr->prio;
  1940. do {
  1941. total_load_moved +=
  1942. class->load_balance(this_rq, this_cpu, busiest,
  1943. ULONG_MAX, max_load_move - total_load_moved,
  1944. sd, idle, all_pinned, &this_best_prio);
  1945. class = class->next;
  1946. } while (class && max_load_move > total_load_moved);
  1947. return total_load_moved > 0;
  1948. }
  1949. /*
  1950. * move_one_task tries to move exactly one task from busiest to this_rq, as
  1951. * part of active balancing operations within "domain".
  1952. * Returns 1 if successful and 0 otherwise.
  1953. *
  1954. * Called with both runqueues locked.
  1955. */
  1956. static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1957. struct sched_domain *sd, enum cpu_idle_type idle)
  1958. {
  1959. struct sched_class *class;
  1960. int this_best_prio = MAX_PRIO;
  1961. for (class = sched_class_highest; class; class = class->next)
  1962. if (class->load_balance(this_rq, this_cpu, busiest,
  1963. 1, ULONG_MAX, sd, idle, NULL,
  1964. &this_best_prio))
  1965. return 1;
  1966. return 0;
  1967. }
  1968. /*
  1969. * find_busiest_group finds and returns the busiest CPU group within the
  1970. * domain. It calculates and returns the amount of weighted load which
  1971. * should be moved to restore balance via the imbalance parameter.
  1972. */
  1973. static struct sched_group *
  1974. find_busiest_group(struct sched_domain *sd, int this_cpu,
  1975. unsigned long *imbalance, enum cpu_idle_type idle,
  1976. int *sd_idle, cpumask_t *cpus, int *balance)
  1977. {
  1978. struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
  1979. unsigned long max_load, avg_load, total_load, this_load, total_pwr;
  1980. unsigned long max_pull;
  1981. unsigned long busiest_load_per_task, busiest_nr_running;
  1982. unsigned long this_load_per_task, this_nr_running;
  1983. int load_idx;
  1984. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  1985. int power_savings_balance = 1;
  1986. unsigned long leader_nr_running = 0, min_load_per_task = 0;
  1987. unsigned long min_nr_running = ULONG_MAX;
  1988. struct sched_group *group_min = NULL, *group_leader = NULL;
  1989. #endif
  1990. max_load = this_load = total_load = total_pwr = 0;
  1991. busiest_load_per_task = busiest_nr_running = 0;
  1992. this_load_per_task = this_nr_running = 0;
  1993. if (idle == CPU_NOT_IDLE)
  1994. load_idx = sd->busy_idx;
  1995. else if (idle == CPU_NEWLY_IDLE)
  1996. load_idx = sd->newidle_idx;
  1997. else
  1998. load_idx = sd->idle_idx;
  1999. do {
  2000. unsigned long load, group_capacity;
  2001. int local_group;
  2002. int i;
  2003. unsigned int balance_cpu = -1, first_idle_cpu = 0;
  2004. unsigned long sum_nr_running, sum_weighted_load;
  2005. local_group = cpu_isset(this_cpu, group->cpumask);
  2006. if (local_group)
  2007. balance_cpu = first_cpu(group->cpumask);
  2008. /* Tally up the load of all CPUs in the group */
  2009. sum_weighted_load = sum_nr_running = avg_load = 0;
  2010. for_each_cpu_mask(i, group->cpumask) {
  2011. struct rq *rq;
  2012. if (!cpu_isset(i, *cpus))
  2013. continue;
  2014. rq = cpu_rq(i);
  2015. if (*sd_idle && rq->nr_running)
  2016. *sd_idle = 0;
  2017. /* Bias balancing toward cpus of our domain */
  2018. if (local_group) {
  2019. if (idle_cpu(i) && !first_idle_cpu) {
  2020. first_idle_cpu = 1;
  2021. balance_cpu = i;
  2022. }
  2023. load = target_load(i, load_idx);
  2024. } else
  2025. load = source_load(i, load_idx);
  2026. avg_load += load;
  2027. sum_nr_running += rq->nr_running;
  2028. sum_weighted_load += weighted_cpuload(i);
  2029. }
  2030. /*
  2031. * First idle cpu or the first cpu(busiest) in this sched group
  2032. * is eligible for doing load balancing at this and above
  2033. * domains. In the newly idle case, we will allow all the cpu's
  2034. * to do the newly idle load balance.
  2035. */
  2036. if (idle != CPU_NEWLY_IDLE && local_group &&
  2037. balance_cpu != this_cpu && balance) {
  2038. *balance = 0;
  2039. goto ret;
  2040. }
  2041. total_load += avg_load;
  2042. total_pwr += group->__cpu_power;
  2043. /* Adjust by relative CPU power of the group */
  2044. avg_load = sg_div_cpu_power(group,
  2045. avg_load * SCHED_LOAD_SCALE);
  2046. group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
  2047. if (local_group) {
  2048. this_load = avg_load;
  2049. this = group;
  2050. this_nr_running = sum_nr_running;
  2051. this_load_per_task = sum_weighted_load;
  2052. } else if (avg_load > max_load &&
  2053. sum_nr_running > group_capacity) {
  2054. max_load = avg_load;
  2055. busiest = group;
  2056. busiest_nr_running = sum_nr_running;
  2057. busiest_load_per_task = sum_weighted_load;
  2058. }
  2059. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2060. /*
  2061. * Busy processors will not participate in power savings
  2062. * balance.
  2063. */
  2064. if (idle == CPU_NOT_IDLE ||
  2065. !(sd->flags & SD_POWERSAVINGS_BALANCE))
  2066. goto group_next;
  2067. /*
  2068. * If the local group is idle or completely loaded
  2069. * no need to do power savings balance at this domain
  2070. */
  2071. if (local_group && (this_nr_running >= group_capacity ||
  2072. !this_nr_running))
  2073. power_savings_balance = 0;
  2074. /*
  2075. * If a group is already running at full capacity or idle,
  2076. * don't include that group in power savings calculations
  2077. */
  2078. if (!power_savings_balance || sum_nr_running >= group_capacity
  2079. || !sum_nr_running)
  2080. goto group_next;
  2081. /*
  2082. * Calculate the group which has the least non-idle load.
  2083. * This is the group from where we need to pick up the load
  2084. * for saving power
  2085. */
  2086. if ((sum_nr_running < min_nr_running) ||
  2087. (sum_nr_running == min_nr_running &&
  2088. first_cpu(group->cpumask) <
  2089. first_cpu(group_min->cpumask))) {
  2090. group_min = group;
  2091. min_nr_running = sum_nr_running;
  2092. min_load_per_task = sum_weighted_load /
  2093. sum_nr_running;
  2094. }
  2095. /*
  2096. * Calculate the group which is almost near its
  2097. * capacity but still has some space to pick up some load
  2098. * from other group and save more power
  2099. */
  2100. if (sum_nr_running <= group_capacity - 1) {
  2101. if (sum_nr_running > leader_nr_running ||
  2102. (sum_nr_running == leader_nr_running &&
  2103. first_cpu(group->cpumask) >
  2104. first_cpu(group_leader->cpumask))) {
  2105. group_leader = group;
  2106. leader_nr_running = sum_nr_running;
  2107. }
  2108. }
  2109. group_next:
  2110. #endif
  2111. group = group->next;
  2112. } while (group != sd->groups);
  2113. if (!busiest || this_load >= max_load || busiest_nr_running == 0)
  2114. goto out_balanced;
  2115. avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
  2116. if (this_load >= avg_load ||
  2117. 100*max_load <= sd->imbalance_pct*this_load)
  2118. goto out_balanced;
  2119. busiest_load_per_task /= busiest_nr_running;
  2120. /*
  2121. * We're trying to get all the cpus to the average_load, so we don't
  2122. * want to push ourselves above the average load, nor do we wish to
  2123. * reduce the max loaded cpu below the average load, as either of these
  2124. * actions would just result in more rebalancing later, and ping-pong
  2125. * tasks around. Thus we look for the minimum possible imbalance.
  2126. * Negative imbalances (*we* are more loaded than anyone else) will
  2127. * be counted as no imbalance for these purposes -- we can't fix that
  2128. * by pulling tasks to us. Be careful of negative numbers as they'll
  2129. * appear as very large values with unsigned longs.
  2130. */
  2131. if (max_load <= busiest_load_per_task)
  2132. goto out_balanced;
  2133. /*
  2134. * In the presence of smp nice balancing, certain scenarios can have
  2135. * max load less than avg load(as we skip the groups at or below
  2136. * its cpu_power, while calculating max_load..)
  2137. */
  2138. if (max_load < avg_load) {
  2139. *imbalance = 0;
  2140. goto small_imbalance;
  2141. }
  2142. /* Don't want to pull so many tasks that a group would go idle */
  2143. max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
  2144. /* How much load to actually move to equalise the imbalance */
  2145. *imbalance = min(max_pull * busiest->__cpu_power,
  2146. (avg_load - this_load) * this->__cpu_power)
  2147. / SCHED_LOAD_SCALE;
  2148. /*
  2149. * if *imbalance is less than the average load per runnable task
  2150. * there is no gaurantee that any tasks will be moved so we'll have
  2151. * a think about bumping its value to force at least one task to be
  2152. * moved
  2153. */
  2154. if (*imbalance < busiest_load_per_task) {
  2155. unsigned long tmp, pwr_now, pwr_move;
  2156. unsigned int imbn;
  2157. small_imbalance:
  2158. pwr_move = pwr_now = 0;
  2159. imbn = 2;
  2160. if (this_nr_running) {
  2161. this_load_per_task /= this_nr_running;
  2162. if (busiest_load_per_task > this_load_per_task)
  2163. imbn = 1;
  2164. } else
  2165. this_load_per_task = SCHED_LOAD_SCALE;
  2166. if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
  2167. busiest_load_per_task * imbn) {
  2168. *imbalance = busiest_load_per_task;
  2169. return busiest;
  2170. }
  2171. /*
  2172. * OK, we don't have enough imbalance to justify moving tasks,
  2173. * however we may be able to increase total CPU power used by
  2174. * moving them.
  2175. */
  2176. pwr_now += busiest->__cpu_power *
  2177. min(busiest_load_per_task, max_load);
  2178. pwr_now += this->__cpu_power *
  2179. min(this_load_per_task, this_load);
  2180. pwr_now /= SCHED_LOAD_SCALE;
  2181. /* Amount of load we'd subtract */
  2182. tmp = sg_div_cpu_power(busiest,
  2183. busiest_load_per_task * SCHED_LOAD_SCALE);
  2184. if (max_load > tmp)
  2185. pwr_move += busiest->__cpu_power *
  2186. min(busiest_load_per_task, max_load - tmp);
  2187. /* Amount of load we'd add */
  2188. if (max_load * busiest->__cpu_power <
  2189. busiest_load_per_task * SCHED_LOAD_SCALE)
  2190. tmp = sg_div_cpu_power(this,
  2191. max_load * busiest->__cpu_power);
  2192. else
  2193. tmp = sg_div_cpu_power(this,
  2194. busiest_load_per_task * SCHED_LOAD_SCALE);
  2195. pwr_move += this->__cpu_power *
  2196. min(this_load_per_task, this_load + tmp);
  2197. pwr_move /= SCHED_LOAD_SCALE;
  2198. /* Move if we gain throughput */
  2199. if (pwr_move > pwr_now)
  2200. *imbalance = busiest_load_per_task;
  2201. }
  2202. return busiest;
  2203. out_balanced:
  2204. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2205. if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
  2206. goto ret;
  2207. if (this == group_leader && group_leader != group_min) {
  2208. *imbalance = min_load_per_task;
  2209. return group_min;
  2210. }
  2211. #endif
  2212. ret:
  2213. *imbalance = 0;
  2214. return NULL;
  2215. }
  2216. /*
  2217. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  2218. */
  2219. static struct rq *
  2220. find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
  2221. unsigned long imbalance, cpumask_t *cpus)
  2222. {
  2223. struct rq *busiest = NULL, *rq;
  2224. unsigned long max_load = 0;
  2225. int i;
  2226. for_each_cpu_mask(i, group->cpumask) {
  2227. unsigned long wl;
  2228. if (!cpu_isset(i, *cpus))
  2229. continue;
  2230. rq = cpu_rq(i);
  2231. wl = weighted_cpuload(i);
  2232. if (rq->nr_running == 1 && wl > imbalance)
  2233. continue;
  2234. if (wl > max_load) {
  2235. max_load = wl;
  2236. busiest = rq;
  2237. }
  2238. }
  2239. return busiest;
  2240. }
  2241. /*
  2242. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  2243. * so long as it is large enough.
  2244. */
  2245. #define MAX_PINNED_INTERVAL 512
  2246. /*
  2247. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  2248. * tasks if there is an imbalance.
  2249. */
  2250. static int load_balance(int this_cpu, struct rq *this_rq,
  2251. struct sched_domain *sd, enum cpu_idle_type idle,
  2252. int *balance)
  2253. {
  2254. int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
  2255. struct sched_group *group;
  2256. unsigned long imbalance;
  2257. struct rq *busiest;
  2258. cpumask_t cpus = CPU_MASK_ALL;
  2259. unsigned long flags;
  2260. /*
  2261. * When power savings policy is enabled for the parent domain, idle
  2262. * sibling can pick up load irrespective of busy siblings. In this case,
  2263. * let the state of idle sibling percolate up as CPU_IDLE, instead of
  2264. * portraying it as CPU_NOT_IDLE.
  2265. */
  2266. if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
  2267. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2268. sd_idle = 1;
  2269. schedstat_inc(sd, lb_cnt[idle]);
  2270. redo:
  2271. group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
  2272. &cpus, balance);
  2273. if (*balance == 0)
  2274. goto out_balanced;
  2275. if (!group) {
  2276. schedstat_inc(sd, lb_nobusyg[idle]);
  2277. goto out_balanced;
  2278. }
  2279. busiest = find_busiest_queue(group, idle, imbalance, &cpus);
  2280. if (!busiest) {
  2281. schedstat_inc(sd, lb_nobusyq[idle]);
  2282. goto out_balanced;
  2283. }
  2284. BUG_ON(busiest == this_rq);
  2285. schedstat_add(sd, lb_imbalance[idle], imbalance);
  2286. ld_moved = 0;
  2287. if (busiest->nr_running > 1) {
  2288. /*
  2289. * Attempt to move tasks. If find_busiest_group has found
  2290. * an imbalance but busiest->nr_running <= 1, the group is
  2291. * still unbalanced. ld_moved simply stays zero, so it is
  2292. * correctly treated as an imbalance.
  2293. */
  2294. local_irq_save(flags);
  2295. double_rq_lock(this_rq, busiest);
  2296. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  2297. imbalance, sd, idle, &all_pinned);
  2298. double_rq_unlock(this_rq, busiest);
  2299. local_irq_restore(flags);
  2300. /*
  2301. * some other cpu did the load balance for us.
  2302. */
  2303. if (ld_moved && this_cpu != smp_processor_id())
  2304. resched_cpu(this_cpu);
  2305. /* All tasks on this runqueue were pinned by CPU affinity */
  2306. if (unlikely(all_pinned)) {
  2307. cpu_clear(cpu_of(busiest), cpus);
  2308. if (!cpus_empty(cpus))
  2309. goto redo;
  2310. goto out_balanced;
  2311. }
  2312. }
  2313. if (!ld_moved) {
  2314. schedstat_inc(sd, lb_failed[idle]);
  2315. sd->nr_balance_failed++;
  2316. if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
  2317. spin_lock_irqsave(&busiest->lock, flags);
  2318. /* don't kick the migration_thread, if the curr
  2319. * task on busiest cpu can't be moved to this_cpu
  2320. */
  2321. if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
  2322. spin_unlock_irqrestore(&busiest->lock, flags);
  2323. all_pinned = 1;
  2324. goto out_one_pinned;
  2325. }
  2326. if (!busiest->active_balance) {
  2327. busiest->active_balance = 1;
  2328. busiest->push_cpu = this_cpu;
  2329. active_balance = 1;
  2330. }
  2331. spin_unlock_irqrestore(&busiest->lock, flags);
  2332. if (active_balance)
  2333. wake_up_process(busiest->migration_thread);
  2334. /*
  2335. * We've kicked active balancing, reset the failure
  2336. * counter.
  2337. */
  2338. sd->nr_balance_failed = sd->cache_nice_tries+1;
  2339. }
  2340. } else
  2341. sd->nr_balance_failed = 0;
  2342. if (likely(!active_balance)) {
  2343. /* We were unbalanced, so reset the balancing interval */
  2344. sd->balance_interval = sd->min_interval;
  2345. } else {
  2346. /*
  2347. * If we've begun active balancing, start to back off. This
  2348. * case may not be covered by the all_pinned logic if there
  2349. * is only 1 task on the busy runqueue (because we don't call
  2350. * move_tasks).
  2351. */
  2352. if (sd->balance_interval < sd->max_interval)
  2353. sd->balance_interval *= 2;
  2354. }
  2355. if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2356. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2357. return -1;
  2358. return ld_moved;
  2359. out_balanced:
  2360. schedstat_inc(sd, lb_balanced[idle]);
  2361. sd->nr_balance_failed = 0;
  2362. out_one_pinned:
  2363. /* tune up the balancing interval */
  2364. if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
  2365. (sd->balance_interval < sd->max_interval))
  2366. sd->balance_interval *= 2;
  2367. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2368. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2369. return -1;
  2370. return 0;
  2371. }
  2372. /*
  2373. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  2374. * tasks if there is an imbalance.
  2375. *
  2376. * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
  2377. * this_rq is locked.
  2378. */
  2379. static int
  2380. load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
  2381. {
  2382. struct sched_group *group;
  2383. struct rq *busiest = NULL;
  2384. unsigned long imbalance;
  2385. int ld_moved = 0;
  2386. int sd_idle = 0;
  2387. int all_pinned = 0;
  2388. cpumask_t cpus = CPU_MASK_ALL;
  2389. /*
  2390. * When power savings policy is enabled for the parent domain, idle
  2391. * sibling can pick up load irrespective of busy siblings. In this case,
  2392. * let the state of idle sibling percolate up as IDLE, instead of
  2393. * portraying it as CPU_NOT_IDLE.
  2394. */
  2395. if (sd->flags & SD_SHARE_CPUPOWER &&
  2396. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2397. sd_idle = 1;
  2398. schedstat_inc(sd, lb_cnt[CPU_NEWLY_IDLE]);
  2399. redo:
  2400. group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
  2401. &sd_idle, &cpus, NULL);
  2402. if (!group) {
  2403. schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
  2404. goto out_balanced;
  2405. }
  2406. busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
  2407. &cpus);
  2408. if (!busiest) {
  2409. schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
  2410. goto out_balanced;
  2411. }
  2412. BUG_ON(busiest == this_rq);
  2413. schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
  2414. ld_moved = 0;
  2415. if (busiest->nr_running > 1) {
  2416. /* Attempt to move tasks */
  2417. double_lock_balance(this_rq, busiest);
  2418. /* this_rq->clock is already updated */
  2419. update_rq_clock(busiest);
  2420. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  2421. imbalance, sd, CPU_NEWLY_IDLE,
  2422. &all_pinned);
  2423. spin_unlock(&busiest->lock);
  2424. if (unlikely(all_pinned)) {
  2425. cpu_clear(cpu_of(busiest), cpus);
  2426. if (!cpus_empty(cpus))
  2427. goto redo;
  2428. }
  2429. }
  2430. if (!ld_moved) {
  2431. schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
  2432. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2433. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2434. return -1;
  2435. } else
  2436. sd->nr_balance_failed = 0;
  2437. return ld_moved;
  2438. out_balanced:
  2439. schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
  2440. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2441. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2442. return -1;
  2443. sd->nr_balance_failed = 0;
  2444. return 0;
  2445. }
  2446. /*
  2447. * idle_balance is called by schedule() if this_cpu is about to become
  2448. * idle. Attempts to pull tasks from other CPUs.
  2449. */
  2450. static void idle_balance(int this_cpu, struct rq *this_rq)
  2451. {
  2452. struct sched_domain *sd;
  2453. int pulled_task = -1;
  2454. unsigned long next_balance = jiffies + HZ;
  2455. for_each_domain(this_cpu, sd) {
  2456. unsigned long interval;
  2457. if (!(sd->flags & SD_LOAD_BALANCE))
  2458. continue;
  2459. if (sd->flags & SD_BALANCE_NEWIDLE)
  2460. /* If we've pulled tasks over stop searching: */
  2461. pulled_task = load_balance_newidle(this_cpu,
  2462. this_rq, sd);
  2463. interval = msecs_to_jiffies(sd->balance_interval);
  2464. if (time_after(next_balance, sd->last_balance + interval))
  2465. next_balance = sd->last_balance + interval;
  2466. if (pulled_task)
  2467. break;
  2468. }
  2469. if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
  2470. /*
  2471. * We are going idle. next_balance may be set based on
  2472. * a busy processor. So reset next_balance.
  2473. */
  2474. this_rq->next_balance = next_balance;
  2475. }
  2476. }
  2477. /*
  2478. * active_load_balance is run by migration threads. It pushes running tasks
  2479. * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
  2480. * running on each physical CPU where possible, and avoids physical /
  2481. * logical imbalances.
  2482. *
  2483. * Called with busiest_rq locked.
  2484. */
  2485. static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
  2486. {
  2487. int target_cpu = busiest_rq->push_cpu;
  2488. struct sched_domain *sd;
  2489. struct rq *target_rq;
  2490. /* Is there any task to move? */
  2491. if (busiest_rq->nr_running <= 1)
  2492. return;
  2493. target_rq = cpu_rq(target_cpu);
  2494. /*
  2495. * This condition is "impossible", if it occurs
  2496. * we need to fix it. Originally reported by
  2497. * Bjorn Helgaas on a 128-cpu setup.
  2498. */
  2499. BUG_ON(busiest_rq == target_rq);
  2500. /* move a task from busiest_rq to target_rq */
  2501. double_lock_balance(busiest_rq, target_rq);
  2502. update_rq_clock(busiest_rq);
  2503. update_rq_clock(target_rq);
  2504. /* Search for an sd spanning us and the target CPU. */
  2505. for_each_domain(target_cpu, sd) {
  2506. if ((sd->flags & SD_LOAD_BALANCE) &&
  2507. cpu_isset(busiest_cpu, sd->span))
  2508. break;
  2509. }
  2510. if (likely(sd)) {
  2511. schedstat_inc(sd, alb_cnt);
  2512. if (move_one_task(target_rq, target_cpu, busiest_rq,
  2513. sd, CPU_IDLE))
  2514. schedstat_inc(sd, alb_pushed);
  2515. else
  2516. schedstat_inc(sd, alb_failed);
  2517. }
  2518. spin_unlock(&target_rq->lock);
  2519. }
  2520. #ifdef CONFIG_NO_HZ
  2521. static struct {
  2522. atomic_t load_balancer;
  2523. cpumask_t cpu_mask;
  2524. } nohz ____cacheline_aligned = {
  2525. .load_balancer = ATOMIC_INIT(-1),
  2526. .cpu_mask = CPU_MASK_NONE,
  2527. };
  2528. /*
  2529. * This routine will try to nominate the ilb (idle load balancing)
  2530. * owner among the cpus whose ticks are stopped. ilb owner will do the idle
  2531. * load balancing on behalf of all those cpus. If all the cpus in the system
  2532. * go into this tickless mode, then there will be no ilb owner (as there is
  2533. * no need for one) and all the cpus will sleep till the next wakeup event
  2534. * arrives...
  2535. *
  2536. * For the ilb owner, tick is not stopped. And this tick will be used
  2537. * for idle load balancing. ilb owner will still be part of
  2538. * nohz.cpu_mask..
  2539. *
  2540. * While stopping the tick, this cpu will become the ilb owner if there
  2541. * is no other owner. And will be the owner till that cpu becomes busy
  2542. * or if all cpus in the system stop their ticks at which point
  2543. * there is no need for ilb owner.
  2544. *
  2545. * When the ilb owner becomes busy, it nominates another owner, during the
  2546. * next busy scheduler_tick()
  2547. */
  2548. int select_nohz_load_balancer(int stop_tick)
  2549. {
  2550. int cpu = smp_processor_id();
  2551. if (stop_tick) {
  2552. cpu_set(cpu, nohz.cpu_mask);
  2553. cpu_rq(cpu)->in_nohz_recently = 1;
  2554. /*
  2555. * If we are going offline and still the leader, give up!
  2556. */
  2557. if (cpu_is_offline(cpu) &&
  2558. atomic_read(&nohz.load_balancer) == cpu) {
  2559. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  2560. BUG();
  2561. return 0;
  2562. }
  2563. /* time for ilb owner also to sleep */
  2564. if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
  2565. if (atomic_read(&nohz.load_balancer) == cpu)
  2566. atomic_set(&nohz.load_balancer, -1);
  2567. return 0;
  2568. }
  2569. if (atomic_read(&nohz.load_balancer) == -1) {
  2570. /* make me the ilb owner */
  2571. if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
  2572. return 1;
  2573. } else if (atomic_read(&nohz.load_balancer) == cpu)
  2574. return 1;
  2575. } else {
  2576. if (!cpu_isset(cpu, nohz.cpu_mask))
  2577. return 0;
  2578. cpu_clear(cpu, nohz.cpu_mask);
  2579. if (atomic_read(&nohz.load_balancer) == cpu)
  2580. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  2581. BUG();
  2582. }
  2583. return 0;
  2584. }
  2585. #endif
  2586. static DEFINE_SPINLOCK(balancing);
  2587. /*
  2588. * It checks each scheduling domain to see if it is due to be balanced,
  2589. * and initiates a balancing operation if so.
  2590. *
  2591. * Balancing parameters are set up in arch_init_sched_domains.
  2592. */
  2593. static inline void rebalance_domains(int cpu, enum cpu_idle_type idle)
  2594. {
  2595. int balance = 1;
  2596. struct rq *rq = cpu_rq(cpu);
  2597. unsigned long interval;
  2598. struct sched_domain *sd;
  2599. /* Earliest time when we have to do rebalance again */
  2600. unsigned long next_balance = jiffies + 60*HZ;
  2601. int update_next_balance = 0;
  2602. for_each_domain(cpu, sd) {
  2603. if (!(sd->flags & SD_LOAD_BALANCE))
  2604. continue;
  2605. interval = sd->balance_interval;
  2606. if (idle != CPU_IDLE)
  2607. interval *= sd->busy_factor;
  2608. /* scale ms to jiffies */
  2609. interval = msecs_to_jiffies(interval);
  2610. if (unlikely(!interval))
  2611. interval = 1;
  2612. if (interval > HZ*NR_CPUS/10)
  2613. interval = HZ*NR_CPUS/10;
  2614. if (sd->flags & SD_SERIALIZE) {
  2615. if (!spin_trylock(&balancing))
  2616. goto out;
  2617. }
  2618. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  2619. if (load_balance(cpu, rq, sd, idle, &balance)) {
  2620. /*
  2621. * We've pulled tasks over so either we're no
  2622. * longer idle, or one of our SMT siblings is
  2623. * not idle.
  2624. */
  2625. idle = CPU_NOT_IDLE;
  2626. }
  2627. sd->last_balance = jiffies;
  2628. }
  2629. if (sd->flags & SD_SERIALIZE)
  2630. spin_unlock(&balancing);
  2631. out:
  2632. if (time_after(next_balance, sd->last_balance + interval)) {
  2633. next_balance = sd->last_balance + interval;
  2634. update_next_balance = 1;
  2635. }
  2636. /*
  2637. * Stop the load balance at this level. There is another
  2638. * CPU in our sched group which is doing load balancing more
  2639. * actively.
  2640. */
  2641. if (!balance)
  2642. break;
  2643. }
  2644. /*
  2645. * next_balance will be updated only when there is a need.
  2646. * When the cpu is attached to null domain for ex, it will not be
  2647. * updated.
  2648. */
  2649. if (likely(update_next_balance))
  2650. rq->next_balance = next_balance;
  2651. }
  2652. /*
  2653. * run_rebalance_domains is triggered when needed from the scheduler tick.
  2654. * In CONFIG_NO_HZ case, the idle load balance owner will do the
  2655. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  2656. */
  2657. static void run_rebalance_domains(struct softirq_action *h)
  2658. {
  2659. int this_cpu = smp_processor_id();
  2660. struct rq *this_rq = cpu_rq(this_cpu);
  2661. enum cpu_idle_type idle = this_rq->idle_at_tick ?
  2662. CPU_IDLE : CPU_NOT_IDLE;
  2663. rebalance_domains(this_cpu, idle);
  2664. #ifdef CONFIG_NO_HZ
  2665. /*
  2666. * If this cpu is the owner for idle load balancing, then do the
  2667. * balancing on behalf of the other idle cpus whose ticks are
  2668. * stopped.
  2669. */
  2670. if (this_rq->idle_at_tick &&
  2671. atomic_read(&nohz.load_balancer) == this_cpu) {
  2672. cpumask_t cpus = nohz.cpu_mask;
  2673. struct rq *rq;
  2674. int balance_cpu;
  2675. cpu_clear(this_cpu, cpus);
  2676. for_each_cpu_mask(balance_cpu, cpus) {
  2677. /*
  2678. * If this cpu gets work to do, stop the load balancing
  2679. * work being done for other cpus. Next load
  2680. * balancing owner will pick it up.
  2681. */
  2682. if (need_resched())
  2683. break;
  2684. rebalance_domains(balance_cpu, CPU_IDLE);
  2685. rq = cpu_rq(balance_cpu);
  2686. if (time_after(this_rq->next_balance, rq->next_balance))
  2687. this_rq->next_balance = rq->next_balance;
  2688. }
  2689. }
  2690. #endif
  2691. }
  2692. /*
  2693. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  2694. *
  2695. * In case of CONFIG_NO_HZ, this is the place where we nominate a new
  2696. * idle load balancing owner or decide to stop the periodic load balancing,
  2697. * if the whole system is idle.
  2698. */
  2699. static inline void trigger_load_balance(struct rq *rq, int cpu)
  2700. {
  2701. #ifdef CONFIG_NO_HZ
  2702. /*
  2703. * If we were in the nohz mode recently and busy at the current
  2704. * scheduler tick, then check if we need to nominate new idle
  2705. * load balancer.
  2706. */
  2707. if (rq->in_nohz_recently && !rq->idle_at_tick) {
  2708. rq->in_nohz_recently = 0;
  2709. if (atomic_read(&nohz.load_balancer) == cpu) {
  2710. cpu_clear(cpu, nohz.cpu_mask);
  2711. atomic_set(&nohz.load_balancer, -1);
  2712. }
  2713. if (atomic_read(&nohz.load_balancer) == -1) {
  2714. /*
  2715. * simple selection for now: Nominate the
  2716. * first cpu in the nohz list to be the next
  2717. * ilb owner.
  2718. *
  2719. * TBD: Traverse the sched domains and nominate
  2720. * the nearest cpu in the nohz.cpu_mask.
  2721. */
  2722. int ilb = first_cpu(nohz.cpu_mask);
  2723. if (ilb != NR_CPUS)
  2724. resched_cpu(ilb);
  2725. }
  2726. }
  2727. /*
  2728. * If this cpu is idle and doing idle load balancing for all the
  2729. * cpus with ticks stopped, is it time for that to stop?
  2730. */
  2731. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
  2732. cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
  2733. resched_cpu(cpu);
  2734. return;
  2735. }
  2736. /*
  2737. * If this cpu is idle and the idle load balancing is done by
  2738. * someone else, then no need raise the SCHED_SOFTIRQ
  2739. */
  2740. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
  2741. cpu_isset(cpu, nohz.cpu_mask))
  2742. return;
  2743. #endif
  2744. if (time_after_eq(jiffies, rq->next_balance))
  2745. raise_softirq(SCHED_SOFTIRQ);
  2746. }
  2747. #else /* CONFIG_SMP */
  2748. /*
  2749. * on UP we do not need to balance between CPUs:
  2750. */
  2751. static inline void idle_balance(int cpu, struct rq *rq)
  2752. {
  2753. }
  2754. /* Avoid "used but not defined" warning on UP */
  2755. static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2756. unsigned long max_nr_move, unsigned long max_load_move,
  2757. struct sched_domain *sd, enum cpu_idle_type idle,
  2758. int *all_pinned, unsigned long *load_moved,
  2759. int *this_best_prio, struct rq_iterator *iterator)
  2760. {
  2761. *load_moved = 0;
  2762. return 0;
  2763. }
  2764. #endif
  2765. DEFINE_PER_CPU(struct kernel_stat, kstat);
  2766. EXPORT_PER_CPU_SYMBOL(kstat);
  2767. /*
  2768. * Return p->sum_exec_runtime plus any more ns on the sched_clock
  2769. * that have not yet been banked in case the task is currently running.
  2770. */
  2771. unsigned long long task_sched_runtime(struct task_struct *p)
  2772. {
  2773. unsigned long flags;
  2774. u64 ns, delta_exec;
  2775. struct rq *rq;
  2776. rq = task_rq_lock(p, &flags);
  2777. ns = p->se.sum_exec_runtime;
  2778. if (rq->curr == p) {
  2779. update_rq_clock(rq);
  2780. delta_exec = rq->clock - p->se.exec_start;
  2781. if ((s64)delta_exec > 0)
  2782. ns += delta_exec;
  2783. }
  2784. task_rq_unlock(rq, &flags);
  2785. return ns;
  2786. }
  2787. /*
  2788. * Account user cpu time to a process.
  2789. * @p: the process that the cpu time gets accounted to
  2790. * @hardirq_offset: the offset to subtract from hardirq_count()
  2791. * @cputime: the cpu time spent in user space since the last update
  2792. */
  2793. void account_user_time(struct task_struct *p, cputime_t cputime)
  2794. {
  2795. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2796. cputime64_t tmp;
  2797. p->utime = cputime_add(p->utime, cputime);
  2798. /* Add user time to cpustat. */
  2799. tmp = cputime_to_cputime64(cputime);
  2800. if (TASK_NICE(p) > 0)
  2801. cpustat->nice = cputime64_add(cpustat->nice, tmp);
  2802. else
  2803. cpustat->user = cputime64_add(cpustat->user, tmp);
  2804. }
  2805. /*
  2806. * Account system cpu time to a process.
  2807. * @p: the process that the cpu time gets accounted to
  2808. * @hardirq_offset: the offset to subtract from hardirq_count()
  2809. * @cputime: the cpu time spent in kernel space since the last update
  2810. */
  2811. void account_system_time(struct task_struct *p, int hardirq_offset,
  2812. cputime_t cputime)
  2813. {
  2814. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2815. struct rq *rq = this_rq();
  2816. cputime64_t tmp;
  2817. p->stime = cputime_add(p->stime, cputime);
  2818. /* Add system time to cpustat. */
  2819. tmp = cputime_to_cputime64(cputime);
  2820. if (hardirq_count() - hardirq_offset)
  2821. cpustat->irq = cputime64_add(cpustat->irq, tmp);
  2822. else if (softirq_count())
  2823. cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
  2824. else if (p != rq->idle)
  2825. cpustat->system = cputime64_add(cpustat->system, tmp);
  2826. else if (atomic_read(&rq->nr_iowait) > 0)
  2827. cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
  2828. else
  2829. cpustat->idle = cputime64_add(cpustat->idle, tmp);
  2830. /* Account for system time used */
  2831. acct_update_integrals(p);
  2832. }
  2833. /*
  2834. * Account for involuntary wait time.
  2835. * @p: the process from which the cpu time has been stolen
  2836. * @steal: the cpu time spent in involuntary wait
  2837. */
  2838. void account_steal_time(struct task_struct *p, cputime_t steal)
  2839. {
  2840. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2841. cputime64_t tmp = cputime_to_cputime64(steal);
  2842. struct rq *rq = this_rq();
  2843. if (p == rq->idle) {
  2844. p->stime = cputime_add(p->stime, steal);
  2845. if (atomic_read(&rq->nr_iowait) > 0)
  2846. cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
  2847. else
  2848. cpustat->idle = cputime64_add(cpustat->idle, tmp);
  2849. } else
  2850. cpustat->steal = cputime64_add(cpustat->steal, tmp);
  2851. }
  2852. /*
  2853. * This function gets called by the timer code, with HZ frequency.
  2854. * We call it with interrupts disabled.
  2855. *
  2856. * It also gets called by the fork code, when changing the parent's
  2857. * timeslices.
  2858. */
  2859. void scheduler_tick(void)
  2860. {
  2861. int cpu = smp_processor_id();
  2862. struct rq *rq = cpu_rq(cpu);
  2863. struct task_struct *curr = rq->curr;
  2864. u64 next_tick = rq->tick_timestamp + TICK_NSEC;
  2865. spin_lock(&rq->lock);
  2866. __update_rq_clock(rq);
  2867. /*
  2868. * Let rq->clock advance by at least TICK_NSEC:
  2869. */
  2870. if (unlikely(rq->clock < next_tick))
  2871. rq->clock = next_tick;
  2872. rq->tick_timestamp = rq->clock;
  2873. update_cpu_load(rq);
  2874. if (curr != rq->idle) /* FIXME: needed? */
  2875. curr->sched_class->task_tick(rq, curr);
  2876. spin_unlock(&rq->lock);
  2877. #ifdef CONFIG_SMP
  2878. rq->idle_at_tick = idle_cpu(cpu);
  2879. trigger_load_balance(rq, cpu);
  2880. #endif
  2881. }
  2882. #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
  2883. void fastcall add_preempt_count(int val)
  2884. {
  2885. /*
  2886. * Underflow?
  2887. */
  2888. if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
  2889. return;
  2890. preempt_count() += val;
  2891. /*
  2892. * Spinlock count overflowing soon?
  2893. */
  2894. DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
  2895. PREEMPT_MASK - 10);
  2896. }
  2897. EXPORT_SYMBOL(add_preempt_count);
  2898. void fastcall sub_preempt_count(int val)
  2899. {
  2900. /*
  2901. * Underflow?
  2902. */
  2903. if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
  2904. return;
  2905. /*
  2906. * Is the spinlock portion underflowing?
  2907. */
  2908. if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
  2909. !(preempt_count() & PREEMPT_MASK)))
  2910. return;
  2911. preempt_count() -= val;
  2912. }
  2913. EXPORT_SYMBOL(sub_preempt_count);
  2914. #endif
  2915. /*
  2916. * Print scheduling while atomic bug:
  2917. */
  2918. static noinline void __schedule_bug(struct task_struct *prev)
  2919. {
  2920. printk(KERN_ERR "BUG: scheduling while atomic: %s/0x%08x/%d\n",
  2921. prev->comm, preempt_count(), prev->pid);
  2922. debug_show_held_locks(prev);
  2923. if (irqs_disabled())
  2924. print_irqtrace_events(prev);
  2925. dump_stack();
  2926. }
  2927. /*
  2928. * Various schedule()-time debugging checks and statistics:
  2929. */
  2930. static inline void schedule_debug(struct task_struct *prev)
  2931. {
  2932. /*
  2933. * Test if we are atomic. Since do_exit() needs to call into
  2934. * schedule() atomically, we ignore that path for now.
  2935. * Otherwise, whine if we are scheduling when we should not be.
  2936. */
  2937. if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
  2938. __schedule_bug(prev);
  2939. profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  2940. schedstat_inc(this_rq(), sched_cnt);
  2941. }
  2942. /*
  2943. * Pick up the highest-prio task:
  2944. */
  2945. static inline struct task_struct *
  2946. pick_next_task(struct rq *rq, struct task_struct *prev)
  2947. {
  2948. struct sched_class *class;
  2949. struct task_struct *p;
  2950. /*
  2951. * Optimization: we know that if all tasks are in
  2952. * the fair class we can call that function directly:
  2953. */
  2954. if (likely(rq->nr_running == rq->cfs.nr_running)) {
  2955. p = fair_sched_class.pick_next_task(rq);
  2956. if (likely(p))
  2957. return p;
  2958. }
  2959. class = sched_class_highest;
  2960. for ( ; ; ) {
  2961. p = class->pick_next_task(rq);
  2962. if (p)
  2963. return p;
  2964. /*
  2965. * Will never be NULL as the idle class always
  2966. * returns a non-NULL p:
  2967. */
  2968. class = class->next;
  2969. }
  2970. }
  2971. /*
  2972. * schedule() is the main scheduler function.
  2973. */
  2974. asmlinkage void __sched schedule(void)
  2975. {
  2976. struct task_struct *prev, *next;
  2977. long *switch_count;
  2978. struct rq *rq;
  2979. int cpu;
  2980. need_resched:
  2981. preempt_disable();
  2982. cpu = smp_processor_id();
  2983. rq = cpu_rq(cpu);
  2984. rcu_qsctr_inc(cpu);
  2985. prev = rq->curr;
  2986. switch_count = &prev->nivcsw;
  2987. release_kernel_lock(prev);
  2988. need_resched_nonpreemptible:
  2989. schedule_debug(prev);
  2990. spin_lock_irq(&rq->lock);
  2991. clear_tsk_need_resched(prev);
  2992. __update_rq_clock(rq);
  2993. if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  2994. if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
  2995. unlikely(signal_pending(prev)))) {
  2996. prev->state = TASK_RUNNING;
  2997. } else {
  2998. deactivate_task(rq, prev, 1);
  2999. }
  3000. switch_count = &prev->nvcsw;
  3001. }
  3002. if (unlikely(!rq->nr_running))
  3003. idle_balance(cpu, rq);
  3004. prev->sched_class->put_prev_task(rq, prev);
  3005. next = pick_next_task(rq, prev);
  3006. sched_info_switch(prev, next);
  3007. if (likely(prev != next)) {
  3008. rq->nr_switches++;
  3009. rq->curr = next;
  3010. ++*switch_count;
  3011. context_switch(rq, prev, next); /* unlocks the rq */
  3012. } else
  3013. spin_unlock_irq(&rq->lock);
  3014. if (unlikely(reacquire_kernel_lock(current) < 0)) {
  3015. cpu = smp_processor_id();
  3016. rq = cpu_rq(cpu);
  3017. goto need_resched_nonpreemptible;
  3018. }
  3019. preempt_enable_no_resched();
  3020. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  3021. goto need_resched;
  3022. }
  3023. EXPORT_SYMBOL(schedule);
  3024. #ifdef CONFIG_PREEMPT
  3025. /*
  3026. * this is the entry point to schedule() from in-kernel preemption
  3027. * off of preempt_enable. Kernel preemptions off return from interrupt
  3028. * occur there and call schedule directly.
  3029. */
  3030. asmlinkage void __sched preempt_schedule(void)
  3031. {
  3032. struct thread_info *ti = current_thread_info();
  3033. #ifdef CONFIG_PREEMPT_BKL
  3034. struct task_struct *task = current;
  3035. int saved_lock_depth;
  3036. #endif
  3037. /*
  3038. * If there is a non-zero preempt_count or interrupts are disabled,
  3039. * we do not want to preempt the current task. Just return..
  3040. */
  3041. if (likely(ti->preempt_count || irqs_disabled()))
  3042. return;
  3043. need_resched:
  3044. add_preempt_count(PREEMPT_ACTIVE);
  3045. /*
  3046. * We keep the big kernel semaphore locked, but we
  3047. * clear ->lock_depth so that schedule() doesnt
  3048. * auto-release the semaphore:
  3049. */
  3050. #ifdef CONFIG_PREEMPT_BKL
  3051. saved_lock_depth = task->lock_depth;
  3052. task->lock_depth = -1;
  3053. #endif
  3054. schedule();
  3055. #ifdef CONFIG_PREEMPT_BKL
  3056. task->lock_depth = saved_lock_depth;
  3057. #endif
  3058. sub_preempt_count(PREEMPT_ACTIVE);
  3059. /* we could miss a preemption opportunity between schedule and now */
  3060. barrier();
  3061. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  3062. goto need_resched;
  3063. }
  3064. EXPORT_SYMBOL(preempt_schedule);
  3065. /*
  3066. * this is the entry point to schedule() from kernel preemption
  3067. * off of irq context.
  3068. * Note, that this is called and return with irqs disabled. This will
  3069. * protect us against recursive calling from irq.
  3070. */
  3071. asmlinkage void __sched preempt_schedule_irq(void)
  3072. {
  3073. struct thread_info *ti = current_thread_info();
  3074. #ifdef CONFIG_PREEMPT_BKL
  3075. struct task_struct *task = current;
  3076. int saved_lock_depth;
  3077. #endif
  3078. /* Catch callers which need to be fixed */
  3079. BUG_ON(ti->preempt_count || !irqs_disabled());
  3080. need_resched:
  3081. add_preempt_count(PREEMPT_ACTIVE);
  3082. /*
  3083. * We keep the big kernel semaphore locked, but we
  3084. * clear ->lock_depth so that schedule() doesnt
  3085. * auto-release the semaphore:
  3086. */
  3087. #ifdef CONFIG_PREEMPT_BKL
  3088. saved_lock_depth = task->lock_depth;
  3089. task->lock_depth = -1;
  3090. #endif
  3091. local_irq_enable();
  3092. schedule();
  3093. local_irq_disable();
  3094. #ifdef CONFIG_PREEMPT_BKL
  3095. task->lock_depth = saved_lock_depth;
  3096. #endif
  3097. sub_preempt_count(PREEMPT_ACTIVE);
  3098. /* we could miss a preemption opportunity between schedule and now */
  3099. barrier();
  3100. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  3101. goto need_resched;
  3102. }
  3103. #endif /* CONFIG_PREEMPT */
  3104. int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
  3105. void *key)
  3106. {
  3107. return try_to_wake_up(curr->private, mode, sync);
  3108. }
  3109. EXPORT_SYMBOL(default_wake_function);
  3110. /*
  3111. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  3112. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  3113. * number) then we wake all the non-exclusive tasks and one exclusive task.
  3114. *
  3115. * There are circumstances in which we can try to wake a task which has already
  3116. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  3117. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  3118. */
  3119. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  3120. int nr_exclusive, int sync, void *key)
  3121. {
  3122. wait_queue_t *curr, *next;
  3123. list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
  3124. unsigned flags = curr->flags;
  3125. if (curr->func(curr, mode, sync, key) &&
  3126. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  3127. break;
  3128. }
  3129. }
  3130. /**
  3131. * __wake_up - wake up threads blocked on a waitqueue.
  3132. * @q: the waitqueue
  3133. * @mode: which threads
  3134. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  3135. * @key: is directly passed to the wakeup function
  3136. */
  3137. void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
  3138. int nr_exclusive, void *key)
  3139. {
  3140. unsigned long flags;
  3141. spin_lock_irqsave(&q->lock, flags);
  3142. __wake_up_common(q, mode, nr_exclusive, 0, key);
  3143. spin_unlock_irqrestore(&q->lock, flags);
  3144. }
  3145. EXPORT_SYMBOL(__wake_up);
  3146. /*
  3147. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  3148. */
  3149. void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
  3150. {
  3151. __wake_up_common(q, mode, 1, 0, NULL);
  3152. }
  3153. /**
  3154. * __wake_up_sync - wake up threads blocked on a waitqueue.
  3155. * @q: the waitqueue
  3156. * @mode: which threads
  3157. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  3158. *
  3159. * The sync wakeup differs that the waker knows that it will schedule
  3160. * away soon, so while the target thread will be woken up, it will not
  3161. * be migrated to another CPU - ie. the two threads are 'synchronized'
  3162. * with each other. This can prevent needless bouncing between CPUs.
  3163. *
  3164. * On UP it can prevent extra preemption.
  3165. */
  3166. void fastcall
  3167. __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  3168. {
  3169. unsigned long flags;
  3170. int sync = 1;
  3171. if (unlikely(!q))
  3172. return;
  3173. if (unlikely(!nr_exclusive))
  3174. sync = 0;
  3175. spin_lock_irqsave(&q->lock, flags);
  3176. __wake_up_common(q, mode, nr_exclusive, sync, NULL);
  3177. spin_unlock_irqrestore(&q->lock, flags);
  3178. }
  3179. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  3180. void fastcall complete(struct completion *x)
  3181. {
  3182. unsigned long flags;
  3183. spin_lock_irqsave(&x->wait.lock, flags);
  3184. x->done++;
  3185. __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
  3186. 1, 0, NULL);
  3187. spin_unlock_irqrestore(&x->wait.lock, flags);
  3188. }
  3189. EXPORT_SYMBOL(complete);
  3190. void fastcall complete_all(struct completion *x)
  3191. {
  3192. unsigned long flags;
  3193. spin_lock_irqsave(&x->wait.lock, flags);
  3194. x->done += UINT_MAX/2;
  3195. __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
  3196. 0, 0, NULL);
  3197. spin_unlock_irqrestore(&x->wait.lock, flags);
  3198. }
  3199. EXPORT_SYMBOL(complete_all);
  3200. void fastcall __sched wait_for_completion(struct completion *x)
  3201. {
  3202. might_sleep();
  3203. spin_lock_irq(&x->wait.lock);
  3204. if (!x->done) {
  3205. DECLARE_WAITQUEUE(wait, current);
  3206. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3207. __add_wait_queue_tail(&x->wait, &wait);
  3208. do {
  3209. __set_current_state(TASK_UNINTERRUPTIBLE);
  3210. spin_unlock_irq(&x->wait.lock);
  3211. schedule();
  3212. spin_lock_irq(&x->wait.lock);
  3213. } while (!x->done);
  3214. __remove_wait_queue(&x->wait, &wait);
  3215. }
  3216. x->done--;
  3217. spin_unlock_irq(&x->wait.lock);
  3218. }
  3219. EXPORT_SYMBOL(wait_for_completion);
  3220. unsigned long fastcall __sched
  3221. wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  3222. {
  3223. might_sleep();
  3224. spin_lock_irq(&x->wait.lock);
  3225. if (!x->done) {
  3226. DECLARE_WAITQUEUE(wait, current);
  3227. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3228. __add_wait_queue_tail(&x->wait, &wait);
  3229. do {
  3230. __set_current_state(TASK_UNINTERRUPTIBLE);
  3231. spin_unlock_irq(&x->wait.lock);
  3232. timeout = schedule_timeout(timeout);
  3233. spin_lock_irq(&x->wait.lock);
  3234. if (!timeout) {
  3235. __remove_wait_queue(&x->wait, &wait);
  3236. goto out;
  3237. }
  3238. } while (!x->done);
  3239. __remove_wait_queue(&x->wait, &wait);
  3240. }
  3241. x->done--;
  3242. out:
  3243. spin_unlock_irq(&x->wait.lock);
  3244. return timeout;
  3245. }
  3246. EXPORT_SYMBOL(wait_for_completion_timeout);
  3247. int fastcall __sched wait_for_completion_interruptible(struct completion *x)
  3248. {
  3249. int ret = 0;
  3250. might_sleep();
  3251. spin_lock_irq(&x->wait.lock);
  3252. if (!x->done) {
  3253. DECLARE_WAITQUEUE(wait, current);
  3254. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3255. __add_wait_queue_tail(&x->wait, &wait);
  3256. do {
  3257. if (signal_pending(current)) {
  3258. ret = -ERESTARTSYS;
  3259. __remove_wait_queue(&x->wait, &wait);
  3260. goto out;
  3261. }
  3262. __set_current_state(TASK_INTERRUPTIBLE);
  3263. spin_unlock_irq(&x->wait.lock);
  3264. schedule();
  3265. spin_lock_irq(&x->wait.lock);
  3266. } while (!x->done);
  3267. __remove_wait_queue(&x->wait, &wait);
  3268. }
  3269. x->done--;
  3270. out:
  3271. spin_unlock_irq(&x->wait.lock);
  3272. return ret;
  3273. }
  3274. EXPORT_SYMBOL(wait_for_completion_interruptible);
  3275. unsigned long fastcall __sched
  3276. wait_for_completion_interruptible_timeout(struct completion *x,
  3277. unsigned long timeout)
  3278. {
  3279. might_sleep();
  3280. spin_lock_irq(&x->wait.lock);
  3281. if (!x->done) {
  3282. DECLARE_WAITQUEUE(wait, current);
  3283. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3284. __add_wait_queue_tail(&x->wait, &wait);
  3285. do {
  3286. if (signal_pending(current)) {
  3287. timeout = -ERESTARTSYS;
  3288. __remove_wait_queue(&x->wait, &wait);
  3289. goto out;
  3290. }
  3291. __set_current_state(TASK_INTERRUPTIBLE);
  3292. spin_unlock_irq(&x->wait.lock);
  3293. timeout = schedule_timeout(timeout);
  3294. spin_lock_irq(&x->wait.lock);
  3295. if (!timeout) {
  3296. __remove_wait_queue(&x->wait, &wait);
  3297. goto out;
  3298. }
  3299. } while (!x->done);
  3300. __remove_wait_queue(&x->wait, &wait);
  3301. }
  3302. x->done--;
  3303. out:
  3304. spin_unlock_irq(&x->wait.lock);
  3305. return timeout;
  3306. }
  3307. EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  3308. static inline void
  3309. sleep_on_head(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
  3310. {
  3311. spin_lock_irqsave(&q->lock, *flags);
  3312. __add_wait_queue(q, wait);
  3313. spin_unlock(&q->lock);
  3314. }
  3315. static inline void
  3316. sleep_on_tail(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
  3317. {
  3318. spin_lock_irq(&q->lock);
  3319. __remove_wait_queue(q, wait);
  3320. spin_unlock_irqrestore(&q->lock, *flags);
  3321. }
  3322. void __sched interruptible_sleep_on(wait_queue_head_t *q)
  3323. {
  3324. unsigned long flags;
  3325. wait_queue_t wait;
  3326. init_waitqueue_entry(&wait, current);
  3327. current->state = TASK_INTERRUPTIBLE;
  3328. sleep_on_head(q, &wait, &flags);
  3329. schedule();
  3330. sleep_on_tail(q, &wait, &flags);
  3331. }
  3332. EXPORT_SYMBOL(interruptible_sleep_on);
  3333. long __sched
  3334. interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
  3335. {
  3336. unsigned long flags;
  3337. wait_queue_t wait;
  3338. init_waitqueue_entry(&wait, current);
  3339. current->state = TASK_INTERRUPTIBLE;
  3340. sleep_on_head(q, &wait, &flags);
  3341. timeout = schedule_timeout(timeout);
  3342. sleep_on_tail(q, &wait, &flags);
  3343. return timeout;
  3344. }
  3345. EXPORT_SYMBOL(interruptible_sleep_on_timeout);
  3346. void __sched sleep_on(wait_queue_head_t *q)
  3347. {
  3348. unsigned long flags;
  3349. wait_queue_t wait;
  3350. init_waitqueue_entry(&wait, current);
  3351. current->state = TASK_UNINTERRUPTIBLE;
  3352. sleep_on_head(q, &wait, &flags);
  3353. schedule();
  3354. sleep_on_tail(q, &wait, &flags);
  3355. }
  3356. EXPORT_SYMBOL(sleep_on);
  3357. long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
  3358. {
  3359. unsigned long flags;
  3360. wait_queue_t wait;
  3361. init_waitqueue_entry(&wait, current);
  3362. current->state = TASK_UNINTERRUPTIBLE;
  3363. sleep_on_head(q, &wait, &flags);
  3364. timeout = schedule_timeout(timeout);
  3365. sleep_on_tail(q, &wait, &flags);
  3366. return timeout;
  3367. }
  3368. EXPORT_SYMBOL(sleep_on_timeout);
  3369. #ifdef CONFIG_RT_MUTEXES
  3370. /*
  3371. * rt_mutex_setprio - set the current priority of a task
  3372. * @p: task
  3373. * @prio: prio value (kernel-internal form)
  3374. *
  3375. * This function changes the 'effective' priority of a task. It does
  3376. * not touch ->normal_prio like __setscheduler().
  3377. *
  3378. * Used by the rt_mutex code to implement priority inheritance logic.
  3379. */
  3380. void rt_mutex_setprio(struct task_struct *p, int prio)
  3381. {
  3382. int oldprio, on_rq, running;
  3383. unsigned long flags;
  3384. struct rq *rq;
  3385. BUG_ON(prio < 0 || prio > MAX_PRIO);
  3386. rq = task_rq_lock(p, &flags);
  3387. update_rq_clock(rq);
  3388. oldprio = p->prio;
  3389. on_rq = p->se.on_rq;
  3390. running = task_running(rq, p);
  3391. if (on_rq) {
  3392. dequeue_task(rq, p, 0);
  3393. if (running)
  3394. p->sched_class->put_prev_task(rq, p);
  3395. }
  3396. if (rt_prio(prio))
  3397. p->sched_class = &rt_sched_class;
  3398. else
  3399. p->sched_class = &fair_sched_class;
  3400. p->prio = prio;
  3401. if (on_rq) {
  3402. if (running)
  3403. p->sched_class->set_curr_task(rq);
  3404. enqueue_task(rq, p, 0);
  3405. /*
  3406. * Reschedule if we are currently running on this runqueue and
  3407. * our priority decreased, or if we are not currently running on
  3408. * this runqueue and our priority is higher than the current's
  3409. */
  3410. if (running) {
  3411. if (p->prio > oldprio)
  3412. resched_task(rq->curr);
  3413. } else {
  3414. check_preempt_curr(rq, p);
  3415. }
  3416. }
  3417. task_rq_unlock(rq, &flags);
  3418. }
  3419. #endif
  3420. void set_user_nice(struct task_struct *p, long nice)
  3421. {
  3422. int old_prio, delta, on_rq;
  3423. unsigned long flags;
  3424. struct rq *rq;
  3425. if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
  3426. return;
  3427. /*
  3428. * We have to be careful, if called from sys_setpriority(),
  3429. * the task might be in the middle of scheduling on another CPU.
  3430. */
  3431. rq = task_rq_lock(p, &flags);
  3432. update_rq_clock(rq);
  3433. /*
  3434. * The RT priorities are set via sched_setscheduler(), but we still
  3435. * allow the 'normal' nice value to be set - but as expected
  3436. * it wont have any effect on scheduling until the task is
  3437. * SCHED_FIFO/SCHED_RR:
  3438. */
  3439. if (task_has_rt_policy(p)) {
  3440. p->static_prio = NICE_TO_PRIO(nice);
  3441. goto out_unlock;
  3442. }
  3443. on_rq = p->se.on_rq;
  3444. if (on_rq) {
  3445. dequeue_task(rq, p, 0);
  3446. dec_load(rq, p);
  3447. }
  3448. p->static_prio = NICE_TO_PRIO(nice);
  3449. set_load_weight(p);
  3450. old_prio = p->prio;
  3451. p->prio = effective_prio(p);
  3452. delta = p->prio - old_prio;
  3453. if (on_rq) {
  3454. enqueue_task(rq, p, 0);
  3455. inc_load(rq, p);
  3456. /*
  3457. * If the task increased its priority or is running and
  3458. * lowered its priority, then reschedule its CPU:
  3459. */
  3460. if (delta < 0 || (delta > 0 && task_running(rq, p)))
  3461. resched_task(rq->curr);
  3462. }
  3463. out_unlock:
  3464. task_rq_unlock(rq, &flags);
  3465. }
  3466. EXPORT_SYMBOL(set_user_nice);
  3467. /*
  3468. * can_nice - check if a task can reduce its nice value
  3469. * @p: task
  3470. * @nice: nice value
  3471. */
  3472. int can_nice(const struct task_struct *p, const int nice)
  3473. {
  3474. /* convert nice value [19,-20] to rlimit style value [1,40] */
  3475. int nice_rlim = 20 - nice;
  3476. return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
  3477. capable(CAP_SYS_NICE));
  3478. }
  3479. #ifdef __ARCH_WANT_SYS_NICE
  3480. /*
  3481. * sys_nice - change the priority of the current process.
  3482. * @increment: priority increment
  3483. *
  3484. * sys_setpriority is a more generic, but much slower function that
  3485. * does similar things.
  3486. */
  3487. asmlinkage long sys_nice(int increment)
  3488. {
  3489. long nice, retval;
  3490. /*
  3491. * Setpriority might change our priority at the same moment.
  3492. * We don't have to worry. Conceptually one call occurs first
  3493. * and we have a single winner.
  3494. */
  3495. if (increment < -40)
  3496. increment = -40;
  3497. if (increment > 40)
  3498. increment = 40;
  3499. nice = PRIO_TO_NICE(current->static_prio) + increment;
  3500. if (nice < -20)
  3501. nice = -20;
  3502. if (nice > 19)
  3503. nice = 19;
  3504. if (increment < 0 && !can_nice(current, nice))
  3505. return -EPERM;
  3506. retval = security_task_setnice(current, nice);
  3507. if (retval)
  3508. return retval;
  3509. set_user_nice(current, nice);
  3510. return 0;
  3511. }
  3512. #endif
  3513. /**
  3514. * task_prio - return the priority value of a given task.
  3515. * @p: the task in question.
  3516. *
  3517. * This is the priority value as seen by users in /proc.
  3518. * RT tasks are offset by -200. Normal tasks are centered
  3519. * around 0, value goes from -16 to +15.
  3520. */
  3521. int task_prio(const struct task_struct *p)
  3522. {
  3523. return p->prio - MAX_RT_PRIO;
  3524. }
  3525. /**
  3526. * task_nice - return the nice value of a given task.
  3527. * @p: the task in question.
  3528. */
  3529. int task_nice(const struct task_struct *p)
  3530. {
  3531. return TASK_NICE(p);
  3532. }
  3533. EXPORT_SYMBOL_GPL(task_nice);
  3534. /**
  3535. * idle_cpu - is a given cpu idle currently?
  3536. * @cpu: the processor in question.
  3537. */
  3538. int idle_cpu(int cpu)
  3539. {
  3540. return cpu_curr(cpu) == cpu_rq(cpu)->idle;
  3541. }
  3542. /**
  3543. * idle_task - return the idle task for a given cpu.
  3544. * @cpu: the processor in question.
  3545. */
  3546. struct task_struct *idle_task(int cpu)
  3547. {
  3548. return cpu_rq(cpu)->idle;
  3549. }
  3550. /**
  3551. * find_process_by_pid - find a process with a matching PID value.
  3552. * @pid: the pid in question.
  3553. */
  3554. static inline struct task_struct *find_process_by_pid(pid_t pid)
  3555. {
  3556. return pid ? find_task_by_pid(pid) : current;
  3557. }
  3558. /* Actually do priority change: must hold rq lock. */
  3559. static void
  3560. __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
  3561. {
  3562. BUG_ON(p->se.on_rq);
  3563. p->policy = policy;
  3564. switch (p->policy) {
  3565. case SCHED_NORMAL:
  3566. case SCHED_BATCH:
  3567. case SCHED_IDLE:
  3568. p->sched_class = &fair_sched_class;
  3569. break;
  3570. case SCHED_FIFO:
  3571. case SCHED_RR:
  3572. p->sched_class = &rt_sched_class;
  3573. break;
  3574. }
  3575. p->rt_priority = prio;
  3576. p->normal_prio = normal_prio(p);
  3577. /* we are holding p->pi_lock already */
  3578. p->prio = rt_mutex_getprio(p);
  3579. set_load_weight(p);
  3580. }
  3581. /**
  3582. * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
  3583. * @p: the task in question.
  3584. * @policy: new policy.
  3585. * @param: structure containing the new RT priority.
  3586. *
  3587. * NOTE that the task may be already dead.
  3588. */
  3589. int sched_setscheduler(struct task_struct *p, int policy,
  3590. struct sched_param *param)
  3591. {
  3592. int retval, oldprio, oldpolicy = -1, on_rq, running;
  3593. unsigned long flags;
  3594. struct rq *rq;
  3595. /* may grab non-irq protected spin_locks */
  3596. BUG_ON(in_interrupt());
  3597. recheck:
  3598. /* double check policy once rq lock held */
  3599. if (policy < 0)
  3600. policy = oldpolicy = p->policy;
  3601. else if (policy != SCHED_FIFO && policy != SCHED_RR &&
  3602. policy != SCHED_NORMAL && policy != SCHED_BATCH &&
  3603. policy != SCHED_IDLE)
  3604. return -EINVAL;
  3605. /*
  3606. * Valid priorities for SCHED_FIFO and SCHED_RR are
  3607. * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
  3608. * SCHED_BATCH and SCHED_IDLE is 0.
  3609. */
  3610. if (param->sched_priority < 0 ||
  3611. (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
  3612. (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
  3613. return -EINVAL;
  3614. if (rt_policy(policy) != (param->sched_priority != 0))
  3615. return -EINVAL;
  3616. /*
  3617. * Allow unprivileged RT tasks to decrease priority:
  3618. */
  3619. if (!capable(CAP_SYS_NICE)) {
  3620. if (rt_policy(policy)) {
  3621. unsigned long rlim_rtprio;
  3622. if (!lock_task_sighand(p, &flags))
  3623. return -ESRCH;
  3624. rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
  3625. unlock_task_sighand(p, &flags);
  3626. /* can't set/change the rt policy */
  3627. if (policy != p->policy && !rlim_rtprio)
  3628. return -EPERM;
  3629. /* can't increase priority */
  3630. if (param->sched_priority > p->rt_priority &&
  3631. param->sched_priority > rlim_rtprio)
  3632. return -EPERM;
  3633. }
  3634. /*
  3635. * Like positive nice levels, dont allow tasks to
  3636. * move out of SCHED_IDLE either:
  3637. */
  3638. if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
  3639. return -EPERM;
  3640. /* can't change other user's priorities */
  3641. if ((current->euid != p->euid) &&
  3642. (current->euid != p->uid))
  3643. return -EPERM;
  3644. }
  3645. retval = security_task_setscheduler(p, policy, param);
  3646. if (retval)
  3647. return retval;
  3648. /*
  3649. * make sure no PI-waiters arrive (or leave) while we are
  3650. * changing the priority of the task:
  3651. */
  3652. spin_lock_irqsave(&p->pi_lock, flags);
  3653. /*
  3654. * To be able to change p->policy safely, the apropriate
  3655. * runqueue lock must be held.
  3656. */
  3657. rq = __task_rq_lock(p);
  3658. /* recheck policy now with rq lock held */
  3659. if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  3660. policy = oldpolicy = -1;
  3661. __task_rq_unlock(rq);
  3662. spin_unlock_irqrestore(&p->pi_lock, flags);
  3663. goto recheck;
  3664. }
  3665. update_rq_clock(rq);
  3666. on_rq = p->se.on_rq;
  3667. running = task_running(rq, p);
  3668. if (on_rq) {
  3669. deactivate_task(rq, p, 0);
  3670. if (running)
  3671. p->sched_class->put_prev_task(rq, p);
  3672. }
  3673. oldprio = p->prio;
  3674. __setscheduler(rq, p, policy, param->sched_priority);
  3675. if (on_rq) {
  3676. if (running)
  3677. p->sched_class->set_curr_task(rq);
  3678. activate_task(rq, p, 0);
  3679. /*
  3680. * Reschedule if we are currently running on this runqueue and
  3681. * our priority decreased, or if we are not currently running on
  3682. * this runqueue and our priority is higher than the current's
  3683. */
  3684. if (running) {
  3685. if (p->prio > oldprio)
  3686. resched_task(rq->curr);
  3687. } else {
  3688. check_preempt_curr(rq, p);
  3689. }
  3690. }
  3691. __task_rq_unlock(rq);
  3692. spin_unlock_irqrestore(&p->pi_lock, flags);
  3693. rt_mutex_adjust_pi(p);
  3694. return 0;
  3695. }
  3696. EXPORT_SYMBOL_GPL(sched_setscheduler);
  3697. static int
  3698. do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  3699. {
  3700. struct sched_param lparam;
  3701. struct task_struct *p;
  3702. int retval;
  3703. if (!param || pid < 0)
  3704. return -EINVAL;
  3705. if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  3706. return -EFAULT;
  3707. rcu_read_lock();
  3708. retval = -ESRCH;
  3709. p = find_process_by_pid(pid);
  3710. if (p != NULL)
  3711. retval = sched_setscheduler(p, policy, &lparam);
  3712. rcu_read_unlock();
  3713. return retval;
  3714. }
  3715. /**
  3716. * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  3717. * @pid: the pid in question.
  3718. * @policy: new policy.
  3719. * @param: structure containing the new RT priority.
  3720. */
  3721. asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
  3722. struct sched_param __user *param)
  3723. {
  3724. /* negative values for policy are not valid */
  3725. if (policy < 0)
  3726. return -EINVAL;
  3727. return do_sched_setscheduler(pid, policy, param);
  3728. }
  3729. /**
  3730. * sys_sched_setparam - set/change the RT priority of a thread
  3731. * @pid: the pid in question.
  3732. * @param: structure containing the new RT priority.
  3733. */
  3734. asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
  3735. {
  3736. return do_sched_setscheduler(pid, -1, param);
  3737. }
  3738. /**
  3739. * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  3740. * @pid: the pid in question.
  3741. */
  3742. asmlinkage long sys_sched_getscheduler(pid_t pid)
  3743. {
  3744. struct task_struct *p;
  3745. int retval = -EINVAL;
  3746. if (pid < 0)
  3747. goto out_nounlock;
  3748. retval = -ESRCH;
  3749. read_lock(&tasklist_lock);
  3750. p = find_process_by_pid(pid);
  3751. if (p) {
  3752. retval = security_task_getscheduler(p);
  3753. if (!retval)
  3754. retval = p->policy;
  3755. }
  3756. read_unlock(&tasklist_lock);
  3757. out_nounlock:
  3758. return retval;
  3759. }
  3760. /**
  3761. * sys_sched_getscheduler - get the RT priority of a thread
  3762. * @pid: the pid in question.
  3763. * @param: structure containing the RT priority.
  3764. */
  3765. asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
  3766. {
  3767. struct sched_param lp;
  3768. struct task_struct *p;
  3769. int retval = -EINVAL;
  3770. if (!param || pid < 0)
  3771. goto out_nounlock;
  3772. read_lock(&tasklist_lock);
  3773. p = find_process_by_pid(pid);
  3774. retval = -ESRCH;
  3775. if (!p)
  3776. goto out_unlock;
  3777. retval = security_task_getscheduler(p);
  3778. if (retval)
  3779. goto out_unlock;
  3780. lp.sched_priority = p->rt_priority;
  3781. read_unlock(&tasklist_lock);
  3782. /*
  3783. * This one might sleep, we cannot do it with a spinlock held ...
  3784. */
  3785. retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  3786. out_nounlock:
  3787. return retval;
  3788. out_unlock:
  3789. read_unlock(&tasklist_lock);
  3790. return retval;
  3791. }
  3792. long sched_setaffinity(pid_t pid, cpumask_t new_mask)
  3793. {
  3794. cpumask_t cpus_allowed;
  3795. struct task_struct *p;
  3796. int retval;
  3797. mutex_lock(&sched_hotcpu_mutex);
  3798. read_lock(&tasklist_lock);
  3799. p = find_process_by_pid(pid);
  3800. if (!p) {
  3801. read_unlock(&tasklist_lock);
  3802. mutex_unlock(&sched_hotcpu_mutex);
  3803. return -ESRCH;
  3804. }
  3805. /*
  3806. * It is not safe to call set_cpus_allowed with the
  3807. * tasklist_lock held. We will bump the task_struct's
  3808. * usage count and then drop tasklist_lock.
  3809. */
  3810. get_task_struct(p);
  3811. read_unlock(&tasklist_lock);
  3812. retval = -EPERM;
  3813. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  3814. !capable(CAP_SYS_NICE))
  3815. goto out_unlock;
  3816. retval = security_task_setscheduler(p, 0, NULL);
  3817. if (retval)
  3818. goto out_unlock;
  3819. cpus_allowed = cpuset_cpus_allowed(p);
  3820. cpus_and(new_mask, new_mask, cpus_allowed);
  3821. retval = set_cpus_allowed(p, new_mask);
  3822. out_unlock:
  3823. put_task_struct(p);
  3824. mutex_unlock(&sched_hotcpu_mutex);
  3825. return retval;
  3826. }
  3827. static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  3828. cpumask_t *new_mask)
  3829. {
  3830. if (len < sizeof(cpumask_t)) {
  3831. memset(new_mask, 0, sizeof(cpumask_t));
  3832. } else if (len > sizeof(cpumask_t)) {
  3833. len = sizeof(cpumask_t);
  3834. }
  3835. return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  3836. }
  3837. /**
  3838. * sys_sched_setaffinity - set the cpu affinity of a process
  3839. * @pid: pid of the process
  3840. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3841. * @user_mask_ptr: user-space pointer to the new cpu mask
  3842. */
  3843. asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
  3844. unsigned long __user *user_mask_ptr)
  3845. {
  3846. cpumask_t new_mask;
  3847. int retval;
  3848. retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
  3849. if (retval)
  3850. return retval;
  3851. return sched_setaffinity(pid, new_mask);
  3852. }
  3853. /*
  3854. * Represents all cpu's present in the system
  3855. * In systems capable of hotplug, this map could dynamically grow
  3856. * as new cpu's are detected in the system via any platform specific
  3857. * method, such as ACPI for e.g.
  3858. */
  3859. cpumask_t cpu_present_map __read_mostly;
  3860. EXPORT_SYMBOL(cpu_present_map);
  3861. #ifndef CONFIG_SMP
  3862. cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
  3863. EXPORT_SYMBOL(cpu_online_map);
  3864. cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
  3865. EXPORT_SYMBOL(cpu_possible_map);
  3866. #endif
  3867. long sched_getaffinity(pid_t pid, cpumask_t *mask)
  3868. {
  3869. struct task_struct *p;
  3870. int retval;
  3871. mutex_lock(&sched_hotcpu_mutex);
  3872. read_lock(&tasklist_lock);
  3873. retval = -ESRCH;
  3874. p = find_process_by_pid(pid);
  3875. if (!p)
  3876. goto out_unlock;
  3877. retval = security_task_getscheduler(p);
  3878. if (retval)
  3879. goto out_unlock;
  3880. cpus_and(*mask, p->cpus_allowed, cpu_online_map);
  3881. out_unlock:
  3882. read_unlock(&tasklist_lock);
  3883. mutex_unlock(&sched_hotcpu_mutex);
  3884. return retval;
  3885. }
  3886. /**
  3887. * sys_sched_getaffinity - get the cpu affinity of a process
  3888. * @pid: pid of the process
  3889. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3890. * @user_mask_ptr: user-space pointer to hold the current cpu mask
  3891. */
  3892. asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
  3893. unsigned long __user *user_mask_ptr)
  3894. {
  3895. int ret;
  3896. cpumask_t mask;
  3897. if (len < sizeof(cpumask_t))
  3898. return -EINVAL;
  3899. ret = sched_getaffinity(pid, &mask);
  3900. if (ret < 0)
  3901. return ret;
  3902. if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
  3903. return -EFAULT;
  3904. return sizeof(cpumask_t);
  3905. }
  3906. /**
  3907. * sys_sched_yield - yield the current processor to other threads.
  3908. *
  3909. * This function yields the current CPU to other tasks. If there are no
  3910. * other threads running on this CPU then this function will return.
  3911. */
  3912. asmlinkage long sys_sched_yield(void)
  3913. {
  3914. struct rq *rq = this_rq_lock();
  3915. schedstat_inc(rq, yld_cnt);
  3916. current->sched_class->yield_task(rq);
  3917. /*
  3918. * Since we are going to call schedule() anyway, there's
  3919. * no need to preempt or enable interrupts:
  3920. */
  3921. __release(rq->lock);
  3922. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  3923. _raw_spin_unlock(&rq->lock);
  3924. preempt_enable_no_resched();
  3925. schedule();
  3926. return 0;
  3927. }
  3928. static void __cond_resched(void)
  3929. {
  3930. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  3931. __might_sleep(__FILE__, __LINE__);
  3932. #endif
  3933. /*
  3934. * The BKS might be reacquired before we have dropped
  3935. * PREEMPT_ACTIVE, which could trigger a second
  3936. * cond_resched() call.
  3937. */
  3938. do {
  3939. add_preempt_count(PREEMPT_ACTIVE);
  3940. schedule();
  3941. sub_preempt_count(PREEMPT_ACTIVE);
  3942. } while (need_resched());
  3943. }
  3944. int __sched cond_resched(void)
  3945. {
  3946. if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
  3947. system_state == SYSTEM_RUNNING) {
  3948. __cond_resched();
  3949. return 1;
  3950. }
  3951. return 0;
  3952. }
  3953. EXPORT_SYMBOL(cond_resched);
  3954. /*
  3955. * cond_resched_lock() - if a reschedule is pending, drop the given lock,
  3956. * call schedule, and on return reacquire the lock.
  3957. *
  3958. * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  3959. * operations here to prevent schedule() from being called twice (once via
  3960. * spin_unlock(), once by hand).
  3961. */
  3962. int cond_resched_lock(spinlock_t *lock)
  3963. {
  3964. int ret = 0;
  3965. if (need_lockbreak(lock)) {
  3966. spin_unlock(lock);
  3967. cpu_relax();
  3968. ret = 1;
  3969. spin_lock(lock);
  3970. }
  3971. if (need_resched() && system_state == SYSTEM_RUNNING) {
  3972. spin_release(&lock->dep_map, 1, _THIS_IP_);
  3973. _raw_spin_unlock(lock);
  3974. preempt_enable_no_resched();
  3975. __cond_resched();
  3976. ret = 1;
  3977. spin_lock(lock);
  3978. }
  3979. return ret;
  3980. }
  3981. EXPORT_SYMBOL(cond_resched_lock);
  3982. int __sched cond_resched_softirq(void)
  3983. {
  3984. BUG_ON(!in_softirq());
  3985. if (need_resched() && system_state == SYSTEM_RUNNING) {
  3986. local_bh_enable();
  3987. __cond_resched();
  3988. local_bh_disable();
  3989. return 1;
  3990. }
  3991. return 0;
  3992. }
  3993. EXPORT_SYMBOL(cond_resched_softirq);
  3994. /**
  3995. * yield - yield the current processor to other threads.
  3996. *
  3997. * This is a shortcut for kernel-space yielding - it marks the
  3998. * thread runnable and calls sys_sched_yield().
  3999. */
  4000. void __sched yield(void)
  4001. {
  4002. set_current_state(TASK_RUNNING);
  4003. sys_sched_yield();
  4004. }
  4005. EXPORT_SYMBOL(yield);
  4006. /*
  4007. * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  4008. * that process accounting knows that this is a task in IO wait state.
  4009. *
  4010. * But don't do that if it is a deliberate, throttling IO wait (this task
  4011. * has set its backing_dev_info: the queue against which it should throttle)
  4012. */
  4013. void __sched io_schedule(void)
  4014. {
  4015. struct rq *rq = &__raw_get_cpu_var(runqueues);
  4016. delayacct_blkio_start();
  4017. atomic_inc(&rq->nr_iowait);
  4018. schedule();
  4019. atomic_dec(&rq->nr_iowait);
  4020. delayacct_blkio_end();
  4021. }
  4022. EXPORT_SYMBOL(io_schedule);
  4023. long __sched io_schedule_timeout(long timeout)
  4024. {
  4025. struct rq *rq = &__raw_get_cpu_var(runqueues);
  4026. long ret;
  4027. delayacct_blkio_start();
  4028. atomic_inc(&rq->nr_iowait);
  4029. ret = schedule_timeout(timeout);
  4030. atomic_dec(&rq->nr_iowait);
  4031. delayacct_blkio_end();
  4032. return ret;
  4033. }
  4034. /**
  4035. * sys_sched_get_priority_max - return maximum RT priority.
  4036. * @policy: scheduling class.
  4037. *
  4038. * this syscall returns the maximum rt_priority that can be used
  4039. * by a given scheduling class.
  4040. */
  4041. asmlinkage long sys_sched_get_priority_max(int policy)
  4042. {
  4043. int ret = -EINVAL;
  4044. switch (policy) {
  4045. case SCHED_FIFO:
  4046. case SCHED_RR:
  4047. ret = MAX_USER_RT_PRIO-1;
  4048. break;
  4049. case SCHED_NORMAL:
  4050. case SCHED_BATCH:
  4051. case SCHED_IDLE:
  4052. ret = 0;
  4053. break;
  4054. }
  4055. return ret;
  4056. }
  4057. /**
  4058. * sys_sched_get_priority_min - return minimum RT priority.
  4059. * @policy: scheduling class.
  4060. *
  4061. * this syscall returns the minimum rt_priority that can be used
  4062. * by a given scheduling class.
  4063. */
  4064. asmlinkage long sys_sched_get_priority_min(int policy)
  4065. {
  4066. int ret = -EINVAL;
  4067. switch (policy) {
  4068. case SCHED_FIFO:
  4069. case SCHED_RR:
  4070. ret = 1;
  4071. break;
  4072. case SCHED_NORMAL:
  4073. case SCHED_BATCH:
  4074. case SCHED_IDLE:
  4075. ret = 0;
  4076. }
  4077. return ret;
  4078. }
  4079. /**
  4080. * sys_sched_rr_get_interval - return the default timeslice of a process.
  4081. * @pid: pid of the process.
  4082. * @interval: userspace pointer to the timeslice value.
  4083. *
  4084. * this syscall writes the default timeslice value of a given process
  4085. * into the user-space timespec buffer. A value of '0' means infinity.
  4086. */
  4087. asmlinkage
  4088. long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
  4089. {
  4090. struct task_struct *p;
  4091. int retval = -EINVAL;
  4092. struct timespec t;
  4093. if (pid < 0)
  4094. goto out_nounlock;
  4095. retval = -ESRCH;
  4096. read_lock(&tasklist_lock);
  4097. p = find_process_by_pid(pid);
  4098. if (!p)
  4099. goto out_unlock;
  4100. retval = security_task_getscheduler(p);
  4101. if (retval)
  4102. goto out_unlock;
  4103. jiffies_to_timespec(p->policy == SCHED_FIFO ?
  4104. 0 : static_prio_timeslice(p->static_prio), &t);
  4105. read_unlock(&tasklist_lock);
  4106. retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  4107. out_nounlock:
  4108. return retval;
  4109. out_unlock:
  4110. read_unlock(&tasklist_lock);
  4111. return retval;
  4112. }
  4113. static const char stat_nam[] = "RSDTtZX";
  4114. static void show_task(struct task_struct *p)
  4115. {
  4116. unsigned long free = 0;
  4117. unsigned state;
  4118. state = p->state ? __ffs(p->state) + 1 : 0;
  4119. printk("%-13.13s %c", p->comm,
  4120. state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
  4121. #if BITS_PER_LONG == 32
  4122. if (state == TASK_RUNNING)
  4123. printk(" running ");
  4124. else
  4125. printk(" %08lx ", thread_saved_pc(p));
  4126. #else
  4127. if (state == TASK_RUNNING)
  4128. printk(" running task ");
  4129. else
  4130. printk(" %016lx ", thread_saved_pc(p));
  4131. #endif
  4132. #ifdef CONFIG_DEBUG_STACK_USAGE
  4133. {
  4134. unsigned long *n = end_of_stack(p);
  4135. while (!*n)
  4136. n++;
  4137. free = (unsigned long)n - (unsigned long)end_of_stack(p);
  4138. }
  4139. #endif
  4140. printk("%5lu %5d %6d\n", free, p->pid, p->parent->pid);
  4141. if (state != TASK_RUNNING)
  4142. show_stack(p, NULL);
  4143. }
  4144. void show_state_filter(unsigned long state_filter)
  4145. {
  4146. struct task_struct *g, *p;
  4147. #if BITS_PER_LONG == 32
  4148. printk(KERN_INFO
  4149. " task PC stack pid father\n");
  4150. #else
  4151. printk(KERN_INFO
  4152. " task PC stack pid father\n");
  4153. #endif
  4154. read_lock(&tasklist_lock);
  4155. do_each_thread(g, p) {
  4156. /*
  4157. * reset the NMI-timeout, listing all files on a slow
  4158. * console might take alot of time:
  4159. */
  4160. touch_nmi_watchdog();
  4161. if (!state_filter || (p->state & state_filter))
  4162. show_task(p);
  4163. } while_each_thread(g, p);
  4164. touch_all_softlockup_watchdogs();
  4165. #ifdef CONFIG_SCHED_DEBUG
  4166. sysrq_sched_debug_show();
  4167. #endif
  4168. read_unlock(&tasklist_lock);
  4169. /*
  4170. * Only show locks if all tasks are dumped:
  4171. */
  4172. if (state_filter == -1)
  4173. debug_show_all_locks();
  4174. }
  4175. void __cpuinit init_idle_bootup_task(struct task_struct *idle)
  4176. {
  4177. idle->sched_class = &idle_sched_class;
  4178. }
  4179. /**
  4180. * init_idle - set up an idle thread for a given CPU
  4181. * @idle: task in question
  4182. * @cpu: cpu the idle task belongs to
  4183. *
  4184. * NOTE: this function does not set the idle thread's NEED_RESCHED
  4185. * flag, to make booting more robust.
  4186. */
  4187. void __cpuinit init_idle(struct task_struct *idle, int cpu)
  4188. {
  4189. struct rq *rq = cpu_rq(cpu);
  4190. unsigned long flags;
  4191. __sched_fork(idle);
  4192. idle->se.exec_start = sched_clock();
  4193. idle->prio = idle->normal_prio = MAX_PRIO;
  4194. idle->cpus_allowed = cpumask_of_cpu(cpu);
  4195. __set_task_cpu(idle, cpu);
  4196. spin_lock_irqsave(&rq->lock, flags);
  4197. rq->curr = rq->idle = idle;
  4198. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  4199. idle->oncpu = 1;
  4200. #endif
  4201. spin_unlock_irqrestore(&rq->lock, flags);
  4202. /* Set the preempt count _outside_ the spinlocks! */
  4203. #if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL)
  4204. task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
  4205. #else
  4206. task_thread_info(idle)->preempt_count = 0;
  4207. #endif
  4208. /*
  4209. * The idle tasks have their own, simple scheduling class:
  4210. */
  4211. idle->sched_class = &idle_sched_class;
  4212. }
  4213. /*
  4214. * In a system that switches off the HZ timer nohz_cpu_mask
  4215. * indicates which cpus entered this state. This is used
  4216. * in the rcu update to wait only for active cpus. For system
  4217. * which do not switch off the HZ timer nohz_cpu_mask should
  4218. * always be CPU_MASK_NONE.
  4219. */
  4220. cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
  4221. #ifdef CONFIG_SMP
  4222. /*
  4223. * This is how migration works:
  4224. *
  4225. * 1) we queue a struct migration_req structure in the source CPU's
  4226. * runqueue and wake up that CPU's migration thread.
  4227. * 2) we down() the locked semaphore => thread blocks.
  4228. * 3) migration thread wakes up (implicitly it forces the migrated
  4229. * thread off the CPU)
  4230. * 4) it gets the migration request and checks whether the migrated
  4231. * task is still in the wrong runqueue.
  4232. * 5) if it's in the wrong runqueue then the migration thread removes
  4233. * it and puts it into the right queue.
  4234. * 6) migration thread up()s the semaphore.
  4235. * 7) we wake up and the migration is done.
  4236. */
  4237. /*
  4238. * Change a given task's CPU affinity. Migrate the thread to a
  4239. * proper CPU and schedule it away if the CPU it's executing on
  4240. * is removed from the allowed bitmask.
  4241. *
  4242. * NOTE: the caller must have a valid reference to the task, the
  4243. * task must not exit() & deallocate itself prematurely. The
  4244. * call is not atomic; no spinlocks may be held.
  4245. */
  4246. int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
  4247. {
  4248. struct migration_req req;
  4249. unsigned long flags;
  4250. struct rq *rq;
  4251. int ret = 0;
  4252. rq = task_rq_lock(p, &flags);
  4253. if (!cpus_intersects(new_mask, cpu_online_map)) {
  4254. ret = -EINVAL;
  4255. goto out;
  4256. }
  4257. p->cpus_allowed = new_mask;
  4258. /* Can the task run on the task's current CPU? If so, we're done */
  4259. if (cpu_isset(task_cpu(p), new_mask))
  4260. goto out;
  4261. if (migrate_task(p, any_online_cpu(new_mask), &req)) {
  4262. /* Need help from migration thread: drop lock and wait. */
  4263. task_rq_unlock(rq, &flags);
  4264. wake_up_process(rq->migration_thread);
  4265. wait_for_completion(&req.done);
  4266. tlb_migrate_finish(p->mm);
  4267. return 0;
  4268. }
  4269. out:
  4270. task_rq_unlock(rq, &flags);
  4271. return ret;
  4272. }
  4273. EXPORT_SYMBOL_GPL(set_cpus_allowed);
  4274. /*
  4275. * Move (not current) task off this cpu, onto dest cpu. We're doing
  4276. * this because either it can't run here any more (set_cpus_allowed()
  4277. * away from this CPU, or CPU going down), or because we're
  4278. * attempting to rebalance this task on exec (sched_exec).
  4279. *
  4280. * So we race with normal scheduler movements, but that's OK, as long
  4281. * as the task is no longer on this CPU.
  4282. *
  4283. * Returns non-zero if task was successfully migrated.
  4284. */
  4285. static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
  4286. {
  4287. struct rq *rq_dest, *rq_src;
  4288. int ret = 0, on_rq;
  4289. if (unlikely(cpu_is_offline(dest_cpu)))
  4290. return ret;
  4291. rq_src = cpu_rq(src_cpu);
  4292. rq_dest = cpu_rq(dest_cpu);
  4293. double_rq_lock(rq_src, rq_dest);
  4294. /* Already moved. */
  4295. if (task_cpu(p) != src_cpu)
  4296. goto out;
  4297. /* Affinity changed (again). */
  4298. if (!cpu_isset(dest_cpu, p->cpus_allowed))
  4299. goto out;
  4300. on_rq = p->se.on_rq;
  4301. if (on_rq)
  4302. deactivate_task(rq_src, p, 0);
  4303. set_task_cpu(p, dest_cpu);
  4304. if (on_rq) {
  4305. activate_task(rq_dest, p, 0);
  4306. check_preempt_curr(rq_dest, p);
  4307. }
  4308. ret = 1;
  4309. out:
  4310. double_rq_unlock(rq_src, rq_dest);
  4311. return ret;
  4312. }
  4313. /*
  4314. * migration_thread - this is a highprio system thread that performs
  4315. * thread migration by bumping thread off CPU then 'pushing' onto
  4316. * another runqueue.
  4317. */
  4318. static int migration_thread(void *data)
  4319. {
  4320. int cpu = (long)data;
  4321. struct rq *rq;
  4322. rq = cpu_rq(cpu);
  4323. BUG_ON(rq->migration_thread != current);
  4324. set_current_state(TASK_INTERRUPTIBLE);
  4325. while (!kthread_should_stop()) {
  4326. struct migration_req *req;
  4327. struct list_head *head;
  4328. spin_lock_irq(&rq->lock);
  4329. if (cpu_is_offline(cpu)) {
  4330. spin_unlock_irq(&rq->lock);
  4331. goto wait_to_die;
  4332. }
  4333. if (rq->active_balance) {
  4334. active_load_balance(rq, cpu);
  4335. rq->active_balance = 0;
  4336. }
  4337. head = &rq->migration_queue;
  4338. if (list_empty(head)) {
  4339. spin_unlock_irq(&rq->lock);
  4340. schedule();
  4341. set_current_state(TASK_INTERRUPTIBLE);
  4342. continue;
  4343. }
  4344. req = list_entry(head->next, struct migration_req, list);
  4345. list_del_init(head->next);
  4346. spin_unlock(&rq->lock);
  4347. __migrate_task(req->task, cpu, req->dest_cpu);
  4348. local_irq_enable();
  4349. complete(&req->done);
  4350. }
  4351. __set_current_state(TASK_RUNNING);
  4352. return 0;
  4353. wait_to_die:
  4354. /* Wait for kthread_stop */
  4355. set_current_state(TASK_INTERRUPTIBLE);
  4356. while (!kthread_should_stop()) {
  4357. schedule();
  4358. set_current_state(TASK_INTERRUPTIBLE);
  4359. }
  4360. __set_current_state(TASK_RUNNING);
  4361. return 0;
  4362. }
  4363. #ifdef CONFIG_HOTPLUG_CPU
  4364. /*
  4365. * Figure out where task on dead CPU should go, use force if neccessary.
  4366. * NOTE: interrupts should be disabled by the caller
  4367. */
  4368. static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
  4369. {
  4370. unsigned long flags;
  4371. cpumask_t mask;
  4372. struct rq *rq;
  4373. int dest_cpu;
  4374. restart:
  4375. /* On same node? */
  4376. mask = node_to_cpumask(cpu_to_node(dead_cpu));
  4377. cpus_and(mask, mask, p->cpus_allowed);
  4378. dest_cpu = any_online_cpu(mask);
  4379. /* On any allowed CPU? */
  4380. if (dest_cpu == NR_CPUS)
  4381. dest_cpu = any_online_cpu(p->cpus_allowed);
  4382. /* No more Mr. Nice Guy. */
  4383. if (dest_cpu == NR_CPUS) {
  4384. rq = task_rq_lock(p, &flags);
  4385. cpus_setall(p->cpus_allowed);
  4386. dest_cpu = any_online_cpu(p->cpus_allowed);
  4387. task_rq_unlock(rq, &flags);
  4388. /*
  4389. * Don't tell them about moving exiting tasks or
  4390. * kernel threads (both mm NULL), since they never
  4391. * leave kernel.
  4392. */
  4393. if (p->mm && printk_ratelimit())
  4394. printk(KERN_INFO "process %d (%s) no "
  4395. "longer affine to cpu%d\n",
  4396. p->pid, p->comm, dead_cpu);
  4397. }
  4398. if (!__migrate_task(p, dead_cpu, dest_cpu))
  4399. goto restart;
  4400. }
  4401. /*
  4402. * While a dead CPU has no uninterruptible tasks queued at this point,
  4403. * it might still have a nonzero ->nr_uninterruptible counter, because
  4404. * for performance reasons the counter is not stricly tracking tasks to
  4405. * their home CPUs. So we just add the counter to another CPU's counter,
  4406. * to keep the global sum constant after CPU-down:
  4407. */
  4408. static void migrate_nr_uninterruptible(struct rq *rq_src)
  4409. {
  4410. struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
  4411. unsigned long flags;
  4412. local_irq_save(flags);
  4413. double_rq_lock(rq_src, rq_dest);
  4414. rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
  4415. rq_src->nr_uninterruptible = 0;
  4416. double_rq_unlock(rq_src, rq_dest);
  4417. local_irq_restore(flags);
  4418. }
  4419. /* Run through task list and migrate tasks from the dead cpu. */
  4420. static void migrate_live_tasks(int src_cpu)
  4421. {
  4422. struct task_struct *p, *t;
  4423. write_lock_irq(&tasklist_lock);
  4424. do_each_thread(t, p) {
  4425. if (p == current)
  4426. continue;
  4427. if (task_cpu(p) == src_cpu)
  4428. move_task_off_dead_cpu(src_cpu, p);
  4429. } while_each_thread(t, p);
  4430. write_unlock_irq(&tasklist_lock);
  4431. }
  4432. /*
  4433. * Schedules idle task to be the next runnable task on current CPU.
  4434. * It does so by boosting its priority to highest possible and adding it to
  4435. * the _front_ of the runqueue. Used by CPU offline code.
  4436. */
  4437. void sched_idle_next(void)
  4438. {
  4439. int this_cpu = smp_processor_id();
  4440. struct rq *rq = cpu_rq(this_cpu);
  4441. struct task_struct *p = rq->idle;
  4442. unsigned long flags;
  4443. /* cpu has to be offline */
  4444. BUG_ON(cpu_online(this_cpu));
  4445. /*
  4446. * Strictly not necessary since rest of the CPUs are stopped by now
  4447. * and interrupts disabled on the current cpu.
  4448. */
  4449. spin_lock_irqsave(&rq->lock, flags);
  4450. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  4451. /* Add idle task to the _front_ of its priority queue: */
  4452. activate_idle_task(p, rq);
  4453. spin_unlock_irqrestore(&rq->lock, flags);
  4454. }
  4455. /*
  4456. * Ensures that the idle task is using init_mm right before its cpu goes
  4457. * offline.
  4458. */
  4459. void idle_task_exit(void)
  4460. {
  4461. struct mm_struct *mm = current->active_mm;
  4462. BUG_ON(cpu_online(smp_processor_id()));
  4463. if (mm != &init_mm)
  4464. switch_mm(mm, &init_mm, current);
  4465. mmdrop(mm);
  4466. }
  4467. /* called under rq->lock with disabled interrupts */
  4468. static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
  4469. {
  4470. struct rq *rq = cpu_rq(dead_cpu);
  4471. /* Must be exiting, otherwise would be on tasklist. */
  4472. BUG_ON(p->exit_state != EXIT_ZOMBIE && p->exit_state != EXIT_DEAD);
  4473. /* Cannot have done final schedule yet: would have vanished. */
  4474. BUG_ON(p->state == TASK_DEAD);
  4475. get_task_struct(p);
  4476. /*
  4477. * Drop lock around migration; if someone else moves it,
  4478. * that's OK. No task can be added to this CPU, so iteration is
  4479. * fine.
  4480. * NOTE: interrupts should be left disabled --dev@
  4481. */
  4482. spin_unlock(&rq->lock);
  4483. move_task_off_dead_cpu(dead_cpu, p);
  4484. spin_lock(&rq->lock);
  4485. put_task_struct(p);
  4486. }
  4487. /* release_task() removes task from tasklist, so we won't find dead tasks. */
  4488. static void migrate_dead_tasks(unsigned int dead_cpu)
  4489. {
  4490. struct rq *rq = cpu_rq(dead_cpu);
  4491. struct task_struct *next;
  4492. for ( ; ; ) {
  4493. if (!rq->nr_running)
  4494. break;
  4495. update_rq_clock(rq);
  4496. next = pick_next_task(rq, rq->curr);
  4497. if (!next)
  4498. break;
  4499. migrate_dead(dead_cpu, next);
  4500. }
  4501. }
  4502. #endif /* CONFIG_HOTPLUG_CPU */
  4503. #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
  4504. static struct ctl_table sd_ctl_dir[] = {
  4505. {
  4506. .procname = "sched_domain",
  4507. .mode = 0555,
  4508. },
  4509. {0,},
  4510. };
  4511. static struct ctl_table sd_ctl_root[] = {
  4512. {
  4513. .ctl_name = CTL_KERN,
  4514. .procname = "kernel",
  4515. .mode = 0555,
  4516. .child = sd_ctl_dir,
  4517. },
  4518. {0,},
  4519. };
  4520. static struct ctl_table *sd_alloc_ctl_entry(int n)
  4521. {
  4522. struct ctl_table *entry =
  4523. kmalloc(n * sizeof(struct ctl_table), GFP_KERNEL);
  4524. BUG_ON(!entry);
  4525. memset(entry, 0, n * sizeof(struct ctl_table));
  4526. return entry;
  4527. }
  4528. static void
  4529. set_table_entry(struct ctl_table *entry,
  4530. const char *procname, void *data, int maxlen,
  4531. mode_t mode, proc_handler *proc_handler)
  4532. {
  4533. entry->procname = procname;
  4534. entry->data = data;
  4535. entry->maxlen = maxlen;
  4536. entry->mode = mode;
  4537. entry->proc_handler = proc_handler;
  4538. }
  4539. static struct ctl_table *
  4540. sd_alloc_ctl_domain_table(struct sched_domain *sd)
  4541. {
  4542. struct ctl_table *table = sd_alloc_ctl_entry(14);
  4543. set_table_entry(&table[0], "min_interval", &sd->min_interval,
  4544. sizeof(long), 0644, proc_doulongvec_minmax);
  4545. set_table_entry(&table[1], "max_interval", &sd->max_interval,
  4546. sizeof(long), 0644, proc_doulongvec_minmax);
  4547. set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
  4548. sizeof(int), 0644, proc_dointvec_minmax);
  4549. set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
  4550. sizeof(int), 0644, proc_dointvec_minmax);
  4551. set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
  4552. sizeof(int), 0644, proc_dointvec_minmax);
  4553. set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
  4554. sizeof(int), 0644, proc_dointvec_minmax);
  4555. set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
  4556. sizeof(int), 0644, proc_dointvec_minmax);
  4557. set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
  4558. sizeof(int), 0644, proc_dointvec_minmax);
  4559. set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
  4560. sizeof(int), 0644, proc_dointvec_minmax);
  4561. set_table_entry(&table[10], "cache_nice_tries",
  4562. &sd->cache_nice_tries,
  4563. sizeof(int), 0644, proc_dointvec_minmax);
  4564. set_table_entry(&table[12], "flags", &sd->flags,
  4565. sizeof(int), 0644, proc_dointvec_minmax);
  4566. return table;
  4567. }
  4568. static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
  4569. {
  4570. struct ctl_table *entry, *table;
  4571. struct sched_domain *sd;
  4572. int domain_num = 0, i;
  4573. char buf[32];
  4574. for_each_domain(cpu, sd)
  4575. domain_num++;
  4576. entry = table = sd_alloc_ctl_entry(domain_num + 1);
  4577. i = 0;
  4578. for_each_domain(cpu, sd) {
  4579. snprintf(buf, 32, "domain%d", i);
  4580. entry->procname = kstrdup(buf, GFP_KERNEL);
  4581. entry->mode = 0555;
  4582. entry->child = sd_alloc_ctl_domain_table(sd);
  4583. entry++;
  4584. i++;
  4585. }
  4586. return table;
  4587. }
  4588. static struct ctl_table_header *sd_sysctl_header;
  4589. static void init_sched_domain_sysctl(void)
  4590. {
  4591. int i, cpu_num = num_online_cpus();
  4592. struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
  4593. char buf[32];
  4594. sd_ctl_dir[0].child = entry;
  4595. for (i = 0; i < cpu_num; i++, entry++) {
  4596. snprintf(buf, 32, "cpu%d", i);
  4597. entry->procname = kstrdup(buf, GFP_KERNEL);
  4598. entry->mode = 0555;
  4599. entry->child = sd_alloc_ctl_cpu_table(i);
  4600. }
  4601. sd_sysctl_header = register_sysctl_table(sd_ctl_root);
  4602. }
  4603. #else
  4604. static void init_sched_domain_sysctl(void)
  4605. {
  4606. }
  4607. #endif
  4608. /*
  4609. * migration_call - callback that gets triggered when a CPU is added.
  4610. * Here we can start up the necessary migration thread for the new CPU.
  4611. */
  4612. static int __cpuinit
  4613. migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  4614. {
  4615. struct task_struct *p;
  4616. int cpu = (long)hcpu;
  4617. unsigned long flags;
  4618. struct rq *rq;
  4619. switch (action) {
  4620. case CPU_LOCK_ACQUIRE:
  4621. mutex_lock(&sched_hotcpu_mutex);
  4622. break;
  4623. case CPU_UP_PREPARE:
  4624. case CPU_UP_PREPARE_FROZEN:
  4625. p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
  4626. if (IS_ERR(p))
  4627. return NOTIFY_BAD;
  4628. kthread_bind(p, cpu);
  4629. /* Must be high prio: stop_machine expects to yield to it. */
  4630. rq = task_rq_lock(p, &flags);
  4631. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  4632. task_rq_unlock(rq, &flags);
  4633. cpu_rq(cpu)->migration_thread = p;
  4634. break;
  4635. case CPU_ONLINE:
  4636. case CPU_ONLINE_FROZEN:
  4637. /* Strictly unneccessary, as first user will wake it. */
  4638. wake_up_process(cpu_rq(cpu)->migration_thread);
  4639. break;
  4640. #ifdef CONFIG_HOTPLUG_CPU
  4641. case CPU_UP_CANCELED:
  4642. case CPU_UP_CANCELED_FROZEN:
  4643. if (!cpu_rq(cpu)->migration_thread)
  4644. break;
  4645. /* Unbind it from offline cpu so it can run. Fall thru. */
  4646. kthread_bind(cpu_rq(cpu)->migration_thread,
  4647. any_online_cpu(cpu_online_map));
  4648. kthread_stop(cpu_rq(cpu)->migration_thread);
  4649. cpu_rq(cpu)->migration_thread = NULL;
  4650. break;
  4651. case CPU_DEAD:
  4652. case CPU_DEAD_FROZEN:
  4653. migrate_live_tasks(cpu);
  4654. rq = cpu_rq(cpu);
  4655. kthread_stop(rq->migration_thread);
  4656. rq->migration_thread = NULL;
  4657. /* Idle task back to normal (off runqueue, low prio) */
  4658. rq = task_rq_lock(rq->idle, &flags);
  4659. update_rq_clock(rq);
  4660. deactivate_task(rq, rq->idle, 0);
  4661. rq->idle->static_prio = MAX_PRIO;
  4662. __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
  4663. rq->idle->sched_class = &idle_sched_class;
  4664. migrate_dead_tasks(cpu);
  4665. task_rq_unlock(rq, &flags);
  4666. migrate_nr_uninterruptible(rq);
  4667. BUG_ON(rq->nr_running != 0);
  4668. /* No need to migrate the tasks: it was best-effort if
  4669. * they didn't take sched_hotcpu_mutex. Just wake up
  4670. * the requestors. */
  4671. spin_lock_irq(&rq->lock);
  4672. while (!list_empty(&rq->migration_queue)) {
  4673. struct migration_req *req;
  4674. req = list_entry(rq->migration_queue.next,
  4675. struct migration_req, list);
  4676. list_del_init(&req->list);
  4677. complete(&req->done);
  4678. }
  4679. spin_unlock_irq(&rq->lock);
  4680. break;
  4681. #endif
  4682. case CPU_LOCK_RELEASE:
  4683. mutex_unlock(&sched_hotcpu_mutex);
  4684. break;
  4685. }
  4686. return NOTIFY_OK;
  4687. }
  4688. /* Register at highest priority so that task migration (migrate_all_tasks)
  4689. * happens before everything else.
  4690. */
  4691. static struct notifier_block __cpuinitdata migration_notifier = {
  4692. .notifier_call = migration_call,
  4693. .priority = 10
  4694. };
  4695. int __init migration_init(void)
  4696. {
  4697. void *cpu = (void *)(long)smp_processor_id();
  4698. int err;
  4699. /* Start one for the boot CPU: */
  4700. err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  4701. BUG_ON(err == NOTIFY_BAD);
  4702. migration_call(&migration_notifier, CPU_ONLINE, cpu);
  4703. register_cpu_notifier(&migration_notifier);
  4704. return 0;
  4705. }
  4706. #endif
  4707. #ifdef CONFIG_SMP
  4708. /* Number of possible processor ids */
  4709. int nr_cpu_ids __read_mostly = NR_CPUS;
  4710. EXPORT_SYMBOL(nr_cpu_ids);
  4711. #undef SCHED_DOMAIN_DEBUG
  4712. #ifdef SCHED_DOMAIN_DEBUG
  4713. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  4714. {
  4715. int level = 0;
  4716. if (!sd) {
  4717. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  4718. return;
  4719. }
  4720. printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  4721. do {
  4722. int i;
  4723. char str[NR_CPUS];
  4724. struct sched_group *group = sd->groups;
  4725. cpumask_t groupmask;
  4726. cpumask_scnprintf(str, NR_CPUS, sd->span);
  4727. cpus_clear(groupmask);
  4728. printk(KERN_DEBUG);
  4729. for (i = 0; i < level + 1; i++)
  4730. printk(" ");
  4731. printk("domain %d: ", level);
  4732. if (!(sd->flags & SD_LOAD_BALANCE)) {
  4733. printk("does not load-balance\n");
  4734. if (sd->parent)
  4735. printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
  4736. " has parent");
  4737. break;
  4738. }
  4739. printk("span %s\n", str);
  4740. if (!cpu_isset(cpu, sd->span))
  4741. printk(KERN_ERR "ERROR: domain->span does not contain "
  4742. "CPU%d\n", cpu);
  4743. if (!cpu_isset(cpu, group->cpumask))
  4744. printk(KERN_ERR "ERROR: domain->groups does not contain"
  4745. " CPU%d\n", cpu);
  4746. printk(KERN_DEBUG);
  4747. for (i = 0; i < level + 2; i++)
  4748. printk(" ");
  4749. printk("groups:");
  4750. do {
  4751. if (!group) {
  4752. printk("\n");
  4753. printk(KERN_ERR "ERROR: group is NULL\n");
  4754. break;
  4755. }
  4756. if (!group->__cpu_power) {
  4757. printk("\n");
  4758. printk(KERN_ERR "ERROR: domain->cpu_power not "
  4759. "set\n");
  4760. }
  4761. if (!cpus_weight(group->cpumask)) {
  4762. printk("\n");
  4763. printk(KERN_ERR "ERROR: empty group\n");
  4764. }
  4765. if (cpus_intersects(groupmask, group->cpumask)) {
  4766. printk("\n");
  4767. printk(KERN_ERR "ERROR: repeated CPUs\n");
  4768. }
  4769. cpus_or(groupmask, groupmask, group->cpumask);
  4770. cpumask_scnprintf(str, NR_CPUS, group->cpumask);
  4771. printk(" %s", str);
  4772. group = group->next;
  4773. } while (group != sd->groups);
  4774. printk("\n");
  4775. if (!cpus_equal(sd->span, groupmask))
  4776. printk(KERN_ERR "ERROR: groups don't span "
  4777. "domain->span\n");
  4778. level++;
  4779. sd = sd->parent;
  4780. if (!sd)
  4781. continue;
  4782. if (!cpus_subset(groupmask, sd->span))
  4783. printk(KERN_ERR "ERROR: parent span is not a superset "
  4784. "of domain->span\n");
  4785. } while (sd);
  4786. }
  4787. #else
  4788. # define sched_domain_debug(sd, cpu) do { } while (0)
  4789. #endif
  4790. static int sd_degenerate(struct sched_domain *sd)
  4791. {
  4792. if (cpus_weight(sd->span) == 1)
  4793. return 1;
  4794. /* Following flags need at least 2 groups */
  4795. if (sd->flags & (SD_LOAD_BALANCE |
  4796. SD_BALANCE_NEWIDLE |
  4797. SD_BALANCE_FORK |
  4798. SD_BALANCE_EXEC |
  4799. SD_SHARE_CPUPOWER |
  4800. SD_SHARE_PKG_RESOURCES)) {
  4801. if (sd->groups != sd->groups->next)
  4802. return 0;
  4803. }
  4804. /* Following flags don't use groups */
  4805. if (sd->flags & (SD_WAKE_IDLE |
  4806. SD_WAKE_AFFINE |
  4807. SD_WAKE_BALANCE))
  4808. return 0;
  4809. return 1;
  4810. }
  4811. static int
  4812. sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  4813. {
  4814. unsigned long cflags = sd->flags, pflags = parent->flags;
  4815. if (sd_degenerate(parent))
  4816. return 1;
  4817. if (!cpus_equal(sd->span, parent->span))
  4818. return 0;
  4819. /* Does parent contain flags not in child? */
  4820. /* WAKE_BALANCE is a subset of WAKE_AFFINE */
  4821. if (cflags & SD_WAKE_AFFINE)
  4822. pflags &= ~SD_WAKE_BALANCE;
  4823. /* Flags needing groups don't count if only 1 group in parent */
  4824. if (parent->groups == parent->groups->next) {
  4825. pflags &= ~(SD_LOAD_BALANCE |
  4826. SD_BALANCE_NEWIDLE |
  4827. SD_BALANCE_FORK |
  4828. SD_BALANCE_EXEC |
  4829. SD_SHARE_CPUPOWER |
  4830. SD_SHARE_PKG_RESOURCES);
  4831. }
  4832. if (~cflags & pflags)
  4833. return 0;
  4834. return 1;
  4835. }
  4836. /*
  4837. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  4838. * hold the hotplug lock.
  4839. */
  4840. static void cpu_attach_domain(struct sched_domain *sd, int cpu)
  4841. {
  4842. struct rq *rq = cpu_rq(cpu);
  4843. struct sched_domain *tmp;
  4844. /* Remove the sched domains which do not contribute to scheduling. */
  4845. for (tmp = sd; tmp; tmp = tmp->parent) {
  4846. struct sched_domain *parent = tmp->parent;
  4847. if (!parent)
  4848. break;
  4849. if (sd_parent_degenerate(tmp, parent)) {
  4850. tmp->parent = parent->parent;
  4851. if (parent->parent)
  4852. parent->parent->child = tmp;
  4853. }
  4854. }
  4855. if (sd && sd_degenerate(sd)) {
  4856. sd = sd->parent;
  4857. if (sd)
  4858. sd->child = NULL;
  4859. }
  4860. sched_domain_debug(sd, cpu);
  4861. rcu_assign_pointer(rq->sd, sd);
  4862. }
  4863. /* cpus with isolated domains */
  4864. static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
  4865. /* Setup the mask of cpus configured for isolated domains */
  4866. static int __init isolated_cpu_setup(char *str)
  4867. {
  4868. int ints[NR_CPUS], i;
  4869. str = get_options(str, ARRAY_SIZE(ints), ints);
  4870. cpus_clear(cpu_isolated_map);
  4871. for (i = 1; i <= ints[0]; i++)
  4872. if (ints[i] < NR_CPUS)
  4873. cpu_set(ints[i], cpu_isolated_map);
  4874. return 1;
  4875. }
  4876. __setup ("isolcpus=", isolated_cpu_setup);
  4877. /*
  4878. * init_sched_build_groups takes the cpumask we wish to span, and a pointer
  4879. * to a function which identifies what group(along with sched group) a CPU
  4880. * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
  4881. * (due to the fact that we keep track of groups covered with a cpumask_t).
  4882. *
  4883. * init_sched_build_groups will build a circular linked list of the groups
  4884. * covered by the given span, and will set each group's ->cpumask correctly,
  4885. * and ->cpu_power to 0.
  4886. */
  4887. static void
  4888. init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
  4889. int (*group_fn)(int cpu, const cpumask_t *cpu_map,
  4890. struct sched_group **sg))
  4891. {
  4892. struct sched_group *first = NULL, *last = NULL;
  4893. cpumask_t covered = CPU_MASK_NONE;
  4894. int i;
  4895. for_each_cpu_mask(i, span) {
  4896. struct sched_group *sg;
  4897. int group = group_fn(i, cpu_map, &sg);
  4898. int j;
  4899. if (cpu_isset(i, covered))
  4900. continue;
  4901. sg->cpumask = CPU_MASK_NONE;
  4902. sg->__cpu_power = 0;
  4903. for_each_cpu_mask(j, span) {
  4904. if (group_fn(j, cpu_map, NULL) != group)
  4905. continue;
  4906. cpu_set(j, covered);
  4907. cpu_set(j, sg->cpumask);
  4908. }
  4909. if (!first)
  4910. first = sg;
  4911. if (last)
  4912. last->next = sg;
  4913. last = sg;
  4914. }
  4915. last->next = first;
  4916. }
  4917. #define SD_NODES_PER_DOMAIN 16
  4918. #ifdef CONFIG_NUMA
  4919. /**
  4920. * find_next_best_node - find the next node to include in a sched_domain
  4921. * @node: node whose sched_domain we're building
  4922. * @used_nodes: nodes already in the sched_domain
  4923. *
  4924. * Find the next node to include in a given scheduling domain. Simply
  4925. * finds the closest node not already in the @used_nodes map.
  4926. *
  4927. * Should use nodemask_t.
  4928. */
  4929. static int find_next_best_node(int node, unsigned long *used_nodes)
  4930. {
  4931. int i, n, val, min_val, best_node = 0;
  4932. min_val = INT_MAX;
  4933. for (i = 0; i < MAX_NUMNODES; i++) {
  4934. /* Start at @node */
  4935. n = (node + i) % MAX_NUMNODES;
  4936. if (!nr_cpus_node(n))
  4937. continue;
  4938. /* Skip already used nodes */
  4939. if (test_bit(n, used_nodes))
  4940. continue;
  4941. /* Simple min distance search */
  4942. val = node_distance(node, n);
  4943. if (val < min_val) {
  4944. min_val = val;
  4945. best_node = n;
  4946. }
  4947. }
  4948. set_bit(best_node, used_nodes);
  4949. return best_node;
  4950. }
  4951. /**
  4952. * sched_domain_node_span - get a cpumask for a node's sched_domain
  4953. * @node: node whose cpumask we're constructing
  4954. * @size: number of nodes to include in this span
  4955. *
  4956. * Given a node, construct a good cpumask for its sched_domain to span. It
  4957. * should be one that prevents unnecessary balancing, but also spreads tasks
  4958. * out optimally.
  4959. */
  4960. static cpumask_t sched_domain_node_span(int node)
  4961. {
  4962. DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
  4963. cpumask_t span, nodemask;
  4964. int i;
  4965. cpus_clear(span);
  4966. bitmap_zero(used_nodes, MAX_NUMNODES);
  4967. nodemask = node_to_cpumask(node);
  4968. cpus_or(span, span, nodemask);
  4969. set_bit(node, used_nodes);
  4970. for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
  4971. int next_node = find_next_best_node(node, used_nodes);
  4972. nodemask = node_to_cpumask(next_node);
  4973. cpus_or(span, span, nodemask);
  4974. }
  4975. return span;
  4976. }
  4977. #endif
  4978. int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
  4979. /*
  4980. * SMT sched-domains:
  4981. */
  4982. #ifdef CONFIG_SCHED_SMT
  4983. static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
  4984. static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
  4985. static int cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map,
  4986. struct sched_group **sg)
  4987. {
  4988. if (sg)
  4989. *sg = &per_cpu(sched_group_cpus, cpu);
  4990. return cpu;
  4991. }
  4992. #endif
  4993. /*
  4994. * multi-core sched-domains:
  4995. */
  4996. #ifdef CONFIG_SCHED_MC
  4997. static DEFINE_PER_CPU(struct sched_domain, core_domains);
  4998. static DEFINE_PER_CPU(struct sched_group, sched_group_core);
  4999. #endif
  5000. #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
  5001. static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
  5002. struct sched_group **sg)
  5003. {
  5004. int group;
  5005. cpumask_t mask = cpu_sibling_map[cpu];
  5006. cpus_and(mask, mask, *cpu_map);
  5007. group = first_cpu(mask);
  5008. if (sg)
  5009. *sg = &per_cpu(sched_group_core, group);
  5010. return group;
  5011. }
  5012. #elif defined(CONFIG_SCHED_MC)
  5013. static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
  5014. struct sched_group **sg)
  5015. {
  5016. if (sg)
  5017. *sg = &per_cpu(sched_group_core, cpu);
  5018. return cpu;
  5019. }
  5020. #endif
  5021. static DEFINE_PER_CPU(struct sched_domain, phys_domains);
  5022. static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
  5023. static int cpu_to_phys_group(int cpu, const cpumask_t *cpu_map,
  5024. struct sched_group **sg)
  5025. {
  5026. int group;
  5027. #ifdef CONFIG_SCHED_MC
  5028. cpumask_t mask = cpu_coregroup_map(cpu);
  5029. cpus_and(mask, mask, *cpu_map);
  5030. group = first_cpu(mask);
  5031. #elif defined(CONFIG_SCHED_SMT)
  5032. cpumask_t mask = cpu_sibling_map[cpu];
  5033. cpus_and(mask, mask, *cpu_map);
  5034. group = first_cpu(mask);
  5035. #else
  5036. group = cpu;
  5037. #endif
  5038. if (sg)
  5039. *sg = &per_cpu(sched_group_phys, group);
  5040. return group;
  5041. }
  5042. #ifdef CONFIG_NUMA
  5043. /*
  5044. * The init_sched_build_groups can't handle what we want to do with node
  5045. * groups, so roll our own. Now each node has its own list of groups which
  5046. * gets dynamically allocated.
  5047. */
  5048. static DEFINE_PER_CPU(struct sched_domain, node_domains);
  5049. static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
  5050. static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
  5051. static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
  5052. static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
  5053. struct sched_group **sg)
  5054. {
  5055. cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
  5056. int group;
  5057. cpus_and(nodemask, nodemask, *cpu_map);
  5058. group = first_cpu(nodemask);
  5059. if (sg)
  5060. *sg = &per_cpu(sched_group_allnodes, group);
  5061. return group;
  5062. }
  5063. static void init_numa_sched_groups_power(struct sched_group *group_head)
  5064. {
  5065. struct sched_group *sg = group_head;
  5066. int j;
  5067. if (!sg)
  5068. return;
  5069. next_sg:
  5070. for_each_cpu_mask(j, sg->cpumask) {
  5071. struct sched_domain *sd;
  5072. sd = &per_cpu(phys_domains, j);
  5073. if (j != first_cpu(sd->groups->cpumask)) {
  5074. /*
  5075. * Only add "power" once for each
  5076. * physical package.
  5077. */
  5078. continue;
  5079. }
  5080. sg_inc_cpu_power(sg, sd->groups->__cpu_power);
  5081. }
  5082. sg = sg->next;
  5083. if (sg != group_head)
  5084. goto next_sg;
  5085. }
  5086. #endif
  5087. #ifdef CONFIG_NUMA
  5088. /* Free memory allocated for various sched_group structures */
  5089. static void free_sched_groups(const cpumask_t *cpu_map)
  5090. {
  5091. int cpu, i;
  5092. for_each_cpu_mask(cpu, *cpu_map) {
  5093. struct sched_group **sched_group_nodes
  5094. = sched_group_nodes_bycpu[cpu];
  5095. if (!sched_group_nodes)
  5096. continue;
  5097. for (i = 0; i < MAX_NUMNODES; i++) {
  5098. cpumask_t nodemask = node_to_cpumask(i);
  5099. struct sched_group *oldsg, *sg = sched_group_nodes[i];
  5100. cpus_and(nodemask, nodemask, *cpu_map);
  5101. if (cpus_empty(nodemask))
  5102. continue;
  5103. if (sg == NULL)
  5104. continue;
  5105. sg = sg->next;
  5106. next_sg:
  5107. oldsg = sg;
  5108. sg = sg->next;
  5109. kfree(oldsg);
  5110. if (oldsg != sched_group_nodes[i])
  5111. goto next_sg;
  5112. }
  5113. kfree(sched_group_nodes);
  5114. sched_group_nodes_bycpu[cpu] = NULL;
  5115. }
  5116. }
  5117. #else
  5118. static void free_sched_groups(const cpumask_t *cpu_map)
  5119. {
  5120. }
  5121. #endif
  5122. /*
  5123. * Initialize sched groups cpu_power.
  5124. *
  5125. * cpu_power indicates the capacity of sched group, which is used while
  5126. * distributing the load between different sched groups in a sched domain.
  5127. * Typically cpu_power for all the groups in a sched domain will be same unless
  5128. * there are asymmetries in the topology. If there are asymmetries, group
  5129. * having more cpu_power will pickup more load compared to the group having
  5130. * less cpu_power.
  5131. *
  5132. * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
  5133. * the maximum number of tasks a group can handle in the presence of other idle
  5134. * or lightly loaded groups in the same sched domain.
  5135. */
  5136. static void init_sched_groups_power(int cpu, struct sched_domain *sd)
  5137. {
  5138. struct sched_domain *child;
  5139. struct sched_group *group;
  5140. WARN_ON(!sd || !sd->groups);
  5141. if (cpu != first_cpu(sd->groups->cpumask))
  5142. return;
  5143. child = sd->child;
  5144. sd->groups->__cpu_power = 0;
  5145. /*
  5146. * For perf policy, if the groups in child domain share resources
  5147. * (for example cores sharing some portions of the cache hierarchy
  5148. * or SMT), then set this domain groups cpu_power such that each group
  5149. * can handle only one task, when there are other idle groups in the
  5150. * same sched domain.
  5151. */
  5152. if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
  5153. (child->flags &
  5154. (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
  5155. sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
  5156. return;
  5157. }
  5158. /*
  5159. * add cpu_power of each child group to this groups cpu_power
  5160. */
  5161. group = child->groups;
  5162. do {
  5163. sg_inc_cpu_power(sd->groups, group->__cpu_power);
  5164. group = group->next;
  5165. } while (group != child->groups);
  5166. }
  5167. /*
  5168. * Build sched domains for a given set of cpus and attach the sched domains
  5169. * to the individual cpus
  5170. */
  5171. static int build_sched_domains(const cpumask_t *cpu_map)
  5172. {
  5173. int i;
  5174. #ifdef CONFIG_NUMA
  5175. struct sched_group **sched_group_nodes = NULL;
  5176. int sd_allnodes = 0;
  5177. /*
  5178. * Allocate the per-node list of sched groups
  5179. */
  5180. sched_group_nodes = kzalloc(sizeof(struct sched_group *)*MAX_NUMNODES,
  5181. GFP_KERNEL);
  5182. if (!sched_group_nodes) {
  5183. printk(KERN_WARNING "Can not alloc sched group node list\n");
  5184. return -ENOMEM;
  5185. }
  5186. sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
  5187. #endif
  5188. /*
  5189. * Set up domains for cpus specified by the cpu_map.
  5190. */
  5191. for_each_cpu_mask(i, *cpu_map) {
  5192. struct sched_domain *sd = NULL, *p;
  5193. cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
  5194. cpus_and(nodemask, nodemask, *cpu_map);
  5195. #ifdef CONFIG_NUMA
  5196. if (cpus_weight(*cpu_map) >
  5197. SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
  5198. sd = &per_cpu(allnodes_domains, i);
  5199. *sd = SD_ALLNODES_INIT;
  5200. sd->span = *cpu_map;
  5201. cpu_to_allnodes_group(i, cpu_map, &sd->groups);
  5202. p = sd;
  5203. sd_allnodes = 1;
  5204. } else
  5205. p = NULL;
  5206. sd = &per_cpu(node_domains, i);
  5207. *sd = SD_NODE_INIT;
  5208. sd->span = sched_domain_node_span(cpu_to_node(i));
  5209. sd->parent = p;
  5210. if (p)
  5211. p->child = sd;
  5212. cpus_and(sd->span, sd->span, *cpu_map);
  5213. #endif
  5214. p = sd;
  5215. sd = &per_cpu(phys_domains, i);
  5216. *sd = SD_CPU_INIT;
  5217. sd->span = nodemask;
  5218. sd->parent = p;
  5219. if (p)
  5220. p->child = sd;
  5221. cpu_to_phys_group(i, cpu_map, &sd->groups);
  5222. #ifdef CONFIG_SCHED_MC
  5223. p = sd;
  5224. sd = &per_cpu(core_domains, i);
  5225. *sd = SD_MC_INIT;
  5226. sd->span = cpu_coregroup_map(i);
  5227. cpus_and(sd->span, sd->span, *cpu_map);
  5228. sd->parent = p;
  5229. p->child = sd;
  5230. cpu_to_core_group(i, cpu_map, &sd->groups);
  5231. #endif
  5232. #ifdef CONFIG_SCHED_SMT
  5233. p = sd;
  5234. sd = &per_cpu(cpu_domains, i);
  5235. *sd = SD_SIBLING_INIT;
  5236. sd->span = cpu_sibling_map[i];
  5237. cpus_and(sd->span, sd->span, *cpu_map);
  5238. sd->parent = p;
  5239. p->child = sd;
  5240. cpu_to_cpu_group(i, cpu_map, &sd->groups);
  5241. #endif
  5242. }
  5243. #ifdef CONFIG_SCHED_SMT
  5244. /* Set up CPU (sibling) groups */
  5245. for_each_cpu_mask(i, *cpu_map) {
  5246. cpumask_t this_sibling_map = cpu_sibling_map[i];
  5247. cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
  5248. if (i != first_cpu(this_sibling_map))
  5249. continue;
  5250. init_sched_build_groups(this_sibling_map, cpu_map,
  5251. &cpu_to_cpu_group);
  5252. }
  5253. #endif
  5254. #ifdef CONFIG_SCHED_MC
  5255. /* Set up multi-core groups */
  5256. for_each_cpu_mask(i, *cpu_map) {
  5257. cpumask_t this_core_map = cpu_coregroup_map(i);
  5258. cpus_and(this_core_map, this_core_map, *cpu_map);
  5259. if (i != first_cpu(this_core_map))
  5260. continue;
  5261. init_sched_build_groups(this_core_map, cpu_map,
  5262. &cpu_to_core_group);
  5263. }
  5264. #endif
  5265. /* Set up physical groups */
  5266. for (i = 0; i < MAX_NUMNODES; i++) {
  5267. cpumask_t nodemask = node_to_cpumask(i);
  5268. cpus_and(nodemask, nodemask, *cpu_map);
  5269. if (cpus_empty(nodemask))
  5270. continue;
  5271. init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
  5272. }
  5273. #ifdef CONFIG_NUMA
  5274. /* Set up node groups */
  5275. if (sd_allnodes)
  5276. init_sched_build_groups(*cpu_map, cpu_map,
  5277. &cpu_to_allnodes_group);
  5278. for (i = 0; i < MAX_NUMNODES; i++) {
  5279. /* Set up node groups */
  5280. struct sched_group *sg, *prev;
  5281. cpumask_t nodemask = node_to_cpumask(i);
  5282. cpumask_t domainspan;
  5283. cpumask_t covered = CPU_MASK_NONE;
  5284. int j;
  5285. cpus_and(nodemask, nodemask, *cpu_map);
  5286. if (cpus_empty(nodemask)) {
  5287. sched_group_nodes[i] = NULL;
  5288. continue;
  5289. }
  5290. domainspan = sched_domain_node_span(i);
  5291. cpus_and(domainspan, domainspan, *cpu_map);
  5292. sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
  5293. if (!sg) {
  5294. printk(KERN_WARNING "Can not alloc domain group for "
  5295. "node %d\n", i);
  5296. goto error;
  5297. }
  5298. sched_group_nodes[i] = sg;
  5299. for_each_cpu_mask(j, nodemask) {
  5300. struct sched_domain *sd;
  5301. sd = &per_cpu(node_domains, j);
  5302. sd->groups = sg;
  5303. }
  5304. sg->__cpu_power = 0;
  5305. sg->cpumask = nodemask;
  5306. sg->next = sg;
  5307. cpus_or(covered, covered, nodemask);
  5308. prev = sg;
  5309. for (j = 0; j < MAX_NUMNODES; j++) {
  5310. cpumask_t tmp, notcovered;
  5311. int n = (i + j) % MAX_NUMNODES;
  5312. cpus_complement(notcovered, covered);
  5313. cpus_and(tmp, notcovered, *cpu_map);
  5314. cpus_and(tmp, tmp, domainspan);
  5315. if (cpus_empty(tmp))
  5316. break;
  5317. nodemask = node_to_cpumask(n);
  5318. cpus_and(tmp, tmp, nodemask);
  5319. if (cpus_empty(tmp))
  5320. continue;
  5321. sg = kmalloc_node(sizeof(struct sched_group),
  5322. GFP_KERNEL, i);
  5323. if (!sg) {
  5324. printk(KERN_WARNING
  5325. "Can not alloc domain group for node %d\n", j);
  5326. goto error;
  5327. }
  5328. sg->__cpu_power = 0;
  5329. sg->cpumask = tmp;
  5330. sg->next = prev->next;
  5331. cpus_or(covered, covered, tmp);
  5332. prev->next = sg;
  5333. prev = sg;
  5334. }
  5335. }
  5336. #endif
  5337. /* Calculate CPU power for physical packages and nodes */
  5338. #ifdef CONFIG_SCHED_SMT
  5339. for_each_cpu_mask(i, *cpu_map) {
  5340. struct sched_domain *sd = &per_cpu(cpu_domains, i);
  5341. init_sched_groups_power(i, sd);
  5342. }
  5343. #endif
  5344. #ifdef CONFIG_SCHED_MC
  5345. for_each_cpu_mask(i, *cpu_map) {
  5346. struct sched_domain *sd = &per_cpu(core_domains, i);
  5347. init_sched_groups_power(i, sd);
  5348. }
  5349. #endif
  5350. for_each_cpu_mask(i, *cpu_map) {
  5351. struct sched_domain *sd = &per_cpu(phys_domains, i);
  5352. init_sched_groups_power(i, sd);
  5353. }
  5354. #ifdef CONFIG_NUMA
  5355. for (i = 0; i < MAX_NUMNODES; i++)
  5356. init_numa_sched_groups_power(sched_group_nodes[i]);
  5357. if (sd_allnodes) {
  5358. struct sched_group *sg;
  5359. cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
  5360. init_numa_sched_groups_power(sg);
  5361. }
  5362. #endif
  5363. /* Attach the domains */
  5364. for_each_cpu_mask(i, *cpu_map) {
  5365. struct sched_domain *sd;
  5366. #ifdef CONFIG_SCHED_SMT
  5367. sd = &per_cpu(cpu_domains, i);
  5368. #elif defined(CONFIG_SCHED_MC)
  5369. sd = &per_cpu(core_domains, i);
  5370. #else
  5371. sd = &per_cpu(phys_domains, i);
  5372. #endif
  5373. cpu_attach_domain(sd, i);
  5374. }
  5375. return 0;
  5376. #ifdef CONFIG_NUMA
  5377. error:
  5378. free_sched_groups(cpu_map);
  5379. return -ENOMEM;
  5380. #endif
  5381. }
  5382. /*
  5383. * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  5384. */
  5385. static int arch_init_sched_domains(const cpumask_t *cpu_map)
  5386. {
  5387. cpumask_t cpu_default_map;
  5388. int err;
  5389. /*
  5390. * Setup mask for cpus without special case scheduling requirements.
  5391. * For now this just excludes isolated cpus, but could be used to
  5392. * exclude other special cases in the future.
  5393. */
  5394. cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map);
  5395. err = build_sched_domains(&cpu_default_map);
  5396. return err;
  5397. }
  5398. static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
  5399. {
  5400. free_sched_groups(cpu_map);
  5401. }
  5402. /*
  5403. * Detach sched domains from a group of cpus specified in cpu_map
  5404. * These cpus will now be attached to the NULL domain
  5405. */
  5406. static void detach_destroy_domains(const cpumask_t *cpu_map)
  5407. {
  5408. int i;
  5409. for_each_cpu_mask(i, *cpu_map)
  5410. cpu_attach_domain(NULL, i);
  5411. synchronize_sched();
  5412. arch_destroy_sched_domains(cpu_map);
  5413. }
  5414. /*
  5415. * Partition sched domains as specified by the cpumasks below.
  5416. * This attaches all cpus from the cpumasks to the NULL domain,
  5417. * waits for a RCU quiescent period, recalculates sched
  5418. * domain information and then attaches them back to the
  5419. * correct sched domains
  5420. * Call with hotplug lock held
  5421. */
  5422. int partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2)
  5423. {
  5424. cpumask_t change_map;
  5425. int err = 0;
  5426. cpus_and(*partition1, *partition1, cpu_online_map);
  5427. cpus_and(*partition2, *partition2, cpu_online_map);
  5428. cpus_or(change_map, *partition1, *partition2);
  5429. /* Detach sched domains from all of the affected cpus */
  5430. detach_destroy_domains(&change_map);
  5431. if (!cpus_empty(*partition1))
  5432. err = build_sched_domains(partition1);
  5433. if (!err && !cpus_empty(*partition2))
  5434. err = build_sched_domains(partition2);
  5435. return err;
  5436. }
  5437. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  5438. static int arch_reinit_sched_domains(void)
  5439. {
  5440. int err;
  5441. mutex_lock(&sched_hotcpu_mutex);
  5442. detach_destroy_domains(&cpu_online_map);
  5443. err = arch_init_sched_domains(&cpu_online_map);
  5444. mutex_unlock(&sched_hotcpu_mutex);
  5445. return err;
  5446. }
  5447. static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
  5448. {
  5449. int ret;
  5450. if (buf[0] != '0' && buf[0] != '1')
  5451. return -EINVAL;
  5452. if (smt)
  5453. sched_smt_power_savings = (buf[0] == '1');
  5454. else
  5455. sched_mc_power_savings = (buf[0] == '1');
  5456. ret = arch_reinit_sched_domains();
  5457. return ret ? ret : count;
  5458. }
  5459. #ifdef CONFIG_SCHED_MC
  5460. static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
  5461. {
  5462. return sprintf(page, "%u\n", sched_mc_power_savings);
  5463. }
  5464. static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
  5465. const char *buf, size_t count)
  5466. {
  5467. return sched_power_savings_store(buf, count, 0);
  5468. }
  5469. static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
  5470. sched_mc_power_savings_store);
  5471. #endif
  5472. #ifdef CONFIG_SCHED_SMT
  5473. static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
  5474. {
  5475. return sprintf(page, "%u\n", sched_smt_power_savings);
  5476. }
  5477. static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
  5478. const char *buf, size_t count)
  5479. {
  5480. return sched_power_savings_store(buf, count, 1);
  5481. }
  5482. static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
  5483. sched_smt_power_savings_store);
  5484. #endif
  5485. int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
  5486. {
  5487. int err = 0;
  5488. #ifdef CONFIG_SCHED_SMT
  5489. if (smt_capable())
  5490. err = sysfs_create_file(&cls->kset.kobj,
  5491. &attr_sched_smt_power_savings.attr);
  5492. #endif
  5493. #ifdef CONFIG_SCHED_MC
  5494. if (!err && mc_capable())
  5495. err = sysfs_create_file(&cls->kset.kobj,
  5496. &attr_sched_mc_power_savings.attr);
  5497. #endif
  5498. return err;
  5499. }
  5500. #endif
  5501. /*
  5502. * Force a reinitialization of the sched domains hierarchy. The domains
  5503. * and groups cannot be updated in place without racing with the balancing
  5504. * code, so we temporarily attach all running cpus to the NULL domain
  5505. * which will prevent rebalancing while the sched domains are recalculated.
  5506. */
  5507. static int update_sched_domains(struct notifier_block *nfb,
  5508. unsigned long action, void *hcpu)
  5509. {
  5510. switch (action) {
  5511. case CPU_UP_PREPARE:
  5512. case CPU_UP_PREPARE_FROZEN:
  5513. case CPU_DOWN_PREPARE:
  5514. case CPU_DOWN_PREPARE_FROZEN:
  5515. detach_destroy_domains(&cpu_online_map);
  5516. return NOTIFY_OK;
  5517. case CPU_UP_CANCELED:
  5518. case CPU_UP_CANCELED_FROZEN:
  5519. case CPU_DOWN_FAILED:
  5520. case CPU_DOWN_FAILED_FROZEN:
  5521. case CPU_ONLINE:
  5522. case CPU_ONLINE_FROZEN:
  5523. case CPU_DEAD:
  5524. case CPU_DEAD_FROZEN:
  5525. /*
  5526. * Fall through and re-initialise the domains.
  5527. */
  5528. break;
  5529. default:
  5530. return NOTIFY_DONE;
  5531. }
  5532. /* The hotplug lock is already held by cpu_up/cpu_down */
  5533. arch_init_sched_domains(&cpu_online_map);
  5534. return NOTIFY_OK;
  5535. }
  5536. void __init sched_init_smp(void)
  5537. {
  5538. cpumask_t non_isolated_cpus;
  5539. mutex_lock(&sched_hotcpu_mutex);
  5540. arch_init_sched_domains(&cpu_online_map);
  5541. cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
  5542. if (cpus_empty(non_isolated_cpus))
  5543. cpu_set(smp_processor_id(), non_isolated_cpus);
  5544. mutex_unlock(&sched_hotcpu_mutex);
  5545. /* XXX: Theoretical race here - CPU may be hotplugged now */
  5546. hotcpu_notifier(update_sched_domains, 0);
  5547. init_sched_domain_sysctl();
  5548. /* Move init over to a non-isolated CPU */
  5549. if (set_cpus_allowed(current, non_isolated_cpus) < 0)
  5550. BUG();
  5551. }
  5552. #else
  5553. void __init sched_init_smp(void)
  5554. {
  5555. }
  5556. #endif /* CONFIG_SMP */
  5557. int in_sched_functions(unsigned long addr)
  5558. {
  5559. /* Linker adds these: start and end of __sched functions */
  5560. extern char __sched_text_start[], __sched_text_end[];
  5561. return in_lock_functions(addr) ||
  5562. (addr >= (unsigned long)__sched_text_start
  5563. && addr < (unsigned long)__sched_text_end);
  5564. }
  5565. static inline void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
  5566. {
  5567. cfs_rq->tasks_timeline = RB_ROOT;
  5568. #ifdef CONFIG_FAIR_GROUP_SCHED
  5569. cfs_rq->rq = rq;
  5570. #endif
  5571. }
  5572. void __init sched_init(void)
  5573. {
  5574. int highest_cpu = 0;
  5575. int i, j;
  5576. /*
  5577. * Link up the scheduling class hierarchy:
  5578. */
  5579. rt_sched_class.next = &fair_sched_class;
  5580. fair_sched_class.next = &idle_sched_class;
  5581. idle_sched_class.next = NULL;
  5582. for_each_possible_cpu(i) {
  5583. struct rt_prio_array *array;
  5584. struct rq *rq;
  5585. rq = cpu_rq(i);
  5586. spin_lock_init(&rq->lock);
  5587. lockdep_set_class(&rq->lock, &rq->rq_lock_key);
  5588. rq->nr_running = 0;
  5589. rq->clock = 1;
  5590. init_cfs_rq(&rq->cfs, rq);
  5591. #ifdef CONFIG_FAIR_GROUP_SCHED
  5592. INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
  5593. {
  5594. struct cfs_rq *cfs_rq = &per_cpu(init_cfs_rq, i);
  5595. struct sched_entity *se =
  5596. &per_cpu(init_sched_entity, i);
  5597. init_cfs_rq_p[i] = cfs_rq;
  5598. init_cfs_rq(cfs_rq, rq);
  5599. cfs_rq->tg = &init_task_grp;
  5600. list_add(&cfs_rq->leaf_cfs_rq_list,
  5601. &rq->leaf_cfs_rq_list);
  5602. init_sched_entity_p[i] = se;
  5603. se->cfs_rq = &rq->cfs;
  5604. se->my_q = cfs_rq;
  5605. se->load.weight = NICE_0_LOAD;
  5606. se->load.inv_weight = div64_64(1ULL<<32, NICE_0_LOAD);
  5607. se->parent = NULL;
  5608. }
  5609. init_task_grp.shares = NICE_0_LOAD;
  5610. #endif
  5611. for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
  5612. rq->cpu_load[j] = 0;
  5613. #ifdef CONFIG_SMP
  5614. rq->sd = NULL;
  5615. rq->active_balance = 0;
  5616. rq->next_balance = jiffies;
  5617. rq->push_cpu = 0;
  5618. rq->cpu = i;
  5619. rq->migration_thread = NULL;
  5620. INIT_LIST_HEAD(&rq->migration_queue);
  5621. #endif
  5622. atomic_set(&rq->nr_iowait, 0);
  5623. array = &rq->rt.active;
  5624. for (j = 0; j < MAX_RT_PRIO; j++) {
  5625. INIT_LIST_HEAD(array->queue + j);
  5626. __clear_bit(j, array->bitmap);
  5627. }
  5628. highest_cpu = i;
  5629. /* delimiter for bitsearch: */
  5630. __set_bit(MAX_RT_PRIO, array->bitmap);
  5631. }
  5632. set_load_weight(&init_task);
  5633. #ifdef CONFIG_PREEMPT_NOTIFIERS
  5634. INIT_HLIST_HEAD(&init_task.preempt_notifiers);
  5635. #endif
  5636. #ifdef CONFIG_SMP
  5637. nr_cpu_ids = highest_cpu + 1;
  5638. open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
  5639. #endif
  5640. #ifdef CONFIG_RT_MUTEXES
  5641. plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
  5642. #endif
  5643. /*
  5644. * The boot idle thread does lazy MMU switching as well:
  5645. */
  5646. atomic_inc(&init_mm.mm_count);
  5647. enter_lazy_tlb(&init_mm, current);
  5648. /*
  5649. * Make us the idle thread. Technically, schedule() should not be
  5650. * called from this thread, however somewhere below it might be,
  5651. * but because we are the idle thread, we just pick up running again
  5652. * when this runqueue becomes "idle".
  5653. */
  5654. init_idle(current, smp_processor_id());
  5655. /*
  5656. * During early bootup we pretend to be a normal task:
  5657. */
  5658. current->sched_class = &fair_sched_class;
  5659. }
  5660. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  5661. void __might_sleep(char *file, int line)
  5662. {
  5663. #ifdef in_atomic
  5664. static unsigned long prev_jiffy; /* ratelimiting */
  5665. if ((in_atomic() || irqs_disabled()) &&
  5666. system_state == SYSTEM_RUNNING && !oops_in_progress) {
  5667. if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  5668. return;
  5669. prev_jiffy = jiffies;
  5670. printk(KERN_ERR "BUG: sleeping function called from invalid"
  5671. " context at %s:%d\n", file, line);
  5672. printk("in_atomic():%d, irqs_disabled():%d\n",
  5673. in_atomic(), irqs_disabled());
  5674. debug_show_held_locks(current);
  5675. if (irqs_disabled())
  5676. print_irqtrace_events(current);
  5677. dump_stack();
  5678. }
  5679. #endif
  5680. }
  5681. EXPORT_SYMBOL(__might_sleep);
  5682. #endif
  5683. #ifdef CONFIG_MAGIC_SYSRQ
  5684. void normalize_rt_tasks(void)
  5685. {
  5686. struct task_struct *g, *p;
  5687. unsigned long flags;
  5688. struct rq *rq;
  5689. int on_rq;
  5690. read_lock_irq(&tasklist_lock);
  5691. do_each_thread(g, p) {
  5692. p->se.exec_start = 0;
  5693. #ifdef CONFIG_SCHEDSTATS
  5694. p->se.wait_start = 0;
  5695. p->se.sleep_start = 0;
  5696. p->se.block_start = 0;
  5697. #endif
  5698. task_rq(p)->clock = 0;
  5699. if (!rt_task(p)) {
  5700. /*
  5701. * Renice negative nice level userspace
  5702. * tasks back to 0:
  5703. */
  5704. if (TASK_NICE(p) < 0 && p->mm)
  5705. set_user_nice(p, 0);
  5706. continue;
  5707. }
  5708. spin_lock_irqsave(&p->pi_lock, flags);
  5709. rq = __task_rq_lock(p);
  5710. #ifdef CONFIG_SMP
  5711. /*
  5712. * Do not touch the migration thread:
  5713. */
  5714. if (p == rq->migration_thread)
  5715. goto out_unlock;
  5716. #endif
  5717. update_rq_clock(rq);
  5718. on_rq = p->se.on_rq;
  5719. if (on_rq)
  5720. deactivate_task(rq, p, 0);
  5721. __setscheduler(rq, p, SCHED_NORMAL, 0);
  5722. if (on_rq) {
  5723. activate_task(rq, p, 0);
  5724. resched_task(rq->curr);
  5725. }
  5726. #ifdef CONFIG_SMP
  5727. out_unlock:
  5728. #endif
  5729. __task_rq_unlock(rq);
  5730. spin_unlock_irqrestore(&p->pi_lock, flags);
  5731. } while_each_thread(g, p);
  5732. read_unlock_irq(&tasklist_lock);
  5733. }
  5734. #endif /* CONFIG_MAGIC_SYSRQ */
  5735. #ifdef CONFIG_IA64
  5736. /*
  5737. * These functions are only useful for the IA64 MCA handling.
  5738. *
  5739. * They can only be called when the whole system has been
  5740. * stopped - every CPU needs to be quiescent, and no scheduling
  5741. * activity can take place. Using them for anything else would
  5742. * be a serious bug, and as a result, they aren't even visible
  5743. * under any other configuration.
  5744. */
  5745. /**
  5746. * curr_task - return the current task for a given cpu.
  5747. * @cpu: the processor in question.
  5748. *
  5749. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  5750. */
  5751. struct task_struct *curr_task(int cpu)
  5752. {
  5753. return cpu_curr(cpu);
  5754. }
  5755. /**
  5756. * set_curr_task - set the current task for a given cpu.
  5757. * @cpu: the processor in question.
  5758. * @p: the task pointer to set.
  5759. *
  5760. * Description: This function must only be used when non-maskable interrupts
  5761. * are serviced on a separate stack. It allows the architecture to switch the
  5762. * notion of the current task on a cpu in a non-blocking manner. This function
  5763. * must be called with all CPU's synchronized, and interrupts disabled, the
  5764. * and caller must save the original value of the current task (see
  5765. * curr_task() above) and restore that value before reenabling interrupts and
  5766. * re-starting the system.
  5767. *
  5768. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  5769. */
  5770. void set_curr_task(int cpu, struct task_struct *p)
  5771. {
  5772. cpu_curr(cpu) = p;
  5773. }
  5774. #endif
  5775. #ifdef CONFIG_FAIR_GROUP_SCHED
  5776. /* return corresponding task_grp object of a container */
  5777. static inline struct task_grp *container_tg(struct container *cont)
  5778. {
  5779. return container_of(container_subsys_state(cont, cpu_subsys_id),
  5780. struct task_grp, css);
  5781. }
  5782. /* allocate runqueue etc for a new task group */
  5783. static struct container_subsys_state *
  5784. sched_create_group(struct container_subsys *ss, struct container *cont)
  5785. {
  5786. struct task_grp *tg;
  5787. struct cfs_rq *cfs_rq;
  5788. struct sched_entity *se;
  5789. int i;
  5790. if (!cont->parent) {
  5791. /* This is early initialization for the top container */
  5792. init_task_grp.css.container = cont;
  5793. return &init_task_grp.css;
  5794. }
  5795. /* we support only 1-level deep hierarchical scheduler atm */
  5796. if (cont->parent->parent)
  5797. return ERR_PTR(-EINVAL);
  5798. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  5799. if (!tg)
  5800. return ERR_PTR(-ENOMEM);
  5801. tg->cfs_rq = kzalloc(sizeof(cfs_rq) * num_possible_cpus(), GFP_KERNEL);
  5802. if (!tg->cfs_rq)
  5803. goto err;
  5804. tg->se = kzalloc(sizeof(se) * num_possible_cpus(), GFP_KERNEL);
  5805. if (!tg->se)
  5806. goto err;
  5807. for_each_possible_cpu(i) {
  5808. struct rq *rq = cpu_rq(i);
  5809. cfs_rq = kmalloc_node(sizeof(struct cfs_rq), GFP_KERNEL,
  5810. cpu_to_node(i));
  5811. if (!cfs_rq)
  5812. goto err;
  5813. se = kmalloc_node(sizeof(struct sched_entity), GFP_KERNEL,
  5814. cpu_to_node(i));
  5815. if (!se)
  5816. goto err;
  5817. memset(cfs_rq, 0, sizeof(struct cfs_rq));
  5818. memset(se, 0, sizeof(struct sched_entity));
  5819. tg->cfs_rq[i] = cfs_rq;
  5820. init_cfs_rq(cfs_rq, rq);
  5821. cfs_rq->tg = tg;
  5822. list_add_rcu(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
  5823. tg->se[i] = se;
  5824. se->cfs_rq = &rq->cfs;
  5825. se->my_q = cfs_rq;
  5826. se->load.weight = NICE_0_LOAD;
  5827. se->load.inv_weight = div64_64(1ULL<<32, NICE_0_LOAD);
  5828. se->parent = NULL;
  5829. }
  5830. tg->shares = NICE_0_LOAD;
  5831. /* Bind the container to task_grp object we just created */
  5832. tg->css.container = cont;
  5833. return &tg->css;
  5834. err:
  5835. for_each_possible_cpu(i) {
  5836. if (tg->cfs_rq && tg->cfs_rq[i])
  5837. kfree(tg->cfs_rq[i]);
  5838. if (tg->se && tg->se[i])
  5839. kfree(tg->se[i]);
  5840. }
  5841. if (tg->cfs_rq)
  5842. kfree(tg->cfs_rq);
  5843. if (tg->se)
  5844. kfree(tg->se);
  5845. if (tg)
  5846. kfree(tg);
  5847. return ERR_PTR(-ENOMEM);
  5848. }
  5849. /* destroy runqueue etc associated with a task group */
  5850. static void sched_destroy_group(struct container_subsys *ss,
  5851. struct container *cont)
  5852. {
  5853. struct task_grp *tg = container_tg(cont);
  5854. struct cfs_rq *cfs_rq;
  5855. struct sched_entity *se;
  5856. int i;
  5857. for_each_possible_cpu(i) {
  5858. cfs_rq = tg->cfs_rq[i];
  5859. list_del_rcu(&cfs_rq->leaf_cfs_rq_list);
  5860. }
  5861. /* wait for possible concurrent references to cfs_rqs complete */
  5862. synchronize_sched();
  5863. /* now it should be safe to free those cfs_rqs */
  5864. for_each_possible_cpu(i) {
  5865. cfs_rq = tg->cfs_rq[i];
  5866. kfree(cfs_rq);
  5867. se = tg->se[i];
  5868. kfree(se);
  5869. }
  5870. kfree(tg->cfs_rq);
  5871. kfree(tg->se);
  5872. kfree(tg);
  5873. }
  5874. static int sched_can_attach(struct container_subsys *ss,
  5875. struct container *cont, struct task_struct *tsk)
  5876. {
  5877. /* We don't support RT-tasks being in separate groups */
  5878. if (tsk->sched_class != &fair_sched_class)
  5879. return -EINVAL;
  5880. return 0;
  5881. }
  5882. /* change task's runqueue when it moves between groups */
  5883. static void sched_move_task(struct container_subsys *ss, struct container *cont,
  5884. struct container *old_cont, struct task_struct *tsk)
  5885. {
  5886. int on_rq, running;
  5887. unsigned long flags;
  5888. struct rq *rq;
  5889. rq = task_rq_lock(tsk, &flags);
  5890. if (tsk->sched_class != &fair_sched_class)
  5891. goto done;
  5892. update_rq_clock(rq);
  5893. running = task_running(rq, tsk);
  5894. on_rq = tsk->se.on_rq;
  5895. if (on_rq) {
  5896. dequeue_task(rq, tsk, 0);
  5897. if (unlikely(running))
  5898. tsk->sched_class->put_prev_task(rq, tsk);
  5899. }
  5900. set_task_cfs_rq(tsk);
  5901. if (on_rq) {
  5902. if (unlikely(running))
  5903. tsk->sched_class->set_curr_task(rq);
  5904. enqueue_task(rq, tsk, 0);
  5905. }
  5906. done:
  5907. task_rq_unlock(rq, &flags);
  5908. }
  5909. static void set_se_shares(struct sched_entity *se, unsigned long shares)
  5910. {
  5911. struct cfs_rq *cfs_rq = se->cfs_rq;
  5912. struct rq *rq = cfs_rq->rq;
  5913. int on_rq;
  5914. spin_lock_irq(&rq->lock);
  5915. on_rq = se->on_rq;
  5916. if (on_rq)
  5917. dequeue_entity(cfs_rq, se, 0);
  5918. se->load.weight = shares;
  5919. se->load.inv_weight = div64_64((1ULL<<32), shares);
  5920. if (on_rq)
  5921. enqueue_entity(cfs_rq, se, 0);
  5922. spin_unlock_irq(&rq->lock);
  5923. }
  5924. static ssize_t cpu_shares_write(struct container *cont, struct cftype *cftype,
  5925. struct file *file, const char __user *userbuf,
  5926. size_t nbytes, loff_t *ppos)
  5927. {
  5928. int i;
  5929. unsigned long shareval;
  5930. struct task_grp *tg = container_tg(cont);
  5931. char buffer[2*sizeof(unsigned long) + 1];
  5932. if (nbytes > 2*sizeof(unsigned long)) /* safety check */
  5933. return -E2BIG;
  5934. if (copy_from_user(buffer, userbuf, nbytes))
  5935. return -EFAULT;
  5936. buffer[nbytes] = 0; /* nul-terminate */
  5937. shareval = simple_strtoul(buffer, NULL, 10);
  5938. tg->shares = shareval;
  5939. for_each_possible_cpu(i)
  5940. set_se_shares(tg->se[i], shareval);
  5941. return nbytes;
  5942. }
  5943. static u64 cpu_shares_read_uint(struct container *cont, struct cftype *cft)
  5944. {
  5945. struct task_grp *tg = container_tg(cont);
  5946. return (u64) tg->shares;
  5947. }
  5948. struct cftype cpuctl_share = {
  5949. .name = "shares",
  5950. .read_uint = cpu_shares_read_uint,
  5951. .write = cpu_shares_write,
  5952. };
  5953. static int sched_populate(struct container_subsys *ss, struct container *cont)
  5954. {
  5955. return container_add_file(cont, ss, &cpuctl_share);
  5956. }
  5957. struct container_subsys cpu_subsys = {
  5958. .name = "cpu",
  5959. .create = sched_create_group,
  5960. .destroy = sched_destroy_group,
  5961. .can_attach = sched_can_attach,
  5962. .attach = sched_move_task,
  5963. .populate = sched_populate,
  5964. .subsys_id = cpu_subsys_id,
  5965. .early_init = 1,
  5966. };
  5967. #endif /* CONFIG_FAIR_GROUP_SCHED */