sched.c 222 KB

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