sched.c 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561
  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/syscalls.h>
  56. #include <linux/times.h>
  57. #include <linux/tsacct_kern.h>
  58. #include <linux/kprobes.h>
  59. #include <linux/delayacct.h>
  60. #include <linux/reciprocal_div.h>
  61. #include <linux/unistd.h>
  62. #include <asm/tlb.h>
  63. /*
  64. * Scheduler clock - returns current time in nanosec units.
  65. * This is default implementation.
  66. * Architectures and sub-architectures can override this.
  67. */
  68. unsigned long long __attribute__((weak)) sched_clock(void)
  69. {
  70. return (unsigned long long)jiffies * (1000000000 / HZ);
  71. }
  72. /*
  73. * Convert user-nice values [ -20 ... 0 ... 19 ]
  74. * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  75. * and back.
  76. */
  77. #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  78. #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  79. #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  80. /*
  81. * 'User priority' is the nice value converted to something we
  82. * can work with better when scaling various scheduler parameters,
  83. * it's a [ 0 ... 39 ] range.
  84. */
  85. #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
  86. #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  87. #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  88. /*
  89. * Some helpers for converting nanosecond timing to jiffy resolution
  90. */
  91. #define NS_TO_JIFFIES(TIME) ((TIME) / (1000000000 / HZ))
  92. #define JIFFIES_TO_NS(TIME) ((TIME) * (1000000000 / HZ))
  93. #define NICE_0_LOAD SCHED_LOAD_SCALE
  94. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  95. /*
  96. * These are the 'tuning knobs' of the scheduler:
  97. *
  98. * Minimum timeslice is 5 msecs (or 1 jiffy, whichever is larger),
  99. * default timeslice is 100 msecs, maximum timeslice is 800 msecs.
  100. * Timeslices get refilled after they expire.
  101. */
  102. #define MIN_TIMESLICE max(5 * HZ / 1000, 1)
  103. #define DEF_TIMESLICE (100 * HZ / 1000)
  104. #ifdef CONFIG_SMP
  105. /*
  106. * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
  107. * Since cpu_power is a 'constant', we can use a reciprocal divide.
  108. */
  109. static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
  110. {
  111. return reciprocal_divide(load, sg->reciprocal_cpu_power);
  112. }
  113. /*
  114. * Each time a sched group cpu_power is changed,
  115. * we must compute its reciprocal value
  116. */
  117. static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
  118. {
  119. sg->__cpu_power += val;
  120. sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
  121. }
  122. #endif
  123. #define SCALE_PRIO(x, prio) \
  124. max(x * (MAX_PRIO - prio) / (MAX_USER_PRIO / 2), MIN_TIMESLICE)
  125. /*
  126. * static_prio_timeslice() scales user-nice values [ -20 ... 0 ... 19 ]
  127. * to time slice values: [800ms ... 100ms ... 5ms]
  128. */
  129. static unsigned int static_prio_timeslice(int static_prio)
  130. {
  131. if (static_prio == NICE_TO_PRIO(19))
  132. return 1;
  133. if (static_prio < NICE_TO_PRIO(0))
  134. return SCALE_PRIO(DEF_TIMESLICE * 4, static_prio);
  135. else
  136. return SCALE_PRIO(DEF_TIMESLICE, static_prio);
  137. }
  138. static inline int rt_policy(int policy)
  139. {
  140. if (unlikely(policy == SCHED_FIFO) || unlikely(policy == SCHED_RR))
  141. return 1;
  142. return 0;
  143. }
  144. static inline int task_has_rt_policy(struct task_struct *p)
  145. {
  146. return rt_policy(p->policy);
  147. }
  148. /*
  149. * This is the priority-queue data structure of the RT scheduling class:
  150. */
  151. struct rt_prio_array {
  152. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  153. struct list_head queue[MAX_RT_PRIO];
  154. };
  155. struct load_stat {
  156. struct load_weight load;
  157. u64 load_update_start, load_update_last;
  158. unsigned long delta_fair, delta_exec, delta_stat;
  159. };
  160. /* CFS-related fields in a runqueue */
  161. struct cfs_rq {
  162. struct load_weight load;
  163. unsigned long nr_running;
  164. s64 fair_clock;
  165. u64 exec_clock;
  166. s64 wait_runtime;
  167. u64 sleeper_bonus;
  168. unsigned long wait_runtime_overruns, wait_runtime_underruns;
  169. struct rb_root tasks_timeline;
  170. struct rb_node *rb_leftmost;
  171. struct rb_node *rb_load_balance_curr;
  172. #ifdef CONFIG_FAIR_GROUP_SCHED
  173. /* 'curr' points to currently running entity on this cfs_rq.
  174. * It is set to NULL otherwise (i.e when none are currently running).
  175. */
  176. struct sched_entity *curr;
  177. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  178. /* leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  179. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  180. * (like users, containers etc.)
  181. *
  182. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  183. * list is used during load balance.
  184. */
  185. struct list_head leaf_cfs_rq_list; /* Better name : task_cfs_rq_list? */
  186. #endif
  187. };
  188. /* Real-Time classes' related field in a runqueue: */
  189. struct rt_rq {
  190. struct rt_prio_array active;
  191. int rt_load_balance_idx;
  192. struct list_head *rt_load_balance_head, *rt_load_balance_curr;
  193. };
  194. /*
  195. * This is the main, per-CPU runqueue data structure.
  196. *
  197. * Locking rule: those places that want to lock multiple runqueues
  198. * (such as the load balancing or the thread migration code), lock
  199. * acquire operations must be ordered by ascending &runqueue.
  200. */
  201. struct rq {
  202. spinlock_t lock; /* runqueue lock */
  203. /*
  204. * nr_running and cpu_load should be in the same cacheline because
  205. * remote CPUs use both these fields when doing load calculation.
  206. */
  207. unsigned long nr_running;
  208. #define CPU_LOAD_IDX_MAX 5
  209. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  210. unsigned char idle_at_tick;
  211. #ifdef CONFIG_NO_HZ
  212. unsigned char in_nohz_recently;
  213. #endif
  214. struct load_stat ls; /* capture load from *all* tasks on this cpu */
  215. unsigned long nr_load_updates;
  216. u64 nr_switches;
  217. struct cfs_rq cfs;
  218. #ifdef CONFIG_FAIR_GROUP_SCHED
  219. struct list_head leaf_cfs_rq_list; /* list of leaf cfs_rq on this cpu */
  220. #endif
  221. struct rt_rq rt;
  222. /*
  223. * This is part of a global counter where only the total sum
  224. * over all CPUs matters. A task can increase this counter on
  225. * one CPU and if it got migrated afterwards it may decrease
  226. * it on another CPU. Always updated under the runqueue lock:
  227. */
  228. unsigned long nr_uninterruptible;
  229. struct task_struct *curr, *idle;
  230. unsigned long next_balance;
  231. struct mm_struct *prev_mm;
  232. u64 clock, prev_clock_raw;
  233. s64 clock_max_delta;
  234. unsigned int clock_warps, clock_overflows;
  235. unsigned int clock_unstable_events;
  236. atomic_t nr_iowait;
  237. #ifdef CONFIG_SMP
  238. struct sched_domain *sd;
  239. /* For active balancing */
  240. int active_balance;
  241. int push_cpu;
  242. int cpu; /* cpu of this runqueue */
  243. struct task_struct *migration_thread;
  244. struct list_head migration_queue;
  245. #endif
  246. #ifdef CONFIG_SCHEDSTATS
  247. /* latency stats */
  248. struct sched_info rq_sched_info;
  249. /* sys_sched_yield() stats */
  250. unsigned long yld_exp_empty;
  251. unsigned long yld_act_empty;
  252. unsigned long yld_both_empty;
  253. unsigned long yld_cnt;
  254. /* schedule() stats */
  255. unsigned long sched_switch;
  256. unsigned long sched_cnt;
  257. unsigned long sched_goidle;
  258. /* try_to_wake_up() stats */
  259. unsigned long ttwu_cnt;
  260. unsigned long ttwu_local;
  261. #endif
  262. struct lock_class_key rq_lock_key;
  263. };
  264. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  265. static DEFINE_MUTEX(sched_hotcpu_mutex);
  266. static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
  267. {
  268. rq->curr->sched_class->check_preempt_curr(rq, p);
  269. }
  270. static inline int cpu_of(struct rq *rq)
  271. {
  272. #ifdef CONFIG_SMP
  273. return rq->cpu;
  274. #else
  275. return 0;
  276. #endif
  277. }
  278. /*
  279. * Per-runqueue clock, as finegrained as the platform can give us:
  280. */
  281. static unsigned long long __rq_clock(struct rq *rq)
  282. {
  283. u64 prev_raw = rq->prev_clock_raw;
  284. u64 now = sched_clock();
  285. s64 delta = now - prev_raw;
  286. u64 clock = rq->clock;
  287. /*
  288. * Protect against sched_clock() occasionally going backwards:
  289. */
  290. if (unlikely(delta < 0)) {
  291. clock++;
  292. rq->clock_warps++;
  293. } else {
  294. /*
  295. * Catch too large forward jumps too:
  296. */
  297. if (unlikely(delta > 2*TICK_NSEC)) {
  298. clock++;
  299. rq->clock_overflows++;
  300. } else {
  301. if (unlikely(delta > rq->clock_max_delta))
  302. rq->clock_max_delta = delta;
  303. clock += delta;
  304. }
  305. }
  306. rq->prev_clock_raw = now;
  307. rq->clock = clock;
  308. return clock;
  309. }
  310. static inline unsigned long long rq_clock(struct rq *rq)
  311. {
  312. int this_cpu = smp_processor_id();
  313. if (this_cpu == cpu_of(rq))
  314. return __rq_clock(rq);
  315. return rq->clock;
  316. }
  317. /*
  318. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  319. * See detach_destroy_domains: synchronize_sched for details.
  320. *
  321. * The domain tree of any CPU may only be accessed from within
  322. * preempt-disabled sections.
  323. */
  324. #define for_each_domain(cpu, __sd) \
  325. for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
  326. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  327. #define this_rq() (&__get_cpu_var(runqueues))
  328. #define task_rq(p) cpu_rq(task_cpu(p))
  329. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  330. /*
  331. * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
  332. * clock constructed from sched_clock():
  333. */
  334. unsigned long long cpu_clock(int cpu)
  335. {
  336. struct rq *rq = cpu_rq(cpu);
  337. unsigned long long now;
  338. unsigned long flags;
  339. spin_lock_irqsave(&rq->lock, flags);
  340. now = rq_clock(rq);
  341. spin_unlock_irqrestore(&rq->lock, 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
  1454. * and rescheduled
  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. *
  1464. * This is safe to call from within a preemption notifier.
  1465. */
  1466. void preempt_notifier_unregister(struct preempt_notifier *notifier)
  1467. {
  1468. hlist_del(&notifier->link);
  1469. }
  1470. EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
  1471. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1472. {
  1473. struct preempt_notifier *notifier;
  1474. struct hlist_node *node;
  1475. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  1476. notifier->ops->sched_in(notifier, raw_smp_processor_id());
  1477. }
  1478. static void
  1479. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1480. struct task_struct *next)
  1481. {
  1482. struct preempt_notifier *notifier;
  1483. struct hlist_node *node;
  1484. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  1485. notifier->ops->sched_out(notifier, next);
  1486. }
  1487. #else
  1488. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1489. {
  1490. }
  1491. static void
  1492. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1493. struct task_struct *next)
  1494. {
  1495. }
  1496. #endif
  1497. /**
  1498. * prepare_task_switch - prepare to switch tasks
  1499. * @rq: the runqueue preparing to switch
  1500. * @next: the task we are going to switch to.
  1501. *
  1502. * This is called with the rq lock held and interrupts off. It must
  1503. * be paired with a subsequent finish_task_switch after the context
  1504. * switch.
  1505. *
  1506. * prepare_task_switch sets up locking and calls architecture specific
  1507. * hooks.
  1508. */
  1509. static inline void
  1510. prepare_task_switch(struct rq *rq, struct task_struct *prev,
  1511. struct task_struct *next)
  1512. {
  1513. fire_sched_out_preempt_notifiers(prev, next);
  1514. prepare_lock_switch(rq, next);
  1515. prepare_arch_switch(next);
  1516. }
  1517. /**
  1518. * finish_task_switch - clean up after a task-switch
  1519. * @rq: runqueue associated with task-switch
  1520. * @prev: the thread we just switched away from.
  1521. *
  1522. * finish_task_switch must be called after the context switch, paired
  1523. * with a prepare_task_switch call before the context switch.
  1524. * finish_task_switch will reconcile locking set up by prepare_task_switch,
  1525. * and do any other architecture-specific cleanup actions.
  1526. *
  1527. * Note that we may have delayed dropping an mm in context_switch(). If
  1528. * so, we finish that here outside of the runqueue lock. (Doing it
  1529. * with the lock held can cause deadlocks; see schedule() for
  1530. * details.)
  1531. */
  1532. static inline void finish_task_switch(struct rq *rq, struct task_struct *prev)
  1533. __releases(rq->lock)
  1534. {
  1535. struct mm_struct *mm = rq->prev_mm;
  1536. long prev_state;
  1537. rq->prev_mm = NULL;
  1538. /*
  1539. * A task struct has one reference for the use as "current".
  1540. * If a task dies, then it sets TASK_DEAD in tsk->state and calls
  1541. * schedule one last time. The schedule call will never return, and
  1542. * the scheduled task must drop that reference.
  1543. * The test for TASK_DEAD must occur while the runqueue locks are
  1544. * still held, otherwise prev could be scheduled on another cpu, die
  1545. * there before we look at prev->state, and then the reference would
  1546. * be dropped twice.
  1547. * Manfred Spraul <manfred@colorfullife.com>
  1548. */
  1549. prev_state = prev->state;
  1550. finish_arch_switch(prev);
  1551. finish_lock_switch(rq, prev);
  1552. fire_sched_in_preempt_notifiers(current);
  1553. if (mm)
  1554. mmdrop(mm);
  1555. if (unlikely(prev_state == TASK_DEAD)) {
  1556. /*
  1557. * Remove function-return probe instances associated with this
  1558. * task and put them back on the free list.
  1559. */
  1560. kprobe_flush_task(prev);
  1561. put_task_struct(prev);
  1562. }
  1563. }
  1564. /**
  1565. * schedule_tail - first thing a freshly forked thread must call.
  1566. * @prev: the thread we just switched away from.
  1567. */
  1568. asmlinkage void schedule_tail(struct task_struct *prev)
  1569. __releases(rq->lock)
  1570. {
  1571. struct rq *rq = this_rq();
  1572. finish_task_switch(rq, prev);
  1573. #ifdef __ARCH_WANT_UNLOCKED_CTXSW
  1574. /* In this case, finish_task_switch does not reenable preemption */
  1575. preempt_enable();
  1576. #endif
  1577. if (current->set_child_tid)
  1578. put_user(current->pid, current->set_child_tid);
  1579. }
  1580. /*
  1581. * context_switch - switch to the new MM and the new
  1582. * thread's register state.
  1583. */
  1584. static inline void
  1585. context_switch(struct rq *rq, struct task_struct *prev,
  1586. struct task_struct *next)
  1587. {
  1588. struct mm_struct *mm, *oldmm;
  1589. prepare_task_switch(rq, prev, next);
  1590. mm = next->mm;
  1591. oldmm = prev->active_mm;
  1592. /*
  1593. * For paravirt, this is coupled with an exit in switch_to to
  1594. * combine the page table reload and the switch backend into
  1595. * one hypercall.
  1596. */
  1597. arch_enter_lazy_cpu_mode();
  1598. if (unlikely(!mm)) {
  1599. next->active_mm = oldmm;
  1600. atomic_inc(&oldmm->mm_count);
  1601. enter_lazy_tlb(oldmm, next);
  1602. } else
  1603. switch_mm(oldmm, mm, next);
  1604. if (unlikely(!prev->mm)) {
  1605. prev->active_mm = NULL;
  1606. rq->prev_mm = oldmm;
  1607. }
  1608. /*
  1609. * Since the runqueue lock will be released by the next
  1610. * task (which is an invalid locking op but in the case
  1611. * of the scheduler it's an obvious special-case), so we
  1612. * do an early lockdep release here:
  1613. */
  1614. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  1615. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  1616. #endif
  1617. /* Here we just switch the register state and the stack. */
  1618. switch_to(prev, next, prev);
  1619. barrier();
  1620. /*
  1621. * this_rq must be evaluated again because prev may have moved
  1622. * CPUs since it called schedule(), thus the 'rq' on its stack
  1623. * frame will be invalid.
  1624. */
  1625. finish_task_switch(this_rq(), prev);
  1626. }
  1627. /*
  1628. * nr_running, nr_uninterruptible and nr_context_switches:
  1629. *
  1630. * externally visible scheduler statistics: current number of runnable
  1631. * threads, current number of uninterruptible-sleeping threads, total
  1632. * number of context switches performed since bootup.
  1633. */
  1634. unsigned long nr_running(void)
  1635. {
  1636. unsigned long i, sum = 0;
  1637. for_each_online_cpu(i)
  1638. sum += cpu_rq(i)->nr_running;
  1639. return sum;
  1640. }
  1641. unsigned long nr_uninterruptible(void)
  1642. {
  1643. unsigned long i, sum = 0;
  1644. for_each_possible_cpu(i)
  1645. sum += cpu_rq(i)->nr_uninterruptible;
  1646. /*
  1647. * Since we read the counters lockless, it might be slightly
  1648. * inaccurate. Do not allow it to go below zero though:
  1649. */
  1650. if (unlikely((long)sum < 0))
  1651. sum = 0;
  1652. return sum;
  1653. }
  1654. unsigned long long nr_context_switches(void)
  1655. {
  1656. int i;
  1657. unsigned long long sum = 0;
  1658. for_each_possible_cpu(i)
  1659. sum += cpu_rq(i)->nr_switches;
  1660. return sum;
  1661. }
  1662. unsigned long nr_iowait(void)
  1663. {
  1664. unsigned long i, sum = 0;
  1665. for_each_possible_cpu(i)
  1666. sum += atomic_read(&cpu_rq(i)->nr_iowait);
  1667. return sum;
  1668. }
  1669. unsigned long nr_active(void)
  1670. {
  1671. unsigned long i, running = 0, uninterruptible = 0;
  1672. for_each_online_cpu(i) {
  1673. running += cpu_rq(i)->nr_running;
  1674. uninterruptible += cpu_rq(i)->nr_uninterruptible;
  1675. }
  1676. if (unlikely((long)uninterruptible < 0))
  1677. uninterruptible = 0;
  1678. return running + uninterruptible;
  1679. }
  1680. /*
  1681. * Update rq->cpu_load[] statistics. This function is usually called every
  1682. * scheduler tick (TICK_NSEC).
  1683. */
  1684. static void update_cpu_load(struct rq *this_rq)
  1685. {
  1686. u64 fair_delta64, exec_delta64, idle_delta64, sample_interval64, tmp64;
  1687. unsigned long total_load = this_rq->ls.load.weight;
  1688. unsigned long this_load = total_load;
  1689. struct load_stat *ls = &this_rq->ls;
  1690. u64 now = __rq_clock(this_rq);
  1691. int i, scale;
  1692. this_rq->nr_load_updates++;
  1693. if (unlikely(!(sysctl_sched_features & SCHED_FEAT_PRECISE_CPU_LOAD)))
  1694. goto do_avg;
  1695. /* Update delta_fair/delta_exec fields first */
  1696. update_curr_load(this_rq, now);
  1697. fair_delta64 = ls->delta_fair + 1;
  1698. ls->delta_fair = 0;
  1699. exec_delta64 = ls->delta_exec + 1;
  1700. ls->delta_exec = 0;
  1701. sample_interval64 = now - ls->load_update_last;
  1702. ls->load_update_last = now;
  1703. if ((s64)sample_interval64 < (s64)TICK_NSEC)
  1704. sample_interval64 = TICK_NSEC;
  1705. if (exec_delta64 > sample_interval64)
  1706. exec_delta64 = sample_interval64;
  1707. idle_delta64 = sample_interval64 - exec_delta64;
  1708. tmp64 = div64_64(SCHED_LOAD_SCALE * exec_delta64, fair_delta64);
  1709. tmp64 = div64_64(tmp64 * exec_delta64, sample_interval64);
  1710. this_load = (unsigned long)tmp64;
  1711. do_avg:
  1712. /* Update our load: */
  1713. for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
  1714. unsigned long old_load, new_load;
  1715. /* scale is effectively 1 << i now, and >> i divides by scale */
  1716. old_load = this_rq->cpu_load[i];
  1717. new_load = this_load;
  1718. this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
  1719. }
  1720. }
  1721. #ifdef CONFIG_SMP
  1722. /*
  1723. * double_rq_lock - safely lock two runqueues
  1724. *
  1725. * Note this does not disable interrupts like task_rq_lock,
  1726. * you need to do so manually before calling.
  1727. */
  1728. static void double_rq_lock(struct rq *rq1, struct rq *rq2)
  1729. __acquires(rq1->lock)
  1730. __acquires(rq2->lock)
  1731. {
  1732. BUG_ON(!irqs_disabled());
  1733. if (rq1 == rq2) {
  1734. spin_lock(&rq1->lock);
  1735. __acquire(rq2->lock); /* Fake it out ;) */
  1736. } else {
  1737. if (rq1 < rq2) {
  1738. spin_lock(&rq1->lock);
  1739. spin_lock(&rq2->lock);
  1740. } else {
  1741. spin_lock(&rq2->lock);
  1742. spin_lock(&rq1->lock);
  1743. }
  1744. }
  1745. }
  1746. /*
  1747. * double_rq_unlock - safely unlock two runqueues
  1748. *
  1749. * Note this does not restore interrupts like task_rq_unlock,
  1750. * you need to do so manually after calling.
  1751. */
  1752. static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  1753. __releases(rq1->lock)
  1754. __releases(rq2->lock)
  1755. {
  1756. spin_unlock(&rq1->lock);
  1757. if (rq1 != rq2)
  1758. spin_unlock(&rq2->lock);
  1759. else
  1760. __release(rq2->lock);
  1761. }
  1762. /*
  1763. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1764. */
  1765. static void double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1766. __releases(this_rq->lock)
  1767. __acquires(busiest->lock)
  1768. __acquires(this_rq->lock)
  1769. {
  1770. if (unlikely(!irqs_disabled())) {
  1771. /* printk() doesn't work good under rq->lock */
  1772. spin_unlock(&this_rq->lock);
  1773. BUG_ON(1);
  1774. }
  1775. if (unlikely(!spin_trylock(&busiest->lock))) {
  1776. if (busiest < this_rq) {
  1777. spin_unlock(&this_rq->lock);
  1778. spin_lock(&busiest->lock);
  1779. spin_lock(&this_rq->lock);
  1780. } else
  1781. spin_lock(&busiest->lock);
  1782. }
  1783. }
  1784. /*
  1785. * If dest_cpu is allowed for this process, migrate the task to it.
  1786. * This is accomplished by forcing the cpu_allowed mask to only
  1787. * allow dest_cpu, which will force the cpu onto dest_cpu. Then
  1788. * the cpu_allowed mask is restored.
  1789. */
  1790. static void sched_migrate_task(struct task_struct *p, int dest_cpu)
  1791. {
  1792. struct migration_req req;
  1793. unsigned long flags;
  1794. struct rq *rq;
  1795. rq = task_rq_lock(p, &flags);
  1796. if (!cpu_isset(dest_cpu, p->cpus_allowed)
  1797. || unlikely(cpu_is_offline(dest_cpu)))
  1798. goto out;
  1799. /* force the process onto the specified CPU */
  1800. if (migrate_task(p, dest_cpu, &req)) {
  1801. /* Need to wait for migration thread (might exit: take ref). */
  1802. struct task_struct *mt = rq->migration_thread;
  1803. get_task_struct(mt);
  1804. task_rq_unlock(rq, &flags);
  1805. wake_up_process(mt);
  1806. put_task_struct(mt);
  1807. wait_for_completion(&req.done);
  1808. return;
  1809. }
  1810. out:
  1811. task_rq_unlock(rq, &flags);
  1812. }
  1813. /*
  1814. * sched_exec - execve() is a valuable balancing opportunity, because at
  1815. * this point the task has the smallest effective memory and cache footprint.
  1816. */
  1817. void sched_exec(void)
  1818. {
  1819. int new_cpu, this_cpu = get_cpu();
  1820. new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
  1821. put_cpu();
  1822. if (new_cpu != this_cpu)
  1823. sched_migrate_task(current, new_cpu);
  1824. }
  1825. /*
  1826. * pull_task - move a task from a remote runqueue to the local runqueue.
  1827. * Both runqueues must be locked.
  1828. */
  1829. static void pull_task(struct rq *src_rq, struct task_struct *p,
  1830. struct rq *this_rq, int this_cpu)
  1831. {
  1832. deactivate_task(src_rq, p, 0);
  1833. set_task_cpu(p, this_cpu);
  1834. activate_task(this_rq, p, 0);
  1835. /*
  1836. * Note that idle threads have a prio of MAX_PRIO, for this test
  1837. * to be always true for them.
  1838. */
  1839. check_preempt_curr(this_rq, p);
  1840. }
  1841. /*
  1842. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  1843. */
  1844. static
  1845. int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
  1846. struct sched_domain *sd, enum cpu_idle_type idle,
  1847. int *all_pinned)
  1848. {
  1849. /*
  1850. * We do not migrate tasks that are:
  1851. * 1) running (obviously), or
  1852. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  1853. * 3) are cache-hot on their current CPU.
  1854. */
  1855. if (!cpu_isset(this_cpu, p->cpus_allowed))
  1856. return 0;
  1857. *all_pinned = 0;
  1858. if (task_running(rq, p))
  1859. return 0;
  1860. /*
  1861. * Aggressive migration if too many balance attempts have failed:
  1862. */
  1863. if (sd->nr_balance_failed > sd->cache_nice_tries)
  1864. return 1;
  1865. return 1;
  1866. }
  1867. static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1868. unsigned long max_nr_move, unsigned long max_load_move,
  1869. struct sched_domain *sd, enum cpu_idle_type idle,
  1870. int *all_pinned, unsigned long *load_moved,
  1871. int this_best_prio, int best_prio, int best_prio_seen,
  1872. struct rq_iterator *iterator)
  1873. {
  1874. int pulled = 0, pinned = 0, skip_for_load;
  1875. struct task_struct *p;
  1876. long rem_load_move = max_load_move;
  1877. if (max_nr_move == 0 || max_load_move == 0)
  1878. goto out;
  1879. pinned = 1;
  1880. /*
  1881. * Start the load-balancing iterator:
  1882. */
  1883. p = iterator->start(iterator->arg);
  1884. next:
  1885. if (!p)
  1886. goto out;
  1887. /*
  1888. * To help distribute high priority tasks accross CPUs we don't
  1889. * skip a task if it will be the highest priority task (i.e. smallest
  1890. * prio value) on its new queue regardless of its load weight
  1891. */
  1892. skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
  1893. SCHED_LOAD_SCALE_FUZZ;
  1894. if (skip_for_load && p->prio < this_best_prio)
  1895. skip_for_load = !best_prio_seen && p->prio == best_prio;
  1896. if (skip_for_load ||
  1897. !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
  1898. best_prio_seen |= p->prio == best_prio;
  1899. p = iterator->next(iterator->arg);
  1900. goto next;
  1901. }
  1902. pull_task(busiest, p, this_rq, this_cpu);
  1903. pulled++;
  1904. rem_load_move -= p->se.load.weight;
  1905. /*
  1906. * We only want to steal up to the prescribed number of tasks
  1907. * and the prescribed amount of weighted load.
  1908. */
  1909. if (pulled < max_nr_move && rem_load_move > 0) {
  1910. if (p->prio < this_best_prio)
  1911. this_best_prio = p->prio;
  1912. p = iterator->next(iterator->arg);
  1913. goto next;
  1914. }
  1915. out:
  1916. /*
  1917. * Right now, this is the only place pull_task() is called,
  1918. * so we can safely collect pull_task() stats here rather than
  1919. * inside pull_task().
  1920. */
  1921. schedstat_add(sd, lb_gained[idle], pulled);
  1922. if (all_pinned)
  1923. *all_pinned = pinned;
  1924. *load_moved = max_load_move - rem_load_move;
  1925. return pulled;
  1926. }
  1927. /*
  1928. * move_tasks tries to move up to max_nr_move tasks and max_load_move weighted
  1929. * load from busiest to this_rq, as part of a balancing operation within
  1930. * "domain". Returns the number of tasks moved.
  1931. *
  1932. * Called with both runqueues locked.
  1933. */
  1934. static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1935. unsigned long max_nr_move, unsigned long max_load_move,
  1936. struct sched_domain *sd, enum cpu_idle_type idle,
  1937. int *all_pinned)
  1938. {
  1939. struct sched_class *class = sched_class_highest;
  1940. unsigned long load_moved, total_nr_moved = 0, nr_moved;
  1941. long rem_load_move = max_load_move;
  1942. do {
  1943. nr_moved = class->load_balance(this_rq, this_cpu, busiest,
  1944. max_nr_move, (unsigned long)rem_load_move,
  1945. sd, idle, all_pinned, &load_moved);
  1946. total_nr_moved += nr_moved;
  1947. max_nr_move -= nr_moved;
  1948. rem_load_move -= load_moved;
  1949. class = class->next;
  1950. } while (class && max_nr_move && rem_load_move > 0);
  1951. return total_nr_moved;
  1952. }
  1953. /*
  1954. * find_busiest_group finds and returns the busiest CPU group within the
  1955. * domain. It calculates and returns the amount of weighted load which
  1956. * should be moved to restore balance via the imbalance parameter.
  1957. */
  1958. static struct sched_group *
  1959. find_busiest_group(struct sched_domain *sd, int this_cpu,
  1960. unsigned long *imbalance, enum cpu_idle_type idle,
  1961. int *sd_idle, cpumask_t *cpus, int *balance)
  1962. {
  1963. struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
  1964. unsigned long max_load, avg_load, total_load, this_load, total_pwr;
  1965. unsigned long max_pull;
  1966. unsigned long busiest_load_per_task, busiest_nr_running;
  1967. unsigned long this_load_per_task, this_nr_running;
  1968. int load_idx;
  1969. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  1970. int power_savings_balance = 1;
  1971. unsigned long leader_nr_running = 0, min_load_per_task = 0;
  1972. unsigned long min_nr_running = ULONG_MAX;
  1973. struct sched_group *group_min = NULL, *group_leader = NULL;
  1974. #endif
  1975. max_load = this_load = total_load = total_pwr = 0;
  1976. busiest_load_per_task = busiest_nr_running = 0;
  1977. this_load_per_task = this_nr_running = 0;
  1978. if (idle == CPU_NOT_IDLE)
  1979. load_idx = sd->busy_idx;
  1980. else if (idle == CPU_NEWLY_IDLE)
  1981. load_idx = sd->newidle_idx;
  1982. else
  1983. load_idx = sd->idle_idx;
  1984. do {
  1985. unsigned long load, group_capacity;
  1986. int local_group;
  1987. int i;
  1988. unsigned int balance_cpu = -1, first_idle_cpu = 0;
  1989. unsigned long sum_nr_running, sum_weighted_load;
  1990. local_group = cpu_isset(this_cpu, group->cpumask);
  1991. if (local_group)
  1992. balance_cpu = first_cpu(group->cpumask);
  1993. /* Tally up the load of all CPUs in the group */
  1994. sum_weighted_load = sum_nr_running = avg_load = 0;
  1995. for_each_cpu_mask(i, group->cpumask) {
  1996. struct rq *rq;
  1997. if (!cpu_isset(i, *cpus))
  1998. continue;
  1999. rq = cpu_rq(i);
  2000. if (*sd_idle && rq->nr_running)
  2001. *sd_idle = 0;
  2002. /* Bias balancing toward cpus of our domain */
  2003. if (local_group) {
  2004. if (idle_cpu(i) && !first_idle_cpu) {
  2005. first_idle_cpu = 1;
  2006. balance_cpu = i;
  2007. }
  2008. load = target_load(i, load_idx);
  2009. } else
  2010. load = source_load(i, load_idx);
  2011. avg_load += load;
  2012. sum_nr_running += rq->nr_running;
  2013. sum_weighted_load += weighted_cpuload(i);
  2014. }
  2015. /*
  2016. * First idle cpu or the first cpu(busiest) in this sched group
  2017. * is eligible for doing load balancing at this and above
  2018. * domains. In the newly idle case, we will allow all the cpu's
  2019. * to do the newly idle load balance.
  2020. */
  2021. if (idle != CPU_NEWLY_IDLE && local_group &&
  2022. balance_cpu != this_cpu && balance) {
  2023. *balance = 0;
  2024. goto ret;
  2025. }
  2026. total_load += avg_load;
  2027. total_pwr += group->__cpu_power;
  2028. /* Adjust by relative CPU power of the group */
  2029. avg_load = sg_div_cpu_power(group,
  2030. avg_load * SCHED_LOAD_SCALE);
  2031. group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
  2032. if (local_group) {
  2033. this_load = avg_load;
  2034. this = group;
  2035. this_nr_running = sum_nr_running;
  2036. this_load_per_task = sum_weighted_load;
  2037. } else if (avg_load > max_load &&
  2038. sum_nr_running > group_capacity) {
  2039. max_load = avg_load;
  2040. busiest = group;
  2041. busiest_nr_running = sum_nr_running;
  2042. busiest_load_per_task = sum_weighted_load;
  2043. }
  2044. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2045. /*
  2046. * Busy processors will not participate in power savings
  2047. * balance.
  2048. */
  2049. if (idle == CPU_NOT_IDLE ||
  2050. !(sd->flags & SD_POWERSAVINGS_BALANCE))
  2051. goto group_next;
  2052. /*
  2053. * If the local group is idle or completely loaded
  2054. * no need to do power savings balance at this domain
  2055. */
  2056. if (local_group && (this_nr_running >= group_capacity ||
  2057. !this_nr_running))
  2058. power_savings_balance = 0;
  2059. /*
  2060. * If a group is already running at full capacity or idle,
  2061. * don't include that group in power savings calculations
  2062. */
  2063. if (!power_savings_balance || sum_nr_running >= group_capacity
  2064. || !sum_nr_running)
  2065. goto group_next;
  2066. /*
  2067. * Calculate the group which has the least non-idle load.
  2068. * This is the group from where we need to pick up the load
  2069. * for saving power
  2070. */
  2071. if ((sum_nr_running < min_nr_running) ||
  2072. (sum_nr_running == min_nr_running &&
  2073. first_cpu(group->cpumask) <
  2074. first_cpu(group_min->cpumask))) {
  2075. group_min = group;
  2076. min_nr_running = sum_nr_running;
  2077. min_load_per_task = sum_weighted_load /
  2078. sum_nr_running;
  2079. }
  2080. /*
  2081. * Calculate the group which is almost near its
  2082. * capacity but still has some space to pick up some load
  2083. * from other group and save more power
  2084. */
  2085. if (sum_nr_running <= group_capacity - 1) {
  2086. if (sum_nr_running > leader_nr_running ||
  2087. (sum_nr_running == leader_nr_running &&
  2088. first_cpu(group->cpumask) >
  2089. first_cpu(group_leader->cpumask))) {
  2090. group_leader = group;
  2091. leader_nr_running = sum_nr_running;
  2092. }
  2093. }
  2094. group_next:
  2095. #endif
  2096. group = group->next;
  2097. } while (group != sd->groups);
  2098. if (!busiest || this_load >= max_load || busiest_nr_running == 0)
  2099. goto out_balanced;
  2100. avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
  2101. if (this_load >= avg_load ||
  2102. 100*max_load <= sd->imbalance_pct*this_load)
  2103. goto out_balanced;
  2104. busiest_load_per_task /= busiest_nr_running;
  2105. /*
  2106. * We're trying to get all the cpus to the average_load, so we don't
  2107. * want to push ourselves above the average load, nor do we wish to
  2108. * reduce the max loaded cpu below the average load, as either of these
  2109. * actions would just result in more rebalancing later, and ping-pong
  2110. * tasks around. Thus we look for the minimum possible imbalance.
  2111. * Negative imbalances (*we* are more loaded than anyone else) will
  2112. * be counted as no imbalance for these purposes -- we can't fix that
  2113. * by pulling tasks to us. Be careful of negative numbers as they'll
  2114. * appear as very large values with unsigned longs.
  2115. */
  2116. if (max_load <= busiest_load_per_task)
  2117. goto out_balanced;
  2118. /*
  2119. * In the presence of smp nice balancing, certain scenarios can have
  2120. * max load less than avg load(as we skip the groups at or below
  2121. * its cpu_power, while calculating max_load..)
  2122. */
  2123. if (max_load < avg_load) {
  2124. *imbalance = 0;
  2125. goto small_imbalance;
  2126. }
  2127. /* Don't want to pull so many tasks that a group would go idle */
  2128. max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
  2129. /* How much load to actually move to equalise the imbalance */
  2130. *imbalance = min(max_pull * busiest->__cpu_power,
  2131. (avg_load - this_load) * this->__cpu_power)
  2132. / SCHED_LOAD_SCALE;
  2133. /*
  2134. * if *imbalance is less than the average load per runnable task
  2135. * there is no gaurantee that any tasks will be moved so we'll have
  2136. * a think about bumping its value to force at least one task to be
  2137. * moved
  2138. */
  2139. if (*imbalance + SCHED_LOAD_SCALE_FUZZ < busiest_load_per_task/2) {
  2140. unsigned long tmp, pwr_now, pwr_move;
  2141. unsigned int imbn;
  2142. small_imbalance:
  2143. pwr_move = pwr_now = 0;
  2144. imbn = 2;
  2145. if (this_nr_running) {
  2146. this_load_per_task /= this_nr_running;
  2147. if (busiest_load_per_task > this_load_per_task)
  2148. imbn = 1;
  2149. } else
  2150. this_load_per_task = SCHED_LOAD_SCALE;
  2151. if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
  2152. busiest_load_per_task * imbn) {
  2153. *imbalance = busiest_load_per_task;
  2154. return busiest;
  2155. }
  2156. /*
  2157. * OK, we don't have enough imbalance to justify moving tasks,
  2158. * however we may be able to increase total CPU power used by
  2159. * moving them.
  2160. */
  2161. pwr_now += busiest->__cpu_power *
  2162. min(busiest_load_per_task, max_load);
  2163. pwr_now += this->__cpu_power *
  2164. min(this_load_per_task, this_load);
  2165. pwr_now /= SCHED_LOAD_SCALE;
  2166. /* Amount of load we'd subtract */
  2167. tmp = sg_div_cpu_power(busiest,
  2168. busiest_load_per_task * SCHED_LOAD_SCALE);
  2169. if (max_load > tmp)
  2170. pwr_move += busiest->__cpu_power *
  2171. min(busiest_load_per_task, max_load - tmp);
  2172. /* Amount of load we'd add */
  2173. if (max_load * busiest->__cpu_power <
  2174. busiest_load_per_task * SCHED_LOAD_SCALE)
  2175. tmp = sg_div_cpu_power(this,
  2176. max_load * busiest->__cpu_power);
  2177. else
  2178. tmp = sg_div_cpu_power(this,
  2179. busiest_load_per_task * SCHED_LOAD_SCALE);
  2180. pwr_move += this->__cpu_power *
  2181. min(this_load_per_task, this_load + tmp);
  2182. pwr_move /= SCHED_LOAD_SCALE;
  2183. /* Move if we gain throughput */
  2184. if (pwr_move <= pwr_now)
  2185. goto out_balanced;
  2186. *imbalance = busiest_load_per_task;
  2187. }
  2188. return busiest;
  2189. out_balanced:
  2190. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2191. if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
  2192. goto ret;
  2193. if (this == group_leader && group_leader != group_min) {
  2194. *imbalance = min_load_per_task;
  2195. return group_min;
  2196. }
  2197. #endif
  2198. ret:
  2199. *imbalance = 0;
  2200. return NULL;
  2201. }
  2202. /*
  2203. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  2204. */
  2205. static struct rq *
  2206. find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
  2207. unsigned long imbalance, cpumask_t *cpus)
  2208. {
  2209. struct rq *busiest = NULL, *rq;
  2210. unsigned long max_load = 0;
  2211. int i;
  2212. for_each_cpu_mask(i, group->cpumask) {
  2213. unsigned long wl;
  2214. if (!cpu_isset(i, *cpus))
  2215. continue;
  2216. rq = cpu_rq(i);
  2217. wl = weighted_cpuload(i);
  2218. if (rq->nr_running == 1 && wl > imbalance)
  2219. continue;
  2220. if (wl > max_load) {
  2221. max_load = wl;
  2222. busiest = rq;
  2223. }
  2224. }
  2225. return busiest;
  2226. }
  2227. /*
  2228. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  2229. * so long as it is large enough.
  2230. */
  2231. #define MAX_PINNED_INTERVAL 512
  2232. static inline unsigned long minus_1_or_zero(unsigned long n)
  2233. {
  2234. return n > 0 ? n - 1 : 0;
  2235. }
  2236. /*
  2237. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  2238. * tasks if there is an imbalance.
  2239. */
  2240. static int load_balance(int this_cpu, struct rq *this_rq,
  2241. struct sched_domain *sd, enum cpu_idle_type idle,
  2242. int *balance)
  2243. {
  2244. int nr_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
  2245. struct sched_group *group;
  2246. unsigned long imbalance;
  2247. struct rq *busiest;
  2248. cpumask_t cpus = CPU_MASK_ALL;
  2249. unsigned long flags;
  2250. /*
  2251. * When power savings policy is enabled for the parent domain, idle
  2252. * sibling can pick up load irrespective of busy siblings. In this case,
  2253. * let the state of idle sibling percolate up as CPU_IDLE, instead of
  2254. * portraying it as CPU_NOT_IDLE.
  2255. */
  2256. if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
  2257. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2258. sd_idle = 1;
  2259. schedstat_inc(sd, lb_cnt[idle]);
  2260. redo:
  2261. group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
  2262. &cpus, balance);
  2263. if (*balance == 0)
  2264. goto out_balanced;
  2265. if (!group) {
  2266. schedstat_inc(sd, lb_nobusyg[idle]);
  2267. goto out_balanced;
  2268. }
  2269. busiest = find_busiest_queue(group, idle, imbalance, &cpus);
  2270. if (!busiest) {
  2271. schedstat_inc(sd, lb_nobusyq[idle]);
  2272. goto out_balanced;
  2273. }
  2274. BUG_ON(busiest == this_rq);
  2275. schedstat_add(sd, lb_imbalance[idle], imbalance);
  2276. nr_moved = 0;
  2277. if (busiest->nr_running > 1) {
  2278. /*
  2279. * Attempt to move tasks. If find_busiest_group has found
  2280. * an imbalance but busiest->nr_running <= 1, the group is
  2281. * still unbalanced. nr_moved simply stays zero, so it is
  2282. * correctly treated as an imbalance.
  2283. */
  2284. local_irq_save(flags);
  2285. double_rq_lock(this_rq, busiest);
  2286. nr_moved = move_tasks(this_rq, this_cpu, busiest,
  2287. minus_1_or_zero(busiest->nr_running),
  2288. imbalance, sd, idle, &all_pinned);
  2289. double_rq_unlock(this_rq, busiest);
  2290. local_irq_restore(flags);
  2291. /*
  2292. * some other cpu did the load balance for us.
  2293. */
  2294. if (nr_moved && this_cpu != smp_processor_id())
  2295. resched_cpu(this_cpu);
  2296. /* All tasks on this runqueue were pinned by CPU affinity */
  2297. if (unlikely(all_pinned)) {
  2298. cpu_clear(cpu_of(busiest), cpus);
  2299. if (!cpus_empty(cpus))
  2300. goto redo;
  2301. goto out_balanced;
  2302. }
  2303. }
  2304. if (!nr_moved) {
  2305. schedstat_inc(sd, lb_failed[idle]);
  2306. sd->nr_balance_failed++;
  2307. if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
  2308. spin_lock_irqsave(&busiest->lock, flags);
  2309. /* don't kick the migration_thread, if the curr
  2310. * task on busiest cpu can't be moved to this_cpu
  2311. */
  2312. if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
  2313. spin_unlock_irqrestore(&busiest->lock, flags);
  2314. all_pinned = 1;
  2315. goto out_one_pinned;
  2316. }
  2317. if (!busiest->active_balance) {
  2318. busiest->active_balance = 1;
  2319. busiest->push_cpu = this_cpu;
  2320. active_balance = 1;
  2321. }
  2322. spin_unlock_irqrestore(&busiest->lock, flags);
  2323. if (active_balance)
  2324. wake_up_process(busiest->migration_thread);
  2325. /*
  2326. * We've kicked active balancing, reset the failure
  2327. * counter.
  2328. */
  2329. sd->nr_balance_failed = sd->cache_nice_tries+1;
  2330. }
  2331. } else
  2332. sd->nr_balance_failed = 0;
  2333. if (likely(!active_balance)) {
  2334. /* We were unbalanced, so reset the balancing interval */
  2335. sd->balance_interval = sd->min_interval;
  2336. } else {
  2337. /*
  2338. * If we've begun active balancing, start to back off. This
  2339. * case may not be covered by the all_pinned logic if there
  2340. * is only 1 task on the busy runqueue (because we don't call
  2341. * move_tasks).
  2342. */
  2343. if (sd->balance_interval < sd->max_interval)
  2344. sd->balance_interval *= 2;
  2345. }
  2346. if (!nr_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2347. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2348. return -1;
  2349. return nr_moved;
  2350. out_balanced:
  2351. schedstat_inc(sd, lb_balanced[idle]);
  2352. sd->nr_balance_failed = 0;
  2353. out_one_pinned:
  2354. /* tune up the balancing interval */
  2355. if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
  2356. (sd->balance_interval < sd->max_interval))
  2357. sd->balance_interval *= 2;
  2358. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2359. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2360. return -1;
  2361. return 0;
  2362. }
  2363. /*
  2364. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  2365. * tasks if there is an imbalance.
  2366. *
  2367. * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
  2368. * this_rq is locked.
  2369. */
  2370. static int
  2371. load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
  2372. {
  2373. struct sched_group *group;
  2374. struct rq *busiest = NULL;
  2375. unsigned long imbalance;
  2376. int nr_moved = 0;
  2377. int sd_idle = 0;
  2378. int all_pinned = 0;
  2379. cpumask_t cpus = CPU_MASK_ALL;
  2380. /*
  2381. * When power savings policy is enabled for the parent domain, idle
  2382. * sibling can pick up load irrespective of busy siblings. In this case,
  2383. * let the state of idle sibling percolate up as IDLE, instead of
  2384. * portraying it as CPU_NOT_IDLE.
  2385. */
  2386. if (sd->flags & SD_SHARE_CPUPOWER &&
  2387. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2388. sd_idle = 1;
  2389. schedstat_inc(sd, lb_cnt[CPU_NEWLY_IDLE]);
  2390. redo:
  2391. group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
  2392. &sd_idle, &cpus, NULL);
  2393. if (!group) {
  2394. schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
  2395. goto out_balanced;
  2396. }
  2397. busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance,
  2398. &cpus);
  2399. if (!busiest) {
  2400. schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
  2401. goto out_balanced;
  2402. }
  2403. BUG_ON(busiest == this_rq);
  2404. schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
  2405. nr_moved = 0;
  2406. if (busiest->nr_running > 1) {
  2407. /* Attempt to move tasks */
  2408. double_lock_balance(this_rq, busiest);
  2409. nr_moved = move_tasks(this_rq, this_cpu, busiest,
  2410. minus_1_or_zero(busiest->nr_running),
  2411. imbalance, sd, CPU_NEWLY_IDLE,
  2412. &all_pinned);
  2413. spin_unlock(&busiest->lock);
  2414. if (unlikely(all_pinned)) {
  2415. cpu_clear(cpu_of(busiest), cpus);
  2416. if (!cpus_empty(cpus))
  2417. goto redo;
  2418. }
  2419. }
  2420. if (!nr_moved) {
  2421. schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
  2422. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2423. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2424. return -1;
  2425. } else
  2426. sd->nr_balance_failed = 0;
  2427. return nr_moved;
  2428. out_balanced:
  2429. schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
  2430. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2431. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2432. return -1;
  2433. sd->nr_balance_failed = 0;
  2434. return 0;
  2435. }
  2436. /*
  2437. * idle_balance is called by schedule() if this_cpu is about to become
  2438. * idle. Attempts to pull tasks from other CPUs.
  2439. */
  2440. static void idle_balance(int this_cpu, struct rq *this_rq)
  2441. {
  2442. struct sched_domain *sd;
  2443. int pulled_task = -1;
  2444. unsigned long next_balance = jiffies + HZ;
  2445. for_each_domain(this_cpu, sd) {
  2446. unsigned long interval;
  2447. if (!(sd->flags & SD_LOAD_BALANCE))
  2448. continue;
  2449. if (sd->flags & SD_BALANCE_NEWIDLE)
  2450. /* If we've pulled tasks over stop searching: */
  2451. pulled_task = load_balance_newidle(this_cpu,
  2452. this_rq, sd);
  2453. interval = msecs_to_jiffies(sd->balance_interval);
  2454. if (time_after(next_balance, sd->last_balance + interval))
  2455. next_balance = sd->last_balance + interval;
  2456. if (pulled_task)
  2457. break;
  2458. }
  2459. if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
  2460. /*
  2461. * We are going idle. next_balance may be set based on
  2462. * a busy processor. So reset next_balance.
  2463. */
  2464. this_rq->next_balance = next_balance;
  2465. }
  2466. }
  2467. /*
  2468. * active_load_balance is run by migration threads. It pushes running tasks
  2469. * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
  2470. * running on each physical CPU where possible, and avoids physical /
  2471. * logical imbalances.
  2472. *
  2473. * Called with busiest_rq locked.
  2474. */
  2475. static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
  2476. {
  2477. int target_cpu = busiest_rq->push_cpu;
  2478. struct sched_domain *sd;
  2479. struct rq *target_rq;
  2480. /* Is there any task to move? */
  2481. if (busiest_rq->nr_running <= 1)
  2482. return;
  2483. target_rq = cpu_rq(target_cpu);
  2484. /*
  2485. * This condition is "impossible", if it occurs
  2486. * we need to fix it. Originally reported by
  2487. * Bjorn Helgaas on a 128-cpu setup.
  2488. */
  2489. BUG_ON(busiest_rq == target_rq);
  2490. /* move a task from busiest_rq to target_rq */
  2491. double_lock_balance(busiest_rq, target_rq);
  2492. /* Search for an sd spanning us and the target CPU. */
  2493. for_each_domain(target_cpu, sd) {
  2494. if ((sd->flags & SD_LOAD_BALANCE) &&
  2495. cpu_isset(busiest_cpu, sd->span))
  2496. break;
  2497. }
  2498. if (likely(sd)) {
  2499. schedstat_inc(sd, alb_cnt);
  2500. if (move_tasks(target_rq, target_cpu, busiest_rq, 1,
  2501. RTPRIO_TO_LOAD_WEIGHT(100), sd, CPU_IDLE,
  2502. NULL))
  2503. schedstat_inc(sd, alb_pushed);
  2504. else
  2505. schedstat_inc(sd, alb_failed);
  2506. }
  2507. spin_unlock(&target_rq->lock);
  2508. }
  2509. #ifdef CONFIG_NO_HZ
  2510. static struct {
  2511. atomic_t load_balancer;
  2512. cpumask_t cpu_mask;
  2513. } nohz ____cacheline_aligned = {
  2514. .load_balancer = ATOMIC_INIT(-1),
  2515. .cpu_mask = CPU_MASK_NONE,
  2516. };
  2517. /*
  2518. * This routine will try to nominate the ilb (idle load balancing)
  2519. * owner among the cpus whose ticks are stopped. ilb owner will do the idle
  2520. * load balancing on behalf of all those cpus. If all the cpus in the system
  2521. * go into this tickless mode, then there will be no ilb owner (as there is
  2522. * no need for one) and all the cpus will sleep till the next wakeup event
  2523. * arrives...
  2524. *
  2525. * For the ilb owner, tick is not stopped. And this tick will be used
  2526. * for idle load balancing. ilb owner will still be part of
  2527. * nohz.cpu_mask..
  2528. *
  2529. * While stopping the tick, this cpu will become the ilb owner if there
  2530. * is no other owner. And will be the owner till that cpu becomes busy
  2531. * or if all cpus in the system stop their ticks at which point
  2532. * there is no need for ilb owner.
  2533. *
  2534. * When the ilb owner becomes busy, it nominates another owner, during the
  2535. * next busy scheduler_tick()
  2536. */
  2537. int select_nohz_load_balancer(int stop_tick)
  2538. {
  2539. int cpu = smp_processor_id();
  2540. if (stop_tick) {
  2541. cpu_set(cpu, nohz.cpu_mask);
  2542. cpu_rq(cpu)->in_nohz_recently = 1;
  2543. /*
  2544. * If we are going offline and still the leader, give up!
  2545. */
  2546. if (cpu_is_offline(cpu) &&
  2547. atomic_read(&nohz.load_balancer) == cpu) {
  2548. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  2549. BUG();
  2550. return 0;
  2551. }
  2552. /* time for ilb owner also to sleep */
  2553. if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
  2554. if (atomic_read(&nohz.load_balancer) == cpu)
  2555. atomic_set(&nohz.load_balancer, -1);
  2556. return 0;
  2557. }
  2558. if (atomic_read(&nohz.load_balancer) == -1) {
  2559. /* make me the ilb owner */
  2560. if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
  2561. return 1;
  2562. } else if (atomic_read(&nohz.load_balancer) == cpu)
  2563. return 1;
  2564. } else {
  2565. if (!cpu_isset(cpu, nohz.cpu_mask))
  2566. return 0;
  2567. cpu_clear(cpu, nohz.cpu_mask);
  2568. if (atomic_read(&nohz.load_balancer) == cpu)
  2569. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  2570. BUG();
  2571. }
  2572. return 0;
  2573. }
  2574. #endif
  2575. static DEFINE_SPINLOCK(balancing);
  2576. /*
  2577. * It checks each scheduling domain to see if it is due to be balanced,
  2578. * and initiates a balancing operation if so.
  2579. *
  2580. * Balancing parameters are set up in arch_init_sched_domains.
  2581. */
  2582. static inline void rebalance_domains(int cpu, enum cpu_idle_type idle)
  2583. {
  2584. int balance = 1;
  2585. struct rq *rq = cpu_rq(cpu);
  2586. unsigned long interval;
  2587. struct sched_domain *sd;
  2588. /* Earliest time when we have to do rebalance again */
  2589. unsigned long next_balance = jiffies + 60*HZ;
  2590. for_each_domain(cpu, sd) {
  2591. if (!(sd->flags & SD_LOAD_BALANCE))
  2592. continue;
  2593. interval = sd->balance_interval;
  2594. if (idle != CPU_IDLE)
  2595. interval *= sd->busy_factor;
  2596. /* scale ms to jiffies */
  2597. interval = msecs_to_jiffies(interval);
  2598. if (unlikely(!interval))
  2599. interval = 1;
  2600. if (interval > HZ*NR_CPUS/10)
  2601. interval = HZ*NR_CPUS/10;
  2602. if (sd->flags & SD_SERIALIZE) {
  2603. if (!spin_trylock(&balancing))
  2604. goto out;
  2605. }
  2606. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  2607. if (load_balance(cpu, rq, sd, idle, &balance)) {
  2608. /*
  2609. * We've pulled tasks over so either we're no
  2610. * longer idle, or one of our SMT siblings is
  2611. * not idle.
  2612. */
  2613. idle = CPU_NOT_IDLE;
  2614. }
  2615. sd->last_balance = jiffies;
  2616. }
  2617. if (sd->flags & SD_SERIALIZE)
  2618. spin_unlock(&balancing);
  2619. out:
  2620. if (time_after(next_balance, sd->last_balance + interval))
  2621. next_balance = sd->last_balance + interval;
  2622. /*
  2623. * Stop the load balance at this level. There is another
  2624. * CPU in our sched group which is doing load balancing more
  2625. * actively.
  2626. */
  2627. if (!balance)
  2628. break;
  2629. }
  2630. rq->next_balance = next_balance;
  2631. }
  2632. /*
  2633. * run_rebalance_domains is triggered when needed from the scheduler tick.
  2634. * In CONFIG_NO_HZ case, the idle load balance owner will do the
  2635. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  2636. */
  2637. static void run_rebalance_domains(struct softirq_action *h)
  2638. {
  2639. int this_cpu = smp_processor_id();
  2640. struct rq *this_rq = cpu_rq(this_cpu);
  2641. enum cpu_idle_type idle = this_rq->idle_at_tick ?
  2642. CPU_IDLE : CPU_NOT_IDLE;
  2643. rebalance_domains(this_cpu, idle);
  2644. #ifdef CONFIG_NO_HZ
  2645. /*
  2646. * If this cpu is the owner for idle load balancing, then do the
  2647. * balancing on behalf of the other idle cpus whose ticks are
  2648. * stopped.
  2649. */
  2650. if (this_rq->idle_at_tick &&
  2651. atomic_read(&nohz.load_balancer) == this_cpu) {
  2652. cpumask_t cpus = nohz.cpu_mask;
  2653. struct rq *rq;
  2654. int balance_cpu;
  2655. cpu_clear(this_cpu, cpus);
  2656. for_each_cpu_mask(balance_cpu, cpus) {
  2657. /*
  2658. * If this cpu gets work to do, stop the load balancing
  2659. * work being done for other cpus. Next load
  2660. * balancing owner will pick it up.
  2661. */
  2662. if (need_resched())
  2663. break;
  2664. rebalance_domains(balance_cpu, SCHED_IDLE);
  2665. rq = cpu_rq(balance_cpu);
  2666. if (time_after(this_rq->next_balance, rq->next_balance))
  2667. this_rq->next_balance = rq->next_balance;
  2668. }
  2669. }
  2670. #endif
  2671. }
  2672. /*
  2673. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  2674. *
  2675. * In case of CONFIG_NO_HZ, this is the place where we nominate a new
  2676. * idle load balancing owner or decide to stop the periodic load balancing,
  2677. * if the whole system is idle.
  2678. */
  2679. static inline void trigger_load_balance(struct rq *rq, int cpu)
  2680. {
  2681. #ifdef CONFIG_NO_HZ
  2682. /*
  2683. * If we were in the nohz mode recently and busy at the current
  2684. * scheduler tick, then check if we need to nominate new idle
  2685. * load balancer.
  2686. */
  2687. if (rq->in_nohz_recently && !rq->idle_at_tick) {
  2688. rq->in_nohz_recently = 0;
  2689. if (atomic_read(&nohz.load_balancer) == cpu) {
  2690. cpu_clear(cpu, nohz.cpu_mask);
  2691. atomic_set(&nohz.load_balancer, -1);
  2692. }
  2693. if (atomic_read(&nohz.load_balancer) == -1) {
  2694. /*
  2695. * simple selection for now: Nominate the
  2696. * first cpu in the nohz list to be the next
  2697. * ilb owner.
  2698. *
  2699. * TBD: Traverse the sched domains and nominate
  2700. * the nearest cpu in the nohz.cpu_mask.
  2701. */
  2702. int ilb = first_cpu(nohz.cpu_mask);
  2703. if (ilb != NR_CPUS)
  2704. resched_cpu(ilb);
  2705. }
  2706. }
  2707. /*
  2708. * If this cpu is idle and doing idle load balancing for all the
  2709. * cpus with ticks stopped, is it time for that to stop?
  2710. */
  2711. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
  2712. cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
  2713. resched_cpu(cpu);
  2714. return;
  2715. }
  2716. /*
  2717. * If this cpu is idle and the idle load balancing is done by
  2718. * someone else, then no need raise the SCHED_SOFTIRQ
  2719. */
  2720. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
  2721. cpu_isset(cpu, nohz.cpu_mask))
  2722. return;
  2723. #endif
  2724. if (time_after_eq(jiffies, rq->next_balance))
  2725. raise_softirq(SCHED_SOFTIRQ);
  2726. }
  2727. #else /* CONFIG_SMP */
  2728. /*
  2729. * on UP we do not need to balance between CPUs:
  2730. */
  2731. static inline void idle_balance(int cpu, struct rq *rq)
  2732. {
  2733. }
  2734. /* Avoid "used but not defined" warning on UP */
  2735. static int balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2736. unsigned long max_nr_move, unsigned long max_load_move,
  2737. struct sched_domain *sd, enum cpu_idle_type idle,
  2738. int *all_pinned, unsigned long *load_moved,
  2739. int this_best_prio, int best_prio, int best_prio_seen,
  2740. struct rq_iterator *iterator)
  2741. {
  2742. *load_moved = 0;
  2743. return 0;
  2744. }
  2745. #endif
  2746. DEFINE_PER_CPU(struct kernel_stat, kstat);
  2747. EXPORT_PER_CPU_SYMBOL(kstat);
  2748. /*
  2749. * Return p->sum_exec_runtime plus any more ns on the sched_clock
  2750. * that have not yet been banked in case the task is currently running.
  2751. */
  2752. unsigned long long task_sched_runtime(struct task_struct *p)
  2753. {
  2754. unsigned long flags;
  2755. u64 ns, delta_exec;
  2756. struct rq *rq;
  2757. rq = task_rq_lock(p, &flags);
  2758. ns = p->se.sum_exec_runtime;
  2759. if (rq->curr == p) {
  2760. delta_exec = rq_clock(rq) - p->se.exec_start;
  2761. if ((s64)delta_exec > 0)
  2762. ns += delta_exec;
  2763. }
  2764. task_rq_unlock(rq, &flags);
  2765. return ns;
  2766. }
  2767. /*
  2768. * Account user cpu time to a process.
  2769. * @p: the process that the cpu time gets accounted to
  2770. * @hardirq_offset: the offset to subtract from hardirq_count()
  2771. * @cputime: the cpu time spent in user space since the last update
  2772. */
  2773. void account_user_time(struct task_struct *p, cputime_t cputime)
  2774. {
  2775. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2776. cputime64_t tmp;
  2777. p->utime = cputime_add(p->utime, cputime);
  2778. /* Add user time to cpustat. */
  2779. tmp = cputime_to_cputime64(cputime);
  2780. if (TASK_NICE(p) > 0)
  2781. cpustat->nice = cputime64_add(cpustat->nice, tmp);
  2782. else
  2783. cpustat->user = cputime64_add(cpustat->user, tmp);
  2784. }
  2785. /*
  2786. * Account system cpu time to a process.
  2787. * @p: the process that the cpu time gets accounted to
  2788. * @hardirq_offset: the offset to subtract from hardirq_count()
  2789. * @cputime: the cpu time spent in kernel space since the last update
  2790. */
  2791. void account_system_time(struct task_struct *p, int hardirq_offset,
  2792. cputime_t cputime)
  2793. {
  2794. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2795. struct rq *rq = this_rq();
  2796. cputime64_t tmp;
  2797. p->stime = cputime_add(p->stime, cputime);
  2798. /* Add system time to cpustat. */
  2799. tmp = cputime_to_cputime64(cputime);
  2800. if (hardirq_count() - hardirq_offset)
  2801. cpustat->irq = cputime64_add(cpustat->irq, tmp);
  2802. else if (softirq_count())
  2803. cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
  2804. else if (p != rq->idle)
  2805. cpustat->system = cputime64_add(cpustat->system, tmp);
  2806. else if (atomic_read(&rq->nr_iowait) > 0)
  2807. cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
  2808. else
  2809. cpustat->idle = cputime64_add(cpustat->idle, tmp);
  2810. /* Account for system time used */
  2811. acct_update_integrals(p);
  2812. }
  2813. /*
  2814. * Account for involuntary wait time.
  2815. * @p: the process from which the cpu time has been stolen
  2816. * @steal: the cpu time spent in involuntary wait
  2817. */
  2818. void account_steal_time(struct task_struct *p, cputime_t steal)
  2819. {
  2820. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  2821. cputime64_t tmp = cputime_to_cputime64(steal);
  2822. struct rq *rq = this_rq();
  2823. if (p == rq->idle) {
  2824. p->stime = cputime_add(p->stime, steal);
  2825. if (atomic_read(&rq->nr_iowait) > 0)
  2826. cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
  2827. else
  2828. cpustat->idle = cputime64_add(cpustat->idle, tmp);
  2829. } else
  2830. cpustat->steal = cputime64_add(cpustat->steal, tmp);
  2831. }
  2832. /*
  2833. * This function gets called by the timer code, with HZ frequency.
  2834. * We call it with interrupts disabled.
  2835. *
  2836. * It also gets called by the fork code, when changing the parent's
  2837. * timeslices.
  2838. */
  2839. void scheduler_tick(void)
  2840. {
  2841. int cpu = smp_processor_id();
  2842. struct rq *rq = cpu_rq(cpu);
  2843. struct task_struct *curr = rq->curr;
  2844. spin_lock(&rq->lock);
  2845. if (curr != rq->idle) /* FIXME: needed? */
  2846. curr->sched_class->task_tick(rq, curr);
  2847. update_cpu_load(rq);
  2848. spin_unlock(&rq->lock);
  2849. #ifdef CONFIG_SMP
  2850. rq->idle_at_tick = idle_cpu(cpu);
  2851. trigger_load_balance(rq, cpu);
  2852. #endif
  2853. }
  2854. #if defined(CONFIG_PREEMPT) && defined(CONFIG_DEBUG_PREEMPT)
  2855. void fastcall add_preempt_count(int val)
  2856. {
  2857. /*
  2858. * Underflow?
  2859. */
  2860. if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
  2861. return;
  2862. preempt_count() += val;
  2863. /*
  2864. * Spinlock count overflowing soon?
  2865. */
  2866. DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
  2867. PREEMPT_MASK - 10);
  2868. }
  2869. EXPORT_SYMBOL(add_preempt_count);
  2870. void fastcall sub_preempt_count(int val)
  2871. {
  2872. /*
  2873. * Underflow?
  2874. */
  2875. if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
  2876. return;
  2877. /*
  2878. * Is the spinlock portion underflowing?
  2879. */
  2880. if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
  2881. !(preempt_count() & PREEMPT_MASK)))
  2882. return;
  2883. preempt_count() -= val;
  2884. }
  2885. EXPORT_SYMBOL(sub_preempt_count);
  2886. #endif
  2887. /*
  2888. * Print scheduling while atomic bug:
  2889. */
  2890. static noinline void __schedule_bug(struct task_struct *prev)
  2891. {
  2892. printk(KERN_ERR "BUG: scheduling while atomic: %s/0x%08x/%d\n",
  2893. prev->comm, preempt_count(), prev->pid);
  2894. debug_show_held_locks(prev);
  2895. if (irqs_disabled())
  2896. print_irqtrace_events(prev);
  2897. dump_stack();
  2898. }
  2899. /*
  2900. * Various schedule()-time debugging checks and statistics:
  2901. */
  2902. static inline void schedule_debug(struct task_struct *prev)
  2903. {
  2904. /*
  2905. * Test if we are atomic. Since do_exit() needs to call into
  2906. * schedule() atomically, we ignore that path for now.
  2907. * Otherwise, whine if we are scheduling when we should not be.
  2908. */
  2909. if (unlikely(in_atomic_preempt_off()) && unlikely(!prev->exit_state))
  2910. __schedule_bug(prev);
  2911. profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  2912. schedstat_inc(this_rq(), sched_cnt);
  2913. }
  2914. /*
  2915. * Pick up the highest-prio task:
  2916. */
  2917. static inline struct task_struct *
  2918. pick_next_task(struct rq *rq, struct task_struct *prev, u64 now)
  2919. {
  2920. struct sched_class *class;
  2921. struct task_struct *p;
  2922. /*
  2923. * Optimization: we know that if all tasks are in
  2924. * the fair class we can call that function directly:
  2925. */
  2926. if (likely(rq->nr_running == rq->cfs.nr_running)) {
  2927. p = fair_sched_class.pick_next_task(rq, now);
  2928. if (likely(p))
  2929. return p;
  2930. }
  2931. class = sched_class_highest;
  2932. for ( ; ; ) {
  2933. p = class->pick_next_task(rq, now);
  2934. if (p)
  2935. return p;
  2936. /*
  2937. * Will never be NULL as the idle class always
  2938. * returns a non-NULL p:
  2939. */
  2940. class = class->next;
  2941. }
  2942. }
  2943. /*
  2944. * schedule() is the main scheduler function.
  2945. */
  2946. asmlinkage void __sched schedule(void)
  2947. {
  2948. struct task_struct *prev, *next;
  2949. long *switch_count;
  2950. struct rq *rq;
  2951. u64 now;
  2952. int cpu;
  2953. need_resched:
  2954. preempt_disable();
  2955. cpu = smp_processor_id();
  2956. rq = cpu_rq(cpu);
  2957. rcu_qsctr_inc(cpu);
  2958. prev = rq->curr;
  2959. switch_count = &prev->nivcsw;
  2960. release_kernel_lock(prev);
  2961. need_resched_nonpreemptible:
  2962. schedule_debug(prev);
  2963. spin_lock_irq(&rq->lock);
  2964. clear_tsk_need_resched(prev);
  2965. if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  2966. if (unlikely((prev->state & TASK_INTERRUPTIBLE) &&
  2967. unlikely(signal_pending(prev)))) {
  2968. prev->state = TASK_RUNNING;
  2969. } else {
  2970. deactivate_task(rq, prev, 1);
  2971. }
  2972. switch_count = &prev->nvcsw;
  2973. }
  2974. if (unlikely(!rq->nr_running))
  2975. idle_balance(cpu, rq);
  2976. now = __rq_clock(rq);
  2977. prev->sched_class->put_prev_task(rq, prev, now);
  2978. next = pick_next_task(rq, prev, now);
  2979. sched_info_switch(prev, next);
  2980. if (likely(prev != next)) {
  2981. rq->nr_switches++;
  2982. rq->curr = next;
  2983. ++*switch_count;
  2984. context_switch(rq, prev, next); /* unlocks the rq */
  2985. } else
  2986. spin_unlock_irq(&rq->lock);
  2987. if (unlikely(reacquire_kernel_lock(current) < 0)) {
  2988. cpu = smp_processor_id();
  2989. rq = cpu_rq(cpu);
  2990. goto need_resched_nonpreemptible;
  2991. }
  2992. preempt_enable_no_resched();
  2993. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  2994. goto need_resched;
  2995. }
  2996. EXPORT_SYMBOL(schedule);
  2997. #ifdef CONFIG_PREEMPT
  2998. /*
  2999. * this is the entry point to schedule() from in-kernel preemption
  3000. * off of preempt_enable. Kernel preemptions off return from interrupt
  3001. * occur there and call schedule directly.
  3002. */
  3003. asmlinkage void __sched preempt_schedule(void)
  3004. {
  3005. struct thread_info *ti = current_thread_info();
  3006. #ifdef CONFIG_PREEMPT_BKL
  3007. struct task_struct *task = current;
  3008. int saved_lock_depth;
  3009. #endif
  3010. /*
  3011. * If there is a non-zero preempt_count or interrupts are disabled,
  3012. * we do not want to preempt the current task. Just return..
  3013. */
  3014. if (likely(ti->preempt_count || irqs_disabled()))
  3015. return;
  3016. need_resched:
  3017. add_preempt_count(PREEMPT_ACTIVE);
  3018. /*
  3019. * We keep the big kernel semaphore locked, but we
  3020. * clear ->lock_depth so that schedule() doesnt
  3021. * auto-release the semaphore:
  3022. */
  3023. #ifdef CONFIG_PREEMPT_BKL
  3024. saved_lock_depth = task->lock_depth;
  3025. task->lock_depth = -1;
  3026. #endif
  3027. schedule();
  3028. #ifdef CONFIG_PREEMPT_BKL
  3029. task->lock_depth = saved_lock_depth;
  3030. #endif
  3031. sub_preempt_count(PREEMPT_ACTIVE);
  3032. /* we could miss a preemption opportunity between schedule and now */
  3033. barrier();
  3034. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  3035. goto need_resched;
  3036. }
  3037. EXPORT_SYMBOL(preempt_schedule);
  3038. /*
  3039. * this is the entry point to schedule() from kernel preemption
  3040. * off of irq context.
  3041. * Note, that this is called and return with irqs disabled. This will
  3042. * protect us against recursive calling from irq.
  3043. */
  3044. asmlinkage void __sched preempt_schedule_irq(void)
  3045. {
  3046. struct thread_info *ti = current_thread_info();
  3047. #ifdef CONFIG_PREEMPT_BKL
  3048. struct task_struct *task = current;
  3049. int saved_lock_depth;
  3050. #endif
  3051. /* Catch callers which need to be fixed */
  3052. BUG_ON(ti->preempt_count || !irqs_disabled());
  3053. need_resched:
  3054. add_preempt_count(PREEMPT_ACTIVE);
  3055. /*
  3056. * We keep the big kernel semaphore locked, but we
  3057. * clear ->lock_depth so that schedule() doesnt
  3058. * auto-release the semaphore:
  3059. */
  3060. #ifdef CONFIG_PREEMPT_BKL
  3061. saved_lock_depth = task->lock_depth;
  3062. task->lock_depth = -1;
  3063. #endif
  3064. local_irq_enable();
  3065. schedule();
  3066. local_irq_disable();
  3067. #ifdef CONFIG_PREEMPT_BKL
  3068. task->lock_depth = saved_lock_depth;
  3069. #endif
  3070. sub_preempt_count(PREEMPT_ACTIVE);
  3071. /* we could miss a preemption opportunity between schedule and now */
  3072. barrier();
  3073. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  3074. goto need_resched;
  3075. }
  3076. #endif /* CONFIG_PREEMPT */
  3077. int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
  3078. void *key)
  3079. {
  3080. return try_to_wake_up(curr->private, mode, sync);
  3081. }
  3082. EXPORT_SYMBOL(default_wake_function);
  3083. /*
  3084. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  3085. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  3086. * number) then we wake all the non-exclusive tasks and one exclusive task.
  3087. *
  3088. * There are circumstances in which we can try to wake a task which has already
  3089. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  3090. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  3091. */
  3092. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  3093. int nr_exclusive, int sync, void *key)
  3094. {
  3095. struct list_head *tmp, *next;
  3096. list_for_each_safe(tmp, next, &q->task_list) {
  3097. wait_queue_t *curr = list_entry(tmp, wait_queue_t, task_list);
  3098. unsigned flags = curr->flags;
  3099. if (curr->func(curr, mode, sync, key) &&
  3100. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  3101. break;
  3102. }
  3103. }
  3104. /**
  3105. * __wake_up - wake up threads blocked on a waitqueue.
  3106. * @q: the waitqueue
  3107. * @mode: which threads
  3108. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  3109. * @key: is directly passed to the wakeup function
  3110. */
  3111. void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
  3112. int nr_exclusive, void *key)
  3113. {
  3114. unsigned long flags;
  3115. spin_lock_irqsave(&q->lock, flags);
  3116. __wake_up_common(q, mode, nr_exclusive, 0, key);
  3117. spin_unlock_irqrestore(&q->lock, flags);
  3118. }
  3119. EXPORT_SYMBOL(__wake_up);
  3120. /*
  3121. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  3122. */
  3123. void fastcall __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
  3124. {
  3125. __wake_up_common(q, mode, 1, 0, NULL);
  3126. }
  3127. /**
  3128. * __wake_up_sync - wake up threads blocked on a waitqueue.
  3129. * @q: the waitqueue
  3130. * @mode: which threads
  3131. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  3132. *
  3133. * The sync wakeup differs that the waker knows that it will schedule
  3134. * away soon, so while the target thread will be woken up, it will not
  3135. * be migrated to another CPU - ie. the two threads are 'synchronized'
  3136. * with each other. This can prevent needless bouncing between CPUs.
  3137. *
  3138. * On UP it can prevent extra preemption.
  3139. */
  3140. void fastcall
  3141. __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  3142. {
  3143. unsigned long flags;
  3144. int sync = 1;
  3145. if (unlikely(!q))
  3146. return;
  3147. if (unlikely(!nr_exclusive))
  3148. sync = 0;
  3149. spin_lock_irqsave(&q->lock, flags);
  3150. __wake_up_common(q, mode, nr_exclusive, sync, NULL);
  3151. spin_unlock_irqrestore(&q->lock, flags);
  3152. }
  3153. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  3154. void fastcall complete(struct completion *x)
  3155. {
  3156. unsigned long flags;
  3157. spin_lock_irqsave(&x->wait.lock, flags);
  3158. x->done++;
  3159. __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
  3160. 1, 0, NULL);
  3161. spin_unlock_irqrestore(&x->wait.lock, flags);
  3162. }
  3163. EXPORT_SYMBOL(complete);
  3164. void fastcall complete_all(struct completion *x)
  3165. {
  3166. unsigned long flags;
  3167. spin_lock_irqsave(&x->wait.lock, flags);
  3168. x->done += UINT_MAX/2;
  3169. __wake_up_common(&x->wait, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE,
  3170. 0, 0, NULL);
  3171. spin_unlock_irqrestore(&x->wait.lock, flags);
  3172. }
  3173. EXPORT_SYMBOL(complete_all);
  3174. void fastcall __sched wait_for_completion(struct completion *x)
  3175. {
  3176. might_sleep();
  3177. spin_lock_irq(&x->wait.lock);
  3178. if (!x->done) {
  3179. DECLARE_WAITQUEUE(wait, current);
  3180. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3181. __add_wait_queue_tail(&x->wait, &wait);
  3182. do {
  3183. __set_current_state(TASK_UNINTERRUPTIBLE);
  3184. spin_unlock_irq(&x->wait.lock);
  3185. schedule();
  3186. spin_lock_irq(&x->wait.lock);
  3187. } while (!x->done);
  3188. __remove_wait_queue(&x->wait, &wait);
  3189. }
  3190. x->done--;
  3191. spin_unlock_irq(&x->wait.lock);
  3192. }
  3193. EXPORT_SYMBOL(wait_for_completion);
  3194. unsigned long fastcall __sched
  3195. wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  3196. {
  3197. might_sleep();
  3198. spin_lock_irq(&x->wait.lock);
  3199. if (!x->done) {
  3200. DECLARE_WAITQUEUE(wait, current);
  3201. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3202. __add_wait_queue_tail(&x->wait, &wait);
  3203. do {
  3204. __set_current_state(TASK_UNINTERRUPTIBLE);
  3205. spin_unlock_irq(&x->wait.lock);
  3206. timeout = schedule_timeout(timeout);
  3207. spin_lock_irq(&x->wait.lock);
  3208. if (!timeout) {
  3209. __remove_wait_queue(&x->wait, &wait);
  3210. goto out;
  3211. }
  3212. } while (!x->done);
  3213. __remove_wait_queue(&x->wait, &wait);
  3214. }
  3215. x->done--;
  3216. out:
  3217. spin_unlock_irq(&x->wait.lock);
  3218. return timeout;
  3219. }
  3220. EXPORT_SYMBOL(wait_for_completion_timeout);
  3221. int fastcall __sched wait_for_completion_interruptible(struct completion *x)
  3222. {
  3223. int ret = 0;
  3224. might_sleep();
  3225. spin_lock_irq(&x->wait.lock);
  3226. if (!x->done) {
  3227. DECLARE_WAITQUEUE(wait, current);
  3228. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3229. __add_wait_queue_tail(&x->wait, &wait);
  3230. do {
  3231. if (signal_pending(current)) {
  3232. ret = -ERESTARTSYS;
  3233. __remove_wait_queue(&x->wait, &wait);
  3234. goto out;
  3235. }
  3236. __set_current_state(TASK_INTERRUPTIBLE);
  3237. spin_unlock_irq(&x->wait.lock);
  3238. schedule();
  3239. spin_lock_irq(&x->wait.lock);
  3240. } while (!x->done);
  3241. __remove_wait_queue(&x->wait, &wait);
  3242. }
  3243. x->done--;
  3244. out:
  3245. spin_unlock_irq(&x->wait.lock);
  3246. return ret;
  3247. }
  3248. EXPORT_SYMBOL(wait_for_completion_interruptible);
  3249. unsigned long fastcall __sched
  3250. wait_for_completion_interruptible_timeout(struct completion *x,
  3251. unsigned long timeout)
  3252. {
  3253. might_sleep();
  3254. spin_lock_irq(&x->wait.lock);
  3255. if (!x->done) {
  3256. DECLARE_WAITQUEUE(wait, current);
  3257. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3258. __add_wait_queue_tail(&x->wait, &wait);
  3259. do {
  3260. if (signal_pending(current)) {
  3261. timeout = -ERESTARTSYS;
  3262. __remove_wait_queue(&x->wait, &wait);
  3263. goto out;
  3264. }
  3265. __set_current_state(TASK_INTERRUPTIBLE);
  3266. spin_unlock_irq(&x->wait.lock);
  3267. timeout = schedule_timeout(timeout);
  3268. spin_lock_irq(&x->wait.lock);
  3269. if (!timeout) {
  3270. __remove_wait_queue(&x->wait, &wait);
  3271. goto out;
  3272. }
  3273. } while (!x->done);
  3274. __remove_wait_queue(&x->wait, &wait);
  3275. }
  3276. x->done--;
  3277. out:
  3278. spin_unlock_irq(&x->wait.lock);
  3279. return timeout;
  3280. }
  3281. EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  3282. static inline void
  3283. sleep_on_head(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
  3284. {
  3285. spin_lock_irqsave(&q->lock, *flags);
  3286. __add_wait_queue(q, wait);
  3287. spin_unlock(&q->lock);
  3288. }
  3289. static inline void
  3290. sleep_on_tail(wait_queue_head_t *q, wait_queue_t *wait, unsigned long *flags)
  3291. {
  3292. spin_lock_irq(&q->lock);
  3293. __remove_wait_queue(q, wait);
  3294. spin_unlock_irqrestore(&q->lock, *flags);
  3295. }
  3296. void __sched interruptible_sleep_on(wait_queue_head_t *q)
  3297. {
  3298. unsigned long flags;
  3299. wait_queue_t wait;
  3300. init_waitqueue_entry(&wait, current);
  3301. current->state = TASK_INTERRUPTIBLE;
  3302. sleep_on_head(q, &wait, &flags);
  3303. schedule();
  3304. sleep_on_tail(q, &wait, &flags);
  3305. }
  3306. EXPORT_SYMBOL(interruptible_sleep_on);
  3307. long __sched
  3308. interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
  3309. {
  3310. unsigned long flags;
  3311. wait_queue_t wait;
  3312. init_waitqueue_entry(&wait, current);
  3313. current->state = TASK_INTERRUPTIBLE;
  3314. sleep_on_head(q, &wait, &flags);
  3315. timeout = schedule_timeout(timeout);
  3316. sleep_on_tail(q, &wait, &flags);
  3317. return timeout;
  3318. }
  3319. EXPORT_SYMBOL(interruptible_sleep_on_timeout);
  3320. void __sched sleep_on(wait_queue_head_t *q)
  3321. {
  3322. unsigned long flags;
  3323. wait_queue_t wait;
  3324. init_waitqueue_entry(&wait, current);
  3325. current->state = TASK_UNINTERRUPTIBLE;
  3326. sleep_on_head(q, &wait, &flags);
  3327. schedule();
  3328. sleep_on_tail(q, &wait, &flags);
  3329. }
  3330. EXPORT_SYMBOL(sleep_on);
  3331. long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
  3332. {
  3333. unsigned long flags;
  3334. wait_queue_t wait;
  3335. init_waitqueue_entry(&wait, current);
  3336. current->state = TASK_UNINTERRUPTIBLE;
  3337. sleep_on_head(q, &wait, &flags);
  3338. timeout = schedule_timeout(timeout);
  3339. sleep_on_tail(q, &wait, &flags);
  3340. return timeout;
  3341. }
  3342. EXPORT_SYMBOL(sleep_on_timeout);
  3343. #ifdef CONFIG_RT_MUTEXES
  3344. /*
  3345. * rt_mutex_setprio - set the current priority of a task
  3346. * @p: task
  3347. * @prio: prio value (kernel-internal form)
  3348. *
  3349. * This function changes the 'effective' priority of a task. It does
  3350. * not touch ->normal_prio like __setscheduler().
  3351. *
  3352. * Used by the rt_mutex code to implement priority inheritance logic.
  3353. */
  3354. void rt_mutex_setprio(struct task_struct *p, int prio)
  3355. {
  3356. unsigned long flags;
  3357. int oldprio, on_rq;
  3358. struct rq *rq;
  3359. u64 now;
  3360. BUG_ON(prio < 0 || prio > MAX_PRIO);
  3361. rq = task_rq_lock(p, &flags);
  3362. now = rq_clock(rq);
  3363. oldprio = p->prio;
  3364. on_rq = p->se.on_rq;
  3365. if (on_rq)
  3366. dequeue_task(rq, p, 0, now);
  3367. if (rt_prio(prio))
  3368. p->sched_class = &rt_sched_class;
  3369. else
  3370. p->sched_class = &fair_sched_class;
  3371. p->prio = prio;
  3372. if (on_rq) {
  3373. enqueue_task(rq, p, 0, now);
  3374. /*
  3375. * Reschedule if we are currently running on this runqueue and
  3376. * our priority decreased, or if we are not currently running on
  3377. * this runqueue and our priority is higher than the current's
  3378. */
  3379. if (task_running(rq, p)) {
  3380. if (p->prio > oldprio)
  3381. resched_task(rq->curr);
  3382. } else {
  3383. check_preempt_curr(rq, p);
  3384. }
  3385. }
  3386. task_rq_unlock(rq, &flags);
  3387. }
  3388. #endif
  3389. void set_user_nice(struct task_struct *p, long nice)
  3390. {
  3391. int old_prio, delta, on_rq;
  3392. unsigned long flags;
  3393. struct rq *rq;
  3394. u64 now;
  3395. if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
  3396. return;
  3397. /*
  3398. * We have to be careful, if called from sys_setpriority(),
  3399. * the task might be in the middle of scheduling on another CPU.
  3400. */
  3401. rq = task_rq_lock(p, &flags);
  3402. now = rq_clock(rq);
  3403. /*
  3404. * The RT priorities are set via sched_setscheduler(), but we still
  3405. * allow the 'normal' nice value to be set - but as expected
  3406. * it wont have any effect on scheduling until the task is
  3407. * SCHED_FIFO/SCHED_RR:
  3408. */
  3409. if (task_has_rt_policy(p)) {
  3410. p->static_prio = NICE_TO_PRIO(nice);
  3411. goto out_unlock;
  3412. }
  3413. on_rq = p->se.on_rq;
  3414. if (on_rq) {
  3415. dequeue_task(rq, p, 0, now);
  3416. dec_load(rq, p, now);
  3417. }
  3418. p->static_prio = NICE_TO_PRIO(nice);
  3419. set_load_weight(p);
  3420. old_prio = p->prio;
  3421. p->prio = effective_prio(p);
  3422. delta = p->prio - old_prio;
  3423. if (on_rq) {
  3424. enqueue_task(rq, p, 0, now);
  3425. inc_load(rq, p, now);
  3426. /*
  3427. * If the task increased its priority or is running and
  3428. * lowered its priority, then reschedule its CPU:
  3429. */
  3430. if (delta < 0 || (delta > 0 && task_running(rq, p)))
  3431. resched_task(rq->curr);
  3432. }
  3433. out_unlock:
  3434. task_rq_unlock(rq, &flags);
  3435. }
  3436. EXPORT_SYMBOL(set_user_nice);
  3437. /*
  3438. * can_nice - check if a task can reduce its nice value
  3439. * @p: task
  3440. * @nice: nice value
  3441. */
  3442. int can_nice(const struct task_struct *p, const int nice)
  3443. {
  3444. /* convert nice value [19,-20] to rlimit style value [1,40] */
  3445. int nice_rlim = 20 - nice;
  3446. return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
  3447. capable(CAP_SYS_NICE));
  3448. }
  3449. #ifdef __ARCH_WANT_SYS_NICE
  3450. /*
  3451. * sys_nice - change the priority of the current process.
  3452. * @increment: priority increment
  3453. *
  3454. * sys_setpriority is a more generic, but much slower function that
  3455. * does similar things.
  3456. */
  3457. asmlinkage long sys_nice(int increment)
  3458. {
  3459. long nice, retval;
  3460. /*
  3461. * Setpriority might change our priority at the same moment.
  3462. * We don't have to worry. Conceptually one call occurs first
  3463. * and we have a single winner.
  3464. */
  3465. if (increment < -40)
  3466. increment = -40;
  3467. if (increment > 40)
  3468. increment = 40;
  3469. nice = PRIO_TO_NICE(current->static_prio) + increment;
  3470. if (nice < -20)
  3471. nice = -20;
  3472. if (nice > 19)
  3473. nice = 19;
  3474. if (increment < 0 && !can_nice(current, nice))
  3475. return -EPERM;
  3476. retval = security_task_setnice(current, nice);
  3477. if (retval)
  3478. return retval;
  3479. set_user_nice(current, nice);
  3480. return 0;
  3481. }
  3482. #endif
  3483. /**
  3484. * task_prio - return the priority value of a given task.
  3485. * @p: the task in question.
  3486. *
  3487. * This is the priority value as seen by users in /proc.
  3488. * RT tasks are offset by -200. Normal tasks are centered
  3489. * around 0, value goes from -16 to +15.
  3490. */
  3491. int task_prio(const struct task_struct *p)
  3492. {
  3493. return p->prio - MAX_RT_PRIO;
  3494. }
  3495. /**
  3496. * task_nice - return the nice value of a given task.
  3497. * @p: the task in question.
  3498. */
  3499. int task_nice(const struct task_struct *p)
  3500. {
  3501. return TASK_NICE(p);
  3502. }
  3503. EXPORT_SYMBOL_GPL(task_nice);
  3504. /**
  3505. * idle_cpu - is a given cpu idle currently?
  3506. * @cpu: the processor in question.
  3507. */
  3508. int idle_cpu(int cpu)
  3509. {
  3510. return cpu_curr(cpu) == cpu_rq(cpu)->idle;
  3511. }
  3512. /**
  3513. * idle_task - return the idle task for a given cpu.
  3514. * @cpu: the processor in question.
  3515. */
  3516. struct task_struct *idle_task(int cpu)
  3517. {
  3518. return cpu_rq(cpu)->idle;
  3519. }
  3520. /**
  3521. * find_process_by_pid - find a process with a matching PID value.
  3522. * @pid: the pid in question.
  3523. */
  3524. static inline struct task_struct *find_process_by_pid(pid_t pid)
  3525. {
  3526. return pid ? find_task_by_pid(pid) : current;
  3527. }
  3528. /* Actually do priority change: must hold rq lock. */
  3529. static void
  3530. __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
  3531. {
  3532. BUG_ON(p->se.on_rq);
  3533. p->policy = policy;
  3534. switch (p->policy) {
  3535. case SCHED_NORMAL:
  3536. case SCHED_BATCH:
  3537. case SCHED_IDLE:
  3538. p->sched_class = &fair_sched_class;
  3539. break;
  3540. case SCHED_FIFO:
  3541. case SCHED_RR:
  3542. p->sched_class = &rt_sched_class;
  3543. break;
  3544. }
  3545. p->rt_priority = prio;
  3546. p->normal_prio = normal_prio(p);
  3547. /* we are holding p->pi_lock already */
  3548. p->prio = rt_mutex_getprio(p);
  3549. set_load_weight(p);
  3550. }
  3551. /**
  3552. * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
  3553. * @p: the task in question.
  3554. * @policy: new policy.
  3555. * @param: structure containing the new RT priority.
  3556. *
  3557. * NOTE that the task may be already dead.
  3558. */
  3559. int sched_setscheduler(struct task_struct *p, int policy,
  3560. struct sched_param *param)
  3561. {
  3562. int retval, oldprio, oldpolicy = -1, on_rq;
  3563. unsigned long flags;
  3564. struct rq *rq;
  3565. /* may grab non-irq protected spin_locks */
  3566. BUG_ON(in_interrupt());
  3567. recheck:
  3568. /* double check policy once rq lock held */
  3569. if (policy < 0)
  3570. policy = oldpolicy = p->policy;
  3571. else if (policy != SCHED_FIFO && policy != SCHED_RR &&
  3572. policy != SCHED_NORMAL && policy != SCHED_BATCH &&
  3573. policy != SCHED_IDLE)
  3574. return -EINVAL;
  3575. /*
  3576. * Valid priorities for SCHED_FIFO and SCHED_RR are
  3577. * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
  3578. * SCHED_BATCH and SCHED_IDLE is 0.
  3579. */
  3580. if (param->sched_priority < 0 ||
  3581. (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
  3582. (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
  3583. return -EINVAL;
  3584. if (rt_policy(policy) != (param->sched_priority != 0))
  3585. return -EINVAL;
  3586. /*
  3587. * Allow unprivileged RT tasks to decrease priority:
  3588. */
  3589. if (!capable(CAP_SYS_NICE)) {
  3590. if (rt_policy(policy)) {
  3591. unsigned long rlim_rtprio;
  3592. if (!lock_task_sighand(p, &flags))
  3593. return -ESRCH;
  3594. rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
  3595. unlock_task_sighand(p, &flags);
  3596. /* can't set/change the rt policy */
  3597. if (policy != p->policy && !rlim_rtprio)
  3598. return -EPERM;
  3599. /* can't increase priority */
  3600. if (param->sched_priority > p->rt_priority &&
  3601. param->sched_priority > rlim_rtprio)
  3602. return -EPERM;
  3603. }
  3604. /*
  3605. * Like positive nice levels, dont allow tasks to
  3606. * move out of SCHED_IDLE either:
  3607. */
  3608. if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
  3609. return -EPERM;
  3610. /* can't change other user's priorities */
  3611. if ((current->euid != p->euid) &&
  3612. (current->euid != p->uid))
  3613. return -EPERM;
  3614. }
  3615. retval = security_task_setscheduler(p, policy, param);
  3616. if (retval)
  3617. return retval;
  3618. /*
  3619. * make sure no PI-waiters arrive (or leave) while we are
  3620. * changing the priority of the task:
  3621. */
  3622. spin_lock_irqsave(&p->pi_lock, flags);
  3623. /*
  3624. * To be able to change p->policy safely, the apropriate
  3625. * runqueue lock must be held.
  3626. */
  3627. rq = __task_rq_lock(p);
  3628. /* recheck policy now with rq lock held */
  3629. if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  3630. policy = oldpolicy = -1;
  3631. __task_rq_unlock(rq);
  3632. spin_unlock_irqrestore(&p->pi_lock, flags);
  3633. goto recheck;
  3634. }
  3635. on_rq = p->se.on_rq;
  3636. if (on_rq)
  3637. deactivate_task(rq, p, 0);
  3638. oldprio = p->prio;
  3639. __setscheduler(rq, p, policy, param->sched_priority);
  3640. if (on_rq) {
  3641. activate_task(rq, p, 0);
  3642. /*
  3643. * Reschedule if we are currently running on this runqueue and
  3644. * our priority decreased, or if we are not currently running on
  3645. * this runqueue and our priority is higher than the current's
  3646. */
  3647. if (task_running(rq, p)) {
  3648. if (p->prio > oldprio)
  3649. resched_task(rq->curr);
  3650. } else {
  3651. check_preempt_curr(rq, p);
  3652. }
  3653. }
  3654. __task_rq_unlock(rq);
  3655. spin_unlock_irqrestore(&p->pi_lock, flags);
  3656. rt_mutex_adjust_pi(p);
  3657. return 0;
  3658. }
  3659. EXPORT_SYMBOL_GPL(sched_setscheduler);
  3660. static int
  3661. do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  3662. {
  3663. struct sched_param lparam;
  3664. struct task_struct *p;
  3665. int retval;
  3666. if (!param || pid < 0)
  3667. return -EINVAL;
  3668. if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  3669. return -EFAULT;
  3670. rcu_read_lock();
  3671. retval = -ESRCH;
  3672. p = find_process_by_pid(pid);
  3673. if (p != NULL)
  3674. retval = sched_setscheduler(p, policy, &lparam);
  3675. rcu_read_unlock();
  3676. return retval;
  3677. }
  3678. /**
  3679. * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  3680. * @pid: the pid in question.
  3681. * @policy: new policy.
  3682. * @param: structure containing the new RT priority.
  3683. */
  3684. asmlinkage long sys_sched_setscheduler(pid_t pid, int policy,
  3685. struct sched_param __user *param)
  3686. {
  3687. /* negative values for policy are not valid */
  3688. if (policy < 0)
  3689. return -EINVAL;
  3690. return do_sched_setscheduler(pid, policy, param);
  3691. }
  3692. /**
  3693. * sys_sched_setparam - set/change the RT priority of a thread
  3694. * @pid: the pid in question.
  3695. * @param: structure containing the new RT priority.
  3696. */
  3697. asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
  3698. {
  3699. return do_sched_setscheduler(pid, -1, param);
  3700. }
  3701. /**
  3702. * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  3703. * @pid: the pid in question.
  3704. */
  3705. asmlinkage long sys_sched_getscheduler(pid_t pid)
  3706. {
  3707. struct task_struct *p;
  3708. int retval = -EINVAL;
  3709. if (pid < 0)
  3710. goto out_nounlock;
  3711. retval = -ESRCH;
  3712. read_lock(&tasklist_lock);
  3713. p = find_process_by_pid(pid);
  3714. if (p) {
  3715. retval = security_task_getscheduler(p);
  3716. if (!retval)
  3717. retval = p->policy;
  3718. }
  3719. read_unlock(&tasklist_lock);
  3720. out_nounlock:
  3721. return retval;
  3722. }
  3723. /**
  3724. * sys_sched_getscheduler - get the RT priority of a thread
  3725. * @pid: the pid in question.
  3726. * @param: structure containing the RT priority.
  3727. */
  3728. asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
  3729. {
  3730. struct sched_param lp;
  3731. struct task_struct *p;
  3732. int retval = -EINVAL;
  3733. if (!param || pid < 0)
  3734. goto out_nounlock;
  3735. read_lock(&tasklist_lock);
  3736. p = find_process_by_pid(pid);
  3737. retval = -ESRCH;
  3738. if (!p)
  3739. goto out_unlock;
  3740. retval = security_task_getscheduler(p);
  3741. if (retval)
  3742. goto out_unlock;
  3743. lp.sched_priority = p->rt_priority;
  3744. read_unlock(&tasklist_lock);
  3745. /*
  3746. * This one might sleep, we cannot do it with a spinlock held ...
  3747. */
  3748. retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  3749. out_nounlock:
  3750. return retval;
  3751. out_unlock:
  3752. read_unlock(&tasklist_lock);
  3753. return retval;
  3754. }
  3755. long sched_setaffinity(pid_t pid, cpumask_t new_mask)
  3756. {
  3757. cpumask_t cpus_allowed;
  3758. struct task_struct *p;
  3759. int retval;
  3760. mutex_lock(&sched_hotcpu_mutex);
  3761. read_lock(&tasklist_lock);
  3762. p = find_process_by_pid(pid);
  3763. if (!p) {
  3764. read_unlock(&tasklist_lock);
  3765. mutex_unlock(&sched_hotcpu_mutex);
  3766. return -ESRCH;
  3767. }
  3768. /*
  3769. * It is not safe to call set_cpus_allowed with the
  3770. * tasklist_lock held. We will bump the task_struct's
  3771. * usage count and then drop tasklist_lock.
  3772. */
  3773. get_task_struct(p);
  3774. read_unlock(&tasklist_lock);
  3775. retval = -EPERM;
  3776. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  3777. !capable(CAP_SYS_NICE))
  3778. goto out_unlock;
  3779. retval = security_task_setscheduler(p, 0, NULL);
  3780. if (retval)
  3781. goto out_unlock;
  3782. cpus_allowed = cpuset_cpus_allowed(p);
  3783. cpus_and(new_mask, new_mask, cpus_allowed);
  3784. retval = set_cpus_allowed(p, new_mask);
  3785. out_unlock:
  3786. put_task_struct(p);
  3787. mutex_unlock(&sched_hotcpu_mutex);
  3788. return retval;
  3789. }
  3790. static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  3791. cpumask_t *new_mask)
  3792. {
  3793. if (len < sizeof(cpumask_t)) {
  3794. memset(new_mask, 0, sizeof(cpumask_t));
  3795. } else if (len > sizeof(cpumask_t)) {
  3796. len = sizeof(cpumask_t);
  3797. }
  3798. return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  3799. }
  3800. /**
  3801. * sys_sched_setaffinity - set the cpu affinity of a process
  3802. * @pid: pid of the process
  3803. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3804. * @user_mask_ptr: user-space pointer to the new cpu mask
  3805. */
  3806. asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
  3807. unsigned long __user *user_mask_ptr)
  3808. {
  3809. cpumask_t new_mask;
  3810. int retval;
  3811. retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
  3812. if (retval)
  3813. return retval;
  3814. return sched_setaffinity(pid, new_mask);
  3815. }
  3816. /*
  3817. * Represents all cpu's present in the system
  3818. * In systems capable of hotplug, this map could dynamically grow
  3819. * as new cpu's are detected in the system via any platform specific
  3820. * method, such as ACPI for e.g.
  3821. */
  3822. cpumask_t cpu_present_map __read_mostly;
  3823. EXPORT_SYMBOL(cpu_present_map);
  3824. #ifndef CONFIG_SMP
  3825. cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
  3826. EXPORT_SYMBOL(cpu_online_map);
  3827. cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
  3828. EXPORT_SYMBOL(cpu_possible_map);
  3829. #endif
  3830. long sched_getaffinity(pid_t pid, cpumask_t *mask)
  3831. {
  3832. struct task_struct *p;
  3833. int retval;
  3834. mutex_lock(&sched_hotcpu_mutex);
  3835. read_lock(&tasklist_lock);
  3836. retval = -ESRCH;
  3837. p = find_process_by_pid(pid);
  3838. if (!p)
  3839. goto out_unlock;
  3840. retval = security_task_getscheduler(p);
  3841. if (retval)
  3842. goto out_unlock;
  3843. cpus_and(*mask, p->cpus_allowed, cpu_online_map);
  3844. out_unlock:
  3845. read_unlock(&tasklist_lock);
  3846. mutex_unlock(&sched_hotcpu_mutex);
  3847. if (retval)
  3848. return retval;
  3849. return 0;
  3850. }
  3851. /**
  3852. * sys_sched_getaffinity - get the cpu affinity of a process
  3853. * @pid: pid of the process
  3854. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3855. * @user_mask_ptr: user-space pointer to hold the current cpu mask
  3856. */
  3857. asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
  3858. unsigned long __user *user_mask_ptr)
  3859. {
  3860. int ret;
  3861. cpumask_t mask;
  3862. if (len < sizeof(cpumask_t))
  3863. return -EINVAL;
  3864. ret = sched_getaffinity(pid, &mask);
  3865. if (ret < 0)
  3866. return ret;
  3867. if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
  3868. return -EFAULT;
  3869. return sizeof(cpumask_t);
  3870. }
  3871. /**
  3872. * sys_sched_yield - yield the current processor to other threads.
  3873. *
  3874. * This function yields the current CPU to other tasks. If there are no
  3875. * other threads running on this CPU then this function will return.
  3876. */
  3877. asmlinkage long sys_sched_yield(void)
  3878. {
  3879. struct rq *rq = this_rq_lock();
  3880. schedstat_inc(rq, yld_cnt);
  3881. if (unlikely(rq->nr_running == 1))
  3882. schedstat_inc(rq, yld_act_empty);
  3883. else
  3884. current->sched_class->yield_task(rq, current);
  3885. /*
  3886. * Since we are going to call schedule() anyway, there's
  3887. * no need to preempt or enable interrupts:
  3888. */
  3889. __release(rq->lock);
  3890. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  3891. _raw_spin_unlock(&rq->lock);
  3892. preempt_enable_no_resched();
  3893. schedule();
  3894. return 0;
  3895. }
  3896. static void __cond_resched(void)
  3897. {
  3898. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  3899. __might_sleep(__FILE__, __LINE__);
  3900. #endif
  3901. /*
  3902. * The BKS might be reacquired before we have dropped
  3903. * PREEMPT_ACTIVE, which could trigger a second
  3904. * cond_resched() call.
  3905. */
  3906. do {
  3907. add_preempt_count(PREEMPT_ACTIVE);
  3908. schedule();
  3909. sub_preempt_count(PREEMPT_ACTIVE);
  3910. } while (need_resched());
  3911. }
  3912. int __sched cond_resched(void)
  3913. {
  3914. if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
  3915. system_state == SYSTEM_RUNNING) {
  3916. __cond_resched();
  3917. return 1;
  3918. }
  3919. return 0;
  3920. }
  3921. EXPORT_SYMBOL(cond_resched);
  3922. /*
  3923. * cond_resched_lock() - if a reschedule is pending, drop the given lock,
  3924. * call schedule, and on return reacquire the lock.
  3925. *
  3926. * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  3927. * operations here to prevent schedule() from being called twice (once via
  3928. * spin_unlock(), once by hand).
  3929. */
  3930. int cond_resched_lock(spinlock_t *lock)
  3931. {
  3932. int ret = 0;
  3933. if (need_lockbreak(lock)) {
  3934. spin_unlock(lock);
  3935. cpu_relax();
  3936. ret = 1;
  3937. spin_lock(lock);
  3938. }
  3939. if (need_resched() && system_state == SYSTEM_RUNNING) {
  3940. spin_release(&lock->dep_map, 1, _THIS_IP_);
  3941. _raw_spin_unlock(lock);
  3942. preempt_enable_no_resched();
  3943. __cond_resched();
  3944. ret = 1;
  3945. spin_lock(lock);
  3946. }
  3947. return ret;
  3948. }
  3949. EXPORT_SYMBOL(cond_resched_lock);
  3950. int __sched cond_resched_softirq(void)
  3951. {
  3952. BUG_ON(!in_softirq());
  3953. if (need_resched() && system_state == SYSTEM_RUNNING) {
  3954. local_bh_enable();
  3955. __cond_resched();
  3956. local_bh_disable();
  3957. return 1;
  3958. }
  3959. return 0;
  3960. }
  3961. EXPORT_SYMBOL(cond_resched_softirq);
  3962. /**
  3963. * yield - yield the current processor to other threads.
  3964. *
  3965. * This is a shortcut for kernel-space yielding - it marks the
  3966. * thread runnable and calls sys_sched_yield().
  3967. */
  3968. void __sched yield(void)
  3969. {
  3970. set_current_state(TASK_RUNNING);
  3971. sys_sched_yield();
  3972. }
  3973. EXPORT_SYMBOL(yield);
  3974. /*
  3975. * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  3976. * that process accounting knows that this is a task in IO wait state.
  3977. *
  3978. * But don't do that if it is a deliberate, throttling IO wait (this task
  3979. * has set its backing_dev_info: the queue against which it should throttle)
  3980. */
  3981. void __sched io_schedule(void)
  3982. {
  3983. struct rq *rq = &__raw_get_cpu_var(runqueues);
  3984. delayacct_blkio_start();
  3985. atomic_inc(&rq->nr_iowait);
  3986. schedule();
  3987. atomic_dec(&rq->nr_iowait);
  3988. delayacct_blkio_end();
  3989. }
  3990. EXPORT_SYMBOL(io_schedule);
  3991. long __sched io_schedule_timeout(long timeout)
  3992. {
  3993. struct rq *rq = &__raw_get_cpu_var(runqueues);
  3994. long ret;
  3995. delayacct_blkio_start();
  3996. atomic_inc(&rq->nr_iowait);
  3997. ret = schedule_timeout(timeout);
  3998. atomic_dec(&rq->nr_iowait);
  3999. delayacct_blkio_end();
  4000. return ret;
  4001. }
  4002. /**
  4003. * sys_sched_get_priority_max - return maximum RT priority.
  4004. * @policy: scheduling class.
  4005. *
  4006. * this syscall returns the maximum rt_priority that can be used
  4007. * by a given scheduling class.
  4008. */
  4009. asmlinkage long sys_sched_get_priority_max(int policy)
  4010. {
  4011. int ret = -EINVAL;
  4012. switch (policy) {
  4013. case SCHED_FIFO:
  4014. case SCHED_RR:
  4015. ret = MAX_USER_RT_PRIO-1;
  4016. break;
  4017. case SCHED_NORMAL:
  4018. case SCHED_BATCH:
  4019. case SCHED_IDLE:
  4020. ret = 0;
  4021. break;
  4022. }
  4023. return ret;
  4024. }
  4025. /**
  4026. * sys_sched_get_priority_min - return minimum RT priority.
  4027. * @policy: scheduling class.
  4028. *
  4029. * this syscall returns the minimum rt_priority that can be used
  4030. * by a given scheduling class.
  4031. */
  4032. asmlinkage long sys_sched_get_priority_min(int policy)
  4033. {
  4034. int ret = -EINVAL;
  4035. switch (policy) {
  4036. case SCHED_FIFO:
  4037. case SCHED_RR:
  4038. ret = 1;
  4039. break;
  4040. case SCHED_NORMAL:
  4041. case SCHED_BATCH:
  4042. case SCHED_IDLE:
  4043. ret = 0;
  4044. }
  4045. return ret;
  4046. }
  4047. /**
  4048. * sys_sched_rr_get_interval - return the default timeslice of a process.
  4049. * @pid: pid of the process.
  4050. * @interval: userspace pointer to the timeslice value.
  4051. *
  4052. * this syscall writes the default timeslice value of a given process
  4053. * into the user-space timespec buffer. A value of '0' means infinity.
  4054. */
  4055. asmlinkage
  4056. long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
  4057. {
  4058. struct task_struct *p;
  4059. int retval = -EINVAL;
  4060. struct timespec t;
  4061. if (pid < 0)
  4062. goto out_nounlock;
  4063. retval = -ESRCH;
  4064. read_lock(&tasklist_lock);
  4065. p = find_process_by_pid(pid);
  4066. if (!p)
  4067. goto out_unlock;
  4068. retval = security_task_getscheduler(p);
  4069. if (retval)
  4070. goto out_unlock;
  4071. jiffies_to_timespec(p->policy == SCHED_FIFO ?
  4072. 0 : static_prio_timeslice(p->static_prio), &t);
  4073. read_unlock(&tasklist_lock);
  4074. retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  4075. out_nounlock:
  4076. return retval;
  4077. out_unlock:
  4078. read_unlock(&tasklist_lock);
  4079. return retval;
  4080. }
  4081. static const char stat_nam[] = "RSDTtZX";
  4082. static void show_task(struct task_struct *p)
  4083. {
  4084. unsigned long free = 0;
  4085. unsigned state;
  4086. state = p->state ? __ffs(p->state) + 1 : 0;
  4087. printk("%-13.13s %c", p->comm,
  4088. state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
  4089. #if BITS_PER_LONG == 32
  4090. if (state == TASK_RUNNING)
  4091. printk(" running ");
  4092. else
  4093. printk(" %08lx ", thread_saved_pc(p));
  4094. #else
  4095. if (state == TASK_RUNNING)
  4096. printk(" running task ");
  4097. else
  4098. printk(" %016lx ", thread_saved_pc(p));
  4099. #endif
  4100. #ifdef CONFIG_DEBUG_STACK_USAGE
  4101. {
  4102. unsigned long *n = end_of_stack(p);
  4103. while (!*n)
  4104. n++;
  4105. free = (unsigned long)n - (unsigned long)end_of_stack(p);
  4106. }
  4107. #endif
  4108. printk("%5lu %5d %6d\n", free, p->pid, p->parent->pid);
  4109. if (state != TASK_RUNNING)
  4110. show_stack(p, NULL);
  4111. }
  4112. void show_state_filter(unsigned long state_filter)
  4113. {
  4114. struct task_struct *g, *p;
  4115. #if BITS_PER_LONG == 32
  4116. printk(KERN_INFO
  4117. " task PC stack pid father\n");
  4118. #else
  4119. printk(KERN_INFO
  4120. " task PC stack pid father\n");
  4121. #endif
  4122. read_lock(&tasklist_lock);
  4123. do_each_thread(g, p) {
  4124. /*
  4125. * reset the NMI-timeout, listing all files on a slow
  4126. * console might take alot of time:
  4127. */
  4128. touch_nmi_watchdog();
  4129. if (!state_filter || (p->state & state_filter))
  4130. show_task(p);
  4131. } while_each_thread(g, p);
  4132. touch_all_softlockup_watchdogs();
  4133. #ifdef CONFIG_SCHED_DEBUG
  4134. sysrq_sched_debug_show();
  4135. #endif
  4136. read_unlock(&tasklist_lock);
  4137. /*
  4138. * Only show locks if all tasks are dumped:
  4139. */
  4140. if (state_filter == -1)
  4141. debug_show_all_locks();
  4142. }
  4143. void __cpuinit init_idle_bootup_task(struct task_struct *idle)
  4144. {
  4145. idle->sched_class = &idle_sched_class;
  4146. }
  4147. /**
  4148. * init_idle - set up an idle thread for a given CPU
  4149. * @idle: task in question
  4150. * @cpu: cpu the idle task belongs to
  4151. *
  4152. * NOTE: this function does not set the idle thread's NEED_RESCHED
  4153. * flag, to make booting more robust.
  4154. */
  4155. void __cpuinit init_idle(struct task_struct *idle, int cpu)
  4156. {
  4157. struct rq *rq = cpu_rq(cpu);
  4158. unsigned long flags;
  4159. __sched_fork(idle);
  4160. idle->se.exec_start = sched_clock();
  4161. idle->prio = idle->normal_prio = MAX_PRIO;
  4162. idle->cpus_allowed = cpumask_of_cpu(cpu);
  4163. __set_task_cpu(idle, cpu);
  4164. spin_lock_irqsave(&rq->lock, flags);
  4165. rq->curr = rq->idle = idle;
  4166. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  4167. idle->oncpu = 1;
  4168. #endif
  4169. spin_unlock_irqrestore(&rq->lock, flags);
  4170. /* Set the preempt count _outside_ the spinlocks! */
  4171. #if defined(CONFIG_PREEMPT) && !defined(CONFIG_PREEMPT_BKL)
  4172. task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
  4173. #else
  4174. task_thread_info(idle)->preempt_count = 0;
  4175. #endif
  4176. /*
  4177. * The idle tasks have their own, simple scheduling class:
  4178. */
  4179. idle->sched_class = &idle_sched_class;
  4180. }
  4181. /*
  4182. * In a system that switches off the HZ timer nohz_cpu_mask
  4183. * indicates which cpus entered this state. This is used
  4184. * in the rcu update to wait only for active cpus. For system
  4185. * which do not switch off the HZ timer nohz_cpu_mask should
  4186. * always be CPU_MASK_NONE.
  4187. */
  4188. cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
  4189. /*
  4190. * Increase the granularity value when there are more CPUs,
  4191. * because with more CPUs the 'effective latency' as visible
  4192. * to users decreases. But the relationship is not linear,
  4193. * so pick a second-best guess by going with the log2 of the
  4194. * number of CPUs.
  4195. *
  4196. * This idea comes from the SD scheduler of Con Kolivas:
  4197. */
  4198. static inline void sched_init_granularity(void)
  4199. {
  4200. unsigned int factor = 1 + ilog2(num_online_cpus());
  4201. const unsigned long gran_limit = 100000000;
  4202. sysctl_sched_granularity *= factor;
  4203. if (sysctl_sched_granularity > gran_limit)
  4204. sysctl_sched_granularity = gran_limit;
  4205. sysctl_sched_runtime_limit = sysctl_sched_granularity * 4;
  4206. sysctl_sched_wakeup_granularity = sysctl_sched_granularity / 2;
  4207. }
  4208. #ifdef CONFIG_SMP
  4209. /*
  4210. * This is how migration works:
  4211. *
  4212. * 1) we queue a struct migration_req structure in the source CPU's
  4213. * runqueue and wake up that CPU's migration thread.
  4214. * 2) we down() the locked semaphore => thread blocks.
  4215. * 3) migration thread wakes up (implicitly it forces the migrated
  4216. * thread off the CPU)
  4217. * 4) it gets the migration request and checks whether the migrated
  4218. * task is still in the wrong runqueue.
  4219. * 5) if it's in the wrong runqueue then the migration thread removes
  4220. * it and puts it into the right queue.
  4221. * 6) migration thread up()s the semaphore.
  4222. * 7) we wake up and the migration is done.
  4223. */
  4224. /*
  4225. * Change a given task's CPU affinity. Migrate the thread to a
  4226. * proper CPU and schedule it away if the CPU it's executing on
  4227. * is removed from the allowed bitmask.
  4228. *
  4229. * NOTE: the caller must have a valid reference to the task, the
  4230. * task must not exit() & deallocate itself prematurely. The
  4231. * call is not atomic; no spinlocks may be held.
  4232. */
  4233. int set_cpus_allowed(struct task_struct *p, cpumask_t new_mask)
  4234. {
  4235. struct migration_req req;
  4236. unsigned long flags;
  4237. struct rq *rq;
  4238. int ret = 0;
  4239. rq = task_rq_lock(p, &flags);
  4240. if (!cpus_intersects(new_mask, cpu_online_map)) {
  4241. ret = -EINVAL;
  4242. goto out;
  4243. }
  4244. p->cpus_allowed = new_mask;
  4245. /* Can the task run on the task's current CPU? If so, we're done */
  4246. if (cpu_isset(task_cpu(p), new_mask))
  4247. goto out;
  4248. if (migrate_task(p, any_online_cpu(new_mask), &req)) {
  4249. /* Need help from migration thread: drop lock and wait. */
  4250. task_rq_unlock(rq, &flags);
  4251. wake_up_process(rq->migration_thread);
  4252. wait_for_completion(&req.done);
  4253. tlb_migrate_finish(p->mm);
  4254. return 0;
  4255. }
  4256. out:
  4257. task_rq_unlock(rq, &flags);
  4258. return ret;
  4259. }
  4260. EXPORT_SYMBOL_GPL(set_cpus_allowed);
  4261. /*
  4262. * Move (not current) task off this cpu, onto dest cpu. We're doing
  4263. * this because either it can't run here any more (set_cpus_allowed()
  4264. * away from this CPU, or CPU going down), or because we're
  4265. * attempting to rebalance this task on exec (sched_exec).
  4266. *
  4267. * So we race with normal scheduler movements, but that's OK, as long
  4268. * as the task is no longer on this CPU.
  4269. *
  4270. * Returns non-zero if task was successfully migrated.
  4271. */
  4272. static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
  4273. {
  4274. struct rq *rq_dest, *rq_src;
  4275. int ret = 0, on_rq;
  4276. if (unlikely(cpu_is_offline(dest_cpu)))
  4277. return ret;
  4278. rq_src = cpu_rq(src_cpu);
  4279. rq_dest = cpu_rq(dest_cpu);
  4280. double_rq_lock(rq_src, rq_dest);
  4281. /* Already moved. */
  4282. if (task_cpu(p) != src_cpu)
  4283. goto out;
  4284. /* Affinity changed (again). */
  4285. if (!cpu_isset(dest_cpu, p->cpus_allowed))
  4286. goto out;
  4287. on_rq = p->se.on_rq;
  4288. if (on_rq)
  4289. deactivate_task(rq_src, p, 0);
  4290. set_task_cpu(p, dest_cpu);
  4291. if (on_rq) {
  4292. activate_task(rq_dest, p, 0);
  4293. check_preempt_curr(rq_dest, p);
  4294. }
  4295. ret = 1;
  4296. out:
  4297. double_rq_unlock(rq_src, rq_dest);
  4298. return ret;
  4299. }
  4300. /*
  4301. * migration_thread - this is a highprio system thread that performs
  4302. * thread migration by bumping thread off CPU then 'pushing' onto
  4303. * another runqueue.
  4304. */
  4305. static int migration_thread(void *data)
  4306. {
  4307. int cpu = (long)data;
  4308. struct rq *rq;
  4309. rq = cpu_rq(cpu);
  4310. BUG_ON(rq->migration_thread != current);
  4311. set_current_state(TASK_INTERRUPTIBLE);
  4312. while (!kthread_should_stop()) {
  4313. struct migration_req *req;
  4314. struct list_head *head;
  4315. spin_lock_irq(&rq->lock);
  4316. if (cpu_is_offline(cpu)) {
  4317. spin_unlock_irq(&rq->lock);
  4318. goto wait_to_die;
  4319. }
  4320. if (rq->active_balance) {
  4321. active_load_balance(rq, cpu);
  4322. rq->active_balance = 0;
  4323. }
  4324. head = &rq->migration_queue;
  4325. if (list_empty(head)) {
  4326. spin_unlock_irq(&rq->lock);
  4327. schedule();
  4328. set_current_state(TASK_INTERRUPTIBLE);
  4329. continue;
  4330. }
  4331. req = list_entry(head->next, struct migration_req, list);
  4332. list_del_init(head->next);
  4333. spin_unlock(&rq->lock);
  4334. __migrate_task(req->task, cpu, req->dest_cpu);
  4335. local_irq_enable();
  4336. complete(&req->done);
  4337. }
  4338. __set_current_state(TASK_RUNNING);
  4339. return 0;
  4340. wait_to_die:
  4341. /* Wait for kthread_stop */
  4342. set_current_state(TASK_INTERRUPTIBLE);
  4343. while (!kthread_should_stop()) {
  4344. schedule();
  4345. set_current_state(TASK_INTERRUPTIBLE);
  4346. }
  4347. __set_current_state(TASK_RUNNING);
  4348. return 0;
  4349. }
  4350. #ifdef CONFIG_HOTPLUG_CPU
  4351. /*
  4352. * Figure out where task on dead CPU should go, use force if neccessary.
  4353. * NOTE: interrupts should be disabled by the caller
  4354. */
  4355. static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
  4356. {
  4357. unsigned long flags;
  4358. cpumask_t mask;
  4359. struct rq *rq;
  4360. int dest_cpu;
  4361. restart:
  4362. /* On same node? */
  4363. mask = node_to_cpumask(cpu_to_node(dead_cpu));
  4364. cpus_and(mask, mask, p->cpus_allowed);
  4365. dest_cpu = any_online_cpu(mask);
  4366. /* On any allowed CPU? */
  4367. if (dest_cpu == NR_CPUS)
  4368. dest_cpu = any_online_cpu(p->cpus_allowed);
  4369. /* No more Mr. Nice Guy. */
  4370. if (dest_cpu == NR_CPUS) {
  4371. rq = task_rq_lock(p, &flags);
  4372. cpus_setall(p->cpus_allowed);
  4373. dest_cpu = any_online_cpu(p->cpus_allowed);
  4374. task_rq_unlock(rq, &flags);
  4375. /*
  4376. * Don't tell them about moving exiting tasks or
  4377. * kernel threads (both mm NULL), since they never
  4378. * leave kernel.
  4379. */
  4380. if (p->mm && printk_ratelimit())
  4381. printk(KERN_INFO "process %d (%s) no "
  4382. "longer affine to cpu%d\n",
  4383. p->pid, p->comm, dead_cpu);
  4384. }
  4385. if (!__migrate_task(p, dead_cpu, dest_cpu))
  4386. goto restart;
  4387. }
  4388. /*
  4389. * While a dead CPU has no uninterruptible tasks queued at this point,
  4390. * it might still have a nonzero ->nr_uninterruptible counter, because
  4391. * for performance reasons the counter is not stricly tracking tasks to
  4392. * their home CPUs. So we just add the counter to another CPU's counter,
  4393. * to keep the global sum constant after CPU-down:
  4394. */
  4395. static void migrate_nr_uninterruptible(struct rq *rq_src)
  4396. {
  4397. struct rq *rq_dest = cpu_rq(any_online_cpu(CPU_MASK_ALL));
  4398. unsigned long flags;
  4399. local_irq_save(flags);
  4400. double_rq_lock(rq_src, rq_dest);
  4401. rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
  4402. rq_src->nr_uninterruptible = 0;
  4403. double_rq_unlock(rq_src, rq_dest);
  4404. local_irq_restore(flags);
  4405. }
  4406. /* Run through task list and migrate tasks from the dead cpu. */
  4407. static void migrate_live_tasks(int src_cpu)
  4408. {
  4409. struct task_struct *p, *t;
  4410. write_lock_irq(&tasklist_lock);
  4411. do_each_thread(t, p) {
  4412. if (p == current)
  4413. continue;
  4414. if (task_cpu(p) == src_cpu)
  4415. move_task_off_dead_cpu(src_cpu, p);
  4416. } while_each_thread(t, p);
  4417. write_unlock_irq(&tasklist_lock);
  4418. }
  4419. /*
  4420. * Schedules idle task to be the next runnable task on current CPU.
  4421. * It does so by boosting its priority to highest possible and adding it to
  4422. * the _front_ of the runqueue. Used by CPU offline code.
  4423. */
  4424. void sched_idle_next(void)
  4425. {
  4426. int this_cpu = smp_processor_id();
  4427. struct rq *rq = cpu_rq(this_cpu);
  4428. struct task_struct *p = rq->idle;
  4429. unsigned long flags;
  4430. /* cpu has to be offline */
  4431. BUG_ON(cpu_online(this_cpu));
  4432. /*
  4433. * Strictly not necessary since rest of the CPUs are stopped by now
  4434. * and interrupts disabled on the current cpu.
  4435. */
  4436. spin_lock_irqsave(&rq->lock, flags);
  4437. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  4438. /* Add idle task to the _front_ of its priority queue: */
  4439. activate_idle_task(p, rq);
  4440. spin_unlock_irqrestore(&rq->lock, flags);
  4441. }
  4442. /*
  4443. * Ensures that the idle task is using init_mm right before its cpu goes
  4444. * offline.
  4445. */
  4446. void idle_task_exit(void)
  4447. {
  4448. struct mm_struct *mm = current->active_mm;
  4449. BUG_ON(cpu_online(smp_processor_id()));
  4450. if (mm != &init_mm)
  4451. switch_mm(mm, &init_mm, current);
  4452. mmdrop(mm);
  4453. }
  4454. /* called under rq->lock with disabled interrupts */
  4455. static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
  4456. {
  4457. struct rq *rq = cpu_rq(dead_cpu);
  4458. /* Must be exiting, otherwise would be on tasklist. */
  4459. BUG_ON(p->exit_state != EXIT_ZOMBIE && p->exit_state != EXIT_DEAD);
  4460. /* Cannot have done final schedule yet: would have vanished. */
  4461. BUG_ON(p->state == TASK_DEAD);
  4462. get_task_struct(p);
  4463. /*
  4464. * Drop lock around migration; if someone else moves it,
  4465. * that's OK. No task can be added to this CPU, so iteration is
  4466. * fine.
  4467. * NOTE: interrupts should be left disabled --dev@
  4468. */
  4469. spin_unlock(&rq->lock);
  4470. move_task_off_dead_cpu(dead_cpu, p);
  4471. spin_lock(&rq->lock);
  4472. put_task_struct(p);
  4473. }
  4474. /* release_task() removes task from tasklist, so we won't find dead tasks. */
  4475. static void migrate_dead_tasks(unsigned int dead_cpu)
  4476. {
  4477. struct rq *rq = cpu_rq(dead_cpu);
  4478. struct task_struct *next;
  4479. for ( ; ; ) {
  4480. if (!rq->nr_running)
  4481. break;
  4482. next = pick_next_task(rq, rq->curr, rq_clock(rq));
  4483. if (!next)
  4484. break;
  4485. migrate_dead(dead_cpu, next);
  4486. }
  4487. }
  4488. #endif /* CONFIG_HOTPLUG_CPU */
  4489. /*
  4490. * migration_call - callback that gets triggered when a CPU is added.
  4491. * Here we can start up the necessary migration thread for the new CPU.
  4492. */
  4493. static int __cpuinit
  4494. migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  4495. {
  4496. struct task_struct *p;
  4497. int cpu = (long)hcpu;
  4498. unsigned long flags;
  4499. struct rq *rq;
  4500. switch (action) {
  4501. case CPU_LOCK_ACQUIRE:
  4502. mutex_lock(&sched_hotcpu_mutex);
  4503. break;
  4504. case CPU_UP_PREPARE:
  4505. case CPU_UP_PREPARE_FROZEN:
  4506. p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
  4507. if (IS_ERR(p))
  4508. return NOTIFY_BAD;
  4509. kthread_bind(p, cpu);
  4510. /* Must be high prio: stop_machine expects to yield to it. */
  4511. rq = task_rq_lock(p, &flags);
  4512. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  4513. task_rq_unlock(rq, &flags);
  4514. cpu_rq(cpu)->migration_thread = p;
  4515. break;
  4516. case CPU_ONLINE:
  4517. case CPU_ONLINE_FROZEN:
  4518. /* Strictly unneccessary, as first user will wake it. */
  4519. wake_up_process(cpu_rq(cpu)->migration_thread);
  4520. break;
  4521. #ifdef CONFIG_HOTPLUG_CPU
  4522. case CPU_UP_CANCELED:
  4523. case CPU_UP_CANCELED_FROZEN:
  4524. if (!cpu_rq(cpu)->migration_thread)
  4525. break;
  4526. /* Unbind it from offline cpu so it can run. Fall thru. */
  4527. kthread_bind(cpu_rq(cpu)->migration_thread,
  4528. any_online_cpu(cpu_online_map));
  4529. kthread_stop(cpu_rq(cpu)->migration_thread);
  4530. cpu_rq(cpu)->migration_thread = NULL;
  4531. break;
  4532. case CPU_DEAD:
  4533. case CPU_DEAD_FROZEN:
  4534. migrate_live_tasks(cpu);
  4535. rq = cpu_rq(cpu);
  4536. kthread_stop(rq->migration_thread);
  4537. rq->migration_thread = NULL;
  4538. /* Idle task back to normal (off runqueue, low prio) */
  4539. rq = task_rq_lock(rq->idle, &flags);
  4540. deactivate_task(rq, rq->idle, 0);
  4541. rq->idle->static_prio = MAX_PRIO;
  4542. __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
  4543. rq->idle->sched_class = &idle_sched_class;
  4544. migrate_dead_tasks(cpu);
  4545. task_rq_unlock(rq, &flags);
  4546. migrate_nr_uninterruptible(rq);
  4547. BUG_ON(rq->nr_running != 0);
  4548. /* No need to migrate the tasks: it was best-effort if
  4549. * they didn't take sched_hotcpu_mutex. Just wake up
  4550. * the requestors. */
  4551. spin_lock_irq(&rq->lock);
  4552. while (!list_empty(&rq->migration_queue)) {
  4553. struct migration_req *req;
  4554. req = list_entry(rq->migration_queue.next,
  4555. struct migration_req, list);
  4556. list_del_init(&req->list);
  4557. complete(&req->done);
  4558. }
  4559. spin_unlock_irq(&rq->lock);
  4560. break;
  4561. #endif
  4562. case CPU_LOCK_RELEASE:
  4563. mutex_unlock(&sched_hotcpu_mutex);
  4564. break;
  4565. }
  4566. return NOTIFY_OK;
  4567. }
  4568. /* Register at highest priority so that task migration (migrate_all_tasks)
  4569. * happens before everything else.
  4570. */
  4571. static struct notifier_block __cpuinitdata migration_notifier = {
  4572. .notifier_call = migration_call,
  4573. .priority = 10
  4574. };
  4575. int __init migration_init(void)
  4576. {
  4577. void *cpu = (void *)(long)smp_processor_id();
  4578. int err;
  4579. /* Start one for the boot CPU: */
  4580. err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  4581. BUG_ON(err == NOTIFY_BAD);
  4582. migration_call(&migration_notifier, CPU_ONLINE, cpu);
  4583. register_cpu_notifier(&migration_notifier);
  4584. return 0;
  4585. }
  4586. #endif
  4587. #ifdef CONFIG_SMP
  4588. /* Number of possible processor ids */
  4589. int nr_cpu_ids __read_mostly = NR_CPUS;
  4590. EXPORT_SYMBOL(nr_cpu_ids);
  4591. #undef SCHED_DOMAIN_DEBUG
  4592. #ifdef SCHED_DOMAIN_DEBUG
  4593. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  4594. {
  4595. int level = 0;
  4596. if (!sd) {
  4597. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  4598. return;
  4599. }
  4600. printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  4601. do {
  4602. int i;
  4603. char str[NR_CPUS];
  4604. struct sched_group *group = sd->groups;
  4605. cpumask_t groupmask;
  4606. cpumask_scnprintf(str, NR_CPUS, sd->span);
  4607. cpus_clear(groupmask);
  4608. printk(KERN_DEBUG);
  4609. for (i = 0; i < level + 1; i++)
  4610. printk(" ");
  4611. printk("domain %d: ", level);
  4612. if (!(sd->flags & SD_LOAD_BALANCE)) {
  4613. printk("does not load-balance\n");
  4614. if (sd->parent)
  4615. printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
  4616. " has parent");
  4617. break;
  4618. }
  4619. printk("span %s\n", str);
  4620. if (!cpu_isset(cpu, sd->span))
  4621. printk(KERN_ERR "ERROR: domain->span does not contain "
  4622. "CPU%d\n", cpu);
  4623. if (!cpu_isset(cpu, group->cpumask))
  4624. printk(KERN_ERR "ERROR: domain->groups does not contain"
  4625. " CPU%d\n", cpu);
  4626. printk(KERN_DEBUG);
  4627. for (i = 0; i < level + 2; i++)
  4628. printk(" ");
  4629. printk("groups:");
  4630. do {
  4631. if (!group) {
  4632. printk("\n");
  4633. printk(KERN_ERR "ERROR: group is NULL\n");
  4634. break;
  4635. }
  4636. if (!group->__cpu_power) {
  4637. printk("\n");
  4638. printk(KERN_ERR "ERROR: domain->cpu_power not "
  4639. "set\n");
  4640. }
  4641. if (!cpus_weight(group->cpumask)) {
  4642. printk("\n");
  4643. printk(KERN_ERR "ERROR: empty group\n");
  4644. }
  4645. if (cpus_intersects(groupmask, group->cpumask)) {
  4646. printk("\n");
  4647. printk(KERN_ERR "ERROR: repeated CPUs\n");
  4648. }
  4649. cpus_or(groupmask, groupmask, group->cpumask);
  4650. cpumask_scnprintf(str, NR_CPUS, group->cpumask);
  4651. printk(" %s", str);
  4652. group = group->next;
  4653. } while (group != sd->groups);
  4654. printk("\n");
  4655. if (!cpus_equal(sd->span, groupmask))
  4656. printk(KERN_ERR "ERROR: groups don't span "
  4657. "domain->span\n");
  4658. level++;
  4659. sd = sd->parent;
  4660. if (!sd)
  4661. continue;
  4662. if (!cpus_subset(groupmask, sd->span))
  4663. printk(KERN_ERR "ERROR: parent span is not a superset "
  4664. "of domain->span\n");
  4665. } while (sd);
  4666. }
  4667. #else
  4668. # define sched_domain_debug(sd, cpu) do { } while (0)
  4669. #endif
  4670. static int sd_degenerate(struct sched_domain *sd)
  4671. {
  4672. if (cpus_weight(sd->span) == 1)
  4673. return 1;
  4674. /* Following flags need at least 2 groups */
  4675. if (sd->flags & (SD_LOAD_BALANCE |
  4676. SD_BALANCE_NEWIDLE |
  4677. SD_BALANCE_FORK |
  4678. SD_BALANCE_EXEC |
  4679. SD_SHARE_CPUPOWER |
  4680. SD_SHARE_PKG_RESOURCES)) {
  4681. if (sd->groups != sd->groups->next)
  4682. return 0;
  4683. }
  4684. /* Following flags don't use groups */
  4685. if (sd->flags & (SD_WAKE_IDLE |
  4686. SD_WAKE_AFFINE |
  4687. SD_WAKE_BALANCE))
  4688. return 0;
  4689. return 1;
  4690. }
  4691. static int
  4692. sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  4693. {
  4694. unsigned long cflags = sd->flags, pflags = parent->flags;
  4695. if (sd_degenerate(parent))
  4696. return 1;
  4697. if (!cpus_equal(sd->span, parent->span))
  4698. return 0;
  4699. /* Does parent contain flags not in child? */
  4700. /* WAKE_BALANCE is a subset of WAKE_AFFINE */
  4701. if (cflags & SD_WAKE_AFFINE)
  4702. pflags &= ~SD_WAKE_BALANCE;
  4703. /* Flags needing groups don't count if only 1 group in parent */
  4704. if (parent->groups == parent->groups->next) {
  4705. pflags &= ~(SD_LOAD_BALANCE |
  4706. SD_BALANCE_NEWIDLE |
  4707. SD_BALANCE_FORK |
  4708. SD_BALANCE_EXEC |
  4709. SD_SHARE_CPUPOWER |
  4710. SD_SHARE_PKG_RESOURCES);
  4711. }
  4712. if (~cflags & pflags)
  4713. return 0;
  4714. return 1;
  4715. }
  4716. /*
  4717. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  4718. * hold the hotplug lock.
  4719. */
  4720. static void cpu_attach_domain(struct sched_domain *sd, int cpu)
  4721. {
  4722. struct rq *rq = cpu_rq(cpu);
  4723. struct sched_domain *tmp;
  4724. /* Remove the sched domains which do not contribute to scheduling. */
  4725. for (tmp = sd; tmp; tmp = tmp->parent) {
  4726. struct sched_domain *parent = tmp->parent;
  4727. if (!parent)
  4728. break;
  4729. if (sd_parent_degenerate(tmp, parent)) {
  4730. tmp->parent = parent->parent;
  4731. if (parent->parent)
  4732. parent->parent->child = tmp;
  4733. }
  4734. }
  4735. if (sd && sd_degenerate(sd)) {
  4736. sd = sd->parent;
  4737. if (sd)
  4738. sd->child = NULL;
  4739. }
  4740. sched_domain_debug(sd, cpu);
  4741. rcu_assign_pointer(rq->sd, sd);
  4742. }
  4743. /* cpus with isolated domains */
  4744. static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
  4745. /* Setup the mask of cpus configured for isolated domains */
  4746. static int __init isolated_cpu_setup(char *str)
  4747. {
  4748. int ints[NR_CPUS], i;
  4749. str = get_options(str, ARRAY_SIZE(ints), ints);
  4750. cpus_clear(cpu_isolated_map);
  4751. for (i = 1; i <= ints[0]; i++)
  4752. if (ints[i] < NR_CPUS)
  4753. cpu_set(ints[i], cpu_isolated_map);
  4754. return 1;
  4755. }
  4756. __setup ("isolcpus=", isolated_cpu_setup);
  4757. /*
  4758. * init_sched_build_groups takes the cpumask we wish to span, and a pointer
  4759. * to a function which identifies what group(along with sched group) a CPU
  4760. * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
  4761. * (due to the fact that we keep track of groups covered with a cpumask_t).
  4762. *
  4763. * init_sched_build_groups will build a circular linked list of the groups
  4764. * covered by the given span, and will set each group's ->cpumask correctly,
  4765. * and ->cpu_power to 0.
  4766. */
  4767. static void
  4768. init_sched_build_groups(cpumask_t span, const cpumask_t *cpu_map,
  4769. int (*group_fn)(int cpu, const cpumask_t *cpu_map,
  4770. struct sched_group **sg))
  4771. {
  4772. struct sched_group *first = NULL, *last = NULL;
  4773. cpumask_t covered = CPU_MASK_NONE;
  4774. int i;
  4775. for_each_cpu_mask(i, span) {
  4776. struct sched_group *sg;
  4777. int group = group_fn(i, cpu_map, &sg);
  4778. int j;
  4779. if (cpu_isset(i, covered))
  4780. continue;
  4781. sg->cpumask = CPU_MASK_NONE;
  4782. sg->__cpu_power = 0;
  4783. for_each_cpu_mask(j, span) {
  4784. if (group_fn(j, cpu_map, NULL) != group)
  4785. continue;
  4786. cpu_set(j, covered);
  4787. cpu_set(j, sg->cpumask);
  4788. }
  4789. if (!first)
  4790. first = sg;
  4791. if (last)
  4792. last->next = sg;
  4793. last = sg;
  4794. }
  4795. last->next = first;
  4796. }
  4797. #define SD_NODES_PER_DOMAIN 16
  4798. #ifdef CONFIG_NUMA
  4799. /**
  4800. * find_next_best_node - find the next node to include in a sched_domain
  4801. * @node: node whose sched_domain we're building
  4802. * @used_nodes: nodes already in the sched_domain
  4803. *
  4804. * Find the next node to include in a given scheduling domain. Simply
  4805. * finds the closest node not already in the @used_nodes map.
  4806. *
  4807. * Should use nodemask_t.
  4808. */
  4809. static int find_next_best_node(int node, unsigned long *used_nodes)
  4810. {
  4811. int i, n, val, min_val, best_node = 0;
  4812. min_val = INT_MAX;
  4813. for (i = 0; i < MAX_NUMNODES; i++) {
  4814. /* Start at @node */
  4815. n = (node + i) % MAX_NUMNODES;
  4816. if (!nr_cpus_node(n))
  4817. continue;
  4818. /* Skip already used nodes */
  4819. if (test_bit(n, used_nodes))
  4820. continue;
  4821. /* Simple min distance search */
  4822. val = node_distance(node, n);
  4823. if (val < min_val) {
  4824. min_val = val;
  4825. best_node = n;
  4826. }
  4827. }
  4828. set_bit(best_node, used_nodes);
  4829. return best_node;
  4830. }
  4831. /**
  4832. * sched_domain_node_span - get a cpumask for a node's sched_domain
  4833. * @node: node whose cpumask we're constructing
  4834. * @size: number of nodes to include in this span
  4835. *
  4836. * Given a node, construct a good cpumask for its sched_domain to span. It
  4837. * should be one that prevents unnecessary balancing, but also spreads tasks
  4838. * out optimally.
  4839. */
  4840. static cpumask_t sched_domain_node_span(int node)
  4841. {
  4842. DECLARE_BITMAP(used_nodes, MAX_NUMNODES);
  4843. cpumask_t span, nodemask;
  4844. int i;
  4845. cpus_clear(span);
  4846. bitmap_zero(used_nodes, MAX_NUMNODES);
  4847. nodemask = node_to_cpumask(node);
  4848. cpus_or(span, span, nodemask);
  4849. set_bit(node, used_nodes);
  4850. for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
  4851. int next_node = find_next_best_node(node, used_nodes);
  4852. nodemask = node_to_cpumask(next_node);
  4853. cpus_or(span, span, nodemask);
  4854. }
  4855. return span;
  4856. }
  4857. #endif
  4858. int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
  4859. /*
  4860. * SMT sched-domains:
  4861. */
  4862. #ifdef CONFIG_SCHED_SMT
  4863. static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
  4864. static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
  4865. static int cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map,
  4866. struct sched_group **sg)
  4867. {
  4868. if (sg)
  4869. *sg = &per_cpu(sched_group_cpus, cpu);
  4870. return cpu;
  4871. }
  4872. #endif
  4873. /*
  4874. * multi-core sched-domains:
  4875. */
  4876. #ifdef CONFIG_SCHED_MC
  4877. static DEFINE_PER_CPU(struct sched_domain, core_domains);
  4878. static DEFINE_PER_CPU(struct sched_group, sched_group_core);
  4879. #endif
  4880. #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
  4881. static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
  4882. struct sched_group **sg)
  4883. {
  4884. int group;
  4885. cpumask_t mask = cpu_sibling_map[cpu];
  4886. cpus_and(mask, mask, *cpu_map);
  4887. group = first_cpu(mask);
  4888. if (sg)
  4889. *sg = &per_cpu(sched_group_core, group);
  4890. return group;
  4891. }
  4892. #elif defined(CONFIG_SCHED_MC)
  4893. static int cpu_to_core_group(int cpu, const cpumask_t *cpu_map,
  4894. struct sched_group **sg)
  4895. {
  4896. if (sg)
  4897. *sg = &per_cpu(sched_group_core, cpu);
  4898. return cpu;
  4899. }
  4900. #endif
  4901. static DEFINE_PER_CPU(struct sched_domain, phys_domains);
  4902. static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
  4903. static int cpu_to_phys_group(int cpu, const cpumask_t *cpu_map,
  4904. struct sched_group **sg)
  4905. {
  4906. int group;
  4907. #ifdef CONFIG_SCHED_MC
  4908. cpumask_t mask = cpu_coregroup_map(cpu);
  4909. cpus_and(mask, mask, *cpu_map);
  4910. group = first_cpu(mask);
  4911. #elif defined(CONFIG_SCHED_SMT)
  4912. cpumask_t mask = cpu_sibling_map[cpu];
  4913. cpus_and(mask, mask, *cpu_map);
  4914. group = first_cpu(mask);
  4915. #else
  4916. group = cpu;
  4917. #endif
  4918. if (sg)
  4919. *sg = &per_cpu(sched_group_phys, group);
  4920. return group;
  4921. }
  4922. #ifdef CONFIG_NUMA
  4923. /*
  4924. * The init_sched_build_groups can't handle what we want to do with node
  4925. * groups, so roll our own. Now each node has its own list of groups which
  4926. * gets dynamically allocated.
  4927. */
  4928. static DEFINE_PER_CPU(struct sched_domain, node_domains);
  4929. static struct sched_group **sched_group_nodes_bycpu[NR_CPUS];
  4930. static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
  4931. static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
  4932. static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
  4933. struct sched_group **sg)
  4934. {
  4935. cpumask_t nodemask = node_to_cpumask(cpu_to_node(cpu));
  4936. int group;
  4937. cpus_and(nodemask, nodemask, *cpu_map);
  4938. group = first_cpu(nodemask);
  4939. if (sg)
  4940. *sg = &per_cpu(sched_group_allnodes, group);
  4941. return group;
  4942. }
  4943. static void init_numa_sched_groups_power(struct sched_group *group_head)
  4944. {
  4945. struct sched_group *sg = group_head;
  4946. int j;
  4947. if (!sg)
  4948. return;
  4949. next_sg:
  4950. for_each_cpu_mask(j, sg->cpumask) {
  4951. struct sched_domain *sd;
  4952. sd = &per_cpu(phys_domains, j);
  4953. if (j != first_cpu(sd->groups->cpumask)) {
  4954. /*
  4955. * Only add "power" once for each
  4956. * physical package.
  4957. */
  4958. continue;
  4959. }
  4960. sg_inc_cpu_power(sg, sd->groups->__cpu_power);
  4961. }
  4962. sg = sg->next;
  4963. if (sg != group_head)
  4964. goto next_sg;
  4965. }
  4966. #endif
  4967. #ifdef CONFIG_NUMA
  4968. /* Free memory allocated for various sched_group structures */
  4969. static void free_sched_groups(const cpumask_t *cpu_map)
  4970. {
  4971. int cpu, i;
  4972. for_each_cpu_mask(cpu, *cpu_map) {
  4973. struct sched_group **sched_group_nodes
  4974. = sched_group_nodes_bycpu[cpu];
  4975. if (!sched_group_nodes)
  4976. continue;
  4977. for (i = 0; i < MAX_NUMNODES; i++) {
  4978. cpumask_t nodemask = node_to_cpumask(i);
  4979. struct sched_group *oldsg, *sg = sched_group_nodes[i];
  4980. cpus_and(nodemask, nodemask, *cpu_map);
  4981. if (cpus_empty(nodemask))
  4982. continue;
  4983. if (sg == NULL)
  4984. continue;
  4985. sg = sg->next;
  4986. next_sg:
  4987. oldsg = sg;
  4988. sg = sg->next;
  4989. kfree(oldsg);
  4990. if (oldsg != sched_group_nodes[i])
  4991. goto next_sg;
  4992. }
  4993. kfree(sched_group_nodes);
  4994. sched_group_nodes_bycpu[cpu] = NULL;
  4995. }
  4996. }
  4997. #else
  4998. static void free_sched_groups(const cpumask_t *cpu_map)
  4999. {
  5000. }
  5001. #endif
  5002. /*
  5003. * Initialize sched groups cpu_power.
  5004. *
  5005. * cpu_power indicates the capacity of sched group, which is used while
  5006. * distributing the load between different sched groups in a sched domain.
  5007. * Typically cpu_power for all the groups in a sched domain will be same unless
  5008. * there are asymmetries in the topology. If there are asymmetries, group
  5009. * having more cpu_power will pickup more load compared to the group having
  5010. * less cpu_power.
  5011. *
  5012. * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
  5013. * the maximum number of tasks a group can handle in the presence of other idle
  5014. * or lightly loaded groups in the same sched domain.
  5015. */
  5016. static void init_sched_groups_power(int cpu, struct sched_domain *sd)
  5017. {
  5018. struct sched_domain *child;
  5019. struct sched_group *group;
  5020. WARN_ON(!sd || !sd->groups);
  5021. if (cpu != first_cpu(sd->groups->cpumask))
  5022. return;
  5023. child = sd->child;
  5024. sd->groups->__cpu_power = 0;
  5025. /*
  5026. * For perf policy, if the groups in child domain share resources
  5027. * (for example cores sharing some portions of the cache hierarchy
  5028. * or SMT), then set this domain groups cpu_power such that each group
  5029. * can handle only one task, when there are other idle groups in the
  5030. * same sched domain.
  5031. */
  5032. if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
  5033. (child->flags &
  5034. (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
  5035. sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
  5036. return;
  5037. }
  5038. /*
  5039. * add cpu_power of each child group to this groups cpu_power
  5040. */
  5041. group = child->groups;
  5042. do {
  5043. sg_inc_cpu_power(sd->groups, group->__cpu_power);
  5044. group = group->next;
  5045. } while (group != child->groups);
  5046. }
  5047. /*
  5048. * Build sched domains for a given set of cpus and attach the sched domains
  5049. * to the individual cpus
  5050. */
  5051. static int build_sched_domains(const cpumask_t *cpu_map)
  5052. {
  5053. int i;
  5054. #ifdef CONFIG_NUMA
  5055. struct sched_group **sched_group_nodes = NULL;
  5056. int sd_allnodes = 0;
  5057. /*
  5058. * Allocate the per-node list of sched groups
  5059. */
  5060. sched_group_nodes = kzalloc(sizeof(struct sched_group *)*MAX_NUMNODES,
  5061. GFP_KERNEL);
  5062. if (!sched_group_nodes) {
  5063. printk(KERN_WARNING "Can not alloc sched group node list\n");
  5064. return -ENOMEM;
  5065. }
  5066. sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
  5067. #endif
  5068. /*
  5069. * Set up domains for cpus specified by the cpu_map.
  5070. */
  5071. for_each_cpu_mask(i, *cpu_map) {
  5072. struct sched_domain *sd = NULL, *p;
  5073. cpumask_t nodemask = node_to_cpumask(cpu_to_node(i));
  5074. cpus_and(nodemask, nodemask, *cpu_map);
  5075. #ifdef CONFIG_NUMA
  5076. if (cpus_weight(*cpu_map) >
  5077. SD_NODES_PER_DOMAIN*cpus_weight(nodemask)) {
  5078. sd = &per_cpu(allnodes_domains, i);
  5079. *sd = SD_ALLNODES_INIT;
  5080. sd->span = *cpu_map;
  5081. cpu_to_allnodes_group(i, cpu_map, &sd->groups);
  5082. p = sd;
  5083. sd_allnodes = 1;
  5084. } else
  5085. p = NULL;
  5086. sd = &per_cpu(node_domains, i);
  5087. *sd = SD_NODE_INIT;
  5088. sd->span = sched_domain_node_span(cpu_to_node(i));
  5089. sd->parent = p;
  5090. if (p)
  5091. p->child = sd;
  5092. cpus_and(sd->span, sd->span, *cpu_map);
  5093. #endif
  5094. p = sd;
  5095. sd = &per_cpu(phys_domains, i);
  5096. *sd = SD_CPU_INIT;
  5097. sd->span = nodemask;
  5098. sd->parent = p;
  5099. if (p)
  5100. p->child = sd;
  5101. cpu_to_phys_group(i, cpu_map, &sd->groups);
  5102. #ifdef CONFIG_SCHED_MC
  5103. p = sd;
  5104. sd = &per_cpu(core_domains, i);
  5105. *sd = SD_MC_INIT;
  5106. sd->span = cpu_coregroup_map(i);
  5107. cpus_and(sd->span, sd->span, *cpu_map);
  5108. sd->parent = p;
  5109. p->child = sd;
  5110. cpu_to_core_group(i, cpu_map, &sd->groups);
  5111. #endif
  5112. #ifdef CONFIG_SCHED_SMT
  5113. p = sd;
  5114. sd = &per_cpu(cpu_domains, i);
  5115. *sd = SD_SIBLING_INIT;
  5116. sd->span = cpu_sibling_map[i];
  5117. cpus_and(sd->span, sd->span, *cpu_map);
  5118. sd->parent = p;
  5119. p->child = sd;
  5120. cpu_to_cpu_group(i, cpu_map, &sd->groups);
  5121. #endif
  5122. }
  5123. #ifdef CONFIG_SCHED_SMT
  5124. /* Set up CPU (sibling) groups */
  5125. for_each_cpu_mask(i, *cpu_map) {
  5126. cpumask_t this_sibling_map = cpu_sibling_map[i];
  5127. cpus_and(this_sibling_map, this_sibling_map, *cpu_map);
  5128. if (i != first_cpu(this_sibling_map))
  5129. continue;
  5130. init_sched_build_groups(this_sibling_map, cpu_map,
  5131. &cpu_to_cpu_group);
  5132. }
  5133. #endif
  5134. #ifdef CONFIG_SCHED_MC
  5135. /* Set up multi-core groups */
  5136. for_each_cpu_mask(i, *cpu_map) {
  5137. cpumask_t this_core_map = cpu_coregroup_map(i);
  5138. cpus_and(this_core_map, this_core_map, *cpu_map);
  5139. if (i != first_cpu(this_core_map))
  5140. continue;
  5141. init_sched_build_groups(this_core_map, cpu_map,
  5142. &cpu_to_core_group);
  5143. }
  5144. #endif
  5145. /* Set up physical groups */
  5146. for (i = 0; i < MAX_NUMNODES; i++) {
  5147. cpumask_t nodemask = node_to_cpumask(i);
  5148. cpus_and(nodemask, nodemask, *cpu_map);
  5149. if (cpus_empty(nodemask))
  5150. continue;
  5151. init_sched_build_groups(nodemask, cpu_map, &cpu_to_phys_group);
  5152. }
  5153. #ifdef CONFIG_NUMA
  5154. /* Set up node groups */
  5155. if (sd_allnodes)
  5156. init_sched_build_groups(*cpu_map, cpu_map,
  5157. &cpu_to_allnodes_group);
  5158. for (i = 0; i < MAX_NUMNODES; i++) {
  5159. /* Set up node groups */
  5160. struct sched_group *sg, *prev;
  5161. cpumask_t nodemask = node_to_cpumask(i);
  5162. cpumask_t domainspan;
  5163. cpumask_t covered = CPU_MASK_NONE;
  5164. int j;
  5165. cpus_and(nodemask, nodemask, *cpu_map);
  5166. if (cpus_empty(nodemask)) {
  5167. sched_group_nodes[i] = NULL;
  5168. continue;
  5169. }
  5170. domainspan = sched_domain_node_span(i);
  5171. cpus_and(domainspan, domainspan, *cpu_map);
  5172. sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
  5173. if (!sg) {
  5174. printk(KERN_WARNING "Can not alloc domain group for "
  5175. "node %d\n", i);
  5176. goto error;
  5177. }
  5178. sched_group_nodes[i] = sg;
  5179. for_each_cpu_mask(j, nodemask) {
  5180. struct sched_domain *sd;
  5181. sd = &per_cpu(node_domains, j);
  5182. sd->groups = sg;
  5183. }
  5184. sg->__cpu_power = 0;
  5185. sg->cpumask = nodemask;
  5186. sg->next = sg;
  5187. cpus_or(covered, covered, nodemask);
  5188. prev = sg;
  5189. for (j = 0; j < MAX_NUMNODES; j++) {
  5190. cpumask_t tmp, notcovered;
  5191. int n = (i + j) % MAX_NUMNODES;
  5192. cpus_complement(notcovered, covered);
  5193. cpus_and(tmp, notcovered, *cpu_map);
  5194. cpus_and(tmp, tmp, domainspan);
  5195. if (cpus_empty(tmp))
  5196. break;
  5197. nodemask = node_to_cpumask(n);
  5198. cpus_and(tmp, tmp, nodemask);
  5199. if (cpus_empty(tmp))
  5200. continue;
  5201. sg = kmalloc_node(sizeof(struct sched_group),
  5202. GFP_KERNEL, i);
  5203. if (!sg) {
  5204. printk(KERN_WARNING
  5205. "Can not alloc domain group for node %d\n", j);
  5206. goto error;
  5207. }
  5208. sg->__cpu_power = 0;
  5209. sg->cpumask = tmp;
  5210. sg->next = prev->next;
  5211. cpus_or(covered, covered, tmp);
  5212. prev->next = sg;
  5213. prev = sg;
  5214. }
  5215. }
  5216. #endif
  5217. /* Calculate CPU power for physical packages and nodes */
  5218. #ifdef CONFIG_SCHED_SMT
  5219. for_each_cpu_mask(i, *cpu_map) {
  5220. struct sched_domain *sd = &per_cpu(cpu_domains, i);
  5221. init_sched_groups_power(i, sd);
  5222. }
  5223. #endif
  5224. #ifdef CONFIG_SCHED_MC
  5225. for_each_cpu_mask(i, *cpu_map) {
  5226. struct sched_domain *sd = &per_cpu(core_domains, i);
  5227. init_sched_groups_power(i, sd);
  5228. }
  5229. #endif
  5230. for_each_cpu_mask(i, *cpu_map) {
  5231. struct sched_domain *sd = &per_cpu(phys_domains, i);
  5232. init_sched_groups_power(i, sd);
  5233. }
  5234. #ifdef CONFIG_NUMA
  5235. for (i = 0; i < MAX_NUMNODES; i++)
  5236. init_numa_sched_groups_power(sched_group_nodes[i]);
  5237. if (sd_allnodes) {
  5238. struct sched_group *sg;
  5239. cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg);
  5240. init_numa_sched_groups_power(sg);
  5241. }
  5242. #endif
  5243. /* Attach the domains */
  5244. for_each_cpu_mask(i, *cpu_map) {
  5245. struct sched_domain *sd;
  5246. #ifdef CONFIG_SCHED_SMT
  5247. sd = &per_cpu(cpu_domains, i);
  5248. #elif defined(CONFIG_SCHED_MC)
  5249. sd = &per_cpu(core_domains, i);
  5250. #else
  5251. sd = &per_cpu(phys_domains, i);
  5252. #endif
  5253. cpu_attach_domain(sd, i);
  5254. }
  5255. return 0;
  5256. #ifdef CONFIG_NUMA
  5257. error:
  5258. free_sched_groups(cpu_map);
  5259. return -ENOMEM;
  5260. #endif
  5261. }
  5262. /*
  5263. * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  5264. */
  5265. static int arch_init_sched_domains(const cpumask_t *cpu_map)
  5266. {
  5267. cpumask_t cpu_default_map;
  5268. int err;
  5269. /*
  5270. * Setup mask for cpus without special case scheduling requirements.
  5271. * For now this just excludes isolated cpus, but could be used to
  5272. * exclude other special cases in the future.
  5273. */
  5274. cpus_andnot(cpu_default_map, *cpu_map, cpu_isolated_map);
  5275. err = build_sched_domains(&cpu_default_map);
  5276. return err;
  5277. }
  5278. static void arch_destroy_sched_domains(const cpumask_t *cpu_map)
  5279. {
  5280. free_sched_groups(cpu_map);
  5281. }
  5282. /*
  5283. * Detach sched domains from a group of cpus specified in cpu_map
  5284. * These cpus will now be attached to the NULL domain
  5285. */
  5286. static void detach_destroy_domains(const cpumask_t *cpu_map)
  5287. {
  5288. int i;
  5289. for_each_cpu_mask(i, *cpu_map)
  5290. cpu_attach_domain(NULL, i);
  5291. synchronize_sched();
  5292. arch_destroy_sched_domains(cpu_map);
  5293. }
  5294. /*
  5295. * Partition sched domains as specified by the cpumasks below.
  5296. * This attaches all cpus from the cpumasks to the NULL domain,
  5297. * waits for a RCU quiescent period, recalculates sched
  5298. * domain information and then attaches them back to the
  5299. * correct sched domains
  5300. * Call with hotplug lock held
  5301. */
  5302. int partition_sched_domains(cpumask_t *partition1, cpumask_t *partition2)
  5303. {
  5304. cpumask_t change_map;
  5305. int err = 0;
  5306. cpus_and(*partition1, *partition1, cpu_online_map);
  5307. cpus_and(*partition2, *partition2, cpu_online_map);
  5308. cpus_or(change_map, *partition1, *partition2);
  5309. /* Detach sched domains from all of the affected cpus */
  5310. detach_destroy_domains(&change_map);
  5311. if (!cpus_empty(*partition1))
  5312. err = build_sched_domains(partition1);
  5313. if (!err && !cpus_empty(*partition2))
  5314. err = build_sched_domains(partition2);
  5315. return err;
  5316. }
  5317. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  5318. int arch_reinit_sched_domains(void)
  5319. {
  5320. int err;
  5321. mutex_lock(&sched_hotcpu_mutex);
  5322. detach_destroy_domains(&cpu_online_map);
  5323. err = arch_init_sched_domains(&cpu_online_map);
  5324. mutex_unlock(&sched_hotcpu_mutex);
  5325. return err;
  5326. }
  5327. static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
  5328. {
  5329. int ret;
  5330. if (buf[0] != '0' && buf[0] != '1')
  5331. return -EINVAL;
  5332. if (smt)
  5333. sched_smt_power_savings = (buf[0] == '1');
  5334. else
  5335. sched_mc_power_savings = (buf[0] == '1');
  5336. ret = arch_reinit_sched_domains();
  5337. return ret ? ret : count;
  5338. }
  5339. int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
  5340. {
  5341. int err = 0;
  5342. #ifdef CONFIG_SCHED_SMT
  5343. if (smt_capable())
  5344. err = sysfs_create_file(&cls->kset.kobj,
  5345. &attr_sched_smt_power_savings.attr);
  5346. #endif
  5347. #ifdef CONFIG_SCHED_MC
  5348. if (!err && mc_capable())
  5349. err = sysfs_create_file(&cls->kset.kobj,
  5350. &attr_sched_mc_power_savings.attr);
  5351. #endif
  5352. return err;
  5353. }
  5354. #endif
  5355. #ifdef CONFIG_SCHED_MC
  5356. static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
  5357. {
  5358. return sprintf(page, "%u\n", sched_mc_power_savings);
  5359. }
  5360. static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
  5361. const char *buf, size_t count)
  5362. {
  5363. return sched_power_savings_store(buf, count, 0);
  5364. }
  5365. SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
  5366. sched_mc_power_savings_store);
  5367. #endif
  5368. #ifdef CONFIG_SCHED_SMT
  5369. static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
  5370. {
  5371. return sprintf(page, "%u\n", sched_smt_power_savings);
  5372. }
  5373. static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
  5374. const char *buf, size_t count)
  5375. {
  5376. return sched_power_savings_store(buf, count, 1);
  5377. }
  5378. SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
  5379. sched_smt_power_savings_store);
  5380. #endif
  5381. /*
  5382. * Force a reinitialization of the sched domains hierarchy. The domains
  5383. * and groups cannot be updated in place without racing with the balancing
  5384. * code, so we temporarily attach all running cpus to the NULL domain
  5385. * which will prevent rebalancing while the sched domains are recalculated.
  5386. */
  5387. static int update_sched_domains(struct notifier_block *nfb,
  5388. unsigned long action, void *hcpu)
  5389. {
  5390. switch (action) {
  5391. case CPU_UP_PREPARE:
  5392. case CPU_UP_PREPARE_FROZEN:
  5393. case CPU_DOWN_PREPARE:
  5394. case CPU_DOWN_PREPARE_FROZEN:
  5395. detach_destroy_domains(&cpu_online_map);
  5396. return NOTIFY_OK;
  5397. case CPU_UP_CANCELED:
  5398. case CPU_UP_CANCELED_FROZEN:
  5399. case CPU_DOWN_FAILED:
  5400. case CPU_DOWN_FAILED_FROZEN:
  5401. case CPU_ONLINE:
  5402. case CPU_ONLINE_FROZEN:
  5403. case CPU_DEAD:
  5404. case CPU_DEAD_FROZEN:
  5405. /*
  5406. * Fall through and re-initialise the domains.
  5407. */
  5408. break;
  5409. default:
  5410. return NOTIFY_DONE;
  5411. }
  5412. /* The hotplug lock is already held by cpu_up/cpu_down */
  5413. arch_init_sched_domains(&cpu_online_map);
  5414. return NOTIFY_OK;
  5415. }
  5416. void __init sched_init_smp(void)
  5417. {
  5418. cpumask_t non_isolated_cpus;
  5419. mutex_lock(&sched_hotcpu_mutex);
  5420. arch_init_sched_domains(&cpu_online_map);
  5421. cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
  5422. if (cpus_empty(non_isolated_cpus))
  5423. cpu_set(smp_processor_id(), non_isolated_cpus);
  5424. mutex_unlock(&sched_hotcpu_mutex);
  5425. /* XXX: Theoretical race here - CPU may be hotplugged now */
  5426. hotcpu_notifier(update_sched_domains, 0);
  5427. /* Move init over to a non-isolated CPU */
  5428. if (set_cpus_allowed(current, non_isolated_cpus) < 0)
  5429. BUG();
  5430. sched_init_granularity();
  5431. }
  5432. #else
  5433. void __init sched_init_smp(void)
  5434. {
  5435. sched_init_granularity();
  5436. }
  5437. #endif /* CONFIG_SMP */
  5438. int in_sched_functions(unsigned long addr)
  5439. {
  5440. /* Linker adds these: start and end of __sched functions */
  5441. extern char __sched_text_start[], __sched_text_end[];
  5442. return in_lock_functions(addr) ||
  5443. (addr >= (unsigned long)__sched_text_start
  5444. && addr < (unsigned long)__sched_text_end);
  5445. }
  5446. static inline void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
  5447. {
  5448. cfs_rq->tasks_timeline = RB_ROOT;
  5449. cfs_rq->fair_clock = 1;
  5450. #ifdef CONFIG_FAIR_GROUP_SCHED
  5451. cfs_rq->rq = rq;
  5452. #endif
  5453. }
  5454. void __init sched_init(void)
  5455. {
  5456. u64 now = sched_clock();
  5457. int highest_cpu = 0;
  5458. int i, j;
  5459. /*
  5460. * Link up the scheduling class hierarchy:
  5461. */
  5462. rt_sched_class.next = &fair_sched_class;
  5463. fair_sched_class.next = &idle_sched_class;
  5464. idle_sched_class.next = NULL;
  5465. for_each_possible_cpu(i) {
  5466. struct rt_prio_array *array;
  5467. struct rq *rq;
  5468. rq = cpu_rq(i);
  5469. spin_lock_init(&rq->lock);
  5470. lockdep_set_class(&rq->lock, &rq->rq_lock_key);
  5471. rq->nr_running = 0;
  5472. rq->clock = 1;
  5473. init_cfs_rq(&rq->cfs, rq);
  5474. #ifdef CONFIG_FAIR_GROUP_SCHED
  5475. INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
  5476. list_add(&rq->cfs.leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
  5477. #endif
  5478. rq->ls.load_update_last = now;
  5479. rq->ls.load_update_start = now;
  5480. for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
  5481. rq->cpu_load[j] = 0;
  5482. #ifdef CONFIG_SMP
  5483. rq->sd = NULL;
  5484. rq->active_balance = 0;
  5485. rq->next_balance = jiffies;
  5486. rq->push_cpu = 0;
  5487. rq->cpu = i;
  5488. rq->migration_thread = NULL;
  5489. INIT_LIST_HEAD(&rq->migration_queue);
  5490. #endif
  5491. atomic_set(&rq->nr_iowait, 0);
  5492. array = &rq->rt.active;
  5493. for (j = 0; j < MAX_RT_PRIO; j++) {
  5494. INIT_LIST_HEAD(array->queue + j);
  5495. __clear_bit(j, array->bitmap);
  5496. }
  5497. highest_cpu = i;
  5498. /* delimiter for bitsearch: */
  5499. __set_bit(MAX_RT_PRIO, array->bitmap);
  5500. }
  5501. set_load_weight(&init_task);
  5502. #ifdef CONFIG_PREEMPT_NOTIFIERS
  5503. INIT_HLIST_HEAD(&init_task.preempt_notifiers);
  5504. #endif
  5505. #ifdef CONFIG_SMP
  5506. nr_cpu_ids = highest_cpu + 1;
  5507. open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
  5508. #endif
  5509. #ifdef CONFIG_RT_MUTEXES
  5510. plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
  5511. #endif
  5512. /*
  5513. * The boot idle thread does lazy MMU switching as well:
  5514. */
  5515. atomic_inc(&init_mm.mm_count);
  5516. enter_lazy_tlb(&init_mm, current);
  5517. /*
  5518. * Make us the idle thread. Technically, schedule() should not be
  5519. * called from this thread, however somewhere below it might be,
  5520. * but because we are the idle thread, we just pick up running again
  5521. * when this runqueue becomes "idle".
  5522. */
  5523. init_idle(current, smp_processor_id());
  5524. /*
  5525. * During early bootup we pretend to be a normal task:
  5526. */
  5527. current->sched_class = &fair_sched_class;
  5528. }
  5529. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  5530. void __might_sleep(char *file, int line)
  5531. {
  5532. #ifdef in_atomic
  5533. static unsigned long prev_jiffy; /* ratelimiting */
  5534. if ((in_atomic() || irqs_disabled()) &&
  5535. system_state == SYSTEM_RUNNING && !oops_in_progress) {
  5536. if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  5537. return;
  5538. prev_jiffy = jiffies;
  5539. printk(KERN_ERR "BUG: sleeping function called from invalid"
  5540. " context at %s:%d\n", file, line);
  5541. printk("in_atomic():%d, irqs_disabled():%d\n",
  5542. in_atomic(), irqs_disabled());
  5543. debug_show_held_locks(current);
  5544. if (irqs_disabled())
  5545. print_irqtrace_events(current);
  5546. dump_stack();
  5547. }
  5548. #endif
  5549. }
  5550. EXPORT_SYMBOL(__might_sleep);
  5551. #endif
  5552. #ifdef CONFIG_MAGIC_SYSRQ
  5553. void normalize_rt_tasks(void)
  5554. {
  5555. struct task_struct *g, *p;
  5556. unsigned long flags;
  5557. struct rq *rq;
  5558. int on_rq;
  5559. read_lock_irq(&tasklist_lock);
  5560. do_each_thread(g, p) {
  5561. p->se.fair_key = 0;
  5562. p->se.wait_runtime = 0;
  5563. p->se.wait_start_fair = 0;
  5564. p->se.wait_start = 0;
  5565. p->se.exec_start = 0;
  5566. p->se.sleep_start = 0;
  5567. p->se.sleep_start_fair = 0;
  5568. p->se.block_start = 0;
  5569. task_rq(p)->cfs.fair_clock = 0;
  5570. task_rq(p)->clock = 0;
  5571. if (!rt_task(p)) {
  5572. /*
  5573. * Renice negative nice level userspace
  5574. * tasks back to 0:
  5575. */
  5576. if (TASK_NICE(p) < 0 && p->mm)
  5577. set_user_nice(p, 0);
  5578. continue;
  5579. }
  5580. spin_lock_irqsave(&p->pi_lock, flags);
  5581. rq = __task_rq_lock(p);
  5582. #ifdef CONFIG_SMP
  5583. /*
  5584. * Do not touch the migration thread:
  5585. */
  5586. if (p == rq->migration_thread)
  5587. goto out_unlock;
  5588. #endif
  5589. on_rq = p->se.on_rq;
  5590. if (on_rq)
  5591. deactivate_task(task_rq(p), p, 0);
  5592. __setscheduler(rq, p, SCHED_NORMAL, 0);
  5593. if (on_rq) {
  5594. activate_task(task_rq(p), p, 0);
  5595. resched_task(rq->curr);
  5596. }
  5597. #ifdef CONFIG_SMP
  5598. out_unlock:
  5599. #endif
  5600. __task_rq_unlock(rq);
  5601. spin_unlock_irqrestore(&p->pi_lock, flags);
  5602. } while_each_thread(g, p);
  5603. read_unlock_irq(&tasklist_lock);
  5604. }
  5605. #endif /* CONFIG_MAGIC_SYSRQ */
  5606. #ifdef CONFIG_IA64
  5607. /*
  5608. * These functions are only useful for the IA64 MCA handling.
  5609. *
  5610. * They can only be called when the whole system has been
  5611. * stopped - every CPU needs to be quiescent, and no scheduling
  5612. * activity can take place. Using them for anything else would
  5613. * be a serious bug, and as a result, they aren't even visible
  5614. * under any other configuration.
  5615. */
  5616. /**
  5617. * curr_task - return the current task for a given cpu.
  5618. * @cpu: the processor in question.
  5619. *
  5620. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  5621. */
  5622. struct task_struct *curr_task(int cpu)
  5623. {
  5624. return cpu_curr(cpu);
  5625. }
  5626. /**
  5627. * set_curr_task - set the current task for a given cpu.
  5628. * @cpu: the processor in question.
  5629. * @p: the task pointer to set.
  5630. *
  5631. * Description: This function must only be used when non-maskable interrupts
  5632. * are serviced on a separate stack. It allows the architecture to switch the
  5633. * notion of the current task on a cpu in a non-blocking manner. This function
  5634. * must be called with all CPU's synchronized, and interrupts disabled, the
  5635. * and caller must save the original value of the current task (see
  5636. * curr_task() above) and restore that value before reenabling interrupts and
  5637. * re-starting the system.
  5638. *
  5639. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  5640. */
  5641. void set_curr_task(int cpu, struct task_struct *p)
  5642. {
  5643. cpu_curr(cpu) = p;
  5644. }
  5645. #endif