sched.c 126 KB

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