core.c 201 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415
  1. /*
  2. * kernel/sched/core.c
  3. *
  4. * Kernel scheduler and related syscalls
  5. *
  6. * Copyright (C) 1991-2002 Linus Torvalds
  7. *
  8. * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
  9. * make semaphores SMP safe
  10. * 1998-11-19 Implemented schedule_timeout() and related stuff
  11. * by Andrea Arcangeli
  12. * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
  13. * hybrid priority-list and round-robin design with
  14. * an array-switch method of distributing timeslices
  15. * and per-CPU runqueues. Cleanups and useful suggestions
  16. * by Davide Libenzi, preemptible kernel bits by Robert Love.
  17. * 2003-09-03 Interactivity tuning by Con Kolivas.
  18. * 2004-04-02 Scheduler domains code by Nick Piggin
  19. * 2007-04-15 Work begun on replacing all interactivity tuning with a
  20. * fair scheduling design by Con Kolivas.
  21. * 2007-05-05 Load balancing (smp-nice) and other improvements
  22. * by Peter Williams
  23. * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
  24. * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
  25. * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
  26. * Thomas Gleixner, Mike Kravetz
  27. */
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/nmi.h>
  31. #include <linux/init.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/highmem.h>
  34. #include <asm/mmu_context.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/capability.h>
  37. #include <linux/completion.h>
  38. #include <linux/kernel_stat.h>
  39. #include <linux/debug_locks.h>
  40. #include <linux/perf_event.h>
  41. #include <linux/security.h>
  42. #include <linux/notifier.h>
  43. #include <linux/profile.h>
  44. #include <linux/freezer.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/delay.h>
  48. #include <linux/pid_namespace.h>
  49. #include <linux/smp.h>
  50. #include <linux/threads.h>
  51. #include <linux/timer.h>
  52. #include <linux/rcupdate.h>
  53. #include <linux/cpu.h>
  54. #include <linux/cpuset.h>
  55. #include <linux/percpu.h>
  56. #include <linux/proc_fs.h>
  57. #include <linux/seq_file.h>
  58. #include <linux/sysctl.h>
  59. #include <linux/syscalls.h>
  60. #include <linux/times.h>
  61. #include <linux/tsacct_kern.h>
  62. #include <linux/kprobes.h>
  63. #include <linux/delayacct.h>
  64. #include <linux/unistd.h>
  65. #include <linux/pagemap.h>
  66. #include <linux/hrtimer.h>
  67. #include <linux/tick.h>
  68. #include <linux/debugfs.h>
  69. #include <linux/ctype.h>
  70. #include <linux/ftrace.h>
  71. #include <linux/slab.h>
  72. #include <linux/init_task.h>
  73. #include <linux/binfmts.h>
  74. #include <linux/context_tracking.h>
  75. #include <linux/compiler.h>
  76. #include <asm/switch_to.h>
  77. #include <asm/tlb.h>
  78. #include <asm/irq_regs.h>
  79. #include <asm/mutex.h>
  80. #ifdef CONFIG_PARAVIRT
  81. #include <asm/paravirt.h>
  82. #endif
  83. #include "sched.h"
  84. #include "../workqueue_internal.h"
  85. #include "../smpboot.h"
  86. #define CREATE_TRACE_POINTS
  87. #include <trace/events/sched.h>
  88. DEFINE_MUTEX(sched_domains_mutex);
  89. DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  90. static void update_rq_clock_task(struct rq *rq, s64 delta);
  91. void update_rq_clock(struct rq *rq)
  92. {
  93. s64 delta;
  94. lockdep_assert_held(&rq->lock);
  95. if (rq->clock_skip_update & RQCF_ACT_SKIP)
  96. return;
  97. delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
  98. if (delta < 0)
  99. return;
  100. rq->clock += delta;
  101. update_rq_clock_task(rq, delta);
  102. }
  103. /*
  104. * Debugging: various feature bits
  105. */
  106. #define SCHED_FEAT(name, enabled) \
  107. (1UL << __SCHED_FEAT_##name) * enabled |
  108. const_debug unsigned int sysctl_sched_features =
  109. #include "features.h"
  110. 0;
  111. #undef SCHED_FEAT
  112. #ifdef CONFIG_SCHED_DEBUG
  113. #define SCHED_FEAT(name, enabled) \
  114. #name ,
  115. static const char * const sched_feat_names[] = {
  116. #include "features.h"
  117. };
  118. #undef SCHED_FEAT
  119. static int sched_feat_show(struct seq_file *m, void *v)
  120. {
  121. int i;
  122. for (i = 0; i < __SCHED_FEAT_NR; i++) {
  123. if (!(sysctl_sched_features & (1UL << i)))
  124. seq_puts(m, "NO_");
  125. seq_printf(m, "%s ", sched_feat_names[i]);
  126. }
  127. seq_puts(m, "\n");
  128. return 0;
  129. }
  130. #ifdef HAVE_JUMP_LABEL
  131. #define jump_label_key__true STATIC_KEY_INIT_TRUE
  132. #define jump_label_key__false STATIC_KEY_INIT_FALSE
  133. #define SCHED_FEAT(name, enabled) \
  134. jump_label_key__##enabled ,
  135. struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {
  136. #include "features.h"
  137. };
  138. #undef SCHED_FEAT
  139. static void sched_feat_disable(int i)
  140. {
  141. if (static_key_enabled(&sched_feat_keys[i]))
  142. static_key_slow_dec(&sched_feat_keys[i]);
  143. }
  144. static void sched_feat_enable(int i)
  145. {
  146. if (!static_key_enabled(&sched_feat_keys[i]))
  147. static_key_slow_inc(&sched_feat_keys[i]);
  148. }
  149. #else
  150. static void sched_feat_disable(int i) { };
  151. static void sched_feat_enable(int i) { };
  152. #endif /* HAVE_JUMP_LABEL */
  153. static int sched_feat_set(char *cmp)
  154. {
  155. int i;
  156. int neg = 0;
  157. if (strncmp(cmp, "NO_", 3) == 0) {
  158. neg = 1;
  159. cmp += 3;
  160. }
  161. for (i = 0; i < __SCHED_FEAT_NR; i++) {
  162. if (strcmp(cmp, sched_feat_names[i]) == 0) {
  163. if (neg) {
  164. sysctl_sched_features &= ~(1UL << i);
  165. sched_feat_disable(i);
  166. } else {
  167. sysctl_sched_features |= (1UL << i);
  168. sched_feat_enable(i);
  169. }
  170. break;
  171. }
  172. }
  173. return i;
  174. }
  175. static ssize_t
  176. sched_feat_write(struct file *filp, const char __user *ubuf,
  177. size_t cnt, loff_t *ppos)
  178. {
  179. char buf[64];
  180. char *cmp;
  181. int i;
  182. struct inode *inode;
  183. if (cnt > 63)
  184. cnt = 63;
  185. if (copy_from_user(&buf, ubuf, cnt))
  186. return -EFAULT;
  187. buf[cnt] = 0;
  188. cmp = strstrip(buf);
  189. /* Ensure the static_key remains in a consistent state */
  190. inode = file_inode(filp);
  191. mutex_lock(&inode->i_mutex);
  192. i = sched_feat_set(cmp);
  193. mutex_unlock(&inode->i_mutex);
  194. if (i == __SCHED_FEAT_NR)
  195. return -EINVAL;
  196. *ppos += cnt;
  197. return cnt;
  198. }
  199. static int sched_feat_open(struct inode *inode, struct file *filp)
  200. {
  201. return single_open(filp, sched_feat_show, NULL);
  202. }
  203. static const struct file_operations sched_feat_fops = {
  204. .open = sched_feat_open,
  205. .write = sched_feat_write,
  206. .read = seq_read,
  207. .llseek = seq_lseek,
  208. .release = single_release,
  209. };
  210. static __init int sched_init_debug(void)
  211. {
  212. debugfs_create_file("sched_features", 0644, NULL, NULL,
  213. &sched_feat_fops);
  214. return 0;
  215. }
  216. late_initcall(sched_init_debug);
  217. #endif /* CONFIG_SCHED_DEBUG */
  218. /*
  219. * Number of tasks to iterate in a single balance run.
  220. * Limited because this is done with IRQs disabled.
  221. */
  222. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  223. /*
  224. * period over which we average the RT time consumption, measured
  225. * in ms.
  226. *
  227. * default: 1s
  228. */
  229. const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
  230. /*
  231. * period over which we measure -rt task cpu usage in us.
  232. * default: 1s
  233. */
  234. unsigned int sysctl_sched_rt_period = 1000000;
  235. __read_mostly int scheduler_running;
  236. /*
  237. * part of the period that we allow rt tasks to run in us.
  238. * default: 0.95s
  239. */
  240. int sysctl_sched_rt_runtime = 950000;
  241. /* cpus with isolated domains */
  242. cpumask_var_t cpu_isolated_map;
  243. /*
  244. * this_rq_lock - lock this runqueue and disable interrupts.
  245. */
  246. static struct rq *this_rq_lock(void)
  247. __acquires(rq->lock)
  248. {
  249. struct rq *rq;
  250. local_irq_disable();
  251. rq = this_rq();
  252. raw_spin_lock(&rq->lock);
  253. return rq;
  254. }
  255. #ifdef CONFIG_SCHED_HRTICK
  256. /*
  257. * Use HR-timers to deliver accurate preemption points.
  258. */
  259. static void hrtick_clear(struct rq *rq)
  260. {
  261. if (hrtimer_active(&rq->hrtick_timer))
  262. hrtimer_cancel(&rq->hrtick_timer);
  263. }
  264. /*
  265. * High-resolution timer tick.
  266. * Runs from hardirq context with interrupts disabled.
  267. */
  268. static enum hrtimer_restart hrtick(struct hrtimer *timer)
  269. {
  270. struct rq *rq = container_of(timer, struct rq, hrtick_timer);
  271. WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
  272. raw_spin_lock(&rq->lock);
  273. update_rq_clock(rq);
  274. rq->curr->sched_class->task_tick(rq, rq->curr, 1);
  275. raw_spin_unlock(&rq->lock);
  276. return HRTIMER_NORESTART;
  277. }
  278. #ifdef CONFIG_SMP
  279. static void __hrtick_restart(struct rq *rq)
  280. {
  281. struct hrtimer *timer = &rq->hrtick_timer;
  282. hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
  283. }
  284. /*
  285. * called from hardirq (IPI) context
  286. */
  287. static void __hrtick_start(void *arg)
  288. {
  289. struct rq *rq = arg;
  290. raw_spin_lock(&rq->lock);
  291. __hrtick_restart(rq);
  292. rq->hrtick_csd_pending = 0;
  293. raw_spin_unlock(&rq->lock);
  294. }
  295. /*
  296. * Called to set the hrtick timer state.
  297. *
  298. * called with rq->lock held and irqs disabled
  299. */
  300. void hrtick_start(struct rq *rq, u64 delay)
  301. {
  302. struct hrtimer *timer = &rq->hrtick_timer;
  303. ktime_t time;
  304. s64 delta;
  305. /*
  306. * Don't schedule slices shorter than 10000ns, that just
  307. * doesn't make sense and can cause timer DoS.
  308. */
  309. delta = max_t(s64, delay, 10000LL);
  310. time = ktime_add_ns(timer->base->get_time(), delta);
  311. hrtimer_set_expires(timer, time);
  312. if (rq == this_rq()) {
  313. __hrtick_restart(rq);
  314. } else if (!rq->hrtick_csd_pending) {
  315. smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
  316. rq->hrtick_csd_pending = 1;
  317. }
  318. }
  319. static int
  320. hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
  321. {
  322. int cpu = (int)(long)hcpu;
  323. switch (action) {
  324. case CPU_UP_CANCELED:
  325. case CPU_UP_CANCELED_FROZEN:
  326. case CPU_DOWN_PREPARE:
  327. case CPU_DOWN_PREPARE_FROZEN:
  328. case CPU_DEAD:
  329. case CPU_DEAD_FROZEN:
  330. hrtick_clear(cpu_rq(cpu));
  331. return NOTIFY_OK;
  332. }
  333. return NOTIFY_DONE;
  334. }
  335. static __init void init_hrtick(void)
  336. {
  337. hotcpu_notifier(hotplug_hrtick, 0);
  338. }
  339. #else
  340. /*
  341. * Called to set the hrtick timer state.
  342. *
  343. * called with rq->lock held and irqs disabled
  344. */
  345. void hrtick_start(struct rq *rq, u64 delay)
  346. {
  347. /*
  348. * Don't schedule slices shorter than 10000ns, that just
  349. * doesn't make sense. Rely on vruntime for fairness.
  350. */
  351. delay = max_t(u64, delay, 10000LL);
  352. hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
  353. HRTIMER_MODE_REL_PINNED);
  354. }
  355. static inline void init_hrtick(void)
  356. {
  357. }
  358. #endif /* CONFIG_SMP */
  359. static void init_rq_hrtick(struct rq *rq)
  360. {
  361. #ifdef CONFIG_SMP
  362. rq->hrtick_csd_pending = 0;
  363. rq->hrtick_csd.flags = 0;
  364. rq->hrtick_csd.func = __hrtick_start;
  365. rq->hrtick_csd.info = rq;
  366. #endif
  367. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  368. rq->hrtick_timer.function = hrtick;
  369. }
  370. #else /* CONFIG_SCHED_HRTICK */
  371. static inline void hrtick_clear(struct rq *rq)
  372. {
  373. }
  374. static inline void init_rq_hrtick(struct rq *rq)
  375. {
  376. }
  377. static inline void init_hrtick(void)
  378. {
  379. }
  380. #endif /* CONFIG_SCHED_HRTICK */
  381. /*
  382. * cmpxchg based fetch_or, macro so it works for different integer types
  383. */
  384. #define fetch_or(ptr, val) \
  385. ({ typeof(*(ptr)) __old, __val = *(ptr); \
  386. for (;;) { \
  387. __old = cmpxchg((ptr), __val, __val | (val)); \
  388. if (__old == __val) \
  389. break; \
  390. __val = __old; \
  391. } \
  392. __old; \
  393. })
  394. #if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
  395. /*
  396. * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
  397. * this avoids any races wrt polling state changes and thereby avoids
  398. * spurious IPIs.
  399. */
  400. static bool set_nr_and_not_polling(struct task_struct *p)
  401. {
  402. struct thread_info *ti = task_thread_info(p);
  403. return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
  404. }
  405. /*
  406. * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
  407. *
  408. * If this returns true, then the idle task promises to call
  409. * sched_ttwu_pending() and reschedule soon.
  410. */
  411. static bool set_nr_if_polling(struct task_struct *p)
  412. {
  413. struct thread_info *ti = task_thread_info(p);
  414. typeof(ti->flags) old, val = READ_ONCE(ti->flags);
  415. for (;;) {
  416. if (!(val & _TIF_POLLING_NRFLAG))
  417. return false;
  418. if (val & _TIF_NEED_RESCHED)
  419. return true;
  420. old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED);
  421. if (old == val)
  422. break;
  423. val = old;
  424. }
  425. return true;
  426. }
  427. #else
  428. static bool set_nr_and_not_polling(struct task_struct *p)
  429. {
  430. set_tsk_need_resched(p);
  431. return true;
  432. }
  433. #ifdef CONFIG_SMP
  434. static bool set_nr_if_polling(struct task_struct *p)
  435. {
  436. return false;
  437. }
  438. #endif
  439. #endif
  440. void wake_q_add(struct wake_q_head *head, struct task_struct *task)
  441. {
  442. struct wake_q_node *node = &task->wake_q;
  443. /*
  444. * Atomically grab the task, if ->wake_q is !nil already it means
  445. * its already queued (either by us or someone else) and will get the
  446. * wakeup due to that.
  447. *
  448. * This cmpxchg() implies a full barrier, which pairs with the write
  449. * barrier implied by the wakeup in wake_up_list().
  450. */
  451. if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL))
  452. return;
  453. get_task_struct(task);
  454. /*
  455. * The head is context local, there can be no concurrency.
  456. */
  457. *head->lastp = node;
  458. head->lastp = &node->next;
  459. }
  460. void wake_up_q(struct wake_q_head *head)
  461. {
  462. struct wake_q_node *node = head->first;
  463. while (node != WAKE_Q_TAIL) {
  464. struct task_struct *task;
  465. task = container_of(node, struct task_struct, wake_q);
  466. BUG_ON(!task);
  467. /* task can safely be re-inserted now */
  468. node = node->next;
  469. task->wake_q.next = NULL;
  470. /*
  471. * wake_up_process() implies a wmb() to pair with the queueing
  472. * in wake_q_add() so as not to miss wakeups.
  473. */
  474. wake_up_process(task);
  475. put_task_struct(task);
  476. }
  477. }
  478. /*
  479. * resched_curr - mark rq's current task 'to be rescheduled now'.
  480. *
  481. * On UP this means the setting of the need_resched flag, on SMP it
  482. * might also involve a cross-CPU call to trigger the scheduler on
  483. * the target CPU.
  484. */
  485. void resched_curr(struct rq *rq)
  486. {
  487. struct task_struct *curr = rq->curr;
  488. int cpu;
  489. lockdep_assert_held(&rq->lock);
  490. if (test_tsk_need_resched(curr))
  491. return;
  492. cpu = cpu_of(rq);
  493. if (cpu == smp_processor_id()) {
  494. set_tsk_need_resched(curr);
  495. set_preempt_need_resched();
  496. return;
  497. }
  498. if (set_nr_and_not_polling(curr))
  499. smp_send_reschedule(cpu);
  500. else
  501. trace_sched_wake_idle_without_ipi(cpu);
  502. }
  503. void resched_cpu(int cpu)
  504. {
  505. struct rq *rq = cpu_rq(cpu);
  506. unsigned long flags;
  507. if (!raw_spin_trylock_irqsave(&rq->lock, flags))
  508. return;
  509. resched_curr(rq);
  510. raw_spin_unlock_irqrestore(&rq->lock, flags);
  511. }
  512. #ifdef CONFIG_SMP
  513. #ifdef CONFIG_NO_HZ_COMMON
  514. /*
  515. * In the semi idle case, use the nearest busy cpu for migrating timers
  516. * from an idle cpu. This is good for power-savings.
  517. *
  518. * We don't do similar optimization for completely idle system, as
  519. * selecting an idle cpu will add more delays to the timers than intended
  520. * (as that cpu's timer base may not be uptodate wrt jiffies etc).
  521. */
  522. int get_nohz_timer_target(void)
  523. {
  524. int i, cpu = smp_processor_id();
  525. struct sched_domain *sd;
  526. if (!idle_cpu(cpu))
  527. return cpu;
  528. rcu_read_lock();
  529. for_each_domain(cpu, sd) {
  530. for_each_cpu(i, sched_domain_span(sd)) {
  531. if (!idle_cpu(i)) {
  532. cpu = i;
  533. goto unlock;
  534. }
  535. }
  536. }
  537. unlock:
  538. rcu_read_unlock();
  539. return cpu;
  540. }
  541. /*
  542. * When add_timer_on() enqueues a timer into the timer wheel of an
  543. * idle CPU then this timer might expire before the next timer event
  544. * which is scheduled to wake up that CPU. In case of a completely
  545. * idle system the next event might even be infinite time into the
  546. * future. wake_up_idle_cpu() ensures that the CPU is woken up and
  547. * leaves the inner idle loop so the newly added timer is taken into
  548. * account when the CPU goes back to idle and evaluates the timer
  549. * wheel for the next timer event.
  550. */
  551. static void wake_up_idle_cpu(int cpu)
  552. {
  553. struct rq *rq = cpu_rq(cpu);
  554. if (cpu == smp_processor_id())
  555. return;
  556. if (set_nr_and_not_polling(rq->idle))
  557. smp_send_reschedule(cpu);
  558. else
  559. trace_sched_wake_idle_without_ipi(cpu);
  560. }
  561. static bool wake_up_full_nohz_cpu(int cpu)
  562. {
  563. /*
  564. * We just need the target to call irq_exit() and re-evaluate
  565. * the next tick. The nohz full kick at least implies that.
  566. * If needed we can still optimize that later with an
  567. * empty IRQ.
  568. */
  569. if (tick_nohz_full_cpu(cpu)) {
  570. if (cpu != smp_processor_id() ||
  571. tick_nohz_tick_stopped())
  572. tick_nohz_full_kick_cpu(cpu);
  573. return true;
  574. }
  575. return false;
  576. }
  577. void wake_up_nohz_cpu(int cpu)
  578. {
  579. if (!wake_up_full_nohz_cpu(cpu))
  580. wake_up_idle_cpu(cpu);
  581. }
  582. static inline bool got_nohz_idle_kick(void)
  583. {
  584. int cpu = smp_processor_id();
  585. if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)))
  586. return false;
  587. if (idle_cpu(cpu) && !need_resched())
  588. return true;
  589. /*
  590. * We can't run Idle Load Balance on this CPU for this time so we
  591. * cancel it and clear NOHZ_BALANCE_KICK
  592. */
  593. clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu));
  594. return false;
  595. }
  596. #else /* CONFIG_NO_HZ_COMMON */
  597. static inline bool got_nohz_idle_kick(void)
  598. {
  599. return false;
  600. }
  601. #endif /* CONFIG_NO_HZ_COMMON */
  602. #ifdef CONFIG_NO_HZ_FULL
  603. bool sched_can_stop_tick(void)
  604. {
  605. /*
  606. * FIFO realtime policy runs the highest priority task. Other runnable
  607. * tasks are of a lower priority. The scheduler tick does nothing.
  608. */
  609. if (current->policy == SCHED_FIFO)
  610. return true;
  611. /*
  612. * Round-robin realtime tasks time slice with other tasks at the same
  613. * realtime priority. Is this task the only one at this priority?
  614. */
  615. if (current->policy == SCHED_RR) {
  616. struct sched_rt_entity *rt_se = &current->rt;
  617. return rt_se->run_list.prev == rt_se->run_list.next;
  618. }
  619. /*
  620. * More than one running task need preemption.
  621. * nr_running update is assumed to be visible
  622. * after IPI is sent from wakers.
  623. */
  624. if (this_rq()->nr_running > 1)
  625. return false;
  626. return true;
  627. }
  628. #endif /* CONFIG_NO_HZ_FULL */
  629. void sched_avg_update(struct rq *rq)
  630. {
  631. s64 period = sched_avg_period();
  632. while ((s64)(rq_clock(rq) - rq->age_stamp) > period) {
  633. /*
  634. * Inline assembly required to prevent the compiler
  635. * optimising this loop into a divmod call.
  636. * See __iter_div_u64_rem() for another example of this.
  637. */
  638. asm("" : "+rm" (rq->age_stamp));
  639. rq->age_stamp += period;
  640. rq->rt_avg /= 2;
  641. }
  642. }
  643. #endif /* CONFIG_SMP */
  644. #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
  645. (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
  646. /*
  647. * Iterate task_group tree rooted at *from, calling @down when first entering a
  648. * node and @up when leaving it for the final time.
  649. *
  650. * Caller must hold rcu_lock or sufficient equivalent.
  651. */
  652. int walk_tg_tree_from(struct task_group *from,
  653. tg_visitor down, tg_visitor up, void *data)
  654. {
  655. struct task_group *parent, *child;
  656. int ret;
  657. parent = from;
  658. down:
  659. ret = (*down)(parent, data);
  660. if (ret)
  661. goto out;
  662. list_for_each_entry_rcu(child, &parent->children, siblings) {
  663. parent = child;
  664. goto down;
  665. up:
  666. continue;
  667. }
  668. ret = (*up)(parent, data);
  669. if (ret || parent == from)
  670. goto out;
  671. child = parent;
  672. parent = parent->parent;
  673. if (parent)
  674. goto up;
  675. out:
  676. return ret;
  677. }
  678. int tg_nop(struct task_group *tg, void *data)
  679. {
  680. return 0;
  681. }
  682. #endif
  683. static void set_load_weight(struct task_struct *p)
  684. {
  685. int prio = p->static_prio - MAX_RT_PRIO;
  686. struct load_weight *load = &p->se.load;
  687. /*
  688. * SCHED_IDLE tasks get minimal weight:
  689. */
  690. if (p->policy == SCHED_IDLE) {
  691. load->weight = scale_load(WEIGHT_IDLEPRIO);
  692. load->inv_weight = WMULT_IDLEPRIO;
  693. return;
  694. }
  695. load->weight = scale_load(prio_to_weight[prio]);
  696. load->inv_weight = prio_to_wmult[prio];
  697. }
  698. static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
  699. {
  700. update_rq_clock(rq);
  701. sched_info_queued(rq, p);
  702. p->sched_class->enqueue_task(rq, p, flags);
  703. }
  704. static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
  705. {
  706. update_rq_clock(rq);
  707. sched_info_dequeued(rq, p);
  708. p->sched_class->dequeue_task(rq, p, flags);
  709. }
  710. void activate_task(struct rq *rq, struct task_struct *p, int flags)
  711. {
  712. if (task_contributes_to_load(p))
  713. rq->nr_uninterruptible--;
  714. enqueue_task(rq, p, flags);
  715. }
  716. void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
  717. {
  718. if (task_contributes_to_load(p))
  719. rq->nr_uninterruptible++;
  720. dequeue_task(rq, p, flags);
  721. }
  722. static void update_rq_clock_task(struct rq *rq, s64 delta)
  723. {
  724. /*
  725. * In theory, the compile should just see 0 here, and optimize out the call
  726. * to sched_rt_avg_update. But I don't trust it...
  727. */
  728. #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
  729. s64 steal = 0, irq_delta = 0;
  730. #endif
  731. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  732. irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
  733. /*
  734. * Since irq_time is only updated on {soft,}irq_exit, we might run into
  735. * this case when a previous update_rq_clock() happened inside a
  736. * {soft,}irq region.
  737. *
  738. * When this happens, we stop ->clock_task and only update the
  739. * prev_irq_time stamp to account for the part that fit, so that a next
  740. * update will consume the rest. This ensures ->clock_task is
  741. * monotonic.
  742. *
  743. * It does however cause some slight miss-attribution of {soft,}irq
  744. * time, a more accurate solution would be to update the irq_time using
  745. * the current rq->clock timestamp, except that would require using
  746. * atomic ops.
  747. */
  748. if (irq_delta > delta)
  749. irq_delta = delta;
  750. rq->prev_irq_time += irq_delta;
  751. delta -= irq_delta;
  752. #endif
  753. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  754. if (static_key_false((&paravirt_steal_rq_enabled))) {
  755. steal = paravirt_steal_clock(cpu_of(rq));
  756. steal -= rq->prev_steal_time_rq;
  757. if (unlikely(steal > delta))
  758. steal = delta;
  759. rq->prev_steal_time_rq += steal;
  760. delta -= steal;
  761. }
  762. #endif
  763. rq->clock_task += delta;
  764. #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
  765. if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
  766. sched_rt_avg_update(rq, irq_delta + steal);
  767. #endif
  768. }
  769. void sched_set_stop_task(int cpu, struct task_struct *stop)
  770. {
  771. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  772. struct task_struct *old_stop = cpu_rq(cpu)->stop;
  773. if (stop) {
  774. /*
  775. * Make it appear like a SCHED_FIFO task, its something
  776. * userspace knows about and won't get confused about.
  777. *
  778. * Also, it will make PI more or less work without too
  779. * much confusion -- but then, stop work should not
  780. * rely on PI working anyway.
  781. */
  782. sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
  783. stop->sched_class = &stop_sched_class;
  784. }
  785. cpu_rq(cpu)->stop = stop;
  786. if (old_stop) {
  787. /*
  788. * Reset it back to a normal scheduling class so that
  789. * it can die in pieces.
  790. */
  791. old_stop->sched_class = &rt_sched_class;
  792. }
  793. }
  794. /*
  795. * __normal_prio - return the priority that is based on the static prio
  796. */
  797. static inline int __normal_prio(struct task_struct *p)
  798. {
  799. return p->static_prio;
  800. }
  801. /*
  802. * Calculate the expected normal priority: i.e. priority
  803. * without taking RT-inheritance into account. Might be
  804. * boosted by interactivity modifiers. Changes upon fork,
  805. * setprio syscalls, and whenever the interactivity
  806. * estimator recalculates.
  807. */
  808. static inline int normal_prio(struct task_struct *p)
  809. {
  810. int prio;
  811. if (task_has_dl_policy(p))
  812. prio = MAX_DL_PRIO-1;
  813. else if (task_has_rt_policy(p))
  814. prio = MAX_RT_PRIO-1 - p->rt_priority;
  815. else
  816. prio = __normal_prio(p);
  817. return prio;
  818. }
  819. /*
  820. * Calculate the current priority, i.e. the priority
  821. * taken into account by the scheduler. This value might
  822. * be boosted by RT tasks, or might be boosted by
  823. * interactivity modifiers. Will be RT if the task got
  824. * RT-boosted. If not then it returns p->normal_prio.
  825. */
  826. static int effective_prio(struct task_struct *p)
  827. {
  828. p->normal_prio = normal_prio(p);
  829. /*
  830. * If we are RT tasks or we were boosted to RT priority,
  831. * keep the priority unchanged. Otherwise, update priority
  832. * to the normal priority:
  833. */
  834. if (!rt_prio(p->prio))
  835. return p->normal_prio;
  836. return p->prio;
  837. }
  838. /**
  839. * task_curr - is this task currently executing on a CPU?
  840. * @p: the task in question.
  841. *
  842. * Return: 1 if the task is currently executing. 0 otherwise.
  843. */
  844. inline int task_curr(const struct task_struct *p)
  845. {
  846. return cpu_curr(task_cpu(p)) == p;
  847. }
  848. /*
  849. * Can drop rq->lock because from sched_class::switched_from() methods drop it.
  850. */
  851. static inline void check_class_changed(struct rq *rq, struct task_struct *p,
  852. const struct sched_class *prev_class,
  853. int oldprio)
  854. {
  855. if (prev_class != p->sched_class) {
  856. if (prev_class->switched_from)
  857. prev_class->switched_from(rq, p);
  858. /* Possble rq->lock 'hole'. */
  859. p->sched_class->switched_to(rq, p);
  860. } else if (oldprio != p->prio || dl_task(p))
  861. p->sched_class->prio_changed(rq, p, oldprio);
  862. }
  863. void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
  864. {
  865. const struct sched_class *class;
  866. if (p->sched_class == rq->curr->sched_class) {
  867. rq->curr->sched_class->check_preempt_curr(rq, p, flags);
  868. } else {
  869. for_each_class(class) {
  870. if (class == rq->curr->sched_class)
  871. break;
  872. if (class == p->sched_class) {
  873. resched_curr(rq);
  874. break;
  875. }
  876. }
  877. }
  878. /*
  879. * A queue event has occurred, and we're going to schedule. In
  880. * this case, we can save a useless back to back clock update.
  881. */
  882. if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
  883. rq_clock_skip_update(rq, true);
  884. }
  885. #ifdef CONFIG_SMP
  886. void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
  887. {
  888. #ifdef CONFIG_SCHED_DEBUG
  889. /*
  890. * We should never call set_task_cpu() on a blocked task,
  891. * ttwu() will sort out the placement.
  892. */
  893. WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
  894. !p->on_rq);
  895. #ifdef CONFIG_LOCKDEP
  896. /*
  897. * The caller should hold either p->pi_lock or rq->lock, when changing
  898. * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
  899. *
  900. * sched_move_task() holds both and thus holding either pins the cgroup,
  901. * see task_group().
  902. *
  903. * Furthermore, all task_rq users should acquire both locks, see
  904. * task_rq_lock().
  905. */
  906. WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
  907. lockdep_is_held(&task_rq(p)->lock)));
  908. #endif
  909. #endif
  910. trace_sched_migrate_task(p, new_cpu);
  911. if (task_cpu(p) != new_cpu) {
  912. if (p->sched_class->migrate_task_rq)
  913. p->sched_class->migrate_task_rq(p, new_cpu);
  914. p->se.nr_migrations++;
  915. perf_event_task_migrate(p);
  916. }
  917. __set_task_cpu(p, new_cpu);
  918. }
  919. static void __migrate_swap_task(struct task_struct *p, int cpu)
  920. {
  921. if (task_on_rq_queued(p)) {
  922. struct rq *src_rq, *dst_rq;
  923. src_rq = task_rq(p);
  924. dst_rq = cpu_rq(cpu);
  925. deactivate_task(src_rq, p, 0);
  926. set_task_cpu(p, cpu);
  927. activate_task(dst_rq, p, 0);
  928. check_preempt_curr(dst_rq, p, 0);
  929. } else {
  930. /*
  931. * Task isn't running anymore; make it appear like we migrated
  932. * it before it went to sleep. This means on wakeup we make the
  933. * previous cpu our targer instead of where it really is.
  934. */
  935. p->wake_cpu = cpu;
  936. }
  937. }
  938. struct migration_swap_arg {
  939. struct task_struct *src_task, *dst_task;
  940. int src_cpu, dst_cpu;
  941. };
  942. static int migrate_swap_stop(void *data)
  943. {
  944. struct migration_swap_arg *arg = data;
  945. struct rq *src_rq, *dst_rq;
  946. int ret = -EAGAIN;
  947. src_rq = cpu_rq(arg->src_cpu);
  948. dst_rq = cpu_rq(arg->dst_cpu);
  949. double_raw_lock(&arg->src_task->pi_lock,
  950. &arg->dst_task->pi_lock);
  951. double_rq_lock(src_rq, dst_rq);
  952. if (task_cpu(arg->dst_task) != arg->dst_cpu)
  953. goto unlock;
  954. if (task_cpu(arg->src_task) != arg->src_cpu)
  955. goto unlock;
  956. if (!cpumask_test_cpu(arg->dst_cpu, tsk_cpus_allowed(arg->src_task)))
  957. goto unlock;
  958. if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task)))
  959. goto unlock;
  960. __migrate_swap_task(arg->src_task, arg->dst_cpu);
  961. __migrate_swap_task(arg->dst_task, arg->src_cpu);
  962. ret = 0;
  963. unlock:
  964. double_rq_unlock(src_rq, dst_rq);
  965. raw_spin_unlock(&arg->dst_task->pi_lock);
  966. raw_spin_unlock(&arg->src_task->pi_lock);
  967. return ret;
  968. }
  969. /*
  970. * Cross migrate two tasks
  971. */
  972. int migrate_swap(struct task_struct *cur, struct task_struct *p)
  973. {
  974. struct migration_swap_arg arg;
  975. int ret = -EINVAL;
  976. arg = (struct migration_swap_arg){
  977. .src_task = cur,
  978. .src_cpu = task_cpu(cur),
  979. .dst_task = p,
  980. .dst_cpu = task_cpu(p),
  981. };
  982. if (arg.src_cpu == arg.dst_cpu)
  983. goto out;
  984. /*
  985. * These three tests are all lockless; this is OK since all of them
  986. * will be re-checked with proper locks held further down the line.
  987. */
  988. if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
  989. goto out;
  990. if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task)))
  991. goto out;
  992. if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task)))
  993. goto out;
  994. trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
  995. ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
  996. out:
  997. return ret;
  998. }
  999. struct migration_arg {
  1000. struct task_struct *task;
  1001. int dest_cpu;
  1002. };
  1003. static int migration_cpu_stop(void *data);
  1004. /*
  1005. * wait_task_inactive - wait for a thread to unschedule.
  1006. *
  1007. * If @match_state is nonzero, it's the @p->state value just checked and
  1008. * not expected to change. If it changes, i.e. @p might have woken up,
  1009. * then return zero. When we succeed in waiting for @p to be off its CPU,
  1010. * we return a positive number (its total switch count). If a second call
  1011. * a short while later returns the same number, the caller can be sure that
  1012. * @p has remained unscheduled the whole time.
  1013. *
  1014. * The caller must ensure that the task *will* unschedule sometime soon,
  1015. * else this function might spin for a *long* time. This function can't
  1016. * be called with interrupts off, or it may introduce deadlock with
  1017. * smp_call_function() if an IPI is sent by the same process we are
  1018. * waiting to become inactive.
  1019. */
  1020. unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  1021. {
  1022. unsigned long flags;
  1023. int running, queued;
  1024. unsigned long ncsw;
  1025. struct rq *rq;
  1026. for (;;) {
  1027. /*
  1028. * We do the initial early heuristics without holding
  1029. * any task-queue locks at all. We'll only try to get
  1030. * the runqueue lock when things look like they will
  1031. * work out!
  1032. */
  1033. rq = task_rq(p);
  1034. /*
  1035. * If the task is actively running on another CPU
  1036. * still, just relax and busy-wait without holding
  1037. * any locks.
  1038. *
  1039. * NOTE! Since we don't hold any locks, it's not
  1040. * even sure that "rq" stays as the right runqueue!
  1041. * But we don't care, since "task_running()" will
  1042. * return false if the runqueue has changed and p
  1043. * is actually now running somewhere else!
  1044. */
  1045. while (task_running(rq, p)) {
  1046. if (match_state && unlikely(p->state != match_state))
  1047. return 0;
  1048. cpu_relax();
  1049. }
  1050. /*
  1051. * Ok, time to look more closely! We need the rq
  1052. * lock now, to be *sure*. If we're wrong, we'll
  1053. * just go back and repeat.
  1054. */
  1055. rq = task_rq_lock(p, &flags);
  1056. trace_sched_wait_task(p);
  1057. running = task_running(rq, p);
  1058. queued = task_on_rq_queued(p);
  1059. ncsw = 0;
  1060. if (!match_state || p->state == match_state)
  1061. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  1062. task_rq_unlock(rq, p, &flags);
  1063. /*
  1064. * If it changed from the expected state, bail out now.
  1065. */
  1066. if (unlikely(!ncsw))
  1067. break;
  1068. /*
  1069. * Was it really running after all now that we
  1070. * checked with the proper locks actually held?
  1071. *
  1072. * Oops. Go back and try again..
  1073. */
  1074. if (unlikely(running)) {
  1075. cpu_relax();
  1076. continue;
  1077. }
  1078. /*
  1079. * It's not enough that it's not actively running,
  1080. * it must be off the runqueue _entirely_, and not
  1081. * preempted!
  1082. *
  1083. * So if it was still runnable (but just not actively
  1084. * running right now), it's preempted, and we should
  1085. * yield - it could be a while.
  1086. */
  1087. if (unlikely(queued)) {
  1088. ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
  1089. set_current_state(TASK_UNINTERRUPTIBLE);
  1090. schedule_hrtimeout(&to, HRTIMER_MODE_REL);
  1091. continue;
  1092. }
  1093. /*
  1094. * Ahh, all good. It wasn't running, and it wasn't
  1095. * runnable, which means that it will never become
  1096. * running in the future either. We're all done!
  1097. */
  1098. break;
  1099. }
  1100. return ncsw;
  1101. }
  1102. /***
  1103. * kick_process - kick a running thread to enter/exit the kernel
  1104. * @p: the to-be-kicked thread
  1105. *
  1106. * Cause a process which is running on another CPU to enter
  1107. * kernel-mode, without any delay. (to get signals handled.)
  1108. *
  1109. * NOTE: this function doesn't have to take the runqueue lock,
  1110. * because all it wants to ensure is that the remote task enters
  1111. * the kernel. If the IPI races and the task has been migrated
  1112. * to another CPU then no harm is done and the purpose has been
  1113. * achieved as well.
  1114. */
  1115. void kick_process(struct task_struct *p)
  1116. {
  1117. int cpu;
  1118. preempt_disable();
  1119. cpu = task_cpu(p);
  1120. if ((cpu != smp_processor_id()) && task_curr(p))
  1121. smp_send_reschedule(cpu);
  1122. preempt_enable();
  1123. }
  1124. EXPORT_SYMBOL_GPL(kick_process);
  1125. #endif /* CONFIG_SMP */
  1126. #ifdef CONFIG_SMP
  1127. /*
  1128. * ->cpus_allowed is protected by both rq->lock and p->pi_lock
  1129. */
  1130. static int select_fallback_rq(int cpu, struct task_struct *p)
  1131. {
  1132. int nid = cpu_to_node(cpu);
  1133. const struct cpumask *nodemask = NULL;
  1134. enum { cpuset, possible, fail } state = cpuset;
  1135. int dest_cpu;
  1136. /*
  1137. * If the node that the cpu is on has been offlined, cpu_to_node()
  1138. * will return -1. There is no cpu on the node, and we should
  1139. * select the cpu on the other node.
  1140. */
  1141. if (nid != -1) {
  1142. nodemask = cpumask_of_node(nid);
  1143. /* Look for allowed, online CPU in same node. */
  1144. for_each_cpu(dest_cpu, nodemask) {
  1145. if (!cpu_online(dest_cpu))
  1146. continue;
  1147. if (!cpu_active(dest_cpu))
  1148. continue;
  1149. if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
  1150. return dest_cpu;
  1151. }
  1152. }
  1153. for (;;) {
  1154. /* Any allowed, online CPU? */
  1155. for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) {
  1156. if (!cpu_online(dest_cpu))
  1157. continue;
  1158. if (!cpu_active(dest_cpu))
  1159. continue;
  1160. goto out;
  1161. }
  1162. switch (state) {
  1163. case cpuset:
  1164. /* No more Mr. Nice Guy. */
  1165. cpuset_cpus_allowed_fallback(p);
  1166. state = possible;
  1167. break;
  1168. case possible:
  1169. do_set_cpus_allowed(p, cpu_possible_mask);
  1170. state = fail;
  1171. break;
  1172. case fail:
  1173. BUG();
  1174. break;
  1175. }
  1176. }
  1177. out:
  1178. if (state != cpuset) {
  1179. /*
  1180. * Don't tell them about moving exiting tasks or
  1181. * kernel threads (both mm NULL), since they never
  1182. * leave kernel.
  1183. */
  1184. if (p->mm && printk_ratelimit()) {
  1185. printk_deferred("process %d (%s) no longer affine to cpu%d\n",
  1186. task_pid_nr(p), p->comm, cpu);
  1187. }
  1188. }
  1189. return dest_cpu;
  1190. }
  1191. /*
  1192. * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
  1193. */
  1194. static inline
  1195. int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
  1196. {
  1197. if (p->nr_cpus_allowed > 1)
  1198. cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags);
  1199. /*
  1200. * In order not to call set_task_cpu() on a blocking task we need
  1201. * to rely on ttwu() to place the task on a valid ->cpus_allowed
  1202. * cpu.
  1203. *
  1204. * Since this is common to all placement strategies, this lives here.
  1205. *
  1206. * [ this allows ->select_task() to simply return task_cpu(p) and
  1207. * not worry about this generic constraint ]
  1208. */
  1209. if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) ||
  1210. !cpu_online(cpu)))
  1211. cpu = select_fallback_rq(task_cpu(p), p);
  1212. return cpu;
  1213. }
  1214. static void update_avg(u64 *avg, u64 sample)
  1215. {
  1216. s64 diff = sample - *avg;
  1217. *avg += diff >> 3;
  1218. }
  1219. #endif
  1220. static void
  1221. ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
  1222. {
  1223. #ifdef CONFIG_SCHEDSTATS
  1224. struct rq *rq = this_rq();
  1225. #ifdef CONFIG_SMP
  1226. int this_cpu = smp_processor_id();
  1227. if (cpu == this_cpu) {
  1228. schedstat_inc(rq, ttwu_local);
  1229. schedstat_inc(p, se.statistics.nr_wakeups_local);
  1230. } else {
  1231. struct sched_domain *sd;
  1232. schedstat_inc(p, se.statistics.nr_wakeups_remote);
  1233. rcu_read_lock();
  1234. for_each_domain(this_cpu, sd) {
  1235. if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  1236. schedstat_inc(sd, ttwu_wake_remote);
  1237. break;
  1238. }
  1239. }
  1240. rcu_read_unlock();
  1241. }
  1242. if (wake_flags & WF_MIGRATED)
  1243. schedstat_inc(p, se.statistics.nr_wakeups_migrate);
  1244. #endif /* CONFIG_SMP */
  1245. schedstat_inc(rq, ttwu_count);
  1246. schedstat_inc(p, se.statistics.nr_wakeups);
  1247. if (wake_flags & WF_SYNC)
  1248. schedstat_inc(p, se.statistics.nr_wakeups_sync);
  1249. #endif /* CONFIG_SCHEDSTATS */
  1250. }
  1251. static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
  1252. {
  1253. activate_task(rq, p, en_flags);
  1254. p->on_rq = TASK_ON_RQ_QUEUED;
  1255. /* if a worker is waking up, notify workqueue */
  1256. if (p->flags & PF_WQ_WORKER)
  1257. wq_worker_waking_up(p, cpu_of(rq));
  1258. }
  1259. /*
  1260. * Mark the task runnable and perform wakeup-preemption.
  1261. */
  1262. static void
  1263. ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  1264. {
  1265. check_preempt_curr(rq, p, wake_flags);
  1266. trace_sched_wakeup(p, true);
  1267. p->state = TASK_RUNNING;
  1268. #ifdef CONFIG_SMP
  1269. if (p->sched_class->task_woken)
  1270. p->sched_class->task_woken(rq, p);
  1271. if (rq->idle_stamp) {
  1272. u64 delta = rq_clock(rq) - rq->idle_stamp;
  1273. u64 max = 2*rq->max_idle_balance_cost;
  1274. update_avg(&rq->avg_idle, delta);
  1275. if (rq->avg_idle > max)
  1276. rq->avg_idle = max;
  1277. rq->idle_stamp = 0;
  1278. }
  1279. #endif
  1280. }
  1281. static void
  1282. ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)
  1283. {
  1284. #ifdef CONFIG_SMP
  1285. if (p->sched_contributes_to_load)
  1286. rq->nr_uninterruptible--;
  1287. #endif
  1288. ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);
  1289. ttwu_do_wakeup(rq, p, wake_flags);
  1290. }
  1291. /*
  1292. * Called in case the task @p isn't fully descheduled from its runqueue,
  1293. * in this case we must do a remote wakeup. Its a 'light' wakeup though,
  1294. * since all we need to do is flip p->state to TASK_RUNNING, since
  1295. * the task is still ->on_rq.
  1296. */
  1297. static int ttwu_remote(struct task_struct *p, int wake_flags)
  1298. {
  1299. struct rq *rq;
  1300. int ret = 0;
  1301. rq = __task_rq_lock(p);
  1302. if (task_on_rq_queued(p)) {
  1303. /* check_preempt_curr() may use rq clock */
  1304. update_rq_clock(rq);
  1305. ttwu_do_wakeup(rq, p, wake_flags);
  1306. ret = 1;
  1307. }
  1308. __task_rq_unlock(rq);
  1309. return ret;
  1310. }
  1311. #ifdef CONFIG_SMP
  1312. void sched_ttwu_pending(void)
  1313. {
  1314. struct rq *rq = this_rq();
  1315. struct llist_node *llist = llist_del_all(&rq->wake_list);
  1316. struct task_struct *p;
  1317. unsigned long flags;
  1318. if (!llist)
  1319. return;
  1320. raw_spin_lock_irqsave(&rq->lock, flags);
  1321. while (llist) {
  1322. p = llist_entry(llist, struct task_struct, wake_entry);
  1323. llist = llist_next(llist);
  1324. ttwu_do_activate(rq, p, 0);
  1325. }
  1326. raw_spin_unlock_irqrestore(&rq->lock, flags);
  1327. }
  1328. void scheduler_ipi(void)
  1329. {
  1330. /*
  1331. * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
  1332. * TIF_NEED_RESCHED remotely (for the first time) will also send
  1333. * this IPI.
  1334. */
  1335. preempt_fold_need_resched();
  1336. if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
  1337. return;
  1338. /*
  1339. * Not all reschedule IPI handlers call irq_enter/irq_exit, since
  1340. * traditionally all their work was done from the interrupt return
  1341. * path. Now that we actually do some work, we need to make sure
  1342. * we do call them.
  1343. *
  1344. * Some archs already do call them, luckily irq_enter/exit nest
  1345. * properly.
  1346. *
  1347. * Arguably we should visit all archs and update all handlers,
  1348. * however a fair share of IPIs are still resched only so this would
  1349. * somewhat pessimize the simple resched case.
  1350. */
  1351. irq_enter();
  1352. sched_ttwu_pending();
  1353. /*
  1354. * Check if someone kicked us for doing the nohz idle load balance.
  1355. */
  1356. if (unlikely(got_nohz_idle_kick())) {
  1357. this_rq()->idle_balance = 1;
  1358. raise_softirq_irqoff(SCHED_SOFTIRQ);
  1359. }
  1360. irq_exit();
  1361. }
  1362. static void ttwu_queue_remote(struct task_struct *p, int cpu)
  1363. {
  1364. struct rq *rq = cpu_rq(cpu);
  1365. if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) {
  1366. if (!set_nr_if_polling(rq->idle))
  1367. smp_send_reschedule(cpu);
  1368. else
  1369. trace_sched_wake_idle_without_ipi(cpu);
  1370. }
  1371. }
  1372. void wake_up_if_idle(int cpu)
  1373. {
  1374. struct rq *rq = cpu_rq(cpu);
  1375. unsigned long flags;
  1376. rcu_read_lock();
  1377. if (!is_idle_task(rcu_dereference(rq->curr)))
  1378. goto out;
  1379. if (set_nr_if_polling(rq->idle)) {
  1380. trace_sched_wake_idle_without_ipi(cpu);
  1381. } else {
  1382. raw_spin_lock_irqsave(&rq->lock, flags);
  1383. if (is_idle_task(rq->curr))
  1384. smp_send_reschedule(cpu);
  1385. /* Else cpu is not in idle, do nothing here */
  1386. raw_spin_unlock_irqrestore(&rq->lock, flags);
  1387. }
  1388. out:
  1389. rcu_read_unlock();
  1390. }
  1391. bool cpus_share_cache(int this_cpu, int that_cpu)
  1392. {
  1393. return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
  1394. }
  1395. #endif /* CONFIG_SMP */
  1396. static void ttwu_queue(struct task_struct *p, int cpu)
  1397. {
  1398. struct rq *rq = cpu_rq(cpu);
  1399. #if defined(CONFIG_SMP)
  1400. if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
  1401. sched_clock_cpu(cpu); /* sync clocks x-cpu */
  1402. ttwu_queue_remote(p, cpu);
  1403. return;
  1404. }
  1405. #endif
  1406. raw_spin_lock(&rq->lock);
  1407. ttwu_do_activate(rq, p, 0);
  1408. raw_spin_unlock(&rq->lock);
  1409. }
  1410. /**
  1411. * try_to_wake_up - wake up a thread
  1412. * @p: the thread to be awakened
  1413. * @state: the mask of task states that can be woken
  1414. * @wake_flags: wake modifier flags (WF_*)
  1415. *
  1416. * Put it on the run-queue if it's not already there. The "current"
  1417. * thread is always on the run-queue (except when the actual
  1418. * re-schedule is in progress), and as such you're allowed to do
  1419. * the simpler "current->state = TASK_RUNNING" to mark yourself
  1420. * runnable without the overhead of this.
  1421. *
  1422. * Return: %true if @p was woken up, %false if it was already running.
  1423. * or @state didn't match @p's state.
  1424. */
  1425. static int
  1426. try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
  1427. {
  1428. unsigned long flags;
  1429. int cpu, success = 0;
  1430. /*
  1431. * If we are going to wake up a thread waiting for CONDITION we
  1432. * need to ensure that CONDITION=1 done by the caller can not be
  1433. * reordered with p->state check below. This pairs with mb() in
  1434. * set_current_state() the waiting thread does.
  1435. */
  1436. smp_mb__before_spinlock();
  1437. raw_spin_lock_irqsave(&p->pi_lock, flags);
  1438. if (!(p->state & state))
  1439. goto out;
  1440. success = 1; /* we're going to change ->state */
  1441. cpu = task_cpu(p);
  1442. if (p->on_rq && ttwu_remote(p, wake_flags))
  1443. goto stat;
  1444. #ifdef CONFIG_SMP
  1445. /*
  1446. * If the owning (remote) cpu is still in the middle of schedule() with
  1447. * this task as prev, wait until its done referencing the task.
  1448. */
  1449. while (p->on_cpu)
  1450. cpu_relax();
  1451. /*
  1452. * Pairs with the smp_wmb() in finish_lock_switch().
  1453. */
  1454. smp_rmb();
  1455. p->sched_contributes_to_load = !!task_contributes_to_load(p);
  1456. p->state = TASK_WAKING;
  1457. if (p->sched_class->task_waking)
  1458. p->sched_class->task_waking(p);
  1459. cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
  1460. if (task_cpu(p) != cpu) {
  1461. wake_flags |= WF_MIGRATED;
  1462. set_task_cpu(p, cpu);
  1463. }
  1464. #endif /* CONFIG_SMP */
  1465. ttwu_queue(p, cpu);
  1466. stat:
  1467. ttwu_stat(p, cpu, wake_flags);
  1468. out:
  1469. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  1470. return success;
  1471. }
  1472. /**
  1473. * try_to_wake_up_local - try to wake up a local task with rq lock held
  1474. * @p: the thread to be awakened
  1475. *
  1476. * Put @p on the run-queue if it's not already there. The caller must
  1477. * ensure that this_rq() is locked, @p is bound to this_rq() and not
  1478. * the current task.
  1479. */
  1480. static void try_to_wake_up_local(struct task_struct *p)
  1481. {
  1482. struct rq *rq = task_rq(p);
  1483. if (WARN_ON_ONCE(rq != this_rq()) ||
  1484. WARN_ON_ONCE(p == current))
  1485. return;
  1486. lockdep_assert_held(&rq->lock);
  1487. if (!raw_spin_trylock(&p->pi_lock)) {
  1488. raw_spin_unlock(&rq->lock);
  1489. raw_spin_lock(&p->pi_lock);
  1490. raw_spin_lock(&rq->lock);
  1491. }
  1492. if (!(p->state & TASK_NORMAL))
  1493. goto out;
  1494. if (!task_on_rq_queued(p))
  1495. ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  1496. ttwu_do_wakeup(rq, p, 0);
  1497. ttwu_stat(p, smp_processor_id(), 0);
  1498. out:
  1499. raw_spin_unlock(&p->pi_lock);
  1500. }
  1501. /**
  1502. * wake_up_process - Wake up a specific process
  1503. * @p: The process to be woken up.
  1504. *
  1505. * Attempt to wake up the nominated process and move it to the set of runnable
  1506. * processes.
  1507. *
  1508. * Return: 1 if the process was woken up, 0 if it was already running.
  1509. *
  1510. * It may be assumed that this function implies a write memory barrier before
  1511. * changing the task state if and only if any tasks are woken up.
  1512. */
  1513. int wake_up_process(struct task_struct *p)
  1514. {
  1515. WARN_ON(task_is_stopped_or_traced(p));
  1516. return try_to_wake_up(p, TASK_NORMAL, 0);
  1517. }
  1518. EXPORT_SYMBOL(wake_up_process);
  1519. int wake_up_state(struct task_struct *p, unsigned int state)
  1520. {
  1521. return try_to_wake_up(p, state, 0);
  1522. }
  1523. /*
  1524. * This function clears the sched_dl_entity static params.
  1525. */
  1526. void __dl_clear_params(struct task_struct *p)
  1527. {
  1528. struct sched_dl_entity *dl_se = &p->dl;
  1529. dl_se->dl_runtime = 0;
  1530. dl_se->dl_deadline = 0;
  1531. dl_se->dl_period = 0;
  1532. dl_se->flags = 0;
  1533. dl_se->dl_bw = 0;
  1534. dl_se->dl_throttled = 0;
  1535. dl_se->dl_new = 1;
  1536. dl_se->dl_yielded = 0;
  1537. }
  1538. /*
  1539. * Perform scheduler related setup for a newly forked process p.
  1540. * p is forked by current.
  1541. *
  1542. * __sched_fork() is basic setup used by init_idle() too:
  1543. */
  1544. static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
  1545. {
  1546. p->on_rq = 0;
  1547. p->se.on_rq = 0;
  1548. p->se.exec_start = 0;
  1549. p->se.sum_exec_runtime = 0;
  1550. p->se.prev_sum_exec_runtime = 0;
  1551. p->se.nr_migrations = 0;
  1552. p->se.vruntime = 0;
  1553. #ifdef CONFIG_SMP
  1554. p->se.avg.decay_count = 0;
  1555. #endif
  1556. INIT_LIST_HEAD(&p->se.group_node);
  1557. #ifdef CONFIG_SCHEDSTATS
  1558. memset(&p->se.statistics, 0, sizeof(p->se.statistics));
  1559. #endif
  1560. RB_CLEAR_NODE(&p->dl.rb_node);
  1561. init_dl_task_timer(&p->dl);
  1562. __dl_clear_params(p);
  1563. INIT_LIST_HEAD(&p->rt.run_list);
  1564. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1565. INIT_HLIST_HEAD(&p->preempt_notifiers);
  1566. #endif
  1567. #ifdef CONFIG_NUMA_BALANCING
  1568. if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
  1569. p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
  1570. p->mm->numa_scan_seq = 0;
  1571. }
  1572. if (clone_flags & CLONE_VM)
  1573. p->numa_preferred_nid = current->numa_preferred_nid;
  1574. else
  1575. p->numa_preferred_nid = -1;
  1576. p->node_stamp = 0ULL;
  1577. p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
  1578. p->numa_scan_period = sysctl_numa_balancing_scan_delay;
  1579. p->numa_work.next = &p->numa_work;
  1580. p->numa_faults = NULL;
  1581. p->last_task_numa_placement = 0;
  1582. p->last_sum_exec_runtime = 0;
  1583. p->numa_group = NULL;
  1584. #endif /* CONFIG_NUMA_BALANCING */
  1585. }
  1586. #ifdef CONFIG_NUMA_BALANCING
  1587. #ifdef CONFIG_SCHED_DEBUG
  1588. void set_numabalancing_state(bool enabled)
  1589. {
  1590. if (enabled)
  1591. sched_feat_set("NUMA");
  1592. else
  1593. sched_feat_set("NO_NUMA");
  1594. }
  1595. #else
  1596. __read_mostly bool numabalancing_enabled;
  1597. void set_numabalancing_state(bool enabled)
  1598. {
  1599. numabalancing_enabled = enabled;
  1600. }
  1601. #endif /* CONFIG_SCHED_DEBUG */
  1602. #ifdef CONFIG_PROC_SYSCTL
  1603. int sysctl_numa_balancing(struct ctl_table *table, int write,
  1604. void __user *buffer, size_t *lenp, loff_t *ppos)
  1605. {
  1606. struct ctl_table t;
  1607. int err;
  1608. int state = numabalancing_enabled;
  1609. if (write && !capable(CAP_SYS_ADMIN))
  1610. return -EPERM;
  1611. t = *table;
  1612. t.data = &state;
  1613. err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
  1614. if (err < 0)
  1615. return err;
  1616. if (write)
  1617. set_numabalancing_state(state);
  1618. return err;
  1619. }
  1620. #endif
  1621. #endif
  1622. /*
  1623. * fork()/clone()-time setup:
  1624. */
  1625. int sched_fork(unsigned long clone_flags, struct task_struct *p)
  1626. {
  1627. unsigned long flags;
  1628. int cpu = get_cpu();
  1629. __sched_fork(clone_flags, p);
  1630. /*
  1631. * We mark the process as running here. This guarantees that
  1632. * nobody will actually run it, and a signal or other external
  1633. * event cannot wake it up and insert it on the runqueue either.
  1634. */
  1635. p->state = TASK_RUNNING;
  1636. /*
  1637. * Make sure we do not leak PI boosting priority to the child.
  1638. */
  1639. p->prio = current->normal_prio;
  1640. /*
  1641. * Revert to default priority/policy on fork if requested.
  1642. */
  1643. if (unlikely(p->sched_reset_on_fork)) {
  1644. if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
  1645. p->policy = SCHED_NORMAL;
  1646. p->static_prio = NICE_TO_PRIO(0);
  1647. p->rt_priority = 0;
  1648. } else if (PRIO_TO_NICE(p->static_prio) < 0)
  1649. p->static_prio = NICE_TO_PRIO(0);
  1650. p->prio = p->normal_prio = __normal_prio(p);
  1651. set_load_weight(p);
  1652. /*
  1653. * We don't need the reset flag anymore after the fork. It has
  1654. * fulfilled its duty:
  1655. */
  1656. p->sched_reset_on_fork = 0;
  1657. }
  1658. if (dl_prio(p->prio)) {
  1659. put_cpu();
  1660. return -EAGAIN;
  1661. } else if (rt_prio(p->prio)) {
  1662. p->sched_class = &rt_sched_class;
  1663. } else {
  1664. p->sched_class = &fair_sched_class;
  1665. }
  1666. if (p->sched_class->task_fork)
  1667. p->sched_class->task_fork(p);
  1668. /*
  1669. * The child is not yet in the pid-hash so no cgroup attach races,
  1670. * and the cgroup is pinned to this child due to cgroup_fork()
  1671. * is ran before sched_fork().
  1672. *
  1673. * Silence PROVE_RCU.
  1674. */
  1675. raw_spin_lock_irqsave(&p->pi_lock, flags);
  1676. set_task_cpu(p, cpu);
  1677. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  1678. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  1679. if (likely(sched_info_on()))
  1680. memset(&p->sched_info, 0, sizeof(p->sched_info));
  1681. #endif
  1682. #if defined(CONFIG_SMP)
  1683. p->on_cpu = 0;
  1684. #endif
  1685. init_task_preempt_count(p);
  1686. #ifdef CONFIG_SMP
  1687. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  1688. RB_CLEAR_NODE(&p->pushable_dl_tasks);
  1689. #endif
  1690. put_cpu();
  1691. return 0;
  1692. }
  1693. unsigned long to_ratio(u64 period, u64 runtime)
  1694. {
  1695. if (runtime == RUNTIME_INF)
  1696. return 1ULL << 20;
  1697. /*
  1698. * Doing this here saves a lot of checks in all
  1699. * the calling paths, and returning zero seems
  1700. * safe for them anyway.
  1701. */
  1702. if (period == 0)
  1703. return 0;
  1704. return div64_u64(runtime << 20, period);
  1705. }
  1706. #ifdef CONFIG_SMP
  1707. inline struct dl_bw *dl_bw_of(int i)
  1708. {
  1709. rcu_lockdep_assert(rcu_read_lock_sched_held(),
  1710. "sched RCU must be held");
  1711. return &cpu_rq(i)->rd->dl_bw;
  1712. }
  1713. static inline int dl_bw_cpus(int i)
  1714. {
  1715. struct root_domain *rd = cpu_rq(i)->rd;
  1716. int cpus = 0;
  1717. rcu_lockdep_assert(rcu_read_lock_sched_held(),
  1718. "sched RCU must be held");
  1719. for_each_cpu_and(i, rd->span, cpu_active_mask)
  1720. cpus++;
  1721. return cpus;
  1722. }
  1723. #else
  1724. inline struct dl_bw *dl_bw_of(int i)
  1725. {
  1726. return &cpu_rq(i)->dl.dl_bw;
  1727. }
  1728. static inline int dl_bw_cpus(int i)
  1729. {
  1730. return 1;
  1731. }
  1732. #endif
  1733. /*
  1734. * We must be sure that accepting a new task (or allowing changing the
  1735. * parameters of an existing one) is consistent with the bandwidth
  1736. * constraints. If yes, this function also accordingly updates the currently
  1737. * allocated bandwidth to reflect the new situation.
  1738. *
  1739. * This function is called while holding p's rq->lock.
  1740. *
  1741. * XXX we should delay bw change until the task's 0-lag point, see
  1742. * __setparam_dl().
  1743. */
  1744. static int dl_overflow(struct task_struct *p, int policy,
  1745. const struct sched_attr *attr)
  1746. {
  1747. struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
  1748. u64 period = attr->sched_period ?: attr->sched_deadline;
  1749. u64 runtime = attr->sched_runtime;
  1750. u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
  1751. int cpus, err = -1;
  1752. if (new_bw == p->dl.dl_bw)
  1753. return 0;
  1754. /*
  1755. * Either if a task, enters, leave, or stays -deadline but changes
  1756. * its parameters, we may need to update accordingly the total
  1757. * allocated bandwidth of the container.
  1758. */
  1759. raw_spin_lock(&dl_b->lock);
  1760. cpus = dl_bw_cpus(task_cpu(p));
  1761. if (dl_policy(policy) && !task_has_dl_policy(p) &&
  1762. !__dl_overflow(dl_b, cpus, 0, new_bw)) {
  1763. __dl_add(dl_b, new_bw);
  1764. err = 0;
  1765. } else if (dl_policy(policy) && task_has_dl_policy(p) &&
  1766. !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
  1767. __dl_clear(dl_b, p->dl.dl_bw);
  1768. __dl_add(dl_b, new_bw);
  1769. err = 0;
  1770. } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
  1771. __dl_clear(dl_b, p->dl.dl_bw);
  1772. err = 0;
  1773. }
  1774. raw_spin_unlock(&dl_b->lock);
  1775. return err;
  1776. }
  1777. extern void init_dl_bw(struct dl_bw *dl_b);
  1778. /*
  1779. * wake_up_new_task - wake up a newly created task for the first time.
  1780. *
  1781. * This function will do some initial scheduler statistics housekeeping
  1782. * that must be done for every newly created context, then puts the task
  1783. * on the runqueue and wakes it.
  1784. */
  1785. void wake_up_new_task(struct task_struct *p)
  1786. {
  1787. unsigned long flags;
  1788. struct rq *rq;
  1789. raw_spin_lock_irqsave(&p->pi_lock, flags);
  1790. #ifdef CONFIG_SMP
  1791. /*
  1792. * Fork balancing, do it here and not earlier because:
  1793. * - cpus_allowed can change in the fork path
  1794. * - any previously selected cpu might disappear through hotplug
  1795. */
  1796. set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
  1797. #endif
  1798. /* Initialize new task's runnable average */
  1799. init_task_runnable_average(p);
  1800. rq = __task_rq_lock(p);
  1801. activate_task(rq, p, 0);
  1802. p->on_rq = TASK_ON_RQ_QUEUED;
  1803. trace_sched_wakeup_new(p, true);
  1804. check_preempt_curr(rq, p, WF_FORK);
  1805. #ifdef CONFIG_SMP
  1806. if (p->sched_class->task_woken)
  1807. p->sched_class->task_woken(rq, p);
  1808. #endif
  1809. task_rq_unlock(rq, p, &flags);
  1810. }
  1811. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1812. static struct static_key preempt_notifier_key = STATIC_KEY_INIT_FALSE;
  1813. /**
  1814. * preempt_notifier_register - tell me when current is being preempted & rescheduled
  1815. * @notifier: notifier struct to register
  1816. */
  1817. void preempt_notifier_register(struct preempt_notifier *notifier)
  1818. {
  1819. static_key_slow_inc(&preempt_notifier_key);
  1820. hlist_add_head(&notifier->link, &current->preempt_notifiers);
  1821. }
  1822. EXPORT_SYMBOL_GPL(preempt_notifier_register);
  1823. /**
  1824. * preempt_notifier_unregister - no longer interested in preemption notifications
  1825. * @notifier: notifier struct to unregister
  1826. *
  1827. * This is *not* safe to call from within a preemption notifier.
  1828. */
  1829. void preempt_notifier_unregister(struct preempt_notifier *notifier)
  1830. {
  1831. hlist_del(&notifier->link);
  1832. static_key_slow_dec(&preempt_notifier_key);
  1833. }
  1834. EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
  1835. static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1836. {
  1837. struct preempt_notifier *notifier;
  1838. hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
  1839. notifier->ops->sched_in(notifier, raw_smp_processor_id());
  1840. }
  1841. static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1842. {
  1843. if (static_key_false(&preempt_notifier_key))
  1844. __fire_sched_in_preempt_notifiers(curr);
  1845. }
  1846. static void
  1847. __fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1848. struct task_struct *next)
  1849. {
  1850. struct preempt_notifier *notifier;
  1851. hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
  1852. notifier->ops->sched_out(notifier, next);
  1853. }
  1854. static __always_inline void
  1855. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1856. struct task_struct *next)
  1857. {
  1858. if (static_key_false(&preempt_notifier_key))
  1859. __fire_sched_out_preempt_notifiers(curr, next);
  1860. }
  1861. #else /* !CONFIG_PREEMPT_NOTIFIERS */
  1862. static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1863. {
  1864. }
  1865. static inline void
  1866. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1867. struct task_struct *next)
  1868. {
  1869. }
  1870. #endif /* CONFIG_PREEMPT_NOTIFIERS */
  1871. /**
  1872. * prepare_task_switch - prepare to switch tasks
  1873. * @rq: the runqueue preparing to switch
  1874. * @prev: the current task that is being switched out
  1875. * @next: the task we are going to switch to.
  1876. *
  1877. * This is called with the rq lock held and interrupts off. It must
  1878. * be paired with a subsequent finish_task_switch after the context
  1879. * switch.
  1880. *
  1881. * prepare_task_switch sets up locking and calls architecture specific
  1882. * hooks.
  1883. */
  1884. static inline void
  1885. prepare_task_switch(struct rq *rq, struct task_struct *prev,
  1886. struct task_struct *next)
  1887. {
  1888. trace_sched_switch(prev, next);
  1889. sched_info_switch(rq, prev, next);
  1890. perf_event_task_sched_out(prev, next);
  1891. fire_sched_out_preempt_notifiers(prev, next);
  1892. prepare_lock_switch(rq, next);
  1893. prepare_arch_switch(next);
  1894. }
  1895. /**
  1896. * finish_task_switch - clean up after a task-switch
  1897. * @prev: the thread we just switched away from.
  1898. *
  1899. * finish_task_switch must be called after the context switch, paired
  1900. * with a prepare_task_switch call before the context switch.
  1901. * finish_task_switch will reconcile locking set up by prepare_task_switch,
  1902. * and do any other architecture-specific cleanup actions.
  1903. *
  1904. * Note that we may have delayed dropping an mm in context_switch(). If
  1905. * so, we finish that here outside of the runqueue lock. (Doing it
  1906. * with the lock held can cause deadlocks; see schedule() for
  1907. * details.)
  1908. *
  1909. * The context switch have flipped the stack from under us and restored the
  1910. * local variables which were saved when this task called schedule() in the
  1911. * past. prev == current is still correct but we need to recalculate this_rq
  1912. * because prev may have moved to another CPU.
  1913. */
  1914. static struct rq *finish_task_switch(struct task_struct *prev)
  1915. __releases(rq->lock)
  1916. {
  1917. struct rq *rq = this_rq();
  1918. struct mm_struct *mm = rq->prev_mm;
  1919. long prev_state;
  1920. rq->prev_mm = NULL;
  1921. /*
  1922. * A task struct has one reference for the use as "current".
  1923. * If a task dies, then it sets TASK_DEAD in tsk->state and calls
  1924. * schedule one last time. The schedule call will never return, and
  1925. * the scheduled task must drop that reference.
  1926. * The test for TASK_DEAD must occur while the runqueue locks are
  1927. * still held, otherwise prev could be scheduled on another cpu, die
  1928. * there before we look at prev->state, and then the reference would
  1929. * be dropped twice.
  1930. * Manfred Spraul <manfred@colorfullife.com>
  1931. */
  1932. prev_state = prev->state;
  1933. vtime_task_switch(prev);
  1934. finish_arch_switch(prev);
  1935. perf_event_task_sched_in(prev, current);
  1936. finish_lock_switch(rq, prev);
  1937. finish_arch_post_lock_switch();
  1938. fire_sched_in_preempt_notifiers(current);
  1939. if (mm)
  1940. mmdrop(mm);
  1941. if (unlikely(prev_state == TASK_DEAD)) {
  1942. if (prev->sched_class->task_dead)
  1943. prev->sched_class->task_dead(prev);
  1944. /*
  1945. * Remove function-return probe instances associated with this
  1946. * task and put them back on the free list.
  1947. */
  1948. kprobe_flush_task(prev);
  1949. put_task_struct(prev);
  1950. }
  1951. tick_nohz_task_switch(current);
  1952. return rq;
  1953. }
  1954. #ifdef CONFIG_SMP
  1955. /* rq->lock is NOT held, but preemption is disabled */
  1956. static inline void post_schedule(struct rq *rq)
  1957. {
  1958. if (rq->post_schedule) {
  1959. unsigned long flags;
  1960. raw_spin_lock_irqsave(&rq->lock, flags);
  1961. if (rq->curr->sched_class->post_schedule)
  1962. rq->curr->sched_class->post_schedule(rq);
  1963. raw_spin_unlock_irqrestore(&rq->lock, flags);
  1964. rq->post_schedule = 0;
  1965. }
  1966. }
  1967. #else
  1968. static inline void post_schedule(struct rq *rq)
  1969. {
  1970. }
  1971. #endif
  1972. /**
  1973. * schedule_tail - first thing a freshly forked thread must call.
  1974. * @prev: the thread we just switched away from.
  1975. */
  1976. asmlinkage __visible void schedule_tail(struct task_struct *prev)
  1977. __releases(rq->lock)
  1978. {
  1979. struct rq *rq;
  1980. /* finish_task_switch() drops rq->lock and enables preemtion */
  1981. preempt_disable();
  1982. rq = finish_task_switch(prev);
  1983. post_schedule(rq);
  1984. preempt_enable();
  1985. if (current->set_child_tid)
  1986. put_user(task_pid_vnr(current), current->set_child_tid);
  1987. }
  1988. /*
  1989. * context_switch - switch to the new MM and the new thread's register state.
  1990. */
  1991. static inline struct rq *
  1992. context_switch(struct rq *rq, struct task_struct *prev,
  1993. struct task_struct *next)
  1994. {
  1995. struct mm_struct *mm, *oldmm;
  1996. prepare_task_switch(rq, prev, next);
  1997. mm = next->mm;
  1998. oldmm = prev->active_mm;
  1999. /*
  2000. * For paravirt, this is coupled with an exit in switch_to to
  2001. * combine the page table reload and the switch backend into
  2002. * one hypercall.
  2003. */
  2004. arch_start_context_switch(prev);
  2005. if (!mm) {
  2006. next->active_mm = oldmm;
  2007. atomic_inc(&oldmm->mm_count);
  2008. enter_lazy_tlb(oldmm, next);
  2009. } else
  2010. switch_mm(oldmm, mm, next);
  2011. if (!prev->mm) {
  2012. prev->active_mm = NULL;
  2013. rq->prev_mm = oldmm;
  2014. }
  2015. /*
  2016. * Since the runqueue lock will be released by the next
  2017. * task (which is an invalid locking op but in the case
  2018. * of the scheduler it's an obvious special-case), so we
  2019. * do an early lockdep release here:
  2020. */
  2021. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  2022. /* Here we just switch the register state and the stack. */
  2023. switch_to(prev, next, prev);
  2024. barrier();
  2025. return finish_task_switch(prev);
  2026. }
  2027. /*
  2028. * nr_running and nr_context_switches:
  2029. *
  2030. * externally visible scheduler statistics: current number of runnable
  2031. * threads, total number of context switches performed since bootup.
  2032. */
  2033. unsigned long nr_running(void)
  2034. {
  2035. unsigned long i, sum = 0;
  2036. for_each_online_cpu(i)
  2037. sum += cpu_rq(i)->nr_running;
  2038. return sum;
  2039. }
  2040. /*
  2041. * Check if only the current task is running on the cpu.
  2042. */
  2043. bool single_task_running(void)
  2044. {
  2045. if (cpu_rq(smp_processor_id())->nr_running == 1)
  2046. return true;
  2047. else
  2048. return false;
  2049. }
  2050. EXPORT_SYMBOL(single_task_running);
  2051. unsigned long long nr_context_switches(void)
  2052. {
  2053. int i;
  2054. unsigned long long sum = 0;
  2055. for_each_possible_cpu(i)
  2056. sum += cpu_rq(i)->nr_switches;
  2057. return sum;
  2058. }
  2059. unsigned long nr_iowait(void)
  2060. {
  2061. unsigned long i, sum = 0;
  2062. for_each_possible_cpu(i)
  2063. sum += atomic_read(&cpu_rq(i)->nr_iowait);
  2064. return sum;
  2065. }
  2066. unsigned long nr_iowait_cpu(int cpu)
  2067. {
  2068. struct rq *this = cpu_rq(cpu);
  2069. return atomic_read(&this->nr_iowait);
  2070. }
  2071. void get_iowait_load(unsigned long *nr_waiters, unsigned long *load)
  2072. {
  2073. struct rq *rq = this_rq();
  2074. *nr_waiters = atomic_read(&rq->nr_iowait);
  2075. *load = rq->load.weight;
  2076. }
  2077. #ifdef CONFIG_SMP
  2078. /*
  2079. * sched_exec - execve() is a valuable balancing opportunity, because at
  2080. * this point the task has the smallest effective memory and cache footprint.
  2081. */
  2082. void sched_exec(void)
  2083. {
  2084. struct task_struct *p = current;
  2085. unsigned long flags;
  2086. int dest_cpu;
  2087. raw_spin_lock_irqsave(&p->pi_lock, flags);
  2088. dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0);
  2089. if (dest_cpu == smp_processor_id())
  2090. goto unlock;
  2091. if (likely(cpu_active(dest_cpu))) {
  2092. struct migration_arg arg = { p, dest_cpu };
  2093. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  2094. stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
  2095. return;
  2096. }
  2097. unlock:
  2098. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  2099. }
  2100. #endif
  2101. DEFINE_PER_CPU(struct kernel_stat, kstat);
  2102. DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
  2103. EXPORT_PER_CPU_SYMBOL(kstat);
  2104. EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
  2105. /*
  2106. * Return accounted runtime for the task.
  2107. * In case the task is currently running, return the runtime plus current's
  2108. * pending runtime that have not been accounted yet.
  2109. */
  2110. unsigned long long task_sched_runtime(struct task_struct *p)
  2111. {
  2112. unsigned long flags;
  2113. struct rq *rq;
  2114. u64 ns;
  2115. #if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
  2116. /*
  2117. * 64-bit doesn't need locks to atomically read a 64bit value.
  2118. * So we have a optimization chance when the task's delta_exec is 0.
  2119. * Reading ->on_cpu is racy, but this is ok.
  2120. *
  2121. * If we race with it leaving cpu, we'll take a lock. So we're correct.
  2122. * If we race with it entering cpu, unaccounted time is 0. This is
  2123. * indistinguishable from the read occurring a few cycles earlier.
  2124. * If we see ->on_cpu without ->on_rq, the task is leaving, and has
  2125. * been accounted, so we're correct here as well.
  2126. */
  2127. if (!p->on_cpu || !task_on_rq_queued(p))
  2128. return p->se.sum_exec_runtime;
  2129. #endif
  2130. rq = task_rq_lock(p, &flags);
  2131. /*
  2132. * Must be ->curr _and_ ->on_rq. If dequeued, we would
  2133. * project cycles that may never be accounted to this
  2134. * thread, breaking clock_gettime().
  2135. */
  2136. if (task_current(rq, p) && task_on_rq_queued(p)) {
  2137. update_rq_clock(rq);
  2138. p->sched_class->update_curr(rq);
  2139. }
  2140. ns = p->se.sum_exec_runtime;
  2141. task_rq_unlock(rq, p, &flags);
  2142. return ns;
  2143. }
  2144. /*
  2145. * This function gets called by the timer code, with HZ frequency.
  2146. * We call it with interrupts disabled.
  2147. */
  2148. void scheduler_tick(void)
  2149. {
  2150. int cpu = smp_processor_id();
  2151. struct rq *rq = cpu_rq(cpu);
  2152. struct task_struct *curr = rq->curr;
  2153. sched_clock_tick();
  2154. raw_spin_lock(&rq->lock);
  2155. update_rq_clock(rq);
  2156. curr->sched_class->task_tick(rq, curr, 0);
  2157. update_cpu_load_active(rq);
  2158. calc_global_load_tick(rq);
  2159. raw_spin_unlock(&rq->lock);
  2160. perf_event_task_tick();
  2161. #ifdef CONFIG_SMP
  2162. rq->idle_balance = idle_cpu(cpu);
  2163. trigger_load_balance(rq);
  2164. #endif
  2165. rq_last_tick_reset(rq);
  2166. }
  2167. #ifdef CONFIG_NO_HZ_FULL
  2168. /**
  2169. * scheduler_tick_max_deferment
  2170. *
  2171. * Keep at least one tick per second when a single
  2172. * active task is running because the scheduler doesn't
  2173. * yet completely support full dynticks environment.
  2174. *
  2175. * This makes sure that uptime, CFS vruntime, load
  2176. * balancing, etc... continue to move forward, even
  2177. * with a very low granularity.
  2178. *
  2179. * Return: Maximum deferment in nanoseconds.
  2180. */
  2181. u64 scheduler_tick_max_deferment(void)
  2182. {
  2183. struct rq *rq = this_rq();
  2184. unsigned long next, now = READ_ONCE(jiffies);
  2185. next = rq->last_sched_tick + HZ;
  2186. if (time_before_eq(next, now))
  2187. return 0;
  2188. return jiffies_to_nsecs(next - now);
  2189. }
  2190. #endif
  2191. notrace unsigned long get_parent_ip(unsigned long addr)
  2192. {
  2193. if (in_lock_functions(addr)) {
  2194. addr = CALLER_ADDR2;
  2195. if (in_lock_functions(addr))
  2196. addr = CALLER_ADDR3;
  2197. }
  2198. return addr;
  2199. }
  2200. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  2201. defined(CONFIG_PREEMPT_TRACER))
  2202. void preempt_count_add(int val)
  2203. {
  2204. #ifdef CONFIG_DEBUG_PREEMPT
  2205. /*
  2206. * Underflow?
  2207. */
  2208. if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
  2209. return;
  2210. #endif
  2211. __preempt_count_add(val);
  2212. #ifdef CONFIG_DEBUG_PREEMPT
  2213. /*
  2214. * Spinlock count overflowing soon?
  2215. */
  2216. DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
  2217. PREEMPT_MASK - 10);
  2218. #endif
  2219. if (preempt_count() == val) {
  2220. unsigned long ip = get_parent_ip(CALLER_ADDR1);
  2221. #ifdef CONFIG_DEBUG_PREEMPT
  2222. current->preempt_disable_ip = ip;
  2223. #endif
  2224. trace_preempt_off(CALLER_ADDR0, ip);
  2225. }
  2226. }
  2227. EXPORT_SYMBOL(preempt_count_add);
  2228. NOKPROBE_SYMBOL(preempt_count_add);
  2229. void preempt_count_sub(int val)
  2230. {
  2231. #ifdef CONFIG_DEBUG_PREEMPT
  2232. /*
  2233. * Underflow?
  2234. */
  2235. if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
  2236. return;
  2237. /*
  2238. * Is the spinlock portion underflowing?
  2239. */
  2240. if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
  2241. !(preempt_count() & PREEMPT_MASK)))
  2242. return;
  2243. #endif
  2244. if (preempt_count() == val)
  2245. trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  2246. __preempt_count_sub(val);
  2247. }
  2248. EXPORT_SYMBOL(preempt_count_sub);
  2249. NOKPROBE_SYMBOL(preempt_count_sub);
  2250. #endif
  2251. /*
  2252. * Print scheduling while atomic bug:
  2253. */
  2254. static noinline void __schedule_bug(struct task_struct *prev)
  2255. {
  2256. if (oops_in_progress)
  2257. return;
  2258. printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
  2259. prev->comm, prev->pid, preempt_count());
  2260. debug_show_held_locks(prev);
  2261. print_modules();
  2262. if (irqs_disabled())
  2263. print_irqtrace_events(prev);
  2264. #ifdef CONFIG_DEBUG_PREEMPT
  2265. if (in_atomic_preempt_off()) {
  2266. pr_err("Preemption disabled at:");
  2267. print_ip_sym(current->preempt_disable_ip);
  2268. pr_cont("\n");
  2269. }
  2270. #endif
  2271. dump_stack();
  2272. add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
  2273. }
  2274. /*
  2275. * Various schedule()-time debugging checks and statistics:
  2276. */
  2277. static inline void schedule_debug(struct task_struct *prev)
  2278. {
  2279. #ifdef CONFIG_SCHED_STACK_END_CHECK
  2280. BUG_ON(unlikely(task_stack_end_corrupted(prev)));
  2281. #endif
  2282. /*
  2283. * Test if we are atomic. Since do_exit() needs to call into
  2284. * schedule() atomically, we ignore that path. Otherwise whine
  2285. * if we are scheduling when we should not.
  2286. */
  2287. if (unlikely(in_atomic_preempt_off() && prev->state != TASK_DEAD))
  2288. __schedule_bug(prev);
  2289. rcu_sleep_check();
  2290. profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  2291. schedstat_inc(this_rq(), sched_count);
  2292. }
  2293. /*
  2294. * Pick up the highest-prio task:
  2295. */
  2296. static inline struct task_struct *
  2297. pick_next_task(struct rq *rq, struct task_struct *prev)
  2298. {
  2299. const struct sched_class *class = &fair_sched_class;
  2300. struct task_struct *p;
  2301. /*
  2302. * Optimization: we know that if all tasks are in
  2303. * the fair class we can call that function directly:
  2304. */
  2305. if (likely(prev->sched_class == class &&
  2306. rq->nr_running == rq->cfs.h_nr_running)) {
  2307. p = fair_sched_class.pick_next_task(rq, prev);
  2308. if (unlikely(p == RETRY_TASK))
  2309. goto again;
  2310. /* assumes fair_sched_class->next == idle_sched_class */
  2311. if (unlikely(!p))
  2312. p = idle_sched_class.pick_next_task(rq, prev);
  2313. return p;
  2314. }
  2315. again:
  2316. for_each_class(class) {
  2317. p = class->pick_next_task(rq, prev);
  2318. if (p) {
  2319. if (unlikely(p == RETRY_TASK))
  2320. goto again;
  2321. return p;
  2322. }
  2323. }
  2324. BUG(); /* the idle class will always have a runnable task */
  2325. }
  2326. /*
  2327. * __schedule() is the main scheduler function.
  2328. *
  2329. * The main means of driving the scheduler and thus entering this function are:
  2330. *
  2331. * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
  2332. *
  2333. * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
  2334. * paths. For example, see arch/x86/entry_64.S.
  2335. *
  2336. * To drive preemption between tasks, the scheduler sets the flag in timer
  2337. * interrupt handler scheduler_tick().
  2338. *
  2339. * 3. Wakeups don't really cause entry into schedule(). They add a
  2340. * task to the run-queue and that's it.
  2341. *
  2342. * Now, if the new task added to the run-queue preempts the current
  2343. * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
  2344. * called on the nearest possible occasion:
  2345. *
  2346. * - If the kernel is preemptible (CONFIG_PREEMPT=y):
  2347. *
  2348. * - in syscall or exception context, at the next outmost
  2349. * preempt_enable(). (this might be as soon as the wake_up()'s
  2350. * spin_unlock()!)
  2351. *
  2352. * - in IRQ context, return from interrupt-handler to
  2353. * preemptible context
  2354. *
  2355. * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
  2356. * then at the next:
  2357. *
  2358. * - cond_resched() call
  2359. * - explicit schedule() call
  2360. * - return from syscall or exception to user-space
  2361. * - return from interrupt-handler to user-space
  2362. *
  2363. * WARNING: must be called with preemption disabled!
  2364. */
  2365. static void __sched __schedule(void)
  2366. {
  2367. struct task_struct *prev, *next;
  2368. unsigned long *switch_count;
  2369. struct rq *rq;
  2370. int cpu;
  2371. cpu = smp_processor_id();
  2372. rq = cpu_rq(cpu);
  2373. rcu_note_context_switch();
  2374. prev = rq->curr;
  2375. schedule_debug(prev);
  2376. if (sched_feat(HRTICK))
  2377. hrtick_clear(rq);
  2378. /*
  2379. * Make sure that signal_pending_state()->signal_pending() below
  2380. * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
  2381. * done by the caller to avoid the race with signal_wake_up().
  2382. */
  2383. smp_mb__before_spinlock();
  2384. raw_spin_lock_irq(&rq->lock);
  2385. rq->clock_skip_update <<= 1; /* promote REQ to ACT */
  2386. switch_count = &prev->nivcsw;
  2387. if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  2388. if (unlikely(signal_pending_state(prev->state, prev))) {
  2389. prev->state = TASK_RUNNING;
  2390. } else {
  2391. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  2392. prev->on_rq = 0;
  2393. /*
  2394. * If a worker went to sleep, notify and ask workqueue
  2395. * whether it wants to wake up a task to maintain
  2396. * concurrency.
  2397. */
  2398. if (prev->flags & PF_WQ_WORKER) {
  2399. struct task_struct *to_wakeup;
  2400. to_wakeup = wq_worker_sleeping(prev, cpu);
  2401. if (to_wakeup)
  2402. try_to_wake_up_local(to_wakeup);
  2403. }
  2404. }
  2405. switch_count = &prev->nvcsw;
  2406. }
  2407. if (task_on_rq_queued(prev))
  2408. update_rq_clock(rq);
  2409. next = pick_next_task(rq, prev);
  2410. clear_tsk_need_resched(prev);
  2411. clear_preempt_need_resched();
  2412. rq->clock_skip_update = 0;
  2413. if (likely(prev != next)) {
  2414. rq->nr_switches++;
  2415. rq->curr = next;
  2416. ++*switch_count;
  2417. rq = context_switch(rq, prev, next); /* unlocks the rq */
  2418. cpu = cpu_of(rq);
  2419. } else
  2420. raw_spin_unlock_irq(&rq->lock);
  2421. post_schedule(rq);
  2422. }
  2423. static inline void sched_submit_work(struct task_struct *tsk)
  2424. {
  2425. if (!tsk->state || tsk_is_pi_blocked(tsk))
  2426. return;
  2427. /*
  2428. * If we are going to sleep and we have plugged IO queued,
  2429. * make sure to submit it to avoid deadlocks.
  2430. */
  2431. if (blk_needs_flush_plug(tsk))
  2432. blk_schedule_flush_plug(tsk);
  2433. }
  2434. asmlinkage __visible void __sched schedule(void)
  2435. {
  2436. struct task_struct *tsk = current;
  2437. sched_submit_work(tsk);
  2438. do {
  2439. preempt_disable();
  2440. __schedule();
  2441. sched_preempt_enable_no_resched();
  2442. } while (need_resched());
  2443. }
  2444. EXPORT_SYMBOL(schedule);
  2445. #ifdef CONFIG_CONTEXT_TRACKING
  2446. asmlinkage __visible void __sched schedule_user(void)
  2447. {
  2448. /*
  2449. * If we come here after a random call to set_need_resched(),
  2450. * or we have been woken up remotely but the IPI has not yet arrived,
  2451. * we haven't yet exited the RCU idle mode. Do it here manually until
  2452. * we find a better solution.
  2453. *
  2454. * NB: There are buggy callers of this function. Ideally we
  2455. * should warn if prev_state != CONTEXT_USER, but that will trigger
  2456. * too frequently to make sense yet.
  2457. */
  2458. enum ctx_state prev_state = exception_enter();
  2459. schedule();
  2460. exception_exit(prev_state);
  2461. }
  2462. #endif
  2463. /**
  2464. * schedule_preempt_disabled - called with preemption disabled
  2465. *
  2466. * Returns with preemption disabled. Note: preempt_count must be 1
  2467. */
  2468. void __sched schedule_preempt_disabled(void)
  2469. {
  2470. sched_preempt_enable_no_resched();
  2471. schedule();
  2472. preempt_disable();
  2473. }
  2474. static void __sched notrace preempt_schedule_common(void)
  2475. {
  2476. do {
  2477. preempt_active_enter();
  2478. __schedule();
  2479. preempt_active_exit();
  2480. /*
  2481. * Check again in case we missed a preemption opportunity
  2482. * between schedule and now.
  2483. */
  2484. } while (need_resched());
  2485. }
  2486. #ifdef CONFIG_PREEMPT
  2487. /*
  2488. * this is the entry point to schedule() from in-kernel preemption
  2489. * off of preempt_enable. Kernel preemptions off return from interrupt
  2490. * occur there and call schedule directly.
  2491. */
  2492. asmlinkage __visible void __sched notrace preempt_schedule(void)
  2493. {
  2494. /*
  2495. * If there is a non-zero preempt_count or interrupts are disabled,
  2496. * we do not want to preempt the current task. Just return..
  2497. */
  2498. if (likely(!preemptible()))
  2499. return;
  2500. preempt_schedule_common();
  2501. }
  2502. NOKPROBE_SYMBOL(preempt_schedule);
  2503. EXPORT_SYMBOL(preempt_schedule);
  2504. /**
  2505. * preempt_schedule_notrace - preempt_schedule called by tracing
  2506. *
  2507. * The tracing infrastructure uses preempt_enable_notrace to prevent
  2508. * recursion and tracing preempt enabling caused by the tracing
  2509. * infrastructure itself. But as tracing can happen in areas coming
  2510. * from userspace or just about to enter userspace, a preempt enable
  2511. * can occur before user_exit() is called. This will cause the scheduler
  2512. * to be called when the system is still in usermode.
  2513. *
  2514. * To prevent this, the preempt_enable_notrace will use this function
  2515. * instead of preempt_schedule() to exit user context if needed before
  2516. * calling the scheduler.
  2517. */
  2518. asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
  2519. {
  2520. enum ctx_state prev_ctx;
  2521. if (likely(!preemptible()))
  2522. return;
  2523. do {
  2524. /*
  2525. * Use raw __prempt_count() ops that don't call function.
  2526. * We can't call functions before disabling preemption which
  2527. * disarm preemption tracing recursions.
  2528. */
  2529. __preempt_count_add(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET);
  2530. barrier();
  2531. /*
  2532. * Needs preempt disabled in case user_exit() is traced
  2533. * and the tracer calls preempt_enable_notrace() causing
  2534. * an infinite recursion.
  2535. */
  2536. prev_ctx = exception_enter();
  2537. __schedule();
  2538. exception_exit(prev_ctx);
  2539. barrier();
  2540. __preempt_count_sub(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET);
  2541. } while (need_resched());
  2542. }
  2543. EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
  2544. #endif /* CONFIG_PREEMPT */
  2545. /*
  2546. * this is the entry point to schedule() from kernel preemption
  2547. * off of irq context.
  2548. * Note, that this is called and return with irqs disabled. This will
  2549. * protect us against recursive calling from irq.
  2550. */
  2551. asmlinkage __visible void __sched preempt_schedule_irq(void)
  2552. {
  2553. enum ctx_state prev_state;
  2554. /* Catch callers which need to be fixed */
  2555. BUG_ON(preempt_count() || !irqs_disabled());
  2556. prev_state = exception_enter();
  2557. do {
  2558. preempt_active_enter();
  2559. local_irq_enable();
  2560. __schedule();
  2561. local_irq_disable();
  2562. preempt_active_exit();
  2563. } while (need_resched());
  2564. exception_exit(prev_state);
  2565. }
  2566. int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
  2567. void *key)
  2568. {
  2569. return try_to_wake_up(curr->private, mode, wake_flags);
  2570. }
  2571. EXPORT_SYMBOL(default_wake_function);
  2572. #ifdef CONFIG_RT_MUTEXES
  2573. /*
  2574. * rt_mutex_setprio - set the current priority of a task
  2575. * @p: task
  2576. * @prio: prio value (kernel-internal form)
  2577. *
  2578. * This function changes the 'effective' priority of a task. It does
  2579. * not touch ->normal_prio like __setscheduler().
  2580. *
  2581. * Used by the rt_mutex code to implement priority inheritance
  2582. * logic. Call site only calls if the priority of the task changed.
  2583. */
  2584. void rt_mutex_setprio(struct task_struct *p, int prio)
  2585. {
  2586. int oldprio, queued, running, enqueue_flag = 0;
  2587. struct rq *rq;
  2588. const struct sched_class *prev_class;
  2589. BUG_ON(prio > MAX_PRIO);
  2590. rq = __task_rq_lock(p);
  2591. /*
  2592. * Idle task boosting is a nono in general. There is one
  2593. * exception, when PREEMPT_RT and NOHZ is active:
  2594. *
  2595. * The idle task calls get_next_timer_interrupt() and holds
  2596. * the timer wheel base->lock on the CPU and another CPU wants
  2597. * to access the timer (probably to cancel it). We can safely
  2598. * ignore the boosting request, as the idle CPU runs this code
  2599. * with interrupts disabled and will complete the lock
  2600. * protected section without being interrupted. So there is no
  2601. * real need to boost.
  2602. */
  2603. if (unlikely(p == rq->idle)) {
  2604. WARN_ON(p != rq->curr);
  2605. WARN_ON(p->pi_blocked_on);
  2606. goto out_unlock;
  2607. }
  2608. trace_sched_pi_setprio(p, prio);
  2609. oldprio = p->prio;
  2610. prev_class = p->sched_class;
  2611. queued = task_on_rq_queued(p);
  2612. running = task_current(rq, p);
  2613. if (queued)
  2614. dequeue_task(rq, p, 0);
  2615. if (running)
  2616. put_prev_task(rq, p);
  2617. /*
  2618. * Boosting condition are:
  2619. * 1. -rt task is running and holds mutex A
  2620. * --> -dl task blocks on mutex A
  2621. *
  2622. * 2. -dl task is running and holds mutex A
  2623. * --> -dl task blocks on mutex A and could preempt the
  2624. * running task
  2625. */
  2626. if (dl_prio(prio)) {
  2627. struct task_struct *pi_task = rt_mutex_get_top_task(p);
  2628. if (!dl_prio(p->normal_prio) ||
  2629. (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) {
  2630. p->dl.dl_boosted = 1;
  2631. enqueue_flag = ENQUEUE_REPLENISH;
  2632. } else
  2633. p->dl.dl_boosted = 0;
  2634. p->sched_class = &dl_sched_class;
  2635. } else if (rt_prio(prio)) {
  2636. if (dl_prio(oldprio))
  2637. p->dl.dl_boosted = 0;
  2638. if (oldprio < prio)
  2639. enqueue_flag = ENQUEUE_HEAD;
  2640. p->sched_class = &rt_sched_class;
  2641. } else {
  2642. if (dl_prio(oldprio))
  2643. p->dl.dl_boosted = 0;
  2644. if (rt_prio(oldprio))
  2645. p->rt.timeout = 0;
  2646. p->sched_class = &fair_sched_class;
  2647. }
  2648. p->prio = prio;
  2649. if (running)
  2650. p->sched_class->set_curr_task(rq);
  2651. if (queued)
  2652. enqueue_task(rq, p, enqueue_flag);
  2653. check_class_changed(rq, p, prev_class, oldprio);
  2654. out_unlock:
  2655. __task_rq_unlock(rq);
  2656. }
  2657. #endif
  2658. void set_user_nice(struct task_struct *p, long nice)
  2659. {
  2660. int old_prio, delta, queued;
  2661. unsigned long flags;
  2662. struct rq *rq;
  2663. if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
  2664. return;
  2665. /*
  2666. * We have to be careful, if called from sys_setpriority(),
  2667. * the task might be in the middle of scheduling on another CPU.
  2668. */
  2669. rq = task_rq_lock(p, &flags);
  2670. /*
  2671. * The RT priorities are set via sched_setscheduler(), but we still
  2672. * allow the 'normal' nice value to be set - but as expected
  2673. * it wont have any effect on scheduling until the task is
  2674. * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
  2675. */
  2676. if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
  2677. p->static_prio = NICE_TO_PRIO(nice);
  2678. goto out_unlock;
  2679. }
  2680. queued = task_on_rq_queued(p);
  2681. if (queued)
  2682. dequeue_task(rq, p, 0);
  2683. p->static_prio = NICE_TO_PRIO(nice);
  2684. set_load_weight(p);
  2685. old_prio = p->prio;
  2686. p->prio = effective_prio(p);
  2687. delta = p->prio - old_prio;
  2688. if (queued) {
  2689. enqueue_task(rq, p, 0);
  2690. /*
  2691. * If the task increased its priority or is running and
  2692. * lowered its priority, then reschedule its CPU:
  2693. */
  2694. if (delta < 0 || (delta > 0 && task_running(rq, p)))
  2695. resched_curr(rq);
  2696. }
  2697. out_unlock:
  2698. task_rq_unlock(rq, p, &flags);
  2699. }
  2700. EXPORT_SYMBOL(set_user_nice);
  2701. /*
  2702. * can_nice - check if a task can reduce its nice value
  2703. * @p: task
  2704. * @nice: nice value
  2705. */
  2706. int can_nice(const struct task_struct *p, const int nice)
  2707. {
  2708. /* convert nice value [19,-20] to rlimit style value [1,40] */
  2709. int nice_rlim = nice_to_rlimit(nice);
  2710. return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
  2711. capable(CAP_SYS_NICE));
  2712. }
  2713. #ifdef __ARCH_WANT_SYS_NICE
  2714. /*
  2715. * sys_nice - change the priority of the current process.
  2716. * @increment: priority increment
  2717. *
  2718. * sys_setpriority is a more generic, but much slower function that
  2719. * does similar things.
  2720. */
  2721. SYSCALL_DEFINE1(nice, int, increment)
  2722. {
  2723. long nice, retval;
  2724. /*
  2725. * Setpriority might change our priority at the same moment.
  2726. * We don't have to worry. Conceptually one call occurs first
  2727. * and we have a single winner.
  2728. */
  2729. increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
  2730. nice = task_nice(current) + increment;
  2731. nice = clamp_val(nice, MIN_NICE, MAX_NICE);
  2732. if (increment < 0 && !can_nice(current, nice))
  2733. return -EPERM;
  2734. retval = security_task_setnice(current, nice);
  2735. if (retval)
  2736. return retval;
  2737. set_user_nice(current, nice);
  2738. return 0;
  2739. }
  2740. #endif
  2741. /**
  2742. * task_prio - return the priority value of a given task.
  2743. * @p: the task in question.
  2744. *
  2745. * Return: The priority value as seen by users in /proc.
  2746. * RT tasks are offset by -200. Normal tasks are centered
  2747. * around 0, value goes from -16 to +15.
  2748. */
  2749. int task_prio(const struct task_struct *p)
  2750. {
  2751. return p->prio - MAX_RT_PRIO;
  2752. }
  2753. /**
  2754. * idle_cpu - is a given cpu idle currently?
  2755. * @cpu: the processor in question.
  2756. *
  2757. * Return: 1 if the CPU is currently idle. 0 otherwise.
  2758. */
  2759. int idle_cpu(int cpu)
  2760. {
  2761. struct rq *rq = cpu_rq(cpu);
  2762. if (rq->curr != rq->idle)
  2763. return 0;
  2764. if (rq->nr_running)
  2765. return 0;
  2766. #ifdef CONFIG_SMP
  2767. if (!llist_empty(&rq->wake_list))
  2768. return 0;
  2769. #endif
  2770. return 1;
  2771. }
  2772. /**
  2773. * idle_task - return the idle task for a given cpu.
  2774. * @cpu: the processor in question.
  2775. *
  2776. * Return: The idle task for the cpu @cpu.
  2777. */
  2778. struct task_struct *idle_task(int cpu)
  2779. {
  2780. return cpu_rq(cpu)->idle;
  2781. }
  2782. /**
  2783. * find_process_by_pid - find a process with a matching PID value.
  2784. * @pid: the pid in question.
  2785. *
  2786. * The task of @pid, if found. %NULL otherwise.
  2787. */
  2788. static struct task_struct *find_process_by_pid(pid_t pid)
  2789. {
  2790. return pid ? find_task_by_vpid(pid) : current;
  2791. }
  2792. /*
  2793. * This function initializes the sched_dl_entity of a newly becoming
  2794. * SCHED_DEADLINE task.
  2795. *
  2796. * Only the static values are considered here, the actual runtime and the
  2797. * absolute deadline will be properly calculated when the task is enqueued
  2798. * for the first time with its new policy.
  2799. */
  2800. static void
  2801. __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
  2802. {
  2803. struct sched_dl_entity *dl_se = &p->dl;
  2804. dl_se->dl_runtime = attr->sched_runtime;
  2805. dl_se->dl_deadline = attr->sched_deadline;
  2806. dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
  2807. dl_se->flags = attr->sched_flags;
  2808. dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
  2809. /*
  2810. * Changing the parameters of a task is 'tricky' and we're not doing
  2811. * the correct thing -- also see task_dead_dl() and switched_from_dl().
  2812. *
  2813. * What we SHOULD do is delay the bandwidth release until the 0-lag
  2814. * point. This would include retaining the task_struct until that time
  2815. * and change dl_overflow() to not immediately decrement the current
  2816. * amount.
  2817. *
  2818. * Instead we retain the current runtime/deadline and let the new
  2819. * parameters take effect after the current reservation period lapses.
  2820. * This is safe (albeit pessimistic) because the 0-lag point is always
  2821. * before the current scheduling deadline.
  2822. *
  2823. * We can still have temporary overloads because we do not delay the
  2824. * change in bandwidth until that time; so admission control is
  2825. * not on the safe side. It does however guarantee tasks will never
  2826. * consume more than promised.
  2827. */
  2828. }
  2829. /*
  2830. * sched_setparam() passes in -1 for its policy, to let the functions
  2831. * it calls know not to change it.
  2832. */
  2833. #define SETPARAM_POLICY -1
  2834. static void __setscheduler_params(struct task_struct *p,
  2835. const struct sched_attr *attr)
  2836. {
  2837. int policy = attr->sched_policy;
  2838. if (policy == SETPARAM_POLICY)
  2839. policy = p->policy;
  2840. p->policy = policy;
  2841. if (dl_policy(policy))
  2842. __setparam_dl(p, attr);
  2843. else if (fair_policy(policy))
  2844. p->static_prio = NICE_TO_PRIO(attr->sched_nice);
  2845. /*
  2846. * __sched_setscheduler() ensures attr->sched_priority == 0 when
  2847. * !rt_policy. Always setting this ensures that things like
  2848. * getparam()/getattr() don't report silly values for !rt tasks.
  2849. */
  2850. p->rt_priority = attr->sched_priority;
  2851. p->normal_prio = normal_prio(p);
  2852. set_load_weight(p);
  2853. }
  2854. /* Actually do priority change: must hold pi & rq lock. */
  2855. static void __setscheduler(struct rq *rq, struct task_struct *p,
  2856. const struct sched_attr *attr, bool keep_boost)
  2857. {
  2858. __setscheduler_params(p, attr);
  2859. /*
  2860. * Keep a potential priority boosting if called from
  2861. * sched_setscheduler().
  2862. */
  2863. if (keep_boost)
  2864. p->prio = rt_mutex_get_effective_prio(p, normal_prio(p));
  2865. else
  2866. p->prio = normal_prio(p);
  2867. if (dl_prio(p->prio))
  2868. p->sched_class = &dl_sched_class;
  2869. else if (rt_prio(p->prio))
  2870. p->sched_class = &rt_sched_class;
  2871. else
  2872. p->sched_class = &fair_sched_class;
  2873. }
  2874. static void
  2875. __getparam_dl(struct task_struct *p, struct sched_attr *attr)
  2876. {
  2877. struct sched_dl_entity *dl_se = &p->dl;
  2878. attr->sched_priority = p->rt_priority;
  2879. attr->sched_runtime = dl_se->dl_runtime;
  2880. attr->sched_deadline = dl_se->dl_deadline;
  2881. attr->sched_period = dl_se->dl_period;
  2882. attr->sched_flags = dl_se->flags;
  2883. }
  2884. /*
  2885. * This function validates the new parameters of a -deadline task.
  2886. * We ask for the deadline not being zero, and greater or equal
  2887. * than the runtime, as well as the period of being zero or
  2888. * greater than deadline. Furthermore, we have to be sure that
  2889. * user parameters are above the internal resolution of 1us (we
  2890. * check sched_runtime only since it is always the smaller one) and
  2891. * below 2^63 ns (we have to check both sched_deadline and
  2892. * sched_period, as the latter can be zero).
  2893. */
  2894. static bool
  2895. __checkparam_dl(const struct sched_attr *attr)
  2896. {
  2897. /* deadline != 0 */
  2898. if (attr->sched_deadline == 0)
  2899. return false;
  2900. /*
  2901. * Since we truncate DL_SCALE bits, make sure we're at least
  2902. * that big.
  2903. */
  2904. if (attr->sched_runtime < (1ULL << DL_SCALE))
  2905. return false;
  2906. /*
  2907. * Since we use the MSB for wrap-around and sign issues, make
  2908. * sure it's not set (mind that period can be equal to zero).
  2909. */
  2910. if (attr->sched_deadline & (1ULL << 63) ||
  2911. attr->sched_period & (1ULL << 63))
  2912. return false;
  2913. /* runtime <= deadline <= period (if period != 0) */
  2914. if ((attr->sched_period != 0 &&
  2915. attr->sched_period < attr->sched_deadline) ||
  2916. attr->sched_deadline < attr->sched_runtime)
  2917. return false;
  2918. return true;
  2919. }
  2920. /*
  2921. * check the target process has a UID that matches the current process's
  2922. */
  2923. static bool check_same_owner(struct task_struct *p)
  2924. {
  2925. const struct cred *cred = current_cred(), *pcred;
  2926. bool match;
  2927. rcu_read_lock();
  2928. pcred = __task_cred(p);
  2929. match = (uid_eq(cred->euid, pcred->euid) ||
  2930. uid_eq(cred->euid, pcred->uid));
  2931. rcu_read_unlock();
  2932. return match;
  2933. }
  2934. static bool dl_param_changed(struct task_struct *p,
  2935. const struct sched_attr *attr)
  2936. {
  2937. struct sched_dl_entity *dl_se = &p->dl;
  2938. if (dl_se->dl_runtime != attr->sched_runtime ||
  2939. dl_se->dl_deadline != attr->sched_deadline ||
  2940. dl_se->dl_period != attr->sched_period ||
  2941. dl_se->flags != attr->sched_flags)
  2942. return true;
  2943. return false;
  2944. }
  2945. static int __sched_setscheduler(struct task_struct *p,
  2946. const struct sched_attr *attr,
  2947. bool user)
  2948. {
  2949. int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
  2950. MAX_RT_PRIO - 1 - attr->sched_priority;
  2951. int retval, oldprio, oldpolicy = -1, queued, running;
  2952. int new_effective_prio, policy = attr->sched_policy;
  2953. unsigned long flags;
  2954. const struct sched_class *prev_class;
  2955. struct rq *rq;
  2956. int reset_on_fork;
  2957. /* may grab non-irq protected spin_locks */
  2958. BUG_ON(in_interrupt());
  2959. recheck:
  2960. /* double check policy once rq lock held */
  2961. if (policy < 0) {
  2962. reset_on_fork = p->sched_reset_on_fork;
  2963. policy = oldpolicy = p->policy;
  2964. } else {
  2965. reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
  2966. if (policy != SCHED_DEADLINE &&
  2967. policy != SCHED_FIFO && policy != SCHED_RR &&
  2968. policy != SCHED_NORMAL && policy != SCHED_BATCH &&
  2969. policy != SCHED_IDLE)
  2970. return -EINVAL;
  2971. }
  2972. if (attr->sched_flags & ~(SCHED_FLAG_RESET_ON_FORK))
  2973. return -EINVAL;
  2974. /*
  2975. * Valid priorities for SCHED_FIFO and SCHED_RR are
  2976. * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
  2977. * SCHED_BATCH and SCHED_IDLE is 0.
  2978. */
  2979. if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
  2980. (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
  2981. return -EINVAL;
  2982. if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
  2983. (rt_policy(policy) != (attr->sched_priority != 0)))
  2984. return -EINVAL;
  2985. /*
  2986. * Allow unprivileged RT tasks to decrease priority:
  2987. */
  2988. if (user && !capable(CAP_SYS_NICE)) {
  2989. if (fair_policy(policy)) {
  2990. if (attr->sched_nice < task_nice(p) &&
  2991. !can_nice(p, attr->sched_nice))
  2992. return -EPERM;
  2993. }
  2994. if (rt_policy(policy)) {
  2995. unsigned long rlim_rtprio =
  2996. task_rlimit(p, RLIMIT_RTPRIO);
  2997. /* can't set/change the rt policy */
  2998. if (policy != p->policy && !rlim_rtprio)
  2999. return -EPERM;
  3000. /* can't increase priority */
  3001. if (attr->sched_priority > p->rt_priority &&
  3002. attr->sched_priority > rlim_rtprio)
  3003. return -EPERM;
  3004. }
  3005. /*
  3006. * Can't set/change SCHED_DEADLINE policy at all for now
  3007. * (safest behavior); in the future we would like to allow
  3008. * unprivileged DL tasks to increase their relative deadline
  3009. * or reduce their runtime (both ways reducing utilization)
  3010. */
  3011. if (dl_policy(policy))
  3012. return -EPERM;
  3013. /*
  3014. * Treat SCHED_IDLE as nice 20. Only allow a switch to
  3015. * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
  3016. */
  3017. if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) {
  3018. if (!can_nice(p, task_nice(p)))
  3019. return -EPERM;
  3020. }
  3021. /* can't change other user's priorities */
  3022. if (!check_same_owner(p))
  3023. return -EPERM;
  3024. /* Normal users shall not reset the sched_reset_on_fork flag */
  3025. if (p->sched_reset_on_fork && !reset_on_fork)
  3026. return -EPERM;
  3027. }
  3028. if (user) {
  3029. retval = security_task_setscheduler(p);
  3030. if (retval)
  3031. return retval;
  3032. }
  3033. /*
  3034. * make sure no PI-waiters arrive (or leave) while we are
  3035. * changing the priority of the task:
  3036. *
  3037. * To be able to change p->policy safely, the appropriate
  3038. * runqueue lock must be held.
  3039. */
  3040. rq = task_rq_lock(p, &flags);
  3041. /*
  3042. * Changing the policy of the stop threads its a very bad idea
  3043. */
  3044. if (p == rq->stop) {
  3045. task_rq_unlock(rq, p, &flags);
  3046. return -EINVAL;
  3047. }
  3048. /*
  3049. * If not changing anything there's no need to proceed further,
  3050. * but store a possible modification of reset_on_fork.
  3051. */
  3052. if (unlikely(policy == p->policy)) {
  3053. if (fair_policy(policy) && attr->sched_nice != task_nice(p))
  3054. goto change;
  3055. if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
  3056. goto change;
  3057. if (dl_policy(policy) && dl_param_changed(p, attr))
  3058. goto change;
  3059. p->sched_reset_on_fork = reset_on_fork;
  3060. task_rq_unlock(rq, p, &flags);
  3061. return 0;
  3062. }
  3063. change:
  3064. if (user) {
  3065. #ifdef CONFIG_RT_GROUP_SCHED
  3066. /*
  3067. * Do not allow realtime tasks into groups that have no runtime
  3068. * assigned.
  3069. */
  3070. if (rt_bandwidth_enabled() && rt_policy(policy) &&
  3071. task_group(p)->rt_bandwidth.rt_runtime == 0 &&
  3072. !task_group_is_autogroup(task_group(p))) {
  3073. task_rq_unlock(rq, p, &flags);
  3074. return -EPERM;
  3075. }
  3076. #endif
  3077. #ifdef CONFIG_SMP
  3078. if (dl_bandwidth_enabled() && dl_policy(policy)) {
  3079. cpumask_t *span = rq->rd->span;
  3080. /*
  3081. * Don't allow tasks with an affinity mask smaller than
  3082. * the entire root_domain to become SCHED_DEADLINE. We
  3083. * will also fail if there's no bandwidth available.
  3084. */
  3085. if (!cpumask_subset(span, &p->cpus_allowed) ||
  3086. rq->rd->dl_bw.bw == 0) {
  3087. task_rq_unlock(rq, p, &flags);
  3088. return -EPERM;
  3089. }
  3090. }
  3091. #endif
  3092. }
  3093. /* recheck policy now with rq lock held */
  3094. if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  3095. policy = oldpolicy = -1;
  3096. task_rq_unlock(rq, p, &flags);
  3097. goto recheck;
  3098. }
  3099. /*
  3100. * If setscheduling to SCHED_DEADLINE (or changing the parameters
  3101. * of a SCHED_DEADLINE task) we need to check if enough bandwidth
  3102. * is available.
  3103. */
  3104. if ((dl_policy(policy) || dl_task(p)) && dl_overflow(p, policy, attr)) {
  3105. task_rq_unlock(rq, p, &flags);
  3106. return -EBUSY;
  3107. }
  3108. p->sched_reset_on_fork = reset_on_fork;
  3109. oldprio = p->prio;
  3110. /*
  3111. * Take priority boosted tasks into account. If the new
  3112. * effective priority is unchanged, we just store the new
  3113. * normal parameters and do not touch the scheduler class and
  3114. * the runqueue. This will be done when the task deboost
  3115. * itself.
  3116. */
  3117. new_effective_prio = rt_mutex_get_effective_prio(p, newprio);
  3118. if (new_effective_prio == oldprio) {
  3119. __setscheduler_params(p, attr);
  3120. task_rq_unlock(rq, p, &flags);
  3121. return 0;
  3122. }
  3123. queued = task_on_rq_queued(p);
  3124. running = task_current(rq, p);
  3125. if (queued)
  3126. dequeue_task(rq, p, 0);
  3127. if (running)
  3128. put_prev_task(rq, p);
  3129. prev_class = p->sched_class;
  3130. __setscheduler(rq, p, attr, true);
  3131. if (running)
  3132. p->sched_class->set_curr_task(rq);
  3133. if (queued) {
  3134. /*
  3135. * We enqueue to tail when the priority of a task is
  3136. * increased (user space view).
  3137. */
  3138. enqueue_task(rq, p, oldprio <= p->prio ? ENQUEUE_HEAD : 0);
  3139. }
  3140. check_class_changed(rq, p, prev_class, oldprio);
  3141. task_rq_unlock(rq, p, &flags);
  3142. rt_mutex_adjust_pi(p);
  3143. return 0;
  3144. }
  3145. static int _sched_setscheduler(struct task_struct *p, int policy,
  3146. const struct sched_param *param, bool check)
  3147. {
  3148. struct sched_attr attr = {
  3149. .sched_policy = policy,
  3150. .sched_priority = param->sched_priority,
  3151. .sched_nice = PRIO_TO_NICE(p->static_prio),
  3152. };
  3153. /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
  3154. if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
  3155. attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
  3156. policy &= ~SCHED_RESET_ON_FORK;
  3157. attr.sched_policy = policy;
  3158. }
  3159. return __sched_setscheduler(p, &attr, check);
  3160. }
  3161. /**
  3162. * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
  3163. * @p: the task in question.
  3164. * @policy: new policy.
  3165. * @param: structure containing the new RT priority.
  3166. *
  3167. * Return: 0 on success. An error code otherwise.
  3168. *
  3169. * NOTE that the task may be already dead.
  3170. */
  3171. int sched_setscheduler(struct task_struct *p, int policy,
  3172. const struct sched_param *param)
  3173. {
  3174. return _sched_setscheduler(p, policy, param, true);
  3175. }
  3176. EXPORT_SYMBOL_GPL(sched_setscheduler);
  3177. int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
  3178. {
  3179. return __sched_setscheduler(p, attr, true);
  3180. }
  3181. EXPORT_SYMBOL_GPL(sched_setattr);
  3182. /**
  3183. * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
  3184. * @p: the task in question.
  3185. * @policy: new policy.
  3186. * @param: structure containing the new RT priority.
  3187. *
  3188. * Just like sched_setscheduler, only don't bother checking if the
  3189. * current context has permission. For example, this is needed in
  3190. * stop_machine(): we create temporary high priority worker threads,
  3191. * but our caller might not have that capability.
  3192. *
  3193. * Return: 0 on success. An error code otherwise.
  3194. */
  3195. int sched_setscheduler_nocheck(struct task_struct *p, int policy,
  3196. const struct sched_param *param)
  3197. {
  3198. return _sched_setscheduler(p, policy, param, false);
  3199. }
  3200. static int
  3201. do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  3202. {
  3203. struct sched_param lparam;
  3204. struct task_struct *p;
  3205. int retval;
  3206. if (!param || pid < 0)
  3207. return -EINVAL;
  3208. if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  3209. return -EFAULT;
  3210. rcu_read_lock();
  3211. retval = -ESRCH;
  3212. p = find_process_by_pid(pid);
  3213. if (p != NULL)
  3214. retval = sched_setscheduler(p, policy, &lparam);
  3215. rcu_read_unlock();
  3216. return retval;
  3217. }
  3218. /*
  3219. * Mimics kernel/events/core.c perf_copy_attr().
  3220. */
  3221. static int sched_copy_attr(struct sched_attr __user *uattr,
  3222. struct sched_attr *attr)
  3223. {
  3224. u32 size;
  3225. int ret;
  3226. if (!access_ok(VERIFY_WRITE, uattr, SCHED_ATTR_SIZE_VER0))
  3227. return -EFAULT;
  3228. /*
  3229. * zero the full structure, so that a short copy will be nice.
  3230. */
  3231. memset(attr, 0, sizeof(*attr));
  3232. ret = get_user(size, &uattr->size);
  3233. if (ret)
  3234. return ret;
  3235. if (size > PAGE_SIZE) /* silly large */
  3236. goto err_size;
  3237. if (!size) /* abi compat */
  3238. size = SCHED_ATTR_SIZE_VER0;
  3239. if (size < SCHED_ATTR_SIZE_VER0)
  3240. goto err_size;
  3241. /*
  3242. * If we're handed a bigger struct than we know of,
  3243. * ensure all the unknown bits are 0 - i.e. new
  3244. * user-space does not rely on any kernel feature
  3245. * extensions we dont know about yet.
  3246. */
  3247. if (size > sizeof(*attr)) {
  3248. unsigned char __user *addr;
  3249. unsigned char __user *end;
  3250. unsigned char val;
  3251. addr = (void __user *)uattr + sizeof(*attr);
  3252. end = (void __user *)uattr + size;
  3253. for (; addr < end; addr++) {
  3254. ret = get_user(val, addr);
  3255. if (ret)
  3256. return ret;
  3257. if (val)
  3258. goto err_size;
  3259. }
  3260. size = sizeof(*attr);
  3261. }
  3262. ret = copy_from_user(attr, uattr, size);
  3263. if (ret)
  3264. return -EFAULT;
  3265. /*
  3266. * XXX: do we want to be lenient like existing syscalls; or do we want
  3267. * to be strict and return an error on out-of-bounds values?
  3268. */
  3269. attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
  3270. return 0;
  3271. err_size:
  3272. put_user(sizeof(*attr), &uattr->size);
  3273. return -E2BIG;
  3274. }
  3275. /**
  3276. * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  3277. * @pid: the pid in question.
  3278. * @policy: new policy.
  3279. * @param: structure containing the new RT priority.
  3280. *
  3281. * Return: 0 on success. An error code otherwise.
  3282. */
  3283. SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
  3284. struct sched_param __user *, param)
  3285. {
  3286. /* negative values for policy are not valid */
  3287. if (policy < 0)
  3288. return -EINVAL;
  3289. return do_sched_setscheduler(pid, policy, param);
  3290. }
  3291. /**
  3292. * sys_sched_setparam - set/change the RT priority of a thread
  3293. * @pid: the pid in question.
  3294. * @param: structure containing the new RT priority.
  3295. *
  3296. * Return: 0 on success. An error code otherwise.
  3297. */
  3298. SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
  3299. {
  3300. return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
  3301. }
  3302. /**
  3303. * sys_sched_setattr - same as above, but with extended sched_attr
  3304. * @pid: the pid in question.
  3305. * @uattr: structure containing the extended parameters.
  3306. * @flags: for future extension.
  3307. */
  3308. SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
  3309. unsigned int, flags)
  3310. {
  3311. struct sched_attr attr;
  3312. struct task_struct *p;
  3313. int retval;
  3314. if (!uattr || pid < 0 || flags)
  3315. return -EINVAL;
  3316. retval = sched_copy_attr(uattr, &attr);
  3317. if (retval)
  3318. return retval;
  3319. if ((int)attr.sched_policy < 0)
  3320. return -EINVAL;
  3321. rcu_read_lock();
  3322. retval = -ESRCH;
  3323. p = find_process_by_pid(pid);
  3324. if (p != NULL)
  3325. retval = sched_setattr(p, &attr);
  3326. rcu_read_unlock();
  3327. return retval;
  3328. }
  3329. /**
  3330. * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  3331. * @pid: the pid in question.
  3332. *
  3333. * Return: On success, the policy of the thread. Otherwise, a negative error
  3334. * code.
  3335. */
  3336. SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
  3337. {
  3338. struct task_struct *p;
  3339. int retval;
  3340. if (pid < 0)
  3341. return -EINVAL;
  3342. retval = -ESRCH;
  3343. rcu_read_lock();
  3344. p = find_process_by_pid(pid);
  3345. if (p) {
  3346. retval = security_task_getscheduler(p);
  3347. if (!retval)
  3348. retval = p->policy
  3349. | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
  3350. }
  3351. rcu_read_unlock();
  3352. return retval;
  3353. }
  3354. /**
  3355. * sys_sched_getparam - get the RT priority of a thread
  3356. * @pid: the pid in question.
  3357. * @param: structure containing the RT priority.
  3358. *
  3359. * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
  3360. * code.
  3361. */
  3362. SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
  3363. {
  3364. struct sched_param lp = { .sched_priority = 0 };
  3365. struct task_struct *p;
  3366. int retval;
  3367. if (!param || pid < 0)
  3368. return -EINVAL;
  3369. rcu_read_lock();
  3370. p = find_process_by_pid(pid);
  3371. retval = -ESRCH;
  3372. if (!p)
  3373. goto out_unlock;
  3374. retval = security_task_getscheduler(p);
  3375. if (retval)
  3376. goto out_unlock;
  3377. if (task_has_rt_policy(p))
  3378. lp.sched_priority = p->rt_priority;
  3379. rcu_read_unlock();
  3380. /*
  3381. * This one might sleep, we cannot do it with a spinlock held ...
  3382. */
  3383. retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  3384. return retval;
  3385. out_unlock:
  3386. rcu_read_unlock();
  3387. return retval;
  3388. }
  3389. static int sched_read_attr(struct sched_attr __user *uattr,
  3390. struct sched_attr *attr,
  3391. unsigned int usize)
  3392. {
  3393. int ret;
  3394. if (!access_ok(VERIFY_WRITE, uattr, usize))
  3395. return -EFAULT;
  3396. /*
  3397. * If we're handed a smaller struct than we know of,
  3398. * ensure all the unknown bits are 0 - i.e. old
  3399. * user-space does not get uncomplete information.
  3400. */
  3401. if (usize < sizeof(*attr)) {
  3402. unsigned char *addr;
  3403. unsigned char *end;
  3404. addr = (void *)attr + usize;
  3405. end = (void *)attr + sizeof(*attr);
  3406. for (; addr < end; addr++) {
  3407. if (*addr)
  3408. return -EFBIG;
  3409. }
  3410. attr->size = usize;
  3411. }
  3412. ret = copy_to_user(uattr, attr, attr->size);
  3413. if (ret)
  3414. return -EFAULT;
  3415. return 0;
  3416. }
  3417. /**
  3418. * sys_sched_getattr - similar to sched_getparam, but with sched_attr
  3419. * @pid: the pid in question.
  3420. * @uattr: structure containing the extended parameters.
  3421. * @size: sizeof(attr) for fwd/bwd comp.
  3422. * @flags: for future extension.
  3423. */
  3424. SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
  3425. unsigned int, size, unsigned int, flags)
  3426. {
  3427. struct sched_attr attr = {
  3428. .size = sizeof(struct sched_attr),
  3429. };
  3430. struct task_struct *p;
  3431. int retval;
  3432. if (!uattr || pid < 0 || size > PAGE_SIZE ||
  3433. size < SCHED_ATTR_SIZE_VER0 || flags)
  3434. return -EINVAL;
  3435. rcu_read_lock();
  3436. p = find_process_by_pid(pid);
  3437. retval = -ESRCH;
  3438. if (!p)
  3439. goto out_unlock;
  3440. retval = security_task_getscheduler(p);
  3441. if (retval)
  3442. goto out_unlock;
  3443. attr.sched_policy = p->policy;
  3444. if (p->sched_reset_on_fork)
  3445. attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
  3446. if (task_has_dl_policy(p))
  3447. __getparam_dl(p, &attr);
  3448. else if (task_has_rt_policy(p))
  3449. attr.sched_priority = p->rt_priority;
  3450. else
  3451. attr.sched_nice = task_nice(p);
  3452. rcu_read_unlock();
  3453. retval = sched_read_attr(uattr, &attr, size);
  3454. return retval;
  3455. out_unlock:
  3456. rcu_read_unlock();
  3457. return retval;
  3458. }
  3459. long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
  3460. {
  3461. cpumask_var_t cpus_allowed, new_mask;
  3462. struct task_struct *p;
  3463. int retval;
  3464. rcu_read_lock();
  3465. p = find_process_by_pid(pid);
  3466. if (!p) {
  3467. rcu_read_unlock();
  3468. return -ESRCH;
  3469. }
  3470. /* Prevent p going away */
  3471. get_task_struct(p);
  3472. rcu_read_unlock();
  3473. if (p->flags & PF_NO_SETAFFINITY) {
  3474. retval = -EINVAL;
  3475. goto out_put_task;
  3476. }
  3477. if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
  3478. retval = -ENOMEM;
  3479. goto out_put_task;
  3480. }
  3481. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
  3482. retval = -ENOMEM;
  3483. goto out_free_cpus_allowed;
  3484. }
  3485. retval = -EPERM;
  3486. if (!check_same_owner(p)) {
  3487. rcu_read_lock();
  3488. if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
  3489. rcu_read_unlock();
  3490. goto out_free_new_mask;
  3491. }
  3492. rcu_read_unlock();
  3493. }
  3494. retval = security_task_setscheduler(p);
  3495. if (retval)
  3496. goto out_free_new_mask;
  3497. cpuset_cpus_allowed(p, cpus_allowed);
  3498. cpumask_and(new_mask, in_mask, cpus_allowed);
  3499. /*
  3500. * Since bandwidth control happens on root_domain basis,
  3501. * if admission test is enabled, we only admit -deadline
  3502. * tasks allowed to run on all the CPUs in the task's
  3503. * root_domain.
  3504. */
  3505. #ifdef CONFIG_SMP
  3506. if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
  3507. rcu_read_lock();
  3508. if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
  3509. retval = -EBUSY;
  3510. rcu_read_unlock();
  3511. goto out_free_new_mask;
  3512. }
  3513. rcu_read_unlock();
  3514. }
  3515. #endif
  3516. again:
  3517. retval = set_cpus_allowed_ptr(p, new_mask);
  3518. if (!retval) {
  3519. cpuset_cpus_allowed(p, cpus_allowed);
  3520. if (!cpumask_subset(new_mask, cpus_allowed)) {
  3521. /*
  3522. * We must have raced with a concurrent cpuset
  3523. * update. Just reset the cpus_allowed to the
  3524. * cpuset's cpus_allowed
  3525. */
  3526. cpumask_copy(new_mask, cpus_allowed);
  3527. goto again;
  3528. }
  3529. }
  3530. out_free_new_mask:
  3531. free_cpumask_var(new_mask);
  3532. out_free_cpus_allowed:
  3533. free_cpumask_var(cpus_allowed);
  3534. out_put_task:
  3535. put_task_struct(p);
  3536. return retval;
  3537. }
  3538. static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  3539. struct cpumask *new_mask)
  3540. {
  3541. if (len < cpumask_size())
  3542. cpumask_clear(new_mask);
  3543. else if (len > cpumask_size())
  3544. len = cpumask_size();
  3545. return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  3546. }
  3547. /**
  3548. * sys_sched_setaffinity - set the cpu affinity of a process
  3549. * @pid: pid of the process
  3550. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3551. * @user_mask_ptr: user-space pointer to the new cpu mask
  3552. *
  3553. * Return: 0 on success. An error code otherwise.
  3554. */
  3555. SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
  3556. unsigned long __user *, user_mask_ptr)
  3557. {
  3558. cpumask_var_t new_mask;
  3559. int retval;
  3560. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  3561. return -ENOMEM;
  3562. retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
  3563. if (retval == 0)
  3564. retval = sched_setaffinity(pid, new_mask);
  3565. free_cpumask_var(new_mask);
  3566. return retval;
  3567. }
  3568. long sched_getaffinity(pid_t pid, struct cpumask *mask)
  3569. {
  3570. struct task_struct *p;
  3571. unsigned long flags;
  3572. int retval;
  3573. rcu_read_lock();
  3574. retval = -ESRCH;
  3575. p = find_process_by_pid(pid);
  3576. if (!p)
  3577. goto out_unlock;
  3578. retval = security_task_getscheduler(p);
  3579. if (retval)
  3580. goto out_unlock;
  3581. raw_spin_lock_irqsave(&p->pi_lock, flags);
  3582. cpumask_and(mask, &p->cpus_allowed, cpu_active_mask);
  3583. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  3584. out_unlock:
  3585. rcu_read_unlock();
  3586. return retval;
  3587. }
  3588. /**
  3589. * sys_sched_getaffinity - get the cpu affinity of a process
  3590. * @pid: pid of the process
  3591. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3592. * @user_mask_ptr: user-space pointer to hold the current cpu mask
  3593. *
  3594. * Return: 0 on success. An error code otherwise.
  3595. */
  3596. SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
  3597. unsigned long __user *, user_mask_ptr)
  3598. {
  3599. int ret;
  3600. cpumask_var_t mask;
  3601. if ((len * BITS_PER_BYTE) < nr_cpu_ids)
  3602. return -EINVAL;
  3603. if (len & (sizeof(unsigned long)-1))
  3604. return -EINVAL;
  3605. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  3606. return -ENOMEM;
  3607. ret = sched_getaffinity(pid, mask);
  3608. if (ret == 0) {
  3609. size_t retlen = min_t(size_t, len, cpumask_size());
  3610. if (copy_to_user(user_mask_ptr, mask, retlen))
  3611. ret = -EFAULT;
  3612. else
  3613. ret = retlen;
  3614. }
  3615. free_cpumask_var(mask);
  3616. return ret;
  3617. }
  3618. /**
  3619. * sys_sched_yield - yield the current processor to other threads.
  3620. *
  3621. * This function yields the current CPU to other tasks. If there are no
  3622. * other threads running on this CPU then this function will return.
  3623. *
  3624. * Return: 0.
  3625. */
  3626. SYSCALL_DEFINE0(sched_yield)
  3627. {
  3628. struct rq *rq = this_rq_lock();
  3629. schedstat_inc(rq, yld_count);
  3630. current->sched_class->yield_task(rq);
  3631. /*
  3632. * Since we are going to call schedule() anyway, there's
  3633. * no need to preempt or enable interrupts:
  3634. */
  3635. __release(rq->lock);
  3636. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  3637. do_raw_spin_unlock(&rq->lock);
  3638. sched_preempt_enable_no_resched();
  3639. schedule();
  3640. return 0;
  3641. }
  3642. int __sched _cond_resched(void)
  3643. {
  3644. if (should_resched()) {
  3645. preempt_schedule_common();
  3646. return 1;
  3647. }
  3648. return 0;
  3649. }
  3650. EXPORT_SYMBOL(_cond_resched);
  3651. /*
  3652. * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
  3653. * call schedule, and on return reacquire the lock.
  3654. *
  3655. * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  3656. * operations here to prevent schedule() from being called twice (once via
  3657. * spin_unlock(), once by hand).
  3658. */
  3659. int __cond_resched_lock(spinlock_t *lock)
  3660. {
  3661. int resched = should_resched();
  3662. int ret = 0;
  3663. lockdep_assert_held(lock);
  3664. if (spin_needbreak(lock) || resched) {
  3665. spin_unlock(lock);
  3666. if (resched)
  3667. preempt_schedule_common();
  3668. else
  3669. cpu_relax();
  3670. ret = 1;
  3671. spin_lock(lock);
  3672. }
  3673. return ret;
  3674. }
  3675. EXPORT_SYMBOL(__cond_resched_lock);
  3676. int __sched __cond_resched_softirq(void)
  3677. {
  3678. BUG_ON(!in_softirq());
  3679. if (should_resched()) {
  3680. local_bh_enable();
  3681. preempt_schedule_common();
  3682. local_bh_disable();
  3683. return 1;
  3684. }
  3685. return 0;
  3686. }
  3687. EXPORT_SYMBOL(__cond_resched_softirq);
  3688. /**
  3689. * yield - yield the current processor to other threads.
  3690. *
  3691. * Do not ever use this function, there's a 99% chance you're doing it wrong.
  3692. *
  3693. * The scheduler is at all times free to pick the calling task as the most
  3694. * eligible task to run, if removing the yield() call from your code breaks
  3695. * it, its already broken.
  3696. *
  3697. * Typical broken usage is:
  3698. *
  3699. * while (!event)
  3700. * yield();
  3701. *
  3702. * where one assumes that yield() will let 'the other' process run that will
  3703. * make event true. If the current task is a SCHED_FIFO task that will never
  3704. * happen. Never use yield() as a progress guarantee!!
  3705. *
  3706. * If you want to use yield() to wait for something, use wait_event().
  3707. * If you want to use yield() to be 'nice' for others, use cond_resched().
  3708. * If you still want to use yield(), do not!
  3709. */
  3710. void __sched yield(void)
  3711. {
  3712. set_current_state(TASK_RUNNING);
  3713. sys_sched_yield();
  3714. }
  3715. EXPORT_SYMBOL(yield);
  3716. /**
  3717. * yield_to - yield the current processor to another thread in
  3718. * your thread group, or accelerate that thread toward the
  3719. * processor it's on.
  3720. * @p: target task
  3721. * @preempt: whether task preemption is allowed or not
  3722. *
  3723. * It's the caller's job to ensure that the target task struct
  3724. * can't go away on us before we can do any checks.
  3725. *
  3726. * Return:
  3727. * true (>0) if we indeed boosted the target task.
  3728. * false (0) if we failed to boost the target.
  3729. * -ESRCH if there's no task to yield to.
  3730. */
  3731. int __sched yield_to(struct task_struct *p, bool preempt)
  3732. {
  3733. struct task_struct *curr = current;
  3734. struct rq *rq, *p_rq;
  3735. unsigned long flags;
  3736. int yielded = 0;
  3737. local_irq_save(flags);
  3738. rq = this_rq();
  3739. again:
  3740. p_rq = task_rq(p);
  3741. /*
  3742. * If we're the only runnable task on the rq and target rq also
  3743. * has only one task, there's absolutely no point in yielding.
  3744. */
  3745. if (rq->nr_running == 1 && p_rq->nr_running == 1) {
  3746. yielded = -ESRCH;
  3747. goto out_irq;
  3748. }
  3749. double_rq_lock(rq, p_rq);
  3750. if (task_rq(p) != p_rq) {
  3751. double_rq_unlock(rq, p_rq);
  3752. goto again;
  3753. }
  3754. if (!curr->sched_class->yield_to_task)
  3755. goto out_unlock;
  3756. if (curr->sched_class != p->sched_class)
  3757. goto out_unlock;
  3758. if (task_running(p_rq, p) || p->state)
  3759. goto out_unlock;
  3760. yielded = curr->sched_class->yield_to_task(rq, p, preempt);
  3761. if (yielded) {
  3762. schedstat_inc(rq, yld_count);
  3763. /*
  3764. * Make p's CPU reschedule; pick_next_entity takes care of
  3765. * fairness.
  3766. */
  3767. if (preempt && rq != p_rq)
  3768. resched_curr(p_rq);
  3769. }
  3770. out_unlock:
  3771. double_rq_unlock(rq, p_rq);
  3772. out_irq:
  3773. local_irq_restore(flags);
  3774. if (yielded > 0)
  3775. schedule();
  3776. return yielded;
  3777. }
  3778. EXPORT_SYMBOL_GPL(yield_to);
  3779. /*
  3780. * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  3781. * that process accounting knows that this is a task in IO wait state.
  3782. */
  3783. long __sched io_schedule_timeout(long timeout)
  3784. {
  3785. int old_iowait = current->in_iowait;
  3786. struct rq *rq;
  3787. long ret;
  3788. current->in_iowait = 1;
  3789. blk_schedule_flush_plug(current);
  3790. delayacct_blkio_start();
  3791. rq = raw_rq();
  3792. atomic_inc(&rq->nr_iowait);
  3793. ret = schedule_timeout(timeout);
  3794. current->in_iowait = old_iowait;
  3795. atomic_dec(&rq->nr_iowait);
  3796. delayacct_blkio_end();
  3797. return ret;
  3798. }
  3799. EXPORT_SYMBOL(io_schedule_timeout);
  3800. /**
  3801. * sys_sched_get_priority_max - return maximum RT priority.
  3802. * @policy: scheduling class.
  3803. *
  3804. * Return: On success, this syscall returns the maximum
  3805. * rt_priority that can be used by a given scheduling class.
  3806. * On failure, a negative error code is returned.
  3807. */
  3808. SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
  3809. {
  3810. int ret = -EINVAL;
  3811. switch (policy) {
  3812. case SCHED_FIFO:
  3813. case SCHED_RR:
  3814. ret = MAX_USER_RT_PRIO-1;
  3815. break;
  3816. case SCHED_DEADLINE:
  3817. case SCHED_NORMAL:
  3818. case SCHED_BATCH:
  3819. case SCHED_IDLE:
  3820. ret = 0;
  3821. break;
  3822. }
  3823. return ret;
  3824. }
  3825. /**
  3826. * sys_sched_get_priority_min - return minimum RT priority.
  3827. * @policy: scheduling class.
  3828. *
  3829. * Return: On success, this syscall returns the minimum
  3830. * rt_priority that can be used by a given scheduling class.
  3831. * On failure, a negative error code is returned.
  3832. */
  3833. SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
  3834. {
  3835. int ret = -EINVAL;
  3836. switch (policy) {
  3837. case SCHED_FIFO:
  3838. case SCHED_RR:
  3839. ret = 1;
  3840. break;
  3841. case SCHED_DEADLINE:
  3842. case SCHED_NORMAL:
  3843. case SCHED_BATCH:
  3844. case SCHED_IDLE:
  3845. ret = 0;
  3846. }
  3847. return ret;
  3848. }
  3849. /**
  3850. * sys_sched_rr_get_interval - return the default timeslice of a process.
  3851. * @pid: pid of the process.
  3852. * @interval: userspace pointer to the timeslice value.
  3853. *
  3854. * this syscall writes the default timeslice value of a given process
  3855. * into the user-space timespec buffer. A value of '0' means infinity.
  3856. *
  3857. * Return: On success, 0 and the timeslice is in @interval. Otherwise,
  3858. * an error code.
  3859. */
  3860. SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
  3861. struct timespec __user *, interval)
  3862. {
  3863. struct task_struct *p;
  3864. unsigned int time_slice;
  3865. unsigned long flags;
  3866. struct rq *rq;
  3867. int retval;
  3868. struct timespec t;
  3869. if (pid < 0)
  3870. return -EINVAL;
  3871. retval = -ESRCH;
  3872. rcu_read_lock();
  3873. p = find_process_by_pid(pid);
  3874. if (!p)
  3875. goto out_unlock;
  3876. retval = security_task_getscheduler(p);
  3877. if (retval)
  3878. goto out_unlock;
  3879. rq = task_rq_lock(p, &flags);
  3880. time_slice = 0;
  3881. if (p->sched_class->get_rr_interval)
  3882. time_slice = p->sched_class->get_rr_interval(rq, p);
  3883. task_rq_unlock(rq, p, &flags);
  3884. rcu_read_unlock();
  3885. jiffies_to_timespec(time_slice, &t);
  3886. retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  3887. return retval;
  3888. out_unlock:
  3889. rcu_read_unlock();
  3890. return retval;
  3891. }
  3892. static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
  3893. void sched_show_task(struct task_struct *p)
  3894. {
  3895. unsigned long free = 0;
  3896. int ppid;
  3897. unsigned long state = p->state;
  3898. if (state)
  3899. state = __ffs(state) + 1;
  3900. printk(KERN_INFO "%-15.15s %c", p->comm,
  3901. state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
  3902. #if BITS_PER_LONG == 32
  3903. if (state == TASK_RUNNING)
  3904. printk(KERN_CONT " running ");
  3905. else
  3906. printk(KERN_CONT " %08lx ", thread_saved_pc(p));
  3907. #else
  3908. if (state == TASK_RUNNING)
  3909. printk(KERN_CONT " running task ");
  3910. else
  3911. printk(KERN_CONT " %016lx ", thread_saved_pc(p));
  3912. #endif
  3913. #ifdef CONFIG_DEBUG_STACK_USAGE
  3914. free = stack_not_used(p);
  3915. #endif
  3916. ppid = 0;
  3917. rcu_read_lock();
  3918. if (pid_alive(p))
  3919. ppid = task_pid_nr(rcu_dereference(p->real_parent));
  3920. rcu_read_unlock();
  3921. printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
  3922. task_pid_nr(p), ppid,
  3923. (unsigned long)task_thread_info(p)->flags);
  3924. print_worker_info(KERN_INFO, p);
  3925. show_stack(p, NULL);
  3926. }
  3927. void show_state_filter(unsigned long state_filter)
  3928. {
  3929. struct task_struct *g, *p;
  3930. #if BITS_PER_LONG == 32
  3931. printk(KERN_INFO
  3932. " task PC stack pid father\n");
  3933. #else
  3934. printk(KERN_INFO
  3935. " task PC stack pid father\n");
  3936. #endif
  3937. rcu_read_lock();
  3938. for_each_process_thread(g, p) {
  3939. /*
  3940. * reset the NMI-timeout, listing all files on a slow
  3941. * console might take a lot of time:
  3942. */
  3943. touch_nmi_watchdog();
  3944. if (!state_filter || (p->state & state_filter))
  3945. sched_show_task(p);
  3946. }
  3947. touch_all_softlockup_watchdogs();
  3948. #ifdef CONFIG_SCHED_DEBUG
  3949. sysrq_sched_debug_show();
  3950. #endif
  3951. rcu_read_unlock();
  3952. /*
  3953. * Only show locks if all tasks are dumped:
  3954. */
  3955. if (!state_filter)
  3956. debug_show_all_locks();
  3957. }
  3958. void init_idle_bootup_task(struct task_struct *idle)
  3959. {
  3960. idle->sched_class = &idle_sched_class;
  3961. }
  3962. /**
  3963. * init_idle - set up an idle thread for a given CPU
  3964. * @idle: task in question
  3965. * @cpu: cpu the idle task belongs to
  3966. *
  3967. * NOTE: this function does not set the idle thread's NEED_RESCHED
  3968. * flag, to make booting more robust.
  3969. */
  3970. void init_idle(struct task_struct *idle, int cpu)
  3971. {
  3972. struct rq *rq = cpu_rq(cpu);
  3973. unsigned long flags;
  3974. raw_spin_lock_irqsave(&rq->lock, flags);
  3975. __sched_fork(0, idle);
  3976. idle->state = TASK_RUNNING;
  3977. idle->se.exec_start = sched_clock();
  3978. do_set_cpus_allowed(idle, cpumask_of(cpu));
  3979. /*
  3980. * We're having a chicken and egg problem, even though we are
  3981. * holding rq->lock, the cpu isn't yet set to this cpu so the
  3982. * lockdep check in task_group() will fail.
  3983. *
  3984. * Similar case to sched_fork(). / Alternatively we could
  3985. * use task_rq_lock() here and obtain the other rq->lock.
  3986. *
  3987. * Silence PROVE_RCU
  3988. */
  3989. rcu_read_lock();
  3990. __set_task_cpu(idle, cpu);
  3991. rcu_read_unlock();
  3992. rq->curr = rq->idle = idle;
  3993. idle->on_rq = TASK_ON_RQ_QUEUED;
  3994. #if defined(CONFIG_SMP)
  3995. idle->on_cpu = 1;
  3996. #endif
  3997. raw_spin_unlock_irqrestore(&rq->lock, flags);
  3998. /* Set the preempt count _outside_ the spinlocks! */
  3999. init_idle_preempt_count(idle, cpu);
  4000. /*
  4001. * The idle tasks have their own, simple scheduling class:
  4002. */
  4003. idle->sched_class = &idle_sched_class;
  4004. ftrace_graph_init_idle_task(idle, cpu);
  4005. vtime_init_idle(idle, cpu);
  4006. #if defined(CONFIG_SMP)
  4007. sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
  4008. #endif
  4009. }
  4010. int cpuset_cpumask_can_shrink(const struct cpumask *cur,
  4011. const struct cpumask *trial)
  4012. {
  4013. int ret = 1, trial_cpus;
  4014. struct dl_bw *cur_dl_b;
  4015. unsigned long flags;
  4016. if (!cpumask_weight(cur))
  4017. return ret;
  4018. rcu_read_lock_sched();
  4019. cur_dl_b = dl_bw_of(cpumask_any(cur));
  4020. trial_cpus = cpumask_weight(trial);
  4021. raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
  4022. if (cur_dl_b->bw != -1 &&
  4023. cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
  4024. ret = 0;
  4025. raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
  4026. rcu_read_unlock_sched();
  4027. return ret;
  4028. }
  4029. int task_can_attach(struct task_struct *p,
  4030. const struct cpumask *cs_cpus_allowed)
  4031. {
  4032. int ret = 0;
  4033. /*
  4034. * Kthreads which disallow setaffinity shouldn't be moved
  4035. * to a new cpuset; we don't want to change their cpu
  4036. * affinity and isolating such threads by their set of
  4037. * allowed nodes is unnecessary. Thus, cpusets are not
  4038. * applicable for such threads. This prevents checking for
  4039. * success of set_cpus_allowed_ptr() on all attached tasks
  4040. * before cpus_allowed may be changed.
  4041. */
  4042. if (p->flags & PF_NO_SETAFFINITY) {
  4043. ret = -EINVAL;
  4044. goto out;
  4045. }
  4046. #ifdef CONFIG_SMP
  4047. if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
  4048. cs_cpus_allowed)) {
  4049. unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
  4050. cs_cpus_allowed);
  4051. struct dl_bw *dl_b;
  4052. bool overflow;
  4053. int cpus;
  4054. unsigned long flags;
  4055. rcu_read_lock_sched();
  4056. dl_b = dl_bw_of(dest_cpu);
  4057. raw_spin_lock_irqsave(&dl_b->lock, flags);
  4058. cpus = dl_bw_cpus(dest_cpu);
  4059. overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
  4060. if (overflow)
  4061. ret = -EBUSY;
  4062. else {
  4063. /*
  4064. * We reserve space for this task in the destination
  4065. * root_domain, as we can't fail after this point.
  4066. * We will free resources in the source root_domain
  4067. * later on (see set_cpus_allowed_dl()).
  4068. */
  4069. __dl_add(dl_b, p->dl.dl_bw);
  4070. }
  4071. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  4072. rcu_read_unlock_sched();
  4073. }
  4074. #endif
  4075. out:
  4076. return ret;
  4077. }
  4078. #ifdef CONFIG_SMP
  4079. /*
  4080. * move_queued_task - move a queued task to new rq.
  4081. *
  4082. * Returns (locked) new rq. Old rq's lock is released.
  4083. */
  4084. static struct rq *move_queued_task(struct task_struct *p, int new_cpu)
  4085. {
  4086. struct rq *rq = task_rq(p);
  4087. lockdep_assert_held(&rq->lock);
  4088. dequeue_task(rq, p, 0);
  4089. p->on_rq = TASK_ON_RQ_MIGRATING;
  4090. set_task_cpu(p, new_cpu);
  4091. raw_spin_unlock(&rq->lock);
  4092. rq = cpu_rq(new_cpu);
  4093. raw_spin_lock(&rq->lock);
  4094. BUG_ON(task_cpu(p) != new_cpu);
  4095. p->on_rq = TASK_ON_RQ_QUEUED;
  4096. enqueue_task(rq, p, 0);
  4097. check_preempt_curr(rq, p, 0);
  4098. return rq;
  4099. }
  4100. void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  4101. {
  4102. if (p->sched_class->set_cpus_allowed)
  4103. p->sched_class->set_cpus_allowed(p, new_mask);
  4104. cpumask_copy(&p->cpus_allowed, new_mask);
  4105. p->nr_cpus_allowed = cpumask_weight(new_mask);
  4106. }
  4107. /*
  4108. * This is how migration works:
  4109. *
  4110. * 1) we invoke migration_cpu_stop() on the target CPU using
  4111. * stop_one_cpu().
  4112. * 2) stopper starts to run (implicitly forcing the migrated thread
  4113. * off the CPU)
  4114. * 3) it checks whether the migrated task is still in the wrong runqueue.
  4115. * 4) if it's in the wrong runqueue then the migration thread removes
  4116. * it and puts it into the right queue.
  4117. * 5) stopper completes and stop_one_cpu() returns and the migration
  4118. * is done.
  4119. */
  4120. /*
  4121. * Change a given task's CPU affinity. Migrate the thread to a
  4122. * proper CPU and schedule it away if the CPU it's executing on
  4123. * is removed from the allowed bitmask.
  4124. *
  4125. * NOTE: the caller must have a valid reference to the task, the
  4126. * task must not exit() & deallocate itself prematurely. The
  4127. * call is not atomic; no spinlocks may be held.
  4128. */
  4129. int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
  4130. {
  4131. unsigned long flags;
  4132. struct rq *rq;
  4133. unsigned int dest_cpu;
  4134. int ret = 0;
  4135. rq = task_rq_lock(p, &flags);
  4136. if (cpumask_equal(&p->cpus_allowed, new_mask))
  4137. goto out;
  4138. if (!cpumask_intersects(new_mask, cpu_active_mask)) {
  4139. ret = -EINVAL;
  4140. goto out;
  4141. }
  4142. do_set_cpus_allowed(p, new_mask);
  4143. /* Can the task run on the task's current CPU? If so, we're done */
  4144. if (cpumask_test_cpu(task_cpu(p), new_mask))
  4145. goto out;
  4146. dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
  4147. if (task_running(rq, p) || p->state == TASK_WAKING) {
  4148. struct migration_arg arg = { p, dest_cpu };
  4149. /* Need help from migration thread: drop lock and wait. */
  4150. task_rq_unlock(rq, p, &flags);
  4151. stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  4152. tlb_migrate_finish(p->mm);
  4153. return 0;
  4154. } else if (task_on_rq_queued(p))
  4155. rq = move_queued_task(p, dest_cpu);
  4156. out:
  4157. task_rq_unlock(rq, p, &flags);
  4158. return ret;
  4159. }
  4160. EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
  4161. /*
  4162. * Move (not current) task off this cpu, onto dest cpu. We're doing
  4163. * this because either it can't run here any more (set_cpus_allowed()
  4164. * away from this CPU, or CPU going down), or because we're
  4165. * attempting to rebalance this task on exec (sched_exec).
  4166. *
  4167. * So we race with normal scheduler movements, but that's OK, as long
  4168. * as the task is no longer on this CPU.
  4169. *
  4170. * Returns non-zero if task was successfully migrated.
  4171. */
  4172. static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
  4173. {
  4174. struct rq *rq;
  4175. int ret = 0;
  4176. if (unlikely(!cpu_active(dest_cpu)))
  4177. return ret;
  4178. rq = cpu_rq(src_cpu);
  4179. raw_spin_lock(&p->pi_lock);
  4180. raw_spin_lock(&rq->lock);
  4181. /* Already moved. */
  4182. if (task_cpu(p) != src_cpu)
  4183. goto done;
  4184. /* Affinity changed (again). */
  4185. if (!cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
  4186. goto fail;
  4187. /*
  4188. * If we're not on a rq, the next wake-up will ensure we're
  4189. * placed properly.
  4190. */
  4191. if (task_on_rq_queued(p))
  4192. rq = move_queued_task(p, dest_cpu);
  4193. done:
  4194. ret = 1;
  4195. fail:
  4196. raw_spin_unlock(&rq->lock);
  4197. raw_spin_unlock(&p->pi_lock);
  4198. return ret;
  4199. }
  4200. #ifdef CONFIG_NUMA_BALANCING
  4201. /* Migrate current task p to target_cpu */
  4202. int migrate_task_to(struct task_struct *p, int target_cpu)
  4203. {
  4204. struct migration_arg arg = { p, target_cpu };
  4205. int curr_cpu = task_cpu(p);
  4206. if (curr_cpu == target_cpu)
  4207. return 0;
  4208. if (!cpumask_test_cpu(target_cpu, tsk_cpus_allowed(p)))
  4209. return -EINVAL;
  4210. /* TODO: This is not properly updating schedstats */
  4211. trace_sched_move_numa(p, curr_cpu, target_cpu);
  4212. return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
  4213. }
  4214. /*
  4215. * Requeue a task on a given node and accurately track the number of NUMA
  4216. * tasks on the runqueues
  4217. */
  4218. void sched_setnuma(struct task_struct *p, int nid)
  4219. {
  4220. struct rq *rq;
  4221. unsigned long flags;
  4222. bool queued, running;
  4223. rq = task_rq_lock(p, &flags);
  4224. queued = task_on_rq_queued(p);
  4225. running = task_current(rq, p);
  4226. if (queued)
  4227. dequeue_task(rq, p, 0);
  4228. if (running)
  4229. put_prev_task(rq, p);
  4230. p->numa_preferred_nid = nid;
  4231. if (running)
  4232. p->sched_class->set_curr_task(rq);
  4233. if (queued)
  4234. enqueue_task(rq, p, 0);
  4235. task_rq_unlock(rq, p, &flags);
  4236. }
  4237. #endif
  4238. /*
  4239. * migration_cpu_stop - this will be executed by a highprio stopper thread
  4240. * and performs thread migration by bumping thread off CPU then
  4241. * 'pushing' onto another runqueue.
  4242. */
  4243. static int migration_cpu_stop(void *data)
  4244. {
  4245. struct migration_arg *arg = data;
  4246. /*
  4247. * The original target cpu might have gone down and we might
  4248. * be on another cpu but it doesn't matter.
  4249. */
  4250. local_irq_disable();
  4251. /*
  4252. * We need to explicitly wake pending tasks before running
  4253. * __migrate_task() such that we will not miss enforcing cpus_allowed
  4254. * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
  4255. */
  4256. sched_ttwu_pending();
  4257. __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
  4258. local_irq_enable();
  4259. return 0;
  4260. }
  4261. #ifdef CONFIG_HOTPLUG_CPU
  4262. /*
  4263. * Ensures that the idle task is using init_mm right before its cpu goes
  4264. * offline.
  4265. */
  4266. void idle_task_exit(void)
  4267. {
  4268. struct mm_struct *mm = current->active_mm;
  4269. BUG_ON(cpu_online(smp_processor_id()));
  4270. if (mm != &init_mm) {
  4271. switch_mm(mm, &init_mm, current);
  4272. finish_arch_post_lock_switch();
  4273. }
  4274. mmdrop(mm);
  4275. }
  4276. /*
  4277. * Since this CPU is going 'away' for a while, fold any nr_active delta
  4278. * we might have. Assumes we're called after migrate_tasks() so that the
  4279. * nr_active count is stable.
  4280. *
  4281. * Also see the comment "Global load-average calculations".
  4282. */
  4283. static void calc_load_migrate(struct rq *rq)
  4284. {
  4285. long delta = calc_load_fold_active(rq);
  4286. if (delta)
  4287. atomic_long_add(delta, &calc_load_tasks);
  4288. }
  4289. static void put_prev_task_fake(struct rq *rq, struct task_struct *prev)
  4290. {
  4291. }
  4292. static const struct sched_class fake_sched_class = {
  4293. .put_prev_task = put_prev_task_fake,
  4294. };
  4295. static struct task_struct fake_task = {
  4296. /*
  4297. * Avoid pull_{rt,dl}_task()
  4298. */
  4299. .prio = MAX_PRIO + 1,
  4300. .sched_class = &fake_sched_class,
  4301. };
  4302. /*
  4303. * Migrate all tasks from the rq, sleeping tasks will be migrated by
  4304. * try_to_wake_up()->select_task_rq().
  4305. *
  4306. * Called with rq->lock held even though we'er in stop_machine() and
  4307. * there's no concurrency possible, we hold the required locks anyway
  4308. * because of lock validation efforts.
  4309. */
  4310. static void migrate_tasks(unsigned int dead_cpu)
  4311. {
  4312. struct rq *rq = cpu_rq(dead_cpu);
  4313. struct task_struct *next, *stop = rq->stop;
  4314. int dest_cpu;
  4315. /*
  4316. * Fudge the rq selection such that the below task selection loop
  4317. * doesn't get stuck on the currently eligible stop task.
  4318. *
  4319. * We're currently inside stop_machine() and the rq is either stuck
  4320. * in the stop_machine_cpu_stop() loop, or we're executing this code,
  4321. * either way we should never end up calling schedule() until we're
  4322. * done here.
  4323. */
  4324. rq->stop = NULL;
  4325. /*
  4326. * put_prev_task() and pick_next_task() sched
  4327. * class method both need to have an up-to-date
  4328. * value of rq->clock[_task]
  4329. */
  4330. update_rq_clock(rq);
  4331. for ( ; ; ) {
  4332. /*
  4333. * There's this thread running, bail when that's the only
  4334. * remaining thread.
  4335. */
  4336. if (rq->nr_running == 1)
  4337. break;
  4338. next = pick_next_task(rq, &fake_task);
  4339. BUG_ON(!next);
  4340. next->sched_class->put_prev_task(rq, next);
  4341. /* Find suitable destination for @next, with force if needed. */
  4342. dest_cpu = select_fallback_rq(dead_cpu, next);
  4343. raw_spin_unlock(&rq->lock);
  4344. __migrate_task(next, dead_cpu, dest_cpu);
  4345. raw_spin_lock(&rq->lock);
  4346. }
  4347. rq->stop = stop;
  4348. }
  4349. #endif /* CONFIG_HOTPLUG_CPU */
  4350. #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
  4351. static struct ctl_table sd_ctl_dir[] = {
  4352. {
  4353. .procname = "sched_domain",
  4354. .mode = 0555,
  4355. },
  4356. {}
  4357. };
  4358. static struct ctl_table sd_ctl_root[] = {
  4359. {
  4360. .procname = "kernel",
  4361. .mode = 0555,
  4362. .child = sd_ctl_dir,
  4363. },
  4364. {}
  4365. };
  4366. static struct ctl_table *sd_alloc_ctl_entry(int n)
  4367. {
  4368. struct ctl_table *entry =
  4369. kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
  4370. return entry;
  4371. }
  4372. static void sd_free_ctl_entry(struct ctl_table **tablep)
  4373. {
  4374. struct ctl_table *entry;
  4375. /*
  4376. * In the intermediate directories, both the child directory and
  4377. * procname are dynamically allocated and could fail but the mode
  4378. * will always be set. In the lowest directory the names are
  4379. * static strings and all have proc handlers.
  4380. */
  4381. for (entry = *tablep; entry->mode; entry++) {
  4382. if (entry->child)
  4383. sd_free_ctl_entry(&entry->child);
  4384. if (entry->proc_handler == NULL)
  4385. kfree(entry->procname);
  4386. }
  4387. kfree(*tablep);
  4388. *tablep = NULL;
  4389. }
  4390. static int min_load_idx = 0;
  4391. static int max_load_idx = CPU_LOAD_IDX_MAX-1;
  4392. static void
  4393. set_table_entry(struct ctl_table *entry,
  4394. const char *procname, void *data, int maxlen,
  4395. umode_t mode, proc_handler *proc_handler,
  4396. bool load_idx)
  4397. {
  4398. entry->procname = procname;
  4399. entry->data = data;
  4400. entry->maxlen = maxlen;
  4401. entry->mode = mode;
  4402. entry->proc_handler = proc_handler;
  4403. if (load_idx) {
  4404. entry->extra1 = &min_load_idx;
  4405. entry->extra2 = &max_load_idx;
  4406. }
  4407. }
  4408. static struct ctl_table *
  4409. sd_alloc_ctl_domain_table(struct sched_domain *sd)
  4410. {
  4411. struct ctl_table *table = sd_alloc_ctl_entry(14);
  4412. if (table == NULL)
  4413. return NULL;
  4414. set_table_entry(&table[0], "min_interval", &sd->min_interval,
  4415. sizeof(long), 0644, proc_doulongvec_minmax, false);
  4416. set_table_entry(&table[1], "max_interval", &sd->max_interval,
  4417. sizeof(long), 0644, proc_doulongvec_minmax, false);
  4418. set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
  4419. sizeof(int), 0644, proc_dointvec_minmax, true);
  4420. set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
  4421. sizeof(int), 0644, proc_dointvec_minmax, true);
  4422. set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
  4423. sizeof(int), 0644, proc_dointvec_minmax, true);
  4424. set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
  4425. sizeof(int), 0644, proc_dointvec_minmax, true);
  4426. set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
  4427. sizeof(int), 0644, proc_dointvec_minmax, true);
  4428. set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
  4429. sizeof(int), 0644, proc_dointvec_minmax, false);
  4430. set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
  4431. sizeof(int), 0644, proc_dointvec_minmax, false);
  4432. set_table_entry(&table[9], "cache_nice_tries",
  4433. &sd->cache_nice_tries,
  4434. sizeof(int), 0644, proc_dointvec_minmax, false);
  4435. set_table_entry(&table[10], "flags", &sd->flags,
  4436. sizeof(int), 0644, proc_dointvec_minmax, false);
  4437. set_table_entry(&table[11], "max_newidle_lb_cost",
  4438. &sd->max_newidle_lb_cost,
  4439. sizeof(long), 0644, proc_doulongvec_minmax, false);
  4440. set_table_entry(&table[12], "name", sd->name,
  4441. CORENAME_MAX_SIZE, 0444, proc_dostring, false);
  4442. /* &table[13] is terminator */
  4443. return table;
  4444. }
  4445. static struct ctl_table *sd_alloc_ctl_cpu_table(int cpu)
  4446. {
  4447. struct ctl_table *entry, *table;
  4448. struct sched_domain *sd;
  4449. int domain_num = 0, i;
  4450. char buf[32];
  4451. for_each_domain(cpu, sd)
  4452. domain_num++;
  4453. entry = table = sd_alloc_ctl_entry(domain_num + 1);
  4454. if (table == NULL)
  4455. return NULL;
  4456. i = 0;
  4457. for_each_domain(cpu, sd) {
  4458. snprintf(buf, 32, "domain%d", i);
  4459. entry->procname = kstrdup(buf, GFP_KERNEL);
  4460. entry->mode = 0555;
  4461. entry->child = sd_alloc_ctl_domain_table(sd);
  4462. entry++;
  4463. i++;
  4464. }
  4465. return table;
  4466. }
  4467. static struct ctl_table_header *sd_sysctl_header;
  4468. static void register_sched_domain_sysctl(void)
  4469. {
  4470. int i, cpu_num = num_possible_cpus();
  4471. struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
  4472. char buf[32];
  4473. WARN_ON(sd_ctl_dir[0].child);
  4474. sd_ctl_dir[0].child = entry;
  4475. if (entry == NULL)
  4476. return;
  4477. for_each_possible_cpu(i) {
  4478. snprintf(buf, 32, "cpu%d", i);
  4479. entry->procname = kstrdup(buf, GFP_KERNEL);
  4480. entry->mode = 0555;
  4481. entry->child = sd_alloc_ctl_cpu_table(i);
  4482. entry++;
  4483. }
  4484. WARN_ON(sd_sysctl_header);
  4485. sd_sysctl_header = register_sysctl_table(sd_ctl_root);
  4486. }
  4487. /* may be called multiple times per register */
  4488. static void unregister_sched_domain_sysctl(void)
  4489. {
  4490. if (sd_sysctl_header)
  4491. unregister_sysctl_table(sd_sysctl_header);
  4492. sd_sysctl_header = NULL;
  4493. if (sd_ctl_dir[0].child)
  4494. sd_free_ctl_entry(&sd_ctl_dir[0].child);
  4495. }
  4496. #else
  4497. static void register_sched_domain_sysctl(void)
  4498. {
  4499. }
  4500. static void unregister_sched_domain_sysctl(void)
  4501. {
  4502. }
  4503. #endif
  4504. static void set_rq_online(struct rq *rq)
  4505. {
  4506. if (!rq->online) {
  4507. const struct sched_class *class;
  4508. cpumask_set_cpu(rq->cpu, rq->rd->online);
  4509. rq->online = 1;
  4510. for_each_class(class) {
  4511. if (class->rq_online)
  4512. class->rq_online(rq);
  4513. }
  4514. }
  4515. }
  4516. static void set_rq_offline(struct rq *rq)
  4517. {
  4518. if (rq->online) {
  4519. const struct sched_class *class;
  4520. for_each_class(class) {
  4521. if (class->rq_offline)
  4522. class->rq_offline(rq);
  4523. }
  4524. cpumask_clear_cpu(rq->cpu, rq->rd->online);
  4525. rq->online = 0;
  4526. }
  4527. }
  4528. /*
  4529. * migration_call - callback that gets triggered when a CPU is added.
  4530. * Here we can start up the necessary migration thread for the new CPU.
  4531. */
  4532. static int
  4533. migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  4534. {
  4535. int cpu = (long)hcpu;
  4536. unsigned long flags;
  4537. struct rq *rq = cpu_rq(cpu);
  4538. switch (action & ~CPU_TASKS_FROZEN) {
  4539. case CPU_UP_PREPARE:
  4540. rq->calc_load_update = calc_load_update;
  4541. break;
  4542. case CPU_ONLINE:
  4543. /* Update our root-domain */
  4544. raw_spin_lock_irqsave(&rq->lock, flags);
  4545. if (rq->rd) {
  4546. BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  4547. set_rq_online(rq);
  4548. }
  4549. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4550. break;
  4551. #ifdef CONFIG_HOTPLUG_CPU
  4552. case CPU_DYING:
  4553. sched_ttwu_pending();
  4554. /* Update our root-domain */
  4555. raw_spin_lock_irqsave(&rq->lock, flags);
  4556. if (rq->rd) {
  4557. BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  4558. set_rq_offline(rq);
  4559. }
  4560. migrate_tasks(cpu);
  4561. BUG_ON(rq->nr_running != 1); /* the migration thread */
  4562. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4563. break;
  4564. case CPU_DEAD:
  4565. calc_load_migrate(rq);
  4566. break;
  4567. #endif
  4568. }
  4569. update_max_interval();
  4570. return NOTIFY_OK;
  4571. }
  4572. /*
  4573. * Register at high priority so that task migration (migrate_all_tasks)
  4574. * happens before everything else. This has to be lower priority than
  4575. * the notifier in the perf_event subsystem, though.
  4576. */
  4577. static struct notifier_block migration_notifier = {
  4578. .notifier_call = migration_call,
  4579. .priority = CPU_PRI_MIGRATION,
  4580. };
  4581. static void set_cpu_rq_start_time(void)
  4582. {
  4583. int cpu = smp_processor_id();
  4584. struct rq *rq = cpu_rq(cpu);
  4585. rq->age_stamp = sched_clock_cpu(cpu);
  4586. }
  4587. static int sched_cpu_active(struct notifier_block *nfb,
  4588. unsigned long action, void *hcpu)
  4589. {
  4590. switch (action & ~CPU_TASKS_FROZEN) {
  4591. case CPU_STARTING:
  4592. set_cpu_rq_start_time();
  4593. return NOTIFY_OK;
  4594. case CPU_DOWN_FAILED:
  4595. set_cpu_active((long)hcpu, true);
  4596. return NOTIFY_OK;
  4597. default:
  4598. return NOTIFY_DONE;
  4599. }
  4600. }
  4601. static int sched_cpu_inactive(struct notifier_block *nfb,
  4602. unsigned long action, void *hcpu)
  4603. {
  4604. switch (action & ~CPU_TASKS_FROZEN) {
  4605. case CPU_DOWN_PREPARE:
  4606. set_cpu_active((long)hcpu, false);
  4607. return NOTIFY_OK;
  4608. default:
  4609. return NOTIFY_DONE;
  4610. }
  4611. }
  4612. static int __init migration_init(void)
  4613. {
  4614. void *cpu = (void *)(long)smp_processor_id();
  4615. int err;
  4616. /* Initialize migration for the boot CPU */
  4617. err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  4618. BUG_ON(err == NOTIFY_BAD);
  4619. migration_call(&migration_notifier, CPU_ONLINE, cpu);
  4620. register_cpu_notifier(&migration_notifier);
  4621. /* Register cpu active notifiers */
  4622. cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
  4623. cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
  4624. return 0;
  4625. }
  4626. early_initcall(migration_init);
  4627. #endif
  4628. #ifdef CONFIG_SMP
  4629. static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
  4630. #ifdef CONFIG_SCHED_DEBUG
  4631. static __read_mostly int sched_debug_enabled;
  4632. static int __init sched_debug_setup(char *str)
  4633. {
  4634. sched_debug_enabled = 1;
  4635. return 0;
  4636. }
  4637. early_param("sched_debug", sched_debug_setup);
  4638. static inline bool sched_debug(void)
  4639. {
  4640. return sched_debug_enabled;
  4641. }
  4642. static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
  4643. struct cpumask *groupmask)
  4644. {
  4645. struct sched_group *group = sd->groups;
  4646. cpumask_clear(groupmask);
  4647. printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
  4648. if (!(sd->flags & SD_LOAD_BALANCE)) {
  4649. printk("does not load-balance\n");
  4650. if (sd->parent)
  4651. printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
  4652. " has parent");
  4653. return -1;
  4654. }
  4655. printk(KERN_CONT "span %*pbl level %s\n",
  4656. cpumask_pr_args(sched_domain_span(sd)), sd->name);
  4657. if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  4658. printk(KERN_ERR "ERROR: domain->span does not contain "
  4659. "CPU%d\n", cpu);
  4660. }
  4661. if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
  4662. printk(KERN_ERR "ERROR: domain->groups does not contain"
  4663. " CPU%d\n", cpu);
  4664. }
  4665. printk(KERN_DEBUG "%*s groups:", level + 1, "");
  4666. do {
  4667. if (!group) {
  4668. printk("\n");
  4669. printk(KERN_ERR "ERROR: group is NULL\n");
  4670. break;
  4671. }
  4672. if (!cpumask_weight(sched_group_cpus(group))) {
  4673. printk(KERN_CONT "\n");
  4674. printk(KERN_ERR "ERROR: empty group\n");
  4675. break;
  4676. }
  4677. if (!(sd->flags & SD_OVERLAP) &&
  4678. cpumask_intersects(groupmask, sched_group_cpus(group))) {
  4679. printk(KERN_CONT "\n");
  4680. printk(KERN_ERR "ERROR: repeated CPUs\n");
  4681. break;
  4682. }
  4683. cpumask_or(groupmask, groupmask, sched_group_cpus(group));
  4684. printk(KERN_CONT " %*pbl",
  4685. cpumask_pr_args(sched_group_cpus(group)));
  4686. if (group->sgc->capacity != SCHED_CAPACITY_SCALE) {
  4687. printk(KERN_CONT " (cpu_capacity = %d)",
  4688. group->sgc->capacity);
  4689. }
  4690. group = group->next;
  4691. } while (group != sd->groups);
  4692. printk(KERN_CONT "\n");
  4693. if (!cpumask_equal(sched_domain_span(sd), groupmask))
  4694. printk(KERN_ERR "ERROR: groups don't span domain->span\n");
  4695. if (sd->parent &&
  4696. !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
  4697. printk(KERN_ERR "ERROR: parent span is not a superset "
  4698. "of domain->span\n");
  4699. return 0;
  4700. }
  4701. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  4702. {
  4703. int level = 0;
  4704. if (!sched_debug_enabled)
  4705. return;
  4706. if (!sd) {
  4707. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  4708. return;
  4709. }
  4710. printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  4711. for (;;) {
  4712. if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
  4713. break;
  4714. level++;
  4715. sd = sd->parent;
  4716. if (!sd)
  4717. break;
  4718. }
  4719. }
  4720. #else /* !CONFIG_SCHED_DEBUG */
  4721. # define sched_domain_debug(sd, cpu) do { } while (0)
  4722. static inline bool sched_debug(void)
  4723. {
  4724. return false;
  4725. }
  4726. #endif /* CONFIG_SCHED_DEBUG */
  4727. static int sd_degenerate(struct sched_domain *sd)
  4728. {
  4729. if (cpumask_weight(sched_domain_span(sd)) == 1)
  4730. return 1;
  4731. /* Following flags need at least 2 groups */
  4732. if (sd->flags & (SD_LOAD_BALANCE |
  4733. SD_BALANCE_NEWIDLE |
  4734. SD_BALANCE_FORK |
  4735. SD_BALANCE_EXEC |
  4736. SD_SHARE_CPUCAPACITY |
  4737. SD_SHARE_PKG_RESOURCES |
  4738. SD_SHARE_POWERDOMAIN)) {
  4739. if (sd->groups != sd->groups->next)
  4740. return 0;
  4741. }
  4742. /* Following flags don't use groups */
  4743. if (sd->flags & (SD_WAKE_AFFINE))
  4744. return 0;
  4745. return 1;
  4746. }
  4747. static int
  4748. sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  4749. {
  4750. unsigned long cflags = sd->flags, pflags = parent->flags;
  4751. if (sd_degenerate(parent))
  4752. return 1;
  4753. if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
  4754. return 0;
  4755. /* Flags needing groups don't count if only 1 group in parent */
  4756. if (parent->groups == parent->groups->next) {
  4757. pflags &= ~(SD_LOAD_BALANCE |
  4758. SD_BALANCE_NEWIDLE |
  4759. SD_BALANCE_FORK |
  4760. SD_BALANCE_EXEC |
  4761. SD_SHARE_CPUCAPACITY |
  4762. SD_SHARE_PKG_RESOURCES |
  4763. SD_PREFER_SIBLING |
  4764. SD_SHARE_POWERDOMAIN);
  4765. if (nr_node_ids == 1)
  4766. pflags &= ~SD_SERIALIZE;
  4767. }
  4768. if (~cflags & pflags)
  4769. return 0;
  4770. return 1;
  4771. }
  4772. static void free_rootdomain(struct rcu_head *rcu)
  4773. {
  4774. struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
  4775. cpupri_cleanup(&rd->cpupri);
  4776. cpudl_cleanup(&rd->cpudl);
  4777. free_cpumask_var(rd->dlo_mask);
  4778. free_cpumask_var(rd->rto_mask);
  4779. free_cpumask_var(rd->online);
  4780. free_cpumask_var(rd->span);
  4781. kfree(rd);
  4782. }
  4783. static void rq_attach_root(struct rq *rq, struct root_domain *rd)
  4784. {
  4785. struct root_domain *old_rd = NULL;
  4786. unsigned long flags;
  4787. raw_spin_lock_irqsave(&rq->lock, flags);
  4788. if (rq->rd) {
  4789. old_rd = rq->rd;
  4790. if (cpumask_test_cpu(rq->cpu, old_rd->online))
  4791. set_rq_offline(rq);
  4792. cpumask_clear_cpu(rq->cpu, old_rd->span);
  4793. /*
  4794. * If we dont want to free the old_rd yet then
  4795. * set old_rd to NULL to skip the freeing later
  4796. * in this function:
  4797. */
  4798. if (!atomic_dec_and_test(&old_rd->refcount))
  4799. old_rd = NULL;
  4800. }
  4801. atomic_inc(&rd->refcount);
  4802. rq->rd = rd;
  4803. cpumask_set_cpu(rq->cpu, rd->span);
  4804. if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
  4805. set_rq_online(rq);
  4806. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4807. if (old_rd)
  4808. call_rcu_sched(&old_rd->rcu, free_rootdomain);
  4809. }
  4810. static int init_rootdomain(struct root_domain *rd)
  4811. {
  4812. memset(rd, 0, sizeof(*rd));
  4813. if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
  4814. goto out;
  4815. if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
  4816. goto free_span;
  4817. if (!alloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
  4818. goto free_online;
  4819. if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
  4820. goto free_dlo_mask;
  4821. init_dl_bw(&rd->dl_bw);
  4822. if (cpudl_init(&rd->cpudl) != 0)
  4823. goto free_dlo_mask;
  4824. if (cpupri_init(&rd->cpupri) != 0)
  4825. goto free_rto_mask;
  4826. return 0;
  4827. free_rto_mask:
  4828. free_cpumask_var(rd->rto_mask);
  4829. free_dlo_mask:
  4830. free_cpumask_var(rd->dlo_mask);
  4831. free_online:
  4832. free_cpumask_var(rd->online);
  4833. free_span:
  4834. free_cpumask_var(rd->span);
  4835. out:
  4836. return -ENOMEM;
  4837. }
  4838. /*
  4839. * By default the system creates a single root-domain with all cpus as
  4840. * members (mimicking the global state we have today).
  4841. */
  4842. struct root_domain def_root_domain;
  4843. static void init_defrootdomain(void)
  4844. {
  4845. init_rootdomain(&def_root_domain);
  4846. atomic_set(&def_root_domain.refcount, 1);
  4847. }
  4848. static struct root_domain *alloc_rootdomain(void)
  4849. {
  4850. struct root_domain *rd;
  4851. rd = kmalloc(sizeof(*rd), GFP_KERNEL);
  4852. if (!rd)
  4853. return NULL;
  4854. if (init_rootdomain(rd) != 0) {
  4855. kfree(rd);
  4856. return NULL;
  4857. }
  4858. return rd;
  4859. }
  4860. static void free_sched_groups(struct sched_group *sg, int free_sgc)
  4861. {
  4862. struct sched_group *tmp, *first;
  4863. if (!sg)
  4864. return;
  4865. first = sg;
  4866. do {
  4867. tmp = sg->next;
  4868. if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
  4869. kfree(sg->sgc);
  4870. kfree(sg);
  4871. sg = tmp;
  4872. } while (sg != first);
  4873. }
  4874. static void free_sched_domain(struct rcu_head *rcu)
  4875. {
  4876. struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
  4877. /*
  4878. * If its an overlapping domain it has private groups, iterate and
  4879. * nuke them all.
  4880. */
  4881. if (sd->flags & SD_OVERLAP) {
  4882. free_sched_groups(sd->groups, 1);
  4883. } else if (atomic_dec_and_test(&sd->groups->ref)) {
  4884. kfree(sd->groups->sgc);
  4885. kfree(sd->groups);
  4886. }
  4887. kfree(sd);
  4888. }
  4889. static void destroy_sched_domain(struct sched_domain *sd, int cpu)
  4890. {
  4891. call_rcu(&sd->rcu, free_sched_domain);
  4892. }
  4893. static void destroy_sched_domains(struct sched_domain *sd, int cpu)
  4894. {
  4895. for (; sd; sd = sd->parent)
  4896. destroy_sched_domain(sd, cpu);
  4897. }
  4898. /*
  4899. * Keep a special pointer to the highest sched_domain that has
  4900. * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
  4901. * allows us to avoid some pointer chasing select_idle_sibling().
  4902. *
  4903. * Also keep a unique ID per domain (we use the first cpu number in
  4904. * the cpumask of the domain), this allows us to quickly tell if
  4905. * two cpus are in the same cache domain, see cpus_share_cache().
  4906. */
  4907. DEFINE_PER_CPU(struct sched_domain *, sd_llc);
  4908. DEFINE_PER_CPU(int, sd_llc_size);
  4909. DEFINE_PER_CPU(int, sd_llc_id);
  4910. DEFINE_PER_CPU(struct sched_domain *, sd_numa);
  4911. DEFINE_PER_CPU(struct sched_domain *, sd_busy);
  4912. DEFINE_PER_CPU(struct sched_domain *, sd_asym);
  4913. static void update_top_cache_domain(int cpu)
  4914. {
  4915. struct sched_domain *sd;
  4916. struct sched_domain *busy_sd = NULL;
  4917. int id = cpu;
  4918. int size = 1;
  4919. sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
  4920. if (sd) {
  4921. id = cpumask_first(sched_domain_span(sd));
  4922. size = cpumask_weight(sched_domain_span(sd));
  4923. busy_sd = sd->parent; /* sd_busy */
  4924. }
  4925. rcu_assign_pointer(per_cpu(sd_busy, cpu), busy_sd);
  4926. rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
  4927. per_cpu(sd_llc_size, cpu) = size;
  4928. per_cpu(sd_llc_id, cpu) = id;
  4929. sd = lowest_flag_domain(cpu, SD_NUMA);
  4930. rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
  4931. sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
  4932. rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
  4933. }
  4934. /*
  4935. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  4936. * hold the hotplug lock.
  4937. */
  4938. static void
  4939. cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
  4940. {
  4941. struct rq *rq = cpu_rq(cpu);
  4942. struct sched_domain *tmp;
  4943. /* Remove the sched domains which do not contribute to scheduling. */
  4944. for (tmp = sd; tmp; ) {
  4945. struct sched_domain *parent = tmp->parent;
  4946. if (!parent)
  4947. break;
  4948. if (sd_parent_degenerate(tmp, parent)) {
  4949. tmp->parent = parent->parent;
  4950. if (parent->parent)
  4951. parent->parent->child = tmp;
  4952. /*
  4953. * Transfer SD_PREFER_SIBLING down in case of a
  4954. * degenerate parent; the spans match for this
  4955. * so the property transfers.
  4956. */
  4957. if (parent->flags & SD_PREFER_SIBLING)
  4958. tmp->flags |= SD_PREFER_SIBLING;
  4959. destroy_sched_domain(parent, cpu);
  4960. } else
  4961. tmp = tmp->parent;
  4962. }
  4963. if (sd && sd_degenerate(sd)) {
  4964. tmp = sd;
  4965. sd = sd->parent;
  4966. destroy_sched_domain(tmp, cpu);
  4967. if (sd)
  4968. sd->child = NULL;
  4969. }
  4970. sched_domain_debug(sd, cpu);
  4971. rq_attach_root(rq, rd);
  4972. tmp = rq->sd;
  4973. rcu_assign_pointer(rq->sd, sd);
  4974. destroy_sched_domains(tmp, cpu);
  4975. update_top_cache_domain(cpu);
  4976. }
  4977. /* Setup the mask of cpus configured for isolated domains */
  4978. static int __init isolated_cpu_setup(char *str)
  4979. {
  4980. alloc_bootmem_cpumask_var(&cpu_isolated_map);
  4981. cpulist_parse(str, cpu_isolated_map);
  4982. return 1;
  4983. }
  4984. __setup("isolcpus=", isolated_cpu_setup);
  4985. struct s_data {
  4986. struct sched_domain ** __percpu sd;
  4987. struct root_domain *rd;
  4988. };
  4989. enum s_alloc {
  4990. sa_rootdomain,
  4991. sa_sd,
  4992. sa_sd_storage,
  4993. sa_none,
  4994. };
  4995. /*
  4996. * Build an iteration mask that can exclude certain CPUs from the upwards
  4997. * domain traversal.
  4998. *
  4999. * Asymmetric node setups can result in situations where the domain tree is of
  5000. * unequal depth, make sure to skip domains that already cover the entire
  5001. * range.
  5002. *
  5003. * In that case build_sched_domains() will have terminated the iteration early
  5004. * and our sibling sd spans will be empty. Domains should always include the
  5005. * cpu they're built on, so check that.
  5006. *
  5007. */
  5008. static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
  5009. {
  5010. const struct cpumask *span = sched_domain_span(sd);
  5011. struct sd_data *sdd = sd->private;
  5012. struct sched_domain *sibling;
  5013. int i;
  5014. for_each_cpu(i, span) {
  5015. sibling = *per_cpu_ptr(sdd->sd, i);
  5016. if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
  5017. continue;
  5018. cpumask_set_cpu(i, sched_group_mask(sg));
  5019. }
  5020. }
  5021. /*
  5022. * Return the canonical balance cpu for this group, this is the first cpu
  5023. * of this group that's also in the iteration mask.
  5024. */
  5025. int group_balance_cpu(struct sched_group *sg)
  5026. {
  5027. return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
  5028. }
  5029. static int
  5030. build_overlap_sched_groups(struct sched_domain *sd, int cpu)
  5031. {
  5032. struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
  5033. const struct cpumask *span = sched_domain_span(sd);
  5034. struct cpumask *covered = sched_domains_tmpmask;
  5035. struct sd_data *sdd = sd->private;
  5036. struct sched_domain *sibling;
  5037. int i;
  5038. cpumask_clear(covered);
  5039. for_each_cpu(i, span) {
  5040. struct cpumask *sg_span;
  5041. if (cpumask_test_cpu(i, covered))
  5042. continue;
  5043. sibling = *per_cpu_ptr(sdd->sd, i);
  5044. /* See the comment near build_group_mask(). */
  5045. if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
  5046. continue;
  5047. sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
  5048. GFP_KERNEL, cpu_to_node(cpu));
  5049. if (!sg)
  5050. goto fail;
  5051. sg_span = sched_group_cpus(sg);
  5052. if (sibling->child)
  5053. cpumask_copy(sg_span, sched_domain_span(sibling->child));
  5054. else
  5055. cpumask_set_cpu(i, sg_span);
  5056. cpumask_or(covered, covered, sg_span);
  5057. sg->sgc = *per_cpu_ptr(sdd->sgc, i);
  5058. if (atomic_inc_return(&sg->sgc->ref) == 1)
  5059. build_group_mask(sd, sg);
  5060. /*
  5061. * Initialize sgc->capacity such that even if we mess up the
  5062. * domains and no possible iteration will get us here, we won't
  5063. * die on a /0 trap.
  5064. */
  5065. sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
  5066. /*
  5067. * Make sure the first group of this domain contains the
  5068. * canonical balance cpu. Otherwise the sched_domain iteration
  5069. * breaks. See update_sg_lb_stats().
  5070. */
  5071. if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
  5072. group_balance_cpu(sg) == cpu)
  5073. groups = sg;
  5074. if (!first)
  5075. first = sg;
  5076. if (last)
  5077. last->next = sg;
  5078. last = sg;
  5079. last->next = first;
  5080. }
  5081. sd->groups = groups;
  5082. return 0;
  5083. fail:
  5084. free_sched_groups(first, 0);
  5085. return -ENOMEM;
  5086. }
  5087. static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
  5088. {
  5089. struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
  5090. struct sched_domain *child = sd->child;
  5091. if (child)
  5092. cpu = cpumask_first(sched_domain_span(child));
  5093. if (sg) {
  5094. *sg = *per_cpu_ptr(sdd->sg, cpu);
  5095. (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
  5096. atomic_set(&(*sg)->sgc->ref, 1); /* for claim_allocations */
  5097. }
  5098. return cpu;
  5099. }
  5100. /*
  5101. * build_sched_groups will build a circular linked list of the groups
  5102. * covered by the given span, and will set each group's ->cpumask correctly,
  5103. * and ->cpu_capacity to 0.
  5104. *
  5105. * Assumes the sched_domain tree is fully constructed
  5106. */
  5107. static int
  5108. build_sched_groups(struct sched_domain *sd, int cpu)
  5109. {
  5110. struct sched_group *first = NULL, *last = NULL;
  5111. struct sd_data *sdd = sd->private;
  5112. const struct cpumask *span = sched_domain_span(sd);
  5113. struct cpumask *covered;
  5114. int i;
  5115. get_group(cpu, sdd, &sd->groups);
  5116. atomic_inc(&sd->groups->ref);
  5117. if (cpu != cpumask_first(span))
  5118. return 0;
  5119. lockdep_assert_held(&sched_domains_mutex);
  5120. covered = sched_domains_tmpmask;
  5121. cpumask_clear(covered);
  5122. for_each_cpu(i, span) {
  5123. struct sched_group *sg;
  5124. int group, j;
  5125. if (cpumask_test_cpu(i, covered))
  5126. continue;
  5127. group = get_group(i, sdd, &sg);
  5128. cpumask_setall(sched_group_mask(sg));
  5129. for_each_cpu(j, span) {
  5130. if (get_group(j, sdd, NULL) != group)
  5131. continue;
  5132. cpumask_set_cpu(j, covered);
  5133. cpumask_set_cpu(j, sched_group_cpus(sg));
  5134. }
  5135. if (!first)
  5136. first = sg;
  5137. if (last)
  5138. last->next = sg;
  5139. last = sg;
  5140. }
  5141. last->next = first;
  5142. return 0;
  5143. }
  5144. /*
  5145. * Initialize sched groups cpu_capacity.
  5146. *
  5147. * cpu_capacity indicates the capacity of sched group, which is used while
  5148. * distributing the load between different sched groups in a sched domain.
  5149. * Typically cpu_capacity for all the groups in a sched domain will be same
  5150. * unless there are asymmetries in the topology. If there are asymmetries,
  5151. * group having more cpu_capacity will pickup more load compared to the
  5152. * group having less cpu_capacity.
  5153. */
  5154. static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
  5155. {
  5156. struct sched_group *sg = sd->groups;
  5157. WARN_ON(!sg);
  5158. do {
  5159. sg->group_weight = cpumask_weight(sched_group_cpus(sg));
  5160. sg = sg->next;
  5161. } while (sg != sd->groups);
  5162. if (cpu != group_balance_cpu(sg))
  5163. return;
  5164. update_group_capacity(sd, cpu);
  5165. atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight);
  5166. }
  5167. /*
  5168. * Initializers for schedule domains
  5169. * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
  5170. */
  5171. static int default_relax_domain_level = -1;
  5172. int sched_domain_level_max;
  5173. static int __init setup_relax_domain_level(char *str)
  5174. {
  5175. if (kstrtoint(str, 0, &default_relax_domain_level))
  5176. pr_warn("Unable to set relax_domain_level\n");
  5177. return 1;
  5178. }
  5179. __setup("relax_domain_level=", setup_relax_domain_level);
  5180. static void set_domain_attribute(struct sched_domain *sd,
  5181. struct sched_domain_attr *attr)
  5182. {
  5183. int request;
  5184. if (!attr || attr->relax_domain_level < 0) {
  5185. if (default_relax_domain_level < 0)
  5186. return;
  5187. else
  5188. request = default_relax_domain_level;
  5189. } else
  5190. request = attr->relax_domain_level;
  5191. if (request < sd->level) {
  5192. /* turn off idle balance on this domain */
  5193. sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
  5194. } else {
  5195. /* turn on idle balance on this domain */
  5196. sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
  5197. }
  5198. }
  5199. static void __sdt_free(const struct cpumask *cpu_map);
  5200. static int __sdt_alloc(const struct cpumask *cpu_map);
  5201. static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
  5202. const struct cpumask *cpu_map)
  5203. {
  5204. switch (what) {
  5205. case sa_rootdomain:
  5206. if (!atomic_read(&d->rd->refcount))
  5207. free_rootdomain(&d->rd->rcu); /* fall through */
  5208. case sa_sd:
  5209. free_percpu(d->sd); /* fall through */
  5210. case sa_sd_storage:
  5211. __sdt_free(cpu_map); /* fall through */
  5212. case sa_none:
  5213. break;
  5214. }
  5215. }
  5216. static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
  5217. const struct cpumask *cpu_map)
  5218. {
  5219. memset(d, 0, sizeof(*d));
  5220. if (__sdt_alloc(cpu_map))
  5221. return sa_sd_storage;
  5222. d->sd = alloc_percpu(struct sched_domain *);
  5223. if (!d->sd)
  5224. return sa_sd_storage;
  5225. d->rd = alloc_rootdomain();
  5226. if (!d->rd)
  5227. return sa_sd;
  5228. return sa_rootdomain;
  5229. }
  5230. /*
  5231. * NULL the sd_data elements we've used to build the sched_domain and
  5232. * sched_group structure so that the subsequent __free_domain_allocs()
  5233. * will not free the data we're using.
  5234. */
  5235. static void claim_allocations(int cpu, struct sched_domain *sd)
  5236. {
  5237. struct sd_data *sdd = sd->private;
  5238. WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
  5239. *per_cpu_ptr(sdd->sd, cpu) = NULL;
  5240. if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
  5241. *per_cpu_ptr(sdd->sg, cpu) = NULL;
  5242. if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
  5243. *per_cpu_ptr(sdd->sgc, cpu) = NULL;
  5244. }
  5245. #ifdef CONFIG_NUMA
  5246. static int sched_domains_numa_levels;
  5247. enum numa_topology_type sched_numa_topology_type;
  5248. static int *sched_domains_numa_distance;
  5249. int sched_max_numa_distance;
  5250. static struct cpumask ***sched_domains_numa_masks;
  5251. static int sched_domains_curr_level;
  5252. #endif
  5253. /*
  5254. * SD_flags allowed in topology descriptions.
  5255. *
  5256. * SD_SHARE_CPUCAPACITY - describes SMT topologies
  5257. * SD_SHARE_PKG_RESOURCES - describes shared caches
  5258. * SD_NUMA - describes NUMA topologies
  5259. * SD_SHARE_POWERDOMAIN - describes shared power domain
  5260. *
  5261. * Odd one out:
  5262. * SD_ASYM_PACKING - describes SMT quirks
  5263. */
  5264. #define TOPOLOGY_SD_FLAGS \
  5265. (SD_SHARE_CPUCAPACITY | \
  5266. SD_SHARE_PKG_RESOURCES | \
  5267. SD_NUMA | \
  5268. SD_ASYM_PACKING | \
  5269. SD_SHARE_POWERDOMAIN)
  5270. static struct sched_domain *
  5271. sd_init(struct sched_domain_topology_level *tl, int cpu)
  5272. {
  5273. struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
  5274. int sd_weight, sd_flags = 0;
  5275. #ifdef CONFIG_NUMA
  5276. /*
  5277. * Ugly hack to pass state to sd_numa_mask()...
  5278. */
  5279. sched_domains_curr_level = tl->numa_level;
  5280. #endif
  5281. sd_weight = cpumask_weight(tl->mask(cpu));
  5282. if (tl->sd_flags)
  5283. sd_flags = (*tl->sd_flags)();
  5284. if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
  5285. "wrong sd_flags in topology description\n"))
  5286. sd_flags &= ~TOPOLOGY_SD_FLAGS;
  5287. *sd = (struct sched_domain){
  5288. .min_interval = sd_weight,
  5289. .max_interval = 2*sd_weight,
  5290. .busy_factor = 32,
  5291. .imbalance_pct = 125,
  5292. .cache_nice_tries = 0,
  5293. .busy_idx = 0,
  5294. .idle_idx = 0,
  5295. .newidle_idx = 0,
  5296. .wake_idx = 0,
  5297. .forkexec_idx = 0,
  5298. .flags = 1*SD_LOAD_BALANCE
  5299. | 1*SD_BALANCE_NEWIDLE
  5300. | 1*SD_BALANCE_EXEC
  5301. | 1*SD_BALANCE_FORK
  5302. | 0*SD_BALANCE_WAKE
  5303. | 1*SD_WAKE_AFFINE
  5304. | 0*SD_SHARE_CPUCAPACITY
  5305. | 0*SD_SHARE_PKG_RESOURCES
  5306. | 0*SD_SERIALIZE
  5307. | 0*SD_PREFER_SIBLING
  5308. | 0*SD_NUMA
  5309. | sd_flags
  5310. ,
  5311. .last_balance = jiffies,
  5312. .balance_interval = sd_weight,
  5313. .smt_gain = 0,
  5314. .max_newidle_lb_cost = 0,
  5315. .next_decay_max_lb_cost = jiffies,
  5316. #ifdef CONFIG_SCHED_DEBUG
  5317. .name = tl->name,
  5318. #endif
  5319. };
  5320. /*
  5321. * Convert topological properties into behaviour.
  5322. */
  5323. if (sd->flags & SD_SHARE_CPUCAPACITY) {
  5324. sd->flags |= SD_PREFER_SIBLING;
  5325. sd->imbalance_pct = 110;
  5326. sd->smt_gain = 1178; /* ~15% */
  5327. } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
  5328. sd->imbalance_pct = 117;
  5329. sd->cache_nice_tries = 1;
  5330. sd->busy_idx = 2;
  5331. #ifdef CONFIG_NUMA
  5332. } else if (sd->flags & SD_NUMA) {
  5333. sd->cache_nice_tries = 2;
  5334. sd->busy_idx = 3;
  5335. sd->idle_idx = 2;
  5336. sd->flags |= SD_SERIALIZE;
  5337. if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
  5338. sd->flags &= ~(SD_BALANCE_EXEC |
  5339. SD_BALANCE_FORK |
  5340. SD_WAKE_AFFINE);
  5341. }
  5342. #endif
  5343. } else {
  5344. sd->flags |= SD_PREFER_SIBLING;
  5345. sd->cache_nice_tries = 1;
  5346. sd->busy_idx = 2;
  5347. sd->idle_idx = 1;
  5348. }
  5349. sd->private = &tl->data;
  5350. return sd;
  5351. }
  5352. /*
  5353. * Topology list, bottom-up.
  5354. */
  5355. static struct sched_domain_topology_level default_topology[] = {
  5356. #ifdef CONFIG_SCHED_SMT
  5357. { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
  5358. #endif
  5359. #ifdef CONFIG_SCHED_MC
  5360. { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
  5361. #endif
  5362. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  5363. { NULL, },
  5364. };
  5365. struct sched_domain_topology_level *sched_domain_topology = default_topology;
  5366. #define for_each_sd_topology(tl) \
  5367. for (tl = sched_domain_topology; tl->mask; tl++)
  5368. void set_sched_topology(struct sched_domain_topology_level *tl)
  5369. {
  5370. sched_domain_topology = tl;
  5371. }
  5372. #ifdef CONFIG_NUMA
  5373. static const struct cpumask *sd_numa_mask(int cpu)
  5374. {
  5375. return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
  5376. }
  5377. static void sched_numa_warn(const char *str)
  5378. {
  5379. static int done = false;
  5380. int i,j;
  5381. if (done)
  5382. return;
  5383. done = true;
  5384. printk(KERN_WARNING "ERROR: %s\n\n", str);
  5385. for (i = 0; i < nr_node_ids; i++) {
  5386. printk(KERN_WARNING " ");
  5387. for (j = 0; j < nr_node_ids; j++)
  5388. printk(KERN_CONT "%02d ", node_distance(i,j));
  5389. printk(KERN_CONT "\n");
  5390. }
  5391. printk(KERN_WARNING "\n");
  5392. }
  5393. bool find_numa_distance(int distance)
  5394. {
  5395. int i;
  5396. if (distance == node_distance(0, 0))
  5397. return true;
  5398. for (i = 0; i < sched_domains_numa_levels; i++) {
  5399. if (sched_domains_numa_distance[i] == distance)
  5400. return true;
  5401. }
  5402. return false;
  5403. }
  5404. /*
  5405. * A system can have three types of NUMA topology:
  5406. * NUMA_DIRECT: all nodes are directly connected, or not a NUMA system
  5407. * NUMA_GLUELESS_MESH: some nodes reachable through intermediary nodes
  5408. * NUMA_BACKPLANE: nodes can reach other nodes through a backplane
  5409. *
  5410. * The difference between a glueless mesh topology and a backplane
  5411. * topology lies in whether communication between not directly
  5412. * connected nodes goes through intermediary nodes (where programs
  5413. * could run), or through backplane controllers. This affects
  5414. * placement of programs.
  5415. *
  5416. * The type of topology can be discerned with the following tests:
  5417. * - If the maximum distance between any nodes is 1 hop, the system
  5418. * is directly connected.
  5419. * - If for two nodes A and B, located N > 1 hops away from each other,
  5420. * there is an intermediary node C, which is < N hops away from both
  5421. * nodes A and B, the system is a glueless mesh.
  5422. */
  5423. static void init_numa_topology_type(void)
  5424. {
  5425. int a, b, c, n;
  5426. n = sched_max_numa_distance;
  5427. if (n <= 1)
  5428. sched_numa_topology_type = NUMA_DIRECT;
  5429. for_each_online_node(a) {
  5430. for_each_online_node(b) {
  5431. /* Find two nodes furthest removed from each other. */
  5432. if (node_distance(a, b) < n)
  5433. continue;
  5434. /* Is there an intermediary node between a and b? */
  5435. for_each_online_node(c) {
  5436. if (node_distance(a, c) < n &&
  5437. node_distance(b, c) < n) {
  5438. sched_numa_topology_type =
  5439. NUMA_GLUELESS_MESH;
  5440. return;
  5441. }
  5442. }
  5443. sched_numa_topology_type = NUMA_BACKPLANE;
  5444. return;
  5445. }
  5446. }
  5447. }
  5448. static void sched_init_numa(void)
  5449. {
  5450. int next_distance, curr_distance = node_distance(0, 0);
  5451. struct sched_domain_topology_level *tl;
  5452. int level = 0;
  5453. int i, j, k;
  5454. sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
  5455. if (!sched_domains_numa_distance)
  5456. return;
  5457. /*
  5458. * O(nr_nodes^2) deduplicating selection sort -- in order to find the
  5459. * unique distances in the node_distance() table.
  5460. *
  5461. * Assumes node_distance(0,j) includes all distances in
  5462. * node_distance(i,j) in order to avoid cubic time.
  5463. */
  5464. next_distance = curr_distance;
  5465. for (i = 0; i < nr_node_ids; i++) {
  5466. for (j = 0; j < nr_node_ids; j++) {
  5467. for (k = 0; k < nr_node_ids; k++) {
  5468. int distance = node_distance(i, k);
  5469. if (distance > curr_distance &&
  5470. (distance < next_distance ||
  5471. next_distance == curr_distance))
  5472. next_distance = distance;
  5473. /*
  5474. * While not a strong assumption it would be nice to know
  5475. * about cases where if node A is connected to B, B is not
  5476. * equally connected to A.
  5477. */
  5478. if (sched_debug() && node_distance(k, i) != distance)
  5479. sched_numa_warn("Node-distance not symmetric");
  5480. if (sched_debug() && i && !find_numa_distance(distance))
  5481. sched_numa_warn("Node-0 not representative");
  5482. }
  5483. if (next_distance != curr_distance) {
  5484. sched_domains_numa_distance[level++] = next_distance;
  5485. sched_domains_numa_levels = level;
  5486. curr_distance = next_distance;
  5487. } else break;
  5488. }
  5489. /*
  5490. * In case of sched_debug() we verify the above assumption.
  5491. */
  5492. if (!sched_debug())
  5493. break;
  5494. }
  5495. if (!level)
  5496. return;
  5497. /*
  5498. * 'level' contains the number of unique distances, excluding the
  5499. * identity distance node_distance(i,i).
  5500. *
  5501. * The sched_domains_numa_distance[] array includes the actual distance
  5502. * numbers.
  5503. */
  5504. /*
  5505. * Here, we should temporarily reset sched_domains_numa_levels to 0.
  5506. * If it fails to allocate memory for array sched_domains_numa_masks[][],
  5507. * the array will contain less then 'level' members. This could be
  5508. * dangerous when we use it to iterate array sched_domains_numa_masks[][]
  5509. * in other functions.
  5510. *
  5511. * We reset it to 'level' at the end of this function.
  5512. */
  5513. sched_domains_numa_levels = 0;
  5514. sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
  5515. if (!sched_domains_numa_masks)
  5516. return;
  5517. /*
  5518. * Now for each level, construct a mask per node which contains all
  5519. * cpus of nodes that are that many hops away from us.
  5520. */
  5521. for (i = 0; i < level; i++) {
  5522. sched_domains_numa_masks[i] =
  5523. kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
  5524. if (!sched_domains_numa_masks[i])
  5525. return;
  5526. for (j = 0; j < nr_node_ids; j++) {
  5527. struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
  5528. if (!mask)
  5529. return;
  5530. sched_domains_numa_masks[i][j] = mask;
  5531. for (k = 0; k < nr_node_ids; k++) {
  5532. if (node_distance(j, k) > sched_domains_numa_distance[i])
  5533. continue;
  5534. cpumask_or(mask, mask, cpumask_of_node(k));
  5535. }
  5536. }
  5537. }
  5538. /* Compute default topology size */
  5539. for (i = 0; sched_domain_topology[i].mask; i++);
  5540. tl = kzalloc((i + level + 1) *
  5541. sizeof(struct sched_domain_topology_level), GFP_KERNEL);
  5542. if (!tl)
  5543. return;
  5544. /*
  5545. * Copy the default topology bits..
  5546. */
  5547. for (i = 0; sched_domain_topology[i].mask; i++)
  5548. tl[i] = sched_domain_topology[i];
  5549. /*
  5550. * .. and append 'j' levels of NUMA goodness.
  5551. */
  5552. for (j = 0; j < level; i++, j++) {
  5553. tl[i] = (struct sched_domain_topology_level){
  5554. .mask = sd_numa_mask,
  5555. .sd_flags = cpu_numa_flags,
  5556. .flags = SDTL_OVERLAP,
  5557. .numa_level = j,
  5558. SD_INIT_NAME(NUMA)
  5559. };
  5560. }
  5561. sched_domain_topology = tl;
  5562. sched_domains_numa_levels = level;
  5563. sched_max_numa_distance = sched_domains_numa_distance[level - 1];
  5564. init_numa_topology_type();
  5565. }
  5566. static void sched_domains_numa_masks_set(int cpu)
  5567. {
  5568. int i, j;
  5569. int node = cpu_to_node(cpu);
  5570. for (i = 0; i < sched_domains_numa_levels; i++) {
  5571. for (j = 0; j < nr_node_ids; j++) {
  5572. if (node_distance(j, node) <= sched_domains_numa_distance[i])
  5573. cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
  5574. }
  5575. }
  5576. }
  5577. static void sched_domains_numa_masks_clear(int cpu)
  5578. {
  5579. int i, j;
  5580. for (i = 0; i < sched_domains_numa_levels; i++) {
  5581. for (j = 0; j < nr_node_ids; j++)
  5582. cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
  5583. }
  5584. }
  5585. /*
  5586. * Update sched_domains_numa_masks[level][node] array when new cpus
  5587. * are onlined.
  5588. */
  5589. static int sched_domains_numa_masks_update(struct notifier_block *nfb,
  5590. unsigned long action,
  5591. void *hcpu)
  5592. {
  5593. int cpu = (long)hcpu;
  5594. switch (action & ~CPU_TASKS_FROZEN) {
  5595. case CPU_ONLINE:
  5596. sched_domains_numa_masks_set(cpu);
  5597. break;
  5598. case CPU_DEAD:
  5599. sched_domains_numa_masks_clear(cpu);
  5600. break;
  5601. default:
  5602. return NOTIFY_DONE;
  5603. }
  5604. return NOTIFY_OK;
  5605. }
  5606. #else
  5607. static inline void sched_init_numa(void)
  5608. {
  5609. }
  5610. static int sched_domains_numa_masks_update(struct notifier_block *nfb,
  5611. unsigned long action,
  5612. void *hcpu)
  5613. {
  5614. return 0;
  5615. }
  5616. #endif /* CONFIG_NUMA */
  5617. static int __sdt_alloc(const struct cpumask *cpu_map)
  5618. {
  5619. struct sched_domain_topology_level *tl;
  5620. int j;
  5621. for_each_sd_topology(tl) {
  5622. struct sd_data *sdd = &tl->data;
  5623. sdd->sd = alloc_percpu(struct sched_domain *);
  5624. if (!sdd->sd)
  5625. return -ENOMEM;
  5626. sdd->sg = alloc_percpu(struct sched_group *);
  5627. if (!sdd->sg)
  5628. return -ENOMEM;
  5629. sdd->sgc = alloc_percpu(struct sched_group_capacity *);
  5630. if (!sdd->sgc)
  5631. return -ENOMEM;
  5632. for_each_cpu(j, cpu_map) {
  5633. struct sched_domain *sd;
  5634. struct sched_group *sg;
  5635. struct sched_group_capacity *sgc;
  5636. sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
  5637. GFP_KERNEL, cpu_to_node(j));
  5638. if (!sd)
  5639. return -ENOMEM;
  5640. *per_cpu_ptr(sdd->sd, j) = sd;
  5641. sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
  5642. GFP_KERNEL, cpu_to_node(j));
  5643. if (!sg)
  5644. return -ENOMEM;
  5645. sg->next = sg;
  5646. *per_cpu_ptr(sdd->sg, j) = sg;
  5647. sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
  5648. GFP_KERNEL, cpu_to_node(j));
  5649. if (!sgc)
  5650. return -ENOMEM;
  5651. *per_cpu_ptr(sdd->sgc, j) = sgc;
  5652. }
  5653. }
  5654. return 0;
  5655. }
  5656. static void __sdt_free(const struct cpumask *cpu_map)
  5657. {
  5658. struct sched_domain_topology_level *tl;
  5659. int j;
  5660. for_each_sd_topology(tl) {
  5661. struct sd_data *sdd = &tl->data;
  5662. for_each_cpu(j, cpu_map) {
  5663. struct sched_domain *sd;
  5664. if (sdd->sd) {
  5665. sd = *per_cpu_ptr(sdd->sd, j);
  5666. if (sd && (sd->flags & SD_OVERLAP))
  5667. free_sched_groups(sd->groups, 0);
  5668. kfree(*per_cpu_ptr(sdd->sd, j));
  5669. }
  5670. if (sdd->sg)
  5671. kfree(*per_cpu_ptr(sdd->sg, j));
  5672. if (sdd->sgc)
  5673. kfree(*per_cpu_ptr(sdd->sgc, j));
  5674. }
  5675. free_percpu(sdd->sd);
  5676. sdd->sd = NULL;
  5677. free_percpu(sdd->sg);
  5678. sdd->sg = NULL;
  5679. free_percpu(sdd->sgc);
  5680. sdd->sgc = NULL;
  5681. }
  5682. }
  5683. struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
  5684. const struct cpumask *cpu_map, struct sched_domain_attr *attr,
  5685. struct sched_domain *child, int cpu)
  5686. {
  5687. struct sched_domain *sd = sd_init(tl, cpu);
  5688. if (!sd)
  5689. return child;
  5690. cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
  5691. if (child) {
  5692. sd->level = child->level + 1;
  5693. sched_domain_level_max = max(sched_domain_level_max, sd->level);
  5694. child->parent = sd;
  5695. sd->child = child;
  5696. if (!cpumask_subset(sched_domain_span(child),
  5697. sched_domain_span(sd))) {
  5698. pr_err("BUG: arch topology borken\n");
  5699. #ifdef CONFIG_SCHED_DEBUG
  5700. pr_err(" the %s domain not a subset of the %s domain\n",
  5701. child->name, sd->name);
  5702. #endif
  5703. /* Fixup, ensure @sd has at least @child cpus. */
  5704. cpumask_or(sched_domain_span(sd),
  5705. sched_domain_span(sd),
  5706. sched_domain_span(child));
  5707. }
  5708. }
  5709. set_domain_attribute(sd, attr);
  5710. return sd;
  5711. }
  5712. /*
  5713. * Build sched domains for a given set of cpus and attach the sched domains
  5714. * to the individual cpus
  5715. */
  5716. static int build_sched_domains(const struct cpumask *cpu_map,
  5717. struct sched_domain_attr *attr)
  5718. {
  5719. enum s_alloc alloc_state;
  5720. struct sched_domain *sd;
  5721. struct s_data d;
  5722. int i, ret = -ENOMEM;
  5723. alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
  5724. if (alloc_state != sa_rootdomain)
  5725. goto error;
  5726. /* Set up domains for cpus specified by the cpu_map. */
  5727. for_each_cpu(i, cpu_map) {
  5728. struct sched_domain_topology_level *tl;
  5729. sd = NULL;
  5730. for_each_sd_topology(tl) {
  5731. sd = build_sched_domain(tl, cpu_map, attr, sd, i);
  5732. if (tl == sched_domain_topology)
  5733. *per_cpu_ptr(d.sd, i) = sd;
  5734. if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
  5735. sd->flags |= SD_OVERLAP;
  5736. if (cpumask_equal(cpu_map, sched_domain_span(sd)))
  5737. break;
  5738. }
  5739. }
  5740. /* Build the groups for the domains */
  5741. for_each_cpu(i, cpu_map) {
  5742. for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
  5743. sd->span_weight = cpumask_weight(sched_domain_span(sd));
  5744. if (sd->flags & SD_OVERLAP) {
  5745. if (build_overlap_sched_groups(sd, i))
  5746. goto error;
  5747. } else {
  5748. if (build_sched_groups(sd, i))
  5749. goto error;
  5750. }
  5751. }
  5752. }
  5753. /* Calculate CPU capacity for physical packages and nodes */
  5754. for (i = nr_cpumask_bits-1; i >= 0; i--) {
  5755. if (!cpumask_test_cpu(i, cpu_map))
  5756. continue;
  5757. for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
  5758. claim_allocations(i, sd);
  5759. init_sched_groups_capacity(i, sd);
  5760. }
  5761. }
  5762. /* Attach the domains */
  5763. rcu_read_lock();
  5764. for_each_cpu(i, cpu_map) {
  5765. sd = *per_cpu_ptr(d.sd, i);
  5766. cpu_attach_domain(sd, d.rd, i);
  5767. }
  5768. rcu_read_unlock();
  5769. ret = 0;
  5770. error:
  5771. __free_domain_allocs(&d, alloc_state, cpu_map);
  5772. return ret;
  5773. }
  5774. static cpumask_var_t *doms_cur; /* current sched domains */
  5775. static int ndoms_cur; /* number of sched domains in 'doms_cur' */
  5776. static struct sched_domain_attr *dattr_cur;
  5777. /* attribues of custom domains in 'doms_cur' */
  5778. /*
  5779. * Special case: If a kmalloc of a doms_cur partition (array of
  5780. * cpumask) fails, then fallback to a single sched domain,
  5781. * as determined by the single cpumask fallback_doms.
  5782. */
  5783. static cpumask_var_t fallback_doms;
  5784. /*
  5785. * arch_update_cpu_topology lets virtualized architectures update the
  5786. * cpu core maps. It is supposed to return 1 if the topology changed
  5787. * or 0 if it stayed the same.
  5788. */
  5789. int __weak arch_update_cpu_topology(void)
  5790. {
  5791. return 0;
  5792. }
  5793. cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
  5794. {
  5795. int i;
  5796. cpumask_var_t *doms;
  5797. doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
  5798. if (!doms)
  5799. return NULL;
  5800. for (i = 0; i < ndoms; i++) {
  5801. if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
  5802. free_sched_domains(doms, i);
  5803. return NULL;
  5804. }
  5805. }
  5806. return doms;
  5807. }
  5808. void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
  5809. {
  5810. unsigned int i;
  5811. for (i = 0; i < ndoms; i++)
  5812. free_cpumask_var(doms[i]);
  5813. kfree(doms);
  5814. }
  5815. /*
  5816. * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  5817. * For now this just excludes isolated cpus, but could be used to
  5818. * exclude other special cases in the future.
  5819. */
  5820. static int init_sched_domains(const struct cpumask *cpu_map)
  5821. {
  5822. int err;
  5823. arch_update_cpu_topology();
  5824. ndoms_cur = 1;
  5825. doms_cur = alloc_sched_domains(ndoms_cur);
  5826. if (!doms_cur)
  5827. doms_cur = &fallback_doms;
  5828. cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
  5829. err = build_sched_domains(doms_cur[0], NULL);
  5830. register_sched_domain_sysctl();
  5831. return err;
  5832. }
  5833. /*
  5834. * Detach sched domains from a group of cpus specified in cpu_map
  5835. * These cpus will now be attached to the NULL domain
  5836. */
  5837. static void detach_destroy_domains(const struct cpumask *cpu_map)
  5838. {
  5839. int i;
  5840. rcu_read_lock();
  5841. for_each_cpu(i, cpu_map)
  5842. cpu_attach_domain(NULL, &def_root_domain, i);
  5843. rcu_read_unlock();
  5844. }
  5845. /* handle null as "default" */
  5846. static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
  5847. struct sched_domain_attr *new, int idx_new)
  5848. {
  5849. struct sched_domain_attr tmp;
  5850. /* fast path */
  5851. if (!new && !cur)
  5852. return 1;
  5853. tmp = SD_ATTR_INIT;
  5854. return !memcmp(cur ? (cur + idx_cur) : &tmp,
  5855. new ? (new + idx_new) : &tmp,
  5856. sizeof(struct sched_domain_attr));
  5857. }
  5858. /*
  5859. * Partition sched domains as specified by the 'ndoms_new'
  5860. * cpumasks in the array doms_new[] of cpumasks. This compares
  5861. * doms_new[] to the current sched domain partitioning, doms_cur[].
  5862. * It destroys each deleted domain and builds each new domain.
  5863. *
  5864. * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
  5865. * The masks don't intersect (don't overlap.) We should setup one
  5866. * sched domain for each mask. CPUs not in any of the cpumasks will
  5867. * not be load balanced. If the same cpumask appears both in the
  5868. * current 'doms_cur' domains and in the new 'doms_new', we can leave
  5869. * it as it is.
  5870. *
  5871. * The passed in 'doms_new' should be allocated using
  5872. * alloc_sched_domains. This routine takes ownership of it and will
  5873. * free_sched_domains it when done with it. If the caller failed the
  5874. * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
  5875. * and partition_sched_domains() will fallback to the single partition
  5876. * 'fallback_doms', it also forces the domains to be rebuilt.
  5877. *
  5878. * If doms_new == NULL it will be replaced with cpu_online_mask.
  5879. * ndoms_new == 0 is a special case for destroying existing domains,
  5880. * and it will not create the default domain.
  5881. *
  5882. * Call with hotplug lock held
  5883. */
  5884. void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
  5885. struct sched_domain_attr *dattr_new)
  5886. {
  5887. int i, j, n;
  5888. int new_topology;
  5889. mutex_lock(&sched_domains_mutex);
  5890. /* always unregister in case we don't destroy any domains */
  5891. unregister_sched_domain_sysctl();
  5892. /* Let architecture update cpu core mappings. */
  5893. new_topology = arch_update_cpu_topology();
  5894. n = doms_new ? ndoms_new : 0;
  5895. /* Destroy deleted domains */
  5896. for (i = 0; i < ndoms_cur; i++) {
  5897. for (j = 0; j < n && !new_topology; j++) {
  5898. if (cpumask_equal(doms_cur[i], doms_new[j])
  5899. && dattrs_equal(dattr_cur, i, dattr_new, j))
  5900. goto match1;
  5901. }
  5902. /* no match - a current sched domain not in new doms_new[] */
  5903. detach_destroy_domains(doms_cur[i]);
  5904. match1:
  5905. ;
  5906. }
  5907. n = ndoms_cur;
  5908. if (doms_new == NULL) {
  5909. n = 0;
  5910. doms_new = &fallback_doms;
  5911. cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
  5912. WARN_ON_ONCE(dattr_new);
  5913. }
  5914. /* Build new domains */
  5915. for (i = 0; i < ndoms_new; i++) {
  5916. for (j = 0; j < n && !new_topology; j++) {
  5917. if (cpumask_equal(doms_new[i], doms_cur[j])
  5918. && dattrs_equal(dattr_new, i, dattr_cur, j))
  5919. goto match2;
  5920. }
  5921. /* no match - add a new doms_new */
  5922. build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
  5923. match2:
  5924. ;
  5925. }
  5926. /* Remember the new sched domains */
  5927. if (doms_cur != &fallback_doms)
  5928. free_sched_domains(doms_cur, ndoms_cur);
  5929. kfree(dattr_cur); /* kfree(NULL) is safe */
  5930. doms_cur = doms_new;
  5931. dattr_cur = dattr_new;
  5932. ndoms_cur = ndoms_new;
  5933. register_sched_domain_sysctl();
  5934. mutex_unlock(&sched_domains_mutex);
  5935. }
  5936. static int num_cpus_frozen; /* used to mark begin/end of suspend/resume */
  5937. /*
  5938. * Update cpusets according to cpu_active mask. If cpusets are
  5939. * disabled, cpuset_update_active_cpus() becomes a simple wrapper
  5940. * around partition_sched_domains().
  5941. *
  5942. * If we come here as part of a suspend/resume, don't touch cpusets because we
  5943. * want to restore it back to its original state upon resume anyway.
  5944. */
  5945. static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
  5946. void *hcpu)
  5947. {
  5948. switch (action) {
  5949. case CPU_ONLINE_FROZEN:
  5950. case CPU_DOWN_FAILED_FROZEN:
  5951. /*
  5952. * num_cpus_frozen tracks how many CPUs are involved in suspend
  5953. * resume sequence. As long as this is not the last online
  5954. * operation in the resume sequence, just build a single sched
  5955. * domain, ignoring cpusets.
  5956. */
  5957. num_cpus_frozen--;
  5958. if (likely(num_cpus_frozen)) {
  5959. partition_sched_domains(1, NULL, NULL);
  5960. break;
  5961. }
  5962. /*
  5963. * This is the last CPU online operation. So fall through and
  5964. * restore the original sched domains by considering the
  5965. * cpuset configurations.
  5966. */
  5967. case CPU_ONLINE:
  5968. cpuset_update_active_cpus(true);
  5969. break;
  5970. default:
  5971. return NOTIFY_DONE;
  5972. }
  5973. return NOTIFY_OK;
  5974. }
  5975. static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
  5976. void *hcpu)
  5977. {
  5978. unsigned long flags;
  5979. long cpu = (long)hcpu;
  5980. struct dl_bw *dl_b;
  5981. bool overflow;
  5982. int cpus;
  5983. switch (action) {
  5984. case CPU_DOWN_PREPARE:
  5985. rcu_read_lock_sched();
  5986. dl_b = dl_bw_of(cpu);
  5987. raw_spin_lock_irqsave(&dl_b->lock, flags);
  5988. cpus = dl_bw_cpus(cpu);
  5989. overflow = __dl_overflow(dl_b, cpus, 0, 0);
  5990. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  5991. rcu_read_unlock_sched();
  5992. if (overflow)
  5993. return notifier_from_errno(-EBUSY);
  5994. cpuset_update_active_cpus(false);
  5995. break;
  5996. case CPU_DOWN_PREPARE_FROZEN:
  5997. num_cpus_frozen++;
  5998. partition_sched_domains(1, NULL, NULL);
  5999. break;
  6000. default:
  6001. return NOTIFY_DONE;
  6002. }
  6003. return NOTIFY_OK;
  6004. }
  6005. void __init sched_init_smp(void)
  6006. {
  6007. cpumask_var_t non_isolated_cpus;
  6008. alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
  6009. alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
  6010. /* nohz_full won't take effect without isolating the cpus. */
  6011. tick_nohz_full_add_cpus_to(cpu_isolated_map);
  6012. sched_init_numa();
  6013. /*
  6014. * There's no userspace yet to cause hotplug operations; hence all the
  6015. * cpu masks are stable and all blatant races in the below code cannot
  6016. * happen.
  6017. */
  6018. mutex_lock(&sched_domains_mutex);
  6019. init_sched_domains(cpu_active_mask);
  6020. cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
  6021. if (cpumask_empty(non_isolated_cpus))
  6022. cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
  6023. mutex_unlock(&sched_domains_mutex);
  6024. hotcpu_notifier(sched_domains_numa_masks_update, CPU_PRI_SCHED_ACTIVE);
  6025. hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
  6026. hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
  6027. init_hrtick();
  6028. /* Move init over to a non-isolated CPU */
  6029. if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
  6030. BUG();
  6031. sched_init_granularity();
  6032. free_cpumask_var(non_isolated_cpus);
  6033. init_sched_rt_class();
  6034. init_sched_dl_class();
  6035. }
  6036. #else
  6037. void __init sched_init_smp(void)
  6038. {
  6039. sched_init_granularity();
  6040. }
  6041. #endif /* CONFIG_SMP */
  6042. int in_sched_functions(unsigned long addr)
  6043. {
  6044. return in_lock_functions(addr) ||
  6045. (addr >= (unsigned long)__sched_text_start
  6046. && addr < (unsigned long)__sched_text_end);
  6047. }
  6048. #ifdef CONFIG_CGROUP_SCHED
  6049. /*
  6050. * Default task group.
  6051. * Every task in system belongs to this group at bootup.
  6052. */
  6053. struct task_group root_task_group;
  6054. LIST_HEAD(task_groups);
  6055. #endif
  6056. DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
  6057. void __init sched_init(void)
  6058. {
  6059. int i, j;
  6060. unsigned long alloc_size = 0, ptr;
  6061. #ifdef CONFIG_FAIR_GROUP_SCHED
  6062. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  6063. #endif
  6064. #ifdef CONFIG_RT_GROUP_SCHED
  6065. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  6066. #endif
  6067. if (alloc_size) {
  6068. ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
  6069. #ifdef CONFIG_FAIR_GROUP_SCHED
  6070. root_task_group.se = (struct sched_entity **)ptr;
  6071. ptr += nr_cpu_ids * sizeof(void **);
  6072. root_task_group.cfs_rq = (struct cfs_rq **)ptr;
  6073. ptr += nr_cpu_ids * sizeof(void **);
  6074. #endif /* CONFIG_FAIR_GROUP_SCHED */
  6075. #ifdef CONFIG_RT_GROUP_SCHED
  6076. root_task_group.rt_se = (struct sched_rt_entity **)ptr;
  6077. ptr += nr_cpu_ids * sizeof(void **);
  6078. root_task_group.rt_rq = (struct rt_rq **)ptr;
  6079. ptr += nr_cpu_ids * sizeof(void **);
  6080. #endif /* CONFIG_RT_GROUP_SCHED */
  6081. }
  6082. #ifdef CONFIG_CPUMASK_OFFSTACK
  6083. for_each_possible_cpu(i) {
  6084. per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
  6085. cpumask_size(), GFP_KERNEL, cpu_to_node(i));
  6086. }
  6087. #endif /* CONFIG_CPUMASK_OFFSTACK */
  6088. init_rt_bandwidth(&def_rt_bandwidth,
  6089. global_rt_period(), global_rt_runtime());
  6090. init_dl_bandwidth(&def_dl_bandwidth,
  6091. global_rt_period(), global_rt_runtime());
  6092. #ifdef CONFIG_SMP
  6093. init_defrootdomain();
  6094. #endif
  6095. #ifdef CONFIG_RT_GROUP_SCHED
  6096. init_rt_bandwidth(&root_task_group.rt_bandwidth,
  6097. global_rt_period(), global_rt_runtime());
  6098. #endif /* CONFIG_RT_GROUP_SCHED */
  6099. #ifdef CONFIG_CGROUP_SCHED
  6100. list_add(&root_task_group.list, &task_groups);
  6101. INIT_LIST_HEAD(&root_task_group.children);
  6102. INIT_LIST_HEAD(&root_task_group.siblings);
  6103. autogroup_init(&init_task);
  6104. #endif /* CONFIG_CGROUP_SCHED */
  6105. for_each_possible_cpu(i) {
  6106. struct rq *rq;
  6107. rq = cpu_rq(i);
  6108. raw_spin_lock_init(&rq->lock);
  6109. rq->nr_running = 0;
  6110. rq->calc_load_active = 0;
  6111. rq->calc_load_update = jiffies + LOAD_FREQ;
  6112. init_cfs_rq(&rq->cfs);
  6113. init_rt_rq(&rq->rt);
  6114. init_dl_rq(&rq->dl);
  6115. #ifdef CONFIG_FAIR_GROUP_SCHED
  6116. root_task_group.shares = ROOT_TASK_GROUP_LOAD;
  6117. INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
  6118. /*
  6119. * How much cpu bandwidth does root_task_group get?
  6120. *
  6121. * In case of task-groups formed thr' the cgroup filesystem, it
  6122. * gets 100% of the cpu resources in the system. This overall
  6123. * system cpu resource is divided among the tasks of
  6124. * root_task_group and its child task-groups in a fair manner,
  6125. * based on each entity's (task or task-group's) weight
  6126. * (se->load.weight).
  6127. *
  6128. * In other words, if root_task_group has 10 tasks of weight
  6129. * 1024) and two child groups A0 and A1 (of weight 1024 each),
  6130. * then A0's share of the cpu resource is:
  6131. *
  6132. * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
  6133. *
  6134. * We achieve this by letting root_task_group's tasks sit
  6135. * directly in rq->cfs (i.e root_task_group->se[] = NULL).
  6136. */
  6137. init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
  6138. init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
  6139. #endif /* CONFIG_FAIR_GROUP_SCHED */
  6140. rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
  6141. #ifdef CONFIG_RT_GROUP_SCHED
  6142. init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
  6143. #endif
  6144. for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
  6145. rq->cpu_load[j] = 0;
  6146. rq->last_load_update_tick = jiffies;
  6147. #ifdef CONFIG_SMP
  6148. rq->sd = NULL;
  6149. rq->rd = NULL;
  6150. rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
  6151. rq->post_schedule = 0;
  6152. rq->active_balance = 0;
  6153. rq->next_balance = jiffies;
  6154. rq->push_cpu = 0;
  6155. rq->cpu = i;
  6156. rq->online = 0;
  6157. rq->idle_stamp = 0;
  6158. rq->avg_idle = 2*sysctl_sched_migration_cost;
  6159. rq->max_idle_balance_cost = sysctl_sched_migration_cost;
  6160. INIT_LIST_HEAD(&rq->cfs_tasks);
  6161. rq_attach_root(rq, &def_root_domain);
  6162. #ifdef CONFIG_NO_HZ_COMMON
  6163. rq->nohz_flags = 0;
  6164. #endif
  6165. #ifdef CONFIG_NO_HZ_FULL
  6166. rq->last_sched_tick = 0;
  6167. #endif
  6168. #endif
  6169. init_rq_hrtick(rq);
  6170. atomic_set(&rq->nr_iowait, 0);
  6171. }
  6172. set_load_weight(&init_task);
  6173. #ifdef CONFIG_PREEMPT_NOTIFIERS
  6174. INIT_HLIST_HEAD(&init_task.preempt_notifiers);
  6175. #endif
  6176. /*
  6177. * The boot idle thread does lazy MMU switching as well:
  6178. */
  6179. atomic_inc(&init_mm.mm_count);
  6180. enter_lazy_tlb(&init_mm, current);
  6181. /*
  6182. * During early bootup we pretend to be a normal task:
  6183. */
  6184. current->sched_class = &fair_sched_class;
  6185. /*
  6186. * Make us the idle thread. Technically, schedule() should not be
  6187. * called from this thread, however somewhere below it might be,
  6188. * but because we are the idle thread, we just pick up running again
  6189. * when this runqueue becomes "idle".
  6190. */
  6191. init_idle(current, smp_processor_id());
  6192. calc_load_update = jiffies + LOAD_FREQ;
  6193. #ifdef CONFIG_SMP
  6194. zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
  6195. /* May be allocated at isolcpus cmdline parse time */
  6196. if (cpu_isolated_map == NULL)
  6197. zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
  6198. idle_thread_set_boot_cpu();
  6199. set_cpu_rq_start_time();
  6200. #endif
  6201. init_sched_fair_class();
  6202. scheduler_running = 1;
  6203. }
  6204. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  6205. static inline int preempt_count_equals(int preempt_offset)
  6206. {
  6207. int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
  6208. return (nested == preempt_offset);
  6209. }
  6210. void __might_sleep(const char *file, int line, int preempt_offset)
  6211. {
  6212. /*
  6213. * Blocking primitives will set (and therefore destroy) current->state,
  6214. * since we will exit with TASK_RUNNING make sure we enter with it,
  6215. * otherwise we will destroy state.
  6216. */
  6217. WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
  6218. "do not call blocking ops when !TASK_RUNNING; "
  6219. "state=%lx set at [<%p>] %pS\n",
  6220. current->state,
  6221. (void *)current->task_state_change,
  6222. (void *)current->task_state_change);
  6223. ___might_sleep(file, line, preempt_offset);
  6224. }
  6225. EXPORT_SYMBOL(__might_sleep);
  6226. void ___might_sleep(const char *file, int line, int preempt_offset)
  6227. {
  6228. static unsigned long prev_jiffy; /* ratelimiting */
  6229. rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
  6230. if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
  6231. !is_idle_task(current)) ||
  6232. system_state != SYSTEM_RUNNING || oops_in_progress)
  6233. return;
  6234. if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  6235. return;
  6236. prev_jiffy = jiffies;
  6237. printk(KERN_ERR
  6238. "BUG: sleeping function called from invalid context at %s:%d\n",
  6239. file, line);
  6240. printk(KERN_ERR
  6241. "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
  6242. in_atomic(), irqs_disabled(),
  6243. current->pid, current->comm);
  6244. if (task_stack_end_corrupted(current))
  6245. printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
  6246. debug_show_held_locks(current);
  6247. if (irqs_disabled())
  6248. print_irqtrace_events(current);
  6249. #ifdef CONFIG_DEBUG_PREEMPT
  6250. if (!preempt_count_equals(preempt_offset)) {
  6251. pr_err("Preemption disabled at:");
  6252. print_ip_sym(current->preempt_disable_ip);
  6253. pr_cont("\n");
  6254. }
  6255. #endif
  6256. dump_stack();
  6257. }
  6258. EXPORT_SYMBOL(___might_sleep);
  6259. #endif
  6260. #ifdef CONFIG_MAGIC_SYSRQ
  6261. static void normalize_task(struct rq *rq, struct task_struct *p)
  6262. {
  6263. const struct sched_class *prev_class = p->sched_class;
  6264. struct sched_attr attr = {
  6265. .sched_policy = SCHED_NORMAL,
  6266. };
  6267. int old_prio = p->prio;
  6268. int queued;
  6269. queued = task_on_rq_queued(p);
  6270. if (queued)
  6271. dequeue_task(rq, p, 0);
  6272. __setscheduler(rq, p, &attr, false);
  6273. if (queued) {
  6274. enqueue_task(rq, p, 0);
  6275. resched_curr(rq);
  6276. }
  6277. check_class_changed(rq, p, prev_class, old_prio);
  6278. }
  6279. void normalize_rt_tasks(void)
  6280. {
  6281. struct task_struct *g, *p;
  6282. unsigned long flags;
  6283. struct rq *rq;
  6284. read_lock(&tasklist_lock);
  6285. for_each_process_thread(g, p) {
  6286. /*
  6287. * Only normalize user tasks:
  6288. */
  6289. if (p->flags & PF_KTHREAD)
  6290. continue;
  6291. p->se.exec_start = 0;
  6292. #ifdef CONFIG_SCHEDSTATS
  6293. p->se.statistics.wait_start = 0;
  6294. p->se.statistics.sleep_start = 0;
  6295. p->se.statistics.block_start = 0;
  6296. #endif
  6297. if (!dl_task(p) && !rt_task(p)) {
  6298. /*
  6299. * Renice negative nice level userspace
  6300. * tasks back to 0:
  6301. */
  6302. if (task_nice(p) < 0)
  6303. set_user_nice(p, 0);
  6304. continue;
  6305. }
  6306. rq = task_rq_lock(p, &flags);
  6307. normalize_task(rq, p);
  6308. task_rq_unlock(rq, p, &flags);
  6309. }
  6310. read_unlock(&tasklist_lock);
  6311. }
  6312. #endif /* CONFIG_MAGIC_SYSRQ */
  6313. #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
  6314. /*
  6315. * These functions are only useful for the IA64 MCA handling, or kdb.
  6316. *
  6317. * They can only be called when the whole system has been
  6318. * stopped - every CPU needs to be quiescent, and no scheduling
  6319. * activity can take place. Using them for anything else would
  6320. * be a serious bug, and as a result, they aren't even visible
  6321. * under any other configuration.
  6322. */
  6323. /**
  6324. * curr_task - return the current task for a given cpu.
  6325. * @cpu: the processor in question.
  6326. *
  6327. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  6328. *
  6329. * Return: The current task for @cpu.
  6330. */
  6331. struct task_struct *curr_task(int cpu)
  6332. {
  6333. return cpu_curr(cpu);
  6334. }
  6335. #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
  6336. #ifdef CONFIG_IA64
  6337. /**
  6338. * set_curr_task - set the current task for a given cpu.
  6339. * @cpu: the processor in question.
  6340. * @p: the task pointer to set.
  6341. *
  6342. * Description: This function must only be used when non-maskable interrupts
  6343. * are serviced on a separate stack. It allows the architecture to switch the
  6344. * notion of the current task on a cpu in a non-blocking manner. This function
  6345. * must be called with all CPU's synchronized, and interrupts disabled, the
  6346. * and caller must save the original value of the current task (see
  6347. * curr_task() above) and restore that value before reenabling interrupts and
  6348. * re-starting the system.
  6349. *
  6350. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  6351. */
  6352. void set_curr_task(int cpu, struct task_struct *p)
  6353. {
  6354. cpu_curr(cpu) = p;
  6355. }
  6356. #endif
  6357. #ifdef CONFIG_CGROUP_SCHED
  6358. /* task_group_lock serializes the addition/removal of task groups */
  6359. static DEFINE_SPINLOCK(task_group_lock);
  6360. static void free_sched_group(struct task_group *tg)
  6361. {
  6362. free_fair_sched_group(tg);
  6363. free_rt_sched_group(tg);
  6364. autogroup_free(tg);
  6365. kfree(tg);
  6366. }
  6367. /* allocate runqueue etc for a new task group */
  6368. struct task_group *sched_create_group(struct task_group *parent)
  6369. {
  6370. struct task_group *tg;
  6371. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  6372. if (!tg)
  6373. return ERR_PTR(-ENOMEM);
  6374. if (!alloc_fair_sched_group(tg, parent))
  6375. goto err;
  6376. if (!alloc_rt_sched_group(tg, parent))
  6377. goto err;
  6378. return tg;
  6379. err:
  6380. free_sched_group(tg);
  6381. return ERR_PTR(-ENOMEM);
  6382. }
  6383. void sched_online_group(struct task_group *tg, struct task_group *parent)
  6384. {
  6385. unsigned long flags;
  6386. spin_lock_irqsave(&task_group_lock, flags);
  6387. list_add_rcu(&tg->list, &task_groups);
  6388. WARN_ON(!parent); /* root should already exist */
  6389. tg->parent = parent;
  6390. INIT_LIST_HEAD(&tg->children);
  6391. list_add_rcu(&tg->siblings, &parent->children);
  6392. spin_unlock_irqrestore(&task_group_lock, flags);
  6393. }
  6394. /* rcu callback to free various structures associated with a task group */
  6395. static void free_sched_group_rcu(struct rcu_head *rhp)
  6396. {
  6397. /* now it should be safe to free those cfs_rqs */
  6398. free_sched_group(container_of(rhp, struct task_group, rcu));
  6399. }
  6400. /* Destroy runqueue etc associated with a task group */
  6401. void sched_destroy_group(struct task_group *tg)
  6402. {
  6403. /* wait for possible concurrent references to cfs_rqs complete */
  6404. call_rcu(&tg->rcu, free_sched_group_rcu);
  6405. }
  6406. void sched_offline_group(struct task_group *tg)
  6407. {
  6408. unsigned long flags;
  6409. int i;
  6410. /* end participation in shares distribution */
  6411. for_each_possible_cpu(i)
  6412. unregister_fair_sched_group(tg, i);
  6413. spin_lock_irqsave(&task_group_lock, flags);
  6414. list_del_rcu(&tg->list);
  6415. list_del_rcu(&tg->siblings);
  6416. spin_unlock_irqrestore(&task_group_lock, flags);
  6417. }
  6418. /* change task's runqueue when it moves between groups.
  6419. * The caller of this function should have put the task in its new group
  6420. * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
  6421. * reflect its new group.
  6422. */
  6423. void sched_move_task(struct task_struct *tsk)
  6424. {
  6425. struct task_group *tg;
  6426. int queued, running;
  6427. unsigned long flags;
  6428. struct rq *rq;
  6429. rq = task_rq_lock(tsk, &flags);
  6430. running = task_current(rq, tsk);
  6431. queued = task_on_rq_queued(tsk);
  6432. if (queued)
  6433. dequeue_task(rq, tsk, 0);
  6434. if (unlikely(running))
  6435. put_prev_task(rq, tsk);
  6436. /*
  6437. * All callers are synchronized by task_rq_lock(); we do not use RCU
  6438. * which is pointless here. Thus, we pass "true" to task_css_check()
  6439. * to prevent lockdep warnings.
  6440. */
  6441. tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
  6442. struct task_group, css);
  6443. tg = autogroup_task_group(tsk, tg);
  6444. tsk->sched_task_group = tg;
  6445. #ifdef CONFIG_FAIR_GROUP_SCHED
  6446. if (tsk->sched_class->task_move_group)
  6447. tsk->sched_class->task_move_group(tsk, queued);
  6448. else
  6449. #endif
  6450. set_task_rq(tsk, task_cpu(tsk));
  6451. if (unlikely(running))
  6452. tsk->sched_class->set_curr_task(rq);
  6453. if (queued)
  6454. enqueue_task(rq, tsk, 0);
  6455. task_rq_unlock(rq, tsk, &flags);
  6456. }
  6457. #endif /* CONFIG_CGROUP_SCHED */
  6458. #ifdef CONFIG_RT_GROUP_SCHED
  6459. /*
  6460. * Ensure that the real time constraints are schedulable.
  6461. */
  6462. static DEFINE_MUTEX(rt_constraints_mutex);
  6463. /* Must be called with tasklist_lock held */
  6464. static inline int tg_has_rt_tasks(struct task_group *tg)
  6465. {
  6466. struct task_struct *g, *p;
  6467. /*
  6468. * Autogroups do not have RT tasks; see autogroup_create().
  6469. */
  6470. if (task_group_is_autogroup(tg))
  6471. return 0;
  6472. for_each_process_thread(g, p) {
  6473. if (rt_task(p) && task_group(p) == tg)
  6474. return 1;
  6475. }
  6476. return 0;
  6477. }
  6478. struct rt_schedulable_data {
  6479. struct task_group *tg;
  6480. u64 rt_period;
  6481. u64 rt_runtime;
  6482. };
  6483. static int tg_rt_schedulable(struct task_group *tg, void *data)
  6484. {
  6485. struct rt_schedulable_data *d = data;
  6486. struct task_group *child;
  6487. unsigned long total, sum = 0;
  6488. u64 period, runtime;
  6489. period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  6490. runtime = tg->rt_bandwidth.rt_runtime;
  6491. if (tg == d->tg) {
  6492. period = d->rt_period;
  6493. runtime = d->rt_runtime;
  6494. }
  6495. /*
  6496. * Cannot have more runtime than the period.
  6497. */
  6498. if (runtime > period && runtime != RUNTIME_INF)
  6499. return -EINVAL;
  6500. /*
  6501. * Ensure we don't starve existing RT tasks.
  6502. */
  6503. if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
  6504. return -EBUSY;
  6505. total = to_ratio(period, runtime);
  6506. /*
  6507. * Nobody can have more than the global setting allows.
  6508. */
  6509. if (total > to_ratio(global_rt_period(), global_rt_runtime()))
  6510. return -EINVAL;
  6511. /*
  6512. * The sum of our children's runtime should not exceed our own.
  6513. */
  6514. list_for_each_entry_rcu(child, &tg->children, siblings) {
  6515. period = ktime_to_ns(child->rt_bandwidth.rt_period);
  6516. runtime = child->rt_bandwidth.rt_runtime;
  6517. if (child == d->tg) {
  6518. period = d->rt_period;
  6519. runtime = d->rt_runtime;
  6520. }
  6521. sum += to_ratio(period, runtime);
  6522. }
  6523. if (sum > total)
  6524. return -EINVAL;
  6525. return 0;
  6526. }
  6527. static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
  6528. {
  6529. int ret;
  6530. struct rt_schedulable_data data = {
  6531. .tg = tg,
  6532. .rt_period = period,
  6533. .rt_runtime = runtime,
  6534. };
  6535. rcu_read_lock();
  6536. ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
  6537. rcu_read_unlock();
  6538. return ret;
  6539. }
  6540. static int tg_set_rt_bandwidth(struct task_group *tg,
  6541. u64 rt_period, u64 rt_runtime)
  6542. {
  6543. int i, err = 0;
  6544. /*
  6545. * Disallowing the root group RT runtime is BAD, it would disallow the
  6546. * kernel creating (and or operating) RT threads.
  6547. */
  6548. if (tg == &root_task_group && rt_runtime == 0)
  6549. return -EINVAL;
  6550. /* No period doesn't make any sense. */
  6551. if (rt_period == 0)
  6552. return -EINVAL;
  6553. mutex_lock(&rt_constraints_mutex);
  6554. read_lock(&tasklist_lock);
  6555. err = __rt_schedulable(tg, rt_period, rt_runtime);
  6556. if (err)
  6557. goto unlock;
  6558. raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  6559. tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
  6560. tg->rt_bandwidth.rt_runtime = rt_runtime;
  6561. for_each_possible_cpu(i) {
  6562. struct rt_rq *rt_rq = tg->rt_rq[i];
  6563. raw_spin_lock(&rt_rq->rt_runtime_lock);
  6564. rt_rq->rt_runtime = rt_runtime;
  6565. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  6566. }
  6567. raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  6568. unlock:
  6569. read_unlock(&tasklist_lock);
  6570. mutex_unlock(&rt_constraints_mutex);
  6571. return err;
  6572. }
  6573. static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
  6574. {
  6575. u64 rt_runtime, rt_period;
  6576. rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  6577. rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
  6578. if (rt_runtime_us < 0)
  6579. rt_runtime = RUNTIME_INF;
  6580. return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
  6581. }
  6582. static long sched_group_rt_runtime(struct task_group *tg)
  6583. {
  6584. u64 rt_runtime_us;
  6585. if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
  6586. return -1;
  6587. rt_runtime_us = tg->rt_bandwidth.rt_runtime;
  6588. do_div(rt_runtime_us, NSEC_PER_USEC);
  6589. return rt_runtime_us;
  6590. }
  6591. static int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
  6592. {
  6593. u64 rt_runtime, rt_period;
  6594. rt_period = rt_period_us * NSEC_PER_USEC;
  6595. rt_runtime = tg->rt_bandwidth.rt_runtime;
  6596. return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
  6597. }
  6598. static long sched_group_rt_period(struct task_group *tg)
  6599. {
  6600. u64 rt_period_us;
  6601. rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
  6602. do_div(rt_period_us, NSEC_PER_USEC);
  6603. return rt_period_us;
  6604. }
  6605. #endif /* CONFIG_RT_GROUP_SCHED */
  6606. #ifdef CONFIG_RT_GROUP_SCHED
  6607. static int sched_rt_global_constraints(void)
  6608. {
  6609. int ret = 0;
  6610. mutex_lock(&rt_constraints_mutex);
  6611. read_lock(&tasklist_lock);
  6612. ret = __rt_schedulable(NULL, 0, 0);
  6613. read_unlock(&tasklist_lock);
  6614. mutex_unlock(&rt_constraints_mutex);
  6615. return ret;
  6616. }
  6617. static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
  6618. {
  6619. /* Don't accept realtime tasks when there is no way for them to run */
  6620. if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
  6621. return 0;
  6622. return 1;
  6623. }
  6624. #else /* !CONFIG_RT_GROUP_SCHED */
  6625. static int sched_rt_global_constraints(void)
  6626. {
  6627. unsigned long flags;
  6628. int i, ret = 0;
  6629. raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
  6630. for_each_possible_cpu(i) {
  6631. struct rt_rq *rt_rq = &cpu_rq(i)->rt;
  6632. raw_spin_lock(&rt_rq->rt_runtime_lock);
  6633. rt_rq->rt_runtime = global_rt_runtime();
  6634. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  6635. }
  6636. raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
  6637. return ret;
  6638. }
  6639. #endif /* CONFIG_RT_GROUP_SCHED */
  6640. static int sched_dl_global_validate(void)
  6641. {
  6642. u64 runtime = global_rt_runtime();
  6643. u64 period = global_rt_period();
  6644. u64 new_bw = to_ratio(period, runtime);
  6645. struct dl_bw *dl_b;
  6646. int cpu, ret = 0;
  6647. unsigned long flags;
  6648. /*
  6649. * Here we want to check the bandwidth not being set to some
  6650. * value smaller than the currently allocated bandwidth in
  6651. * any of the root_domains.
  6652. *
  6653. * FIXME: Cycling on all the CPUs is overdoing, but simpler than
  6654. * cycling on root_domains... Discussion on different/better
  6655. * solutions is welcome!
  6656. */
  6657. for_each_possible_cpu(cpu) {
  6658. rcu_read_lock_sched();
  6659. dl_b = dl_bw_of(cpu);
  6660. raw_spin_lock_irqsave(&dl_b->lock, flags);
  6661. if (new_bw < dl_b->total_bw)
  6662. ret = -EBUSY;
  6663. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  6664. rcu_read_unlock_sched();
  6665. if (ret)
  6666. break;
  6667. }
  6668. return ret;
  6669. }
  6670. static void sched_dl_do_global(void)
  6671. {
  6672. u64 new_bw = -1;
  6673. struct dl_bw *dl_b;
  6674. int cpu;
  6675. unsigned long flags;
  6676. def_dl_bandwidth.dl_period = global_rt_period();
  6677. def_dl_bandwidth.dl_runtime = global_rt_runtime();
  6678. if (global_rt_runtime() != RUNTIME_INF)
  6679. new_bw = to_ratio(global_rt_period(), global_rt_runtime());
  6680. /*
  6681. * FIXME: As above...
  6682. */
  6683. for_each_possible_cpu(cpu) {
  6684. rcu_read_lock_sched();
  6685. dl_b = dl_bw_of(cpu);
  6686. raw_spin_lock_irqsave(&dl_b->lock, flags);
  6687. dl_b->bw = new_bw;
  6688. raw_spin_unlock_irqrestore(&dl_b->lock, flags);
  6689. rcu_read_unlock_sched();
  6690. }
  6691. }
  6692. static int sched_rt_global_validate(void)
  6693. {
  6694. if (sysctl_sched_rt_period <= 0)
  6695. return -EINVAL;
  6696. if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
  6697. (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
  6698. return -EINVAL;
  6699. return 0;
  6700. }
  6701. static void sched_rt_do_global(void)
  6702. {
  6703. def_rt_bandwidth.rt_runtime = global_rt_runtime();
  6704. def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
  6705. }
  6706. int sched_rt_handler(struct ctl_table *table, int write,
  6707. void __user *buffer, size_t *lenp,
  6708. loff_t *ppos)
  6709. {
  6710. int old_period, old_runtime;
  6711. static DEFINE_MUTEX(mutex);
  6712. int ret;
  6713. mutex_lock(&mutex);
  6714. old_period = sysctl_sched_rt_period;
  6715. old_runtime = sysctl_sched_rt_runtime;
  6716. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  6717. if (!ret && write) {
  6718. ret = sched_rt_global_validate();
  6719. if (ret)
  6720. goto undo;
  6721. ret = sched_dl_global_validate();
  6722. if (ret)
  6723. goto undo;
  6724. ret = sched_rt_global_constraints();
  6725. if (ret)
  6726. goto undo;
  6727. sched_rt_do_global();
  6728. sched_dl_do_global();
  6729. }
  6730. if (0) {
  6731. undo:
  6732. sysctl_sched_rt_period = old_period;
  6733. sysctl_sched_rt_runtime = old_runtime;
  6734. }
  6735. mutex_unlock(&mutex);
  6736. return ret;
  6737. }
  6738. int sched_rr_handler(struct ctl_table *table, int write,
  6739. void __user *buffer, size_t *lenp,
  6740. loff_t *ppos)
  6741. {
  6742. int ret;
  6743. static DEFINE_MUTEX(mutex);
  6744. mutex_lock(&mutex);
  6745. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  6746. /* make sure that internally we keep jiffies */
  6747. /* also, writing zero resets timeslice to default */
  6748. if (!ret && write) {
  6749. sched_rr_timeslice = sched_rr_timeslice <= 0 ?
  6750. RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice);
  6751. }
  6752. mutex_unlock(&mutex);
  6753. return ret;
  6754. }
  6755. #ifdef CONFIG_CGROUP_SCHED
  6756. static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
  6757. {
  6758. return css ? container_of(css, struct task_group, css) : NULL;
  6759. }
  6760. static struct cgroup_subsys_state *
  6761. cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
  6762. {
  6763. struct task_group *parent = css_tg(parent_css);
  6764. struct task_group *tg;
  6765. if (!parent) {
  6766. /* This is early initialization for the top cgroup */
  6767. return &root_task_group.css;
  6768. }
  6769. tg = sched_create_group(parent);
  6770. if (IS_ERR(tg))
  6771. return ERR_PTR(-ENOMEM);
  6772. return &tg->css;
  6773. }
  6774. static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
  6775. {
  6776. struct task_group *tg = css_tg(css);
  6777. struct task_group *parent = css_tg(css->parent);
  6778. if (parent)
  6779. sched_online_group(tg, parent);
  6780. return 0;
  6781. }
  6782. static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
  6783. {
  6784. struct task_group *tg = css_tg(css);
  6785. sched_destroy_group(tg);
  6786. }
  6787. static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css)
  6788. {
  6789. struct task_group *tg = css_tg(css);
  6790. sched_offline_group(tg);
  6791. }
  6792. static void cpu_cgroup_fork(struct task_struct *task)
  6793. {
  6794. sched_move_task(task);
  6795. }
  6796. static int cpu_cgroup_can_attach(struct cgroup_subsys_state *css,
  6797. struct cgroup_taskset *tset)
  6798. {
  6799. struct task_struct *task;
  6800. cgroup_taskset_for_each(task, tset) {
  6801. #ifdef CONFIG_RT_GROUP_SCHED
  6802. if (!sched_rt_can_attach(css_tg(css), task))
  6803. return -EINVAL;
  6804. #else
  6805. /* We don't support RT-tasks being in separate groups */
  6806. if (task->sched_class != &fair_sched_class)
  6807. return -EINVAL;
  6808. #endif
  6809. }
  6810. return 0;
  6811. }
  6812. static void cpu_cgroup_attach(struct cgroup_subsys_state *css,
  6813. struct cgroup_taskset *tset)
  6814. {
  6815. struct task_struct *task;
  6816. cgroup_taskset_for_each(task, tset)
  6817. sched_move_task(task);
  6818. }
  6819. static void cpu_cgroup_exit(struct cgroup_subsys_state *css,
  6820. struct cgroup_subsys_state *old_css,
  6821. struct task_struct *task)
  6822. {
  6823. /*
  6824. * cgroup_exit() is called in the copy_process() failure path.
  6825. * Ignore this case since the task hasn't ran yet, this avoids
  6826. * trying to poke a half freed task state from generic code.
  6827. */
  6828. if (!(task->flags & PF_EXITING))
  6829. return;
  6830. sched_move_task(task);
  6831. }
  6832. #ifdef CONFIG_FAIR_GROUP_SCHED
  6833. static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
  6834. struct cftype *cftype, u64 shareval)
  6835. {
  6836. return sched_group_set_shares(css_tg(css), scale_load(shareval));
  6837. }
  6838. static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
  6839. struct cftype *cft)
  6840. {
  6841. struct task_group *tg = css_tg(css);
  6842. return (u64) scale_load_down(tg->shares);
  6843. }
  6844. #ifdef CONFIG_CFS_BANDWIDTH
  6845. static DEFINE_MUTEX(cfs_constraints_mutex);
  6846. const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
  6847. const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
  6848. static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
  6849. static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
  6850. {
  6851. int i, ret = 0, runtime_enabled, runtime_was_enabled;
  6852. struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
  6853. if (tg == &root_task_group)
  6854. return -EINVAL;
  6855. /*
  6856. * Ensure we have at some amount of bandwidth every period. This is
  6857. * to prevent reaching a state of large arrears when throttled via
  6858. * entity_tick() resulting in prolonged exit starvation.
  6859. */
  6860. if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
  6861. return -EINVAL;
  6862. /*
  6863. * Likewise, bound things on the otherside by preventing insane quota
  6864. * periods. This also allows us to normalize in computing quota
  6865. * feasibility.
  6866. */
  6867. if (period > max_cfs_quota_period)
  6868. return -EINVAL;
  6869. /*
  6870. * Prevent race between setting of cfs_rq->runtime_enabled and
  6871. * unthrottle_offline_cfs_rqs().
  6872. */
  6873. get_online_cpus();
  6874. mutex_lock(&cfs_constraints_mutex);
  6875. ret = __cfs_schedulable(tg, period, quota);
  6876. if (ret)
  6877. goto out_unlock;
  6878. runtime_enabled = quota != RUNTIME_INF;
  6879. runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
  6880. /*
  6881. * If we need to toggle cfs_bandwidth_used, off->on must occur
  6882. * before making related changes, and on->off must occur afterwards
  6883. */
  6884. if (runtime_enabled && !runtime_was_enabled)
  6885. cfs_bandwidth_usage_inc();
  6886. raw_spin_lock_irq(&cfs_b->lock);
  6887. cfs_b->period = ns_to_ktime(period);
  6888. cfs_b->quota = quota;
  6889. __refill_cfs_bandwidth_runtime(cfs_b);
  6890. /* restart the period timer (if active) to handle new period expiry */
  6891. if (runtime_enabled)
  6892. start_cfs_bandwidth(cfs_b);
  6893. raw_spin_unlock_irq(&cfs_b->lock);
  6894. for_each_online_cpu(i) {
  6895. struct cfs_rq *cfs_rq = tg->cfs_rq[i];
  6896. struct rq *rq = cfs_rq->rq;
  6897. raw_spin_lock_irq(&rq->lock);
  6898. cfs_rq->runtime_enabled = runtime_enabled;
  6899. cfs_rq->runtime_remaining = 0;
  6900. if (cfs_rq->throttled)
  6901. unthrottle_cfs_rq(cfs_rq);
  6902. raw_spin_unlock_irq(&rq->lock);
  6903. }
  6904. if (runtime_was_enabled && !runtime_enabled)
  6905. cfs_bandwidth_usage_dec();
  6906. out_unlock:
  6907. mutex_unlock(&cfs_constraints_mutex);
  6908. put_online_cpus();
  6909. return ret;
  6910. }
  6911. int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
  6912. {
  6913. u64 quota, period;
  6914. period = ktime_to_ns(tg->cfs_bandwidth.period);
  6915. if (cfs_quota_us < 0)
  6916. quota = RUNTIME_INF;
  6917. else
  6918. quota = (u64)cfs_quota_us * NSEC_PER_USEC;
  6919. return tg_set_cfs_bandwidth(tg, period, quota);
  6920. }
  6921. long tg_get_cfs_quota(struct task_group *tg)
  6922. {
  6923. u64 quota_us;
  6924. if (tg->cfs_bandwidth.quota == RUNTIME_INF)
  6925. return -1;
  6926. quota_us = tg->cfs_bandwidth.quota;
  6927. do_div(quota_us, NSEC_PER_USEC);
  6928. return quota_us;
  6929. }
  6930. int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
  6931. {
  6932. u64 quota, period;
  6933. period = (u64)cfs_period_us * NSEC_PER_USEC;
  6934. quota = tg->cfs_bandwidth.quota;
  6935. return tg_set_cfs_bandwidth(tg, period, quota);
  6936. }
  6937. long tg_get_cfs_period(struct task_group *tg)
  6938. {
  6939. u64 cfs_period_us;
  6940. cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
  6941. do_div(cfs_period_us, NSEC_PER_USEC);
  6942. return cfs_period_us;
  6943. }
  6944. static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
  6945. struct cftype *cft)
  6946. {
  6947. return tg_get_cfs_quota(css_tg(css));
  6948. }
  6949. static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
  6950. struct cftype *cftype, s64 cfs_quota_us)
  6951. {
  6952. return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
  6953. }
  6954. static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
  6955. struct cftype *cft)
  6956. {
  6957. return tg_get_cfs_period(css_tg(css));
  6958. }
  6959. static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
  6960. struct cftype *cftype, u64 cfs_period_us)
  6961. {
  6962. return tg_set_cfs_period(css_tg(css), cfs_period_us);
  6963. }
  6964. struct cfs_schedulable_data {
  6965. struct task_group *tg;
  6966. u64 period, quota;
  6967. };
  6968. /*
  6969. * normalize group quota/period to be quota/max_period
  6970. * note: units are usecs
  6971. */
  6972. static u64 normalize_cfs_quota(struct task_group *tg,
  6973. struct cfs_schedulable_data *d)
  6974. {
  6975. u64 quota, period;
  6976. if (tg == d->tg) {
  6977. period = d->period;
  6978. quota = d->quota;
  6979. } else {
  6980. period = tg_get_cfs_period(tg);
  6981. quota = tg_get_cfs_quota(tg);
  6982. }
  6983. /* note: these should typically be equivalent */
  6984. if (quota == RUNTIME_INF || quota == -1)
  6985. return RUNTIME_INF;
  6986. return to_ratio(period, quota);
  6987. }
  6988. static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
  6989. {
  6990. struct cfs_schedulable_data *d = data;
  6991. struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
  6992. s64 quota = 0, parent_quota = -1;
  6993. if (!tg->parent) {
  6994. quota = RUNTIME_INF;
  6995. } else {
  6996. struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
  6997. quota = normalize_cfs_quota(tg, d);
  6998. parent_quota = parent_b->hierarchical_quota;
  6999. /*
  7000. * ensure max(child_quota) <= parent_quota, inherit when no
  7001. * limit is set
  7002. */
  7003. if (quota == RUNTIME_INF)
  7004. quota = parent_quota;
  7005. else if (parent_quota != RUNTIME_INF && quota > parent_quota)
  7006. return -EINVAL;
  7007. }
  7008. cfs_b->hierarchical_quota = quota;
  7009. return 0;
  7010. }
  7011. static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
  7012. {
  7013. int ret;
  7014. struct cfs_schedulable_data data = {
  7015. .tg = tg,
  7016. .period = period,
  7017. .quota = quota,
  7018. };
  7019. if (quota != RUNTIME_INF) {
  7020. do_div(data.period, NSEC_PER_USEC);
  7021. do_div(data.quota, NSEC_PER_USEC);
  7022. }
  7023. rcu_read_lock();
  7024. ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
  7025. rcu_read_unlock();
  7026. return ret;
  7027. }
  7028. static int cpu_stats_show(struct seq_file *sf, void *v)
  7029. {
  7030. struct task_group *tg = css_tg(seq_css(sf));
  7031. struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
  7032. seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
  7033. seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
  7034. seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
  7035. return 0;
  7036. }
  7037. #endif /* CONFIG_CFS_BANDWIDTH */
  7038. #endif /* CONFIG_FAIR_GROUP_SCHED */
  7039. #ifdef CONFIG_RT_GROUP_SCHED
  7040. static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
  7041. struct cftype *cft, s64 val)
  7042. {
  7043. return sched_group_set_rt_runtime(css_tg(css), val);
  7044. }
  7045. static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
  7046. struct cftype *cft)
  7047. {
  7048. return sched_group_rt_runtime(css_tg(css));
  7049. }
  7050. static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
  7051. struct cftype *cftype, u64 rt_period_us)
  7052. {
  7053. return sched_group_set_rt_period(css_tg(css), rt_period_us);
  7054. }
  7055. static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
  7056. struct cftype *cft)
  7057. {
  7058. return sched_group_rt_period(css_tg(css));
  7059. }
  7060. #endif /* CONFIG_RT_GROUP_SCHED */
  7061. static struct cftype cpu_files[] = {
  7062. #ifdef CONFIG_FAIR_GROUP_SCHED
  7063. {
  7064. .name = "shares",
  7065. .read_u64 = cpu_shares_read_u64,
  7066. .write_u64 = cpu_shares_write_u64,
  7067. },
  7068. #endif
  7069. #ifdef CONFIG_CFS_BANDWIDTH
  7070. {
  7071. .name = "cfs_quota_us",
  7072. .read_s64 = cpu_cfs_quota_read_s64,
  7073. .write_s64 = cpu_cfs_quota_write_s64,
  7074. },
  7075. {
  7076. .name = "cfs_period_us",
  7077. .read_u64 = cpu_cfs_period_read_u64,
  7078. .write_u64 = cpu_cfs_period_write_u64,
  7079. },
  7080. {
  7081. .name = "stat",
  7082. .seq_show = cpu_stats_show,
  7083. },
  7084. #endif
  7085. #ifdef CONFIG_RT_GROUP_SCHED
  7086. {
  7087. .name = "rt_runtime_us",
  7088. .read_s64 = cpu_rt_runtime_read,
  7089. .write_s64 = cpu_rt_runtime_write,
  7090. },
  7091. {
  7092. .name = "rt_period_us",
  7093. .read_u64 = cpu_rt_period_read_uint,
  7094. .write_u64 = cpu_rt_period_write_uint,
  7095. },
  7096. #endif
  7097. { } /* terminate */
  7098. };
  7099. struct cgroup_subsys cpu_cgrp_subsys = {
  7100. .css_alloc = cpu_cgroup_css_alloc,
  7101. .css_free = cpu_cgroup_css_free,
  7102. .css_online = cpu_cgroup_css_online,
  7103. .css_offline = cpu_cgroup_css_offline,
  7104. .fork = cpu_cgroup_fork,
  7105. .can_attach = cpu_cgroup_can_attach,
  7106. .attach = cpu_cgroup_attach,
  7107. .exit = cpu_cgroup_exit,
  7108. .legacy_cftypes = cpu_files,
  7109. .early_init = 1,
  7110. };
  7111. #endif /* CONFIG_CGROUP_SCHED */
  7112. void dump_cpu_task(int cpu)
  7113. {
  7114. pr_info("Task dump for CPU %d:\n", cpu);
  7115. sched_show_task(cpu_curr(cpu));
  7116. }