sched.c 142 KB

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