1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461 |
- #include <linux/mm.h>
- #include <linux/module.h>
- #include <linux/nmi.h>
- #include <linux/init.h>
- #include <linux/uaccess.h>
- #include <linux/highmem.h>
- #include <asm/mmu_context.h>
- #include <linux/interrupt.h>
- #include <linux/capability.h>
- #include <linux/completion.h>
- #include <linux/kernel_stat.h>
- #include <linux/debug_locks.h>
- #include <linux/perf_event.h>
- #include <linux/security.h>
- #include <linux/notifier.h>
- #include <linux/profile.h>
- #include <linux/freezer.h>
- #include <linux/vmalloc.h>
- #include <linux/blkdev.h>
- #include <linux/delay.h>
- #include <linux/pid_namespace.h>
- #include <linux/smp.h>
- #include <linux/threads.h>
- #include <linux/timer.h>
- #include <linux/rcupdate.h>
- #include <linux/cpu.h>
- #include <linux/cpuset.h>
- #include <linux/percpu.h>
- #include <linux/proc_fs.h>
- #include <linux/seq_file.h>
- #include <linux/sysctl.h>
- #include <linux/syscalls.h>
- #include <linux/times.h>
- #include <linux/tsacct_kern.h>
- #include <linux/kprobes.h>
- #include <linux/delayacct.h>
- #include <linux/unistd.h>
- #include <linux/pagemap.h>
- #include <linux/hrtimer.h>
- #include <linux/tick.h>
- #include <linux/debugfs.h>
- #include <linux/ctype.h>
- #include <linux/ftrace.h>
- #include <linux/slab.h>
- #include <linux/init_task.h>
- #include <linux/binfmts.h>
- #include <linux/context_tracking.h>
- #include <linux/compiler.h>
- #include <asm/switch_to.h>
- #include <asm/tlb.h>
- #include <asm/irq_regs.h>
- #include <asm/mutex.h>
- #ifdef CONFIG_PARAVIRT
- #include <asm/paravirt.h>
- #endif
- #include "sched.h"
- #include "../workqueue_internal.h"
- #include "../smpboot.h"
- #define CREATE_TRACE_POINTS
- #include <trace/events/sched.h>
- DEFINE_MUTEX(sched_domains_mutex);
- DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
- static void update_rq_clock_task(struct rq *rq, s64 delta);
- void update_rq_clock(struct rq *rq)
- {
- s64 delta;
- lockdep_assert_held(&rq->lock);
- if (rq->clock_skip_update & RQCF_ACT_SKIP)
- return;
- delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
- if (delta < 0)
- return;
- rq->clock += delta;
- update_rq_clock_task(rq, delta);
- }
- #define SCHED_FEAT(name, enabled) \
- (1UL << __SCHED_FEAT_##name) * enabled |
- const_debug unsigned int sysctl_sched_features =
- #include "features.h"
- 0;
- #undef SCHED_FEAT
- #ifdef CONFIG_SCHED_DEBUG
- #define SCHED_FEAT(name, enabled) \
- #name ,
- static const char * const sched_feat_names[] = {
- #include "features.h"
- };
- #undef SCHED_FEAT
- static int sched_feat_show(struct seq_file *m, void *v)
- {
- int i;
- for (i = 0; i < __SCHED_FEAT_NR; i++) {
- if (!(sysctl_sched_features & (1UL << i)))
- seq_puts(m, "NO_");
- seq_printf(m, "%s ", sched_feat_names[i]);
- }
- seq_puts(m, "\n");
- return 0;
- }
- #ifdef HAVE_JUMP_LABEL
- #define jump_label_key__true STATIC_KEY_INIT_TRUE
- #define jump_label_key__false STATIC_KEY_INIT_FALSE
- #define SCHED_FEAT(name, enabled) \
- jump_label_key__##enabled ,
- struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {
- #include "features.h"
- };
- #undef SCHED_FEAT
- static void sched_feat_disable(int i)
- {
- if (static_key_enabled(&sched_feat_keys[i]))
- static_key_slow_dec(&sched_feat_keys[i]);
- }
- static void sched_feat_enable(int i)
- {
- if (!static_key_enabled(&sched_feat_keys[i]))
- static_key_slow_inc(&sched_feat_keys[i]);
- }
- #else
- static void sched_feat_disable(int i) { };
- static void sched_feat_enable(int i) { };
- #endif /* HAVE_JUMP_LABEL */
- static int sched_feat_set(char *cmp)
- {
- int i;
- int neg = 0;
- if (strncmp(cmp, "NO_", 3) == 0) {
- neg = 1;
- cmp += 3;
- }
- for (i = 0; i < __SCHED_FEAT_NR; i++) {
- if (strcmp(cmp, sched_feat_names[i]) == 0) {
- if (neg) {
- sysctl_sched_features &= ~(1UL << i);
- sched_feat_disable(i);
- } else {
- sysctl_sched_features |= (1UL << i);
- sched_feat_enable(i);
- }
- break;
- }
- }
- return i;
- }
- static ssize_t
- sched_feat_write(struct file *filp, const char __user *ubuf,
- size_t cnt, loff_t *ppos)
- {
- char buf[64];
- char *cmp;
- int i;
- struct inode *inode;
- if (cnt > 63)
- cnt = 63;
- if (copy_from_user(&buf, ubuf, cnt))
- return -EFAULT;
- buf[cnt] = 0;
- cmp = strstrip(buf);
-
- inode = file_inode(filp);
- mutex_lock(&inode->i_mutex);
- i = sched_feat_set(cmp);
- mutex_unlock(&inode->i_mutex);
- if (i == __SCHED_FEAT_NR)
- return -EINVAL;
- *ppos += cnt;
- return cnt;
- }
- static int sched_feat_open(struct inode *inode, struct file *filp)
- {
- return single_open(filp, sched_feat_show, NULL);
- }
- static const struct file_operations sched_feat_fops = {
- .open = sched_feat_open,
- .write = sched_feat_write,
- .read = seq_read,
- .llseek = seq_lseek,
- .release = single_release,
- };
- static __init int sched_init_debug(void)
- {
- debugfs_create_file("sched_features", 0644, NULL, NULL,
- &sched_feat_fops);
- return 0;
- }
- late_initcall(sched_init_debug);
- #endif
- const_debug unsigned int sysctl_sched_nr_migrate = 32;
- const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
- unsigned int sysctl_sched_rt_period = 1000000;
- __read_mostly int scheduler_running;
- int sysctl_sched_rt_runtime = 950000;
- cpumask_var_t cpu_isolated_map;
- static struct rq *this_rq_lock(void)
- __acquires(rq->lock)
- {
- struct rq *rq;
- local_irq_disable();
- rq = this_rq();
- raw_spin_lock(&rq->lock);
- return rq;
- }
- #ifdef CONFIG_SCHED_HRTICK
- static void hrtick_clear(struct rq *rq)
- {
- if (hrtimer_active(&rq->hrtick_timer))
- hrtimer_cancel(&rq->hrtick_timer);
- }
- static enum hrtimer_restart hrtick(struct hrtimer *timer)
- {
- struct rq *rq = container_of(timer, struct rq, hrtick_timer);
- WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
- raw_spin_lock(&rq->lock);
- update_rq_clock(rq);
- rq->curr->sched_class->task_tick(rq, rq->curr, 1);
- raw_spin_unlock(&rq->lock);
- 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);
- }
- static void __hrtick_start(void *arg)
- {
- struct rq *rq = arg;
- raw_spin_lock(&rq->lock);
- __hrtick_restart(rq);
- rq->hrtick_csd_pending = 0;
- raw_spin_unlock(&rq->lock);
- }
- void hrtick_start(struct rq *rq, u64 delay)
- {
- struct hrtimer *timer = &rq->hrtick_timer;
- ktime_t time;
- s64 delta;
-
- 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;
- }
- }
- static int
- hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
- {
- int cpu = (int)(long)hcpu;
- switch (action) {
- case CPU_UP_CANCELED:
- case CPU_UP_CANCELED_FROZEN:
- case CPU_DOWN_PREPARE:
- case CPU_DOWN_PREPARE_FROZEN:
- case CPU_DEAD:
- case CPU_DEAD_FROZEN:
- hrtick_clear(cpu_rq(cpu));
- return NOTIFY_OK;
- }
- return NOTIFY_DONE;
- }
- static __init void init_hrtick(void)
- {
- hotcpu_notifier(hotplug_hrtick, 0);
- }
- #else
- void hrtick_start(struct rq *rq, u64 delay)
- {
-
- delay = max_t(u64, delay, 10000LL);
- hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay),
- HRTIMER_MODE_REL_PINNED);
- }
- static inline void init_hrtick(void)
- {
- }
- #endif
- 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
- static inline void hrtick_clear(struct rq *rq)
- {
- }
- static inline void init_rq_hrtick(struct rq *rq)
- {
- }
- static inline void init_hrtick(void)
- {
- }
- #endif
- #define fetch_or(ptr, val) \
- ({ typeof(*(ptr)) __old, __val = *(ptr); \
- for (;;) { \
- __old = cmpxchg((ptr), __val, __val | (val)); \
- if (__old == __val) \
- break; \
- __val = __old; \
- } \
- __old; \
- })
- #if defined(CONFIG_SMP) && defined(TIF_POLLING_NRFLAG)
- 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);
- }
- 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;
-
- if (cmpxchg(&node->next, NULL, WAKE_Q_TAIL))
- return;
- get_task_struct(task);
-
- *head->lastp = node;
- head->lastp = &node->next;
- }
- 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);
-
- node = node->next;
- task->wake_q.next = NULL;
-
- wake_up_process(task);
- put_task_struct(task);
- }
- }
- 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;
- if (!raw_spin_trylock_irqsave(&rq->lock, flags))
- return;
- resched_curr(rq);
- raw_spin_unlock_irqrestore(&rq->lock, flags);
- }
- #ifdef CONFIG_SMP
- #ifdef CONFIG_NO_HZ_COMMON
- int get_nohz_timer_target(void)
- {
- int i, cpu = smp_processor_id();
- struct sched_domain *sd;
- if (!idle_cpu(cpu))
- return cpu;
- rcu_read_lock();
- for_each_domain(cpu, sd) {
- for_each_cpu(i, sched_domain_span(sd)) {
- if (!idle_cpu(i)) {
- cpu = i;
- goto unlock;
- }
- }
- }
- unlock:
- rcu_read_unlock();
- return cpu;
- }
- 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)
- {
-
- 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;
- }
- 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;
-
- clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu));
- return false;
- }
- #else
- static inline bool got_nohz_idle_kick(void)
- {
- return false;
- }
- #endif
- #ifdef CONFIG_NO_HZ_FULL
- bool sched_can_stop_tick(void)
- {
-
- if (current->policy == SCHED_FIFO)
- return true;
-
- if (current->policy == SCHED_RR) {
- struct sched_rt_entity *rt_se = ¤t->rt;
- return rt_se->run_list.prev == rt_se->run_list.next;
- }
-
- if (this_rq()->nr_running > 1)
- return false;
- return true;
- }
- #endif
- void sched_avg_update(struct rq *rq)
- {
- s64 period = sched_avg_period();
- while ((s64)(rq_clock(rq) - rq->age_stamp) > period) {
-
- asm("" : "+rm" (rq->age_stamp));
- rq->age_stamp += period;
- rq->rt_avg /= 2;
- }
- }
- #endif
- #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
- (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
- 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;
-
- if (p->policy == SCHED_IDLE) {
- load->weight = scale_load(WEIGHT_IDLEPRIO);
- load->inv_weight = WMULT_IDLEPRIO;
- return;
- }
- load->weight = scale_load(prio_to_weight[prio]);
- load->inv_weight = prio_to_wmult[prio];
- }
- static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
- {
- update_rq_clock(rq);
- sched_info_queued(rq, p);
- p->sched_class->enqueue_task(rq, p, flags);
- }
- static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
- {
- update_rq_clock(rq);
- sched_info_dequeued(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);
- }
- static void update_rq_clock_task(struct rq *rq, s64 delta)
- {
- #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;
-
- 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 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) {
-
- sched_setscheduler_nocheck(stop, SCHED_FIFO, ¶m);
- stop->sched_class = &stop_sched_class;
- }
- cpu_rq(cpu)->stop = stop;
- if (old_stop) {
-
- old_stop->sched_class = &rt_sched_class;
- }
- }
- static inline int __normal_prio(struct task_struct *p)
- {
- return p->static_prio;
- }
- 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;
- }
- static int effective_prio(struct task_struct *p)
- {
- p->normal_prio = normal_prio(p);
-
- if (!rt_prio(p->prio))
- return p->normal_prio;
- return p->prio;
- }
- inline int task_curr(const struct task_struct *p)
- {
- return cpu_curr(task_cpu(p)) == p;
- }
- 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;
- }
- }
- }
-
- if (task_on_rq_queued(rq->curr) && test_tsk_need_resched(rq->curr))
- rq_clock_skip_update(rq, true);
- }
- #ifdef CONFIG_SMP
- static struct rq *move_queued_task(struct rq *rq, struct task_struct *p, int new_cpu)
- {
- lockdep_assert_held(&rq->lock);
- dequeue_task(rq, p, 0);
- p->on_rq = TASK_ON_RQ_MIGRATING;
- set_task_cpu(p, new_cpu);
- raw_spin_unlock(&rq->lock);
- rq = cpu_rq(new_cpu);
- raw_spin_lock(&rq->lock);
- BUG_ON(task_cpu(p) != new_cpu);
- p->on_rq = TASK_ON_RQ_QUEUED;
- enqueue_task(rq, p, 0);
- check_preempt_curr(rq, p, 0);
- return rq;
- }
- struct migration_arg {
- struct task_struct *task;
- int dest_cpu;
- };
- static struct rq *__migrate_task(struct rq *rq, struct task_struct *p, int dest_cpu)
- {
- if (unlikely(!cpu_active(dest_cpu)))
- return rq;
-
- if (!cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
- return rq;
- rq = move_queued_task(rq, p, dest_cpu);
- return rq;
- }
- static int migration_cpu_stop(void *data)
- {
- struct migration_arg *arg = data;
- struct task_struct *p = arg->task;
- struct rq *rq = this_rq();
-
- local_irq_disable();
-
- sched_ttwu_pending();
- raw_spin_lock(&p->pi_lock);
- raw_spin_lock(&rq->lock);
-
- if (task_rq(p) == rq && task_on_rq_queued(p))
- rq = __migrate_task(rq, p, arg->dest_cpu);
- raw_spin_unlock(&rq->lock);
- raw_spin_unlock(&p->pi_lock);
- local_irq_enable();
- return 0;
- }
- void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
- {
- if (p->sched_class->set_cpus_allowed)
- p->sched_class->set_cpus_allowed(p, new_mask);
- cpumask_copy(&p->cpus_allowed, new_mask);
- p->nr_cpus_allowed = cpumask_weight(new_mask);
- }
- int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
- {
- unsigned long flags;
- struct rq *rq;
- unsigned int dest_cpu;
- int ret = 0;
- rq = task_rq_lock(p, &flags);
- if (cpumask_equal(&p->cpus_allowed, new_mask))
- goto out;
- if (!cpumask_intersects(new_mask, cpu_active_mask)) {
- ret = -EINVAL;
- goto out;
- }
- do_set_cpus_allowed(p, new_mask);
-
- if (cpumask_test_cpu(task_cpu(p), new_mask))
- goto out;
- dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
- if (task_running(rq, p) || p->state == TASK_WAKING) {
- struct migration_arg arg = { p, dest_cpu };
-
- task_rq_unlock(rq, p, &flags);
- stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
- tlb_migrate_finish(p->mm);
- return 0;
- } else if (task_on_rq_queued(p)) {
-
- lockdep_unpin_lock(&rq->lock);
- rq = move_queued_task(rq, p, dest_cpu);
- lockdep_pin_lock(&rq->lock);
- }
- out:
- task_rq_unlock(rq, p, &flags);
- return ret;
- }
- EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
- void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
- {
- #ifdef CONFIG_SCHED_DEBUG
-
- WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
- !p->on_rq);
- #ifdef CONFIG_LOCKDEP
-
- WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
- lockdep_is_held(&task_rq(p)->lock)));
- #endif
- #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, new_cpu);
- p->se.nr_migrations++;
- perf_event_task_migrate(p);
- }
- __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;
- src_rq = task_rq(p);
- dst_rq = cpu_rq(cpu);
- deactivate_task(src_rq, p, 0);
- set_task_cpu(p, cpu);
- activate_task(dst_rq, p, 0);
- check_preempt_curr(dst_rq, p, 0);
- } else {
-
- 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;
- 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, tsk_cpus_allowed(arg->src_task)))
- goto unlock;
- if (!cpumask_test_cpu(arg->src_cpu, tsk_cpus_allowed(arg->dst_task)))
- 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;
- }
- 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;
-
- if (!cpu_active(arg.src_cpu) || !cpu_active(arg.dst_cpu))
- goto out;
- if (!cpumask_test_cpu(arg.dst_cpu, tsk_cpus_allowed(arg.src_task)))
- goto out;
- if (!cpumask_test_cpu(arg.src_cpu, tsk_cpus_allowed(arg.dst_task)))
- 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;
- }
- unsigned long wait_task_inactive(struct task_struct *p, long match_state)
- {
- unsigned long flags;
- int running, queued;
- unsigned long ncsw;
- struct rq *rq;
- for (;;) {
-
- rq = task_rq(p);
-
- while (task_running(rq, p)) {
- if (match_state && unlikely(p->state != match_state))
- return 0;
- cpu_relax();
- }
-
- rq = task_rq_lock(p, &flags);
- 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;
- task_rq_unlock(rq, p, &flags);
-
- if (unlikely(!ncsw))
- break;
-
- if (unlikely(running)) {
- cpu_relax();
- continue;
- }
-
- if (unlikely(queued)) {
- ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
- set_current_state(TASK_UNINTERRUPTIBLE);
- schedule_hrtimeout(&to, HRTIMER_MODE_REL);
- continue;
- }
-
- break;
- }
- return ncsw;
- }
- 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);
- static int select_fallback_rq(int cpu, struct task_struct *p)
- {
- int nid = cpu_to_node(cpu);
- const struct cpumask *nodemask = NULL;
- enum { cpuset, possible, fail } state = cpuset;
- int dest_cpu;
-
- if (nid != -1) {
- nodemask = cpumask_of_node(nid);
-
- for_each_cpu(dest_cpu, nodemask) {
- if (!cpu_online(dest_cpu))
- continue;
- if (!cpu_active(dest_cpu))
- continue;
- if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
- return dest_cpu;
- }
- }
- for (;;) {
-
- for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) {
- if (!cpu_online(dest_cpu))
- continue;
- if (!cpu_active(dest_cpu))
- continue;
- goto out;
- }
- switch (state) {
- case cpuset:
-
- cpuset_cpus_allowed_fallback(p);
- state = possible;
- break;
- case possible:
- do_set_cpus_allowed(p, cpu_possible_mask);
- state = fail;
- break;
- case fail:
- BUG();
- break;
- }
- }
- out:
- if (state != cpuset) {
-
- 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;
- }
- static inline
- int select_task_rq(struct task_struct *p, int cpu, int sd_flags, int wake_flags)
- {
- 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);
-
- if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) ||
- !cpu_online(cpu)))
- cpu = select_fallback_rq(task_cpu(p), p);
- return cpu;
- }
- static void update_avg(u64 *avg, u64 sample)
- {
- s64 diff = sample - *avg;
- *avg += diff >> 3;
- }
- #endif
- static void
- ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
- {
- #ifdef CONFIG_SCHEDSTATS
- struct rq *rq = this_rq();
- #ifdef CONFIG_SMP
- int this_cpu = smp_processor_id();
- if (cpu == this_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(this_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
- 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);
- #endif
- }
- static 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 (p->flags & PF_WQ_WORKER)
- wq_worker_waking_up(p, cpu_of(rq));
- }
- static void
- ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
- {
- check_preempt_curr(rq, p, wake_flags);
- trace_sched_wakeup(p, true);
- p->state = TASK_RUNNING;
- #ifdef CONFIG_SMP
- if (p->sched_class->task_woken) {
-
- lockdep_unpin_lock(&rq->lock);
- p->sched_class->task_woken(rq, p);
- lockdep_pin_lock(&rq->lock);
- }
- 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)
- {
- lockdep_assert_held(&rq->lock);
- #ifdef CONFIG_SMP
- if (p->sched_contributes_to_load)
- rq->nr_uninterruptible--;
- #endif
- ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);
- ttwu_do_wakeup(rq, p, wake_flags);
- }
- static int ttwu_remote(struct task_struct *p, int wake_flags)
- {
- struct rq *rq;
- int ret = 0;
- rq = __task_rq_lock(p);
- if (task_on_rq_queued(p)) {
-
- update_rq_clock(rq);
- ttwu_do_wakeup(rq, p, wake_flags);
- ret = 1;
- }
- __task_rq_unlock(rq);
- 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;
- unsigned long flags;
- if (!llist)
- return;
- raw_spin_lock_irqsave(&rq->lock, flags);
- lockdep_pin_lock(&rq->lock);
- while (llist) {
- p = llist_entry(llist, struct task_struct, wake_entry);
- llist = llist_next(llist);
- ttwu_do_activate(rq, p, 0);
- }
- lockdep_unpin_lock(&rq->lock);
- raw_spin_unlock_irqrestore(&rq->lock, flags);
- }
- void scheduler_ipi(void)
- {
-
- preempt_fold_need_resched();
- if (llist_empty(&this_rq()->wake_list) && !got_nohz_idle_kick())
- return;
-
- irq_enter();
- sched_ttwu_pending();
-
- if (unlikely(got_nohz_idle_kick())) {
- this_rq()->idle_balance = 1;
- raise_softirq_irqoff(SCHED_SOFTIRQ);
- }
- irq_exit();
- }
- static void ttwu_queue_remote(struct task_struct *p, int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- 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);
- unsigned long flags;
- 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 {
- raw_spin_lock_irqsave(&rq->lock, flags);
- if (is_idle_task(rq->curr))
- smp_send_reschedule(cpu);
-
- raw_spin_unlock_irqrestore(&rq->lock, flags);
- }
- 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
- static void ttwu_queue(struct task_struct *p, int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- #if defined(CONFIG_SMP)
- if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
- sched_clock_cpu(cpu);
- ttwu_queue_remote(p, cpu);
- return;
- }
- #endif
- raw_spin_lock(&rq->lock);
- lockdep_pin_lock(&rq->lock);
- ttwu_do_activate(rq, p, 0);
- lockdep_unpin_lock(&rq->lock);
- raw_spin_unlock(&rq->lock);
- }
- static int
- try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
- {
- unsigned long flags;
- int cpu, success = 0;
-
- smp_mb__before_spinlock();
- raw_spin_lock_irqsave(&p->pi_lock, flags);
- if (!(p->state & state))
- goto out;
- success = 1;
- cpu = task_cpu(p);
- if (p->on_rq && ttwu_remote(p, wake_flags))
- goto stat;
- #ifdef CONFIG_SMP
-
- while (p->on_cpu)
- cpu_relax();
-
- smp_rmb();
- p->sched_contributes_to_load = !!task_contributes_to_load(p);
- p->state = TASK_WAKING;
- if (p->sched_class->task_waking)
- p->sched_class->task_waking(p);
- cpu = select_task_rq(p, p->wake_cpu, SD_BALANCE_WAKE, wake_flags);
- if (task_cpu(p) != cpu) {
- wake_flags |= WF_MIGRATED;
- set_task_cpu(p, cpu);
- }
- #endif
- ttwu_queue(p, cpu);
- stat:
- ttwu_stat(p, cpu, wake_flags);
- out:
- raw_spin_unlock_irqrestore(&p->pi_lock, flags);
- return success;
- }
- static void try_to_wake_up_local(struct task_struct *p)
- {
- 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)) {
-
- lockdep_unpin_lock(&rq->lock);
- raw_spin_unlock(&rq->lock);
- raw_spin_lock(&p->pi_lock);
- raw_spin_lock(&rq->lock);
- lockdep_pin_lock(&rq->lock);
- }
- if (!(p->state & TASK_NORMAL))
- goto out;
- if (!task_on_rq_queued(p))
- ttwu_activate(rq, p, ENQUEUE_WAKEUP);
- ttwu_do_wakeup(rq, p, 0);
- ttwu_stat(p, smp_processor_id(), 0);
- out:
- raw_spin_unlock(&p->pi_lock);
- }
- int wake_up_process(struct task_struct *p)
- {
- WARN_ON(task_is_stopped_or_traced(p));
- return try_to_wake_up(p, TASK_NORMAL, 0);
- }
- EXPORT_SYMBOL(wake_up_process);
- int wake_up_state(struct task_struct *p, unsigned int state)
- {
- return try_to_wake_up(p, state, 0);
- }
- void __dl_clear_params(struct task_struct *p)
- {
- struct sched_dl_entity *dl_se = &p->dl;
- dl_se->dl_runtime = 0;
- dl_se->dl_deadline = 0;
- dl_se->dl_period = 0;
- dl_se->flags = 0;
- dl_se->dl_bw = 0;
- dl_se->dl_throttled = 0;
- dl_se->dl_new = 1;
- dl_se->dl_yielded = 0;
- }
- 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_SMP
- p->se.avg.decay_count = 0;
- #endif
- INIT_LIST_HEAD(&p->se.group_node);
- #ifdef CONFIG_SCHEDSTATS
- memset(&p->se.statistics, 0, sizeof(p->se.statistics));
- #endif
- RB_CLEAR_NODE(&p->dl.rb_node);
- init_dl_task_timer(&p->dl);
- __dl_clear_params(p);
- INIT_LIST_HEAD(&p->rt.run_list);
- #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
- }
- #ifdef CONFIG_NUMA_BALANCING
- #ifdef CONFIG_SCHED_DEBUG
- void set_numabalancing_state(bool enabled)
- {
- if (enabled)
- sched_feat_set("NUMA");
- else
- sched_feat_set("NO_NUMA");
- }
- #else
- __read_mostly bool numabalancing_enabled;
- void set_numabalancing_state(bool enabled)
- {
- numabalancing_enabled = enabled;
- }
- #endif
- #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 = numabalancing_enabled;
- 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
- int sched_fork(unsigned long clone_flags, struct task_struct *p)
- {
- unsigned long flags;
- int cpu = get_cpu();
- __sched_fork(clone_flags, p);
-
- p->state = TASK_RUNNING;
-
- p->prio = current->normal_prio;
-
- if (unlikely(p->sched_reset_on_fork)) {
- 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);
-
- 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;
- }
- if (p->sched_class->task_fork)
- p->sched_class->task_fork(p);
-
- raw_spin_lock_irqsave(&p->pi_lock, flags);
- set_task_cpu(p, cpu);
- 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 1ULL << 20;
-
- if (period == 0)
- return 0;
- return div64_u64(runtime << 20, period);
- }
- #ifdef CONFIG_SMP
- inline struct dl_bw *dl_bw_of(int i)
- {
- rcu_lockdep_assert(rcu_read_lock_sched_held(),
- "sched RCU must be held");
- return &cpu_rq(i)->rd->dl_bw;
- }
- static inline int dl_bw_cpus(int i)
- {
- struct root_domain *rd = cpu_rq(i)->rd;
- int cpus = 0;
- rcu_lockdep_assert(rcu_read_lock_sched_held(),
- "sched RCU must be held");
- for_each_cpu_and(i, rd->span, cpu_active_mask)
- cpus++;
- return cpus;
- }
- #else
- inline struct dl_bw *dl_bw_of(int i)
- {
- return &cpu_rq(i)->dl.dl_bw;
- }
- static inline int dl_bw_cpus(int i)
- {
- return 1;
- }
- #endif
- static int dl_overflow(struct task_struct *p, int policy,
- const struct sched_attr *attr)
- {
- struct dl_bw *dl_b = dl_bw_of(task_cpu(p));
- u64 period = attr->sched_period ?: attr->sched_deadline;
- u64 runtime = attr->sched_runtime;
- u64 new_bw = dl_policy(policy) ? to_ratio(period, runtime) : 0;
- int cpus, err = -1;
- if (new_bw == p->dl.dl_bw)
- return 0;
-
- raw_spin_lock(&dl_b->lock);
- cpus = dl_bw_cpus(task_cpu(p));
- if (dl_policy(policy) && !task_has_dl_policy(p) &&
- !__dl_overflow(dl_b, cpus, 0, new_bw)) {
- __dl_add(dl_b, new_bw);
- err = 0;
- } else if (dl_policy(policy) && task_has_dl_policy(p) &&
- !__dl_overflow(dl_b, cpus, p->dl.dl_bw, new_bw)) {
- __dl_clear(dl_b, p->dl.dl_bw);
- __dl_add(dl_b, new_bw);
- err = 0;
- } else if (!dl_policy(policy) && task_has_dl_policy(p)) {
- __dl_clear(dl_b, p->dl.dl_bw);
- err = 0;
- }
- raw_spin_unlock(&dl_b->lock);
- return err;
- }
- extern void init_dl_bw(struct dl_bw *dl_b);
- void wake_up_new_task(struct task_struct *p)
- {
- unsigned long flags;
- struct rq *rq;
- raw_spin_lock_irqsave(&p->pi_lock, flags);
- #ifdef CONFIG_SMP
-
- set_task_cpu(p, select_task_rq(p, task_cpu(p), SD_BALANCE_FORK, 0));
- #endif
-
- init_task_runnable_average(p);
- rq = __task_rq_lock(p);
- activate_task(rq, p, 0);
- p->on_rq = TASK_ON_RQ_QUEUED;
- trace_sched_wakeup_new(p, true);
- check_preempt_curr(rq, p, WF_FORK);
- #ifdef CONFIG_SMP
- if (p->sched_class->task_woken)
- p->sched_class->task_woken(rq, p);
- #endif
- task_rq_unlock(rq, p, &flags);
- }
- #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);
- 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);
- 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
- 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
- static inline void
- prepare_task_switch(struct rq *rq, struct task_struct *prev,
- struct task_struct *next)
- {
- trace_sched_switch(prev, next);
- sched_info_switch(rq, prev, next);
- perf_event_task_sched_out(prev, next);
- fire_sched_out_preempt_notifiers(prev, next);
- prepare_lock_switch(rq, next);
- prepare_arch_switch(next);
- }
- 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;
- rq->prev_mm = NULL;
-
- prev_state = prev->state;
- vtime_task_switch(prev);
- finish_arch_switch(prev);
- perf_event_task_sched_in(prev, current);
- finish_lock_switch(rq, prev);
- finish_arch_post_lock_switch();
- 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);
-
- kprobe_flush_task(prev);
- put_task_struct(prev);
- }
- tick_nohz_task_switch(current);
- return rq;
- }
- #ifdef CONFIG_SMP
- 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
- asmlinkage __visible void schedule_tail(struct task_struct *prev)
- __releases(rq->lock)
- {
- struct rq *rq;
-
- preempt_disable();
- 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);
- }
- static inline struct rq *
- context_switch(struct rq *rq, struct task_struct *prev,
- struct task_struct *next)
- {
- struct mm_struct *mm, *oldmm;
- prepare_task_switch(rq, prev, next);
- mm = next->mm;
- oldmm = prev->active_mm;
-
- arch_start_context_switch(prev);
- if (!mm) {
- next->active_mm = oldmm;
- atomic_inc(&oldmm->mm_count);
- enter_lazy_tlb(oldmm, next);
- } else
- switch_mm(oldmm, mm, next);
- if (!prev->mm) {
- prev->active_mm = NULL;
- rq->prev_mm = oldmm;
- }
-
- lockdep_unpin_lock(&rq->lock);
- spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
-
- switch_to(prev, next, prev);
- barrier();
- return finish_task_switch(prev);
- }
- unsigned long nr_running(void)
- {
- unsigned long i, sum = 0;
- for_each_online_cpu(i)
- sum += cpu_rq(i)->nr_running;
- return sum;
- }
- bool single_task_running(void)
- {
- if (cpu_rq(smp_processor_id())->nr_running == 1)
- return true;
- else
- return false;
- }
- 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;
- }
- 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;
- }
- 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
- 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);
- if (dest_cpu == smp_processor_id())
- goto unlock;
- if (likely(cpu_active(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);
- unsigned long long task_sched_runtime(struct task_struct *p)
- {
- unsigned long flags;
- struct rq *rq;
- u64 ns;
- #if defined(CONFIG_64BIT) && defined(CONFIG_SMP)
-
- if (!p->on_cpu || !task_on_rq_queued(p))
- return p->se.sum_exec_runtime;
- #endif
- rq = task_rq_lock(p, &flags);
-
- if (task_current(rq, p) && task_on_rq_queued(p)) {
- update_rq_clock(rq);
- p->sched_class->update_curr(rq);
- }
- ns = p->se.sum_exec_runtime;
- task_rq_unlock(rq, p, &flags);
- return ns;
- }
- void scheduler_tick(void)
- {
- int cpu = smp_processor_id();
- struct rq *rq = cpu_rq(cpu);
- struct task_struct *curr = rq->curr;
- sched_clock_tick();
- raw_spin_lock(&rq->lock);
- update_rq_clock(rq);
- curr->sched_class->task_tick(rq, curr, 0);
- update_cpu_load_active(rq);
- calc_global_load_tick(rq);
- raw_spin_unlock(&rq->lock);
- perf_event_task_tick();
- #ifdef CONFIG_SMP
- rq->idle_balance = idle_cpu(cpu);
- trigger_load_balance(rq);
- #endif
- rq_last_tick_reset(rq);
- }
- #ifdef CONFIG_NO_HZ_FULL
- 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
- notrace unsigned long get_parent_ip(unsigned long addr)
- {
- if (in_lock_functions(addr)) {
- addr = CALLER_ADDR2;
- if (in_lock_functions(addr))
- addr = CALLER_ADDR3;
- }
- return addr;
- }
- #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
- defined(CONFIG_PREEMPT_TRACER))
- void preempt_count_add(int val)
- {
- #ifdef CONFIG_DEBUG_PREEMPT
-
- if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
- return;
- #endif
- __preempt_count_add(val);
- #ifdef CONFIG_DEBUG_PREEMPT
-
- DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
- PREEMPT_MASK - 10);
- #endif
- if (preempt_count() == val) {
- unsigned long ip = get_parent_ip(CALLER_ADDR1);
- #ifdef CONFIG_DEBUG_PREEMPT
- current->preempt_disable_ip = ip;
- #endif
- trace_preempt_off(CALLER_ADDR0, ip);
- }
- }
- EXPORT_SYMBOL(preempt_count_add);
- NOKPROBE_SYMBOL(preempt_count_add);
- void preempt_count_sub(int val)
- {
- #ifdef CONFIG_DEBUG_PREEMPT
-
- if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
- return;
-
- if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
- !(preempt_count() & PREEMPT_MASK)))
- return;
- #endif
- if (preempt_count() == val)
- trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
- __preempt_count_sub(val);
- }
- EXPORT_SYMBOL(preempt_count_sub);
- NOKPROBE_SYMBOL(preempt_count_sub);
- #endif
- static noinline void __schedule_bug(struct task_struct *prev)
- {
- 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);
- #ifdef CONFIG_DEBUG_PREEMPT
- if (in_atomic_preempt_off()) {
- pr_err("Preemption disabled at:");
- print_ip_sym(current->preempt_disable_ip);
- pr_cont("\n");
- }
- #endif
- dump_stack();
- add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
- }
- static inline void schedule_debug(struct task_struct *prev)
- {
- #ifdef CONFIG_SCHED_STACK_END_CHECK
- BUG_ON(unlikely(task_stack_end_corrupted(prev)));
- #endif
-
- if (unlikely(in_atomic_preempt_off() && prev->state != TASK_DEAD))
- __schedule_bug(prev);
- rcu_sleep_check();
- profile_hit(SCHED_PROFILING, __builtin_return_address(0));
- schedstat_inc(this_rq(), sched_count);
- }
- static inline struct task_struct *
- pick_next_task(struct rq *rq, struct task_struct *prev)
- {
- const struct sched_class *class = &fair_sched_class;
- struct task_struct *p;
-
- if (likely(prev->sched_class == class &&
- rq->nr_running == rq->cfs.h_nr_running)) {
- p = fair_sched_class.pick_next_task(rq, prev);
- if (unlikely(p == RETRY_TASK))
- goto again;
-
- if (unlikely(!p))
- p = idle_sched_class.pick_next_task(rq, prev);
- return p;
- }
- again:
- for_each_class(class) {
- p = class->pick_next_task(rq, prev);
- if (p) {
- if (unlikely(p == RETRY_TASK))
- goto again;
- return p;
- }
- }
- BUG();
- }
- static void __sched __schedule(void)
- {
- struct task_struct *prev, *next;
- unsigned long *switch_count;
- struct rq *rq;
- int cpu;
- cpu = smp_processor_id();
- rq = cpu_rq(cpu);
- rcu_note_context_switch();
- prev = rq->curr;
- schedule_debug(prev);
- if (sched_feat(HRTICK))
- hrtick_clear(rq);
-
- smp_mb__before_spinlock();
- raw_spin_lock_irq(&rq->lock);
- lockdep_pin_lock(&rq->lock);
- rq->clock_skip_update <<= 1;
- switch_count = &prev->nivcsw;
- if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
- if (unlikely(signal_pending_state(prev->state, prev))) {
- prev->state = TASK_RUNNING;
- } else {
- deactivate_task(rq, prev, DEQUEUE_SLEEP);
- prev->on_rq = 0;
-
- if (prev->flags & PF_WQ_WORKER) {
- struct task_struct *to_wakeup;
- to_wakeup = wq_worker_sleeping(prev, cpu);
- if (to_wakeup)
- try_to_wake_up_local(to_wakeup);
- }
- }
- switch_count = &prev->nvcsw;
- }
- if (task_on_rq_queued(prev))
- update_rq_clock(rq);
- next = pick_next_task(rq, prev);
- clear_tsk_need_resched(prev);
- clear_preempt_need_resched();
- rq->clock_skip_update = 0;
- if (likely(prev != next)) {
- rq->nr_switches++;
- rq->curr = next;
- ++*switch_count;
- rq = context_switch(rq, prev, next);
- cpu = cpu_of(rq);
- } else {
- lockdep_unpin_lock(&rq->lock);
- raw_spin_unlock_irq(&rq->lock);
- }
- balance_callback(rq);
- }
- static inline void sched_submit_work(struct task_struct *tsk)
- {
- if (!tsk->state || tsk_is_pi_blocked(tsk))
- return;
-
- 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();
- sched_preempt_enable_no_resched();
- } while (need_resched());
- }
- EXPORT_SYMBOL(schedule);
- #ifdef CONFIG_CONTEXT_TRACKING
- asmlinkage __visible void __sched schedule_user(void)
- {
-
- enum ctx_state prev_state = exception_enter();
- schedule();
- exception_exit(prev_state);
- }
- #endif
- void __sched schedule_preempt_disabled(void)
- {
- sched_preempt_enable_no_resched();
- schedule();
- preempt_disable();
- }
- static void __sched notrace preempt_schedule_common(void)
- {
- do {
- preempt_active_enter();
- __schedule();
- preempt_active_exit();
-
- } while (need_resched());
- }
- #ifdef CONFIG_PREEMPT
- asmlinkage __visible void __sched notrace preempt_schedule(void)
- {
-
- if (likely(!preemptible()))
- return;
- preempt_schedule_common();
- }
- NOKPROBE_SYMBOL(preempt_schedule);
- EXPORT_SYMBOL(preempt_schedule);
- asmlinkage __visible void __sched notrace preempt_schedule_notrace(void)
- {
- enum ctx_state prev_ctx;
- if (likely(!preemptible()))
- return;
- do {
-
- __preempt_count_add(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET);
- barrier();
-
- prev_ctx = exception_enter();
- __schedule();
- exception_exit(prev_ctx);
- barrier();
- __preempt_count_sub(PREEMPT_ACTIVE + PREEMPT_DISABLE_OFFSET);
- } while (need_resched());
- }
- EXPORT_SYMBOL_GPL(preempt_schedule_notrace);
- #endif
- asmlinkage __visible void __sched preempt_schedule_irq(void)
- {
- enum ctx_state prev_state;
-
- BUG_ON(preempt_count() || !irqs_disabled());
- prev_state = exception_enter();
- do {
- preempt_active_enter();
- local_irq_enable();
- __schedule();
- local_irq_disable();
- preempt_active_exit();
- } while (need_resched());
- exception_exit(prev_state);
- }
- int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
- void *key)
- {
- return try_to_wake_up(curr->private, mode, wake_flags);
- }
- EXPORT_SYMBOL(default_wake_function);
- #ifdef CONFIG_RT_MUTEXES
- void rt_mutex_setprio(struct task_struct *p, int prio)
- {
- int oldprio, queued, running, enqueue_flag = 0;
- struct rq *rq;
- const struct sched_class *prev_class;
- BUG_ON(prio > MAX_PRIO);
- rq = __task_rq_lock(p);
-
- if (unlikely(p == rq->idle)) {
- WARN_ON(p != rq->curr);
- WARN_ON(p->pi_blocked_on);
- goto out_unlock;
- }
- trace_sched_pi_setprio(p, prio);
- oldprio = p->prio;
- prev_class = p->sched_class;
- queued = task_on_rq_queued(p);
- running = task_current(rq, p);
- if (queued)
- dequeue_task(rq, p, 0);
- if (running)
- put_prev_task(rq, p);
-
- if (dl_prio(prio)) {
- struct task_struct *pi_task = rt_mutex_get_top_task(p);
- if (!dl_prio(p->normal_prio) ||
- (pi_task && dl_entity_preempt(&pi_task->dl, &p->dl))) {
- p->dl.dl_boosted = 1;
- enqueue_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)
- enqueue_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 (running)
- p->sched_class->set_curr_task(rq);
- if (queued)
- enqueue_task(rq, p, enqueue_flag);
- check_class_changed(rq, p, prev_class, oldprio);
- out_unlock:
- preempt_disable();
- __task_rq_unlock(rq);
- balance_callback(rq);
- preempt_enable();
- }
- #endif
- void set_user_nice(struct task_struct *p, long nice)
- {
- int old_prio, delta, queued;
- unsigned long flags;
- struct rq *rq;
- if (task_nice(p) == nice || nice < MIN_NICE || nice > MAX_NICE)
- return;
-
- rq = task_rq_lock(p, &flags);
-
- 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);
- if (queued)
- dequeue_task(rq, p, 0);
- 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, 0);
-
- if (delta < 0 || (delta > 0 && task_running(rq, p)))
- resched_curr(rq);
- }
- out_unlock:
- task_rq_unlock(rq, p, &flags);
- }
- EXPORT_SYMBOL(set_user_nice);
- int can_nice(const struct task_struct *p, const int nice)
- {
-
- int nice_rlim = nice_to_rlimit(nice);
- return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
- capable(CAP_SYS_NICE));
- }
- #ifdef __ARCH_WANT_SYS_NICE
- SYSCALL_DEFINE1(nice, int, increment)
- {
- long nice, retval;
-
- 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
- int task_prio(const struct task_struct *p)
- {
- return p->prio - MAX_RT_PRIO;
- }
- 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;
- }
- struct task_struct *idle_task(int cpu)
- {
- return cpu_rq(cpu)->idle;
- }
- static struct task_struct *find_process_by_pid(pid_t pid)
- {
- return pid ? find_task_by_vpid(pid) : current;
- }
- static void
- __setparam_dl(struct task_struct *p, const struct sched_attr *attr)
- {
- struct sched_dl_entity *dl_se = &p->dl;
- dl_se->dl_runtime = attr->sched_runtime;
- dl_se->dl_deadline = attr->sched_deadline;
- dl_se->dl_period = attr->sched_period ?: dl_se->dl_deadline;
- dl_se->flags = attr->sched_flags;
- dl_se->dl_bw = to_ratio(dl_se->dl_period, dl_se->dl_runtime);
-
- }
- #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;
- p->policy = policy;
- if (dl_policy(policy))
- __setparam_dl(p, attr);
- else if (fair_policy(policy))
- p->static_prio = NICE_TO_PRIO(attr->sched_nice);
-
- p->rt_priority = attr->sched_priority;
- p->normal_prio = normal_prio(p);
- set_load_weight(p);
- }
- static void __setscheduler(struct rq *rq, struct task_struct *p,
- const struct sched_attr *attr, bool keep_boost)
- {
- __setscheduler_params(p, attr);
-
- if (keep_boost)
- p->prio = rt_mutex_get_effective_prio(p, normal_prio(p));
- else
- p->prio = normal_prio(p);
- if (dl_prio(p->prio))
- p->sched_class = &dl_sched_class;
- else if (rt_prio(p->prio))
- p->sched_class = &rt_sched_class;
- else
- p->sched_class = &fair_sched_class;
- }
- static void
- __getparam_dl(struct task_struct *p, struct sched_attr *attr)
- {
- struct sched_dl_entity *dl_se = &p->dl;
- attr->sched_priority = p->rt_priority;
- attr->sched_runtime = dl_se->dl_runtime;
- attr->sched_deadline = dl_se->dl_deadline;
- attr->sched_period = dl_se->dl_period;
- attr->sched_flags = dl_se->flags;
- }
- static bool
- __checkparam_dl(const struct sched_attr *attr)
- {
-
- if (attr->sched_deadline == 0)
- return false;
-
- if (attr->sched_runtime < (1ULL << DL_SCALE))
- return false;
-
- if (attr->sched_deadline & (1ULL << 63) ||
- attr->sched_period & (1ULL << 63))
- return false;
-
- if ((attr->sched_period != 0 &&
- attr->sched_period < attr->sched_deadline) ||
- attr->sched_deadline < attr->sched_runtime)
- return false;
- return true;
- }
- 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 bool dl_param_changed(struct task_struct *p,
- const struct sched_attr *attr)
- {
- struct sched_dl_entity *dl_se = &p->dl;
- if (dl_se->dl_runtime != attr->sched_runtime ||
- dl_se->dl_deadline != attr->sched_deadline ||
- dl_se->dl_period != attr->sched_period ||
- dl_se->flags != attr->sched_flags)
- return true;
- return false;
- }
- 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;
- unsigned long flags;
- const struct sched_class *prev_class;
- struct rq *rq;
- int reset_on_fork;
-
- BUG_ON(in_interrupt());
- recheck:
-
- 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 (policy != SCHED_DEADLINE &&
- policy != SCHED_FIFO && policy != SCHED_RR &&
- policy != SCHED_NORMAL && policy != SCHED_BATCH &&
- policy != SCHED_IDLE)
- return -EINVAL;
- }
- if (attr->sched_flags & ~(SCHED_FLAG_RESET_ON_FORK))
- return -EINVAL;
-
- 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;
-
- 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);
-
- if (policy != p->policy && !rlim_rtprio)
- return -EPERM;
-
- if (attr->sched_priority > p->rt_priority &&
- attr->sched_priority > rlim_rtprio)
- return -EPERM;
- }
-
- if (dl_policy(policy))
- return -EPERM;
-
- if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) {
- if (!can_nice(p, task_nice(p)))
- return -EPERM;
- }
-
- if (!check_same_owner(p))
- return -EPERM;
-
- if (p->sched_reset_on_fork && !reset_on_fork)
- return -EPERM;
- }
- if (user) {
- retval = security_task_setscheduler(p);
- if (retval)
- return retval;
- }
-
- rq = task_rq_lock(p, &flags);
-
- if (p == rq->stop) {
- task_rq_unlock(rq, p, &flags);
- return -EINVAL;
- }
-
- 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, &flags);
- return 0;
- }
- change:
- if (user) {
- #ifdef CONFIG_RT_GROUP_SCHED
-
- 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, &flags);
- return -EPERM;
- }
- #endif
- #ifdef CONFIG_SMP
- if (dl_bandwidth_enabled() && dl_policy(policy)) {
- cpumask_t *span = rq->rd->span;
-
- if (!cpumask_subset(span, &p->cpus_allowed) ||
- rq->rd->dl_bw.bw == 0) {
- task_rq_unlock(rq, p, &flags);
- return -EPERM;
- }
- }
- #endif
- }
-
- if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
- policy = oldpolicy = -1;
- task_rq_unlock(rq, p, &flags);
- goto recheck;
- }
-
- if ((dl_policy(policy) || dl_task(p)) && dl_overflow(p, policy, attr)) {
- task_rq_unlock(rq, p, &flags);
- return -EBUSY;
- }
- p->sched_reset_on_fork = reset_on_fork;
- oldprio = p->prio;
- if (pi) {
-
- new_effective_prio = rt_mutex_get_effective_prio(p, newprio);
- if (new_effective_prio == oldprio) {
- __setscheduler_params(p, attr);
- task_rq_unlock(rq, p, &flags);
- return 0;
- }
- }
- queued = task_on_rq_queued(p);
- running = task_current(rq, p);
- if (queued)
- dequeue_task(rq, p, 0);
- if (running)
- put_prev_task(rq, p);
- prev_class = p->sched_class;
- __setscheduler(rq, p, attr, pi);
- if (running)
- p->sched_class->set_curr_task(rq);
- if (queued) {
-
- enqueue_task(rq, p, oldprio <= p->prio ? ENQUEUE_HEAD : 0);
- }
- check_class_changed(rq, p, prev_class, oldprio);
- preempt_disable();
- task_rq_unlock(rq, p, &flags);
- if (pi)
- rt_mutex_adjust_pi(p);
-
- 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),
- };
-
- 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);
- }
- 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);
- int sched_setscheduler_nocheck(struct task_struct *p, int policy,
- const struct sched_param *param)
- {
- return _sched_setscheduler(p, policy, param, false);
- }
- 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;
- }
- 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;
-
- memset(attr, 0, sizeof(*attr));
- ret = get_user(size, &uattr->size);
- if (ret)
- return ret;
- if (size > PAGE_SIZE)
- goto err_size;
- if (!size)
- size = SCHED_ATTR_SIZE_VER0;
- if (size < SCHED_ATTR_SIZE_VER0)
- goto err_size;
-
- 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;
-
- attr->sched_nice = clamp(attr->sched_nice, MIN_NICE, MAX_NICE);
- return 0;
- err_size:
- put_user(sizeof(*attr), &uattr->size);
- return -E2BIG;
- }
- 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);
- }
- SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
- {
- return do_sched_setscheduler(pid, SETPARAM_POLICY, param);
- }
- 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;
- }
- 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;
- }
- 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();
-
- 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 (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;
- }
- 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;
- rcu_read_lock();
- p = find_process_by_pid(pid);
- if (!p) {
- rcu_read_unlock();
- return -ESRCH;
- }
-
- 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);
-
- #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:
- retval = set_cpus_allowed_ptr(p, new_mask);
- if (!retval) {
- cpuset_cpus_allowed(p, cpus_allowed);
- if (!cpumask_subset(new_mask, cpus_allowed)) {
-
- cpumask_copy(new_mask, cpus_allowed);
- goto again;
- }
- }
- 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;
- }
- 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;
- }
- 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;
- }
- SYSCALL_DEFINE0(sched_yield)
- {
- struct rq *rq = this_rq_lock();
- schedstat_inc(rq, yld_count);
- current->sched_class->yield_task(rq);
-
- __release(rq->lock);
- spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
- do_raw_spin_unlock(&rq->lock);
- sched_preempt_enable_no_resched();
- schedule();
- return 0;
- }
- int __sched _cond_resched(void)
- {
- if (should_resched()) {
- preempt_schedule_common();
- return 1;
- }
- return 0;
- }
- EXPORT_SYMBOL(_cond_resched);
- int __cond_resched_lock(spinlock_t *lock)
- {
- int resched = should_resched();
- 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()) {
- local_bh_enable();
- preempt_schedule_common();
- local_bh_disable();
- return 1;
- }
- return 0;
- }
- EXPORT_SYMBOL(__cond_resched_softirq);
- void __sched yield(void)
- {
- set_current_state(TASK_RUNNING);
- sys_sched_yield();
- }
- EXPORT_SYMBOL(yield);
- 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 (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);
-
- 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);
- long __sched io_schedule_timeout(long timeout)
- {
- int old_iowait = current->in_iowait;
- struct rq *rq;
- long ret;
- current->in_iowait = 1;
- blk_schedule_flush_plug(current);
- delayacct_blkio_start();
- rq = raw_rq();
- atomic_inc(&rq->nr_iowait);
- ret = schedule_timeout(timeout);
- current->in_iowait = old_iowait;
- atomic_dec(&rq->nr_iowait);
- delayacct_blkio_end();
- return ret;
- }
- EXPORT_SYMBOL(io_schedule_timeout);
- 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;
- }
- 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;
- }
- SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
- struct timespec __user *, interval)
- {
- struct task_struct *p;
- unsigned int time_slice;
- unsigned long flags;
- struct rq *rq;
- int retval;
- struct timespec t;
- 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, &flags);
- 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, &flags);
- 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;
- }
- static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
- void sched_show_task(struct task_struct *p)
- {
- unsigned long free = 0;
- int ppid;
- unsigned long state = p->state;
- if (state)
- state = __ffs(state) + 1;
- printk(KERN_INFO "%-15.15s %c", p->comm,
- state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
- #if BITS_PER_LONG == 32
- if (state == TASK_RUNNING)
- printk(KERN_CONT " running ");
- else
- printk(KERN_CONT " %08lx ", thread_saved_pc(p));
- #else
- if (state == TASK_RUNNING)
- printk(KERN_CONT " running task ");
- else
- printk(KERN_CONT " %016lx ", thread_saved_pc(p));
- #endif
- #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);
- }
- 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) {
-
- touch_nmi_watchdog();
- if (!state_filter || (p->state & state_filter))
- sched_show_task(p);
- }
- touch_all_softlockup_watchdogs();
- #ifdef CONFIG_SCHED_DEBUG
- sysrq_sched_debug_show();
- #endif
- rcu_read_unlock();
-
- if (!state_filter)
- debug_show_all_locks();
- }
- void init_idle_bootup_task(struct task_struct *idle)
- {
- idle->sched_class = &idle_sched_class;
- }
- void init_idle(struct task_struct *idle, int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- unsigned long flags;
- raw_spin_lock_irqsave(&rq->lock, flags);
- __sched_fork(0, idle);
- idle->state = TASK_RUNNING;
- idle->se.exec_start = sched_clock();
- do_set_cpus_allowed(idle, cpumask_of(cpu));
-
- rcu_read_lock();
- __set_task_cpu(idle, cpu);
- rcu_read_unlock();
- rq->curr = rq->idle = idle;
- idle->on_rq = TASK_ON_RQ_QUEUED;
- #if defined(CONFIG_SMP)
- idle->on_cpu = 1;
- #endif
- raw_spin_unlock_irqrestore(&rq->lock, flags);
-
- init_idle_preempt_count(idle, cpu);
-
- idle->sched_class = &idle_sched_class;
- ftrace_graph_init_idle_task(idle, cpu);
- vtime_init_idle(idle, cpu);
- #if defined(CONFIG_SMP)
- sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
- #endif
- }
- int cpuset_cpumask_can_shrink(const struct cpumask *cur,
- const struct cpumask *trial)
- {
- int ret = 1, trial_cpus;
- struct dl_bw *cur_dl_b;
- unsigned long flags;
- if (!cpumask_weight(cur))
- return ret;
- rcu_read_lock_sched();
- cur_dl_b = dl_bw_of(cpumask_any(cur));
- trial_cpus = cpumask_weight(trial);
- raw_spin_lock_irqsave(&cur_dl_b->lock, flags);
- if (cur_dl_b->bw != -1 &&
- cur_dl_b->bw * trial_cpus < cur_dl_b->total_bw)
- ret = 0;
- raw_spin_unlock_irqrestore(&cur_dl_b->lock, flags);
- rcu_read_unlock_sched();
- return ret;
- }
- int task_can_attach(struct task_struct *p,
- const struct cpumask *cs_cpus_allowed)
- {
- int ret = 0;
-
- if (p->flags & PF_NO_SETAFFINITY) {
- ret = -EINVAL;
- goto out;
- }
- #ifdef CONFIG_SMP
- if (dl_task(p) && !cpumask_intersects(task_rq(p)->rd->span,
- cs_cpus_allowed)) {
- unsigned int dest_cpu = cpumask_any_and(cpu_active_mask,
- cs_cpus_allowed);
- struct dl_bw *dl_b;
- bool overflow;
- int cpus;
- unsigned long flags;
- rcu_read_lock_sched();
- dl_b = dl_bw_of(dest_cpu);
- raw_spin_lock_irqsave(&dl_b->lock, flags);
- cpus = dl_bw_cpus(dest_cpu);
- overflow = __dl_overflow(dl_b, cpus, 0, p->dl.dl_bw);
- if (overflow)
- ret = -EBUSY;
- else {
-
- __dl_add(dl_b, p->dl.dl_bw);
- }
- raw_spin_unlock_irqrestore(&dl_b->lock, flags);
- rcu_read_unlock_sched();
- }
- #endif
- out:
- return ret;
- }
- #ifdef CONFIG_SMP
- #ifdef CONFIG_NUMA_BALANCING
- 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, tsk_cpus_allowed(p)))
- return -EINVAL;
-
- trace_sched_move_numa(p, curr_cpu, target_cpu);
- return stop_one_cpu(curr_cpu, migration_cpu_stop, &arg);
- }
- void sched_setnuma(struct task_struct *p, int nid)
- {
- struct rq *rq;
- unsigned long flags;
- bool queued, running;
- rq = task_rq_lock(p, &flags);
- queued = task_on_rq_queued(p);
- running = task_current(rq, p);
- if (queued)
- dequeue_task(rq, p, 0);
- if (running)
- put_prev_task(rq, p);
- p->numa_preferred_nid = nid;
- if (running)
- p->sched_class->set_curr_task(rq);
- if (queued)
- enqueue_task(rq, p, 0);
- task_rq_unlock(rq, p, &flags);
- }
- #endif
- #ifdef CONFIG_HOTPLUG_CPU
- 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);
- }
- static void calc_load_migrate(struct rq *rq)
- {
- long delta = calc_load_fold_active(rq);
- 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 = {
-
- .prio = MAX_PRIO + 1,
- .sched_class = &fake_sched_class,
- };
- static void migrate_tasks(struct rq *dead_rq)
- {
- struct rq *rq = dead_rq;
- struct task_struct *next, *stop = rq->stop;
- int dest_cpu;
-
- rq->stop = NULL;
-
- update_rq_clock(rq);
- for (;;) {
-
- if (rq->nr_running == 1)
- break;
-
- lockdep_pin_lock(&rq->lock);
- next = pick_next_task(rq, &fake_task);
- BUG_ON(!next);
- next->sched_class->put_prev_task(rq, next);
-
- dest_cpu = select_fallback_rq(dead_rq->cpu, next);
- lockdep_unpin_lock(&rq->lock);
- rq = __migrate_task(rq, next, dest_cpu);
- if (rq != dead_rq) {
- raw_spin_unlock(&rq->lock);
- rq = dead_rq;
- raw_spin_lock(&rq->lock);
- }
- }
- rq->stop = stop;
- }
- #endif
- #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
- static struct ctl_table sd_ctl_dir[] = {
- {
- .procname = "sched_domain",
- .mode = 0555,
- },
- {}
- };
- static struct ctl_table sd_ctl_root[] = {
- {
- .procname = "kernel",
- .mode = 0555,
- .child = sd_ctl_dir,
- },
- {}
- };
- static struct ctl_table *sd_alloc_ctl_entry(int n)
- {
- struct ctl_table *entry =
- kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
- return entry;
- }
- static void sd_free_ctl_entry(struct ctl_table **tablep)
- {
- struct ctl_table *entry;
-
- for (entry = *tablep; entry->mode; entry++) {
- if (entry->child)
- sd_free_ctl_entry(&entry->child);
- if (entry->proc_handler == NULL)
- kfree(entry->procname);
- }
- kfree(*tablep);
- *tablep = NULL;
- }
- static int min_load_idx = 0;
- static int max_load_idx = CPU_LOAD_IDX_MAX-1;
- static void
- set_table_entry(struct ctl_table *entry,
- const char *procname, void *data, int maxlen,
- umode_t mode, proc_handler *proc_handler,
- bool load_idx)
- {
- entry->procname = procname;
- entry->data = data;
- entry->maxlen = maxlen;
- entry->mode = mode;
- entry->proc_handler = proc_handler;
- if (load_idx) {
- entry->extra1 = &min_load_idx;
- entry->extra2 = &max_load_idx;
- }
- }
- static struct ctl_table *
- sd_alloc_ctl_domain_table(struct sched_domain *sd)
- {
- struct ctl_table *table = sd_alloc_ctl_entry(14);
- if (table == NULL)
- return NULL;
- set_table_entry(&table[0], "min_interval", &sd->min_interval,
- sizeof(long), 0644, proc_doulongvec_minmax, false);
- set_table_entry(&table[1], "max_interval", &sd->max_interval,
- sizeof(long), 0644, proc_doulongvec_minmax, false);
- set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
- sizeof(int), 0644, proc_dointvec_minmax, true);
- set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
- sizeof(int), 0644, proc_dointvec_minmax, true);
- set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
- sizeof(int), 0644, proc_dointvec_minmax, true);
- set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
- sizeof(int), 0644, proc_dointvec_minmax, true);
- set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
- sizeof(int), 0644, proc_dointvec_minmax, true);
- set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
- sizeof(int), 0644, proc_dointvec_minmax, false);
- set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
- sizeof(int), 0644, proc_dointvec_minmax, false);
- set_table_entry(&table[9], "cache_nice_tries",
- &sd->cache_nice_tries,
- sizeof(int), 0644, proc_dointvec_minmax, false);
- set_table_entry(&table[10], "flags", &sd->flags,
- sizeof(int), 0644, proc_dointvec_minmax, false);
- set_table_entry(&table[11], "max_newidle_lb_cost",
- &sd->max_newidle_lb_cost,
- sizeof(long), 0644, proc_doulongvec_minmax, false);
- set_table_entry(&table[12], "name", sd->name,
- CORENAME_MAX_SIZE, 0444, proc_dostring, false);
-
- return table;
- }
- static struct ctl_table *sd_alloc_ctl_cpu_table(int cpu)
- {
- struct ctl_table *entry, *table;
- struct sched_domain *sd;
- int domain_num = 0, i;
- char buf[32];
- for_each_domain(cpu, sd)
- domain_num++;
- entry = table = sd_alloc_ctl_entry(domain_num + 1);
- if (table == NULL)
- return NULL;
- i = 0;
- for_each_domain(cpu, sd) {
- snprintf(buf, 32, "domain%d", i);
- entry->procname = kstrdup(buf, GFP_KERNEL);
- entry->mode = 0555;
- entry->child = sd_alloc_ctl_domain_table(sd);
- entry++;
- i++;
- }
- return table;
- }
- static struct ctl_table_header *sd_sysctl_header;
- static void register_sched_domain_sysctl(void)
- {
- int i, cpu_num = num_possible_cpus();
- struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
- char buf[32];
- WARN_ON(sd_ctl_dir[0].child);
- sd_ctl_dir[0].child = entry;
- if (entry == NULL)
- return;
- for_each_possible_cpu(i) {
- snprintf(buf, 32, "cpu%d", i);
- entry->procname = kstrdup(buf, GFP_KERNEL);
- entry->mode = 0555;
- entry->child = sd_alloc_ctl_cpu_table(i);
- entry++;
- }
- WARN_ON(sd_sysctl_header);
- sd_sysctl_header = register_sysctl_table(sd_ctl_root);
- }
- static void unregister_sched_domain_sysctl(void)
- {
- if (sd_sysctl_header)
- unregister_sysctl_table(sd_sysctl_header);
- sd_sysctl_header = NULL;
- if (sd_ctl_dir[0].child)
- sd_free_ctl_entry(&sd_ctl_dir[0].child);
- }
- #else
- static void register_sched_domain_sysctl(void)
- {
- }
- static void unregister_sched_domain_sysctl(void)
- {
- }
- #endif
- static 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);
- }
- }
- }
- static 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 int
- migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
- {
- int cpu = (long)hcpu;
- unsigned long flags;
- struct rq *rq = cpu_rq(cpu);
- switch (action & ~CPU_TASKS_FROZEN) {
- case CPU_UP_PREPARE:
- rq->calc_load_update = calc_load_update;
- break;
- case CPU_ONLINE:
-
- raw_spin_lock_irqsave(&rq->lock, flags);
- if (rq->rd) {
- BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
- set_rq_online(rq);
- }
- raw_spin_unlock_irqrestore(&rq->lock, flags);
- break;
- #ifdef CONFIG_HOTPLUG_CPU
- case CPU_DYING:
- sched_ttwu_pending();
-
- raw_spin_lock_irqsave(&rq->lock, flags);
- if (rq->rd) {
- BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
- set_rq_offline(rq);
- }
- migrate_tasks(rq);
- BUG_ON(rq->nr_running != 1);
- raw_spin_unlock_irqrestore(&rq->lock, flags);
- break;
- case CPU_DEAD:
- calc_load_migrate(rq);
- break;
- #endif
- }
- update_max_interval();
- return NOTIFY_OK;
- }
- static struct notifier_block migration_notifier = {
- .notifier_call = migration_call,
- .priority = CPU_PRI_MIGRATION,
- };
- static void set_cpu_rq_start_time(void)
- {
- int cpu = smp_processor_id();
- struct rq *rq = cpu_rq(cpu);
- rq->age_stamp = sched_clock_cpu(cpu);
- }
- static int sched_cpu_active(struct notifier_block *nfb,
- unsigned long action, void *hcpu)
- {
- switch (action & ~CPU_TASKS_FROZEN) {
- case CPU_STARTING:
- set_cpu_rq_start_time();
- return NOTIFY_OK;
- case CPU_DOWN_FAILED:
- set_cpu_active((long)hcpu, true);
- return NOTIFY_OK;
- default:
- return NOTIFY_DONE;
- }
- }
- static int sched_cpu_inactive(struct notifier_block *nfb,
- unsigned long action, void *hcpu)
- {
- switch (action & ~CPU_TASKS_FROZEN) {
- case CPU_DOWN_PREPARE:
- set_cpu_active((long)hcpu, false);
- return NOTIFY_OK;
- default:
- return NOTIFY_DONE;
- }
- }
- static int __init migration_init(void)
- {
- void *cpu = (void *)(long)smp_processor_id();
- int err;
-
- err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
- BUG_ON(err == NOTIFY_BAD);
- migration_call(&migration_notifier, CPU_ONLINE, cpu);
- register_cpu_notifier(&migration_notifier);
-
- cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
- cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
- return 0;
- }
- early_initcall(migration_init);
- static cpumask_var_t sched_domains_tmpmask;
- #ifdef CONFIG_SCHED_DEBUG
- static __read_mostly int sched_debug_enabled;
- static int __init sched_debug_setup(char *str)
- {
- sched_debug_enabled = 1;
- return 0;
- }
- early_param("sched_debug", sched_debug_setup);
- static inline bool sched_debug(void)
- {
- return sched_debug_enabled;
- }
- static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
- struct cpumask *groupmask)
- {
- struct sched_group *group = sd->groups;
- cpumask_clear(groupmask);
- printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
- if (!(sd->flags & SD_LOAD_BALANCE)) {
- printk("does not load-balance\n");
- if (sd->parent)
- printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
- " has parent");
- return -1;
- }
- printk(KERN_CONT "span %*pbl level %s\n",
- cpumask_pr_args(sched_domain_span(sd)), sd->name);
- if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
- printk(KERN_ERR "ERROR: domain->span does not contain "
- "CPU%d\n", cpu);
- }
- if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
- printk(KERN_ERR "ERROR: domain->groups does not contain"
- " CPU%d\n", cpu);
- }
- printk(KERN_DEBUG "%*s groups:", level + 1, "");
- do {
- if (!group) {
- printk("\n");
- printk(KERN_ERR "ERROR: group is NULL\n");
- break;
- }
- if (!cpumask_weight(sched_group_cpus(group))) {
- printk(KERN_CONT "\n");
- printk(KERN_ERR "ERROR: empty group\n");
- break;
- }
- if (!(sd->flags & SD_OVERLAP) &&
- cpumask_intersects(groupmask, sched_group_cpus(group))) {
- printk(KERN_CONT "\n");
- printk(KERN_ERR "ERROR: repeated CPUs\n");
- break;
- }
- cpumask_or(groupmask, groupmask, sched_group_cpus(group));
- printk(KERN_CONT " %*pbl",
- cpumask_pr_args(sched_group_cpus(group)));
- if (group->sgc->capacity != SCHED_CAPACITY_SCALE) {
- printk(KERN_CONT " (cpu_capacity = %d)",
- group->sgc->capacity);
- }
- group = group->next;
- } while (group != sd->groups);
- printk(KERN_CONT "\n");
- if (!cpumask_equal(sched_domain_span(sd), groupmask))
- printk(KERN_ERR "ERROR: groups don't span domain->span\n");
- if (sd->parent &&
- !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
- printk(KERN_ERR "ERROR: parent span is not a superset "
- "of domain->span\n");
- return 0;
- }
- static void sched_domain_debug(struct sched_domain *sd, int cpu)
- {
- int level = 0;
- if (!sched_debug_enabled)
- return;
- if (!sd) {
- printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
- return;
- }
- printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
- for (;;) {
- if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
- break;
- level++;
- sd = sd->parent;
- if (!sd)
- break;
- }
- }
- #else
- # define sched_domain_debug(sd, cpu) do { } while (0)
- static inline bool sched_debug(void)
- {
- return false;
- }
- #endif
- static int sd_degenerate(struct sched_domain *sd)
- {
- if (cpumask_weight(sched_domain_span(sd)) == 1)
- return 1;
-
- if (sd->flags & (SD_LOAD_BALANCE |
- SD_BALANCE_NEWIDLE |
- SD_BALANCE_FORK |
- SD_BALANCE_EXEC |
- SD_SHARE_CPUCAPACITY |
- SD_SHARE_PKG_RESOURCES |
- SD_SHARE_POWERDOMAIN)) {
- if (sd->groups != sd->groups->next)
- return 0;
- }
-
- if (sd->flags & (SD_WAKE_AFFINE))
- return 0;
- return 1;
- }
- static int
- sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
- {
- unsigned long cflags = sd->flags, pflags = parent->flags;
- if (sd_degenerate(parent))
- return 1;
- if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
- return 0;
-
- if (parent->groups == parent->groups->next) {
- pflags &= ~(SD_LOAD_BALANCE |
- SD_BALANCE_NEWIDLE |
- SD_BALANCE_FORK |
- SD_BALANCE_EXEC |
- SD_SHARE_CPUCAPACITY |
- SD_SHARE_PKG_RESOURCES |
- SD_PREFER_SIBLING |
- SD_SHARE_POWERDOMAIN);
- if (nr_node_ids == 1)
- pflags &= ~SD_SERIALIZE;
- }
- if (~cflags & pflags)
- return 0;
- return 1;
- }
- static void free_rootdomain(struct rcu_head *rcu)
- {
- struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
- cpupri_cleanup(&rd->cpupri);
- cpudl_cleanup(&rd->cpudl);
- free_cpumask_var(rd->dlo_mask);
- free_cpumask_var(rd->rto_mask);
- free_cpumask_var(rd->online);
- free_cpumask_var(rd->span);
- kfree(rd);
- }
- static void rq_attach_root(struct rq *rq, struct root_domain *rd)
- {
- struct root_domain *old_rd = NULL;
- unsigned long flags;
- raw_spin_lock_irqsave(&rq->lock, flags);
- if (rq->rd) {
- old_rd = rq->rd;
- if (cpumask_test_cpu(rq->cpu, old_rd->online))
- set_rq_offline(rq);
- cpumask_clear_cpu(rq->cpu, old_rd->span);
-
- if (!atomic_dec_and_test(&old_rd->refcount))
- old_rd = NULL;
- }
- atomic_inc(&rd->refcount);
- rq->rd = rd;
- cpumask_set_cpu(rq->cpu, rd->span);
- if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
- set_rq_online(rq);
- raw_spin_unlock_irqrestore(&rq->lock, flags);
- if (old_rd)
- call_rcu_sched(&old_rd->rcu, free_rootdomain);
- }
- static int init_rootdomain(struct root_domain *rd)
- {
- memset(rd, 0, sizeof(*rd));
- if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
- goto out;
- if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
- goto free_span;
- if (!alloc_cpumask_var(&rd->dlo_mask, GFP_KERNEL))
- goto free_online;
- if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
- goto free_dlo_mask;
- init_dl_bw(&rd->dl_bw);
- if (cpudl_init(&rd->cpudl) != 0)
- goto free_dlo_mask;
- if (cpupri_init(&rd->cpupri) != 0)
- goto free_rto_mask;
- return 0;
- free_rto_mask:
- free_cpumask_var(rd->rto_mask);
- free_dlo_mask:
- free_cpumask_var(rd->dlo_mask);
- free_online:
- free_cpumask_var(rd->online);
- free_span:
- free_cpumask_var(rd->span);
- out:
- return -ENOMEM;
- }
- struct root_domain def_root_domain;
- static void init_defrootdomain(void)
- {
- init_rootdomain(&def_root_domain);
- atomic_set(&def_root_domain.refcount, 1);
- }
- static struct root_domain *alloc_rootdomain(void)
- {
- struct root_domain *rd;
- rd = kmalloc(sizeof(*rd), GFP_KERNEL);
- if (!rd)
- return NULL;
- if (init_rootdomain(rd) != 0) {
- kfree(rd);
- return NULL;
- }
- return rd;
- }
- static void free_sched_groups(struct sched_group *sg, int free_sgc)
- {
- struct sched_group *tmp, *first;
- if (!sg)
- return;
- first = sg;
- do {
- tmp = sg->next;
- if (free_sgc && atomic_dec_and_test(&sg->sgc->ref))
- kfree(sg->sgc);
- kfree(sg);
- sg = tmp;
- } while (sg != first);
- }
- static void free_sched_domain(struct rcu_head *rcu)
- {
- struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
-
- if (sd->flags & SD_OVERLAP) {
- free_sched_groups(sd->groups, 1);
- } else if (atomic_dec_and_test(&sd->groups->ref)) {
- kfree(sd->groups->sgc);
- kfree(sd->groups);
- }
- kfree(sd);
- }
- static void destroy_sched_domain(struct sched_domain *sd, int cpu)
- {
- call_rcu(&sd->rcu, free_sched_domain);
- }
- static void destroy_sched_domains(struct sched_domain *sd, int cpu)
- {
- for (; sd; sd = sd->parent)
- destroy_sched_domain(sd, cpu);
- }
- DEFINE_PER_CPU(struct sched_domain *, sd_llc);
- DEFINE_PER_CPU(int, sd_llc_size);
- DEFINE_PER_CPU(int, sd_llc_id);
- DEFINE_PER_CPU(struct sched_domain *, sd_numa);
- DEFINE_PER_CPU(struct sched_domain *, sd_busy);
- DEFINE_PER_CPU(struct sched_domain *, sd_asym);
- static void update_top_cache_domain(int cpu)
- {
- struct sched_domain *sd;
- struct sched_domain *busy_sd = NULL;
- int id = cpu;
- int size = 1;
- sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
- if (sd) {
- id = cpumask_first(sched_domain_span(sd));
- size = cpumask_weight(sched_domain_span(sd));
- busy_sd = sd->parent;
- }
- rcu_assign_pointer(per_cpu(sd_busy, cpu), busy_sd);
- rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
- per_cpu(sd_llc_size, cpu) = size;
- per_cpu(sd_llc_id, cpu) = id;
- sd = lowest_flag_domain(cpu, SD_NUMA);
- rcu_assign_pointer(per_cpu(sd_numa, cpu), sd);
- sd = highest_flag_domain(cpu, SD_ASYM_PACKING);
- rcu_assign_pointer(per_cpu(sd_asym, cpu), sd);
- }
- static void
- cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
- {
- struct rq *rq = cpu_rq(cpu);
- struct sched_domain *tmp;
-
- for (tmp = sd; tmp; ) {
- struct sched_domain *parent = tmp->parent;
- if (!parent)
- break;
- if (sd_parent_degenerate(tmp, parent)) {
- tmp->parent = parent->parent;
- if (parent->parent)
- parent->parent->child = tmp;
-
- if (parent->flags & SD_PREFER_SIBLING)
- tmp->flags |= SD_PREFER_SIBLING;
- destroy_sched_domain(parent, cpu);
- } else
- tmp = tmp->parent;
- }
- if (sd && sd_degenerate(sd)) {
- tmp = sd;
- sd = sd->parent;
- destroy_sched_domain(tmp, cpu);
- if (sd)
- sd->child = NULL;
- }
- sched_domain_debug(sd, cpu);
- rq_attach_root(rq, rd);
- tmp = rq->sd;
- rcu_assign_pointer(rq->sd, sd);
- destroy_sched_domains(tmp, cpu);
- update_top_cache_domain(cpu);
- }
- static int __init isolated_cpu_setup(char *str)
- {
- alloc_bootmem_cpumask_var(&cpu_isolated_map);
- cpulist_parse(str, cpu_isolated_map);
- return 1;
- }
- __setup("isolcpus=", isolated_cpu_setup);
- struct s_data {
- struct sched_domain ** __percpu sd;
- struct root_domain *rd;
- };
- enum s_alloc {
- sa_rootdomain,
- sa_sd,
- sa_sd_storage,
- sa_none,
- };
- static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
- {
- const struct cpumask *span = sched_domain_span(sd);
- struct sd_data *sdd = sd->private;
- struct sched_domain *sibling;
- int i;
- for_each_cpu(i, span) {
- sibling = *per_cpu_ptr(sdd->sd, i);
- if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
- continue;
- cpumask_set_cpu(i, sched_group_mask(sg));
- }
- }
- int group_balance_cpu(struct sched_group *sg)
- {
- return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
- }
- static int
- build_overlap_sched_groups(struct sched_domain *sd, int cpu)
- {
- struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
- const struct cpumask *span = sched_domain_span(sd);
- struct cpumask *covered = sched_domains_tmpmask;
- struct sd_data *sdd = sd->private;
- struct sched_domain *sibling;
- int i;
- cpumask_clear(covered);
- for_each_cpu(i, span) {
- struct cpumask *sg_span;
- if (cpumask_test_cpu(i, covered))
- continue;
- sibling = *per_cpu_ptr(sdd->sd, i);
-
- if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
- continue;
- sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
- GFP_KERNEL, cpu_to_node(cpu));
- if (!sg)
- goto fail;
- sg_span = sched_group_cpus(sg);
- if (sibling->child)
- cpumask_copy(sg_span, sched_domain_span(sibling->child));
- else
- cpumask_set_cpu(i, sg_span);
- cpumask_or(covered, covered, sg_span);
- sg->sgc = *per_cpu_ptr(sdd->sgc, i);
- if (atomic_inc_return(&sg->sgc->ref) == 1)
- build_group_mask(sd, sg);
-
- sg->sgc->capacity = SCHED_CAPACITY_SCALE * cpumask_weight(sg_span);
-
- if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
- group_balance_cpu(sg) == cpu)
- groups = sg;
- if (!first)
- first = sg;
- if (last)
- last->next = sg;
- last = sg;
- last->next = first;
- }
- sd->groups = groups;
- return 0;
- fail:
- free_sched_groups(first, 0);
- return -ENOMEM;
- }
- static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
- {
- struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
- struct sched_domain *child = sd->child;
- if (child)
- cpu = cpumask_first(sched_domain_span(child));
- if (sg) {
- *sg = *per_cpu_ptr(sdd->sg, cpu);
- (*sg)->sgc = *per_cpu_ptr(sdd->sgc, cpu);
- atomic_set(&(*sg)->sgc->ref, 1);
- }
- return cpu;
- }
- static int
- build_sched_groups(struct sched_domain *sd, int cpu)
- {
- struct sched_group *first = NULL, *last = NULL;
- struct sd_data *sdd = sd->private;
- const struct cpumask *span = sched_domain_span(sd);
- struct cpumask *covered;
- int i;
- get_group(cpu, sdd, &sd->groups);
- atomic_inc(&sd->groups->ref);
- if (cpu != cpumask_first(span))
- return 0;
- lockdep_assert_held(&sched_domains_mutex);
- covered = sched_domains_tmpmask;
- cpumask_clear(covered);
- for_each_cpu(i, span) {
- struct sched_group *sg;
- int group, j;
- if (cpumask_test_cpu(i, covered))
- continue;
- group = get_group(i, sdd, &sg);
- cpumask_setall(sched_group_mask(sg));
- for_each_cpu(j, span) {
- if (get_group(j, sdd, NULL) != group)
- continue;
- cpumask_set_cpu(j, covered);
- cpumask_set_cpu(j, sched_group_cpus(sg));
- }
- if (!first)
- first = sg;
- if (last)
- last->next = sg;
- last = sg;
- }
- last->next = first;
- return 0;
- }
- static void init_sched_groups_capacity(int cpu, struct sched_domain *sd)
- {
- struct sched_group *sg = sd->groups;
- WARN_ON(!sg);
- do {
- sg->group_weight = cpumask_weight(sched_group_cpus(sg));
- sg = sg->next;
- } while (sg != sd->groups);
- if (cpu != group_balance_cpu(sg))
- return;
- update_group_capacity(sd, cpu);
- atomic_set(&sg->sgc->nr_busy_cpus, sg->group_weight);
- }
- static int default_relax_domain_level = -1;
- int sched_domain_level_max;
- static int __init setup_relax_domain_level(char *str)
- {
- if (kstrtoint(str, 0, &default_relax_domain_level))
- pr_warn("Unable to set relax_domain_level\n");
- return 1;
- }
- __setup("relax_domain_level=", setup_relax_domain_level);
- static void set_domain_attribute(struct sched_domain *sd,
- struct sched_domain_attr *attr)
- {
- int request;
- if (!attr || attr->relax_domain_level < 0) {
- if (default_relax_domain_level < 0)
- return;
- else
- request = default_relax_domain_level;
- } else
- request = attr->relax_domain_level;
- if (request < sd->level) {
-
- sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
- } else {
-
- sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
- }
- }
- static void __sdt_free(const struct cpumask *cpu_map);
- static int __sdt_alloc(const struct cpumask *cpu_map);
- static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
- const struct cpumask *cpu_map)
- {
- switch (what) {
- case sa_rootdomain:
- if (!atomic_read(&d->rd->refcount))
- free_rootdomain(&d->rd->rcu);
- case sa_sd:
- free_percpu(d->sd);
- case sa_sd_storage:
- __sdt_free(cpu_map);
- case sa_none:
- break;
- }
- }
- static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
- const struct cpumask *cpu_map)
- {
- memset(d, 0, sizeof(*d));
- if (__sdt_alloc(cpu_map))
- return sa_sd_storage;
- d->sd = alloc_percpu(struct sched_domain *);
- if (!d->sd)
- return sa_sd_storage;
- d->rd = alloc_rootdomain();
- if (!d->rd)
- return sa_sd;
- return sa_rootdomain;
- }
- static void claim_allocations(int cpu, struct sched_domain *sd)
- {
- struct sd_data *sdd = sd->private;
- WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
- *per_cpu_ptr(sdd->sd, cpu) = NULL;
- if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
- *per_cpu_ptr(sdd->sg, cpu) = NULL;
- if (atomic_read(&(*per_cpu_ptr(sdd->sgc, cpu))->ref))
- *per_cpu_ptr(sdd->sgc, cpu) = NULL;
- }
- #ifdef CONFIG_NUMA
- static int sched_domains_numa_levels;
- enum numa_topology_type sched_numa_topology_type;
- static int *sched_domains_numa_distance;
- int sched_max_numa_distance;
- static struct cpumask ***sched_domains_numa_masks;
- static int sched_domains_curr_level;
- #endif
- #define TOPOLOGY_SD_FLAGS \
- (SD_SHARE_CPUCAPACITY | \
- SD_SHARE_PKG_RESOURCES | \
- SD_NUMA | \
- SD_ASYM_PACKING | \
- SD_SHARE_POWERDOMAIN)
- static struct sched_domain *
- sd_init(struct sched_domain_topology_level *tl, int cpu)
- {
- struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
- int sd_weight, sd_flags = 0;
- #ifdef CONFIG_NUMA
-
- sched_domains_curr_level = tl->numa_level;
- #endif
- sd_weight = cpumask_weight(tl->mask(cpu));
- if (tl->sd_flags)
- sd_flags = (*tl->sd_flags)();
- if (WARN_ONCE(sd_flags & ~TOPOLOGY_SD_FLAGS,
- "wrong sd_flags in topology description\n"))
- sd_flags &= ~TOPOLOGY_SD_FLAGS;
- *sd = (struct sched_domain){
- .min_interval = sd_weight,
- .max_interval = 2*sd_weight,
- .busy_factor = 32,
- .imbalance_pct = 125,
- .cache_nice_tries = 0,
- .busy_idx = 0,
- .idle_idx = 0,
- .newidle_idx = 0,
- .wake_idx = 0,
- .forkexec_idx = 0,
- .flags = 1*SD_LOAD_BALANCE
- | 1*SD_BALANCE_NEWIDLE
- | 1*SD_BALANCE_EXEC
- | 1*SD_BALANCE_FORK
- | 0*SD_BALANCE_WAKE
- | 1*SD_WAKE_AFFINE
- | 0*SD_SHARE_CPUCAPACITY
- | 0*SD_SHARE_PKG_RESOURCES
- | 0*SD_SERIALIZE
- | 0*SD_PREFER_SIBLING
- | 0*SD_NUMA
- | sd_flags
- ,
- .last_balance = jiffies,
- .balance_interval = sd_weight,
- .smt_gain = 0,
- .max_newidle_lb_cost = 0,
- .next_decay_max_lb_cost = jiffies,
- #ifdef CONFIG_SCHED_DEBUG
- .name = tl->name,
- #endif
- };
-
- if (sd->flags & SD_SHARE_CPUCAPACITY) {
- sd->flags |= SD_PREFER_SIBLING;
- sd->imbalance_pct = 110;
- sd->smt_gain = 1178;
- } else if (sd->flags & SD_SHARE_PKG_RESOURCES) {
- sd->imbalance_pct = 117;
- sd->cache_nice_tries = 1;
- sd->busy_idx = 2;
- #ifdef CONFIG_NUMA
- } else if (sd->flags & SD_NUMA) {
- sd->cache_nice_tries = 2;
- sd->busy_idx = 3;
- sd->idle_idx = 2;
- sd->flags |= SD_SERIALIZE;
- if (sched_domains_numa_distance[tl->numa_level] > RECLAIM_DISTANCE) {
- sd->flags &= ~(SD_BALANCE_EXEC |
- SD_BALANCE_FORK |
- SD_WAKE_AFFINE);
- }
- #endif
- } else {
- sd->flags |= SD_PREFER_SIBLING;
- sd->cache_nice_tries = 1;
- sd->busy_idx = 2;
- sd->idle_idx = 1;
- }
- sd->private = &tl->data;
- return sd;
- }
- static struct sched_domain_topology_level default_topology[] = {
- #ifdef CONFIG_SCHED_SMT
- { cpu_smt_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
- #endif
- #ifdef CONFIG_SCHED_MC
- { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
- #endif
- { cpu_cpu_mask, SD_INIT_NAME(DIE) },
- { NULL, },
- };
- struct sched_domain_topology_level *sched_domain_topology = default_topology;
- #define for_each_sd_topology(tl) \
- for (tl = sched_domain_topology; tl->mask; tl++)
- void set_sched_topology(struct sched_domain_topology_level *tl)
- {
- sched_domain_topology = tl;
- }
- #ifdef CONFIG_NUMA
- static const struct cpumask *sd_numa_mask(int cpu)
- {
- return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
- }
- static void sched_numa_warn(const char *str)
- {
- static int done = false;
- int i,j;
- if (done)
- return;
- done = true;
- printk(KERN_WARNING "ERROR: %s\n\n", str);
- for (i = 0; i < nr_node_ids; i++) {
- printk(KERN_WARNING " ");
- for (j = 0; j < nr_node_ids; j++)
- printk(KERN_CONT "%02d ", node_distance(i,j));
- printk(KERN_CONT "\n");
- }
- printk(KERN_WARNING "\n");
- }
- bool find_numa_distance(int distance)
- {
- int i;
- if (distance == node_distance(0, 0))
- return true;
- for (i = 0; i < sched_domains_numa_levels; i++) {
- if (sched_domains_numa_distance[i] == distance)
- return true;
- }
- return false;
- }
- static void init_numa_topology_type(void)
- {
- int a, b, c, n;
- n = sched_max_numa_distance;
- if (n <= 1)
- sched_numa_topology_type = NUMA_DIRECT;
- for_each_online_node(a) {
- for_each_online_node(b) {
-
- if (node_distance(a, b) < n)
- continue;
-
- for_each_online_node(c) {
- if (node_distance(a, c) < n &&
- node_distance(b, c) < n) {
- sched_numa_topology_type =
- NUMA_GLUELESS_MESH;
- return;
- }
- }
- sched_numa_topology_type = NUMA_BACKPLANE;
- return;
- }
- }
- }
- static void sched_init_numa(void)
- {
- int next_distance, curr_distance = node_distance(0, 0);
- struct sched_domain_topology_level *tl;
- int level = 0;
- int i, j, k;
- sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
- if (!sched_domains_numa_distance)
- return;
-
- next_distance = curr_distance;
- for (i = 0; i < nr_node_ids; i++) {
- for (j = 0; j < nr_node_ids; j++) {
- for (k = 0; k < nr_node_ids; k++) {
- int distance = node_distance(i, k);
- if (distance > curr_distance &&
- (distance < next_distance ||
- next_distance == curr_distance))
- next_distance = distance;
-
- if (sched_debug() && node_distance(k, i) != distance)
- sched_numa_warn("Node-distance not symmetric");
- if (sched_debug() && i && !find_numa_distance(distance))
- sched_numa_warn("Node-0 not representative");
- }
- if (next_distance != curr_distance) {
- sched_domains_numa_distance[level++] = next_distance;
- sched_domains_numa_levels = level;
- curr_distance = next_distance;
- } else break;
- }
-
- if (!sched_debug())
- break;
- }
- if (!level)
- return;
-
-
- sched_domains_numa_levels = 0;
- sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
- if (!sched_domains_numa_masks)
- return;
-
- for (i = 0; i < level; i++) {
- sched_domains_numa_masks[i] =
- kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
- if (!sched_domains_numa_masks[i])
- return;
- for (j = 0; j < nr_node_ids; j++) {
- struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
- if (!mask)
- return;
- sched_domains_numa_masks[i][j] = mask;
- for (k = 0; k < nr_node_ids; k++) {
- if (node_distance(j, k) > sched_domains_numa_distance[i])
- continue;
- cpumask_or(mask, mask, cpumask_of_node(k));
- }
- }
- }
-
- for (i = 0; sched_domain_topology[i].mask; i++);
- tl = kzalloc((i + level + 1) *
- sizeof(struct sched_domain_topology_level), GFP_KERNEL);
- if (!tl)
- return;
-
- for (i = 0; sched_domain_topology[i].mask; i++)
- tl[i] = sched_domain_topology[i];
-
- for (j = 0; j < level; i++, j++) {
- tl[i] = (struct sched_domain_topology_level){
- .mask = sd_numa_mask,
- .sd_flags = cpu_numa_flags,
- .flags = SDTL_OVERLAP,
- .numa_level = j,
- SD_INIT_NAME(NUMA)
- };
- }
- sched_domain_topology = tl;
- sched_domains_numa_levels = level;
- sched_max_numa_distance = sched_domains_numa_distance[level - 1];
- init_numa_topology_type();
- }
- static void sched_domains_numa_masks_set(int cpu)
- {
- int i, j;
- int node = cpu_to_node(cpu);
- for (i = 0; i < sched_domains_numa_levels; i++) {
- for (j = 0; j < nr_node_ids; j++) {
- if (node_distance(j, node) <= sched_domains_numa_distance[i])
- cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
- }
- }
- }
- static void sched_domains_numa_masks_clear(int cpu)
- {
- int i, j;
- for (i = 0; i < sched_domains_numa_levels; i++) {
- for (j = 0; j < nr_node_ids; j++)
- cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
- }
- }
- static int sched_domains_numa_masks_update(struct notifier_block *nfb,
- unsigned long action,
- void *hcpu)
- {
- int cpu = (long)hcpu;
- switch (action & ~CPU_TASKS_FROZEN) {
- case CPU_ONLINE:
- sched_domains_numa_masks_set(cpu);
- break;
- case CPU_DEAD:
- sched_domains_numa_masks_clear(cpu);
- break;
- default:
- return NOTIFY_DONE;
- }
- return NOTIFY_OK;
- }
- #else
- static inline void sched_init_numa(void)
- {
- }
- static int sched_domains_numa_masks_update(struct notifier_block *nfb,
- unsigned long action,
- void *hcpu)
- {
- return 0;
- }
- #endif
- static int __sdt_alloc(const struct cpumask *cpu_map)
- {
- struct sched_domain_topology_level *tl;
- int j;
- for_each_sd_topology(tl) {
- struct sd_data *sdd = &tl->data;
- sdd->sd = alloc_percpu(struct sched_domain *);
- if (!sdd->sd)
- return -ENOMEM;
- sdd->sg = alloc_percpu(struct sched_group *);
- if (!sdd->sg)
- return -ENOMEM;
- sdd->sgc = alloc_percpu(struct sched_group_capacity *);
- if (!sdd->sgc)
- return -ENOMEM;
- for_each_cpu(j, cpu_map) {
- struct sched_domain *sd;
- struct sched_group *sg;
- struct sched_group_capacity *sgc;
- sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
- GFP_KERNEL, cpu_to_node(j));
- if (!sd)
- return -ENOMEM;
- *per_cpu_ptr(sdd->sd, j) = sd;
- sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
- GFP_KERNEL, cpu_to_node(j));
- if (!sg)
- return -ENOMEM;
- sg->next = sg;
- *per_cpu_ptr(sdd->sg, j) = sg;
- sgc = kzalloc_node(sizeof(struct sched_group_capacity) + cpumask_size(),
- GFP_KERNEL, cpu_to_node(j));
- if (!sgc)
- return -ENOMEM;
- *per_cpu_ptr(sdd->sgc, j) = sgc;
- }
- }
- return 0;
- }
- static void __sdt_free(const struct cpumask *cpu_map)
- {
- struct sched_domain_topology_level *tl;
- int j;
- for_each_sd_topology(tl) {
- struct sd_data *sdd = &tl->data;
- for_each_cpu(j, cpu_map) {
- struct sched_domain *sd;
- if (sdd->sd) {
- sd = *per_cpu_ptr(sdd->sd, j);
- if (sd && (sd->flags & SD_OVERLAP))
- free_sched_groups(sd->groups, 0);
- kfree(*per_cpu_ptr(sdd->sd, j));
- }
- if (sdd->sg)
- kfree(*per_cpu_ptr(sdd->sg, j));
- if (sdd->sgc)
- kfree(*per_cpu_ptr(sdd->sgc, j));
- }
- free_percpu(sdd->sd);
- sdd->sd = NULL;
- free_percpu(sdd->sg);
- sdd->sg = NULL;
- free_percpu(sdd->sgc);
- sdd->sgc = NULL;
- }
- }
- struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
- const struct cpumask *cpu_map, struct sched_domain_attr *attr,
- struct sched_domain *child, int cpu)
- {
- struct sched_domain *sd = sd_init(tl, cpu);
- if (!sd)
- return child;
- cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
- if (child) {
- sd->level = child->level + 1;
- sched_domain_level_max = max(sched_domain_level_max, sd->level);
- child->parent = sd;
- sd->child = child;
- if (!cpumask_subset(sched_domain_span(child),
- sched_domain_span(sd))) {
- pr_err("BUG: arch topology borken\n");
- #ifdef CONFIG_SCHED_DEBUG
- pr_err(" the %s domain not a subset of the %s domain\n",
- child->name, sd->name);
- #endif
-
- cpumask_or(sched_domain_span(sd),
- sched_domain_span(sd),
- sched_domain_span(child));
- }
- }
- set_domain_attribute(sd, attr);
- return sd;
- }
- static int build_sched_domains(const struct cpumask *cpu_map,
- struct sched_domain_attr *attr)
- {
- enum s_alloc alloc_state;
- struct sched_domain *sd;
- struct s_data d;
- int i, ret = -ENOMEM;
- alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
- if (alloc_state != sa_rootdomain)
- goto error;
-
- for_each_cpu(i, cpu_map) {
- struct sched_domain_topology_level *tl;
- sd = NULL;
- for_each_sd_topology(tl) {
- sd = build_sched_domain(tl, cpu_map, attr, sd, i);
- if (tl == sched_domain_topology)
- *per_cpu_ptr(d.sd, i) = sd;
- if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
- sd->flags |= SD_OVERLAP;
- if (cpumask_equal(cpu_map, sched_domain_span(sd)))
- break;
- }
- }
-
- for_each_cpu(i, cpu_map) {
- for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
- sd->span_weight = cpumask_weight(sched_domain_span(sd));
- if (sd->flags & SD_OVERLAP) {
- if (build_overlap_sched_groups(sd, i))
- goto error;
- } else {
- if (build_sched_groups(sd, i))
- goto error;
- }
- }
- }
-
- for (i = nr_cpumask_bits-1; i >= 0; i--) {
- if (!cpumask_test_cpu(i, cpu_map))
- continue;
- for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
- claim_allocations(i, sd);
- init_sched_groups_capacity(i, sd);
- }
- }
-
- rcu_read_lock();
- for_each_cpu(i, cpu_map) {
- sd = *per_cpu_ptr(d.sd, i);
- cpu_attach_domain(sd, d.rd, i);
- }
- rcu_read_unlock();
- ret = 0;
- error:
- __free_domain_allocs(&d, alloc_state, cpu_map);
- return ret;
- }
- static cpumask_var_t *doms_cur;
- static int ndoms_cur;
- static struct sched_domain_attr *dattr_cur;
-
- static cpumask_var_t fallback_doms;
- int __weak arch_update_cpu_topology(void)
- {
- return 0;
- }
- cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
- {
- int i;
- cpumask_var_t *doms;
- doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
- if (!doms)
- return NULL;
- for (i = 0; i < ndoms; i++) {
- if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
- free_sched_domains(doms, i);
- return NULL;
- }
- }
- return doms;
- }
- void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
- {
- unsigned int i;
- for (i = 0; i < ndoms; i++)
- free_cpumask_var(doms[i]);
- kfree(doms);
- }
- static int init_sched_domains(const struct cpumask *cpu_map)
- {
- int err;
- arch_update_cpu_topology();
- ndoms_cur = 1;
- doms_cur = alloc_sched_domains(ndoms_cur);
- if (!doms_cur)
- doms_cur = &fallback_doms;
- cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
- err = build_sched_domains(doms_cur[0], NULL);
- register_sched_domain_sysctl();
- return err;
- }
- static void detach_destroy_domains(const struct cpumask *cpu_map)
- {
- int i;
- rcu_read_lock();
- for_each_cpu(i, cpu_map)
- cpu_attach_domain(NULL, &def_root_domain, i);
- rcu_read_unlock();
- }
- static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
- struct sched_domain_attr *new, int idx_new)
- {
- struct sched_domain_attr tmp;
-
- if (!new && !cur)
- return 1;
- tmp = SD_ATTR_INIT;
- return !memcmp(cur ? (cur + idx_cur) : &tmp,
- new ? (new + idx_new) : &tmp,
- sizeof(struct sched_domain_attr));
- }
- void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
- struct sched_domain_attr *dattr_new)
- {
- int i, j, n;
- int new_topology;
- mutex_lock(&sched_domains_mutex);
-
- unregister_sched_domain_sysctl();
-
- new_topology = arch_update_cpu_topology();
- n = doms_new ? ndoms_new : 0;
-
- for (i = 0; i < ndoms_cur; i++) {
- for (j = 0; j < n && !new_topology; j++) {
- if (cpumask_equal(doms_cur[i], doms_new[j])
- && dattrs_equal(dattr_cur, i, dattr_new, j))
- goto match1;
- }
-
- detach_destroy_domains(doms_cur[i]);
- match1:
- ;
- }
- n = ndoms_cur;
- if (doms_new == NULL) {
- n = 0;
- doms_new = &fallback_doms;
- cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
- WARN_ON_ONCE(dattr_new);
- }
-
- for (i = 0; i < ndoms_new; i++) {
- for (j = 0; j < n && !new_topology; j++) {
- if (cpumask_equal(doms_new[i], doms_cur[j])
- && dattrs_equal(dattr_new, i, dattr_cur, j))
- goto match2;
- }
-
- build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
- match2:
- ;
- }
-
- if (doms_cur != &fallback_doms)
- free_sched_domains(doms_cur, ndoms_cur);
- kfree(dattr_cur);
- doms_cur = doms_new;
- dattr_cur = dattr_new;
- ndoms_cur = ndoms_new;
- register_sched_domain_sysctl();
- mutex_unlock(&sched_domains_mutex);
- }
- static int num_cpus_frozen;
- static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
- void *hcpu)
- {
- switch (action) {
- case CPU_ONLINE_FROZEN:
- case CPU_DOWN_FAILED_FROZEN:
-
- num_cpus_frozen--;
- if (likely(num_cpus_frozen)) {
- partition_sched_domains(1, NULL, NULL);
- break;
- }
-
- case CPU_ONLINE:
- cpuset_update_active_cpus(true);
- break;
- default:
- return NOTIFY_DONE;
- }
- return NOTIFY_OK;
- }
- static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
- void *hcpu)
- {
- unsigned long flags;
- long cpu = (long)hcpu;
- struct dl_bw *dl_b;
- bool overflow;
- int cpus;
- switch (action) {
- case CPU_DOWN_PREPARE:
- rcu_read_lock_sched();
- dl_b = dl_bw_of(cpu);
- raw_spin_lock_irqsave(&dl_b->lock, flags);
- cpus = dl_bw_cpus(cpu);
- overflow = __dl_overflow(dl_b, cpus, 0, 0);
- raw_spin_unlock_irqrestore(&dl_b->lock, flags);
- rcu_read_unlock_sched();
- if (overflow)
- return notifier_from_errno(-EBUSY);
- cpuset_update_active_cpus(false);
- break;
- case CPU_DOWN_PREPARE_FROZEN:
- num_cpus_frozen++;
- partition_sched_domains(1, NULL, NULL);
- break;
- default:
- return NOTIFY_DONE;
- }
- return NOTIFY_OK;
- }
- void __init sched_init_smp(void)
- {
- cpumask_var_t non_isolated_cpus;
- alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
- alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
-
- tick_nohz_full_add_cpus_to(cpu_isolated_map);
- sched_init_numa();
-
- mutex_lock(&sched_domains_mutex);
- init_sched_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);
- hotcpu_notifier(sched_domains_numa_masks_update, CPU_PRI_SCHED_ACTIVE);
- hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
- hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
- init_hrtick();
-
- 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();
- }
- #else
- void __init sched_init_smp(void)
- {
- sched_init_granularity();
- }
- #endif
- 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
- struct task_group root_task_group;
- LIST_HEAD(task_groups);
- #endif
- DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
- void __init sched_init(void)
- {
- int i, j;
- unsigned long alloc_size = 0, ptr;
- #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);
- #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
- #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
- }
- #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));
- }
- #endif
- 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
- #ifdef CONFIG_CGROUP_SCHED
- 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
- 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);
-
- init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
- init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
- #endif
- 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;
- rq->last_load_update_tick = jiffies;
- #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;
- INIT_LIST_HEAD(&rq->cfs_tasks);
- rq_attach_root(rq, &def_root_domain);
- #ifdef CONFIG_NO_HZ_COMMON
- rq->nohz_flags = 0;
- #endif
- #ifdef CONFIG_NO_HZ_FULL
- rq->last_sched_tick = 0;
- #endif
- #endif
- init_rq_hrtick(rq);
- atomic_set(&rq->nr_iowait, 0);
- }
- set_load_weight(&init_task);
- #ifdef CONFIG_PREEMPT_NOTIFIERS
- INIT_HLIST_HEAD(&init_task.preempt_notifiers);
- #endif
-
- atomic_inc(&init_mm.mm_count);
- enter_lazy_tlb(&init_mm, current);
-
- current->sched_class = &fair_sched_class;
-
- init_idle(current, smp_processor_id());
- calc_load_update = jiffies + LOAD_FREQ;
- #ifdef CONFIG_SMP
- zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
-
- if (cpu_isolated_map == NULL)
- zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
- idle_thread_set_boot_cpu();
- set_cpu_rq_start_time();
- #endif
- init_sched_fair_class();
- scheduler_running = 1;
- }
- #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
- static inline int preempt_count_equals(int preempt_offset)
- {
- int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
- return (nested == preempt_offset);
- }
- void __might_sleep(const char *file, int line, int preempt_offset)
- {
-
- 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)
- {
- static unsigned long prev_jiffy;
- rcu_sleep_check();
- if ((preempt_count_equals(preempt_offset) && !irqs_disabled() &&
- !is_idle_task(current)) ||
- system_state != SYSTEM_RUNNING || oops_in_progress)
- return;
- if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
- return;
- prev_jiffy = jiffies;
- 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);
- #ifdef CONFIG_DEBUG_PREEMPT
- if (!preempt_count_equals(preempt_offset)) {
- pr_err("Preemption disabled at:");
- print_ip_sym(current->preempt_disable_ip);
- pr_cont("\n");
- }
- #endif
- dump_stack();
- }
- 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) {
-
- if (p->flags & PF_KTHREAD)
- continue;
- p->se.exec_start = 0;
- #ifdef CONFIG_SCHEDSTATS
- p->se.statistics.wait_start = 0;
- p->se.statistics.sleep_start = 0;
- p->se.statistics.block_start = 0;
- #endif
- if (!dl_task(p) && !rt_task(p)) {
-
- if (task_nice(p) < 0)
- set_user_nice(p, 0);
- continue;
- }
- __sched_setscheduler(p, &attr, false, false);
- }
- read_unlock(&tasklist_lock);
- }
- #endif
- #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
- struct task_struct *curr_task(int cpu)
- {
- return cpu_curr(cpu);
- }
- #endif
- #ifdef CONFIG_IA64
- void set_curr_task(int cpu, struct task_struct *p)
- {
- cpu_curr(cpu) = p;
- }
- #endif
- #ifdef CONFIG_CGROUP_SCHED
- static DEFINE_SPINLOCK(task_group_lock);
- static void free_sched_group(struct task_group *tg)
- {
- free_fair_sched_group(tg);
- free_rt_sched_group(tg);
- autogroup_free(tg);
- kfree(tg);
- }
- struct task_group *sched_create_group(struct task_group *parent)
- {
- struct task_group *tg;
- tg = kzalloc(sizeof(*tg), GFP_KERNEL);
- if (!tg)
- return ERR_PTR(-ENOMEM);
- if (!alloc_fair_sched_group(tg, parent))
- goto err;
- if (!alloc_rt_sched_group(tg, parent))
- goto err;
- return tg;
- err:
- free_sched_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);
- 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);
- }
- static void free_sched_group_rcu(struct rcu_head *rhp)
- {
-
- free_sched_group(container_of(rhp, struct task_group, rcu));
- }
- void sched_destroy_group(struct task_group *tg)
- {
-
- call_rcu(&tg->rcu, free_sched_group_rcu);
- }
- void sched_offline_group(struct task_group *tg)
- {
- unsigned long flags;
- int i;
-
- for_each_possible_cpu(i)
- unregister_fair_sched_group(tg, i);
- spin_lock_irqsave(&task_group_lock, flags);
- list_del_rcu(&tg->list);
- list_del_rcu(&tg->siblings);
- spin_unlock_irqrestore(&task_group_lock, flags);
- }
- void sched_move_task(struct task_struct *tsk)
- {
- struct task_group *tg;
- int queued, running;
- unsigned long flags;
- struct rq *rq;
- rq = task_rq_lock(tsk, &flags);
- running = task_current(rq, tsk);
- queued = task_on_rq_queued(tsk);
- if (queued)
- dequeue_task(rq, tsk, 0);
- if (unlikely(running))
- put_prev_task(rq, tsk);
-
- 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_move_group)
- tsk->sched_class->task_move_group(tsk, queued);
- else
- #endif
- set_task_rq(tsk, task_cpu(tsk));
- if (unlikely(running))
- tsk->sched_class->set_curr_task(rq);
- if (queued)
- enqueue_task(rq, tsk, 0);
- task_rq_unlock(rq, tsk, &flags);
- }
- #endif
- #ifdef CONFIG_RT_GROUP_SCHED
- static DEFINE_MUTEX(rt_constraints_mutex);
- static inline int tg_has_rt_tasks(struct task_group *tg)
- {
- struct task_struct *g, *p;
-
- if (task_group_is_autogroup(tg))
- return 0;
- for_each_process_thread(g, p) {
- if (rt_task(p) && task_group(p) == tg)
- return 1;
- }
- return 0;
- }
- struct rt_schedulable_data {
- struct task_group *tg;
- u64 rt_period;
- u64 rt_runtime;
- };
- static int tg_rt_schedulable(struct task_group *tg, void *data)
- {
- struct rt_schedulable_data *d = data;
- struct task_group *child;
- unsigned long total, sum = 0;
- u64 period, runtime;
- period = ktime_to_ns(tg->rt_bandwidth.rt_period);
- runtime = tg->rt_bandwidth.rt_runtime;
- if (tg == d->tg) {
- period = d->rt_period;
- runtime = d->rt_runtime;
- }
-
- if (runtime > period && runtime != RUNTIME_INF)
- return -EINVAL;
-
- if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
- return -EBUSY;
- total = to_ratio(period, runtime);
-
- if (total > to_ratio(global_rt_period(), global_rt_runtime()))
- return -EINVAL;
-
- list_for_each_entry_rcu(child, &tg->children, siblings) {
- period = ktime_to_ns(child->rt_bandwidth.rt_period);
- runtime = child->rt_bandwidth.rt_runtime;
- if (child == d->tg) {
- period = d->rt_period;
- runtime = d->rt_runtime;
- }
- sum += to_ratio(period, runtime);
- }
- if (sum > total)
- return -EINVAL;
- return 0;
- }
- static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
- {
- int ret;
- struct rt_schedulable_data data = {
- .tg = tg,
- .rt_period = period,
- .rt_runtime = runtime,
- };
- rcu_read_lock();
- ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
- rcu_read_unlock();
- return ret;
- }
- static int tg_set_rt_bandwidth(struct task_group *tg,
- u64 rt_period, u64 rt_runtime)
- {
- int i, err = 0;
-
- if (tg == &root_task_group && rt_runtime == 0)
- return -EINVAL;
-
- if (rt_period == 0)
- return -EINVAL;
- mutex_lock(&rt_constraints_mutex);
- read_lock(&tasklist_lock);
- err = __rt_schedulable(tg, rt_period, rt_runtime);
- if (err)
- goto unlock;
- raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
- tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
- tg->rt_bandwidth.rt_runtime = rt_runtime;
- for_each_possible_cpu(i) {
- struct rt_rq *rt_rq = tg->rt_rq[i];
- raw_spin_lock(&rt_rq->rt_runtime_lock);
- rt_rq->rt_runtime = rt_runtime;
- raw_spin_unlock(&rt_rq->rt_runtime_lock);
- }
- raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
- unlock:
- read_unlock(&tasklist_lock);
- mutex_unlock(&rt_constraints_mutex);
- return err;
- }
- static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
- {
- u64 rt_runtime, rt_period;
- rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
- rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
- if (rt_runtime_us < 0)
- rt_runtime = RUNTIME_INF;
- return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
- }
- static long sched_group_rt_runtime(struct task_group *tg)
- {
- u64 rt_runtime_us;
- if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
- return -1;
- rt_runtime_us = tg->rt_bandwidth.rt_runtime;
- do_div(rt_runtime_us, NSEC_PER_USEC);
- return rt_runtime_us;
- }
- static int sched_group_set_rt_period(struct task_group *tg, u64 rt_period_us)
- {
- u64 rt_runtime, rt_period;
- rt_period = rt_period_us * NSEC_PER_USEC;
- rt_runtime = tg->rt_bandwidth.rt_runtime;
- return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
- }
- static long sched_group_rt_period(struct task_group *tg)
- {
- u64 rt_period_us;
- rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
- do_div(rt_period_us, NSEC_PER_USEC);
- return rt_period_us;
- }
- #endif
- #ifdef CONFIG_RT_GROUP_SCHED
- static int sched_rt_global_constraints(void)
- {
- int ret = 0;
- mutex_lock(&rt_constraints_mutex);
- read_lock(&tasklist_lock);
- ret = __rt_schedulable(NULL, 0, 0);
- read_unlock(&tasklist_lock);
- mutex_unlock(&rt_constraints_mutex);
- return ret;
- }
- static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
- {
-
- if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
- return 0;
- return 1;
- }
- #else
- static int sched_rt_global_constraints(void)
- {
- unsigned long flags;
- int i, ret = 0;
- raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
- for_each_possible_cpu(i) {
- struct rt_rq *rt_rq = &cpu_rq(i)->rt;
- raw_spin_lock(&rt_rq->rt_runtime_lock);
- rt_rq->rt_runtime = global_rt_runtime();
- raw_spin_unlock(&rt_rq->rt_runtime_lock);
- }
- raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
- return ret;
- }
- #endif
- static int sched_dl_global_validate(void)
- {
- u64 runtime = global_rt_runtime();
- u64 period = global_rt_period();
- u64 new_bw = to_ratio(period, runtime);
- struct dl_bw *dl_b;
- int cpu, ret = 0;
- unsigned long flags;
-
- for_each_possible_cpu(cpu) {
- rcu_read_lock_sched();
- dl_b = dl_bw_of(cpu);
- raw_spin_lock_irqsave(&dl_b->lock, flags);
- if (new_bw < dl_b->total_bw)
- ret = -EBUSY;
- raw_spin_unlock_irqrestore(&dl_b->lock, flags);
- rcu_read_unlock_sched();
- if (ret)
- break;
- }
- return ret;
- }
- static void sched_dl_do_global(void)
- {
- u64 new_bw = -1;
- struct dl_bw *dl_b;
- int cpu;
- unsigned long flags;
- def_dl_bandwidth.dl_period = global_rt_period();
- def_dl_bandwidth.dl_runtime = global_rt_runtime();
- if (global_rt_runtime() != RUNTIME_INF)
- new_bw = to_ratio(global_rt_period(), global_rt_runtime());
-
- for_each_possible_cpu(cpu) {
- rcu_read_lock_sched();
- dl_b = dl_bw_of(cpu);
- raw_spin_lock_irqsave(&dl_b->lock, flags);
- dl_b->bw = new_bw;
- raw_spin_unlock_irqrestore(&dl_b->lock, flags);
- rcu_read_unlock_sched();
- }
- }
- static int sched_rt_global_validate(void)
- {
- if (sysctl_sched_rt_period <= 0)
- return -EINVAL;
- if ((sysctl_sched_rt_runtime != RUNTIME_INF) &&
- (sysctl_sched_rt_runtime > sysctl_sched_rt_period))
- return -EINVAL;
- return 0;
- }
- static void sched_rt_do_global(void)
- {
- def_rt_bandwidth.rt_runtime = global_rt_runtime();
- def_rt_bandwidth.rt_period = ns_to_ktime(global_rt_period());
- }
- int sched_rt_handler(struct ctl_table *table, int write,
- void __user *buffer, size_t *lenp,
- loff_t *ppos)
- {
- int old_period, old_runtime;
- static DEFINE_MUTEX(mutex);
- int ret;
- mutex_lock(&mutex);
- old_period = sysctl_sched_rt_period;
- old_runtime = sysctl_sched_rt_runtime;
- ret = proc_dointvec(table, write, buffer, lenp, ppos);
- if (!ret && write) {
- ret = sched_rt_global_validate();
- if (ret)
- goto undo;
- ret = sched_dl_global_validate();
- if (ret)
- goto undo;
- ret = sched_rt_global_constraints();
- if (ret)
- goto undo;
- sched_rt_do_global();
- sched_dl_do_global();
- }
- if (0) {
- undo:
- sysctl_sched_rt_period = old_period;
- sysctl_sched_rt_runtime = old_runtime;
- }
- mutex_unlock(&mutex);
- return ret;
- }
- int sched_rr_handler(struct ctl_table *table, int write,
- void __user *buffer, size_t *lenp,
- loff_t *ppos)
- {
- int ret;
- static DEFINE_MUTEX(mutex);
- mutex_lock(&mutex);
- ret = proc_dointvec(table, write, buffer, lenp, ppos);
-
-
- if (!ret && write) {
- sched_rr_timeslice = sched_rr_timeslice <= 0 ?
- RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice);
- }
- mutex_unlock(&mutex);
- return ret;
- }
- #ifdef CONFIG_CGROUP_SCHED
- 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) {
-
- return &root_task_group.css;
- }
- tg = sched_create_group(parent);
- if (IS_ERR(tg))
- return ERR_PTR(-ENOMEM);
- return &tg->css;
- }
- 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_free(struct cgroup_subsys_state *css)
- {
- struct task_group *tg = css_tg(css);
- sched_destroy_group(tg);
- }
- static void cpu_cgroup_css_offline(struct cgroup_subsys_state *css)
- {
- struct task_group *tg = css_tg(css);
- sched_offline_group(tg);
- }
- static void cpu_cgroup_fork(struct task_struct *task)
- {
- sched_move_task(task);
- }
- static int cpu_cgroup_can_attach(struct cgroup_subsys_state *css,
- struct cgroup_taskset *tset)
- {
- struct task_struct *task;
- cgroup_taskset_for_each(task, tset) {
- #ifdef CONFIG_RT_GROUP_SCHED
- if (!sched_rt_can_attach(css_tg(css), task))
- return -EINVAL;
- #else
-
- if (task->sched_class != &fair_sched_class)
- return -EINVAL;
- #endif
- }
- return 0;
- }
- static void cpu_cgroup_attach(struct cgroup_subsys_state *css,
- struct cgroup_taskset *tset)
- {
- struct task_struct *task;
- cgroup_taskset_for_each(task, tset)
- sched_move_task(task);
- }
- static void cpu_cgroup_exit(struct cgroup_subsys_state *css,
- struct cgroup_subsys_state *old_css,
- struct task_struct *task)
- {
-
- if (!(task->flags & PF_EXITING))
- return;
- sched_move_task(task);
- }
- #ifdef CONFIG_FAIR_GROUP_SCHED
- static int cpu_shares_write_u64(struct cgroup_subsys_state *css,
- struct cftype *cftype, u64 shareval)
- {
- 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;
- const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC;
- 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;
-
- if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
- return -EINVAL;
-
- if (period > max_cfs_quota_period)
- return -EINVAL;
-
- 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 (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);
-
- 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;
- raw_spin_lock_irq(&rq->lock);
- cfs_rq->runtime_enabled = runtime_enabled;
- cfs_rq->runtime_remaining = 0;
- if (cfs_rq->throttled)
- unthrottle_cfs_rq(cfs_rq);
- raw_spin_unlock_irq(&rq->lock);
- }
- 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
- quota = (u64)cfs_quota_us * NSEC_PER_USEC;
- 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;
- 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;
- };
- 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);
- }
-
- 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;
-
- 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
- #endif
- #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
- 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
- { }
- };
- struct cgroup_subsys cpu_cgrp_subsys = {
- .css_alloc = cpu_cgroup_css_alloc,
- .css_free = cpu_cgroup_css_free,
- .css_online = cpu_cgroup_css_online,
- .css_offline = cpu_cgroup_css_offline,
- .fork = cpu_cgroup_fork,
- .can_attach = cpu_cgroup_can_attach,
- .attach = cpu_cgroup_attach,
- .exit = cpu_cgroup_exit,
- .legacy_cftypes = cpu_files,
- .early_init = 1,
- };
- #endif
- void dump_cpu_task(int cpu)
- {
- pr_info("Task dump for CPU %d:\n", cpu);
- sched_show_task(cpu_curr(cpu));
- }
|