sched.c 142 KB

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