sched.c 124 KB

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