sched.c 244 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018
  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. * 2007-04-15 Work begun on replacing all interactivity tuning with a
  20. * fair scheduling design by Con Kolivas.
  21. * 2007-05-05 Load balancing (smp-nice) and other improvements
  22. * by Peter Williams
  23. * 2007-05-06 Interactivity improvements to CFS by Mike Galbraith
  24. * 2007-07-01 Group scheduling enhancements by Srivatsa Vaddagiri
  25. * 2007-11-29 RT balancing improvements by Steven Rostedt, Gregory Haskins,
  26. * Thomas Gleixner, Mike Kravetz
  27. */
  28. #include <linux/mm.h>
  29. #include <linux/module.h>
  30. #include <linux/nmi.h>
  31. #include <linux/init.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/highmem.h>
  34. #include <linux/smp_lock.h>
  35. #include <asm/mmu_context.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/capability.h>
  38. #include <linux/completion.h>
  39. #include <linux/kernel_stat.h>
  40. #include <linux/debug_locks.h>
  41. #include <linux/security.h>
  42. #include <linux/notifier.h>
  43. #include <linux/profile.h>
  44. #include <linux/freezer.h>
  45. #include <linux/vmalloc.h>
  46. #include <linux/blkdev.h>
  47. #include <linux/delay.h>
  48. #include <linux/pid_namespace.h>
  49. #include <linux/smp.h>
  50. #include <linux/threads.h>
  51. #include <linux/timer.h>
  52. #include <linux/rcupdate.h>
  53. #include <linux/cpu.h>
  54. #include <linux/cpuset.h>
  55. #include <linux/percpu.h>
  56. #include <linux/kthread.h>
  57. #include <linux/proc_fs.h>
  58. #include <linux/seq_file.h>
  59. #include <linux/sysctl.h>
  60. #include <linux/syscalls.h>
  61. #include <linux/times.h>
  62. #include <linux/tsacct_kern.h>
  63. #include <linux/kprobes.h>
  64. #include <linux/delayacct.h>
  65. #include <linux/reciprocal_div.h>
  66. #include <linux/unistd.h>
  67. #include <linux/pagemap.h>
  68. #include <linux/hrtimer.h>
  69. #include <linux/tick.h>
  70. #include <linux/bootmem.h>
  71. #include <linux/debugfs.h>
  72. #include <linux/ctype.h>
  73. #include <linux/ftrace.h>
  74. #include <trace/sched.h>
  75. #include <asm/tlb.h>
  76. #include <asm/irq_regs.h>
  77. #include "sched_cpupri.h"
  78. /*
  79. * Convert user-nice values [ -20 ... 0 ... 19 ]
  80. * to static priority [ MAX_RT_PRIO..MAX_PRIO-1 ],
  81. * and back.
  82. */
  83. #define NICE_TO_PRIO(nice) (MAX_RT_PRIO + (nice) + 20)
  84. #define PRIO_TO_NICE(prio) ((prio) - MAX_RT_PRIO - 20)
  85. #define TASK_NICE(p) PRIO_TO_NICE((p)->static_prio)
  86. /*
  87. * 'User priority' is the nice value converted to something we
  88. * can work with better when scaling various scheduler parameters,
  89. * it's a [ 0 ... 39 ] range.
  90. */
  91. #define USER_PRIO(p) ((p)-MAX_RT_PRIO)
  92. #define TASK_USER_PRIO(p) USER_PRIO((p)->static_prio)
  93. #define MAX_USER_PRIO (USER_PRIO(MAX_PRIO))
  94. /*
  95. * Helpers for converting nanosecond timing to jiffy resolution
  96. */
  97. #define NS_TO_JIFFIES(TIME) ((unsigned long)(TIME) / (NSEC_PER_SEC / HZ))
  98. #define NICE_0_LOAD SCHED_LOAD_SCALE
  99. #define NICE_0_SHIFT SCHED_LOAD_SHIFT
  100. /*
  101. * These are the 'tuning knobs' of the scheduler:
  102. *
  103. * default timeslice is 100 msecs (used only for SCHED_RR tasks).
  104. * Timeslices get refilled after they expire.
  105. */
  106. #define DEF_TIMESLICE (100 * HZ / 1000)
  107. /*
  108. * single value that denotes runtime == period, ie unlimited time.
  109. */
  110. #define RUNTIME_INF ((u64)~0ULL)
  111. DEFINE_TRACE(sched_wait_task);
  112. DEFINE_TRACE(sched_wakeup);
  113. DEFINE_TRACE(sched_wakeup_new);
  114. DEFINE_TRACE(sched_switch);
  115. DEFINE_TRACE(sched_migrate_task);
  116. #ifdef CONFIG_SMP
  117. static void double_rq_lock(struct rq *rq1, struct rq *rq2);
  118. /*
  119. * Divide a load by a sched group cpu_power : (load / sg->__cpu_power)
  120. * Since cpu_power is a 'constant', we can use a reciprocal divide.
  121. */
  122. static inline u32 sg_div_cpu_power(const struct sched_group *sg, u32 load)
  123. {
  124. return reciprocal_divide(load, sg->reciprocal_cpu_power);
  125. }
  126. /*
  127. * Each time a sched group cpu_power is changed,
  128. * we must compute its reciprocal value
  129. */
  130. static inline void sg_inc_cpu_power(struct sched_group *sg, u32 val)
  131. {
  132. sg->__cpu_power += val;
  133. sg->reciprocal_cpu_power = reciprocal_value(sg->__cpu_power);
  134. }
  135. #endif
  136. static inline int rt_policy(int policy)
  137. {
  138. if (unlikely(policy == SCHED_FIFO || policy == SCHED_RR))
  139. return 1;
  140. return 0;
  141. }
  142. static inline int task_has_rt_policy(struct task_struct *p)
  143. {
  144. return rt_policy(p->policy);
  145. }
  146. /*
  147. * This is the priority-queue data structure of the RT scheduling class:
  148. */
  149. struct rt_prio_array {
  150. DECLARE_BITMAP(bitmap, MAX_RT_PRIO+1); /* include 1 bit for delimiter */
  151. struct list_head queue[MAX_RT_PRIO];
  152. };
  153. struct rt_bandwidth {
  154. /* nests inside the rq lock: */
  155. spinlock_t rt_runtime_lock;
  156. ktime_t rt_period;
  157. u64 rt_runtime;
  158. struct hrtimer rt_period_timer;
  159. };
  160. static struct rt_bandwidth def_rt_bandwidth;
  161. static int do_sched_rt_period_timer(struct rt_bandwidth *rt_b, int overrun);
  162. static enum hrtimer_restart sched_rt_period_timer(struct hrtimer *timer)
  163. {
  164. struct rt_bandwidth *rt_b =
  165. container_of(timer, struct rt_bandwidth, rt_period_timer);
  166. ktime_t now;
  167. int overrun;
  168. int idle = 0;
  169. for (;;) {
  170. now = hrtimer_cb_get_time(timer);
  171. overrun = hrtimer_forward(timer, now, rt_b->rt_period);
  172. if (!overrun)
  173. break;
  174. idle = do_sched_rt_period_timer(rt_b, overrun);
  175. }
  176. return idle ? HRTIMER_NORESTART : HRTIMER_RESTART;
  177. }
  178. static
  179. void init_rt_bandwidth(struct rt_bandwidth *rt_b, u64 period, u64 runtime)
  180. {
  181. rt_b->rt_period = ns_to_ktime(period);
  182. rt_b->rt_runtime = runtime;
  183. spin_lock_init(&rt_b->rt_runtime_lock);
  184. hrtimer_init(&rt_b->rt_period_timer,
  185. CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  186. rt_b->rt_period_timer.function = sched_rt_period_timer;
  187. }
  188. static inline int rt_bandwidth_enabled(void)
  189. {
  190. return sysctl_sched_rt_runtime >= 0;
  191. }
  192. static void start_rt_bandwidth(struct rt_bandwidth *rt_b)
  193. {
  194. ktime_t now;
  195. if (!rt_bandwidth_enabled() || rt_b->rt_runtime == RUNTIME_INF)
  196. return;
  197. if (hrtimer_active(&rt_b->rt_period_timer))
  198. return;
  199. spin_lock(&rt_b->rt_runtime_lock);
  200. for (;;) {
  201. if (hrtimer_active(&rt_b->rt_period_timer))
  202. break;
  203. now = hrtimer_cb_get_time(&rt_b->rt_period_timer);
  204. hrtimer_forward(&rt_b->rt_period_timer, now, rt_b->rt_period);
  205. hrtimer_start_expires(&rt_b->rt_period_timer,
  206. HRTIMER_MODE_ABS);
  207. }
  208. spin_unlock(&rt_b->rt_runtime_lock);
  209. }
  210. #ifdef CONFIG_RT_GROUP_SCHED
  211. static void destroy_rt_bandwidth(struct rt_bandwidth *rt_b)
  212. {
  213. hrtimer_cancel(&rt_b->rt_period_timer);
  214. }
  215. #endif
  216. /*
  217. * sched_domains_mutex serializes calls to arch_init_sched_domains,
  218. * detach_destroy_domains and partition_sched_domains.
  219. */
  220. static DEFINE_MUTEX(sched_domains_mutex);
  221. #ifdef CONFIG_GROUP_SCHED
  222. #include <linux/cgroup.h>
  223. struct cfs_rq;
  224. static LIST_HEAD(task_groups);
  225. /* task group related information */
  226. struct task_group {
  227. #ifdef CONFIG_CGROUP_SCHED
  228. struct cgroup_subsys_state css;
  229. #endif
  230. #ifdef CONFIG_USER_SCHED
  231. uid_t uid;
  232. #endif
  233. #ifdef CONFIG_FAIR_GROUP_SCHED
  234. /* schedulable entities of this group on each cpu */
  235. struct sched_entity **se;
  236. /* runqueue "owned" by this group on each cpu */
  237. struct cfs_rq **cfs_rq;
  238. unsigned long shares;
  239. #endif
  240. #ifdef CONFIG_RT_GROUP_SCHED
  241. struct sched_rt_entity **rt_se;
  242. struct rt_rq **rt_rq;
  243. struct rt_bandwidth rt_bandwidth;
  244. #endif
  245. struct rcu_head rcu;
  246. struct list_head list;
  247. struct task_group *parent;
  248. struct list_head siblings;
  249. struct list_head children;
  250. };
  251. #ifdef CONFIG_USER_SCHED
  252. /* Helper function to pass uid information to create_sched_user() */
  253. void set_tg_uid(struct user_struct *user)
  254. {
  255. user->tg->uid = user->uid;
  256. }
  257. /*
  258. * Root task group.
  259. * Every UID task group (including init_task_group aka UID-0) will
  260. * be a child to this group.
  261. */
  262. struct task_group root_task_group;
  263. #ifdef CONFIG_FAIR_GROUP_SCHED
  264. /* Default task group's sched entity on each cpu */
  265. static DEFINE_PER_CPU(struct sched_entity, init_sched_entity);
  266. /* Default task group's cfs_rq on each cpu */
  267. static DEFINE_PER_CPU(struct cfs_rq, init_cfs_rq) ____cacheline_aligned_in_smp;
  268. #endif /* CONFIG_FAIR_GROUP_SCHED */
  269. #ifdef CONFIG_RT_GROUP_SCHED
  270. static DEFINE_PER_CPU(struct sched_rt_entity, init_sched_rt_entity);
  271. static DEFINE_PER_CPU(struct rt_rq, init_rt_rq) ____cacheline_aligned_in_smp;
  272. #endif /* CONFIG_RT_GROUP_SCHED */
  273. #else /* !CONFIG_USER_SCHED */
  274. #define root_task_group init_task_group
  275. #endif /* CONFIG_USER_SCHED */
  276. /* task_group_lock serializes add/remove of task groups and also changes to
  277. * a task group's cpu shares.
  278. */
  279. static DEFINE_SPINLOCK(task_group_lock);
  280. #ifdef CONFIG_SMP
  281. static int root_task_group_empty(void)
  282. {
  283. return list_empty(&root_task_group.children);
  284. }
  285. #endif
  286. #ifdef CONFIG_FAIR_GROUP_SCHED
  287. #ifdef CONFIG_USER_SCHED
  288. # define INIT_TASK_GROUP_LOAD (2*NICE_0_LOAD)
  289. #else /* !CONFIG_USER_SCHED */
  290. # define INIT_TASK_GROUP_LOAD NICE_0_LOAD
  291. #endif /* CONFIG_USER_SCHED */
  292. /*
  293. * A weight of 0 or 1 can cause arithmetics problems.
  294. * A weight of a cfs_rq is the sum of weights of which entities
  295. * are queued on this cfs_rq, so a weight of a entity should not be
  296. * too large, so as the shares value of a task group.
  297. * (The default weight is 1024 - so there's no practical
  298. * limitation from this.)
  299. */
  300. #define MIN_SHARES 2
  301. #define MAX_SHARES (1UL << 18)
  302. static int init_task_group_load = INIT_TASK_GROUP_LOAD;
  303. #endif
  304. /* Default task group.
  305. * Every task in system belong to this group at bootup.
  306. */
  307. struct task_group init_task_group;
  308. /* return group to which a task belongs */
  309. static inline struct task_group *task_group(struct task_struct *p)
  310. {
  311. struct task_group *tg;
  312. #ifdef CONFIG_USER_SCHED
  313. rcu_read_lock();
  314. tg = __task_cred(p)->user->tg;
  315. rcu_read_unlock();
  316. #elif defined(CONFIG_CGROUP_SCHED)
  317. tg = container_of(task_subsys_state(p, cpu_cgroup_subsys_id),
  318. struct task_group, css);
  319. #else
  320. tg = &init_task_group;
  321. #endif
  322. return tg;
  323. }
  324. /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups */
  325. static inline void set_task_rq(struct task_struct *p, unsigned int cpu)
  326. {
  327. #ifdef CONFIG_FAIR_GROUP_SCHED
  328. p->se.cfs_rq = task_group(p)->cfs_rq[cpu];
  329. p->se.parent = task_group(p)->se[cpu];
  330. #endif
  331. #ifdef CONFIG_RT_GROUP_SCHED
  332. p->rt.rt_rq = task_group(p)->rt_rq[cpu];
  333. p->rt.parent = task_group(p)->rt_se[cpu];
  334. #endif
  335. }
  336. #else
  337. #ifdef CONFIG_SMP
  338. static int root_task_group_empty(void)
  339. {
  340. return 1;
  341. }
  342. #endif
  343. static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { }
  344. static inline struct task_group *task_group(struct task_struct *p)
  345. {
  346. return NULL;
  347. }
  348. #endif /* CONFIG_GROUP_SCHED */
  349. /* CFS-related fields in a runqueue */
  350. struct cfs_rq {
  351. struct load_weight load;
  352. unsigned long nr_running;
  353. u64 exec_clock;
  354. u64 min_vruntime;
  355. struct rb_root tasks_timeline;
  356. struct rb_node *rb_leftmost;
  357. struct list_head tasks;
  358. struct list_head *balance_iterator;
  359. /*
  360. * 'curr' points to currently running entity on this cfs_rq.
  361. * It is set to NULL otherwise (i.e when none are currently running).
  362. */
  363. struct sched_entity *curr, *next, *last;
  364. unsigned int nr_spread_over;
  365. #ifdef CONFIG_FAIR_GROUP_SCHED
  366. struct rq *rq; /* cpu runqueue to which this cfs_rq is attached */
  367. /*
  368. * leaf cfs_rqs are those that hold tasks (lowest schedulable entity in
  369. * a hierarchy). Non-leaf lrqs hold other higher schedulable entities
  370. * (like users, containers etc.)
  371. *
  372. * leaf_cfs_rq_list ties together list of leaf cfs_rq's in a cpu. This
  373. * list is used during load balance.
  374. */
  375. struct list_head leaf_cfs_rq_list;
  376. struct task_group *tg; /* group that "owns" this runqueue */
  377. #ifdef CONFIG_SMP
  378. /*
  379. * the part of load.weight contributed by tasks
  380. */
  381. unsigned long task_weight;
  382. /*
  383. * h_load = weight * f(tg)
  384. *
  385. * Where f(tg) is the recursive weight fraction assigned to
  386. * this group.
  387. */
  388. unsigned long h_load;
  389. /*
  390. * this cpu's part of tg->shares
  391. */
  392. unsigned long shares;
  393. /*
  394. * load.weight at the time we set shares
  395. */
  396. unsigned long rq_weight;
  397. #endif
  398. #endif
  399. };
  400. /* Real-Time classes' related field in a runqueue: */
  401. struct rt_rq {
  402. struct rt_prio_array active;
  403. unsigned long rt_nr_running;
  404. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  405. struct {
  406. int curr; /* highest queued rt task prio */
  407. #ifdef CONFIG_SMP
  408. int next; /* next highest */
  409. #endif
  410. } highest_prio;
  411. #endif
  412. #ifdef CONFIG_SMP
  413. unsigned long rt_nr_migratory;
  414. int overloaded;
  415. struct plist_head pushable_tasks;
  416. #endif
  417. int rt_throttled;
  418. u64 rt_time;
  419. u64 rt_runtime;
  420. /* Nests inside the rq lock: */
  421. spinlock_t rt_runtime_lock;
  422. #ifdef CONFIG_RT_GROUP_SCHED
  423. unsigned long rt_nr_boosted;
  424. struct rq *rq;
  425. struct list_head leaf_rt_rq_list;
  426. struct task_group *tg;
  427. struct sched_rt_entity *rt_se;
  428. #endif
  429. };
  430. #ifdef CONFIG_SMP
  431. /*
  432. * We add the notion of a root-domain which will be used to define per-domain
  433. * variables. Each exclusive cpuset essentially defines an island domain by
  434. * fully partitioning the member cpus from any other cpuset. Whenever a new
  435. * exclusive cpuset is created, we also create and attach a new root-domain
  436. * object.
  437. *
  438. */
  439. struct root_domain {
  440. atomic_t refcount;
  441. cpumask_var_t span;
  442. cpumask_var_t online;
  443. /*
  444. * The "RT overload" flag: it gets set if a CPU has more than
  445. * one runnable RT task.
  446. */
  447. cpumask_var_t rto_mask;
  448. atomic_t rto_count;
  449. #ifdef CONFIG_SMP
  450. struct cpupri cpupri;
  451. #endif
  452. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  453. /*
  454. * Preferred wake up cpu nominated by sched_mc balance that will be
  455. * used when most cpus are idle in the system indicating overall very
  456. * low system utilisation. Triggered at POWERSAVINGS_BALANCE_WAKEUP(2)
  457. */
  458. unsigned int sched_mc_preferred_wakeup_cpu;
  459. #endif
  460. };
  461. /*
  462. * By default the system creates a single root-domain with all cpus as
  463. * members (mimicking the global state we have today).
  464. */
  465. static struct root_domain def_root_domain;
  466. #endif
  467. /*
  468. * This is the main, per-CPU runqueue data structure.
  469. *
  470. * Locking rule: those places that want to lock multiple runqueues
  471. * (such as the load balancing or the thread migration code), lock
  472. * acquire operations must be ordered by ascending &runqueue.
  473. */
  474. struct rq {
  475. /* runqueue lock: */
  476. spinlock_t lock;
  477. /*
  478. * nr_running and cpu_load should be in the same cacheline because
  479. * remote CPUs use both these fields when doing load calculation.
  480. */
  481. unsigned long nr_running;
  482. #define CPU_LOAD_IDX_MAX 5
  483. unsigned long cpu_load[CPU_LOAD_IDX_MAX];
  484. #ifdef CONFIG_NO_HZ
  485. unsigned long last_tick_seen;
  486. unsigned char in_nohz_recently;
  487. #endif
  488. /* capture load from *all* tasks on this cpu: */
  489. struct load_weight load;
  490. unsigned long nr_load_updates;
  491. u64 nr_switches;
  492. struct cfs_rq cfs;
  493. struct rt_rq rt;
  494. #ifdef CONFIG_FAIR_GROUP_SCHED
  495. /* list of leaf cfs_rq on this cpu: */
  496. struct list_head leaf_cfs_rq_list;
  497. #endif
  498. #ifdef CONFIG_RT_GROUP_SCHED
  499. struct list_head leaf_rt_rq_list;
  500. #endif
  501. /*
  502. * This is part of a global counter where only the total sum
  503. * over all CPUs matters. A task can increase this counter on
  504. * one CPU and if it got migrated afterwards it may decrease
  505. * it on another CPU. Always updated under the runqueue lock:
  506. */
  507. unsigned long nr_uninterruptible;
  508. struct task_struct *curr, *idle;
  509. unsigned long next_balance;
  510. struct mm_struct *prev_mm;
  511. u64 clock;
  512. atomic_t nr_iowait;
  513. #ifdef CONFIG_SMP
  514. struct root_domain *rd;
  515. struct sched_domain *sd;
  516. unsigned char idle_at_tick;
  517. /* For active balancing */
  518. int active_balance;
  519. int push_cpu;
  520. /* cpu of this runqueue: */
  521. int cpu;
  522. int online;
  523. unsigned long avg_load_per_task;
  524. struct task_struct *migration_thread;
  525. struct list_head migration_queue;
  526. #endif
  527. #ifdef CONFIG_SCHED_HRTICK
  528. #ifdef CONFIG_SMP
  529. int hrtick_csd_pending;
  530. struct call_single_data hrtick_csd;
  531. #endif
  532. struct hrtimer hrtick_timer;
  533. #endif
  534. #ifdef CONFIG_SCHEDSTATS
  535. /* latency stats */
  536. struct sched_info rq_sched_info;
  537. unsigned long long rq_cpu_time;
  538. /* could above be rq->cfs_rq.exec_clock + rq->rt_rq.rt_runtime ? */
  539. /* sys_sched_yield() stats */
  540. unsigned int yld_count;
  541. /* schedule() stats */
  542. unsigned int sched_switch;
  543. unsigned int sched_count;
  544. unsigned int sched_goidle;
  545. /* try_to_wake_up() stats */
  546. unsigned int ttwu_count;
  547. unsigned int ttwu_local;
  548. /* BKL stats */
  549. unsigned int bkl_count;
  550. #endif
  551. };
  552. static DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  553. static inline void check_preempt_curr(struct rq *rq, struct task_struct *p, int sync)
  554. {
  555. rq->curr->sched_class->check_preempt_curr(rq, p, sync);
  556. }
  557. static inline int cpu_of(struct rq *rq)
  558. {
  559. #ifdef CONFIG_SMP
  560. return rq->cpu;
  561. #else
  562. return 0;
  563. #endif
  564. }
  565. /*
  566. * The domain tree (rq->sd) is protected by RCU's quiescent state transition.
  567. * See detach_destroy_domains: synchronize_sched for details.
  568. *
  569. * The domain tree of any CPU may only be accessed from within
  570. * preempt-disabled sections.
  571. */
  572. #define for_each_domain(cpu, __sd) \
  573. for (__sd = rcu_dereference(cpu_rq(cpu)->sd); __sd; __sd = __sd->parent)
  574. #define cpu_rq(cpu) (&per_cpu(runqueues, (cpu)))
  575. #define this_rq() (&__get_cpu_var(runqueues))
  576. #define task_rq(p) cpu_rq(task_cpu(p))
  577. #define cpu_curr(cpu) (cpu_rq(cpu)->curr)
  578. static inline void update_rq_clock(struct rq *rq)
  579. {
  580. rq->clock = sched_clock_cpu(cpu_of(rq));
  581. }
  582. /*
  583. * Tunables that become constants when CONFIG_SCHED_DEBUG is off:
  584. */
  585. #ifdef CONFIG_SCHED_DEBUG
  586. # define const_debug __read_mostly
  587. #else
  588. # define const_debug static const
  589. #endif
  590. /**
  591. * runqueue_is_locked
  592. *
  593. * Returns true if the current cpu runqueue is locked.
  594. * This interface allows printk to be called with the runqueue lock
  595. * held and know whether or not it is OK to wake up the klogd.
  596. */
  597. int runqueue_is_locked(void)
  598. {
  599. int cpu = get_cpu();
  600. struct rq *rq = cpu_rq(cpu);
  601. int ret;
  602. ret = spin_is_locked(&rq->lock);
  603. put_cpu();
  604. return ret;
  605. }
  606. /*
  607. * Debugging: various feature bits
  608. */
  609. #define SCHED_FEAT(name, enabled) \
  610. __SCHED_FEAT_##name ,
  611. enum {
  612. #include "sched_features.h"
  613. };
  614. #undef SCHED_FEAT
  615. #define SCHED_FEAT(name, enabled) \
  616. (1UL << __SCHED_FEAT_##name) * enabled |
  617. const_debug unsigned int sysctl_sched_features =
  618. #include "sched_features.h"
  619. 0;
  620. #undef SCHED_FEAT
  621. #ifdef CONFIG_SCHED_DEBUG
  622. #define SCHED_FEAT(name, enabled) \
  623. #name ,
  624. static __read_mostly char *sched_feat_names[] = {
  625. #include "sched_features.h"
  626. NULL
  627. };
  628. #undef SCHED_FEAT
  629. static int sched_feat_show(struct seq_file *m, void *v)
  630. {
  631. int i;
  632. for (i = 0; sched_feat_names[i]; i++) {
  633. if (!(sysctl_sched_features & (1UL << i)))
  634. seq_puts(m, "NO_");
  635. seq_printf(m, "%s ", sched_feat_names[i]);
  636. }
  637. seq_puts(m, "\n");
  638. return 0;
  639. }
  640. static ssize_t
  641. sched_feat_write(struct file *filp, const char __user *ubuf,
  642. size_t cnt, loff_t *ppos)
  643. {
  644. char buf[64];
  645. char *cmp = buf;
  646. int neg = 0;
  647. int i;
  648. if (cnt > 63)
  649. cnt = 63;
  650. if (copy_from_user(&buf, ubuf, cnt))
  651. return -EFAULT;
  652. buf[cnt] = 0;
  653. if (strncmp(buf, "NO_", 3) == 0) {
  654. neg = 1;
  655. cmp += 3;
  656. }
  657. for (i = 0; sched_feat_names[i]; i++) {
  658. int len = strlen(sched_feat_names[i]);
  659. if (strncmp(cmp, sched_feat_names[i], len) == 0) {
  660. if (neg)
  661. sysctl_sched_features &= ~(1UL << i);
  662. else
  663. sysctl_sched_features |= (1UL << i);
  664. break;
  665. }
  666. }
  667. if (!sched_feat_names[i])
  668. return -EINVAL;
  669. filp->f_pos += cnt;
  670. return cnt;
  671. }
  672. static int sched_feat_open(struct inode *inode, struct file *filp)
  673. {
  674. return single_open(filp, sched_feat_show, NULL);
  675. }
  676. static struct file_operations sched_feat_fops = {
  677. .open = sched_feat_open,
  678. .write = sched_feat_write,
  679. .read = seq_read,
  680. .llseek = seq_lseek,
  681. .release = single_release,
  682. };
  683. static __init int sched_init_debug(void)
  684. {
  685. debugfs_create_file("sched_features", 0644, NULL, NULL,
  686. &sched_feat_fops);
  687. return 0;
  688. }
  689. late_initcall(sched_init_debug);
  690. #endif
  691. #define sched_feat(x) (sysctl_sched_features & (1UL << __SCHED_FEAT_##x))
  692. /*
  693. * Number of tasks to iterate in a single balance run.
  694. * Limited because this is done with IRQs disabled.
  695. */
  696. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  697. /*
  698. * ratelimit for updating the group shares.
  699. * default: 0.25ms
  700. */
  701. unsigned int sysctl_sched_shares_ratelimit = 250000;
  702. /*
  703. * Inject some fuzzyness into changing the per-cpu group shares
  704. * this avoids remote rq-locks at the expense of fairness.
  705. * default: 4
  706. */
  707. unsigned int sysctl_sched_shares_thresh = 4;
  708. /*
  709. * period over which we measure -rt task cpu usage in us.
  710. * default: 1s
  711. */
  712. unsigned int sysctl_sched_rt_period = 1000000;
  713. static __read_mostly int scheduler_running;
  714. /*
  715. * part of the period that we allow rt tasks to run in us.
  716. * default: 0.95s
  717. */
  718. int sysctl_sched_rt_runtime = 950000;
  719. static inline u64 global_rt_period(void)
  720. {
  721. return (u64)sysctl_sched_rt_period * NSEC_PER_USEC;
  722. }
  723. static inline u64 global_rt_runtime(void)
  724. {
  725. if (sysctl_sched_rt_runtime < 0)
  726. return RUNTIME_INF;
  727. return (u64)sysctl_sched_rt_runtime * NSEC_PER_USEC;
  728. }
  729. #ifndef prepare_arch_switch
  730. # define prepare_arch_switch(next) do { } while (0)
  731. #endif
  732. #ifndef finish_arch_switch
  733. # define finish_arch_switch(prev) do { } while (0)
  734. #endif
  735. static inline int task_current(struct rq *rq, struct task_struct *p)
  736. {
  737. return rq->curr == p;
  738. }
  739. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  740. static inline int task_running(struct rq *rq, struct task_struct *p)
  741. {
  742. return task_current(rq, p);
  743. }
  744. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  745. {
  746. }
  747. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  748. {
  749. #ifdef CONFIG_DEBUG_SPINLOCK
  750. /* this is a valid case when another task releases the spinlock */
  751. rq->lock.owner = current;
  752. #endif
  753. /*
  754. * If we are tracking spinlock dependencies then we have to
  755. * fix up the runqueue lock - which gets 'carried over' from
  756. * prev into current:
  757. */
  758. spin_acquire(&rq->lock.dep_map, 0, 0, _THIS_IP_);
  759. spin_unlock_irq(&rq->lock);
  760. }
  761. #else /* __ARCH_WANT_UNLOCKED_CTXSW */
  762. static inline int task_running(struct rq *rq, struct task_struct *p)
  763. {
  764. #ifdef CONFIG_SMP
  765. return p->oncpu;
  766. #else
  767. return task_current(rq, p);
  768. #endif
  769. }
  770. static inline void prepare_lock_switch(struct rq *rq, struct task_struct *next)
  771. {
  772. #ifdef CONFIG_SMP
  773. /*
  774. * We can optimise this out completely for !SMP, because the
  775. * SMP rebalancing from interrupt is the only thing that cares
  776. * here.
  777. */
  778. next->oncpu = 1;
  779. #endif
  780. #ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  781. spin_unlock_irq(&rq->lock);
  782. #else
  783. spin_unlock(&rq->lock);
  784. #endif
  785. }
  786. static inline void finish_lock_switch(struct rq *rq, struct task_struct *prev)
  787. {
  788. #ifdef CONFIG_SMP
  789. /*
  790. * After ->oncpu is cleared, the task can be moved to a different CPU.
  791. * We must ensure this doesn't happen until the switch is completely
  792. * finished.
  793. */
  794. smp_wmb();
  795. prev->oncpu = 0;
  796. #endif
  797. #ifndef __ARCH_WANT_INTERRUPTS_ON_CTXSW
  798. local_irq_enable();
  799. #endif
  800. }
  801. #endif /* __ARCH_WANT_UNLOCKED_CTXSW */
  802. /*
  803. * __task_rq_lock - lock the runqueue a given task resides on.
  804. * Must be called interrupts disabled.
  805. */
  806. static inline struct rq *__task_rq_lock(struct task_struct *p)
  807. __acquires(rq->lock)
  808. {
  809. for (;;) {
  810. struct rq *rq = task_rq(p);
  811. spin_lock(&rq->lock);
  812. if (likely(rq == task_rq(p)))
  813. return rq;
  814. spin_unlock(&rq->lock);
  815. }
  816. }
  817. /*
  818. * task_rq_lock - lock the runqueue a given task resides on and disable
  819. * interrupts. Note the ordering: we can safely lookup the task_rq without
  820. * explicitly disabling preemption.
  821. */
  822. static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
  823. __acquires(rq->lock)
  824. {
  825. struct rq *rq;
  826. for (;;) {
  827. local_irq_save(*flags);
  828. rq = task_rq(p);
  829. spin_lock(&rq->lock);
  830. if (likely(rq == task_rq(p)))
  831. return rq;
  832. spin_unlock_irqrestore(&rq->lock, *flags);
  833. }
  834. }
  835. void task_rq_unlock_wait(struct task_struct *p)
  836. {
  837. struct rq *rq = task_rq(p);
  838. smp_mb(); /* spin-unlock-wait is not a full memory barrier */
  839. spin_unlock_wait(&rq->lock);
  840. }
  841. static void __task_rq_unlock(struct rq *rq)
  842. __releases(rq->lock)
  843. {
  844. spin_unlock(&rq->lock);
  845. }
  846. static inline void task_rq_unlock(struct rq *rq, unsigned long *flags)
  847. __releases(rq->lock)
  848. {
  849. spin_unlock_irqrestore(&rq->lock, *flags);
  850. }
  851. /*
  852. * this_rq_lock - lock this runqueue and disable interrupts.
  853. */
  854. static struct rq *this_rq_lock(void)
  855. __acquires(rq->lock)
  856. {
  857. struct rq *rq;
  858. local_irq_disable();
  859. rq = this_rq();
  860. spin_lock(&rq->lock);
  861. return rq;
  862. }
  863. #ifdef CONFIG_SCHED_HRTICK
  864. /*
  865. * Use HR-timers to deliver accurate preemption points.
  866. *
  867. * Its all a bit involved since we cannot program an hrt while holding the
  868. * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
  869. * reschedule event.
  870. *
  871. * When we get rescheduled we reprogram the hrtick_timer outside of the
  872. * rq->lock.
  873. */
  874. /*
  875. * Use hrtick when:
  876. * - enabled by features
  877. * - hrtimer is actually high res
  878. */
  879. static inline int hrtick_enabled(struct rq *rq)
  880. {
  881. if (!sched_feat(HRTICK))
  882. return 0;
  883. if (!cpu_active(cpu_of(rq)))
  884. return 0;
  885. return hrtimer_is_hres_active(&rq->hrtick_timer);
  886. }
  887. static void hrtick_clear(struct rq *rq)
  888. {
  889. if (hrtimer_active(&rq->hrtick_timer))
  890. hrtimer_cancel(&rq->hrtick_timer);
  891. }
  892. /*
  893. * High-resolution timer tick.
  894. * Runs from hardirq context with interrupts disabled.
  895. */
  896. static enum hrtimer_restart hrtick(struct hrtimer *timer)
  897. {
  898. struct rq *rq = container_of(timer, struct rq, hrtick_timer);
  899. WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
  900. spin_lock(&rq->lock);
  901. update_rq_clock(rq);
  902. rq->curr->sched_class->task_tick(rq, rq->curr, 1);
  903. spin_unlock(&rq->lock);
  904. return HRTIMER_NORESTART;
  905. }
  906. #ifdef CONFIG_SMP
  907. /*
  908. * called from hardirq (IPI) context
  909. */
  910. static void __hrtick_start(void *arg)
  911. {
  912. struct rq *rq = arg;
  913. spin_lock(&rq->lock);
  914. hrtimer_restart(&rq->hrtick_timer);
  915. rq->hrtick_csd_pending = 0;
  916. spin_unlock(&rq->lock);
  917. }
  918. /*
  919. * Called to set the hrtick timer state.
  920. *
  921. * called with rq->lock held and irqs disabled
  922. */
  923. static void hrtick_start(struct rq *rq, u64 delay)
  924. {
  925. struct hrtimer *timer = &rq->hrtick_timer;
  926. ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
  927. hrtimer_set_expires(timer, time);
  928. if (rq == this_rq()) {
  929. hrtimer_restart(timer);
  930. } else if (!rq->hrtick_csd_pending) {
  931. __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd);
  932. rq->hrtick_csd_pending = 1;
  933. }
  934. }
  935. static int
  936. hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
  937. {
  938. int cpu = (int)(long)hcpu;
  939. switch (action) {
  940. case CPU_UP_CANCELED:
  941. case CPU_UP_CANCELED_FROZEN:
  942. case CPU_DOWN_PREPARE:
  943. case CPU_DOWN_PREPARE_FROZEN:
  944. case CPU_DEAD:
  945. case CPU_DEAD_FROZEN:
  946. hrtick_clear(cpu_rq(cpu));
  947. return NOTIFY_OK;
  948. }
  949. return NOTIFY_DONE;
  950. }
  951. static __init void init_hrtick(void)
  952. {
  953. hotcpu_notifier(hotplug_hrtick, 0);
  954. }
  955. #else
  956. /*
  957. * Called to set the hrtick timer state.
  958. *
  959. * called with rq->lock held and irqs disabled
  960. */
  961. static void hrtick_start(struct rq *rq, u64 delay)
  962. {
  963. hrtimer_start(&rq->hrtick_timer, ns_to_ktime(delay), HRTIMER_MODE_REL);
  964. }
  965. static inline void init_hrtick(void)
  966. {
  967. }
  968. #endif /* CONFIG_SMP */
  969. static void init_rq_hrtick(struct rq *rq)
  970. {
  971. #ifdef CONFIG_SMP
  972. rq->hrtick_csd_pending = 0;
  973. rq->hrtick_csd.flags = 0;
  974. rq->hrtick_csd.func = __hrtick_start;
  975. rq->hrtick_csd.info = rq;
  976. #endif
  977. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  978. rq->hrtick_timer.function = hrtick;
  979. }
  980. #else /* CONFIG_SCHED_HRTICK */
  981. static inline void hrtick_clear(struct rq *rq)
  982. {
  983. }
  984. static inline void init_rq_hrtick(struct rq *rq)
  985. {
  986. }
  987. static inline void init_hrtick(void)
  988. {
  989. }
  990. #endif /* CONFIG_SCHED_HRTICK */
  991. /*
  992. * resched_task - mark a task 'to be rescheduled now'.
  993. *
  994. * On UP this means the setting of the need_resched flag, on SMP it
  995. * might also involve a cross-CPU call to trigger the scheduler on
  996. * the target CPU.
  997. */
  998. #ifdef CONFIG_SMP
  999. #ifndef tsk_is_polling
  1000. #define tsk_is_polling(t) test_tsk_thread_flag(t, TIF_POLLING_NRFLAG)
  1001. #endif
  1002. static void resched_task(struct task_struct *p)
  1003. {
  1004. int cpu;
  1005. assert_spin_locked(&task_rq(p)->lock);
  1006. if (test_tsk_need_resched(p))
  1007. return;
  1008. set_tsk_need_resched(p);
  1009. cpu = task_cpu(p);
  1010. if (cpu == smp_processor_id())
  1011. return;
  1012. /* NEED_RESCHED must be visible before we test polling */
  1013. smp_mb();
  1014. if (!tsk_is_polling(p))
  1015. smp_send_reschedule(cpu);
  1016. }
  1017. static void resched_cpu(int cpu)
  1018. {
  1019. struct rq *rq = cpu_rq(cpu);
  1020. unsigned long flags;
  1021. if (!spin_trylock_irqsave(&rq->lock, flags))
  1022. return;
  1023. resched_task(cpu_curr(cpu));
  1024. spin_unlock_irqrestore(&rq->lock, flags);
  1025. }
  1026. #ifdef CONFIG_NO_HZ
  1027. /*
  1028. * When add_timer_on() enqueues a timer into the timer wheel of an
  1029. * idle CPU then this timer might expire before the next timer event
  1030. * which is scheduled to wake up that CPU. In case of a completely
  1031. * idle system the next event might even be infinite time into the
  1032. * future. wake_up_idle_cpu() ensures that the CPU is woken up and
  1033. * leaves the inner idle loop so the newly added timer is taken into
  1034. * account when the CPU goes back to idle and evaluates the timer
  1035. * wheel for the next timer event.
  1036. */
  1037. void wake_up_idle_cpu(int cpu)
  1038. {
  1039. struct rq *rq = cpu_rq(cpu);
  1040. if (cpu == smp_processor_id())
  1041. return;
  1042. /*
  1043. * This is safe, as this function is called with the timer
  1044. * wheel base lock of (cpu) held. When the CPU is on the way
  1045. * to idle and has not yet set rq->curr to idle then it will
  1046. * be serialized on the timer wheel base lock and take the new
  1047. * timer into account automatically.
  1048. */
  1049. if (rq->curr != rq->idle)
  1050. return;
  1051. /*
  1052. * We can set TIF_RESCHED on the idle task of the other CPU
  1053. * lockless. The worst case is that the other CPU runs the
  1054. * idle task through an additional NOOP schedule()
  1055. */
  1056. set_tsk_need_resched(rq->idle);
  1057. /* NEED_RESCHED must be visible before we test polling */
  1058. smp_mb();
  1059. if (!tsk_is_polling(rq->idle))
  1060. smp_send_reschedule(cpu);
  1061. }
  1062. #endif /* CONFIG_NO_HZ */
  1063. #else /* !CONFIG_SMP */
  1064. static void resched_task(struct task_struct *p)
  1065. {
  1066. assert_spin_locked(&task_rq(p)->lock);
  1067. set_tsk_need_resched(p);
  1068. }
  1069. #endif /* CONFIG_SMP */
  1070. #if BITS_PER_LONG == 32
  1071. # define WMULT_CONST (~0UL)
  1072. #else
  1073. # define WMULT_CONST (1UL << 32)
  1074. #endif
  1075. #define WMULT_SHIFT 32
  1076. /*
  1077. * Shift right and round:
  1078. */
  1079. #define SRR(x, y) (((x) + (1UL << ((y) - 1))) >> (y))
  1080. /*
  1081. * delta *= weight / lw
  1082. */
  1083. static unsigned long
  1084. calc_delta_mine(unsigned long delta_exec, unsigned long weight,
  1085. struct load_weight *lw)
  1086. {
  1087. u64 tmp;
  1088. if (!lw->inv_weight) {
  1089. if (BITS_PER_LONG > 32 && unlikely(lw->weight >= WMULT_CONST))
  1090. lw->inv_weight = 1;
  1091. else
  1092. lw->inv_weight = 1 + (WMULT_CONST-lw->weight/2)
  1093. / (lw->weight+1);
  1094. }
  1095. tmp = (u64)delta_exec * weight;
  1096. /*
  1097. * Check whether we'd overflow the 64-bit multiplication:
  1098. */
  1099. if (unlikely(tmp > WMULT_CONST))
  1100. tmp = SRR(SRR(tmp, WMULT_SHIFT/2) * lw->inv_weight,
  1101. WMULT_SHIFT/2);
  1102. else
  1103. tmp = SRR(tmp * lw->inv_weight, WMULT_SHIFT);
  1104. return (unsigned long)min(tmp, (u64)(unsigned long)LONG_MAX);
  1105. }
  1106. static inline void update_load_add(struct load_weight *lw, unsigned long inc)
  1107. {
  1108. lw->weight += inc;
  1109. lw->inv_weight = 0;
  1110. }
  1111. static inline void update_load_sub(struct load_weight *lw, unsigned long dec)
  1112. {
  1113. lw->weight -= dec;
  1114. lw->inv_weight = 0;
  1115. }
  1116. /*
  1117. * To aid in avoiding the subversion of "niceness" due to uneven distribution
  1118. * of tasks with abnormal "nice" values across CPUs the contribution that
  1119. * each task makes to its run queue's load is weighted according to its
  1120. * scheduling class and "nice" value. For SCHED_NORMAL tasks this is just a
  1121. * scaled version of the new time slice allocation that they receive on time
  1122. * slice expiry etc.
  1123. */
  1124. #define WEIGHT_IDLEPRIO 3
  1125. #define WMULT_IDLEPRIO 1431655765
  1126. /*
  1127. * Nice levels are multiplicative, with a gentle 10% change for every
  1128. * nice level changed. I.e. when a CPU-bound task goes from nice 0 to
  1129. * nice 1, it will get ~10% less CPU time than another CPU-bound task
  1130. * that remained on nice 0.
  1131. *
  1132. * The "10% effect" is relative and cumulative: from _any_ nice level,
  1133. * if you go up 1 level, it's -10% CPU usage, if you go down 1 level
  1134. * it's +10% CPU usage. (to achieve that we use a multiplier of 1.25.
  1135. * If a task goes up by ~10% and another task goes down by ~10% then
  1136. * the relative distance between them is ~25%.)
  1137. */
  1138. static const int prio_to_weight[40] = {
  1139. /* -20 */ 88761, 71755, 56483, 46273, 36291,
  1140. /* -15 */ 29154, 23254, 18705, 14949, 11916,
  1141. /* -10 */ 9548, 7620, 6100, 4904, 3906,
  1142. /* -5 */ 3121, 2501, 1991, 1586, 1277,
  1143. /* 0 */ 1024, 820, 655, 526, 423,
  1144. /* 5 */ 335, 272, 215, 172, 137,
  1145. /* 10 */ 110, 87, 70, 56, 45,
  1146. /* 15 */ 36, 29, 23, 18, 15,
  1147. };
  1148. /*
  1149. * Inverse (2^32/x) values of the prio_to_weight[] array, precalculated.
  1150. *
  1151. * In cases where the weight does not change often, we can use the
  1152. * precalculated inverse to speed up arithmetics by turning divisions
  1153. * into multiplications:
  1154. */
  1155. static const u32 prio_to_wmult[40] = {
  1156. /* -20 */ 48388, 59856, 76040, 92818, 118348,
  1157. /* -15 */ 147320, 184698, 229616, 287308, 360437,
  1158. /* -10 */ 449829, 563644, 704093, 875809, 1099582,
  1159. /* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
  1160. /* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
  1161. /* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
  1162. /* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
  1163. /* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
  1164. };
  1165. static void activate_task(struct rq *rq, struct task_struct *p, int wakeup);
  1166. /*
  1167. * runqueue iterator, to support SMP load-balancing between different
  1168. * scheduling classes, without having to expose their internal data
  1169. * structures to the load-balancing proper:
  1170. */
  1171. struct rq_iterator {
  1172. void *arg;
  1173. struct task_struct *(*start)(void *);
  1174. struct task_struct *(*next)(void *);
  1175. };
  1176. #ifdef CONFIG_SMP
  1177. static unsigned long
  1178. balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1179. unsigned long max_load_move, struct sched_domain *sd,
  1180. enum cpu_idle_type idle, int *all_pinned,
  1181. int *this_best_prio, struct rq_iterator *iterator);
  1182. static int
  1183. iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  1184. struct sched_domain *sd, enum cpu_idle_type idle,
  1185. struct rq_iterator *iterator);
  1186. #endif
  1187. #ifdef CONFIG_CGROUP_CPUACCT
  1188. static void cpuacct_charge(struct task_struct *tsk, u64 cputime);
  1189. #else
  1190. static inline void cpuacct_charge(struct task_struct *tsk, u64 cputime) {}
  1191. #endif
  1192. static inline void inc_cpu_load(struct rq *rq, unsigned long load)
  1193. {
  1194. update_load_add(&rq->load, load);
  1195. }
  1196. static inline void dec_cpu_load(struct rq *rq, unsigned long load)
  1197. {
  1198. update_load_sub(&rq->load, load);
  1199. }
  1200. #if (defined(CONFIG_SMP) && defined(CONFIG_FAIR_GROUP_SCHED)) || defined(CONFIG_RT_GROUP_SCHED)
  1201. typedef int (*tg_visitor)(struct task_group *, void *);
  1202. /*
  1203. * Iterate the full tree, calling @down when first entering a node and @up when
  1204. * leaving it for the final time.
  1205. */
  1206. static int walk_tg_tree(tg_visitor down, tg_visitor up, void *data)
  1207. {
  1208. struct task_group *parent, *child;
  1209. int ret;
  1210. rcu_read_lock();
  1211. parent = &root_task_group;
  1212. down:
  1213. ret = (*down)(parent, data);
  1214. if (ret)
  1215. goto out_unlock;
  1216. list_for_each_entry_rcu(child, &parent->children, siblings) {
  1217. parent = child;
  1218. goto down;
  1219. up:
  1220. continue;
  1221. }
  1222. ret = (*up)(parent, data);
  1223. if (ret)
  1224. goto out_unlock;
  1225. child = parent;
  1226. parent = parent->parent;
  1227. if (parent)
  1228. goto up;
  1229. out_unlock:
  1230. rcu_read_unlock();
  1231. return ret;
  1232. }
  1233. static int tg_nop(struct task_group *tg, void *data)
  1234. {
  1235. return 0;
  1236. }
  1237. #endif
  1238. #ifdef CONFIG_SMP
  1239. static unsigned long source_load(int cpu, int type);
  1240. static unsigned long target_load(int cpu, int type);
  1241. static int task_hot(struct task_struct *p, u64 now, struct sched_domain *sd);
  1242. static unsigned long cpu_avg_load_per_task(int cpu)
  1243. {
  1244. struct rq *rq = cpu_rq(cpu);
  1245. unsigned long nr_running = ACCESS_ONCE(rq->nr_running);
  1246. if (nr_running)
  1247. rq->avg_load_per_task = rq->load.weight / nr_running;
  1248. else
  1249. rq->avg_load_per_task = 0;
  1250. return rq->avg_load_per_task;
  1251. }
  1252. #ifdef CONFIG_FAIR_GROUP_SCHED
  1253. static void __set_se_shares(struct sched_entity *se, unsigned long shares);
  1254. /*
  1255. * Calculate and set the cpu's group shares.
  1256. */
  1257. static void
  1258. update_group_shares_cpu(struct task_group *tg, int cpu,
  1259. unsigned long sd_shares, unsigned long sd_rq_weight)
  1260. {
  1261. unsigned long shares;
  1262. unsigned long rq_weight;
  1263. if (!tg->se[cpu])
  1264. return;
  1265. rq_weight = tg->cfs_rq[cpu]->rq_weight;
  1266. /*
  1267. * \Sum shares * rq_weight
  1268. * shares = -----------------------
  1269. * \Sum rq_weight
  1270. *
  1271. */
  1272. shares = (sd_shares * rq_weight) / sd_rq_weight;
  1273. shares = clamp_t(unsigned long, shares, MIN_SHARES, MAX_SHARES);
  1274. if (abs(shares - tg->se[cpu]->load.weight) >
  1275. sysctl_sched_shares_thresh) {
  1276. struct rq *rq = cpu_rq(cpu);
  1277. unsigned long flags;
  1278. spin_lock_irqsave(&rq->lock, flags);
  1279. tg->cfs_rq[cpu]->shares = shares;
  1280. __set_se_shares(tg->se[cpu], shares);
  1281. spin_unlock_irqrestore(&rq->lock, flags);
  1282. }
  1283. }
  1284. /*
  1285. * Re-compute the task group their per cpu shares over the given domain.
  1286. * This needs to be done in a bottom-up fashion because the rq weight of a
  1287. * parent group depends on the shares of its child groups.
  1288. */
  1289. static int tg_shares_up(struct task_group *tg, void *data)
  1290. {
  1291. unsigned long weight, rq_weight = 0;
  1292. unsigned long shares = 0;
  1293. struct sched_domain *sd = data;
  1294. int i;
  1295. for_each_cpu(i, sched_domain_span(sd)) {
  1296. /*
  1297. * If there are currently no tasks on the cpu pretend there
  1298. * is one of average load so that when a new task gets to
  1299. * run here it will not get delayed by group starvation.
  1300. */
  1301. weight = tg->cfs_rq[i]->load.weight;
  1302. if (!weight)
  1303. weight = NICE_0_LOAD;
  1304. tg->cfs_rq[i]->rq_weight = weight;
  1305. rq_weight += weight;
  1306. shares += tg->cfs_rq[i]->shares;
  1307. }
  1308. if ((!shares && rq_weight) || shares > tg->shares)
  1309. shares = tg->shares;
  1310. if (!sd->parent || !(sd->parent->flags & SD_LOAD_BALANCE))
  1311. shares = tg->shares;
  1312. for_each_cpu(i, sched_domain_span(sd))
  1313. update_group_shares_cpu(tg, i, shares, rq_weight);
  1314. return 0;
  1315. }
  1316. /*
  1317. * Compute the cpu's hierarchical load factor for each task group.
  1318. * This needs to be done in a top-down fashion because the load of a child
  1319. * group is a fraction of its parents load.
  1320. */
  1321. static int tg_load_down(struct task_group *tg, void *data)
  1322. {
  1323. unsigned long load;
  1324. long cpu = (long)data;
  1325. if (!tg->parent) {
  1326. load = cpu_rq(cpu)->load.weight;
  1327. } else {
  1328. load = tg->parent->cfs_rq[cpu]->h_load;
  1329. load *= tg->cfs_rq[cpu]->shares;
  1330. load /= tg->parent->cfs_rq[cpu]->load.weight + 1;
  1331. }
  1332. tg->cfs_rq[cpu]->h_load = load;
  1333. return 0;
  1334. }
  1335. static void update_shares(struct sched_domain *sd)
  1336. {
  1337. u64 now = cpu_clock(raw_smp_processor_id());
  1338. s64 elapsed = now - sd->last_update;
  1339. if (elapsed >= (s64)(u64)sysctl_sched_shares_ratelimit) {
  1340. sd->last_update = now;
  1341. walk_tg_tree(tg_nop, tg_shares_up, sd);
  1342. }
  1343. }
  1344. static void update_shares_locked(struct rq *rq, struct sched_domain *sd)
  1345. {
  1346. spin_unlock(&rq->lock);
  1347. update_shares(sd);
  1348. spin_lock(&rq->lock);
  1349. }
  1350. static void update_h_load(long cpu)
  1351. {
  1352. walk_tg_tree(tg_load_down, tg_nop, (void *)cpu);
  1353. }
  1354. #else
  1355. static inline void update_shares(struct sched_domain *sd)
  1356. {
  1357. }
  1358. static inline void update_shares_locked(struct rq *rq, struct sched_domain *sd)
  1359. {
  1360. }
  1361. #endif
  1362. #ifdef CONFIG_PREEMPT
  1363. /*
  1364. * fair double_lock_balance: Safely acquires both rq->locks in a fair
  1365. * way at the expense of forcing extra atomic operations in all
  1366. * invocations. This assures that the double_lock is acquired using the
  1367. * same underlying policy as the spinlock_t on this architecture, which
  1368. * reduces latency compared to the unfair variant below. However, it
  1369. * also adds more overhead and therefore may reduce throughput.
  1370. */
  1371. static inline int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1372. __releases(this_rq->lock)
  1373. __acquires(busiest->lock)
  1374. __acquires(this_rq->lock)
  1375. {
  1376. spin_unlock(&this_rq->lock);
  1377. double_rq_lock(this_rq, busiest);
  1378. return 1;
  1379. }
  1380. #else
  1381. /*
  1382. * Unfair double_lock_balance: Optimizes throughput at the expense of
  1383. * latency by eliminating extra atomic operations when the locks are
  1384. * already in proper order on entry. This favors lower cpu-ids and will
  1385. * grant the double lock to lower cpus over higher ids under contention,
  1386. * regardless of entry order into the function.
  1387. */
  1388. static int _double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1389. __releases(this_rq->lock)
  1390. __acquires(busiest->lock)
  1391. __acquires(this_rq->lock)
  1392. {
  1393. int ret = 0;
  1394. if (unlikely(!spin_trylock(&busiest->lock))) {
  1395. if (busiest < this_rq) {
  1396. spin_unlock(&this_rq->lock);
  1397. spin_lock(&busiest->lock);
  1398. spin_lock_nested(&this_rq->lock, SINGLE_DEPTH_NESTING);
  1399. ret = 1;
  1400. } else
  1401. spin_lock_nested(&busiest->lock, SINGLE_DEPTH_NESTING);
  1402. }
  1403. return ret;
  1404. }
  1405. #endif /* CONFIG_PREEMPT */
  1406. /*
  1407. * double_lock_balance - lock the busiest runqueue, this_rq is locked already.
  1408. */
  1409. static int double_lock_balance(struct rq *this_rq, struct rq *busiest)
  1410. {
  1411. if (unlikely(!irqs_disabled())) {
  1412. /* printk() doesn't work good under rq->lock */
  1413. spin_unlock(&this_rq->lock);
  1414. BUG_ON(1);
  1415. }
  1416. return _double_lock_balance(this_rq, busiest);
  1417. }
  1418. static inline void double_unlock_balance(struct rq *this_rq, struct rq *busiest)
  1419. __releases(busiest->lock)
  1420. {
  1421. spin_unlock(&busiest->lock);
  1422. lock_set_subclass(&this_rq->lock.dep_map, 0, _RET_IP_);
  1423. }
  1424. #endif
  1425. #ifdef CONFIG_FAIR_GROUP_SCHED
  1426. static void cfs_rq_set_shares(struct cfs_rq *cfs_rq, unsigned long shares)
  1427. {
  1428. #ifdef CONFIG_SMP
  1429. cfs_rq->shares = shares;
  1430. #endif
  1431. }
  1432. #endif
  1433. #include "sched_stats.h"
  1434. #include "sched_idletask.c"
  1435. #include "sched_fair.c"
  1436. #include "sched_rt.c"
  1437. #ifdef CONFIG_SCHED_DEBUG
  1438. # include "sched_debug.c"
  1439. #endif
  1440. #define sched_class_highest (&rt_sched_class)
  1441. #define for_each_class(class) \
  1442. for (class = sched_class_highest; class; class = class->next)
  1443. static void inc_nr_running(struct rq *rq)
  1444. {
  1445. rq->nr_running++;
  1446. }
  1447. static void dec_nr_running(struct rq *rq)
  1448. {
  1449. rq->nr_running--;
  1450. }
  1451. static void set_load_weight(struct task_struct *p)
  1452. {
  1453. if (task_has_rt_policy(p)) {
  1454. p->se.load.weight = prio_to_weight[0] * 2;
  1455. p->se.load.inv_weight = prio_to_wmult[0] >> 1;
  1456. return;
  1457. }
  1458. /*
  1459. * SCHED_IDLE tasks get minimal weight:
  1460. */
  1461. if (p->policy == SCHED_IDLE) {
  1462. p->se.load.weight = WEIGHT_IDLEPRIO;
  1463. p->se.load.inv_weight = WMULT_IDLEPRIO;
  1464. return;
  1465. }
  1466. p->se.load.weight = prio_to_weight[p->static_prio - MAX_RT_PRIO];
  1467. p->se.load.inv_weight = prio_to_wmult[p->static_prio - MAX_RT_PRIO];
  1468. }
  1469. static void update_avg(u64 *avg, u64 sample)
  1470. {
  1471. s64 diff = sample - *avg;
  1472. *avg += diff >> 3;
  1473. }
  1474. static void enqueue_task(struct rq *rq, struct task_struct *p, int wakeup)
  1475. {
  1476. if (wakeup)
  1477. p->se.start_runtime = p->se.sum_exec_runtime;
  1478. sched_info_queued(p);
  1479. p->sched_class->enqueue_task(rq, p, wakeup);
  1480. p->se.on_rq = 1;
  1481. }
  1482. static void dequeue_task(struct rq *rq, struct task_struct *p, int sleep)
  1483. {
  1484. if (sleep) {
  1485. if (p->se.last_wakeup) {
  1486. update_avg(&p->se.avg_overlap,
  1487. p->se.sum_exec_runtime - p->se.last_wakeup);
  1488. p->se.last_wakeup = 0;
  1489. } else {
  1490. update_avg(&p->se.avg_wakeup,
  1491. sysctl_sched_wakeup_granularity);
  1492. }
  1493. }
  1494. sched_info_dequeued(p);
  1495. p->sched_class->dequeue_task(rq, p, sleep);
  1496. p->se.on_rq = 0;
  1497. }
  1498. /*
  1499. * __normal_prio - return the priority that is based on the static prio
  1500. */
  1501. static inline int __normal_prio(struct task_struct *p)
  1502. {
  1503. return p->static_prio;
  1504. }
  1505. /*
  1506. * Calculate the expected normal priority: i.e. priority
  1507. * without taking RT-inheritance into account. Might be
  1508. * boosted by interactivity modifiers. Changes upon fork,
  1509. * setprio syscalls, and whenever the interactivity
  1510. * estimator recalculates.
  1511. */
  1512. static inline int normal_prio(struct task_struct *p)
  1513. {
  1514. int prio;
  1515. if (task_has_rt_policy(p))
  1516. prio = MAX_RT_PRIO-1 - p->rt_priority;
  1517. else
  1518. prio = __normal_prio(p);
  1519. return prio;
  1520. }
  1521. /*
  1522. * Calculate the current priority, i.e. the priority
  1523. * taken into account by the scheduler. This value might
  1524. * be boosted by RT tasks, or might be boosted by
  1525. * interactivity modifiers. Will be RT if the task got
  1526. * RT-boosted. If not then it returns p->normal_prio.
  1527. */
  1528. static int effective_prio(struct task_struct *p)
  1529. {
  1530. p->normal_prio = normal_prio(p);
  1531. /*
  1532. * If we are RT tasks or we were boosted to RT priority,
  1533. * keep the priority unchanged. Otherwise, update priority
  1534. * to the normal priority:
  1535. */
  1536. if (!rt_prio(p->prio))
  1537. return p->normal_prio;
  1538. return p->prio;
  1539. }
  1540. /*
  1541. * activate_task - move a task to the runqueue.
  1542. */
  1543. static void activate_task(struct rq *rq, struct task_struct *p, int wakeup)
  1544. {
  1545. if (task_contributes_to_load(p))
  1546. rq->nr_uninterruptible--;
  1547. enqueue_task(rq, p, wakeup);
  1548. inc_nr_running(rq);
  1549. }
  1550. /*
  1551. * deactivate_task - remove a task from the runqueue.
  1552. */
  1553. static void deactivate_task(struct rq *rq, struct task_struct *p, int sleep)
  1554. {
  1555. if (task_contributes_to_load(p))
  1556. rq->nr_uninterruptible++;
  1557. dequeue_task(rq, p, sleep);
  1558. dec_nr_running(rq);
  1559. }
  1560. /**
  1561. * task_curr - is this task currently executing on a CPU?
  1562. * @p: the task in question.
  1563. */
  1564. inline int task_curr(const struct task_struct *p)
  1565. {
  1566. return cpu_curr(task_cpu(p)) == p;
  1567. }
  1568. static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
  1569. {
  1570. set_task_rq(p, cpu);
  1571. #ifdef CONFIG_SMP
  1572. /*
  1573. * After ->cpu is set up to a new value, task_rq_lock(p, ...) can be
  1574. * successfuly executed on another CPU. We must ensure that updates of
  1575. * per-task data have been completed by this moment.
  1576. */
  1577. smp_wmb();
  1578. task_thread_info(p)->cpu = cpu;
  1579. #endif
  1580. }
  1581. static inline void check_class_changed(struct rq *rq, struct task_struct *p,
  1582. const struct sched_class *prev_class,
  1583. int oldprio, int running)
  1584. {
  1585. if (prev_class != p->sched_class) {
  1586. if (prev_class->switched_from)
  1587. prev_class->switched_from(rq, p, running);
  1588. p->sched_class->switched_to(rq, p, running);
  1589. } else
  1590. p->sched_class->prio_changed(rq, p, oldprio, running);
  1591. }
  1592. #ifdef CONFIG_SMP
  1593. /* Used instead of source_load when we know the type == 0 */
  1594. static unsigned long weighted_cpuload(const int cpu)
  1595. {
  1596. return cpu_rq(cpu)->load.weight;
  1597. }
  1598. /*
  1599. * Is this task likely cache-hot:
  1600. */
  1601. static int
  1602. task_hot(struct task_struct *p, u64 now, struct sched_domain *sd)
  1603. {
  1604. s64 delta;
  1605. /*
  1606. * Buddy candidates are cache hot:
  1607. */
  1608. if (sched_feat(CACHE_HOT_BUDDY) &&
  1609. (&p->se == cfs_rq_of(&p->se)->next ||
  1610. &p->se == cfs_rq_of(&p->se)->last))
  1611. return 1;
  1612. if (p->sched_class != &fair_sched_class)
  1613. return 0;
  1614. if (sysctl_sched_migration_cost == -1)
  1615. return 1;
  1616. if (sysctl_sched_migration_cost == 0)
  1617. return 0;
  1618. delta = now - p->se.exec_start;
  1619. return delta < (s64)sysctl_sched_migration_cost;
  1620. }
  1621. void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
  1622. {
  1623. int old_cpu = task_cpu(p);
  1624. struct rq *old_rq = cpu_rq(old_cpu), *new_rq = cpu_rq(new_cpu);
  1625. struct cfs_rq *old_cfsrq = task_cfs_rq(p),
  1626. *new_cfsrq = cpu_cfs_rq(old_cfsrq, new_cpu);
  1627. u64 clock_offset;
  1628. clock_offset = old_rq->clock - new_rq->clock;
  1629. trace_sched_migrate_task(p, task_cpu(p), new_cpu);
  1630. #ifdef CONFIG_SCHEDSTATS
  1631. if (p->se.wait_start)
  1632. p->se.wait_start -= clock_offset;
  1633. if (p->se.sleep_start)
  1634. p->se.sleep_start -= clock_offset;
  1635. if (p->se.block_start)
  1636. p->se.block_start -= clock_offset;
  1637. if (old_cpu != new_cpu) {
  1638. schedstat_inc(p, se.nr_migrations);
  1639. if (task_hot(p, old_rq->clock, NULL))
  1640. schedstat_inc(p, se.nr_forced2_migrations);
  1641. }
  1642. #endif
  1643. p->se.vruntime -= old_cfsrq->min_vruntime -
  1644. new_cfsrq->min_vruntime;
  1645. __set_task_cpu(p, new_cpu);
  1646. }
  1647. struct migration_req {
  1648. struct list_head list;
  1649. struct task_struct *task;
  1650. int dest_cpu;
  1651. struct completion done;
  1652. };
  1653. /*
  1654. * The task's runqueue lock must be held.
  1655. * Returns true if you have to wait for migration thread.
  1656. */
  1657. static int
  1658. migrate_task(struct task_struct *p, int dest_cpu, struct migration_req *req)
  1659. {
  1660. struct rq *rq = task_rq(p);
  1661. /*
  1662. * If the task is not on a runqueue (and not running), then
  1663. * it is sufficient to simply update the task's cpu field.
  1664. */
  1665. if (!p->se.on_rq && !task_running(rq, p)) {
  1666. set_task_cpu(p, dest_cpu);
  1667. return 0;
  1668. }
  1669. init_completion(&req->done);
  1670. req->task = p;
  1671. req->dest_cpu = dest_cpu;
  1672. list_add(&req->list, &rq->migration_queue);
  1673. return 1;
  1674. }
  1675. /*
  1676. * wait_task_inactive - wait for a thread to unschedule.
  1677. *
  1678. * If @match_state is nonzero, it's the @p->state value just checked and
  1679. * not expected to change. If it changes, i.e. @p might have woken up,
  1680. * then return zero. When we succeed in waiting for @p to be off its CPU,
  1681. * we return a positive number (its total switch count). If a second call
  1682. * a short while later returns the same number, the caller can be sure that
  1683. * @p has remained unscheduled the whole time.
  1684. *
  1685. * The caller must ensure that the task *will* unschedule sometime soon,
  1686. * else this function might spin for a *long* time. This function can't
  1687. * be called with interrupts off, or it may introduce deadlock with
  1688. * smp_call_function() if an IPI is sent by the same process we are
  1689. * waiting to become inactive.
  1690. */
  1691. unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  1692. {
  1693. unsigned long flags;
  1694. int running, on_rq;
  1695. unsigned long ncsw;
  1696. struct rq *rq;
  1697. for (;;) {
  1698. /*
  1699. * We do the initial early heuristics without holding
  1700. * any task-queue locks at all. We'll only try to get
  1701. * the runqueue lock when things look like they will
  1702. * work out!
  1703. */
  1704. rq = task_rq(p);
  1705. /*
  1706. * If the task is actively running on another CPU
  1707. * still, just relax and busy-wait without holding
  1708. * any locks.
  1709. *
  1710. * NOTE! Since we don't hold any locks, it's not
  1711. * even sure that "rq" stays as the right runqueue!
  1712. * But we don't care, since "task_running()" will
  1713. * return false if the runqueue has changed and p
  1714. * is actually now running somewhere else!
  1715. */
  1716. while (task_running(rq, p)) {
  1717. if (match_state && unlikely(p->state != match_state))
  1718. return 0;
  1719. cpu_relax();
  1720. }
  1721. /*
  1722. * Ok, time to look more closely! We need the rq
  1723. * lock now, to be *sure*. If we're wrong, we'll
  1724. * just go back and repeat.
  1725. */
  1726. rq = task_rq_lock(p, &flags);
  1727. trace_sched_wait_task(rq, p);
  1728. running = task_running(rq, p);
  1729. on_rq = p->se.on_rq;
  1730. ncsw = 0;
  1731. if (!match_state || p->state == match_state)
  1732. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  1733. task_rq_unlock(rq, &flags);
  1734. /*
  1735. * If it changed from the expected state, bail out now.
  1736. */
  1737. if (unlikely(!ncsw))
  1738. break;
  1739. /*
  1740. * Was it really running after all now that we
  1741. * checked with the proper locks actually held?
  1742. *
  1743. * Oops. Go back and try again..
  1744. */
  1745. if (unlikely(running)) {
  1746. cpu_relax();
  1747. continue;
  1748. }
  1749. /*
  1750. * It's not enough that it's not actively running,
  1751. * it must be off the runqueue _entirely_, and not
  1752. * preempted!
  1753. *
  1754. * So if it was still runnable (but just not actively
  1755. * running right now), it's preempted, and we should
  1756. * yield - it could be a while.
  1757. */
  1758. if (unlikely(on_rq)) {
  1759. schedule_timeout_uninterruptible(1);
  1760. continue;
  1761. }
  1762. /*
  1763. * Ahh, all good. It wasn't running, and it wasn't
  1764. * runnable, which means that it will never become
  1765. * running in the future either. We're all done!
  1766. */
  1767. break;
  1768. }
  1769. return ncsw;
  1770. }
  1771. /***
  1772. * kick_process - kick a running thread to enter/exit the kernel
  1773. * @p: the to-be-kicked thread
  1774. *
  1775. * Cause a process which is running on another CPU to enter
  1776. * kernel-mode, without any delay. (to get signals handled.)
  1777. *
  1778. * NOTE: this function doesnt have to take the runqueue lock,
  1779. * because all it wants to ensure is that the remote task enters
  1780. * the kernel. If the IPI races and the task has been migrated
  1781. * to another CPU then no harm is done and the purpose has been
  1782. * achieved as well.
  1783. */
  1784. void kick_process(struct task_struct *p)
  1785. {
  1786. int cpu;
  1787. preempt_disable();
  1788. cpu = task_cpu(p);
  1789. if ((cpu != smp_processor_id()) && task_curr(p))
  1790. smp_send_reschedule(cpu);
  1791. preempt_enable();
  1792. }
  1793. /*
  1794. * Return a low guess at the load of a migration-source cpu weighted
  1795. * according to the scheduling class and "nice" value.
  1796. *
  1797. * We want to under-estimate the load of migration sources, to
  1798. * balance conservatively.
  1799. */
  1800. static unsigned long source_load(int cpu, int type)
  1801. {
  1802. struct rq *rq = cpu_rq(cpu);
  1803. unsigned long total = weighted_cpuload(cpu);
  1804. if (type == 0 || !sched_feat(LB_BIAS))
  1805. return total;
  1806. return min(rq->cpu_load[type-1], total);
  1807. }
  1808. /*
  1809. * Return a high guess at the load of a migration-target cpu weighted
  1810. * according to the scheduling class and "nice" value.
  1811. */
  1812. static unsigned long target_load(int cpu, int type)
  1813. {
  1814. struct rq *rq = cpu_rq(cpu);
  1815. unsigned long total = weighted_cpuload(cpu);
  1816. if (type == 0 || !sched_feat(LB_BIAS))
  1817. return total;
  1818. return max(rq->cpu_load[type-1], total);
  1819. }
  1820. /*
  1821. * find_idlest_group finds and returns the least busy CPU group within the
  1822. * domain.
  1823. */
  1824. static struct sched_group *
  1825. find_idlest_group(struct sched_domain *sd, struct task_struct *p, int this_cpu)
  1826. {
  1827. struct sched_group *idlest = NULL, *this = NULL, *group = sd->groups;
  1828. unsigned long min_load = ULONG_MAX, this_load = 0;
  1829. int load_idx = sd->forkexec_idx;
  1830. int imbalance = 100 + (sd->imbalance_pct-100)/2;
  1831. do {
  1832. unsigned long load, avg_load;
  1833. int local_group;
  1834. int i;
  1835. /* Skip over this group if it has no CPUs allowed */
  1836. if (!cpumask_intersects(sched_group_cpus(group),
  1837. &p->cpus_allowed))
  1838. continue;
  1839. local_group = cpumask_test_cpu(this_cpu,
  1840. sched_group_cpus(group));
  1841. /* Tally up the load of all CPUs in the group */
  1842. avg_load = 0;
  1843. for_each_cpu(i, sched_group_cpus(group)) {
  1844. /* Bias balancing toward cpus of our domain */
  1845. if (local_group)
  1846. load = source_load(i, load_idx);
  1847. else
  1848. load = target_load(i, load_idx);
  1849. avg_load += load;
  1850. }
  1851. /* Adjust by relative CPU power of the group */
  1852. avg_load = sg_div_cpu_power(group,
  1853. avg_load * SCHED_LOAD_SCALE);
  1854. if (local_group) {
  1855. this_load = avg_load;
  1856. this = group;
  1857. } else if (avg_load < min_load) {
  1858. min_load = avg_load;
  1859. idlest = group;
  1860. }
  1861. } while (group = group->next, group != sd->groups);
  1862. if (!idlest || 100*this_load < imbalance*min_load)
  1863. return NULL;
  1864. return idlest;
  1865. }
  1866. /*
  1867. * find_idlest_cpu - find the idlest cpu among the cpus in group.
  1868. */
  1869. static int
  1870. find_idlest_cpu(struct sched_group *group, struct task_struct *p, int this_cpu)
  1871. {
  1872. unsigned long load, min_load = ULONG_MAX;
  1873. int idlest = -1;
  1874. int i;
  1875. /* Traverse only the allowed CPUs */
  1876. for_each_cpu_and(i, sched_group_cpus(group), &p->cpus_allowed) {
  1877. load = weighted_cpuload(i);
  1878. if (load < min_load || (load == min_load && i == this_cpu)) {
  1879. min_load = load;
  1880. idlest = i;
  1881. }
  1882. }
  1883. return idlest;
  1884. }
  1885. /*
  1886. * sched_balance_self: balance the current task (running on cpu) in domains
  1887. * that have the 'flag' flag set. In practice, this is SD_BALANCE_FORK and
  1888. * SD_BALANCE_EXEC.
  1889. *
  1890. * Balance, ie. select the least loaded group.
  1891. *
  1892. * Returns the target CPU number, or the same CPU if no balancing is needed.
  1893. *
  1894. * preempt must be disabled.
  1895. */
  1896. static int sched_balance_self(int cpu, int flag)
  1897. {
  1898. struct task_struct *t = current;
  1899. struct sched_domain *tmp, *sd = NULL;
  1900. for_each_domain(cpu, tmp) {
  1901. /*
  1902. * If power savings logic is enabled for a domain, stop there.
  1903. */
  1904. if (tmp->flags & SD_POWERSAVINGS_BALANCE)
  1905. break;
  1906. if (tmp->flags & flag)
  1907. sd = tmp;
  1908. }
  1909. if (sd)
  1910. update_shares(sd);
  1911. while (sd) {
  1912. struct sched_group *group;
  1913. int new_cpu, weight;
  1914. if (!(sd->flags & flag)) {
  1915. sd = sd->child;
  1916. continue;
  1917. }
  1918. group = find_idlest_group(sd, t, cpu);
  1919. if (!group) {
  1920. sd = sd->child;
  1921. continue;
  1922. }
  1923. new_cpu = find_idlest_cpu(group, t, cpu);
  1924. if (new_cpu == -1 || new_cpu == cpu) {
  1925. /* Now try balancing at a lower domain level of cpu */
  1926. sd = sd->child;
  1927. continue;
  1928. }
  1929. /* Now try balancing at a lower domain level of new_cpu */
  1930. cpu = new_cpu;
  1931. weight = cpumask_weight(sched_domain_span(sd));
  1932. sd = NULL;
  1933. for_each_domain(cpu, tmp) {
  1934. if (weight <= cpumask_weight(sched_domain_span(tmp)))
  1935. break;
  1936. if (tmp->flags & flag)
  1937. sd = tmp;
  1938. }
  1939. /* while loop will break here if sd == NULL */
  1940. }
  1941. return cpu;
  1942. }
  1943. #endif /* CONFIG_SMP */
  1944. /***
  1945. * try_to_wake_up - wake up a thread
  1946. * @p: the to-be-woken-up thread
  1947. * @state: the mask of task states that can be woken
  1948. * @sync: do a synchronous wakeup?
  1949. *
  1950. * Put it on the run-queue if it's not already there. The "current"
  1951. * thread is always on the run-queue (except when the actual
  1952. * re-schedule is in progress), and as such you're allowed to do
  1953. * the simpler "current->state = TASK_RUNNING" to mark yourself
  1954. * runnable without the overhead of this.
  1955. *
  1956. * returns failure only if the task is already active.
  1957. */
  1958. static int try_to_wake_up(struct task_struct *p, unsigned int state, int sync)
  1959. {
  1960. int cpu, orig_cpu, this_cpu, success = 0;
  1961. unsigned long flags;
  1962. long old_state;
  1963. struct rq *rq;
  1964. if (!sched_feat(SYNC_WAKEUPS))
  1965. sync = 0;
  1966. #ifdef CONFIG_SMP
  1967. if (sched_feat(LB_WAKEUP_UPDATE) && !root_task_group_empty()) {
  1968. struct sched_domain *sd;
  1969. this_cpu = raw_smp_processor_id();
  1970. cpu = task_cpu(p);
  1971. for_each_domain(this_cpu, sd) {
  1972. if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  1973. update_shares(sd);
  1974. break;
  1975. }
  1976. }
  1977. }
  1978. #endif
  1979. smp_wmb();
  1980. rq = task_rq_lock(p, &flags);
  1981. update_rq_clock(rq);
  1982. old_state = p->state;
  1983. if (!(old_state & state))
  1984. goto out;
  1985. if (p->se.on_rq)
  1986. goto out_running;
  1987. cpu = task_cpu(p);
  1988. orig_cpu = cpu;
  1989. this_cpu = smp_processor_id();
  1990. #ifdef CONFIG_SMP
  1991. if (unlikely(task_running(rq, p)))
  1992. goto out_activate;
  1993. cpu = p->sched_class->select_task_rq(p, sync);
  1994. if (cpu != orig_cpu) {
  1995. set_task_cpu(p, cpu);
  1996. task_rq_unlock(rq, &flags);
  1997. /* might preempt at this point */
  1998. rq = task_rq_lock(p, &flags);
  1999. old_state = p->state;
  2000. if (!(old_state & state))
  2001. goto out;
  2002. if (p->se.on_rq)
  2003. goto out_running;
  2004. this_cpu = smp_processor_id();
  2005. cpu = task_cpu(p);
  2006. }
  2007. #ifdef CONFIG_SCHEDSTATS
  2008. schedstat_inc(rq, ttwu_count);
  2009. if (cpu == this_cpu)
  2010. schedstat_inc(rq, ttwu_local);
  2011. else {
  2012. struct sched_domain *sd;
  2013. for_each_domain(this_cpu, sd) {
  2014. if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  2015. schedstat_inc(sd, ttwu_wake_remote);
  2016. break;
  2017. }
  2018. }
  2019. }
  2020. #endif /* CONFIG_SCHEDSTATS */
  2021. out_activate:
  2022. #endif /* CONFIG_SMP */
  2023. schedstat_inc(p, se.nr_wakeups);
  2024. if (sync)
  2025. schedstat_inc(p, se.nr_wakeups_sync);
  2026. if (orig_cpu != cpu)
  2027. schedstat_inc(p, se.nr_wakeups_migrate);
  2028. if (cpu == this_cpu)
  2029. schedstat_inc(p, se.nr_wakeups_local);
  2030. else
  2031. schedstat_inc(p, se.nr_wakeups_remote);
  2032. activate_task(rq, p, 1);
  2033. success = 1;
  2034. /*
  2035. * Only attribute actual wakeups done by this task.
  2036. */
  2037. if (!in_interrupt()) {
  2038. struct sched_entity *se = &current->se;
  2039. u64 sample = se->sum_exec_runtime;
  2040. if (se->last_wakeup)
  2041. sample -= se->last_wakeup;
  2042. else
  2043. sample -= se->start_runtime;
  2044. update_avg(&se->avg_wakeup, sample);
  2045. se->last_wakeup = se->sum_exec_runtime;
  2046. }
  2047. out_running:
  2048. trace_sched_wakeup(rq, p, success);
  2049. check_preempt_curr(rq, p, sync);
  2050. p->state = TASK_RUNNING;
  2051. #ifdef CONFIG_SMP
  2052. if (p->sched_class->task_wake_up)
  2053. p->sched_class->task_wake_up(rq, p);
  2054. #endif
  2055. out:
  2056. task_rq_unlock(rq, &flags);
  2057. return success;
  2058. }
  2059. int wake_up_process(struct task_struct *p)
  2060. {
  2061. return try_to_wake_up(p, TASK_ALL, 0);
  2062. }
  2063. EXPORT_SYMBOL(wake_up_process);
  2064. int wake_up_state(struct task_struct *p, unsigned int state)
  2065. {
  2066. return try_to_wake_up(p, state, 0);
  2067. }
  2068. /*
  2069. * Perform scheduler related setup for a newly forked process p.
  2070. * p is forked by current.
  2071. *
  2072. * __sched_fork() is basic setup used by init_idle() too:
  2073. */
  2074. static void __sched_fork(struct task_struct *p)
  2075. {
  2076. p->se.exec_start = 0;
  2077. p->se.sum_exec_runtime = 0;
  2078. p->se.prev_sum_exec_runtime = 0;
  2079. p->se.last_wakeup = 0;
  2080. p->se.avg_overlap = 0;
  2081. p->se.start_runtime = 0;
  2082. p->se.avg_wakeup = sysctl_sched_wakeup_granularity;
  2083. #ifdef CONFIG_SCHEDSTATS
  2084. p->se.wait_start = 0;
  2085. p->se.sum_sleep_runtime = 0;
  2086. p->se.sleep_start = 0;
  2087. p->se.block_start = 0;
  2088. p->se.sleep_max = 0;
  2089. p->se.block_max = 0;
  2090. p->se.exec_max = 0;
  2091. p->se.slice_max = 0;
  2092. p->se.wait_max = 0;
  2093. #endif
  2094. INIT_LIST_HEAD(&p->rt.run_list);
  2095. p->se.on_rq = 0;
  2096. INIT_LIST_HEAD(&p->se.group_node);
  2097. #ifdef CONFIG_PREEMPT_NOTIFIERS
  2098. INIT_HLIST_HEAD(&p->preempt_notifiers);
  2099. #endif
  2100. /*
  2101. * We mark the process as running here, but have not actually
  2102. * inserted it onto the runqueue yet. This guarantees that
  2103. * nobody will actually run it, and a signal or other external
  2104. * event cannot wake it up and insert it on the runqueue either.
  2105. */
  2106. p->state = TASK_RUNNING;
  2107. }
  2108. /*
  2109. * fork()/clone()-time setup:
  2110. */
  2111. void sched_fork(struct task_struct *p, int clone_flags)
  2112. {
  2113. int cpu = get_cpu();
  2114. __sched_fork(p);
  2115. #ifdef CONFIG_SMP
  2116. cpu = sched_balance_self(cpu, SD_BALANCE_FORK);
  2117. #endif
  2118. set_task_cpu(p, cpu);
  2119. /*
  2120. * Make sure we do not leak PI boosting priority to the child:
  2121. */
  2122. p->prio = current->normal_prio;
  2123. if (!rt_prio(p->prio))
  2124. p->sched_class = &fair_sched_class;
  2125. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  2126. if (likely(sched_info_on()))
  2127. memset(&p->sched_info, 0, sizeof(p->sched_info));
  2128. #endif
  2129. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  2130. p->oncpu = 0;
  2131. #endif
  2132. #ifdef CONFIG_PREEMPT
  2133. /* Want to start with kernel preemption disabled. */
  2134. task_thread_info(p)->preempt_count = 1;
  2135. #endif
  2136. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  2137. put_cpu();
  2138. }
  2139. /*
  2140. * wake_up_new_task - wake up a newly created task for the first time.
  2141. *
  2142. * This function will do some initial scheduler statistics housekeeping
  2143. * that must be done for every newly created context, then puts the task
  2144. * on the runqueue and wakes it.
  2145. */
  2146. void wake_up_new_task(struct task_struct *p, unsigned long clone_flags)
  2147. {
  2148. unsigned long flags;
  2149. struct rq *rq;
  2150. rq = task_rq_lock(p, &flags);
  2151. BUG_ON(p->state != TASK_RUNNING);
  2152. update_rq_clock(rq);
  2153. p->prio = effective_prio(p);
  2154. if (!p->sched_class->task_new || !current->se.on_rq) {
  2155. activate_task(rq, p, 0);
  2156. } else {
  2157. /*
  2158. * Let the scheduling class do new task startup
  2159. * management (if any):
  2160. */
  2161. p->sched_class->task_new(rq, p);
  2162. inc_nr_running(rq);
  2163. }
  2164. trace_sched_wakeup_new(rq, p, 1);
  2165. check_preempt_curr(rq, p, 0);
  2166. #ifdef CONFIG_SMP
  2167. if (p->sched_class->task_wake_up)
  2168. p->sched_class->task_wake_up(rq, p);
  2169. #endif
  2170. task_rq_unlock(rq, &flags);
  2171. }
  2172. #ifdef CONFIG_PREEMPT_NOTIFIERS
  2173. /**
  2174. * preempt_notifier_register - tell me when current is being preempted & rescheduled
  2175. * @notifier: notifier struct to register
  2176. */
  2177. void preempt_notifier_register(struct preempt_notifier *notifier)
  2178. {
  2179. hlist_add_head(&notifier->link, &current->preempt_notifiers);
  2180. }
  2181. EXPORT_SYMBOL_GPL(preempt_notifier_register);
  2182. /**
  2183. * preempt_notifier_unregister - no longer interested in preemption notifications
  2184. * @notifier: notifier struct to unregister
  2185. *
  2186. * This is safe to call from within a preemption notifier.
  2187. */
  2188. void preempt_notifier_unregister(struct preempt_notifier *notifier)
  2189. {
  2190. hlist_del(&notifier->link);
  2191. }
  2192. EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
  2193. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  2194. {
  2195. struct preempt_notifier *notifier;
  2196. struct hlist_node *node;
  2197. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  2198. notifier->ops->sched_in(notifier, raw_smp_processor_id());
  2199. }
  2200. static void
  2201. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  2202. struct task_struct *next)
  2203. {
  2204. struct preempt_notifier *notifier;
  2205. struct hlist_node *node;
  2206. hlist_for_each_entry(notifier, node, &curr->preempt_notifiers, link)
  2207. notifier->ops->sched_out(notifier, next);
  2208. }
  2209. #else /* !CONFIG_PREEMPT_NOTIFIERS */
  2210. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  2211. {
  2212. }
  2213. static void
  2214. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  2215. struct task_struct *next)
  2216. {
  2217. }
  2218. #endif /* CONFIG_PREEMPT_NOTIFIERS */
  2219. /**
  2220. * prepare_task_switch - prepare to switch tasks
  2221. * @rq: the runqueue preparing to switch
  2222. * @prev: the current task that is being switched out
  2223. * @next: the task we are going to switch to.
  2224. *
  2225. * This is called with the rq lock held and interrupts off. It must
  2226. * be paired with a subsequent finish_task_switch after the context
  2227. * switch.
  2228. *
  2229. * prepare_task_switch sets up locking and calls architecture specific
  2230. * hooks.
  2231. */
  2232. static inline void
  2233. prepare_task_switch(struct rq *rq, struct task_struct *prev,
  2234. struct task_struct *next)
  2235. {
  2236. fire_sched_out_preempt_notifiers(prev, next);
  2237. prepare_lock_switch(rq, next);
  2238. prepare_arch_switch(next);
  2239. }
  2240. /**
  2241. * finish_task_switch - clean up after a task-switch
  2242. * @rq: runqueue associated with task-switch
  2243. * @prev: the thread we just switched away from.
  2244. *
  2245. * finish_task_switch must be called after the context switch, paired
  2246. * with a prepare_task_switch call before the context switch.
  2247. * finish_task_switch will reconcile locking set up by prepare_task_switch,
  2248. * and do any other architecture-specific cleanup actions.
  2249. *
  2250. * Note that we may have delayed dropping an mm in context_switch(). If
  2251. * so, we finish that here outside of the runqueue lock. (Doing it
  2252. * with the lock held can cause deadlocks; see schedule() for
  2253. * details.)
  2254. */
  2255. static void finish_task_switch(struct rq *rq, struct task_struct *prev)
  2256. __releases(rq->lock)
  2257. {
  2258. struct mm_struct *mm = rq->prev_mm;
  2259. long prev_state;
  2260. #ifdef CONFIG_SMP
  2261. int post_schedule = 0;
  2262. if (current->sched_class->needs_post_schedule)
  2263. post_schedule = current->sched_class->needs_post_schedule(rq);
  2264. #endif
  2265. rq->prev_mm = NULL;
  2266. /*
  2267. * A task struct has one reference for the use as "current".
  2268. * If a task dies, then it sets TASK_DEAD in tsk->state and calls
  2269. * schedule one last time. The schedule call will never return, and
  2270. * the scheduled task must drop that reference.
  2271. * The test for TASK_DEAD must occur while the runqueue locks are
  2272. * still held, otherwise prev could be scheduled on another cpu, die
  2273. * there before we look at prev->state, and then the reference would
  2274. * be dropped twice.
  2275. * Manfred Spraul <manfred@colorfullife.com>
  2276. */
  2277. prev_state = prev->state;
  2278. finish_arch_switch(prev);
  2279. finish_lock_switch(rq, prev);
  2280. #ifdef CONFIG_SMP
  2281. if (post_schedule)
  2282. current->sched_class->post_schedule(rq);
  2283. #endif
  2284. fire_sched_in_preempt_notifiers(current);
  2285. if (mm)
  2286. mmdrop(mm);
  2287. if (unlikely(prev_state == TASK_DEAD)) {
  2288. /*
  2289. * Remove function-return probe instances associated with this
  2290. * task and put them back on the free list.
  2291. */
  2292. kprobe_flush_task(prev);
  2293. put_task_struct(prev);
  2294. }
  2295. }
  2296. /**
  2297. * schedule_tail - first thing a freshly forked thread must call.
  2298. * @prev: the thread we just switched away from.
  2299. */
  2300. asmlinkage void schedule_tail(struct task_struct *prev)
  2301. __releases(rq->lock)
  2302. {
  2303. struct rq *rq = this_rq();
  2304. finish_task_switch(rq, prev);
  2305. #ifdef __ARCH_WANT_UNLOCKED_CTXSW
  2306. /* In this case, finish_task_switch does not reenable preemption */
  2307. preempt_enable();
  2308. #endif
  2309. if (current->set_child_tid)
  2310. put_user(task_pid_vnr(current), current->set_child_tid);
  2311. }
  2312. /*
  2313. * context_switch - switch to the new MM and the new
  2314. * thread's register state.
  2315. */
  2316. static inline void
  2317. context_switch(struct rq *rq, struct task_struct *prev,
  2318. struct task_struct *next)
  2319. {
  2320. struct mm_struct *mm, *oldmm;
  2321. prepare_task_switch(rq, prev, next);
  2322. trace_sched_switch(rq, prev, next);
  2323. mm = next->mm;
  2324. oldmm = prev->active_mm;
  2325. /*
  2326. * For paravirt, this is coupled with an exit in switch_to to
  2327. * combine the page table reload and the switch backend into
  2328. * one hypercall.
  2329. */
  2330. arch_enter_lazy_cpu_mode();
  2331. if (unlikely(!mm)) {
  2332. next->active_mm = oldmm;
  2333. atomic_inc(&oldmm->mm_count);
  2334. enter_lazy_tlb(oldmm, next);
  2335. } else
  2336. switch_mm(oldmm, mm, next);
  2337. if (unlikely(!prev->mm)) {
  2338. prev->active_mm = NULL;
  2339. rq->prev_mm = oldmm;
  2340. }
  2341. /*
  2342. * Since the runqueue lock will be released by the next
  2343. * task (which is an invalid locking op but in the case
  2344. * of the scheduler it's an obvious special-case), so we
  2345. * do an early lockdep release here:
  2346. */
  2347. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  2348. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  2349. #endif
  2350. /* Here we just switch the register state and the stack. */
  2351. switch_to(prev, next, prev);
  2352. barrier();
  2353. /*
  2354. * this_rq must be evaluated again because prev may have moved
  2355. * CPUs since it called schedule(), thus the 'rq' on its stack
  2356. * frame will be invalid.
  2357. */
  2358. finish_task_switch(this_rq(), prev);
  2359. }
  2360. /*
  2361. * nr_running, nr_uninterruptible and nr_context_switches:
  2362. *
  2363. * externally visible scheduler statistics: current number of runnable
  2364. * threads, current number of uninterruptible-sleeping threads, total
  2365. * number of context switches performed since bootup.
  2366. */
  2367. unsigned long nr_running(void)
  2368. {
  2369. unsigned long i, sum = 0;
  2370. for_each_online_cpu(i)
  2371. sum += cpu_rq(i)->nr_running;
  2372. return sum;
  2373. }
  2374. unsigned long nr_uninterruptible(void)
  2375. {
  2376. unsigned long i, sum = 0;
  2377. for_each_possible_cpu(i)
  2378. sum += cpu_rq(i)->nr_uninterruptible;
  2379. /*
  2380. * Since we read the counters lockless, it might be slightly
  2381. * inaccurate. Do not allow it to go below zero though:
  2382. */
  2383. if (unlikely((long)sum < 0))
  2384. sum = 0;
  2385. return sum;
  2386. }
  2387. unsigned long long nr_context_switches(void)
  2388. {
  2389. int i;
  2390. unsigned long long sum = 0;
  2391. for_each_possible_cpu(i)
  2392. sum += cpu_rq(i)->nr_switches;
  2393. return sum;
  2394. }
  2395. unsigned long nr_iowait(void)
  2396. {
  2397. unsigned long i, sum = 0;
  2398. for_each_possible_cpu(i)
  2399. sum += atomic_read(&cpu_rq(i)->nr_iowait);
  2400. return sum;
  2401. }
  2402. unsigned long nr_active(void)
  2403. {
  2404. unsigned long i, running = 0, uninterruptible = 0;
  2405. for_each_online_cpu(i) {
  2406. running += cpu_rq(i)->nr_running;
  2407. uninterruptible += cpu_rq(i)->nr_uninterruptible;
  2408. }
  2409. if (unlikely((long)uninterruptible < 0))
  2410. uninterruptible = 0;
  2411. return running + uninterruptible;
  2412. }
  2413. /*
  2414. * Update rq->cpu_load[] statistics. This function is usually called every
  2415. * scheduler tick (TICK_NSEC).
  2416. */
  2417. static void update_cpu_load(struct rq *this_rq)
  2418. {
  2419. unsigned long this_load = this_rq->load.weight;
  2420. int i, scale;
  2421. this_rq->nr_load_updates++;
  2422. /* Update our load: */
  2423. for (i = 0, scale = 1; i < CPU_LOAD_IDX_MAX; i++, scale += scale) {
  2424. unsigned long old_load, new_load;
  2425. /* scale is effectively 1 << i now, and >> i divides by scale */
  2426. old_load = this_rq->cpu_load[i];
  2427. new_load = this_load;
  2428. /*
  2429. * Round up the averaging division if load is increasing. This
  2430. * prevents us from getting stuck on 9 if the load is 10, for
  2431. * example.
  2432. */
  2433. if (new_load > old_load)
  2434. new_load += scale-1;
  2435. this_rq->cpu_load[i] = (old_load*(scale-1) + new_load) >> i;
  2436. }
  2437. }
  2438. #ifdef CONFIG_SMP
  2439. /*
  2440. * double_rq_lock - safely lock two runqueues
  2441. *
  2442. * Note this does not disable interrupts like task_rq_lock,
  2443. * you need to do so manually before calling.
  2444. */
  2445. static void double_rq_lock(struct rq *rq1, struct rq *rq2)
  2446. __acquires(rq1->lock)
  2447. __acquires(rq2->lock)
  2448. {
  2449. BUG_ON(!irqs_disabled());
  2450. if (rq1 == rq2) {
  2451. spin_lock(&rq1->lock);
  2452. __acquire(rq2->lock); /* Fake it out ;) */
  2453. } else {
  2454. if (rq1 < rq2) {
  2455. spin_lock(&rq1->lock);
  2456. spin_lock_nested(&rq2->lock, SINGLE_DEPTH_NESTING);
  2457. } else {
  2458. spin_lock(&rq2->lock);
  2459. spin_lock_nested(&rq1->lock, SINGLE_DEPTH_NESTING);
  2460. }
  2461. }
  2462. update_rq_clock(rq1);
  2463. update_rq_clock(rq2);
  2464. }
  2465. /*
  2466. * double_rq_unlock - safely unlock two runqueues
  2467. *
  2468. * Note this does not restore interrupts like task_rq_unlock,
  2469. * you need to do so manually after calling.
  2470. */
  2471. static void double_rq_unlock(struct rq *rq1, struct rq *rq2)
  2472. __releases(rq1->lock)
  2473. __releases(rq2->lock)
  2474. {
  2475. spin_unlock(&rq1->lock);
  2476. if (rq1 != rq2)
  2477. spin_unlock(&rq2->lock);
  2478. else
  2479. __release(rq2->lock);
  2480. }
  2481. /*
  2482. * If dest_cpu is allowed for this process, migrate the task to it.
  2483. * This is accomplished by forcing the cpu_allowed mask to only
  2484. * allow dest_cpu, which will force the cpu onto dest_cpu. Then
  2485. * the cpu_allowed mask is restored.
  2486. */
  2487. static void sched_migrate_task(struct task_struct *p, int dest_cpu)
  2488. {
  2489. struct migration_req req;
  2490. unsigned long flags;
  2491. struct rq *rq;
  2492. rq = task_rq_lock(p, &flags);
  2493. if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed)
  2494. || unlikely(!cpu_active(dest_cpu)))
  2495. goto out;
  2496. /* force the process onto the specified CPU */
  2497. if (migrate_task(p, dest_cpu, &req)) {
  2498. /* Need to wait for migration thread (might exit: take ref). */
  2499. struct task_struct *mt = rq->migration_thread;
  2500. get_task_struct(mt);
  2501. task_rq_unlock(rq, &flags);
  2502. wake_up_process(mt);
  2503. put_task_struct(mt);
  2504. wait_for_completion(&req.done);
  2505. return;
  2506. }
  2507. out:
  2508. task_rq_unlock(rq, &flags);
  2509. }
  2510. /*
  2511. * sched_exec - execve() is a valuable balancing opportunity, because at
  2512. * this point the task has the smallest effective memory and cache footprint.
  2513. */
  2514. void sched_exec(void)
  2515. {
  2516. int new_cpu, this_cpu = get_cpu();
  2517. new_cpu = sched_balance_self(this_cpu, SD_BALANCE_EXEC);
  2518. put_cpu();
  2519. if (new_cpu != this_cpu)
  2520. sched_migrate_task(current, new_cpu);
  2521. }
  2522. /*
  2523. * pull_task - move a task from a remote runqueue to the local runqueue.
  2524. * Both runqueues must be locked.
  2525. */
  2526. static void pull_task(struct rq *src_rq, struct task_struct *p,
  2527. struct rq *this_rq, int this_cpu)
  2528. {
  2529. deactivate_task(src_rq, p, 0);
  2530. set_task_cpu(p, this_cpu);
  2531. activate_task(this_rq, p, 0);
  2532. /*
  2533. * Note that idle threads have a prio of MAX_PRIO, for this test
  2534. * to be always true for them.
  2535. */
  2536. check_preempt_curr(this_rq, p, 0);
  2537. }
  2538. /*
  2539. * can_migrate_task - may task p from runqueue rq be migrated to this_cpu?
  2540. */
  2541. static
  2542. int can_migrate_task(struct task_struct *p, struct rq *rq, int this_cpu,
  2543. struct sched_domain *sd, enum cpu_idle_type idle,
  2544. int *all_pinned)
  2545. {
  2546. int tsk_cache_hot = 0;
  2547. /*
  2548. * We do not migrate tasks that are:
  2549. * 1) running (obviously), or
  2550. * 2) cannot be migrated to this CPU due to cpus_allowed, or
  2551. * 3) are cache-hot on their current CPU.
  2552. */
  2553. if (!cpumask_test_cpu(this_cpu, &p->cpus_allowed)) {
  2554. schedstat_inc(p, se.nr_failed_migrations_affine);
  2555. return 0;
  2556. }
  2557. *all_pinned = 0;
  2558. if (task_running(rq, p)) {
  2559. schedstat_inc(p, se.nr_failed_migrations_running);
  2560. return 0;
  2561. }
  2562. /*
  2563. * Aggressive migration if:
  2564. * 1) task is cache cold, or
  2565. * 2) too many balance attempts have failed.
  2566. */
  2567. tsk_cache_hot = task_hot(p, rq->clock, sd);
  2568. if (!tsk_cache_hot ||
  2569. sd->nr_balance_failed > sd->cache_nice_tries) {
  2570. #ifdef CONFIG_SCHEDSTATS
  2571. if (tsk_cache_hot) {
  2572. schedstat_inc(sd, lb_hot_gained[idle]);
  2573. schedstat_inc(p, se.nr_forced_migrations);
  2574. }
  2575. #endif
  2576. return 1;
  2577. }
  2578. if (tsk_cache_hot) {
  2579. schedstat_inc(p, se.nr_failed_migrations_hot);
  2580. return 0;
  2581. }
  2582. return 1;
  2583. }
  2584. static unsigned long
  2585. balance_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2586. unsigned long max_load_move, struct sched_domain *sd,
  2587. enum cpu_idle_type idle, int *all_pinned,
  2588. int *this_best_prio, struct rq_iterator *iterator)
  2589. {
  2590. int loops = 0, pulled = 0, pinned = 0;
  2591. struct task_struct *p;
  2592. long rem_load_move = max_load_move;
  2593. if (max_load_move == 0)
  2594. goto out;
  2595. pinned = 1;
  2596. /*
  2597. * Start the load-balancing iterator:
  2598. */
  2599. p = iterator->start(iterator->arg);
  2600. next:
  2601. if (!p || loops++ > sysctl_sched_nr_migrate)
  2602. goto out;
  2603. if ((p->se.load.weight >> 1) > rem_load_move ||
  2604. !can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
  2605. p = iterator->next(iterator->arg);
  2606. goto next;
  2607. }
  2608. pull_task(busiest, p, this_rq, this_cpu);
  2609. pulled++;
  2610. rem_load_move -= p->se.load.weight;
  2611. #ifdef CONFIG_PREEMPT
  2612. /*
  2613. * NEWIDLE balancing is a source of latency, so preemptible kernels
  2614. * will stop after the first task is pulled to minimize the critical
  2615. * section.
  2616. */
  2617. if (idle == CPU_NEWLY_IDLE)
  2618. goto out;
  2619. #endif
  2620. /*
  2621. * We only want to steal up to the prescribed amount of weighted load.
  2622. */
  2623. if (rem_load_move > 0) {
  2624. if (p->prio < *this_best_prio)
  2625. *this_best_prio = p->prio;
  2626. p = iterator->next(iterator->arg);
  2627. goto next;
  2628. }
  2629. out:
  2630. /*
  2631. * Right now, this is one of only two places pull_task() is called,
  2632. * so we can safely collect pull_task() stats here rather than
  2633. * inside pull_task().
  2634. */
  2635. schedstat_add(sd, lb_gained[idle], pulled);
  2636. if (all_pinned)
  2637. *all_pinned = pinned;
  2638. return max_load_move - rem_load_move;
  2639. }
  2640. /*
  2641. * move_tasks tries to move up to max_load_move weighted load from busiest to
  2642. * this_rq, as part of a balancing operation within domain "sd".
  2643. * Returns 1 if successful and 0 otherwise.
  2644. *
  2645. * Called with both runqueues locked.
  2646. */
  2647. static int move_tasks(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2648. unsigned long max_load_move,
  2649. struct sched_domain *sd, enum cpu_idle_type idle,
  2650. int *all_pinned)
  2651. {
  2652. const struct sched_class *class = sched_class_highest;
  2653. unsigned long total_load_moved = 0;
  2654. int this_best_prio = this_rq->curr->prio;
  2655. do {
  2656. total_load_moved +=
  2657. class->load_balance(this_rq, this_cpu, busiest,
  2658. max_load_move - total_load_moved,
  2659. sd, idle, all_pinned, &this_best_prio);
  2660. class = class->next;
  2661. #ifdef CONFIG_PREEMPT
  2662. /*
  2663. * NEWIDLE balancing is a source of latency, so preemptible
  2664. * kernels will stop after the first task is pulled to minimize
  2665. * the critical section.
  2666. */
  2667. if (idle == CPU_NEWLY_IDLE && this_rq->nr_running)
  2668. break;
  2669. #endif
  2670. } while (class && max_load_move > total_load_moved);
  2671. return total_load_moved > 0;
  2672. }
  2673. static int
  2674. iter_move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2675. struct sched_domain *sd, enum cpu_idle_type idle,
  2676. struct rq_iterator *iterator)
  2677. {
  2678. struct task_struct *p = iterator->start(iterator->arg);
  2679. int pinned = 0;
  2680. while (p) {
  2681. if (can_migrate_task(p, busiest, this_cpu, sd, idle, &pinned)) {
  2682. pull_task(busiest, p, this_rq, this_cpu);
  2683. /*
  2684. * Right now, this is only the second place pull_task()
  2685. * is called, so we can safely collect pull_task()
  2686. * stats here rather than inside pull_task().
  2687. */
  2688. schedstat_inc(sd, lb_gained[idle]);
  2689. return 1;
  2690. }
  2691. p = iterator->next(iterator->arg);
  2692. }
  2693. return 0;
  2694. }
  2695. /*
  2696. * move_one_task tries to move exactly one task from busiest to this_rq, as
  2697. * part of active balancing operations within "domain".
  2698. * Returns 1 if successful and 0 otherwise.
  2699. *
  2700. * Called with both runqueues locked.
  2701. */
  2702. static int move_one_task(struct rq *this_rq, int this_cpu, struct rq *busiest,
  2703. struct sched_domain *sd, enum cpu_idle_type idle)
  2704. {
  2705. const struct sched_class *class;
  2706. for (class = sched_class_highest; class; class = class->next)
  2707. if (class->move_one_task(this_rq, this_cpu, busiest, sd, idle))
  2708. return 1;
  2709. return 0;
  2710. }
  2711. /********** Helpers for find_busiest_group ************************/
  2712. /*
  2713. * sd_lb_stats - Structure to store the statistics of a sched_domain
  2714. * during load balancing.
  2715. */
  2716. struct sd_lb_stats {
  2717. struct sched_group *busiest; /* Busiest group in this sd */
  2718. struct sched_group *this; /* Local group in this sd */
  2719. unsigned long total_load; /* Total load of all groups in sd */
  2720. unsigned long total_pwr; /* Total power of all groups in sd */
  2721. unsigned long avg_load; /* Average load across all groups in sd */
  2722. /** Statistics of this group */
  2723. unsigned long this_load;
  2724. unsigned long this_load_per_task;
  2725. unsigned long this_nr_running;
  2726. /* Statistics of the busiest group */
  2727. unsigned long max_load;
  2728. unsigned long busiest_load_per_task;
  2729. unsigned long busiest_nr_running;
  2730. int group_imb; /* Is there imbalance in this sd */
  2731. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2732. int power_savings_balance; /* Is powersave balance needed for this sd */
  2733. struct sched_group *group_min; /* Least loaded group in sd */
  2734. struct sched_group *group_leader; /* Group which relieves group_min */
  2735. unsigned long min_load_per_task; /* load_per_task in group_min */
  2736. unsigned long leader_nr_running; /* Nr running of group_leader */
  2737. unsigned long min_nr_running; /* Nr running of group_min */
  2738. #endif
  2739. };
  2740. /*
  2741. * sg_lb_stats - stats of a sched_group required for load_balancing
  2742. */
  2743. struct sg_lb_stats {
  2744. unsigned long avg_load; /*Avg load across the CPUs of the group */
  2745. unsigned long group_load; /* Total load over the CPUs of the group */
  2746. unsigned long sum_nr_running; /* Nr tasks running in the group */
  2747. unsigned long sum_weighted_load; /* Weighted load of group's tasks */
  2748. unsigned long group_capacity;
  2749. int group_imb; /* Is there an imbalance in the group ? */
  2750. };
  2751. /**
  2752. * group_first_cpu - Returns the first cpu in the cpumask of a sched_group.
  2753. * @group: The group whose first cpu is to be returned.
  2754. */
  2755. static inline unsigned int group_first_cpu(struct sched_group *group)
  2756. {
  2757. return cpumask_first(sched_group_cpus(group));
  2758. }
  2759. /**
  2760. * get_sd_load_idx - Obtain the load index for a given sched domain.
  2761. * @sd: The sched_domain whose load_idx is to be obtained.
  2762. * @idle: The Idle status of the CPU for whose sd load_icx is obtained.
  2763. */
  2764. static inline int get_sd_load_idx(struct sched_domain *sd,
  2765. enum cpu_idle_type idle)
  2766. {
  2767. int load_idx;
  2768. switch (idle) {
  2769. case CPU_NOT_IDLE:
  2770. load_idx = sd->busy_idx;
  2771. break;
  2772. case CPU_NEWLY_IDLE:
  2773. load_idx = sd->newidle_idx;
  2774. break;
  2775. default:
  2776. load_idx = sd->idle_idx;
  2777. break;
  2778. }
  2779. return load_idx;
  2780. }
  2781. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  2782. /**
  2783. * init_sd_power_savings_stats - Initialize power savings statistics for
  2784. * the given sched_domain, during load balancing.
  2785. *
  2786. * @sd: Sched domain whose power-savings statistics are to be initialized.
  2787. * @sds: Variable containing the statistics for sd.
  2788. * @idle: Idle status of the CPU at which we're performing load-balancing.
  2789. */
  2790. static inline void init_sd_power_savings_stats(struct sched_domain *sd,
  2791. struct sd_lb_stats *sds, enum cpu_idle_type idle)
  2792. {
  2793. /*
  2794. * Busy processors will not participate in power savings
  2795. * balance.
  2796. */
  2797. if (idle == CPU_NOT_IDLE || !(sd->flags & SD_POWERSAVINGS_BALANCE))
  2798. sds->power_savings_balance = 0;
  2799. else {
  2800. sds->power_savings_balance = 1;
  2801. sds->min_nr_running = ULONG_MAX;
  2802. sds->leader_nr_running = 0;
  2803. }
  2804. }
  2805. /**
  2806. * update_sd_power_savings_stats - Update the power saving stats for a
  2807. * sched_domain while performing load balancing.
  2808. *
  2809. * @group: sched_group belonging to the sched_domain under consideration.
  2810. * @sds: Variable containing the statistics of the sched_domain
  2811. * @local_group: Does group contain the CPU for which we're performing
  2812. * load balancing ?
  2813. * @sgs: Variable containing the statistics of the group.
  2814. */
  2815. static inline void update_sd_power_savings_stats(struct sched_group *group,
  2816. struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
  2817. {
  2818. if (!sds->power_savings_balance)
  2819. return;
  2820. /*
  2821. * If the local group is idle or completely loaded
  2822. * no need to do power savings balance at this domain
  2823. */
  2824. if (local_group && (sds->this_nr_running >= sgs->group_capacity ||
  2825. !sds->this_nr_running))
  2826. sds->power_savings_balance = 0;
  2827. /*
  2828. * If a group is already running at full capacity or idle,
  2829. * don't include that group in power savings calculations
  2830. */
  2831. if (!sds->power_savings_balance ||
  2832. sgs->sum_nr_running >= sgs->group_capacity ||
  2833. !sgs->sum_nr_running)
  2834. return;
  2835. /*
  2836. * Calculate the group which has the least non-idle load.
  2837. * This is the group from where we need to pick up the load
  2838. * for saving power
  2839. */
  2840. if ((sgs->sum_nr_running < sds->min_nr_running) ||
  2841. (sgs->sum_nr_running == sds->min_nr_running &&
  2842. group_first_cpu(group) > group_first_cpu(sds->group_min))) {
  2843. sds->group_min = group;
  2844. sds->min_nr_running = sgs->sum_nr_running;
  2845. sds->min_load_per_task = sgs->sum_weighted_load /
  2846. sgs->sum_nr_running;
  2847. }
  2848. /*
  2849. * Calculate the group which is almost near its
  2850. * capacity but still has some space to pick up some load
  2851. * from other group and save more power
  2852. */
  2853. if (sgs->sum_nr_running > sgs->group_capacity - 1)
  2854. return;
  2855. if (sgs->sum_nr_running > sds->leader_nr_running ||
  2856. (sgs->sum_nr_running == sds->leader_nr_running &&
  2857. group_first_cpu(group) < group_first_cpu(sds->group_leader))) {
  2858. sds->group_leader = group;
  2859. sds->leader_nr_running = sgs->sum_nr_running;
  2860. }
  2861. }
  2862. /**
  2863. * check_power_save_busiest_group - see if there is potential for some power-savings balance
  2864. * @sds: Variable containing the statistics of the sched_domain
  2865. * under consideration.
  2866. * @this_cpu: Cpu at which we're currently performing load-balancing.
  2867. * @imbalance: Variable to store the imbalance.
  2868. *
  2869. * Description:
  2870. * Check if we have potential to perform some power-savings balance.
  2871. * If yes, set the busiest group to be the least loaded group in the
  2872. * sched_domain, so that it's CPUs can be put to idle.
  2873. *
  2874. * Returns 1 if there is potential to perform power-savings balance.
  2875. * Else returns 0.
  2876. */
  2877. static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
  2878. int this_cpu, unsigned long *imbalance)
  2879. {
  2880. if (!sds->power_savings_balance)
  2881. return 0;
  2882. if (sds->this != sds->group_leader ||
  2883. sds->group_leader == sds->group_min)
  2884. return 0;
  2885. *imbalance = sds->min_load_per_task;
  2886. sds->busiest = sds->group_min;
  2887. if (sched_mc_power_savings >= POWERSAVINGS_BALANCE_WAKEUP) {
  2888. cpu_rq(this_cpu)->rd->sched_mc_preferred_wakeup_cpu =
  2889. group_first_cpu(sds->group_leader);
  2890. }
  2891. return 1;
  2892. }
  2893. #else /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
  2894. static inline void init_sd_power_savings_stats(struct sched_domain *sd,
  2895. struct sd_lb_stats *sds, enum cpu_idle_type idle)
  2896. {
  2897. return;
  2898. }
  2899. static inline void update_sd_power_savings_stats(struct sched_group *group,
  2900. struct sd_lb_stats *sds, int local_group, struct sg_lb_stats *sgs)
  2901. {
  2902. return;
  2903. }
  2904. static inline int check_power_save_busiest_group(struct sd_lb_stats *sds,
  2905. int this_cpu, unsigned long *imbalance)
  2906. {
  2907. return 0;
  2908. }
  2909. #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
  2910. /**
  2911. * update_sg_lb_stats - Update sched_group's statistics for load balancing.
  2912. * @group: sched_group whose statistics are to be updated.
  2913. * @this_cpu: Cpu for which load balance is currently performed.
  2914. * @idle: Idle status of this_cpu
  2915. * @load_idx: Load index of sched_domain of this_cpu for load calc.
  2916. * @sd_idle: Idle status of the sched_domain containing group.
  2917. * @local_group: Does group contain this_cpu.
  2918. * @cpus: Set of cpus considered for load balancing.
  2919. * @balance: Should we balance.
  2920. * @sgs: variable to hold the statistics for this group.
  2921. */
  2922. static inline void update_sg_lb_stats(struct sched_group *group, int this_cpu,
  2923. enum cpu_idle_type idle, int load_idx, int *sd_idle,
  2924. int local_group, const struct cpumask *cpus,
  2925. int *balance, struct sg_lb_stats *sgs)
  2926. {
  2927. unsigned long load, max_cpu_load, min_cpu_load;
  2928. int i;
  2929. unsigned int balance_cpu = -1, first_idle_cpu = 0;
  2930. unsigned long sum_avg_load_per_task;
  2931. unsigned long avg_load_per_task;
  2932. if (local_group)
  2933. balance_cpu = group_first_cpu(group);
  2934. /* Tally up the load of all CPUs in the group */
  2935. sum_avg_load_per_task = avg_load_per_task = 0;
  2936. max_cpu_load = 0;
  2937. min_cpu_load = ~0UL;
  2938. for_each_cpu_and(i, sched_group_cpus(group), cpus) {
  2939. struct rq *rq = cpu_rq(i);
  2940. if (*sd_idle && rq->nr_running)
  2941. *sd_idle = 0;
  2942. /* Bias balancing toward cpus of our domain */
  2943. if (local_group) {
  2944. if (idle_cpu(i) && !first_idle_cpu) {
  2945. first_idle_cpu = 1;
  2946. balance_cpu = i;
  2947. }
  2948. load = target_load(i, load_idx);
  2949. } else {
  2950. load = source_load(i, load_idx);
  2951. if (load > max_cpu_load)
  2952. max_cpu_load = load;
  2953. if (min_cpu_load > load)
  2954. min_cpu_load = load;
  2955. }
  2956. sgs->group_load += load;
  2957. sgs->sum_nr_running += rq->nr_running;
  2958. sgs->sum_weighted_load += weighted_cpuload(i);
  2959. sum_avg_load_per_task += cpu_avg_load_per_task(i);
  2960. }
  2961. /*
  2962. * First idle cpu or the first cpu(busiest) in this sched group
  2963. * is eligible for doing load balancing at this and above
  2964. * domains. In the newly idle case, we will allow all the cpu's
  2965. * to do the newly idle load balance.
  2966. */
  2967. if (idle != CPU_NEWLY_IDLE && local_group &&
  2968. balance_cpu != this_cpu && balance) {
  2969. *balance = 0;
  2970. return;
  2971. }
  2972. /* Adjust by relative CPU power of the group */
  2973. sgs->avg_load = sg_div_cpu_power(group,
  2974. sgs->group_load * SCHED_LOAD_SCALE);
  2975. /*
  2976. * Consider the group unbalanced when the imbalance is larger
  2977. * than the average weight of two tasks.
  2978. *
  2979. * APZ: with cgroup the avg task weight can vary wildly and
  2980. * might not be a suitable number - should we keep a
  2981. * normalized nr_running number somewhere that negates
  2982. * the hierarchy?
  2983. */
  2984. avg_load_per_task = sg_div_cpu_power(group,
  2985. sum_avg_load_per_task * SCHED_LOAD_SCALE);
  2986. if ((max_cpu_load - min_cpu_load) > 2*avg_load_per_task)
  2987. sgs->group_imb = 1;
  2988. sgs->group_capacity = group->__cpu_power / SCHED_LOAD_SCALE;
  2989. }
  2990. /**
  2991. * update_sd_lb_stats - Update sched_group's statistics for load balancing.
  2992. * @sd: sched_domain whose statistics are to be updated.
  2993. * @this_cpu: Cpu for which load balance is currently performed.
  2994. * @idle: Idle status of this_cpu
  2995. * @sd_idle: Idle status of the sched_domain containing group.
  2996. * @cpus: Set of cpus considered for load balancing.
  2997. * @balance: Should we balance.
  2998. * @sds: variable to hold the statistics for this sched_domain.
  2999. */
  3000. static inline void update_sd_lb_stats(struct sched_domain *sd, int this_cpu,
  3001. enum cpu_idle_type idle, int *sd_idle,
  3002. const struct cpumask *cpus, int *balance,
  3003. struct sd_lb_stats *sds)
  3004. {
  3005. struct sched_group *group = sd->groups;
  3006. struct sg_lb_stats sgs;
  3007. int load_idx;
  3008. init_sd_power_savings_stats(sd, sds, idle);
  3009. load_idx = get_sd_load_idx(sd, idle);
  3010. do {
  3011. int local_group;
  3012. local_group = cpumask_test_cpu(this_cpu,
  3013. sched_group_cpus(group));
  3014. memset(&sgs, 0, sizeof(sgs));
  3015. update_sg_lb_stats(group, this_cpu, idle, load_idx, sd_idle,
  3016. local_group, cpus, balance, &sgs);
  3017. if (local_group && balance && !(*balance))
  3018. return;
  3019. sds->total_load += sgs.group_load;
  3020. sds->total_pwr += group->__cpu_power;
  3021. if (local_group) {
  3022. sds->this_load = sgs.avg_load;
  3023. sds->this = group;
  3024. sds->this_nr_running = sgs.sum_nr_running;
  3025. sds->this_load_per_task = sgs.sum_weighted_load;
  3026. } else if (sgs.avg_load > sds->max_load &&
  3027. (sgs.sum_nr_running > sgs.group_capacity ||
  3028. sgs.group_imb)) {
  3029. sds->max_load = sgs.avg_load;
  3030. sds->busiest = group;
  3031. sds->busiest_nr_running = sgs.sum_nr_running;
  3032. sds->busiest_load_per_task = sgs.sum_weighted_load;
  3033. sds->group_imb = sgs.group_imb;
  3034. }
  3035. update_sd_power_savings_stats(group, sds, local_group, &sgs);
  3036. group = group->next;
  3037. } while (group != sd->groups);
  3038. }
  3039. /**
  3040. * fix_small_imbalance - Calculate the minor imbalance that exists
  3041. * amongst the groups of a sched_domain, during
  3042. * load balancing.
  3043. * @sds: Statistics of the sched_domain whose imbalance is to be calculated.
  3044. * @this_cpu: The cpu at whose sched_domain we're performing load-balance.
  3045. * @imbalance: Variable to store the imbalance.
  3046. */
  3047. static inline void fix_small_imbalance(struct sd_lb_stats *sds,
  3048. int this_cpu, unsigned long *imbalance)
  3049. {
  3050. unsigned long tmp, pwr_now = 0, pwr_move = 0;
  3051. unsigned int imbn = 2;
  3052. if (sds->this_nr_running) {
  3053. sds->this_load_per_task /= sds->this_nr_running;
  3054. if (sds->busiest_load_per_task >
  3055. sds->this_load_per_task)
  3056. imbn = 1;
  3057. } else
  3058. sds->this_load_per_task =
  3059. cpu_avg_load_per_task(this_cpu);
  3060. if (sds->max_load - sds->this_load + sds->busiest_load_per_task >=
  3061. sds->busiest_load_per_task * imbn) {
  3062. *imbalance = sds->busiest_load_per_task;
  3063. return;
  3064. }
  3065. /*
  3066. * OK, we don't have enough imbalance to justify moving tasks,
  3067. * however we may be able to increase total CPU power used by
  3068. * moving them.
  3069. */
  3070. pwr_now += sds->busiest->__cpu_power *
  3071. min(sds->busiest_load_per_task, sds->max_load);
  3072. pwr_now += sds->this->__cpu_power *
  3073. min(sds->this_load_per_task, sds->this_load);
  3074. pwr_now /= SCHED_LOAD_SCALE;
  3075. /* Amount of load we'd subtract */
  3076. tmp = sg_div_cpu_power(sds->busiest,
  3077. sds->busiest_load_per_task * SCHED_LOAD_SCALE);
  3078. if (sds->max_load > tmp)
  3079. pwr_move += sds->busiest->__cpu_power *
  3080. min(sds->busiest_load_per_task, sds->max_load - tmp);
  3081. /* Amount of load we'd add */
  3082. if (sds->max_load * sds->busiest->__cpu_power <
  3083. sds->busiest_load_per_task * SCHED_LOAD_SCALE)
  3084. tmp = sg_div_cpu_power(sds->this,
  3085. sds->max_load * sds->busiest->__cpu_power);
  3086. else
  3087. tmp = sg_div_cpu_power(sds->this,
  3088. sds->busiest_load_per_task * SCHED_LOAD_SCALE);
  3089. pwr_move += sds->this->__cpu_power *
  3090. min(sds->this_load_per_task, sds->this_load + tmp);
  3091. pwr_move /= SCHED_LOAD_SCALE;
  3092. /* Move if we gain throughput */
  3093. if (pwr_move > pwr_now)
  3094. *imbalance = sds->busiest_load_per_task;
  3095. }
  3096. /**
  3097. * calculate_imbalance - Calculate the amount of imbalance present within the
  3098. * groups of a given sched_domain during load balance.
  3099. * @sds: statistics of the sched_domain whose imbalance is to be calculated.
  3100. * @this_cpu: Cpu for which currently load balance is being performed.
  3101. * @imbalance: The variable to store the imbalance.
  3102. */
  3103. static inline void calculate_imbalance(struct sd_lb_stats *sds, int this_cpu,
  3104. unsigned long *imbalance)
  3105. {
  3106. unsigned long max_pull;
  3107. /*
  3108. * In the presence of smp nice balancing, certain scenarios can have
  3109. * max load less than avg load(as we skip the groups at or below
  3110. * its cpu_power, while calculating max_load..)
  3111. */
  3112. if (sds->max_load < sds->avg_load) {
  3113. *imbalance = 0;
  3114. return fix_small_imbalance(sds, this_cpu, imbalance);
  3115. }
  3116. /* Don't want to pull so many tasks that a group would go idle */
  3117. max_pull = min(sds->max_load - sds->avg_load,
  3118. sds->max_load - sds->busiest_load_per_task);
  3119. /* How much load to actually move to equalise the imbalance */
  3120. *imbalance = min(max_pull * sds->busiest->__cpu_power,
  3121. (sds->avg_load - sds->this_load) * sds->this->__cpu_power)
  3122. / SCHED_LOAD_SCALE;
  3123. /*
  3124. * if *imbalance is less than the average load per runnable task
  3125. * there is no gaurantee that any tasks will be moved so we'll have
  3126. * a think about bumping its value to force at least one task to be
  3127. * moved
  3128. */
  3129. if (*imbalance < sds->busiest_load_per_task)
  3130. return fix_small_imbalance(sds, this_cpu, imbalance);
  3131. }
  3132. /******* find_busiest_group() helpers end here *********************/
  3133. /**
  3134. * find_busiest_group - Returns the busiest group within the sched_domain
  3135. * if there is an imbalance. If there isn't an imbalance, and
  3136. * the user has opted for power-savings, it returns a group whose
  3137. * CPUs can be put to idle by rebalancing those tasks elsewhere, if
  3138. * such a group exists.
  3139. *
  3140. * Also calculates the amount of weighted load which should be moved
  3141. * to restore balance.
  3142. *
  3143. * @sd: The sched_domain whose busiest group is to be returned.
  3144. * @this_cpu: The cpu for which load balancing is currently being performed.
  3145. * @imbalance: Variable which stores amount of weighted load which should
  3146. * be moved to restore balance/put a group to idle.
  3147. * @idle: The idle status of this_cpu.
  3148. * @sd_idle: The idleness of sd
  3149. * @cpus: The set of CPUs under consideration for load-balancing.
  3150. * @balance: Pointer to a variable indicating if this_cpu
  3151. * is the appropriate cpu to perform load balancing at this_level.
  3152. *
  3153. * Returns: - the busiest group if imbalance exists.
  3154. * - If no imbalance and user has opted for power-savings balance,
  3155. * return the least loaded group whose CPUs can be
  3156. * put to idle by rebalancing its tasks onto our group.
  3157. */
  3158. static struct sched_group *
  3159. find_busiest_group(struct sched_domain *sd, int this_cpu,
  3160. unsigned long *imbalance, enum cpu_idle_type idle,
  3161. int *sd_idle, const struct cpumask *cpus, int *balance)
  3162. {
  3163. struct sd_lb_stats sds;
  3164. memset(&sds, 0, sizeof(sds));
  3165. /*
  3166. * Compute the various statistics relavent for load balancing at
  3167. * this level.
  3168. */
  3169. update_sd_lb_stats(sd, this_cpu, idle, sd_idle, cpus,
  3170. balance, &sds);
  3171. /* Cases where imbalance does not exist from POV of this_cpu */
  3172. /* 1) this_cpu is not the appropriate cpu to perform load balancing
  3173. * at this level.
  3174. * 2) There is no busy sibling group to pull from.
  3175. * 3) This group is the busiest group.
  3176. * 4) This group is more busy than the avg busieness at this
  3177. * sched_domain.
  3178. * 5) The imbalance is within the specified limit.
  3179. * 6) Any rebalance would lead to ping-pong
  3180. */
  3181. if (balance && !(*balance))
  3182. goto ret;
  3183. if (!sds.busiest || sds.busiest_nr_running == 0)
  3184. goto out_balanced;
  3185. if (sds.this_load >= sds.max_load)
  3186. goto out_balanced;
  3187. sds.avg_load = (SCHED_LOAD_SCALE * sds.total_load) / sds.total_pwr;
  3188. if (sds.this_load >= sds.avg_load)
  3189. goto out_balanced;
  3190. if (100 * sds.max_load <= sd->imbalance_pct * sds.this_load)
  3191. goto out_balanced;
  3192. sds.busiest_load_per_task /= sds.busiest_nr_running;
  3193. if (sds.group_imb)
  3194. sds.busiest_load_per_task =
  3195. min(sds.busiest_load_per_task, sds.avg_load);
  3196. /*
  3197. * We're trying to get all the cpus to the average_load, so we don't
  3198. * want to push ourselves above the average load, nor do we wish to
  3199. * reduce the max loaded cpu below the average load, as either of these
  3200. * actions would just result in more rebalancing later, and ping-pong
  3201. * tasks around. Thus we look for the minimum possible imbalance.
  3202. * Negative imbalances (*we* are more loaded than anyone else) will
  3203. * be counted as no imbalance for these purposes -- we can't fix that
  3204. * by pulling tasks to us. Be careful of negative numbers as they'll
  3205. * appear as very large values with unsigned longs.
  3206. */
  3207. if (sds.max_load <= sds.busiest_load_per_task)
  3208. goto out_balanced;
  3209. /* Looks like there is an imbalance. Compute it */
  3210. calculate_imbalance(&sds, this_cpu, imbalance);
  3211. return sds.busiest;
  3212. out_balanced:
  3213. /*
  3214. * There is no obvious imbalance. But check if we can do some balancing
  3215. * to save power.
  3216. */
  3217. if (check_power_save_busiest_group(&sds, this_cpu, imbalance))
  3218. return sds.busiest;
  3219. ret:
  3220. *imbalance = 0;
  3221. return NULL;
  3222. }
  3223. /*
  3224. * find_busiest_queue - find the busiest runqueue among the cpus in group.
  3225. */
  3226. static struct rq *
  3227. find_busiest_queue(struct sched_group *group, enum cpu_idle_type idle,
  3228. unsigned long imbalance, const struct cpumask *cpus)
  3229. {
  3230. struct rq *busiest = NULL, *rq;
  3231. unsigned long max_load = 0;
  3232. int i;
  3233. for_each_cpu(i, sched_group_cpus(group)) {
  3234. unsigned long wl;
  3235. if (!cpumask_test_cpu(i, cpus))
  3236. continue;
  3237. rq = cpu_rq(i);
  3238. wl = weighted_cpuload(i);
  3239. if (rq->nr_running == 1 && wl > imbalance)
  3240. continue;
  3241. if (wl > max_load) {
  3242. max_load = wl;
  3243. busiest = rq;
  3244. }
  3245. }
  3246. return busiest;
  3247. }
  3248. /*
  3249. * Max backoff if we encounter pinned tasks. Pretty arbitrary value, but
  3250. * so long as it is large enough.
  3251. */
  3252. #define MAX_PINNED_INTERVAL 512
  3253. /*
  3254. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  3255. * tasks if there is an imbalance.
  3256. */
  3257. static int load_balance(int this_cpu, struct rq *this_rq,
  3258. struct sched_domain *sd, enum cpu_idle_type idle,
  3259. int *balance, struct cpumask *cpus)
  3260. {
  3261. int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
  3262. struct sched_group *group;
  3263. unsigned long imbalance;
  3264. struct rq *busiest;
  3265. unsigned long flags;
  3266. cpumask_setall(cpus);
  3267. /*
  3268. * When power savings policy is enabled for the parent domain, idle
  3269. * sibling can pick up load irrespective of busy siblings. In this case,
  3270. * let the state of idle sibling percolate up as CPU_IDLE, instead of
  3271. * portraying it as CPU_NOT_IDLE.
  3272. */
  3273. if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
  3274. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3275. sd_idle = 1;
  3276. schedstat_inc(sd, lb_count[idle]);
  3277. redo:
  3278. update_shares(sd);
  3279. group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
  3280. cpus, balance);
  3281. if (*balance == 0)
  3282. goto out_balanced;
  3283. if (!group) {
  3284. schedstat_inc(sd, lb_nobusyg[idle]);
  3285. goto out_balanced;
  3286. }
  3287. busiest = find_busiest_queue(group, idle, imbalance, cpus);
  3288. if (!busiest) {
  3289. schedstat_inc(sd, lb_nobusyq[idle]);
  3290. goto out_balanced;
  3291. }
  3292. BUG_ON(busiest == this_rq);
  3293. schedstat_add(sd, lb_imbalance[idle], imbalance);
  3294. ld_moved = 0;
  3295. if (busiest->nr_running > 1) {
  3296. /*
  3297. * Attempt to move tasks. If find_busiest_group has found
  3298. * an imbalance but busiest->nr_running <= 1, the group is
  3299. * still unbalanced. ld_moved simply stays zero, so it is
  3300. * correctly treated as an imbalance.
  3301. */
  3302. local_irq_save(flags);
  3303. double_rq_lock(this_rq, busiest);
  3304. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  3305. imbalance, sd, idle, &all_pinned);
  3306. double_rq_unlock(this_rq, busiest);
  3307. local_irq_restore(flags);
  3308. /*
  3309. * some other cpu did the load balance for us.
  3310. */
  3311. if (ld_moved && this_cpu != smp_processor_id())
  3312. resched_cpu(this_cpu);
  3313. /* All tasks on this runqueue were pinned by CPU affinity */
  3314. if (unlikely(all_pinned)) {
  3315. cpumask_clear_cpu(cpu_of(busiest), cpus);
  3316. if (!cpumask_empty(cpus))
  3317. goto redo;
  3318. goto out_balanced;
  3319. }
  3320. }
  3321. if (!ld_moved) {
  3322. schedstat_inc(sd, lb_failed[idle]);
  3323. sd->nr_balance_failed++;
  3324. if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
  3325. spin_lock_irqsave(&busiest->lock, flags);
  3326. /* don't kick the migration_thread, if the curr
  3327. * task on busiest cpu can't be moved to this_cpu
  3328. */
  3329. if (!cpumask_test_cpu(this_cpu,
  3330. &busiest->curr->cpus_allowed)) {
  3331. spin_unlock_irqrestore(&busiest->lock, flags);
  3332. all_pinned = 1;
  3333. goto out_one_pinned;
  3334. }
  3335. if (!busiest->active_balance) {
  3336. busiest->active_balance = 1;
  3337. busiest->push_cpu = this_cpu;
  3338. active_balance = 1;
  3339. }
  3340. spin_unlock_irqrestore(&busiest->lock, flags);
  3341. if (active_balance)
  3342. wake_up_process(busiest->migration_thread);
  3343. /*
  3344. * We've kicked active balancing, reset the failure
  3345. * counter.
  3346. */
  3347. sd->nr_balance_failed = sd->cache_nice_tries+1;
  3348. }
  3349. } else
  3350. sd->nr_balance_failed = 0;
  3351. if (likely(!active_balance)) {
  3352. /* We were unbalanced, so reset the balancing interval */
  3353. sd->balance_interval = sd->min_interval;
  3354. } else {
  3355. /*
  3356. * If we've begun active balancing, start to back off. This
  3357. * case may not be covered by the all_pinned logic if there
  3358. * is only 1 task on the busy runqueue (because we don't call
  3359. * move_tasks).
  3360. */
  3361. if (sd->balance_interval < sd->max_interval)
  3362. sd->balance_interval *= 2;
  3363. }
  3364. if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3365. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3366. ld_moved = -1;
  3367. goto out;
  3368. out_balanced:
  3369. schedstat_inc(sd, lb_balanced[idle]);
  3370. sd->nr_balance_failed = 0;
  3371. out_one_pinned:
  3372. /* tune up the balancing interval */
  3373. if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
  3374. (sd->balance_interval < sd->max_interval))
  3375. sd->balance_interval *= 2;
  3376. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3377. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3378. ld_moved = -1;
  3379. else
  3380. ld_moved = 0;
  3381. out:
  3382. if (ld_moved)
  3383. update_shares(sd);
  3384. return ld_moved;
  3385. }
  3386. /*
  3387. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  3388. * tasks if there is an imbalance.
  3389. *
  3390. * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
  3391. * this_rq is locked.
  3392. */
  3393. static int
  3394. load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd,
  3395. struct cpumask *cpus)
  3396. {
  3397. struct sched_group *group;
  3398. struct rq *busiest = NULL;
  3399. unsigned long imbalance;
  3400. int ld_moved = 0;
  3401. int sd_idle = 0;
  3402. int all_pinned = 0;
  3403. cpumask_setall(cpus);
  3404. /*
  3405. * When power savings policy is enabled for the parent domain, idle
  3406. * sibling can pick up load irrespective of busy siblings. In this case,
  3407. * let the state of idle sibling percolate up as IDLE, instead of
  3408. * portraying it as CPU_NOT_IDLE.
  3409. */
  3410. if (sd->flags & SD_SHARE_CPUPOWER &&
  3411. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3412. sd_idle = 1;
  3413. schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
  3414. redo:
  3415. update_shares_locked(this_rq, sd);
  3416. group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
  3417. &sd_idle, cpus, NULL);
  3418. if (!group) {
  3419. schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
  3420. goto out_balanced;
  3421. }
  3422. busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
  3423. if (!busiest) {
  3424. schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
  3425. goto out_balanced;
  3426. }
  3427. BUG_ON(busiest == this_rq);
  3428. schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
  3429. ld_moved = 0;
  3430. if (busiest->nr_running > 1) {
  3431. /* Attempt to move tasks */
  3432. double_lock_balance(this_rq, busiest);
  3433. /* this_rq->clock is already updated */
  3434. update_rq_clock(busiest);
  3435. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  3436. imbalance, sd, CPU_NEWLY_IDLE,
  3437. &all_pinned);
  3438. double_unlock_balance(this_rq, busiest);
  3439. if (unlikely(all_pinned)) {
  3440. cpumask_clear_cpu(cpu_of(busiest), cpus);
  3441. if (!cpumask_empty(cpus))
  3442. goto redo;
  3443. }
  3444. }
  3445. if (!ld_moved) {
  3446. int active_balance = 0;
  3447. schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
  3448. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3449. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3450. return -1;
  3451. if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
  3452. return -1;
  3453. if (sd->nr_balance_failed++ < 2)
  3454. return -1;
  3455. /*
  3456. * The only task running in a non-idle cpu can be moved to this
  3457. * cpu in an attempt to completely freeup the other CPU
  3458. * package. The same method used to move task in load_balance()
  3459. * have been extended for load_balance_newidle() to speedup
  3460. * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2)
  3461. *
  3462. * The package power saving logic comes from
  3463. * find_busiest_group(). If there are no imbalance, then
  3464. * f_b_g() will return NULL. However when sched_mc={1,2} then
  3465. * f_b_g() will select a group from which a running task may be
  3466. * pulled to this cpu in order to make the other package idle.
  3467. * If there is no opportunity to make a package idle and if
  3468. * there are no imbalance, then f_b_g() will return NULL and no
  3469. * action will be taken in load_balance_newidle().
  3470. *
  3471. * Under normal task pull operation due to imbalance, there
  3472. * will be more than one task in the source run queue and
  3473. * move_tasks() will succeed. ld_moved will be true and this
  3474. * active balance code will not be triggered.
  3475. */
  3476. /* Lock busiest in correct order while this_rq is held */
  3477. double_lock_balance(this_rq, busiest);
  3478. /*
  3479. * don't kick the migration_thread, if the curr
  3480. * task on busiest cpu can't be moved to this_cpu
  3481. */
  3482. if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
  3483. double_unlock_balance(this_rq, busiest);
  3484. all_pinned = 1;
  3485. return ld_moved;
  3486. }
  3487. if (!busiest->active_balance) {
  3488. busiest->active_balance = 1;
  3489. busiest->push_cpu = this_cpu;
  3490. active_balance = 1;
  3491. }
  3492. double_unlock_balance(this_rq, busiest);
  3493. /*
  3494. * Should not call ttwu while holding a rq->lock
  3495. */
  3496. spin_unlock(&this_rq->lock);
  3497. if (active_balance)
  3498. wake_up_process(busiest->migration_thread);
  3499. spin_lock(&this_rq->lock);
  3500. } else
  3501. sd->nr_balance_failed = 0;
  3502. update_shares_locked(this_rq, sd);
  3503. return ld_moved;
  3504. out_balanced:
  3505. schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
  3506. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3507. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3508. return -1;
  3509. sd->nr_balance_failed = 0;
  3510. return 0;
  3511. }
  3512. /*
  3513. * idle_balance is called by schedule() if this_cpu is about to become
  3514. * idle. Attempts to pull tasks from other CPUs.
  3515. */
  3516. static void idle_balance(int this_cpu, struct rq *this_rq)
  3517. {
  3518. struct sched_domain *sd;
  3519. int pulled_task = 0;
  3520. unsigned long next_balance = jiffies + HZ;
  3521. cpumask_var_t tmpmask;
  3522. if (!alloc_cpumask_var(&tmpmask, GFP_ATOMIC))
  3523. return;
  3524. for_each_domain(this_cpu, sd) {
  3525. unsigned long interval;
  3526. if (!(sd->flags & SD_LOAD_BALANCE))
  3527. continue;
  3528. if (sd->flags & SD_BALANCE_NEWIDLE)
  3529. /* If we've pulled tasks over stop searching: */
  3530. pulled_task = load_balance_newidle(this_cpu, this_rq,
  3531. sd, tmpmask);
  3532. interval = msecs_to_jiffies(sd->balance_interval);
  3533. if (time_after(next_balance, sd->last_balance + interval))
  3534. next_balance = sd->last_balance + interval;
  3535. if (pulled_task)
  3536. break;
  3537. }
  3538. if (pulled_task || time_after(jiffies, this_rq->next_balance)) {
  3539. /*
  3540. * We are going idle. next_balance may be set based on
  3541. * a busy processor. So reset next_balance.
  3542. */
  3543. this_rq->next_balance = next_balance;
  3544. }
  3545. free_cpumask_var(tmpmask);
  3546. }
  3547. /*
  3548. * active_load_balance is run by migration threads. It pushes running tasks
  3549. * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
  3550. * running on each physical CPU where possible, and avoids physical /
  3551. * logical imbalances.
  3552. *
  3553. * Called with busiest_rq locked.
  3554. */
  3555. static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
  3556. {
  3557. int target_cpu = busiest_rq->push_cpu;
  3558. struct sched_domain *sd;
  3559. struct rq *target_rq;
  3560. /* Is there any task to move? */
  3561. if (busiest_rq->nr_running <= 1)
  3562. return;
  3563. target_rq = cpu_rq(target_cpu);
  3564. /*
  3565. * This condition is "impossible", if it occurs
  3566. * we need to fix it. Originally reported by
  3567. * Bjorn Helgaas on a 128-cpu setup.
  3568. */
  3569. BUG_ON(busiest_rq == target_rq);
  3570. /* move a task from busiest_rq to target_rq */
  3571. double_lock_balance(busiest_rq, target_rq);
  3572. update_rq_clock(busiest_rq);
  3573. update_rq_clock(target_rq);
  3574. /* Search for an sd spanning us and the target CPU. */
  3575. for_each_domain(target_cpu, sd) {
  3576. if ((sd->flags & SD_LOAD_BALANCE) &&
  3577. cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
  3578. break;
  3579. }
  3580. if (likely(sd)) {
  3581. schedstat_inc(sd, alb_count);
  3582. if (move_one_task(target_rq, target_cpu, busiest_rq,
  3583. sd, CPU_IDLE))
  3584. schedstat_inc(sd, alb_pushed);
  3585. else
  3586. schedstat_inc(sd, alb_failed);
  3587. }
  3588. double_unlock_balance(busiest_rq, target_rq);
  3589. }
  3590. #ifdef CONFIG_NO_HZ
  3591. static struct {
  3592. atomic_t load_balancer;
  3593. cpumask_var_t cpu_mask;
  3594. } nohz ____cacheline_aligned = {
  3595. .load_balancer = ATOMIC_INIT(-1),
  3596. };
  3597. /*
  3598. * This routine will try to nominate the ilb (idle load balancing)
  3599. * owner among the cpus whose ticks are stopped. ilb owner will do the idle
  3600. * load balancing on behalf of all those cpus. If all the cpus in the system
  3601. * go into this tickless mode, then there will be no ilb owner (as there is
  3602. * no need for one) and all the cpus will sleep till the next wakeup event
  3603. * arrives...
  3604. *
  3605. * For the ilb owner, tick is not stopped. And this tick will be used
  3606. * for idle load balancing. ilb owner will still be part of
  3607. * nohz.cpu_mask..
  3608. *
  3609. * While stopping the tick, this cpu will become the ilb owner if there
  3610. * is no other owner. And will be the owner till that cpu becomes busy
  3611. * or if all cpus in the system stop their ticks at which point
  3612. * there is no need for ilb owner.
  3613. *
  3614. * When the ilb owner becomes busy, it nominates another owner, during the
  3615. * next busy scheduler_tick()
  3616. */
  3617. int select_nohz_load_balancer(int stop_tick)
  3618. {
  3619. int cpu = smp_processor_id();
  3620. if (stop_tick) {
  3621. cpu_rq(cpu)->in_nohz_recently = 1;
  3622. if (!cpu_active(cpu)) {
  3623. if (atomic_read(&nohz.load_balancer) != cpu)
  3624. return 0;
  3625. /*
  3626. * If we are going offline and still the leader,
  3627. * give up!
  3628. */
  3629. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  3630. BUG();
  3631. return 0;
  3632. }
  3633. cpumask_set_cpu(cpu, nohz.cpu_mask);
  3634. /* time for ilb owner also to sleep */
  3635. if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
  3636. if (atomic_read(&nohz.load_balancer) == cpu)
  3637. atomic_set(&nohz.load_balancer, -1);
  3638. return 0;
  3639. }
  3640. if (atomic_read(&nohz.load_balancer) == -1) {
  3641. /* make me the ilb owner */
  3642. if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
  3643. return 1;
  3644. } else if (atomic_read(&nohz.load_balancer) == cpu)
  3645. return 1;
  3646. } else {
  3647. if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
  3648. return 0;
  3649. cpumask_clear_cpu(cpu, nohz.cpu_mask);
  3650. if (atomic_read(&nohz.load_balancer) == cpu)
  3651. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  3652. BUG();
  3653. }
  3654. return 0;
  3655. }
  3656. #endif
  3657. static DEFINE_SPINLOCK(balancing);
  3658. /*
  3659. * It checks each scheduling domain to see if it is due to be balanced,
  3660. * and initiates a balancing operation if so.
  3661. *
  3662. * Balancing parameters are set up in arch_init_sched_domains.
  3663. */
  3664. static void rebalance_domains(int cpu, enum cpu_idle_type idle)
  3665. {
  3666. int balance = 1;
  3667. struct rq *rq = cpu_rq(cpu);
  3668. unsigned long interval;
  3669. struct sched_domain *sd;
  3670. /* Earliest time when we have to do rebalance again */
  3671. unsigned long next_balance = jiffies + 60*HZ;
  3672. int update_next_balance = 0;
  3673. int need_serialize;
  3674. cpumask_var_t tmp;
  3675. /* Fails alloc? Rebalancing probably not a priority right now. */
  3676. if (!alloc_cpumask_var(&tmp, GFP_ATOMIC))
  3677. return;
  3678. for_each_domain(cpu, sd) {
  3679. if (!(sd->flags & SD_LOAD_BALANCE))
  3680. continue;
  3681. interval = sd->balance_interval;
  3682. if (idle != CPU_IDLE)
  3683. interval *= sd->busy_factor;
  3684. /* scale ms to jiffies */
  3685. interval = msecs_to_jiffies(interval);
  3686. if (unlikely(!interval))
  3687. interval = 1;
  3688. if (interval > HZ*NR_CPUS/10)
  3689. interval = HZ*NR_CPUS/10;
  3690. need_serialize = sd->flags & SD_SERIALIZE;
  3691. if (need_serialize) {
  3692. if (!spin_trylock(&balancing))
  3693. goto out;
  3694. }
  3695. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  3696. if (load_balance(cpu, rq, sd, idle, &balance, tmp)) {
  3697. /*
  3698. * We've pulled tasks over so either we're no
  3699. * longer idle, or one of our SMT siblings is
  3700. * not idle.
  3701. */
  3702. idle = CPU_NOT_IDLE;
  3703. }
  3704. sd->last_balance = jiffies;
  3705. }
  3706. if (need_serialize)
  3707. spin_unlock(&balancing);
  3708. out:
  3709. if (time_after(next_balance, sd->last_balance + interval)) {
  3710. next_balance = sd->last_balance + interval;
  3711. update_next_balance = 1;
  3712. }
  3713. /*
  3714. * Stop the load balance at this level. There is another
  3715. * CPU in our sched group which is doing load balancing more
  3716. * actively.
  3717. */
  3718. if (!balance)
  3719. break;
  3720. }
  3721. /*
  3722. * next_balance will be updated only when there is a need.
  3723. * When the cpu is attached to null domain for ex, it will not be
  3724. * updated.
  3725. */
  3726. if (likely(update_next_balance))
  3727. rq->next_balance = next_balance;
  3728. free_cpumask_var(tmp);
  3729. }
  3730. /*
  3731. * run_rebalance_domains is triggered when needed from the scheduler tick.
  3732. * In CONFIG_NO_HZ case, the idle load balance owner will do the
  3733. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  3734. */
  3735. static void run_rebalance_domains(struct softirq_action *h)
  3736. {
  3737. int this_cpu = smp_processor_id();
  3738. struct rq *this_rq = cpu_rq(this_cpu);
  3739. enum cpu_idle_type idle = this_rq->idle_at_tick ?
  3740. CPU_IDLE : CPU_NOT_IDLE;
  3741. rebalance_domains(this_cpu, idle);
  3742. #ifdef CONFIG_NO_HZ
  3743. /*
  3744. * If this cpu is the owner for idle load balancing, then do the
  3745. * balancing on behalf of the other idle cpus whose ticks are
  3746. * stopped.
  3747. */
  3748. if (this_rq->idle_at_tick &&
  3749. atomic_read(&nohz.load_balancer) == this_cpu) {
  3750. struct rq *rq;
  3751. int balance_cpu;
  3752. for_each_cpu(balance_cpu, nohz.cpu_mask) {
  3753. if (balance_cpu == this_cpu)
  3754. continue;
  3755. /*
  3756. * If this cpu gets work to do, stop the load balancing
  3757. * work being done for other cpus. Next load
  3758. * balancing owner will pick it up.
  3759. */
  3760. if (need_resched())
  3761. break;
  3762. rebalance_domains(balance_cpu, CPU_IDLE);
  3763. rq = cpu_rq(balance_cpu);
  3764. if (time_after(this_rq->next_balance, rq->next_balance))
  3765. this_rq->next_balance = rq->next_balance;
  3766. }
  3767. }
  3768. #endif
  3769. }
  3770. static inline int on_null_domain(int cpu)
  3771. {
  3772. return !rcu_dereference(cpu_rq(cpu)->sd);
  3773. }
  3774. /*
  3775. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  3776. *
  3777. * In case of CONFIG_NO_HZ, this is the place where we nominate a new
  3778. * idle load balancing owner or decide to stop the periodic load balancing,
  3779. * if the whole system is idle.
  3780. */
  3781. static inline void trigger_load_balance(struct rq *rq, int cpu)
  3782. {
  3783. #ifdef CONFIG_NO_HZ
  3784. /*
  3785. * If we were in the nohz mode recently and busy at the current
  3786. * scheduler tick, then check if we need to nominate new idle
  3787. * load balancer.
  3788. */
  3789. if (rq->in_nohz_recently && !rq->idle_at_tick) {
  3790. rq->in_nohz_recently = 0;
  3791. if (atomic_read(&nohz.load_balancer) == cpu) {
  3792. cpumask_clear_cpu(cpu, nohz.cpu_mask);
  3793. atomic_set(&nohz.load_balancer, -1);
  3794. }
  3795. if (atomic_read(&nohz.load_balancer) == -1) {
  3796. /*
  3797. * simple selection for now: Nominate the
  3798. * first cpu in the nohz list to be the next
  3799. * ilb owner.
  3800. *
  3801. * TBD: Traverse the sched domains and nominate
  3802. * the nearest cpu in the nohz.cpu_mask.
  3803. */
  3804. int ilb = cpumask_first(nohz.cpu_mask);
  3805. if (ilb < nr_cpu_ids)
  3806. resched_cpu(ilb);
  3807. }
  3808. }
  3809. /*
  3810. * If this cpu is idle and doing idle load balancing for all the
  3811. * cpus with ticks stopped, is it time for that to stop?
  3812. */
  3813. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
  3814. cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
  3815. resched_cpu(cpu);
  3816. return;
  3817. }
  3818. /*
  3819. * If this cpu is idle and the idle load balancing is done by
  3820. * someone else, then no need raise the SCHED_SOFTIRQ
  3821. */
  3822. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
  3823. cpumask_test_cpu(cpu, nohz.cpu_mask))
  3824. return;
  3825. #endif
  3826. /* Don't need to rebalance while attached to NULL domain */
  3827. if (time_after_eq(jiffies, rq->next_balance) &&
  3828. likely(!on_null_domain(cpu)))
  3829. raise_softirq(SCHED_SOFTIRQ);
  3830. }
  3831. #else /* CONFIG_SMP */
  3832. /*
  3833. * on UP we do not need to balance between CPUs:
  3834. */
  3835. static inline void idle_balance(int cpu, struct rq *rq)
  3836. {
  3837. }
  3838. #endif
  3839. DEFINE_PER_CPU(struct kernel_stat, kstat);
  3840. EXPORT_PER_CPU_SYMBOL(kstat);
  3841. /*
  3842. * Return any ns on the sched_clock that have not yet been banked in
  3843. * @p in case that task is currently running.
  3844. */
  3845. unsigned long long task_delta_exec(struct task_struct *p)
  3846. {
  3847. unsigned long flags;
  3848. struct rq *rq;
  3849. u64 ns = 0;
  3850. rq = task_rq_lock(p, &flags);
  3851. if (task_current(rq, p)) {
  3852. u64 delta_exec;
  3853. update_rq_clock(rq);
  3854. delta_exec = rq->clock - p->se.exec_start;
  3855. if ((s64)delta_exec > 0)
  3856. ns = delta_exec;
  3857. }
  3858. task_rq_unlock(rq, &flags);
  3859. return ns;
  3860. }
  3861. /*
  3862. * Account user cpu time to a process.
  3863. * @p: the process that the cpu time gets accounted to
  3864. * @cputime: the cpu time spent in user space since the last update
  3865. * @cputime_scaled: cputime scaled by cpu frequency
  3866. */
  3867. void account_user_time(struct task_struct *p, cputime_t cputime,
  3868. cputime_t cputime_scaled)
  3869. {
  3870. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3871. cputime64_t tmp;
  3872. /* Add user time to process. */
  3873. p->utime = cputime_add(p->utime, cputime);
  3874. p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
  3875. account_group_user_time(p, cputime);
  3876. /* Add user time to cpustat. */
  3877. tmp = cputime_to_cputime64(cputime);
  3878. if (TASK_NICE(p) > 0)
  3879. cpustat->nice = cputime64_add(cpustat->nice, tmp);
  3880. else
  3881. cpustat->user = cputime64_add(cpustat->user, tmp);
  3882. /* Account for user time used */
  3883. acct_update_integrals(p);
  3884. }
  3885. /*
  3886. * Account guest cpu time to a process.
  3887. * @p: the process that the cpu time gets accounted to
  3888. * @cputime: the cpu time spent in virtual machine since the last update
  3889. * @cputime_scaled: cputime scaled by cpu frequency
  3890. */
  3891. static void account_guest_time(struct task_struct *p, cputime_t cputime,
  3892. cputime_t cputime_scaled)
  3893. {
  3894. cputime64_t tmp;
  3895. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3896. tmp = cputime_to_cputime64(cputime);
  3897. /* Add guest time to process. */
  3898. p->utime = cputime_add(p->utime, cputime);
  3899. p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
  3900. account_group_user_time(p, cputime);
  3901. p->gtime = cputime_add(p->gtime, cputime);
  3902. /* Add guest time to cpustat. */
  3903. cpustat->user = cputime64_add(cpustat->user, tmp);
  3904. cpustat->guest = cputime64_add(cpustat->guest, tmp);
  3905. }
  3906. /*
  3907. * Account system cpu time to a process.
  3908. * @p: the process that the cpu time gets accounted to
  3909. * @hardirq_offset: the offset to subtract from hardirq_count()
  3910. * @cputime: the cpu time spent in kernel space since the last update
  3911. * @cputime_scaled: cputime scaled by cpu frequency
  3912. */
  3913. void account_system_time(struct task_struct *p, int hardirq_offset,
  3914. cputime_t cputime, cputime_t cputime_scaled)
  3915. {
  3916. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3917. cputime64_t tmp;
  3918. if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
  3919. account_guest_time(p, cputime, cputime_scaled);
  3920. return;
  3921. }
  3922. /* Add system time to process. */
  3923. p->stime = cputime_add(p->stime, cputime);
  3924. p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
  3925. account_group_system_time(p, cputime);
  3926. /* Add system time to cpustat. */
  3927. tmp = cputime_to_cputime64(cputime);
  3928. if (hardirq_count() - hardirq_offset)
  3929. cpustat->irq = cputime64_add(cpustat->irq, tmp);
  3930. else if (softirq_count())
  3931. cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
  3932. else
  3933. cpustat->system = cputime64_add(cpustat->system, tmp);
  3934. /* Account for system time used */
  3935. acct_update_integrals(p);
  3936. }
  3937. /*
  3938. * Account for involuntary wait time.
  3939. * @steal: the cpu time spent in involuntary wait
  3940. */
  3941. void account_steal_time(cputime_t cputime)
  3942. {
  3943. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3944. cputime64_t cputime64 = cputime_to_cputime64(cputime);
  3945. cpustat->steal = cputime64_add(cpustat->steal, cputime64);
  3946. }
  3947. /*
  3948. * Account for idle time.
  3949. * @cputime: the cpu time spent in idle wait
  3950. */
  3951. void account_idle_time(cputime_t cputime)
  3952. {
  3953. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3954. cputime64_t cputime64 = cputime_to_cputime64(cputime);
  3955. struct rq *rq = this_rq();
  3956. if (atomic_read(&rq->nr_iowait) > 0)
  3957. cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
  3958. else
  3959. cpustat->idle = cputime64_add(cpustat->idle, cputime64);
  3960. }
  3961. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  3962. /*
  3963. * Account a single tick of cpu time.
  3964. * @p: the process that the cpu time gets accounted to
  3965. * @user_tick: indicates if the tick is a user or a system tick
  3966. */
  3967. void account_process_tick(struct task_struct *p, int user_tick)
  3968. {
  3969. cputime_t one_jiffy = jiffies_to_cputime(1);
  3970. cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
  3971. struct rq *rq = this_rq();
  3972. if (user_tick)
  3973. account_user_time(p, one_jiffy, one_jiffy_scaled);
  3974. else if (p != rq->idle)
  3975. account_system_time(p, HARDIRQ_OFFSET, one_jiffy,
  3976. one_jiffy_scaled);
  3977. else
  3978. account_idle_time(one_jiffy);
  3979. }
  3980. /*
  3981. * Account multiple ticks of steal time.
  3982. * @p: the process from which the cpu time has been stolen
  3983. * @ticks: number of stolen ticks
  3984. */
  3985. void account_steal_ticks(unsigned long ticks)
  3986. {
  3987. account_steal_time(jiffies_to_cputime(ticks));
  3988. }
  3989. /*
  3990. * Account multiple ticks of idle time.
  3991. * @ticks: number of stolen ticks
  3992. */
  3993. void account_idle_ticks(unsigned long ticks)
  3994. {
  3995. account_idle_time(jiffies_to_cputime(ticks));
  3996. }
  3997. #endif
  3998. /*
  3999. * Use precise platform statistics if available:
  4000. */
  4001. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  4002. cputime_t task_utime(struct task_struct *p)
  4003. {
  4004. return p->utime;
  4005. }
  4006. cputime_t task_stime(struct task_struct *p)
  4007. {
  4008. return p->stime;
  4009. }
  4010. #else
  4011. cputime_t task_utime(struct task_struct *p)
  4012. {
  4013. clock_t utime = cputime_to_clock_t(p->utime),
  4014. total = utime + cputime_to_clock_t(p->stime);
  4015. u64 temp;
  4016. /*
  4017. * Use CFS's precise accounting:
  4018. */
  4019. temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
  4020. if (total) {
  4021. temp *= utime;
  4022. do_div(temp, total);
  4023. }
  4024. utime = (clock_t)temp;
  4025. p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
  4026. return p->prev_utime;
  4027. }
  4028. cputime_t task_stime(struct task_struct *p)
  4029. {
  4030. clock_t stime;
  4031. /*
  4032. * Use CFS's precise accounting. (we subtract utime from
  4033. * the total, to make sure the total observed by userspace
  4034. * grows monotonically - apps rely on that):
  4035. */
  4036. stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
  4037. cputime_to_clock_t(task_utime(p));
  4038. if (stime >= 0)
  4039. p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
  4040. return p->prev_stime;
  4041. }
  4042. #endif
  4043. inline cputime_t task_gtime(struct task_struct *p)
  4044. {
  4045. return p->gtime;
  4046. }
  4047. /*
  4048. * This function gets called by the timer code, with HZ frequency.
  4049. * We call it with interrupts disabled.
  4050. *
  4051. * It also gets called by the fork code, when changing the parent's
  4052. * timeslices.
  4053. */
  4054. void scheduler_tick(void)
  4055. {
  4056. int cpu = smp_processor_id();
  4057. struct rq *rq = cpu_rq(cpu);
  4058. struct task_struct *curr = rq->curr;
  4059. sched_clock_tick();
  4060. spin_lock(&rq->lock);
  4061. update_rq_clock(rq);
  4062. update_cpu_load(rq);
  4063. curr->sched_class->task_tick(rq, curr, 0);
  4064. spin_unlock(&rq->lock);
  4065. #ifdef CONFIG_SMP
  4066. rq->idle_at_tick = idle_cpu(cpu);
  4067. trigger_load_balance(rq, cpu);
  4068. #endif
  4069. }
  4070. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  4071. defined(CONFIG_PREEMPT_TRACER))
  4072. static inline unsigned long get_parent_ip(unsigned long addr)
  4073. {
  4074. if (in_lock_functions(addr)) {
  4075. addr = CALLER_ADDR2;
  4076. if (in_lock_functions(addr))
  4077. addr = CALLER_ADDR3;
  4078. }
  4079. return addr;
  4080. }
  4081. void __kprobes add_preempt_count(int val)
  4082. {
  4083. #ifdef CONFIG_DEBUG_PREEMPT
  4084. /*
  4085. * Underflow?
  4086. */
  4087. if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
  4088. return;
  4089. #endif
  4090. preempt_count() += val;
  4091. #ifdef CONFIG_DEBUG_PREEMPT
  4092. /*
  4093. * Spinlock count overflowing soon?
  4094. */
  4095. DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
  4096. PREEMPT_MASK - 10);
  4097. #endif
  4098. if (preempt_count() == val)
  4099. trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  4100. }
  4101. EXPORT_SYMBOL(add_preempt_count);
  4102. void __kprobes sub_preempt_count(int val)
  4103. {
  4104. #ifdef CONFIG_DEBUG_PREEMPT
  4105. /*
  4106. * Underflow?
  4107. */
  4108. if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
  4109. return;
  4110. /*
  4111. * Is the spinlock portion underflowing?
  4112. */
  4113. if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
  4114. !(preempt_count() & PREEMPT_MASK)))
  4115. return;
  4116. #endif
  4117. if (preempt_count() == val)
  4118. trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  4119. preempt_count() -= val;
  4120. }
  4121. EXPORT_SYMBOL(sub_preempt_count);
  4122. #endif
  4123. /*
  4124. * Print scheduling while atomic bug:
  4125. */
  4126. static noinline void __schedule_bug(struct task_struct *prev)
  4127. {
  4128. struct pt_regs *regs = get_irq_regs();
  4129. printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
  4130. prev->comm, prev->pid, preempt_count());
  4131. debug_show_held_locks(prev);
  4132. print_modules();
  4133. if (irqs_disabled())
  4134. print_irqtrace_events(prev);
  4135. if (regs)
  4136. show_regs(regs);
  4137. else
  4138. dump_stack();
  4139. }
  4140. /*
  4141. * Various schedule()-time debugging checks and statistics:
  4142. */
  4143. static inline void schedule_debug(struct task_struct *prev)
  4144. {
  4145. /*
  4146. * Test if we are atomic. Since do_exit() needs to call into
  4147. * schedule() atomically, we ignore that path for now.
  4148. * Otherwise, whine if we are scheduling when we should not be.
  4149. */
  4150. if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
  4151. __schedule_bug(prev);
  4152. profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  4153. schedstat_inc(this_rq(), sched_count);
  4154. #ifdef CONFIG_SCHEDSTATS
  4155. if (unlikely(prev->lock_depth >= 0)) {
  4156. schedstat_inc(this_rq(), bkl_count);
  4157. schedstat_inc(prev, sched_info.bkl_count);
  4158. }
  4159. #endif
  4160. }
  4161. static void put_prev_task(struct rq *rq, struct task_struct *prev)
  4162. {
  4163. if (prev->state == TASK_RUNNING) {
  4164. u64 runtime = prev->se.sum_exec_runtime;
  4165. runtime -= prev->se.prev_sum_exec_runtime;
  4166. runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
  4167. /*
  4168. * In order to avoid avg_overlap growing stale when we are
  4169. * indeed overlapping and hence not getting put to sleep, grow
  4170. * the avg_overlap on preemption.
  4171. *
  4172. * We use the average preemption runtime because that
  4173. * correlates to the amount of cache footprint a task can
  4174. * build up.
  4175. */
  4176. update_avg(&prev->se.avg_overlap, runtime);
  4177. }
  4178. prev->sched_class->put_prev_task(rq, prev);
  4179. }
  4180. /*
  4181. * Pick up the highest-prio task:
  4182. */
  4183. static inline struct task_struct *
  4184. pick_next_task(struct rq *rq)
  4185. {
  4186. const struct sched_class *class;
  4187. struct task_struct *p;
  4188. /*
  4189. * Optimization: we know that if all tasks are in
  4190. * the fair class we can call that function directly:
  4191. */
  4192. if (likely(rq->nr_running == rq->cfs.nr_running)) {
  4193. p = fair_sched_class.pick_next_task(rq);
  4194. if (likely(p))
  4195. return p;
  4196. }
  4197. class = sched_class_highest;
  4198. for ( ; ; ) {
  4199. p = class->pick_next_task(rq);
  4200. if (p)
  4201. return p;
  4202. /*
  4203. * Will never be NULL as the idle class always
  4204. * returns a non-NULL p:
  4205. */
  4206. class = class->next;
  4207. }
  4208. }
  4209. /*
  4210. * schedule() is the main scheduler function.
  4211. */
  4212. asmlinkage void __sched schedule(void)
  4213. {
  4214. struct task_struct *prev, *next;
  4215. unsigned long *switch_count;
  4216. struct rq *rq;
  4217. int cpu;
  4218. need_resched:
  4219. preempt_disable();
  4220. cpu = smp_processor_id();
  4221. rq = cpu_rq(cpu);
  4222. rcu_qsctr_inc(cpu);
  4223. prev = rq->curr;
  4224. switch_count = &prev->nivcsw;
  4225. release_kernel_lock(prev);
  4226. need_resched_nonpreemptible:
  4227. schedule_debug(prev);
  4228. if (sched_feat(HRTICK))
  4229. hrtick_clear(rq);
  4230. spin_lock_irq(&rq->lock);
  4231. update_rq_clock(rq);
  4232. clear_tsk_need_resched(prev);
  4233. if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  4234. if (unlikely(signal_pending_state(prev->state, prev)))
  4235. prev->state = TASK_RUNNING;
  4236. else
  4237. deactivate_task(rq, prev, 1);
  4238. switch_count = &prev->nvcsw;
  4239. }
  4240. #ifdef CONFIG_SMP
  4241. if (prev->sched_class->pre_schedule)
  4242. prev->sched_class->pre_schedule(rq, prev);
  4243. #endif
  4244. if (unlikely(!rq->nr_running))
  4245. idle_balance(cpu, rq);
  4246. put_prev_task(rq, prev);
  4247. next = pick_next_task(rq);
  4248. if (likely(prev != next)) {
  4249. sched_info_switch(prev, next);
  4250. rq->nr_switches++;
  4251. rq->curr = next;
  4252. ++*switch_count;
  4253. context_switch(rq, prev, next); /* unlocks the rq */
  4254. /*
  4255. * the context switch might have flipped the stack from under
  4256. * us, hence refresh the local variables.
  4257. */
  4258. cpu = smp_processor_id();
  4259. rq = cpu_rq(cpu);
  4260. } else
  4261. spin_unlock_irq(&rq->lock);
  4262. if (unlikely(reacquire_kernel_lock(current) < 0))
  4263. goto need_resched_nonpreemptible;
  4264. preempt_enable_no_resched();
  4265. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  4266. goto need_resched;
  4267. }
  4268. EXPORT_SYMBOL(schedule);
  4269. #ifdef CONFIG_PREEMPT
  4270. /*
  4271. * this is the entry point to schedule() from in-kernel preemption
  4272. * off of preempt_enable. Kernel preemptions off return from interrupt
  4273. * occur there and call schedule directly.
  4274. */
  4275. asmlinkage void __sched preempt_schedule(void)
  4276. {
  4277. struct thread_info *ti = current_thread_info();
  4278. /*
  4279. * If there is a non-zero preempt_count or interrupts are disabled,
  4280. * we do not want to preempt the current task. Just return..
  4281. */
  4282. if (likely(ti->preempt_count || irqs_disabled()))
  4283. return;
  4284. do {
  4285. add_preempt_count(PREEMPT_ACTIVE);
  4286. schedule();
  4287. sub_preempt_count(PREEMPT_ACTIVE);
  4288. /*
  4289. * Check again in case we missed a preemption opportunity
  4290. * between schedule and now.
  4291. */
  4292. barrier();
  4293. } while (need_resched());
  4294. }
  4295. EXPORT_SYMBOL(preempt_schedule);
  4296. /*
  4297. * this is the entry point to schedule() from kernel preemption
  4298. * off of irq context.
  4299. * Note, that this is called and return with irqs disabled. This will
  4300. * protect us against recursive calling from irq.
  4301. */
  4302. asmlinkage void __sched preempt_schedule_irq(void)
  4303. {
  4304. struct thread_info *ti = current_thread_info();
  4305. /* Catch callers which need to be fixed */
  4306. BUG_ON(ti->preempt_count || !irqs_disabled());
  4307. do {
  4308. add_preempt_count(PREEMPT_ACTIVE);
  4309. local_irq_enable();
  4310. schedule();
  4311. local_irq_disable();
  4312. sub_preempt_count(PREEMPT_ACTIVE);
  4313. /*
  4314. * Check again in case we missed a preemption opportunity
  4315. * between schedule and now.
  4316. */
  4317. barrier();
  4318. } while (need_resched());
  4319. }
  4320. #endif /* CONFIG_PREEMPT */
  4321. int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
  4322. void *key)
  4323. {
  4324. return try_to_wake_up(curr->private, mode, sync);
  4325. }
  4326. EXPORT_SYMBOL(default_wake_function);
  4327. /*
  4328. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  4329. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  4330. * number) then we wake all the non-exclusive tasks and one exclusive task.
  4331. *
  4332. * There are circumstances in which we can try to wake a task which has already
  4333. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  4334. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  4335. */
  4336. void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  4337. int nr_exclusive, int sync, void *key)
  4338. {
  4339. wait_queue_t *curr, *next;
  4340. list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
  4341. unsigned flags = curr->flags;
  4342. if (curr->func(curr, mode, sync, key) &&
  4343. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  4344. break;
  4345. }
  4346. }
  4347. /**
  4348. * __wake_up - wake up threads blocked on a waitqueue.
  4349. * @q: the waitqueue
  4350. * @mode: which threads
  4351. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  4352. * @key: is directly passed to the wakeup function
  4353. */
  4354. void __wake_up(wait_queue_head_t *q, unsigned int mode,
  4355. int nr_exclusive, void *key)
  4356. {
  4357. unsigned long flags;
  4358. spin_lock_irqsave(&q->lock, flags);
  4359. __wake_up_common(q, mode, nr_exclusive, 0, key);
  4360. spin_unlock_irqrestore(&q->lock, flags);
  4361. }
  4362. EXPORT_SYMBOL(__wake_up);
  4363. /*
  4364. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  4365. */
  4366. void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
  4367. {
  4368. __wake_up_common(q, mode, 1, 0, NULL);
  4369. }
  4370. /**
  4371. * __wake_up_sync - wake up threads blocked on a waitqueue.
  4372. * @q: the waitqueue
  4373. * @mode: which threads
  4374. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  4375. *
  4376. * The sync wakeup differs that the waker knows that it will schedule
  4377. * away soon, so while the target thread will be woken up, it will not
  4378. * be migrated to another CPU - ie. the two threads are 'synchronized'
  4379. * with each other. This can prevent needless bouncing between CPUs.
  4380. *
  4381. * On UP it can prevent extra preemption.
  4382. */
  4383. void
  4384. __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  4385. {
  4386. unsigned long flags;
  4387. int sync = 1;
  4388. if (unlikely(!q))
  4389. return;
  4390. if (unlikely(!nr_exclusive))
  4391. sync = 0;
  4392. spin_lock_irqsave(&q->lock, flags);
  4393. __wake_up_common(q, mode, nr_exclusive, sync, NULL);
  4394. spin_unlock_irqrestore(&q->lock, flags);
  4395. }
  4396. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  4397. /**
  4398. * complete: - signals a single thread waiting on this completion
  4399. * @x: holds the state of this particular completion
  4400. *
  4401. * This will wake up a single thread waiting on this completion. Threads will be
  4402. * awakened in the same order in which they were queued.
  4403. *
  4404. * See also complete_all(), wait_for_completion() and related routines.
  4405. */
  4406. void complete(struct completion *x)
  4407. {
  4408. unsigned long flags;
  4409. spin_lock_irqsave(&x->wait.lock, flags);
  4410. x->done++;
  4411. __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
  4412. spin_unlock_irqrestore(&x->wait.lock, flags);
  4413. }
  4414. EXPORT_SYMBOL(complete);
  4415. /**
  4416. * complete_all: - signals all threads waiting on this completion
  4417. * @x: holds the state of this particular completion
  4418. *
  4419. * This will wake up all threads waiting on this particular completion event.
  4420. */
  4421. void complete_all(struct completion *x)
  4422. {
  4423. unsigned long flags;
  4424. spin_lock_irqsave(&x->wait.lock, flags);
  4425. x->done += UINT_MAX/2;
  4426. __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
  4427. spin_unlock_irqrestore(&x->wait.lock, flags);
  4428. }
  4429. EXPORT_SYMBOL(complete_all);
  4430. static inline long __sched
  4431. do_wait_for_common(struct completion *x, long timeout, int state)
  4432. {
  4433. if (!x->done) {
  4434. DECLARE_WAITQUEUE(wait, current);
  4435. wait.flags |= WQ_FLAG_EXCLUSIVE;
  4436. __add_wait_queue_tail(&x->wait, &wait);
  4437. do {
  4438. if (signal_pending_state(state, current)) {
  4439. timeout = -ERESTARTSYS;
  4440. break;
  4441. }
  4442. __set_current_state(state);
  4443. spin_unlock_irq(&x->wait.lock);
  4444. timeout = schedule_timeout(timeout);
  4445. spin_lock_irq(&x->wait.lock);
  4446. } while (!x->done && timeout);
  4447. __remove_wait_queue(&x->wait, &wait);
  4448. if (!x->done)
  4449. return timeout;
  4450. }
  4451. x->done--;
  4452. return timeout ?: 1;
  4453. }
  4454. static long __sched
  4455. wait_for_common(struct completion *x, long timeout, int state)
  4456. {
  4457. might_sleep();
  4458. spin_lock_irq(&x->wait.lock);
  4459. timeout = do_wait_for_common(x, timeout, state);
  4460. spin_unlock_irq(&x->wait.lock);
  4461. return timeout;
  4462. }
  4463. /**
  4464. * wait_for_completion: - waits for completion of a task
  4465. * @x: holds the state of this particular completion
  4466. *
  4467. * This waits to be signaled for completion of a specific task. It is NOT
  4468. * interruptible and there is no timeout.
  4469. *
  4470. * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
  4471. * and interrupt capability. Also see complete().
  4472. */
  4473. void __sched wait_for_completion(struct completion *x)
  4474. {
  4475. wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
  4476. }
  4477. EXPORT_SYMBOL(wait_for_completion);
  4478. /**
  4479. * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
  4480. * @x: holds the state of this particular completion
  4481. * @timeout: timeout value in jiffies
  4482. *
  4483. * This waits for either a completion of a specific task to be signaled or for a
  4484. * specified timeout to expire. The timeout is in jiffies. It is not
  4485. * interruptible.
  4486. */
  4487. unsigned long __sched
  4488. wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  4489. {
  4490. return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
  4491. }
  4492. EXPORT_SYMBOL(wait_for_completion_timeout);
  4493. /**
  4494. * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
  4495. * @x: holds the state of this particular completion
  4496. *
  4497. * This waits for completion of a specific task to be signaled. It is
  4498. * interruptible.
  4499. */
  4500. int __sched wait_for_completion_interruptible(struct completion *x)
  4501. {
  4502. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
  4503. if (t == -ERESTARTSYS)
  4504. return t;
  4505. return 0;
  4506. }
  4507. EXPORT_SYMBOL(wait_for_completion_interruptible);
  4508. /**
  4509. * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
  4510. * @x: holds the state of this particular completion
  4511. * @timeout: timeout value in jiffies
  4512. *
  4513. * This waits for either a completion of a specific task to be signaled or for a
  4514. * specified timeout to expire. It is interruptible. The timeout is in jiffies.
  4515. */
  4516. unsigned long __sched
  4517. wait_for_completion_interruptible_timeout(struct completion *x,
  4518. unsigned long timeout)
  4519. {
  4520. return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
  4521. }
  4522. EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  4523. /**
  4524. * wait_for_completion_killable: - waits for completion of a task (killable)
  4525. * @x: holds the state of this particular completion
  4526. *
  4527. * This waits to be signaled for completion of a specific task. It can be
  4528. * interrupted by a kill signal.
  4529. */
  4530. int __sched wait_for_completion_killable(struct completion *x)
  4531. {
  4532. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
  4533. if (t == -ERESTARTSYS)
  4534. return t;
  4535. return 0;
  4536. }
  4537. EXPORT_SYMBOL(wait_for_completion_killable);
  4538. /**
  4539. * try_wait_for_completion - try to decrement a completion without blocking
  4540. * @x: completion structure
  4541. *
  4542. * Returns: 0 if a decrement cannot be done without blocking
  4543. * 1 if a decrement succeeded.
  4544. *
  4545. * If a completion is being used as a counting completion,
  4546. * attempt to decrement the counter without blocking. This
  4547. * enables us to avoid waiting if the resource the completion
  4548. * is protecting is not available.
  4549. */
  4550. bool try_wait_for_completion(struct completion *x)
  4551. {
  4552. int ret = 1;
  4553. spin_lock_irq(&x->wait.lock);
  4554. if (!x->done)
  4555. ret = 0;
  4556. else
  4557. x->done--;
  4558. spin_unlock_irq(&x->wait.lock);
  4559. return ret;
  4560. }
  4561. EXPORT_SYMBOL(try_wait_for_completion);
  4562. /**
  4563. * completion_done - Test to see if a completion has any waiters
  4564. * @x: completion structure
  4565. *
  4566. * Returns: 0 if there are waiters (wait_for_completion() in progress)
  4567. * 1 if there are no waiters.
  4568. *
  4569. */
  4570. bool completion_done(struct completion *x)
  4571. {
  4572. int ret = 1;
  4573. spin_lock_irq(&x->wait.lock);
  4574. if (!x->done)
  4575. ret = 0;
  4576. spin_unlock_irq(&x->wait.lock);
  4577. return ret;
  4578. }
  4579. EXPORT_SYMBOL(completion_done);
  4580. static long __sched
  4581. sleep_on_common(wait_queue_head_t *q, int state, long timeout)
  4582. {
  4583. unsigned long flags;
  4584. wait_queue_t wait;
  4585. init_waitqueue_entry(&wait, current);
  4586. __set_current_state(state);
  4587. spin_lock_irqsave(&q->lock, flags);
  4588. __add_wait_queue(q, &wait);
  4589. spin_unlock(&q->lock);
  4590. timeout = schedule_timeout(timeout);
  4591. spin_lock_irq(&q->lock);
  4592. __remove_wait_queue(q, &wait);
  4593. spin_unlock_irqrestore(&q->lock, flags);
  4594. return timeout;
  4595. }
  4596. void __sched interruptible_sleep_on(wait_queue_head_t *q)
  4597. {
  4598. sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  4599. }
  4600. EXPORT_SYMBOL(interruptible_sleep_on);
  4601. long __sched
  4602. interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
  4603. {
  4604. return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
  4605. }
  4606. EXPORT_SYMBOL(interruptible_sleep_on_timeout);
  4607. void __sched sleep_on(wait_queue_head_t *q)
  4608. {
  4609. sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  4610. }
  4611. EXPORT_SYMBOL(sleep_on);
  4612. long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
  4613. {
  4614. return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
  4615. }
  4616. EXPORT_SYMBOL(sleep_on_timeout);
  4617. #ifdef CONFIG_RT_MUTEXES
  4618. /*
  4619. * rt_mutex_setprio - set the current priority of a task
  4620. * @p: task
  4621. * @prio: prio value (kernel-internal form)
  4622. *
  4623. * This function changes the 'effective' priority of a task. It does
  4624. * not touch ->normal_prio like __setscheduler().
  4625. *
  4626. * Used by the rt_mutex code to implement priority inheritance logic.
  4627. */
  4628. void rt_mutex_setprio(struct task_struct *p, int prio)
  4629. {
  4630. unsigned long flags;
  4631. int oldprio, on_rq, running;
  4632. struct rq *rq;
  4633. const struct sched_class *prev_class = p->sched_class;
  4634. BUG_ON(prio < 0 || prio > MAX_PRIO);
  4635. rq = task_rq_lock(p, &flags);
  4636. update_rq_clock(rq);
  4637. oldprio = p->prio;
  4638. on_rq = p->se.on_rq;
  4639. running = task_current(rq, p);
  4640. if (on_rq)
  4641. dequeue_task(rq, p, 0);
  4642. if (running)
  4643. p->sched_class->put_prev_task(rq, p);
  4644. if (rt_prio(prio))
  4645. p->sched_class = &rt_sched_class;
  4646. else
  4647. p->sched_class = &fair_sched_class;
  4648. p->prio = prio;
  4649. if (running)
  4650. p->sched_class->set_curr_task(rq);
  4651. if (on_rq) {
  4652. enqueue_task(rq, p, 0);
  4653. check_class_changed(rq, p, prev_class, oldprio, running);
  4654. }
  4655. task_rq_unlock(rq, &flags);
  4656. }
  4657. #endif
  4658. void set_user_nice(struct task_struct *p, long nice)
  4659. {
  4660. int old_prio, delta, on_rq;
  4661. unsigned long flags;
  4662. struct rq *rq;
  4663. if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
  4664. return;
  4665. /*
  4666. * We have to be careful, if called from sys_setpriority(),
  4667. * the task might be in the middle of scheduling on another CPU.
  4668. */
  4669. rq = task_rq_lock(p, &flags);
  4670. update_rq_clock(rq);
  4671. /*
  4672. * The RT priorities are set via sched_setscheduler(), but we still
  4673. * allow the 'normal' nice value to be set - but as expected
  4674. * it wont have any effect on scheduling until the task is
  4675. * SCHED_FIFO/SCHED_RR:
  4676. */
  4677. if (task_has_rt_policy(p)) {
  4678. p->static_prio = NICE_TO_PRIO(nice);
  4679. goto out_unlock;
  4680. }
  4681. on_rq = p->se.on_rq;
  4682. if (on_rq)
  4683. dequeue_task(rq, p, 0);
  4684. p->static_prio = NICE_TO_PRIO(nice);
  4685. set_load_weight(p);
  4686. old_prio = p->prio;
  4687. p->prio = effective_prio(p);
  4688. delta = p->prio - old_prio;
  4689. if (on_rq) {
  4690. enqueue_task(rq, p, 0);
  4691. /*
  4692. * If the task increased its priority or is running and
  4693. * lowered its priority, then reschedule its CPU:
  4694. */
  4695. if (delta < 0 || (delta > 0 && task_running(rq, p)))
  4696. resched_task(rq->curr);
  4697. }
  4698. out_unlock:
  4699. task_rq_unlock(rq, &flags);
  4700. }
  4701. EXPORT_SYMBOL(set_user_nice);
  4702. /*
  4703. * can_nice - check if a task can reduce its nice value
  4704. * @p: task
  4705. * @nice: nice value
  4706. */
  4707. int can_nice(const struct task_struct *p, const int nice)
  4708. {
  4709. /* convert nice value [19,-20] to rlimit style value [1,40] */
  4710. int nice_rlim = 20 - nice;
  4711. return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
  4712. capable(CAP_SYS_NICE));
  4713. }
  4714. #ifdef __ARCH_WANT_SYS_NICE
  4715. /*
  4716. * sys_nice - change the priority of the current process.
  4717. * @increment: priority increment
  4718. *
  4719. * sys_setpriority is a more generic, but much slower function that
  4720. * does similar things.
  4721. */
  4722. SYSCALL_DEFINE1(nice, int, increment)
  4723. {
  4724. long nice, retval;
  4725. /*
  4726. * Setpriority might change our priority at the same moment.
  4727. * We don't have to worry. Conceptually one call occurs first
  4728. * and we have a single winner.
  4729. */
  4730. if (increment < -40)
  4731. increment = -40;
  4732. if (increment > 40)
  4733. increment = 40;
  4734. nice = TASK_NICE(current) + increment;
  4735. if (nice < -20)
  4736. nice = -20;
  4737. if (nice > 19)
  4738. nice = 19;
  4739. if (increment < 0 && !can_nice(current, nice))
  4740. return -EPERM;
  4741. retval = security_task_setnice(current, nice);
  4742. if (retval)
  4743. return retval;
  4744. set_user_nice(current, nice);
  4745. return 0;
  4746. }
  4747. #endif
  4748. /**
  4749. * task_prio - return the priority value of a given task.
  4750. * @p: the task in question.
  4751. *
  4752. * This is the priority value as seen by users in /proc.
  4753. * RT tasks are offset by -200. Normal tasks are centered
  4754. * around 0, value goes from -16 to +15.
  4755. */
  4756. int task_prio(const struct task_struct *p)
  4757. {
  4758. return p->prio - MAX_RT_PRIO;
  4759. }
  4760. /**
  4761. * task_nice - return the nice value of a given task.
  4762. * @p: the task in question.
  4763. */
  4764. int task_nice(const struct task_struct *p)
  4765. {
  4766. return TASK_NICE(p);
  4767. }
  4768. EXPORT_SYMBOL(task_nice);
  4769. /**
  4770. * idle_cpu - is a given cpu idle currently?
  4771. * @cpu: the processor in question.
  4772. */
  4773. int idle_cpu(int cpu)
  4774. {
  4775. return cpu_curr(cpu) == cpu_rq(cpu)->idle;
  4776. }
  4777. /**
  4778. * idle_task - return the idle task for a given cpu.
  4779. * @cpu: the processor in question.
  4780. */
  4781. struct task_struct *idle_task(int cpu)
  4782. {
  4783. return cpu_rq(cpu)->idle;
  4784. }
  4785. /**
  4786. * find_process_by_pid - find a process with a matching PID value.
  4787. * @pid: the pid in question.
  4788. */
  4789. static struct task_struct *find_process_by_pid(pid_t pid)
  4790. {
  4791. return pid ? find_task_by_vpid(pid) : current;
  4792. }
  4793. /* Actually do priority change: must hold rq lock. */
  4794. static void
  4795. __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
  4796. {
  4797. BUG_ON(p->se.on_rq);
  4798. p->policy = policy;
  4799. switch (p->policy) {
  4800. case SCHED_NORMAL:
  4801. case SCHED_BATCH:
  4802. case SCHED_IDLE:
  4803. p->sched_class = &fair_sched_class;
  4804. break;
  4805. case SCHED_FIFO:
  4806. case SCHED_RR:
  4807. p->sched_class = &rt_sched_class;
  4808. break;
  4809. }
  4810. p->rt_priority = prio;
  4811. p->normal_prio = normal_prio(p);
  4812. /* we are holding p->pi_lock already */
  4813. p->prio = rt_mutex_getprio(p);
  4814. set_load_weight(p);
  4815. }
  4816. /*
  4817. * check the target process has a UID that matches the current process's
  4818. */
  4819. static bool check_same_owner(struct task_struct *p)
  4820. {
  4821. const struct cred *cred = current_cred(), *pcred;
  4822. bool match;
  4823. rcu_read_lock();
  4824. pcred = __task_cred(p);
  4825. match = (cred->euid == pcred->euid ||
  4826. cred->euid == pcred->uid);
  4827. rcu_read_unlock();
  4828. return match;
  4829. }
  4830. static int __sched_setscheduler(struct task_struct *p, int policy,
  4831. struct sched_param *param, bool user)
  4832. {
  4833. int retval, oldprio, oldpolicy = -1, on_rq, running;
  4834. unsigned long flags;
  4835. const struct sched_class *prev_class = p->sched_class;
  4836. struct rq *rq;
  4837. /* may grab non-irq protected spin_locks */
  4838. BUG_ON(in_interrupt());
  4839. recheck:
  4840. /* double check policy once rq lock held */
  4841. if (policy < 0)
  4842. policy = oldpolicy = p->policy;
  4843. else if (policy != SCHED_FIFO && policy != SCHED_RR &&
  4844. policy != SCHED_NORMAL && policy != SCHED_BATCH &&
  4845. policy != SCHED_IDLE)
  4846. return -EINVAL;
  4847. /*
  4848. * Valid priorities for SCHED_FIFO and SCHED_RR are
  4849. * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
  4850. * SCHED_BATCH and SCHED_IDLE is 0.
  4851. */
  4852. if (param->sched_priority < 0 ||
  4853. (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
  4854. (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
  4855. return -EINVAL;
  4856. if (rt_policy(policy) != (param->sched_priority != 0))
  4857. return -EINVAL;
  4858. /*
  4859. * Allow unprivileged RT tasks to decrease priority:
  4860. */
  4861. if (user && !capable(CAP_SYS_NICE)) {
  4862. if (rt_policy(policy)) {
  4863. unsigned long rlim_rtprio;
  4864. if (!lock_task_sighand(p, &flags))
  4865. return -ESRCH;
  4866. rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
  4867. unlock_task_sighand(p, &flags);
  4868. /* can't set/change the rt policy */
  4869. if (policy != p->policy && !rlim_rtprio)
  4870. return -EPERM;
  4871. /* can't increase priority */
  4872. if (param->sched_priority > p->rt_priority &&
  4873. param->sched_priority > rlim_rtprio)
  4874. return -EPERM;
  4875. }
  4876. /*
  4877. * Like positive nice levels, dont allow tasks to
  4878. * move out of SCHED_IDLE either:
  4879. */
  4880. if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
  4881. return -EPERM;
  4882. /* can't change other user's priorities */
  4883. if (!check_same_owner(p))
  4884. return -EPERM;
  4885. }
  4886. if (user) {
  4887. #ifdef CONFIG_RT_GROUP_SCHED
  4888. /*
  4889. * Do not allow realtime tasks into groups that have no runtime
  4890. * assigned.
  4891. */
  4892. if (rt_bandwidth_enabled() && rt_policy(policy) &&
  4893. task_group(p)->rt_bandwidth.rt_runtime == 0)
  4894. return -EPERM;
  4895. #endif
  4896. retval = security_task_setscheduler(p, policy, param);
  4897. if (retval)
  4898. return retval;
  4899. }
  4900. /*
  4901. * make sure no PI-waiters arrive (or leave) while we are
  4902. * changing the priority of the task:
  4903. */
  4904. spin_lock_irqsave(&p->pi_lock, flags);
  4905. /*
  4906. * To be able to change p->policy safely, the apropriate
  4907. * runqueue lock must be held.
  4908. */
  4909. rq = __task_rq_lock(p);
  4910. /* recheck policy now with rq lock held */
  4911. if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  4912. policy = oldpolicy = -1;
  4913. __task_rq_unlock(rq);
  4914. spin_unlock_irqrestore(&p->pi_lock, flags);
  4915. goto recheck;
  4916. }
  4917. update_rq_clock(rq);
  4918. on_rq = p->se.on_rq;
  4919. running = task_current(rq, p);
  4920. if (on_rq)
  4921. deactivate_task(rq, p, 0);
  4922. if (running)
  4923. p->sched_class->put_prev_task(rq, p);
  4924. oldprio = p->prio;
  4925. __setscheduler(rq, p, policy, param->sched_priority);
  4926. if (running)
  4927. p->sched_class->set_curr_task(rq);
  4928. if (on_rq) {
  4929. activate_task(rq, p, 0);
  4930. check_class_changed(rq, p, prev_class, oldprio, running);
  4931. }
  4932. __task_rq_unlock(rq);
  4933. spin_unlock_irqrestore(&p->pi_lock, flags);
  4934. rt_mutex_adjust_pi(p);
  4935. return 0;
  4936. }
  4937. /**
  4938. * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
  4939. * @p: the task in question.
  4940. * @policy: new policy.
  4941. * @param: structure containing the new RT priority.
  4942. *
  4943. * NOTE that the task may be already dead.
  4944. */
  4945. int sched_setscheduler(struct task_struct *p, int policy,
  4946. struct sched_param *param)
  4947. {
  4948. return __sched_setscheduler(p, policy, param, true);
  4949. }
  4950. EXPORT_SYMBOL_GPL(sched_setscheduler);
  4951. /**
  4952. * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
  4953. * @p: the task in question.
  4954. * @policy: new policy.
  4955. * @param: structure containing the new RT priority.
  4956. *
  4957. * Just like sched_setscheduler, only don't bother checking if the
  4958. * current context has permission. For example, this is needed in
  4959. * stop_machine(): we create temporary high priority worker threads,
  4960. * but our caller might not have that capability.
  4961. */
  4962. int sched_setscheduler_nocheck(struct task_struct *p, int policy,
  4963. struct sched_param *param)
  4964. {
  4965. return __sched_setscheduler(p, policy, param, false);
  4966. }
  4967. static int
  4968. do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  4969. {
  4970. struct sched_param lparam;
  4971. struct task_struct *p;
  4972. int retval;
  4973. if (!param || pid < 0)
  4974. return -EINVAL;
  4975. if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  4976. return -EFAULT;
  4977. rcu_read_lock();
  4978. retval = -ESRCH;
  4979. p = find_process_by_pid(pid);
  4980. if (p != NULL)
  4981. retval = sched_setscheduler(p, policy, &lparam);
  4982. rcu_read_unlock();
  4983. return retval;
  4984. }
  4985. /**
  4986. * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  4987. * @pid: the pid in question.
  4988. * @policy: new policy.
  4989. * @param: structure containing the new RT priority.
  4990. */
  4991. SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
  4992. struct sched_param __user *, param)
  4993. {
  4994. /* negative values for policy are not valid */
  4995. if (policy < 0)
  4996. return -EINVAL;
  4997. return do_sched_setscheduler(pid, policy, param);
  4998. }
  4999. /**
  5000. * sys_sched_setparam - set/change the RT priority of a thread
  5001. * @pid: the pid in question.
  5002. * @param: structure containing the new RT priority.
  5003. */
  5004. SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
  5005. {
  5006. return do_sched_setscheduler(pid, -1, param);
  5007. }
  5008. /**
  5009. * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  5010. * @pid: the pid in question.
  5011. */
  5012. SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
  5013. {
  5014. struct task_struct *p;
  5015. int retval;
  5016. if (pid < 0)
  5017. return -EINVAL;
  5018. retval = -ESRCH;
  5019. read_lock(&tasklist_lock);
  5020. p = find_process_by_pid(pid);
  5021. if (p) {
  5022. retval = security_task_getscheduler(p);
  5023. if (!retval)
  5024. retval = p->policy;
  5025. }
  5026. read_unlock(&tasklist_lock);
  5027. return retval;
  5028. }
  5029. /**
  5030. * sys_sched_getscheduler - get the RT priority of a thread
  5031. * @pid: the pid in question.
  5032. * @param: structure containing the RT priority.
  5033. */
  5034. SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
  5035. {
  5036. struct sched_param lp;
  5037. struct task_struct *p;
  5038. int retval;
  5039. if (!param || pid < 0)
  5040. return -EINVAL;
  5041. read_lock(&tasklist_lock);
  5042. p = find_process_by_pid(pid);
  5043. retval = -ESRCH;
  5044. if (!p)
  5045. goto out_unlock;
  5046. retval = security_task_getscheduler(p);
  5047. if (retval)
  5048. goto out_unlock;
  5049. lp.sched_priority = p->rt_priority;
  5050. read_unlock(&tasklist_lock);
  5051. /*
  5052. * This one might sleep, we cannot do it with a spinlock held ...
  5053. */
  5054. retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  5055. return retval;
  5056. out_unlock:
  5057. read_unlock(&tasklist_lock);
  5058. return retval;
  5059. }
  5060. long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
  5061. {
  5062. cpumask_var_t cpus_allowed, new_mask;
  5063. struct task_struct *p;
  5064. int retval;
  5065. get_online_cpus();
  5066. read_lock(&tasklist_lock);
  5067. p = find_process_by_pid(pid);
  5068. if (!p) {
  5069. read_unlock(&tasklist_lock);
  5070. put_online_cpus();
  5071. return -ESRCH;
  5072. }
  5073. /*
  5074. * It is not safe to call set_cpus_allowed with the
  5075. * tasklist_lock held. We will bump the task_struct's
  5076. * usage count and then drop tasklist_lock.
  5077. */
  5078. get_task_struct(p);
  5079. read_unlock(&tasklist_lock);
  5080. if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
  5081. retval = -ENOMEM;
  5082. goto out_put_task;
  5083. }
  5084. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
  5085. retval = -ENOMEM;
  5086. goto out_free_cpus_allowed;
  5087. }
  5088. retval = -EPERM;
  5089. if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
  5090. goto out_unlock;
  5091. retval = security_task_setscheduler(p, 0, NULL);
  5092. if (retval)
  5093. goto out_unlock;
  5094. cpuset_cpus_allowed(p, cpus_allowed);
  5095. cpumask_and(new_mask, in_mask, cpus_allowed);
  5096. again:
  5097. retval = set_cpus_allowed_ptr(p, new_mask);
  5098. if (!retval) {
  5099. cpuset_cpus_allowed(p, cpus_allowed);
  5100. if (!cpumask_subset(new_mask, cpus_allowed)) {
  5101. /*
  5102. * We must have raced with a concurrent cpuset
  5103. * update. Just reset the cpus_allowed to the
  5104. * cpuset's cpus_allowed
  5105. */
  5106. cpumask_copy(new_mask, cpus_allowed);
  5107. goto again;
  5108. }
  5109. }
  5110. out_unlock:
  5111. free_cpumask_var(new_mask);
  5112. out_free_cpus_allowed:
  5113. free_cpumask_var(cpus_allowed);
  5114. out_put_task:
  5115. put_task_struct(p);
  5116. put_online_cpus();
  5117. return retval;
  5118. }
  5119. static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  5120. struct cpumask *new_mask)
  5121. {
  5122. if (len < cpumask_size())
  5123. cpumask_clear(new_mask);
  5124. else if (len > cpumask_size())
  5125. len = cpumask_size();
  5126. return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  5127. }
  5128. /**
  5129. * sys_sched_setaffinity - set the cpu affinity of a process
  5130. * @pid: pid of the process
  5131. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  5132. * @user_mask_ptr: user-space pointer to the new cpu mask
  5133. */
  5134. SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
  5135. unsigned long __user *, user_mask_ptr)
  5136. {
  5137. cpumask_var_t new_mask;
  5138. int retval;
  5139. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  5140. return -ENOMEM;
  5141. retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
  5142. if (retval == 0)
  5143. retval = sched_setaffinity(pid, new_mask);
  5144. free_cpumask_var(new_mask);
  5145. return retval;
  5146. }
  5147. long sched_getaffinity(pid_t pid, struct cpumask *mask)
  5148. {
  5149. struct task_struct *p;
  5150. int retval;
  5151. get_online_cpus();
  5152. read_lock(&tasklist_lock);
  5153. retval = -ESRCH;
  5154. p = find_process_by_pid(pid);
  5155. if (!p)
  5156. goto out_unlock;
  5157. retval = security_task_getscheduler(p);
  5158. if (retval)
  5159. goto out_unlock;
  5160. cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
  5161. out_unlock:
  5162. read_unlock(&tasklist_lock);
  5163. put_online_cpus();
  5164. return retval;
  5165. }
  5166. /**
  5167. * sys_sched_getaffinity - get the cpu affinity of a process
  5168. * @pid: pid of the process
  5169. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  5170. * @user_mask_ptr: user-space pointer to hold the current cpu mask
  5171. */
  5172. SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
  5173. unsigned long __user *, user_mask_ptr)
  5174. {
  5175. int ret;
  5176. cpumask_var_t mask;
  5177. if (len < cpumask_size())
  5178. return -EINVAL;
  5179. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  5180. return -ENOMEM;
  5181. ret = sched_getaffinity(pid, mask);
  5182. if (ret == 0) {
  5183. if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
  5184. ret = -EFAULT;
  5185. else
  5186. ret = cpumask_size();
  5187. }
  5188. free_cpumask_var(mask);
  5189. return ret;
  5190. }
  5191. /**
  5192. * sys_sched_yield - yield the current processor to other threads.
  5193. *
  5194. * This function yields the current CPU to other tasks. If there are no
  5195. * other threads running on this CPU then this function will return.
  5196. */
  5197. SYSCALL_DEFINE0(sched_yield)
  5198. {
  5199. struct rq *rq = this_rq_lock();
  5200. schedstat_inc(rq, yld_count);
  5201. current->sched_class->yield_task(rq);
  5202. /*
  5203. * Since we are going to call schedule() anyway, there's
  5204. * no need to preempt or enable interrupts:
  5205. */
  5206. __release(rq->lock);
  5207. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  5208. _raw_spin_unlock(&rq->lock);
  5209. preempt_enable_no_resched();
  5210. schedule();
  5211. return 0;
  5212. }
  5213. static void __cond_resched(void)
  5214. {
  5215. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  5216. __might_sleep(__FILE__, __LINE__);
  5217. #endif
  5218. /*
  5219. * The BKS might be reacquired before we have dropped
  5220. * PREEMPT_ACTIVE, which could trigger a second
  5221. * cond_resched() call.
  5222. */
  5223. do {
  5224. add_preempt_count(PREEMPT_ACTIVE);
  5225. schedule();
  5226. sub_preempt_count(PREEMPT_ACTIVE);
  5227. } while (need_resched());
  5228. }
  5229. int __sched _cond_resched(void)
  5230. {
  5231. if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
  5232. system_state == SYSTEM_RUNNING) {
  5233. __cond_resched();
  5234. return 1;
  5235. }
  5236. return 0;
  5237. }
  5238. EXPORT_SYMBOL(_cond_resched);
  5239. /*
  5240. * cond_resched_lock() - if a reschedule is pending, drop the given lock,
  5241. * call schedule, and on return reacquire the lock.
  5242. *
  5243. * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  5244. * operations here to prevent schedule() from being called twice (once via
  5245. * spin_unlock(), once by hand).
  5246. */
  5247. int cond_resched_lock(spinlock_t *lock)
  5248. {
  5249. int resched = need_resched() && system_state == SYSTEM_RUNNING;
  5250. int ret = 0;
  5251. if (spin_needbreak(lock) || resched) {
  5252. spin_unlock(lock);
  5253. if (resched && need_resched())
  5254. __cond_resched();
  5255. else
  5256. cpu_relax();
  5257. ret = 1;
  5258. spin_lock(lock);
  5259. }
  5260. return ret;
  5261. }
  5262. EXPORT_SYMBOL(cond_resched_lock);
  5263. int __sched cond_resched_softirq(void)
  5264. {
  5265. BUG_ON(!in_softirq());
  5266. if (need_resched() && system_state == SYSTEM_RUNNING) {
  5267. local_bh_enable();
  5268. __cond_resched();
  5269. local_bh_disable();
  5270. return 1;
  5271. }
  5272. return 0;
  5273. }
  5274. EXPORT_SYMBOL(cond_resched_softirq);
  5275. /**
  5276. * yield - yield the current processor to other threads.
  5277. *
  5278. * This is a shortcut for kernel-space yielding - it marks the
  5279. * thread runnable and calls sys_sched_yield().
  5280. */
  5281. void __sched yield(void)
  5282. {
  5283. set_current_state(TASK_RUNNING);
  5284. sys_sched_yield();
  5285. }
  5286. EXPORT_SYMBOL(yield);
  5287. /*
  5288. * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  5289. * that process accounting knows that this is a task in IO wait state.
  5290. *
  5291. * But don't do that if it is a deliberate, throttling IO wait (this task
  5292. * has set its backing_dev_info: the queue against which it should throttle)
  5293. */
  5294. void __sched io_schedule(void)
  5295. {
  5296. struct rq *rq = &__raw_get_cpu_var(runqueues);
  5297. delayacct_blkio_start();
  5298. atomic_inc(&rq->nr_iowait);
  5299. schedule();
  5300. atomic_dec(&rq->nr_iowait);
  5301. delayacct_blkio_end();
  5302. }
  5303. EXPORT_SYMBOL(io_schedule);
  5304. long __sched io_schedule_timeout(long timeout)
  5305. {
  5306. struct rq *rq = &__raw_get_cpu_var(runqueues);
  5307. long ret;
  5308. delayacct_blkio_start();
  5309. atomic_inc(&rq->nr_iowait);
  5310. ret = schedule_timeout(timeout);
  5311. atomic_dec(&rq->nr_iowait);
  5312. delayacct_blkio_end();
  5313. return ret;
  5314. }
  5315. /**
  5316. * sys_sched_get_priority_max - return maximum RT priority.
  5317. * @policy: scheduling class.
  5318. *
  5319. * this syscall returns the maximum rt_priority that can be used
  5320. * by a given scheduling class.
  5321. */
  5322. SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
  5323. {
  5324. int ret = -EINVAL;
  5325. switch (policy) {
  5326. case SCHED_FIFO:
  5327. case SCHED_RR:
  5328. ret = MAX_USER_RT_PRIO-1;
  5329. break;
  5330. case SCHED_NORMAL:
  5331. case SCHED_BATCH:
  5332. case SCHED_IDLE:
  5333. ret = 0;
  5334. break;
  5335. }
  5336. return ret;
  5337. }
  5338. /**
  5339. * sys_sched_get_priority_min - return minimum RT priority.
  5340. * @policy: scheduling class.
  5341. *
  5342. * this syscall returns the minimum rt_priority that can be used
  5343. * by a given scheduling class.
  5344. */
  5345. SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
  5346. {
  5347. int ret = -EINVAL;
  5348. switch (policy) {
  5349. case SCHED_FIFO:
  5350. case SCHED_RR:
  5351. ret = 1;
  5352. break;
  5353. case SCHED_NORMAL:
  5354. case SCHED_BATCH:
  5355. case SCHED_IDLE:
  5356. ret = 0;
  5357. }
  5358. return ret;
  5359. }
  5360. /**
  5361. * sys_sched_rr_get_interval - return the default timeslice of a process.
  5362. * @pid: pid of the process.
  5363. * @interval: userspace pointer to the timeslice value.
  5364. *
  5365. * this syscall writes the default timeslice value of a given process
  5366. * into the user-space timespec buffer. A value of '0' means infinity.
  5367. */
  5368. SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
  5369. struct timespec __user *, interval)
  5370. {
  5371. struct task_struct *p;
  5372. unsigned int time_slice;
  5373. int retval;
  5374. struct timespec t;
  5375. if (pid < 0)
  5376. return -EINVAL;
  5377. retval = -ESRCH;
  5378. read_lock(&tasklist_lock);
  5379. p = find_process_by_pid(pid);
  5380. if (!p)
  5381. goto out_unlock;
  5382. retval = security_task_getscheduler(p);
  5383. if (retval)
  5384. goto out_unlock;
  5385. /*
  5386. * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
  5387. * tasks that are on an otherwise idle runqueue:
  5388. */
  5389. time_slice = 0;
  5390. if (p->policy == SCHED_RR) {
  5391. time_slice = DEF_TIMESLICE;
  5392. } else if (p->policy != SCHED_FIFO) {
  5393. struct sched_entity *se = &p->se;
  5394. unsigned long flags;
  5395. struct rq *rq;
  5396. rq = task_rq_lock(p, &flags);
  5397. if (rq->cfs.load.weight)
  5398. time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
  5399. task_rq_unlock(rq, &flags);
  5400. }
  5401. read_unlock(&tasklist_lock);
  5402. jiffies_to_timespec(time_slice, &t);
  5403. retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  5404. return retval;
  5405. out_unlock:
  5406. read_unlock(&tasklist_lock);
  5407. return retval;
  5408. }
  5409. static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
  5410. void sched_show_task(struct task_struct *p)
  5411. {
  5412. unsigned long free = 0;
  5413. unsigned state;
  5414. state = p->state ? __ffs(p->state) + 1 : 0;
  5415. printk(KERN_INFO "%-13.13s %c", p->comm,
  5416. state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
  5417. #if BITS_PER_LONG == 32
  5418. if (state == TASK_RUNNING)
  5419. printk(KERN_CONT " running ");
  5420. else
  5421. printk(KERN_CONT " %08lx ", thread_saved_pc(p));
  5422. #else
  5423. if (state == TASK_RUNNING)
  5424. printk(KERN_CONT " running task ");
  5425. else
  5426. printk(KERN_CONT " %016lx ", thread_saved_pc(p));
  5427. #endif
  5428. #ifdef CONFIG_DEBUG_STACK_USAGE
  5429. free = stack_not_used(p);
  5430. #endif
  5431. printk(KERN_CONT "%5lu %5d %6d\n", free,
  5432. task_pid_nr(p), task_pid_nr(p->real_parent));
  5433. show_stack(p, NULL);
  5434. }
  5435. void show_state_filter(unsigned long state_filter)
  5436. {
  5437. struct task_struct *g, *p;
  5438. #if BITS_PER_LONG == 32
  5439. printk(KERN_INFO
  5440. " task PC stack pid father\n");
  5441. #else
  5442. printk(KERN_INFO
  5443. " task PC stack pid father\n");
  5444. #endif
  5445. read_lock(&tasklist_lock);
  5446. do_each_thread(g, p) {
  5447. /*
  5448. * reset the NMI-timeout, listing all files on a slow
  5449. * console might take alot of time:
  5450. */
  5451. touch_nmi_watchdog();
  5452. if (!state_filter || (p->state & state_filter))
  5453. sched_show_task(p);
  5454. } while_each_thread(g, p);
  5455. touch_all_softlockup_watchdogs();
  5456. #ifdef CONFIG_SCHED_DEBUG
  5457. sysrq_sched_debug_show();
  5458. #endif
  5459. read_unlock(&tasklist_lock);
  5460. /*
  5461. * Only show locks if all tasks are dumped:
  5462. */
  5463. if (state_filter == -1)
  5464. debug_show_all_locks();
  5465. }
  5466. void __cpuinit init_idle_bootup_task(struct task_struct *idle)
  5467. {
  5468. idle->sched_class = &idle_sched_class;
  5469. }
  5470. /**
  5471. * init_idle - set up an idle thread for a given CPU
  5472. * @idle: task in question
  5473. * @cpu: cpu the idle task belongs to
  5474. *
  5475. * NOTE: this function does not set the idle thread's NEED_RESCHED
  5476. * flag, to make booting more robust.
  5477. */
  5478. void __cpuinit init_idle(struct task_struct *idle, int cpu)
  5479. {
  5480. struct rq *rq = cpu_rq(cpu);
  5481. unsigned long flags;
  5482. spin_lock_irqsave(&rq->lock, flags);
  5483. __sched_fork(idle);
  5484. idle->se.exec_start = sched_clock();
  5485. idle->prio = idle->normal_prio = MAX_PRIO;
  5486. cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
  5487. __set_task_cpu(idle, cpu);
  5488. rq->curr = rq->idle = idle;
  5489. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  5490. idle->oncpu = 1;
  5491. #endif
  5492. spin_unlock_irqrestore(&rq->lock, flags);
  5493. /* Set the preempt count _outside_ the spinlocks! */
  5494. #if defined(CONFIG_PREEMPT)
  5495. task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
  5496. #else
  5497. task_thread_info(idle)->preempt_count = 0;
  5498. #endif
  5499. /*
  5500. * The idle tasks have their own, simple scheduling class:
  5501. */
  5502. idle->sched_class = &idle_sched_class;
  5503. ftrace_graph_init_task(idle);
  5504. }
  5505. /*
  5506. * In a system that switches off the HZ timer nohz_cpu_mask
  5507. * indicates which cpus entered this state. This is used
  5508. * in the rcu update to wait only for active cpus. For system
  5509. * which do not switch off the HZ timer nohz_cpu_mask should
  5510. * always be CPU_BITS_NONE.
  5511. */
  5512. cpumask_var_t nohz_cpu_mask;
  5513. /*
  5514. * Increase the granularity value when there are more CPUs,
  5515. * because with more CPUs the 'effective latency' as visible
  5516. * to users decreases. But the relationship is not linear,
  5517. * so pick a second-best guess by going with the log2 of the
  5518. * number of CPUs.
  5519. *
  5520. * This idea comes from the SD scheduler of Con Kolivas:
  5521. */
  5522. static inline void sched_init_granularity(void)
  5523. {
  5524. unsigned int factor = 1 + ilog2(num_online_cpus());
  5525. const unsigned long limit = 200000000;
  5526. sysctl_sched_min_granularity *= factor;
  5527. if (sysctl_sched_min_granularity > limit)
  5528. sysctl_sched_min_granularity = limit;
  5529. sysctl_sched_latency *= factor;
  5530. if (sysctl_sched_latency > limit)
  5531. sysctl_sched_latency = limit;
  5532. sysctl_sched_wakeup_granularity *= factor;
  5533. sysctl_sched_shares_ratelimit *= factor;
  5534. }
  5535. #ifdef CONFIG_SMP
  5536. /*
  5537. * This is how migration works:
  5538. *
  5539. * 1) we queue a struct migration_req structure in the source CPU's
  5540. * runqueue and wake up that CPU's migration thread.
  5541. * 2) we down() the locked semaphore => thread blocks.
  5542. * 3) migration thread wakes up (implicitly it forces the migrated
  5543. * thread off the CPU)
  5544. * 4) it gets the migration request and checks whether the migrated
  5545. * task is still in the wrong runqueue.
  5546. * 5) if it's in the wrong runqueue then the migration thread removes
  5547. * it and puts it into the right queue.
  5548. * 6) migration thread up()s the semaphore.
  5549. * 7) we wake up and the migration is done.
  5550. */
  5551. /*
  5552. * Change a given task's CPU affinity. Migrate the thread to a
  5553. * proper CPU and schedule it away if the CPU it's executing on
  5554. * is removed from the allowed bitmask.
  5555. *
  5556. * NOTE: the caller must have a valid reference to the task, the
  5557. * task must not exit() & deallocate itself prematurely. The
  5558. * call is not atomic; no spinlocks may be held.
  5559. */
  5560. int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
  5561. {
  5562. struct migration_req req;
  5563. unsigned long flags;
  5564. struct rq *rq;
  5565. int ret = 0;
  5566. rq = task_rq_lock(p, &flags);
  5567. if (!cpumask_intersects(new_mask, cpu_online_mask)) {
  5568. ret = -EINVAL;
  5569. goto out;
  5570. }
  5571. if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
  5572. !cpumask_equal(&p->cpus_allowed, new_mask))) {
  5573. ret = -EINVAL;
  5574. goto out;
  5575. }
  5576. if (p->sched_class->set_cpus_allowed)
  5577. p->sched_class->set_cpus_allowed(p, new_mask);
  5578. else {
  5579. cpumask_copy(&p->cpus_allowed, new_mask);
  5580. p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
  5581. }
  5582. /* Can the task run on the task's current CPU? If so, we're done */
  5583. if (cpumask_test_cpu(task_cpu(p), new_mask))
  5584. goto out;
  5585. if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) {
  5586. /* Need help from migration thread: drop lock and wait. */
  5587. task_rq_unlock(rq, &flags);
  5588. wake_up_process(rq->migration_thread);
  5589. wait_for_completion(&req.done);
  5590. tlb_migrate_finish(p->mm);
  5591. return 0;
  5592. }
  5593. out:
  5594. task_rq_unlock(rq, &flags);
  5595. return ret;
  5596. }
  5597. EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
  5598. /*
  5599. * Move (not current) task off this cpu, onto dest cpu. We're doing
  5600. * this because either it can't run here any more (set_cpus_allowed()
  5601. * away from this CPU, or CPU going down), or because we're
  5602. * attempting to rebalance this task on exec (sched_exec).
  5603. *
  5604. * So we race with normal scheduler movements, but that's OK, as long
  5605. * as the task is no longer on this CPU.
  5606. *
  5607. * Returns non-zero if task was successfully migrated.
  5608. */
  5609. static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
  5610. {
  5611. struct rq *rq_dest, *rq_src;
  5612. int ret = 0, on_rq;
  5613. if (unlikely(!cpu_active(dest_cpu)))
  5614. return ret;
  5615. rq_src = cpu_rq(src_cpu);
  5616. rq_dest = cpu_rq(dest_cpu);
  5617. double_rq_lock(rq_src, rq_dest);
  5618. /* Already moved. */
  5619. if (task_cpu(p) != src_cpu)
  5620. goto done;
  5621. /* Affinity changed (again). */
  5622. if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
  5623. goto fail;
  5624. on_rq = p->se.on_rq;
  5625. if (on_rq)
  5626. deactivate_task(rq_src, p, 0);
  5627. set_task_cpu(p, dest_cpu);
  5628. if (on_rq) {
  5629. activate_task(rq_dest, p, 0);
  5630. check_preempt_curr(rq_dest, p, 0);
  5631. }
  5632. done:
  5633. ret = 1;
  5634. fail:
  5635. double_rq_unlock(rq_src, rq_dest);
  5636. return ret;
  5637. }
  5638. /*
  5639. * migration_thread - this is a highprio system thread that performs
  5640. * thread migration by bumping thread off CPU then 'pushing' onto
  5641. * another runqueue.
  5642. */
  5643. static int migration_thread(void *data)
  5644. {
  5645. int cpu = (long)data;
  5646. struct rq *rq;
  5647. rq = cpu_rq(cpu);
  5648. BUG_ON(rq->migration_thread != current);
  5649. set_current_state(TASK_INTERRUPTIBLE);
  5650. while (!kthread_should_stop()) {
  5651. struct migration_req *req;
  5652. struct list_head *head;
  5653. spin_lock_irq(&rq->lock);
  5654. if (cpu_is_offline(cpu)) {
  5655. spin_unlock_irq(&rq->lock);
  5656. goto wait_to_die;
  5657. }
  5658. if (rq->active_balance) {
  5659. active_load_balance(rq, cpu);
  5660. rq->active_balance = 0;
  5661. }
  5662. head = &rq->migration_queue;
  5663. if (list_empty(head)) {
  5664. spin_unlock_irq(&rq->lock);
  5665. schedule();
  5666. set_current_state(TASK_INTERRUPTIBLE);
  5667. continue;
  5668. }
  5669. req = list_entry(head->next, struct migration_req, list);
  5670. list_del_init(head->next);
  5671. spin_unlock(&rq->lock);
  5672. __migrate_task(req->task, cpu, req->dest_cpu);
  5673. local_irq_enable();
  5674. complete(&req->done);
  5675. }
  5676. __set_current_state(TASK_RUNNING);
  5677. return 0;
  5678. wait_to_die:
  5679. /* Wait for kthread_stop */
  5680. set_current_state(TASK_INTERRUPTIBLE);
  5681. while (!kthread_should_stop()) {
  5682. schedule();
  5683. set_current_state(TASK_INTERRUPTIBLE);
  5684. }
  5685. __set_current_state(TASK_RUNNING);
  5686. return 0;
  5687. }
  5688. #ifdef CONFIG_HOTPLUG_CPU
  5689. static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
  5690. {
  5691. int ret;
  5692. local_irq_disable();
  5693. ret = __migrate_task(p, src_cpu, dest_cpu);
  5694. local_irq_enable();
  5695. return ret;
  5696. }
  5697. /*
  5698. * Figure out where task on dead CPU should go, use force if necessary.
  5699. */
  5700. static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
  5701. {
  5702. int dest_cpu;
  5703. const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(dead_cpu));
  5704. again:
  5705. /* Look for allowed, online CPU in same node. */
  5706. for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask)
  5707. if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
  5708. goto move;
  5709. /* Any allowed, online CPU? */
  5710. dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask);
  5711. if (dest_cpu < nr_cpu_ids)
  5712. goto move;
  5713. /* No more Mr. Nice Guy. */
  5714. if (dest_cpu >= nr_cpu_ids) {
  5715. cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
  5716. dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed);
  5717. /*
  5718. * Don't tell them about moving exiting tasks or
  5719. * kernel threads (both mm NULL), since they never
  5720. * leave kernel.
  5721. */
  5722. if (p->mm && printk_ratelimit()) {
  5723. printk(KERN_INFO "process %d (%s) no "
  5724. "longer affine to cpu%d\n",
  5725. task_pid_nr(p), p->comm, dead_cpu);
  5726. }
  5727. }
  5728. move:
  5729. /* It can have affinity changed while we were choosing. */
  5730. if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
  5731. goto again;
  5732. }
  5733. /*
  5734. * While a dead CPU has no uninterruptible tasks queued at this point,
  5735. * it might still have a nonzero ->nr_uninterruptible counter, because
  5736. * for performance reasons the counter is not stricly tracking tasks to
  5737. * their home CPUs. So we just add the counter to another CPU's counter,
  5738. * to keep the global sum constant after CPU-down:
  5739. */
  5740. static void migrate_nr_uninterruptible(struct rq *rq_src)
  5741. {
  5742. struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask));
  5743. unsigned long flags;
  5744. local_irq_save(flags);
  5745. double_rq_lock(rq_src, rq_dest);
  5746. rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
  5747. rq_src->nr_uninterruptible = 0;
  5748. double_rq_unlock(rq_src, rq_dest);
  5749. local_irq_restore(flags);
  5750. }
  5751. /* Run through task list and migrate tasks from the dead cpu. */
  5752. static void migrate_live_tasks(int src_cpu)
  5753. {
  5754. struct task_struct *p, *t;
  5755. read_lock(&tasklist_lock);
  5756. do_each_thread(t, p) {
  5757. if (p == current)
  5758. continue;
  5759. if (task_cpu(p) == src_cpu)
  5760. move_task_off_dead_cpu(src_cpu, p);
  5761. } while_each_thread(t, p);
  5762. read_unlock(&tasklist_lock);
  5763. }
  5764. /*
  5765. * Schedules idle task to be the next runnable task on current CPU.
  5766. * It does so by boosting its priority to highest possible.
  5767. * Used by CPU offline code.
  5768. */
  5769. void sched_idle_next(void)
  5770. {
  5771. int this_cpu = smp_processor_id();
  5772. struct rq *rq = cpu_rq(this_cpu);
  5773. struct task_struct *p = rq->idle;
  5774. unsigned long flags;
  5775. /* cpu has to be offline */
  5776. BUG_ON(cpu_online(this_cpu));
  5777. /*
  5778. * Strictly not necessary since rest of the CPUs are stopped by now
  5779. * and interrupts disabled on the current cpu.
  5780. */
  5781. spin_lock_irqsave(&rq->lock, flags);
  5782. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  5783. update_rq_clock(rq);
  5784. activate_task(rq, p, 0);
  5785. spin_unlock_irqrestore(&rq->lock, flags);
  5786. }
  5787. /*
  5788. * Ensures that the idle task is using init_mm right before its cpu goes
  5789. * offline.
  5790. */
  5791. void idle_task_exit(void)
  5792. {
  5793. struct mm_struct *mm = current->active_mm;
  5794. BUG_ON(cpu_online(smp_processor_id()));
  5795. if (mm != &init_mm)
  5796. switch_mm(mm, &init_mm, current);
  5797. mmdrop(mm);
  5798. }
  5799. /* called under rq->lock with disabled interrupts */
  5800. static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
  5801. {
  5802. struct rq *rq = cpu_rq(dead_cpu);
  5803. /* Must be exiting, otherwise would be on tasklist. */
  5804. BUG_ON(!p->exit_state);
  5805. /* Cannot have done final schedule yet: would have vanished. */
  5806. BUG_ON(p->state == TASK_DEAD);
  5807. get_task_struct(p);
  5808. /*
  5809. * Drop lock around migration; if someone else moves it,
  5810. * that's OK. No task can be added to this CPU, so iteration is
  5811. * fine.
  5812. */
  5813. spin_unlock_irq(&rq->lock);
  5814. move_task_off_dead_cpu(dead_cpu, p);
  5815. spin_lock_irq(&rq->lock);
  5816. put_task_struct(p);
  5817. }
  5818. /* release_task() removes task from tasklist, so we won't find dead tasks. */
  5819. static void migrate_dead_tasks(unsigned int dead_cpu)
  5820. {
  5821. struct rq *rq = cpu_rq(dead_cpu);
  5822. struct task_struct *next;
  5823. for ( ; ; ) {
  5824. if (!rq->nr_running)
  5825. break;
  5826. update_rq_clock(rq);
  5827. next = pick_next_task(rq);
  5828. if (!next)
  5829. break;
  5830. next->sched_class->put_prev_task(rq, next);
  5831. migrate_dead(dead_cpu, next);
  5832. }
  5833. }
  5834. #endif /* CONFIG_HOTPLUG_CPU */
  5835. #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
  5836. static struct ctl_table sd_ctl_dir[] = {
  5837. {
  5838. .procname = "sched_domain",
  5839. .mode = 0555,
  5840. },
  5841. {0, },
  5842. };
  5843. static struct ctl_table sd_ctl_root[] = {
  5844. {
  5845. .ctl_name = CTL_KERN,
  5846. .procname = "kernel",
  5847. .mode = 0555,
  5848. .child = sd_ctl_dir,
  5849. },
  5850. {0, },
  5851. };
  5852. static struct ctl_table *sd_alloc_ctl_entry(int n)
  5853. {
  5854. struct ctl_table *entry =
  5855. kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
  5856. return entry;
  5857. }
  5858. static void sd_free_ctl_entry(struct ctl_table **tablep)
  5859. {
  5860. struct ctl_table *entry;
  5861. /*
  5862. * In the intermediate directories, both the child directory and
  5863. * procname are dynamically allocated and could fail but the mode
  5864. * will always be set. In the lowest directory the names are
  5865. * static strings and all have proc handlers.
  5866. */
  5867. for (entry = *tablep; entry->mode; entry++) {
  5868. if (entry->child)
  5869. sd_free_ctl_entry(&entry->child);
  5870. if (entry->proc_handler == NULL)
  5871. kfree(entry->procname);
  5872. }
  5873. kfree(*tablep);
  5874. *tablep = NULL;
  5875. }
  5876. static void
  5877. set_table_entry(struct ctl_table *entry,
  5878. const char *procname, void *data, int maxlen,
  5879. mode_t mode, proc_handler *proc_handler)
  5880. {
  5881. entry->procname = procname;
  5882. entry->data = data;
  5883. entry->maxlen = maxlen;
  5884. entry->mode = mode;
  5885. entry->proc_handler = proc_handler;
  5886. }
  5887. static struct ctl_table *
  5888. sd_alloc_ctl_domain_table(struct sched_domain *sd)
  5889. {
  5890. struct ctl_table *table = sd_alloc_ctl_entry(13);
  5891. if (table == NULL)
  5892. return NULL;
  5893. set_table_entry(&table[0], "min_interval", &sd->min_interval,
  5894. sizeof(long), 0644, proc_doulongvec_minmax);
  5895. set_table_entry(&table[1], "max_interval", &sd->max_interval,
  5896. sizeof(long), 0644, proc_doulongvec_minmax);
  5897. set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
  5898. sizeof(int), 0644, proc_dointvec_minmax);
  5899. set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
  5900. sizeof(int), 0644, proc_dointvec_minmax);
  5901. set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
  5902. sizeof(int), 0644, proc_dointvec_minmax);
  5903. set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
  5904. sizeof(int), 0644, proc_dointvec_minmax);
  5905. set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
  5906. sizeof(int), 0644, proc_dointvec_minmax);
  5907. set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
  5908. sizeof(int), 0644, proc_dointvec_minmax);
  5909. set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
  5910. sizeof(int), 0644, proc_dointvec_minmax);
  5911. set_table_entry(&table[9], "cache_nice_tries",
  5912. &sd->cache_nice_tries,
  5913. sizeof(int), 0644, proc_dointvec_minmax);
  5914. set_table_entry(&table[10], "flags", &sd->flags,
  5915. sizeof(int), 0644, proc_dointvec_minmax);
  5916. set_table_entry(&table[11], "name", sd->name,
  5917. CORENAME_MAX_SIZE, 0444, proc_dostring);
  5918. /* &table[12] is terminator */
  5919. return table;
  5920. }
  5921. static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
  5922. {
  5923. struct ctl_table *entry, *table;
  5924. struct sched_domain *sd;
  5925. int domain_num = 0, i;
  5926. char buf[32];
  5927. for_each_domain(cpu, sd)
  5928. domain_num++;
  5929. entry = table = sd_alloc_ctl_entry(domain_num + 1);
  5930. if (table == NULL)
  5931. return NULL;
  5932. i = 0;
  5933. for_each_domain(cpu, sd) {
  5934. snprintf(buf, 32, "domain%d", i);
  5935. entry->procname = kstrdup(buf, GFP_KERNEL);
  5936. entry->mode = 0555;
  5937. entry->child = sd_alloc_ctl_domain_table(sd);
  5938. entry++;
  5939. i++;
  5940. }
  5941. return table;
  5942. }
  5943. static struct ctl_table_header *sd_sysctl_header;
  5944. static void register_sched_domain_sysctl(void)
  5945. {
  5946. int i, cpu_num = num_online_cpus();
  5947. struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
  5948. char buf[32];
  5949. WARN_ON(sd_ctl_dir[0].child);
  5950. sd_ctl_dir[0].child = entry;
  5951. if (entry == NULL)
  5952. return;
  5953. for_each_online_cpu(i) {
  5954. snprintf(buf, 32, "cpu%d", i);
  5955. entry->procname = kstrdup(buf, GFP_KERNEL);
  5956. entry->mode = 0555;
  5957. entry->child = sd_alloc_ctl_cpu_table(i);
  5958. entry++;
  5959. }
  5960. WARN_ON(sd_sysctl_header);
  5961. sd_sysctl_header = register_sysctl_table(sd_ctl_root);
  5962. }
  5963. /* may be called multiple times per register */
  5964. static void unregister_sched_domain_sysctl(void)
  5965. {
  5966. if (sd_sysctl_header)
  5967. unregister_sysctl_table(sd_sysctl_header);
  5968. sd_sysctl_header = NULL;
  5969. if (sd_ctl_dir[0].child)
  5970. sd_free_ctl_entry(&sd_ctl_dir[0].child);
  5971. }
  5972. #else
  5973. static void register_sched_domain_sysctl(void)
  5974. {
  5975. }
  5976. static void unregister_sched_domain_sysctl(void)
  5977. {
  5978. }
  5979. #endif
  5980. static void set_rq_online(struct rq *rq)
  5981. {
  5982. if (!rq->online) {
  5983. const struct sched_class *class;
  5984. cpumask_set_cpu(rq->cpu, rq->rd->online);
  5985. rq->online = 1;
  5986. for_each_class(class) {
  5987. if (class->rq_online)
  5988. class->rq_online(rq);
  5989. }
  5990. }
  5991. }
  5992. static void set_rq_offline(struct rq *rq)
  5993. {
  5994. if (rq->online) {
  5995. const struct sched_class *class;
  5996. for_each_class(class) {
  5997. if (class->rq_offline)
  5998. class->rq_offline(rq);
  5999. }
  6000. cpumask_clear_cpu(rq->cpu, rq->rd->online);
  6001. rq->online = 0;
  6002. }
  6003. }
  6004. /*
  6005. * migration_call - callback that gets triggered when a CPU is added.
  6006. * Here we can start up the necessary migration thread for the new CPU.
  6007. */
  6008. static int __cpuinit
  6009. migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  6010. {
  6011. struct task_struct *p;
  6012. int cpu = (long)hcpu;
  6013. unsigned long flags;
  6014. struct rq *rq;
  6015. switch (action) {
  6016. case CPU_UP_PREPARE:
  6017. case CPU_UP_PREPARE_FROZEN:
  6018. p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
  6019. if (IS_ERR(p))
  6020. return NOTIFY_BAD;
  6021. kthread_bind(p, cpu);
  6022. /* Must be high prio: stop_machine expects to yield to it. */
  6023. rq = task_rq_lock(p, &flags);
  6024. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  6025. task_rq_unlock(rq, &flags);
  6026. cpu_rq(cpu)->migration_thread = p;
  6027. break;
  6028. case CPU_ONLINE:
  6029. case CPU_ONLINE_FROZEN:
  6030. /* Strictly unnecessary, as first user will wake it. */
  6031. wake_up_process(cpu_rq(cpu)->migration_thread);
  6032. /* Update our root-domain */
  6033. rq = cpu_rq(cpu);
  6034. spin_lock_irqsave(&rq->lock, flags);
  6035. if (rq->rd) {
  6036. BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  6037. set_rq_online(rq);
  6038. }
  6039. spin_unlock_irqrestore(&rq->lock, flags);
  6040. break;
  6041. #ifdef CONFIG_HOTPLUG_CPU
  6042. case CPU_UP_CANCELED:
  6043. case CPU_UP_CANCELED_FROZEN:
  6044. if (!cpu_rq(cpu)->migration_thread)
  6045. break;
  6046. /* Unbind it from offline cpu so it can run. Fall thru. */
  6047. kthread_bind(cpu_rq(cpu)->migration_thread,
  6048. cpumask_any(cpu_online_mask));
  6049. kthread_stop(cpu_rq(cpu)->migration_thread);
  6050. cpu_rq(cpu)->migration_thread = NULL;
  6051. break;
  6052. case CPU_DEAD:
  6053. case CPU_DEAD_FROZEN:
  6054. cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
  6055. migrate_live_tasks(cpu);
  6056. rq = cpu_rq(cpu);
  6057. kthread_stop(rq->migration_thread);
  6058. rq->migration_thread = NULL;
  6059. /* Idle task back to normal (off runqueue, low prio) */
  6060. spin_lock_irq(&rq->lock);
  6061. update_rq_clock(rq);
  6062. deactivate_task(rq, rq->idle, 0);
  6063. rq->idle->static_prio = MAX_PRIO;
  6064. __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
  6065. rq->idle->sched_class = &idle_sched_class;
  6066. migrate_dead_tasks(cpu);
  6067. spin_unlock_irq(&rq->lock);
  6068. cpuset_unlock();
  6069. migrate_nr_uninterruptible(rq);
  6070. BUG_ON(rq->nr_running != 0);
  6071. /*
  6072. * No need to migrate the tasks: it was best-effort if
  6073. * they didn't take sched_hotcpu_mutex. Just wake up
  6074. * the requestors.
  6075. */
  6076. spin_lock_irq(&rq->lock);
  6077. while (!list_empty(&rq->migration_queue)) {
  6078. struct migration_req *req;
  6079. req = list_entry(rq->migration_queue.next,
  6080. struct migration_req, list);
  6081. list_del_init(&req->list);
  6082. spin_unlock_irq(&rq->lock);
  6083. complete(&req->done);
  6084. spin_lock_irq(&rq->lock);
  6085. }
  6086. spin_unlock_irq(&rq->lock);
  6087. break;
  6088. case CPU_DYING:
  6089. case CPU_DYING_FROZEN:
  6090. /* Update our root-domain */
  6091. rq = cpu_rq(cpu);
  6092. spin_lock_irqsave(&rq->lock, flags);
  6093. if (rq->rd) {
  6094. BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  6095. set_rq_offline(rq);
  6096. }
  6097. spin_unlock_irqrestore(&rq->lock, flags);
  6098. break;
  6099. #endif
  6100. }
  6101. return NOTIFY_OK;
  6102. }
  6103. /* Register at highest priority so that task migration (migrate_all_tasks)
  6104. * happens before everything else.
  6105. */
  6106. static struct notifier_block __cpuinitdata migration_notifier = {
  6107. .notifier_call = migration_call,
  6108. .priority = 10
  6109. };
  6110. static int __init migration_init(void)
  6111. {
  6112. void *cpu = (void *)(long)smp_processor_id();
  6113. int err;
  6114. /* Start one for the boot CPU: */
  6115. err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  6116. BUG_ON(err == NOTIFY_BAD);
  6117. migration_call(&migration_notifier, CPU_ONLINE, cpu);
  6118. register_cpu_notifier(&migration_notifier);
  6119. return err;
  6120. }
  6121. early_initcall(migration_init);
  6122. #endif
  6123. #ifdef CONFIG_SMP
  6124. #ifdef CONFIG_SCHED_DEBUG
  6125. static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
  6126. struct cpumask *groupmask)
  6127. {
  6128. struct sched_group *group = sd->groups;
  6129. char str[256];
  6130. cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
  6131. cpumask_clear(groupmask);
  6132. printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
  6133. if (!(sd->flags & SD_LOAD_BALANCE)) {
  6134. printk("does not load-balance\n");
  6135. if (sd->parent)
  6136. printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
  6137. " has parent");
  6138. return -1;
  6139. }
  6140. printk(KERN_CONT "span %s level %s\n", str, sd->name);
  6141. if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  6142. printk(KERN_ERR "ERROR: domain->span does not contain "
  6143. "CPU%d\n", cpu);
  6144. }
  6145. if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
  6146. printk(KERN_ERR "ERROR: domain->groups does not contain"
  6147. " CPU%d\n", cpu);
  6148. }
  6149. printk(KERN_DEBUG "%*s groups:", level + 1, "");
  6150. do {
  6151. if (!group) {
  6152. printk("\n");
  6153. printk(KERN_ERR "ERROR: group is NULL\n");
  6154. break;
  6155. }
  6156. if (!group->__cpu_power) {
  6157. printk(KERN_CONT "\n");
  6158. printk(KERN_ERR "ERROR: domain->cpu_power not "
  6159. "set\n");
  6160. break;
  6161. }
  6162. if (!cpumask_weight(sched_group_cpus(group))) {
  6163. printk(KERN_CONT "\n");
  6164. printk(KERN_ERR "ERROR: empty group\n");
  6165. break;
  6166. }
  6167. if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
  6168. printk(KERN_CONT "\n");
  6169. printk(KERN_ERR "ERROR: repeated CPUs\n");
  6170. break;
  6171. }
  6172. cpumask_or(groupmask, groupmask, sched_group_cpus(group));
  6173. cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
  6174. printk(KERN_CONT " %s", str);
  6175. group = group->next;
  6176. } while (group != sd->groups);
  6177. printk(KERN_CONT "\n");
  6178. if (!cpumask_equal(sched_domain_span(sd), groupmask))
  6179. printk(KERN_ERR "ERROR: groups don't span domain->span\n");
  6180. if (sd->parent &&
  6181. !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
  6182. printk(KERN_ERR "ERROR: parent span is not a superset "
  6183. "of domain->span\n");
  6184. return 0;
  6185. }
  6186. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  6187. {
  6188. cpumask_var_t groupmask;
  6189. int level = 0;
  6190. if (!sd) {
  6191. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  6192. return;
  6193. }
  6194. printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  6195. if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
  6196. printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
  6197. return;
  6198. }
  6199. for (;;) {
  6200. if (sched_domain_debug_one(sd, cpu, level, groupmask))
  6201. break;
  6202. level++;
  6203. sd = sd->parent;
  6204. if (!sd)
  6205. break;
  6206. }
  6207. free_cpumask_var(groupmask);
  6208. }
  6209. #else /* !CONFIG_SCHED_DEBUG */
  6210. # define sched_domain_debug(sd, cpu) do { } while (0)
  6211. #endif /* CONFIG_SCHED_DEBUG */
  6212. static int sd_degenerate(struct sched_domain *sd)
  6213. {
  6214. if (cpumask_weight(sched_domain_span(sd)) == 1)
  6215. return 1;
  6216. /* Following flags need at least 2 groups */
  6217. if (sd->flags & (SD_LOAD_BALANCE |
  6218. SD_BALANCE_NEWIDLE |
  6219. SD_BALANCE_FORK |
  6220. SD_BALANCE_EXEC |
  6221. SD_SHARE_CPUPOWER |
  6222. SD_SHARE_PKG_RESOURCES)) {
  6223. if (sd->groups != sd->groups->next)
  6224. return 0;
  6225. }
  6226. /* Following flags don't use groups */
  6227. if (sd->flags & (SD_WAKE_IDLE |
  6228. SD_WAKE_AFFINE |
  6229. SD_WAKE_BALANCE))
  6230. return 0;
  6231. return 1;
  6232. }
  6233. static int
  6234. sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  6235. {
  6236. unsigned long cflags = sd->flags, pflags = parent->flags;
  6237. if (sd_degenerate(parent))
  6238. return 1;
  6239. if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
  6240. return 0;
  6241. /* Does parent contain flags not in child? */
  6242. /* WAKE_BALANCE is a subset of WAKE_AFFINE */
  6243. if (cflags & SD_WAKE_AFFINE)
  6244. pflags &= ~SD_WAKE_BALANCE;
  6245. /* Flags needing groups don't count if only 1 group in parent */
  6246. if (parent->groups == parent->groups->next) {
  6247. pflags &= ~(SD_LOAD_BALANCE |
  6248. SD_BALANCE_NEWIDLE |
  6249. SD_BALANCE_FORK |
  6250. SD_BALANCE_EXEC |
  6251. SD_SHARE_CPUPOWER |
  6252. SD_SHARE_PKG_RESOURCES);
  6253. if (nr_node_ids == 1)
  6254. pflags &= ~SD_SERIALIZE;
  6255. }
  6256. if (~cflags & pflags)
  6257. return 0;
  6258. return 1;
  6259. }
  6260. static void free_rootdomain(struct root_domain *rd)
  6261. {
  6262. cpupri_cleanup(&rd->cpupri);
  6263. free_cpumask_var(rd->rto_mask);
  6264. free_cpumask_var(rd->online);
  6265. free_cpumask_var(rd->span);
  6266. kfree(rd);
  6267. }
  6268. static void rq_attach_root(struct rq *rq, struct root_domain *rd)
  6269. {
  6270. struct root_domain *old_rd = NULL;
  6271. unsigned long flags;
  6272. spin_lock_irqsave(&rq->lock, flags);
  6273. if (rq->rd) {
  6274. old_rd = rq->rd;
  6275. if (cpumask_test_cpu(rq->cpu, old_rd->online))
  6276. set_rq_offline(rq);
  6277. cpumask_clear_cpu(rq->cpu, old_rd->span);
  6278. /*
  6279. * If we dont want to free the old_rt yet then
  6280. * set old_rd to NULL to skip the freeing later
  6281. * in this function:
  6282. */
  6283. if (!atomic_dec_and_test(&old_rd->refcount))
  6284. old_rd = NULL;
  6285. }
  6286. atomic_inc(&rd->refcount);
  6287. rq->rd = rd;
  6288. cpumask_set_cpu(rq->cpu, rd->span);
  6289. if (cpumask_test_cpu(rq->cpu, cpu_online_mask))
  6290. set_rq_online(rq);
  6291. spin_unlock_irqrestore(&rq->lock, flags);
  6292. if (old_rd)
  6293. free_rootdomain(old_rd);
  6294. }
  6295. static int __init_refok init_rootdomain(struct root_domain *rd, bool bootmem)
  6296. {
  6297. memset(rd, 0, sizeof(*rd));
  6298. if (bootmem) {
  6299. alloc_bootmem_cpumask_var(&def_root_domain.span);
  6300. alloc_bootmem_cpumask_var(&def_root_domain.online);
  6301. alloc_bootmem_cpumask_var(&def_root_domain.rto_mask);
  6302. cpupri_init(&rd->cpupri, true);
  6303. return 0;
  6304. }
  6305. if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
  6306. goto out;
  6307. if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
  6308. goto free_span;
  6309. if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
  6310. goto free_online;
  6311. if (cpupri_init(&rd->cpupri, false) != 0)
  6312. goto free_rto_mask;
  6313. return 0;
  6314. free_rto_mask:
  6315. free_cpumask_var(rd->rto_mask);
  6316. free_online:
  6317. free_cpumask_var(rd->online);
  6318. free_span:
  6319. free_cpumask_var(rd->span);
  6320. out:
  6321. return -ENOMEM;
  6322. }
  6323. static void init_defrootdomain(void)
  6324. {
  6325. init_rootdomain(&def_root_domain, true);
  6326. atomic_set(&def_root_domain.refcount, 1);
  6327. }
  6328. static struct root_domain *alloc_rootdomain(void)
  6329. {
  6330. struct root_domain *rd;
  6331. rd = kmalloc(sizeof(*rd), GFP_KERNEL);
  6332. if (!rd)
  6333. return NULL;
  6334. if (init_rootdomain(rd, false) != 0) {
  6335. kfree(rd);
  6336. return NULL;
  6337. }
  6338. return rd;
  6339. }
  6340. /*
  6341. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  6342. * hold the hotplug lock.
  6343. */
  6344. static void
  6345. cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
  6346. {
  6347. struct rq *rq = cpu_rq(cpu);
  6348. struct sched_domain *tmp;
  6349. /* Remove the sched domains which do not contribute to scheduling. */
  6350. for (tmp = sd; tmp; ) {
  6351. struct sched_domain *parent = tmp->parent;
  6352. if (!parent)
  6353. break;
  6354. if (sd_parent_degenerate(tmp, parent)) {
  6355. tmp->parent = parent->parent;
  6356. if (parent->parent)
  6357. parent->parent->child = tmp;
  6358. } else
  6359. tmp = tmp->parent;
  6360. }
  6361. if (sd && sd_degenerate(sd)) {
  6362. sd = sd->parent;
  6363. if (sd)
  6364. sd->child = NULL;
  6365. }
  6366. sched_domain_debug(sd, cpu);
  6367. rq_attach_root(rq, rd);
  6368. rcu_assign_pointer(rq->sd, sd);
  6369. }
  6370. /* cpus with isolated domains */
  6371. static cpumask_var_t cpu_isolated_map;
  6372. /* Setup the mask of cpus configured for isolated domains */
  6373. static int __init isolated_cpu_setup(char *str)
  6374. {
  6375. cpulist_parse(str, cpu_isolated_map);
  6376. return 1;
  6377. }
  6378. __setup("isolcpus=", isolated_cpu_setup);
  6379. /*
  6380. * init_sched_build_groups takes the cpumask we wish to span, and a pointer
  6381. * to a function which identifies what group(along with sched group) a CPU
  6382. * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
  6383. * (due to the fact that we keep track of groups covered with a struct cpumask).
  6384. *
  6385. * init_sched_build_groups will build a circular linked list of the groups
  6386. * covered by the given span, and will set each group's ->cpumask correctly,
  6387. * and ->cpu_power to 0.
  6388. */
  6389. static void
  6390. init_sched_build_groups(const struct cpumask *span,
  6391. const struct cpumask *cpu_map,
  6392. int (*group_fn)(int cpu, const struct cpumask *cpu_map,
  6393. struct sched_group **sg,
  6394. struct cpumask *tmpmask),
  6395. struct cpumask *covered, struct cpumask *tmpmask)
  6396. {
  6397. struct sched_group *first = NULL, *last = NULL;
  6398. int i;
  6399. cpumask_clear(covered);
  6400. for_each_cpu(i, span) {
  6401. struct sched_group *sg;
  6402. int group = group_fn(i, cpu_map, &sg, tmpmask);
  6403. int j;
  6404. if (cpumask_test_cpu(i, covered))
  6405. continue;
  6406. cpumask_clear(sched_group_cpus(sg));
  6407. sg->__cpu_power = 0;
  6408. for_each_cpu(j, span) {
  6409. if (group_fn(j, cpu_map, NULL, tmpmask) != group)
  6410. continue;
  6411. cpumask_set_cpu(j, covered);
  6412. cpumask_set_cpu(j, sched_group_cpus(sg));
  6413. }
  6414. if (!first)
  6415. first = sg;
  6416. if (last)
  6417. last->next = sg;
  6418. last = sg;
  6419. }
  6420. last->next = first;
  6421. }
  6422. #define SD_NODES_PER_DOMAIN 16
  6423. #ifdef CONFIG_NUMA
  6424. /**
  6425. * find_next_best_node - find the next node to include in a sched_domain
  6426. * @node: node whose sched_domain we're building
  6427. * @used_nodes: nodes already in the sched_domain
  6428. *
  6429. * Find the next node to include in a given scheduling domain. Simply
  6430. * finds the closest node not already in the @used_nodes map.
  6431. *
  6432. * Should use nodemask_t.
  6433. */
  6434. static int find_next_best_node(int node, nodemask_t *used_nodes)
  6435. {
  6436. int i, n, val, min_val, best_node = 0;
  6437. min_val = INT_MAX;
  6438. for (i = 0; i < nr_node_ids; i++) {
  6439. /* Start at @node */
  6440. n = (node + i) % nr_node_ids;
  6441. if (!nr_cpus_node(n))
  6442. continue;
  6443. /* Skip already used nodes */
  6444. if (node_isset(n, *used_nodes))
  6445. continue;
  6446. /* Simple min distance search */
  6447. val = node_distance(node, n);
  6448. if (val < min_val) {
  6449. min_val = val;
  6450. best_node = n;
  6451. }
  6452. }
  6453. node_set(best_node, *used_nodes);
  6454. return best_node;
  6455. }
  6456. /**
  6457. * sched_domain_node_span - get a cpumask for a node's sched_domain
  6458. * @node: node whose cpumask we're constructing
  6459. * @span: resulting cpumask
  6460. *
  6461. * Given a node, construct a good cpumask for its sched_domain to span. It
  6462. * should be one that prevents unnecessary balancing, but also spreads tasks
  6463. * out optimally.
  6464. */
  6465. static void sched_domain_node_span(int node, struct cpumask *span)
  6466. {
  6467. nodemask_t used_nodes;
  6468. int i;
  6469. cpumask_clear(span);
  6470. nodes_clear(used_nodes);
  6471. cpumask_or(span, span, cpumask_of_node(node));
  6472. node_set(node, used_nodes);
  6473. for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
  6474. int next_node = find_next_best_node(node, &used_nodes);
  6475. cpumask_or(span, span, cpumask_of_node(next_node));
  6476. }
  6477. }
  6478. #endif /* CONFIG_NUMA */
  6479. int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
  6480. /*
  6481. * The cpus mask in sched_group and sched_domain hangs off the end.
  6482. * FIXME: use cpumask_var_t or dynamic percpu alloc to avoid wasting space
  6483. * for nr_cpu_ids < CONFIG_NR_CPUS.
  6484. */
  6485. struct static_sched_group {
  6486. struct sched_group sg;
  6487. DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
  6488. };
  6489. struct static_sched_domain {
  6490. struct sched_domain sd;
  6491. DECLARE_BITMAP(span, CONFIG_NR_CPUS);
  6492. };
  6493. /*
  6494. * SMT sched-domains:
  6495. */
  6496. #ifdef CONFIG_SCHED_SMT
  6497. static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
  6498. static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus);
  6499. static int
  6500. cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
  6501. struct sched_group **sg, struct cpumask *unused)
  6502. {
  6503. if (sg)
  6504. *sg = &per_cpu(sched_group_cpus, cpu).sg;
  6505. return cpu;
  6506. }
  6507. #endif /* CONFIG_SCHED_SMT */
  6508. /*
  6509. * multi-core sched-domains:
  6510. */
  6511. #ifdef CONFIG_SCHED_MC
  6512. static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
  6513. static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
  6514. #endif /* CONFIG_SCHED_MC */
  6515. #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
  6516. static int
  6517. cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
  6518. struct sched_group **sg, struct cpumask *mask)
  6519. {
  6520. int group;
  6521. cpumask_and(mask, &per_cpu(cpu_sibling_map, cpu), cpu_map);
  6522. group = cpumask_first(mask);
  6523. if (sg)
  6524. *sg = &per_cpu(sched_group_core, group).sg;
  6525. return group;
  6526. }
  6527. #elif defined(CONFIG_SCHED_MC)
  6528. static int
  6529. cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
  6530. struct sched_group **sg, struct cpumask *unused)
  6531. {
  6532. if (sg)
  6533. *sg = &per_cpu(sched_group_core, cpu).sg;
  6534. return cpu;
  6535. }
  6536. #endif
  6537. static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
  6538. static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
  6539. static int
  6540. cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
  6541. struct sched_group **sg, struct cpumask *mask)
  6542. {
  6543. int group;
  6544. #ifdef CONFIG_SCHED_MC
  6545. cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
  6546. group = cpumask_first(mask);
  6547. #elif defined(CONFIG_SCHED_SMT)
  6548. cpumask_and(mask, &per_cpu(cpu_sibling_map, cpu), cpu_map);
  6549. group = cpumask_first(mask);
  6550. #else
  6551. group = cpu;
  6552. #endif
  6553. if (sg)
  6554. *sg = &per_cpu(sched_group_phys, group).sg;
  6555. return group;
  6556. }
  6557. #ifdef CONFIG_NUMA
  6558. /*
  6559. * The init_sched_build_groups can't handle what we want to do with node
  6560. * groups, so roll our own. Now each node has its own list of groups which
  6561. * gets dynamically allocated.
  6562. */
  6563. static DEFINE_PER_CPU(struct static_sched_domain, node_domains);
  6564. static struct sched_group ***sched_group_nodes_bycpu;
  6565. static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains);
  6566. static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
  6567. static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
  6568. struct sched_group **sg,
  6569. struct cpumask *nodemask)
  6570. {
  6571. int group;
  6572. cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
  6573. group = cpumask_first(nodemask);
  6574. if (sg)
  6575. *sg = &per_cpu(sched_group_allnodes, group).sg;
  6576. return group;
  6577. }
  6578. static void init_numa_sched_groups_power(struct sched_group *group_head)
  6579. {
  6580. struct sched_group *sg = group_head;
  6581. int j;
  6582. if (!sg)
  6583. return;
  6584. do {
  6585. for_each_cpu(j, sched_group_cpus(sg)) {
  6586. struct sched_domain *sd;
  6587. sd = &per_cpu(phys_domains, j).sd;
  6588. if (j != cpumask_first(sched_group_cpus(sd->groups))) {
  6589. /*
  6590. * Only add "power" once for each
  6591. * physical package.
  6592. */
  6593. continue;
  6594. }
  6595. sg_inc_cpu_power(sg, sd->groups->__cpu_power);
  6596. }
  6597. sg = sg->next;
  6598. } while (sg != group_head);
  6599. }
  6600. #endif /* CONFIG_NUMA */
  6601. #ifdef CONFIG_NUMA
  6602. /* Free memory allocated for various sched_group structures */
  6603. static void free_sched_groups(const struct cpumask *cpu_map,
  6604. struct cpumask *nodemask)
  6605. {
  6606. int cpu, i;
  6607. for_each_cpu(cpu, cpu_map) {
  6608. struct sched_group **sched_group_nodes
  6609. = sched_group_nodes_bycpu[cpu];
  6610. if (!sched_group_nodes)
  6611. continue;
  6612. for (i = 0; i < nr_node_ids; i++) {
  6613. struct sched_group *oldsg, *sg = sched_group_nodes[i];
  6614. cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
  6615. if (cpumask_empty(nodemask))
  6616. continue;
  6617. if (sg == NULL)
  6618. continue;
  6619. sg = sg->next;
  6620. next_sg:
  6621. oldsg = sg;
  6622. sg = sg->next;
  6623. kfree(oldsg);
  6624. if (oldsg != sched_group_nodes[i])
  6625. goto next_sg;
  6626. }
  6627. kfree(sched_group_nodes);
  6628. sched_group_nodes_bycpu[cpu] = NULL;
  6629. }
  6630. }
  6631. #else /* !CONFIG_NUMA */
  6632. static void free_sched_groups(const struct cpumask *cpu_map,
  6633. struct cpumask *nodemask)
  6634. {
  6635. }
  6636. #endif /* CONFIG_NUMA */
  6637. /*
  6638. * Initialize sched groups cpu_power.
  6639. *
  6640. * cpu_power indicates the capacity of sched group, which is used while
  6641. * distributing the load between different sched groups in a sched domain.
  6642. * Typically cpu_power for all the groups in a sched domain will be same unless
  6643. * there are asymmetries in the topology. If there are asymmetries, group
  6644. * having more cpu_power will pickup more load compared to the group having
  6645. * less cpu_power.
  6646. *
  6647. * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
  6648. * the maximum number of tasks a group can handle in the presence of other idle
  6649. * or lightly loaded groups in the same sched domain.
  6650. */
  6651. static void init_sched_groups_power(int cpu, struct sched_domain *sd)
  6652. {
  6653. struct sched_domain *child;
  6654. struct sched_group *group;
  6655. WARN_ON(!sd || !sd->groups);
  6656. if (cpu != cpumask_first(sched_group_cpus(sd->groups)))
  6657. return;
  6658. child = sd->child;
  6659. sd->groups->__cpu_power = 0;
  6660. /*
  6661. * For perf policy, if the groups in child domain share resources
  6662. * (for example cores sharing some portions of the cache hierarchy
  6663. * or SMT), then set this domain groups cpu_power such that each group
  6664. * can handle only one task, when there are other idle groups in the
  6665. * same sched domain.
  6666. */
  6667. if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
  6668. (child->flags &
  6669. (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
  6670. sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
  6671. return;
  6672. }
  6673. /*
  6674. * add cpu_power of each child group to this groups cpu_power
  6675. */
  6676. group = child->groups;
  6677. do {
  6678. sg_inc_cpu_power(sd->groups, group->__cpu_power);
  6679. group = group->next;
  6680. } while (group != child->groups);
  6681. }
  6682. /*
  6683. * Initializers for schedule domains
  6684. * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
  6685. */
  6686. #ifdef CONFIG_SCHED_DEBUG
  6687. # define SD_INIT_NAME(sd, type) sd->name = #type
  6688. #else
  6689. # define SD_INIT_NAME(sd, type) do { } while (0)
  6690. #endif
  6691. #define SD_INIT(sd, type) sd_init_##type(sd)
  6692. #define SD_INIT_FUNC(type) \
  6693. static noinline void sd_init_##type(struct sched_domain *sd) \
  6694. { \
  6695. memset(sd, 0, sizeof(*sd)); \
  6696. *sd = SD_##type##_INIT; \
  6697. sd->level = SD_LV_##type; \
  6698. SD_INIT_NAME(sd, type); \
  6699. }
  6700. SD_INIT_FUNC(CPU)
  6701. #ifdef CONFIG_NUMA
  6702. SD_INIT_FUNC(ALLNODES)
  6703. SD_INIT_FUNC(NODE)
  6704. #endif
  6705. #ifdef CONFIG_SCHED_SMT
  6706. SD_INIT_FUNC(SIBLING)
  6707. #endif
  6708. #ifdef CONFIG_SCHED_MC
  6709. SD_INIT_FUNC(MC)
  6710. #endif
  6711. static int default_relax_domain_level = -1;
  6712. static int __init setup_relax_domain_level(char *str)
  6713. {
  6714. unsigned long val;
  6715. val = simple_strtoul(str, NULL, 0);
  6716. if (val < SD_LV_MAX)
  6717. default_relax_domain_level = val;
  6718. return 1;
  6719. }
  6720. __setup("relax_domain_level=", setup_relax_domain_level);
  6721. static void set_domain_attribute(struct sched_domain *sd,
  6722. struct sched_domain_attr *attr)
  6723. {
  6724. int request;
  6725. if (!attr || attr->relax_domain_level < 0) {
  6726. if (default_relax_domain_level < 0)
  6727. return;
  6728. else
  6729. request = default_relax_domain_level;
  6730. } else
  6731. request = attr->relax_domain_level;
  6732. if (request < sd->level) {
  6733. /* turn off idle balance on this domain */
  6734. sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
  6735. } else {
  6736. /* turn on idle balance on this domain */
  6737. sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
  6738. }
  6739. }
  6740. /*
  6741. * Build sched domains for a given set of cpus and attach the sched domains
  6742. * to the individual cpus
  6743. */
  6744. static int __build_sched_domains(const struct cpumask *cpu_map,
  6745. struct sched_domain_attr *attr)
  6746. {
  6747. int i, err = -ENOMEM;
  6748. struct root_domain *rd;
  6749. cpumask_var_t nodemask, this_sibling_map, this_core_map, send_covered,
  6750. tmpmask;
  6751. #ifdef CONFIG_NUMA
  6752. cpumask_var_t domainspan, covered, notcovered;
  6753. struct sched_group **sched_group_nodes = NULL;
  6754. int sd_allnodes = 0;
  6755. if (!alloc_cpumask_var(&domainspan, GFP_KERNEL))
  6756. goto out;
  6757. if (!alloc_cpumask_var(&covered, GFP_KERNEL))
  6758. goto free_domainspan;
  6759. if (!alloc_cpumask_var(&notcovered, GFP_KERNEL))
  6760. goto free_covered;
  6761. #endif
  6762. if (!alloc_cpumask_var(&nodemask, GFP_KERNEL))
  6763. goto free_notcovered;
  6764. if (!alloc_cpumask_var(&this_sibling_map, GFP_KERNEL))
  6765. goto free_nodemask;
  6766. if (!alloc_cpumask_var(&this_core_map, GFP_KERNEL))
  6767. goto free_this_sibling_map;
  6768. if (!alloc_cpumask_var(&send_covered, GFP_KERNEL))
  6769. goto free_this_core_map;
  6770. if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
  6771. goto free_send_covered;
  6772. #ifdef CONFIG_NUMA
  6773. /*
  6774. * Allocate the per-node list of sched groups
  6775. */
  6776. sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
  6777. GFP_KERNEL);
  6778. if (!sched_group_nodes) {
  6779. printk(KERN_WARNING "Can not alloc sched group node list\n");
  6780. goto free_tmpmask;
  6781. }
  6782. #endif
  6783. rd = alloc_rootdomain();
  6784. if (!rd) {
  6785. printk(KERN_WARNING "Cannot alloc root domain\n");
  6786. goto free_sched_groups;
  6787. }
  6788. #ifdef CONFIG_NUMA
  6789. sched_group_nodes_bycpu[cpumask_first(cpu_map)] = sched_group_nodes;
  6790. #endif
  6791. /*
  6792. * Set up domains for cpus specified by the cpu_map.
  6793. */
  6794. for_each_cpu(i, cpu_map) {
  6795. struct sched_domain *sd = NULL, *p;
  6796. cpumask_and(nodemask, cpumask_of_node(cpu_to_node(i)), cpu_map);
  6797. #ifdef CONFIG_NUMA
  6798. if (cpumask_weight(cpu_map) >
  6799. SD_NODES_PER_DOMAIN*cpumask_weight(nodemask)) {
  6800. sd = &per_cpu(allnodes_domains, i).sd;
  6801. SD_INIT(sd, ALLNODES);
  6802. set_domain_attribute(sd, attr);
  6803. cpumask_copy(sched_domain_span(sd), cpu_map);
  6804. cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
  6805. p = sd;
  6806. sd_allnodes = 1;
  6807. } else
  6808. p = NULL;
  6809. sd = &per_cpu(node_domains, i).sd;
  6810. SD_INIT(sd, NODE);
  6811. set_domain_attribute(sd, attr);
  6812. sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
  6813. sd->parent = p;
  6814. if (p)
  6815. p->child = sd;
  6816. cpumask_and(sched_domain_span(sd),
  6817. sched_domain_span(sd), cpu_map);
  6818. #endif
  6819. p = sd;
  6820. sd = &per_cpu(phys_domains, i).sd;
  6821. SD_INIT(sd, CPU);
  6822. set_domain_attribute(sd, attr);
  6823. cpumask_copy(sched_domain_span(sd), nodemask);
  6824. sd->parent = p;
  6825. if (p)
  6826. p->child = sd;
  6827. cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
  6828. #ifdef CONFIG_SCHED_MC
  6829. p = sd;
  6830. sd = &per_cpu(core_domains, i).sd;
  6831. SD_INIT(sd, MC);
  6832. set_domain_attribute(sd, attr);
  6833. cpumask_and(sched_domain_span(sd), cpu_map,
  6834. cpu_coregroup_mask(i));
  6835. sd->parent = p;
  6836. p->child = sd;
  6837. cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
  6838. #endif
  6839. #ifdef CONFIG_SCHED_SMT
  6840. p = sd;
  6841. sd = &per_cpu(cpu_domains, i).sd;
  6842. SD_INIT(sd, SIBLING);
  6843. set_domain_attribute(sd, attr);
  6844. cpumask_and(sched_domain_span(sd),
  6845. &per_cpu(cpu_sibling_map, i), cpu_map);
  6846. sd->parent = p;
  6847. p->child = sd;
  6848. cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
  6849. #endif
  6850. }
  6851. #ifdef CONFIG_SCHED_SMT
  6852. /* Set up CPU (sibling) groups */
  6853. for_each_cpu(i, cpu_map) {
  6854. cpumask_and(this_sibling_map,
  6855. &per_cpu(cpu_sibling_map, i), cpu_map);
  6856. if (i != cpumask_first(this_sibling_map))
  6857. continue;
  6858. init_sched_build_groups(this_sibling_map, cpu_map,
  6859. &cpu_to_cpu_group,
  6860. send_covered, tmpmask);
  6861. }
  6862. #endif
  6863. #ifdef CONFIG_SCHED_MC
  6864. /* Set up multi-core groups */
  6865. for_each_cpu(i, cpu_map) {
  6866. cpumask_and(this_core_map, cpu_coregroup_mask(i), cpu_map);
  6867. if (i != cpumask_first(this_core_map))
  6868. continue;
  6869. init_sched_build_groups(this_core_map, cpu_map,
  6870. &cpu_to_core_group,
  6871. send_covered, tmpmask);
  6872. }
  6873. #endif
  6874. /* Set up physical groups */
  6875. for (i = 0; i < nr_node_ids; i++) {
  6876. cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
  6877. if (cpumask_empty(nodemask))
  6878. continue;
  6879. init_sched_build_groups(nodemask, cpu_map,
  6880. &cpu_to_phys_group,
  6881. send_covered, tmpmask);
  6882. }
  6883. #ifdef CONFIG_NUMA
  6884. /* Set up node groups */
  6885. if (sd_allnodes) {
  6886. init_sched_build_groups(cpu_map, cpu_map,
  6887. &cpu_to_allnodes_group,
  6888. send_covered, tmpmask);
  6889. }
  6890. for (i = 0; i < nr_node_ids; i++) {
  6891. /* Set up node groups */
  6892. struct sched_group *sg, *prev;
  6893. int j;
  6894. cpumask_clear(covered);
  6895. cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
  6896. if (cpumask_empty(nodemask)) {
  6897. sched_group_nodes[i] = NULL;
  6898. continue;
  6899. }
  6900. sched_domain_node_span(i, domainspan);
  6901. cpumask_and(domainspan, domainspan, cpu_map);
  6902. sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
  6903. GFP_KERNEL, i);
  6904. if (!sg) {
  6905. printk(KERN_WARNING "Can not alloc domain group for "
  6906. "node %d\n", i);
  6907. goto error;
  6908. }
  6909. sched_group_nodes[i] = sg;
  6910. for_each_cpu(j, nodemask) {
  6911. struct sched_domain *sd;
  6912. sd = &per_cpu(node_domains, j).sd;
  6913. sd->groups = sg;
  6914. }
  6915. sg->__cpu_power = 0;
  6916. cpumask_copy(sched_group_cpus(sg), nodemask);
  6917. sg->next = sg;
  6918. cpumask_or(covered, covered, nodemask);
  6919. prev = sg;
  6920. for (j = 0; j < nr_node_ids; j++) {
  6921. int n = (i + j) % nr_node_ids;
  6922. cpumask_complement(notcovered, covered);
  6923. cpumask_and(tmpmask, notcovered, cpu_map);
  6924. cpumask_and(tmpmask, tmpmask, domainspan);
  6925. if (cpumask_empty(tmpmask))
  6926. break;
  6927. cpumask_and(tmpmask, tmpmask, cpumask_of_node(n));
  6928. if (cpumask_empty(tmpmask))
  6929. continue;
  6930. sg = kmalloc_node(sizeof(struct sched_group) +
  6931. cpumask_size(),
  6932. GFP_KERNEL, i);
  6933. if (!sg) {
  6934. printk(KERN_WARNING
  6935. "Can not alloc domain group for node %d\n", j);
  6936. goto error;
  6937. }
  6938. sg->__cpu_power = 0;
  6939. cpumask_copy(sched_group_cpus(sg), tmpmask);
  6940. sg->next = prev->next;
  6941. cpumask_or(covered, covered, tmpmask);
  6942. prev->next = sg;
  6943. prev = sg;
  6944. }
  6945. }
  6946. #endif
  6947. /* Calculate CPU power for physical packages and nodes */
  6948. #ifdef CONFIG_SCHED_SMT
  6949. for_each_cpu(i, cpu_map) {
  6950. struct sched_domain *sd = &per_cpu(cpu_domains, i).sd;
  6951. init_sched_groups_power(i, sd);
  6952. }
  6953. #endif
  6954. #ifdef CONFIG_SCHED_MC
  6955. for_each_cpu(i, cpu_map) {
  6956. struct sched_domain *sd = &per_cpu(core_domains, i).sd;
  6957. init_sched_groups_power(i, sd);
  6958. }
  6959. #endif
  6960. for_each_cpu(i, cpu_map) {
  6961. struct sched_domain *sd = &per_cpu(phys_domains, i).sd;
  6962. init_sched_groups_power(i, sd);
  6963. }
  6964. #ifdef CONFIG_NUMA
  6965. for (i = 0; i < nr_node_ids; i++)
  6966. init_numa_sched_groups_power(sched_group_nodes[i]);
  6967. if (sd_allnodes) {
  6968. struct sched_group *sg;
  6969. cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
  6970. tmpmask);
  6971. init_numa_sched_groups_power(sg);
  6972. }
  6973. #endif
  6974. /* Attach the domains */
  6975. for_each_cpu(i, cpu_map) {
  6976. struct sched_domain *sd;
  6977. #ifdef CONFIG_SCHED_SMT
  6978. sd = &per_cpu(cpu_domains, i).sd;
  6979. #elif defined(CONFIG_SCHED_MC)
  6980. sd = &per_cpu(core_domains, i).sd;
  6981. #else
  6982. sd = &per_cpu(phys_domains, i).sd;
  6983. #endif
  6984. cpu_attach_domain(sd, rd, i);
  6985. }
  6986. err = 0;
  6987. free_tmpmask:
  6988. free_cpumask_var(tmpmask);
  6989. free_send_covered:
  6990. free_cpumask_var(send_covered);
  6991. free_this_core_map:
  6992. free_cpumask_var(this_core_map);
  6993. free_this_sibling_map:
  6994. free_cpumask_var(this_sibling_map);
  6995. free_nodemask:
  6996. free_cpumask_var(nodemask);
  6997. free_notcovered:
  6998. #ifdef CONFIG_NUMA
  6999. free_cpumask_var(notcovered);
  7000. free_covered:
  7001. free_cpumask_var(covered);
  7002. free_domainspan:
  7003. free_cpumask_var(domainspan);
  7004. out:
  7005. #endif
  7006. return err;
  7007. free_sched_groups:
  7008. #ifdef CONFIG_NUMA
  7009. kfree(sched_group_nodes);
  7010. #endif
  7011. goto free_tmpmask;
  7012. #ifdef CONFIG_NUMA
  7013. error:
  7014. free_sched_groups(cpu_map, tmpmask);
  7015. free_rootdomain(rd);
  7016. goto free_tmpmask;
  7017. #endif
  7018. }
  7019. static int build_sched_domains(const struct cpumask *cpu_map)
  7020. {
  7021. return __build_sched_domains(cpu_map, NULL);
  7022. }
  7023. static struct cpumask *doms_cur; /* current sched domains */
  7024. static int ndoms_cur; /* number of sched domains in 'doms_cur' */
  7025. static struct sched_domain_attr *dattr_cur;
  7026. /* attribues of custom domains in 'doms_cur' */
  7027. /*
  7028. * Special case: If a kmalloc of a doms_cur partition (array of
  7029. * cpumask) fails, then fallback to a single sched domain,
  7030. * as determined by the single cpumask fallback_doms.
  7031. */
  7032. static cpumask_var_t fallback_doms;
  7033. /*
  7034. * arch_update_cpu_topology lets virtualized architectures update the
  7035. * cpu core maps. It is supposed to return 1 if the topology changed
  7036. * or 0 if it stayed the same.
  7037. */
  7038. int __attribute__((weak)) arch_update_cpu_topology(void)
  7039. {
  7040. return 0;
  7041. }
  7042. /*
  7043. * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  7044. * For now this just excludes isolated cpus, but could be used to
  7045. * exclude other special cases in the future.
  7046. */
  7047. static int arch_init_sched_domains(const struct cpumask *cpu_map)
  7048. {
  7049. int err;
  7050. arch_update_cpu_topology();
  7051. ndoms_cur = 1;
  7052. doms_cur = kmalloc(cpumask_size(), GFP_KERNEL);
  7053. if (!doms_cur)
  7054. doms_cur = fallback_doms;
  7055. cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map);
  7056. dattr_cur = NULL;
  7057. err = build_sched_domains(doms_cur);
  7058. register_sched_domain_sysctl();
  7059. return err;
  7060. }
  7061. static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
  7062. struct cpumask *tmpmask)
  7063. {
  7064. free_sched_groups(cpu_map, tmpmask);
  7065. }
  7066. /*
  7067. * Detach sched domains from a group of cpus specified in cpu_map
  7068. * These cpus will now be attached to the NULL domain
  7069. */
  7070. static void detach_destroy_domains(const struct cpumask *cpu_map)
  7071. {
  7072. /* Save because hotplug lock held. */
  7073. static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
  7074. int i;
  7075. for_each_cpu(i, cpu_map)
  7076. cpu_attach_domain(NULL, &def_root_domain, i);
  7077. synchronize_sched();
  7078. arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
  7079. }
  7080. /* handle null as "default" */
  7081. static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
  7082. struct sched_domain_attr *new, int idx_new)
  7083. {
  7084. struct sched_domain_attr tmp;
  7085. /* fast path */
  7086. if (!new && !cur)
  7087. return 1;
  7088. tmp = SD_ATTR_INIT;
  7089. return !memcmp(cur ? (cur + idx_cur) : &tmp,
  7090. new ? (new + idx_new) : &tmp,
  7091. sizeof(struct sched_domain_attr));
  7092. }
  7093. /*
  7094. * Partition sched domains as specified by the 'ndoms_new'
  7095. * cpumasks in the array doms_new[] of cpumasks. This compares
  7096. * doms_new[] to the current sched domain partitioning, doms_cur[].
  7097. * It destroys each deleted domain and builds each new domain.
  7098. *
  7099. * 'doms_new' is an array of cpumask's of length 'ndoms_new'.
  7100. * The masks don't intersect (don't overlap.) We should setup one
  7101. * sched domain for each mask. CPUs not in any of the cpumasks will
  7102. * not be load balanced. If the same cpumask appears both in the
  7103. * current 'doms_cur' domains and in the new 'doms_new', we can leave
  7104. * it as it is.
  7105. *
  7106. * The passed in 'doms_new' should be kmalloc'd. This routine takes
  7107. * ownership of it and will kfree it when done with it. If the caller
  7108. * failed the kmalloc call, then it can pass in doms_new == NULL &&
  7109. * ndoms_new == 1, and partition_sched_domains() will fallback to
  7110. * the single partition 'fallback_doms', it also forces the domains
  7111. * to be rebuilt.
  7112. *
  7113. * If doms_new == NULL it will be replaced with cpu_online_mask.
  7114. * ndoms_new == 0 is a special case for destroying existing domains,
  7115. * and it will not create the default domain.
  7116. *
  7117. * Call with hotplug lock held
  7118. */
  7119. /* FIXME: Change to struct cpumask *doms_new[] */
  7120. void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
  7121. struct sched_domain_attr *dattr_new)
  7122. {
  7123. int i, j, n;
  7124. int new_topology;
  7125. mutex_lock(&sched_domains_mutex);
  7126. /* always unregister in case we don't destroy any domains */
  7127. unregister_sched_domain_sysctl();
  7128. /* Let architecture update cpu core mappings. */
  7129. new_topology = arch_update_cpu_topology();
  7130. n = doms_new ? ndoms_new : 0;
  7131. /* Destroy deleted domains */
  7132. for (i = 0; i < ndoms_cur; i++) {
  7133. for (j = 0; j < n && !new_topology; j++) {
  7134. if (cpumask_equal(&doms_cur[i], &doms_new[j])
  7135. && dattrs_equal(dattr_cur, i, dattr_new, j))
  7136. goto match1;
  7137. }
  7138. /* no match - a current sched domain not in new doms_new[] */
  7139. detach_destroy_domains(doms_cur + i);
  7140. match1:
  7141. ;
  7142. }
  7143. if (doms_new == NULL) {
  7144. ndoms_cur = 0;
  7145. doms_new = fallback_doms;
  7146. cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map);
  7147. WARN_ON_ONCE(dattr_new);
  7148. }
  7149. /* Build new domains */
  7150. for (i = 0; i < ndoms_new; i++) {
  7151. for (j = 0; j < ndoms_cur && !new_topology; j++) {
  7152. if (cpumask_equal(&doms_new[i], &doms_cur[j])
  7153. && dattrs_equal(dattr_new, i, dattr_cur, j))
  7154. goto match2;
  7155. }
  7156. /* no match - add a new doms_new */
  7157. __build_sched_domains(doms_new + i,
  7158. dattr_new ? dattr_new + i : NULL);
  7159. match2:
  7160. ;
  7161. }
  7162. /* Remember the new sched domains */
  7163. if (doms_cur != fallback_doms)
  7164. kfree(doms_cur);
  7165. kfree(dattr_cur); /* kfree(NULL) is safe */
  7166. doms_cur = doms_new;
  7167. dattr_cur = dattr_new;
  7168. ndoms_cur = ndoms_new;
  7169. register_sched_domain_sysctl();
  7170. mutex_unlock(&sched_domains_mutex);
  7171. }
  7172. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  7173. static void arch_reinit_sched_domains(void)
  7174. {
  7175. get_online_cpus();
  7176. /* Destroy domains first to force the rebuild */
  7177. partition_sched_domains(0, NULL, NULL);
  7178. rebuild_sched_domains();
  7179. put_online_cpus();
  7180. }
  7181. static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
  7182. {
  7183. unsigned int level = 0;
  7184. if (sscanf(buf, "%u", &level) != 1)
  7185. return -EINVAL;
  7186. /*
  7187. * level is always be positive so don't check for
  7188. * level < POWERSAVINGS_BALANCE_NONE which is 0
  7189. * What happens on 0 or 1 byte write,
  7190. * need to check for count as well?
  7191. */
  7192. if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
  7193. return -EINVAL;
  7194. if (smt)
  7195. sched_smt_power_savings = level;
  7196. else
  7197. sched_mc_power_savings = level;
  7198. arch_reinit_sched_domains();
  7199. return count;
  7200. }
  7201. #ifdef CONFIG_SCHED_MC
  7202. static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
  7203. char *page)
  7204. {
  7205. return sprintf(page, "%u\n", sched_mc_power_savings);
  7206. }
  7207. static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
  7208. const char *buf, size_t count)
  7209. {
  7210. return sched_power_savings_store(buf, count, 0);
  7211. }
  7212. static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
  7213. sched_mc_power_savings_show,
  7214. sched_mc_power_savings_store);
  7215. #endif
  7216. #ifdef CONFIG_SCHED_SMT
  7217. static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
  7218. char *page)
  7219. {
  7220. return sprintf(page, "%u\n", sched_smt_power_savings);
  7221. }
  7222. static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
  7223. const char *buf, size_t count)
  7224. {
  7225. return sched_power_savings_store(buf, count, 1);
  7226. }
  7227. static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
  7228. sched_smt_power_savings_show,
  7229. sched_smt_power_savings_store);
  7230. #endif
  7231. int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
  7232. {
  7233. int err = 0;
  7234. #ifdef CONFIG_SCHED_SMT
  7235. if (smt_capable())
  7236. err = sysfs_create_file(&cls->kset.kobj,
  7237. &attr_sched_smt_power_savings.attr);
  7238. #endif
  7239. #ifdef CONFIG_SCHED_MC
  7240. if (!err && mc_capable())
  7241. err = sysfs_create_file(&cls->kset.kobj,
  7242. &attr_sched_mc_power_savings.attr);
  7243. #endif
  7244. return err;
  7245. }
  7246. #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
  7247. #ifndef CONFIG_CPUSETS
  7248. /*
  7249. * Add online and remove offline CPUs from the scheduler domains.
  7250. * When cpusets are enabled they take over this function.
  7251. */
  7252. static int update_sched_domains(struct notifier_block *nfb,
  7253. unsigned long action, void *hcpu)
  7254. {
  7255. switch (action) {
  7256. case CPU_ONLINE:
  7257. case CPU_ONLINE_FROZEN:
  7258. case CPU_DEAD:
  7259. case CPU_DEAD_FROZEN:
  7260. partition_sched_domains(1, NULL, NULL);
  7261. return NOTIFY_OK;
  7262. default:
  7263. return NOTIFY_DONE;
  7264. }
  7265. }
  7266. #endif
  7267. static int update_runtime(struct notifier_block *nfb,
  7268. unsigned long action, void *hcpu)
  7269. {
  7270. int cpu = (int)(long)hcpu;
  7271. switch (action) {
  7272. case CPU_DOWN_PREPARE:
  7273. case CPU_DOWN_PREPARE_FROZEN:
  7274. disable_runtime(cpu_rq(cpu));
  7275. return NOTIFY_OK;
  7276. case CPU_DOWN_FAILED:
  7277. case CPU_DOWN_FAILED_FROZEN:
  7278. case CPU_ONLINE:
  7279. case CPU_ONLINE_FROZEN:
  7280. enable_runtime(cpu_rq(cpu));
  7281. return NOTIFY_OK;
  7282. default:
  7283. return NOTIFY_DONE;
  7284. }
  7285. }
  7286. void __init sched_init_smp(void)
  7287. {
  7288. cpumask_var_t non_isolated_cpus;
  7289. alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
  7290. #if defined(CONFIG_NUMA)
  7291. sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
  7292. GFP_KERNEL);
  7293. BUG_ON(sched_group_nodes_bycpu == NULL);
  7294. #endif
  7295. get_online_cpus();
  7296. mutex_lock(&sched_domains_mutex);
  7297. arch_init_sched_domains(cpu_online_mask);
  7298. cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
  7299. if (cpumask_empty(non_isolated_cpus))
  7300. cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
  7301. mutex_unlock(&sched_domains_mutex);
  7302. put_online_cpus();
  7303. #ifndef CONFIG_CPUSETS
  7304. /* XXX: Theoretical race here - CPU may be hotplugged now */
  7305. hotcpu_notifier(update_sched_domains, 0);
  7306. #endif
  7307. /* RT runtime code needs to handle some hotplug events */
  7308. hotcpu_notifier(update_runtime, 0);
  7309. init_hrtick();
  7310. /* Move init over to a non-isolated CPU */
  7311. if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
  7312. BUG();
  7313. sched_init_granularity();
  7314. free_cpumask_var(non_isolated_cpus);
  7315. alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
  7316. init_sched_rt_class();
  7317. }
  7318. #else
  7319. void __init sched_init_smp(void)
  7320. {
  7321. sched_init_granularity();
  7322. }
  7323. #endif /* CONFIG_SMP */
  7324. int in_sched_functions(unsigned long addr)
  7325. {
  7326. return in_lock_functions(addr) ||
  7327. (addr >= (unsigned long)__sched_text_start
  7328. && addr < (unsigned long)__sched_text_end);
  7329. }
  7330. static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
  7331. {
  7332. cfs_rq->tasks_timeline = RB_ROOT;
  7333. INIT_LIST_HEAD(&cfs_rq->tasks);
  7334. #ifdef CONFIG_FAIR_GROUP_SCHED
  7335. cfs_rq->rq = rq;
  7336. #endif
  7337. cfs_rq->min_vruntime = (u64)(-(1LL << 20));
  7338. }
  7339. static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
  7340. {
  7341. struct rt_prio_array *array;
  7342. int i;
  7343. array = &rt_rq->active;
  7344. for (i = 0; i < MAX_RT_PRIO; i++) {
  7345. INIT_LIST_HEAD(array->queue + i);
  7346. __clear_bit(i, array->bitmap);
  7347. }
  7348. /* delimiter for bitsearch: */
  7349. __set_bit(MAX_RT_PRIO, array->bitmap);
  7350. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  7351. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  7352. #ifdef CONFIG_SMP
  7353. rt_rq->highest_prio.next = MAX_RT_PRIO;
  7354. #endif
  7355. #endif
  7356. #ifdef CONFIG_SMP
  7357. rt_rq->rt_nr_migratory = 0;
  7358. rt_rq->overloaded = 0;
  7359. plist_head_init(&rq->rt.pushable_tasks, &rq->lock);
  7360. #endif
  7361. rt_rq->rt_time = 0;
  7362. rt_rq->rt_throttled = 0;
  7363. rt_rq->rt_runtime = 0;
  7364. spin_lock_init(&rt_rq->rt_runtime_lock);
  7365. #ifdef CONFIG_RT_GROUP_SCHED
  7366. rt_rq->rt_nr_boosted = 0;
  7367. rt_rq->rq = rq;
  7368. #endif
  7369. }
  7370. #ifdef CONFIG_FAIR_GROUP_SCHED
  7371. static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  7372. struct sched_entity *se, int cpu, int add,
  7373. struct sched_entity *parent)
  7374. {
  7375. struct rq *rq = cpu_rq(cpu);
  7376. tg->cfs_rq[cpu] = cfs_rq;
  7377. init_cfs_rq(cfs_rq, rq);
  7378. cfs_rq->tg = tg;
  7379. if (add)
  7380. list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
  7381. tg->se[cpu] = se;
  7382. /* se could be NULL for init_task_group */
  7383. if (!se)
  7384. return;
  7385. if (!parent)
  7386. se->cfs_rq = &rq->cfs;
  7387. else
  7388. se->cfs_rq = parent->my_q;
  7389. se->my_q = cfs_rq;
  7390. se->load.weight = tg->shares;
  7391. se->load.inv_weight = 0;
  7392. se->parent = parent;
  7393. }
  7394. #endif
  7395. #ifdef CONFIG_RT_GROUP_SCHED
  7396. static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  7397. struct sched_rt_entity *rt_se, int cpu, int add,
  7398. struct sched_rt_entity *parent)
  7399. {
  7400. struct rq *rq = cpu_rq(cpu);
  7401. tg->rt_rq[cpu] = rt_rq;
  7402. init_rt_rq(rt_rq, rq);
  7403. rt_rq->tg = tg;
  7404. rt_rq->rt_se = rt_se;
  7405. rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
  7406. if (add)
  7407. list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
  7408. tg->rt_se[cpu] = rt_se;
  7409. if (!rt_se)
  7410. return;
  7411. if (!parent)
  7412. rt_se->rt_rq = &rq->rt;
  7413. else
  7414. rt_se->rt_rq = parent->my_q;
  7415. rt_se->my_q = rt_rq;
  7416. rt_se->parent = parent;
  7417. INIT_LIST_HEAD(&rt_se->run_list);
  7418. }
  7419. #endif
  7420. void __init sched_init(void)
  7421. {
  7422. int i, j;
  7423. unsigned long alloc_size = 0, ptr;
  7424. #ifdef CONFIG_FAIR_GROUP_SCHED
  7425. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  7426. #endif
  7427. #ifdef CONFIG_RT_GROUP_SCHED
  7428. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  7429. #endif
  7430. #ifdef CONFIG_USER_SCHED
  7431. alloc_size *= 2;
  7432. #endif
  7433. /*
  7434. * As sched_init() is called before page_alloc is setup,
  7435. * we use alloc_bootmem().
  7436. */
  7437. if (alloc_size) {
  7438. ptr = (unsigned long)alloc_bootmem(alloc_size);
  7439. #ifdef CONFIG_FAIR_GROUP_SCHED
  7440. init_task_group.se = (struct sched_entity **)ptr;
  7441. ptr += nr_cpu_ids * sizeof(void **);
  7442. init_task_group.cfs_rq = (struct cfs_rq **)ptr;
  7443. ptr += nr_cpu_ids * sizeof(void **);
  7444. #ifdef CONFIG_USER_SCHED
  7445. root_task_group.se = (struct sched_entity **)ptr;
  7446. ptr += nr_cpu_ids * sizeof(void **);
  7447. root_task_group.cfs_rq = (struct cfs_rq **)ptr;
  7448. ptr += nr_cpu_ids * sizeof(void **);
  7449. #endif /* CONFIG_USER_SCHED */
  7450. #endif /* CONFIG_FAIR_GROUP_SCHED */
  7451. #ifdef CONFIG_RT_GROUP_SCHED
  7452. init_task_group.rt_se = (struct sched_rt_entity **)ptr;
  7453. ptr += nr_cpu_ids * sizeof(void **);
  7454. init_task_group.rt_rq = (struct rt_rq **)ptr;
  7455. ptr += nr_cpu_ids * sizeof(void **);
  7456. #ifdef CONFIG_USER_SCHED
  7457. root_task_group.rt_se = (struct sched_rt_entity **)ptr;
  7458. ptr += nr_cpu_ids * sizeof(void **);
  7459. root_task_group.rt_rq = (struct rt_rq **)ptr;
  7460. ptr += nr_cpu_ids * sizeof(void **);
  7461. #endif /* CONFIG_USER_SCHED */
  7462. #endif /* CONFIG_RT_GROUP_SCHED */
  7463. }
  7464. #ifdef CONFIG_SMP
  7465. init_defrootdomain();
  7466. #endif
  7467. init_rt_bandwidth(&def_rt_bandwidth,
  7468. global_rt_period(), global_rt_runtime());
  7469. #ifdef CONFIG_RT_GROUP_SCHED
  7470. init_rt_bandwidth(&init_task_group.rt_bandwidth,
  7471. global_rt_period(), global_rt_runtime());
  7472. #ifdef CONFIG_USER_SCHED
  7473. init_rt_bandwidth(&root_task_group.rt_bandwidth,
  7474. global_rt_period(), RUNTIME_INF);
  7475. #endif /* CONFIG_USER_SCHED */
  7476. #endif /* CONFIG_RT_GROUP_SCHED */
  7477. #ifdef CONFIG_GROUP_SCHED
  7478. list_add(&init_task_group.list, &task_groups);
  7479. INIT_LIST_HEAD(&init_task_group.children);
  7480. #ifdef CONFIG_USER_SCHED
  7481. INIT_LIST_HEAD(&root_task_group.children);
  7482. init_task_group.parent = &root_task_group;
  7483. list_add(&init_task_group.siblings, &root_task_group.children);
  7484. #endif /* CONFIG_USER_SCHED */
  7485. #endif /* CONFIG_GROUP_SCHED */
  7486. for_each_possible_cpu(i) {
  7487. struct rq *rq;
  7488. rq = cpu_rq(i);
  7489. spin_lock_init(&rq->lock);
  7490. rq->nr_running = 0;
  7491. init_cfs_rq(&rq->cfs, rq);
  7492. init_rt_rq(&rq->rt, rq);
  7493. #ifdef CONFIG_FAIR_GROUP_SCHED
  7494. init_task_group.shares = init_task_group_load;
  7495. INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
  7496. #ifdef CONFIG_CGROUP_SCHED
  7497. /*
  7498. * How much cpu bandwidth does init_task_group get?
  7499. *
  7500. * In case of task-groups formed thr' the cgroup filesystem, it
  7501. * gets 100% of the cpu resources in the system. This overall
  7502. * system cpu resource is divided among the tasks of
  7503. * init_task_group and its child task-groups in a fair manner,
  7504. * based on each entity's (task or task-group's) weight
  7505. * (se->load.weight).
  7506. *
  7507. * In other words, if init_task_group has 10 tasks of weight
  7508. * 1024) and two child groups A0 and A1 (of weight 1024 each),
  7509. * then A0's share of the cpu resource is:
  7510. *
  7511. * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
  7512. *
  7513. * We achieve this by letting init_task_group's tasks sit
  7514. * directly in rq->cfs (i.e init_task_group->se[] = NULL).
  7515. */
  7516. init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
  7517. #elif defined CONFIG_USER_SCHED
  7518. root_task_group.shares = NICE_0_LOAD;
  7519. init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
  7520. /*
  7521. * In case of task-groups formed thr' the user id of tasks,
  7522. * init_task_group represents tasks belonging to root user.
  7523. * Hence it forms a sibling of all subsequent groups formed.
  7524. * In this case, init_task_group gets only a fraction of overall
  7525. * system cpu resource, based on the weight assigned to root
  7526. * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
  7527. * by letting tasks of init_task_group sit in a separate cfs_rq
  7528. * (init_cfs_rq) and having one entity represent this group of
  7529. * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
  7530. */
  7531. init_tg_cfs_entry(&init_task_group,
  7532. &per_cpu(init_cfs_rq, i),
  7533. &per_cpu(init_sched_entity, i), i, 1,
  7534. root_task_group.se[i]);
  7535. #endif
  7536. #endif /* CONFIG_FAIR_GROUP_SCHED */
  7537. rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
  7538. #ifdef CONFIG_RT_GROUP_SCHED
  7539. INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
  7540. #ifdef CONFIG_CGROUP_SCHED
  7541. init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
  7542. #elif defined CONFIG_USER_SCHED
  7543. init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
  7544. init_tg_rt_entry(&init_task_group,
  7545. &per_cpu(init_rt_rq, i),
  7546. &per_cpu(init_sched_rt_entity, i), i, 1,
  7547. root_task_group.rt_se[i]);
  7548. #endif
  7549. #endif
  7550. for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
  7551. rq->cpu_load[j] = 0;
  7552. #ifdef CONFIG_SMP
  7553. rq->sd = NULL;
  7554. rq->rd = NULL;
  7555. rq->active_balance = 0;
  7556. rq->next_balance = jiffies;
  7557. rq->push_cpu = 0;
  7558. rq->cpu = i;
  7559. rq->online = 0;
  7560. rq->migration_thread = NULL;
  7561. INIT_LIST_HEAD(&rq->migration_queue);
  7562. rq_attach_root(rq, &def_root_domain);
  7563. #endif
  7564. init_rq_hrtick(rq);
  7565. atomic_set(&rq->nr_iowait, 0);
  7566. }
  7567. set_load_weight(&init_task);
  7568. #ifdef CONFIG_PREEMPT_NOTIFIERS
  7569. INIT_HLIST_HEAD(&init_task.preempt_notifiers);
  7570. #endif
  7571. #ifdef CONFIG_SMP
  7572. open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
  7573. #endif
  7574. #ifdef CONFIG_RT_MUTEXES
  7575. plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
  7576. #endif
  7577. /*
  7578. * The boot idle thread does lazy MMU switching as well:
  7579. */
  7580. atomic_inc(&init_mm.mm_count);
  7581. enter_lazy_tlb(&init_mm, current);
  7582. /*
  7583. * Make us the idle thread. Technically, schedule() should not be
  7584. * called from this thread, however somewhere below it might be,
  7585. * but because we are the idle thread, we just pick up running again
  7586. * when this runqueue becomes "idle".
  7587. */
  7588. init_idle(current, smp_processor_id());
  7589. /*
  7590. * During early bootup we pretend to be a normal task:
  7591. */
  7592. current->sched_class = &fair_sched_class;
  7593. /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
  7594. alloc_bootmem_cpumask_var(&nohz_cpu_mask);
  7595. #ifdef CONFIG_SMP
  7596. #ifdef CONFIG_NO_HZ
  7597. alloc_bootmem_cpumask_var(&nohz.cpu_mask);
  7598. #endif
  7599. alloc_bootmem_cpumask_var(&cpu_isolated_map);
  7600. #endif /* SMP */
  7601. scheduler_running = 1;
  7602. }
  7603. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  7604. void __might_sleep(char *file, int line)
  7605. {
  7606. #ifdef in_atomic
  7607. static unsigned long prev_jiffy; /* ratelimiting */
  7608. if ((!in_atomic() && !irqs_disabled()) ||
  7609. system_state != SYSTEM_RUNNING || oops_in_progress)
  7610. return;
  7611. if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  7612. return;
  7613. prev_jiffy = jiffies;
  7614. printk(KERN_ERR
  7615. "BUG: sleeping function called from invalid context at %s:%d\n",
  7616. file, line);
  7617. printk(KERN_ERR
  7618. "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
  7619. in_atomic(), irqs_disabled(),
  7620. current->pid, current->comm);
  7621. debug_show_held_locks(current);
  7622. if (irqs_disabled())
  7623. print_irqtrace_events(current);
  7624. dump_stack();
  7625. #endif
  7626. }
  7627. EXPORT_SYMBOL(__might_sleep);
  7628. #endif
  7629. #ifdef CONFIG_MAGIC_SYSRQ
  7630. static void normalize_task(struct rq *rq, struct task_struct *p)
  7631. {
  7632. int on_rq;
  7633. update_rq_clock(rq);
  7634. on_rq = p->se.on_rq;
  7635. if (on_rq)
  7636. deactivate_task(rq, p, 0);
  7637. __setscheduler(rq, p, SCHED_NORMAL, 0);
  7638. if (on_rq) {
  7639. activate_task(rq, p, 0);
  7640. resched_task(rq->curr);
  7641. }
  7642. }
  7643. void normalize_rt_tasks(void)
  7644. {
  7645. struct task_struct *g, *p;
  7646. unsigned long flags;
  7647. struct rq *rq;
  7648. read_lock_irqsave(&tasklist_lock, flags);
  7649. do_each_thread(g, p) {
  7650. /*
  7651. * Only normalize user tasks:
  7652. */
  7653. if (!p->mm)
  7654. continue;
  7655. p->se.exec_start = 0;
  7656. #ifdef CONFIG_SCHEDSTATS
  7657. p->se.wait_start = 0;
  7658. p->se.sleep_start = 0;
  7659. p->se.block_start = 0;
  7660. #endif
  7661. if (!rt_task(p)) {
  7662. /*
  7663. * Renice negative nice level userspace
  7664. * tasks back to 0:
  7665. */
  7666. if (TASK_NICE(p) < 0 && p->mm)
  7667. set_user_nice(p, 0);
  7668. continue;
  7669. }
  7670. spin_lock(&p->pi_lock);
  7671. rq = __task_rq_lock(p);
  7672. normalize_task(rq, p);
  7673. __task_rq_unlock(rq);
  7674. spin_unlock(&p->pi_lock);
  7675. } while_each_thread(g, p);
  7676. read_unlock_irqrestore(&tasklist_lock, flags);
  7677. }
  7678. #endif /* CONFIG_MAGIC_SYSRQ */
  7679. #ifdef CONFIG_IA64
  7680. /*
  7681. * These functions are only useful for the IA64 MCA handling.
  7682. *
  7683. * They can only be called when the whole system has been
  7684. * stopped - every CPU needs to be quiescent, and no scheduling
  7685. * activity can take place. Using them for anything else would
  7686. * be a serious bug, and as a result, they aren't even visible
  7687. * under any other configuration.
  7688. */
  7689. /**
  7690. * curr_task - return the current task for a given cpu.
  7691. * @cpu: the processor in question.
  7692. *
  7693. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  7694. */
  7695. struct task_struct *curr_task(int cpu)
  7696. {
  7697. return cpu_curr(cpu);
  7698. }
  7699. /**
  7700. * set_curr_task - set the current task for a given cpu.
  7701. * @cpu: the processor in question.
  7702. * @p: the task pointer to set.
  7703. *
  7704. * Description: This function must only be used when non-maskable interrupts
  7705. * are serviced on a separate stack. It allows the architecture to switch the
  7706. * notion of the current task on a cpu in a non-blocking manner. This function
  7707. * must be called with all CPU's synchronized, and interrupts disabled, the
  7708. * and caller must save the original value of the current task (see
  7709. * curr_task() above) and restore that value before reenabling interrupts and
  7710. * re-starting the system.
  7711. *
  7712. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  7713. */
  7714. void set_curr_task(int cpu, struct task_struct *p)
  7715. {
  7716. cpu_curr(cpu) = p;
  7717. }
  7718. #endif
  7719. #ifdef CONFIG_FAIR_GROUP_SCHED
  7720. static void free_fair_sched_group(struct task_group *tg)
  7721. {
  7722. int i;
  7723. for_each_possible_cpu(i) {
  7724. if (tg->cfs_rq)
  7725. kfree(tg->cfs_rq[i]);
  7726. if (tg->se)
  7727. kfree(tg->se[i]);
  7728. }
  7729. kfree(tg->cfs_rq);
  7730. kfree(tg->se);
  7731. }
  7732. static
  7733. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  7734. {
  7735. struct cfs_rq *cfs_rq;
  7736. struct sched_entity *se;
  7737. struct rq *rq;
  7738. int i;
  7739. tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
  7740. if (!tg->cfs_rq)
  7741. goto err;
  7742. tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
  7743. if (!tg->se)
  7744. goto err;
  7745. tg->shares = NICE_0_LOAD;
  7746. for_each_possible_cpu(i) {
  7747. rq = cpu_rq(i);
  7748. cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
  7749. GFP_KERNEL, cpu_to_node(i));
  7750. if (!cfs_rq)
  7751. goto err;
  7752. se = kzalloc_node(sizeof(struct sched_entity),
  7753. GFP_KERNEL, cpu_to_node(i));
  7754. if (!se)
  7755. goto err;
  7756. init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
  7757. }
  7758. return 1;
  7759. err:
  7760. return 0;
  7761. }
  7762. static inline void register_fair_sched_group(struct task_group *tg, int cpu)
  7763. {
  7764. list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
  7765. &cpu_rq(cpu)->leaf_cfs_rq_list);
  7766. }
  7767. static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
  7768. {
  7769. list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
  7770. }
  7771. #else /* !CONFG_FAIR_GROUP_SCHED */
  7772. static inline void free_fair_sched_group(struct task_group *tg)
  7773. {
  7774. }
  7775. static inline
  7776. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  7777. {
  7778. return 1;
  7779. }
  7780. static inline void register_fair_sched_group(struct task_group *tg, int cpu)
  7781. {
  7782. }
  7783. static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
  7784. {
  7785. }
  7786. #endif /* CONFIG_FAIR_GROUP_SCHED */
  7787. #ifdef CONFIG_RT_GROUP_SCHED
  7788. static void free_rt_sched_group(struct task_group *tg)
  7789. {
  7790. int i;
  7791. destroy_rt_bandwidth(&tg->rt_bandwidth);
  7792. for_each_possible_cpu(i) {
  7793. if (tg->rt_rq)
  7794. kfree(tg->rt_rq[i]);
  7795. if (tg->rt_se)
  7796. kfree(tg->rt_se[i]);
  7797. }
  7798. kfree(tg->rt_rq);
  7799. kfree(tg->rt_se);
  7800. }
  7801. static
  7802. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  7803. {
  7804. struct rt_rq *rt_rq;
  7805. struct sched_rt_entity *rt_se;
  7806. struct rq *rq;
  7807. int i;
  7808. tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
  7809. if (!tg->rt_rq)
  7810. goto err;
  7811. tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
  7812. if (!tg->rt_se)
  7813. goto err;
  7814. init_rt_bandwidth(&tg->rt_bandwidth,
  7815. ktime_to_ns(def_rt_bandwidth.rt_period), 0);
  7816. for_each_possible_cpu(i) {
  7817. rq = cpu_rq(i);
  7818. rt_rq = kzalloc_node(sizeof(struct rt_rq),
  7819. GFP_KERNEL, cpu_to_node(i));
  7820. if (!rt_rq)
  7821. goto err;
  7822. rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
  7823. GFP_KERNEL, cpu_to_node(i));
  7824. if (!rt_se)
  7825. goto err;
  7826. init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
  7827. }
  7828. return 1;
  7829. err:
  7830. return 0;
  7831. }
  7832. static inline void register_rt_sched_group(struct task_group *tg, int cpu)
  7833. {
  7834. list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
  7835. &cpu_rq(cpu)->leaf_rt_rq_list);
  7836. }
  7837. static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
  7838. {
  7839. list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
  7840. }
  7841. #else /* !CONFIG_RT_GROUP_SCHED */
  7842. static inline void free_rt_sched_group(struct task_group *tg)
  7843. {
  7844. }
  7845. static inline
  7846. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  7847. {
  7848. return 1;
  7849. }
  7850. static inline void register_rt_sched_group(struct task_group *tg, int cpu)
  7851. {
  7852. }
  7853. static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
  7854. {
  7855. }
  7856. #endif /* CONFIG_RT_GROUP_SCHED */
  7857. #ifdef CONFIG_GROUP_SCHED
  7858. static void free_sched_group(struct task_group *tg)
  7859. {
  7860. free_fair_sched_group(tg);
  7861. free_rt_sched_group(tg);
  7862. kfree(tg);
  7863. }
  7864. /* allocate runqueue etc for a new task group */
  7865. struct task_group *sched_create_group(struct task_group *parent)
  7866. {
  7867. struct task_group *tg;
  7868. unsigned long flags;
  7869. int i;
  7870. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  7871. if (!tg)
  7872. return ERR_PTR(-ENOMEM);
  7873. if (!alloc_fair_sched_group(tg, parent))
  7874. goto err;
  7875. if (!alloc_rt_sched_group(tg, parent))
  7876. goto err;
  7877. spin_lock_irqsave(&task_group_lock, flags);
  7878. for_each_possible_cpu(i) {
  7879. register_fair_sched_group(tg, i);
  7880. register_rt_sched_group(tg, i);
  7881. }
  7882. list_add_rcu(&tg->list, &task_groups);
  7883. WARN_ON(!parent); /* root should already exist */
  7884. tg->parent = parent;
  7885. INIT_LIST_HEAD(&tg->children);
  7886. list_add_rcu(&tg->siblings, &parent->children);
  7887. spin_unlock_irqrestore(&task_group_lock, flags);
  7888. return tg;
  7889. err:
  7890. free_sched_group(tg);
  7891. return ERR_PTR(-ENOMEM);
  7892. }
  7893. /* rcu callback to free various structures associated with a task group */
  7894. static void free_sched_group_rcu(struct rcu_head *rhp)
  7895. {
  7896. /* now it should be safe to free those cfs_rqs */
  7897. free_sched_group(container_of(rhp, struct task_group, rcu));
  7898. }
  7899. /* Destroy runqueue etc associated with a task group */
  7900. void sched_destroy_group(struct task_group *tg)
  7901. {
  7902. unsigned long flags;
  7903. int i;
  7904. spin_lock_irqsave(&task_group_lock, flags);
  7905. for_each_possible_cpu(i) {
  7906. unregister_fair_sched_group(tg, i);
  7907. unregister_rt_sched_group(tg, i);
  7908. }
  7909. list_del_rcu(&tg->list);
  7910. list_del_rcu(&tg->siblings);
  7911. spin_unlock_irqrestore(&task_group_lock, flags);
  7912. /* wait for possible concurrent references to cfs_rqs complete */
  7913. call_rcu(&tg->rcu, free_sched_group_rcu);
  7914. }
  7915. /* change task's runqueue when it moves between groups.
  7916. * The caller of this function should have put the task in its new group
  7917. * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
  7918. * reflect its new group.
  7919. */
  7920. void sched_move_task(struct task_struct *tsk)
  7921. {
  7922. int on_rq, running;
  7923. unsigned long flags;
  7924. struct rq *rq;
  7925. rq = task_rq_lock(tsk, &flags);
  7926. update_rq_clock(rq);
  7927. running = task_current(rq, tsk);
  7928. on_rq = tsk->se.on_rq;
  7929. if (on_rq)
  7930. dequeue_task(rq, tsk, 0);
  7931. if (unlikely(running))
  7932. tsk->sched_class->put_prev_task(rq, tsk);
  7933. set_task_rq(tsk, task_cpu(tsk));
  7934. #ifdef CONFIG_FAIR_GROUP_SCHED
  7935. if (tsk->sched_class->moved_group)
  7936. tsk->sched_class->moved_group(tsk);
  7937. #endif
  7938. if (unlikely(running))
  7939. tsk->sched_class->set_curr_task(rq);
  7940. if (on_rq)
  7941. enqueue_task(rq, tsk, 0);
  7942. task_rq_unlock(rq, &flags);
  7943. }
  7944. #endif /* CONFIG_GROUP_SCHED */
  7945. #ifdef CONFIG_FAIR_GROUP_SCHED
  7946. static void __set_se_shares(struct sched_entity *se, unsigned long shares)
  7947. {
  7948. struct cfs_rq *cfs_rq = se->cfs_rq;
  7949. int on_rq;
  7950. on_rq = se->on_rq;
  7951. if (on_rq)
  7952. dequeue_entity(cfs_rq, se, 0);
  7953. se->load.weight = shares;
  7954. se->load.inv_weight = 0;
  7955. if (on_rq)
  7956. enqueue_entity(cfs_rq, se, 0);
  7957. }
  7958. static void set_se_shares(struct sched_entity *se, unsigned long shares)
  7959. {
  7960. struct cfs_rq *cfs_rq = se->cfs_rq;
  7961. struct rq *rq = cfs_rq->rq;
  7962. unsigned long flags;
  7963. spin_lock_irqsave(&rq->lock, flags);
  7964. __set_se_shares(se, shares);
  7965. spin_unlock_irqrestore(&rq->lock, flags);
  7966. }
  7967. static DEFINE_MUTEX(shares_mutex);
  7968. int sched_group_set_shares(struct task_group *tg, unsigned long shares)
  7969. {
  7970. int i;
  7971. unsigned long flags;
  7972. /*
  7973. * We can't change the weight of the root cgroup.
  7974. */
  7975. if (!tg->se[0])
  7976. return -EINVAL;
  7977. if (shares < MIN_SHARES)
  7978. shares = MIN_SHARES;
  7979. else if (shares > MAX_SHARES)
  7980. shares = MAX_SHARES;
  7981. mutex_lock(&shares_mutex);
  7982. if (tg->shares == shares)
  7983. goto done;
  7984. spin_lock_irqsave(&task_group_lock, flags);
  7985. for_each_possible_cpu(i)
  7986. unregister_fair_sched_group(tg, i);
  7987. list_del_rcu(&tg->siblings);
  7988. spin_unlock_irqrestore(&task_group_lock, flags);
  7989. /* wait for any ongoing reference to this group to finish */
  7990. synchronize_sched();
  7991. /*
  7992. * Now we are free to modify the group's share on each cpu
  7993. * w/o tripping rebalance_share or load_balance_fair.
  7994. */
  7995. tg->shares = shares;
  7996. for_each_possible_cpu(i) {
  7997. /*
  7998. * force a rebalance
  7999. */
  8000. cfs_rq_set_shares(tg->cfs_rq[i], 0);
  8001. set_se_shares(tg->se[i], shares);
  8002. }
  8003. /*
  8004. * Enable load balance activity on this group, by inserting it back on
  8005. * each cpu's rq->leaf_cfs_rq_list.
  8006. */
  8007. spin_lock_irqsave(&task_group_lock, flags);
  8008. for_each_possible_cpu(i)
  8009. register_fair_sched_group(tg, i);
  8010. list_add_rcu(&tg->siblings, &tg->parent->children);
  8011. spin_unlock_irqrestore(&task_group_lock, flags);
  8012. done:
  8013. mutex_unlock(&shares_mutex);
  8014. return 0;
  8015. }
  8016. unsigned long sched_group_shares(struct task_group *tg)
  8017. {
  8018. return tg->shares;
  8019. }
  8020. #endif
  8021. #ifdef CONFIG_RT_GROUP_SCHED
  8022. /*
  8023. * Ensure that the real time constraints are schedulable.
  8024. */
  8025. static DEFINE_MUTEX(rt_constraints_mutex);
  8026. static unsigned long to_ratio(u64 period, u64 runtime)
  8027. {
  8028. if (runtime == RUNTIME_INF)
  8029. return 1ULL << 20;
  8030. return div64_u64(runtime << 20, period);
  8031. }
  8032. /* Must be called with tasklist_lock held */
  8033. static inline int tg_has_rt_tasks(struct task_group *tg)
  8034. {
  8035. struct task_struct *g, *p;
  8036. do_each_thread(g, p) {
  8037. if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
  8038. return 1;
  8039. } while_each_thread(g, p);
  8040. return 0;
  8041. }
  8042. struct rt_schedulable_data {
  8043. struct task_group *tg;
  8044. u64 rt_period;
  8045. u64 rt_runtime;
  8046. };
  8047. static int tg_schedulable(struct task_group *tg, void *data)
  8048. {
  8049. struct rt_schedulable_data *d = data;
  8050. struct task_group *child;
  8051. unsigned long total, sum = 0;
  8052. u64 period, runtime;
  8053. period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  8054. runtime = tg->rt_bandwidth.rt_runtime;
  8055. if (tg == d->tg) {
  8056. period = d->rt_period;
  8057. runtime = d->rt_runtime;
  8058. }
  8059. #ifdef CONFIG_USER_SCHED
  8060. if (tg == &root_task_group) {
  8061. period = global_rt_period();
  8062. runtime = global_rt_runtime();
  8063. }
  8064. #endif
  8065. /*
  8066. * Cannot have more runtime than the period.
  8067. */
  8068. if (runtime > period && runtime != RUNTIME_INF)
  8069. return -EINVAL;
  8070. /*
  8071. * Ensure we don't starve existing RT tasks.
  8072. */
  8073. if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
  8074. return -EBUSY;
  8075. total = to_ratio(period, runtime);
  8076. /*
  8077. * Nobody can have more than the global setting allows.
  8078. */
  8079. if (total > to_ratio(global_rt_period(), global_rt_runtime()))
  8080. return -EINVAL;
  8081. /*
  8082. * The sum of our children's runtime should not exceed our own.
  8083. */
  8084. list_for_each_entry_rcu(child, &tg->children, siblings) {
  8085. period = ktime_to_ns(child->rt_bandwidth.rt_period);
  8086. runtime = child->rt_bandwidth.rt_runtime;
  8087. if (child == d->tg) {
  8088. period = d->rt_period;
  8089. runtime = d->rt_runtime;
  8090. }
  8091. sum += to_ratio(period, runtime);
  8092. }
  8093. if (sum > total)
  8094. return -EINVAL;
  8095. return 0;
  8096. }
  8097. static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
  8098. {
  8099. struct rt_schedulable_data data = {
  8100. .tg = tg,
  8101. .rt_period = period,
  8102. .rt_runtime = runtime,
  8103. };
  8104. return walk_tg_tree(tg_schedulable, tg_nop, &data);
  8105. }
  8106. static int tg_set_bandwidth(struct task_group *tg,
  8107. u64 rt_period, u64 rt_runtime)
  8108. {
  8109. int i, err = 0;
  8110. mutex_lock(&rt_constraints_mutex);
  8111. read_lock(&tasklist_lock);
  8112. err = __rt_schedulable(tg, rt_period, rt_runtime);
  8113. if (err)
  8114. goto unlock;
  8115. spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  8116. tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
  8117. tg->rt_bandwidth.rt_runtime = rt_runtime;
  8118. for_each_possible_cpu(i) {
  8119. struct rt_rq *rt_rq = tg->rt_rq[i];
  8120. spin_lock(&rt_rq->rt_runtime_lock);
  8121. rt_rq->rt_runtime = rt_runtime;
  8122. spin_unlock(&rt_rq->rt_runtime_lock);
  8123. }
  8124. spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  8125. unlock:
  8126. read_unlock(&tasklist_lock);
  8127. mutex_unlock(&rt_constraints_mutex);
  8128. return err;
  8129. }
  8130. int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
  8131. {
  8132. u64 rt_runtime, rt_period;
  8133. rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  8134. rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
  8135. if (rt_runtime_us < 0)
  8136. rt_runtime = RUNTIME_INF;
  8137. return tg_set_bandwidth(tg, rt_period, rt_runtime);
  8138. }
  8139. long sched_group_rt_runtime(struct task_group *tg)
  8140. {
  8141. u64 rt_runtime_us;
  8142. if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
  8143. return -1;
  8144. rt_runtime_us = tg->rt_bandwidth.rt_runtime;
  8145. do_div(rt_runtime_us, NSEC_PER_USEC);
  8146. return rt_runtime_us;
  8147. }
  8148. int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
  8149. {
  8150. u64 rt_runtime, rt_period;
  8151. rt_period = (u64)rt_period_us * NSEC_PER_USEC;
  8152. rt_runtime = tg->rt_bandwidth.rt_runtime;
  8153. if (rt_period == 0)
  8154. return -EINVAL;
  8155. return tg_set_bandwidth(tg, rt_period, rt_runtime);
  8156. }
  8157. long sched_group_rt_period(struct task_group *tg)
  8158. {
  8159. u64 rt_period_us;
  8160. rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
  8161. do_div(rt_period_us, NSEC_PER_USEC);
  8162. return rt_period_us;
  8163. }
  8164. static int sched_rt_global_constraints(void)
  8165. {
  8166. u64 runtime, period;
  8167. int ret = 0;
  8168. if (sysctl_sched_rt_period <= 0)
  8169. return -EINVAL;
  8170. runtime = global_rt_runtime();
  8171. period = global_rt_period();
  8172. /*
  8173. * Sanity check on the sysctl variables.
  8174. */
  8175. if (runtime > period && runtime != RUNTIME_INF)
  8176. return -EINVAL;
  8177. mutex_lock(&rt_constraints_mutex);
  8178. read_lock(&tasklist_lock);
  8179. ret = __rt_schedulable(NULL, 0, 0);
  8180. read_unlock(&tasklist_lock);
  8181. mutex_unlock(&rt_constraints_mutex);
  8182. return ret;
  8183. }
  8184. int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
  8185. {
  8186. /* Don't accept realtime tasks when there is no way for them to run */
  8187. if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
  8188. return 0;
  8189. return 1;
  8190. }
  8191. #else /* !CONFIG_RT_GROUP_SCHED */
  8192. static int sched_rt_global_constraints(void)
  8193. {
  8194. unsigned long flags;
  8195. int i;
  8196. if (sysctl_sched_rt_period <= 0)
  8197. return -EINVAL;
  8198. spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
  8199. for_each_possible_cpu(i) {
  8200. struct rt_rq *rt_rq = &cpu_rq(i)->rt;
  8201. spin_lock(&rt_rq->rt_runtime_lock);
  8202. rt_rq->rt_runtime = global_rt_runtime();
  8203. spin_unlock(&rt_rq->rt_runtime_lock);
  8204. }
  8205. spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
  8206. return 0;
  8207. }
  8208. #endif /* CONFIG_RT_GROUP_SCHED */
  8209. int sched_rt_handler(struct ctl_table *table, int write,
  8210. struct file *filp, void __user *buffer, size_t *lenp,
  8211. loff_t *ppos)
  8212. {
  8213. int ret;
  8214. int old_period, old_runtime;
  8215. static DEFINE_MUTEX(mutex);
  8216. mutex_lock(&mutex);
  8217. old_period = sysctl_sched_rt_period;
  8218. old_runtime = sysctl_sched_rt_runtime;
  8219. ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  8220. if (!ret && write) {
  8221. ret = sched_rt_global_constraints();
  8222. if (ret) {
  8223. sysctl_sched_rt_period = old_period;
  8224. sysctl_sched_rt_runtime = old_runtime;
  8225. } else {
  8226. def_rt_bandwidth.rt_runtime = global_rt_runtime();
  8227. def_rt_bandwidth.rt_period =
  8228. ns_to_ktime(global_rt_period());
  8229. }
  8230. }
  8231. mutex_unlock(&mutex);
  8232. return ret;
  8233. }
  8234. #ifdef CONFIG_CGROUP_SCHED
  8235. /* return corresponding task_group object of a cgroup */
  8236. static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
  8237. {
  8238. return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
  8239. struct task_group, css);
  8240. }
  8241. static struct cgroup_subsys_state *
  8242. cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8243. {
  8244. struct task_group *tg, *parent;
  8245. if (!cgrp->parent) {
  8246. /* This is early initialization for the top cgroup */
  8247. return &init_task_group.css;
  8248. }
  8249. parent = cgroup_tg(cgrp->parent);
  8250. tg = sched_create_group(parent);
  8251. if (IS_ERR(tg))
  8252. return ERR_PTR(-ENOMEM);
  8253. return &tg->css;
  8254. }
  8255. static void
  8256. cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8257. {
  8258. struct task_group *tg = cgroup_tg(cgrp);
  8259. sched_destroy_group(tg);
  8260. }
  8261. static int
  8262. cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  8263. struct task_struct *tsk)
  8264. {
  8265. #ifdef CONFIG_RT_GROUP_SCHED
  8266. if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
  8267. return -EINVAL;
  8268. #else
  8269. /* We don't support RT-tasks being in separate groups */
  8270. if (tsk->sched_class != &fair_sched_class)
  8271. return -EINVAL;
  8272. #endif
  8273. return 0;
  8274. }
  8275. static void
  8276. cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  8277. struct cgroup *old_cont, struct task_struct *tsk)
  8278. {
  8279. sched_move_task(tsk);
  8280. }
  8281. #ifdef CONFIG_FAIR_GROUP_SCHED
  8282. static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
  8283. u64 shareval)
  8284. {
  8285. return sched_group_set_shares(cgroup_tg(cgrp), shareval);
  8286. }
  8287. static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
  8288. {
  8289. struct task_group *tg = cgroup_tg(cgrp);
  8290. return (u64) tg->shares;
  8291. }
  8292. #endif /* CONFIG_FAIR_GROUP_SCHED */
  8293. #ifdef CONFIG_RT_GROUP_SCHED
  8294. static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
  8295. s64 val)
  8296. {
  8297. return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
  8298. }
  8299. static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
  8300. {
  8301. return sched_group_rt_runtime(cgroup_tg(cgrp));
  8302. }
  8303. static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
  8304. u64 rt_period_us)
  8305. {
  8306. return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
  8307. }
  8308. static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
  8309. {
  8310. return sched_group_rt_period(cgroup_tg(cgrp));
  8311. }
  8312. #endif /* CONFIG_RT_GROUP_SCHED */
  8313. static struct cftype cpu_files[] = {
  8314. #ifdef CONFIG_FAIR_GROUP_SCHED
  8315. {
  8316. .name = "shares",
  8317. .read_u64 = cpu_shares_read_u64,
  8318. .write_u64 = cpu_shares_write_u64,
  8319. },
  8320. #endif
  8321. #ifdef CONFIG_RT_GROUP_SCHED
  8322. {
  8323. .name = "rt_runtime_us",
  8324. .read_s64 = cpu_rt_runtime_read,
  8325. .write_s64 = cpu_rt_runtime_write,
  8326. },
  8327. {
  8328. .name = "rt_period_us",
  8329. .read_u64 = cpu_rt_period_read_uint,
  8330. .write_u64 = cpu_rt_period_write_uint,
  8331. },
  8332. #endif
  8333. };
  8334. static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  8335. {
  8336. return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
  8337. }
  8338. struct cgroup_subsys cpu_cgroup_subsys = {
  8339. .name = "cpu",
  8340. .create = cpu_cgroup_create,
  8341. .destroy = cpu_cgroup_destroy,
  8342. .can_attach = cpu_cgroup_can_attach,
  8343. .attach = cpu_cgroup_attach,
  8344. .populate = cpu_cgroup_populate,
  8345. .subsys_id = cpu_cgroup_subsys_id,
  8346. .early_init = 1,
  8347. };
  8348. #endif /* CONFIG_CGROUP_SCHED */
  8349. #ifdef CONFIG_CGROUP_CPUACCT
  8350. /*
  8351. * CPU accounting code for task groups.
  8352. *
  8353. * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
  8354. * (balbir@in.ibm.com).
  8355. */
  8356. /* track cpu usage of a group of tasks and its child groups */
  8357. struct cpuacct {
  8358. struct cgroup_subsys_state css;
  8359. /* cpuusage holds pointer to a u64-type object on every cpu */
  8360. u64 *cpuusage;
  8361. struct cpuacct *parent;
  8362. };
  8363. struct cgroup_subsys cpuacct_subsys;
  8364. /* return cpu accounting group corresponding to this container */
  8365. static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
  8366. {
  8367. return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
  8368. struct cpuacct, css);
  8369. }
  8370. /* return cpu accounting group to which this task belongs */
  8371. static inline struct cpuacct *task_ca(struct task_struct *tsk)
  8372. {
  8373. return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
  8374. struct cpuacct, css);
  8375. }
  8376. /* create a new cpu accounting group */
  8377. static struct cgroup_subsys_state *cpuacct_create(
  8378. struct cgroup_subsys *ss, struct cgroup *cgrp)
  8379. {
  8380. struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  8381. if (!ca)
  8382. return ERR_PTR(-ENOMEM);
  8383. ca->cpuusage = alloc_percpu(u64);
  8384. if (!ca->cpuusage) {
  8385. kfree(ca);
  8386. return ERR_PTR(-ENOMEM);
  8387. }
  8388. if (cgrp->parent)
  8389. ca->parent = cgroup_ca(cgrp->parent);
  8390. return &ca->css;
  8391. }
  8392. /* destroy an existing cpu accounting group */
  8393. static void
  8394. cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8395. {
  8396. struct cpuacct *ca = cgroup_ca(cgrp);
  8397. free_percpu(ca->cpuusage);
  8398. kfree(ca);
  8399. }
  8400. static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
  8401. {
  8402. u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  8403. u64 data;
  8404. #ifndef CONFIG_64BIT
  8405. /*
  8406. * Take rq->lock to make 64-bit read safe on 32-bit platforms.
  8407. */
  8408. spin_lock_irq(&cpu_rq(cpu)->lock);
  8409. data = *cpuusage;
  8410. spin_unlock_irq(&cpu_rq(cpu)->lock);
  8411. #else
  8412. data = *cpuusage;
  8413. #endif
  8414. return data;
  8415. }
  8416. static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
  8417. {
  8418. u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  8419. #ifndef CONFIG_64BIT
  8420. /*
  8421. * Take rq->lock to make 64-bit write safe on 32-bit platforms.
  8422. */
  8423. spin_lock_irq(&cpu_rq(cpu)->lock);
  8424. *cpuusage = val;
  8425. spin_unlock_irq(&cpu_rq(cpu)->lock);
  8426. #else
  8427. *cpuusage = val;
  8428. #endif
  8429. }
  8430. /* return total cpu usage (in nanoseconds) of a group */
  8431. static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
  8432. {
  8433. struct cpuacct *ca = cgroup_ca(cgrp);
  8434. u64 totalcpuusage = 0;
  8435. int i;
  8436. for_each_present_cpu(i)
  8437. totalcpuusage += cpuacct_cpuusage_read(ca, i);
  8438. return totalcpuusage;
  8439. }
  8440. static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
  8441. u64 reset)
  8442. {
  8443. struct cpuacct *ca = cgroup_ca(cgrp);
  8444. int err = 0;
  8445. int i;
  8446. if (reset) {
  8447. err = -EINVAL;
  8448. goto out;
  8449. }
  8450. for_each_present_cpu(i)
  8451. cpuacct_cpuusage_write(ca, i, 0);
  8452. out:
  8453. return err;
  8454. }
  8455. static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
  8456. struct seq_file *m)
  8457. {
  8458. struct cpuacct *ca = cgroup_ca(cgroup);
  8459. u64 percpu;
  8460. int i;
  8461. for_each_present_cpu(i) {
  8462. percpu = cpuacct_cpuusage_read(ca, i);
  8463. seq_printf(m, "%llu ", (unsigned long long) percpu);
  8464. }
  8465. seq_printf(m, "\n");
  8466. return 0;
  8467. }
  8468. static struct cftype files[] = {
  8469. {
  8470. .name = "usage",
  8471. .read_u64 = cpuusage_read,
  8472. .write_u64 = cpuusage_write,
  8473. },
  8474. {
  8475. .name = "usage_percpu",
  8476. .read_seq_string = cpuacct_percpu_seq_read,
  8477. },
  8478. };
  8479. static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8480. {
  8481. return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
  8482. }
  8483. /*
  8484. * charge this task's execution time to its accounting group.
  8485. *
  8486. * called with rq->lock held.
  8487. */
  8488. static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
  8489. {
  8490. struct cpuacct *ca;
  8491. int cpu;
  8492. if (unlikely(!cpuacct_subsys.active))
  8493. return;
  8494. cpu = task_cpu(tsk);
  8495. ca = task_ca(tsk);
  8496. for (; ca; ca = ca->parent) {
  8497. u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  8498. *cpuusage += cputime;
  8499. }
  8500. }
  8501. struct cgroup_subsys cpuacct_subsys = {
  8502. .name = "cpuacct",
  8503. .create = cpuacct_create,
  8504. .destroy = cpuacct_destroy,
  8505. .populate = cpuacct_populate,
  8506. .subsys_id = cpuacct_subsys_id,
  8507. };
  8508. #endif /* CONFIG_CGROUP_CPUACCT */