123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796 |
- /*
- * kernel/sched/core.c
- *
- * Core kernel scheduler code and related syscalls
- *
- * Copyright (C) 1991-2002 Linus Torvalds
- */
- #include <linux/sched.h>
- #include <linux/sched/clock.h>
- #include <linux/sched/energy.h>
- #include <uapi/linux/sched/types.h>
- #include <linux/sched/loadavg.h>
- #include <linux/sched/hotplug.h>
- #include <linux/wait_bit.h>
- #include <linux/cpuset.h>
- #include <linux/delayacct.h>
- #include <linux/init_task.h>
- #include <linux/context_tracking.h>
- #include <linux/rcupdate_wait.h>
- #include <linux/blkdev.h>
- #include <linux/kcov.h>
- #include <linux/kprobes.h>
- #include <linux/mmu_context.h>
- #include <linux/module.h>
- #include <linux/nmi.h>
- #include <linux/prefetch.h>
- #include <linux/profile.h>
- #include <linux/scs.h>
- #include <linux/security.h>
- #include <linux/syscalls.h>
- #include <linux/hrtimer.h>
- #include <linux/smp.h>
- #include <linux/timer.h>
- #include <asm/switch_to.h>
- #include <asm/tlb.h>
- #ifdef CONFIG_PARAVIRT
- #include <asm/paravirt.h>
- #endif
- #include "sched.h"
- #include "../workqueue_internal.h"
- #include "../smpboot.h"
- #include <mt-plat/perf_tracker.h>
- #define CREATE_TRACE_POINTS
- #include <trace/events/sched.h>
- #include "walt.h"
- #include "mtk_mcdi_api.h"
- #if defined(CONFIG_MTK_GIC_V3_EXT)
- #include <linux/irqchip/mtk-gic-extend.h>
- #endif
- #include <mt-plat/l3cc_common.h>
- #ifdef CONFIG_MTK_TASK_TURBO
- #include <mt-plat/turbo_common.h>
- #endif
- #ifdef CONFIG_MEDIATEK_SOLUTION
- #include "mtk_secure_api.h"
- #include <linux/arm-smccc.h>
- #define GIC_ISO_CODE (1 << 0)
- #endif
- #ifdef CONFIG_MTK_QOS_FRAMEWORK
- #include <mt-plat/mtk_qos_prefetch_common.h>
- #endif /* CONFIG_MTK_QOS_FRAMEWORK */
- DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
- DEFINE_MUTEX(sched_isolation_mutex);
- struct cpumask cpu_all_masks;
- struct cpumask available_cpus;
- enum iso_prio_t iso_prio = ISO_UNSET;
- /*
- * Debugging: various feature bits
- */
- #define SCHED_FEAT(name, enabled) \
- (1UL << __SCHED_FEAT_##name) * enabled |
- const_debug unsigned int sysctl_sched_features =
- #include "features.h"
- 0;
- #undef SCHED_FEAT
- /*
- * Number of tasks to iterate in a single balance run.
- * Limited because this is done with IRQs disabled.
- */
- const_debug unsigned int sysctl_sched_nr_migrate = 32;
- /*
- * period over which we average the RT time consumption, measured
- * in ms.
- *
- * default: 1s
- */
- const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
- /*
- * period over which we measure -rt task CPU usage in us.
- * default: 1s
- */
- unsigned int sysctl_sched_rt_period = 1000000;
- __read_mostly int scheduler_running;
- /*
- * part of the period that we allow rt tasks to run in us.
- * default: 0.95s
- */
- int sysctl_sched_rt_runtime = 950000;
- /* CPUs with isolated domains */
- cpumask_var_t cpu_isolated_map;
- /*
- * __task_rq_lock - lock the rq @p resides on.
- */
- struct rq *__task_rq_lock(struct task_struct *p, struct rq_flags *rf)
- __acquires(rq->lock)
- {
- struct rq *rq;
- lockdep_assert_held(&p->pi_lock);
- for (;;) {
- rq = task_rq(p);
- raw_spin_lock(&rq->lock);
- if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
- rq_pin_lock(rq, rf);
- return rq;
- }
- raw_spin_unlock(&rq->lock);
- while (unlikely(task_on_rq_migrating(p)))
- cpu_relax();
- }
- }
- /*
- * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
- */
- struct rq *task_rq_lock(struct task_struct *p, struct rq_flags *rf)
- __acquires(p->pi_lock)
- __acquires(rq->lock)
- {
- struct rq *rq;
- for (;;) {
- raw_spin_lock_irqsave(&p->pi_lock, rf->flags);
- rq = task_rq(p);
- raw_spin_lock(&rq->lock);
- /*
- * move_queued_task() task_rq_lock()
- *
- * ACQUIRE (rq->lock)
- * [S] ->on_rq = MIGRATING [L] rq = task_rq()
- * WMB (__set_task_cpu()) ACQUIRE (rq->lock);
- * [S] ->cpu = new_cpu [L] task_rq()
- * [L] ->on_rq
- * RELEASE (rq->lock)
- *
- * If we observe the old cpu in task_rq_lock, the acquire of
- * the old rq->lock will fully serialize against the stores.
- *
- * If we observe the new CPU in task_rq_lock, the acquire will
- * pair with the WMB to ensure we must then also see migrating.
- */
- if (likely(rq == task_rq(p) && !task_on_rq_migrating(p))) {
- rq_pin_lock(rq, rf);
- return rq;
- }
- raw_spin_unlock(&rq->lock);
- raw_spin_unlock_irqrestore(&p->pi_lock, rf->flags);
- while (unlikely(task_on_rq_migrating(p)))
- cpu_relax();
- }
- }
- /*
- * RQ-clock updating methods:
- */
- static void update_rq_clock_task(struct rq *rq, s64 delta)
- {
- /*
- * In theory, the compile should just see 0 here, and optimize out the call
- * to sched_rt_avg_update. But I don't trust it...
- */
- #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
- s64 steal = 0, irq_delta = 0;
- #endif
- #ifdef CONFIG_IRQ_TIME_ACCOUNTING
- irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
- /*
- * Since irq_time is only updated on {soft,}irq_exit, we might run into
- * this case when a previous update_rq_clock() happened inside a
- * {soft,}irq region.
- *
- * When this happens, we stop ->clock_task and only update the
- * prev_irq_time stamp to account for the part that fit, so that a next
- * update will consume the rest. This ensures ->clock_task is
- * monotonic.
- *
- * It does however cause some slight miss-attribution of {soft,}irq
- * time, a more accurate solution would be to update the irq_time using
- * the current rq->clock timestamp, except that would require using
- * atomic ops.
- */
- if (irq_delta > delta)
- irq_delta = delta;
- rq->prev_irq_time += irq_delta;
- delta -= irq_delta;
- #endif
- #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
- if (static_key_false((¶virt_steal_rq_enabled))) {
- steal = paravirt_steal_clock(cpu_of(rq));
- steal -= rq->prev_steal_time_rq;
- if (unlikely(steal > delta))
- steal = delta;
- rq->prev_steal_time_rq += steal;
- delta -= steal;
- }
- #endif
- rq->clock_task += delta;
- #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
- if ((irq_delta + steal) && sched_feat(NONTASK_CAPACITY))
- sched_rt_avg_update(rq, irq_delta + steal);
- #endif
- }
- void update_rq_clock(struct rq *rq)
- {
- s64 delta;
- lockdep_assert_held(&rq->lock);
- if (rq->clock_update_flags & RQCF_ACT_SKIP)
- return;
- #ifdef CONFIG_SCHED_DEBUG
- if (sched_feat(WARN_DOUBLE_CLOCK))
- SCHED_WARN_ON(rq->clock_update_flags & RQCF_UPDATED);
- rq->clock_update_flags |= RQCF_UPDATED;
- #endif
- delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
- if (delta < 0)
- return;
- rq->clock += delta;
- update_rq_clock_task(rq, delta);
- }
- #ifdef CONFIG_SCHED_HRTICK
- /*
- * Use HR-timers to deliver accurate preemption points.
- */
- static void hrtick_clear(struct rq *rq)
- {
- if (hrtimer_active(&rq->hrtick_timer))
- hrtimer_cancel(&rq->hrtick_timer);
- }
- /*
- * High-resolution timer tick.
- * Runs from hardirq context with interrupts disabled.
- */
- static enum hrtimer_restart hrtick(struct hrtimer *timer)
- {
- struct rq *rq = container_of(timer, struct rq, hrtick_timer);
- struct rq_flags rf;
- WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
- rq_lock(rq, &rf);
- update_rq_clock(rq);
- rq->curr->sched_class->task_tick(rq, rq->curr, 1);
- rq_unlock(rq, &rf);
- return HRTIMER_NORESTART;
- }
- #ifdef CONFIG_SMP
- static void __hrtick_restart(struct rq *rq)
- {
- struct hrtimer *timer = &rq->hrtick_timer;
- hrtimer_start_expires(timer, HRTIMER_MODE_ABS_PINNED);
- }
- /*
- * called from hardirq (IPI) context
- */
- static void __hrtick_start(void *arg)
- {
- struct rq *rq = arg;
- struct rq_flags rf;
- rq_lock(rq, &rf);
- __hrtick_restart(rq);
- rq->hrtick_csd_pending = 0;
- rq_unlock(rq, &rf);
- }
- /*
- * Called to set the hrtick timer state.
- *
- * called with rq->lock held and irqs disabled
- */
- void hrtick_start(struct rq *rq, u64 delay)
- {
- struct hrtimer *timer = &rq->hrtick_timer;
- ktime_t time;
- s64 delta;
- /*
- * Don't schedule slices shorter than 10000ns, that just
- * doesn't make sense and can cause timer DoS.
- */
- delta = max_t(s64, delay, 10000LL);
- time = ktime_add_ns(timer->base->get_time(), delta);
- hrtimer_set_expires(timer, time);
- if (rq == this_rq()) {
- __hrtick_restart(rq);
- } else if (!rq->hrtick_csd_pending) {
- smp_call_function_single_async(cpu_of(rq), &rq->hrtick_csd);
- rq->hrtick_csd_pending = 1;
- }
- }
- #else
- /*
- * Called to set the hrtick timer state.
- *
- * called with rq->lock held and irqs disabled
- */
- void hrtick_start(struct rq *rq, u64 delay)
- {
- /*
- * Don't schedule slices shorter than 10000ns, that just
- * doesn't make sense. Rely on vruntime for fairness.
- */
- delay = max_t(u64, delay, 10000LL);
- hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
- HRTIMER_MODE_REL_PINNED);
- }
- #endif /* CONFIG_SMP */
- static void init_rq_hrtick(struct rq *rq)
- {
- #ifdef CONFIG_SMP
- rq->hrtick_csd_pending = 0;
- rq->hrtick_csd.flags = 0;
- rq->hrtick_csd.func = __hrtick_start;
- rq->hrtick_csd.info = rq;
- #endif
- hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
- rq->hrtick_timer.function = hrtick;
- }
- #else /* CONFIG_SCHED_HRTICK */
- static inline void hrtick_clear(struct rq *rq)
- {
- }
- static inline void init_rq_hrtick(struct rq *rq)
- {
- }
- #endif /* CONFIG_SCHED_HRTICK */
- /*
- * cmpxchg based fetch_or, macro so it works for different integer types
- */
- #define fetch_or(ptr, mask) \
- ({ \
- typeof(ptr) _ptr = (ptr); \
- typeof(mask) _mask = (mask); \
- typeof(*_ptr) _old, _val = *_ptr; \
- \
- for (;;) { \
- _old = cmpxchg(_ptr, _val, _val | _mask); \
- if (_old == _val) \
- break; \
- _val = _old; \
- } \
- _old; \
- })
- #if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
- /*
- * Atomically set TIF_NEED_RESCHED and test for TIF_POLLING_NRFLAG,
- * this avoids any races wrt polling state changes and thereby avoids
- * spurious IPIs.
- */
- static bool set_nr_and_not_polling(struct task_struct *p)
- {
- struct thread_info *ti = task_thread_info(p);
- return !(fetch_or(&ti->flags, _TIF_NEED_RESCHED) & _TIF_POLLING_NRFLAG);
- }
- /*
- * Atomically set TIF_NEED_RESCHED if TIF_POLLING_NRFLAG is set.
- *
- * If this returns true, then the idle task promises to call
- * sched_ttwu_pending() and reschedule soon.
- */
- static bool set_nr_if_polling(struct task_struct *p)
- {
- struct thread_info *ti = task_thread_info(p);
- typeof(ti->flags) old, val = READ_ONCE(ti->flags);
- for (;;) {
- if (!(val & _TIF_POLLING_NRFLAG))
- return false;
- if (val & _TIF_NEED_RESCHED)
- return true;
- old = cmpxchg(&ti->flags, val, val | _TIF_NEED_RESCHED);
- if (old == val)
- break;
- val = old;
- }
- return true;
- }
- #else
- static bool set_nr_and_not_polling(struct task_struct *p)
- {
- set_tsk_need_resched(p);
- return true;
- }
- #ifdef CONFIG_SMP
- static bool set_nr_if_polling(struct task_struct *p)
- {
- return false;
- }
- #endif
- #endif
- void wake_q_add(struct wake_q_head *head, struct task_struct *task)
- {
- struct wake_q_node *node = &task->wake_q;
- /*
- * Atomically grab the task, if ->wake_q is !nil already it means
- * its already queued (either by us or someone else) and will get the
- * wakeup due to that.
- *
- * In order to ensure that a pending wakeup will observe our pending
- * state, even in the failed case, an explicit smp_mb() must be used.
- */
- smp_mb__before_atomic();
- if (cmpxchg_relaxed(&node->next, NULL, WAKE_Q_TAIL))
- return;
- head->count++;
- get_task_struct(task);
- /*
- * The head is context local, there can be no concurrency.
- */
- *head->lastp = node;
- head->lastp = &node->next;
- }
- static int
- try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags,
- int sibling_count_hint);
- void wake_up_q(struct wake_q_head *head)
- {
- struct wake_q_node *node = head->first;
- while (node != WAKE_Q_TAIL) {
- struct task_struct *task;
- task = container_of(node, struct task_struct, wake_q);
- BUG_ON(!task);
- /* Task can safely be re-inserted now: */
- node = node->next;
- task->wake_q.next = NULL;
- /*
- * try_to_wake_up() implies a wmb() to pair with the queueing
- * in wake_q_add() so as not to miss wakeups.
- */
- try_to_wake_up(task, TASK_NORMAL, 0, head->count);
- put_task_struct(task);
- }
- }
- /*
- * resched_curr - mark rq's current task 'to be rescheduled now'.
- *
- * On UP this means the setting of the need_resched flag, on SMP it
- * might also involve a cross-CPU call to trigger the scheduler on
- * the target CPU.
- */
- void resched_curr(struct rq *rq)
- {
- struct task_struct *curr = rq->curr;
- int cpu;
- lockdep_assert_held(&rq->lock);
- if (test_tsk_need_resched(curr))
- return;
- cpu = cpu_of(rq);
- if (cpu == smp_processor_id()) {
- set_tsk_need_resched(curr);
- set_preempt_need_resched();
- return;
- }
- if (set_nr_and_not_polling(curr))
- smp_send_reschedule(cpu);
- else
- trace_sched_wake_idle_without_ipi(cpu);
- }
- void resched_cpu(int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- unsigned long flags;
- raw_spin_lock_irqsave(&rq->lock, flags);
- if (cpu_online(cpu) || cpu == smp_processor_id())
- resched_curr(rq);
- raw_spin_unlock_irqrestore(&rq->lock, flags);
- }
- #ifdef CONFIG_SMP
- #ifdef CONFIG_NO_HZ_COMMON
- /*
- * In the semi idle case, use the nearest busy CPU for migrating timers
- * from an idle CPU. This is good for power-savings.
- *
- * We don't do similar optimization for completely idle system, as
- * selecting an idle CPU will add more delays to the timers than intended
- * (as that CPU's timer base may not be uptodate wrt jiffies etc).
- */
- int get_nohz_timer_target(void)
- {
- int i, cpu = smp_processor_id();
- struct sched_domain *sd;
- if (!idle_cpu(cpu) && is_housekeeping_cpu(cpu))
- return cpu;
- rcu_read_lock();
- for_each_domain(cpu, sd) {
- for_each_cpu(i, sched_domain_span(sd)) {
- if (cpu == i)
- continue;
- if (!idle_cpu(i) && is_housekeeping_cpu(i)) {
- cpu = i;
- goto unlock;
- }
- }
- }
- if (!is_housekeeping_cpu(cpu))
- cpu = housekeeping_any_cpu();
- unlock:
- rcu_read_unlock();
- return cpu;
- }
- /*
- * When add_timer_on() enqueues a timer into the timer wheel of an
- * idle CPU then this timer might expire before the next timer event
- * which is scheduled to wake up that CPU. In case of a completely
- * idle system the next event might even be infinite time into the
- * future. wake_up_idle_cpu() ensures that the CPU is woken up and
- * leaves the inner idle loop so the newly added timer is taken into
- * account when the CPU goes back to idle and evaluates the timer
- * wheel for the next timer event.
- */
- static void wake_up_idle_cpu(int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- if (cpu == smp_processor_id())
- return;
- if (set_nr_and_not_polling(rq->idle))
- smp_send_reschedule(cpu);
- else
- trace_sched_wake_idle_without_ipi(cpu);
- }
- static bool wake_up_full_nohz_cpu(int cpu)
- {
- /*
- * We just need the target to call irq_exit() and re-evaluate
- * the next tick. The nohz full kick at least implies that.
- * If needed we can still optimize that later with an
- * empty IRQ.
- */
- if (cpu_is_offline(cpu))
- return true; /* Don't try to wake offline CPUs. */
- if (tick_nohz_full_cpu(cpu)) {
- if (cpu != smp_processor_id() ||
- tick_nohz_tick_stopped())
- tick_nohz_full_kick_cpu(cpu);
- return true;
- }
- return false;
- }
- /*
- * Wake up the specified CPU. If the CPU is going offline, it is the
- * caller's responsibility to deal with the lost wakeup, for example,
- * by hooking into the CPU_DEAD notifier like timers and hrtimers do.
- */
- void wake_up_nohz_cpu(int cpu)
- {
- if (!wake_up_full_nohz_cpu(cpu))
- wake_up_idle_cpu(cpu);
- }
- static inline bool got_nohz_idle_kick(void)
- {
- int cpu = smp_processor_id();
- if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)))
- return false;
- if (idle_cpu(cpu) && !need_resched())
- return true;
- /*
- * We can't run Idle Load Balance on this CPU for this time so we
- * cancel it and clear NOHZ_BALANCE_KICK
- */
- clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu));
- return false;
- }
- #else /* CONFIG_NO_HZ_COMMON */
- static inline bool got_nohz_idle_kick(void)
- {
- return false;
- }
- #endif /* CONFIG_NO_HZ_COMMON */
- #ifdef CONFIG_NO_HZ_FULL
- bool sched_can_stop_tick(struct rq *rq)
- {
- int fifo_nr_running;
- /* Deadline tasks, even if single, need the tick */
- if (rq->dl.dl_nr_running)
- return false;
- /*
- * If there are more than one RR tasks, we need the tick to effect the
- * actual RR behaviour.
- */
- if (rq->rt.rr_nr_running) {
- if (rq->rt.rr_nr_running == 1)
- return true;
- else
- return false;
- }
- /*
- * If there's no RR tasks, but FIFO tasks, we can skip the tick, no
- * forced preemption between FIFO tasks.
- */
- fifo_nr_running = rq->rt.rt_nr_running - rq->rt.rr_nr_running;
- if (fifo_nr_running)
- return true;
- /*
- * If there are no DL,RR/FIFO tasks, there must only be CFS tasks left;
- * if there's more than one we need the tick for involuntary
- * preemption.
- */
- if (rq->nr_running > 1)
- return false;
- return true;
- }
- #endif /* CONFIG_NO_HZ_FULL */
- void sched_avg_update(struct rq *rq)
- {
- s64 period = sched_avg_period();
- while ((s64)(rq_clock(rq) - rq->age_stamp) > period) {
- /*
- * Inline assembly required to prevent the compiler
- * optimising this loop into a divmod call.
- * See __iter_div_u64_rem() for another example of this.
- */
- asm("" : "+rm" (rq->age_stamp));
- rq->age_stamp += period;
- rq->rt_avg /= 2;
- }
- }
- #endif /* CONFIG_SMP */
- #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
- (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
- /*
- * Iterate task_group tree rooted at *from, calling @down when first entering a
- * node and @up when leaving it for the final time.
- *
- * Caller must hold rcu_lock or sufficient equivalent.
- */
- int walk_tg_tree_from(struct task_group *from,
- tg_visitor down, tg_visitor up, void *data)
- {
- struct task_group *parent, *child;
- int ret;
- parent = from;
- down:
- ret = (*down)(parent, data);
- if (ret)
- goto out;
- list_for_each_entry_rcu(child, &parent->children, siblings) {
- parent = child;
- goto down;
- up:
- continue;
- }
- ret = (*up)(parent, data);
- if (ret || parent == from)
- goto out;
- child = parent;
- parent = parent->parent;
- if (parent)
- goto up;
- out:
- return ret;
- }
- int tg_nop(struct task_group *tg, void *data)
- {
- return 0;
- }
- #endif
- static void set_load_weight(struct task_struct *p)
- {
- int prio = p->static_prio - MAX_RT_PRIO;
- struct load_weight *load = &p->se.load;
- /*
- * SCHED_IDLE tasks get minimal weight:
- */
- if (idle_policy(p->policy)) {
- load->weight = scale_load(WEIGHT_IDLEPRIO);
- load->inv_weight = WMULT_IDLEPRIO;
- return;
- }
- load->weight = scale_load(sched_prio_to_weight[prio]);
- load->inv_weight = sched_prio_to_wmult[prio];
- }
- #ifdef CONFIG_UCLAMP_MAP_OPP
- #include <linux/sort.h>
- #include <linux/cpufreq.h>
- int total_opp_count;
- int opp_capacity_tbl_ready;
- unsigned int *opp_capacity_tbl;
- static int cap_compare(const void *lhs, const void *rhs)
- {
- unsigned int lhs_cap = *(const unsigned int *)(lhs);
- unsigned int rhs_cap = *(const unsigned int *)(rhs);
- if (lhs_cap < rhs_cap)
- return -1;
- if (lhs_cap > rhs_cap)
- return 1;
- return 0;
- }
- static int system_opp_count(void)
- {
- int cpu, cid, prev_cid = -1;
- int count = 0;
- struct sched_domain *sd;
- struct sched_group *sg;
- const struct sched_group_energy *sge;
- rcu_read_lock();
- for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
- cid = arch_get_cluster_id(cpu);
- if (cid == prev_cid)
- continue;
- sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd);
- if (sd) {
- sg = sd->groups;
- sge = sg->sge;
- } else {
- rcu_read_unlock();
- pr_info("sched: %s no sd\n", __func__);
- return -1;
- }
- count += sge->nr_cap_states;
- prev_cid = cid;
- }
- rcu_read_unlock();
- return count;
- }
- #ifdef CONFIG_NONLINEAR_FREQ_CTL
- static inline unsigned int get_opp_capacity(struct cpufreq_policy *policy,
- int row)
- {
- struct upower_tbl *upower_tbl;
- upower_tbl = upower_get_core_tbl(policy->cpu);
- return upower_tbl->row[row].cap;
- }
- #else
- static inline unsigned int get_opp_capacity(struct cpufreq_policy *policy,
- int row)
- {
- unsigned int cap, orig_cap;
- unsigned long freq, max_freq;
- max_freq = policy->cpuinfo.max_freq;
- orig_cap = capacity_orig_of(policy->cpu);
- freq = policy->freq_table[row].frequency;
- cap = orig_cap * freq / max_freq;
- return cap;
- }
- #endif
- void init_opp_capacity_tbl(void)
- {
- int cpu, cid, prev_cid = -1;
- int count = 0;
- int i, idx = 0;
- unsigned int cap;
- struct sched_domain *sd;
- struct sched_group *sg;
- const struct sched_group_energy *sge;
- struct cpufreq_policy *policy;
- count = system_opp_count();
- if (count < 0)
- return;
- opp_capacity_tbl =
- kmalloc_array(count, sizeof(unsigned int), GFP_KERNEL);
- if (!opp_capacity_tbl)
- return;
- rcu_read_lock();
- for (cpu = 0; cpu < nr_cpu_ids; cpu++) {
- cid = arch_get_cluster_id(cpu);
- if (cid == prev_cid)
- continue;
- sd = rcu_dereference_check_sched_domain(cpu_rq(cpu)->sd);
- if (sd) {
- sg = sd->groups;
- sge = sg->sge;
- } else {
- pr_info("sched: %s no sd\n", __func__);
- goto free_unlock;
- }
- policy = cpufreq_cpu_get(cpu);
- if (!policy) {
- pr_info("policy not ready\n");
- goto free_unlock;
- }
- for (i = 0; i < sge->nr_cap_states; i++) {
- cap = get_opp_capacity(policy, i);
- opp_capacity_tbl[idx] = cap;
- idx++;
- }
- cpufreq_cpu_put(policy);
- prev_cid = cid;
- }
- rcu_read_unlock();
- sort(opp_capacity_tbl, count, sizeof(unsigned int),
- &cap_compare, NULL);
- opp_capacity_tbl[count - 1] = SCHED_CAPACITY_SCALE;
- total_opp_count = count;
- opp_capacity_tbl_ready = 1;
- return;
- free_unlock:
- rcu_read_unlock();
- kfree(opp_capacity_tbl);
- }
- unsigned int find_fit_capacity(unsigned int cap)
- {
- int i;
- if (unlikely(!opp_capacity_tbl_ready))
- return cap;
- if (cap == 0)
- return cap;
- for (i = 0; i < total_opp_count; i++) {
- if (opp_capacity_tbl[i] >= cap)
- return opp_capacity_tbl[i];
- }
- return SCHED_CAPACITY_SCALE;
- }
- #else
- int opp_capacity_tbl_ready = 1;
- void init_opp_capacity_tbl(void) {}
- unsigned int find_fit_capacity(unsigned int cap)
- {
- return cap;
- }
- #endif
- #ifdef CONFIG_UCLAMP_TASK
- /**
- * uclamp_mutex: serializes updates of utilization clamp values
- *
- * Utilization clamp value updates are triggered from user-space (slow-path)
- * but require refcounting updates on data structures used by scheduler's
- * enqueue/dequeue operations (fast-path).
- * While fast-path refcounting is enforced by atomic operations, this mutex
- * ensures that we serialize user-space requests thus avoiding the risk of
- * conflicting updates or API abuses.
- */
- DEFINE_MUTEX(uclamp_mutex);
- /*
- * Minimum utilization for all tasks
- * default: 0
- */
- unsigned int sysctl_sched_uclamp_util_min;
- /*
- * Maximum utilization for all tasks
- * default: 1024
- */
- unsigned int sysctl_sched_uclamp_util_max = SCHED_CAPACITY_SCALE;
- /*
- * Tasks's clamp values are required to be within this range
- */
- static struct uclamp_se uclamp_default[UCLAMP_CNT];
- static struct uclamp_se uclamp_default_perf[UCLAMP_CNT];
- /**
- * uclamp_map: reference count utilization clamp groups
- * @value: the utilization "clamp value" tracked by this clamp group
- * @se_count: the number of scheduling entities using this "clamp value"
- */
- union uclamp_map {
- struct {
- unsigned long value : SCHED_CAPACITY_SHIFT + 1;
- unsigned long se_count : BITS_PER_LONG -
- SCHED_CAPACITY_SHIFT - 1;
- };
- unsigned long data;
- atomic_long_t adata;
- };
- /**
- * uclamp_maps: map SEs "clamp value" into CPUs "clamp group"
- *
- * Since only a limited number of different "clamp values" are supported, we
- * map each value into a "clamp group" (group_id) used at tasks {en,de}queued
- * time to update a per-CPU refcounter tracking the number or RUNNABLE tasks
- * requesting that clamp value.
- * A "clamp index" (clamp_id) is used to define the kind of clamping, i.e. min
- * and max utilization.
- *
- * A matrix is thus required to map "clamp values" (value) to "clamp groups"
- * (group_id), for each "clamp index" (clamp_id), where:
- * - rows are indexed by clamp_id and they collect the clamp groups for a
- * given clamp index
- * - columns are indexed by group_id and they collect the clamp values which
- * maps to that clamp group
- *
- * Thus, the column index of a given (clamp_id, value) pair represents the
- * clamp group (group_id) used by the fast-path's per-CPU refcounter.
- *
- * uclamp_maps is a matrix of
- * +------- UCLAMP_CNT by UCLAMP_GROUPS entries
- * | |
- * | /---------------+---------------\
- * | +------------+ +------------+
- * | / UCLAMP_MIN | value | | value |
- * | | | se_count |...... | se_count |
- * | | +------------+ +------------+
- * +--+ +------------+ +------------+
- * | | value | | value |
- * \ UCLAMP_MAX | se_count |...... | se_count |
- * +-----^------+ +----^-------+
- * |
- * |
- * +
- * uclamp_maps[clamp_id][group_id].value
- */
- static union uclamp_map uclamp_maps[UCLAMP_CNT][UCLAMP_GROUPS];
- /*
- * uclamp_group_value: get the "group value" for a given "clamp value"
- * @value: the utiliation "clamp value" to translate
- *
- * The number of clamp group, which is defined at compile time, allows to
- * track a finite number of different clamp values. Thus clamp values are
- * grouped into bins each one representing a different "group value".
- * This method returns the "group value" corresponding to the specified
- * "clamp value".
- */
- static inline unsigned int uclamp_group_value(unsigned int clamp_value)
- {
- #define UCLAMP_GROUP_DELTA (SCHED_CAPACITY_SCALE / CONFIG_UCLAMP_GROUPS_COUNT)
- #define UCLAMP_GROUP_UPPER (UCLAMP_GROUP_DELTA * CONFIG_UCLAMP_GROUPS_COUNT)
- if (clamp_value >= UCLAMP_GROUP_UPPER)
- return SCHED_CAPACITY_SCALE;
- return UCLAMP_GROUP_DELTA * (clamp_value / UCLAMP_GROUP_DELTA);
- }
- /**
- * uclamp_cpu_update: updates the utilization clamp of a CPU
- * @rq: the CPU's rq which utilization clamp has to be updated
- * @clamp_id: the clamp index to update
- *
- * When tasks are enqueued/dequeued on/from a CPU, the set of currently active
- * clamp groups can change. Since each clamp group enforces a different
- * utilization clamp value, once the set of active groups changes it can be
- * required to re-compute what is the new clamp value to apply for that CPU.
- *
- * For the specified clamp index, this method computes the new CPU utilization
- * clamp to use until the next change on the set of active clamp groups.
- */
- static inline void uclamp_cpu_update(struct rq *rq, unsigned int clamp_id,
- unsigned int last_clamp_value)
- {
- unsigned int group_id;
- int max_value = -1;
- for (group_id = 0; group_id < UCLAMP_GROUPS; ++group_id) {
- if (!rq->uclamp.group[clamp_id][group_id].tasks)
- continue;
- /* Both min and max clamps are MAX aggregated */
- if (max_value < rq->uclamp.group[clamp_id][group_id].value)
- max_value = rq->uclamp.group[clamp_id][group_id].value;
- if (max_value >= SCHED_CAPACITY_SCALE)
- break;
- }
- /*
- * Just for the UCLAMP_MAX value, in case there are no RUNNABLE
- * task, we want to keep the CPU clamped to the last task's clamp
- * value. This is to avoid frequency spikes to MAX when one CPU, with
- * an high blocked utilization, sleeps and another CPU, in the same
- * frequency domain, do not see anymore the clamp on the first CPU.
- *
- * The UCLAMP_FLAG_IDLE is set whenever we detect, from the above
- * loop, that there are no more RUNNABLE taks on that CPU.
- * In this case we enforce the CPU util_max to that of the last
- * dequeued task.
- */
- if (max_value < 0) {
- if (clamp_id == UCLAMP_MAX) {
- rq->uclamp.flags |= UCLAMP_FLAG_IDLE;
- max_value = last_clamp_value;
- } else {
- max_value = uclamp_none(UCLAMP_MIN);
- }
- }
- rq->uclamp.value[clamp_id] = max_value;
- }
- #if defined(CONFIG_UCLAMP_TASK_GROUP) && defined(CONFIG_SCHED_TUNE)
- #define uclamp_apply_defaults(p) false
- #elif defined(CONFIG_UCLAMP_TASK_GROUP)
- /**
- * uclamp_apply_defaults: check if p is subject to system default clamps
- * @p: the task to check
- *
- * Tasks in the root group or autogroups are always and only limited by system
- * defaults. All others instead are limited by their TG's specific value.
- * This method checks the conditions under witch a task is subject to system
- * default clamps.
- */
- static inline bool uclamp_apply_defaults(struct task_struct *p)
- {
- if (task_group_is_autogroup(task_group(p)))
- return true;
- if (task_group(p) == &root_task_group)
- return true;
- return false;
- }
- #else
- #define uclamp_apply_defaults(p) true
- #endif
- /**
- * uclamp_effective_group_id: get the effective clamp group index of a task
- * @p: the task to get the effective clamp value for
- * @clamp_id: the clamp index to consider
- *
- * The effective clamp group index of a task depends on:
- * - the task specific clamp value, explicitly requested from userspace
- * - the task group effective clamp value, for tasks not in the root group or
- * in an autogroup
- * - the system default clamp value, defined by the sysadmin
- * and tasks specific's clamp values are always restricted, with increasing
- * priority, by their task group first and the system defaults after.
- *
- * This method returns the effective group index for a task, depending on its
- * status and a proper aggregation of the clamp values listed above.
- * Moreover, it ensures that the task's effective value:
- * task_struct::uclamp::effective::value
- * is updated to represent the clamp value corresponding to the taks effective
- * group index.
- */
- static inline unsigned int uclamp_effective_group_id(struct task_struct *p,
- unsigned int clamp_id)
- {
- struct uclamp_se *default_clamp;
- unsigned int clamp_value;
- unsigned int group_id;
- /* Task currently refcounted into a CPU clamp group */
- if (p->uclamp[clamp_id].active)
- return p->uclamp[clamp_id].effective.group_id;
- /* Task specific clamp value */
- clamp_value = p->uclamp[clamp_id].value;
- group_id = p->uclamp[clamp_id].group_id;
- if (!uclamp_apply_defaults(p)) {
- #if defined(CONFIG_UCLAMP_TASK_GROUP) && defined(CONFIG_SCHED_TUNE)
- struct uclamp_se *uc_se;
- unsigned int clamp_max;
- unsigned int group_max;
- /* Group specific clamp value */
- uc_se = task_schedtune_uclamp(p, clamp_id);
- clamp_max = uc_se->effective.value;
- group_max = uc_se->effective.group_id;
- /* Use group clamp value restrict task clamp value */
- if (!p->uclamp[clamp_id].user_defined ||
- (clamp_max != uclamp_none(clamp_id) &&
- clamp_value != clamp_max)) {
- clamp_value = clamp_max;
- group_id = group_max;
- }
- #elif defined(CONFIG_UCLAMP_TASK_GROUP)
- unsigned int clamp_max =
- task_group(p)->uclamp[clamp_id].effective.value;
- unsigned int group_max =
- task_group(p)->uclamp[clamp_id].effective.group_id;
- if (!p->uclamp[clamp_id].user_defined ||
- clamp_value > clamp_max) {
- clamp_value = clamp_max;
- group_id = group_max;
- }
- #endif
- goto done;
- }
- /* RT tasks have different default values */
- default_clamp = task_has_rt_policy(p)
- ? uclamp_default_perf
- : uclamp_default;
- /* System default restriction */
- if (unlikely(clamp_value < default_clamp[UCLAMP_MIN].value ||
- clamp_value > default_clamp[UCLAMP_MAX].value)) {
- /*
- * Unconditionally enforce system defaults, which is a simpler
- * solution compared to a proper clamping.
- */
- clamp_value = default_clamp[clamp_id].value;
- group_id = default_clamp[clamp_id].group_id;
- }
- done:
- p->uclamp[clamp_id].effective.value = clamp_value;
- p->uclamp[clamp_id].effective.group_id = group_id;
- return group_id;
- }
- unsigned int uclamp_task_effective_util(struct task_struct *p,
- unsigned int clamp_id)
- {
- uclamp_effective_group_id(p, clamp_id);
- return p->uclamp[clamp_id].effective.value;
- }
- unsigned int uclamp_task_util(struct task_struct *p,
- unsigned int clamp_id)
- {
- return p->uclamp[clamp_id].value;
- }
- /**
- * uclamp_cpu_get_id(): increase reference count for a clamp group on a CPU
- * @p: the task being enqueued on a CPU
- * @rq: the CPU's rq where the clamp group has to be reference counted
- * @clamp_id: the clamp index to update
- *
- * Once a task is enqueued on a CPU's rq, with increasing priority, we
- * reference count the most restrictive clamp group between the task specific
- * clamp value, the clamp value of its task group and the system default clamp
- * value.
- */
- static inline void uclamp_cpu_get_id(struct task_struct *p, struct rq *rq,
- unsigned int clamp_id)
- {
- unsigned int effective;
- unsigned int group_id;
- if (unlikely(!p->uclamp[clamp_id].mapped))
- return;
- group_id = uclamp_effective_group_id(p, clamp_id);
- p->uclamp[clamp_id].active = true;
- rq->uclamp.group[clamp_id][group_id].tasks += 1;
- effective = p->uclamp[clamp_id].effective.value;
- if (unlikely(rq->uclamp.flags & UCLAMP_FLAG_IDLE)) {
- /*
- * Reset clamp holds on idle exit.
- * This function is called for both UCLAMP_MIN (before) and
- * UCLAMP_MAX (after). Let's reset the flag only the second
- * once we know that UCLAMP_MIN has been already updated.
- */
- if (clamp_id == UCLAMP_MAX)
- rq->uclamp.flags &= ~UCLAMP_FLAG_IDLE;
- rq->uclamp.value[clamp_id] = effective;
- }
- /* CPU's clamp groups track the max effective clamp value */
- if (effective > rq->uclamp.group[clamp_id][group_id].value)
- rq->uclamp.group[clamp_id][group_id].value = effective;
- if (rq->uclamp.value[clamp_id] < effective)
- rq->uclamp.value[clamp_id] = effective;
- trace_uclamp_cpu_get_id(p, rq, clamp_id);
- }
- /**
- * uclamp_cpu_put_id(): decrease reference count for a clamp group on a CPU
- * @p: the task being dequeued from a CPU
- * @rq: the CPU's rq from where the clamp group has to be released
- * @clamp_id: the clamp index to update
- *
- * When a task is dequeued from a CPU's rq, the CPU's clamp group reference
- * counted by the task is released.
- * If this was the last task reference coutning the current max clamp group,
- * then the CPU clamping is updated to find the new max for the specified
- * clamp index.
- */
- static inline void uclamp_cpu_put_id(struct task_struct *p, struct rq *rq,
- unsigned int clamp_id)
- {
- unsigned int clamp_value;
- unsigned int group_id;
- if (unlikely(!p->uclamp[clamp_id].mapped))
- return;
- group_id = uclamp_effective_group_id(p, clamp_id);
- p->uclamp[clamp_id].active = false;
- if (likely(rq->uclamp.group[clamp_id][group_id].tasks))
- rq->uclamp.group[clamp_id][group_id].tasks -= 1;
- #ifdef CONFIG_SCHED_DEBUG
- else {
- printk_deferred("[name:uclamp&] invalid CPU[%d] clamp group [%u:%u] refcount\n",
- cpu_of(rq), clamp_id, group_id);
- }
- #endif
- if (likely(rq->uclamp.group[clamp_id][group_id].tasks))
- return;
- clamp_value = rq->uclamp.group[clamp_id][group_id].value;
- #ifdef CONFIG_SCHED_DEBUG
- if (unlikely(clamp_value > rq->uclamp.value[clamp_id])) {
- printk_deferred("[name:uclamp&] invalid CPU[%d] clamp group [%u:%u] value\n",
- cpu_of(rq), clamp_id, group_id);
- }
- #endif
- if (clamp_value >= rq->uclamp.value[clamp_id]) {
- /*
- * Each CPU's clamp group value is reset to its nominal group
- * value whenever there are anymore RUNNABLE tasks refcounting
- * that clamp group.
- */
- rq->uclamp.group[clamp_id][group_id].value =
- uclamp_maps[clamp_id][group_id].value;
- uclamp_cpu_update(rq, clamp_id, clamp_value);
- }
- trace_uclamp_cpu_put_id(p, rq, clamp_id, clamp_value);
- }
- /**
- * uclamp_cpu_get(): increase CPU's clamp group refcount
- * @rq: the CPU's rq where the task is enqueued
- * @p: the task being enqueued
- *
- * When a task is enqueued on a CPU's rq, all the clamp groups currently
- * enforced on a task are reference counted on that rq. Since not all
- * scheduling classes have utilization clamping support, their tasks will
- * be silently ignored.
- *
- * This method updates the utilization clamp constraints considering the
- * requirements for the specified task. Thus, this update must be done before
- * calling into the scheduling classes, which will eventually update schedutil
- * considering the new task requirements.
- */
- static inline void uclamp_cpu_get(struct rq *rq, struct task_struct *p)
- {
- unsigned int clamp_id;
- if (unlikely(!p->sched_class->uclamp_enabled))
- return;
- for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id)
- uclamp_cpu_get_id(p, rq, clamp_id);
- }
- /**
- * uclamp_cpu_put(): decrease CPU's clamp group refcount
- * @rq: the CPU's rq from where the task is dequeued
- * @p: the task being dequeued
- *
- * When a task is dequeued from a CPU's rq, all the clamp groups the task has
- * reference counted at enqueue time are now released.
- *
- * This method updates the utilization clamp constraints considering the
- * requirements for the specified task. Thus, this update must be done before
- * calling into the scheduling classes, which will eventually update schedutil
- * considering the new task requirements.
- */
- static inline void uclamp_cpu_put(struct rq *rq, struct task_struct *p)
- {
- unsigned int clamp_id;
- if (unlikely(!p->sched_class->uclamp_enabled))
- return;
- for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id)
- uclamp_cpu_put_id(p, rq, clamp_id);
- }
- /**
- * uclamp_task_update_active: update the clamp group of a RUNNABLE task
- * @p: the task which clamp groups must be updated
- * @clamp_id: the clamp index to consider
- *
- * Each time the clamp value of a task group is changed, the old and new clamp
- * groups must be updated for each CPU containing a RUNNABLE task belonging to
- * that task group. Sleeping tasks are not updated since they will be enqueued
- * with the proper clamp group index at their next activation.
- */
- static inline void
- uclamp_task_update_active(struct task_struct *p, unsigned int clamp_id)
- {
- struct rq_flags rf;
- struct rq *rq;
- /*
- * Lock the task and the CPU where the task is (or was) queued.
- *
- * We might lock the (previous) rq of a !RUNNABLE task, but that's the
- * price to pay to safely serialize util_{min,max} updates with
- * enqueues, dequeues and migration operations.
- * This is the same locking schema used by __set_cpus_allowed_ptr().
- */
- rq = task_rq_lock(p, &rf);
- /*
- * The setting of the clamp group is serialized by task_rq_lock().
- * Thus, if the task is not yet RUNNABLE and its task_struct is not
- * affecting a valid clamp group, then the next time it's going to be
- * enqueued it will already see the updated clamp group value.
- */
- if (!p->uclamp[clamp_id].active)
- goto done;
- uclamp_cpu_put_id(p, rq, clamp_id);
- uclamp_cpu_get_id(p, rq, clamp_id);
- done:
- task_rq_unlock(rq, p, &rf);
- }
- /**
- * uclamp_group_put: decrease the reference count for a clamp group
- * @clamp_id: the clamp index which was affected by a task group
- * @group_id: the clamp group to release
- *
- * When the clamp value for a task group is changed we decrease the reference
- * count for the clamp group mapping its current clamp value.
- */
- void uclamp_group_put(unsigned int clamp_id, unsigned int group_id)
- {
- union uclamp_map *uc_maps = &uclamp_maps[clamp_id][0];
- union uclamp_map uc_map_old, uc_map_new;
- long res;
- retry:
- uc_map_old.data = atomic_long_read(&uc_maps[group_id].adata);
- #ifdef CONFIG_SCHED_DEBUG
- #define UCLAMP_GRPERR "invalid SE clamp group [%u:%u] refcount\n"
- if (unlikely(!uc_map_old.se_count)) {
- pr_err_ratelimited(UCLAMP_GRPERR, clamp_id, group_id);
- return;
- }
- #endif
- uc_map_new = uc_map_old;
- uc_map_new.se_count -= 1;
- res = atomic_long_cmpxchg(&uc_maps[group_id].adata,
- uc_map_old.data, uc_map_new.data);
- if (res != uc_map_old.data)
- goto retry;
- }
- static inline void uclamp_group_get_tg(struct cgroup_subsys_state *css,
- int clamp_id, unsigned int group_id)
- {
- struct css_task_iter it;
- struct task_struct *p;
- /*
- * In lazy update mode, tasks will be accounted into the right clamp
- * group the next time they will be requeued.
- */
- if (unlikely(sched_feat(UCLAMP_LAZY_UPDATE)))
- return;
- /* Update clamp groups for RUNNABLE tasks in this TG */
- css_task_iter_start(css, 0, &it);
- while ((p = css_task_iter_next(&it)))
- uclamp_task_update_active(p, clamp_id);
- css_task_iter_end(&it);
- }
- /**
- * uclamp_group_get: increase the reference count for a clamp group
- * @p: the task which clamp value must be tracked
- * @css: the task group which clamp value must be tracked
- * @uc_se: the utilization clamp data for the task
- * @clamp_id: the clamp index affected by the task
- * @clamp_value: the new clamp value for the task
- *
- * Each time a task changes its utilization clamp value, for a specified clamp
- * index, we need to find an available clamp group which can be used to track
- * this new clamp value. The corresponding clamp group index will be used to
- * reference count the corresponding clamp value while the task is enqueued on
- * a CPU.
- */
- void uclamp_group_get(struct task_struct *p,
- struct cgroup_subsys_state *css,
- struct uclamp_se *uc_se,
- unsigned int clamp_id, unsigned int clamp_value)
- {
- union uclamp_map *uc_maps = &uclamp_maps[clamp_id][0];
- unsigned int prev_group_id = uc_se->group_id;
- union uclamp_map uc_map_old, uc_map_new;
- unsigned int free_group_id;
- unsigned int group_value;
- unsigned int group_id;
- unsigned long res;
- int cpu;
- #ifdef CONFIG_UCLAMP_MAP_OPP
- group_value = clamp_value;
- #else
- group_value = uclamp_group_value(clamp_value);
- #endif
- retry:
- free_group_id = UCLAMP_GROUPS;
- for (group_id = 0; group_id < UCLAMP_GROUPS; ++group_id) {
- uc_map_old.data = atomic_long_read(&uc_maps[group_id].adata);
- if (free_group_id == UCLAMP_GROUPS && !uc_map_old.se_count)
- free_group_id = group_id;
- if (uc_map_old.value == group_value)
- break;
- }
- if (group_id >= UCLAMP_GROUPS) {
- #ifdef CONFIG_SCHED_DEBUG
- #define UCLAMP_MAPERR "clamp value [%u] mapping to clamp group failed\n"
- if (unlikely(free_group_id == UCLAMP_GROUPS)) {
- pr_err_ratelimited(UCLAMP_MAPERR, clamp_value);
- return;
- }
- #endif
- group_id = free_group_id;
- uc_map_old.data = atomic_long_read(&uc_maps[group_id].adata);
- }
- uc_map_new.se_count = uc_map_old.se_count + 1;
- uc_map_new.value = group_value;
- res = atomic_long_cmpxchg(&uc_maps[group_id].adata,
- uc_map_old.data, uc_map_new.data);
- if (res != uc_map_old.data)
- goto retry;
- /* Ensure each CPU tracks the correct value for this clamp group */
- if (likely(uc_map_new.se_count > 1))
- goto done;
- for_each_possible_cpu(cpu) {
- struct uclamp_cpu *uc_cpu = &cpu_rq(cpu)->uclamp;
- /* Refcounting is expected to be always 0 for free groups */
- if (unlikely(uc_cpu->group[clamp_id][group_id].tasks)) {
- #ifdef CONFIG_SCHED_DEBUG
- WARN(1, "invalid CPU[%d] clamp group [%u:%u] refcount: [%u]\n",
- cpu, clamp_id, group_id,
- uc_cpu->group[clamp_id][group_id].tasks);
- #endif
- uc_cpu->group[clamp_id][group_id].tasks = 0;
- }
- if (uc_cpu->group[clamp_id][group_id].value == group_value)
- continue;
- uc_cpu->group[clamp_id][group_id].value = group_value;
- }
- done:
- /* Update SE's clamp values and attach it to new clamp group */
- uc_se->value = clamp_value;
- uc_se->group_id = group_id;
- /* Newly created TG don't have tasks assigned */
- if (css) {
- uc_se->effective.value = clamp_value;
- uc_se->effective.group_id = group_id;
- uclamp_group_get_tg(css, clamp_id, group_id);
- }
- /* Update CPU's clamp group refcounts of RUNNABLE task */
- if (p)
- uclamp_task_update_active(p, clamp_id);
- /* Release the previous clamp group */
- if (uc_se->mapped)
- uclamp_group_put(clamp_id, prev_group_id);
- uc_se->mapped = true;
- }
- int sched_uclamp_handler(struct ctl_table *table, int write,
- void __user *buffer, size_t *lenp,
- loff_t *ppos)
- {
- int old_min, old_max;
- int result = 0;
- mutex_lock(&uclamp_mutex);
- old_min = sysctl_sched_uclamp_util_min;
- old_max = sysctl_sched_uclamp_util_max;
- result = proc_dointvec(table, write, buffer, lenp, ppos);
- if (result)
- goto undo;
- if (!write)
- goto done;
- if (sysctl_sched_uclamp_util_min > sysctl_sched_uclamp_util_max ||
- sysctl_sched_uclamp_util_max > SCHED_CAPACITY_SCALE) {
- result = -EINVAL;
- goto undo;
- }
- sysctl_sched_uclamp_util_min =
- find_fit_capacity(sysctl_sched_uclamp_util_min);
- sysctl_sched_uclamp_util_max =
- find_fit_capacity(sysctl_sched_uclamp_util_max);
- if (old_min != sysctl_sched_uclamp_util_min) {
- uclamp_group_get(NULL, NULL, &uclamp_default[UCLAMP_MIN],
- UCLAMP_MIN, sysctl_sched_uclamp_util_min);
- }
- if (old_max != sysctl_sched_uclamp_util_max) {
- uclamp_group_get(NULL, NULL, &uclamp_default[UCLAMP_MAX],
- UCLAMP_MAX, sysctl_sched_uclamp_util_max);
- }
- goto done;
- undo:
- sysctl_sched_uclamp_util_min = old_min;
- sysctl_sched_uclamp_util_max = old_max;
- done:
- mutex_unlock(&uclamp_mutex);
- return result;
- }
- #if defined(CONFIG_UCLAMP_TASK_GROUP) && !defined(CONFIG_SCHED_TUNE)
- /*
- * free_uclamp_sched_group: release utilization clamp references of a TG
- * @tg: the task group being removed
- *
- * An empty task group can be removed only when it has no more tasks or child
- * groups. This means that we can also safely release all the reference
- * counting to clamp groups.
- */
- static inline void free_uclamp_sched_group(struct task_group *tg)
- {
- int clamp_id;
- for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id)
- uclamp_group_put(clamp_id, tg->uclamp[clamp_id].group_id);
- }
- /**
- * alloc_uclamp_sched_group: initialize a new TG's for utilization clamping
- * @tg: the newly created task group
- * @parent: its parent task group
- *
- * A newly created task group inherits its utilization clamp values, for all
- * clamp indexes, from its parent task group.
- * This ensures that its values are properly initialized and that the task
- * group is accounted in the same parent's group index.
- *
- * Return: 0 on error
- */
- static inline int alloc_uclamp_sched_group(struct task_group *tg,
- struct task_group *parent)
- {
- int clamp_id;
- for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
- uclamp_group_get(NULL, NULL, &tg->uclamp[clamp_id],
- clamp_id, parent->uclamp[clamp_id].value);
- tg->uclamp[clamp_id].effective.value =
- parent->uclamp[clamp_id].effective.value;
- tg->uclamp[clamp_id].effective.group_id =
- parent->uclamp[clamp_id].effective.group_id;
- }
- return 1;
- }
- #else /* CONFIG_UCLAMP_TASK_GROUP */
- static inline void free_uclamp_sched_group(struct task_group *tg) { }
- static inline int alloc_uclamp_sched_group(struct task_group *tg,
- struct task_group *parent)
- {
- return 1;
- }
- #endif /* CONFIG_UCLAMP_TASK_GROUP */
- /**
- * uclamp_exit_task: release referenced clamp groups
- * @p: the task exiting
- *
- * When a task terminates, release all its (eventually) refcounted
- * task-specific clamp groups.
- */
- void uclamp_exit_task(struct task_struct *p)
- {
- unsigned int clamp_id;
- if (unlikely(!p->sched_class->uclamp_enabled))
- return;
- for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
- if (!p->uclamp[clamp_id].mapped)
- continue;
- uclamp_group_put(clamp_id, p->uclamp[clamp_id].group_id);
- }
- }
- /**
- * uclamp_fork: refcount task-specific clamp values for a new task
- */
- static void uclamp_fork(struct task_struct *p, bool reset)
- {
- unsigned int clamp_id;
- if (unlikely(!p->sched_class->uclamp_enabled))
- return;
- for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
- unsigned int clamp_value = uclamp_none(clamp_id);
- memset(&p->uclamp[clamp_id], 0, sizeof(struct uclamp_se));
- p->uclamp[clamp_id].user_defined = false;
- p->uclamp[clamp_id].mapped = false;
- p->uclamp[clamp_id].active = false;
- uclamp_group_get(p, NULL, &p->uclamp[clamp_id],
- clamp_id, clamp_value);
- }
- }
- static struct task_struct *find_process_by_pid(pid_t pid);
- int set_task_util_min(pid_t pid, unsigned int util_min)
- {
- unsigned int upper_bound;
- struct task_struct *p;
- int ret = 0;
- if (!opp_capacity_tbl_ready)
- init_opp_capacity_tbl();
- util_min = find_fit_capacity(util_min);
- mutex_lock(&uclamp_mutex);
- rcu_read_lock();
- p = find_process_by_pid(pid);
- if (!p) {
- ret = -ESRCH;
- goto out;
- }
- upper_bound = p->uclamp[UCLAMP_MAX].value;
- if (util_min > upper_bound || util_min < 0) {
- ret = -EINVAL;
- goto out;
- }
- p->uclamp[UCLAMP_MIN].user_defined = true;
- uclamp_group_get(p, NULL, &p->uclamp[UCLAMP_MIN],
- UCLAMP_MIN, util_min);
- out:
- rcu_read_unlock();
- mutex_unlock(&uclamp_mutex);
- return ret;
- }
- EXPORT_SYMBOL(set_task_util_min);
- int set_task_util_min_pct(pid_t pid, unsigned int pct)
- {
- unsigned int util_min;
- if (pid <= 0)
- return -EINVAL;
- if (pct < 0 || pct > 100)
- return -ERANGE;
- util_min = scale_from_percent(pct);
- return set_task_util_min(pid, util_min);
- }
- EXPORT_SYMBOL(set_task_util_min_pct);
- int set_task_util_max(pid_t pid, unsigned int util_max)
- {
- unsigned int lower_bound;
- struct task_struct *p;
- int ret = 0;
- if (!opp_capacity_tbl_ready)
- init_opp_capacity_tbl();
- util_max = find_fit_capacity(util_max);
- mutex_lock(&uclamp_mutex);
- rcu_read_lock();
- p = find_process_by_pid(pid);
- if (!p) {
- ret = -ESRCH;
- goto out;
- }
- lower_bound = p->uclamp[UCLAMP_MIN].value;
- if (util_max < lower_bound || util_max > 1024) {
- ret = -EINVAL;
- goto out;
- }
- p->uclamp[UCLAMP_MAX].user_defined = true;
- uclamp_group_get(p, NULL, &p->uclamp[UCLAMP_MAX],
- UCLAMP_MAX, util_max);
- out:
- rcu_read_unlock();
- mutex_unlock(&uclamp_mutex);
- return ret;
- }
- EXPORT_SYMBOL(set_task_util_max);
- int set_task_util_max_pct(pid_t pid, unsigned int pct)
- {
- unsigned int util_max;
- if (pid <= 0)
- return -EINVAL;
- if (pct < 0 || pct > 100)
- return -ERANGE;
- util_max = scale_from_percent(pct);
- return set_task_util_max(pid, util_max);
- }
- EXPORT_SYMBOL(set_task_util_max_pct);
- /**
- * init_uclamp: initialize data structures required for utilization clamping
- */
- static void __init init_uclamp(void)
- {
- struct uclamp_se *uc_se;
- unsigned int clamp_id;
- int cpu;
- mutex_init(&uclamp_mutex);
- for_each_possible_cpu(cpu)
- memset(&cpu_rq(cpu)->uclamp, 0, sizeof(struct uclamp_cpu));
- memset(uclamp_maps, 0, sizeof(uclamp_maps));
- for (clamp_id = 0; clamp_id < UCLAMP_CNT; ++clamp_id) {
- uc_se = &init_task.uclamp[clamp_id];
- uclamp_group_get(NULL, NULL, uc_se, clamp_id,
- uclamp_none(clamp_id));
- uc_se = &uclamp_default[clamp_id];
- uclamp_group_get(NULL, NULL, uc_se, clamp_id,
- uclamp_none(clamp_id));
- /* RT tasks by default will go to max frequency */
- uc_se = &uclamp_default_perf[clamp_id];
- uclamp_group_get(NULL, NULL, uc_se, clamp_id,
- uclamp_none(UCLAMP_MAX));
- #if defined(CONFIG_UCLAMP_TASK_GROUP) && defined(CONFIG_SCHED_TUNE)
- schedtune_init_uclamp();
- #elif defined(CONFIG_UCLAMP_TASK_GROUP)
- /* Init root TG's clamp group */
- uc_se = &root_task_group.uclamp[clamp_id];
- uclamp_group_get(NULL, NULL, uc_se, clamp_id,
- uclamp_none(UCLAMP_MAX));
- uc_se->effective.group_id = uc_se->group_id;
- uc_se->effective.value = uc_se->value;
- #endif
- }
- }
- #else /* CONFIG_UCLAMP_TASK */
- unsigned int uclamp_task_effective_util(struct task_struct *p,
- unsigned int clamp_id)
- {
- return 0;
- }
- unsigned int uclamp_task_util(struct task_struct *p,
- unsigned int clamp_id)
- {
- return 0;
- }
- static inline void uclamp_fork(struct task_struct *p, bool reset) { }
- static inline void init_uclamp(void) { }
- static inline void uclamp_cpu_get(struct rq *rq, struct task_struct *p) { }
- static inline void uclamp_cpu_put(struct rq *rq, struct task_struct *p) { }
- #endif /* CONFIG_UCLAMP_TASK */
- void set_capacity_margin(unsigned int margin)
- {
- capacity_margin = margin;
- }
- EXPORT_SYMBOL(set_capacity_margin);
- unsigned int get_capacity_margin(void)
- {
- return capacity_margin;
- }
- EXPORT_SYMBOL(get_capacity_margin);
- static inline void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
- {
- if (!(flags & ENQUEUE_NOCLOCK))
- update_rq_clock(rq);
- if (!(flags & ENQUEUE_RESTORE)) {
- sched_info_queued(rq, p);
- psi_enqueue(p, flags & ENQUEUE_WAKEUP);
- }
- uclamp_cpu_get(rq, p);
- p->sched_class->enqueue_task(rq, p, flags);
- /* update last_enqueued_ts for big task rotation */
- p->last_enqueued_ts = ktime_get_ns();
- }
- static inline void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
- {
- if (!(flags & DEQUEUE_NOCLOCK))
- update_rq_clock(rq);
- if (!(flags & DEQUEUE_SAVE)) {
- sched_info_dequeued(rq, p);
- psi_dequeue(p, flags & DEQUEUE_SLEEP);
- }
- uclamp_cpu_put(rq, p);
- p->sched_class->dequeue_task(rq, p, flags);
- }
- void activate_task(struct rq *rq, struct task_struct *p, int flags)
- {
- if (task_contributes_to_load(p))
- rq->nr_uninterruptible--;
- enqueue_task(rq, p, flags);
- }
- void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
- {
- if (task_contributes_to_load(p))
- rq->nr_uninterruptible++;
- dequeue_task(rq, p, flags);
- }
- /*
- * __normal_prio - return the priority that is based on the static prio
- */
- static inline int __normal_prio(struct task_struct *p)
- {
- return p->static_prio;
- }
- /*
- * Calculate the expected normal priority: i.e. priority
- * without taking RT-inheritance into account. Might be
- * boosted by interactivity modifiers. Changes upon fork,
- * setprio syscalls, and whenever the interactivity
- * estimator recalculates.
- */
- static inline int normal_prio(struct task_struct *p)
- {
- int prio;
- if (task_has_dl_policy(p))
- prio = MAX_DL_PRIO-1;
- else if (task_has_rt_policy(p))
- prio = MAX_RT_PRIO-1 - p->rt_priority;
- else
- prio = __normal_prio(p);
- return prio;
- }
- /*
- * Calculate the current priority, i.e. the priority
- * taken into account by the scheduler. This value might
- * be boosted by RT tasks, or might be boosted by
- * interactivity modifiers. Will be RT if the task got
- * RT-boosted. If not then it returns p->normal_prio.
- */
- static int effective_prio(struct task_struct *p)
- {
- p->normal_prio = normal_prio(p);
- /*
- * If we are RT tasks or we were boosted to RT priority,
- * keep the priority unchanged. Otherwise, update priority
- * to the normal priority:
- */
- if (!rt_prio(p->prio))
- return p->normal_prio;
- return p->prio;
- }
- /**
- * task_curr - is this task currently executing on a CPU?
- * @p: the task in question.
- *
- * Return: 1 if the task is currently executing. 0 otherwise.
- */
- inline int task_curr(const struct task_struct *p)
- {
- return cpu_curr(task_cpu(p)) == p;
- }
- /*
- * switched_from, switched_to and prio_changed must _NOT_ drop rq->lock,
- * use the balance_callback list if you want balancing.
- *
- * this means any call to check_class_changed() must be followed by a call to
- * balance_callback().
- */
- static inline void check_class_changed(struct rq *rq, struct task_struct *p,
- const struct sched_class *prev_class,
- int oldprio)
- {
- if (prev_class != p->sched_class) {
- if (prev_class->switched_from)
- prev_class->switched_from(rq, p);
- p->sched_class->switched_to(rq, p);
- } else if (oldprio != p->prio || dl_task(p))
- p->sched_class->prio_changed(rq, p, oldprio);
- }
- void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
- {
- const struct sched_class *class;
- if (p->sched_class == rq->curr->sched_class) {
- rq->curr->sched_class->check_preempt_curr(rq, p, flags);
- } else {
- for_each_class(class) {
- if (class == rq->curr->sched_class)
- break;
- if (class == p->sched_class) {
- resched_curr(rq);
- break;
- }
- }
- }
- /*
- * A queue event has occurred, and we're going to schedule. In
- * this case, we can save a useless back to back clock update.
- */
- if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
- rq_clock_skip_update(rq, true);
- }
- #ifdef CONFIG_SMP
- static inline bool is_per_cpu_kthread(struct task_struct *p)
- {
- if (!(p->flags & PF_KTHREAD))
- return false;
- if (p->nr_cpus_allowed != 1)
- return false;
- return true;
- }
- /*
- * Per-CPU kthreads are allowed to run on !actie && online CPUs, see
- * __set_cpus_allowed_ptr() and select_fallback_rq().
- */
- static inline bool is_cpu_allowed(struct task_struct *p, int cpu)
- {
- if (!cpumask_test_cpu(cpu, &p->cpus_allowed))
- return false;
- if (is_per_cpu_kthread(p))
- return cpu_online(cpu);
- return cpu_active(cpu);
- }
- /*
- * This is how migration works:
- *
- * 1) we invoke migration_cpu_stop() on the target CPU using
- * stop_one_cpu().
- * 2) stopper starts to run (implicitly forcing the migrated thread
- * off the CPU)
- * 3) it checks whether the migrated task is still in the wrong runqueue.
- * 4) if it's in the wrong runqueue then the migration thread removes
- * it and puts it into the right queue.
- * 5) stopper completes and stop_one_cpu() returns and the migration
- * is done.
- */
- /*
- * move_queued_task - move a queued task to new rq.
- *
- * Returns (locked) new rq. Old rq's lock is released.
- */
- static struct rq *move_queued_task(struct rq *rq, struct rq_flags *rf,
- struct task_struct *p, int new_cpu)
- {
- lockdep_assert_held(&rq->lock);
- p->on_rq = TASK_ON_RQ_MIGRATING;
- dequeue_task(rq, p, DEQUEUE_NOCLOCK);
- rq_unpin_lock(rq, rf);
- double_lock_balance(rq, cpu_rq(new_cpu));
- set_task_cpu(p, new_cpu);
- double_rq_unlock(cpu_rq(new_cpu), rq);
- rq = cpu_rq(new_cpu);
- rq_lock(rq, rf);
- BUG_ON(task_cpu(p) != new_cpu);
- enqueue_task(rq, p, 0);
- p->on_rq = TASK_ON_RQ_QUEUED;
- check_preempt_curr(rq, p, 0);
- return rq;
- }
- struct migration_arg {
- struct task_struct *task;
- int dest_cpu;
- };
- /*
- * Move (not current) task off this CPU, onto the destination CPU. We're doing
- * this because either it can't run here any more (set_cpus_allowed()
- * away from this CPU, or CPU going down), or because we're
- * attempting to rebalance this task on exec (sched_exec).
- *
- * So we race with normal scheduler movements, but that's OK, as long
- * as the task is no longer on this CPU.
- */
- struct rq *__migrate_task(struct rq *rq, struct rq_flags *rf,
- struct task_struct *p, int dest_cpu)
- {
- /* Affinity changed (again). */
- if (!is_cpu_allowed(p, dest_cpu))
- return rq;
- update_rq_clock(rq);
- rq = move_queued_task(rq, rf, p, dest_cpu);
- return rq;
- }
- /*
- * migration_cpu_stop - this will be executed by a highprio stopper thread
- * and performs thread migration by bumping thread off CPU then
- * 'pushing' onto another runqueue.
- */
- static int migration_cpu_stop(void *data)
- {
- struct migration_arg *arg = data;
- struct task_struct *p = arg->task;
- struct rq *rq = this_rq();
- struct rq_flags rf;
- /*
- * The original target CPU might have gone down and we might
- * be on another CPU but it doesn't matter.
- */
- local_irq_disable();
- /*
- * We need to explicitly wake pending tasks before running
- * __migrate_task() such that we will not miss enforcing cpus_allowed
- * during wakeups, see set_cpus_allowed_ptr()'s TASK_WAKING test.
- */
- sched_ttwu_pending();
- raw_spin_lock(&p->pi_lock);
- rq_lock(rq, &rf);
- /*
- * If task_rq(p) != rq, it cannot be migrated here, because we're
- * holding rq->lock, if p->on_rq == 0 it cannot get enqueued because
- * we're holding p->pi_lock.
- */
- if (task_rq(p) == rq) {
- if (task_on_rq_queued(p))
- rq = __migrate_task(rq, &rf, p, arg->dest_cpu);
- else
- p->wake_cpu = arg->dest_cpu;
- }
- rq_unlock(rq, &rf);
- raw_spin_unlock(&p->pi_lock);
- local_irq_enable();
- return 0;
- }
- /*
- * sched_class::set_cpus_allowed must do the below, but is not required to
- * actually call this function.
- */
- void set_cpus_allowed_common(struct task_struct *p, const struct cpumask *new_mask)
- {
- cpumask_copy(&p->cpus_allowed, new_mask);
- p->nr_cpus_allowed = cpumask_weight(new_mask);
- }
- void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
- {
- struct rq *rq = task_rq(p);
- bool queued, running;
- lockdep_assert_held(&p->pi_lock);
- queued = task_on_rq_queued(p);
- running = task_current(rq, p);
- if (queued) {
- /*
- * Because __kthread_bind() calls this on blocked tasks without
- * holding rq->lock.
- */
- lockdep_assert_held(&rq->lock);
- dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
- }
- if (running)
- put_prev_task(rq, p);
- p->sched_class->set_cpus_allowed(p, new_mask);
- if (queued)
- enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
- if (running)
- set_curr_task(rq, p);
- }
- /*
- * Change a given task's CPU affinity. Migrate the thread to a
- * proper CPU and schedule it away if the CPU it's executing on
- * is removed from the allowed bitmask.
- *
- * NOTE: the caller must have a valid reference to the task, the
- * task must not exit() & deallocate itself prematurely. The
- * call is not atomic; no spinlocks may be held.
- */
- static int __set_cpus_allowed_ptr(struct task_struct *p,
- const struct cpumask *new_mask, bool check)
- {
- const struct cpumask *cpu_valid_mask = cpu_active_mask;
- unsigned int dest_cpu;
- struct rq_flags rf;
- struct rq *rq;
- int ret = 0;
- cpumask_t allowed_mask;
- rq = task_rq_lock(p, &rf);
- update_rq_clock(rq);
- if (p->flags & PF_KTHREAD) {
- /*
- * Kernel threads are allowed on online && !active CPUs
- */
- cpu_valid_mask = cpu_online_mask;
- }
- /*
- * Must re-check here, to close a race against __kthread_bind(),
- * sched_setaffinity() is not guaranteed to observe the flag.
- */
- if (check && (p->flags & PF_NO_SETAFFINITY)) {
- ret = -EINVAL;
- goto out;
- }
- if (cpumask_equal(&p->cpus_allowed, new_mask))
- goto out;
- cpumask_andnot(&allowed_mask, new_mask, cpu_isolated_mask);
- cpumask_and(&allowed_mask, &allowed_mask, cpu_valid_mask);
- dest_cpu = cpumask_any(&allowed_mask);
- if (dest_cpu >= nr_cpu_ids) {
- /* If p is a kthread, ignore isolated mask. */
- if (p->flags & PF_KTHREAD)
- cpumask_and(&allowed_mask, cpu_valid_mask, new_mask);
- else
- cpumask_andnot(&allowed_mask,
- cpu_valid_mask, cpu_isolated_mask);
- dest_cpu = cpumask_any(&allowed_mask);
- if (dest_cpu >= nr_cpu_ids) {
- ret = -EINVAL;
- goto out;
- }
- }
- do_set_cpus_allowed(p, new_mask);
- if (p->flags & PF_KTHREAD) {
- /*
- * For kernel threads that do indeed end up on online &&
- * !active we want to ensure they are strict per-CPU threads.
- */
- WARN_ON(cpumask_intersects(new_mask, cpu_online_mask) &&
- !cpumask_intersects(new_mask, cpu_active_mask) &&
- p->nr_cpus_allowed != 1);
- }
- /* Can the task run on the task's current CPU? If so, we're done */
- if (cpumask_test_cpu(task_cpu(p), &allowed_mask))
- goto out;
- if (task_running(rq, p) || p->state == TASK_WAKING) {
- struct migration_arg arg = { p, dest_cpu };
- /* Need help from migration thread: drop lock and wait. */
- task_rq_unlock(rq, p, &rf);
- stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
- tlb_migrate_finish(p->mm);
- return 0;
- } else if (task_on_rq_queued(p)) {
- /*
- * OK, since we're going to drop the lock immediately
- * afterwards anyway.
- */
- if (cpu_online(dest_cpu))
- rq = move_queued_task(rq, &rf, p, dest_cpu);
- }
- out:
- task_rq_unlock(rq, p, &rf);
- return ret;
- }
- int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
- {
- return __set_cpus_allowed_ptr(p, new_mask, false);
- }
- EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
- void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
- {
- #ifdef CONFIG_SCHED_DEBUG
- /*
- * We should never call set_task_cpu() on a blocked task,
- * ttwu() will sort out the placement.
- */
- WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
- !p->on_rq);
- /*
- * Migrating fair class task must have p->on_rq = TASK_ON_RQ_MIGRATING,
- * because schedstat_wait_{start,end} rebase migrating task's wait_start
- * time relying on p->on_rq.
- */
- WARN_ON_ONCE(p->state == TASK_RUNNING &&
- p->sched_class == &fair_sched_class &&
- (p->on_rq && !task_on_rq_migrating(p)));
- #ifdef CONFIG_LOCKDEP
- /*
- * The caller should hold either p->pi_lock or rq->lock, when changing
- * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
- *
- * sched_move_task() holds both and thus holding either pins the cgroup,
- * see task_group().
- *
- * Furthermore, all task_rq users should acquire both locks, see
- * task_rq_lock().
- */
- WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
- lockdep_is_held(&task_rq(p)->lock)));
- #endif
- /*
- * Clearly, migrating tasks to offline CPUs is a fairly daft thing.
- */
- WARN_ON_ONCE(!cpu_online(new_cpu));
- #endif
- trace_sched_migrate_task(p, new_cpu);
- if (task_cpu(p) != new_cpu) {
- if (p->sched_class->migrate_task_rq)
- p->sched_class->migrate_task_rq(p);
- p->se.nr_migrations++;
- perf_event_task_migrate(p);
- walt_fixup_busy_time(p, new_cpu);
- }
- __set_task_cpu(p, new_cpu);
- }
- static void __migrate_swap_task(struct task_struct *p, int cpu)
- {
- if (task_on_rq_queued(p)) {
- struct rq *src_rq, *dst_rq;
- struct rq_flags srf, drf;
- src_rq = task_rq(p);
- dst_rq = cpu_rq(cpu);
- rq_pin_lock(src_rq, &srf);
- rq_pin_lock(dst_rq, &drf);
- p->on_rq = TASK_ON_RQ_MIGRATING;
- deactivate_task(src_rq, p, 0);
- set_task_cpu(p, cpu);
- activate_task(dst_rq, p, 0);
- p->on_rq = TASK_ON_RQ_QUEUED;
- check_preempt_curr(dst_rq, p, 0);
- rq_unpin_lock(dst_rq, &drf);
- rq_unpin_lock(src_rq, &srf);
- } else {
- /*
- * Task isn't running anymore; make it appear like we migrated
- * it before it went to sleep. This means on wakeup we make the
- * previous CPU our target instead of where it really is.
- */
- p->wake_cpu = cpu;
- }
- }
- struct migration_swap_arg {
- struct task_struct *src_task, *dst_task;
- int src_cpu, dst_cpu;
- };
- static int migrate_swap_stop(void *data)
- {
- struct migration_swap_arg *arg = data;
- struct rq *src_rq, *dst_rq;
- int ret = -EAGAIN;
- if (!cpu_active(arg->src_cpu) || !cpu_active(arg->dst_cpu))
- return -EAGAIN;
- src_rq = cpu_rq(arg->src_cpu);
- dst_rq = cpu_rq(arg->dst_cpu);
- double_raw_lock(&arg->src_task->pi_lock,
- &arg->dst_task->pi_lock);
- double_rq_lock(src_rq, dst_rq);
- if (task_cpu(arg->dst_task) != arg->dst_cpu)
- goto unlock;
- if (task_cpu(arg->src_task) != arg->src_cpu)
- goto unlock;
- if (!cpumask_test_cpu(arg->dst_cpu, &arg->src_task->cpus_allowed))
- goto unlock;
- if (!cpumask_test_cpu(arg->src_cpu, &arg->dst_task->cpus_allowed))
- goto unlock;
- __migrate_swap_task(arg->src_task, arg->dst_cpu);
- __migrate_swap_task(arg->dst_task, arg->src_cpu);
- ret = 0;
- unlock:
- double_rq_unlock(src_rq, dst_rq);
- raw_spin_unlock(&arg->dst_task->pi_lock);
- raw_spin_unlock(&arg->src_task->pi_lock);
- return ret;
- }
- /*
- * Cross migrate two tasks
- */
- int migrate_swap(struct task_struct *cur, struct task_struct *p)
- {
- struct migration_swap_arg arg;
- int ret = -EINVAL;
- arg = (struct migration_swap_arg){
- .src_task = cur,
- .src_cpu = task_cpu(cur),
- .dst_task = p,
- .dst_cpu = task_cpu(p),
- };
- if (arg.src_cpu == arg.dst_cpu)
- goto out;
- /*
- * These three tests are all lockless; this is OK since all of them
- * will be re-checked with proper locks held further down the line.
- */
- if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
- goto out;
- if (!cpumask_test_cpu(arg.dst_cpu, &arg.src_task->cpus_allowed))
- goto out;
- if (!cpumask_test_cpu(arg.src_cpu, &arg.dst_task->cpus_allowed))
- goto out;
- trace_sched_swap_numa(cur, arg.src_cpu, p, arg.dst_cpu);
- ret = stop_two_cpus(arg.dst_cpu, arg.src_cpu, migrate_swap_stop, &arg);
- out:
- return ret;
- }
- /*
- * wait_task_inactive - wait for a thread to unschedule.
- *
- * If @match_state is nonzero, it's the @p->state value just checked and
- * not expected to change. If it changes, i.e. @p might have woken up,
- * then return zero. When we succeed in waiting for @p to be off its CPU,
- * we return a positive number (its total switch count). If a second call
- * a short while later returns the same number, the caller can be sure that
- * @p has remained unscheduled the whole time.
- *
- * The caller must ensure that the task *will* unschedule sometime soon,
- * else this function might spin for a *long* time. This function can't
- * be called with interrupts off, or it may introduce deadlock with
- * smp_call_function() if an IPI is sent by the same process we are
- * waiting to become inactive.
- */
- unsigned long wait_task_inactive(struct task_struct *p, long match_state)
- {
- int running, queued;
- struct rq_flags rf;
- unsigned long ncsw;
- struct rq *rq;
- for (;;) {
- /*
- * We do the initial early heuristics without holding
- * any task-queue locks at all. We'll only try to get
- * the runqueue lock when things look like they will
- * work out!
- */
- rq = task_rq(p);
- /*
- * If the task is actively running on another CPU
- * still, just relax and busy-wait without holding
- * any locks.
- *
- * NOTE! Since we don't hold any locks, it's not
- * even sure that "rq" stays as the right runqueue!
- * But we don't care, since "task_running()" will
- * return false if the runqueue has changed and p
- * is actually now running somewhere else!
- */
- while (task_running(rq, p)) {
- if (match_state && unlikely(p->state != match_state))
- return 0;
- cpu_relax();
- }
- /*
- * Ok, time to look more closely! We need the rq
- * lock now, to be *sure*. If we're wrong, we'll
- * just go back and repeat.
- */
- rq = task_rq_lock(p, &rf);
- trace_sched_wait_task(p);
- running = task_running(rq, p);
- queued = task_on_rq_queued(p);
- ncsw = 0;
- if (!match_state || p->state == match_state)
- ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
- task_rq_unlock(rq, p, &rf);
- /*
- * If it changed from the expected state, bail out now.
- */
- if (unlikely(!ncsw))
- break;
- /*
- * Was it really running after all now that we
- * checked with the proper locks actually held?
- *
- * Oops. Go back and try again..
- */
- if (unlikely(running)) {
- cpu_relax();
- continue;
- }
- /*
- * It's not enough that it's not actively running,
- * it must be off the runqueue _entirely_, and not
- * preempted!
- *
- * So if it was still runnable (but just not actively
- * running right now), it's preempted, and we should
- * yield - it could be a while.
- */
- if (unlikely(queued)) {
- ktime_t to = NSEC_PER_SEC / HZ;
- set_current_state(TASK_UNINTERRUPTIBLE);
- schedule_hrtimeout(&to, HRTIMER_MODE_REL);
- continue;
- }
- /*
- * Ahh, all good. It wasn't running, and it wasn't
- * runnable, which means that it will never become
- * running in the future either. We're all done!
- */
- break;
- }
- return ncsw;
- }
- /***
- * kick_process - kick a running thread to enter/exit the kernel
- * @p: the to-be-kicked thread
- *
- * Cause a process which is running on another CPU to enter
- * kernel-mode, without any delay. (to get signals handled.)
- *
- * NOTE: this function doesn't have to take the runqueue lock,
- * because all it wants to ensure is that the remote task enters
- * the kernel. If the IPI races and the task has been migrated
- * to another CPU then no harm is done and the purpose has been
- * achieved as well.
- */
- void kick_process(struct task_struct *p)
- {
- int cpu;
- preempt_disable();
- cpu = task_cpu(p);
- if ((cpu != smp_processor_id()) && task_curr(p))
- smp_send_reschedule(cpu);
- preempt_enable();
- }
- EXPORT_SYMBOL_GPL(kick_process);
- /*
- * ->cpus_allowed is protected by both rq->lock and p->pi_lock
- *
- * A few notes on cpu_active vs cpu_online:
- *
- * - cpu_active must be a subset of cpu_online
- *
- * - on cpu-up we allow per-cpu kthreads on the online && !active cpu,
- * see __set_cpus_allowed_ptr(). At this point the newly online
- * CPU isn't yet part of the sched domains, and balancing will not
- * see it.
- *
- * - on CPU-down we clear cpu_active() to mask the sched domains and
- * avoid the load balancer to place new tasks on the to be removed
- * CPU. Existing tasks will remain running there and will be taken
- * off.
- *
- * This means that fallback selection must not select !active CPUs.
- * And can assume that any active CPU must be online. Conversely
- * select_task_rq() below may allow selection of !active CPUs in order
- * to satisfy the above rules.
- */
- static int select_fallback_rq(int cpu, struct task_struct *p, bool allow_iso)
- {
- int nid = cpu_to_node(cpu);
- const struct cpumask *nodemask = NULL;
- enum { cpuset, possible, fail, bug } state = cpuset;
- int dest_cpu;
- int isolated_candidate = -1;
- /*
- * If the node that the CPU is on has been offlined, cpu_to_node()
- * will return -1. There is no CPU on the node, and we should
- * select the CPU on the other node.
- */
- if (nid != -1) {
- nodemask = cpumask_of_node(nid);
- /* Look for allowed, online CPU in same node. */
- for_each_cpu(dest_cpu, nodemask) {
- if (!cpu_active(dest_cpu))
- continue;
- if (cpu_isolated(dest_cpu))
- continue;
- if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
- return dest_cpu;
- }
- }
- for (;;) {
- /* Any allowed, online CPU? */
- for_each_cpu(dest_cpu, &p->cpus_allowed) {
- if (!is_cpu_allowed(p, dest_cpu))
- continue;
- if (cpu_isolated(dest_cpu)) {
- if (allow_iso)
- isolated_candidate = dest_cpu;
- continue;
- }
- goto out;
- }
- if (isolated_candidate != -1) {
- dest_cpu = isolated_candidate;
- goto out;
- }
- /* No more Mr. Nice Guy. */
- switch (state) {
- case cpuset:
- if (IS_ENABLED(CONFIG_CPUSETS)) {
- cpuset_cpus_allowed_fallback(p);
- state = possible;
- break;
- }
- /* Fall-through */
- case possible:
- do_set_cpus_allowed(p, cpu_possible_mask);
- state = fail;
- break;
- case fail:
- allow_iso = true;
- state = bug;
- break;
- case bug:
- BUG();
- break;
- }
- }
- out:
- if (state != cpuset) {
- /*
- * Don't tell them about moving exiting tasks or
- * kernel threads (both mm NULL), since they never
- * leave kernel.
- */
- if (p->mm && printk_ratelimit()) {
- printk_deferred("process %d (%s) no longer affine to cpu%d\n",
- task_pid_nr(p), p->comm, cpu);
- }
- }
- return dest_cpu;
- }
- /*
- * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
- */
- static inline
- int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags,
- int sibling_count_hint)
- {
- bool allow_isolated = (p->flags & PF_KTHREAD);
- bool select_fallback = false;
- cpumask_t cpu_unisolated_mask;
- lockdep_assert_held(&p->pi_lock);
- if (p->nr_cpus_allowed > 1)
- cpu = p->sched_class->select_task_rq(p, cpu, sd_flags, wake_flags,
- sibling_count_hint);
- else
- cpu = cpumask_any(&p->cpus_allowed);
- /*
- * In order not to call set_task_cpu() on a blocking task we need
- * to rely on ttwu() to place the task on a valid ->cpus_allowed
- * CPU.
- *
- * Since this is common to all placement strategies, this lives here.
- *
- * [ this allows ->select_task() to simply return task_cpu(p) and
- * not worry about this generic constraint ]
- */
- cpumask_andnot(&cpu_unisolated_mask, cpu_possible_mask,
- cpu_isolated_mask);
- /*
- * If kernel thread select a isolated CPU but it has other allowed CPU,
- * go to select_fallback_rq to choose allowed and un-isolated CPU.
- */
- if (allow_isolated && cpu_isolated(cpu) &&
- cpumask_intersects(tsk_cpus_allowed(p), &cpu_unisolated_mask)) {
- select_fallback = true;
- }
- if (unlikely(!is_cpu_allowed(p, cpu)) ||
- (cpu_isolated(cpu) && !allow_isolated) ||
- select_fallback)
- cpu = select_fallback_rq(task_cpu(p), p, allow_isolated);
- return cpu;
- }
- static void update_avg(u64 *avg, u64 sample)
- {
- s64 diff = sample - *avg;
- *avg += diff >> 3;
- }
- void sched_set_stop_task(int cpu, struct task_struct *stop)
- {
- struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
- struct task_struct *old_stop = cpu_rq(cpu)->stop;
- if (stop) {
- /*
- * Make it appear like a SCHED_FIFO task, its something
- * userspace knows about and won't get confused about.
- *
- * Also, it will make PI more or less work without too
- * much confusion -- but then, stop work should not
- * rely on PI working anyway.
- */
- sched_setscheduler_nocheck(stop, SCHED_FIFO, ¶m);
- stop->sched_class = &stop_sched_class;
- }
- cpu_rq(cpu)->stop = stop;
- if (old_stop) {
- /*
- * Reset it back to a normal scheduling class so that
- * it can die in pieces.
- */
- old_stop->sched_class = &rt_sched_class;
- }
- }
- #else
- static inline int __set_cpus_allowed_ptr(struct task_struct *p,
- const struct cpumask *new_mask, bool check)
- {
- return set_cpus_allowed_ptr(p, new_mask);
- }
- #endif /* CONFIG_SMP */
- static void
- ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
- {
- struct rq *rq;
- if (!schedstat_enabled())
- return;
- rq = this_rq();
- #ifdef CONFIG_SMP
- if (cpu == rq->cpu) {
- schedstat_inc(rq->ttwu_local);
- schedstat_inc(p->se.statistics.nr_wakeups_local);
- } else {
- struct sched_domain *sd;
- schedstat_inc(p->se.statistics.nr_wakeups_remote);
- rcu_read_lock();
- for_each_domain(rq->cpu, sd) {
- if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
- schedstat_inc(sd->ttwu_wake_remote);
- break;
- }
- }
- rcu_read_unlock();
- }
- if (wake_flags & WF_MIGRATED)
- schedstat_inc(p->se.statistics.nr_wakeups_migrate);
- #endif /* CONFIG_SMP */
- schedstat_inc(rq->ttwu_count);
- schedstat_inc(p->se.statistics.nr_wakeups);
- if (wake_flags & WF_SYNC)
- schedstat_inc(p->se.statistics.nr_wakeups_sync);
- }
- static inline void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
- {
- activate_task(rq, p, en_flags);
- p->on_rq = TASK_ON_RQ_QUEUED;
- /* If a worker is waking up, notify the workqueue: */
- if (p->flags & PF_WQ_WORKER)
- wq_worker_waking_up(p, cpu_of(rq));
- }
- /*
- * Mark the task runnable and perform wakeup-preemption.
- */
- static void ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags,
- struct rq_flags *rf)
- {
- check_preempt_curr(rq, p, wake_flags);
- p->state = TASK_RUNNING;
- trace_sched_wakeup(p);
- #ifdef CONFIG_SMP
- if (p->sched_class->task_woken) {
- /*
- * Our task @p is fully woken up and running; so its safe to
- * drop the rq->lock, hereafter rq is only used for statistics.
- */
- rq_unpin_lock(rq, rf);
- p->sched_class->task_woken(rq, p);
- rq_repin_lock(rq, rf);
- }
- if (rq->idle_stamp) {
- u64 delta = rq_clock(rq) - rq->idle_stamp;
- u64 max = 2*rq->max_idle_balance_cost;
- update_avg(&rq->avg_idle, delta);
- if (rq->avg_idle > max)
- rq->avg_idle = max;
- rq->idle_stamp = 0;
- }
- #endif
- }
- static void
- ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags,
- struct rq_flags *rf)
- {
- int en_flags = ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK;
- lockdep_assert_held(&rq->lock);
- #ifdef CONFIG_SMP
- if (p->sched_contributes_to_load)
- rq->nr_uninterruptible--;
- if (wake_flags & WF_MIGRATED)
- en_flags |= ENQUEUE_MIGRATED;
- #endif
- ttwu_activate(rq, p, en_flags);
- ttwu_do_wakeup(rq, p, wake_flags, rf);
- }
- /*
- * Called in case the task @p isn't fully descheduled from its runqueue,
- * in this case we must do a remote wakeup. Its a 'light' wakeup though,
- * since all we need to do is flip p->state to TASK_RUNNING, since
- * the task is still ->on_rq.
- */
- static int ttwu_remote(struct task_struct *p, int wake_flags)
- {
- struct rq_flags rf;
- struct rq *rq;
- int ret = 0;
- rq = __task_rq_lock(p, &rf);
- if (task_on_rq_queued(p)) {
- /* check_preempt_curr() may use rq clock */
- update_rq_clock(rq);
- ttwu_do_wakeup(rq, p, wake_flags, &rf);
- ret = 1;
- }
- __task_rq_unlock(rq, &rf);
- return ret;
- }
- #ifdef CONFIG_SMP
- void sched_ttwu_pending(void)
- {
- struct rq *rq = this_rq();
- struct llist_node *llist = llist_del_all(&rq->wake_list);
- struct task_struct *p, *t;
- struct rq_flags rf;
- if (!llist)
- return;
- rq_lock_irqsave(rq, &rf);
- update_rq_clock(rq);
- llist_for_each_entry_safe(p, t, llist, wake_entry)
- ttwu_do_activate(rq, p, p->sched_remote_wakeup ? WF_MIGRATED : 0, &rf);
- rq_unlock_irqrestore(rq, &rf);
- }
- void scheduler_ipi(void)
- {
- int cpu = smp_processor_id();
- /*
- * Fold TIF_NEED_RESCHED into the preempt_count; anybody setting
- * TIF_NEED_RESCHED remotely (for the first time) will also send
- * this IPI.
- */
- preempt_fold_need_resched();
- if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
- return;
- /*
- * Not all reschedule IPI handlers call irq_enter/irq_exit, since
- * traditionally all their work was done from the interrupt return
- * path. Now that we actually do some work, we need to make sure
- * we do call them.
- *
- * Some archs already do call them, luckily irq_enter/exit nest
- * properly.
- *
- * Arguably we should visit all archs and update all handlers,
- * however a fair share of IPIs are still resched only so this would
- * somewhat pessimize the simple resched case.
- */
- irq_enter();
- sched_ttwu_pending();
- /*
- * Check if someone kicked us for doing the nohz idle load balance.
- */
- if (unlikely(got_nohz_idle_kick()) && !cpu_isolated(cpu)) {
- this_rq()->idle_balance = 1;
- raise_softirq_irqoff(SCHED_SOFTIRQ);
- }
- irq_exit();
- }
- static void ttwu_queue_remote(struct task_struct *p, int cpu, int wake_flags)
- {
- struct rq *rq = cpu_rq(cpu);
- p->sched_remote_wakeup = !!(wake_flags & WF_MIGRATED);
- if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list)) {
- if (!set_nr_if_polling(rq->idle))
- smp_send_reschedule(cpu);
- else
- trace_sched_wake_idle_without_ipi(cpu);
- }
- }
- void wake_up_if_idle(int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- struct rq_flags rf;
- rcu_read_lock();
- if (!is_idle_task(rcu_dereference(rq->curr)))
- goto out;
- if (set_nr_if_polling(rq->idle)) {
- trace_sched_wake_idle_without_ipi(cpu);
- } else {
- rq_lock_irqsave(rq, &rf);
- if (is_idle_task(rq->curr))
- smp_send_reschedule(cpu);
- /* Else CPU is not idle, do nothing here: */
- rq_unlock_irqrestore(rq, &rf);
- }
- out:
- rcu_read_unlock();
- }
- bool cpus_share_cache(int this_cpu, int that_cpu)
- {
- return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
- }
- #endif /* CONFIG_SMP */
- static void ttwu_queue(struct task_struct *p, int cpu, int wake_flags)
- {
- struct rq *rq = cpu_rq(cpu);
- struct rq_flags rf;
- #if defined(CONFIG_SMP)
- if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
- sched_clock_cpu(cpu); /* Sync clocks across CPUs */
- ttwu_queue_remote(p, cpu, wake_flags);
- return;
- }
- #endif
- rq_lock(rq, &rf);
- update_rq_clock(rq);
- ttwu_do_activate(rq, p, wake_flags, &rf);
- rq_unlock(rq, &rf);
- }
- /*
- * Notes on Program-Order guarantees on SMP systems.
- *
- * MIGRATION
- *
- * The basic program-order guarantee on SMP systems is that when a task [t]
- * migrates, all its activity on its old CPU [c0] happens-before any subsequent
- * execution on its new CPU [c1].
- *
- * For migration (of runnable tasks) this is provided by the following means:
- *
- * A) UNLOCK of the rq(c0)->lock scheduling out task t
- * B) migration for t is required to synchronize *both* rq(c0)->lock and
- * rq(c1)->lock (if not at the same time, then in that order).
- * C) LOCK of the rq(c1)->lock scheduling in task
- *
- * Transitivity guarantees that B happens after A and C after B.
- * Note: we only require RCpc transitivity.
- * Note: the CPU doing B need not be c0 or c1
- *
- * Example:
- *
- * CPU0 CPU1 CPU2
- *
- * LOCK rq(0)->lock
- * sched-out X
- * sched-in Y
- * UNLOCK rq(0)->lock
- *
- * LOCK rq(0)->lock // orders against CPU0
- * dequeue X
- * UNLOCK rq(0)->lock
- *
- * LOCK rq(1)->lock
- * enqueue X
- * UNLOCK rq(1)->lock
- *
- * LOCK rq(1)->lock // orders against CPU2
- * sched-out Z
- * sched-in X
- * UNLOCK rq(1)->lock
- *
- *
- * BLOCKING -- aka. SLEEP + WAKEUP
- *
- * For blocking we (obviously) need to provide the same guarantee as for
- * migration. However the means are completely different as there is no lock
- * chain to provide order. Instead we do:
- *
- * 1) smp_store_release(X->on_cpu, 0)
- * 2) smp_cond_load_acquire(!X->on_cpu)
- *
- * Example:
- *
- * CPU0 (schedule) CPU1 (try_to_wake_up) CPU2 (schedule)
- *
- * LOCK rq(0)->lock LOCK X->pi_lock
- * dequeue X
- * sched-out X
- * smp_store_release(X->on_cpu, 0);
- *
- * smp_cond_load_acquire(&X->on_cpu, !VAL);
- * X->state = WAKING
- * set_task_cpu(X,2)
- *
- * LOCK rq(2)->lock
- * enqueue X
- * X->state = RUNNING
- * UNLOCK rq(2)->lock
- *
- * LOCK rq(2)->lock // orders against CPU1
- * sched-out Z
- * sched-in X
- * UNLOCK rq(2)->lock
- *
- * UNLOCK X->pi_lock
- * UNLOCK rq(0)->lock
- *
- *
- * However; for wakeups there is a second guarantee we must provide, namely we
- * must observe the state that lead to our wakeup. That is, not only must our
- * task observe its own prior state, it must also observe the stores prior to
- * its wakeup.
- *
- * This means that any means of doing remote wakeups must order the CPU doing
- * the wakeup against the CPU the task is going to end up running on. This,
- * however, is already required for the regular Program-Order guarantee above,
- * since the waking CPU is the one issueing the ACQUIRE (smp_cond_load_acquire).
- *
- */
- #ifdef CONFIG_SMP
- #ifdef CONFIG_SCHED_WALT
- /* utility function to update walt signals at wakeup */
- static inline void walt_try_to_wake_up(struct task_struct *p)
- {
- struct rq *rq = cpu_rq(task_cpu(p));
- struct rq_flags rf;
- u64 wallclock;
- rq_lock_irqsave(rq, &rf);
- wallclock = walt_ktime_clock();
- walt_update_task_ravg(rq->curr, rq, TASK_UPDATE, wallclock, 0);
- walt_update_task_ravg(p, rq, TASK_WAKE, wallclock, 0);
- rq_unlock_irqrestore(rq, &rf);
- }
- #else
- #define walt_try_to_wake_up(a) {}
- #endif
- #endif
- /**
- * try_to_wake_up - wake up a thread
- * @p: the thread to be awakened
- * @state: the mask of task states that can be woken
- * @wake_flags: wake modifier flags (WF_*)
- * @sibling_count_hint: A hint at the number of threads that are being woken up
- * in this event.
- *
- * If (@state & @p->state) @p->state = TASK_RUNNING.
- *
- * If the task was not queued/runnable, also place it back on a runqueue.
- *
- * Atomic against schedule() which would dequeue a task, also see
- * set_current_state().
- *
- * Return: %true if @p->state changes (an actual wakeup was done),
- * %false otherwise.
- */
- static int
- try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags,
- int sibling_count_hint)
- {
- unsigned long flags;
- int cpu, success = 0;
- /*
- * If we are going to wake up a thread waiting for CONDITION we
- * need to ensure that CONDITION=1 done by the caller can not be
- * reordered with p->state check below. This pairs with mb() in
- * set_current_state() the waiting thread does.
- */
- raw_spin_lock_irqsave(&p->pi_lock, flags);
- smp_mb__after_spinlock();
- if (!(p->state & state))
- goto out;
- trace_sched_waking(p);
- /* We're going to change ->state: */
- success = 1;
- cpu = task_cpu(p);
- /*
- * Ensure we load p->on_rq _after_ p->state, otherwise it would
- * be possible to, falsely, observe p->on_rq == 0 and get stuck
- * in smp_cond_load_acquire() below.
- *
- * sched_ttwu_pending() try_to_wake_up()
- * [S] p->on_rq = 1; [L] P->state
- * UNLOCK rq->lock -----.
- * \
- * +--- RMB
- * schedule() /
- * LOCK rq->lock -----'
- * UNLOCK rq->lock
- *
- * [task p]
- * [S] p->state = UNINTERRUPTIBLE [L] p->on_rq
- *
- * Pairs with the UNLOCK+LOCK on rq->lock from the
- * last wakeup of our task and the schedule that got our task
- * current.
- */
- smp_rmb();
- if (p->on_rq && ttwu_remote(p, wake_flags))
- goto stat;
- #ifdef CONFIG_SMP
- /*
- * Ensure we load p->on_cpu _after_ p->on_rq, otherwise it would be
- * possible to, falsely, observe p->on_cpu == 0.
- *
- * One must be running (->on_cpu == 1) in order to remove oneself
- * from the runqueue.
- *
- * [S] ->on_cpu = 1; [L] ->on_rq
- * UNLOCK rq->lock
- * RMB
- * LOCK rq->lock
- * [S] ->on_rq = 0; [L] ->on_cpu
- *
- * Pairs with the full barrier implied in the UNLOCK+LOCK on rq->lock
- * from the consecutive calls to schedule(); the first switching to our
- * task, the second putting it to sleep.
- */
- smp_rmb();
- /*
- * If the owning (remote) CPU is still in the middle of schedule() with
- * this task as prev, wait until its done referencing the task.
- *
- * Pairs with the smp_store_release() in finish_lock_switch().
- *
- * This ensures that tasks getting woken will be fully ordered against
- * their previous state and preserve Program Order.
- */
- smp_cond_load_acquire(&p->on_cpu, !VAL);
- walt_try_to_wake_up(p);
- p->sched_contributes_to_load = !!task_contributes_to_load(p);
- p->state = TASK_WAKING;
- if (p->in_iowait) {
- delayacct_blkio_end(p);
- atomic_dec(&task_rq(p)->nr_iowait);
- }
- cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags,
- sibling_count_hint);
- if (task_cpu(p) != cpu) {
- wake_flags |= WF_MIGRATED;
- psi_ttwu_dequeue(p);
- set_task_cpu(p, cpu);
- }
- #else /* CONFIG_SMP */
- if (p->in_iowait) {
- delayacct_blkio_end(p);
- atomic_dec(&task_rq(p)->nr_iowait);
- }
- #endif /* CONFIG_SMP */
- ttwu_queue(p, cpu, wake_flags);
- stat:
- ttwu_stat(p, cpu, wake_flags);
- out:
- raw_spin_unlock_irqrestore(&p->pi_lock, flags);
- return success;
- }
- /**
- * try_to_wake_up_local - try to wake up a local task with rq lock held
- * @p: the thread to be awakened
- * @rf: request-queue flags for pinning
- *
- * Put @p on the run-queue if it's not already there. The caller must
- * ensure that this_rq() is locked, @p is bound to this_rq() and not
- * the current task.
- */
- static void try_to_wake_up_local(struct task_struct *p, struct rq_flags *rf)
- {
- struct rq *rq = task_rq(p);
- if (WARN_ON_ONCE(rq != this_rq()) ||
- WARN_ON_ONCE(p == current))
- return;
- lockdep_assert_held(&rq->lock);
- if (!raw_spin_trylock(&p->pi_lock)) {
- /*
- * This is OK, because current is on_cpu, which avoids it being
- * picked for load-balance and preemption/IRQs are still
- * disabled avoiding further scheduler activity on it and we've
- * not yet picked a replacement task.
- */
- rq_unlock(rq, rf);
- raw_spin_lock(&p->pi_lock);
- rq_relock(rq, rf);
- }
- if (!(p->state & TASK_NORMAL))
- goto out;
- trace_sched_waking(p);
- if (!task_on_rq_queued(p)) {
- u64 wallclock = walt_ktime_clock();
- walt_update_task_ravg(rq->curr, rq, TASK_UPDATE, wallclock, 0);
- walt_update_task_ravg(p, rq, TASK_WAKE, wallclock, 0);
- if (p->in_iowait) {
- delayacct_blkio_end(p);
- atomic_dec(&rq->nr_iowait);
- }
- ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_NOCLOCK);
- }
- ttwu_do_wakeup(rq, p, 0, rf);
- ttwu_stat(p, smp_processor_id(), 0);
- out:
- raw_spin_unlock(&p->pi_lock);
- }
- /**
- * wake_up_process - Wake up a specific process
- * @p: The process to be woken up.
- *
- * Attempt to wake up the nominated process and move it to the set of runnable
- * processes.
- *
- * Return: 1 if the process was woken up, 0 if it was already running.
- *
- * It may be assumed that this function implies a write memory barrier before
- * changing the task state if and only if any tasks are woken up.
- */
- int wake_up_process(struct task_struct *p)
- {
- return try_to_wake_up(p, TASK_NORMAL, 0, 1);
- }
- EXPORT_SYMBOL(wake_up_process);
- int wake_up_state(struct task_struct *p, unsigned int state)
- {
- return try_to_wake_up(p, state, 0, 1);
- }
- /*
- * Perform scheduler related setup for a newly forked process p.
- * p is forked by current.
- *
- * __sched_fork() is basic setup used by init_idle() too:
- */
- static void __sched_fork(unsigned long clone_flags, struct task_struct *p)
- {
- p->on_rq = 0;
- p->se.on_rq = 0;
- p->se.exec_start = 0;
- p->se.sum_exec_runtime = 0;
- p->se.prev_sum_exec_runtime = 0;
- p->se.nr_migrations = 0;
- p->se.vruntime = 0;
- #ifdef CONFIG_SCHED_WALT
- p->last_sleep_ts = 0;
- #endif
- INIT_LIST_HEAD(&p->se.group_node);
- walt_init_new_task_load(p);
- #ifdef CONFIG_FAIR_GROUP_SCHED
- p->se.cfs_rq = NULL;
- #endif
- #ifdef CONFIG_SCHEDSTATS
- /* Even if schedstat is disabled, there should not be garbage */
- memset(&p->se.statistics, 0, sizeof(p->se.statistics));
- #endif
- RB_CLEAR_NODE(&p->dl.rb_node);
- init_dl_task_timer(&p->dl);
- init_dl_inactive_task_timer(&p->dl);
- __dl_clear_params(p);
- INIT_LIST_HEAD(&p->rt.run_list);
- p->rt.timeout = 0;
- p->rt.time_slice = sched_rr_timeslice;
- p->rt.on_rq = 0;
- p->rt.on_list = 0;
- #ifdef CONFIG_PREEMPT_NOTIFIERS
- INIT_HLIST_HEAD(&p->preempt_notifiers);
- #endif
- #ifdef CONFIG_NUMA_BALANCING
- if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
- p->mm->numa_next_scan = jiffies + msecs_to_jiffies(sysctl_numa_balancing_scan_delay);
- p->mm->numa_scan_seq = 0;
- }
- if (clone_flags & CLONE_VM)
- p->numa_preferred_nid = current->numa_preferred_nid;
- else
- p->numa_preferred_nid = -1;
- p->node_stamp = 0ULL;
- p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
- p->numa_scan_period = sysctl_numa_balancing_scan_delay;
- p->numa_work.next = &p->numa_work;
- p->numa_faults = NULL;
- p->last_task_numa_placement = 0;
- p->last_sum_exec_runtime = 0;
- p->numa_group = NULL;
- #endif /* CONFIG_NUMA_BALANCING */
- }
- DEFINE_STATIC_KEY_FALSE(sched_numa_balancing);
- #ifdef CONFIG_NUMA_BALANCING
- void set_numabalancing_state(bool enabled)
- {
- if (enabled)
- static_branch_enable(&sched_numa_balancing);
- else
- static_branch_disable(&sched_numa_balancing);
- }
- #ifdef CONFIG_PROC_SYSCTL
- int sysctl_numa_balancing(struct ctl_table *table, int write,
- void __user *buffer, size_t *lenp, loff_t *ppos)
- {
- struct ctl_table t;
- int err;
- int state = static_branch_likely(&sched_numa_balancing);
- if (write && !capable(CAP_SYS_ADMIN))
- return -EPERM;
- t = *table;
- t.data = &state;
- err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
- if (err < 0)
- return err;
- if (write)
- set_numabalancing_state(state);
- return err;
- }
- #endif
- #endif
- #ifdef CONFIG_SCHEDSTATS
- DEFINE_STATIC_KEY_FALSE(sched_schedstats);
- static bool __initdata __sched_schedstats = false;
- static void set_schedstats(bool enabled)
- {
- if (enabled)
- static_branch_enable(&sched_schedstats);
- else
- static_branch_disable(&sched_schedstats);
- }
- void force_schedstat_enabled(void)
- {
- if (!schedstat_enabled()) {
- pr_info("kernel profiling enabled schedstats, disable via kernel.sched_schedstats.\n");
- static_branch_enable(&sched_schedstats);
- }
- }
- static int __init setup_schedstats(char *str)
- {
- int ret = 0;
- if (!str)
- goto out;
- /*
- * This code is called before jump labels have been set up, so we can't
- * change the static branch directly just yet. Instead set a temporary
- * variable so init_schedstats() can do it later.
- */
- if (!strcmp(str, "enable")) {
- __sched_schedstats = true;
- ret = 1;
- } else if (!strcmp(str, "disable")) {
- __sched_schedstats = false;
- ret = 1;
- }
- out:
- if (!ret)
- pr_warn("Unable to parse schedstats=\n");
- return ret;
- }
- __setup("schedstats=", setup_schedstats);
- static void __init init_schedstats(void)
- {
- set_schedstats(__sched_schedstats);
- }
- #ifdef CONFIG_PROC_SYSCTL
- int sysctl_schedstats(struct ctl_table *table, int write,
- void __user *buffer, size_t *lenp, loff_t *ppos)
- {
- struct ctl_table t;
- int err;
- int state = static_branch_likely(&sched_schedstats);
- if (write && !capable(CAP_SYS_ADMIN))
- return -EPERM;
- t = *table;
- t.data = &state;
- err = proc_dointvec_minmax(&t, write, buffer, lenp, ppos);
- if (err < 0)
- return err;
- if (write)
- set_schedstats(state);
- return err;
- }
- #endif /* CONFIG_PROC_SYSCTL */
- #else /* !CONFIG_SCHEDSTATS */
- static inline void init_schedstats(void) {}
- #endif /* CONFIG_SCHEDSTATS */
- /*
- * fork()/clone()-time setup:
- */
- int sched_fork(unsigned long clone_flags, struct task_struct *p)
- {
- unsigned long flags;
- bool reset;
- int cpu = get_cpu();
- __sched_fork(clone_flags, p);
- /*
- * We mark the process as NEW here. This guarantees that
- * nobody will actually run it, and a signal or other external
- * event cannot wake it up and insert it on the runqueue either.
- */
- p->state = TASK_NEW;
- /*
- * Make sure we do not leak PI boosting priority to the child.
- */
- p->prio = current->normal_prio;
- #ifdef CONFIG_MTK_TASK_TURBO
- if (unlikely(is_turbo_task(current)))
- set_user_nice(p, current->nice_backup);
- #endif
- /*
- * Revert to default priority/policy on fork if requested.
- */
- reset = p->sched_reset_on_fork;
- if (unlikely(reset)) {
- if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
- p->policy = SCHED_NORMAL;
- p->static_prio = NICE_TO_PRIO(0);
- p->rt_priority = 0;
- } else if (PRIO_TO_NICE(p->static_prio) < 0)
- p->static_prio = NICE_TO_PRIO(0);
- p->prio = p->normal_prio = __normal_prio(p);
- set_load_weight(p);
- /*
- * We don't need the reset flag anymore after the fork. It has
- * fulfilled its duty:
- */
- p->sched_reset_on_fork = 0;
- }
- if (dl_prio(p->prio)) {
- put_cpu();
- return -EAGAIN;
- } else if (rt_prio(p->prio)) {
- p->sched_class = &rt_sched_class;
- } else {
- p->sched_class = &fair_sched_class;
- #ifdef CONFIG_MTK_TASK_TURBO
- /* prio and backup should be aligned */
- p->nice_backup = PRIO_TO_NICE(p->prio);
- #endif
- }
- init_entity_runnable_average(&p->se);
- #ifdef CONFIG_MTK_SCHED_BOOST
- p->cpu_prefer = current->cpu_prefer;
- #ifdef CONFIG_MTK_TASK_TURBO
- if (unlikely(is_turbo_task(current)))
- p->cpu_prefer = 0; // SCHED_PREFER_NONE
- #endif
- #endif
- uclamp_fork(p, reset);
- /*
- * The child is not yet in the pid-hash so no cgroup attach races,
- * and the cgroup is pinned to this child due to cgroup_fork()
- * is ran before sched_fork().
- *
- * Silence PROVE_RCU.
- */
- raw_spin_lock_irqsave(&p->pi_lock, flags);
- /*
- * We're setting the CPU for the first time, we don't migrate,
- * so use __set_task_cpu().
- */
- __set_task_cpu(p, cpu);
- if (p->sched_class->task_fork)
- p->sched_class->task_fork(p);
- raw_spin_unlock_irqrestore(&p->pi_lock, flags);
- #ifdef CONFIG_SCHED_INFO
- if (likely(sched_info_on()))
- memset(&p->sched_info, 0, sizeof(p->sched_info));
- #endif
- #if defined(CONFIG_SMP)
- p->on_cpu = 0;
- #endif
- init_task_preempt_count(p);
- #ifdef CONFIG_SMP
- plist_node_init(&p->pushable_tasks, MAX_PRIO);
- RB_CLEAR_NODE(&p->pushable_dl_tasks);
- #endif
- put_cpu();
- return 0;
- }
- unsigned long to_ratio(u64 period, u64 runtime)
- {
- if (runtime == RUNTIME_INF)
- return BW_UNIT;
- /*
- * Doing this here saves a lot of checks in all
- * the calling paths, and returning zero seems
- * safe for them anyway.
- */
- if (period == 0)
- return 0;
- return div64_u64(runtime << BW_SHIFT, period);
- }
- /*
- * wake_up_new_task - wake up a newly created task for the first time.
- *
- * This function will do some initial scheduler statistics housekeeping
- * that must be done for every newly created context, then puts the task
- * on the runqueue and wakes it.
- */
- void wake_up_new_task(struct task_struct *p)
- {
- struct rq_flags rf;
- struct rq *rq;
- raw_spin_lock_irqsave(&p->pi_lock, rf.flags);
- walt_init_new_task_load(p);
- p->state = TASK_RUNNING;
- #ifdef CONFIG_SMP
- /*
- * Fork balancing, do it here and not earlier because:
- * - cpus_allowed can change in the fork path
- * - any previously selected CPU might disappear through hotplug
- *
- * Use __set_task_cpu() to avoid calling sched_class::migrate_task_rq,
- * as we're not fully set-up yet.
- */
- __set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0, 1));
- #endif
- rq = __task_rq_lock(p, &rf);
- update_rq_clock(rq);
- post_init_entity_util_avg(&p->se);
- p->last_enqueued_ts = ktime_get_ns();
- activate_task(rq, p, ENQUEUE_NOCLOCK);
- walt_mark_task_starting(p);
- p->on_rq = TASK_ON_RQ_QUEUED;
- trace_sched_wakeup_new(p);
- check_preempt_curr(rq, p, WF_FORK);
- #ifdef CONFIG_SMP
- if (p->sched_class->task_woken) {
- /*
- * Nothing relies on rq->lock after this, so its fine to
- * drop it.
- */
- rq_unpin_lock(rq, &rf);
- p->sched_class->task_woken(rq, p);
- rq_repin_lock(rq, &rf);
- }
- #endif
- task_rq_unlock(rq, p, &rf);
- }
- #ifdef CONFIG_PREEMPT_NOTIFIERS
- static struct static_key preempt_notifier_key = STATIC_KEY_INIT_FALSE;
- void preempt_notifier_inc(void)
- {
- static_key_slow_inc(&preempt_notifier_key);
- }
- EXPORT_SYMBOL_GPL(preempt_notifier_inc);
- void preempt_notifier_dec(void)
- {
- static_key_slow_dec(&preempt_notifier_key);
- }
- EXPORT_SYMBOL_GPL(preempt_notifier_dec);
- /**
- * preempt_notifier_register - tell me when current is being preempted & rescheduled
- * @notifier: notifier struct to register
- */
- void preempt_notifier_register(struct preempt_notifier *notifier)
- {
- if (!static_key_false(&preempt_notifier_key))
- WARN(1, "registering preempt_notifier while notifiers disabled\n");
- hlist_add_head(¬ifier->link, ¤t->preempt_notifiers);
- }
- EXPORT_SYMBOL_GPL(preempt_notifier_register);
- /**
- * preempt_notifier_unregister - no longer interested in preemption notifications
- * @notifier: notifier struct to unregister
- *
- * This is *not* safe to call from within a preemption notifier.
- */
- void preempt_notifier_unregister(struct preempt_notifier *notifier)
- {
- hlist_del(¬ifier->link);
- }
- EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
- static void __fire_sched_in_preempt_notifiers(struct task_struct *curr)
- {
- struct preempt_notifier *notifier;
- hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
- notifier->ops->sched_in(notifier, raw_smp_processor_id());
- }
- static __always_inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
- {
- if (static_key_false(&preempt_notifier_key))
- __fire_sched_in_preempt_notifiers(curr);
- }
- static void
- __fire_sched_out_preempt_notifiers(struct task_struct *curr,
- struct task_struct *next)
- {
- struct preempt_notifier *notifier;
- hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
- notifier->ops->sched_out(notifier, next);
- }
- static __always_inline void
- fire_sched_out_preempt_notifiers(struct task_struct *curr,
- struct task_struct *next)
- {
- if (static_key_false(&preempt_notifier_key))
- __fire_sched_out_preempt_notifiers(curr, next);
- }
- #else /* !CONFIG_PREEMPT_NOTIFIERS */
- static inline void fire_sched_in_preempt_notifiers(struct task_struct *curr)
- {
- }
- static inline void
- fire_sched_out_preempt_notifiers(struct task_struct *curr,
- struct task_struct *next)
- {
- }
- #endif /* CONFIG_PREEMPT_NOTIFIERS */
- /**
- * prepare_task_switch - prepare to switch tasks
- * @rq: the runqueue preparing to switch
- * @prev: the current task that is being switched out
- * @next: the task we are going to switch to.
- *
- * This is called with the rq lock held and interrupts off. It must
- * be paired with a subsequent finish_task_switch after the context
- * switch.
- *
- * prepare_task_switch sets up locking and calls architecture specific
- * hooks.
- */
- static inline void
- prepare_task_switch(struct rq *rq, struct task_struct *prev,
- struct task_struct *next)
- {
- kcov_prepare_switch(prev);
- sched_info_switch(rq, prev, next);
- perf_event_task_sched_out(prev, next);
- hook_ca_context_switch(rq, prev, next);
- fire_sched_out_preempt_notifiers(prev, next);
- prepare_lock_switch(rq, next);
- prepare_arch_switch(next);
- }
- /**
- * finish_task_switch - clean up after a task-switch
- * @prev: the thread we just switched away from.
- *
- * finish_task_switch must be called after the context switch, paired
- * with a prepare_task_switch call before the context switch.
- * finish_task_switch will reconcile locking set up by prepare_task_switch,
- * and do any other architecture-specific cleanup actions.
- *
- * Note that we may have delayed dropping an mm in context_switch(). If
- * so, we finish that here outside of the runqueue lock. (Doing it
- * with the lock held can cause deadlocks; see schedule() for
- * details.)
- *
- * The context switch have flipped the stack from under us and restored the
- * local variables which were saved when this task called schedule() in the
- * past. prev == current is still correct but we need to recalculate this_rq
- * because prev may have moved to another CPU.
- */
- static struct rq *finish_task_switch(struct task_struct *prev)
- __releases(rq->lock)
- {
- struct rq *rq = this_rq();
- struct mm_struct *mm = rq->prev_mm;
- long prev_state;
- /*
- * The previous task will have left us with a preempt_count of 2
- * because it left us after:
- *
- * schedule()
- * preempt_disable(); // 1
- * __schedule()
- * raw_spin_lock_irq(&rq->lock) // 2
- *
- * Also, see FORK_PREEMPT_COUNT.
- */
- if (WARN_ONCE(preempt_count() != 2*PREEMPT_DISABLE_OFFSET,
- "corrupted preempt_count: %s/%d/0x%x\n",
- current->comm, current->pid, preempt_count()))
- preempt_count_set(FORK_PREEMPT_COUNT);
- rq->prev_mm = NULL;
- /*
- * A task struct has one reference for the use as "current".
- * If a task dies, then it sets TASK_DEAD in tsk->state and calls
- * schedule one last time. The schedule call will never return, and
- * the scheduled task must drop that reference.
- *
- * We must observe prev->state before clearing prev->on_cpu (in
- * finish_lock_switch), otherwise a concurrent wakeup can get prev
- * running on another CPU and we could rave with its RUNNING -> DEAD
- * transition, resulting in a double drop.
- */
- prev_state = prev->state;
- vtime_task_switch(prev);
- perf_event_task_sched_in(prev, current);
- /*
- * The membarrier system call requires a full memory barrier
- * after storing to rq->curr, before going back to user-space.
- *
- * TODO: This smp_mb__after_unlock_lock can go away if PPC end
- * up adding a full barrier to switch_mm(), or we should figure
- * out if a smp_mb__after_unlock_lock is really the proper API
- * to use.
- */
- smp_mb__after_unlock_lock();
- finish_lock_switch(rq, prev);
- finish_arch_post_lock_switch();
- kcov_finish_switch(current);
- fire_sched_in_preempt_notifiers(current);
- if (mm)
- mmdrop(mm);
- if (unlikely(prev_state == TASK_DEAD)) {
- if (prev->sched_class->task_dead)
- prev->sched_class->task_dead(prev);
- /*
- * Remove function-return probe instances associated with this
- * task and put them back on the free list.
- */
- kprobe_flush_task(prev);
- /* Task is done with its stack. */
- put_task_stack(prev);
- put_task_struct(prev);
- }
- tick_nohz_task_switch();
- return rq;
- }
- #ifdef CONFIG_SMP
- /* rq->lock is NOT held, but preemption is disabled */
- static void __balance_callback(struct rq *rq)
- {
- struct callback_head *head, *next;
- void (*func)(struct rq *rq);
- unsigned long flags;
- raw_spin_lock_irqsave(&rq->lock, flags);
- head = rq->balance_callback;
- rq->balance_callback = NULL;
- while (head) {
- func = (void (*)(struct rq *))head->func;
- next = head->next;
- head->next = NULL;
- head = next;
- func(rq);
- }
- raw_spin_unlock_irqrestore(&rq->lock, flags);
- }
- static inline void balance_callback(struct rq *rq)
- {
- if (unlikely(rq->balance_callback))
- __balance_callback(rq);
- }
- #else
- static inline void balance_callback(struct rq *rq)
- {
- }
- #endif
- /**
- * schedule_tail - first thing a freshly forked thread must call.
- * @prev: the thread we just switched away from.
- */
- asmlinkage __visible void schedule_tail(struct task_struct *prev)
- __releases(rq->lock)
- {
- struct rq *rq;
- /*
- * New tasks start with FORK_PREEMPT_COUNT, see there and
- * finish_task_switch() for details.
- *
- * finish_task_switch() will drop rq->lock() and lower preempt_count
- * and the preempt_enable() will end up enabling preemption (on
- * PREEMPT_COUNT kernels).
- */
- rq = finish_task_switch(prev);
- balance_callback(rq);
- preempt_enable();
- if (current->set_child_tid)
- put_user(task_pid_vnr(current), current->set_child_tid);
- }
- /*
- * context_switch - switch to the new MM and the new thread's register state.
- */
- static __always_inline struct rq *
- context_switch(struct rq *rq, struct task_struct *prev,
- struct task_struct *next, struct rq_flags *rf)
- {
- struct mm_struct *mm, *oldmm;
- prepare_task_switch(rq, prev, next);
- mm = next->mm;
- oldmm = prev->active_mm;
- /*
- * For paravirt, this is coupled with an exit in switch_to to
- * combine the page table reload and the switch backend into
- * one hypercall.
- */
- arch_start_context_switch(prev);
- if (!mm) {
- next->active_mm = oldmm;
- mmgrab(oldmm);
- enter_lazy_tlb(oldmm, next);
- } else
- switch_mm_irqs_off(oldmm, mm, next);
- if (!prev->mm) {
- prev->active_mm = NULL;
- rq->prev_mm = oldmm;
- }
- rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
- /*
- * Since the runqueue lock will be released by the next
- * task (which is an invalid locking op but in the case
- * of the scheduler it's an obvious special-case), so we
- * do an early lockdep release here:
- */
- rq_unpin_lock(rq, rf);
- spin_release(&rq->lock.dep_map, 1, 0UL);
- /* Here we just switch the register state and the stack. */
- switch_to(prev, next, prev);
- barrier();
- return finish_task_switch(prev);
- }
- /*
- * nr_running and nr_context_switches:
- *
- * externally visible scheduler statistics: current number of runnable
- * threads, total number of context switches performed since bootup.
- */
- unsigned long nr_running(void)
- {
- unsigned long i, sum = 0;
- for_each_online_cpu(i)
- sum += cpu_rq(i)->nr_running;
- return sum;
- }
- /*
- * Check if only the current task is running on the CPU.
- *
- * Caution: this function does not check that the caller has disabled
- * preemption, thus the result might have a time-of-check-to-time-of-use
- * race. The caller is responsible to use it correctly, for example:
- *
- * - from a non-preemptable section (of course)
- *
- * - from a thread that is bound to a single CPU
- *
- * - in a loop with very short iterations (e.g. a polling loop)
- */
- bool single_task_running(void)
- {
- return raw_rq()->nr_running == 1;
- }
- EXPORT_SYMBOL(single_task_running);
- unsigned long long nr_context_switches(void)
- {
- int i;
- unsigned long long sum = 0;
- for_each_possible_cpu(i)
- sum += cpu_rq(i)->nr_switches;
- return sum;
- }
- /*
- * IO-wait accounting, and how its mostly bollocks (on SMP).
- *
- * The idea behind IO-wait account is to account the idle time that we could
- * have spend running if it were not for IO. That is, if we were to improve the
- * storage performance, we'd have a proportional reduction in IO-wait time.
- *
- * This all works nicely on UP, where, when a task blocks on IO, we account
- * idle time as IO-wait, because if the storage were faster, it could've been
- * running and we'd not be idle.
- *
- * This has been extended to SMP, by doing the same for each CPU. This however
- * is broken.
- *
- * Imagine for instance the case where two tasks block on one CPU, only the one
- * CPU will have IO-wait accounted, while the other has regular idle. Even
- * though, if the storage were faster, both could've ran at the same time,
- * utilising both CPUs.
- *
- * This means, that when looking globally, the current IO-wait accounting on
- * SMP is a lower bound, by reason of under accounting.
- *
- * Worse, since the numbers are provided per CPU, they are sometimes
- * interpreted per CPU, and that is nonsensical. A blocked task isn't strictly
- * associated with any one particular CPU, it can wake to another CPU than it
- * blocked on. This means the per CPU IO-wait number is meaningless.
- *
- * Task CPU affinities can make all that even more 'interesting'.
- */
- unsigned long nr_iowait(void)
- {
- unsigned long i, sum = 0;
- for_each_possible_cpu(i)
- sum += atomic_read(&cpu_rq(i)->nr_iowait);
- return sum;
- }
- /*
- * Consumers of these two interfaces, like for example the cpufreq menu
- * governor are using nonsensical data. Boosting frequency for a CPU that has
- * IO-wait which might not even end up running the task when it does become
- * runnable.
- */
- unsigned long nr_iowait_cpu(int cpu)
- {
- struct rq *this = cpu_rq(cpu);
- return atomic_read(&this->nr_iowait);
- }
- void get_iowait_load(unsigned long *nr_waiters, unsigned long *load)
- {
- struct rq *rq = this_rq();
- *nr_waiters = atomic_read(&rq->nr_iowait);
- *load = rq->load.weight;
- }
- #ifdef CONFIG_SMP
- /*
- * sched_exec - execve() is a valuable balancing opportunity, because at
- * this point the task has the smallest effective memory and cache footprint.
- */
- void sched_exec(void)
- {
- struct task_struct *p = current;
- unsigned long flags;
- int dest_cpu;
- raw_spin_lock_irqsave(&p->pi_lock, flags);
- dest_cpu = p->sched_class->select_task_rq(p, task_cpu(p), SD_BALANCE_EXEC, 0, 1);
- if (dest_cpu == smp_processor_id())
- goto unlock;
- if (likely(cpu_active(dest_cpu) && likely(!cpu_isolated(dest_cpu)))) {
- struct migration_arg arg = { p, dest_cpu };
- raw_spin_unlock_irqrestore(&p->pi_lock, flags);
- stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
- return;
- }
- unlock:
- raw_spin_unlock_irqrestore(&p->pi_lock, flags);
- }
- #endif
- DEFINE_PER_CPU(struct kernel_stat, kstat);
- DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
- EXPORT_PER_CPU_SYMBOL(kstat);
- EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
- /*
- * The function fair_sched_class.update_curr accesses the struct curr
- * and its field curr->exec_start; when called from task_sched_runtime(),
- * we observe a high rate of cache misses in practice.
- * Prefetching this data results in improved performance.
- */
- static inline void prefetch_curr_exec_start(struct task_struct *p)
- {
- #ifdef CONFIG_FAIR_GROUP_SCHED
- struct sched_entity *curr = (&p->se)->cfs_rq->curr;
- #else
- struct sched_entity *curr = (&task_rq(p)->cfs)->curr;
- #endif
- if (curr == NULL)
- return;
- prefetch(curr);
- prefetch(&curr->exec_start);
- }
- /*
- * Return accounted runtime for the task.
- * In case the task is currently running, return the runtime plus current's
- * pending runtime that have not been accounted yet.
- */
- unsigned long long task_sched_runtime(struct task_struct *p)
- {
- struct rq_flags rf;
- struct rq *rq;
- u64 ns;
- #if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
- /*
- * 64-bit doesn't need locks to atomically read a 64bit value.
- * So we have a optimization chance when the task's delta_exec is 0.
- * Reading ->on_cpu is racy, but this is ok.
- *
- * If we race with it leaving CPU, we'll take a lock. So we're correct.
- * If we race with it entering CPU, unaccounted time is 0. This is
- * indistinguishable from the read occurring a few cycles earlier.
- * If we see ->on_cpu without ->on_rq, the task is leaving, and has
- * been accounted, so we're correct here as well.
- */
- if (!p->on_cpu || !task_on_rq_queued(p))
- return p->se.sum_exec_runtime;
- #endif
- rq = task_rq_lock(p, &rf);
- /*
- * Must be ->curr _and_ ->on_rq. If dequeued, we would
- * project cycles that may never be accounted to this
- * thread, breaking clock_gettime().
- */
- if (task_current(rq, p) && task_on_rq_queued(p)) {
- prefetch_curr_exec_start(p);
- update_rq_clock(rq);
- p->sched_class->update_curr(rq);
- }
- ns = p->se.sum_exec_runtime;
- task_rq_unlock(rq, p, &rf);
- return ns;
- }
- /*
- * This function gets called by the timer code, with HZ frequency.
- * We call it with interrupts disabled.
- */
- void scheduler_tick(void)
- {
- int cpu = smp_processor_id();
- struct rq *rq = cpu_rq(cpu);
- struct task_struct *curr = rq->curr;
- struct rq_flags rf;
- sched_clock_tick();
- rq_lock(rq, &rf);
- walt_set_window_start(rq, &rf);
- walt_update_task_ravg(rq->curr, rq, TASK_UPDATE,
- walt_ktime_clock(), 0);
- update_rq_clock(rq);
- curr->sched_class->task_tick(rq, curr, 0);
- cpu_load_update_active(rq);
- calc_global_load_tick(rq);
- psi_task_tick(rq);
- rq_unlock(rq, &rf);
- perf_event_task_tick();
- #ifdef CONFIG_MTK_CACHE_CONTROL
- hook_ca_scheduler_tick(cpu);
- #endif
- #ifdef CONFIG_MTK_PERF_TRACKER
- perf_tracker(ktime_get_ns());
- #endif
- #ifdef CONFIG_SMP
- rq->idle_balance = idle_cpu(cpu);
- trigger_load_balance(rq);
- #endif
- rq_last_tick_reset(rq);
- #ifdef CONFIG_MTK_SCHED_RQAVG_KS
- sched_max_util_task_tracking();
- #endif
- #ifdef CONFIG_MTK_SCHED_CPULOAD
- cal_cpu_load(cpu);
- #endif
- if (curr->sched_class == &fair_sched_class)
- check_for_migration(rq, curr);
- #ifdef CONFIG_MTK_QOS_FRAMEWORK
- qos_prefetch_tick(cpu);
- #endif /* CONFIG_MTK_QOS_FRAMEWORK */
- }
- #ifdef CONFIG_NO_HZ_FULL
- /**
- * scheduler_tick_max_deferment
- *
- * Keep at least one tick per second when a single
- * active task is running because the scheduler doesn't
- * yet completely support full dynticks environment.
- *
- * This makes sure that uptime, CFS vruntime, load
- * balancing, etc... continue to move forward, even
- * with a very low granularity.
- *
- * Return: Maximum deferment in nanoseconds.
- */
- u64 scheduler_tick_max_deferment(void)
- {
- struct rq *rq = this_rq();
- unsigned long next, now = READ_ONCE(jiffies);
- next = rq->last_sched_tick + HZ;
- if (time_before_eq(next, now))
- return 0;
- return jiffies_to_nsecs(next - now);
- }
- #endif
- #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
- defined(CONFIG_PREEMPT_TRACER))
- /*
- * If the value passed in is equal to the current preempt count
- * then we just disabled preemption. Start timing the latency.
- */
- static inline void preempt_latency_start(int val)
- {
- if (preempt_count() == val) {
- unsigned long ip = get_lock_parent_ip();
- #ifdef CONFIG_DEBUG_PREEMPT
- current->preempt_disable_ip = ip;
- record_preempt_disable_ips(current);
- #endif
- trace_preempt_off(CALLER_ADDR0, ip);
- }
- }
- void preempt_count_add(int val)
- {
- #ifdef CONFIG_DEBUG_PREEMPT
- /*
- * Underflow?
- */
- if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
- return;
- #endif
- __preempt_count_add(val);
- #ifdef CONFIG_DEBUG_PREEMPT
- /*
- * Spinlock count overflowing soon?
- */
- DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
- PREEMPT_MASK - 10);
- #endif
- preempt_latency_start(val);
- }
- EXPORT_SYMBOL(preempt_count_add);
- NOKPROBE_SYMBOL(preempt_count_add);
- /*
- * If the value passed in equals to the current preempt count
- * then we just enabled preemption. Stop timing the latency.
- */
- static inline void preempt_latency_stop(int val)
- {
- if (preempt_count() == val)
- trace_preempt_on(CALLER_ADDR0, get_lock_parent_ip());
- }
- void preempt_count_sub(int val)
- {
- #ifdef CONFIG_DEBUG_PREEMPT
- /*
- * Underflow?
- */
- if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
- return;
- /*
- * Is the spinlock portion underflowing?
- */
- if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
- !(preempt_count() & PREEMPT_MASK)))
- return;
- #endif
- preempt_latency_stop(val);
- __preempt_count_sub(val);
- }
- EXPORT_SYMBOL(preempt_count_sub);
- NOKPROBE_SYMBOL(preempt_count_sub);
- #else
- static inline void preempt_latency_start(int val) { }
- static inline void preempt_latency_stop(int val) { }
- #endif
- static inline unsigned long get_preempt_disable_ip(struct task_struct *p)
- {
- #ifdef CONFIG_DEBUG_PREEMPT
- return p->preempt_disable_ip;
- #else
- return 0;
- #endif
- }
- /*
- * Print scheduling while atomic bug:
- */
- static noinline void __schedule_bug(struct task_struct *prev)
- {
- /* Save this before calling printk(), since that will clobber it */
- unsigned long preempt_disable_ip = get_preempt_disable_ip(current);
- int i = 0;
- if (oops_in_progress)
- return;
- printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
- prev->comm, prev->pid, preempt_count());
- debug_show_held_locks(prev);
- print_modules();
- if (irqs_disabled())
- print_irqtrace_events(prev);
- if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)
- && in_atomic_preempt_off()) {
- pr_err("Preemption disabled at:");
- print_ip_sym(preempt_disable_ip);
- dump_preempt_disable_ips(current);
- pr_cont("\n");
- }
- if (panic_on_warn)
- panic("scheduling while atomic\n");
- dump_stack();
- add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
- BUG_ON(1);
- }
- /*
- * Various schedule()-time debugging checks and statistics:
- */
- static inline void schedule_debug(struct task_struct *prev)
- {
- #ifdef CONFIG_SCHED_STACK_END_CHECK
- if (task_stack_end_corrupted(prev))
- panic("corrupted stack end detected inside scheduler\n");
- #endif
- if (unlikely(in_atomic_preempt_off())) {
- __schedule_bug(prev);
- preempt_count_set(PREEMPT_DISABLED);
- }
- rcu_sleep_check();
- profile_hit(SCHED_PROFILING, __builtin_return_address(0));
- schedstat_inc(this_rq()->sched_count);
- }
- /*
- * Pick up the highest-prio task:
- */
- static inline struct task_struct *
- pick_next_task(struct rq *rq, struct task_struct *prev, struct rq_flags *rf)
- {
- const struct sched_class *class;
- struct task_struct *p;
- /*
- * Optimization: we know that if all tasks are in the fair class we can
- * call that function directly, but only if the @prev task wasn't of a
- * higher scheduling class, because otherwise those loose the
- * opportunity to pull in more work from other CPUs.
- */
- if (likely((prev->sched_class == &idle_sched_class ||
- prev->sched_class == &fair_sched_class) &&
- rq->nr_running == rq->cfs.h_nr_running)) {
- p = fair_sched_class.pick_next_task(rq, prev, rf);
- if (unlikely(p == RETRY_TASK))
- goto again;
- /* Assumes fair_sched_class->next == idle_sched_class */
- if (unlikely(!p))
- p = idle_sched_class.pick_next_task(rq, prev, rf);
- return p;
- }
- again:
- for_each_class(class) {
- p = class->pick_next_task(rq, prev, rf);
- if (p) {
- if (unlikely(p == RETRY_TASK))
- goto again;
- return p;
- }
- }
- /* The idle class should always have a runnable task: */
- BUG();
- }
- /*
- * __schedule() is the main scheduler function.
- *
- * The main means of driving the scheduler and thus entering this function are:
- *
- * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
- *
- * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
- * paths. For example, see arch/x86/entry_64.S.
- *
- * To drive preemption between tasks, the scheduler sets the flag in timer
- * interrupt handler scheduler_tick().
- *
- * 3. Wakeups don't really cause entry into schedule(). They add a
- * task to the run-queue and that's it.
- *
- * Now, if the new task added to the run-queue preempts the current
- * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
- * called on the nearest possible occasion:
- *
- * - If the kernel is preemptible (CONFIG_PREEMPT=y):
- *
- * - in syscall or exception context, at the next outmost
- * preempt_enable(). (this might be as soon as the wake_up()'s
- * spin_unlock()!)
- *
- * - in IRQ context, return from interrupt-handler to
- * preemptible context
- *
- * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
- * then at the next:
- *
- * - cond_resched() call
- * - explicit schedule() call
- * - return from syscall or exception to user-space
- * - return from interrupt-handler to user-space
- *
- * WARNING: must be called with preemption disabled!
- */
- static void __sched notrace __schedule(bool preempt)
- {
- struct task_struct *prev, *next;
- unsigned long *switch_count;
- struct rq_flags rf;
- struct rq *rq;
- int cpu;
- u64 wallclock;
- cpu = smp_processor_id();
- rq = cpu_rq(cpu);
- prev = rq->curr;
- schedule_debug(prev);
- if (sched_feat(HRTICK))
- hrtick_clear(rq);
- local_irq_disable();
- rcu_note_context_switch(preempt);
- /*
- * Make sure that signal_pending_state()->signal_pending() below
- * can't be reordered with __set_current_state(TASK_INTERRUPTIBLE)
- * done by the caller to avoid the race with signal_wake_up().
- */
- rq_lock(rq, &rf);
- smp_mb__after_spinlock();
- /* Promote REQ to ACT */
- rq->clock_update_flags <<= 1;
- update_rq_clock(rq);
- switch_count = &prev->nivcsw;
- if (!preempt && prev->state) {
- if (unlikely(signal_pending_state(prev->state, prev))) {
- prev->state = TASK_RUNNING;
- } else {
- deactivate_task(rq, prev, DEQUEUE_SLEEP | DEQUEUE_NOCLOCK);
- prev->on_rq = 0;
- if (prev->in_iowait) {
- atomic_inc(&rq->nr_iowait);
- delayacct_blkio_start();
- }
- /*
- * If a worker went to sleep, notify and ask workqueue
- * whether it wants to wake up a task to maintain
- * concurrency.
- */
- if (prev->flags & PF_WQ_WORKER) {
- struct task_struct *to_wakeup;
- to_wakeup = wq_worker_sleeping(prev);
- if (to_wakeup)
- try_to_wake_up_local(to_wakeup, &rf);
- }
- }
- switch_count = &prev->nvcsw;
- }
- next = pick_next_task(rq, prev, &rf);
- wallclock = walt_ktime_clock();
- walt_update_task_ravg(prev, rq, PUT_PREV_TASK, wallclock, 0);
- walt_update_task_ravg(next, rq, PICK_NEXT_TASK, wallclock, 0);
- clear_tsk_need_resched(prev);
- clear_preempt_need_resched();
- if (likely(prev != next)) {
- #ifdef CONFIG_SCHED_WALT
- if (!prev->on_rq)
- prev->last_sleep_ts = wallclock;
- #endif
- rq->nr_switches++;
- rq->curr = next;
- /*
- * The membarrier system call requires each architecture
- * to have a full memory barrier after updating
- * rq->curr, before returning to user-space. For TSO
- * (e.g. x86), the architecture must provide its own
- * barrier in switch_mm(). For weakly ordered machines
- * for which spin_unlock() acts as a full memory
- * barrier, finish_lock_switch() in common code takes
- * care of this barrier. For weakly ordered machines for
- * which spin_unlock() acts as a RELEASE barrier (only
- * arm64 and PowerPC), arm64 has a full barrier in
- * switch_to(), and PowerPC has
- * smp_mb__after_unlock_lock() before
- * finish_lock_switch().
- */
- ++*switch_count;
- trace_sched_switch(preempt, prev, next);
- /* Also unlocks the rq: */
- rq = context_switch(rq, prev, next, &rf);
- } else {
- rq->clock_update_flags &= ~(RQCF_ACT_SKIP|RQCF_REQ_SKIP);
- rq_unlock_irq(rq, &rf);
- }
- balance_callback(rq);
- }
- void __noreturn do_task_dead(void)
- {
- /* Causes final put_task_struct in finish_task_switch(): */
- set_special_state(TASK_DEAD);
- /* Tell freezer to ignore us: */
- current->flags |= PF_NOFREEZE;
- __schedule(false);
- BUG();
- /* Avoid "noreturn function does return" - but don't continue if BUG() is a NOP: */
- for (;;)
- cpu_relax();
- }
- static inline void sched_submit_work(struct task_struct *tsk)
- {
- if (!tsk->state || tsk_is_pi_blocked(tsk))
- return;
- /*
- * If we are going to sleep and we have plugged IO queued,
- * make sure to submit it to avoid deadlocks.
- */
- if (blk_needs_flush_plug(tsk))
- blk_schedule_flush_plug(tsk);
- }
- asmlinkage __visible void __sched schedule(void)
- {
- struct task_struct *tsk = current;
- sched_submit_work(tsk);
- do {
- preempt_disable();
- __schedule(false);
- sched_preempt_enable_no_resched();
- } while (need_resched());
- }
- EXPORT_SYMBOL(schedule);
- /*
- * synchronize_rcu_tasks() makes sure that no task is stuck in preempted
- * state (have scheduled out non-voluntarily) by making sure that all
- * tasks have either left the run queue or have gone into user space.
- * As idle tasks do not do either, they must not ever be preempted
- * (schedule out non-voluntarily).
- *
- * schedule_idle() is similar to schedule_preempt_disable() except that it
- * never enables preemption because it does not call sched_submit_work().
- */
- void __sched schedule_idle(void)
- {
- /*
- * As this skips calling sched_submit_work(), which the idle task does
- * regardless because that function is a nop when the task is in a
- * TASK_RUNNING state, make sure this isn't used someplace that the
- * current task can be in any other state. Note, idle is always in the
- * TASK_RUNNING state.
- */
- WARN_ON_ONCE(current->state);
- do {
- __schedule(false);
- } while (need_resched());
- }
- #ifdef CONFIG_CONTEXT_TRACKING
- asmlinkage __visible void __sched schedule_user(void)
- {
- /*
- * If we come here after a random call to set_need_resched(),
- * or we have been woken up remotely but the IPI has not yet arrived,
- * we haven't yet exited the RCU idle mode. Do it here manually until
- * we find a better solution.
- *
- * NB: There are buggy callers of this function. Ideally we
- * should warn if prev_state != CONTEXT_USER, but that will trigger
- * too frequently to make sense yet.
- */
- enum ctx_state prev_state = exception_enter();
- schedule();
- exception_exit(prev_state);
- }
- #endif
- /**
- * schedule_preempt_disabled - called with preemption disabled
- *
- * Returns with preemption disabled. Note: preempt_count must be 1
- */
- void __sched schedule_preempt_disabled(void)
- {
- sched_preempt_enable_no_resched();
- schedule();
- preempt_disable();
- }
- static void __sched notrace preempt_schedule_common(void)
- {
- do {
- /*
- * Because the function tracer can trace preempt_count_sub()
- * and it also uses preempt_enable/disable_notrace(), if
- * NEED_RESCHED is set, the preempt_enable_notrace() called
- * by the function tracer will call this function again and
- * cause infinite recursion.
- *
- * Preemption must be disabled here before the function
- * tracer can trace. Break up preempt_disable() into two
- * calls. One to disable preemption without fear of being
- * traced. The other to still record the preemption latency,
- * which can also be traced by the function tracer.
- */
- preempt_disable_notrace();
- preempt_latency_start(1);
- __schedule(true);
- preempt_latency_stop(1);
- preempt_enable_no_resched_notrace();
- /*
- * Check again in case we missed a preemption opportunity
- * between schedule and now.
- */
- } while (need_resched());
- }
- #ifdef CONFIG_PREEMPT
- /*
- * this is the entry point to schedule() from in-kernel preemption
- * off of preempt_enable. Kernel preemptions off return from interrupt
- * occur there and call schedule directly.
- */
- asmlinkage __visible void __sched notrace preempt_schedule(void)
- {
- /*
- * If there is a non-zero preempt_count or interrupts are disabled,
- * we do not want to preempt the current task. Just return..
- */
- if (likely(!preemptible()))
- return;
- preempt_schedule_common();
- }
- NOKPROBE_SYMBOL(preempt_schedule);
- EXPORT_SYMBOL(preempt_schedule);
- /**
- * preempt_schedule_notrace - preempt_schedule called by tracing
- *
- * The tracing infrastructure uses preempt_enable_notrace to prevent
- * recursion and tracing preempt enabling caused by the tracing
- * infrastructure itself. But as tracing can happen in areas coming
- * from userspace or just about to enter userspace, a preempt enable
- * can occur before user_exit() is called. This will cause the scheduler
- * to be called when the system is still in usermode.
- *
- * To prevent this, the preempt_enable_notrace will use this function
- * instead of preempt_schedule() to exit user context if needed before
- * calling the scheduler.
- */
- asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
- {
- enum ctx_state prev_ctx;
- if (likely(!preemptible()))
- return;
- do {
- /*
- * Because the function tracer can trace preempt_count_sub()
- * and it also uses preempt_enable/disable_notrace(), if
- * NEED_RESCHED is set, the preempt_enable_notrace() called
- * by the function tracer will call this function again and
- * cause infinite recursion.
- *
- * Preemption must be disabled here before the function
- * tracer can trace. Break up preempt_disable() into two
- * calls. One to disable preemption without fear of being
- * traced. The other to still record the preemption latency,
- * which can also be traced by the function tracer.
- */
- preempt_disable_notrace();
- preempt_latency_start(1);
- /*
- * Needs preempt disabled in case user_exit() is traced
- * and the tracer calls preempt_enable_notrace() causing
- * an infinite recursion.
- */
- prev_ctx = exception_enter();
- __schedule(true);
- exception_exit(prev_ctx);
- preempt_latency_stop(1);
- preempt_enable_no_resched_notrace();
- } while (need_resched());
- }
- EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
- #endif /* CONFIG_PREEMPT */
- /*
- * this is the entry point to schedule() from kernel preemption
- * off of irq context.
- * Note, that this is called and return with irqs disabled. This will
- * protect us against recursive calling from irq.
- */
- asmlinkage __visible void __sched preempt_schedule_irq(void)
- {
- enum ctx_state prev_state;
- /* Catch callers which need to be fixed */
- BUG_ON(preempt_count() || !irqs_disabled());
- prev_state = exception_enter();
- do {
- preempt_disable();
- local_irq_enable();
- __schedule(true);
- local_irq_disable();
- sched_preempt_enable_no_resched();
- } while (need_resched());
- exception_exit(prev_state);
- }
- int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags,
- void *key)
- {
- return try_to_wake_up(curr->private, mode, wake_flags, 1);
- }
- EXPORT_SYMBOL(default_wake_function);
- #ifdef CONFIG_RT_MUTEXES
- static inline int __rt_effective_prio(struct task_struct *pi_task, int prio)
- {
- if (pi_task)
- prio = min(prio, pi_task->prio);
- return prio;
- }
- static inline int rt_effective_prio(struct task_struct *p, int prio)
- {
- struct task_struct *pi_task = rt_mutex_get_top_task(p);
- return __rt_effective_prio(pi_task, prio);
- }
- /*
- * rt_mutex_setprio - set the current priority of a task
- * @p: task to boost
- * @pi_task: donor task
- *
- * This function changes the 'effective' priority of a task. It does
- * not touch ->normal_prio like __setscheduler().
- *
- * Used by the rt_mutex code to implement priority inheritance
- * logic. Call site only calls if the priority of the task changed.
- */
- void rt_mutex_setprio(struct task_struct *p, struct task_struct *pi_task)
- {
- int prio, oldprio, queued, running, queue_flag =
- DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
- const struct sched_class *prev_class;
- struct rq_flags rf;
- struct rq *rq;
- #ifdef CONFIG_MTK_TASK_TURBO
- /* if rt boost, recover prio with backup */
- if (unlikely(is_turbo_task(p))) {
- if (!dl_prio(p->prio) && !rt_prio(p->prio)) {
- int backup = p->nice_backup;
- if (backup >= MIN_NICE && backup <= MAX_NICE) {
- p->static_prio = NICE_TO_PRIO(backup);
- p->prio = p->normal_prio = __normal_prio(p);
- set_load_weight(p);
- }
- }
- }
- #endif
- /* XXX used to be waiter->prio, not waiter->task->prio */
- prio = __rt_effective_prio(pi_task, p->normal_prio);
- /*
- * If nothing changed; bail early.
- */
- if (p->pi_top_task == pi_task && prio == p->prio && !dl_prio(prio))
- return;
- rq = __task_rq_lock(p, &rf);
- update_rq_clock(rq);
- /*
- * Set under pi_lock && rq->lock, such that the value can be used under
- * either lock.
- *
- * Note that there is loads of tricky to make this pointer cache work
- * right. rt_mutex_slowunlock()+rt_mutex_postunlock() work together to
- * ensure a task is de-boosted (pi_task is set to NULL) before the
- * task is allowed to run again (and can exit). This ensures the pointer
- * points to a blocked task -- which guaratees the task is present.
- */
- p->pi_top_task = pi_task;
- /*
- * For FIFO/RR we only need to set prio, if that matches we're done.
- */
- if (prio == p->prio && !dl_prio(prio))
- goto out_unlock;
- /*
- * Idle task boosting is a nono in general. There is one
- * exception, when PREEMPT_RT and NOHZ is active:
- *
- * The idle task calls get_next_timer_interrupt() and holds
- * the timer wheel base->lock on the CPU and another CPU wants
- * to access the timer (probably to cancel it). We can safely
- * ignore the boosting request, as the idle CPU runs this code
- * with interrupts disabled and will complete the lock
- * protected section without being interrupted. So there is no
- * real need to boost.
- */
- if (unlikely(p == rq->idle)) {
- WARN_ON(p != rq->curr);
- WARN_ON(p->pi_blocked_on);
- goto out_unlock;
- }
- trace_sched_pi_setprio(p, pi_task);
- oldprio = p->prio;
- if (oldprio == prio)
- queue_flag &= ~DEQUEUE_MOVE;
- prev_class = p->sched_class;
- queued = task_on_rq_queued(p);
- running = task_current(rq, p);
- if (queued)
- dequeue_task(rq, p, queue_flag);
- if (running)
- put_prev_task(rq, p);
- /*
- * Boosting condition are:
- * 1. -rt task is running and holds mutex A
- * --> -dl task blocks on mutex A
- *
- * 2. -dl task is running and holds mutex A
- * --> -dl task blocks on mutex A and could preempt the
- * running task
- */
- if (dl_prio(prio)) {
- if (!dl_prio(p->normal_prio) ||
- (pi_task && dl_prio(pi_task->prio) &&
- dl_entity_preempt(&pi_task->dl, &p->dl))) {
- p->dl.dl_boosted = 1;
- queue_flag |= ENQUEUE_REPLENISH;
- } else
- p->dl.dl_boosted = 0;
- p->sched_class = &dl_sched_class;
- } else if (rt_prio(prio)) {
- if (dl_prio(oldprio))
- p->dl.dl_boosted = 0;
- if (oldprio < prio)
- queue_flag |= ENQUEUE_HEAD;
- p->sched_class = &rt_sched_class;
- } else {
- if (dl_prio(oldprio))
- p->dl.dl_boosted = 0;
- if (rt_prio(oldprio))
- p->rt.timeout = 0;
- p->sched_class = &fair_sched_class;
- }
- p->prio = prio;
- if (queued)
- enqueue_task(rq, p, queue_flag);
- if (running)
- set_curr_task(rq, p);
- check_class_changed(rq, p, prev_class, oldprio);
- out_unlock:
- /* Avoid rq from going away on us: */
- preempt_disable();
- __task_rq_unlock(rq, &rf);
- balance_callback(rq);
- preempt_enable();
- }
- #else
- static inline int rt_effective_prio(struct task_struct *p, int prio)
- {
- return prio;
- }
- #endif
- #ifdef CONFIG_MTK_TASK_TURBO
- #define task_turbo_nice(nice) (nice == 0xbeef || nice == 0xbeee)
- #endif
- void set_user_nice(struct task_struct *p, long nice)
- {
- bool queued, running;
- int old_prio, delta;
- struct rq_flags rf;
- struct rq *rq;
- #ifdef CONFIG_MTK_TASK_TURBO
- if ((nice < MIN_NICE || nice > MAX_NICE) && !task_turbo_nice(nice))
- return;
- #else
- if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
- return;
- #endif
- /*
- * We have to be careful, if called from sys_setpriority(),
- * the task might be in the middle of scheduling on another CPU.
- */
- rq = task_rq_lock(p, &rf);
- update_rq_clock(rq);
- #ifdef CONFIG_MTK_TASK_TURBO
- /* for general use, backup it */
- if (!task_turbo_nice(nice))
- p->nice_backup = nice;
- if (is_turbo_task(p)) {
- nice = rlimit_to_nice(task_rlimit(p, RLIMIT_NICE));
- if (unlikely(nice > MAX_NICE)) {
- printk_deferred("[name:task-turbo&]pid=%d RLIMIT_NICE=%ld is not set\n",
- p->pid, nice);
- nice = p->nice_backup;
- }
- }
- else
- nice = p->nice_backup;
- trace_sched_set_user_nice(p, nice, is_turbo_task(p));
- #endif
- /*
- * The RT priorities are set via sched_setscheduler(), but we still
- * allow the 'normal' nice value to be set - but as expected
- * it wont have any effect on scheduling until the task is
- * SCHED_DEADLINE, SCHED_FIFO or SCHED_RR:
- */
- if (task_has_dl_policy(p) || task_has_rt_policy(p)) {
- p->static_prio = NICE_TO_PRIO(nice);
- goto out_unlock;
- }
- queued = task_on_rq_queued(p);
- running = task_current(rq, p);
- if (queued)
- dequeue_task(rq, p, DEQUEUE_SAVE | DEQUEUE_NOCLOCK);
- if (running)
- put_prev_task(rq, p);
- p->static_prio = NICE_TO_PRIO(nice);
- set_load_weight(p);
- old_prio = p->prio;
- p->prio = effective_prio(p);
- delta = p->prio - old_prio;
- if (queued) {
- enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
- /*
- * If the task increased its priority or is running and
- * lowered its priority, then reschedule its CPU:
- */
- if (delta < 0 || (delta > 0 && task_running(rq, p)))
- resched_curr(rq);
- }
- if (running)
- set_curr_task(rq, p);
- out_unlock:
- task_rq_unlock(rq, p, &rf);
- }
- EXPORT_SYMBOL(set_user_nice);
- /*
- * can_nice - check if a task can reduce its nice value
- * @p: task
- * @nice: nice value
- */
- int can_nice(const struct task_struct *p, const int nice)
- {
- /* Convert nice value [19,-20] to rlimit style value [1,40]: */
- int nice_rlim = nice_to_rlimit(nice);
- return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
- capable(CAP_SYS_NICE));
- }
- #ifdef __ARCH_WANT_SYS_NICE
- /*
- * sys_nice - change the priority of the current process.
- * @increment: priority increment
- *
- * sys_setpriority is a more generic, but much slower function that
- * does similar things.
- */
- SYSCALL_DEFINE1(nice, int, increment)
- {
- long nice, retval;
- /*
- * Setpriority might change our priority at the same moment.
- * We don't have to worry. Conceptually one call occurs first
- * and we have a single winner.
- */
- increment = clamp(increment, -NICE_WIDTH, NICE_WIDTH);
- nice = task_nice(current) + increment;
- nice = clamp_val(nice, MIN_NICE, MAX_NICE);
- if (increment < 0 && !can_nice(current, nice))
- return -EPERM;
- retval = security_task_setnice(current, nice);
- if (retval)
- return retval;
- set_user_nice(current, nice);
- return 0;
- }
- #endif
- /**
- * task_prio - return the priority value of a given task.
- * @p: the task in question.
- *
- * Return: The priority value as seen by users in /proc.
- * RT tasks are offset by -200. Normal tasks are centered
- * around 0, value goes from -16 to +15.
- */
- int task_prio(const struct task_struct *p)
- {
- return p->prio - MAX_RT_PRIO;
- }
- /**
- * idle_cpu - is a given CPU idle currently?
- * @cpu: the processor in question.
- *
- * Return: 1 if the CPU is currently idle. 0 otherwise.
- */
- int idle_cpu(int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- if (rq->curr != rq->idle)
- return 0;
- if (rq->nr_running)
- return 0;
- #ifdef CONFIG_SMP
- if (!llist_empty(&rq->wake_list))
- return 0;
- #endif
- return 1;
- }
- /**
- * idle_task - return the idle task for a given CPU.
- * @cpu: the processor in question.
- *
- * Return: The idle task for the CPU @cpu.
- */
- struct task_struct *idle_task(int cpu)
- {
- return cpu_rq(cpu)->idle;
- }
- /**
- * find_process_by_pid - find a process with a matching PID value.
- * @pid: the pid in question.
- *
- * The task of @pid, if found. %NULL otherwise.
- */
- static struct task_struct *find_process_by_pid(pid_t pid)
- {
- return pid ? find_task_by_vpid(pid) : current;
- }
- /*
- * sched_setparam() passes in -1 for its policy, to let the functions
- * it calls know not to change it.
- */
- #define SETPARAM_POLICY -1
- static void __setscheduler_params(struct task_struct *p,
- const struct sched_attr *attr)
- {
- int policy = attr->sched_policy;
- if (policy == SETPARAM_POLICY)
- policy = p->policy;
- /* Replace SCHED_FIFO with SCHED_RR to reduce latency */
- p->policy = policy == SCHED_FIFO ? SCHED_RR : policy;
- if (dl_policy(policy))
- __setparam_dl(p, attr);
- else if (fair_policy(policy))
- p->static_prio = NICE_TO_PRIO(attr->sched_nice);
- /*
- * __sched_setscheduler() ensures attr->sched_priority == 0 when
- * !rt_policy. Always setting this ensures that things like
- * getparam()/getattr() don't report silly values for !rt tasks.
- */
- p->rt_priority = attr->sched_priority;
- p->normal_prio = normal_prio(p);
- set_load_weight(p);
- }
- /* Actually do priority change: must hold pi & rq lock. */
- static void __setscheduler(struct rq *rq, struct task_struct *p,
- const struct sched_attr *attr, bool keep_boost)
- {
- __setscheduler_params(p, attr);
- /*
- * Keep a potential priority boosting if called from
- * sched_setscheduler().
- */
- p->prio = normal_prio(p);
- if (keep_boost)
- p->prio = rt_effective_prio(p, p->prio);
- if (dl_prio(p->prio))
- p->sched_class = &dl_sched_class;
- else if (rt_prio(p->prio))
- p->sched_class = &rt_sched_class;
- #ifdef CONFIG_MTK_TASK_TURBO
- else {
- p->sched_class = &fair_sched_class;
- p->nice_backup = PRIO_TO_NICE(p->prio);
- }
- #else
- else
- p->sched_class = &fair_sched_class;
- #endif
- }
- /*
- * Check the target process has a UID that matches the current process's:
- */
- static bool check_same_owner(struct task_struct *p)
- {
- const struct cred *cred = current_cred(), *pcred;
- bool match;
- rcu_read_lock();
- pcred = __task_cred(p);
- match = (uid_eq(cred->euid, pcred->euid) ||
- uid_eq(cred->euid, pcred->uid));
- rcu_read_unlock();
- return match;
- }
- static int __sched_setscheduler(struct task_struct *p,
- const struct sched_attr *attr,
- bool user, bool pi)
- {
- int newprio = dl_policy(attr->sched_policy) ? MAX_DL_PRIO - 1 :
- MAX_RT_PRIO - 1 - attr->sched_priority;
- int retval, oldprio, oldpolicy = -1, queued, running;
- int new_effective_prio, policy = attr->sched_policy;
- const struct sched_class *prev_class;
- struct rq_flags rf;
- int reset_on_fork;
- int queue_flags = DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
- struct rq *rq;
- /* The pi code expects interrupts enabled */
- BUG_ON(pi && in_interrupt());
- recheck:
- /* Double check policy once rq lock held: */
- if (policy < 0) {
- reset_on_fork = p->sched_reset_on_fork;
- policy = oldpolicy = p->policy;
- } else {
- reset_on_fork = !!(attr->sched_flags & SCHED_FLAG_RESET_ON_FORK);
- if (!valid_policy(policy))
- return -EINVAL;
- }
- if (attr->sched_flags &
- ~(SCHED_FLAG_RESET_ON_FORK | SCHED_FLAG_RECLAIM))
- return -EINVAL;
- /*
- * Valid priorities for SCHED_FIFO and SCHED_RR are
- * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
- * SCHED_BATCH and SCHED_IDLE is 0.
- */
- if ((p->mm && attr->sched_priority > MAX_USER_RT_PRIO-1) ||
- (!p->mm && attr->sched_priority > MAX_RT_PRIO-1))
- return -EINVAL;
- if ((dl_policy(policy) && !__checkparam_dl(attr)) ||
- (rt_policy(policy) != (attr->sched_priority != 0)))
- return -EINVAL;
- /*
- * Allow unprivileged RT tasks to decrease priority:
- */
- if (user && !capable(CAP_SYS_NICE)) {
- if (fair_policy(policy)) {
- if (attr->sched_nice < task_nice(p) &&
- !can_nice(p, attr->sched_nice))
- return -EPERM;
- }
- if (rt_policy(policy)) {
- unsigned long rlim_rtprio =
- task_rlimit(p, RLIMIT_RTPRIO);
- /* Can't set/change the rt policy: */
- if (policy != p->policy && !rlim_rtprio)
- return -EPERM;
- /* Can't increase priority: */
- if (attr->sched_priority > p->rt_priority &&
- attr->sched_priority > rlim_rtprio)
- return -EPERM;
- }
- /*
- * Can't set/change SCHED_DEADLINE policy at all for now
- * (safest behavior); in the future we would like to allow
- * unprivileged DL tasks to increase their relative deadline
- * or reduce their runtime (both ways reducing utilization)
- */
- if (dl_policy(policy))
- return -EPERM;
- /*
- * Treat SCHED_IDLE as nice 20. Only allow a switch to
- * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
- */
- if (idle_policy(p->policy) && !idle_policy(policy)) {
- if (!can_nice(p, task_nice(p)))
- return -EPERM;
- }
- /* Can't change other user's priorities: */
- if (!check_same_owner(p))
- return -EPERM;
- /* Normal users shall not reset the sched_reset_on_fork flag: */
- if (p->sched_reset_on_fork && !reset_on_fork)
- return -EPERM;
- }
- if (user) {
- retval = security_task_setscheduler(p);
- if (retval)
- return retval;
- }
- /*
- * Make sure no PI-waiters arrive (or leave) while we are
- * changing the priority of the task:
- *
- * To be able to change p->policy safely, the appropriate
- * runqueue lock must be held.
- */
- rq = task_rq_lock(p, &rf);
- update_rq_clock(rq);
- /*
- * Changing the policy of the stop threads its a very bad idea:
- */
- if (p == rq->stop) {
- task_rq_unlock(rq, p, &rf);
- return -EINVAL;
- }
- /*
- * If not changing anything there's no need to proceed further,
- * but store a possible modification of reset_on_fork.
- */
- if (unlikely(policy == p->policy)) {
- if (fair_policy(policy) && attr->sched_nice != task_nice(p))
- goto change;
- if (rt_policy(policy) && attr->sched_priority != p->rt_priority)
- goto change;
- if (dl_policy(policy) && dl_param_changed(p, attr))
- goto change;
- p->sched_reset_on_fork = reset_on_fork;
- task_rq_unlock(rq, p, &rf);
- return 0;
- }
- change:
- if (user) {
- #ifdef CONFIG_RT_GROUP_SCHED
- /*
- * Do not allow realtime tasks into groups that have no runtime
- * assigned.
- */
- if (rt_bandwidth_enabled() && rt_policy(policy) &&
- task_group(p)->rt_bandwidth.rt_runtime == 0 &&
- !task_group_is_autogroup(task_group(p))) {
- task_rq_unlock(rq, p, &rf);
- return -EPERM;
- }
- #endif
- #ifdef CONFIG_SMP
- if (dl_bandwidth_enabled() && dl_policy(policy)) {
- cpumask_t *span = rq->rd->span;
- /*
- * Don't allow tasks with an affinity mask smaller than
- * the entire root_domain to become SCHED_DEADLINE. We
- * will also fail if there's no bandwidth available.
- */
- if (!cpumask_subset(span, &p->cpus_allowed) ||
- rq->rd->dl_bw.bw == 0) {
- task_rq_unlock(rq, p, &rf);
- return -EPERM;
- }
- }
- #endif
- }
- /* Re-check policy now with rq lock held: */
- if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
- policy = oldpolicy = -1;
- task_rq_unlock(rq, p, &rf);
- goto recheck;
- }
- /*
- * If setscheduling to SCHED_DEADLINE (or changing the parameters
- * of a SCHED_DEADLINE task) we need to check if enough bandwidth
- * is available.
- */
- if ((dl_policy(policy) || dl_task(p)) && sched_dl_overflow(p, policy, attr)) {
- task_rq_unlock(rq, p, &rf);
- return -EBUSY;
- }
- p->sched_reset_on_fork = reset_on_fork;
- oldprio = p->prio;
- if (pi) {
- /*
- * Take priority boosted tasks into account. If the new
- * effective priority is unchanged, we just store the new
- * normal parameters and do not touch the scheduler class and
- * the runqueue. This will be done when the task deboost
- * itself.
- */
- new_effective_prio = rt_effective_prio(p, newprio);
- if (new_effective_prio == oldprio)
- queue_flags &= ~DEQUEUE_MOVE;
- }
- queued = task_on_rq_queued(p);
- running = task_current(rq, p);
- if (queued)
- dequeue_task(rq, p, queue_flags);
- if (running)
- put_prev_task(rq, p);
- prev_class = p->sched_class;
- __setscheduler(rq, p, attr, pi);
- if (queued) {
- /*
- * We enqueue to tail when the priority of a task is
- * increased (user space view).
- */
- if (oldprio < p->prio)
- queue_flags |= ENQUEUE_HEAD;
- enqueue_task(rq, p, queue_flags);
- }
- if (running)
- set_curr_task(rq, p);
- check_class_changed(rq, p, prev_class, oldprio);
- /* Avoid rq from going away on us: */
- preempt_disable();
- task_rq_unlock(rq, p, &rf);
- if (pi)
- rt_mutex_adjust_pi(p);
- /* Run balance callbacks after we've adjusted the PI chain: */
- balance_callback(rq);
- preempt_enable();
- return 0;
- }
- static int _sched_setscheduler(struct task_struct *p, int policy,
- const struct sched_param *param, bool check)
- {
- struct sched_attr attr = {
- .sched_policy = policy,
- .sched_priority = param->sched_priority,
- .sched_nice = PRIO_TO_NICE(p->static_prio),
- };
- /* Fixup the legacy SCHED_RESET_ON_FORK hack. */
- if ((policy != SETPARAM_POLICY) && (policy & SCHED_RESET_ON_FORK)) {
- attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
- policy &= ~SCHED_RESET_ON_FORK;
- attr.sched_policy = policy;
- }
- return __sched_setscheduler(p, &attr, check, true);
- }
- /**
- * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
- * @p: the task in question.
- * @policy: new policy.
- * @param: structure containing the new RT priority.
- *
- * Return: 0 on success. An error code otherwise.
- *
- * NOTE that the task may be already dead.
- */
- int sched_setscheduler(struct task_struct *p, int policy,
- const struct sched_param *param)
- {
- return _sched_setscheduler(p, policy, param, true);
- }
- EXPORT_SYMBOL_GPL(sched_setscheduler);
- int sched_setattr(struct task_struct *p, const struct sched_attr *attr)
- {
- return __sched_setscheduler(p, attr, true, true);
- }
- EXPORT_SYMBOL_GPL(sched_setattr);
- /**
- * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
- * @p: the task in question.
- * @policy: new policy.
- * @param: structure containing the new RT priority.
- *
- * Just like sched_setscheduler, only don't bother checking if the
- * current context has permission. For example, this is needed in
- * stop_machine(): we create temporary high priority worker threads,
- * but our caller might not have that capability.
- *
- * Return: 0 on success. An error code otherwise.
- */
- int sched_setscheduler_nocheck(struct task_struct *p, int policy,
- const struct sched_param *param)
- {
- return _sched_setscheduler(p, policy, param, false);
- }
- EXPORT_SYMBOL_GPL(sched_setscheduler_nocheck);
- static int
- do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
- {
- struct sched_param lparam;
- struct task_struct *p;
- int retval;
- if (!param || pid < 0)
- return -EINVAL;
- if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
- return -EFAULT;
- rcu_read_lock();
- retval = -ESRCH;
- p = find_process_by_pid(pid);
- if (p != NULL)
- retval = sched_setscheduler(p, policy, &lparam);
- rcu_read_unlock();
- return retval;
- }
- /*
- * Mimics kernel/events/core.c perf_copy_attr().
- */
- static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *attr)
- {
- u32 size;
- int ret;
- if (!access_ok(VERIFY_WRITE, uattr, SCHED_ATTR_SIZE_VER0))
- return -EFAULT;
- /* Zero the full structure, so that a short copy will be nice: */
- memset(attr, 0, sizeof(*attr));
- ret = get_user(size, &uattr->size);
- if (ret)
- return ret;
- /* Bail out on silly large: */
- if (size > PAGE_SIZE)
- goto err_size;
- /* ABI compatibility quirk: */
- if (!size)
- size = SCHED_ATTR_SIZE_VER0;
- if (size < SCHED_ATTR_SIZE_VER0)
- goto err_size;
- /*
- * If we're handed a bigger struct than we know of,
- * ensure all the unknown bits are 0 - i.e. new
- * user-space does not rely on any kernel feature
- * extensions we dont know about yet.
- */
- if (size > sizeof(*attr)) {
- unsigned char __user *addr;
- unsigned char __user *end;
- unsigned char val;
- addr = (void __user *)uattr + sizeof(*attr);
- end = (void __user *)uattr + size;
- for (; addr < end; addr++) {
- ret = get_user(val, addr);
- if (ret)
- return ret;
- if (val)
- goto err_size;
- }
- size = sizeof(*attr);
- }
- ret = copy_from_user(attr, uattr, size);
- if (ret)
- return -EFAULT;
- /*
- * XXX: Do we want to be lenient like existing syscalls; or do we want
- * to be strict and return an error on out-of-bounds values?
- */
- attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
- return 0;
- err_size:
- put_user(sizeof(*attr), &uattr->size);
- return -E2BIG;
- }
- /**
- * sys_sched_setscheduler - set/change the scheduler policy and RT priority
- * @pid: the pid in question.
- * @policy: new policy.
- * @param: structure containing the new RT priority.
- *
- * Return: 0 on success. An error code otherwise.
- */
- SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy, struct sched_param __user *, param)
- {
- if (policy < 0)
- return -EINVAL;
- return do_sched_setscheduler(pid, policy, param);
- }
- /**
- * sys_sched_setparam - set/change the RT priority of a thread
- * @pid: the pid in question.
- * @param: structure containing the new RT priority.
- *
- * Return: 0 on success. An error code otherwise.
- */
- SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
- {
- return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
- }
- /**
- * sys_sched_setattr - same as above, but with extended sched_attr
- * @pid: the pid in question.
- * @uattr: structure containing the extended parameters.
- * @flags: for future extension.
- */
- SYSCALL_DEFINE3(sched_setattr, pid_t, pid, struct sched_attr __user *, uattr,
- unsigned int, flags)
- {
- struct sched_attr attr;
- struct task_struct *p;
- int retval;
- if (!uattr || pid < 0 || flags)
- return -EINVAL;
- retval = sched_copy_attr(uattr, &attr);
- if (retval)
- return retval;
- if ((int)attr.sched_policy < 0)
- return -EINVAL;
- rcu_read_lock();
- retval = -ESRCH;
- p = find_process_by_pid(pid);
- if (p != NULL)
- retval = sched_setattr(p, &attr);
- rcu_read_unlock();
- return retval;
- }
- /**
- * sys_sched_getscheduler - get the policy (scheduling class) of a thread
- * @pid: the pid in question.
- *
- * Return: On success, the policy of the thread. Otherwise, a negative error
- * code.
- */
- SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
- {
- struct task_struct *p;
- int retval;
- if (pid < 0)
- return -EINVAL;
- retval = -ESRCH;
- rcu_read_lock();
- p = find_process_by_pid(pid);
- if (p) {
- retval = security_task_getscheduler(p);
- if (!retval)
- retval = p->policy
- | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
- }
- rcu_read_unlock();
- return retval;
- }
- /**
- * sys_sched_getparam - get the RT priority of a thread
- * @pid: the pid in question.
- * @param: structure containing the RT priority.
- *
- * Return: On success, 0 and the RT priority is in @param. Otherwise, an error
- * code.
- */
- SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
- {
- struct sched_param lp = { .sched_priority = 0 };
- struct task_struct *p;
- int retval;
- if (!param || pid < 0)
- return -EINVAL;
- rcu_read_lock();
- p = find_process_by_pid(pid);
- retval = -ESRCH;
- if (!p)
- goto out_unlock;
- retval = security_task_getscheduler(p);
- if (retval)
- goto out_unlock;
- if (task_has_rt_policy(p))
- lp.sched_priority = p->rt_priority;
- rcu_read_unlock();
- /*
- * This one might sleep, we cannot do it with a spinlock held ...
- */
- retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
- return retval;
- out_unlock:
- rcu_read_unlock();
- return retval;
- }
- static int sched_read_attr(struct sched_attr __user *uattr,
- struct sched_attr *attr,
- unsigned int usize)
- {
- int ret;
- if (!access_ok(VERIFY_WRITE, uattr, usize))
- return -EFAULT;
- /*
- * If we're handed a smaller struct than we know of,
- * ensure all the unknown bits are 0 - i.e. old
- * user-space does not get uncomplete information.
- */
- if (usize < sizeof(*attr)) {
- unsigned char *addr;
- unsigned char *end;
- addr = (void *)attr + usize;
- end = (void *)attr + sizeof(*attr);
- for (; addr < end; addr++) {
- if (*addr)
- return -EFBIG;
- }
- attr->size = usize;
- }
- ret = copy_to_user(uattr, attr, attr->size);
- if (ret)
- return -EFAULT;
- return 0;
- }
- /**
- * sys_sched_getattr - similar to sched_getparam, but with sched_attr
- * @pid: the pid in question.
- * @uattr: structure containing the extended parameters.
- * @size: sizeof(attr) for fwd/bwd comp.
- * @flags: for future extension.
- */
- SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
- unsigned int, size, unsigned int, flags)
- {
- struct sched_attr attr = {
- .size = sizeof(struct sched_attr),
- };
- struct task_struct *p;
- int retval;
- if (!uattr || pid < 0 || size > PAGE_SIZE ||
- size < SCHED_ATTR_SIZE_VER0 || flags)
- return -EINVAL;
- rcu_read_lock();
- p = find_process_by_pid(pid);
- retval = -ESRCH;
- if (!p)
- goto out_unlock;
- retval = security_task_getscheduler(p);
- if (retval)
- goto out_unlock;
- attr.sched_policy = p->policy;
- if (p->sched_reset_on_fork)
- attr.sched_flags |= SCHED_FLAG_RESET_ON_FORK;
- if (task_has_dl_policy(p))
- __getparam_dl(p, &attr);
- else if (task_has_rt_policy(p))
- attr.sched_priority = p->rt_priority;
- else
- attr.sched_nice = task_nice(p);
- rcu_read_unlock();
- retval = sched_read_attr(uattr, &attr, size);
- return retval;
- out_unlock:
- rcu_read_unlock();
- return retval;
- }
- long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
- {
- cpumask_var_t cpus_allowed, new_mask;
- struct task_struct *p;
- int retval;
- int dest_cpu;
- cpumask_t allowed_mask;
- rcu_read_lock();
- p = find_process_by_pid(pid);
- if (!p) {
- rcu_read_unlock();
- return -ESRCH;
- }
- /* Prevent p going away */
- get_task_struct(p);
- rcu_read_unlock();
- if (p->flags & PF_NO_SETAFFINITY) {
- retval = -EINVAL;
- goto out_put_task;
- }
- if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
- retval = -ENOMEM;
- goto out_put_task;
- }
- if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
- retval = -ENOMEM;
- goto out_free_cpus_allowed;
- }
- retval = -EPERM;
- if (!check_same_owner(p)) {
- rcu_read_lock();
- if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
- rcu_read_unlock();
- goto out_free_new_mask;
- }
- rcu_read_unlock();
- }
- retval = security_task_setscheduler(p);
- if (retval)
- goto out_free_new_mask;
- cpuset_cpus_allowed(p, cpus_allowed);
- cpumask_and(new_mask, in_mask, cpus_allowed);
- /*
- * Since bandwidth control happens on root_domain basis,
- * if admission test is enabled, we only admit -deadline
- * tasks allowed to run on all the CPUs in the task's
- * root_domain.
- */
- #ifdef CONFIG_SMP
- if (task_has_dl_policy(p) && dl_bandwidth_enabled()) {
- rcu_read_lock();
- if (!cpumask_subset(task_rq(p)->rd->span, new_mask)) {
- retval = -EBUSY;
- rcu_read_unlock();
- goto out_free_new_mask;
- }
- rcu_read_unlock();
- }
- #endif
- again:
- cpumask_andnot(&allowed_mask, new_mask, cpu_isolated_mask);
- dest_cpu = cpumask_any_and(cpu_active_mask, &allowed_mask);
- if (dest_cpu < nr_cpu_ids) {
- retval = __set_cpus_allowed_ptr(p, new_mask, true);
- if (!retval) {
- cpuset_cpus_allowed(p, cpus_allowed);
- if (!cpumask_subset(new_mask, cpus_allowed)) {
- /*
- * We must have raced with a concurrent cpuset
- * update. Just reset the cpus_allowed to the
- * cpuset's cpus_allowed
- */
- cpumask_copy(new_mask, cpus_allowed);
- goto again;
- }
- }
- } else {
- retval = -EINVAL;
- }
- out_free_new_mask:
- free_cpumask_var(new_mask);
- out_free_cpus_allowed:
- free_cpumask_var(cpus_allowed);
- out_put_task:
- put_task_struct(p);
- return retval;
- }
- static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
- struct cpumask *new_mask)
- {
- if (len < cpumask_size())
- cpumask_clear(new_mask);
- else if (len > cpumask_size())
- len = cpumask_size();
- return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
- }
- /**
- * sys_sched_setaffinity - set the CPU affinity of a process
- * @pid: pid of the process
- * @len: length in bytes of the bitmask pointed to by user_mask_ptr
- * @user_mask_ptr: user-space pointer to the new CPU mask
- *
- * Return: 0 on success. An error code otherwise.
- */
- SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
- unsigned long __user *, user_mask_ptr)
- {
- cpumask_var_t new_mask;
- int retval;
- if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
- return -ENOMEM;
- retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
- if (retval == 0)
- retval = sched_setaffinity(pid, new_mask);
- free_cpumask_var(new_mask);
- return retval;
- }
- long sched_getaffinity(pid_t pid, struct cpumask *mask)
- {
- struct task_struct *p;
- unsigned long flags;
- int retval;
- rcu_read_lock();
- retval = -ESRCH;
- p = find_process_by_pid(pid);
- if (!p)
- goto out_unlock;
- retval = security_task_getscheduler(p);
- if (retval)
- goto out_unlock;
- raw_spin_lock_irqsave(&p->pi_lock, flags);
- cpumask_and(mask, &p->cpus_allowed, cpu_active_mask);
- raw_spin_unlock_irqrestore(&p->pi_lock, flags);
- out_unlock:
- rcu_read_unlock();
- return retval;
- }
- /**
- * sys_sched_getaffinity - get the CPU affinity of a process
- * @pid: pid of the process
- * @len: length in bytes of the bitmask pointed to by user_mask_ptr
- * @user_mask_ptr: user-space pointer to hold the current CPU mask
- *
- * Return: size of CPU mask copied to user_mask_ptr on success. An
- * error code otherwise.
- */
- SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
- unsigned long __user *, user_mask_ptr)
- {
- int ret;
- cpumask_var_t mask;
- if ((len * BITS_PER_BYTE) < nr_cpu_ids)
- return -EINVAL;
- if (len & (sizeof(unsigned long)-1))
- return -EINVAL;
- if (!alloc_cpumask_var(&mask, GFP_KERNEL))
- return -ENOMEM;
- ret = sched_getaffinity(pid, mask);
- if (ret == 0) {
- size_t retlen = min_t(size_t, len, cpumask_size());
- if (copy_to_user(user_mask_ptr, mask, retlen))
- ret = -EFAULT;
- else
- ret = retlen;
- }
- free_cpumask_var(mask);
- return ret;
- }
- /**
- * sys_sched_yield - yield the current processor to other threads.
- *
- * This function yields the current CPU to other tasks. If there are no
- * other threads running on this CPU then this function will return.
- *
- * Return: 0.
- */
- SYSCALL_DEFINE0(sched_yield)
- {
- struct rq_flags rf;
- struct rq *rq;
- rq = this_rq_lock_irq(&rf);
- schedstat_inc(rq->yld_count);
- current->sched_class->yield_task(rq);
- preempt_disable();
- rq_unlock_irq(rq, &rf);
- sched_preempt_enable_no_resched();
- schedule();
- return 0;
- }
- #ifndef CONFIG_PREEMPT
- int __sched _cond_resched(void)
- {
- if (should_resched(0)) {
- preempt_schedule_common();
- return 1;
- }
- return 0;
- }
- EXPORT_SYMBOL(_cond_resched);
- #endif
- /*
- * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
- * call schedule, and on return reacquire the lock.
- *
- * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
- * operations here to prevent schedule() from being called twice (once via
- * spin_unlock(), once by hand).
- */
- int __cond_resched_lock(spinlock_t *lock)
- {
- int resched = should_resched(PREEMPT_LOCK_OFFSET);
- int ret = 0;
- lockdep_assert_held(lock);
- if (spin_needbreak(lock) || resched) {
- spin_unlock(lock);
- if (resched)
- preempt_schedule_common();
- else
- cpu_relax();
- ret = 1;
- spin_lock(lock);
- }
- return ret;
- }
- EXPORT_SYMBOL(__cond_resched_lock);
- int __sched __cond_resched_softirq(void)
- {
- BUG_ON(!in_softirq());
- if (should_resched(SOFTIRQ_DISABLE_OFFSET)) {
- local_bh_enable();
- preempt_schedule_common();
- local_bh_disable();
- return 1;
- }
- return 0;
- }
- EXPORT_SYMBOL(__cond_resched_softirq);
- /**
- * yield - yield the current processor to other threads.
- *
- * Do not ever use this function, there's a 99% chance you're doing it wrong.
- *
- * The scheduler is at all times free to pick the calling task as the most
- * eligible task to run, if removing the yield() call from your code breaks
- * it, its already broken.
- *
- * Typical broken usage is:
- *
- * while (!event)
- * yield();
- *
- * where one assumes that yield() will let 'the other' process run that will
- * make event true. If the current task is a SCHED_FIFO task that will never
- * happen. Never use yield() as a progress guarantee!!
- *
- * If you want to use yield() to wait for something, use wait_event().
- * If you want to use yield() to be 'nice' for others, use cond_resched().
- * If you still want to use yield(), do not!
- */
- void __sched yield(void)
- {
- set_current_state(TASK_RUNNING);
- sys_sched_yield();
- }
- EXPORT_SYMBOL(yield);
- /**
- * yield_to - yield the current processor to another thread in
- * your thread group, or accelerate that thread toward the
- * processor it's on.
- * @p: target task
- * @preempt: whether task preemption is allowed or not
- *
- * It's the caller's job to ensure that the target task struct
- * can't go away on us before we can do any checks.
- *
- * Return:
- * true (>0) if we indeed boosted the target task.
- * false (0) if we failed to boost the target.
- * -ESRCH if there's no task to yield to.
- */
- int __sched yield_to(struct task_struct *p, bool preempt)
- {
- struct task_struct *curr = current;
- struct rq *rq, *p_rq;
- unsigned long flags;
- int yielded = 0;
- local_irq_save(flags);
- rq = this_rq();
- again:
- p_rq = task_rq(p);
- /*
- * If we're the only runnable task on the rq and target rq also
- * has only one task, there's absolutely no point in yielding.
- */
- if (rq->nr_running == 1 && p_rq->nr_running == 1) {
- yielded = -ESRCH;
- goto out_irq;
- }
- double_rq_lock(rq, p_rq);
- if (task_rq(p) != p_rq) {
- double_rq_unlock(rq, p_rq);
- goto again;
- }
- if (!curr->sched_class->yield_to_task)
- goto out_unlock;
- if (curr->sched_class != p->sched_class)
- goto out_unlock;
- if (task_running(p_rq, p) || p->state)
- goto out_unlock;
- yielded = curr->sched_class->yield_to_task(rq, p, preempt);
- if (yielded) {
- schedstat_inc(rq->yld_count);
- /*
- * Make p's CPU reschedule; pick_next_entity takes care of
- * fairness.
- */
- if (preempt && rq != p_rq)
- resched_curr(p_rq);
- }
- out_unlock:
- double_rq_unlock(rq, p_rq);
- out_irq:
- local_irq_restore(flags);
- if (yielded > 0)
- schedule();
- return yielded;
- }
- EXPORT_SYMBOL_GPL(yield_to);
- int io_schedule_prepare(void)
- {
- int old_iowait = current->in_iowait;
- current->in_iowait = 1;
- blk_schedule_flush_plug(current);
- return old_iowait;
- }
- void io_schedule_finish(int token)
- {
- current->in_iowait = token;
- }
- /*
- * This task is about to go to sleep on IO. Increment rq->nr_iowait so
- * that process accounting knows that this is a task in IO wait state.
- */
- long __sched io_schedule_timeout(long timeout)
- {
- int token;
- long ret;
- token = io_schedule_prepare();
- ret = schedule_timeout(timeout);
- io_schedule_finish(token);
- return ret;
- }
- EXPORT_SYMBOL(io_schedule_timeout);
- void __sched io_schedule(void)
- {
- int token;
- token = io_schedule_prepare();
- schedule();
- io_schedule_finish(token);
- }
- EXPORT_SYMBOL(io_schedule);
- /**
- * sys_sched_get_priority_max - return maximum RT priority.
- * @policy: scheduling class.
- *
- * Return: On success, this syscall returns the maximum
- * rt_priority that can be used by a given scheduling class.
- * On failure, a negative error code is returned.
- */
- SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
- {
- int ret = -EINVAL;
- switch (policy) {
- case SCHED_FIFO:
- case SCHED_RR:
- ret = MAX_USER_RT_PRIO-1;
- break;
- case SCHED_DEADLINE:
- case SCHED_NORMAL:
- case SCHED_BATCH:
- case SCHED_IDLE:
- ret = 0;
- break;
- }
- return ret;
- }
- /**
- * sys_sched_get_priority_min - return minimum RT priority.
- * @policy: scheduling class.
- *
- * Return: On success, this syscall returns the minimum
- * rt_priority that can be used by a given scheduling class.
- * On failure, a negative error code is returned.
- */
- SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
- {
- int ret = -EINVAL;
- switch (policy) {
- case SCHED_FIFO:
- case SCHED_RR:
- ret = 1;
- break;
- case SCHED_DEADLINE:
- case SCHED_NORMAL:
- case SCHED_BATCH:
- case SCHED_IDLE:
- ret = 0;
- }
- return ret;
- }
- /**
- * sys_sched_rr_get_interval - return the default timeslice of a process.
- * @pid: pid of the process.
- * @interval: userspace pointer to the timeslice value.
- *
- * this syscall writes the default timeslice value of a given process
- * into the user-space timespec buffer. A value of '0' means infinity.
- *
- * Return: On success, 0 and the timeslice is in @interval. Otherwise,
- * an error code.
- */
- SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
- struct timespec __user *, interval)
- {
- struct task_struct *p;
- unsigned int time_slice;
- struct rq_flags rf;
- struct timespec t;
- struct rq *rq;
- int retval;
- if (pid < 0)
- return -EINVAL;
- retval = -ESRCH;
- rcu_read_lock();
- p = find_process_by_pid(pid);
- if (!p)
- goto out_unlock;
- retval = security_task_getscheduler(p);
- if (retval)
- goto out_unlock;
- rq = task_rq_lock(p, &rf);
- time_slice = 0;
- if (p->sched_class->get_rr_interval)
- time_slice = p->sched_class->get_rr_interval(rq, p);
- task_rq_unlock(rq, p, &rf);
- rcu_read_unlock();
- jiffies_to_timespec(time_slice, &t);
- retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
- return retval;
- out_unlock:
- rcu_read_unlock();
- return retval;
- }
- void sched_show_task(struct task_struct *p)
- {
- unsigned long free = 0;
- int ppid;
- if (!try_get_task_stack(p))
- return;
- printk(KERN_INFO "%-15.15s %c", p->comm, task_state_to_char(p));
- if (p->state == TASK_RUNNING)
- printk(KERN_CONT " running task ");
- #ifdef CONFIG_DEBUG_STACK_USAGE
- free = stack_not_used(p);
- #endif
- ppid = 0;
- rcu_read_lock();
- if (pid_alive(p))
- ppid = task_pid_nr(rcu_dereference(p->real_parent));
- rcu_read_unlock();
- printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
- task_pid_nr(p), ppid,
- (unsigned long)task_thread_info(p)->flags);
- print_worker_info(KERN_INFO, p);
- show_stack(p, NULL);
- put_task_stack(p);
- }
- static inline bool
- state_filter_match(unsigned long state_filter, struct task_struct *p)
- {
- /* no filter, everything matches */
- if (!state_filter)
- return true;
- /* filter, but doesn't match */
- if (!(p->state & state_filter))
- return false;
- /*
- * When looking for TASK_UNINTERRUPTIBLE skip TASK_IDLE (allows
- * TASK_KILLABLE).
- */
- if (state_filter == TASK_UNINTERRUPTIBLE && p->state == TASK_IDLE)
- return false;
- return true;
- }
- void show_state_filter(unsigned long state_filter)
- {
- struct task_struct *g, *p;
- #if BITS_PER_LONG == 32
- printk(KERN_INFO
- " task PC stack pid father\n");
- #else
- printk(KERN_INFO
- " task PC stack pid father\n");
- #endif
- rcu_read_lock();
- for_each_process_thread(g, p) {
- /*
- * reset the NMI-timeout, listing all files on a slow
- * console might take a lot of time:
- * Also, reset softlockup watchdogs on all CPUs, because
- * another CPU might be blocked waiting for us to process
- * an IPI.
- */
- touch_nmi_watchdog();
- touch_all_softlockup_watchdogs();
- if (state_filter_match(state_filter, p))
- sched_show_task(p);
- }
- #ifdef CONFIG_SCHED_DEBUG
- if (!state_filter)
- sysrq_sched_debug_show();
- #endif
- rcu_read_unlock();
- /*
- * Only show locks if all tasks are dumped:
- */
- if (!state_filter)
- debug_show_all_locks();
- }
- /**
- * init_idle - set up an idle thread for a given CPU
- * @idle: task in question
- * @cpu: CPU the idle task belongs to
- *
- * NOTE: this function does not set the idle thread's NEED_RESCHED
- * flag, to make booting more robust.
- */
- void init_idle(struct task_struct *idle, int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- unsigned long flags;
- __sched_fork(0, idle);
- raw_spin_lock_irqsave(&idle->pi_lock, flags);
- raw_spin_lock(&rq->lock);
- idle->state = TASK_RUNNING;
- idle->se.exec_start = sched_clock();
- idle->flags |= PF_IDLE;
- kasan_unpoison_task_stack(idle);
- #ifdef CONFIG_SMP
- /*
- * Its possible that init_idle() gets called multiple times on a task,
- * in that case do_set_cpus_allowed() will not do the right thing.
- *
- * And since this is boot we can forgo the serialization.
- */
- set_cpus_allowed_common(idle, cpumask_of(cpu));
- #endif
- /*
- * We're having a chicken and egg problem, even though we are
- * holding rq->lock, the CPU isn't yet set to this CPU so the
- * lockdep check in task_group() will fail.
- *
- * Similar case to sched_fork(). / Alternatively we could
- * use task_rq_lock() here and obtain the other rq->lock.
- *
- * Silence PROVE_RCU
- */
- rcu_read_lock();
- __set_task_cpu(idle, cpu);
- rcu_read_unlock();
- rq->curr = rq->idle = idle;
- idle->on_rq = TASK_ON_RQ_QUEUED;
- #ifdef CONFIG_SMP
- idle->on_cpu = 1;
- #endif
- raw_spin_unlock(&rq->lock);
- raw_spin_unlock_irqrestore(&idle->pi_lock, flags);
- /* Set the preempt count _outside_ the spinlocks! */
- init_idle_preempt_count(idle, cpu);
- /*
- * The idle tasks have their own, simple scheduling class:
- */
- idle->sched_class = &idle_sched_class;
- ftrace_graph_init_idle_task(idle, cpu);
- vtime_init_idle(idle, cpu);
- #ifdef CONFIG_SMP
- sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
- #endif
- }
- #ifdef CONFIG_SMP
- int cpuset_cpumask_can_shrink(const struct cpumask *cur,
- const struct cpumask *trial)
- {
- int ret = 1;
- if (!cpumask_weight(cur))
- return ret;
- ret = dl_cpuset_cpumask_can_shrink(cur, trial);
- return ret;
- }
- int task_can_attach(struct task_struct *p,
- const struct cpumask *cs_cpus_allowed)
- {
- int ret = 0;
- /*
- * Kthreads which disallow setaffinity shouldn't be moved
- * to a new cpuset; we don't want to change their CPU
- * affinity and isolating such threads by their set of
- * allowed nodes is unnecessary. Thus, cpusets are not
- * applicable for such threads. This prevents checking for
- * success of set_cpus_allowed_ptr() on all attached tasks
- * before cpus_allowed may be changed.
- */
- if (p->flags & PF_NO_SETAFFINITY) {
- ret = -EINVAL;
- goto out;
- }
- if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
- cs_cpus_allowed))
- ret = dl_task_can_attach(p, cs_cpus_allowed);
- out:
- return ret;
- }
- bool sched_smp_initialized __read_mostly;
- #ifdef CONFIG_NUMA_BALANCING
- /* Migrate current task p to target_cpu */
- int migrate_task_to(struct task_struct *p, int target_cpu)
- {
- struct migration_arg arg = { p, target_cpu };
- int curr_cpu = task_cpu(p);
- if (curr_cpu == target_cpu)
- return 0;
- if (!cpumask_test_cpu(target_cpu, &p->cpus_allowed))
- return -EINVAL;
- /* TODO: This is not properly updating schedstats */
- trace_sched_move_numa(p, curr_cpu, target_cpu);
- return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
- }
- /*
- * Requeue a task on a given node and accurately track the number of NUMA
- * tasks on the runqueues
- */
- void sched_setnuma(struct task_struct *p, int nid)
- {
- bool queued, running;
- struct rq_flags rf;
- struct rq *rq;
- rq = task_rq_lock(p, &rf);
- queued = task_on_rq_queued(p);
- running = task_current(rq, p);
- if (queued)
- dequeue_task(rq, p, DEQUEUE_SAVE);
- if (running)
- put_prev_task(rq, p);
- p->numa_preferred_nid = nid;
- if (queued)
- enqueue_task(rq, p, ENQUEUE_RESTORE | ENQUEUE_NOCLOCK);
- if (running)
- set_curr_task(rq, p);
- task_rq_unlock(rq, p, &rf);
- }
- #endif /* CONFIG_NUMA_BALANCING */
- #ifdef CONFIG_HOTPLUG_CPU
- /*
- * Ensure that the idle task is using init_mm right before its CPU goes
- * offline.
- */
- void idle_task_exit(void)
- {
- struct mm_struct *mm = current->active_mm;
- BUG_ON(cpu_online(smp_processor_id()));
- if (mm != &init_mm) {
- switch_mm(mm, &init_mm, current);
- finish_arch_post_lock_switch();
- }
- mmdrop(mm);
- }
- /*
- * Since this CPU is going 'away' for a while, fold any nr_active delta
- * we might have. Assumes we're called after migrate_tasks() so that the
- * nr_active count is stable. We need to take the teardown thread which
- * is calling this into account, so we hand in adjust = 1 to the load
- * calculation.
- *
- * Also see the comment "Global load-average calculations".
- */
- static void calc_load_migrate(struct rq *rq)
- {
- long delta = calc_load_fold_active(rq, 1);
- if (delta)
- atomic_long_add(delta, &calc_load_tasks);
- }
- static void put_prev_task_fake(struct rq *rq, struct task_struct *prev)
- {
- }
- static const struct sched_class fake_sched_class = {
- .put_prev_task = put_prev_task_fake,
- };
- static struct task_struct fake_task = {
- /*
- * Avoid pull_{rt,dl}_task()
- */
- .prio = MAX_PRIO + 1,
- .sched_class = &fake_sched_class,
- };
- /*
- * Remove a task from the runqueue and pretend that it's migrating. This
- * should prevent migrations for the detached task and disallow further
- * changes to tsk_cpus_allowed.
- */
- static void
- detach_one_task(struct task_struct *p, struct rq *rq, struct list_head *tasks)
- {
- lockdep_assert_held(&rq->lock);
- p->on_rq = TASK_ON_RQ_MIGRATING;
- deactivate_task(rq, p, 0);
- list_add(&p->se.group_node, tasks);
- }
- static void attach_tasks(struct list_head *tasks, struct rq *rq)
- {
- struct task_struct *p;
- lockdep_assert_held(&rq->lock);
- while (!list_empty(tasks)) {
- p = list_first_entry(tasks, struct task_struct, se.group_node);
- list_del_init(&p->se.group_node);
- WARN_ON(task_rq(p) != rq);
- activate_task(rq, p, 0);
- p->on_rq = TASK_ON_RQ_QUEUED;
- }
- }
- /*
- *Migrate all tasks (not pinned if pinned argument say so) from the rq,
- *sleeping tasks will be migrated by try_to_wake_up()->select_task_rq().
- * Called with rq->lock held even though we'er in stop_machine() and
- * there's no concurrency possible, we hold the required locks anyway
- * because of lock validation efforts.
- */
- static void migrate_tasks(struct rq *dead_rq, struct rq_flags *rf,
- bool migrate_pinned_tasks)
- {
- struct rq *rq = dead_rq;
- struct task_struct *next, *stop = rq->stop;
- struct rq_flags orf = *rf;
- int dest_cpu;
- LIST_HEAD(tasks);
- unsigned int num_pinned_kthreads = 1; /* this thread */
- cpumask_t avail_cpus;
- cpumask_andnot(&avail_cpus, cpu_online_mask, cpu_isolated_mask);
- /*
- * Fudge the rq selection such that the below task selection loop
- * doesn't get stuck on the currently eligible stop task.
- *
- * We're currently inside stop_machine() and the rq is either stuck
- * in the stop_machine_cpu_stop() loop, or we're executing this code,
- * either way we should never end up calling schedule() until we're
- * done here.
- */
- rq->stop = NULL;
- /*
- * put_prev_task() and pick_next_task() sched
- * class method both need to have an up-to-date
- * value of rq->clock[_task]
- */
- update_rq_clock(rq);
- unthrottle_offline_rt_rqs(rq);
- for (;;) {
- /*
- * There's this thread running, bail when that's the only
- * remaining thread:
- */
- if (rq->nr_running == 1)
- break;
- /*
- * pick_next_task() assumes pinned rq->lock:
- */
- next = pick_next_task(rq, &fake_task, rf);
- BUG_ON(!next);
- put_prev_task(rq, next);
- if (!migrate_pinned_tasks && next->flags & PF_KTHREAD &&
- !cpumask_intersects(&avail_cpus, &next->cpus_allowed)) {
- detach_one_task(next, rq, &tasks);
- num_pinned_kthreads += 1;
- continue;
- }
- /*
- * Rules for changing task_struct::cpus_allowed are holding
- * both pi_lock and rq->lock, such that holding either
- * stabilizes the mask.
- *
- * Drop rq->lock is not quite as disastrous as it usually is
- * because !cpu_active at this point, which means load-balance
- * will not interfere. Also, stop-machine.
- */
- rq_unlock(rq, rf);
- raw_spin_lock(&next->pi_lock);
- rq_relock(rq, rf);
- /*
- * Since we're inside stop-machine, _nothing_ should have
- * changed the task, WARN if weird stuff happened, because in
- * that case the above rq->lock drop is a fail too.
- * However, during cpu isolation the load balancer might have
- * interferred since we don't stop all CPUs. Ignore warning for
- * this case.
- */
- if (task_rq(next) != rq || !task_on_rq_queued(next)) {
- WARN_ON(migrate_pinned_tasks);
- raw_spin_unlock(&next->pi_lock);
- continue;
- }
- /* Find suitable destination for @next, with force if needed. */
- dest_cpu = select_fallback_rq(dead_rq->cpu, next, false);
- rq = __migrate_task(rq, rf, next, dest_cpu);
- if (rq != dead_rq) {
- rq_unlock(rq, rf);
- rq = dead_rq;
- *rf = orf;
- rq_relock(rq, rf);
- }
- raw_spin_unlock(&next->pi_lock);
- }
- rq->stop = stop;
- if (num_pinned_kthreads > 1)
- attach_tasks(&tasks, rq);
- }
- int do_isolation_work_cpu_stop(void *data)
- {
- unsigned int cpu = smp_processor_id();
- struct rq *rq = cpu_rq(cpu);
- struct rq_flags rf;
- local_irq_disable();
- sched_ttwu_pending();
- rq_lock_irqsave(rq, &rf);
- /*
- * Temporarily mark the rq as offline. This will allow us to
- * move tasks off the CPU.
- */
- if (rq->rd) {
- WARN_ON(!cpumask_test_cpu(cpu, rq->rd->span));
- set_rq_offline(rq);
- }
- migrate_tasks(rq, &rf, false);
- if (rq->rd)
- set_rq_online(rq);
- rq_unlock_irqrestore(rq, &rf);
- /*
- * We might have been in tickless state. Clear NOHZ flags to avoid
- * us being kicked for helping out with balancing
- */
- nohz_balance_clear_nohz_mask(cpu);
- local_irq_enable();
- return 0;
- }
- static void sched_update_group_capacities(int cpu)
- {
- struct sched_domain *sd;
- mutex_lock(&sched_domains_mutex);
- rcu_read_lock();
- for_each_domain(cpu, sd) {
- int balance_cpu = group_balance_cpu(sd->groups);
- init_sched_groups_capacity(cpu, sd);
- /*
- * Need to ensure this is also called with balancing
- * cpu.
- */
- if (cpu != balance_cpu)
- init_sched_groups_capacity(balance_cpu, sd);
- }
- rcu_read_unlock();
- mutex_unlock(&sched_domains_mutex);
- }
- static unsigned int cpu_isolation_vote[NR_CPUS];
- int sched_isolate_count(const cpumask_t *mask, bool include_offline)
- {
- cpumask_t count_mask = CPU_MASK_NONE;
- if (include_offline) {
- cpumask_complement(&count_mask, cpu_online_mask);
- cpumask_or(&count_mask, &count_mask, cpu_isolated_mask);
- cpumask_and(&count_mask, &count_mask, mask);
- } else {
- cpumask_and(&count_mask, mask, cpu_isolated_mask);
- }
- return cpumask_weight(&count_mask);
- }
- void notify_atf_cpu_isolated_status(int cpu)
- {
- #ifdef CONFIG_MEDIATEK_SOLUTION
- unsigned long cur_mask = cpu_isolated_mask->bits[0];
- struct arm_smccc_res res;
- arm_smccc_smc(MTK_SIP_GIC_CONTROL, GIC_ISO_CODE, cur_mask,
- 0, 0, 0, 0, 0, &res);
- #endif
- }
- /*
- * 1) CPU is isolated and cpu is offlined:
- * Unisolate the core.
- * 2) CPU is not isolated and CPU is offlined:
- * No action taken.
- * 3) CPU is offline and request to isolate
- * Request ignored.
- * 4) CPU is offline and isolated:
- * Not a possible state.
- * 5) CPU is online and request to isolate
- * Normal case: Isolate the CPU
- * 6) CPU is not isolated and comes back online
- * Nothing to do
- *
- * Note: The client calling sched_isolate_cpu() is repsonsible for ONLY
- * calling sched_deisolate_cpu() on a CPU that the client previously isolated.
- * Client is also responsible for deisolating when a core goes offline
- * (after CPU is marked offline).
- */
- int _sched_isolate_cpu(int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- cpumask_t avail_cpus;
- int ret_code = 0;
- u64 start_time = 0;
- if (trace_sched_isolate_enabled())
- start_time = sched_clock();
- cpu_maps_update_begin();
- cpumask_andnot(&avail_cpus, cpu_online_mask, cpu_isolated_mask);
- /* We cannot isolate ALL cpus in the system */
- if (cpumask_weight(&avail_cpus) == 1) {
- ret_code = -EINVAL;
- goto out;
- }
- if (!cpu_online(cpu)) {
- ret_code = -EINVAL;
- goto out;
- }
- if (++cpu_isolation_vote[cpu] > 1)
- goto out;
- set_cpu_isolated(cpu, true);
- mcdi_cpu_iso_mask(cpu_isolated_mask->bits[0]);
- cpumask_clear_cpu(cpu, &avail_cpus);
- notify_atf_cpu_isolated_status(cpu);
- /* Migrate timers */
- smp_call_function_any(&avail_cpus, hrtimer_quiesce_cpu, &cpu, 1);
- smp_call_function_any(&avail_cpus, timer_quiesce_cpu, &cpu, 1);
- stop_cpus(cpumask_of(cpu), do_isolation_work_cpu_stop, 0);
- calc_load_migrate(rq);
- update_max_interval();
- sched_update_group_capacities(cpu);
- out:
- cpu_maps_update_done();
- trace_sched_isolate(cpu, cpumask_bits(cpu_isolated_mask)[0],
- start_time, 1);
- printk_deferred("%s: prio=%d, cpu=%d, isolation_cpus=0x%lx\n",
- __func__, iso_prio, cpu, cpu_isolated_mask->bits[0]);
- return ret_code;
- }
- /*
- * Note: The client calling sched_isolate_cpu() is repsonsible for ONLY
- * calling sched_deisolate_cpu() on a CPU that the client previously isolated.
- * Client is also responsible for deisolating when a core goes offline
- * (after CPU is marked offline).
- */
- int __sched_deisolate_cpu_unlocked(int cpu)
- {
- int ret_code = 0;
- struct rq *rq = cpu_rq(cpu);
- u64 start_time = 0;
- if (trace_sched_isolate_enabled())
- start_time = sched_clock();
- if (!cpu_isolation_vote[cpu]) {
- ret_code = -EINVAL;
- goto out;
- }
- if (--cpu_isolation_vote[cpu])
- goto out;
- if (cpu_online(cpu)) {
- unsigned long flags;
- raw_spin_lock_irqsave(&rq->lock, flags);
- rq->age_stamp = sched_clock_cpu(cpu);
- raw_spin_unlock_irqrestore(&rq->lock, flags);
- }
- set_cpu_isolated(cpu, false);
- mcdi_cpu_iso_mask(cpu_isolated_mask->bits[0]);
- notify_atf_cpu_isolated_status(cpu);
- update_max_interval();
- sched_update_group_capacities(cpu);
- if (cpu_online(cpu)) {
- /* Kick CPU to immediately do load balancing */
- if (!test_and_set_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)))
- smp_send_reschedule(cpu);
- }
- out:
- trace_sched_isolate(cpu, cpumask_bits(cpu_isolated_mask)[0],
- start_time, 0);
- printk_deferred("%s: prio=%d, cpu=%d, isolation_cpus=0x%lx\n",
- __func__, iso_prio, cpu, cpu_isolated_mask->bits[0]);
- return ret_code;
- }
- int _sched_deisolate_cpu(int cpu)
- {
- int ret_code;
- cpu_maps_update_begin();
- ret_code = __sched_deisolate_cpu_unlocked(cpu);
- cpu_maps_update_done();
- return ret_code;
- }
- void iso_cpumask_init(void)
- {
- cpumask_copy(&cpu_all_masks, cpu_possible_mask);
- cpumask_setall(&available_cpus);
- }
- /* Use available_cpus to determine cpu isolated or deisolated */
- int set_cpu_isolation(enum iso_prio_t prio, struct cpumask *cpumask_ptr)
- {
- struct cpumask iso_mask;
- struct cpumask deiso_mask;
- int i = 0;
- if (prio > iso_prio)
- return -1;
- if (!cpumask_ptr)
- return -1;
- might_sleep();
- mutex_lock(&sched_isolation_mutex);
- iso_prio = prio;
- /* cpumask of isolated */
- cpumask_or(&iso_mask, cpumask_ptr, cpu_isolated_mask);
- cpumask_complement(&iso_mask, &iso_mask);
- /* cpumask of de-isolated */
- cpumask_and(&deiso_mask, cpumask_ptr, cpu_isolated_mask);
- /* set cpu isolated */
- if (!cpumask_empty(&iso_mask)) {
- for_each_cpu(i, &iso_mask)
- _sched_isolate_cpu(i);
- }
- /* set cpu de-isolated */
- if (!cpumask_empty(&deiso_mask)) {
- for_each_cpu(i, &deiso_mask)
- _sched_deisolate_cpu(i);
- }
- /* all possible cpu de-isolated*/
- if (cpumask_empty(cpu_isolated_mask)) {
- iso_prio = ISO_UNSET;
- cpumask_setall(&available_cpus);
- }
- mutex_unlock(&sched_isolation_mutex);
- return 0;
- }
- /* de-isolated all cpu */
- int unset_cpu_isolation(enum iso_prio_t prio)
- {
- int err;
- err = set_cpu_isolation(prio, &cpu_all_masks);
- return err;
- }
- /*
- * Set cpu to be isolated
- * Success: return 0
- */
- int sched_isolate_cpu(int cpu)
- {
- int err = -1;
- if (cpu >= nr_cpu_ids)
- return err;
- #if defined(CONFIG_MTK_GIC_V3_EXT)
- remove_cpu_from_prefer_schedule_domain(cpu);
- #endif
- cpumask_clear_cpu(cpu, &available_cpus);
- err = set_cpu_isolation(ISO_CUSTOMIZE, &available_cpus);
- return err;
- }
- EXPORT_SYMBOL(sched_isolate_cpu);
- /*
- * Set cpu to be deisolated
- * Success: return 0
- */
- int sched_deisolate_cpu(int cpu)
- {
- int err = -1;
- if (cpu >= nr_cpu_ids)
- return err;
- cpumask_set_cpu(cpu, &available_cpus);
- err = set_cpu_isolation(ISO_CUSTOMIZE, &available_cpus);
- #if defined(CONFIG_MTK_GIC_V3_EXT)
- add_cpu_to_prefer_schedule_domain(cpu);
- #endif
- return err;
- }
- EXPORT_SYMBOL(sched_deisolate_cpu);
- #endif /* CONFIG_HOTPLUG_CPU */
- void set_rq_online(struct rq *rq)
- {
- if (!rq->online) {
- const struct sched_class *class;
- cpumask_set_cpu(rq->cpu, rq->rd->online);
- rq->online = 1;
- for_each_class(class) {
- if (class->rq_online)
- class->rq_online(rq);
- }
- }
- }
- void set_rq_offline(struct rq *rq)
- {
- if (rq->online) {
- const struct sched_class *class;
- for_each_class(class) {
- if (class->rq_offline)
- class->rq_offline(rq);
- }
- cpumask_clear_cpu(rq->cpu, rq->rd->online);
- rq->online = 0;
- }
- }
- static void set_cpu_rq_start_time(unsigned int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- rq->age_stamp = sched_clock_cpu(cpu);
- }
- /*
- * used to mark begin/end of suspend/resume:
- */
- static int num_cpus_frozen;
- /*
- * Update cpusets according to cpu_active mask. If cpusets are
- * disabled, cpuset_update_active_cpus() becomes a simple wrapper
- * around partition_sched_domains().
- *
- * If we come here as part of a suspend/resume, don't touch cpusets because we
- * want to restore it back to its original state upon resume anyway.
- */
- static void cpuset_cpu_active(void)
- {
- if (cpuhp_tasks_frozen) {
- /*
- * num_cpus_frozen tracks how many CPUs are involved in suspend
- * resume sequence. As long as this is not the last online
- * operation in the resume sequence, just build a single sched
- * domain, ignoring cpusets.
- */
- partition_sched_domains(1, NULL, NULL);
- if (--num_cpus_frozen)
- return;
- /*
- * This is the last CPU online operation. So fall through and
- * restore the original sched domains by considering the
- * cpuset configurations.
- */
- cpuset_force_rebuild();
- }
- cpuset_update_active_cpus();
- }
- static int cpuset_cpu_inactive(unsigned int cpu)
- {
- if (!cpuhp_tasks_frozen) {
- if (dl_cpu_busy(cpu))
- return -EBUSY;
- cpuset_update_active_cpus();
- } else {
- num_cpus_frozen++;
- partition_sched_domains(1, NULL, NULL);
- }
- return 0;
- }
- int sched_cpu_activate(unsigned int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- struct rq_flags rf;
- #ifdef CONFIG_SCHED_SMT
- /*
- * When going up, increment the number of cores with SMT present.
- */
- if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
- static_branch_inc_cpuslocked(&sched_smt_present);
- #endif
- set_cpu_active(cpu, true);
- if (sched_smp_initialized) {
- sched_domains_numa_masks_set(cpu);
- cpuset_cpu_active();
- }
- /*
- * Put the rq online, if not already. This happens:
- *
- * 1) In the early boot process, because we build the real domains
- * after all CPUs have been brought up.
- *
- * 2) At runtime, if cpuset_cpu_active() fails to rebuild the
- * domains.
- */
- rq_lock_irqsave(rq, &rf);
- if (rq->rd) {
- BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
- set_rq_online(rq);
- }
- rq_unlock_irqrestore(rq, &rf);
- update_max_interval();
- return 0;
- }
- int sched_cpu_deactivate(unsigned int cpu)
- {
- int ret;
- set_cpu_active(cpu, false);
- /*
- * We've cleared cpu_active_mask, wait for all preempt-disabled and RCU
- * users of this state to go away such that all new such users will
- * observe it.
- *
- * Do sync before park smpboot threads to take care the rcu boost case.
- */
- synchronize_rcu_mult(call_rcu, call_rcu_sched);
- #ifdef CONFIG_SCHED_SMT
- /*
- * When going down, decrement the number of cores with SMT present.
- */
- if (cpumask_weight(cpu_smt_mask(cpu)) == 2)
- static_branch_dec_cpuslocked(&sched_smt_present);
- #endif
- if (!sched_smp_initialized)
- return 0;
- ret = cpuset_cpu_inactive(cpu);
- if (ret) {
- set_cpu_active(cpu, true);
- return ret;
- }
- sched_domains_numa_masks_clear(cpu);
- return 0;
- }
- static void sched_rq_cpu_starting(unsigned int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- rq->calc_load_update = calc_load_update;
- update_max_interval();
- }
- int sched_cpu_starting(unsigned int cpu)
- {
- set_cpu_rq_start_time(cpu);
- sched_rq_cpu_starting(cpu);
- return 0;
- }
- #ifdef CONFIG_HOTPLUG_CPU
- int sched_cpu_dying(unsigned int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- struct rq_flags rf;
- /* Handle pending wakeups and then migrate everything off */
- sched_ttwu_pending();
- rq_lock_irqsave(rq, &rf);
- walt_migrate_sync_cpu(cpu);
- if (rq->rd) {
- BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
- set_rq_offline(rq);
- }
- migrate_tasks(rq, &rf, true);
- BUG_ON(rq->nr_running != 1);
- rq_unlock_irqrestore(rq, &rf);
- calc_load_migrate(rq);
- update_max_interval();
- nohz_balance_exit_idle(cpu);
- hrtick_clear(rq);
- return 0;
- }
- #endif
- void __init sched_init_smp(void)
- {
- cpumask_var_t non_isolated_cpus;
- init_hmp_domains();
- #ifdef CONFIG_MACH_MT6873
- init_efuse_info();
- #endif
- alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
- sched_init_numa();
- /*
- * There's no userspace yet to cause hotplug operations; hence all the
- * CPU masks are stable and all blatant races in the below code cannot
- * happen. The hotplug lock is nevertheless taken to satisfy lockdep,
- * but there won't be any contention on it.
- */
- cpus_read_lock();
- mutex_lock(&sched_domains_mutex);
- sched_init_domains(cpu_active_mask);
- cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
- if (cpumask_empty(non_isolated_cpus))
- cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
- mutex_unlock(&sched_domains_mutex);
- cpus_read_unlock();
- /* Move init over to a non-isolated CPU */
- if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
- BUG();
- sched_init_granularity();
- free_cpumask_var(non_isolated_cpus);
- init_sched_rt_class();
- init_sched_dl_class();
- sched_smp_initialized = true;
- }
- static int __init migration_init(void)
- {
- sched_rq_cpu_starting(smp_processor_id());
- return 0;
- }
- early_initcall(migration_init);
- #else
- void __init sched_init_smp(void)
- {
- sched_init_granularity();
- }
- #endif /* CONFIG_SMP */
- int in_sched_functions(unsigned long addr)
- {
- return in_lock_functions(addr) ||
- (addr >= (unsigned long)__sched_text_start
- && addr < (unsigned long)__sched_text_end);
- }
- #ifdef CONFIG_CGROUP_SCHED
- /*
- * Default task group.
- * Every task in system belongs to this group at bootup.
- */
- struct task_group root_task_group;
- LIST_HEAD(task_groups);
- /* Cacheline aligned slab cache for task_group */
- static struct kmem_cache *task_group_cache __read_mostly;
- #endif
- DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
- DECLARE_PER_CPU(cpumask_var_t, select_idle_mask);
- void __init sched_init(void)
- {
- int i, j;
- unsigned long alloc_size = 0, ptr;
- sched_clock_init();
- wait_bit_init();
- #ifdef CONFIG_FAIR_GROUP_SCHED
- alloc_size += 2 * nr_cpu_ids * sizeof(void **);
- #endif
- #ifdef CONFIG_RT_GROUP_SCHED
- alloc_size += 2 * nr_cpu_ids * sizeof(void **);
- #endif
- if (alloc_size) {
- ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
- iso_cpumask_init();
- #ifdef CONFIG_FAIR_GROUP_SCHED
- root_task_group.se = (struct sched_entity **)ptr;
- ptr += nr_cpu_ids * sizeof(void **);
- root_task_group.cfs_rq = (struct cfs_rq **)ptr;
- ptr += nr_cpu_ids * sizeof(void **);
- #endif /* CONFIG_FAIR_GROUP_SCHED */
- #ifdef CONFIG_RT_GROUP_SCHED
- root_task_group.rt_se = (struct sched_rt_entity **)ptr;
- ptr += nr_cpu_ids * sizeof(void **);
- root_task_group.rt_rq = (struct rt_rq **)ptr;
- ptr += nr_cpu_ids * sizeof(void **);
- #endif /* CONFIG_RT_GROUP_SCHED */
- }
- #ifdef CONFIG_CPUMASK_OFFSTACK
- for_each_possible_cpu(i) {
- per_cpu(load_balance_mask, i) = (cpumask_var_t)kzalloc_node(
- cpumask_size(), GFP_KERNEL, cpu_to_node(i));
- per_cpu(select_idle_mask, i) = (cpumask_var_t)kzalloc_node(
- cpumask_size(), GFP_KERNEL, cpu_to_node(i));
- }
- #endif /* CONFIG_CPUMASK_OFFSTACK */
- init_rt_bandwidth(&def_rt_bandwidth, global_rt_period(), global_rt_runtime());
- init_dl_bandwidth(&def_dl_bandwidth, global_rt_period(), global_rt_runtime());
- #ifdef CONFIG_SMP
- init_defrootdomain();
- #endif
- #ifdef CONFIG_RT_GROUP_SCHED
- init_rt_bandwidth(&root_task_group.rt_bandwidth,
- global_rt_period(), global_rt_runtime());
- #endif /* CONFIG_RT_GROUP_SCHED */
- #ifdef CONFIG_CGROUP_SCHED
- task_group_cache = KMEM_CACHE(task_group, 0);
- list_add(&root_task_group.list, &task_groups);
- INIT_LIST_HEAD(&root_task_group.children);
- INIT_LIST_HEAD(&root_task_group.siblings);
- autogroup_init(&init_task);
- #endif /* CONFIG_CGROUP_SCHED */
- for_each_possible_cpu(i) {
- struct rq *rq;
- rq = cpu_rq(i);
- raw_spin_lock_init(&rq->lock);
- rq->nr_running = 0;
- rq->calc_load_active = 0;
- rq->calc_load_update = jiffies + LOAD_FREQ;
- init_cfs_rq(&rq->cfs);
- init_rt_rq(&rq->rt);
- init_dl_rq(&rq->dl);
- #ifdef CONFIG_FAIR_GROUP_SCHED
- root_task_group.shares = ROOT_TASK_GROUP_LOAD;
- INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
- rq->tmp_alone_branch = &rq->leaf_cfs_rq_list;
- /*
- * How much CPU bandwidth does root_task_group get?
- *
- * In case of task-groups formed thr' the cgroup filesystem, it
- * gets 100% of the CPU resources in the system. This overall
- * system CPU resource is divided among the tasks of
- * root_task_group and its child task-groups in a fair manner,
- * based on each entity's (task or task-group's) weight
- * (se->load.weight).
- *
- * In other words, if root_task_group has 10 tasks of weight
- * 1024) and two child groups A0 and A1 (of weight 1024 each),
- * then A0's share of the CPU resource is:
- *
- * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
- *
- * We achieve this by letting root_task_group's tasks sit
- * directly in rq->cfs (i.e root_task_group->se[] = NULL).
- */
- init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
- init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
- #endif /* CONFIG_FAIR_GROUP_SCHED */
- rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
- #ifdef CONFIG_RT_GROUP_SCHED
- init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
- #endif
- for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
- rq->cpu_load[j] = 0;
- #ifdef CONFIG_SMP
- rq->sd = NULL;
- rq->rd = NULL;
- rq->cpu_capacity = rq->cpu_capacity_orig = SCHED_CAPACITY_SCALE;
- rq->balance_callback = NULL;
- rq->active_balance = 0;
- rq->next_balance = jiffies;
- rq->push_cpu = 0;
- rq->cpu = i;
- rq->online = 0;
- rq->idle_stamp = 0;
- rq->avg_idle = 2*sysctl_sched_migration_cost;
- rq->max_idle_balance_cost = sysctl_sched_migration_cost;
- #ifdef CONFIG_SCHED_WALT
- rq->cur_irqload = 0;
- rq->avg_irqload = 0;
- rq->irqload_ts = 0;
- #endif
- INIT_LIST_HEAD(&rq->cfs_tasks);
- rq_attach_root(rq, &def_root_domain);
- #ifdef CONFIG_NO_HZ_COMMON
- rq->last_load_update_tick = jiffies;
- rq->last_blocked_load_update_tick = jiffies;
- rq->nohz_flags = 0;
- #endif
- #ifdef CONFIG_NO_HZ_FULL
- rq->last_sched_tick = 0;
- #endif
- #endif /* CONFIG_SMP */
- init_rq_hrtick(rq);
- atomic_set(&rq->nr_iowait, 0);
- }
- set_load_weight(&init_task);
- /*
- * The boot idle thread does lazy MMU switching as well:
- */
- mmgrab(&init_mm);
- enter_lazy_tlb(&init_mm, current);
- /*
- * Make us the idle thread. Technically, schedule() should not be
- * called from this thread, however somewhere below it might be,
- * but because we are the idle thread, we just pick up running again
- * when this runqueue becomes "idle".
- */
- init_idle(current, smp_processor_id());
- calc_load_update = jiffies + LOAD_FREQ;
- #ifdef CONFIG_SMP
- /* May be allocated at isolcpus cmdline parse time */
- if (cpu_isolated_map == NULL)
- zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
- idle_thread_set_boot_cpu();
- set_cpu_rq_start_time(smp_processor_id());
- #endif
- init_sched_fair_class();
- init_schedstats();
- init_uclamp();
- init_sched_energy_costs();
- psi_init();
- scheduler_running = 1;
- task_rotate_work_init();
- }
- #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
- static inline int preempt_count_equals(int preempt_offset)
- {
- int nested = preempt_count() + rcu_preempt_depth();
- return (nested == preempt_offset);
- }
- void __might_sleep(const char *file, int line, int preempt_offset)
- {
- /*
- * Blocking primitives will set (and therefore destroy) current->state,
- * since we will exit with TASK_RUNNING make sure we enter with it,
- * otherwise we will destroy state.
- */
- WARN_ONCE(current->state != TASK_RUNNING && current->task_state_change,
- "do not call blocking ops when !TASK_RUNNING; "
- "state=%lx set at [<%p>] %pS\n",
- current->state,
- (void *)current->task_state_change,
- (void *)current->task_state_change);
- ___might_sleep(file, line, preempt_offset);
- }
- EXPORT_SYMBOL(__might_sleep);
- void ___might_sleep(const char *file, int line, int preempt_offset)
- {
- /* Ratelimiting timestamp: */
- static unsigned long prev_jiffy;
- unsigned long preempt_disable_ip;
- /* WARN_ON_ONCE() by default, no rate limit required: */
- rcu_sleep_check();
- if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
- !is_idle_task(current)) ||
- system_state == SYSTEM_BOOTING || system_state > SYSTEM_RUNNING ||
- oops_in_progress)
- return;
- if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
- return;
- prev_jiffy = jiffies;
- /* Save this before calling printk(), since that will clobber it: */
- preempt_disable_ip = get_preempt_disable_ip(current);
- printk(KERN_ERR
- "BUG: sleeping function called from invalid context at %s:%d\n",
- file, line);
- printk(KERN_ERR
- "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
- in_atomic(), irqs_disabled(),
- current->pid, current->comm);
- if (task_stack_end_corrupted(current))
- printk(KERN_EMERG "Thread overran stack, or stack corrupted\n");
- debug_show_held_locks(current);
- if (irqs_disabled())
- print_irqtrace_events(current);
- if (IS_ENABLED(CONFIG_DEBUG_PREEMPT)
- && !preempt_count_equals(preempt_offset)) {
- pr_err("Preemption disabled at:");
- print_ip_sym(preempt_disable_ip);
- dump_preempt_disable_ips(current);
- pr_cont("\n");
- }
- dump_stack();
- add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
- }
- EXPORT_SYMBOL(___might_sleep);
- #endif
- #ifdef CONFIG_MAGIC_SYSRQ
- void normalize_rt_tasks(void)
- {
- struct task_struct *g, *p;
- struct sched_attr attr = {
- .sched_policy = SCHED_NORMAL,
- };
- read_lock(&tasklist_lock);
- for_each_process_thread(g, p) {
- /*
- * Only normalize user tasks:
- */
- if (p->flags & PF_KTHREAD)
- continue;
- p->se.exec_start = 0;
- schedstat_set(p->se.statistics.wait_start, 0);
- schedstat_set(p->se.statistics.sleep_start, 0);
- schedstat_set(p->se.statistics.block_start, 0);
- if (!dl_task(p) && !rt_task(p)) {
- /*
- * Renice negative nice level userspace
- * tasks back to 0:
- */
- if (task_nice(p) < 0)
- set_user_nice(p, 0);
- continue;
- }
- __sched_setscheduler(p, &attr, false, false);
- }
- read_unlock(&tasklist_lock);
- }
- #endif /* CONFIG_MAGIC_SYSRQ */
- #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
- /*
- * These functions are only useful for the IA64 MCA handling, or kdb.
- *
- * They can only be called when the whole system has been
- * stopped - every CPU needs to be quiescent, and no scheduling
- * activity can take place. Using them for anything else would
- * be a serious bug, and as a result, they aren't even visible
- * under any other configuration.
- */
- /**
- * curr_task - return the current task for a given CPU.
- * @cpu: the processor in question.
- *
- * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
- *
- * Return: The current task for @cpu.
- */
- struct task_struct *curr_task(int cpu)
- {
- return cpu_curr(cpu);
- }
- #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
- #ifdef CONFIG_IA64
- /**
- * set_curr_task - set the current task for a given CPU.
- * @cpu: the processor in question.
- * @p: the task pointer to set.
- *
- * Description: This function must only be used when non-maskable interrupts
- * are serviced on a separate stack. It allows the architecture to switch the
- * notion of the current task on a CPU in a non-blocking manner. This function
- * must be called with all CPU's synchronized, and interrupts disabled, the
- * and caller must save the original value of the current task (see
- * curr_task() above) and restore that value before reenabling interrupts and
- * re-starting the system.
- *
- * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
- */
- void ia64_set_curr_task(int cpu, struct task_struct *p)
- {
- cpu_curr(cpu) = p;
- }
- #endif
- #ifdef CONFIG_CGROUP_SCHED
- /* task_group_lock serializes the addition/removal of task groups */
- static DEFINE_SPINLOCK(task_group_lock);
- static void sched_free_group(struct task_group *tg)
- {
- #if defined(CONFIG_UCLAMP_TASK_GROUP) && !defined(CONFIG_SCHED_TUNE)
- free_uclamp_sched_group(tg);
- #endif
- free_fair_sched_group(tg);
- free_rt_sched_group(tg);
- autogroup_free(tg);
- kmem_cache_free(task_group_cache, tg);
- }
- /* allocate runqueue etc for a new task group */
- struct task_group *sched_create_group(struct task_group *parent)
- {
- struct task_group *tg;
- tg = kmem_cache_alloc(task_group_cache, GFP_KERNEL | __GFP_ZERO);
- if (!tg)
- return ERR_PTR(-ENOMEM);
- if (!alloc_fair_sched_group(tg, parent))
- goto err;
- if (!alloc_rt_sched_group(tg, parent))
- goto err;
- #if defined(CONFIG_UCLAMP_TASK_GROUP) && !defined(CONFIG_SCHED_TUNE)
- if (!alloc_uclamp_sched_group(tg, parent))
- goto err;
- #endif
- return tg;
- err:
- sched_free_group(tg);
- return ERR_PTR(-ENOMEM);
- }
- void sched_online_group(struct task_group *tg, struct task_group *parent)
- {
- unsigned long flags;
- spin_lock_irqsave(&task_group_lock, flags);
- list_add_rcu(&tg->list, &task_groups);
- /* Root should already exist: */
- WARN_ON(!parent);
- tg->parent = parent;
- INIT_LIST_HEAD(&tg->children);
- list_add_rcu(&tg->siblings, &parent->children);
- spin_unlock_irqrestore(&task_group_lock, flags);
- online_fair_sched_group(tg);
- }
- /* rcu callback to free various structures associated with a task group */
- static void sched_free_group_rcu(struct rcu_head *rhp)
- {
- /* Now it should be safe to free those cfs_rqs: */
- sched_free_group(container_of(rhp, struct task_group, rcu));
- }
- void sched_destroy_group(struct task_group *tg)
- {
- /* Wait for possible concurrent references to cfs_rqs complete: */
- call_rcu(&tg->rcu, sched_free_group_rcu);
- }
- void sched_offline_group(struct task_group *tg)
- {
- unsigned long flags;
- /* End participation in shares distribution: */
- unregister_fair_sched_group(tg);
- spin_lock_irqsave(&task_group_lock, flags);
- list_del_rcu(&tg->list);
- list_del_rcu(&tg->siblings);
- spin_unlock_irqrestore(&task_group_lock, flags);
- }
- static void sched_change_group(struct task_struct *tsk, int type)
- {
- struct task_group *tg;
- /*
- * All callers are synchronized by task_rq_lock(); we do not use RCU
- * which is pointless here. Thus, we pass "true" to task_css_check()
- * to prevent lockdep warnings.
- */
- tg = container_of(task_css_check(tsk, cpu_cgrp_id, true),
- struct task_group, css);
- tg = autogroup_task_group(tsk, tg);
- tsk->sched_task_group = tg;
- #ifdef CONFIG_FAIR_GROUP_SCHED
- if (tsk->sched_class->task_change_group)
- tsk->sched_class->task_change_group(tsk, type);
- else
- #endif
- set_task_rq(tsk, task_cpu(tsk));
- }
- /*
- * Change task's runqueue when it moves between groups.
- *
- * The caller of this function should have put the task in its new group by
- * now. This function just updates tsk->se.cfs_rq and tsk->se.parent to reflect
- * its new group.
- */
- void sched_move_task(struct task_struct *tsk)
- {
- int queued, running, queue_flags =
- DEQUEUE_SAVE | DEQUEUE_MOVE | DEQUEUE_NOCLOCK;
- struct rq_flags rf;
- struct rq *rq;
- rq = task_rq_lock(tsk, &rf);
- update_rq_clock(rq);
- running = task_current(rq, tsk);
- queued = task_on_rq_queued(tsk);
- if (queued)
- dequeue_task(rq, tsk, queue_flags);
- if (running)
- put_prev_task(rq, tsk);
- sched_change_group(tsk, TASK_MOVE_GROUP);
- if (queued)
- enqueue_task(rq, tsk, queue_flags);
- if (running)
- set_curr_task(rq, tsk);
- task_rq_unlock(rq, tsk, &rf);
- }
- static inline struct task_group *css_tg(struct cgroup_subsys_state *css)
- {
- return css ? container_of(css, struct task_group, css) : NULL;
- }
- static struct cgroup_subsys_state *
- cpu_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
- {
- struct task_group *parent = css_tg(parent_css);
- struct task_group *tg;
- if (!parent) {
- /* This is early initialization for the top cgroup */
- return &root_task_group.css;
- }
- tg = sched_create_group(parent);
- if (IS_ERR(tg))
- return ERR_PTR(-ENOMEM);
- return &tg->css;
- }
- /* Expose task group only after completing cgroup initialization */
- static int cpu_cgroup_css_online(struct cgroup_subsys_state *css)
- {
- struct task_group *tg = css_tg(css);
- struct task_group *parent = css_tg(css->parent);
- if (parent)
- sched_online_group(tg, parent);
- return 0;
- }
- static void cpu_cgroup_css_released(struct cgroup_subsys_state *css)
- {
- struct task_group *tg = css_tg(css);
- sched_offline_group(tg);
- }
- static void cpu_cgroup_css_free(struct cgroup_subsys_state *css)
- {
- struct task_group *tg = css_tg(css);
- /*
- * Relies on the RCU grace period between css_released() and this.
- */
- sched_free_group(tg);
- }
- /*
- * This is called before wake_up_new_task(), therefore we really only
- * have to set its group bits, all the other stuff does not apply.
- */
- static void cpu_cgroup_fork(struct task_struct *task)
- {
- struct rq_flags rf;
- struct rq *rq;
- rq = task_rq_lock(task, &rf);
- update_rq_clock(rq);
- sched_change_group(task, TASK_SET_GROUP);
- task_rq_unlock(rq, task, &rf);
- }
- static int cpu_cgroup_can_attach(struct cgroup_taskset *tset)
- {
- struct task_struct *task;
- struct cgroup_subsys_state *css;
- int ret = 0;
- cgroup_taskset_for_each(task, css, tset) {
- #ifdef CONFIG_RT_GROUP_SCHED
- if (!sched_rt_can_attach(css_tg(css), task))
- return -EINVAL;
- #endif
- /*
- * Serialize against wake_up_new_task() such that if its
- * running, we're sure to observe its full state.
- */
- raw_spin_lock_irq(&task->pi_lock);
- /*
- * Avoid calling sched_move_task() before wake_up_new_task()
- * has happened. This would lead to problems with PELT, due to
- * move wanting to detach+attach while we're not attached yet.
- */
- if (task->state == TASK_NEW)
- ret = -EINVAL;
- raw_spin_unlock_irq(&task->pi_lock);
- if (ret)
- break;
- }
- return ret;
- }
- static void cpu_cgroup_attach(struct cgroup_taskset *tset)
- {
- struct task_struct *task;
- struct cgroup_subsys_state *css;
- cgroup_taskset_for_each(task, css, tset)
- sched_move_task(task);
- }
- #if defined(CONFIG_UCLAMP_TASK_GROUP) && !defined(CONFIG_SCHED_TUNE)
- /**
- * cpu_util_update_hier: propagate effective clamp down the hierarchy
- * @css: the task group to update
- * @clamp_id: the clamp index to update
- * @group_id: the group index mapping the new task clamp value
- * @value: the new task group clamp value
- *
- * The effective clamp for a TG is expected to track the most restrictive
- * value between the TG's clamp value and it's parent effective clamp value.
- * This method achieve that:
- * 1. updating the current TG effective value
- * 2. walking all the descendant task group that needs an update
- *
- * A TG's effective clamp needs to be updated when its current value is not
- * matching the TG's clamp value. In this case indeed either:
- * a) the parent has got a more relaxed clamp value
- * thus potentially we can relax the effective value for this group
- * b) the parent has got a more strict clamp value
- * thus potentially we have to restrict the effective value of this group
- *
- * Restriction and relaxation of current TG's effective clamp values needs to
- * be propagated down to all the descendants. When a subgroup is found which
- * has already its effective clamp value matching its clamp value, then we can
- * safely skip all its descendants which are granted to be already in sync.
- *
- * The TG's group_id is also updated to ensure it tracks the effective clamp
- * value.
- */
- static void cpu_util_update_hier(struct cgroup_subsys_state *css,
- unsigned int clamp_id, unsigned int group_id,
- unsigned int value)
- {
- struct cgroup_subsys_state *top_css = css;
- struct uclamp_se *uc_se, *uc_parent;
- css_for_each_descendant_pre(css, top_css) {
- /*
- * The first visited task group is top_css, which clamp value
- * is the one passed as parameter. For descendent task
- * groups we consider their current value.
- */
- uc_se = &css_tg(css)->uclamp[clamp_id];
- if (css != top_css) {
- value = uc_se->value;
- group_id = uc_se->effective.group_id;
- }
- /*
- * Skip the whole subtrees if the current effective clamp is
- * already matching the TG's clamp value.
- * In this case, all the subtrees already have top_value, or a
- * more restrictive, as effective clamp.
- */
- uc_parent = &css_tg(css)->parent->uclamp[clamp_id];
- if (uc_se->effective.value == value &&
- uc_parent->effective.value >= value) {
- css = css_rightmost_descendant(css);
- continue;
- }
- /* Propagate the most restrictive effective value */
- if (uc_parent->effective.value < value) {
- value = uc_parent->effective.value;
- group_id = uc_parent->effective.group_id;
- }
- if (uc_se->effective.value == value)
- continue;
- uc_se->effective.value = value;
- uc_se->effective.group_id = group_id;
- /* Immediately updated descendants active tasks */
- if (css != top_css)
- uclamp_group_get_tg(css, clamp_id, group_id);
- }
- }
- static int cpu_util_min_write_u64(struct cgroup_subsys_state *css,
- struct cftype *cftype, u64 min_value)
- {
- struct task_group *tg;
- int ret = 0;
- if (min_value > SCHED_CAPACITY_SCALE)
- return -ERANGE;
- if (!opp_capacity_tbl_ready)
- init_opp_capacity_tbl();
- min_value = find_fit_capacity(min_value);
- mutex_lock(&uclamp_mutex);
- rcu_read_lock();
- tg = css_tg(css);
- if (tg->uclamp[UCLAMP_MIN].value == min_value)
- goto out;
- if (tg->uclamp[UCLAMP_MAX].value < min_value) {
- ret = -EINVAL;
- goto out;
- }
- /* Update TG's reference count */
- uclamp_group_get(NULL, css, &tg->uclamp[UCLAMP_MIN],
- UCLAMP_MIN, min_value);
- /* Update effective clamps to track the most restrictive value */
- cpu_util_update_hier(css, UCLAMP_MIN, tg->uclamp[UCLAMP_MIN].group_id,
- min_value);
- out:
- rcu_read_unlock();
- mutex_unlock(&uclamp_mutex);
- return ret;
- }
- static int cpu_util_max_write_u64(struct cgroup_subsys_state *css,
- struct cftype *cftype, u64 max_value)
- {
- struct task_group *tg;
- int ret = 0;
- if (max_value > SCHED_CAPACITY_SCALE)
- return -ERANGE;
- if (!opp_capacity_tbl_ready)
- init_opp_capacity_tbl();
- max_value = find_fit_capacity(max_value);
- mutex_lock(&uclamp_mutex);
- rcu_read_lock();
- tg = css_tg(css);
- if (tg->uclamp[UCLAMP_MAX].value == max_value)
- goto out;
- if (tg->uclamp[UCLAMP_MIN].value > max_value) {
- ret = -EINVAL;
- goto out;
- }
- /* Update TG's reference count */
- uclamp_group_get(NULL, css, &tg->uclamp[UCLAMP_MAX],
- UCLAMP_MAX, max_value);
- /* Update effective clamps to track the most restrictive value */
- cpu_util_update_hier(css, UCLAMP_MAX, tg->uclamp[UCLAMP_MAX].group_id,
- max_value);
- out:
- rcu_read_unlock();
- mutex_unlock(&uclamp_mutex);
- return ret;
- }
- static inline u64 cpu_uclamp_read(struct cgroup_subsys_state *css,
- enum uclamp_id clamp_id,
- bool effective)
- {
- struct task_group *tg;
- u64 util_clamp;
- rcu_read_lock();
- tg = css_tg(css);
- util_clamp = effective
- ? tg->uclamp[clamp_id].effective.value
- : tg->uclamp[clamp_id].value;
- rcu_read_unlock();
- return util_clamp;
- }
- static u64 cpu_util_min_read_u64(struct cgroup_subsys_state *css,
- struct cftype *cft)
- {
- return cpu_uclamp_read(css, UCLAMP_MIN, false);
- }
- static u64 cpu_util_max_read_u64(struct cgroup_subsys_state *css,
- struct cftype *cft)
- {
- return cpu_uclamp_read(css, UCLAMP_MAX, false);
- }
- static u64 cpu_util_min_effective_read_u64(struct cgroup_subsys_state *css,
- struct cftype *cft)
- {
- return cpu_uclamp_read(css, UCLAMP_MIN, true);
- }
- static u64 cpu_util_max_effective_read_u64(struct cgroup_subsys_state *css,
- struct cftype *cft)
- {
- return cpu_uclamp_read(css, UCLAMP_MAX, true);
- }
- #endif /* CONFIG_UCLAMP_TASK_GROUP */
- #ifdef CONFIG_FAIR_GROUP_SCHED
- static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
- struct cftype *cftype, u64 shareval)
- {
- if (shareval > scale_load_down(ULONG_MAX))
- shareval = MAX_SHARES;
- return sched_group_set_shares(css_tg(css), scale_load(shareval));
- }
- static u64 cpu_shares_read_u64(struct cgroup_subsys_state *css,
- struct cftype *cft)
- {
- struct task_group *tg = css_tg(css);
- return (u64) scale_load_down(tg->shares);
- }
- #ifdef CONFIG_CFS_BANDWIDTH
- static DEFINE_MUTEX(cfs_constraints_mutex);
- const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
- const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
- static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
- static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
- {
- int i, ret = 0, runtime_enabled, runtime_was_enabled;
- struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
- if (tg == &root_task_group)
- return -EINVAL;
- /*
- * Ensure we have at some amount of bandwidth every period. This is
- * to prevent reaching a state of large arrears when throttled via
- * entity_tick() resulting in prolonged exit starvation.
- */
- if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
- return -EINVAL;
- /*
- * Likewise, bound things on the otherside by preventing insane quota
- * periods. This also allows us to normalize in computing quota
- * feasibility.
- */
- if (period > max_cfs_quota_period)
- return -EINVAL;
- /*
- * Prevent race between setting of cfs_rq->runtime_enabled and
- * unthrottle_offline_cfs_rqs().
- */
- get_online_cpus();
- mutex_lock(&cfs_constraints_mutex);
- ret = __cfs_schedulable(tg, period, quota);
- if (ret)
- goto out_unlock;
- runtime_enabled = quota != RUNTIME_INF;
- runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
- /*
- * If we need to toggle cfs_bandwidth_used, off->on must occur
- * before making related changes, and on->off must occur afterwards
- */
- if (runtime_enabled && !runtime_was_enabled)
- cfs_bandwidth_usage_inc();
- raw_spin_lock_irq(&cfs_b->lock);
- cfs_b->period = ns_to_ktime(period);
- cfs_b->quota = quota;
- __refill_cfs_bandwidth_runtime(cfs_b);
- /* Restart the period timer (if active) to handle new period expiry: */
- if (runtime_enabled)
- start_cfs_bandwidth(cfs_b);
- raw_spin_unlock_irq(&cfs_b->lock);
- for_each_online_cpu(i) {
- struct cfs_rq *cfs_rq = tg->cfs_rq[i];
- struct rq *rq = cfs_rq->rq;
- struct rq_flags rf;
- rq_lock_irq(rq, &rf);
- cfs_rq->runtime_enabled = runtime_enabled;
- cfs_rq->runtime_remaining = 0;
- if (cfs_rq->throttled)
- unthrottle_cfs_rq(cfs_rq);
- rq_unlock_irq(rq, &rf);
- }
- if (runtime_was_enabled && !runtime_enabled)
- cfs_bandwidth_usage_dec();
- out_unlock:
- mutex_unlock(&cfs_constraints_mutex);
- put_online_cpus();
- return ret;
- }
- int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
- {
- u64 quota, period;
- period = ktime_to_ns(tg->cfs_bandwidth.period);
- if (cfs_quota_us < 0)
- quota = RUNTIME_INF;
- else if ((u64)cfs_quota_us <= U64_MAX / NSEC_PER_USEC)
- quota = (u64)cfs_quota_us * NSEC_PER_USEC;
- else
- return -EINVAL;
- return tg_set_cfs_bandwidth(tg, period, quota);
- }
- long tg_get_cfs_quota(struct task_group *tg)
- {
- u64 quota_us;
- if (tg->cfs_bandwidth.quota == RUNTIME_INF)
- return -1;
- quota_us = tg->cfs_bandwidth.quota;
- do_div(quota_us, NSEC_PER_USEC);
- return quota_us;
- }
- int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
- {
- u64 quota, period;
- if ((u64)cfs_period_us > U64_MAX / NSEC_PER_USEC)
- return -EINVAL;
- period = (u64)cfs_period_us * NSEC_PER_USEC;
- quota = tg->cfs_bandwidth.quota;
- return tg_set_cfs_bandwidth(tg, period, quota);
- }
- long tg_get_cfs_period(struct task_group *tg)
- {
- u64 cfs_period_us;
- cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
- do_div(cfs_period_us, NSEC_PER_USEC);
- return cfs_period_us;
- }
- static s64 cpu_cfs_quota_read_s64(struct cgroup_subsys_state *css,
- struct cftype *cft)
- {
- return tg_get_cfs_quota(css_tg(css));
- }
- static int cpu_cfs_quota_write_s64(struct cgroup_subsys_state *css,
- struct cftype *cftype, s64 cfs_quota_us)
- {
- return tg_set_cfs_quota(css_tg(css), cfs_quota_us);
- }
- static u64 cpu_cfs_period_read_u64(struct cgroup_subsys_state *css,
- struct cftype *cft)
- {
- return tg_get_cfs_period(css_tg(css));
- }
- static int cpu_cfs_period_write_u64(struct cgroup_subsys_state *css,
- struct cftype *cftype, u64 cfs_period_us)
- {
- return tg_set_cfs_period(css_tg(css), cfs_period_us);
- }
- struct cfs_schedulable_data {
- struct task_group *tg;
- u64 period, quota;
- };
- /*
- * normalize group quota/period to be quota/max_period
- * note: units are usecs
- */
- static u64 normalize_cfs_quota(struct task_group *tg,
- struct cfs_schedulable_data *d)
- {
- u64 quota, period;
- if (tg == d->tg) {
- period = d->period;
- quota = d->quota;
- } else {
- period = tg_get_cfs_period(tg);
- quota = tg_get_cfs_quota(tg);
- }
- /* note: these should typically be equivalent */
- if (quota == RUNTIME_INF || quota == -1)
- return RUNTIME_INF;
- return to_ratio(period, quota);
- }
- static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
- {
- struct cfs_schedulable_data *d = data;
- struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
- s64 quota = 0, parent_quota = -1;
- if (!tg->parent) {
- quota = RUNTIME_INF;
- } else {
- struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
- quota = normalize_cfs_quota(tg, d);
- parent_quota = parent_b->hierarchical_quota;
- /*
- * Ensure max(child_quota) <= parent_quota, inherit when no
- * limit is set:
- */
- if (quota == RUNTIME_INF)
- quota = parent_quota;
- else if (parent_quota != RUNTIME_INF && quota > parent_quota)
- return -EINVAL;
- }
- cfs_b->hierarchical_quota = quota;
- return 0;
- }
- static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
- {
- int ret;
- struct cfs_schedulable_data data = {
- .tg = tg,
- .period = period,
- .quota = quota,
- };
- if (quota != RUNTIME_INF) {
- do_div(data.period, NSEC_PER_USEC);
- do_div(data.quota, NSEC_PER_USEC);
- }
- rcu_read_lock();
- ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
- rcu_read_unlock();
- return ret;
- }
- static int cpu_stats_show(struct seq_file *sf, void *v)
- {
- struct task_group *tg = css_tg(seq_css(sf));
- struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
- seq_printf(sf, "nr_periods %d\n", cfs_b->nr_periods);
- seq_printf(sf, "nr_throttled %d\n", cfs_b->nr_throttled);
- seq_printf(sf, "throttled_time %llu\n", cfs_b->throttled_time);
- return 0;
- }
- #endif /* CONFIG_CFS_BANDWIDTH */
- #endif /* CONFIG_FAIR_GROUP_SCHED */
- #ifdef CONFIG_RT_GROUP_SCHED
- static int cpu_rt_runtime_write(struct cgroup_subsys_state *css,
- struct cftype *cft, s64 val)
- {
- return sched_group_set_rt_runtime(css_tg(css), val);
- }
- static s64 cpu_rt_runtime_read(struct cgroup_subsys_state *css,
- struct cftype *cft)
- {
- return sched_group_rt_runtime(css_tg(css));
- }
- static int cpu_rt_period_write_uint(struct cgroup_subsys_state *css,
- struct cftype *cftype, u64 rt_period_us)
- {
- return sched_group_set_rt_period(css_tg(css), rt_period_us);
- }
- static u64 cpu_rt_period_read_uint(struct cgroup_subsys_state *css,
- struct cftype *cft)
- {
- return sched_group_rt_period(css_tg(css));
- }
- #endif /* CONFIG_RT_GROUP_SCHED */
- static struct cftype cpu_files[] = {
- #ifdef CONFIG_FAIR_GROUP_SCHED
- {
- .name = "shares",
- .read_u64 = cpu_shares_read_u64,
- .write_u64 = cpu_shares_write_u64,
- },
- #endif
- #ifdef CONFIG_CFS_BANDWIDTH
- {
- .name = "cfs_quota_us",
- .read_s64 = cpu_cfs_quota_read_s64,
- .write_s64 = cpu_cfs_quota_write_s64,
- },
- {
- .name = "cfs_period_us",
- .read_u64 = cpu_cfs_period_read_u64,
- .write_u64 = cpu_cfs_period_write_u64,
- },
- {
- .name = "stat",
- .seq_show = cpu_stats_show,
- },
- #endif
- #ifdef CONFIG_RT_GROUP_SCHED
- {
- .name = "rt_runtime_us",
- .read_s64 = cpu_rt_runtime_read,
- .write_s64 = cpu_rt_runtime_write,
- },
- {
- .name = "rt_period_us",
- .read_u64 = cpu_rt_period_read_uint,
- .write_u64 = cpu_rt_period_write_uint,
- },
- #endif
- #if defined(CONFIG_UCLAMP_TASK_GROUP) && !defined(CONFIG_SCHED_TUNE)
- {
- .name = "util.min",
- .read_u64 = cpu_util_min_read_u64,
- .write_u64 = cpu_util_min_write_u64,
- },
- {
- .name = "util.min.effective",
- .read_u64 = cpu_util_min_effective_read_u64,
- },
- {
- .name = "util.max",
- .read_u64 = cpu_util_max_read_u64,
- .write_u64 = cpu_util_max_write_u64,
- },
- {
- .name = "util.max.effective",
- .read_u64 = cpu_util_max_effective_read_u64,
- },
- #endif
- { } /* Terminate */
- };
- struct cgroup_subsys cpu_cgrp_subsys = {
- .css_alloc = cpu_cgroup_css_alloc,
- .css_online = cpu_cgroup_css_online,
- .css_released = cpu_cgroup_css_released,
- .css_free = cpu_cgroup_css_free,
- .fork = cpu_cgroup_fork,
- .can_attach = cpu_cgroup_can_attach,
- .attach = cpu_cgroup_attach,
- .legacy_cftypes = cpu_files,
- .early_init = true,
- };
- #endif /* CONFIG_CGROUP_SCHED */
- void dump_cpu_task(int cpu)
- {
- pr_info("Task dump for CPU %d:\n", cpu);
- sched_show_task(cpu_curr(cpu));
- }
- /*
- * Nice levels are multiplicative, with a gentle 10% change for every
- * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
- * nice 1, it will get ~10% less CPU time than another CPU-bound task
- * that remained on nice 0.
- *
- * The "10% effect" is relative and cumulative: from _any_ nice level,
- * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
- * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
- * If a task goes up by ~10% and another task goes down by ~10% then
- * the relative distance between them is ~25%.)
- */
- const int sched_prio_to_weight[40] = {
- /* -20 */ 88761, 71755, 56483, 46273, 36291,
- /* -15 */ 29154, 23254, 18705, 14949, 11916,
- /* -10 */ 9548, 7620, 6100, 4904, 3906,
- /* -5 */ 3121, 2501, 1991, 1586, 1277,
- /* 0 */ 1024, 820, 655, 526, 423,
- /* 5 */ 335, 272, 215, 172, 137,
- /* 10 */ 110, 87, 70, 56, 45,
- /* 15 */ 36, 29, 23, 18, 15,
- };
- /*
- * Inverse (2^32/x) values of the sched_prio_to_weight[] array, precalculated.
- *
- * In cases where the weight does not change often, we can use the
- * precalculated inverse to speed up arithmetics by turning divisions
- * into multiplications:
- */
- const u32 sched_prio_to_wmult[40] = {
- /* -20 */ 48388, 59856, 76040, 92818, 118348,
- /* -15 */ 147320, 184698, 229616, 287308, 360437,
- /* -10 */ 449829, 563644, 704093, 875809, 1099582,
- /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
- /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
- /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
- /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
- /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
- };
|