sched.c 214 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917
  1. /*
  2. * kernel/sched.c
  3. *
  4. * Kernel scheduler and related syscalls
  5. *
  6. * Copyright (C) 1991-2002 Linus Torvalds
  7. *
  8. * 1996-12-23 Modified by Dave Grothe to fix bugs in semaphores and
  9. * make semaphores SMP safe
  10. * 1998-11-19 Implemented schedule_timeout() and related stuff
  11. * by Andrea Arcangeli
  12. * 2002-01-04 New ultra-scalable O(1) scheduler by Ingo Molnar:
  13. * hybrid priority-list and round-robin design with
  14. * an array-switch method of distributing timeslices
  15. * and per-CPU runqueues. Cleanups and useful suggestions
  16. * by Davide Libenzi, preemptible kernel bits by Robert Love.
  17. * 2003-09-03 Interactivity tuning by Con Kolivas.
  18. * 2004-04-02 Scheduler domains code by Nick Piggin
  19. * 2007-04-15 Work begun on replacing all interactivity tuning with a
  20. * fair scheduling design by Con Kolivas.
  21. * 2007-05-05 Load balancing (smp-nice) and other improvements
  22. * by Peter Williams
  23. * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
  24. * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
  25. * 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 <linux/smp_lock.h>
  35. #include <asm/mmu_context.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/capability.h>
  38. #include <linux/completion.h>
  39. #include <linux/kernel_stat.h>
  40. #include <linux/debug_locks.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/kthread.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/reciprocal_div.h>
  65. #include <linux/unistd.h>
  66. #include <linux/pagemap.h>
  67. #include <linux/hrtimer.h>
  68. #include <linux/tick.h>
  69. #include <linux/bootmem.h>
  70. #include <linux/debugfs.h>
  71. #include <linux/ctype.h>
  72. #include <linux/ftrace.h>
  73. #include <asm/tlb.h>
  74. #include <asm/irq_regs.h>
  75. /*
  76. * Convert user-nice values [ -20 ... 0 ... 19 ]
  77. * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  78. * and back.
  79. */
  80. #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  81. #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  82. #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  83. /*
  84. * 'User priority' is the nice value converted to something we
  85. * can work with better when scaling various scheduler parameters,
  86. * it's a [ 0 ... 39 ] range.
  87. */
  88. #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
  89. #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  90. #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  91. /*
  92. * Helpers for converting nanosecond timing to jiffy resolution
  93. */
  94. #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
  95. #define NICE_0_LOAD SCHED_LOAD_SCALE
  96. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  97. /*
  98. * These are the 'tuning knobs' of the scheduler:
  99. *
  100. * default timeslice is 100 msecs (used only for SCHED_RR tasks).
  101. * Timeslices get refilled after they expire.
  102. */
  103. #define DEF_TIMESLICE (100 * HZ / 1000)
  104. /*
  105. * single value that denotes runtime == period, ie unlimited time.
  106. */
  107. #define RUNTIME_INF ((u64)~0ULL)
  108. #ifdef CONFIG_SMP
  109. /*
  110. * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
  111. * Since cpu_power is a 'constant', we can use a reciprocal divide.
  112. */
  113. static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
  114. {
  115. return reciprocal_divide(load, sg->reciprocal_cpu_power);
  116. }
  117. /*
  118. * Each time a sched group cpu_power is changed,
  119. * we must compute its reciprocal value
  120. */
  121. static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
  122. {
  123. sg->__cpu_power += val;
  124. sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
  125. }
  126. #endif
  127. static inline int rt_policy(int policy)
  128. {
  129. if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
  130. return 1;
  131. return 0;
  132. }
  133. static inline int task_has_rt_policy(struct task_struct *p)
  134. {
  135. return rt_policy(p->policy);
  136. }
  137. /*
  138. * This is the priority-queue data structure of the RT scheduling class:
  139. */
  140. struct rt_prio_array {
  141. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  142. struct list_head queue[MAX_RT_PRIO];
  143. };
  144. struct rt_bandwidth {
  145. /* nests inside the rq lock: */
  146. spinlock_t rt_runtime_lock;
  147. ktime_t rt_period;
  148. u64 rt_runtime;
  149. struct hrtimer rt_period_timer;
  150. };
  151. static struct rt_bandwidth def_rt_bandwidth;
  152. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
  153. static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
  154. {
  155. struct rt_bandwidth *rt_b =
  156. container_of(timer, struct rt_bandwidth, rt_period_timer);
  157. ktime_t now;
  158. int overrun;
  159. int idle = 0;
  160. for (;;) {
  161. now = hrtimer_cb_get_time(timer);
  162. overrun = hrtimer_forward(timer, now, rt_b->rt_period);
  163. if (!overrun)
  164. break;
  165. idle = do_sched_rt_period_timer(rt_b, overrun);
  166. }
  167. return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
  168. }
  169. static
  170. void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
  171. {
  172. rt_b->rt_period = ns_to_ktime(period);
  173. rt_b->rt_runtime = runtime;
  174. spin_lock_init(&rt_b->rt_runtime_lock);
  175. hrtimer_init(&rt_b->rt_period_timer,
  176. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  177. rt_b->rt_period_timer.function = sched_rt_period_timer;
  178. rt_b->rt_period_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  179. }
  180. static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
  181. {
  182. ktime_t now;
  183. if (rt_b->rt_runtime == RUNTIME_INF)
  184. return;
  185. if (hrtimer_active(&rt_b->rt_period_timer))
  186. return;
  187. spin_lock(&rt_b->rt_runtime_lock);
  188. for (;;) {
  189. if (hrtimer_active(&rt_b->rt_period_timer))
  190. break;
  191. now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
  192. hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
  193. hrtimer_start(&rt_b->rt_period_timer,
  194. rt_b->rt_period_timer.expires,
  195. HRTIMER_MODE_ABS);
  196. }
  197. spin_unlock(&rt_b->rt_runtime_lock);
  198. }
  199. #ifdef CONFIG_RT_GROUP_SCHED
  200. static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
  201. {
  202. hrtimer_cancel(&rt_b->rt_period_timer);
  203. }
  204. #endif
  205. /*
  206. * sched_domains_mutex serializes calls to arch_init_sched_domains,
  207. * detach_destroy_domains and partition_sched_domains.
  208. */
  209. static DEFINE_MUTEX(sched_domains_mutex);
  210. #ifdef CONFIG_GROUP_SCHED
  211. #include <linux/cgroup.h>
  212. struct cfs_rq;
  213. static LIST_HEAD(task_groups);
  214. /* task group related information */
  215. struct task_group {
  216. #ifdef CONFIG_CGROUP_SCHED
  217. struct cgroup_subsys_state css;
  218. #endif
  219. #ifdef CONFIG_FAIR_GROUP_SCHED
  220. /* schedulable entities of this group on each cpu */
  221. struct sched_entity **se;
  222. /* runqueue "owned" by this group on each cpu */
  223. struct cfs_rq **cfs_rq;
  224. unsigned long shares;
  225. #endif
  226. #ifdef CONFIG_RT_GROUP_SCHED
  227. struct sched_rt_entity **rt_se;
  228. struct rt_rq **rt_rq;
  229. struct rt_bandwidth rt_bandwidth;
  230. #endif
  231. struct rcu_head rcu;
  232. struct list_head list;
  233. struct task_group *parent;
  234. struct list_head siblings;
  235. struct list_head children;
  236. };
  237. #ifdef CONFIG_USER_SCHED
  238. /*
  239. * Root task group.
  240. * Every UID task group (including init_task_group aka UID-0) will
  241. * be a child to this group.
  242. */
  243. struct task_group root_task_group;
  244. #ifdef CONFIG_FAIR_GROUP_SCHED
  245. /* Default task group's sched entity on each cpu */
  246. static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
  247. /* Default task group's cfs_rq on each cpu */
  248. static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
  249. #endif
  250. #ifdef CONFIG_RT_GROUP_SCHED
  251. static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
  252. static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
  253. #endif
  254. #else
  255. #define root_task_group init_task_group
  256. #endif
  257. /* task_group_lock serializes add/remove of task groups and also changes to
  258. * a task group's cpu shares.
  259. */
  260. static DEFINE_SPINLOCK(task_group_lock);
  261. #ifdef CONFIG_FAIR_GROUP_SCHED
  262. #ifdef CONFIG_USER_SCHED
  263. # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
  264. #else
  265. # define INIT_TASK_GROUP_LOAD NICE_0_LOAD
  266. #endif
  267. /*
  268. * A weight of 0 or 1 can cause arithmetics problems.
  269. * A weight of a cfs_rq is the sum of weights of which entities
  270. * are queued on this cfs_rq, so a weight of a entity should not be
  271. * too large, so as the shares value of a task group.
  272. * (The default weight is 1024 - so there's no practical
  273. * limitation from this.)
  274. */
  275. #define MIN_SHARES 2
  276. #define MAX_SHARES (1UL << 18)
  277. static int init_task_group_load = INIT_TASK_GROUP_LOAD;
  278. #endif
  279. /* Default task group.
  280. * Every task in system belong to this group at bootup.
  281. */
  282. struct task_group init_task_group;
  283. /* return group to which a task belongs */
  284. static inline struct task_group *task_group(struct task_struct *p)
  285. {
  286. struct task_group *tg;
  287. #ifdef CONFIG_USER_SCHED
  288. tg = p->user->tg;
  289. #elif defined(CONFIG_CGROUP_SCHED)
  290. tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
  291. struct task_group, css);
  292. #else
  293. tg = &init_task_group;
  294. #endif
  295. return tg;
  296. }
  297. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  298. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  299. {
  300. #ifdef CONFIG_FAIR_GROUP_SCHED
  301. p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
  302. p->se.parent = task_group(p)->se[cpu];
  303. #endif
  304. #ifdef CONFIG_RT_GROUP_SCHED
  305. p->rt.rt_rq = task_group(p)->rt_rq[cpu];
  306. p->rt.parent = task_group(p)->rt_se[cpu];
  307. #endif
  308. }
  309. #else
  310. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  311. #endif /* CONFIG_GROUP_SCHED */
  312. /* CFS-related fields in a runqueue */
  313. struct cfs_rq {
  314. struct load_weight load;
  315. unsigned long nr_running;
  316. u64 exec_clock;
  317. u64 min_vruntime;
  318. struct rb_root tasks_timeline;
  319. struct rb_node *rb_leftmost;
  320. struct list_head tasks;
  321. struct list_head *balance_iterator;
  322. /*
  323. * 'curr' points to currently running entity on this cfs_rq.
  324. * It is set to NULL otherwise (i.e when none are currently running).
  325. */
  326. struct sched_entity *curr, *next;
  327. unsigned long nr_spread_over;
  328. #ifdef CONFIG_FAIR_GROUP_SCHED
  329. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  330. /*
  331. * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  332. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  333. * (like users, containers etc.)
  334. *
  335. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  336. * list is used during load balance.
  337. */
  338. struct list_head leaf_cfs_rq_list;
  339. struct task_group *tg; /* group that "owns" this runqueue */
  340. #endif
  341. };
  342. /* Real-Time classes' related field in a runqueue: */
  343. struct rt_rq {
  344. struct rt_prio_array active;
  345. unsigned long rt_nr_running;
  346. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  347. int highest_prio; /* highest queued rt task prio */
  348. #endif
  349. #ifdef CONFIG_SMP
  350. unsigned long rt_nr_migratory;
  351. int overloaded;
  352. #endif
  353. int rt_throttled;
  354. u64 rt_time;
  355. u64 rt_runtime;
  356. /* Nests inside the rq lock: */
  357. spinlock_t rt_runtime_lock;
  358. #ifdef CONFIG_RT_GROUP_SCHED
  359. unsigned long rt_nr_boosted;
  360. struct rq *rq;
  361. struct list_head leaf_rt_rq_list;
  362. struct task_group *tg;
  363. struct sched_rt_entity *rt_se;
  364. #endif
  365. };
  366. #ifdef CONFIG_SMP
  367. /*
  368. * We add the notion of a root-domain which will be used to define per-domain
  369. * variables. Each exclusive cpuset essentially defines an island domain by
  370. * fully partitioning the member cpus from any other cpuset. Whenever a new
  371. * exclusive cpuset is created, we also create and attach a new root-domain
  372. * object.
  373. *
  374. */
  375. struct root_domain {
  376. atomic_t refcount;
  377. cpumask_t span;
  378. cpumask_t online;
  379. /*
  380. * The "RT overload" flag: it gets set if a CPU has more than
  381. * one runnable RT task.
  382. */
  383. cpumask_t rto_mask;
  384. atomic_t rto_count;
  385. };
  386. /*
  387. * By default the system creates a single root-domain with all cpus as
  388. * members (mimicking the global state we have today).
  389. */
  390. static struct root_domain def_root_domain;
  391. #endif
  392. /*
  393. * This is the main, per-CPU runqueue data structure.
  394. *
  395. * Locking rule: those places that want to lock multiple runqueues
  396. * (such as the load balancing or the thread migration code), lock
  397. * acquire operations must be ordered by ascending &runqueue.
  398. */
  399. struct rq {
  400. /* runqueue lock: */
  401. spinlock_t lock;
  402. /*
  403. * nr_running and cpu_load should be in the same cacheline because
  404. * remote CPUs use both these fields when doing load calculation.
  405. */
  406. unsigned long nr_running;
  407. #define CPU_LOAD_IDX_MAX 5
  408. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  409. unsigned char idle_at_tick;
  410. #ifdef CONFIG_NO_HZ
  411. unsigned long last_tick_seen;
  412. unsigned char in_nohz_recently;
  413. #endif
  414. /* capture load from *all* tasks on this cpu: */
  415. struct load_weight load;
  416. unsigned long nr_load_updates;
  417. u64 nr_switches;
  418. struct cfs_rq cfs;
  419. struct rt_rq rt;
  420. #ifdef CONFIG_FAIR_GROUP_SCHED
  421. /* list of leaf cfs_rq on this cpu: */
  422. struct list_head leaf_cfs_rq_list;
  423. #endif
  424. #ifdef CONFIG_RT_GROUP_SCHED
  425. struct list_head leaf_rt_rq_list;
  426. #endif
  427. /*
  428. * This is part of a global counter where only the total sum
  429. * over all CPUs matters. A task can increase this counter on
  430. * one CPU and if it got migrated afterwards it may decrease
  431. * it on another CPU. Always updated under the runqueue lock:
  432. */
  433. unsigned long nr_uninterruptible;
  434. struct task_struct *curr, *idle;
  435. unsigned long next_balance;
  436. struct mm_struct *prev_mm;
  437. u64 clock;
  438. atomic_t nr_iowait;
  439. #ifdef CONFIG_SMP
  440. struct root_domain *rd;
  441. struct sched_domain *sd;
  442. /* For active balancing */
  443. int active_balance;
  444. int push_cpu;
  445. /* cpu of this runqueue: */
  446. int cpu;
  447. struct task_struct *migration_thread;
  448. struct list_head migration_queue;
  449. #endif
  450. #ifdef CONFIG_SCHED_HRTICK
  451. unsigned long hrtick_flags;
  452. ktime_t hrtick_expire;
  453. struct hrtimer hrtick_timer;
  454. #endif
  455. #ifdef CONFIG_SCHEDSTATS
  456. /* latency stats */
  457. struct sched_info rq_sched_info;
  458. /* sys_sched_yield() stats */
  459. unsigned int yld_exp_empty;
  460. unsigned int yld_act_empty;
  461. unsigned int yld_both_empty;
  462. unsigned int yld_count;
  463. /* schedule() stats */
  464. unsigned int sched_switch;
  465. unsigned int sched_count;
  466. unsigned int sched_goidle;
  467. /* try_to_wake_up() stats */
  468. unsigned int ttwu_count;
  469. unsigned int ttwu_local;
  470. /* BKL stats */
  471. unsigned int bkl_count;
  472. #endif
  473. struct lock_class_key rq_lock_key;
  474. };
  475. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  476. static inline void check_preempt_curr(struct rq *rq, struct task_struct *p)
  477. {
  478. rq->curr->sched_class->check_preempt_curr(rq, p);
  479. }
  480. static inline int cpu_of(struct rq *rq)
  481. {
  482. #ifdef CONFIG_SMP
  483. return rq->cpu;
  484. #else
  485. return 0;
  486. #endif
  487. }
  488. /*
  489. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  490. * See detach_destroy_domains: synchronize_sched for details.
  491. *
  492. * The domain tree of any CPU may only be accessed from within
  493. * preempt-disabled sections.
  494. */
  495. #define for_each_domain(cpu, __sd) \
  496. for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
  497. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  498. #define this_rq() (&__get_cpu_var(runqueues))
  499. #define task_rq(p) cpu_rq(task_cpu(p))
  500. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  501. static inline void update_rq_clock(struct rq *rq)
  502. {
  503. rq->clock = sched_clock_cpu(cpu_of(rq));
  504. }
  505. /*
  506. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  507. */
  508. #ifdef CONFIG_SCHED_DEBUG
  509. # define const_debug __read_mostly
  510. #else
  511. # define const_debug static const
  512. #endif
  513. /**
  514. * runqueue_is_locked
  515. *
  516. * Returns true if the current cpu runqueue is locked.
  517. * This interface allows printk to be called with the runqueue lock
  518. * held and know whether or not it is OK to wake up the klogd.
  519. */
  520. int runqueue_is_locked(void)
  521. {
  522. int cpu = get_cpu();
  523. struct rq *rq = cpu_rq(cpu);
  524. int ret;
  525. ret = spin_is_locked(&rq->lock);
  526. put_cpu();
  527. return ret;
  528. }
  529. /*
  530. * Debugging: various feature bits
  531. */
  532. #define SCHED_FEAT(name, enabled) \
  533. __SCHED_FEAT_##name ,
  534. enum {
  535. #include "sched_features.h"
  536. };
  537. #undef SCHED_FEAT
  538. #define SCHED_FEAT(name, enabled) \
  539. (1UL << __SCHED_FEAT_##name) * enabled |
  540. const_debug unsigned int sysctl_sched_features =
  541. #include "sched_features.h"
  542. 0;
  543. #undef SCHED_FEAT
  544. #ifdef CONFIG_SCHED_DEBUG
  545. #define SCHED_FEAT(name, enabled) \
  546. #name ,
  547. static __read_mostly char *sched_feat_names[] = {
  548. #include "sched_features.h"
  549. NULL
  550. };
  551. #undef SCHED_FEAT
  552. static int sched_feat_open(struct inode *inode, struct file *filp)
  553. {
  554. filp->private_data = inode->i_private;
  555. return 0;
  556. }
  557. static ssize_t
  558. sched_feat_read(struct file *filp, char __user *ubuf,
  559. size_t cnt, loff_t *ppos)
  560. {
  561. char *buf;
  562. int r = 0;
  563. int len = 0;
  564. int i;
  565. for (i = 0; sched_feat_names[i]; i++) {
  566. len += strlen(sched_feat_names[i]);
  567. len += 4;
  568. }
  569. buf = kmalloc(len + 2, GFP_KERNEL);
  570. if (!buf)
  571. return -ENOMEM;
  572. for (i = 0; sched_feat_names[i]; i++) {
  573. if (sysctl_sched_features & (1UL << i))
  574. r += sprintf(buf + r, "%s ", sched_feat_names[i]);
  575. else
  576. r += sprintf(buf + r, "NO_%s ", sched_feat_names[i]);
  577. }
  578. r += sprintf(buf + r, "\n");
  579. WARN_ON(r >= len + 2);
  580. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  581. kfree(buf);
  582. return r;
  583. }
  584. static ssize_t
  585. sched_feat_write(struct file *filp, const char __user *ubuf,
  586. size_t cnt, loff_t *ppos)
  587. {
  588. char buf[64];
  589. char *cmp = buf;
  590. int neg = 0;
  591. int i;
  592. if (cnt > 63)
  593. cnt = 63;
  594. if (copy_from_user(&buf, ubuf, cnt))
  595. return -EFAULT;
  596. buf[cnt] = 0;
  597. if (strncmp(buf, "NO_", 3) == 0) {
  598. neg = 1;
  599. cmp += 3;
  600. }
  601. for (i = 0; sched_feat_names[i]; i++) {
  602. int len = strlen(sched_feat_names[i]);
  603. if (strncmp(cmp, sched_feat_names[i], len) == 0) {
  604. if (neg)
  605. sysctl_sched_features &= ~(1UL << i);
  606. else
  607. sysctl_sched_features |= (1UL << i);
  608. break;
  609. }
  610. }
  611. if (!sched_feat_names[i])
  612. return -EINVAL;
  613. filp->f_pos += cnt;
  614. return cnt;
  615. }
  616. static struct file_operations sched_feat_fops = {
  617. .open = sched_feat_open,
  618. .read = sched_feat_read,
  619. .write = sched_feat_write,
  620. };
  621. static __init int sched_init_debug(void)
  622. {
  623. debugfs_create_file("sched_features", 0644, NULL, NULL,
  624. &sched_feat_fops);
  625. return 0;
  626. }
  627. late_initcall(sched_init_debug);
  628. #endif
  629. #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  630. /*
  631. * Number of tasks to iterate in a single balance run.
  632. * Limited because this is done with IRQs disabled.
  633. */
  634. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  635. /*
  636. * period over which we measure -rt task cpu usage in us.
  637. * default: 1s
  638. */
  639. unsigned int sysctl_sched_rt_period = 1000000;
  640. static __read_mostly int scheduler_running;
  641. /*
  642. * part of the period that we allow rt tasks to run in us.
  643. * default: 0.95s
  644. */
  645. int sysctl_sched_rt_runtime = 950000;
  646. static inline u64 global_rt_period(void)
  647. {
  648. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  649. }
  650. static inline u64 global_rt_runtime(void)
  651. {
  652. if (sysctl_sched_rt_period < 0)
  653. return RUNTIME_INF;
  654. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  655. }
  656. unsigned long long time_sync_thresh = 100000;
  657. static DEFINE_PER_CPU(unsigned long long, time_offset);
  658. static DEFINE_PER_CPU(unsigned long long, prev_cpu_time);
  659. /*
  660. * Global lock which we take every now and then to synchronize
  661. * the CPUs time. This method is not warp-safe, but it's good
  662. * enough to synchronize slowly diverging time sources and thus
  663. * it's good enough for tracing:
  664. */
  665. static DEFINE_SPINLOCK(time_sync_lock);
  666. static unsigned long long prev_global_time;
  667. static unsigned long long __sync_cpu_clock(unsigned long long time, int cpu)
  668. {
  669. /*
  670. * We want this inlined, to not get tracer function calls
  671. * in this critical section:
  672. */
  673. spin_acquire(&time_sync_lock.dep_map, 0, 0, _THIS_IP_);
  674. __raw_spin_lock(&time_sync_lock.raw_lock);
  675. if (time < prev_global_time) {
  676. per_cpu(time_offset, cpu) += prev_global_time - time;
  677. time = prev_global_time;
  678. } else {
  679. prev_global_time = time;
  680. }
  681. __raw_spin_unlock(&time_sync_lock.raw_lock);
  682. spin_release(&time_sync_lock.dep_map, 1, _THIS_IP_);
  683. return time;
  684. }
  685. static unsigned long long __cpu_clock(int cpu)
  686. {
  687. unsigned long long now;
  688. /*
  689. * Only call sched_clock() if the scheduler has already been
  690. * initialized (some code might call cpu_clock() very early):
  691. */
  692. if (unlikely(!scheduler_running))
  693. return 0;
  694. now = sched_clock_cpu(cpu);
  695. return now;
  696. }
  697. /*
  698. * For kernel-internal use: high-speed (but slightly incorrect) per-cpu
  699. * clock constructed from sched_clock():
  700. */
  701. unsigned long long notrace cpu_clock(int cpu)
  702. {
  703. unsigned long long prev_cpu_time, time, delta_time;
  704. unsigned long flags;
  705. local_irq_save(flags);
  706. prev_cpu_time = per_cpu(prev_cpu_time, cpu);
  707. time = __cpu_clock(cpu) + per_cpu(time_offset, cpu);
  708. delta_time = time-prev_cpu_time;
  709. if (unlikely(delta_time > time_sync_thresh)) {
  710. time = __sync_cpu_clock(time, cpu);
  711. per_cpu(prev_cpu_time, cpu) = time;
  712. }
  713. local_irq_restore(flags);
  714. return time;
  715. }
  716. EXPORT_SYMBOL_GPL(cpu_clock);
  717. #ifndef prepare_arch_switch
  718. # define prepare_arch_switch(next) do { } while (0)
  719. #endif
  720. #ifndef finish_arch_switch
  721. # define finish_arch_switch(prev) do { } while (0)
  722. #endif
  723. static inline int task_current(struct rq *rq, struct task_struct *p)
  724. {
  725. return rq->curr == p;
  726. }
  727. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  728. static inline int task_running(struct rq *rq, struct task_struct *p)
  729. {
  730. return task_current(rq, p);
  731. }
  732. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  733. {
  734. }
  735. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  736. {
  737. #ifdef CONFIG_DEBUG_SPINLOCK
  738. /* this is a valid case when another task releases the spinlock */
  739. rq->lock.owner = current;
  740. #endif
  741. /*
  742. * If we are tracking spinlock dependencies then we have to
  743. * fix up the runqueue lock - which gets 'carried over' from
  744. * prev into current:
  745. */
  746. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  747. spin_unlock_irq(&rq->lock);
  748. }
  749. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  750. static inline int task_running(struct rq *rq, struct task_struct *p)
  751. {
  752. #ifdef CONFIG_SMP
  753. return p->oncpu;
  754. #else
  755. return task_current(rq, p);
  756. #endif
  757. }
  758. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  759. {
  760. #ifdef CONFIG_SMP
  761. /*
  762. * We can optimise this out completely for !SMP, because the
  763. * SMP rebalancing from interrupt is the only thing that cares
  764. * here.
  765. */
  766. next->oncpu = 1;
  767. #endif
  768. #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  769. spin_unlock_irq(&rq->lock);
  770. #else
  771. spin_unlock(&rq->lock);
  772. #endif
  773. }
  774. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  775. {
  776. #ifdef CONFIG_SMP
  777. /*
  778. * After ->oncpu is cleared, the task can be moved to a different CPU.
  779. * We must ensure this doesn't happen until the switch is completely
  780. * finished.
  781. */
  782. smp_wmb();
  783. prev->oncpu = 0;
  784. #endif
  785. #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  786. local_irq_enable();
  787. #endif
  788. }
  789. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  790. /*
  791. * __task_rq_lock - lock the runqueue a given task resides on.
  792. * Must be called interrupts disabled.
  793. */
  794. static inline struct rq *__task_rq_lock(struct task_struct *p)
  795. __acquires(rq->lock)
  796. {
  797. for (;;) {
  798. struct rq *rq = task_rq(p);
  799. spin_lock(&rq->lock);
  800. if (likely(rq == task_rq(p)))
  801. return rq;
  802. spin_unlock(&rq->lock);
  803. }
  804. }
  805. /*
  806. * task_rq_lock - lock the runqueue a given task resides on and disable
  807. * interrupts. Note the ordering: we can safely lookup the task_rq without
  808. * explicitly disabling preemption.
  809. */
  810. static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
  811. __acquires(rq->lock)
  812. {
  813. struct rq *rq;
  814. for (;;) {
  815. local_irq_save(*flags);
  816. rq = task_rq(p);
  817. spin_lock(&rq->lock);
  818. if (likely(rq == task_rq(p)))
  819. return rq;
  820. spin_unlock_irqrestore(&rq->lock, *flags);
  821. }
  822. }
  823. static void __task_rq_unlock(struct rq *rq)
  824. __releases(rq->lock)
  825. {
  826. spin_unlock(&rq->lock);
  827. }
  828. static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
  829. __releases(rq->lock)
  830. {
  831. spin_unlock_irqrestore(&rq->lock, *flags);
  832. }
  833. /*
  834. * this_rq_lock - lock this runqueue and disable interrupts.
  835. */
  836. static struct rq *this_rq_lock(void)
  837. __acquires(rq->lock)
  838. {
  839. struct rq *rq;
  840. local_irq_disable();
  841. rq = this_rq();
  842. spin_lock(&rq->lock);
  843. return rq;
  844. }
  845. static void __resched_task(struct task_struct *p, int tif_bit);
  846. static inline void resched_task(struct task_struct *p)
  847. {
  848. __resched_task(p, TIF_NEED_RESCHED);
  849. }
  850. #ifdef CONFIG_SCHED_HRTICK
  851. /*
  852. * Use HR-timers to deliver accurate preemption points.
  853. *
  854. * Its all a bit involved since we cannot program an hrt while holding the
  855. * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
  856. * reschedule event.
  857. *
  858. * When we get rescheduled we reprogram the hrtick_timer outside of the
  859. * rq->lock.
  860. */
  861. static inline void resched_hrt(struct task_struct *p)
  862. {
  863. __resched_task(p, TIF_HRTICK_RESCHED);
  864. }
  865. static inline void resched_rq(struct rq *rq)
  866. {
  867. unsigned long flags;
  868. spin_lock_irqsave(&rq->lock, flags);
  869. resched_task(rq->curr);
  870. spin_unlock_irqrestore(&rq->lock, flags);
  871. }
  872. enum {
  873. HRTICK_SET, /* re-programm hrtick_timer */
  874. HRTICK_RESET, /* not a new slice */
  875. HRTICK_BLOCK, /* stop hrtick operations */
  876. };
  877. /*
  878. * Use hrtick when:
  879. * - enabled by features
  880. * - hrtimer is actually high res
  881. */
  882. static inline int hrtick_enabled(struct rq *rq)
  883. {
  884. if (!sched_feat(HRTICK))
  885. return 0;
  886. if (unlikely(test_bit(HRTICK_BLOCK, &rq->hrtick_flags)))
  887. return 0;
  888. return hrtimer_is_hres_active(&rq->hrtick_timer);
  889. }
  890. /*
  891. * Called to set the hrtick timer state.
  892. *
  893. * called with rq->lock held and irqs disabled
  894. */
  895. static void hrtick_start(struct rq *rq, u64 delay, int reset)
  896. {
  897. assert_spin_locked(&rq->lock);
  898. /*
  899. * preempt at: now + delay
  900. */
  901. rq->hrtick_expire =
  902. ktime_add_ns(rq->hrtick_timer.base->get_time(), delay);
  903. /*
  904. * indicate we need to program the timer
  905. */
  906. __set_bit(HRTICK_SET, &rq->hrtick_flags);
  907. if (reset)
  908. __set_bit(HRTICK_RESET, &rq->hrtick_flags);
  909. /*
  910. * New slices are called from the schedule path and don't need a
  911. * forced reschedule.
  912. */
  913. if (reset)
  914. resched_hrt(rq->curr);
  915. }
  916. static void hrtick_clear(struct rq *rq)
  917. {
  918. if (hrtimer_active(&rq->hrtick_timer))
  919. hrtimer_cancel(&rq->hrtick_timer);
  920. }
  921. /*
  922. * Update the timer from the possible pending state.
  923. */
  924. static void hrtick_set(struct rq *rq)
  925. {
  926. ktime_t time;
  927. int set, reset;
  928. unsigned long flags;
  929. WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
  930. spin_lock_irqsave(&rq->lock, flags);
  931. set = __test_and_clear_bit(HRTICK_SET, &rq->hrtick_flags);
  932. reset = __test_and_clear_bit(HRTICK_RESET, &rq->hrtick_flags);
  933. time = rq->hrtick_expire;
  934. clear_thread_flag(TIF_HRTICK_RESCHED);
  935. spin_unlock_irqrestore(&rq->lock, flags);
  936. if (set) {
  937. hrtimer_start(&rq->hrtick_timer, time, HRTIMER_MODE_ABS);
  938. if (reset && !hrtimer_active(&rq->hrtick_timer))
  939. resched_rq(rq);
  940. } else
  941. hrtick_clear(rq);
  942. }
  943. /*
  944. * High-resolution timer tick.
  945. * Runs from hardirq context with interrupts disabled.
  946. */
  947. static enum hrtimer_restart hrtick(struct hrtimer *timer)
  948. {
  949. struct rq *rq = container_of(timer, struct rq, hrtick_timer);
  950. WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
  951. spin_lock(&rq->lock);
  952. update_rq_clock(rq);
  953. rq->curr->sched_class->task_tick(rq, rq->curr, 1);
  954. spin_unlock(&rq->lock);
  955. return HRTIMER_NORESTART;
  956. }
  957. #ifdef CONFIG_SMP
  958. static void hotplug_hrtick_disable(int cpu)
  959. {
  960. struct rq *rq = cpu_rq(cpu);
  961. unsigned long flags;
  962. spin_lock_irqsave(&rq->lock, flags);
  963. rq->hrtick_flags = 0;
  964. __set_bit(HRTICK_BLOCK, &rq->hrtick_flags);
  965. spin_unlock_irqrestore(&rq->lock, flags);
  966. hrtick_clear(rq);
  967. }
  968. static void hotplug_hrtick_enable(int cpu)
  969. {
  970. struct rq *rq = cpu_rq(cpu);
  971. unsigned long flags;
  972. spin_lock_irqsave(&rq->lock, flags);
  973. __clear_bit(HRTICK_BLOCK, &rq->hrtick_flags);
  974. spin_unlock_irqrestore(&rq->lock, flags);
  975. }
  976. static int
  977. hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
  978. {
  979. int cpu = (int)(long)hcpu;
  980. switch (action) {
  981. case CPU_UP_CANCELED:
  982. case CPU_UP_CANCELED_FROZEN:
  983. case CPU_DOWN_PREPARE:
  984. case CPU_DOWN_PREPARE_FROZEN:
  985. case CPU_DEAD:
  986. case CPU_DEAD_FROZEN:
  987. hotplug_hrtick_disable(cpu);
  988. return NOTIFY_OK;
  989. case CPU_UP_PREPARE:
  990. case CPU_UP_PREPARE_FROZEN:
  991. case CPU_DOWN_FAILED:
  992. case CPU_DOWN_FAILED_FROZEN:
  993. case CPU_ONLINE:
  994. case CPU_ONLINE_FROZEN:
  995. hotplug_hrtick_enable(cpu);
  996. return NOTIFY_OK;
  997. }
  998. return NOTIFY_DONE;
  999. }
  1000. static void init_hrtick(void)
  1001. {
  1002. hotcpu_notifier(hotplug_hrtick, 0);
  1003. }
  1004. #endif /* CONFIG_SMP */
  1005. static void init_rq_hrtick(struct rq *rq)
  1006. {
  1007. rq->hrtick_flags = 0;
  1008. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  1009. rq->hrtick_timer.function = hrtick;
  1010. rq->hrtick_timer.cb_mode = HRTIMER_CB_IRQSAFE_NO_SOFTIRQ;
  1011. }
  1012. void hrtick_resched(void)
  1013. {
  1014. struct rq *rq;
  1015. unsigned long flags;
  1016. if (!test_thread_flag(TIF_HRTICK_RESCHED))
  1017. return;
  1018. local_irq_save(flags);
  1019. rq = cpu_rq(smp_processor_id());
  1020. hrtick_set(rq);
  1021. local_irq_restore(flags);
  1022. }
  1023. #else
  1024. static inline void hrtick_clear(struct rq *rq)
  1025. {
  1026. }
  1027. static inline void hrtick_set(struct rq *rq)
  1028. {
  1029. }
  1030. static inline void init_rq_hrtick(struct rq *rq)
  1031. {
  1032. }
  1033. void hrtick_resched(void)
  1034. {
  1035. }
  1036. static inline void init_hrtick(void)
  1037. {
  1038. }
  1039. #endif
  1040. /*
  1041. * resched_task - mark a task 'to be rescheduled now'.
  1042. *
  1043. * On UP this means the setting of the need_resched flag, on SMP it
  1044. * might also involve a cross-CPU call to trigger the scheduler on
  1045. * the target CPU.
  1046. */
  1047. #ifdef CONFIG_SMP
  1048. #ifndef tsk_is_polling
  1049. #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
  1050. #endif
  1051. static void __resched_task(struct task_struct *p, int tif_bit)
  1052. {
  1053. int cpu;
  1054. assert_spin_locked(&task_rq(p)->lock);
  1055. if (unlikely(test_tsk_thread_flag(p, tif_bit)))
  1056. return;
  1057. set_tsk_thread_flag(p, tif_bit);
  1058. cpu = task_cpu(p);
  1059. if (cpu == smp_processor_id())
  1060. return;
  1061. /* NEED_RESCHED must be visible before we test polling */
  1062. smp_mb();
  1063. if (!tsk_is_polling(p))
  1064. smp_send_reschedule(cpu);
  1065. }
  1066. static void resched_cpu(int cpu)
  1067. {
  1068. struct rq *rq = cpu_rq(cpu);
  1069. unsigned long flags;
  1070. if (!spin_trylock_irqsave(&rq->lock, flags))
  1071. return;
  1072. resched_task(cpu_curr(cpu));
  1073. spin_unlock_irqrestore(&rq->lock, flags);
  1074. }
  1075. #ifdef CONFIG_NO_HZ
  1076. /*
  1077. * When add_timer_on() enqueues a timer into the timer wheel of an
  1078. * idle CPU then this timer might expire before the next timer event
  1079. * which is scheduled to wake up that CPU. In case of a completely
  1080. * idle system the next event might even be infinite time into the
  1081. * future. wake_up_idle_cpu() ensures that the CPU is woken up and
  1082. * leaves the inner idle loop so the newly added timer is taken into
  1083. * account when the CPU goes back to idle and evaluates the timer
  1084. * wheel for the next timer event.
  1085. */
  1086. void wake_up_idle_cpu(int cpu)
  1087. {
  1088. struct rq *rq = cpu_rq(cpu);
  1089. if (cpu == smp_processor_id())
  1090. return;
  1091. /*
  1092. * This is safe, as this function is called with the timer
  1093. * wheel base lock of (cpu) held. When the CPU is on the way
  1094. * to idle and has not yet set rq->curr to idle then it will
  1095. * be serialized on the timer wheel base lock and take the new
  1096. * timer into account automatically.
  1097. */
  1098. if (rq->curr != rq->idle)
  1099. return;
  1100. /*
  1101. * We can set TIF_RESCHED on the idle task of the other CPU
  1102. * lockless. The worst case is that the other CPU runs the
  1103. * idle task through an additional NOOP schedule()
  1104. */
  1105. set_tsk_thread_flag(rq->idle, TIF_NEED_RESCHED);
  1106. /* NEED_RESCHED must be visible before we test polling */
  1107. smp_mb();
  1108. if (!tsk_is_polling(rq->idle))
  1109. smp_send_reschedule(cpu);
  1110. }
  1111. #endif
  1112. #else
  1113. static void __resched_task(struct task_struct *p, int tif_bit)
  1114. {
  1115. assert_spin_locked(&task_rq(p)->lock);
  1116. set_tsk_thread_flag(p, tif_bit);
  1117. }
  1118. #endif
  1119. #if BITS_PER_LONG == 32
  1120. # define WMULT_CONST (~0UL)
  1121. #else
  1122. # define WMULT_CONST (1UL << 32)
  1123. #endif
  1124. #define WMULT_SHIFT 32
  1125. /*
  1126. * Shift right and round:
  1127. */
  1128. #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
  1129. static unsigned long
  1130. calc_delta_mine(unsigned long delta_exec, unsigned long weight,
  1131. struct load_weight *lw)
  1132. {
  1133. u64 tmp;
  1134. if (!lw->inv_weight) {
  1135. if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
  1136. lw->inv_weight = 1;
  1137. else
  1138. lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
  1139. / (lw->weight+1);
  1140. }
  1141. tmp = (u64)delta_exec * weight;
  1142. /*
  1143. * Check whether we'd overflow the 64-bit multiplication:
  1144. */
  1145. if (unlikely(tmp > WMULT_CONST))
  1146. tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
  1147. WMULT_SHIFT/2);
  1148. else
  1149. tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
  1150. return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
  1151. }
  1152. static inline unsigned long
  1153. calc_delta_fair(unsigned long delta_exec, struct load_weight *lw)
  1154. {
  1155. return calc_delta_mine(delta_exec, NICE_0_LOAD, lw);
  1156. }
  1157. static inline void update_load_add(struct load_weight *lw, unsigned long inc)
  1158. {
  1159. lw->weight += inc;
  1160. lw->inv_weight = 0;
  1161. }
  1162. static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
  1163. {
  1164. lw->weight -= dec;
  1165. lw->inv_weight = 0;
  1166. }
  1167. /*
  1168. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  1169. * of tasks with abnormal "nice" values across CPUs the contribution that
  1170. * each task makes to its run queue's load is weighted according to its
  1171. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  1172. * scaled version of the new time slice allocation that they receive on time
  1173. * slice expiry etc.
  1174. */
  1175. #define WEIGHT_IDLEPRIO 2
  1176. #define WMULT_IDLEPRIO (1 << 31)
  1177. /*
  1178. * Nice levels are multiplicative, with a gentle 10% change for every
  1179. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  1180. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  1181. * that remained on nice 0.
  1182. *
  1183. * The "10% effect" is relative and cumulative: from _any_ nice level,
  1184. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  1185. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  1186. * If a task goes up by ~10% and another task goes down by ~10% then
  1187. * the relative distance between them is ~25%.)
  1188. */
  1189. static const int prio_to_weight[40] = {
  1190. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  1191. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  1192. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  1193. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  1194. /* 0 */ 1024, 820, 655, 526, 423,
  1195. /* 5 */ 335, 272, 215, 172, 137,
  1196. /* 10 */ 110, 87, 70, 56, 45,
  1197. /* 15 */ 36, 29, 23, 18, 15,
  1198. };
  1199. /*
  1200. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  1201. *
  1202. * In cases where the weight does not change often, we can use the
  1203. * precalculated inverse to speed up arithmetics by turning divisions
  1204. * into multiplications:
  1205. */
  1206. static const u32 prio_to_wmult[40] = {
  1207. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  1208. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  1209. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  1210. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  1211. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  1212. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  1213. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  1214. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  1215. };
  1216. static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
  1217. /*
  1218. * runqueue iterator, to support SMP load-balancing between different
  1219. * scheduling classes, without having to expose their internal data
  1220. * structures to the load-balancing proper:
  1221. */
  1222. struct rq_iterator {
  1223. void *arg;
  1224. struct task_struct *(*start)(void *);
  1225. struct task_struct *(*next)(void *);
  1226. };
  1227. #ifdef CONFIG_SMP
  1228. static unsigned long
  1229. balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1230. unsigned long max_load_move, struct sched_domain *sd,
  1231. enum cpu_idle_type idle, int *all_pinned,
  1232. int *this_best_prio, struct rq_iterator *iterator);
  1233. static int
  1234. iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1235. struct sched_domain *sd, enum cpu_idle_type idle,
  1236. struct rq_iterator *iterator);
  1237. #endif
  1238. #ifdef CONFIG_CGROUP_CPUACCT
  1239. static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
  1240. #else
  1241. static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
  1242. #endif
  1243. static inline void inc_cpu_load(struct rq *rq, unsigned long load)
  1244. {
  1245. update_load_add(&rq->load, load);
  1246. }
  1247. static inline void dec_cpu_load(struct rq *rq, unsigned long load)
  1248. {
  1249. update_load_sub(&rq->load, load);
  1250. }
  1251. #ifdef CONFIG_SMP
  1252. static unsigned long source_load(int cpu, int type);
  1253. static unsigned long target_load(int cpu, int type);
  1254. static unsigned long cpu_avg_load_per_task(int cpu);
  1255. static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
  1256. #else /* CONFIG_SMP */
  1257. #ifdef CONFIG_FAIR_GROUP_SCHED
  1258. static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
  1259. {
  1260. }
  1261. #endif
  1262. #endif /* CONFIG_SMP */
  1263. #include "sched_stats.h"
  1264. #include "sched_idletask.c"
  1265. #include "sched_fair.c"
  1266. #include "sched_rt.c"
  1267. #ifdef CONFIG_SCHED_DEBUG
  1268. # include "sched_debug.c"
  1269. #endif
  1270. #define sched_class_highest (&rt_sched_class)
  1271. static inline void inc_load(struct rq *rq, const struct task_struct *p)
  1272. {
  1273. update_load_add(&rq->load, p->se.load.weight);
  1274. }
  1275. static inline void dec_load(struct rq *rq, const struct task_struct *p)
  1276. {
  1277. update_load_sub(&rq->load, p->se.load.weight);
  1278. }
  1279. static void inc_nr_running(struct task_struct *p, struct rq *rq)
  1280. {
  1281. rq->nr_running++;
  1282. inc_load(rq, p);
  1283. }
  1284. static void dec_nr_running(struct task_struct *p, struct rq *rq)
  1285. {
  1286. rq->nr_running--;
  1287. dec_load(rq, p);
  1288. }
  1289. static void set_load_weight(struct task_struct *p)
  1290. {
  1291. if (task_has_rt_policy(p)) {
  1292. p->se.load.weight = prio_to_weight[0] * 2;
  1293. p->se.load.inv_weight = prio_to_wmult[0] >> 1;
  1294. return;
  1295. }
  1296. /*
  1297. * SCHED_IDLE tasks get minimal weight:
  1298. */
  1299. if (p->policy == SCHED_IDLE) {
  1300. p->se.load.weight = WEIGHT_IDLEPRIO;
  1301. p->se.load.inv_weight = WMULT_IDLEPRIO;
  1302. return;
  1303. }
  1304. p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
  1305. p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
  1306. }
  1307. static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
  1308. {
  1309. sched_info_queued(p);
  1310. p->sched_class->enqueue_task(rq, p, wakeup);
  1311. p->se.on_rq = 1;
  1312. }
  1313. static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
  1314. {
  1315. p->sched_class->dequeue_task(rq, p, sleep);
  1316. p->se.on_rq = 0;
  1317. }
  1318. /*
  1319. * __normal_prio - return the priority that is based on the static prio
  1320. */
  1321. static inline int __normal_prio(struct task_struct *p)
  1322. {
  1323. return p->static_prio;
  1324. }
  1325. /*
  1326. * Calculate the expected normal priority: i.e. priority
  1327. * without taking RT-inheritance into account. Might be
  1328. * boosted by interactivity modifiers. Changes upon fork,
  1329. * setprio syscalls, and whenever the interactivity
  1330. * estimator recalculates.
  1331. */
  1332. static inline int normal_prio(struct task_struct *p)
  1333. {
  1334. int prio;
  1335. if (task_has_rt_policy(p))
  1336. prio = MAX_RT_PRIO-1 - p->rt_priority;
  1337. else
  1338. prio = __normal_prio(p);
  1339. return prio;
  1340. }
  1341. /*
  1342. * Calculate the current priority, i.e. the priority
  1343. * taken into account by the scheduler. This value might
  1344. * be boosted by RT tasks, or might be boosted by
  1345. * interactivity modifiers. Will be RT if the task got
  1346. * RT-boosted. If not then it returns p->normal_prio.
  1347. */
  1348. static int effective_prio(struct task_struct *p)
  1349. {
  1350. p->normal_prio = normal_prio(p);
  1351. /*
  1352. * If we are RT tasks or we were boosted to RT priority,
  1353. * keep the priority unchanged. Otherwise, update priority
  1354. * to the normal priority:
  1355. */
  1356. if (!rt_prio(p->prio))
  1357. return p->normal_prio;
  1358. return p->prio;
  1359. }
  1360. /*
  1361. * activate_task - move a task to the runqueue.
  1362. */
  1363. static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
  1364. {
  1365. if (task_contributes_to_load(p))
  1366. rq->nr_uninterruptible--;
  1367. enqueue_task(rq, p, wakeup);
  1368. inc_nr_running(p, rq);
  1369. }
  1370. /*
  1371. * deactivate_task - remove a task from the runqueue.
  1372. */
  1373. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
  1374. {
  1375. if (task_contributes_to_load(p))
  1376. rq->nr_uninterruptible++;
  1377. dequeue_task(rq, p, sleep);
  1378. dec_nr_running(p, rq);
  1379. }
  1380. /**
  1381. * task_curr - is this task currently executing on a CPU?
  1382. * @p: the task in question.
  1383. */
  1384. inline int task_curr(const struct task_struct *p)
  1385. {
  1386. return cpu_curr(task_cpu(p)) == p;
  1387. }
  1388. /* Used instead of source_load when we know the type == 0 */
  1389. unsigned long weighted_cpuload(const int cpu)
  1390. {
  1391. return cpu_rq(cpu)->load.weight;
  1392. }
  1393. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  1394. {
  1395. set_task_rq(p, cpu);
  1396. #ifdef CONFIG_SMP
  1397. /*
  1398. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  1399. * successfuly executed on another CPU. We must ensure that updates of
  1400. * per-task data have been completed by this moment.
  1401. */
  1402. smp_wmb();
  1403. task_thread_info(p)->cpu = cpu;
  1404. #endif
  1405. }
  1406. static inline void check_class_changed(struct rq *rq, struct task_struct *p,
  1407. const struct sched_class *prev_class,
  1408. int oldprio, int running)
  1409. {
  1410. if (prev_class != p->sched_class) {
  1411. if (prev_class->switched_from)
  1412. prev_class->switched_from(rq, p, running);
  1413. p->sched_class->switched_to(rq, p, running);
  1414. } else
  1415. p->sched_class->prio_changed(rq, p, oldprio, running);
  1416. }
  1417. #ifdef CONFIG_SMP
  1418. /*
  1419. * Is this task likely cache-hot:
  1420. */
  1421. static int
  1422. task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
  1423. {
  1424. s64 delta;
  1425. /*
  1426. * Buddy candidates are cache hot:
  1427. */
  1428. if (sched_feat(CACHE_HOT_BUDDY) && (&p->se == cfs_rq_of(&p->se)->next))
  1429. return 1;
  1430. if (p->sched_class != &fair_sched_class)
  1431. return 0;
  1432. if (sysctl_sched_migration_cost == -1)
  1433. return 1;
  1434. if (sysctl_sched_migration_cost == 0)
  1435. return 0;
  1436. delta = now - p->se.exec_start;
  1437. return delta < (s64)sysctl_sched_migration_cost;
  1438. }
  1439. void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
  1440. {
  1441. int old_cpu = task_cpu(p);
  1442. struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
  1443. struct cfs_rq *old_cfsrq = task_cfs_rq(p),
  1444. *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
  1445. u64 clock_offset;
  1446. clock_offset = old_rq->clock - new_rq->clock;
  1447. #ifdef CONFIG_SCHEDSTATS
  1448. if (p->se.wait_start)
  1449. p->se.wait_start -= clock_offset;
  1450. if (p->se.sleep_start)
  1451. p->se.sleep_start -= clock_offset;
  1452. if (p->se.block_start)
  1453. p->se.block_start -= clock_offset;
  1454. if (old_cpu != new_cpu) {
  1455. schedstat_inc(p, se.nr_migrations);
  1456. if (task_hot(p, old_rq->clock, NULL))
  1457. schedstat_inc(p, se.nr_forced2_migrations);
  1458. }
  1459. #endif
  1460. p->se.vruntime -= old_cfsrq->min_vruntime -
  1461. new_cfsrq->min_vruntime;
  1462. __set_task_cpu(p, new_cpu);
  1463. }
  1464. struct migration_req {
  1465. struct list_head list;
  1466. struct task_struct *task;
  1467. int dest_cpu;
  1468. struct completion done;
  1469. };
  1470. /*
  1471. * The task's runqueue lock must be held.
  1472. * Returns true if you have to wait for migration thread.
  1473. */
  1474. static int
  1475. migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
  1476. {
  1477. struct rq *rq = task_rq(p);
  1478. /*
  1479. * If the task is not on a runqueue (and not running), then
  1480. * it is sufficient to simply update the task's cpu field.
  1481. */
  1482. if (!p->se.on_rq && !task_running(rq, p)) {
  1483. set_task_cpu(p, dest_cpu);
  1484. return 0;
  1485. }
  1486. init_completion(&req->done);
  1487. req->task = p;
  1488. req->dest_cpu = dest_cpu;
  1489. list_add(&req->list, &rq->migration_queue);
  1490. return 1;
  1491. }
  1492. /*
  1493. * wait_task_inactive - wait for a thread to unschedule.
  1494. *
  1495. * The caller must ensure that the task *will* unschedule sometime soon,
  1496. * else this function might spin for a *long* time. This function can't
  1497. * be called with interrupts off, or it may introduce deadlock with
  1498. * smp_call_function() if an IPI is sent by the same process we are
  1499. * waiting to become inactive.
  1500. */
  1501. void wait_task_inactive(struct task_struct *p)
  1502. {
  1503. unsigned long flags;
  1504. int running, on_rq;
  1505. struct rq *rq;
  1506. for (;;) {
  1507. /*
  1508. * We do the initial early heuristics without holding
  1509. * any task-queue locks at all. We'll only try to get
  1510. * the runqueue lock when things look like they will
  1511. * work out!
  1512. */
  1513. rq = task_rq(p);
  1514. /*
  1515. * If the task is actively running on another CPU
  1516. * still, just relax and busy-wait without holding
  1517. * any locks.
  1518. *
  1519. * NOTE! Since we don't hold any locks, it's not
  1520. * even sure that "rq" stays as the right runqueue!
  1521. * But we don't care, since "task_running()" will
  1522. * return false if the runqueue has changed and p
  1523. * is actually now running somewhere else!
  1524. */
  1525. while (task_running(rq, p))
  1526. cpu_relax();
  1527. /*
  1528. * Ok, time to look more closely! We need the rq
  1529. * lock now, to be *sure*. If we're wrong, we'll
  1530. * just go back and repeat.
  1531. */
  1532. rq = task_rq_lock(p, &flags);
  1533. running = task_running(rq, p);
  1534. on_rq = p->se.on_rq;
  1535. task_rq_unlock(rq, &flags);
  1536. /*
  1537. * Was it really running after all now that we
  1538. * checked with the proper locks actually held?
  1539. *
  1540. * Oops. Go back and try again..
  1541. */
  1542. if (unlikely(running)) {
  1543. cpu_relax();
  1544. continue;
  1545. }
  1546. /*
  1547. * It's not enough that it's not actively running,
  1548. * it must be off the runqueue _entirely_, and not
  1549. * preempted!
  1550. *
  1551. * So if it wa still runnable (but just not actively
  1552. * running right now), it's preempted, and we should
  1553. * yield - it could be a while.
  1554. */
  1555. if (unlikely(on_rq)) {
  1556. schedule_timeout_uninterruptible(1);
  1557. continue;
  1558. }
  1559. /*
  1560. * Ahh, all good. It wasn't running, and it wasn't
  1561. * runnable, which means that it will never become
  1562. * running in the future either. We're all done!
  1563. */
  1564. break;
  1565. }
  1566. }
  1567. /***
  1568. * kick_process - kick a running thread to enter/exit the kernel
  1569. * @p: the to-be-kicked thread
  1570. *
  1571. * Cause a process which is running on another CPU to enter
  1572. * kernel-mode, without any delay. (to get signals handled.)
  1573. *
  1574. * NOTE: this function doesnt have to take the runqueue lock,
  1575. * because all it wants to ensure is that the remote task enters
  1576. * the kernel. If the IPI races and the task has been migrated
  1577. * to another CPU then no harm is done and the purpose has been
  1578. * achieved as well.
  1579. */
  1580. void kick_process(struct task_struct *p)
  1581. {
  1582. int cpu;
  1583. preempt_disable();
  1584. cpu = task_cpu(p);
  1585. if ((cpu != smp_processor_id()) && task_curr(p))
  1586. smp_send_reschedule(cpu);
  1587. preempt_enable();
  1588. }
  1589. /*
  1590. * Return a low guess at the load of a migration-source cpu weighted
  1591. * according to the scheduling class and "nice" value.
  1592. *
  1593. * We want to under-estimate the load of migration sources, to
  1594. * balance conservatively.
  1595. */
  1596. static unsigned long source_load(int cpu, int type)
  1597. {
  1598. struct rq *rq = cpu_rq(cpu);
  1599. unsigned long total = weighted_cpuload(cpu);
  1600. if (type == 0)
  1601. return total;
  1602. return min(rq->cpu_load[type-1], total);
  1603. }
  1604. /*
  1605. * Return a high guess at the load of a migration-target cpu weighted
  1606. * according to the scheduling class and "nice" value.
  1607. */
  1608. static unsigned long target_load(int cpu, int type)
  1609. {
  1610. struct rq *rq = cpu_rq(cpu);
  1611. unsigned long total = weighted_cpuload(cpu);
  1612. if (type == 0)
  1613. return total;
  1614. return max(rq->cpu_load[type-1], total);
  1615. }
  1616. /*
  1617. * Return the average load per task on the cpu's run queue
  1618. */
  1619. static unsigned long cpu_avg_load_per_task(int cpu)
  1620. {
  1621. struct rq *rq = cpu_rq(cpu);
  1622. unsigned long total = weighted_cpuload(cpu);
  1623. unsigned long n = rq->nr_running;
  1624. return n ? total / n : SCHED_LOAD_SCALE;
  1625. }
  1626. /*
  1627. * find_idlest_group finds and returns the least busy CPU group within the
  1628. * domain.
  1629. */
  1630. static struct sched_group *
  1631. find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
  1632. {
  1633. struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
  1634. unsigned long min_load = ULONG_MAX, this_load = 0;
  1635. int load_idx = sd->forkexec_idx;
  1636. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  1637. do {
  1638. unsigned long load, avg_load;
  1639. int local_group;
  1640. int i;
  1641. /* Skip over this group if it has no CPUs allowed */
  1642. if (!cpus_intersects(group->cpumask, p->cpus_allowed))
  1643. continue;
  1644. local_group = cpu_isset(this_cpu, group->cpumask);
  1645. /* Tally up the load of all CPUs in the group */
  1646. avg_load = 0;
  1647. for_each_cpu_mask(i, group->cpumask) {
  1648. /* Bias balancing toward cpus of our domain */
  1649. if (local_group)
  1650. load = source_load(i, load_idx);
  1651. else
  1652. load = target_load(i, load_idx);
  1653. avg_load += load;
  1654. }
  1655. /* Adjust by relative CPU power of the group */
  1656. avg_load = sg_div_cpu_power(group,
  1657. avg_load * SCHED_LOAD_SCALE);
  1658. if (local_group) {
  1659. this_load = avg_load;
  1660. this = group;
  1661. } else if (avg_load < min_load) {
  1662. min_load = avg_load;
  1663. idlest = group;
  1664. }
  1665. } while (group = group->next, group != sd->groups);
  1666. if (!idlest || 100*this_load < imbalance*min_load)
  1667. return NULL;
  1668. return idlest;
  1669. }
  1670. /*
  1671. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  1672. */
  1673. static int
  1674. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu,
  1675. cpumask_t *tmp)
  1676. {
  1677. unsigned long load, min_load = ULONG_MAX;
  1678. int idlest = -1;
  1679. int i;
  1680. /* Traverse only the allowed CPUs */
  1681. cpus_and(*tmp, group->cpumask, p->cpus_allowed);
  1682. for_each_cpu_mask(i, *tmp) {
  1683. load = weighted_cpuload(i);
  1684. if (load < min_load || (load == min_load && i == this_cpu)) {
  1685. min_load = load;
  1686. idlest = i;
  1687. }
  1688. }
  1689. return idlest;
  1690. }
  1691. /*
  1692. * sched_balance_self: balance the current task (running on cpu) in domains
  1693. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  1694. * SD_BALANCE_EXEC.
  1695. *
  1696. * Balance, ie. select the least loaded group.
  1697. *
  1698. * Returns the target CPU number, or the same CPU if no balancing is needed.
  1699. *
  1700. * preempt must be disabled.
  1701. */
  1702. static int sched_balance_self(int cpu, int flag)
  1703. {
  1704. struct task_struct *t = current;
  1705. struct sched_domain *tmp, *sd = NULL;
  1706. for_each_domain(cpu, tmp) {
  1707. /*
  1708. * If power savings logic is enabled for a domain, stop there.
  1709. */
  1710. if (tmp->flags & SD_POWERSAVINGS_BALANCE)
  1711. break;
  1712. if (tmp->flags & flag)
  1713. sd = tmp;
  1714. }
  1715. while (sd) {
  1716. cpumask_t span, tmpmask;
  1717. struct sched_group *group;
  1718. int new_cpu, weight;
  1719. if (!(sd->flags & flag)) {
  1720. sd = sd->child;
  1721. continue;
  1722. }
  1723. span = sd->span;
  1724. group = find_idlest_group(sd, t, cpu);
  1725. if (!group) {
  1726. sd = sd->child;
  1727. continue;
  1728. }
  1729. new_cpu = find_idlest_cpu(group, t, cpu, &tmpmask);
  1730. if (new_cpu == -1 || new_cpu == cpu) {
  1731. /* Now try balancing at a lower domain level of cpu */
  1732. sd = sd->child;
  1733. continue;
  1734. }
  1735. /* Now try balancing at a lower domain level of new_cpu */
  1736. cpu = new_cpu;
  1737. sd = NULL;
  1738. weight = cpus_weight(span);
  1739. for_each_domain(cpu, tmp) {
  1740. if (weight <= cpus_weight(tmp->span))
  1741. break;
  1742. if (tmp->flags & flag)
  1743. sd = tmp;
  1744. }
  1745. /* while loop will break here if sd == NULL */
  1746. }
  1747. return cpu;
  1748. }
  1749. #endif /* CONFIG_SMP */
  1750. /***
  1751. * try_to_wake_up - wake up a thread
  1752. * @p: the to-be-woken-up thread
  1753. * @state: the mask of task states that can be woken
  1754. * @sync: do a synchronous wakeup?
  1755. *
  1756. * Put it on the run-queue if it's not already there. The "current"
  1757. * thread is always on the run-queue (except when the actual
  1758. * re-schedule is in progress), and as such you're allowed to do
  1759. * the simpler "current->state = TASK_RUNNING" to mark yourself
  1760. * runnable without the overhead of this.
  1761. *
  1762. * returns failure only if the task is already active.
  1763. */
  1764. static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
  1765. {
  1766. int cpu, orig_cpu, this_cpu, success = 0;
  1767. unsigned long flags;
  1768. long old_state;
  1769. struct rq *rq;
  1770. if (!sched_feat(SYNC_WAKEUPS))
  1771. sync = 0;
  1772. smp_wmb();
  1773. rq = task_rq_lock(p, &flags);
  1774. old_state = p->state;
  1775. if (!(old_state & state))
  1776. goto out;
  1777. if (p->se.on_rq)
  1778. goto out_running;
  1779. cpu = task_cpu(p);
  1780. orig_cpu = cpu;
  1781. this_cpu = smp_processor_id();
  1782. #ifdef CONFIG_SMP
  1783. if (unlikely(task_running(rq, p)))
  1784. goto out_activate;
  1785. cpu = p->sched_class->select_task_rq(p, sync);
  1786. if (cpu != orig_cpu) {
  1787. set_task_cpu(p, cpu);
  1788. task_rq_unlock(rq, &flags);
  1789. /* might preempt at this point */
  1790. rq = task_rq_lock(p, &flags);
  1791. old_state = p->state;
  1792. if (!(old_state & state))
  1793. goto out;
  1794. if (p->se.on_rq)
  1795. goto out_running;
  1796. this_cpu = smp_processor_id();
  1797. cpu = task_cpu(p);
  1798. }
  1799. #ifdef CONFIG_SCHEDSTATS
  1800. schedstat_inc(rq, ttwu_count);
  1801. if (cpu == this_cpu)
  1802. schedstat_inc(rq, ttwu_local);
  1803. else {
  1804. struct sched_domain *sd;
  1805. for_each_domain(this_cpu, sd) {
  1806. if (cpu_isset(cpu, sd->span)) {
  1807. schedstat_inc(sd, ttwu_wake_remote);
  1808. break;
  1809. }
  1810. }
  1811. }
  1812. #endif
  1813. out_activate:
  1814. #endif /* CONFIG_SMP */
  1815. schedstat_inc(p, se.nr_wakeups);
  1816. if (sync)
  1817. schedstat_inc(p, se.nr_wakeups_sync);
  1818. if (orig_cpu != cpu)
  1819. schedstat_inc(p, se.nr_wakeups_migrate);
  1820. if (cpu == this_cpu)
  1821. schedstat_inc(p, se.nr_wakeups_local);
  1822. else
  1823. schedstat_inc(p, se.nr_wakeups_remote);
  1824. update_rq_clock(rq);
  1825. activate_task(rq, p, 1);
  1826. success = 1;
  1827. out_running:
  1828. trace_mark(kernel_sched_wakeup,
  1829. "pid %d state %ld ## rq %p task %p rq->curr %p",
  1830. p->pid, p->state, rq, p, rq->curr);
  1831. check_preempt_curr(rq, p);
  1832. p->state = TASK_RUNNING;
  1833. #ifdef CONFIG_SMP
  1834. if (p->sched_class->task_wake_up)
  1835. p->sched_class->task_wake_up(rq, p);
  1836. #endif
  1837. out:
  1838. task_rq_unlock(rq, &flags);
  1839. return success;
  1840. }
  1841. int wake_up_process(struct task_struct *p)
  1842. {
  1843. return try_to_wake_up(p, TASK_ALL, 0);
  1844. }
  1845. EXPORT_SYMBOL(wake_up_process);
  1846. int wake_up_state(struct task_struct *p, unsigned int state)
  1847. {
  1848. return try_to_wake_up(p, state, 0);
  1849. }
  1850. /*
  1851. * Perform scheduler related setup for a newly forked process p.
  1852. * p is forked by current.
  1853. *
  1854. * __sched_fork() is basic setup used by init_idle() too:
  1855. */
  1856. static void __sched_fork(struct task_struct *p)
  1857. {
  1858. p->se.exec_start = 0;
  1859. p->se.sum_exec_runtime = 0;
  1860. p->se.prev_sum_exec_runtime = 0;
  1861. p->se.last_wakeup = 0;
  1862. p->se.avg_overlap = 0;
  1863. #ifdef CONFIG_SCHEDSTATS
  1864. p->se.wait_start = 0;
  1865. p->se.sum_sleep_runtime = 0;
  1866. p->se.sleep_start = 0;
  1867. p->se.block_start = 0;
  1868. p->se.sleep_max = 0;
  1869. p->se.block_max = 0;
  1870. p->se.exec_max = 0;
  1871. p->se.slice_max = 0;
  1872. p->se.wait_max = 0;
  1873. #endif
  1874. INIT_LIST_HEAD(&p->rt.run_list);
  1875. p->se.on_rq = 0;
  1876. INIT_LIST_HEAD(&p->se.group_node);
  1877. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1878. INIT_HLIST_HEAD(&p->preempt_notifiers);
  1879. #endif
  1880. /*
  1881. * We mark the process as running here, but have not actually
  1882. * inserted it onto the runqueue yet. This guarantees that
  1883. * nobody will actually run it, and a signal or other external
  1884. * event cannot wake it up and insert it on the runqueue either.
  1885. */
  1886. p->state = TASK_RUNNING;
  1887. }
  1888. /*
  1889. * fork()/clone()-time setup:
  1890. */
  1891. void sched_fork(struct task_struct *p, int clone_flags)
  1892. {
  1893. int cpu = get_cpu();
  1894. __sched_fork(p);
  1895. #ifdef CONFIG_SMP
  1896. cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
  1897. #endif
  1898. set_task_cpu(p, cpu);
  1899. /*
  1900. * Make sure we do not leak PI boosting priority to the child:
  1901. */
  1902. p->prio = current->normal_prio;
  1903. if (!rt_prio(p->prio))
  1904. p->sched_class = &fair_sched_class;
  1905. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  1906. if (likely(sched_info_on()))
  1907. memset(&p->sched_info, 0, sizeof(p->sched_info));
  1908. #endif
  1909. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  1910. p->oncpu = 0;
  1911. #endif
  1912. #ifdef CONFIG_PREEMPT
  1913. /* Want to start with kernel preemption disabled. */
  1914. task_thread_info(p)->preempt_count = 1;
  1915. #endif
  1916. put_cpu();
  1917. }
  1918. /*
  1919. * wake_up_new_task - wake up a newly created task for the first time.
  1920. *
  1921. * This function will do some initial scheduler statistics housekeeping
  1922. * that must be done for every newly created context, then puts the task
  1923. * on the runqueue and wakes it.
  1924. */
  1925. void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
  1926. {
  1927. unsigned long flags;
  1928. struct rq *rq;
  1929. rq = task_rq_lock(p, &flags);
  1930. BUG_ON(p->state != TASK_RUNNING);
  1931. update_rq_clock(rq);
  1932. p->prio = effective_prio(p);
  1933. if (!p->sched_class->task_new || !current->se.on_rq) {
  1934. activate_task(rq, p, 0);
  1935. } else {
  1936. /*
  1937. * Let the scheduling class do new task startup
  1938. * management (if any):
  1939. */
  1940. p->sched_class->task_new(rq, p);
  1941. inc_nr_running(p, rq);
  1942. }
  1943. trace_mark(kernel_sched_wakeup_new,
  1944. "pid %d state %ld ## rq %p task %p rq->curr %p",
  1945. p->pid, p->state, rq, p, rq->curr);
  1946. check_preempt_curr(rq, p);
  1947. #ifdef CONFIG_SMP
  1948. if (p->sched_class->task_wake_up)
  1949. p->sched_class->task_wake_up(rq, p);
  1950. #endif
  1951. task_rq_unlock(rq, &flags);
  1952. }
  1953. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1954. /**
  1955. * preempt_notifier_register - tell me when current is being being preempted & rescheduled
  1956. * @notifier: notifier struct to register
  1957. */
  1958. void preempt_notifier_register(struct preempt_notifier *notifier)
  1959. {
  1960. hlist_add_head(&notifier->link, &current->preempt_notifiers);
  1961. }
  1962. EXPORT_SYMBOL_GPL(preempt_notifier_register);
  1963. /**
  1964. * preempt_notifier_unregister - no longer interested in preemption notifications
  1965. * @notifier: notifier struct to unregister
  1966. *
  1967. * This is safe to call from within a preemption notifier.
  1968. */
  1969. void preempt_notifier_unregister(struct preempt_notifier *notifier)
  1970. {
  1971. hlist_del(&notifier->link);
  1972. }
  1973. EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
  1974. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1975. {
  1976. struct preempt_notifier *notifier;
  1977. struct hlist_node *node;
  1978. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  1979. notifier->ops->sched_in(notifier, raw_smp_processor_id());
  1980. }
  1981. static void
  1982. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1983. struct task_struct *next)
  1984. {
  1985. struct preempt_notifier *notifier;
  1986. struct hlist_node *node;
  1987. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  1988. notifier->ops->sched_out(notifier, next);
  1989. }
  1990. #else
  1991. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1992. {
  1993. }
  1994. static void
  1995. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1996. struct task_struct *next)
  1997. {
  1998. }
  1999. #endif
  2000. /**
  2001. * prepare_task_switch - prepare to switch tasks
  2002. * @rq: the runqueue preparing to switch
  2003. * @prev: the current task that is being switched out
  2004. * @next: the task we are going to switch to.
  2005. *
  2006. * This is called with the rq lock held and interrupts off. It must
  2007. * be paired with a subsequent finish_task_switch after the context
  2008. * switch.
  2009. *
  2010. * prepare_task_switch sets up locking and calls architecture specific
  2011. * hooks.
  2012. */
  2013. static inline void
  2014. prepare_task_switch(struct rq *rq, struct task_struct *prev,
  2015. struct task_struct *next)
  2016. {
  2017. fire_sched_out_preempt_notifiers(prev, next);
  2018. prepare_lock_switch(rq, next);
  2019. prepare_arch_switch(next);
  2020. }
  2021. /**
  2022. * finish_task_switch - clean up after a task-switch
  2023. * @rq: runqueue associated with task-switch
  2024. * @prev: the thread we just switched away from.
  2025. *
  2026. * finish_task_switch must be called after the context switch, paired
  2027. * with a prepare_task_switch call before the context switch.
  2028. * finish_task_switch will reconcile locking set up by prepare_task_switch,
  2029. * and do any other architecture-specific cleanup actions.
  2030. *
  2031. * Note that we may have delayed dropping an mm in context_switch(). If
  2032. * so, we finish that here outside of the runqueue lock. (Doing it
  2033. * with the lock held can cause deadlocks; see schedule() for
  2034. * details.)
  2035. */
  2036. static void finish_task_switch(struct rq *rq, struct task_struct *prev)
  2037. __releases(rq->lock)
  2038. {
  2039. struct mm_struct *mm = rq->prev_mm;
  2040. long prev_state;
  2041. rq->prev_mm = NULL;
  2042. /*
  2043. * A task struct has one reference for the use as "current".
  2044. * If a task dies, then it sets TASK_DEAD in tsk->state and calls
  2045. * schedule one last time. The schedule call will never return, and
  2046. * the scheduled task must drop that reference.
  2047. * The test for TASK_DEAD must occur while the runqueue locks are
  2048. * still held, otherwise prev could be scheduled on another cpu, die
  2049. * there before we look at prev->state, and then the reference would
  2050. * be dropped twice.
  2051. * Manfred Spraul <manfred@colorfullife.com>
  2052. */
  2053. prev_state = prev->state;
  2054. finish_arch_switch(prev);
  2055. finish_lock_switch(rq, prev);
  2056. #ifdef CONFIG_SMP
  2057. if (current->sched_class->post_schedule)
  2058. current->sched_class->post_schedule(rq);
  2059. #endif
  2060. fire_sched_in_preempt_notifiers(current);
  2061. if (mm)
  2062. mmdrop(mm);
  2063. if (unlikely(prev_state == TASK_DEAD)) {
  2064. /*
  2065. * Remove function-return probe instances associated with this
  2066. * task and put them back on the free list.
  2067. */
  2068. kprobe_flush_task(prev);
  2069. put_task_struct(prev);
  2070. }
  2071. }
  2072. /**
  2073. * schedule_tail - first thing a freshly forked thread must call.
  2074. * @prev: the thread we just switched away from.
  2075. */
  2076. asmlinkage void schedule_tail(struct task_struct *prev)
  2077. __releases(rq->lock)
  2078. {
  2079. struct rq *rq = this_rq();
  2080. finish_task_switch(rq, prev);
  2081. #ifdef __ARCH_WANT_UNLOCKED_CTXSW
  2082. /* In this case, finish_task_switch does not reenable preemption */
  2083. preempt_enable();
  2084. #endif
  2085. if (current->set_child_tid)
  2086. put_user(task_pid_vnr(current), current->set_child_tid);
  2087. }
  2088. /*
  2089. * context_switch - switch to the new MM and the new
  2090. * thread's register state.
  2091. */
  2092. static inline void
  2093. context_switch(struct rq *rq, struct task_struct *prev,
  2094. struct task_struct *next)
  2095. {
  2096. struct mm_struct *mm, *oldmm;
  2097. prepare_task_switch(rq, prev, next);
  2098. trace_mark(kernel_sched_schedule,
  2099. "prev_pid %d next_pid %d prev_state %ld "
  2100. "## rq %p prev %p next %p",
  2101. prev->pid, next->pid, prev->state,
  2102. rq, prev, next);
  2103. mm = next->mm;
  2104. oldmm = prev->active_mm;
  2105. /*
  2106. * For paravirt, this is coupled with an exit in switch_to to
  2107. * combine the page table reload and the switch backend into
  2108. * one hypercall.
  2109. */
  2110. arch_enter_lazy_cpu_mode();
  2111. if (unlikely(!mm)) {
  2112. next->active_mm = oldmm;
  2113. atomic_inc(&oldmm->mm_count);
  2114. enter_lazy_tlb(oldmm, next);
  2115. } else
  2116. switch_mm(oldmm, mm, next);
  2117. if (unlikely(!prev->mm)) {
  2118. prev->active_mm = NULL;
  2119. rq->prev_mm = oldmm;
  2120. }
  2121. /*
  2122. * Since the runqueue lock will be released by the next
  2123. * task (which is an invalid locking op but in the case
  2124. * of the scheduler it's an obvious special-case), so we
  2125. * do an early lockdep release here:
  2126. */
  2127. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  2128. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  2129. #endif
  2130. /* Here we just switch the register state and the stack. */
  2131. switch_to(prev, next, prev);
  2132. barrier();
  2133. /*
  2134. * this_rq must be evaluated again because prev may have moved
  2135. * CPUs since it called schedule(), thus the 'rq' on its stack
  2136. * frame will be invalid.
  2137. */
  2138. finish_task_switch(this_rq(), prev);
  2139. }
  2140. /*
  2141. * nr_running, nr_uninterruptible and nr_context_switches:
  2142. *
  2143. * externally visible scheduler statistics: current number of runnable
  2144. * threads, current number of uninterruptible-sleeping threads, total
  2145. * number of context switches performed since bootup.
  2146. */
  2147. unsigned long nr_running(void)
  2148. {
  2149. unsigned long i, sum = 0;
  2150. for_each_online_cpu(i)
  2151. sum += cpu_rq(i)->nr_running;
  2152. return sum;
  2153. }
  2154. unsigned long nr_uninterruptible(void)
  2155. {
  2156. unsigned long i, sum = 0;
  2157. for_each_possible_cpu(i)
  2158. sum += cpu_rq(i)->nr_uninterruptible;
  2159. /*
  2160. * Since we read the counters lockless, it might be slightly
  2161. * inaccurate. Do not allow it to go below zero though:
  2162. */
  2163. if (unlikely((long)sum < 0))
  2164. sum = 0;
  2165. return sum;
  2166. }
  2167. unsigned long long nr_context_switches(void)
  2168. {
  2169. int i;
  2170. unsigned long long sum = 0;
  2171. for_each_possible_cpu(i)
  2172. sum += cpu_rq(i)->nr_switches;
  2173. return sum;
  2174. }
  2175. unsigned long nr_iowait(void)
  2176. {
  2177. unsigned long i, sum = 0;
  2178. for_each_possible_cpu(i)
  2179. sum += atomic_read(&cpu_rq(i)->nr_iowait);
  2180. return sum;
  2181. }
  2182. unsigned long nr_active(void)
  2183. {
  2184. unsigned long i, running = 0, uninterruptible = 0;
  2185. for_each_online_cpu(i) {
  2186. running += cpu_rq(i)->nr_running;
  2187. uninterruptible += cpu_rq(i)->nr_uninterruptible;
  2188. }
  2189. if (unlikely((long)uninterruptible < 0))
  2190. uninterruptible = 0;
  2191. return running + uninterruptible;
  2192. }
  2193. /*
  2194. * Update rq->cpu_load[] statistics. This function is usually called every
  2195. * scheduler tick (TICK_NSEC).
  2196. */
  2197. static void update_cpu_load(struct rq *this_rq)
  2198. {
  2199. unsigned long this_load = this_rq->load.weight;
  2200. int i, scale;
  2201. this_rq->nr_load_updates++;
  2202. /* Update our load: */
  2203. for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
  2204. unsigned long old_load, new_load;
  2205. /* scale is effectively 1 << i now, and >> i divides by scale */
  2206. old_load = this_rq->cpu_load[i];
  2207. new_load = this_load;
  2208. /*
  2209. * Round up the averaging division if load is increasing. This
  2210. * prevents us from getting stuck on 9 if the load is 10, for
  2211. * example.
  2212. */
  2213. if (new_load > old_load)
  2214. new_load += scale-1;
  2215. this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
  2216. }
  2217. }
  2218. #ifdef CONFIG_SMP
  2219. /*
  2220. * double_rq_lock - safely lock two runqueues
  2221. *
  2222. * Note this does not disable interrupts like task_rq_lock,
  2223. * you need to do so manually before calling.
  2224. */
  2225. static void double_rq_lock(struct rq *rq1, struct rq *rq2)
  2226. __acquires(rq1->lock)
  2227. __acquires(rq2->lock)
  2228. {
  2229. BUG_ON(!irqs_disabled());
  2230. if (rq1 == rq2) {
  2231. spin_lock(&rq1->lock);
  2232. __acquire(rq2->lock); /* Fake it out ;) */
  2233. } else {
  2234. if (rq1 < rq2) {
  2235. spin_lock(&rq1->lock);
  2236. spin_lock(&rq2->lock);
  2237. } else {
  2238. spin_lock(&rq2->lock);
  2239. spin_lock(&rq1->lock);
  2240. }
  2241. }
  2242. update_rq_clock(rq1);
  2243. update_rq_clock(rq2);
  2244. }
  2245. /*
  2246. * double_rq_unlock - safely unlock two runqueues
  2247. *
  2248. * Note this does not restore interrupts like task_rq_unlock,
  2249. * you need to do so manually after calling.
  2250. */
  2251. static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  2252. __releases(rq1->lock)
  2253. __releases(rq2->lock)
  2254. {
  2255. spin_unlock(&rq1->lock);
  2256. if (rq1 != rq2)
  2257. spin_unlock(&rq2->lock);
  2258. else
  2259. __release(rq2->lock);
  2260. }
  2261. /*
  2262. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  2263. */
  2264. static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  2265. __releases(this_rq->lock)
  2266. __acquires(busiest->lock)
  2267. __acquires(this_rq->lock)
  2268. {
  2269. int ret = 0;
  2270. if (unlikely(!irqs_disabled())) {
  2271. /* printk() doesn't work good under rq->lock */
  2272. spin_unlock(&this_rq->lock);
  2273. BUG_ON(1);
  2274. }
  2275. if (unlikely(!spin_trylock(&busiest->lock))) {
  2276. if (busiest < this_rq) {
  2277. spin_unlock(&this_rq->lock);
  2278. spin_lock(&busiest->lock);
  2279. spin_lock(&this_rq->lock);
  2280. ret = 1;
  2281. } else
  2282. spin_lock(&busiest->lock);
  2283. }
  2284. return ret;
  2285. }
  2286. /*
  2287. * If dest_cpu is allowed for this process, migrate the task to it.
  2288. * This is accomplished by forcing the cpu_allowed mask to only
  2289. * allow dest_cpu, which will force the cpu onto dest_cpu. Then
  2290. * the cpu_allowed mask is restored.
  2291. */
  2292. static void sched_migrate_task(struct task_struct *p, int dest_cpu)
  2293. {
  2294. struct migration_req req;
  2295. unsigned long flags;
  2296. struct rq *rq;
  2297. rq = task_rq_lock(p, &flags);
  2298. if (!cpu_isset(dest_cpu, p->cpus_allowed)
  2299. || unlikely(cpu_is_offline(dest_cpu)))
  2300. goto out;
  2301. /* force the process onto the specified CPU */
  2302. if (migrate_task(p, dest_cpu, &req)) {
  2303. /* Need to wait for migration thread (might exit: take ref). */
  2304. struct task_struct *mt = rq->migration_thread;
  2305. get_task_struct(mt);
  2306. task_rq_unlock(rq, &flags);
  2307. wake_up_process(mt);
  2308. put_task_struct(mt);
  2309. wait_for_completion(&req.done);
  2310. return;
  2311. }
  2312. out:
  2313. task_rq_unlock(rq, &flags);
  2314. }
  2315. /*
  2316. * sched_exec - execve() is a valuable balancing opportunity, because at
  2317. * this point the task has the smallest effective memory and cache footprint.
  2318. */
  2319. void sched_exec(void)
  2320. {
  2321. int new_cpu, this_cpu = get_cpu();
  2322. new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
  2323. put_cpu();
  2324. if (new_cpu != this_cpu)
  2325. sched_migrate_task(current, new_cpu);
  2326. }
  2327. /*
  2328. * pull_task - move a task from a remote runqueue to the local runqueue.
  2329. * Both runqueues must be locked.
  2330. */
  2331. static void pull_task(struct rq *src_rq, struct task_struct *p,
  2332. struct rq *this_rq, int this_cpu)
  2333. {
  2334. deactivate_task(src_rq, p, 0);
  2335. set_task_cpu(p, this_cpu);
  2336. activate_task(this_rq, p, 0);
  2337. /*
  2338. * Note that idle threads have a prio of MAX_PRIO, for this test
  2339. * to be always true for them.
  2340. */
  2341. check_preempt_curr(this_rq, p);
  2342. }
  2343. /*
  2344. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  2345. */
  2346. static
  2347. int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
  2348. struct sched_domain *sd, enum cpu_idle_type idle,
  2349. int *all_pinned)
  2350. {
  2351. /*
  2352. * We do not migrate tasks that are:
  2353. * 1) running (obviously), or
  2354. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  2355. * 3) are cache-hot on their current CPU.
  2356. */
  2357. if (!cpu_isset(this_cpu, p->cpus_allowed)) {
  2358. schedstat_inc(p, se.nr_failed_migrations_affine);
  2359. return 0;
  2360. }
  2361. *all_pinned = 0;
  2362. if (task_running(rq, p)) {
  2363. schedstat_inc(p, se.nr_failed_migrations_running);
  2364. return 0;
  2365. }
  2366. /*
  2367. * Aggressive migration if:
  2368. * 1) task is cache cold, or
  2369. * 2) too many balance attempts have failed.
  2370. */
  2371. if (!task_hot(p, rq->clock, sd) ||
  2372. sd->nr_balance_failed > sd->cache_nice_tries) {
  2373. #ifdef CONFIG_SCHEDSTATS
  2374. if (task_hot(p, rq->clock, sd)) {
  2375. schedstat_inc(sd, lb_hot_gained[idle]);
  2376. schedstat_inc(p, se.nr_forced_migrations);
  2377. }
  2378. #endif
  2379. return 1;
  2380. }
  2381. if (task_hot(p, rq->clock, sd)) {
  2382. schedstat_inc(p, se.nr_failed_migrations_hot);
  2383. return 0;
  2384. }
  2385. return 1;
  2386. }
  2387. static unsigned long
  2388. balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2389. unsigned long max_load_move, struct sched_domain *sd,
  2390. enum cpu_idle_type idle, int *all_pinned,
  2391. int *this_best_prio, struct rq_iterator *iterator)
  2392. {
  2393. int loops = 0, pulled = 0, pinned = 0, skip_for_load;
  2394. struct task_struct *p;
  2395. long rem_load_move = max_load_move;
  2396. if (max_load_move == 0)
  2397. goto out;
  2398. pinned = 1;
  2399. /*
  2400. * Start the load-balancing iterator:
  2401. */
  2402. p = iterator->start(iterator->arg);
  2403. next:
  2404. if (!p || loops++ > sysctl_sched_nr_migrate)
  2405. goto out;
  2406. /*
  2407. * To help distribute high priority tasks across CPUs we don't
  2408. * skip a task if it will be the highest priority task (i.e. smallest
  2409. * prio value) on its new queue regardless of its load weight
  2410. */
  2411. skip_for_load = (p->se.load.weight >> 1) > rem_load_move +
  2412. SCHED_LOAD_SCALE_FUZZ;
  2413. if ((skip_for_load && p->prio >= *this_best_prio) ||
  2414. !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
  2415. p = iterator->next(iterator->arg);
  2416. goto next;
  2417. }
  2418. pull_task(busiest, p, this_rq, this_cpu);
  2419. pulled++;
  2420. rem_load_move -= p->se.load.weight;
  2421. /*
  2422. * We only want to steal up to the prescribed amount of weighted load.
  2423. */
  2424. if (rem_load_move > 0) {
  2425. if (p->prio < *this_best_prio)
  2426. *this_best_prio = p->prio;
  2427. p = iterator->next(iterator->arg);
  2428. goto next;
  2429. }
  2430. out:
  2431. /*
  2432. * Right now, this is one of only two places pull_task() is called,
  2433. * so we can safely collect pull_task() stats here rather than
  2434. * inside pull_task().
  2435. */
  2436. schedstat_add(sd, lb_gained[idle], pulled);
  2437. if (all_pinned)
  2438. *all_pinned = pinned;
  2439. return max_load_move - rem_load_move;
  2440. }
  2441. /*
  2442. * move_tasks tries to move up to max_load_move weighted load from busiest to
  2443. * this_rq, as part of a balancing operation within domain "sd".
  2444. * Returns 1 if successful and 0 otherwise.
  2445. *
  2446. * Called with both runqueues locked.
  2447. */
  2448. static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2449. unsigned long max_load_move,
  2450. struct sched_domain *sd, enum cpu_idle_type idle,
  2451. int *all_pinned)
  2452. {
  2453. const struct sched_class *class = sched_class_highest;
  2454. unsigned long total_load_moved = 0;
  2455. int this_best_prio = this_rq->curr->prio;
  2456. do {
  2457. total_load_moved +=
  2458. class->load_balance(this_rq, this_cpu, busiest,
  2459. max_load_move - total_load_moved,
  2460. sd, idle, all_pinned, &this_best_prio);
  2461. class = class->next;
  2462. } while (class && max_load_move > total_load_moved);
  2463. return total_load_moved > 0;
  2464. }
  2465. static int
  2466. iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2467. struct sched_domain *sd, enum cpu_idle_type idle,
  2468. struct rq_iterator *iterator)
  2469. {
  2470. struct task_struct *p = iterator->start(iterator->arg);
  2471. int pinned = 0;
  2472. while (p) {
  2473. if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
  2474. pull_task(busiest, p, this_rq, this_cpu);
  2475. /*
  2476. * Right now, this is only the second place pull_task()
  2477. * is called, so we can safely collect pull_task()
  2478. * stats here rather than inside pull_task().
  2479. */
  2480. schedstat_inc(sd, lb_gained[idle]);
  2481. return 1;
  2482. }
  2483. p = iterator->next(iterator->arg);
  2484. }
  2485. return 0;
  2486. }
  2487. /*
  2488. * move_one_task tries to move exactly one task from busiest to this_rq, as
  2489. * part of active balancing operations within "domain".
  2490. * Returns 1 if successful and 0 otherwise.
  2491. *
  2492. * Called with both runqueues locked.
  2493. */
  2494. static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2495. struct sched_domain *sd, enum cpu_idle_type idle)
  2496. {
  2497. const struct sched_class *class;
  2498. for (class = sched_class_highest; class; class = class->next)
  2499. if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
  2500. return 1;
  2501. return 0;
  2502. }
  2503. /*
  2504. * find_busiest_group finds and returns the busiest CPU group within the
  2505. * domain. It calculates and returns the amount of weighted load which
  2506. * should be moved to restore balance via the imbalance parameter.
  2507. */
  2508. static struct sched_group *
  2509. find_busiest_group(struct sched_domain *sd, int this_cpu,
  2510. unsigned long *imbalance, enum cpu_idle_type idle,
  2511. int *sd_idle, const cpumask_t *cpus, int *balance)
  2512. {
  2513. struct sched_group *busiest = NULL, *this = NULL, *group = sd->groups;
  2514. unsigned long max_load, avg_load, total_load, this_load, total_pwr;
  2515. unsigned long max_pull;
  2516. unsigned long busiest_load_per_task, busiest_nr_running;
  2517. unsigned long this_load_per_task, this_nr_running;
  2518. int load_idx, group_imb = 0;
  2519. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2520. int power_savings_balance = 1;
  2521. unsigned long leader_nr_running = 0, min_load_per_task = 0;
  2522. unsigned long min_nr_running = ULONG_MAX;
  2523. struct sched_group *group_min = NULL, *group_leader = NULL;
  2524. #endif
  2525. max_load = this_load = total_load = total_pwr = 0;
  2526. busiest_load_per_task = busiest_nr_running = 0;
  2527. this_load_per_task = this_nr_running = 0;
  2528. if (idle == CPU_NOT_IDLE)
  2529. load_idx = sd->busy_idx;
  2530. else if (idle == CPU_NEWLY_IDLE)
  2531. load_idx = sd->newidle_idx;
  2532. else
  2533. load_idx = sd->idle_idx;
  2534. do {
  2535. unsigned long load, group_capacity, max_cpu_load, min_cpu_load;
  2536. int local_group;
  2537. int i;
  2538. int __group_imb = 0;
  2539. unsigned int balance_cpu = -1, first_idle_cpu = 0;
  2540. unsigned long sum_nr_running, sum_weighted_load;
  2541. local_group = cpu_isset(this_cpu, group->cpumask);
  2542. if (local_group)
  2543. balance_cpu = first_cpu(group->cpumask);
  2544. /* Tally up the load of all CPUs in the group */
  2545. sum_weighted_load = sum_nr_running = avg_load = 0;
  2546. max_cpu_load = 0;
  2547. min_cpu_load = ~0UL;
  2548. for_each_cpu_mask(i, group->cpumask) {
  2549. struct rq *rq;
  2550. if (!cpu_isset(i, *cpus))
  2551. continue;
  2552. rq = cpu_rq(i);
  2553. if (*sd_idle && rq->nr_running)
  2554. *sd_idle = 0;
  2555. /* Bias balancing toward cpus of our domain */
  2556. if (local_group) {
  2557. if (idle_cpu(i) && !first_idle_cpu) {
  2558. first_idle_cpu = 1;
  2559. balance_cpu = i;
  2560. }
  2561. load = target_load(i, load_idx);
  2562. } else {
  2563. load = source_load(i, load_idx);
  2564. if (load > max_cpu_load)
  2565. max_cpu_load = load;
  2566. if (min_cpu_load > load)
  2567. min_cpu_load = load;
  2568. }
  2569. avg_load += load;
  2570. sum_nr_running += rq->nr_running;
  2571. sum_weighted_load += weighted_cpuload(i);
  2572. }
  2573. /*
  2574. * First idle cpu or the first cpu(busiest) in this sched group
  2575. * is eligible for doing load balancing at this and above
  2576. * domains. In the newly idle case, we will allow all the cpu's
  2577. * to do the newly idle load balance.
  2578. */
  2579. if (idle != CPU_NEWLY_IDLE && local_group &&
  2580. balance_cpu != this_cpu && balance) {
  2581. *balance = 0;
  2582. goto ret;
  2583. }
  2584. total_load += avg_load;
  2585. total_pwr += group->__cpu_power;
  2586. /* Adjust by relative CPU power of the group */
  2587. avg_load = sg_div_cpu_power(group,
  2588. avg_load * SCHED_LOAD_SCALE);
  2589. if ((max_cpu_load - min_cpu_load) > SCHED_LOAD_SCALE)
  2590. __group_imb = 1;
  2591. group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
  2592. if (local_group) {
  2593. this_load = avg_load;
  2594. this = group;
  2595. this_nr_running = sum_nr_running;
  2596. this_load_per_task = sum_weighted_load;
  2597. } else if (avg_load > max_load &&
  2598. (sum_nr_running > group_capacity || __group_imb)) {
  2599. max_load = avg_load;
  2600. busiest = group;
  2601. busiest_nr_running = sum_nr_running;
  2602. busiest_load_per_task = sum_weighted_load;
  2603. group_imb = __group_imb;
  2604. }
  2605. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2606. /*
  2607. * Busy processors will not participate in power savings
  2608. * balance.
  2609. */
  2610. if (idle == CPU_NOT_IDLE ||
  2611. !(sd->flags & SD_POWERSAVINGS_BALANCE))
  2612. goto group_next;
  2613. /*
  2614. * If the local group is idle or completely loaded
  2615. * no need to do power savings balance at this domain
  2616. */
  2617. if (local_group && (this_nr_running >= group_capacity ||
  2618. !this_nr_running))
  2619. power_savings_balance = 0;
  2620. /*
  2621. * If a group is already running at full capacity or idle,
  2622. * don't include that group in power savings calculations
  2623. */
  2624. if (!power_savings_balance || sum_nr_running >= group_capacity
  2625. || !sum_nr_running)
  2626. goto group_next;
  2627. /*
  2628. * Calculate the group which has the least non-idle load.
  2629. * This is the group from where we need to pick up the load
  2630. * for saving power
  2631. */
  2632. if ((sum_nr_running < min_nr_running) ||
  2633. (sum_nr_running == min_nr_running &&
  2634. first_cpu(group->cpumask) <
  2635. first_cpu(group_min->cpumask))) {
  2636. group_min = group;
  2637. min_nr_running = sum_nr_running;
  2638. min_load_per_task = sum_weighted_load /
  2639. sum_nr_running;
  2640. }
  2641. /*
  2642. * Calculate the group which is almost near its
  2643. * capacity but still has some space to pick up some load
  2644. * from other group and save more power
  2645. */
  2646. if (sum_nr_running <= group_capacity - 1) {
  2647. if (sum_nr_running > leader_nr_running ||
  2648. (sum_nr_running == leader_nr_running &&
  2649. first_cpu(group->cpumask) >
  2650. first_cpu(group_leader->cpumask))) {
  2651. group_leader = group;
  2652. leader_nr_running = sum_nr_running;
  2653. }
  2654. }
  2655. group_next:
  2656. #endif
  2657. group = group->next;
  2658. } while (group != sd->groups);
  2659. if (!busiest || this_load >= max_load || busiest_nr_running == 0)
  2660. goto out_balanced;
  2661. avg_load = (SCHED_LOAD_SCALE * total_load) / total_pwr;
  2662. if (this_load >= avg_load ||
  2663. 100*max_load <= sd->imbalance_pct*this_load)
  2664. goto out_balanced;
  2665. busiest_load_per_task /= busiest_nr_running;
  2666. if (group_imb)
  2667. busiest_load_per_task = min(busiest_load_per_task, avg_load);
  2668. /*
  2669. * We're trying to get all the cpus to the average_load, so we don't
  2670. * want to push ourselves above the average load, nor do we wish to
  2671. * reduce the max loaded cpu below the average load, as either of these
  2672. * actions would just result in more rebalancing later, and ping-pong
  2673. * tasks around. Thus we look for the minimum possible imbalance.
  2674. * Negative imbalances (*we* are more loaded than anyone else) will
  2675. * be counted as no imbalance for these purposes -- we can't fix that
  2676. * by pulling tasks to us. Be careful of negative numbers as they'll
  2677. * appear as very large values with unsigned longs.
  2678. */
  2679. if (max_load <= busiest_load_per_task)
  2680. goto out_balanced;
  2681. /*
  2682. * In the presence of smp nice balancing, certain scenarios can have
  2683. * max load less than avg load(as we skip the groups at or below
  2684. * its cpu_power, while calculating max_load..)
  2685. */
  2686. if (max_load < avg_load) {
  2687. *imbalance = 0;
  2688. goto small_imbalance;
  2689. }
  2690. /* Don't want to pull so many tasks that a group would go idle */
  2691. max_pull = min(max_load - avg_load, max_load - busiest_load_per_task);
  2692. /* How much load to actually move to equalise the imbalance */
  2693. *imbalance = min(max_pull * busiest->__cpu_power,
  2694. (avg_load - this_load) * this->__cpu_power)
  2695. / SCHED_LOAD_SCALE;
  2696. /*
  2697. * if *imbalance is less than the average load per runnable task
  2698. * there is no gaurantee that any tasks will be moved so we'll have
  2699. * a think about bumping its value to force at least one task to be
  2700. * moved
  2701. */
  2702. if (*imbalance < busiest_load_per_task) {
  2703. unsigned long tmp, pwr_now, pwr_move;
  2704. unsigned int imbn;
  2705. small_imbalance:
  2706. pwr_move = pwr_now = 0;
  2707. imbn = 2;
  2708. if (this_nr_running) {
  2709. this_load_per_task /= this_nr_running;
  2710. if (busiest_load_per_task > this_load_per_task)
  2711. imbn = 1;
  2712. } else
  2713. this_load_per_task = SCHED_LOAD_SCALE;
  2714. if (max_load - this_load + SCHED_LOAD_SCALE_FUZZ >=
  2715. busiest_load_per_task * imbn) {
  2716. *imbalance = busiest_load_per_task;
  2717. return busiest;
  2718. }
  2719. /*
  2720. * OK, we don't have enough imbalance to justify moving tasks,
  2721. * however we may be able to increase total CPU power used by
  2722. * moving them.
  2723. */
  2724. pwr_now += busiest->__cpu_power *
  2725. min(busiest_load_per_task, max_load);
  2726. pwr_now += this->__cpu_power *
  2727. min(this_load_per_task, this_load);
  2728. pwr_now /= SCHED_LOAD_SCALE;
  2729. /* Amount of load we'd subtract */
  2730. tmp = sg_div_cpu_power(busiest,
  2731. busiest_load_per_task * SCHED_LOAD_SCALE);
  2732. if (max_load > tmp)
  2733. pwr_move += busiest->__cpu_power *
  2734. min(busiest_load_per_task, max_load - tmp);
  2735. /* Amount of load we'd add */
  2736. if (max_load * busiest->__cpu_power <
  2737. busiest_load_per_task * SCHED_LOAD_SCALE)
  2738. tmp = sg_div_cpu_power(this,
  2739. max_load * busiest->__cpu_power);
  2740. else
  2741. tmp = sg_div_cpu_power(this,
  2742. busiest_load_per_task * SCHED_LOAD_SCALE);
  2743. pwr_move += this->__cpu_power *
  2744. min(this_load_per_task, this_load + tmp);
  2745. pwr_move /= SCHED_LOAD_SCALE;
  2746. /* Move if we gain throughput */
  2747. if (pwr_move > pwr_now)
  2748. *imbalance = busiest_load_per_task;
  2749. }
  2750. return busiest;
  2751. out_balanced:
  2752. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2753. if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
  2754. goto ret;
  2755. if (this == group_leader && group_leader != group_min) {
  2756. *imbalance = min_load_per_task;
  2757. return group_min;
  2758. }
  2759. #endif
  2760. ret:
  2761. *imbalance = 0;
  2762. return NULL;
  2763. }
  2764. /*
  2765. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  2766. */
  2767. static struct rq *
  2768. find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
  2769. unsigned long imbalance, const cpumask_t *cpus)
  2770. {
  2771. struct rq *busiest = NULL, *rq;
  2772. unsigned long max_load = 0;
  2773. int i;
  2774. for_each_cpu_mask(i, group->cpumask) {
  2775. unsigned long wl;
  2776. if (!cpu_isset(i, *cpus))
  2777. continue;
  2778. rq = cpu_rq(i);
  2779. wl = weighted_cpuload(i);
  2780. if (rq->nr_running == 1 && wl > imbalance)
  2781. continue;
  2782. if (wl > max_load) {
  2783. max_load = wl;
  2784. busiest = rq;
  2785. }
  2786. }
  2787. return busiest;
  2788. }
  2789. /*
  2790. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  2791. * so long as it is large enough.
  2792. */
  2793. #define MAX_PINNED_INTERVAL 512
  2794. /*
  2795. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  2796. * tasks if there is an imbalance.
  2797. */
  2798. static int load_balance(int this_cpu, struct rq *this_rq,
  2799. struct sched_domain *sd, enum cpu_idle_type idle,
  2800. int *balance, cpumask_t *cpus)
  2801. {
  2802. int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
  2803. struct sched_group *group;
  2804. unsigned long imbalance;
  2805. struct rq *busiest;
  2806. unsigned long flags;
  2807. cpus_setall(*cpus);
  2808. /*
  2809. * When power savings policy is enabled for the parent domain, idle
  2810. * sibling can pick up load irrespective of busy siblings. In this case,
  2811. * let the state of idle sibling percolate up as CPU_IDLE, instead of
  2812. * portraying it as CPU_NOT_IDLE.
  2813. */
  2814. if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
  2815. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2816. sd_idle = 1;
  2817. schedstat_inc(sd, lb_count[idle]);
  2818. redo:
  2819. group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
  2820. cpus, balance);
  2821. if (*balance == 0)
  2822. goto out_balanced;
  2823. if (!group) {
  2824. schedstat_inc(sd, lb_nobusyg[idle]);
  2825. goto out_balanced;
  2826. }
  2827. busiest = find_busiest_queue(group, idle, imbalance, cpus);
  2828. if (!busiest) {
  2829. schedstat_inc(sd, lb_nobusyq[idle]);
  2830. goto out_balanced;
  2831. }
  2832. BUG_ON(busiest == this_rq);
  2833. schedstat_add(sd, lb_imbalance[idle], imbalance);
  2834. ld_moved = 0;
  2835. if (busiest->nr_running > 1) {
  2836. /*
  2837. * Attempt to move tasks. If find_busiest_group has found
  2838. * an imbalance but busiest->nr_running <= 1, the group is
  2839. * still unbalanced. ld_moved simply stays zero, so it is
  2840. * correctly treated as an imbalance.
  2841. */
  2842. local_irq_save(flags);
  2843. double_rq_lock(this_rq, busiest);
  2844. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  2845. imbalance, sd, idle, &all_pinned);
  2846. double_rq_unlock(this_rq, busiest);
  2847. local_irq_restore(flags);
  2848. /*
  2849. * some other cpu did the load balance for us.
  2850. */
  2851. if (ld_moved && this_cpu != smp_processor_id())
  2852. resched_cpu(this_cpu);
  2853. /* All tasks on this runqueue were pinned by CPU affinity */
  2854. if (unlikely(all_pinned)) {
  2855. cpu_clear(cpu_of(busiest), *cpus);
  2856. if (!cpus_empty(*cpus))
  2857. goto redo;
  2858. goto out_balanced;
  2859. }
  2860. }
  2861. if (!ld_moved) {
  2862. schedstat_inc(sd, lb_failed[idle]);
  2863. sd->nr_balance_failed++;
  2864. if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
  2865. spin_lock_irqsave(&busiest->lock, flags);
  2866. /* don't kick the migration_thread, if the curr
  2867. * task on busiest cpu can't be moved to this_cpu
  2868. */
  2869. if (!cpu_isset(this_cpu, busiest->curr->cpus_allowed)) {
  2870. spin_unlock_irqrestore(&busiest->lock, flags);
  2871. all_pinned = 1;
  2872. goto out_one_pinned;
  2873. }
  2874. if (!busiest->active_balance) {
  2875. busiest->active_balance = 1;
  2876. busiest->push_cpu = this_cpu;
  2877. active_balance = 1;
  2878. }
  2879. spin_unlock_irqrestore(&busiest->lock, flags);
  2880. if (active_balance)
  2881. wake_up_process(busiest->migration_thread);
  2882. /*
  2883. * We've kicked active balancing, reset the failure
  2884. * counter.
  2885. */
  2886. sd->nr_balance_failed = sd->cache_nice_tries+1;
  2887. }
  2888. } else
  2889. sd->nr_balance_failed = 0;
  2890. if (likely(!active_balance)) {
  2891. /* We were unbalanced, so reset the balancing interval */
  2892. sd->balance_interval = sd->min_interval;
  2893. } else {
  2894. /*
  2895. * If we've begun active balancing, start to back off. This
  2896. * case may not be covered by the all_pinned logic if there
  2897. * is only 1 task on the busy runqueue (because we don't call
  2898. * move_tasks).
  2899. */
  2900. if (sd->balance_interval < sd->max_interval)
  2901. sd->balance_interval *= 2;
  2902. }
  2903. if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2904. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2905. return -1;
  2906. return ld_moved;
  2907. out_balanced:
  2908. schedstat_inc(sd, lb_balanced[idle]);
  2909. sd->nr_balance_failed = 0;
  2910. out_one_pinned:
  2911. /* tune up the balancing interval */
  2912. if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
  2913. (sd->balance_interval < sd->max_interval))
  2914. sd->balance_interval *= 2;
  2915. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2916. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2917. return -1;
  2918. return 0;
  2919. }
  2920. /*
  2921. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  2922. * tasks if there is an imbalance.
  2923. *
  2924. * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
  2925. * this_rq is locked.
  2926. */
  2927. static int
  2928. load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
  2929. cpumask_t *cpus)
  2930. {
  2931. struct sched_group *group;
  2932. struct rq *busiest = NULL;
  2933. unsigned long imbalance;
  2934. int ld_moved = 0;
  2935. int sd_idle = 0;
  2936. int all_pinned = 0;
  2937. cpus_setall(*cpus);
  2938. /*
  2939. * When power savings policy is enabled for the parent domain, idle
  2940. * sibling can pick up load irrespective of busy siblings. In this case,
  2941. * let the state of idle sibling percolate up as IDLE, instead of
  2942. * portraying it as CPU_NOT_IDLE.
  2943. */
  2944. if (sd->flags & SD_SHARE_CPUPOWER &&
  2945. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2946. sd_idle = 1;
  2947. schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
  2948. redo:
  2949. group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
  2950. &sd_idle, cpus, NULL);
  2951. if (!group) {
  2952. schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
  2953. goto out_balanced;
  2954. }
  2955. busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
  2956. if (!busiest) {
  2957. schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
  2958. goto out_balanced;
  2959. }
  2960. BUG_ON(busiest == this_rq);
  2961. schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
  2962. ld_moved = 0;
  2963. if (busiest->nr_running > 1) {
  2964. /* Attempt to move tasks */
  2965. double_lock_balance(this_rq, busiest);
  2966. /* this_rq->clock is already updated */
  2967. update_rq_clock(busiest);
  2968. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  2969. imbalance, sd, CPU_NEWLY_IDLE,
  2970. &all_pinned);
  2971. spin_unlock(&busiest->lock);
  2972. if (unlikely(all_pinned)) {
  2973. cpu_clear(cpu_of(busiest), *cpus);
  2974. if (!cpus_empty(*cpus))
  2975. goto redo;
  2976. }
  2977. }
  2978. if (!ld_moved) {
  2979. schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
  2980. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2981. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2982. return -1;
  2983. } else
  2984. sd->nr_balance_failed = 0;
  2985. return ld_moved;
  2986. out_balanced:
  2987. schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
  2988. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  2989. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  2990. return -1;
  2991. sd->nr_balance_failed = 0;
  2992. return 0;
  2993. }
  2994. /*
  2995. * idle_balance is called by schedule() if this_cpu is about to become
  2996. * idle. Attempts to pull tasks from other CPUs.
  2997. */
  2998. static void idle_balance(int this_cpu, struct rq *this_rq)
  2999. {
  3000. struct sched_domain *sd;
  3001. int pulled_task = -1;
  3002. unsigned long next_balance = jiffies + HZ;
  3003. cpumask_t tmpmask;
  3004. for_each_domain(this_cpu, sd) {
  3005. unsigned long interval;
  3006. if (!(sd->flags & SD_LOAD_BALANCE))
  3007. continue;
  3008. if (sd->flags & SD_BALANCE_NEWIDLE)
  3009. /* If we've pulled tasks over stop searching: */
  3010. pulled_task = load_balance_newidle(this_cpu, this_rq,
  3011. sd, &tmpmask);
  3012. interval = msecs_to_jiffies(sd->balance_interval);
  3013. if (time_after(next_balance, sd->last_balance + interval))
  3014. next_balance = sd->last_balance + interval;
  3015. if (pulled_task)
  3016. break;
  3017. }
  3018. if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
  3019. /*
  3020. * We are going idle. next_balance may be set based on
  3021. * a busy processor. So reset next_balance.
  3022. */
  3023. this_rq->next_balance = next_balance;
  3024. }
  3025. }
  3026. /*
  3027. * active_load_balance is run by migration threads. It pushes running tasks
  3028. * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
  3029. * running on each physical CPU where possible, and avoids physical /
  3030. * logical imbalances.
  3031. *
  3032. * Called with busiest_rq locked.
  3033. */
  3034. static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
  3035. {
  3036. int target_cpu = busiest_rq->push_cpu;
  3037. struct sched_domain *sd;
  3038. struct rq *target_rq;
  3039. /* Is there any task to move? */
  3040. if (busiest_rq->nr_running <= 1)
  3041. return;
  3042. target_rq = cpu_rq(target_cpu);
  3043. /*
  3044. * This condition is "impossible", if it occurs
  3045. * we need to fix it. Originally reported by
  3046. * Bjorn Helgaas on a 128-cpu setup.
  3047. */
  3048. BUG_ON(busiest_rq == target_rq);
  3049. /* move a task from busiest_rq to target_rq */
  3050. double_lock_balance(busiest_rq, target_rq);
  3051. update_rq_clock(busiest_rq);
  3052. update_rq_clock(target_rq);
  3053. /* Search for an sd spanning us and the target CPU. */
  3054. for_each_domain(target_cpu, sd) {
  3055. if ((sd->flags & SD_LOAD_BALANCE) &&
  3056. cpu_isset(busiest_cpu, sd->span))
  3057. break;
  3058. }
  3059. if (likely(sd)) {
  3060. schedstat_inc(sd, alb_count);
  3061. if (move_one_task(target_rq, target_cpu, busiest_rq,
  3062. sd, CPU_IDLE))
  3063. schedstat_inc(sd, alb_pushed);
  3064. else
  3065. schedstat_inc(sd, alb_failed);
  3066. }
  3067. spin_unlock(&target_rq->lock);
  3068. }
  3069. #ifdef CONFIG_NO_HZ
  3070. static struct {
  3071. atomic_t load_balancer;
  3072. cpumask_t cpu_mask;
  3073. } nohz ____cacheline_aligned = {
  3074. .load_balancer = ATOMIC_INIT(-1),
  3075. .cpu_mask = CPU_MASK_NONE,
  3076. };
  3077. /*
  3078. * This routine will try to nominate the ilb (idle load balancing)
  3079. * owner among the cpus whose ticks are stopped. ilb owner will do the idle
  3080. * load balancing on behalf of all those cpus. If all the cpus in the system
  3081. * go into this tickless mode, then there will be no ilb owner (as there is
  3082. * no need for one) and all the cpus will sleep till the next wakeup event
  3083. * arrives...
  3084. *
  3085. * For the ilb owner, tick is not stopped. And this tick will be used
  3086. * for idle load balancing. ilb owner will still be part of
  3087. * nohz.cpu_mask..
  3088. *
  3089. * While stopping the tick, this cpu will become the ilb owner if there
  3090. * is no other owner. And will be the owner till that cpu becomes busy
  3091. * or if all cpus in the system stop their ticks at which point
  3092. * there is no need for ilb owner.
  3093. *
  3094. * When the ilb owner becomes busy, it nominates another owner, during the
  3095. * next busy scheduler_tick()
  3096. */
  3097. int select_nohz_load_balancer(int stop_tick)
  3098. {
  3099. int cpu = smp_processor_id();
  3100. if (stop_tick) {
  3101. cpu_set(cpu, nohz.cpu_mask);
  3102. cpu_rq(cpu)->in_nohz_recently = 1;
  3103. /*
  3104. * If we are going offline and still the leader, give up!
  3105. */
  3106. if (cpu_is_offline(cpu) &&
  3107. atomic_read(&nohz.load_balancer) == cpu) {
  3108. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  3109. BUG();
  3110. return 0;
  3111. }
  3112. /* time for ilb owner also to sleep */
  3113. if (cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
  3114. if (atomic_read(&nohz.load_balancer) == cpu)
  3115. atomic_set(&nohz.load_balancer, -1);
  3116. return 0;
  3117. }
  3118. if (atomic_read(&nohz.load_balancer) == -1) {
  3119. /* make me the ilb owner */
  3120. if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
  3121. return 1;
  3122. } else if (atomic_read(&nohz.load_balancer) == cpu)
  3123. return 1;
  3124. } else {
  3125. if (!cpu_isset(cpu, nohz.cpu_mask))
  3126. return 0;
  3127. cpu_clear(cpu, nohz.cpu_mask);
  3128. if (atomic_read(&nohz.load_balancer) == cpu)
  3129. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  3130. BUG();
  3131. }
  3132. return 0;
  3133. }
  3134. #endif
  3135. static DEFINE_SPINLOCK(balancing);
  3136. /*
  3137. * It checks each scheduling domain to see if it is due to be balanced,
  3138. * and initiates a balancing operation if so.
  3139. *
  3140. * Balancing parameters are set up in arch_init_sched_domains.
  3141. */
  3142. static void rebalance_domains(int cpu, enum cpu_idle_type idle)
  3143. {
  3144. int balance = 1;
  3145. struct rq *rq = cpu_rq(cpu);
  3146. unsigned long interval;
  3147. struct sched_domain *sd;
  3148. /* Earliest time when we have to do rebalance again */
  3149. unsigned long next_balance = jiffies + 60*HZ;
  3150. int update_next_balance = 0;
  3151. cpumask_t tmp;
  3152. for_each_domain(cpu, sd) {
  3153. if (!(sd->flags & SD_LOAD_BALANCE))
  3154. continue;
  3155. interval = sd->balance_interval;
  3156. if (idle != CPU_IDLE)
  3157. interval *= sd->busy_factor;
  3158. /* scale ms to jiffies */
  3159. interval = msecs_to_jiffies(interval);
  3160. if (unlikely(!interval))
  3161. interval = 1;
  3162. if (interval > HZ*NR_CPUS/10)
  3163. interval = HZ*NR_CPUS/10;
  3164. if (sd->flags & SD_SERIALIZE) {
  3165. if (!spin_trylock(&balancing))
  3166. goto out;
  3167. }
  3168. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  3169. if (load_balance(cpu, rq, sd, idle, &balance, &tmp)) {
  3170. /*
  3171. * We've pulled tasks over so either we're no
  3172. * longer idle, or one of our SMT siblings is
  3173. * not idle.
  3174. */
  3175. idle = CPU_NOT_IDLE;
  3176. }
  3177. sd->last_balance = jiffies;
  3178. }
  3179. if (sd->flags & SD_SERIALIZE)
  3180. spin_unlock(&balancing);
  3181. out:
  3182. if (time_after(next_balance, sd->last_balance + interval)) {
  3183. next_balance = sd->last_balance + interval;
  3184. update_next_balance = 1;
  3185. }
  3186. /*
  3187. * Stop the load balance at this level. There is another
  3188. * CPU in our sched group which is doing load balancing more
  3189. * actively.
  3190. */
  3191. if (!balance)
  3192. break;
  3193. }
  3194. /*
  3195. * next_balance will be updated only when there is a need.
  3196. * When the cpu is attached to null domain for ex, it will not be
  3197. * updated.
  3198. */
  3199. if (likely(update_next_balance))
  3200. rq->next_balance = next_balance;
  3201. }
  3202. /*
  3203. * run_rebalance_domains is triggered when needed from the scheduler tick.
  3204. * In CONFIG_NO_HZ case, the idle load balance owner will do the
  3205. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  3206. */
  3207. static void run_rebalance_domains(struct softirq_action *h)
  3208. {
  3209. int this_cpu = smp_processor_id();
  3210. struct rq *this_rq = cpu_rq(this_cpu);
  3211. enum cpu_idle_type idle = this_rq->idle_at_tick ?
  3212. CPU_IDLE : CPU_NOT_IDLE;
  3213. rebalance_domains(this_cpu, idle);
  3214. #ifdef CONFIG_NO_HZ
  3215. /*
  3216. * If this cpu is the owner for idle load balancing, then do the
  3217. * balancing on behalf of the other idle cpus whose ticks are
  3218. * stopped.
  3219. */
  3220. if (this_rq->idle_at_tick &&
  3221. atomic_read(&nohz.load_balancer) == this_cpu) {
  3222. cpumask_t cpus = nohz.cpu_mask;
  3223. struct rq *rq;
  3224. int balance_cpu;
  3225. cpu_clear(this_cpu, cpus);
  3226. for_each_cpu_mask(balance_cpu, cpus) {
  3227. /*
  3228. * If this cpu gets work to do, stop the load balancing
  3229. * work being done for other cpus. Next load
  3230. * balancing owner will pick it up.
  3231. */
  3232. if (need_resched())
  3233. break;
  3234. rebalance_domains(balance_cpu, CPU_IDLE);
  3235. rq = cpu_rq(balance_cpu);
  3236. if (time_after(this_rq->next_balance, rq->next_balance))
  3237. this_rq->next_balance = rq->next_balance;
  3238. }
  3239. }
  3240. #endif
  3241. }
  3242. /*
  3243. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  3244. *
  3245. * In case of CONFIG_NO_HZ, this is the place where we nominate a new
  3246. * idle load balancing owner or decide to stop the periodic load balancing,
  3247. * if the whole system is idle.
  3248. */
  3249. static inline void trigger_load_balance(struct rq *rq, int cpu)
  3250. {
  3251. #ifdef CONFIG_NO_HZ
  3252. /*
  3253. * If we were in the nohz mode recently and busy at the current
  3254. * scheduler tick, then check if we need to nominate new idle
  3255. * load balancer.
  3256. */
  3257. if (rq->in_nohz_recently && !rq->idle_at_tick) {
  3258. rq->in_nohz_recently = 0;
  3259. if (atomic_read(&nohz.load_balancer) == cpu) {
  3260. cpu_clear(cpu, nohz.cpu_mask);
  3261. atomic_set(&nohz.load_balancer, -1);
  3262. }
  3263. if (atomic_read(&nohz.load_balancer) == -1) {
  3264. /*
  3265. * simple selection for now: Nominate the
  3266. * first cpu in the nohz list to be the next
  3267. * ilb owner.
  3268. *
  3269. * TBD: Traverse the sched domains and nominate
  3270. * the nearest cpu in the nohz.cpu_mask.
  3271. */
  3272. int ilb = first_cpu(nohz.cpu_mask);
  3273. if (ilb < nr_cpu_ids)
  3274. resched_cpu(ilb);
  3275. }
  3276. }
  3277. /*
  3278. * If this cpu is idle and doing idle load balancing for all the
  3279. * cpus with ticks stopped, is it time for that to stop?
  3280. */
  3281. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
  3282. cpus_weight(nohz.cpu_mask) == num_online_cpus()) {
  3283. resched_cpu(cpu);
  3284. return;
  3285. }
  3286. /*
  3287. * If this cpu is idle and the idle load balancing is done by
  3288. * someone else, then no need raise the SCHED_SOFTIRQ
  3289. */
  3290. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
  3291. cpu_isset(cpu, nohz.cpu_mask))
  3292. return;
  3293. #endif
  3294. if (time_after_eq(jiffies, rq->next_balance))
  3295. raise_softirq(SCHED_SOFTIRQ);
  3296. }
  3297. #else /* CONFIG_SMP */
  3298. /*
  3299. * on UP we do not need to balance between CPUs:
  3300. */
  3301. static inline void idle_balance(int cpu, struct rq *rq)
  3302. {
  3303. }
  3304. #endif
  3305. DEFINE_PER_CPU(struct kernel_stat, kstat);
  3306. EXPORT_PER_CPU_SYMBOL(kstat);
  3307. /*
  3308. * Return p->sum_exec_runtime plus any more ns on the sched_clock
  3309. * that have not yet been banked in case the task is currently running.
  3310. */
  3311. unsigned long long task_sched_runtime(struct task_struct *p)
  3312. {
  3313. unsigned long flags;
  3314. u64 ns, delta_exec;
  3315. struct rq *rq;
  3316. rq = task_rq_lock(p, &flags);
  3317. ns = p->se.sum_exec_runtime;
  3318. if (task_current(rq, p)) {
  3319. update_rq_clock(rq);
  3320. delta_exec = rq->clock - p->se.exec_start;
  3321. if ((s64)delta_exec > 0)
  3322. ns += delta_exec;
  3323. }
  3324. task_rq_unlock(rq, &flags);
  3325. return ns;
  3326. }
  3327. /*
  3328. * Account user cpu time to a process.
  3329. * @p: the process that the cpu time gets accounted to
  3330. * @cputime: the cpu time spent in user space since the last update
  3331. */
  3332. void account_user_time(struct task_struct *p, cputime_t cputime)
  3333. {
  3334. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3335. cputime64_t tmp;
  3336. p->utime = cputime_add(p->utime, cputime);
  3337. /* Add user time to cpustat. */
  3338. tmp = cputime_to_cputime64(cputime);
  3339. if (TASK_NICE(p) > 0)
  3340. cpustat->nice = cputime64_add(cpustat->nice, tmp);
  3341. else
  3342. cpustat->user = cputime64_add(cpustat->user, tmp);
  3343. }
  3344. /*
  3345. * Account guest cpu time to a process.
  3346. * @p: the process that the cpu time gets accounted to
  3347. * @cputime: the cpu time spent in virtual machine since the last update
  3348. */
  3349. static void account_guest_time(struct task_struct *p, cputime_t cputime)
  3350. {
  3351. cputime64_t tmp;
  3352. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3353. tmp = cputime_to_cputime64(cputime);
  3354. p->utime = cputime_add(p->utime, cputime);
  3355. p->gtime = cputime_add(p->gtime, cputime);
  3356. cpustat->user = cputime64_add(cpustat->user, tmp);
  3357. cpustat->guest = cputime64_add(cpustat->guest, tmp);
  3358. }
  3359. /*
  3360. * Account scaled user cpu time to a process.
  3361. * @p: the process that the cpu time gets accounted to
  3362. * @cputime: the cpu time spent in user space since the last update
  3363. */
  3364. void account_user_time_scaled(struct task_struct *p, cputime_t cputime)
  3365. {
  3366. p->utimescaled = cputime_add(p->utimescaled, cputime);
  3367. }
  3368. /*
  3369. * Account system cpu time to a process.
  3370. * @p: the process that the cpu time gets accounted to
  3371. * @hardirq_offset: the offset to subtract from hardirq_count()
  3372. * @cputime: the cpu time spent in kernel space since the last update
  3373. */
  3374. void account_system_time(struct task_struct *p, int hardirq_offset,
  3375. cputime_t cputime)
  3376. {
  3377. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3378. struct rq *rq = this_rq();
  3379. cputime64_t tmp;
  3380. if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
  3381. account_guest_time(p, cputime);
  3382. return;
  3383. }
  3384. p->stime = cputime_add(p->stime, cputime);
  3385. /* Add system time to cpustat. */
  3386. tmp = cputime_to_cputime64(cputime);
  3387. if (hardirq_count() - hardirq_offset)
  3388. cpustat->irq = cputime64_add(cpustat->irq, tmp);
  3389. else if (softirq_count())
  3390. cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
  3391. else if (p != rq->idle)
  3392. cpustat->system = cputime64_add(cpustat->system, tmp);
  3393. else if (atomic_read(&rq->nr_iowait) > 0)
  3394. cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
  3395. else
  3396. cpustat->idle = cputime64_add(cpustat->idle, tmp);
  3397. /* Account for system time used */
  3398. acct_update_integrals(p);
  3399. }
  3400. /*
  3401. * Account scaled system cpu time to a process.
  3402. * @p: the process that the cpu time gets accounted to
  3403. * @hardirq_offset: the offset to subtract from hardirq_count()
  3404. * @cputime: the cpu time spent in kernel space since the last update
  3405. */
  3406. void account_system_time_scaled(struct task_struct *p, cputime_t cputime)
  3407. {
  3408. p->stimescaled = cputime_add(p->stimescaled, cputime);
  3409. }
  3410. /*
  3411. * Account for involuntary wait time.
  3412. * @p: the process from which the cpu time has been stolen
  3413. * @steal: the cpu time spent in involuntary wait
  3414. */
  3415. void account_steal_time(struct task_struct *p, cputime_t steal)
  3416. {
  3417. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3418. cputime64_t tmp = cputime_to_cputime64(steal);
  3419. struct rq *rq = this_rq();
  3420. if (p == rq->idle) {
  3421. p->stime = cputime_add(p->stime, steal);
  3422. if (atomic_read(&rq->nr_iowait) > 0)
  3423. cpustat->iowait = cputime64_add(cpustat->iowait, tmp);
  3424. else
  3425. cpustat->idle = cputime64_add(cpustat->idle, tmp);
  3426. } else
  3427. cpustat->steal = cputime64_add(cpustat->steal, tmp);
  3428. }
  3429. /*
  3430. * This function gets called by the timer code, with HZ frequency.
  3431. * We call it with interrupts disabled.
  3432. *
  3433. * It also gets called by the fork code, when changing the parent's
  3434. * timeslices.
  3435. */
  3436. void scheduler_tick(void)
  3437. {
  3438. int cpu = smp_processor_id();
  3439. struct rq *rq = cpu_rq(cpu);
  3440. struct task_struct *curr = rq->curr;
  3441. sched_clock_tick();
  3442. spin_lock(&rq->lock);
  3443. update_rq_clock(rq);
  3444. update_cpu_load(rq);
  3445. curr->sched_class->task_tick(rq, curr, 0);
  3446. spin_unlock(&rq->lock);
  3447. #ifdef CONFIG_SMP
  3448. rq->idle_at_tick = idle_cpu(cpu);
  3449. trigger_load_balance(rq, cpu);
  3450. #endif
  3451. }
  3452. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  3453. defined(CONFIG_PREEMPT_TRACER))
  3454. static inline unsigned long get_parent_ip(unsigned long addr)
  3455. {
  3456. if (in_lock_functions(addr)) {
  3457. addr = CALLER_ADDR2;
  3458. if (in_lock_functions(addr))
  3459. addr = CALLER_ADDR3;
  3460. }
  3461. return addr;
  3462. }
  3463. void __kprobes add_preempt_count(int val)
  3464. {
  3465. #ifdef CONFIG_DEBUG_PREEMPT
  3466. /*
  3467. * Underflow?
  3468. */
  3469. if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
  3470. return;
  3471. #endif
  3472. preempt_count() += val;
  3473. #ifdef CONFIG_DEBUG_PREEMPT
  3474. /*
  3475. * Spinlock count overflowing soon?
  3476. */
  3477. DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
  3478. PREEMPT_MASK - 10);
  3479. #endif
  3480. if (preempt_count() == val)
  3481. trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  3482. }
  3483. EXPORT_SYMBOL(add_preempt_count);
  3484. void __kprobes sub_preempt_count(int val)
  3485. {
  3486. #ifdef CONFIG_DEBUG_PREEMPT
  3487. /*
  3488. * Underflow?
  3489. */
  3490. if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
  3491. return;
  3492. /*
  3493. * Is the spinlock portion underflowing?
  3494. */
  3495. if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
  3496. !(preempt_count() & PREEMPT_MASK)))
  3497. return;
  3498. #endif
  3499. if (preempt_count() == val)
  3500. trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  3501. preempt_count() -= val;
  3502. }
  3503. EXPORT_SYMBOL(sub_preempt_count);
  3504. #endif
  3505. /*
  3506. * Print scheduling while atomic bug:
  3507. */
  3508. static noinline void __schedule_bug(struct task_struct *prev)
  3509. {
  3510. struct pt_regs *regs = get_irq_regs();
  3511. printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
  3512. prev->comm, prev->pid, preempt_count());
  3513. debug_show_held_locks(prev);
  3514. if (irqs_disabled())
  3515. print_irqtrace_events(prev);
  3516. if (regs)
  3517. show_regs(regs);
  3518. else
  3519. dump_stack();
  3520. }
  3521. /*
  3522. * Various schedule()-time debugging checks and statistics:
  3523. */
  3524. static inline void schedule_debug(struct task_struct *prev)
  3525. {
  3526. /*
  3527. * Test if we are atomic. Since do_exit() needs to call into
  3528. * schedule() atomically, we ignore that path for now.
  3529. * Otherwise, whine if we are scheduling when we should not be.
  3530. */
  3531. if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
  3532. __schedule_bug(prev);
  3533. profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  3534. schedstat_inc(this_rq(), sched_count);
  3535. #ifdef CONFIG_SCHEDSTATS
  3536. if (unlikely(prev->lock_depth >= 0)) {
  3537. schedstat_inc(this_rq(), bkl_count);
  3538. schedstat_inc(prev, sched_info.bkl_count);
  3539. }
  3540. #endif
  3541. }
  3542. /*
  3543. * Pick up the highest-prio task:
  3544. */
  3545. static inline struct task_struct *
  3546. pick_next_task(struct rq *rq, struct task_struct *prev)
  3547. {
  3548. const struct sched_class *class;
  3549. struct task_struct *p;
  3550. /*
  3551. * Optimization: we know that if all tasks are in
  3552. * the fair class we can call that function directly:
  3553. */
  3554. if (likely(rq->nr_running == rq->cfs.nr_running)) {
  3555. p = fair_sched_class.pick_next_task(rq);
  3556. if (likely(p))
  3557. return p;
  3558. }
  3559. class = sched_class_highest;
  3560. for ( ; ; ) {
  3561. p = class->pick_next_task(rq);
  3562. if (p)
  3563. return p;
  3564. /*
  3565. * Will never be NULL as the idle class always
  3566. * returns a non-NULL p:
  3567. */
  3568. class = class->next;
  3569. }
  3570. }
  3571. /*
  3572. * schedule() is the main scheduler function.
  3573. */
  3574. asmlinkage void __sched schedule(void)
  3575. {
  3576. struct task_struct *prev, *next;
  3577. unsigned long *switch_count;
  3578. struct rq *rq;
  3579. int cpu;
  3580. need_resched:
  3581. preempt_disable();
  3582. cpu = smp_processor_id();
  3583. rq = cpu_rq(cpu);
  3584. rcu_qsctr_inc(cpu);
  3585. prev = rq->curr;
  3586. switch_count = &prev->nivcsw;
  3587. release_kernel_lock(prev);
  3588. need_resched_nonpreemptible:
  3589. schedule_debug(prev);
  3590. hrtick_clear(rq);
  3591. /*
  3592. * Do the rq-clock update outside the rq lock:
  3593. */
  3594. local_irq_disable();
  3595. update_rq_clock(rq);
  3596. spin_lock(&rq->lock);
  3597. clear_tsk_need_resched(prev);
  3598. if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  3599. if (unlikely(signal_pending_state(prev->state, prev)))
  3600. prev->state = TASK_RUNNING;
  3601. else
  3602. deactivate_task(rq, prev, 1);
  3603. switch_count = &prev->nvcsw;
  3604. }
  3605. #ifdef CONFIG_SMP
  3606. if (prev->sched_class->pre_schedule)
  3607. prev->sched_class->pre_schedule(rq, prev);
  3608. #endif
  3609. if (unlikely(!rq->nr_running))
  3610. idle_balance(cpu, rq);
  3611. prev->sched_class->put_prev_task(rq, prev);
  3612. next = pick_next_task(rq, prev);
  3613. if (likely(prev != next)) {
  3614. sched_info_switch(prev, next);
  3615. rq->nr_switches++;
  3616. rq->curr = next;
  3617. ++*switch_count;
  3618. context_switch(rq, prev, next); /* unlocks the rq */
  3619. /*
  3620. * the context switch might have flipped the stack from under
  3621. * us, hence refresh the local variables.
  3622. */
  3623. cpu = smp_processor_id();
  3624. rq = cpu_rq(cpu);
  3625. } else
  3626. spin_unlock_irq(&rq->lock);
  3627. hrtick_set(rq);
  3628. if (unlikely(reacquire_kernel_lock(current) < 0))
  3629. goto need_resched_nonpreemptible;
  3630. preempt_enable_no_resched();
  3631. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  3632. goto need_resched;
  3633. }
  3634. EXPORT_SYMBOL(schedule);
  3635. #ifdef CONFIG_PREEMPT
  3636. /*
  3637. * this is the entry point to schedule() from in-kernel preemption
  3638. * off of preempt_enable. Kernel preemptions off return from interrupt
  3639. * occur there and call schedule directly.
  3640. */
  3641. asmlinkage void __sched preempt_schedule(void)
  3642. {
  3643. struct thread_info *ti = current_thread_info();
  3644. /*
  3645. * If there is a non-zero preempt_count or interrupts are disabled,
  3646. * we do not want to preempt the current task. Just return..
  3647. */
  3648. if (likely(ti->preempt_count || irqs_disabled()))
  3649. return;
  3650. do {
  3651. add_preempt_count(PREEMPT_ACTIVE);
  3652. schedule();
  3653. sub_preempt_count(PREEMPT_ACTIVE);
  3654. /*
  3655. * Check again in case we missed a preemption opportunity
  3656. * between schedule and now.
  3657. */
  3658. barrier();
  3659. } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
  3660. }
  3661. EXPORT_SYMBOL(preempt_schedule);
  3662. /*
  3663. * this is the entry point to schedule() from kernel preemption
  3664. * off of irq context.
  3665. * Note, that this is called and return with irqs disabled. This will
  3666. * protect us against recursive calling from irq.
  3667. */
  3668. asmlinkage void __sched preempt_schedule_irq(void)
  3669. {
  3670. struct thread_info *ti = current_thread_info();
  3671. /* Catch callers which need to be fixed */
  3672. BUG_ON(ti->preempt_count || !irqs_disabled());
  3673. do {
  3674. add_preempt_count(PREEMPT_ACTIVE);
  3675. local_irq_enable();
  3676. schedule();
  3677. local_irq_disable();
  3678. sub_preempt_count(PREEMPT_ACTIVE);
  3679. /*
  3680. * Check again in case we missed a preemption opportunity
  3681. * between schedule and now.
  3682. */
  3683. barrier();
  3684. } while (unlikely(test_thread_flag(TIF_NEED_RESCHED)));
  3685. }
  3686. #endif /* CONFIG_PREEMPT */
  3687. int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
  3688. void *key)
  3689. {
  3690. return try_to_wake_up(curr->private, mode, sync);
  3691. }
  3692. EXPORT_SYMBOL(default_wake_function);
  3693. /*
  3694. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  3695. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  3696. * number) then we wake all the non-exclusive tasks and one exclusive task.
  3697. *
  3698. * There are circumstances in which we can try to wake a task which has already
  3699. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  3700. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  3701. */
  3702. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  3703. int nr_exclusive, int sync, void *key)
  3704. {
  3705. wait_queue_t *curr, *next;
  3706. list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
  3707. unsigned flags = curr->flags;
  3708. if (curr->func(curr, mode, sync, key) &&
  3709. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  3710. break;
  3711. }
  3712. }
  3713. /**
  3714. * __wake_up - wake up threads blocked on a waitqueue.
  3715. * @q: the waitqueue
  3716. * @mode: which threads
  3717. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  3718. * @key: is directly passed to the wakeup function
  3719. */
  3720. void __wake_up(wait_queue_head_t *q, unsigned int mode,
  3721. int nr_exclusive, void *key)
  3722. {
  3723. unsigned long flags;
  3724. spin_lock_irqsave(&q->lock, flags);
  3725. __wake_up_common(q, mode, nr_exclusive, 0, key);
  3726. spin_unlock_irqrestore(&q->lock, flags);
  3727. }
  3728. EXPORT_SYMBOL(__wake_up);
  3729. /*
  3730. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  3731. */
  3732. void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
  3733. {
  3734. __wake_up_common(q, mode, 1, 0, NULL);
  3735. }
  3736. /**
  3737. * __wake_up_sync - wake up threads blocked on a waitqueue.
  3738. * @q: the waitqueue
  3739. * @mode: which threads
  3740. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  3741. *
  3742. * The sync wakeup differs that the waker knows that it will schedule
  3743. * away soon, so while the target thread will be woken up, it will not
  3744. * be migrated to another CPU - ie. the two threads are 'synchronized'
  3745. * with each other. This can prevent needless bouncing between CPUs.
  3746. *
  3747. * On UP it can prevent extra preemption.
  3748. */
  3749. void
  3750. __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  3751. {
  3752. unsigned long flags;
  3753. int sync = 1;
  3754. if (unlikely(!q))
  3755. return;
  3756. if (unlikely(!nr_exclusive))
  3757. sync = 0;
  3758. spin_lock_irqsave(&q->lock, flags);
  3759. __wake_up_common(q, mode, nr_exclusive, sync, NULL);
  3760. spin_unlock_irqrestore(&q->lock, flags);
  3761. }
  3762. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  3763. void complete(struct completion *x)
  3764. {
  3765. unsigned long flags;
  3766. spin_lock_irqsave(&x->wait.lock, flags);
  3767. x->done++;
  3768. __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
  3769. spin_unlock_irqrestore(&x->wait.lock, flags);
  3770. }
  3771. EXPORT_SYMBOL(complete);
  3772. void complete_all(struct completion *x)
  3773. {
  3774. unsigned long flags;
  3775. spin_lock_irqsave(&x->wait.lock, flags);
  3776. x->done += UINT_MAX/2;
  3777. __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
  3778. spin_unlock_irqrestore(&x->wait.lock, flags);
  3779. }
  3780. EXPORT_SYMBOL(complete_all);
  3781. static inline long __sched
  3782. do_wait_for_common(struct completion *x, long timeout, int state)
  3783. {
  3784. if (!x->done) {
  3785. DECLARE_WAITQUEUE(wait, current);
  3786. wait.flags |= WQ_FLAG_EXCLUSIVE;
  3787. __add_wait_queue_tail(&x->wait, &wait);
  3788. do {
  3789. if ((state == TASK_INTERRUPTIBLE &&
  3790. signal_pending(current)) ||
  3791. (state == TASK_KILLABLE &&
  3792. fatal_signal_pending(current))) {
  3793. __remove_wait_queue(&x->wait, &wait);
  3794. return -ERESTARTSYS;
  3795. }
  3796. __set_current_state(state);
  3797. spin_unlock_irq(&x->wait.lock);
  3798. timeout = schedule_timeout(timeout);
  3799. spin_lock_irq(&x->wait.lock);
  3800. if (!timeout) {
  3801. __remove_wait_queue(&x->wait, &wait);
  3802. return timeout;
  3803. }
  3804. } while (!x->done);
  3805. __remove_wait_queue(&x->wait, &wait);
  3806. }
  3807. x->done--;
  3808. return timeout;
  3809. }
  3810. static long __sched
  3811. wait_for_common(struct completion *x, long timeout, int state)
  3812. {
  3813. might_sleep();
  3814. spin_lock_irq(&x->wait.lock);
  3815. timeout = do_wait_for_common(x, timeout, state);
  3816. spin_unlock_irq(&x->wait.lock);
  3817. return timeout;
  3818. }
  3819. void __sched wait_for_completion(struct completion *x)
  3820. {
  3821. wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
  3822. }
  3823. EXPORT_SYMBOL(wait_for_completion);
  3824. unsigned long __sched
  3825. wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  3826. {
  3827. return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
  3828. }
  3829. EXPORT_SYMBOL(wait_for_completion_timeout);
  3830. int __sched wait_for_completion_interruptible(struct completion *x)
  3831. {
  3832. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
  3833. if (t == -ERESTARTSYS)
  3834. return t;
  3835. return 0;
  3836. }
  3837. EXPORT_SYMBOL(wait_for_completion_interruptible);
  3838. unsigned long __sched
  3839. wait_for_completion_interruptible_timeout(struct completion *x,
  3840. unsigned long timeout)
  3841. {
  3842. return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
  3843. }
  3844. EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  3845. int __sched wait_for_completion_killable(struct completion *x)
  3846. {
  3847. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
  3848. if (t == -ERESTARTSYS)
  3849. return t;
  3850. return 0;
  3851. }
  3852. EXPORT_SYMBOL(wait_for_completion_killable);
  3853. static long __sched
  3854. sleep_on_common(wait_queue_head_t *q, int state, long timeout)
  3855. {
  3856. unsigned long flags;
  3857. wait_queue_t wait;
  3858. init_waitqueue_entry(&wait, current);
  3859. __set_current_state(state);
  3860. spin_lock_irqsave(&q->lock, flags);
  3861. __add_wait_queue(q, &wait);
  3862. spin_unlock(&q->lock);
  3863. timeout = schedule_timeout(timeout);
  3864. spin_lock_irq(&q->lock);
  3865. __remove_wait_queue(q, &wait);
  3866. spin_unlock_irqrestore(&q->lock, flags);
  3867. return timeout;
  3868. }
  3869. void __sched interruptible_sleep_on(wait_queue_head_t *q)
  3870. {
  3871. sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  3872. }
  3873. EXPORT_SYMBOL(interruptible_sleep_on);
  3874. long __sched
  3875. interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
  3876. {
  3877. return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
  3878. }
  3879. EXPORT_SYMBOL(interruptible_sleep_on_timeout);
  3880. void __sched sleep_on(wait_queue_head_t *q)
  3881. {
  3882. sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  3883. }
  3884. EXPORT_SYMBOL(sleep_on);
  3885. long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
  3886. {
  3887. return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
  3888. }
  3889. EXPORT_SYMBOL(sleep_on_timeout);
  3890. #ifdef CONFIG_RT_MUTEXES
  3891. /*
  3892. * rt_mutex_setprio - set the current priority of a task
  3893. * @p: task
  3894. * @prio: prio value (kernel-internal form)
  3895. *
  3896. * This function changes the 'effective' priority of a task. It does
  3897. * not touch ->normal_prio like __setscheduler().
  3898. *
  3899. * Used by the rt_mutex code to implement priority inheritance logic.
  3900. */
  3901. void rt_mutex_setprio(struct task_struct *p, int prio)
  3902. {
  3903. unsigned long flags;
  3904. int oldprio, on_rq, running;
  3905. struct rq *rq;
  3906. const struct sched_class *prev_class = p->sched_class;
  3907. BUG_ON(prio < 0 || prio > MAX_PRIO);
  3908. rq = task_rq_lock(p, &flags);
  3909. update_rq_clock(rq);
  3910. oldprio = p->prio;
  3911. on_rq = p->se.on_rq;
  3912. running = task_current(rq, p);
  3913. if (on_rq)
  3914. dequeue_task(rq, p, 0);
  3915. if (running)
  3916. p->sched_class->put_prev_task(rq, p);
  3917. if (rt_prio(prio))
  3918. p->sched_class = &rt_sched_class;
  3919. else
  3920. p->sched_class = &fair_sched_class;
  3921. p->prio = prio;
  3922. if (running)
  3923. p->sched_class->set_curr_task(rq);
  3924. if (on_rq) {
  3925. enqueue_task(rq, p, 0);
  3926. check_class_changed(rq, p, prev_class, oldprio, running);
  3927. }
  3928. task_rq_unlock(rq, &flags);
  3929. }
  3930. #endif
  3931. void set_user_nice(struct task_struct *p, long nice)
  3932. {
  3933. int old_prio, delta, on_rq;
  3934. unsigned long flags;
  3935. struct rq *rq;
  3936. if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
  3937. return;
  3938. /*
  3939. * We have to be careful, if called from sys_setpriority(),
  3940. * the task might be in the middle of scheduling on another CPU.
  3941. */
  3942. rq = task_rq_lock(p, &flags);
  3943. update_rq_clock(rq);
  3944. /*
  3945. * The RT priorities are set via sched_setscheduler(), but we still
  3946. * allow the 'normal' nice value to be set - but as expected
  3947. * it wont have any effect on scheduling until the task is
  3948. * SCHED_FIFO/SCHED_RR:
  3949. */
  3950. if (task_has_rt_policy(p)) {
  3951. p->static_prio = NICE_TO_PRIO(nice);
  3952. goto out_unlock;
  3953. }
  3954. on_rq = p->se.on_rq;
  3955. if (on_rq) {
  3956. dequeue_task(rq, p, 0);
  3957. dec_load(rq, p);
  3958. }
  3959. p->static_prio = NICE_TO_PRIO(nice);
  3960. set_load_weight(p);
  3961. old_prio = p->prio;
  3962. p->prio = effective_prio(p);
  3963. delta = p->prio - old_prio;
  3964. if (on_rq) {
  3965. enqueue_task(rq, p, 0);
  3966. inc_load(rq, p);
  3967. /*
  3968. * If the task increased its priority or is running and
  3969. * lowered its priority, then reschedule its CPU:
  3970. */
  3971. if (delta < 0 || (delta > 0 && task_running(rq, p)))
  3972. resched_task(rq->curr);
  3973. }
  3974. out_unlock:
  3975. task_rq_unlock(rq, &flags);
  3976. }
  3977. EXPORT_SYMBOL(set_user_nice);
  3978. /*
  3979. * can_nice - check if a task can reduce its nice value
  3980. * @p: task
  3981. * @nice: nice value
  3982. */
  3983. int can_nice(const struct task_struct *p, const int nice)
  3984. {
  3985. /* convert nice value [19,-20] to rlimit style value [1,40] */
  3986. int nice_rlim = 20 - nice;
  3987. return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
  3988. capable(CAP_SYS_NICE));
  3989. }
  3990. #ifdef __ARCH_WANT_SYS_NICE
  3991. /*
  3992. * sys_nice - change the priority of the current process.
  3993. * @increment: priority increment
  3994. *
  3995. * sys_setpriority is a more generic, but much slower function that
  3996. * does similar things.
  3997. */
  3998. asmlinkage long sys_nice(int increment)
  3999. {
  4000. long nice, retval;
  4001. /*
  4002. * Setpriority might change our priority at the same moment.
  4003. * We don't have to worry. Conceptually one call occurs first
  4004. * and we have a single winner.
  4005. */
  4006. if (increment < -40)
  4007. increment = -40;
  4008. if (increment > 40)
  4009. increment = 40;
  4010. nice = PRIO_TO_NICE(current->static_prio) + increment;
  4011. if (nice < -20)
  4012. nice = -20;
  4013. if (nice > 19)
  4014. nice = 19;
  4015. if (increment < 0 && !can_nice(current, nice))
  4016. return -EPERM;
  4017. retval = security_task_setnice(current, nice);
  4018. if (retval)
  4019. return retval;
  4020. set_user_nice(current, nice);
  4021. return 0;
  4022. }
  4023. #endif
  4024. /**
  4025. * task_prio - return the priority value of a given task.
  4026. * @p: the task in question.
  4027. *
  4028. * This is the priority value as seen by users in /proc.
  4029. * RT tasks are offset by -200. Normal tasks are centered
  4030. * around 0, value goes from -16 to +15.
  4031. */
  4032. int task_prio(const struct task_struct *p)
  4033. {
  4034. return p->prio - MAX_RT_PRIO;
  4035. }
  4036. /**
  4037. * task_nice - return the nice value of a given task.
  4038. * @p: the task in question.
  4039. */
  4040. int task_nice(const struct task_struct *p)
  4041. {
  4042. return TASK_NICE(p);
  4043. }
  4044. EXPORT_SYMBOL(task_nice);
  4045. /**
  4046. * idle_cpu - is a given cpu idle currently?
  4047. * @cpu: the processor in question.
  4048. */
  4049. int idle_cpu(int cpu)
  4050. {
  4051. return cpu_curr(cpu) == cpu_rq(cpu)->idle;
  4052. }
  4053. /**
  4054. * idle_task - return the idle task for a given cpu.
  4055. * @cpu: the processor in question.
  4056. */
  4057. struct task_struct *idle_task(int cpu)
  4058. {
  4059. return cpu_rq(cpu)->idle;
  4060. }
  4061. /**
  4062. * find_process_by_pid - find a process with a matching PID value.
  4063. * @pid: the pid in question.
  4064. */
  4065. static struct task_struct *find_process_by_pid(pid_t pid)
  4066. {
  4067. return pid ? find_task_by_vpid(pid) : current;
  4068. }
  4069. /* Actually do priority change: must hold rq lock. */
  4070. static void
  4071. __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
  4072. {
  4073. BUG_ON(p->se.on_rq);
  4074. p->policy = policy;
  4075. switch (p->policy) {
  4076. case SCHED_NORMAL:
  4077. case SCHED_BATCH:
  4078. case SCHED_IDLE:
  4079. p->sched_class = &fair_sched_class;
  4080. break;
  4081. case SCHED_FIFO:
  4082. case SCHED_RR:
  4083. p->sched_class = &rt_sched_class;
  4084. break;
  4085. }
  4086. p->rt_priority = prio;
  4087. p->normal_prio = normal_prio(p);
  4088. /* we are holding p->pi_lock already */
  4089. p->prio = rt_mutex_getprio(p);
  4090. set_load_weight(p);
  4091. }
  4092. /**
  4093. * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
  4094. * @p: the task in question.
  4095. * @policy: new policy.
  4096. * @param: structure containing the new RT priority.
  4097. *
  4098. * NOTE that the task may be already dead.
  4099. */
  4100. int sched_setscheduler(struct task_struct *p, int policy,
  4101. struct sched_param *param)
  4102. {
  4103. int retval, oldprio, oldpolicy = -1, on_rq, running;
  4104. unsigned long flags;
  4105. const struct sched_class *prev_class = p->sched_class;
  4106. struct rq *rq;
  4107. /* may grab non-irq protected spin_locks */
  4108. BUG_ON(in_interrupt());
  4109. recheck:
  4110. /* double check policy once rq lock held */
  4111. if (policy < 0)
  4112. policy = oldpolicy = p->policy;
  4113. else if (policy != SCHED_FIFO && policy != SCHED_RR &&
  4114. policy != SCHED_NORMAL && policy != SCHED_BATCH &&
  4115. policy != SCHED_IDLE)
  4116. return -EINVAL;
  4117. /*
  4118. * Valid priorities for SCHED_FIFO and SCHED_RR are
  4119. * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
  4120. * SCHED_BATCH and SCHED_IDLE is 0.
  4121. */
  4122. if (param->sched_priority < 0 ||
  4123. (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
  4124. (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
  4125. return -EINVAL;
  4126. if (rt_policy(policy) != (param->sched_priority != 0))
  4127. return -EINVAL;
  4128. /*
  4129. * Allow unprivileged RT tasks to decrease priority:
  4130. */
  4131. if (!capable(CAP_SYS_NICE)) {
  4132. if (rt_policy(policy)) {
  4133. unsigned long rlim_rtprio;
  4134. if (!lock_task_sighand(p, &flags))
  4135. return -ESRCH;
  4136. rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
  4137. unlock_task_sighand(p, &flags);
  4138. /* can't set/change the rt policy */
  4139. if (policy != p->policy && !rlim_rtprio)
  4140. return -EPERM;
  4141. /* can't increase priority */
  4142. if (param->sched_priority > p->rt_priority &&
  4143. param->sched_priority > rlim_rtprio)
  4144. return -EPERM;
  4145. }
  4146. /*
  4147. * Like positive nice levels, dont allow tasks to
  4148. * move out of SCHED_IDLE either:
  4149. */
  4150. if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
  4151. return -EPERM;
  4152. /* can't change other user's priorities */
  4153. if ((current->euid != p->euid) &&
  4154. (current->euid != p->uid))
  4155. return -EPERM;
  4156. }
  4157. #ifdef CONFIG_RT_GROUP_SCHED
  4158. /*
  4159. * Do not allow realtime tasks into groups that have no runtime
  4160. * assigned.
  4161. */
  4162. if (rt_policy(policy) && task_group(p)->rt_bandwidth.rt_runtime == 0)
  4163. return -EPERM;
  4164. #endif
  4165. retval = security_task_setscheduler(p, policy, param);
  4166. if (retval)
  4167. return retval;
  4168. /*
  4169. * make sure no PI-waiters arrive (or leave) while we are
  4170. * changing the priority of the task:
  4171. */
  4172. spin_lock_irqsave(&p->pi_lock, flags);
  4173. /*
  4174. * To be able to change p->policy safely, the apropriate
  4175. * runqueue lock must be held.
  4176. */
  4177. rq = __task_rq_lock(p);
  4178. /* recheck policy now with rq lock held */
  4179. if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  4180. policy = oldpolicy = -1;
  4181. __task_rq_unlock(rq);
  4182. spin_unlock_irqrestore(&p->pi_lock, flags);
  4183. goto recheck;
  4184. }
  4185. update_rq_clock(rq);
  4186. on_rq = p->se.on_rq;
  4187. running = task_current(rq, p);
  4188. if (on_rq)
  4189. deactivate_task(rq, p, 0);
  4190. if (running)
  4191. p->sched_class->put_prev_task(rq, p);
  4192. oldprio = p->prio;
  4193. __setscheduler(rq, p, policy, param->sched_priority);
  4194. if (running)
  4195. p->sched_class->set_curr_task(rq);
  4196. if (on_rq) {
  4197. activate_task(rq, p, 0);
  4198. check_class_changed(rq, p, prev_class, oldprio, running);
  4199. }
  4200. __task_rq_unlock(rq);
  4201. spin_unlock_irqrestore(&p->pi_lock, flags);
  4202. rt_mutex_adjust_pi(p);
  4203. return 0;
  4204. }
  4205. EXPORT_SYMBOL_GPL(sched_setscheduler);
  4206. static int
  4207. do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  4208. {
  4209. struct sched_param lparam;
  4210. struct task_struct *p;
  4211. int retval;
  4212. if (!param || pid < 0)
  4213. return -EINVAL;
  4214. if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  4215. return -EFAULT;
  4216. rcu_read_lock();
  4217. retval = -ESRCH;
  4218. p = find_process_by_pid(pid);
  4219. if (p != NULL)
  4220. retval = sched_setscheduler(p, policy, &lparam);
  4221. rcu_read_unlock();
  4222. return retval;
  4223. }
  4224. /**
  4225. * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  4226. * @pid: the pid in question.
  4227. * @policy: new policy.
  4228. * @param: structure containing the new RT priority.
  4229. */
  4230. asmlinkage long
  4231. sys_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  4232. {
  4233. /* negative values for policy are not valid */
  4234. if (policy < 0)
  4235. return -EINVAL;
  4236. return do_sched_setscheduler(pid, policy, param);
  4237. }
  4238. /**
  4239. * sys_sched_setparam - set/change the RT priority of a thread
  4240. * @pid: the pid in question.
  4241. * @param: structure containing the new RT priority.
  4242. */
  4243. asmlinkage long sys_sched_setparam(pid_t pid, struct sched_param __user *param)
  4244. {
  4245. return do_sched_setscheduler(pid, -1, param);
  4246. }
  4247. /**
  4248. * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  4249. * @pid: the pid in question.
  4250. */
  4251. asmlinkage long sys_sched_getscheduler(pid_t pid)
  4252. {
  4253. struct task_struct *p;
  4254. int retval;
  4255. if (pid < 0)
  4256. return -EINVAL;
  4257. retval = -ESRCH;
  4258. read_lock(&tasklist_lock);
  4259. p = find_process_by_pid(pid);
  4260. if (p) {
  4261. retval = security_task_getscheduler(p);
  4262. if (!retval)
  4263. retval = p->policy;
  4264. }
  4265. read_unlock(&tasklist_lock);
  4266. return retval;
  4267. }
  4268. /**
  4269. * sys_sched_getscheduler - get the RT priority of a thread
  4270. * @pid: the pid in question.
  4271. * @param: structure containing the RT priority.
  4272. */
  4273. asmlinkage long sys_sched_getparam(pid_t pid, struct sched_param __user *param)
  4274. {
  4275. struct sched_param lp;
  4276. struct task_struct *p;
  4277. int retval;
  4278. if (!param || pid < 0)
  4279. return -EINVAL;
  4280. read_lock(&tasklist_lock);
  4281. p = find_process_by_pid(pid);
  4282. retval = -ESRCH;
  4283. if (!p)
  4284. goto out_unlock;
  4285. retval = security_task_getscheduler(p);
  4286. if (retval)
  4287. goto out_unlock;
  4288. lp.sched_priority = p->rt_priority;
  4289. read_unlock(&tasklist_lock);
  4290. /*
  4291. * This one might sleep, we cannot do it with a spinlock held ...
  4292. */
  4293. retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  4294. return retval;
  4295. out_unlock:
  4296. read_unlock(&tasklist_lock);
  4297. return retval;
  4298. }
  4299. long sched_setaffinity(pid_t pid, const cpumask_t *in_mask)
  4300. {
  4301. cpumask_t cpus_allowed;
  4302. cpumask_t new_mask = *in_mask;
  4303. struct task_struct *p;
  4304. int retval;
  4305. get_online_cpus();
  4306. read_lock(&tasklist_lock);
  4307. p = find_process_by_pid(pid);
  4308. if (!p) {
  4309. read_unlock(&tasklist_lock);
  4310. put_online_cpus();
  4311. return -ESRCH;
  4312. }
  4313. /*
  4314. * It is not safe to call set_cpus_allowed with the
  4315. * tasklist_lock held. We will bump the task_struct's
  4316. * usage count and then drop tasklist_lock.
  4317. */
  4318. get_task_struct(p);
  4319. read_unlock(&tasklist_lock);
  4320. retval = -EPERM;
  4321. if ((current->euid != p->euid) && (current->euid != p->uid) &&
  4322. !capable(CAP_SYS_NICE))
  4323. goto out_unlock;
  4324. retval = security_task_setscheduler(p, 0, NULL);
  4325. if (retval)
  4326. goto out_unlock;
  4327. cpuset_cpus_allowed(p, &cpus_allowed);
  4328. cpus_and(new_mask, new_mask, cpus_allowed);
  4329. again:
  4330. retval = set_cpus_allowed_ptr(p, &new_mask);
  4331. if (!retval) {
  4332. cpuset_cpus_allowed(p, &cpus_allowed);
  4333. if (!cpus_subset(new_mask, cpus_allowed)) {
  4334. /*
  4335. * We must have raced with a concurrent cpuset
  4336. * update. Just reset the cpus_allowed to the
  4337. * cpuset's cpus_allowed
  4338. */
  4339. new_mask = cpus_allowed;
  4340. goto again;
  4341. }
  4342. }
  4343. out_unlock:
  4344. put_task_struct(p);
  4345. put_online_cpus();
  4346. return retval;
  4347. }
  4348. static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  4349. cpumask_t *new_mask)
  4350. {
  4351. if (len < sizeof(cpumask_t)) {
  4352. memset(new_mask, 0, sizeof(cpumask_t));
  4353. } else if (len > sizeof(cpumask_t)) {
  4354. len = sizeof(cpumask_t);
  4355. }
  4356. return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  4357. }
  4358. /**
  4359. * sys_sched_setaffinity - set the cpu affinity of a process
  4360. * @pid: pid of the process
  4361. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  4362. * @user_mask_ptr: user-space pointer to the new cpu mask
  4363. */
  4364. asmlinkage long sys_sched_setaffinity(pid_t pid, unsigned int len,
  4365. unsigned long __user *user_mask_ptr)
  4366. {
  4367. cpumask_t new_mask;
  4368. int retval;
  4369. retval = get_user_cpu_mask(user_mask_ptr, len, &new_mask);
  4370. if (retval)
  4371. return retval;
  4372. return sched_setaffinity(pid, &new_mask);
  4373. }
  4374. /*
  4375. * Represents all cpu's present in the system
  4376. * In systems capable of hotplug, this map could dynamically grow
  4377. * as new cpu's are detected in the system via any platform specific
  4378. * method, such as ACPI for e.g.
  4379. */
  4380. cpumask_t cpu_present_map __read_mostly;
  4381. EXPORT_SYMBOL(cpu_present_map);
  4382. #ifndef CONFIG_SMP
  4383. cpumask_t cpu_online_map __read_mostly = CPU_MASK_ALL;
  4384. EXPORT_SYMBOL(cpu_online_map);
  4385. cpumask_t cpu_possible_map __read_mostly = CPU_MASK_ALL;
  4386. EXPORT_SYMBOL(cpu_possible_map);
  4387. #endif
  4388. long sched_getaffinity(pid_t pid, cpumask_t *mask)
  4389. {
  4390. struct task_struct *p;
  4391. int retval;
  4392. get_online_cpus();
  4393. read_lock(&tasklist_lock);
  4394. retval = -ESRCH;
  4395. p = find_process_by_pid(pid);
  4396. if (!p)
  4397. goto out_unlock;
  4398. retval = security_task_getscheduler(p);
  4399. if (retval)
  4400. goto out_unlock;
  4401. cpus_and(*mask, p->cpus_allowed, cpu_online_map);
  4402. out_unlock:
  4403. read_unlock(&tasklist_lock);
  4404. put_online_cpus();
  4405. return retval;
  4406. }
  4407. /**
  4408. * sys_sched_getaffinity - get the cpu affinity of a process
  4409. * @pid: pid of the process
  4410. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  4411. * @user_mask_ptr: user-space pointer to hold the current cpu mask
  4412. */
  4413. asmlinkage long sys_sched_getaffinity(pid_t pid, unsigned int len,
  4414. unsigned long __user *user_mask_ptr)
  4415. {
  4416. int ret;
  4417. cpumask_t mask;
  4418. if (len < sizeof(cpumask_t))
  4419. return -EINVAL;
  4420. ret = sched_getaffinity(pid, &mask);
  4421. if (ret < 0)
  4422. return ret;
  4423. if (copy_to_user(user_mask_ptr, &mask, sizeof(cpumask_t)))
  4424. return -EFAULT;
  4425. return sizeof(cpumask_t);
  4426. }
  4427. /**
  4428. * sys_sched_yield - yield the current processor to other threads.
  4429. *
  4430. * This function yields the current CPU to other tasks. If there are no
  4431. * other threads running on this CPU then this function will return.
  4432. */
  4433. asmlinkage long sys_sched_yield(void)
  4434. {
  4435. struct rq *rq = this_rq_lock();
  4436. schedstat_inc(rq, yld_count);
  4437. current->sched_class->yield_task(rq);
  4438. /*
  4439. * Since we are going to call schedule() anyway, there's
  4440. * no need to preempt or enable interrupts:
  4441. */
  4442. __release(rq->lock);
  4443. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  4444. _raw_spin_unlock(&rq->lock);
  4445. preempt_enable_no_resched();
  4446. schedule();
  4447. return 0;
  4448. }
  4449. static void __cond_resched(void)
  4450. {
  4451. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  4452. __might_sleep(__FILE__, __LINE__);
  4453. #endif
  4454. /*
  4455. * The BKS might be reacquired before we have dropped
  4456. * PREEMPT_ACTIVE, which could trigger a second
  4457. * cond_resched() call.
  4458. */
  4459. do {
  4460. add_preempt_count(PREEMPT_ACTIVE);
  4461. schedule();
  4462. sub_preempt_count(PREEMPT_ACTIVE);
  4463. } while (need_resched());
  4464. }
  4465. int __sched _cond_resched(void)
  4466. {
  4467. if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
  4468. system_state == SYSTEM_RUNNING) {
  4469. __cond_resched();
  4470. return 1;
  4471. }
  4472. return 0;
  4473. }
  4474. EXPORT_SYMBOL(_cond_resched);
  4475. /*
  4476. * cond_resched_lock() - if a reschedule is pending, drop the given lock,
  4477. * call schedule, and on return reacquire the lock.
  4478. *
  4479. * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  4480. * operations here to prevent schedule() from being called twice (once via
  4481. * spin_unlock(), once by hand).
  4482. */
  4483. int cond_resched_lock(spinlock_t *lock)
  4484. {
  4485. int resched = need_resched() && system_state == SYSTEM_RUNNING;
  4486. int ret = 0;
  4487. if (spin_needbreak(lock) || resched) {
  4488. spin_unlock(lock);
  4489. if (resched && need_resched())
  4490. __cond_resched();
  4491. else
  4492. cpu_relax();
  4493. ret = 1;
  4494. spin_lock(lock);
  4495. }
  4496. return ret;
  4497. }
  4498. EXPORT_SYMBOL(cond_resched_lock);
  4499. int __sched cond_resched_softirq(void)
  4500. {
  4501. BUG_ON(!in_softirq());
  4502. if (need_resched() && system_state == SYSTEM_RUNNING) {
  4503. local_bh_enable();
  4504. __cond_resched();
  4505. local_bh_disable();
  4506. return 1;
  4507. }
  4508. return 0;
  4509. }
  4510. EXPORT_SYMBOL(cond_resched_softirq);
  4511. /**
  4512. * yield - yield the current processor to other threads.
  4513. *
  4514. * This is a shortcut for kernel-space yielding - it marks the
  4515. * thread runnable and calls sys_sched_yield().
  4516. */
  4517. void __sched yield(void)
  4518. {
  4519. set_current_state(TASK_RUNNING);
  4520. sys_sched_yield();
  4521. }
  4522. EXPORT_SYMBOL(yield);
  4523. /*
  4524. * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  4525. * that process accounting knows that this is a task in IO wait state.
  4526. *
  4527. * But don't do that if it is a deliberate, throttling IO wait (this task
  4528. * has set its backing_dev_info: the queue against which it should throttle)
  4529. */
  4530. void __sched io_schedule(void)
  4531. {
  4532. struct rq *rq = &__raw_get_cpu_var(runqueues);
  4533. delayacct_blkio_start();
  4534. atomic_inc(&rq->nr_iowait);
  4535. schedule();
  4536. atomic_dec(&rq->nr_iowait);
  4537. delayacct_blkio_end();
  4538. }
  4539. EXPORT_SYMBOL(io_schedule);
  4540. long __sched io_schedule_timeout(long timeout)
  4541. {
  4542. struct rq *rq = &__raw_get_cpu_var(runqueues);
  4543. long ret;
  4544. delayacct_blkio_start();
  4545. atomic_inc(&rq->nr_iowait);
  4546. ret = schedule_timeout(timeout);
  4547. atomic_dec(&rq->nr_iowait);
  4548. delayacct_blkio_end();
  4549. return ret;
  4550. }
  4551. /**
  4552. * sys_sched_get_priority_max - return maximum RT priority.
  4553. * @policy: scheduling class.
  4554. *
  4555. * this syscall returns the maximum rt_priority that can be used
  4556. * by a given scheduling class.
  4557. */
  4558. asmlinkage long sys_sched_get_priority_max(int policy)
  4559. {
  4560. int ret = -EINVAL;
  4561. switch (policy) {
  4562. case SCHED_FIFO:
  4563. case SCHED_RR:
  4564. ret = MAX_USER_RT_PRIO-1;
  4565. break;
  4566. case SCHED_NORMAL:
  4567. case SCHED_BATCH:
  4568. case SCHED_IDLE:
  4569. ret = 0;
  4570. break;
  4571. }
  4572. return ret;
  4573. }
  4574. /**
  4575. * sys_sched_get_priority_min - return minimum RT priority.
  4576. * @policy: scheduling class.
  4577. *
  4578. * this syscall returns the minimum rt_priority that can be used
  4579. * by a given scheduling class.
  4580. */
  4581. asmlinkage long sys_sched_get_priority_min(int policy)
  4582. {
  4583. int ret = -EINVAL;
  4584. switch (policy) {
  4585. case SCHED_FIFO:
  4586. case SCHED_RR:
  4587. ret = 1;
  4588. break;
  4589. case SCHED_NORMAL:
  4590. case SCHED_BATCH:
  4591. case SCHED_IDLE:
  4592. ret = 0;
  4593. }
  4594. return ret;
  4595. }
  4596. /**
  4597. * sys_sched_rr_get_interval - return the default timeslice of a process.
  4598. * @pid: pid of the process.
  4599. * @interval: userspace pointer to the timeslice value.
  4600. *
  4601. * this syscall writes the default timeslice value of a given process
  4602. * into the user-space timespec buffer. A value of '0' means infinity.
  4603. */
  4604. asmlinkage
  4605. long sys_sched_rr_get_interval(pid_t pid, struct timespec __user *interval)
  4606. {
  4607. struct task_struct *p;
  4608. unsigned int time_slice;
  4609. int retval;
  4610. struct timespec t;
  4611. if (pid < 0)
  4612. return -EINVAL;
  4613. retval = -ESRCH;
  4614. read_lock(&tasklist_lock);
  4615. p = find_process_by_pid(pid);
  4616. if (!p)
  4617. goto out_unlock;
  4618. retval = security_task_getscheduler(p);
  4619. if (retval)
  4620. goto out_unlock;
  4621. /*
  4622. * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
  4623. * tasks that are on an otherwise idle runqueue:
  4624. */
  4625. time_slice = 0;
  4626. if (p->policy == SCHED_RR) {
  4627. time_slice = DEF_TIMESLICE;
  4628. } else if (p->policy != SCHED_FIFO) {
  4629. struct sched_entity *se = &p->se;
  4630. unsigned long flags;
  4631. struct rq *rq;
  4632. rq = task_rq_lock(p, &flags);
  4633. if (rq->cfs.load.weight)
  4634. time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
  4635. task_rq_unlock(rq, &flags);
  4636. }
  4637. read_unlock(&tasklist_lock);
  4638. jiffies_to_timespec(time_slice, &t);
  4639. retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  4640. return retval;
  4641. out_unlock:
  4642. read_unlock(&tasklist_lock);
  4643. return retval;
  4644. }
  4645. static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
  4646. void sched_show_task(struct task_struct *p)
  4647. {
  4648. unsigned long free = 0;
  4649. unsigned state;
  4650. state = p->state ? __ffs(p->state) + 1 : 0;
  4651. printk(KERN_INFO "%-13.13s %c", p->comm,
  4652. state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
  4653. #if BITS_PER_LONG == 32
  4654. if (state == TASK_RUNNING)
  4655. printk(KERN_CONT " running ");
  4656. else
  4657. printk(KERN_CONT " %08lx ", thread_saved_pc(p));
  4658. #else
  4659. if (state == TASK_RUNNING)
  4660. printk(KERN_CONT " running task ");
  4661. else
  4662. printk(KERN_CONT " %016lx ", thread_saved_pc(p));
  4663. #endif
  4664. #ifdef CONFIG_DEBUG_STACK_USAGE
  4665. {
  4666. unsigned long *n = end_of_stack(p);
  4667. while (!*n)
  4668. n++;
  4669. free = (unsigned long)n - (unsigned long)end_of_stack(p);
  4670. }
  4671. #endif
  4672. printk(KERN_CONT "%5lu %5d %6d\n", free,
  4673. task_pid_nr(p), task_pid_nr(p->real_parent));
  4674. show_stack(p, NULL);
  4675. }
  4676. void show_state_filter(unsigned long state_filter)
  4677. {
  4678. struct task_struct *g, *p;
  4679. #if BITS_PER_LONG == 32
  4680. printk(KERN_INFO
  4681. " task PC stack pid father\n");
  4682. #else
  4683. printk(KERN_INFO
  4684. " task PC stack pid father\n");
  4685. #endif
  4686. read_lock(&tasklist_lock);
  4687. do_each_thread(g, p) {
  4688. /*
  4689. * reset the NMI-timeout, listing all files on a slow
  4690. * console might take alot of time:
  4691. */
  4692. touch_nmi_watchdog();
  4693. if (!state_filter || (p->state & state_filter))
  4694. sched_show_task(p);
  4695. } while_each_thread(g, p);
  4696. touch_all_softlockup_watchdogs();
  4697. #ifdef CONFIG_SCHED_DEBUG
  4698. sysrq_sched_debug_show();
  4699. #endif
  4700. read_unlock(&tasklist_lock);
  4701. /*
  4702. * Only show locks if all tasks are dumped:
  4703. */
  4704. if (state_filter == -1)
  4705. debug_show_all_locks();
  4706. }
  4707. void __cpuinit init_idle_bootup_task(struct task_struct *idle)
  4708. {
  4709. idle->sched_class = &idle_sched_class;
  4710. }
  4711. /**
  4712. * init_idle - set up an idle thread for a given CPU
  4713. * @idle: task in question
  4714. * @cpu: cpu the idle task belongs to
  4715. *
  4716. * NOTE: this function does not set the idle thread's NEED_RESCHED
  4717. * flag, to make booting more robust.
  4718. */
  4719. void __cpuinit init_idle(struct task_struct *idle, int cpu)
  4720. {
  4721. struct rq *rq = cpu_rq(cpu);
  4722. unsigned long flags;
  4723. __sched_fork(idle);
  4724. idle->se.exec_start = sched_clock();
  4725. idle->prio = idle->normal_prio = MAX_PRIO;
  4726. idle->cpus_allowed = cpumask_of_cpu(cpu);
  4727. __set_task_cpu(idle, cpu);
  4728. spin_lock_irqsave(&rq->lock, flags);
  4729. rq->curr = rq->idle = idle;
  4730. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  4731. idle->oncpu = 1;
  4732. #endif
  4733. spin_unlock_irqrestore(&rq->lock, flags);
  4734. /* Set the preempt count _outside_ the spinlocks! */
  4735. #if defined(CONFIG_PREEMPT)
  4736. task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
  4737. #else
  4738. task_thread_info(idle)->preempt_count = 0;
  4739. #endif
  4740. /*
  4741. * The idle tasks have their own, simple scheduling class:
  4742. */
  4743. idle->sched_class = &idle_sched_class;
  4744. }
  4745. /*
  4746. * In a system that switches off the HZ timer nohz_cpu_mask
  4747. * indicates which cpus entered this state. This is used
  4748. * in the rcu update to wait only for active cpus. For system
  4749. * which do not switch off the HZ timer nohz_cpu_mask should
  4750. * always be CPU_MASK_NONE.
  4751. */
  4752. cpumask_t nohz_cpu_mask = CPU_MASK_NONE;
  4753. /*
  4754. * Increase the granularity value when there are more CPUs,
  4755. * because with more CPUs the 'effective latency' as visible
  4756. * to users decreases. But the relationship is not linear,
  4757. * so pick a second-best guess by going with the log2 of the
  4758. * number of CPUs.
  4759. *
  4760. * This idea comes from the SD scheduler of Con Kolivas:
  4761. */
  4762. static inline void sched_init_granularity(void)
  4763. {
  4764. unsigned int factor = 1 + ilog2(num_online_cpus());
  4765. const unsigned long limit = 200000000;
  4766. sysctl_sched_min_granularity *= factor;
  4767. if (sysctl_sched_min_granularity > limit)
  4768. sysctl_sched_min_granularity = limit;
  4769. sysctl_sched_latency *= factor;
  4770. if (sysctl_sched_latency > limit)
  4771. sysctl_sched_latency = limit;
  4772. sysctl_sched_wakeup_granularity *= factor;
  4773. }
  4774. #ifdef CONFIG_SMP
  4775. /*
  4776. * This is how migration works:
  4777. *
  4778. * 1) we queue a struct migration_req structure in the source CPU's
  4779. * runqueue and wake up that CPU's migration thread.
  4780. * 2) we down() the locked semaphore => thread blocks.
  4781. * 3) migration thread wakes up (implicitly it forces the migrated
  4782. * thread off the CPU)
  4783. * 4) it gets the migration request and checks whether the migrated
  4784. * task is still in the wrong runqueue.
  4785. * 5) if it's in the wrong runqueue then the migration thread removes
  4786. * it and puts it into the right queue.
  4787. * 6) migration thread up()s the semaphore.
  4788. * 7) we wake up and the migration is done.
  4789. */
  4790. /*
  4791. * Change a given task's CPU affinity. Migrate the thread to a
  4792. * proper CPU and schedule it away if the CPU it's executing on
  4793. * is removed from the allowed bitmask.
  4794. *
  4795. * NOTE: the caller must have a valid reference to the task, the
  4796. * task must not exit() & deallocate itself prematurely. The
  4797. * call is not atomic; no spinlocks may be held.
  4798. */
  4799. int set_cpus_allowed_ptr(struct task_struct *p, const cpumask_t *new_mask)
  4800. {
  4801. struct migration_req req;
  4802. unsigned long flags;
  4803. struct rq *rq;
  4804. int ret = 0;
  4805. rq = task_rq_lock(p, &flags);
  4806. if (!cpus_intersects(*new_mask, cpu_online_map)) {
  4807. ret = -EINVAL;
  4808. goto out;
  4809. }
  4810. if (p->sched_class->set_cpus_allowed)
  4811. p->sched_class->set_cpus_allowed(p, new_mask);
  4812. else {
  4813. p->cpus_allowed = *new_mask;
  4814. p->rt.nr_cpus_allowed = cpus_weight(*new_mask);
  4815. }
  4816. /* Can the task run on the task's current CPU? If so, we're done */
  4817. if (cpu_isset(task_cpu(p), *new_mask))
  4818. goto out;
  4819. if (migrate_task(p, any_online_cpu(*new_mask), &req)) {
  4820. /* Need help from migration thread: drop lock and wait. */
  4821. task_rq_unlock(rq, &flags);
  4822. wake_up_process(rq->migration_thread);
  4823. wait_for_completion(&req.done);
  4824. tlb_migrate_finish(p->mm);
  4825. return 0;
  4826. }
  4827. out:
  4828. task_rq_unlock(rq, &flags);
  4829. return ret;
  4830. }
  4831. EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
  4832. /*
  4833. * Move (not current) task off this cpu, onto dest cpu. We're doing
  4834. * this because either it can't run here any more (set_cpus_allowed()
  4835. * away from this CPU, or CPU going down), or because we're
  4836. * attempting to rebalance this task on exec (sched_exec).
  4837. *
  4838. * So we race with normal scheduler movements, but that's OK, as long
  4839. * as the task is no longer on this CPU.
  4840. *
  4841. * Returns non-zero if task was successfully migrated.
  4842. */
  4843. static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
  4844. {
  4845. struct rq *rq_dest, *rq_src;
  4846. int ret = 0, on_rq;
  4847. if (unlikely(cpu_is_offline(dest_cpu)))
  4848. return ret;
  4849. rq_src = cpu_rq(src_cpu);
  4850. rq_dest = cpu_rq(dest_cpu);
  4851. double_rq_lock(rq_src, rq_dest);
  4852. /* Already moved. */
  4853. if (task_cpu(p) != src_cpu)
  4854. goto out;
  4855. /* Affinity changed (again). */
  4856. if (!cpu_isset(dest_cpu, p->cpus_allowed))
  4857. goto out;
  4858. on_rq = p->se.on_rq;
  4859. if (on_rq)
  4860. deactivate_task(rq_src, p, 0);
  4861. set_task_cpu(p, dest_cpu);
  4862. if (on_rq) {
  4863. activate_task(rq_dest, p, 0);
  4864. check_preempt_curr(rq_dest, p);
  4865. }
  4866. ret = 1;
  4867. out:
  4868. double_rq_unlock(rq_src, rq_dest);
  4869. return ret;
  4870. }
  4871. /*
  4872. * migration_thread - this is a highprio system thread that performs
  4873. * thread migration by bumping thread off CPU then 'pushing' onto
  4874. * another runqueue.
  4875. */
  4876. static int migration_thread(void *data)
  4877. {
  4878. int cpu = (long)data;
  4879. struct rq *rq;
  4880. rq = cpu_rq(cpu);
  4881. BUG_ON(rq->migration_thread != current);
  4882. set_current_state(TASK_INTERRUPTIBLE);
  4883. while (!kthread_should_stop()) {
  4884. struct migration_req *req;
  4885. struct list_head *head;
  4886. spin_lock_irq(&rq->lock);
  4887. if (cpu_is_offline(cpu)) {
  4888. spin_unlock_irq(&rq->lock);
  4889. goto wait_to_die;
  4890. }
  4891. if (rq->active_balance) {
  4892. active_load_balance(rq, cpu);
  4893. rq->active_balance = 0;
  4894. }
  4895. head = &rq->migration_queue;
  4896. if (list_empty(head)) {
  4897. spin_unlock_irq(&rq->lock);
  4898. schedule();
  4899. set_current_state(TASK_INTERRUPTIBLE);
  4900. continue;
  4901. }
  4902. req = list_entry(head->next, struct migration_req, list);
  4903. list_del_init(head->next);
  4904. spin_unlock(&rq->lock);
  4905. __migrate_task(req->task, cpu, req->dest_cpu);
  4906. local_irq_enable();
  4907. complete(&req->done);
  4908. }
  4909. __set_current_state(TASK_RUNNING);
  4910. return 0;
  4911. wait_to_die:
  4912. /* Wait for kthread_stop */
  4913. set_current_state(TASK_INTERRUPTIBLE);
  4914. while (!kthread_should_stop()) {
  4915. schedule();
  4916. set_current_state(TASK_INTERRUPTIBLE);
  4917. }
  4918. __set_current_state(TASK_RUNNING);
  4919. return 0;
  4920. }
  4921. #ifdef CONFIG_HOTPLUG_CPU
  4922. static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
  4923. {
  4924. int ret;
  4925. local_irq_disable();
  4926. ret = __migrate_task(p, src_cpu, dest_cpu);
  4927. local_irq_enable();
  4928. return ret;
  4929. }
  4930. /*
  4931. * Figure out where task on dead CPU should go, use force if necessary.
  4932. * NOTE: interrupts should be disabled by the caller
  4933. */
  4934. static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
  4935. {
  4936. unsigned long flags;
  4937. cpumask_t mask;
  4938. struct rq *rq;
  4939. int dest_cpu;
  4940. do {
  4941. /* On same node? */
  4942. mask = node_to_cpumask(cpu_to_node(dead_cpu));
  4943. cpus_and(mask, mask, p->cpus_allowed);
  4944. dest_cpu = any_online_cpu(mask);
  4945. /* On any allowed CPU? */
  4946. if (dest_cpu >= nr_cpu_ids)
  4947. dest_cpu = any_online_cpu(p->cpus_allowed);
  4948. /* No more Mr. Nice Guy. */
  4949. if (dest_cpu >= nr_cpu_ids) {
  4950. cpumask_t cpus_allowed;
  4951. cpuset_cpus_allowed_locked(p, &cpus_allowed);
  4952. /*
  4953. * Try to stay on the same cpuset, where the
  4954. * current cpuset may be a subset of all cpus.
  4955. * The cpuset_cpus_allowed_locked() variant of
  4956. * cpuset_cpus_allowed() will not block. It must be
  4957. * called within calls to cpuset_lock/cpuset_unlock.
  4958. */
  4959. rq = task_rq_lock(p, &flags);
  4960. p->cpus_allowed = cpus_allowed;
  4961. dest_cpu = any_online_cpu(p->cpus_allowed);
  4962. task_rq_unlock(rq, &flags);
  4963. /*
  4964. * Don't tell them about moving exiting tasks or
  4965. * kernel threads (both mm NULL), since they never
  4966. * leave kernel.
  4967. */
  4968. if (p->mm && printk_ratelimit()) {
  4969. printk(KERN_INFO "process %d (%s) no "
  4970. "longer affine to cpu%d\n",
  4971. task_pid_nr(p), p->comm, dead_cpu);
  4972. }
  4973. }
  4974. } while (!__migrate_task_irq(p, dead_cpu, dest_cpu));
  4975. }
  4976. /*
  4977. * While a dead CPU has no uninterruptible tasks queued at this point,
  4978. * it might still have a nonzero ->nr_uninterruptible counter, because
  4979. * for performance reasons the counter is not stricly tracking tasks to
  4980. * their home CPUs. So we just add the counter to another CPU's counter,
  4981. * to keep the global sum constant after CPU-down:
  4982. */
  4983. static void migrate_nr_uninterruptible(struct rq *rq_src)
  4984. {
  4985. struct rq *rq_dest = cpu_rq(any_online_cpu(*CPU_MASK_ALL_PTR));
  4986. unsigned long flags;
  4987. local_irq_save(flags);
  4988. double_rq_lock(rq_src, rq_dest);
  4989. rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
  4990. rq_src->nr_uninterruptible = 0;
  4991. double_rq_unlock(rq_src, rq_dest);
  4992. local_irq_restore(flags);
  4993. }
  4994. /* Run through task list and migrate tasks from the dead cpu. */
  4995. static void migrate_live_tasks(int src_cpu)
  4996. {
  4997. struct task_struct *p, *t;
  4998. read_lock(&tasklist_lock);
  4999. do_each_thread(t, p) {
  5000. if (p == current)
  5001. continue;
  5002. if (task_cpu(p) == src_cpu)
  5003. move_task_off_dead_cpu(src_cpu, p);
  5004. } while_each_thread(t, p);
  5005. read_unlock(&tasklist_lock);
  5006. }
  5007. /*
  5008. * Schedules idle task to be the next runnable task on current CPU.
  5009. * It does so by boosting its priority to highest possible.
  5010. * Used by CPU offline code.
  5011. */
  5012. void sched_idle_next(void)
  5013. {
  5014. int this_cpu = smp_processor_id();
  5015. struct rq *rq = cpu_rq(this_cpu);
  5016. struct task_struct *p = rq->idle;
  5017. unsigned long flags;
  5018. /* cpu has to be offline */
  5019. BUG_ON(cpu_online(this_cpu));
  5020. /*
  5021. * Strictly not necessary since rest of the CPUs are stopped by now
  5022. * and interrupts disabled on the current cpu.
  5023. */
  5024. spin_lock_irqsave(&rq->lock, flags);
  5025. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  5026. update_rq_clock(rq);
  5027. activate_task(rq, p, 0);
  5028. spin_unlock_irqrestore(&rq->lock, flags);
  5029. }
  5030. /*
  5031. * Ensures that the idle task is using init_mm right before its cpu goes
  5032. * offline.
  5033. */
  5034. void idle_task_exit(void)
  5035. {
  5036. struct mm_struct *mm = current->active_mm;
  5037. BUG_ON(cpu_online(smp_processor_id()));
  5038. if (mm != &init_mm)
  5039. switch_mm(mm, &init_mm, current);
  5040. mmdrop(mm);
  5041. }
  5042. /* called under rq->lock with disabled interrupts */
  5043. static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
  5044. {
  5045. struct rq *rq = cpu_rq(dead_cpu);
  5046. /* Must be exiting, otherwise would be on tasklist. */
  5047. BUG_ON(!p->exit_state);
  5048. /* Cannot have done final schedule yet: would have vanished. */
  5049. BUG_ON(p->state == TASK_DEAD);
  5050. get_task_struct(p);
  5051. /*
  5052. * Drop lock around migration; if someone else moves it,
  5053. * that's OK. No task can be added to this CPU, so iteration is
  5054. * fine.
  5055. */
  5056. spin_unlock_irq(&rq->lock);
  5057. move_task_off_dead_cpu(dead_cpu, p);
  5058. spin_lock_irq(&rq->lock);
  5059. put_task_struct(p);
  5060. }
  5061. /* release_task() removes task from tasklist, so we won't find dead tasks. */
  5062. static void migrate_dead_tasks(unsigned int dead_cpu)
  5063. {
  5064. struct rq *rq = cpu_rq(dead_cpu);
  5065. struct task_struct *next;
  5066. for ( ; ; ) {
  5067. if (!rq->nr_running)
  5068. break;
  5069. update_rq_clock(rq);
  5070. next = pick_next_task(rq, rq->curr);
  5071. if (!next)
  5072. break;
  5073. migrate_dead(dead_cpu, next);
  5074. }
  5075. }
  5076. #endif /* CONFIG_HOTPLUG_CPU */
  5077. #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
  5078. static struct ctl_table sd_ctl_dir[] = {
  5079. {
  5080. .procname = "sched_domain",
  5081. .mode = 0555,
  5082. },
  5083. {0, },
  5084. };
  5085. static struct ctl_table sd_ctl_root[] = {
  5086. {
  5087. .ctl_name = CTL_KERN,
  5088. .procname = "kernel",
  5089. .mode = 0555,
  5090. .child = sd_ctl_dir,
  5091. },
  5092. {0, },
  5093. };
  5094. static struct ctl_table *sd_alloc_ctl_entry(int n)
  5095. {
  5096. struct ctl_table *entry =
  5097. kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
  5098. return entry;
  5099. }
  5100. static void sd_free_ctl_entry(struct ctl_table **tablep)
  5101. {
  5102. struct ctl_table *entry;
  5103. /*
  5104. * In the intermediate directories, both the child directory and
  5105. * procname are dynamically allocated and could fail but the mode
  5106. * will always be set. In the lowest directory the names are
  5107. * static strings and all have proc handlers.
  5108. */
  5109. for (entry = *tablep; entry->mode; entry++) {
  5110. if (entry->child)
  5111. sd_free_ctl_entry(&entry->child);
  5112. if (entry->proc_handler == NULL)
  5113. kfree(entry->procname);
  5114. }
  5115. kfree(*tablep);
  5116. *tablep = NULL;
  5117. }
  5118. static void
  5119. set_table_entry(struct ctl_table *entry,
  5120. const char *procname, void *data, int maxlen,
  5121. mode_t mode, proc_handler *proc_handler)
  5122. {
  5123. entry->procname = procname;
  5124. entry->data = data;
  5125. entry->maxlen = maxlen;
  5126. entry->mode = mode;
  5127. entry->proc_handler = proc_handler;
  5128. }
  5129. static struct ctl_table *
  5130. sd_alloc_ctl_domain_table(struct sched_domain *sd)
  5131. {
  5132. struct ctl_table *table = sd_alloc_ctl_entry(12);
  5133. if (table == NULL)
  5134. return NULL;
  5135. set_table_entry(&table[0], "min_interval", &sd->min_interval,
  5136. sizeof(long), 0644, proc_doulongvec_minmax);
  5137. set_table_entry(&table[1], "max_interval", &sd->max_interval,
  5138. sizeof(long), 0644, proc_doulongvec_minmax);
  5139. set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
  5140. sizeof(int), 0644, proc_dointvec_minmax);
  5141. set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
  5142. sizeof(int), 0644, proc_dointvec_minmax);
  5143. set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
  5144. sizeof(int), 0644, proc_dointvec_minmax);
  5145. set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
  5146. sizeof(int), 0644, proc_dointvec_minmax);
  5147. set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
  5148. sizeof(int), 0644, proc_dointvec_minmax);
  5149. set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
  5150. sizeof(int), 0644, proc_dointvec_minmax);
  5151. set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
  5152. sizeof(int), 0644, proc_dointvec_minmax);
  5153. set_table_entry(&table[9], "cache_nice_tries",
  5154. &sd->cache_nice_tries,
  5155. sizeof(int), 0644, proc_dointvec_minmax);
  5156. set_table_entry(&table[10], "flags", &sd->flags,
  5157. sizeof(int), 0644, proc_dointvec_minmax);
  5158. /* &table[11] is terminator */
  5159. return table;
  5160. }
  5161. static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
  5162. {
  5163. struct ctl_table *entry, *table;
  5164. struct sched_domain *sd;
  5165. int domain_num = 0, i;
  5166. char buf[32];
  5167. for_each_domain(cpu, sd)
  5168. domain_num++;
  5169. entry = table = sd_alloc_ctl_entry(domain_num + 1);
  5170. if (table == NULL)
  5171. return NULL;
  5172. i = 0;
  5173. for_each_domain(cpu, sd) {
  5174. snprintf(buf, 32, "domain%d", i);
  5175. entry->procname = kstrdup(buf, GFP_KERNEL);
  5176. entry->mode = 0555;
  5177. entry->child = sd_alloc_ctl_domain_table(sd);
  5178. entry++;
  5179. i++;
  5180. }
  5181. return table;
  5182. }
  5183. static struct ctl_table_header *sd_sysctl_header;
  5184. static void register_sched_domain_sysctl(void)
  5185. {
  5186. int i, cpu_num = num_online_cpus();
  5187. struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
  5188. char buf[32];
  5189. WARN_ON(sd_ctl_dir[0].child);
  5190. sd_ctl_dir[0].child = entry;
  5191. if (entry == NULL)
  5192. return;
  5193. for_each_online_cpu(i) {
  5194. snprintf(buf, 32, "cpu%d", i);
  5195. entry->procname = kstrdup(buf, GFP_KERNEL);
  5196. entry->mode = 0555;
  5197. entry->child = sd_alloc_ctl_cpu_table(i);
  5198. entry++;
  5199. }
  5200. WARN_ON(sd_sysctl_header);
  5201. sd_sysctl_header = register_sysctl_table(sd_ctl_root);
  5202. }
  5203. /* may be called multiple times per register */
  5204. static void unregister_sched_domain_sysctl(void)
  5205. {
  5206. if (sd_sysctl_header)
  5207. unregister_sysctl_table(sd_sysctl_header);
  5208. sd_sysctl_header = NULL;
  5209. if (sd_ctl_dir[0].child)
  5210. sd_free_ctl_entry(&sd_ctl_dir[0].child);
  5211. }
  5212. #else
  5213. static void register_sched_domain_sysctl(void)
  5214. {
  5215. }
  5216. static void unregister_sched_domain_sysctl(void)
  5217. {
  5218. }
  5219. #endif
  5220. /*
  5221. * migration_call - callback that gets triggered when a CPU is added.
  5222. * Here we can start up the necessary migration thread for the new CPU.
  5223. */
  5224. static int __cpuinit
  5225. migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  5226. {
  5227. struct task_struct *p;
  5228. int cpu = (long)hcpu;
  5229. unsigned long flags;
  5230. struct rq *rq;
  5231. switch (action) {
  5232. case CPU_UP_PREPARE:
  5233. case CPU_UP_PREPARE_FROZEN:
  5234. p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
  5235. if (IS_ERR(p))
  5236. return NOTIFY_BAD;
  5237. kthread_bind(p, cpu);
  5238. /* Must be high prio: stop_machine expects to yield to it. */
  5239. rq = task_rq_lock(p, &flags);
  5240. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  5241. task_rq_unlock(rq, &flags);
  5242. cpu_rq(cpu)->migration_thread = p;
  5243. break;
  5244. case CPU_ONLINE:
  5245. case CPU_ONLINE_FROZEN:
  5246. /* Strictly unnecessary, as first user will wake it. */
  5247. wake_up_process(cpu_rq(cpu)->migration_thread);
  5248. /* Update our root-domain */
  5249. rq = cpu_rq(cpu);
  5250. spin_lock_irqsave(&rq->lock, flags);
  5251. if (rq->rd) {
  5252. BUG_ON(!cpu_isset(cpu, rq->rd->span));
  5253. cpu_set(cpu, rq->rd->online);
  5254. }
  5255. spin_unlock_irqrestore(&rq->lock, flags);
  5256. break;
  5257. #ifdef CONFIG_HOTPLUG_CPU
  5258. case CPU_UP_CANCELED:
  5259. case CPU_UP_CANCELED_FROZEN:
  5260. if (!cpu_rq(cpu)->migration_thread)
  5261. break;
  5262. /* Unbind it from offline cpu so it can run. Fall thru. */
  5263. kthread_bind(cpu_rq(cpu)->migration_thread,
  5264. any_online_cpu(cpu_online_map));
  5265. kthread_stop(cpu_rq(cpu)->migration_thread);
  5266. cpu_rq(cpu)->migration_thread = NULL;
  5267. break;
  5268. case CPU_DEAD:
  5269. case CPU_DEAD_FROZEN:
  5270. cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
  5271. migrate_live_tasks(cpu);
  5272. rq = cpu_rq(cpu);
  5273. kthread_stop(rq->migration_thread);
  5274. rq->migration_thread = NULL;
  5275. /* Idle task back to normal (off runqueue, low prio) */
  5276. spin_lock_irq(&rq->lock);
  5277. update_rq_clock(rq);
  5278. deactivate_task(rq, rq->idle, 0);
  5279. rq->idle->static_prio = MAX_PRIO;
  5280. __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
  5281. rq->idle->sched_class = &idle_sched_class;
  5282. migrate_dead_tasks(cpu);
  5283. spin_unlock_irq(&rq->lock);
  5284. cpuset_unlock();
  5285. migrate_nr_uninterruptible(rq);
  5286. BUG_ON(rq->nr_running != 0);
  5287. /*
  5288. * No need to migrate the tasks: it was best-effort if
  5289. * they didn't take sched_hotcpu_mutex. Just wake up
  5290. * the requestors.
  5291. */
  5292. spin_lock_irq(&rq->lock);
  5293. while (!list_empty(&rq->migration_queue)) {
  5294. struct migration_req *req;
  5295. req = list_entry(rq->migration_queue.next,
  5296. struct migration_req, list);
  5297. list_del_init(&req->list);
  5298. complete(&req->done);
  5299. }
  5300. spin_unlock_irq(&rq->lock);
  5301. break;
  5302. case CPU_DYING:
  5303. case CPU_DYING_FROZEN:
  5304. /* Update our root-domain */
  5305. rq = cpu_rq(cpu);
  5306. spin_lock_irqsave(&rq->lock, flags);
  5307. if (rq->rd) {
  5308. BUG_ON(!cpu_isset(cpu, rq->rd->span));
  5309. cpu_clear(cpu, rq->rd->online);
  5310. }
  5311. spin_unlock_irqrestore(&rq->lock, flags);
  5312. break;
  5313. #endif
  5314. }
  5315. return NOTIFY_OK;
  5316. }
  5317. /* Register at highest priority so that task migration (migrate_all_tasks)
  5318. * happens before everything else.
  5319. */
  5320. static struct notifier_block __cpuinitdata migration_notifier = {
  5321. .notifier_call = migration_call,
  5322. .priority = 10
  5323. };
  5324. void __init migration_init(void)
  5325. {
  5326. void *cpu = (void *)(long)smp_processor_id();
  5327. int err;
  5328. /* Start one for the boot CPU: */
  5329. err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  5330. BUG_ON(err == NOTIFY_BAD);
  5331. migration_call(&migration_notifier, CPU_ONLINE, cpu);
  5332. register_cpu_notifier(&migration_notifier);
  5333. }
  5334. #endif
  5335. #ifdef CONFIG_SMP
  5336. #ifdef CONFIG_SCHED_DEBUG
  5337. static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
  5338. cpumask_t *groupmask)
  5339. {
  5340. struct sched_group *group = sd->groups;
  5341. char str[256];
  5342. cpulist_scnprintf(str, sizeof(str), sd->span);
  5343. cpus_clear(*groupmask);
  5344. printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
  5345. if (!(sd->flags & SD_LOAD_BALANCE)) {
  5346. printk("does not load-balance\n");
  5347. if (sd->parent)
  5348. printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
  5349. " has parent");
  5350. return -1;
  5351. }
  5352. printk(KERN_CONT "span %s\n", str);
  5353. if (!cpu_isset(cpu, sd->span)) {
  5354. printk(KERN_ERR "ERROR: domain->span does not contain "
  5355. "CPU%d\n", cpu);
  5356. }
  5357. if (!cpu_isset(cpu, group->cpumask)) {
  5358. printk(KERN_ERR "ERROR: domain->groups does not contain"
  5359. " CPU%d\n", cpu);
  5360. }
  5361. printk(KERN_DEBUG "%*s groups:", level + 1, "");
  5362. do {
  5363. if (!group) {
  5364. printk("\n");
  5365. printk(KERN_ERR "ERROR: group is NULL\n");
  5366. break;
  5367. }
  5368. if (!group->__cpu_power) {
  5369. printk(KERN_CONT "\n");
  5370. printk(KERN_ERR "ERROR: domain->cpu_power not "
  5371. "set\n");
  5372. break;
  5373. }
  5374. if (!cpus_weight(group->cpumask)) {
  5375. printk(KERN_CONT "\n");
  5376. printk(KERN_ERR "ERROR: empty group\n");
  5377. break;
  5378. }
  5379. if (cpus_intersects(*groupmask, group->cpumask)) {
  5380. printk(KERN_CONT "\n");
  5381. printk(KERN_ERR "ERROR: repeated CPUs\n");
  5382. break;
  5383. }
  5384. cpus_or(*groupmask, *groupmask, group->cpumask);
  5385. cpulist_scnprintf(str, sizeof(str), group->cpumask);
  5386. printk(KERN_CONT " %s", str);
  5387. group = group->next;
  5388. } while (group != sd->groups);
  5389. printk(KERN_CONT "\n");
  5390. if (!cpus_equal(sd->span, *groupmask))
  5391. printk(KERN_ERR "ERROR: groups don't span domain->span\n");
  5392. if (sd->parent && !cpus_subset(*groupmask, sd->parent->span))
  5393. printk(KERN_ERR "ERROR: parent span is not a superset "
  5394. "of domain->span\n");
  5395. return 0;
  5396. }
  5397. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  5398. {
  5399. cpumask_t *groupmask;
  5400. int level = 0;
  5401. if (!sd) {
  5402. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  5403. return;
  5404. }
  5405. printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  5406. groupmask = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
  5407. if (!groupmask) {
  5408. printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
  5409. return;
  5410. }
  5411. for (;;) {
  5412. if (sched_domain_debug_one(sd, cpu, level, groupmask))
  5413. break;
  5414. level++;
  5415. sd = sd->parent;
  5416. if (!sd)
  5417. break;
  5418. }
  5419. kfree(groupmask);
  5420. }
  5421. #else
  5422. # define sched_domain_debug(sd, cpu) do { } while (0)
  5423. #endif
  5424. static int sd_degenerate(struct sched_domain *sd)
  5425. {
  5426. if (cpus_weight(sd->span) == 1)
  5427. return 1;
  5428. /* Following flags need at least 2 groups */
  5429. if (sd->flags & (SD_LOAD_BALANCE |
  5430. SD_BALANCE_NEWIDLE |
  5431. SD_BALANCE_FORK |
  5432. SD_BALANCE_EXEC |
  5433. SD_SHARE_CPUPOWER |
  5434. SD_SHARE_PKG_RESOURCES)) {
  5435. if (sd->groups != sd->groups->next)
  5436. return 0;
  5437. }
  5438. /* Following flags don't use groups */
  5439. if (sd->flags & (SD_WAKE_IDLE |
  5440. SD_WAKE_AFFINE |
  5441. SD_WAKE_BALANCE))
  5442. return 0;
  5443. return 1;
  5444. }
  5445. static int
  5446. sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  5447. {
  5448. unsigned long cflags = sd->flags, pflags = parent->flags;
  5449. if (sd_degenerate(parent))
  5450. return 1;
  5451. if (!cpus_equal(sd->span, parent->span))
  5452. return 0;
  5453. /* Does parent contain flags not in child? */
  5454. /* WAKE_BALANCE is a subset of WAKE_AFFINE */
  5455. if (cflags & SD_WAKE_AFFINE)
  5456. pflags &= ~SD_WAKE_BALANCE;
  5457. /* Flags needing groups don't count if only 1 group in parent */
  5458. if (parent->groups == parent->groups->next) {
  5459. pflags &= ~(SD_LOAD_BALANCE |
  5460. SD_BALANCE_NEWIDLE |
  5461. SD_BALANCE_FORK |
  5462. SD_BALANCE_EXEC |
  5463. SD_SHARE_CPUPOWER |
  5464. SD_SHARE_PKG_RESOURCES);
  5465. }
  5466. if (~cflags & pflags)
  5467. return 0;
  5468. return 1;
  5469. }
  5470. static void rq_attach_root(struct rq *rq, struct root_domain *rd)
  5471. {
  5472. unsigned long flags;
  5473. const struct sched_class *class;
  5474. spin_lock_irqsave(&rq->lock, flags);
  5475. if (rq->rd) {
  5476. struct root_domain *old_rd = rq->rd;
  5477. for (class = sched_class_highest; class; class = class->next) {
  5478. if (class->leave_domain)
  5479. class->leave_domain(rq);
  5480. }
  5481. cpu_clear(rq->cpu, old_rd->span);
  5482. cpu_clear(rq->cpu, old_rd->online);
  5483. if (atomic_dec_and_test(&old_rd->refcount))
  5484. kfree(old_rd);
  5485. }
  5486. atomic_inc(&rd->refcount);
  5487. rq->rd = rd;
  5488. cpu_set(rq->cpu, rd->span);
  5489. if (cpu_isset(rq->cpu, cpu_online_map))
  5490. cpu_set(rq->cpu, rd->online);
  5491. for (class = sched_class_highest; class; class = class->next) {
  5492. if (class->join_domain)
  5493. class->join_domain(rq);
  5494. }
  5495. spin_unlock_irqrestore(&rq->lock, flags);
  5496. }
  5497. static void init_rootdomain(struct root_domain *rd)
  5498. {
  5499. memset(rd, 0, sizeof(*rd));
  5500. cpus_clear(rd->span);
  5501. cpus_clear(rd->online);
  5502. }
  5503. static void init_defrootdomain(void)
  5504. {
  5505. init_rootdomain(&def_root_domain);
  5506. atomic_set(&def_root_domain.refcount, 1);
  5507. }
  5508. static struct root_domain *alloc_rootdomain(void)
  5509. {
  5510. struct root_domain *rd;
  5511. rd = kmalloc(sizeof(*rd), GFP_KERNEL);
  5512. if (!rd)
  5513. return NULL;
  5514. init_rootdomain(rd);
  5515. return rd;
  5516. }
  5517. /*
  5518. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  5519. * hold the hotplug lock.
  5520. */
  5521. static void
  5522. cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
  5523. {
  5524. struct rq *rq = cpu_rq(cpu);
  5525. struct sched_domain *tmp;
  5526. /* Remove the sched domains which do not contribute to scheduling. */
  5527. for (tmp = sd; tmp; tmp = tmp->parent) {
  5528. struct sched_domain *parent = tmp->parent;
  5529. if (!parent)
  5530. break;
  5531. if (sd_parent_degenerate(tmp, parent)) {
  5532. tmp->parent = parent->parent;
  5533. if (parent->parent)
  5534. parent->parent->child = tmp;
  5535. }
  5536. }
  5537. if (sd && sd_degenerate(sd)) {
  5538. sd = sd->parent;
  5539. if (sd)
  5540. sd->child = NULL;
  5541. }
  5542. sched_domain_debug(sd, cpu);
  5543. rq_attach_root(rq, rd);
  5544. rcu_assign_pointer(rq->sd, sd);
  5545. }
  5546. /* cpus with isolated domains */
  5547. static cpumask_t cpu_isolated_map = CPU_MASK_NONE;
  5548. /* Setup the mask of cpus configured for isolated domains */
  5549. static int __init isolated_cpu_setup(char *str)
  5550. {
  5551. int ints[NR_CPUS], i;
  5552. str = get_options(str, ARRAY_SIZE(ints), ints);
  5553. cpus_clear(cpu_isolated_map);
  5554. for (i = 1; i <= ints[0]; i++)
  5555. if (ints[i] < NR_CPUS)
  5556. cpu_set(ints[i], cpu_isolated_map);
  5557. return 1;
  5558. }
  5559. __setup("isolcpus=", isolated_cpu_setup);
  5560. /*
  5561. * init_sched_build_groups takes the cpumask we wish to span, and a pointer
  5562. * to a function which identifies what group(along with sched group) a CPU
  5563. * belongs to. The return value of group_fn must be a >= 0 and < NR_CPUS
  5564. * (due to the fact that we keep track of groups covered with a cpumask_t).
  5565. *
  5566. * init_sched_build_groups will build a circular linked list of the groups
  5567. * covered by the given span, and will set each group's ->cpumask correctly,
  5568. * and ->cpu_power to 0.
  5569. */
  5570. static void
  5571. init_sched_build_groups(const cpumask_t *span, const cpumask_t *cpu_map,
  5572. int (*group_fn)(int cpu, const cpumask_t *cpu_map,
  5573. struct sched_group **sg,
  5574. cpumask_t *tmpmask),
  5575. cpumask_t *covered, cpumask_t *tmpmask)
  5576. {
  5577. struct sched_group *first = NULL, *last = NULL;
  5578. int i;
  5579. cpus_clear(*covered);
  5580. for_each_cpu_mask(i, *span) {
  5581. struct sched_group *sg;
  5582. int group = group_fn(i, cpu_map, &sg, tmpmask);
  5583. int j;
  5584. if (cpu_isset(i, *covered))
  5585. continue;
  5586. cpus_clear(sg->cpumask);
  5587. sg->__cpu_power = 0;
  5588. for_each_cpu_mask(j, *span) {
  5589. if (group_fn(j, cpu_map, NULL, tmpmask) != group)
  5590. continue;
  5591. cpu_set(j, *covered);
  5592. cpu_set(j, sg->cpumask);
  5593. }
  5594. if (!first)
  5595. first = sg;
  5596. if (last)
  5597. last->next = sg;
  5598. last = sg;
  5599. }
  5600. last->next = first;
  5601. }
  5602. #define SD_NODES_PER_DOMAIN 16
  5603. #ifdef CONFIG_NUMA
  5604. /**
  5605. * find_next_best_node - find the next node to include in a sched_domain
  5606. * @node: node whose sched_domain we're building
  5607. * @used_nodes: nodes already in the sched_domain
  5608. *
  5609. * Find the next node to include in a given scheduling domain. Simply
  5610. * finds the closest node not already in the @used_nodes map.
  5611. *
  5612. * Should use nodemask_t.
  5613. */
  5614. static int find_next_best_node(int node, nodemask_t *used_nodes)
  5615. {
  5616. int i, n, val, min_val, best_node = 0;
  5617. min_val = INT_MAX;
  5618. for (i = 0; i < MAX_NUMNODES; i++) {
  5619. /* Start at @node */
  5620. n = (node + i) % MAX_NUMNODES;
  5621. if (!nr_cpus_node(n))
  5622. continue;
  5623. /* Skip already used nodes */
  5624. if (node_isset(n, *used_nodes))
  5625. continue;
  5626. /* Simple min distance search */
  5627. val = node_distance(node, n);
  5628. if (val < min_val) {
  5629. min_val = val;
  5630. best_node = n;
  5631. }
  5632. }
  5633. node_set(best_node, *used_nodes);
  5634. return best_node;
  5635. }
  5636. /**
  5637. * sched_domain_node_span - get a cpumask for a node's sched_domain
  5638. * @node: node whose cpumask we're constructing
  5639. * @span: resulting cpumask
  5640. *
  5641. * Given a node, construct a good cpumask for its sched_domain to span. It
  5642. * should be one that prevents unnecessary balancing, but also spreads tasks
  5643. * out optimally.
  5644. */
  5645. static void sched_domain_node_span(int node, cpumask_t *span)
  5646. {
  5647. nodemask_t used_nodes;
  5648. node_to_cpumask_ptr(nodemask, node);
  5649. int i;
  5650. cpus_clear(*span);
  5651. nodes_clear(used_nodes);
  5652. cpus_or(*span, *span, *nodemask);
  5653. node_set(node, used_nodes);
  5654. for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
  5655. int next_node = find_next_best_node(node, &used_nodes);
  5656. node_to_cpumask_ptr_next(nodemask, next_node);
  5657. cpus_or(*span, *span, *nodemask);
  5658. }
  5659. }
  5660. #endif
  5661. int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
  5662. /*
  5663. * SMT sched-domains:
  5664. */
  5665. #ifdef CONFIG_SCHED_SMT
  5666. static DEFINE_PER_CPU(struct sched_domain, cpu_domains);
  5667. static DEFINE_PER_CPU(struct sched_group, sched_group_cpus);
  5668. static int
  5669. cpu_to_cpu_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
  5670. cpumask_t *unused)
  5671. {
  5672. if (sg)
  5673. *sg = &per_cpu(sched_group_cpus, cpu);
  5674. return cpu;
  5675. }
  5676. #endif
  5677. /*
  5678. * multi-core sched-domains:
  5679. */
  5680. #ifdef CONFIG_SCHED_MC
  5681. static DEFINE_PER_CPU(struct sched_domain, core_domains);
  5682. static DEFINE_PER_CPU(struct sched_group, sched_group_core);
  5683. #endif
  5684. #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
  5685. static int
  5686. cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
  5687. cpumask_t *mask)
  5688. {
  5689. int group;
  5690. *mask = per_cpu(cpu_sibling_map, cpu);
  5691. cpus_and(*mask, *mask, *cpu_map);
  5692. group = first_cpu(*mask);
  5693. if (sg)
  5694. *sg = &per_cpu(sched_group_core, group);
  5695. return group;
  5696. }
  5697. #elif defined(CONFIG_SCHED_MC)
  5698. static int
  5699. cpu_to_core_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
  5700. cpumask_t *unused)
  5701. {
  5702. if (sg)
  5703. *sg = &per_cpu(sched_group_core, cpu);
  5704. return cpu;
  5705. }
  5706. #endif
  5707. static DEFINE_PER_CPU(struct sched_domain, phys_domains);
  5708. static DEFINE_PER_CPU(struct sched_group, sched_group_phys);
  5709. static int
  5710. cpu_to_phys_group(int cpu, const cpumask_t *cpu_map, struct sched_group **sg,
  5711. cpumask_t *mask)
  5712. {
  5713. int group;
  5714. #ifdef CONFIG_SCHED_MC
  5715. *mask = cpu_coregroup_map(cpu);
  5716. cpus_and(*mask, *mask, *cpu_map);
  5717. group = first_cpu(*mask);
  5718. #elif defined(CONFIG_SCHED_SMT)
  5719. *mask = per_cpu(cpu_sibling_map, cpu);
  5720. cpus_and(*mask, *mask, *cpu_map);
  5721. group = first_cpu(*mask);
  5722. #else
  5723. group = cpu;
  5724. #endif
  5725. if (sg)
  5726. *sg = &per_cpu(sched_group_phys, group);
  5727. return group;
  5728. }
  5729. #ifdef CONFIG_NUMA
  5730. /*
  5731. * The init_sched_build_groups can't handle what we want to do with node
  5732. * groups, so roll our own. Now each node has its own list of groups which
  5733. * gets dynamically allocated.
  5734. */
  5735. static DEFINE_PER_CPU(struct sched_domain, node_domains);
  5736. static struct sched_group ***sched_group_nodes_bycpu;
  5737. static DEFINE_PER_CPU(struct sched_domain, allnodes_domains);
  5738. static DEFINE_PER_CPU(struct sched_group, sched_group_allnodes);
  5739. static int cpu_to_allnodes_group(int cpu, const cpumask_t *cpu_map,
  5740. struct sched_group **sg, cpumask_t *nodemask)
  5741. {
  5742. int group;
  5743. *nodemask = node_to_cpumask(cpu_to_node(cpu));
  5744. cpus_and(*nodemask, *nodemask, *cpu_map);
  5745. group = first_cpu(*nodemask);
  5746. if (sg)
  5747. *sg = &per_cpu(sched_group_allnodes, group);
  5748. return group;
  5749. }
  5750. static void init_numa_sched_groups_power(struct sched_group *group_head)
  5751. {
  5752. struct sched_group *sg = group_head;
  5753. int j;
  5754. if (!sg)
  5755. return;
  5756. do {
  5757. for_each_cpu_mask(j, sg->cpumask) {
  5758. struct sched_domain *sd;
  5759. sd = &per_cpu(phys_domains, j);
  5760. if (j != first_cpu(sd->groups->cpumask)) {
  5761. /*
  5762. * Only add "power" once for each
  5763. * physical package.
  5764. */
  5765. continue;
  5766. }
  5767. sg_inc_cpu_power(sg, sd->groups->__cpu_power);
  5768. }
  5769. sg = sg->next;
  5770. } while (sg != group_head);
  5771. }
  5772. #endif
  5773. #ifdef CONFIG_NUMA
  5774. /* Free memory allocated for various sched_group structures */
  5775. static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
  5776. {
  5777. int cpu, i;
  5778. for_each_cpu_mask(cpu, *cpu_map) {
  5779. struct sched_group **sched_group_nodes
  5780. = sched_group_nodes_bycpu[cpu];
  5781. if (!sched_group_nodes)
  5782. continue;
  5783. for (i = 0; i < MAX_NUMNODES; i++) {
  5784. struct sched_group *oldsg, *sg = sched_group_nodes[i];
  5785. *nodemask = node_to_cpumask(i);
  5786. cpus_and(*nodemask, *nodemask, *cpu_map);
  5787. if (cpus_empty(*nodemask))
  5788. continue;
  5789. if (sg == NULL)
  5790. continue;
  5791. sg = sg->next;
  5792. next_sg:
  5793. oldsg = sg;
  5794. sg = sg->next;
  5795. kfree(oldsg);
  5796. if (oldsg != sched_group_nodes[i])
  5797. goto next_sg;
  5798. }
  5799. kfree(sched_group_nodes);
  5800. sched_group_nodes_bycpu[cpu] = NULL;
  5801. }
  5802. }
  5803. #else
  5804. static void free_sched_groups(const cpumask_t *cpu_map, cpumask_t *nodemask)
  5805. {
  5806. }
  5807. #endif
  5808. /*
  5809. * Initialize sched groups cpu_power.
  5810. *
  5811. * cpu_power indicates the capacity of sched group, which is used while
  5812. * distributing the load between different sched groups in a sched domain.
  5813. * Typically cpu_power for all the groups in a sched domain will be same unless
  5814. * there are asymmetries in the topology. If there are asymmetries, group
  5815. * having more cpu_power will pickup more load compared to the group having
  5816. * less cpu_power.
  5817. *
  5818. * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
  5819. * the maximum number of tasks a group can handle in the presence of other idle
  5820. * or lightly loaded groups in the same sched domain.
  5821. */
  5822. static void init_sched_groups_power(int cpu, struct sched_domain *sd)
  5823. {
  5824. struct sched_domain *child;
  5825. struct sched_group *group;
  5826. WARN_ON(!sd || !sd->groups);
  5827. if (cpu != first_cpu(sd->groups->cpumask))
  5828. return;
  5829. child = sd->child;
  5830. sd->groups->__cpu_power = 0;
  5831. /*
  5832. * For perf policy, if the groups in child domain share resources
  5833. * (for example cores sharing some portions of the cache hierarchy
  5834. * or SMT), then set this domain groups cpu_power such that each group
  5835. * can handle only one task, when there are other idle groups in the
  5836. * same sched domain.
  5837. */
  5838. if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
  5839. (child->flags &
  5840. (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
  5841. sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
  5842. return;
  5843. }
  5844. /*
  5845. * add cpu_power of each child group to this groups cpu_power
  5846. */
  5847. group = child->groups;
  5848. do {
  5849. sg_inc_cpu_power(sd->groups, group->__cpu_power);
  5850. group = group->next;
  5851. } while (group != child->groups);
  5852. }
  5853. /*
  5854. * Initializers for schedule domains
  5855. * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
  5856. */
  5857. #define SD_INIT(sd, type) sd_init_##type(sd)
  5858. #define SD_INIT_FUNC(type) \
  5859. static noinline void sd_init_##type(struct sched_domain *sd) \
  5860. { \
  5861. memset(sd, 0, sizeof(*sd)); \
  5862. *sd = SD_##type##_INIT; \
  5863. sd->level = SD_LV_##type; \
  5864. }
  5865. SD_INIT_FUNC(CPU)
  5866. #ifdef CONFIG_NUMA
  5867. SD_INIT_FUNC(ALLNODES)
  5868. SD_INIT_FUNC(NODE)
  5869. #endif
  5870. #ifdef CONFIG_SCHED_SMT
  5871. SD_INIT_FUNC(SIBLING)
  5872. #endif
  5873. #ifdef CONFIG_SCHED_MC
  5874. SD_INIT_FUNC(MC)
  5875. #endif
  5876. /*
  5877. * To minimize stack usage kmalloc room for cpumasks and share the
  5878. * space as the usage in build_sched_domains() dictates. Used only
  5879. * if the amount of space is significant.
  5880. */
  5881. struct allmasks {
  5882. cpumask_t tmpmask; /* make this one first */
  5883. union {
  5884. cpumask_t nodemask;
  5885. cpumask_t this_sibling_map;
  5886. cpumask_t this_core_map;
  5887. };
  5888. cpumask_t send_covered;
  5889. #ifdef CONFIG_NUMA
  5890. cpumask_t domainspan;
  5891. cpumask_t covered;
  5892. cpumask_t notcovered;
  5893. #endif
  5894. };
  5895. #if NR_CPUS > 128
  5896. #define SCHED_CPUMASK_ALLOC 1
  5897. #define SCHED_CPUMASK_FREE(v) kfree(v)
  5898. #define SCHED_CPUMASK_DECLARE(v) struct allmasks *v
  5899. #else
  5900. #define SCHED_CPUMASK_ALLOC 0
  5901. #define SCHED_CPUMASK_FREE(v)
  5902. #define SCHED_CPUMASK_DECLARE(v) struct allmasks _v, *v = &_v
  5903. #endif
  5904. #define SCHED_CPUMASK_VAR(v, a) cpumask_t *v = (cpumask_t *) \
  5905. ((unsigned long)(a) + offsetof(struct allmasks, v))
  5906. static int default_relax_domain_level = -1;
  5907. static int __init setup_relax_domain_level(char *str)
  5908. {
  5909. unsigned long val;
  5910. val = simple_strtoul(str, NULL, 0);
  5911. if (val < SD_LV_MAX)
  5912. default_relax_domain_level = val;
  5913. return 1;
  5914. }
  5915. __setup("relax_domain_level=", setup_relax_domain_level);
  5916. static void set_domain_attribute(struct sched_domain *sd,
  5917. struct sched_domain_attr *attr)
  5918. {
  5919. int request;
  5920. if (!attr || attr->relax_domain_level < 0) {
  5921. if (default_relax_domain_level < 0)
  5922. return;
  5923. else
  5924. request = default_relax_domain_level;
  5925. } else
  5926. request = attr->relax_domain_level;
  5927. if (request < sd->level) {
  5928. /* turn off idle balance on this domain */
  5929. sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
  5930. } else {
  5931. /* turn on idle balance on this domain */
  5932. sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
  5933. }
  5934. }
  5935. /*
  5936. * Build sched domains for a given set of cpus and attach the sched domains
  5937. * to the individual cpus
  5938. */
  5939. static int __build_sched_domains(const cpumask_t *cpu_map,
  5940. struct sched_domain_attr *attr)
  5941. {
  5942. int i;
  5943. struct root_domain *rd;
  5944. SCHED_CPUMASK_DECLARE(allmasks);
  5945. cpumask_t *tmpmask;
  5946. #ifdef CONFIG_NUMA
  5947. struct sched_group **sched_group_nodes = NULL;
  5948. int sd_allnodes = 0;
  5949. /*
  5950. * Allocate the per-node list of sched groups
  5951. */
  5952. sched_group_nodes = kcalloc(MAX_NUMNODES, sizeof(struct sched_group *),
  5953. GFP_KERNEL);
  5954. if (!sched_group_nodes) {
  5955. printk(KERN_WARNING "Can not alloc sched group node list\n");
  5956. return -ENOMEM;
  5957. }
  5958. #endif
  5959. rd = alloc_rootdomain();
  5960. if (!rd) {
  5961. printk(KERN_WARNING "Cannot alloc root domain\n");
  5962. #ifdef CONFIG_NUMA
  5963. kfree(sched_group_nodes);
  5964. #endif
  5965. return -ENOMEM;
  5966. }
  5967. #if SCHED_CPUMASK_ALLOC
  5968. /* get space for all scratch cpumask variables */
  5969. allmasks = kmalloc(sizeof(*allmasks), GFP_KERNEL);
  5970. if (!allmasks) {
  5971. printk(KERN_WARNING "Cannot alloc cpumask array\n");
  5972. kfree(rd);
  5973. #ifdef CONFIG_NUMA
  5974. kfree(sched_group_nodes);
  5975. #endif
  5976. return -ENOMEM;
  5977. }
  5978. #endif
  5979. tmpmask = (cpumask_t *)allmasks;
  5980. #ifdef CONFIG_NUMA
  5981. sched_group_nodes_bycpu[first_cpu(*cpu_map)] = sched_group_nodes;
  5982. #endif
  5983. /*
  5984. * Set up domains for cpus specified by the cpu_map.
  5985. */
  5986. for_each_cpu_mask(i, *cpu_map) {
  5987. struct sched_domain *sd = NULL, *p;
  5988. SCHED_CPUMASK_VAR(nodemask, allmasks);
  5989. *nodemask = node_to_cpumask(cpu_to_node(i));
  5990. cpus_and(*nodemask, *nodemask, *cpu_map);
  5991. #ifdef CONFIG_NUMA
  5992. if (cpus_weight(*cpu_map) >
  5993. SD_NODES_PER_DOMAIN*cpus_weight(*nodemask)) {
  5994. sd = &per_cpu(allnodes_domains, i);
  5995. SD_INIT(sd, ALLNODES);
  5996. set_domain_attribute(sd, attr);
  5997. sd->span = *cpu_map;
  5998. cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
  5999. p = sd;
  6000. sd_allnodes = 1;
  6001. } else
  6002. p = NULL;
  6003. sd = &per_cpu(node_domains, i);
  6004. SD_INIT(sd, NODE);
  6005. set_domain_attribute(sd, attr);
  6006. sched_domain_node_span(cpu_to_node(i), &sd->span);
  6007. sd->parent = p;
  6008. if (p)
  6009. p->child = sd;
  6010. cpus_and(sd->span, sd->span, *cpu_map);
  6011. #endif
  6012. p = sd;
  6013. sd = &per_cpu(phys_domains, i);
  6014. SD_INIT(sd, CPU);
  6015. set_domain_attribute(sd, attr);
  6016. sd->span = *nodemask;
  6017. sd->parent = p;
  6018. if (p)
  6019. p->child = sd;
  6020. cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
  6021. #ifdef CONFIG_SCHED_MC
  6022. p = sd;
  6023. sd = &per_cpu(core_domains, i);
  6024. SD_INIT(sd, MC);
  6025. set_domain_attribute(sd, attr);
  6026. sd->span = cpu_coregroup_map(i);
  6027. cpus_and(sd->span, sd->span, *cpu_map);
  6028. sd->parent = p;
  6029. p->child = sd;
  6030. cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
  6031. #endif
  6032. #ifdef CONFIG_SCHED_SMT
  6033. p = sd;
  6034. sd = &per_cpu(cpu_domains, i);
  6035. SD_INIT(sd, SIBLING);
  6036. set_domain_attribute(sd, attr);
  6037. sd->span = per_cpu(cpu_sibling_map, i);
  6038. cpus_and(sd->span, sd->span, *cpu_map);
  6039. sd->parent = p;
  6040. p->child = sd;
  6041. cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
  6042. #endif
  6043. }
  6044. #ifdef CONFIG_SCHED_SMT
  6045. /* Set up CPU (sibling) groups */
  6046. for_each_cpu_mask(i, *cpu_map) {
  6047. SCHED_CPUMASK_VAR(this_sibling_map, allmasks);
  6048. SCHED_CPUMASK_VAR(send_covered, allmasks);
  6049. *this_sibling_map = per_cpu(cpu_sibling_map, i);
  6050. cpus_and(*this_sibling_map, *this_sibling_map, *cpu_map);
  6051. if (i != first_cpu(*this_sibling_map))
  6052. continue;
  6053. init_sched_build_groups(this_sibling_map, cpu_map,
  6054. &cpu_to_cpu_group,
  6055. send_covered, tmpmask);
  6056. }
  6057. #endif
  6058. #ifdef CONFIG_SCHED_MC
  6059. /* Set up multi-core groups */
  6060. for_each_cpu_mask(i, *cpu_map) {
  6061. SCHED_CPUMASK_VAR(this_core_map, allmasks);
  6062. SCHED_CPUMASK_VAR(send_covered, allmasks);
  6063. *this_core_map = cpu_coregroup_map(i);
  6064. cpus_and(*this_core_map, *this_core_map, *cpu_map);
  6065. if (i != first_cpu(*this_core_map))
  6066. continue;
  6067. init_sched_build_groups(this_core_map, cpu_map,
  6068. &cpu_to_core_group,
  6069. send_covered, tmpmask);
  6070. }
  6071. #endif
  6072. /* Set up physical groups */
  6073. for (i = 0; i < MAX_NUMNODES; i++) {
  6074. SCHED_CPUMASK_VAR(nodemask, allmasks);
  6075. SCHED_CPUMASK_VAR(send_covered, allmasks);
  6076. *nodemask = node_to_cpumask(i);
  6077. cpus_and(*nodemask, *nodemask, *cpu_map);
  6078. if (cpus_empty(*nodemask))
  6079. continue;
  6080. init_sched_build_groups(nodemask, cpu_map,
  6081. &cpu_to_phys_group,
  6082. send_covered, tmpmask);
  6083. }
  6084. #ifdef CONFIG_NUMA
  6085. /* Set up node groups */
  6086. if (sd_allnodes) {
  6087. SCHED_CPUMASK_VAR(send_covered, allmasks);
  6088. init_sched_build_groups(cpu_map, cpu_map,
  6089. &cpu_to_allnodes_group,
  6090. send_covered, tmpmask);
  6091. }
  6092. for (i = 0; i < MAX_NUMNODES; i++) {
  6093. /* Set up node groups */
  6094. struct sched_group *sg, *prev;
  6095. SCHED_CPUMASK_VAR(nodemask, allmasks);
  6096. SCHED_CPUMASK_VAR(domainspan, allmasks);
  6097. SCHED_CPUMASK_VAR(covered, allmasks);
  6098. int j;
  6099. *nodemask = node_to_cpumask(i);
  6100. cpus_clear(*covered);
  6101. cpus_and(*nodemask, *nodemask, *cpu_map);
  6102. if (cpus_empty(*nodemask)) {
  6103. sched_group_nodes[i] = NULL;
  6104. continue;
  6105. }
  6106. sched_domain_node_span(i, domainspan);
  6107. cpus_and(*domainspan, *domainspan, *cpu_map);
  6108. sg = kmalloc_node(sizeof(struct sched_group), GFP_KERNEL, i);
  6109. if (!sg) {
  6110. printk(KERN_WARNING "Can not alloc domain group for "
  6111. "node %d\n", i);
  6112. goto error;
  6113. }
  6114. sched_group_nodes[i] = sg;
  6115. for_each_cpu_mask(j, *nodemask) {
  6116. struct sched_domain *sd;
  6117. sd = &per_cpu(node_domains, j);
  6118. sd->groups = sg;
  6119. }
  6120. sg->__cpu_power = 0;
  6121. sg->cpumask = *nodemask;
  6122. sg->next = sg;
  6123. cpus_or(*covered, *covered, *nodemask);
  6124. prev = sg;
  6125. for (j = 0; j < MAX_NUMNODES; j++) {
  6126. SCHED_CPUMASK_VAR(notcovered, allmasks);
  6127. int n = (i + j) % MAX_NUMNODES;
  6128. node_to_cpumask_ptr(pnodemask, n);
  6129. cpus_complement(*notcovered, *covered);
  6130. cpus_and(*tmpmask, *notcovered, *cpu_map);
  6131. cpus_and(*tmpmask, *tmpmask, *domainspan);
  6132. if (cpus_empty(*tmpmask))
  6133. break;
  6134. cpus_and(*tmpmask, *tmpmask, *pnodemask);
  6135. if (cpus_empty(*tmpmask))
  6136. continue;
  6137. sg = kmalloc_node(sizeof(struct sched_group),
  6138. GFP_KERNEL, i);
  6139. if (!sg) {
  6140. printk(KERN_WARNING
  6141. "Can not alloc domain group for node %d\n", j);
  6142. goto error;
  6143. }
  6144. sg->__cpu_power = 0;
  6145. sg->cpumask = *tmpmask;
  6146. sg->next = prev->next;
  6147. cpus_or(*covered, *covered, *tmpmask);
  6148. prev->next = sg;
  6149. prev = sg;
  6150. }
  6151. }
  6152. #endif
  6153. /* Calculate CPU power for physical packages and nodes */
  6154. #ifdef CONFIG_SCHED_SMT
  6155. for_each_cpu_mask(i, *cpu_map) {
  6156. struct sched_domain *sd = &per_cpu(cpu_domains, i);
  6157. init_sched_groups_power(i, sd);
  6158. }
  6159. #endif
  6160. #ifdef CONFIG_SCHED_MC
  6161. for_each_cpu_mask(i, *cpu_map) {
  6162. struct sched_domain *sd = &per_cpu(core_domains, i);
  6163. init_sched_groups_power(i, sd);
  6164. }
  6165. #endif
  6166. for_each_cpu_mask(i, *cpu_map) {
  6167. struct sched_domain *sd = &per_cpu(phys_domains, i);
  6168. init_sched_groups_power(i, sd);
  6169. }
  6170. #ifdef CONFIG_NUMA
  6171. for (i = 0; i < MAX_NUMNODES; i++)
  6172. init_numa_sched_groups_power(sched_group_nodes[i]);
  6173. if (sd_allnodes) {
  6174. struct sched_group *sg;
  6175. cpu_to_allnodes_group(first_cpu(*cpu_map), cpu_map, &sg,
  6176. tmpmask);
  6177. init_numa_sched_groups_power(sg);
  6178. }
  6179. #endif
  6180. /* Attach the domains */
  6181. for_each_cpu_mask(i, *cpu_map) {
  6182. struct sched_domain *sd;
  6183. #ifdef CONFIG_SCHED_SMT
  6184. sd = &per_cpu(cpu_domains, i);
  6185. #elif defined(CONFIG_SCHED_MC)
  6186. sd = &per_cpu(core_domains, i);
  6187. #else
  6188. sd = &per_cpu(phys_domains, i);
  6189. #endif
  6190. cpu_attach_domain(sd, rd, i);
  6191. }
  6192. SCHED_CPUMASK_FREE((void *)allmasks);
  6193. return 0;
  6194. #ifdef CONFIG_NUMA
  6195. error:
  6196. free_sched_groups(cpu_map, tmpmask);
  6197. SCHED_CPUMASK_FREE((void *)allmasks);
  6198. return -ENOMEM;
  6199. #endif
  6200. }
  6201. static int build_sched_domains(const cpumask_t *cpu_map)
  6202. {
  6203. return __build_sched_domains(cpu_map, NULL);
  6204. }
  6205. static cpumask_t *doms_cur; /* current sched domains */
  6206. static int ndoms_cur; /* number of sched domains in 'doms_cur' */
  6207. static struct sched_domain_attr *dattr_cur;
  6208. /* attribues of custom domains in 'doms_cur' */
  6209. /*
  6210. * Special case: If a kmalloc of a doms_cur partition (array of
  6211. * cpumask_t) fails, then fallback to a single sched domain,
  6212. * as determined by the single cpumask_t fallback_doms.
  6213. */
  6214. static cpumask_t fallback_doms;
  6215. void __attribute__((weak)) arch_update_cpu_topology(void)
  6216. {
  6217. }
  6218. /*
  6219. * Free current domain masks.
  6220. * Called after all cpus are attached to NULL domain.
  6221. */
  6222. static void free_sched_domains(void)
  6223. {
  6224. ndoms_cur = 0;
  6225. if (doms_cur != &fallback_doms)
  6226. kfree(doms_cur);
  6227. doms_cur = &fallback_doms;
  6228. }
  6229. /*
  6230. * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  6231. * For now this just excludes isolated cpus, but could be used to
  6232. * exclude other special cases in the future.
  6233. */
  6234. static int arch_init_sched_domains(const cpumask_t *cpu_map)
  6235. {
  6236. int err;
  6237. arch_update_cpu_topology();
  6238. ndoms_cur = 1;
  6239. doms_cur = kmalloc(sizeof(cpumask_t), GFP_KERNEL);
  6240. if (!doms_cur)
  6241. doms_cur = &fallback_doms;
  6242. cpus_andnot(*doms_cur, *cpu_map, cpu_isolated_map);
  6243. dattr_cur = NULL;
  6244. err = build_sched_domains(doms_cur);
  6245. register_sched_domain_sysctl();
  6246. return err;
  6247. }
  6248. static void arch_destroy_sched_domains(const cpumask_t *cpu_map,
  6249. cpumask_t *tmpmask)
  6250. {
  6251. free_sched_groups(cpu_map, tmpmask);
  6252. }
  6253. /*
  6254. * Detach sched domains from a group of cpus specified in cpu_map
  6255. * These cpus will now be attached to the NULL domain
  6256. */
  6257. static void detach_destroy_domains(const cpumask_t *cpu_map)
  6258. {
  6259. cpumask_t tmpmask;
  6260. int i;
  6261. unregister_sched_domain_sysctl();
  6262. for_each_cpu_mask(i, *cpu_map)
  6263. cpu_attach_domain(NULL, &def_root_domain, i);
  6264. synchronize_sched();
  6265. arch_destroy_sched_domains(cpu_map, &tmpmask);
  6266. }
  6267. /* handle null as "default" */
  6268. static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
  6269. struct sched_domain_attr *new, int idx_new)
  6270. {
  6271. struct sched_domain_attr tmp;
  6272. /* fast path */
  6273. if (!new && !cur)
  6274. return 1;
  6275. tmp = SD_ATTR_INIT;
  6276. return !memcmp(cur ? (cur + idx_cur) : &tmp,
  6277. new ? (new + idx_new) : &tmp,
  6278. sizeof(struct sched_domain_attr));
  6279. }
  6280. /*
  6281. * Partition sched domains as specified by the 'ndoms_new'
  6282. * cpumasks in the array doms_new[] of cpumasks. This compares
  6283. * doms_new[] to the current sched domain partitioning, doms_cur[].
  6284. * It destroys each deleted domain and builds each new domain.
  6285. *
  6286. * 'doms_new' is an array of cpumask_t's of length 'ndoms_new'.
  6287. * The masks don't intersect (don't overlap.) We should setup one
  6288. * sched domain for each mask. CPUs not in any of the cpumasks will
  6289. * not be load balanced. If the same cpumask appears both in the
  6290. * current 'doms_cur' domains and in the new 'doms_new', we can leave
  6291. * it as it is.
  6292. *
  6293. * The passed in 'doms_new' should be kmalloc'd. This routine takes
  6294. * ownership of it and will kfree it when done with it. If the caller
  6295. * failed the kmalloc call, then it can pass in doms_new == NULL,
  6296. * and partition_sched_domains() will fallback to the single partition
  6297. * 'fallback_doms'.
  6298. *
  6299. * Call with hotplug lock held
  6300. */
  6301. void partition_sched_domains(int ndoms_new, cpumask_t *doms_new,
  6302. struct sched_domain_attr *dattr_new)
  6303. {
  6304. int i, j;
  6305. mutex_lock(&sched_domains_mutex);
  6306. /* always unregister in case we don't destroy any domains */
  6307. unregister_sched_domain_sysctl();
  6308. if (doms_new == NULL) {
  6309. ndoms_new = 1;
  6310. doms_new = &fallback_doms;
  6311. cpus_andnot(doms_new[0], cpu_online_map, cpu_isolated_map);
  6312. dattr_new = NULL;
  6313. }
  6314. /* Destroy deleted domains */
  6315. for (i = 0; i < ndoms_cur; i++) {
  6316. for (j = 0; j < ndoms_new; j++) {
  6317. if (cpus_equal(doms_cur[i], doms_new[j])
  6318. && dattrs_equal(dattr_cur, i, dattr_new, j))
  6319. goto match1;
  6320. }
  6321. /* no match - a current sched domain not in new doms_new[] */
  6322. detach_destroy_domains(doms_cur + i);
  6323. match1:
  6324. ;
  6325. }
  6326. /* Build new domains */
  6327. for (i = 0; i < ndoms_new; i++) {
  6328. for (j = 0; j < ndoms_cur; j++) {
  6329. if (cpus_equal(doms_new[i], doms_cur[j])
  6330. && dattrs_equal(dattr_new, i, dattr_cur, j))
  6331. goto match2;
  6332. }
  6333. /* no match - add a new doms_new */
  6334. __build_sched_domains(doms_new + i,
  6335. dattr_new ? dattr_new + i : NULL);
  6336. match2:
  6337. ;
  6338. }
  6339. /* Remember the new sched domains */
  6340. if (doms_cur != &fallback_doms)
  6341. kfree(doms_cur);
  6342. kfree(dattr_cur); /* kfree(NULL) is safe */
  6343. doms_cur = doms_new;
  6344. dattr_cur = dattr_new;
  6345. ndoms_cur = ndoms_new;
  6346. register_sched_domain_sysctl();
  6347. mutex_unlock(&sched_domains_mutex);
  6348. }
  6349. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  6350. int arch_reinit_sched_domains(void)
  6351. {
  6352. int err;
  6353. get_online_cpus();
  6354. mutex_lock(&sched_domains_mutex);
  6355. detach_destroy_domains(&cpu_online_map);
  6356. free_sched_domains();
  6357. err = arch_init_sched_domains(&cpu_online_map);
  6358. mutex_unlock(&sched_domains_mutex);
  6359. put_online_cpus();
  6360. return err;
  6361. }
  6362. static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
  6363. {
  6364. int ret;
  6365. if (buf[0] != '0' && buf[0] != '1')
  6366. return -EINVAL;
  6367. if (smt)
  6368. sched_smt_power_savings = (buf[0] == '1');
  6369. else
  6370. sched_mc_power_savings = (buf[0] == '1');
  6371. ret = arch_reinit_sched_domains();
  6372. return ret ? ret : count;
  6373. }
  6374. #ifdef CONFIG_SCHED_MC
  6375. static ssize_t sched_mc_power_savings_show(struct sys_device *dev, char *page)
  6376. {
  6377. return sprintf(page, "%u\n", sched_mc_power_savings);
  6378. }
  6379. static ssize_t sched_mc_power_savings_store(struct sys_device *dev,
  6380. const char *buf, size_t count)
  6381. {
  6382. return sched_power_savings_store(buf, count, 0);
  6383. }
  6384. static SYSDEV_ATTR(sched_mc_power_savings, 0644, sched_mc_power_savings_show,
  6385. sched_mc_power_savings_store);
  6386. #endif
  6387. #ifdef CONFIG_SCHED_SMT
  6388. static ssize_t sched_smt_power_savings_show(struct sys_device *dev, char *page)
  6389. {
  6390. return sprintf(page, "%u\n", sched_smt_power_savings);
  6391. }
  6392. static ssize_t sched_smt_power_savings_store(struct sys_device *dev,
  6393. const char *buf, size_t count)
  6394. {
  6395. return sched_power_savings_store(buf, count, 1);
  6396. }
  6397. static SYSDEV_ATTR(sched_smt_power_savings, 0644, sched_smt_power_savings_show,
  6398. sched_smt_power_savings_store);
  6399. #endif
  6400. int sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
  6401. {
  6402. int err = 0;
  6403. #ifdef CONFIG_SCHED_SMT
  6404. if (smt_capable())
  6405. err = sysfs_create_file(&cls->kset.kobj,
  6406. &attr_sched_smt_power_savings.attr);
  6407. #endif
  6408. #ifdef CONFIG_SCHED_MC
  6409. if (!err && mc_capable())
  6410. err = sysfs_create_file(&cls->kset.kobj,
  6411. &attr_sched_mc_power_savings.attr);
  6412. #endif
  6413. return err;
  6414. }
  6415. #endif
  6416. /*
  6417. * Force a reinitialization of the sched domains hierarchy. The domains
  6418. * and groups cannot be updated in place without racing with the balancing
  6419. * code, so we temporarily attach all running cpus to the NULL domain
  6420. * which will prevent rebalancing while the sched domains are recalculated.
  6421. */
  6422. static int update_sched_domains(struct notifier_block *nfb,
  6423. unsigned long action, void *hcpu)
  6424. {
  6425. switch (action) {
  6426. case CPU_UP_PREPARE:
  6427. case CPU_UP_PREPARE_FROZEN:
  6428. case CPU_DOWN_PREPARE:
  6429. case CPU_DOWN_PREPARE_FROZEN:
  6430. detach_destroy_domains(&cpu_online_map);
  6431. free_sched_domains();
  6432. return NOTIFY_OK;
  6433. case CPU_UP_CANCELED:
  6434. case CPU_UP_CANCELED_FROZEN:
  6435. case CPU_DOWN_FAILED:
  6436. case CPU_DOWN_FAILED_FROZEN:
  6437. case CPU_ONLINE:
  6438. case CPU_ONLINE_FROZEN:
  6439. case CPU_DEAD:
  6440. case CPU_DEAD_FROZEN:
  6441. /*
  6442. * Fall through and re-initialise the domains.
  6443. */
  6444. break;
  6445. default:
  6446. return NOTIFY_DONE;
  6447. }
  6448. #ifndef CONFIG_CPUSETS
  6449. /*
  6450. * Create default domain partitioning if cpusets are disabled.
  6451. * Otherwise we let cpusets rebuild the domains based on the
  6452. * current setup.
  6453. */
  6454. /* The hotplug lock is already held by cpu_up/cpu_down */
  6455. arch_init_sched_domains(&cpu_online_map);
  6456. #endif
  6457. return NOTIFY_OK;
  6458. }
  6459. void __init sched_init_smp(void)
  6460. {
  6461. cpumask_t non_isolated_cpus;
  6462. #if defined(CONFIG_NUMA)
  6463. sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
  6464. GFP_KERNEL);
  6465. BUG_ON(sched_group_nodes_bycpu == NULL);
  6466. #endif
  6467. get_online_cpus();
  6468. mutex_lock(&sched_domains_mutex);
  6469. arch_init_sched_domains(&cpu_online_map);
  6470. cpus_andnot(non_isolated_cpus, cpu_possible_map, cpu_isolated_map);
  6471. if (cpus_empty(non_isolated_cpus))
  6472. cpu_set(smp_processor_id(), non_isolated_cpus);
  6473. mutex_unlock(&sched_domains_mutex);
  6474. put_online_cpus();
  6475. /* XXX: Theoretical race here - CPU may be hotplugged now */
  6476. hotcpu_notifier(update_sched_domains, 0);
  6477. init_hrtick();
  6478. /* Move init over to a non-isolated CPU */
  6479. if (set_cpus_allowed_ptr(current, &non_isolated_cpus) < 0)
  6480. BUG();
  6481. sched_init_granularity();
  6482. }
  6483. #else
  6484. void __init sched_init_smp(void)
  6485. {
  6486. sched_init_granularity();
  6487. }
  6488. #endif /* CONFIG_SMP */
  6489. int in_sched_functions(unsigned long addr)
  6490. {
  6491. return in_lock_functions(addr) ||
  6492. (addr >= (unsigned long)__sched_text_start
  6493. && addr < (unsigned long)__sched_text_end);
  6494. }
  6495. static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
  6496. {
  6497. cfs_rq->tasks_timeline = RB_ROOT;
  6498. INIT_LIST_HEAD(&cfs_rq->tasks);
  6499. #ifdef CONFIG_FAIR_GROUP_SCHED
  6500. cfs_rq->rq = rq;
  6501. #endif
  6502. cfs_rq->min_vruntime = (u64)(-(1LL << 20));
  6503. }
  6504. static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
  6505. {
  6506. struct rt_prio_array *array;
  6507. int i;
  6508. array = &rt_rq->active;
  6509. for (i = 0; i < MAX_RT_PRIO; i++) {
  6510. INIT_LIST_HEAD(array->queue + i);
  6511. __clear_bit(i, array->bitmap);
  6512. }
  6513. /* delimiter for bitsearch: */
  6514. __set_bit(MAX_RT_PRIO, array->bitmap);
  6515. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  6516. rt_rq->highest_prio = MAX_RT_PRIO;
  6517. #endif
  6518. #ifdef CONFIG_SMP
  6519. rt_rq->rt_nr_migratory = 0;
  6520. rt_rq->overloaded = 0;
  6521. #endif
  6522. rt_rq->rt_time = 0;
  6523. rt_rq->rt_throttled = 0;
  6524. rt_rq->rt_runtime = 0;
  6525. spin_lock_init(&rt_rq->rt_runtime_lock);
  6526. #ifdef CONFIG_RT_GROUP_SCHED
  6527. rt_rq->rt_nr_boosted = 0;
  6528. rt_rq->rq = rq;
  6529. #endif
  6530. }
  6531. #ifdef CONFIG_FAIR_GROUP_SCHED
  6532. static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  6533. struct sched_entity *se, int cpu, int add,
  6534. struct sched_entity *parent)
  6535. {
  6536. struct rq *rq = cpu_rq(cpu);
  6537. tg->cfs_rq[cpu] = cfs_rq;
  6538. init_cfs_rq(cfs_rq, rq);
  6539. cfs_rq->tg = tg;
  6540. if (add)
  6541. list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
  6542. tg->se[cpu] = se;
  6543. /* se could be NULL for init_task_group */
  6544. if (!se)
  6545. return;
  6546. if (!parent)
  6547. se->cfs_rq = &rq->cfs;
  6548. else
  6549. se->cfs_rq = parent->my_q;
  6550. se->my_q = cfs_rq;
  6551. se->load.weight = tg->shares;
  6552. se->load.inv_weight = 0;
  6553. se->parent = parent;
  6554. }
  6555. #endif
  6556. #ifdef CONFIG_RT_GROUP_SCHED
  6557. static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  6558. struct sched_rt_entity *rt_se, int cpu, int add,
  6559. struct sched_rt_entity *parent)
  6560. {
  6561. struct rq *rq = cpu_rq(cpu);
  6562. tg->rt_rq[cpu] = rt_rq;
  6563. init_rt_rq(rt_rq, rq);
  6564. rt_rq->tg = tg;
  6565. rt_rq->rt_se = rt_se;
  6566. rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
  6567. if (add)
  6568. list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
  6569. tg->rt_se[cpu] = rt_se;
  6570. if (!rt_se)
  6571. return;
  6572. if (!parent)
  6573. rt_se->rt_rq = &rq->rt;
  6574. else
  6575. rt_se->rt_rq = parent->my_q;
  6576. rt_se->my_q = rt_rq;
  6577. rt_se->parent = parent;
  6578. INIT_LIST_HEAD(&rt_se->run_list);
  6579. }
  6580. #endif
  6581. void __init sched_init(void)
  6582. {
  6583. int i, j;
  6584. unsigned long alloc_size = 0, ptr;
  6585. #ifdef CONFIG_FAIR_GROUP_SCHED
  6586. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  6587. #endif
  6588. #ifdef CONFIG_RT_GROUP_SCHED
  6589. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  6590. #endif
  6591. #ifdef CONFIG_USER_SCHED
  6592. alloc_size *= 2;
  6593. #endif
  6594. /*
  6595. * As sched_init() is called before page_alloc is setup,
  6596. * we use alloc_bootmem().
  6597. */
  6598. if (alloc_size) {
  6599. ptr = (unsigned long)alloc_bootmem(alloc_size);
  6600. #ifdef CONFIG_FAIR_GROUP_SCHED
  6601. init_task_group.se = (struct sched_entity **)ptr;
  6602. ptr += nr_cpu_ids * sizeof(void **);
  6603. init_task_group.cfs_rq = (struct cfs_rq **)ptr;
  6604. ptr += nr_cpu_ids * sizeof(void **);
  6605. #ifdef CONFIG_USER_SCHED
  6606. root_task_group.se = (struct sched_entity **)ptr;
  6607. ptr += nr_cpu_ids * sizeof(void **);
  6608. root_task_group.cfs_rq = (struct cfs_rq **)ptr;
  6609. ptr += nr_cpu_ids * sizeof(void **);
  6610. #endif
  6611. #endif
  6612. #ifdef CONFIG_RT_GROUP_SCHED
  6613. init_task_group.rt_se = (struct sched_rt_entity **)ptr;
  6614. ptr += nr_cpu_ids * sizeof(void **);
  6615. init_task_group.rt_rq = (struct rt_rq **)ptr;
  6616. ptr += nr_cpu_ids * sizeof(void **);
  6617. #ifdef CONFIG_USER_SCHED
  6618. root_task_group.rt_se = (struct sched_rt_entity **)ptr;
  6619. ptr += nr_cpu_ids * sizeof(void **);
  6620. root_task_group.rt_rq = (struct rt_rq **)ptr;
  6621. ptr += nr_cpu_ids * sizeof(void **);
  6622. #endif
  6623. #endif
  6624. }
  6625. #ifdef CONFIG_SMP
  6626. init_defrootdomain();
  6627. #endif
  6628. init_rt_bandwidth(&def_rt_bandwidth,
  6629. global_rt_period(), global_rt_runtime());
  6630. #ifdef CONFIG_RT_GROUP_SCHED
  6631. init_rt_bandwidth(&init_task_group.rt_bandwidth,
  6632. global_rt_period(), global_rt_runtime());
  6633. #ifdef CONFIG_USER_SCHED
  6634. init_rt_bandwidth(&root_task_group.rt_bandwidth,
  6635. global_rt_period(), RUNTIME_INF);
  6636. #endif
  6637. #endif
  6638. #ifdef CONFIG_GROUP_SCHED
  6639. list_add(&init_task_group.list, &task_groups);
  6640. INIT_LIST_HEAD(&init_task_group.children);
  6641. #ifdef CONFIG_USER_SCHED
  6642. INIT_LIST_HEAD(&root_task_group.children);
  6643. init_task_group.parent = &root_task_group;
  6644. list_add(&init_task_group.siblings, &root_task_group.children);
  6645. #endif
  6646. #endif
  6647. for_each_possible_cpu(i) {
  6648. struct rq *rq;
  6649. rq = cpu_rq(i);
  6650. spin_lock_init(&rq->lock);
  6651. lockdep_set_class(&rq->lock, &rq->rq_lock_key);
  6652. rq->nr_running = 0;
  6653. init_cfs_rq(&rq->cfs, rq);
  6654. init_rt_rq(&rq->rt, rq);
  6655. #ifdef CONFIG_FAIR_GROUP_SCHED
  6656. init_task_group.shares = init_task_group_load;
  6657. INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
  6658. #ifdef CONFIG_CGROUP_SCHED
  6659. /*
  6660. * How much cpu bandwidth does init_task_group get?
  6661. *
  6662. * In case of task-groups formed thr' the cgroup filesystem, it
  6663. * gets 100% of the cpu resources in the system. This overall
  6664. * system cpu resource is divided among the tasks of
  6665. * init_task_group and its child task-groups in a fair manner,
  6666. * based on each entity's (task or task-group's) weight
  6667. * (se->load.weight).
  6668. *
  6669. * In other words, if init_task_group has 10 tasks of weight
  6670. * 1024) and two child groups A0 and A1 (of weight 1024 each),
  6671. * then A0's share of the cpu resource is:
  6672. *
  6673. * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
  6674. *
  6675. * We achieve this by letting init_task_group's tasks sit
  6676. * directly in rq->cfs (i.e init_task_group->se[] = NULL).
  6677. */
  6678. init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
  6679. #elif defined CONFIG_USER_SCHED
  6680. root_task_group.shares = NICE_0_LOAD;
  6681. init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
  6682. /*
  6683. * In case of task-groups formed thr' the user id of tasks,
  6684. * init_task_group represents tasks belonging to root user.
  6685. * Hence it forms a sibling of all subsequent groups formed.
  6686. * In this case, init_task_group gets only a fraction of overall
  6687. * system cpu resource, based on the weight assigned to root
  6688. * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
  6689. * by letting tasks of init_task_group sit in a separate cfs_rq
  6690. * (init_cfs_rq) and having one entity represent this group of
  6691. * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
  6692. */
  6693. init_tg_cfs_entry(&init_task_group,
  6694. &per_cpu(init_cfs_rq, i),
  6695. &per_cpu(init_sched_entity, i), i, 1,
  6696. root_task_group.se[i]);
  6697. #endif
  6698. #endif /* CONFIG_FAIR_GROUP_SCHED */
  6699. rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
  6700. #ifdef CONFIG_RT_GROUP_SCHED
  6701. INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
  6702. #ifdef CONFIG_CGROUP_SCHED
  6703. init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
  6704. #elif defined CONFIG_USER_SCHED
  6705. init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
  6706. init_tg_rt_entry(&init_task_group,
  6707. &per_cpu(init_rt_rq, i),
  6708. &per_cpu(init_sched_rt_entity, i), i, 1,
  6709. root_task_group.rt_se[i]);
  6710. #endif
  6711. #endif
  6712. for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
  6713. rq->cpu_load[j] = 0;
  6714. #ifdef CONFIG_SMP
  6715. rq->sd = NULL;
  6716. rq->rd = NULL;
  6717. rq->active_balance = 0;
  6718. rq->next_balance = jiffies;
  6719. rq->push_cpu = 0;
  6720. rq->cpu = i;
  6721. rq->migration_thread = NULL;
  6722. INIT_LIST_HEAD(&rq->migration_queue);
  6723. rq_attach_root(rq, &def_root_domain);
  6724. #endif
  6725. init_rq_hrtick(rq);
  6726. atomic_set(&rq->nr_iowait, 0);
  6727. }
  6728. set_load_weight(&init_task);
  6729. #ifdef CONFIG_PREEMPT_NOTIFIERS
  6730. INIT_HLIST_HEAD(&init_task.preempt_notifiers);
  6731. #endif
  6732. #ifdef CONFIG_SMP
  6733. open_softirq(SCHED_SOFTIRQ, run_rebalance_domains, NULL);
  6734. #endif
  6735. #ifdef CONFIG_RT_MUTEXES
  6736. plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
  6737. #endif
  6738. /*
  6739. * The boot idle thread does lazy MMU switching as well:
  6740. */
  6741. atomic_inc(&init_mm.mm_count);
  6742. enter_lazy_tlb(&init_mm, current);
  6743. /*
  6744. * Make us the idle thread. Technically, schedule() should not be
  6745. * called from this thread, however somewhere below it might be,
  6746. * but because we are the idle thread, we just pick up running again
  6747. * when this runqueue becomes "idle".
  6748. */
  6749. init_idle(current, smp_processor_id());
  6750. /*
  6751. * During early bootup we pretend to be a normal task:
  6752. */
  6753. current->sched_class = &fair_sched_class;
  6754. scheduler_running = 1;
  6755. }
  6756. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  6757. void __might_sleep(char *file, int line)
  6758. {
  6759. #ifdef in_atomic
  6760. static unsigned long prev_jiffy; /* ratelimiting */
  6761. if ((in_atomic() || irqs_disabled()) &&
  6762. system_state == SYSTEM_RUNNING && !oops_in_progress) {
  6763. if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  6764. return;
  6765. prev_jiffy = jiffies;
  6766. printk(KERN_ERR "BUG: sleeping function called from invalid"
  6767. " context at %s:%d\n", file, line);
  6768. printk("in_atomic():%d, irqs_disabled():%d\n",
  6769. in_atomic(), irqs_disabled());
  6770. debug_show_held_locks(current);
  6771. if (irqs_disabled())
  6772. print_irqtrace_events(current);
  6773. dump_stack();
  6774. }
  6775. #endif
  6776. }
  6777. EXPORT_SYMBOL(__might_sleep);
  6778. #endif
  6779. #ifdef CONFIG_MAGIC_SYSRQ
  6780. static void normalize_task(struct rq *rq, struct task_struct *p)
  6781. {
  6782. int on_rq;
  6783. update_rq_clock(rq);
  6784. on_rq = p->se.on_rq;
  6785. if (on_rq)
  6786. deactivate_task(rq, p, 0);
  6787. __setscheduler(rq, p, SCHED_NORMAL, 0);
  6788. if (on_rq) {
  6789. activate_task(rq, p, 0);
  6790. resched_task(rq->curr);
  6791. }
  6792. }
  6793. void normalize_rt_tasks(void)
  6794. {
  6795. struct task_struct *g, *p;
  6796. unsigned long flags;
  6797. struct rq *rq;
  6798. read_lock_irqsave(&tasklist_lock, flags);
  6799. do_each_thread(g, p) {
  6800. /*
  6801. * Only normalize user tasks:
  6802. */
  6803. if (!p->mm)
  6804. continue;
  6805. p->se.exec_start = 0;
  6806. #ifdef CONFIG_SCHEDSTATS
  6807. p->se.wait_start = 0;
  6808. p->se.sleep_start = 0;
  6809. p->se.block_start = 0;
  6810. #endif
  6811. if (!rt_task(p)) {
  6812. /*
  6813. * Renice negative nice level userspace
  6814. * tasks back to 0:
  6815. */
  6816. if (TASK_NICE(p) < 0 && p->mm)
  6817. set_user_nice(p, 0);
  6818. continue;
  6819. }
  6820. spin_lock(&p->pi_lock);
  6821. rq = __task_rq_lock(p);
  6822. normalize_task(rq, p);
  6823. __task_rq_unlock(rq);
  6824. spin_unlock(&p->pi_lock);
  6825. } while_each_thread(g, p);
  6826. read_unlock_irqrestore(&tasklist_lock, flags);
  6827. }
  6828. #endif /* CONFIG_MAGIC_SYSRQ */
  6829. #ifdef CONFIG_IA64
  6830. /*
  6831. * These functions are only useful for the IA64 MCA handling.
  6832. *
  6833. * They can only be called when the whole system has been
  6834. * stopped - every CPU needs to be quiescent, and no scheduling
  6835. * activity can take place. Using them for anything else would
  6836. * be a serious bug, and as a result, they aren't even visible
  6837. * under any other configuration.
  6838. */
  6839. /**
  6840. * curr_task - return the current task for a given cpu.
  6841. * @cpu: the processor in question.
  6842. *
  6843. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  6844. */
  6845. struct task_struct *curr_task(int cpu)
  6846. {
  6847. return cpu_curr(cpu);
  6848. }
  6849. /**
  6850. * set_curr_task - set the current task for a given cpu.
  6851. * @cpu: the processor in question.
  6852. * @p: the task pointer to set.
  6853. *
  6854. * Description: This function must only be used when non-maskable interrupts
  6855. * are serviced on a separate stack. It allows the architecture to switch the
  6856. * notion of the current task on a cpu in a non-blocking manner. This function
  6857. * must be called with all CPU's synchronized, and interrupts disabled, the
  6858. * and caller must save the original value of the current task (see
  6859. * curr_task() above) and restore that value before reenabling interrupts and
  6860. * re-starting the system.
  6861. *
  6862. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  6863. */
  6864. void set_curr_task(int cpu, struct task_struct *p)
  6865. {
  6866. cpu_curr(cpu) = p;
  6867. }
  6868. #endif
  6869. #ifdef CONFIG_FAIR_GROUP_SCHED
  6870. static void free_fair_sched_group(struct task_group *tg)
  6871. {
  6872. int i;
  6873. for_each_possible_cpu(i) {
  6874. if (tg->cfs_rq)
  6875. kfree(tg->cfs_rq[i]);
  6876. if (tg->se)
  6877. kfree(tg->se[i]);
  6878. }
  6879. kfree(tg->cfs_rq);
  6880. kfree(tg->se);
  6881. }
  6882. static
  6883. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  6884. {
  6885. struct cfs_rq *cfs_rq;
  6886. struct sched_entity *se, *parent_se;
  6887. struct rq *rq;
  6888. int i;
  6889. tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
  6890. if (!tg->cfs_rq)
  6891. goto err;
  6892. tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
  6893. if (!tg->se)
  6894. goto err;
  6895. tg->shares = NICE_0_LOAD;
  6896. for_each_possible_cpu(i) {
  6897. rq = cpu_rq(i);
  6898. cfs_rq = kmalloc_node(sizeof(struct cfs_rq),
  6899. GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
  6900. if (!cfs_rq)
  6901. goto err;
  6902. se = kmalloc_node(sizeof(struct sched_entity),
  6903. GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
  6904. if (!se)
  6905. goto err;
  6906. parent_se = parent ? parent->se[i] : NULL;
  6907. init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent_se);
  6908. }
  6909. return 1;
  6910. err:
  6911. return 0;
  6912. }
  6913. static inline void register_fair_sched_group(struct task_group *tg, int cpu)
  6914. {
  6915. list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
  6916. &cpu_rq(cpu)->leaf_cfs_rq_list);
  6917. }
  6918. static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
  6919. {
  6920. list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
  6921. }
  6922. #else
  6923. static inline void free_fair_sched_group(struct task_group *tg)
  6924. {
  6925. }
  6926. static inline
  6927. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  6928. {
  6929. return 1;
  6930. }
  6931. static inline void register_fair_sched_group(struct task_group *tg, int cpu)
  6932. {
  6933. }
  6934. static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
  6935. {
  6936. }
  6937. #endif
  6938. #ifdef CONFIG_RT_GROUP_SCHED
  6939. static void free_rt_sched_group(struct task_group *tg)
  6940. {
  6941. int i;
  6942. destroy_rt_bandwidth(&tg->rt_bandwidth);
  6943. for_each_possible_cpu(i) {
  6944. if (tg->rt_rq)
  6945. kfree(tg->rt_rq[i]);
  6946. if (tg->rt_se)
  6947. kfree(tg->rt_se[i]);
  6948. }
  6949. kfree(tg->rt_rq);
  6950. kfree(tg->rt_se);
  6951. }
  6952. static
  6953. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  6954. {
  6955. struct rt_rq *rt_rq;
  6956. struct sched_rt_entity *rt_se, *parent_se;
  6957. struct rq *rq;
  6958. int i;
  6959. tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
  6960. if (!tg->rt_rq)
  6961. goto err;
  6962. tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
  6963. if (!tg->rt_se)
  6964. goto err;
  6965. init_rt_bandwidth(&tg->rt_bandwidth,
  6966. ktime_to_ns(def_rt_bandwidth.rt_period), 0);
  6967. for_each_possible_cpu(i) {
  6968. rq = cpu_rq(i);
  6969. rt_rq = kmalloc_node(sizeof(struct rt_rq),
  6970. GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
  6971. if (!rt_rq)
  6972. goto err;
  6973. rt_se = kmalloc_node(sizeof(struct sched_rt_entity),
  6974. GFP_KERNEL|__GFP_ZERO, cpu_to_node(i));
  6975. if (!rt_se)
  6976. goto err;
  6977. parent_se = parent ? parent->rt_se[i] : NULL;
  6978. init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent_se);
  6979. }
  6980. return 1;
  6981. err:
  6982. return 0;
  6983. }
  6984. static inline void register_rt_sched_group(struct task_group *tg, int cpu)
  6985. {
  6986. list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
  6987. &cpu_rq(cpu)->leaf_rt_rq_list);
  6988. }
  6989. static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
  6990. {
  6991. list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
  6992. }
  6993. #else
  6994. static inline void free_rt_sched_group(struct task_group *tg)
  6995. {
  6996. }
  6997. static inline
  6998. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  6999. {
  7000. return 1;
  7001. }
  7002. static inline void register_rt_sched_group(struct task_group *tg, int cpu)
  7003. {
  7004. }
  7005. static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
  7006. {
  7007. }
  7008. #endif
  7009. #ifdef CONFIG_GROUP_SCHED
  7010. static void free_sched_group(struct task_group *tg)
  7011. {
  7012. free_fair_sched_group(tg);
  7013. free_rt_sched_group(tg);
  7014. kfree(tg);
  7015. }
  7016. /* allocate runqueue etc for a new task group */
  7017. struct task_group *sched_create_group(struct task_group *parent)
  7018. {
  7019. struct task_group *tg;
  7020. unsigned long flags;
  7021. int i;
  7022. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  7023. if (!tg)
  7024. return ERR_PTR(-ENOMEM);
  7025. if (!alloc_fair_sched_group(tg, parent))
  7026. goto err;
  7027. if (!alloc_rt_sched_group(tg, parent))
  7028. goto err;
  7029. spin_lock_irqsave(&task_group_lock, flags);
  7030. for_each_possible_cpu(i) {
  7031. register_fair_sched_group(tg, i);
  7032. register_rt_sched_group(tg, i);
  7033. }
  7034. list_add_rcu(&tg->list, &task_groups);
  7035. WARN_ON(!parent); /* root should already exist */
  7036. tg->parent = parent;
  7037. list_add_rcu(&tg->siblings, &parent->children);
  7038. INIT_LIST_HEAD(&tg->children);
  7039. spin_unlock_irqrestore(&task_group_lock, flags);
  7040. return tg;
  7041. err:
  7042. free_sched_group(tg);
  7043. return ERR_PTR(-ENOMEM);
  7044. }
  7045. /* rcu callback to free various structures associated with a task group */
  7046. static void free_sched_group_rcu(struct rcu_head *rhp)
  7047. {
  7048. /* now it should be safe to free those cfs_rqs */
  7049. free_sched_group(container_of(rhp, struct task_group, rcu));
  7050. }
  7051. /* Destroy runqueue etc associated with a task group */
  7052. void sched_destroy_group(struct task_group *tg)
  7053. {
  7054. unsigned long flags;
  7055. int i;
  7056. spin_lock_irqsave(&task_group_lock, flags);
  7057. for_each_possible_cpu(i) {
  7058. unregister_fair_sched_group(tg, i);
  7059. unregister_rt_sched_group(tg, i);
  7060. }
  7061. list_del_rcu(&tg->list);
  7062. list_del_rcu(&tg->siblings);
  7063. spin_unlock_irqrestore(&task_group_lock, flags);
  7064. /* wait for possible concurrent references to cfs_rqs complete */
  7065. call_rcu(&tg->rcu, free_sched_group_rcu);
  7066. }
  7067. /* change task's runqueue when it moves between groups.
  7068. * The caller of this function should have put the task in its new group
  7069. * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
  7070. * reflect its new group.
  7071. */
  7072. void sched_move_task(struct task_struct *tsk)
  7073. {
  7074. int on_rq, running;
  7075. unsigned long flags;
  7076. struct rq *rq;
  7077. rq = task_rq_lock(tsk, &flags);
  7078. update_rq_clock(rq);
  7079. running = task_current(rq, tsk);
  7080. on_rq = tsk->se.on_rq;
  7081. if (on_rq)
  7082. dequeue_task(rq, tsk, 0);
  7083. if (unlikely(running))
  7084. tsk->sched_class->put_prev_task(rq, tsk);
  7085. set_task_rq(tsk, task_cpu(tsk));
  7086. #ifdef CONFIG_FAIR_GROUP_SCHED
  7087. if (tsk->sched_class->moved_group)
  7088. tsk->sched_class->moved_group(tsk);
  7089. #endif
  7090. if (unlikely(running))
  7091. tsk->sched_class->set_curr_task(rq);
  7092. if (on_rq)
  7093. enqueue_task(rq, tsk, 0);
  7094. task_rq_unlock(rq, &flags);
  7095. }
  7096. #endif
  7097. #ifdef CONFIG_FAIR_GROUP_SCHED
  7098. static void set_se_shares(struct sched_entity *se, unsigned long shares)
  7099. {
  7100. struct cfs_rq *cfs_rq = se->cfs_rq;
  7101. struct rq *rq = cfs_rq->rq;
  7102. int on_rq;
  7103. spin_lock_irq(&rq->lock);
  7104. on_rq = se->on_rq;
  7105. if (on_rq)
  7106. dequeue_entity(cfs_rq, se, 0);
  7107. se->load.weight = shares;
  7108. se->load.inv_weight = 0;
  7109. if (on_rq)
  7110. enqueue_entity(cfs_rq, se, 0);
  7111. spin_unlock_irq(&rq->lock);
  7112. }
  7113. static DEFINE_MUTEX(shares_mutex);
  7114. int sched_group_set_shares(struct task_group *tg, unsigned long shares)
  7115. {
  7116. int i;
  7117. unsigned long flags;
  7118. /*
  7119. * We can't change the weight of the root cgroup.
  7120. */
  7121. if (!tg->se[0])
  7122. return -EINVAL;
  7123. if (shares < MIN_SHARES)
  7124. shares = MIN_SHARES;
  7125. else if (shares > MAX_SHARES)
  7126. shares = MAX_SHARES;
  7127. mutex_lock(&shares_mutex);
  7128. if (tg->shares == shares)
  7129. goto done;
  7130. spin_lock_irqsave(&task_group_lock, flags);
  7131. for_each_possible_cpu(i)
  7132. unregister_fair_sched_group(tg, i);
  7133. list_del_rcu(&tg->siblings);
  7134. spin_unlock_irqrestore(&task_group_lock, flags);
  7135. /* wait for any ongoing reference to this group to finish */
  7136. synchronize_sched();
  7137. /*
  7138. * Now we are free to modify the group's share on each cpu
  7139. * w/o tripping rebalance_share or load_balance_fair.
  7140. */
  7141. tg->shares = shares;
  7142. for_each_possible_cpu(i)
  7143. set_se_shares(tg->se[i], shares);
  7144. /*
  7145. * Enable load balance activity on this group, by inserting it back on
  7146. * each cpu's rq->leaf_cfs_rq_list.
  7147. */
  7148. spin_lock_irqsave(&task_group_lock, flags);
  7149. for_each_possible_cpu(i)
  7150. register_fair_sched_group(tg, i);
  7151. list_add_rcu(&tg->siblings, &tg->parent->children);
  7152. spin_unlock_irqrestore(&task_group_lock, flags);
  7153. done:
  7154. mutex_unlock(&shares_mutex);
  7155. return 0;
  7156. }
  7157. unsigned long sched_group_shares(struct task_group *tg)
  7158. {
  7159. return tg->shares;
  7160. }
  7161. #endif
  7162. #ifdef CONFIG_RT_GROUP_SCHED
  7163. /*
  7164. * Ensure that the real time constraints are schedulable.
  7165. */
  7166. static DEFINE_MUTEX(rt_constraints_mutex);
  7167. static unsigned long to_ratio(u64 period, u64 runtime)
  7168. {
  7169. if (runtime == RUNTIME_INF)
  7170. return 1ULL << 16;
  7171. return div64_u64(runtime << 16, period);
  7172. }
  7173. #ifdef CONFIG_CGROUP_SCHED
  7174. static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
  7175. {
  7176. struct task_group *tgi, *parent = tg ? tg->parent : NULL;
  7177. unsigned long total = 0;
  7178. if (!parent) {
  7179. if (global_rt_period() < period)
  7180. return 0;
  7181. return to_ratio(period, runtime) <
  7182. to_ratio(global_rt_period(), global_rt_runtime());
  7183. }
  7184. if (ktime_to_ns(parent->rt_bandwidth.rt_period) < period)
  7185. return 0;
  7186. rcu_read_lock();
  7187. list_for_each_entry_rcu(tgi, &parent->children, siblings) {
  7188. if (tgi == tg)
  7189. continue;
  7190. total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
  7191. tgi->rt_bandwidth.rt_runtime);
  7192. }
  7193. rcu_read_unlock();
  7194. return total + to_ratio(period, runtime) <
  7195. to_ratio(ktime_to_ns(parent->rt_bandwidth.rt_period),
  7196. parent->rt_bandwidth.rt_runtime);
  7197. }
  7198. #elif defined CONFIG_USER_SCHED
  7199. static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
  7200. {
  7201. struct task_group *tgi;
  7202. unsigned long total = 0;
  7203. unsigned long global_ratio =
  7204. to_ratio(global_rt_period(), global_rt_runtime());
  7205. rcu_read_lock();
  7206. list_for_each_entry_rcu(tgi, &task_groups, list) {
  7207. if (tgi == tg)
  7208. continue;
  7209. total += to_ratio(ktime_to_ns(tgi->rt_bandwidth.rt_period),
  7210. tgi->rt_bandwidth.rt_runtime);
  7211. }
  7212. rcu_read_unlock();
  7213. return total + to_ratio(period, runtime) < global_ratio;
  7214. }
  7215. #endif
  7216. /* Must be called with tasklist_lock held */
  7217. static inline int tg_has_rt_tasks(struct task_group *tg)
  7218. {
  7219. struct task_struct *g, *p;
  7220. do_each_thread(g, p) {
  7221. if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
  7222. return 1;
  7223. } while_each_thread(g, p);
  7224. return 0;
  7225. }
  7226. static int tg_set_bandwidth(struct task_group *tg,
  7227. u64 rt_period, u64 rt_runtime)
  7228. {
  7229. int i, err = 0;
  7230. mutex_lock(&rt_constraints_mutex);
  7231. read_lock(&tasklist_lock);
  7232. if (rt_runtime == 0 && tg_has_rt_tasks(tg)) {
  7233. err = -EBUSY;
  7234. goto unlock;
  7235. }
  7236. if (!__rt_schedulable(tg, rt_period, rt_runtime)) {
  7237. err = -EINVAL;
  7238. goto unlock;
  7239. }
  7240. spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  7241. tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
  7242. tg->rt_bandwidth.rt_runtime = rt_runtime;
  7243. for_each_possible_cpu(i) {
  7244. struct rt_rq *rt_rq = tg->rt_rq[i];
  7245. spin_lock(&rt_rq->rt_runtime_lock);
  7246. rt_rq->rt_runtime = rt_runtime;
  7247. spin_unlock(&rt_rq->rt_runtime_lock);
  7248. }
  7249. spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  7250. unlock:
  7251. read_unlock(&tasklist_lock);
  7252. mutex_unlock(&rt_constraints_mutex);
  7253. return err;
  7254. }
  7255. int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
  7256. {
  7257. u64 rt_runtime, rt_period;
  7258. rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  7259. rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
  7260. if (rt_runtime_us < 0)
  7261. rt_runtime = RUNTIME_INF;
  7262. return tg_set_bandwidth(tg, rt_period, rt_runtime);
  7263. }
  7264. long sched_group_rt_runtime(struct task_group *tg)
  7265. {
  7266. u64 rt_runtime_us;
  7267. if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
  7268. return -1;
  7269. rt_runtime_us = tg->rt_bandwidth.rt_runtime;
  7270. do_div(rt_runtime_us, NSEC_PER_USEC);
  7271. return rt_runtime_us;
  7272. }
  7273. int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
  7274. {
  7275. u64 rt_runtime, rt_period;
  7276. rt_period = (u64)rt_period_us * NSEC_PER_USEC;
  7277. rt_runtime = tg->rt_bandwidth.rt_runtime;
  7278. return tg_set_bandwidth(tg, rt_period, rt_runtime);
  7279. }
  7280. long sched_group_rt_period(struct task_group *tg)
  7281. {
  7282. u64 rt_period_us;
  7283. rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
  7284. do_div(rt_period_us, NSEC_PER_USEC);
  7285. return rt_period_us;
  7286. }
  7287. static int sched_rt_global_constraints(void)
  7288. {
  7289. int ret = 0;
  7290. mutex_lock(&rt_constraints_mutex);
  7291. if (!__rt_schedulable(NULL, 1, 0))
  7292. ret = -EINVAL;
  7293. mutex_unlock(&rt_constraints_mutex);
  7294. return ret;
  7295. }
  7296. #else
  7297. static int sched_rt_global_constraints(void)
  7298. {
  7299. unsigned long flags;
  7300. int i;
  7301. spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
  7302. for_each_possible_cpu(i) {
  7303. struct rt_rq *rt_rq = &cpu_rq(i)->rt;
  7304. spin_lock(&rt_rq->rt_runtime_lock);
  7305. rt_rq->rt_runtime = global_rt_runtime();
  7306. spin_unlock(&rt_rq->rt_runtime_lock);
  7307. }
  7308. spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
  7309. return 0;
  7310. }
  7311. #endif
  7312. int sched_rt_handler(struct ctl_table *table, int write,
  7313. struct file *filp, void __user *buffer, size_t *lenp,
  7314. loff_t *ppos)
  7315. {
  7316. int ret;
  7317. int old_period, old_runtime;
  7318. static DEFINE_MUTEX(mutex);
  7319. mutex_lock(&mutex);
  7320. old_period = sysctl_sched_rt_period;
  7321. old_runtime = sysctl_sched_rt_runtime;
  7322. ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  7323. if (!ret && write) {
  7324. ret = sched_rt_global_constraints();
  7325. if (ret) {
  7326. sysctl_sched_rt_period = old_period;
  7327. sysctl_sched_rt_runtime = old_runtime;
  7328. } else {
  7329. def_rt_bandwidth.rt_runtime = global_rt_runtime();
  7330. def_rt_bandwidth.rt_period =
  7331. ns_to_ktime(global_rt_period());
  7332. }
  7333. }
  7334. mutex_unlock(&mutex);
  7335. return ret;
  7336. }
  7337. #ifdef CONFIG_CGROUP_SCHED
  7338. /* return corresponding task_group object of a cgroup */
  7339. static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
  7340. {
  7341. return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
  7342. struct task_group, css);
  7343. }
  7344. static struct cgroup_subsys_state *
  7345. cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
  7346. {
  7347. struct task_group *tg, *parent;
  7348. if (!cgrp->parent) {
  7349. /* This is early initialization for the top cgroup */
  7350. init_task_group.css.cgroup = cgrp;
  7351. return &init_task_group.css;
  7352. }
  7353. parent = cgroup_tg(cgrp->parent);
  7354. tg = sched_create_group(parent);
  7355. if (IS_ERR(tg))
  7356. return ERR_PTR(-ENOMEM);
  7357. /* Bind the cgroup to task_group object we just created */
  7358. tg->css.cgroup = cgrp;
  7359. return &tg->css;
  7360. }
  7361. static void
  7362. cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
  7363. {
  7364. struct task_group *tg = cgroup_tg(cgrp);
  7365. sched_destroy_group(tg);
  7366. }
  7367. static int
  7368. cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  7369. struct task_struct *tsk)
  7370. {
  7371. #ifdef CONFIG_RT_GROUP_SCHED
  7372. /* Don't accept realtime tasks when there is no way for them to run */
  7373. if (rt_task(tsk) && cgroup_tg(cgrp)->rt_bandwidth.rt_runtime == 0)
  7374. return -EINVAL;
  7375. #else
  7376. /* We don't support RT-tasks being in separate groups */
  7377. if (tsk->sched_class != &fair_sched_class)
  7378. return -EINVAL;
  7379. #endif
  7380. return 0;
  7381. }
  7382. static void
  7383. cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  7384. struct cgroup *old_cont, struct task_struct *tsk)
  7385. {
  7386. sched_move_task(tsk);
  7387. }
  7388. #ifdef CONFIG_FAIR_GROUP_SCHED
  7389. static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
  7390. u64 shareval)
  7391. {
  7392. return sched_group_set_shares(cgroup_tg(cgrp), shareval);
  7393. }
  7394. static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
  7395. {
  7396. struct task_group *tg = cgroup_tg(cgrp);
  7397. return (u64) tg->shares;
  7398. }
  7399. #endif
  7400. #ifdef CONFIG_RT_GROUP_SCHED
  7401. static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
  7402. s64 val)
  7403. {
  7404. return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
  7405. }
  7406. static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
  7407. {
  7408. return sched_group_rt_runtime(cgroup_tg(cgrp));
  7409. }
  7410. static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
  7411. u64 rt_period_us)
  7412. {
  7413. return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
  7414. }
  7415. static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
  7416. {
  7417. return sched_group_rt_period(cgroup_tg(cgrp));
  7418. }
  7419. #endif
  7420. static struct cftype cpu_files[] = {
  7421. #ifdef CONFIG_FAIR_GROUP_SCHED
  7422. {
  7423. .name = "shares",
  7424. .read_u64 = cpu_shares_read_u64,
  7425. .write_u64 = cpu_shares_write_u64,
  7426. },
  7427. #endif
  7428. #ifdef CONFIG_RT_GROUP_SCHED
  7429. {
  7430. .name = "rt_runtime_us",
  7431. .read_s64 = cpu_rt_runtime_read,
  7432. .write_s64 = cpu_rt_runtime_write,
  7433. },
  7434. {
  7435. .name = "rt_period_us",
  7436. .read_u64 = cpu_rt_period_read_uint,
  7437. .write_u64 = cpu_rt_period_write_uint,
  7438. },
  7439. #endif
  7440. };
  7441. static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  7442. {
  7443. return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
  7444. }
  7445. struct cgroup_subsys cpu_cgroup_subsys = {
  7446. .name = "cpu",
  7447. .create = cpu_cgroup_create,
  7448. .destroy = cpu_cgroup_destroy,
  7449. .can_attach = cpu_cgroup_can_attach,
  7450. .attach = cpu_cgroup_attach,
  7451. .populate = cpu_cgroup_populate,
  7452. .subsys_id = cpu_cgroup_subsys_id,
  7453. .early_init = 1,
  7454. };
  7455. #endif /* CONFIG_CGROUP_SCHED */
  7456. #ifdef CONFIG_CGROUP_CPUACCT
  7457. /*
  7458. * CPU accounting code for task groups.
  7459. *
  7460. * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
  7461. * (balbir@in.ibm.com).
  7462. */
  7463. /* track cpu usage of a group of tasks */
  7464. struct cpuacct {
  7465. struct cgroup_subsys_state css;
  7466. /* cpuusage holds pointer to a u64-type object on every cpu */
  7467. u64 *cpuusage;
  7468. };
  7469. struct cgroup_subsys cpuacct_subsys;
  7470. /* return cpu accounting group corresponding to this container */
  7471. static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
  7472. {
  7473. return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
  7474. struct cpuacct, css);
  7475. }
  7476. /* return cpu accounting group to which this task belongs */
  7477. static inline struct cpuacct *task_ca(struct task_struct *tsk)
  7478. {
  7479. return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
  7480. struct cpuacct, css);
  7481. }
  7482. /* create a new cpu accounting group */
  7483. static struct cgroup_subsys_state *cpuacct_create(
  7484. struct cgroup_subsys *ss, struct cgroup *cgrp)
  7485. {
  7486. struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  7487. if (!ca)
  7488. return ERR_PTR(-ENOMEM);
  7489. ca->cpuusage = alloc_percpu(u64);
  7490. if (!ca->cpuusage) {
  7491. kfree(ca);
  7492. return ERR_PTR(-ENOMEM);
  7493. }
  7494. return &ca->css;
  7495. }
  7496. /* destroy an existing cpu accounting group */
  7497. static void
  7498. cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
  7499. {
  7500. struct cpuacct *ca = cgroup_ca(cgrp);
  7501. free_percpu(ca->cpuusage);
  7502. kfree(ca);
  7503. }
  7504. /* return total cpu usage (in nanoseconds) of a group */
  7505. static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
  7506. {
  7507. struct cpuacct *ca = cgroup_ca(cgrp);
  7508. u64 totalcpuusage = 0;
  7509. int i;
  7510. for_each_possible_cpu(i) {
  7511. u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
  7512. /*
  7513. * Take rq->lock to make 64-bit addition safe on 32-bit
  7514. * platforms.
  7515. */
  7516. spin_lock_irq(&cpu_rq(i)->lock);
  7517. totalcpuusage += *cpuusage;
  7518. spin_unlock_irq(&cpu_rq(i)->lock);
  7519. }
  7520. return totalcpuusage;
  7521. }
  7522. static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
  7523. u64 reset)
  7524. {
  7525. struct cpuacct *ca = cgroup_ca(cgrp);
  7526. int err = 0;
  7527. int i;
  7528. if (reset) {
  7529. err = -EINVAL;
  7530. goto out;
  7531. }
  7532. for_each_possible_cpu(i) {
  7533. u64 *cpuusage = percpu_ptr(ca->cpuusage, i);
  7534. spin_lock_irq(&cpu_rq(i)->lock);
  7535. *cpuusage = 0;
  7536. spin_unlock_irq(&cpu_rq(i)->lock);
  7537. }
  7538. out:
  7539. return err;
  7540. }
  7541. static struct cftype files[] = {
  7542. {
  7543. .name = "usage",
  7544. .read_u64 = cpuusage_read,
  7545. .write_u64 = cpuusage_write,
  7546. },
  7547. };
  7548. static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
  7549. {
  7550. return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
  7551. }
  7552. /*
  7553. * charge this task's execution time to its accounting group.
  7554. *
  7555. * called with rq->lock held.
  7556. */
  7557. static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
  7558. {
  7559. struct cpuacct *ca;
  7560. if (!cpuacct_subsys.active)
  7561. return;
  7562. ca = task_ca(tsk);
  7563. if (ca) {
  7564. u64 *cpuusage = percpu_ptr(ca->cpuusage, task_cpu(tsk));
  7565. *cpuusage += cputime;
  7566. }
  7567. }
  7568. struct cgroup_subsys cpuacct_subsys = {
  7569. .name = "cpuacct",
  7570. .create = cpuacct_create,
  7571. .destroy = cpuacct_destroy,
  7572. .populate = cpuacct_populate,
  7573. .subsys_id = cpuacct_subsys_id,
  7574. };
  7575. #endif /* CONFIG_CGROUP_CPUACCT */