sched.c 196 KB

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