sched.c 155 KB

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