sched.c 165 KB

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