sched.c 167 KB

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