sched.c 165 KB

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