sched.c 200 KB

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