sched.c 173 KB

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