sched.c 141 KB

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