sched.c 156 KB

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