sched.c 128 KB

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