sched.c 161 KB

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