core.c 192 KB

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