sched.c 172 KB

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