sched.c 246 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099
  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, 0);
  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. /* Working cpumask for load_balance and load_balance_newidle. */
  3254. static DEFINE_PER_CPU(cpumask_var_t, load_balance_tmpmask);
  3255. /*
  3256. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  3257. * tasks if there is an imbalance.
  3258. */
  3259. static int load_balance(int this_cpu, struct rq *this_rq,
  3260. struct sched_domain *sd, enum cpu_idle_type idle,
  3261. int *balance)
  3262. {
  3263. int ld_moved, all_pinned = 0, active_balance = 0, sd_idle = 0;
  3264. struct sched_group *group;
  3265. unsigned long imbalance;
  3266. struct rq *busiest;
  3267. unsigned long flags;
  3268. struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
  3269. cpumask_setall(cpus);
  3270. /*
  3271. * When power savings policy is enabled for the parent domain, idle
  3272. * sibling can pick up load irrespective of busy siblings. In this case,
  3273. * let the state of idle sibling percolate up as CPU_IDLE, instead of
  3274. * portraying it as CPU_NOT_IDLE.
  3275. */
  3276. if (idle != CPU_NOT_IDLE && sd->flags & SD_SHARE_CPUPOWER &&
  3277. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3278. sd_idle = 1;
  3279. schedstat_inc(sd, lb_count[idle]);
  3280. redo:
  3281. update_shares(sd);
  3282. group = find_busiest_group(sd, this_cpu, &imbalance, idle, &sd_idle,
  3283. cpus, balance);
  3284. if (*balance == 0)
  3285. goto out_balanced;
  3286. if (!group) {
  3287. schedstat_inc(sd, lb_nobusyg[idle]);
  3288. goto out_balanced;
  3289. }
  3290. busiest = find_busiest_queue(group, idle, imbalance, cpus);
  3291. if (!busiest) {
  3292. schedstat_inc(sd, lb_nobusyq[idle]);
  3293. goto out_balanced;
  3294. }
  3295. BUG_ON(busiest == this_rq);
  3296. schedstat_add(sd, lb_imbalance[idle], imbalance);
  3297. ld_moved = 0;
  3298. if (busiest->nr_running > 1) {
  3299. /*
  3300. * Attempt to move tasks. If find_busiest_group has found
  3301. * an imbalance but busiest->nr_running <= 1, the group is
  3302. * still unbalanced. ld_moved simply stays zero, so it is
  3303. * correctly treated as an imbalance.
  3304. */
  3305. local_irq_save(flags);
  3306. double_rq_lock(this_rq, busiest);
  3307. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  3308. imbalance, sd, idle, &all_pinned);
  3309. double_rq_unlock(this_rq, busiest);
  3310. local_irq_restore(flags);
  3311. /*
  3312. * some other cpu did the load balance for us.
  3313. */
  3314. if (ld_moved && this_cpu != smp_processor_id())
  3315. resched_cpu(this_cpu);
  3316. /* All tasks on this runqueue were pinned by CPU affinity */
  3317. if (unlikely(all_pinned)) {
  3318. cpumask_clear_cpu(cpu_of(busiest), cpus);
  3319. if (!cpumask_empty(cpus))
  3320. goto redo;
  3321. goto out_balanced;
  3322. }
  3323. }
  3324. if (!ld_moved) {
  3325. schedstat_inc(sd, lb_failed[idle]);
  3326. sd->nr_balance_failed++;
  3327. if (unlikely(sd->nr_balance_failed > sd->cache_nice_tries+2)) {
  3328. spin_lock_irqsave(&busiest->lock, flags);
  3329. /* don't kick the migration_thread, if the curr
  3330. * task on busiest cpu can't be moved to this_cpu
  3331. */
  3332. if (!cpumask_test_cpu(this_cpu,
  3333. &busiest->curr->cpus_allowed)) {
  3334. spin_unlock_irqrestore(&busiest->lock, flags);
  3335. all_pinned = 1;
  3336. goto out_one_pinned;
  3337. }
  3338. if (!busiest->active_balance) {
  3339. busiest->active_balance = 1;
  3340. busiest->push_cpu = this_cpu;
  3341. active_balance = 1;
  3342. }
  3343. spin_unlock_irqrestore(&busiest->lock, flags);
  3344. if (active_balance)
  3345. wake_up_process(busiest->migration_thread);
  3346. /*
  3347. * We've kicked active balancing, reset the failure
  3348. * counter.
  3349. */
  3350. sd->nr_balance_failed = sd->cache_nice_tries+1;
  3351. }
  3352. } else
  3353. sd->nr_balance_failed = 0;
  3354. if (likely(!active_balance)) {
  3355. /* We were unbalanced, so reset the balancing interval */
  3356. sd->balance_interval = sd->min_interval;
  3357. } else {
  3358. /*
  3359. * If we've begun active balancing, start to back off. This
  3360. * case may not be covered by the all_pinned logic if there
  3361. * is only 1 task on the busy runqueue (because we don't call
  3362. * move_tasks).
  3363. */
  3364. if (sd->balance_interval < sd->max_interval)
  3365. sd->balance_interval *= 2;
  3366. }
  3367. if (!ld_moved && !sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3368. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3369. ld_moved = -1;
  3370. goto out;
  3371. out_balanced:
  3372. schedstat_inc(sd, lb_balanced[idle]);
  3373. sd->nr_balance_failed = 0;
  3374. out_one_pinned:
  3375. /* tune up the balancing interval */
  3376. if ((all_pinned && sd->balance_interval < MAX_PINNED_INTERVAL) ||
  3377. (sd->balance_interval < sd->max_interval))
  3378. sd->balance_interval *= 2;
  3379. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3380. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3381. ld_moved = -1;
  3382. else
  3383. ld_moved = 0;
  3384. out:
  3385. if (ld_moved)
  3386. update_shares(sd);
  3387. return ld_moved;
  3388. }
  3389. /*
  3390. * Check this_cpu to ensure it is balanced within domain. Attempt to move
  3391. * tasks if there is an imbalance.
  3392. *
  3393. * Called from schedule when this_rq is about to become idle (CPU_NEWLY_IDLE).
  3394. * this_rq is locked.
  3395. */
  3396. static int
  3397. load_balance_newidle(int this_cpu, struct rq *this_rq, struct sched_domain *sd)
  3398. {
  3399. struct sched_group *group;
  3400. struct rq *busiest = NULL;
  3401. unsigned long imbalance;
  3402. int ld_moved = 0;
  3403. int sd_idle = 0;
  3404. int all_pinned = 0;
  3405. struct cpumask *cpus = __get_cpu_var(load_balance_tmpmask);
  3406. cpumask_setall(cpus);
  3407. /*
  3408. * When power savings policy is enabled for the parent domain, idle
  3409. * sibling can pick up load irrespective of busy siblings. In this case,
  3410. * let the state of idle sibling percolate up as IDLE, instead of
  3411. * portraying it as CPU_NOT_IDLE.
  3412. */
  3413. if (sd->flags & SD_SHARE_CPUPOWER &&
  3414. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3415. sd_idle = 1;
  3416. schedstat_inc(sd, lb_count[CPU_NEWLY_IDLE]);
  3417. redo:
  3418. update_shares_locked(this_rq, sd);
  3419. group = find_busiest_group(sd, this_cpu, &imbalance, CPU_NEWLY_IDLE,
  3420. &sd_idle, cpus, NULL);
  3421. if (!group) {
  3422. schedstat_inc(sd, lb_nobusyg[CPU_NEWLY_IDLE]);
  3423. goto out_balanced;
  3424. }
  3425. busiest = find_busiest_queue(group, CPU_NEWLY_IDLE, imbalance, cpus);
  3426. if (!busiest) {
  3427. schedstat_inc(sd, lb_nobusyq[CPU_NEWLY_IDLE]);
  3428. goto out_balanced;
  3429. }
  3430. BUG_ON(busiest == this_rq);
  3431. schedstat_add(sd, lb_imbalance[CPU_NEWLY_IDLE], imbalance);
  3432. ld_moved = 0;
  3433. if (busiest->nr_running > 1) {
  3434. /* Attempt to move tasks */
  3435. double_lock_balance(this_rq, busiest);
  3436. /* this_rq->clock is already updated */
  3437. update_rq_clock(busiest);
  3438. ld_moved = move_tasks(this_rq, this_cpu, busiest,
  3439. imbalance, sd, CPU_NEWLY_IDLE,
  3440. &all_pinned);
  3441. double_unlock_balance(this_rq, busiest);
  3442. if (unlikely(all_pinned)) {
  3443. cpumask_clear_cpu(cpu_of(busiest), cpus);
  3444. if (!cpumask_empty(cpus))
  3445. goto redo;
  3446. }
  3447. }
  3448. if (!ld_moved) {
  3449. int active_balance = 0;
  3450. schedstat_inc(sd, lb_failed[CPU_NEWLY_IDLE]);
  3451. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3452. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3453. return -1;
  3454. if (sched_mc_power_savings < POWERSAVINGS_BALANCE_WAKEUP)
  3455. return -1;
  3456. if (sd->nr_balance_failed++ < 2)
  3457. return -1;
  3458. /*
  3459. * The only task running in a non-idle cpu can be moved to this
  3460. * cpu in an attempt to completely freeup the other CPU
  3461. * package. The same method used to move task in load_balance()
  3462. * have been extended for load_balance_newidle() to speedup
  3463. * consolidation at sched_mc=POWERSAVINGS_BALANCE_WAKEUP (2)
  3464. *
  3465. * The package power saving logic comes from
  3466. * find_busiest_group(). If there are no imbalance, then
  3467. * f_b_g() will return NULL. However when sched_mc={1,2} then
  3468. * f_b_g() will select a group from which a running task may be
  3469. * pulled to this cpu in order to make the other package idle.
  3470. * If there is no opportunity to make a package idle and if
  3471. * there are no imbalance, then f_b_g() will return NULL and no
  3472. * action will be taken in load_balance_newidle().
  3473. *
  3474. * Under normal task pull operation due to imbalance, there
  3475. * will be more than one task in the source run queue and
  3476. * move_tasks() will succeed. ld_moved will be true and this
  3477. * active balance code will not be triggered.
  3478. */
  3479. /* Lock busiest in correct order while this_rq is held */
  3480. double_lock_balance(this_rq, busiest);
  3481. /*
  3482. * don't kick the migration_thread, if the curr
  3483. * task on busiest cpu can't be moved to this_cpu
  3484. */
  3485. if (!cpumask_test_cpu(this_cpu, &busiest->curr->cpus_allowed)) {
  3486. double_unlock_balance(this_rq, busiest);
  3487. all_pinned = 1;
  3488. return ld_moved;
  3489. }
  3490. if (!busiest->active_balance) {
  3491. busiest->active_balance = 1;
  3492. busiest->push_cpu = this_cpu;
  3493. active_balance = 1;
  3494. }
  3495. double_unlock_balance(this_rq, busiest);
  3496. /*
  3497. * Should not call ttwu while holding a rq->lock
  3498. */
  3499. spin_unlock(&this_rq->lock);
  3500. if (active_balance)
  3501. wake_up_process(busiest->migration_thread);
  3502. spin_lock(&this_rq->lock);
  3503. } else
  3504. sd->nr_balance_failed = 0;
  3505. update_shares_locked(this_rq, sd);
  3506. return ld_moved;
  3507. out_balanced:
  3508. schedstat_inc(sd, lb_balanced[CPU_NEWLY_IDLE]);
  3509. if (!sd_idle && sd->flags & SD_SHARE_CPUPOWER &&
  3510. !test_sd_parent(sd, SD_POWERSAVINGS_BALANCE))
  3511. return -1;
  3512. sd->nr_balance_failed = 0;
  3513. return 0;
  3514. }
  3515. /*
  3516. * idle_balance is called by schedule() if this_cpu is about to become
  3517. * idle. Attempts to pull tasks from other CPUs.
  3518. */
  3519. static void idle_balance(int this_cpu, struct rq *this_rq)
  3520. {
  3521. struct sched_domain *sd;
  3522. int pulled_task = 0;
  3523. unsigned long next_balance = jiffies + HZ;
  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);
  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. }
  3546. /*
  3547. * active_load_balance is run by migration threads. It pushes running tasks
  3548. * off the busiest CPU onto idle CPUs. It requires at least 1 task to be
  3549. * running on each physical CPU where possible, and avoids physical /
  3550. * logical imbalances.
  3551. *
  3552. * Called with busiest_rq locked.
  3553. */
  3554. static void active_load_balance(struct rq *busiest_rq, int busiest_cpu)
  3555. {
  3556. int target_cpu = busiest_rq->push_cpu;
  3557. struct sched_domain *sd;
  3558. struct rq *target_rq;
  3559. /* Is there any task to move? */
  3560. if (busiest_rq->nr_running <= 1)
  3561. return;
  3562. target_rq = cpu_rq(target_cpu);
  3563. /*
  3564. * This condition is "impossible", if it occurs
  3565. * we need to fix it. Originally reported by
  3566. * Bjorn Helgaas on a 128-cpu setup.
  3567. */
  3568. BUG_ON(busiest_rq == target_rq);
  3569. /* move a task from busiest_rq to target_rq */
  3570. double_lock_balance(busiest_rq, target_rq);
  3571. update_rq_clock(busiest_rq);
  3572. update_rq_clock(target_rq);
  3573. /* Search for an sd spanning us and the target CPU. */
  3574. for_each_domain(target_cpu, sd) {
  3575. if ((sd->flags & SD_LOAD_BALANCE) &&
  3576. cpumask_test_cpu(busiest_cpu, sched_domain_span(sd)))
  3577. break;
  3578. }
  3579. if (likely(sd)) {
  3580. schedstat_inc(sd, alb_count);
  3581. if (move_one_task(target_rq, target_cpu, busiest_rq,
  3582. sd, CPU_IDLE))
  3583. schedstat_inc(sd, alb_pushed);
  3584. else
  3585. schedstat_inc(sd, alb_failed);
  3586. }
  3587. double_unlock_balance(busiest_rq, target_rq);
  3588. }
  3589. #ifdef CONFIG_NO_HZ
  3590. static struct {
  3591. atomic_t load_balancer;
  3592. cpumask_var_t cpu_mask;
  3593. } nohz ____cacheline_aligned = {
  3594. .load_balancer = ATOMIC_INIT(-1),
  3595. };
  3596. /*
  3597. * This routine will try to nominate the ilb (idle load balancing)
  3598. * owner among the cpus whose ticks are stopped. ilb owner will do the idle
  3599. * load balancing on behalf of all those cpus. If all the cpus in the system
  3600. * go into this tickless mode, then there will be no ilb owner (as there is
  3601. * no need for one) and all the cpus will sleep till the next wakeup event
  3602. * arrives...
  3603. *
  3604. * For the ilb owner, tick is not stopped. And this tick will be used
  3605. * for idle load balancing. ilb owner will still be part of
  3606. * nohz.cpu_mask..
  3607. *
  3608. * While stopping the tick, this cpu will become the ilb owner if there
  3609. * is no other owner. And will be the owner till that cpu becomes busy
  3610. * or if all cpus in the system stop their ticks at which point
  3611. * there is no need for ilb owner.
  3612. *
  3613. * When the ilb owner becomes busy, it nominates another owner, during the
  3614. * next busy scheduler_tick()
  3615. */
  3616. int select_nohz_load_balancer(int stop_tick)
  3617. {
  3618. int cpu = smp_processor_id();
  3619. if (stop_tick) {
  3620. cpu_rq(cpu)->in_nohz_recently = 1;
  3621. if (!cpu_active(cpu)) {
  3622. if (atomic_read(&nohz.load_balancer) != cpu)
  3623. return 0;
  3624. /*
  3625. * If we are going offline and still the leader,
  3626. * give up!
  3627. */
  3628. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  3629. BUG();
  3630. return 0;
  3631. }
  3632. cpumask_set_cpu(cpu, nohz.cpu_mask);
  3633. /* time for ilb owner also to sleep */
  3634. if (cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
  3635. if (atomic_read(&nohz.load_balancer) == cpu)
  3636. atomic_set(&nohz.load_balancer, -1);
  3637. return 0;
  3638. }
  3639. if (atomic_read(&nohz.load_balancer) == -1) {
  3640. /* make me the ilb owner */
  3641. if (atomic_cmpxchg(&nohz.load_balancer, -1, cpu) == -1)
  3642. return 1;
  3643. } else if (atomic_read(&nohz.load_balancer) == cpu)
  3644. return 1;
  3645. } else {
  3646. if (!cpumask_test_cpu(cpu, nohz.cpu_mask))
  3647. return 0;
  3648. cpumask_clear_cpu(cpu, nohz.cpu_mask);
  3649. if (atomic_read(&nohz.load_balancer) == cpu)
  3650. if (atomic_cmpxchg(&nohz.load_balancer, cpu, -1) != cpu)
  3651. BUG();
  3652. }
  3653. return 0;
  3654. }
  3655. #endif
  3656. static DEFINE_SPINLOCK(balancing);
  3657. /*
  3658. * It checks each scheduling domain to see if it is due to be balanced,
  3659. * and initiates a balancing operation if so.
  3660. *
  3661. * Balancing parameters are set up in arch_init_sched_domains.
  3662. */
  3663. static void rebalance_domains(int cpu, enum cpu_idle_type idle)
  3664. {
  3665. int balance = 1;
  3666. struct rq *rq = cpu_rq(cpu);
  3667. unsigned long interval;
  3668. struct sched_domain *sd;
  3669. /* Earliest time when we have to do rebalance again */
  3670. unsigned long next_balance = jiffies + 60*HZ;
  3671. int update_next_balance = 0;
  3672. int need_serialize;
  3673. for_each_domain(cpu, sd) {
  3674. if (!(sd->flags & SD_LOAD_BALANCE))
  3675. continue;
  3676. interval = sd->balance_interval;
  3677. if (idle != CPU_IDLE)
  3678. interval *= sd->busy_factor;
  3679. /* scale ms to jiffies */
  3680. interval = msecs_to_jiffies(interval);
  3681. if (unlikely(!interval))
  3682. interval = 1;
  3683. if (interval > HZ*NR_CPUS/10)
  3684. interval = HZ*NR_CPUS/10;
  3685. need_serialize = sd->flags & SD_SERIALIZE;
  3686. if (need_serialize) {
  3687. if (!spin_trylock(&balancing))
  3688. goto out;
  3689. }
  3690. if (time_after_eq(jiffies, sd->last_balance + interval)) {
  3691. if (load_balance(cpu, rq, sd, idle, &balance)) {
  3692. /*
  3693. * We've pulled tasks over so either we're no
  3694. * longer idle, or one of our SMT siblings is
  3695. * not idle.
  3696. */
  3697. idle = CPU_NOT_IDLE;
  3698. }
  3699. sd->last_balance = jiffies;
  3700. }
  3701. if (need_serialize)
  3702. spin_unlock(&balancing);
  3703. out:
  3704. if (time_after(next_balance, sd->last_balance + interval)) {
  3705. next_balance = sd->last_balance + interval;
  3706. update_next_balance = 1;
  3707. }
  3708. /*
  3709. * Stop the load balance at this level. There is another
  3710. * CPU in our sched group which is doing load balancing more
  3711. * actively.
  3712. */
  3713. if (!balance)
  3714. break;
  3715. }
  3716. /*
  3717. * next_balance will be updated only when there is a need.
  3718. * When the cpu is attached to null domain for ex, it will not be
  3719. * updated.
  3720. */
  3721. if (likely(update_next_balance))
  3722. rq->next_balance = next_balance;
  3723. }
  3724. /*
  3725. * run_rebalance_domains is triggered when needed from the scheduler tick.
  3726. * In CONFIG_NO_HZ case, the idle load balance owner will do the
  3727. * rebalancing for all the cpus for whom scheduler ticks are stopped.
  3728. */
  3729. static void run_rebalance_domains(struct softirq_action *h)
  3730. {
  3731. int this_cpu = smp_processor_id();
  3732. struct rq *this_rq = cpu_rq(this_cpu);
  3733. enum cpu_idle_type idle = this_rq->idle_at_tick ?
  3734. CPU_IDLE : CPU_NOT_IDLE;
  3735. rebalance_domains(this_cpu, idle);
  3736. #ifdef CONFIG_NO_HZ
  3737. /*
  3738. * If this cpu is the owner for idle load balancing, then do the
  3739. * balancing on behalf of the other idle cpus whose ticks are
  3740. * stopped.
  3741. */
  3742. if (this_rq->idle_at_tick &&
  3743. atomic_read(&nohz.load_balancer) == this_cpu) {
  3744. struct rq *rq;
  3745. int balance_cpu;
  3746. for_each_cpu(balance_cpu, nohz.cpu_mask) {
  3747. if (balance_cpu == this_cpu)
  3748. continue;
  3749. /*
  3750. * If this cpu gets work to do, stop the load balancing
  3751. * work being done for other cpus. Next load
  3752. * balancing owner will pick it up.
  3753. */
  3754. if (need_resched())
  3755. break;
  3756. rebalance_domains(balance_cpu, CPU_IDLE);
  3757. rq = cpu_rq(balance_cpu);
  3758. if (time_after(this_rq->next_balance, rq->next_balance))
  3759. this_rq->next_balance = rq->next_balance;
  3760. }
  3761. }
  3762. #endif
  3763. }
  3764. static inline int on_null_domain(int cpu)
  3765. {
  3766. return !rcu_dereference(cpu_rq(cpu)->sd);
  3767. }
  3768. /*
  3769. * Trigger the SCHED_SOFTIRQ if it is time to do periodic load balancing.
  3770. *
  3771. * In case of CONFIG_NO_HZ, this is the place where we nominate a new
  3772. * idle load balancing owner or decide to stop the periodic load balancing,
  3773. * if the whole system is idle.
  3774. */
  3775. static inline void trigger_load_balance(struct rq *rq, int cpu)
  3776. {
  3777. #ifdef CONFIG_NO_HZ
  3778. /*
  3779. * If we were in the nohz mode recently and busy at the current
  3780. * scheduler tick, then check if we need to nominate new idle
  3781. * load balancer.
  3782. */
  3783. if (rq->in_nohz_recently && !rq->idle_at_tick) {
  3784. rq->in_nohz_recently = 0;
  3785. if (atomic_read(&nohz.load_balancer) == cpu) {
  3786. cpumask_clear_cpu(cpu, nohz.cpu_mask);
  3787. atomic_set(&nohz.load_balancer, -1);
  3788. }
  3789. if (atomic_read(&nohz.load_balancer) == -1) {
  3790. /*
  3791. * simple selection for now: Nominate the
  3792. * first cpu in the nohz list to be the next
  3793. * ilb owner.
  3794. *
  3795. * TBD: Traverse the sched domains and nominate
  3796. * the nearest cpu in the nohz.cpu_mask.
  3797. */
  3798. int ilb = cpumask_first(nohz.cpu_mask);
  3799. if (ilb < nr_cpu_ids)
  3800. resched_cpu(ilb);
  3801. }
  3802. }
  3803. /*
  3804. * If this cpu is idle and doing idle load balancing for all the
  3805. * cpus with ticks stopped, is it time for that to stop?
  3806. */
  3807. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) == cpu &&
  3808. cpumask_weight(nohz.cpu_mask) == num_online_cpus()) {
  3809. resched_cpu(cpu);
  3810. return;
  3811. }
  3812. /*
  3813. * If this cpu is idle and the idle load balancing is done by
  3814. * someone else, then no need raise the SCHED_SOFTIRQ
  3815. */
  3816. if (rq->idle_at_tick && atomic_read(&nohz.load_balancer) != cpu &&
  3817. cpumask_test_cpu(cpu, nohz.cpu_mask))
  3818. return;
  3819. #endif
  3820. /* Don't need to rebalance while attached to NULL domain */
  3821. if (time_after_eq(jiffies, rq->next_balance) &&
  3822. likely(!on_null_domain(cpu)))
  3823. raise_softirq(SCHED_SOFTIRQ);
  3824. }
  3825. #else /* CONFIG_SMP */
  3826. /*
  3827. * on UP we do not need to balance between CPUs:
  3828. */
  3829. static inline void idle_balance(int cpu, struct rq *rq)
  3830. {
  3831. }
  3832. #endif
  3833. DEFINE_PER_CPU(struct kernel_stat, kstat);
  3834. EXPORT_PER_CPU_SYMBOL(kstat);
  3835. /*
  3836. * Return any ns on the sched_clock that have not yet been banked in
  3837. * @p in case that task is currently running.
  3838. */
  3839. unsigned long long task_delta_exec(struct task_struct *p)
  3840. {
  3841. unsigned long flags;
  3842. struct rq *rq;
  3843. u64 ns = 0;
  3844. rq = task_rq_lock(p, &flags);
  3845. if (task_current(rq, p)) {
  3846. u64 delta_exec;
  3847. update_rq_clock(rq);
  3848. delta_exec = rq->clock - p->se.exec_start;
  3849. if ((s64)delta_exec > 0)
  3850. ns = delta_exec;
  3851. }
  3852. task_rq_unlock(rq, &flags);
  3853. return ns;
  3854. }
  3855. /*
  3856. * Account user cpu time to a process.
  3857. * @p: the process that the cpu time gets accounted to
  3858. * @cputime: the cpu time spent in user space since the last update
  3859. * @cputime_scaled: cputime scaled by cpu frequency
  3860. */
  3861. void account_user_time(struct task_struct *p, cputime_t cputime,
  3862. cputime_t cputime_scaled)
  3863. {
  3864. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3865. cputime64_t tmp;
  3866. /* Add user time to process. */
  3867. p->utime = cputime_add(p->utime, cputime);
  3868. p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
  3869. account_group_user_time(p, cputime);
  3870. /* Add user time to cpustat. */
  3871. tmp = cputime_to_cputime64(cputime);
  3872. if (TASK_NICE(p) > 0)
  3873. cpustat->nice = cputime64_add(cpustat->nice, tmp);
  3874. else
  3875. cpustat->user = cputime64_add(cpustat->user, tmp);
  3876. /* Account for user time used */
  3877. acct_update_integrals(p);
  3878. }
  3879. /*
  3880. * Account guest cpu time to a process.
  3881. * @p: the process that the cpu time gets accounted to
  3882. * @cputime: the cpu time spent in virtual machine since the last update
  3883. * @cputime_scaled: cputime scaled by cpu frequency
  3884. */
  3885. static void account_guest_time(struct task_struct *p, cputime_t cputime,
  3886. cputime_t cputime_scaled)
  3887. {
  3888. cputime64_t tmp;
  3889. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3890. tmp = cputime_to_cputime64(cputime);
  3891. /* Add guest time to process. */
  3892. p->utime = cputime_add(p->utime, cputime);
  3893. p->utimescaled = cputime_add(p->utimescaled, cputime_scaled);
  3894. account_group_user_time(p, cputime);
  3895. p->gtime = cputime_add(p->gtime, cputime);
  3896. /* Add guest time to cpustat. */
  3897. cpustat->user = cputime64_add(cpustat->user, tmp);
  3898. cpustat->guest = cputime64_add(cpustat->guest, tmp);
  3899. }
  3900. /*
  3901. * Account system cpu time to a process.
  3902. * @p: the process that the cpu time gets accounted to
  3903. * @hardirq_offset: the offset to subtract from hardirq_count()
  3904. * @cputime: the cpu time spent in kernel space since the last update
  3905. * @cputime_scaled: cputime scaled by cpu frequency
  3906. */
  3907. void account_system_time(struct task_struct *p, int hardirq_offset,
  3908. cputime_t cputime, cputime_t cputime_scaled)
  3909. {
  3910. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3911. cputime64_t tmp;
  3912. if ((p->flags & PF_VCPU) && (irq_count() - hardirq_offset == 0)) {
  3913. account_guest_time(p, cputime, cputime_scaled);
  3914. return;
  3915. }
  3916. /* Add system time to process. */
  3917. p->stime = cputime_add(p->stime, cputime);
  3918. p->stimescaled = cputime_add(p->stimescaled, cputime_scaled);
  3919. account_group_system_time(p, cputime);
  3920. /* Add system time to cpustat. */
  3921. tmp = cputime_to_cputime64(cputime);
  3922. if (hardirq_count() - hardirq_offset)
  3923. cpustat->irq = cputime64_add(cpustat->irq, tmp);
  3924. else if (softirq_count())
  3925. cpustat->softirq = cputime64_add(cpustat->softirq, tmp);
  3926. else
  3927. cpustat->system = cputime64_add(cpustat->system, tmp);
  3928. /* Account for system time used */
  3929. acct_update_integrals(p);
  3930. }
  3931. /*
  3932. * Account for involuntary wait time.
  3933. * @steal: the cpu time spent in involuntary wait
  3934. */
  3935. void account_steal_time(cputime_t cputime)
  3936. {
  3937. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3938. cputime64_t cputime64 = cputime_to_cputime64(cputime);
  3939. cpustat->steal = cputime64_add(cpustat->steal, cputime64);
  3940. }
  3941. /*
  3942. * Account for idle time.
  3943. * @cputime: the cpu time spent in idle wait
  3944. */
  3945. void account_idle_time(cputime_t cputime)
  3946. {
  3947. struct cpu_usage_stat *cpustat = &kstat_this_cpu.cpustat;
  3948. cputime64_t cputime64 = cputime_to_cputime64(cputime);
  3949. struct rq *rq = this_rq();
  3950. if (atomic_read(&rq->nr_iowait) > 0)
  3951. cpustat->iowait = cputime64_add(cpustat->iowait, cputime64);
  3952. else
  3953. cpustat->idle = cputime64_add(cpustat->idle, cputime64);
  3954. }
  3955. #ifndef CONFIG_VIRT_CPU_ACCOUNTING
  3956. /*
  3957. * Account a single tick of cpu time.
  3958. * @p: the process that the cpu time gets accounted to
  3959. * @user_tick: indicates if the tick is a user or a system tick
  3960. */
  3961. void account_process_tick(struct task_struct *p, int user_tick)
  3962. {
  3963. cputime_t one_jiffy = jiffies_to_cputime(1);
  3964. cputime_t one_jiffy_scaled = cputime_to_scaled(one_jiffy);
  3965. struct rq *rq = this_rq();
  3966. if (user_tick)
  3967. account_user_time(p, one_jiffy, one_jiffy_scaled);
  3968. else if (p != rq->idle)
  3969. account_system_time(p, HARDIRQ_OFFSET, one_jiffy,
  3970. one_jiffy_scaled);
  3971. else
  3972. account_idle_time(one_jiffy);
  3973. }
  3974. /*
  3975. * Account multiple ticks of steal time.
  3976. * @p: the process from which the cpu time has been stolen
  3977. * @ticks: number of stolen ticks
  3978. */
  3979. void account_steal_ticks(unsigned long ticks)
  3980. {
  3981. account_steal_time(jiffies_to_cputime(ticks));
  3982. }
  3983. /*
  3984. * Account multiple ticks of idle time.
  3985. * @ticks: number of stolen ticks
  3986. */
  3987. void account_idle_ticks(unsigned long ticks)
  3988. {
  3989. account_idle_time(jiffies_to_cputime(ticks));
  3990. }
  3991. #endif
  3992. /*
  3993. * Use precise platform statistics if available:
  3994. */
  3995. #ifdef CONFIG_VIRT_CPU_ACCOUNTING
  3996. cputime_t task_utime(struct task_struct *p)
  3997. {
  3998. return p->utime;
  3999. }
  4000. cputime_t task_stime(struct task_struct *p)
  4001. {
  4002. return p->stime;
  4003. }
  4004. #else
  4005. cputime_t task_utime(struct task_struct *p)
  4006. {
  4007. clock_t utime = cputime_to_clock_t(p->utime),
  4008. total = utime + cputime_to_clock_t(p->stime);
  4009. u64 temp;
  4010. /*
  4011. * Use CFS's precise accounting:
  4012. */
  4013. temp = (u64)nsec_to_clock_t(p->se.sum_exec_runtime);
  4014. if (total) {
  4015. temp *= utime;
  4016. do_div(temp, total);
  4017. }
  4018. utime = (clock_t)temp;
  4019. p->prev_utime = max(p->prev_utime, clock_t_to_cputime(utime));
  4020. return p->prev_utime;
  4021. }
  4022. cputime_t task_stime(struct task_struct *p)
  4023. {
  4024. clock_t stime;
  4025. /*
  4026. * Use CFS's precise accounting. (we subtract utime from
  4027. * the total, to make sure the total observed by userspace
  4028. * grows monotonically - apps rely on that):
  4029. */
  4030. stime = nsec_to_clock_t(p->se.sum_exec_runtime) -
  4031. cputime_to_clock_t(task_utime(p));
  4032. if (stime >= 0)
  4033. p->prev_stime = max(p->prev_stime, clock_t_to_cputime(stime));
  4034. return p->prev_stime;
  4035. }
  4036. #endif
  4037. inline cputime_t task_gtime(struct task_struct *p)
  4038. {
  4039. return p->gtime;
  4040. }
  4041. /*
  4042. * This function gets called by the timer code, with HZ frequency.
  4043. * We call it with interrupts disabled.
  4044. *
  4045. * It also gets called by the fork code, when changing the parent's
  4046. * timeslices.
  4047. */
  4048. void scheduler_tick(void)
  4049. {
  4050. int cpu = smp_processor_id();
  4051. struct rq *rq = cpu_rq(cpu);
  4052. struct task_struct *curr = rq->curr;
  4053. sched_clock_tick();
  4054. spin_lock(&rq->lock);
  4055. update_rq_clock(rq);
  4056. update_cpu_load(rq);
  4057. curr->sched_class->task_tick(rq, curr, 0);
  4058. spin_unlock(&rq->lock);
  4059. #ifdef CONFIG_SMP
  4060. rq->idle_at_tick = idle_cpu(cpu);
  4061. trigger_load_balance(rq, cpu);
  4062. #endif
  4063. }
  4064. unsigned long get_parent_ip(unsigned long addr)
  4065. {
  4066. if (in_lock_functions(addr)) {
  4067. addr = CALLER_ADDR2;
  4068. if (in_lock_functions(addr))
  4069. addr = CALLER_ADDR3;
  4070. }
  4071. return addr;
  4072. }
  4073. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  4074. defined(CONFIG_PREEMPT_TRACER))
  4075. void __kprobes add_preempt_count(int val)
  4076. {
  4077. #ifdef CONFIG_DEBUG_PREEMPT
  4078. /*
  4079. * Underflow?
  4080. */
  4081. if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
  4082. return;
  4083. #endif
  4084. preempt_count() += val;
  4085. #ifdef CONFIG_DEBUG_PREEMPT
  4086. /*
  4087. * Spinlock count overflowing soon?
  4088. */
  4089. DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
  4090. PREEMPT_MASK - 10);
  4091. #endif
  4092. if (preempt_count() == val)
  4093. trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  4094. }
  4095. EXPORT_SYMBOL(add_preempt_count);
  4096. void __kprobes sub_preempt_count(int val)
  4097. {
  4098. #ifdef CONFIG_DEBUG_PREEMPT
  4099. /*
  4100. * Underflow?
  4101. */
  4102. if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
  4103. return;
  4104. /*
  4105. * Is the spinlock portion underflowing?
  4106. */
  4107. if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
  4108. !(preempt_count() & PREEMPT_MASK)))
  4109. return;
  4110. #endif
  4111. if (preempt_count() == val)
  4112. trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  4113. preempt_count() -= val;
  4114. }
  4115. EXPORT_SYMBOL(sub_preempt_count);
  4116. #endif
  4117. /*
  4118. * Print scheduling while atomic bug:
  4119. */
  4120. static noinline void __schedule_bug(struct task_struct *prev)
  4121. {
  4122. struct pt_regs *regs = get_irq_regs();
  4123. printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
  4124. prev->comm, prev->pid, preempt_count());
  4125. debug_show_held_locks(prev);
  4126. print_modules();
  4127. if (irqs_disabled())
  4128. print_irqtrace_events(prev);
  4129. if (regs)
  4130. show_regs(regs);
  4131. else
  4132. dump_stack();
  4133. }
  4134. /*
  4135. * Various schedule()-time debugging checks and statistics:
  4136. */
  4137. static inline void schedule_debug(struct task_struct *prev)
  4138. {
  4139. /*
  4140. * Test if we are atomic. Since do_exit() needs to call into
  4141. * schedule() atomically, we ignore that path for now.
  4142. * Otherwise, whine if we are scheduling when we should not be.
  4143. */
  4144. if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
  4145. __schedule_bug(prev);
  4146. profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  4147. schedstat_inc(this_rq(), sched_count);
  4148. #ifdef CONFIG_SCHEDSTATS
  4149. if (unlikely(prev->lock_depth >= 0)) {
  4150. schedstat_inc(this_rq(), bkl_count);
  4151. schedstat_inc(prev, sched_info.bkl_count);
  4152. }
  4153. #endif
  4154. }
  4155. static void put_prev_task(struct rq *rq, struct task_struct *prev)
  4156. {
  4157. if (prev->state == TASK_RUNNING) {
  4158. u64 runtime = prev->se.sum_exec_runtime;
  4159. runtime -= prev->se.prev_sum_exec_runtime;
  4160. runtime = min_t(u64, runtime, 2*sysctl_sched_migration_cost);
  4161. /*
  4162. * In order to avoid avg_overlap growing stale when we are
  4163. * indeed overlapping and hence not getting put to sleep, grow
  4164. * the avg_overlap on preemption.
  4165. *
  4166. * We use the average preemption runtime because that
  4167. * correlates to the amount of cache footprint a task can
  4168. * build up.
  4169. */
  4170. update_avg(&prev->se.avg_overlap, runtime);
  4171. }
  4172. prev->sched_class->put_prev_task(rq, prev);
  4173. }
  4174. /*
  4175. * Pick up the highest-prio task:
  4176. */
  4177. static inline struct task_struct *
  4178. pick_next_task(struct rq *rq)
  4179. {
  4180. const struct sched_class *class;
  4181. struct task_struct *p;
  4182. /*
  4183. * Optimization: we know that if all tasks are in
  4184. * the fair class we can call that function directly:
  4185. */
  4186. if (likely(rq->nr_running == rq->cfs.nr_running)) {
  4187. p = fair_sched_class.pick_next_task(rq);
  4188. if (likely(p))
  4189. return p;
  4190. }
  4191. class = sched_class_highest;
  4192. for ( ; ; ) {
  4193. p = class->pick_next_task(rq);
  4194. if (p)
  4195. return p;
  4196. /*
  4197. * Will never be NULL as the idle class always
  4198. * returns a non-NULL p:
  4199. */
  4200. class = class->next;
  4201. }
  4202. }
  4203. /*
  4204. * schedule() is the main scheduler function.
  4205. */
  4206. asmlinkage void __sched __schedule(void)
  4207. {
  4208. struct task_struct *prev, *next;
  4209. unsigned long *switch_count;
  4210. struct rq *rq;
  4211. int cpu;
  4212. cpu = smp_processor_id();
  4213. rq = cpu_rq(cpu);
  4214. rcu_qsctr_inc(cpu);
  4215. prev = rq->curr;
  4216. switch_count = &prev->nivcsw;
  4217. release_kernel_lock(prev);
  4218. need_resched_nonpreemptible:
  4219. schedule_debug(prev);
  4220. if (sched_feat(HRTICK))
  4221. hrtick_clear(rq);
  4222. spin_lock_irq(&rq->lock);
  4223. update_rq_clock(rq);
  4224. clear_tsk_need_resched(prev);
  4225. if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  4226. if (unlikely(signal_pending_state(prev->state, prev)))
  4227. prev->state = TASK_RUNNING;
  4228. else
  4229. deactivate_task(rq, prev, 1);
  4230. switch_count = &prev->nvcsw;
  4231. }
  4232. #ifdef CONFIG_SMP
  4233. if (prev->sched_class->pre_schedule)
  4234. prev->sched_class->pre_schedule(rq, prev);
  4235. #endif
  4236. if (unlikely(!rq->nr_running))
  4237. idle_balance(cpu, rq);
  4238. put_prev_task(rq, prev);
  4239. next = pick_next_task(rq);
  4240. if (likely(prev != next)) {
  4241. sched_info_switch(prev, next);
  4242. rq->nr_switches++;
  4243. rq->curr = next;
  4244. ++*switch_count;
  4245. context_switch(rq, prev, next); /* unlocks the rq */
  4246. /*
  4247. * the context switch might have flipped the stack from under
  4248. * us, hence refresh the local variables.
  4249. */
  4250. cpu = smp_processor_id();
  4251. rq = cpu_rq(cpu);
  4252. } else
  4253. spin_unlock_irq(&rq->lock);
  4254. if (unlikely(reacquire_kernel_lock(current) < 0))
  4255. goto need_resched_nonpreemptible;
  4256. }
  4257. asmlinkage void __sched schedule(void)
  4258. {
  4259. need_resched:
  4260. preempt_disable();
  4261. __schedule();
  4262. preempt_enable_no_resched();
  4263. if (unlikely(test_thread_flag(TIF_NEED_RESCHED)))
  4264. goto need_resched;
  4265. }
  4266. EXPORT_SYMBOL(schedule);
  4267. #ifdef CONFIG_SMP
  4268. /*
  4269. * Look out! "owner" is an entirely speculative pointer
  4270. * access and not reliable.
  4271. */
  4272. int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
  4273. {
  4274. unsigned int cpu;
  4275. struct rq *rq;
  4276. if (!sched_feat(OWNER_SPIN))
  4277. return 0;
  4278. #ifdef CONFIG_DEBUG_PAGEALLOC
  4279. /*
  4280. * Need to access the cpu field knowing that
  4281. * DEBUG_PAGEALLOC could have unmapped it if
  4282. * the mutex owner just released it and exited.
  4283. */
  4284. if (probe_kernel_address(&owner->cpu, cpu))
  4285. goto out;
  4286. #else
  4287. cpu = owner->cpu;
  4288. #endif
  4289. /*
  4290. * Even if the access succeeded (likely case),
  4291. * the cpu field may no longer be valid.
  4292. */
  4293. if (cpu >= nr_cpumask_bits)
  4294. goto out;
  4295. /*
  4296. * We need to validate that we can do a
  4297. * get_cpu() and that we have the percpu area.
  4298. */
  4299. if (!cpu_online(cpu))
  4300. goto out;
  4301. rq = cpu_rq(cpu);
  4302. for (;;) {
  4303. /*
  4304. * Owner changed, break to re-assess state.
  4305. */
  4306. if (lock->owner != owner)
  4307. break;
  4308. /*
  4309. * Is that owner really running on that cpu?
  4310. */
  4311. if (task_thread_info(rq->curr) != owner || need_resched())
  4312. return 0;
  4313. cpu_relax();
  4314. }
  4315. out:
  4316. return 1;
  4317. }
  4318. #endif
  4319. #ifdef CONFIG_PREEMPT
  4320. /*
  4321. * this is the entry point to schedule() from in-kernel preemption
  4322. * off of preempt_enable. Kernel preemptions off return from interrupt
  4323. * occur there and call schedule directly.
  4324. */
  4325. asmlinkage void __sched preempt_schedule(void)
  4326. {
  4327. struct thread_info *ti = current_thread_info();
  4328. /*
  4329. * If there is a non-zero preempt_count or interrupts are disabled,
  4330. * we do not want to preempt the current task. Just return..
  4331. */
  4332. if (likely(ti->preempt_count || irqs_disabled()))
  4333. return;
  4334. do {
  4335. add_preempt_count(PREEMPT_ACTIVE);
  4336. schedule();
  4337. sub_preempt_count(PREEMPT_ACTIVE);
  4338. /*
  4339. * Check again in case we missed a preemption opportunity
  4340. * between schedule and now.
  4341. */
  4342. barrier();
  4343. } while (need_resched());
  4344. }
  4345. EXPORT_SYMBOL(preempt_schedule);
  4346. /*
  4347. * this is the entry point to schedule() from kernel preemption
  4348. * off of irq context.
  4349. * Note, that this is called and return with irqs disabled. This will
  4350. * protect us against recursive calling from irq.
  4351. */
  4352. asmlinkage void __sched preempt_schedule_irq(void)
  4353. {
  4354. struct thread_info *ti = current_thread_info();
  4355. /* Catch callers which need to be fixed */
  4356. BUG_ON(ti->preempt_count || !irqs_disabled());
  4357. do {
  4358. add_preempt_count(PREEMPT_ACTIVE);
  4359. local_irq_enable();
  4360. schedule();
  4361. local_irq_disable();
  4362. sub_preempt_count(PREEMPT_ACTIVE);
  4363. /*
  4364. * Check again in case we missed a preemption opportunity
  4365. * between schedule and now.
  4366. */
  4367. barrier();
  4368. } while (need_resched());
  4369. }
  4370. #endif /* CONFIG_PREEMPT */
  4371. int default_wake_function(wait_queue_t *curr, unsigned mode, int sync,
  4372. void *key)
  4373. {
  4374. return try_to_wake_up(curr->private, mode, sync);
  4375. }
  4376. EXPORT_SYMBOL(default_wake_function);
  4377. /*
  4378. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  4379. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  4380. * number) then we wake all the non-exclusive tasks and one exclusive task.
  4381. *
  4382. * There are circumstances in which we can try to wake a task which has already
  4383. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  4384. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  4385. */
  4386. void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  4387. int nr_exclusive, int sync, void *key)
  4388. {
  4389. wait_queue_t *curr, *next;
  4390. list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
  4391. unsigned flags = curr->flags;
  4392. if (curr->func(curr, mode, sync, key) &&
  4393. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  4394. break;
  4395. }
  4396. }
  4397. /**
  4398. * __wake_up - wake up threads blocked on a waitqueue.
  4399. * @q: the waitqueue
  4400. * @mode: which threads
  4401. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  4402. * @key: is directly passed to the wakeup function
  4403. */
  4404. void __wake_up(wait_queue_head_t *q, unsigned int mode,
  4405. int nr_exclusive, void *key)
  4406. {
  4407. unsigned long flags;
  4408. spin_lock_irqsave(&q->lock, flags);
  4409. __wake_up_common(q, mode, nr_exclusive, 0, key);
  4410. spin_unlock_irqrestore(&q->lock, flags);
  4411. }
  4412. EXPORT_SYMBOL(__wake_up);
  4413. /*
  4414. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  4415. */
  4416. void __wake_up_locked(wait_queue_head_t *q, unsigned int mode)
  4417. {
  4418. __wake_up_common(q, mode, 1, 0, NULL);
  4419. }
  4420. void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
  4421. {
  4422. __wake_up_common(q, mode, 1, 0, key);
  4423. }
  4424. /**
  4425. * __wake_up_sync_key - wake up threads blocked on a waitqueue.
  4426. * @q: the waitqueue
  4427. * @mode: which threads
  4428. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  4429. * @key: opaque value to be passed to wakeup targets
  4430. *
  4431. * The sync wakeup differs that the waker knows that it will schedule
  4432. * away soon, so while the target thread will be woken up, it will not
  4433. * be migrated to another CPU - ie. the two threads are 'synchronized'
  4434. * with each other. This can prevent needless bouncing between CPUs.
  4435. *
  4436. * On UP it can prevent extra preemption.
  4437. */
  4438. void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
  4439. int nr_exclusive, void *key)
  4440. {
  4441. unsigned long flags;
  4442. int sync = 1;
  4443. if (unlikely(!q))
  4444. return;
  4445. if (unlikely(!nr_exclusive))
  4446. sync = 0;
  4447. spin_lock_irqsave(&q->lock, flags);
  4448. __wake_up_common(q, mode, nr_exclusive, sync, key);
  4449. spin_unlock_irqrestore(&q->lock, flags);
  4450. }
  4451. EXPORT_SYMBOL_GPL(__wake_up_sync_key);
  4452. /*
  4453. * __wake_up_sync - see __wake_up_sync_key()
  4454. */
  4455. void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  4456. {
  4457. __wake_up_sync_key(q, mode, nr_exclusive, NULL);
  4458. }
  4459. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  4460. /**
  4461. * complete: - signals a single thread waiting on this completion
  4462. * @x: holds the state of this particular completion
  4463. *
  4464. * This will wake up a single thread waiting on this completion. Threads will be
  4465. * awakened in the same order in which they were queued.
  4466. *
  4467. * See also complete_all(), wait_for_completion() and related routines.
  4468. */
  4469. void complete(struct completion *x)
  4470. {
  4471. unsigned long flags;
  4472. spin_lock_irqsave(&x->wait.lock, flags);
  4473. x->done++;
  4474. __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
  4475. spin_unlock_irqrestore(&x->wait.lock, flags);
  4476. }
  4477. EXPORT_SYMBOL(complete);
  4478. /**
  4479. * complete_all: - signals all threads waiting on this completion
  4480. * @x: holds the state of this particular completion
  4481. *
  4482. * This will wake up all threads waiting on this particular completion event.
  4483. */
  4484. void complete_all(struct completion *x)
  4485. {
  4486. unsigned long flags;
  4487. spin_lock_irqsave(&x->wait.lock, flags);
  4488. x->done += UINT_MAX/2;
  4489. __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
  4490. spin_unlock_irqrestore(&x->wait.lock, flags);
  4491. }
  4492. EXPORT_SYMBOL(complete_all);
  4493. static inline long __sched
  4494. do_wait_for_common(struct completion *x, long timeout, int state)
  4495. {
  4496. if (!x->done) {
  4497. DECLARE_WAITQUEUE(wait, current);
  4498. wait.flags |= WQ_FLAG_EXCLUSIVE;
  4499. __add_wait_queue_tail(&x->wait, &wait);
  4500. do {
  4501. if (signal_pending_state(state, current)) {
  4502. timeout = -ERESTARTSYS;
  4503. break;
  4504. }
  4505. __set_current_state(state);
  4506. spin_unlock_irq(&x->wait.lock);
  4507. timeout = schedule_timeout(timeout);
  4508. spin_lock_irq(&x->wait.lock);
  4509. } while (!x->done && timeout);
  4510. __remove_wait_queue(&x->wait, &wait);
  4511. if (!x->done)
  4512. return timeout;
  4513. }
  4514. x->done--;
  4515. return timeout ?: 1;
  4516. }
  4517. static long __sched
  4518. wait_for_common(struct completion *x, long timeout, int state)
  4519. {
  4520. might_sleep();
  4521. spin_lock_irq(&x->wait.lock);
  4522. timeout = do_wait_for_common(x, timeout, state);
  4523. spin_unlock_irq(&x->wait.lock);
  4524. return timeout;
  4525. }
  4526. /**
  4527. * wait_for_completion: - waits for completion of a task
  4528. * @x: holds the state of this particular completion
  4529. *
  4530. * This waits to be signaled for completion of a specific task. It is NOT
  4531. * interruptible and there is no timeout.
  4532. *
  4533. * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
  4534. * and interrupt capability. Also see complete().
  4535. */
  4536. void __sched wait_for_completion(struct completion *x)
  4537. {
  4538. wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
  4539. }
  4540. EXPORT_SYMBOL(wait_for_completion);
  4541. /**
  4542. * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
  4543. * @x: holds the state of this particular completion
  4544. * @timeout: timeout value in jiffies
  4545. *
  4546. * This waits for either a completion of a specific task to be signaled or for a
  4547. * specified timeout to expire. The timeout is in jiffies. It is not
  4548. * interruptible.
  4549. */
  4550. unsigned long __sched
  4551. wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  4552. {
  4553. return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
  4554. }
  4555. EXPORT_SYMBOL(wait_for_completion_timeout);
  4556. /**
  4557. * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
  4558. * @x: holds the state of this particular completion
  4559. *
  4560. * This waits for completion of a specific task to be signaled. It is
  4561. * interruptible.
  4562. */
  4563. int __sched wait_for_completion_interruptible(struct completion *x)
  4564. {
  4565. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
  4566. if (t == -ERESTARTSYS)
  4567. return t;
  4568. return 0;
  4569. }
  4570. EXPORT_SYMBOL(wait_for_completion_interruptible);
  4571. /**
  4572. * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
  4573. * @x: holds the state of this particular completion
  4574. * @timeout: timeout value in jiffies
  4575. *
  4576. * This waits for either a completion of a specific task to be signaled or for a
  4577. * specified timeout to expire. It is interruptible. The timeout is in jiffies.
  4578. */
  4579. unsigned long __sched
  4580. wait_for_completion_interruptible_timeout(struct completion *x,
  4581. unsigned long timeout)
  4582. {
  4583. return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
  4584. }
  4585. EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  4586. /**
  4587. * wait_for_completion_killable: - waits for completion of a task (killable)
  4588. * @x: holds the state of this particular completion
  4589. *
  4590. * This waits to be signaled for completion of a specific task. It can be
  4591. * interrupted by a kill signal.
  4592. */
  4593. int __sched wait_for_completion_killable(struct completion *x)
  4594. {
  4595. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
  4596. if (t == -ERESTARTSYS)
  4597. return t;
  4598. return 0;
  4599. }
  4600. EXPORT_SYMBOL(wait_for_completion_killable);
  4601. /**
  4602. * try_wait_for_completion - try to decrement a completion without blocking
  4603. * @x: completion structure
  4604. *
  4605. * Returns: 0 if a decrement cannot be done without blocking
  4606. * 1 if a decrement succeeded.
  4607. *
  4608. * If a completion is being used as a counting completion,
  4609. * attempt to decrement the counter without blocking. This
  4610. * enables us to avoid waiting if the resource the completion
  4611. * is protecting is not available.
  4612. */
  4613. bool try_wait_for_completion(struct completion *x)
  4614. {
  4615. int ret = 1;
  4616. spin_lock_irq(&x->wait.lock);
  4617. if (!x->done)
  4618. ret = 0;
  4619. else
  4620. x->done--;
  4621. spin_unlock_irq(&x->wait.lock);
  4622. return ret;
  4623. }
  4624. EXPORT_SYMBOL(try_wait_for_completion);
  4625. /**
  4626. * completion_done - Test to see if a completion has any waiters
  4627. * @x: completion structure
  4628. *
  4629. * Returns: 0 if there are waiters (wait_for_completion() in progress)
  4630. * 1 if there are no waiters.
  4631. *
  4632. */
  4633. bool completion_done(struct completion *x)
  4634. {
  4635. int ret = 1;
  4636. spin_lock_irq(&x->wait.lock);
  4637. if (!x->done)
  4638. ret = 0;
  4639. spin_unlock_irq(&x->wait.lock);
  4640. return ret;
  4641. }
  4642. EXPORT_SYMBOL(completion_done);
  4643. static long __sched
  4644. sleep_on_common(wait_queue_head_t *q, int state, long timeout)
  4645. {
  4646. unsigned long flags;
  4647. wait_queue_t wait;
  4648. init_waitqueue_entry(&wait, current);
  4649. __set_current_state(state);
  4650. spin_lock_irqsave(&q->lock, flags);
  4651. __add_wait_queue(q, &wait);
  4652. spin_unlock(&q->lock);
  4653. timeout = schedule_timeout(timeout);
  4654. spin_lock_irq(&q->lock);
  4655. __remove_wait_queue(q, &wait);
  4656. spin_unlock_irqrestore(&q->lock, flags);
  4657. return timeout;
  4658. }
  4659. void __sched interruptible_sleep_on(wait_queue_head_t *q)
  4660. {
  4661. sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  4662. }
  4663. EXPORT_SYMBOL(interruptible_sleep_on);
  4664. long __sched
  4665. interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
  4666. {
  4667. return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
  4668. }
  4669. EXPORT_SYMBOL(interruptible_sleep_on_timeout);
  4670. void __sched sleep_on(wait_queue_head_t *q)
  4671. {
  4672. sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  4673. }
  4674. EXPORT_SYMBOL(sleep_on);
  4675. long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
  4676. {
  4677. return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
  4678. }
  4679. EXPORT_SYMBOL(sleep_on_timeout);
  4680. #ifdef CONFIG_RT_MUTEXES
  4681. /*
  4682. * rt_mutex_setprio - set the current priority of a task
  4683. * @p: task
  4684. * @prio: prio value (kernel-internal form)
  4685. *
  4686. * This function changes the 'effective' priority of a task. It does
  4687. * not touch ->normal_prio like __setscheduler().
  4688. *
  4689. * Used by the rt_mutex code to implement priority inheritance logic.
  4690. */
  4691. void rt_mutex_setprio(struct task_struct *p, int prio)
  4692. {
  4693. unsigned long flags;
  4694. int oldprio, on_rq, running;
  4695. struct rq *rq;
  4696. const struct sched_class *prev_class = p->sched_class;
  4697. BUG_ON(prio < 0 || prio > MAX_PRIO);
  4698. rq = task_rq_lock(p, &flags);
  4699. update_rq_clock(rq);
  4700. oldprio = p->prio;
  4701. on_rq = p->se.on_rq;
  4702. running = task_current(rq, p);
  4703. if (on_rq)
  4704. dequeue_task(rq, p, 0);
  4705. if (running)
  4706. p->sched_class->put_prev_task(rq, p);
  4707. if (rt_prio(prio))
  4708. p->sched_class = &rt_sched_class;
  4709. else
  4710. p->sched_class = &fair_sched_class;
  4711. p->prio = prio;
  4712. if (running)
  4713. p->sched_class->set_curr_task(rq);
  4714. if (on_rq) {
  4715. enqueue_task(rq, p, 0);
  4716. check_class_changed(rq, p, prev_class, oldprio, running);
  4717. }
  4718. task_rq_unlock(rq, &flags);
  4719. }
  4720. #endif
  4721. void set_user_nice(struct task_struct *p, long nice)
  4722. {
  4723. int old_prio, delta, on_rq;
  4724. unsigned long flags;
  4725. struct rq *rq;
  4726. if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
  4727. return;
  4728. /*
  4729. * We have to be careful, if called from sys_setpriority(),
  4730. * the task might be in the middle of scheduling on another CPU.
  4731. */
  4732. rq = task_rq_lock(p, &flags);
  4733. update_rq_clock(rq);
  4734. /*
  4735. * The RT priorities are set via sched_setscheduler(), but we still
  4736. * allow the 'normal' nice value to be set - but as expected
  4737. * it wont have any effect on scheduling until the task is
  4738. * SCHED_FIFO/SCHED_RR:
  4739. */
  4740. if (task_has_rt_policy(p)) {
  4741. p->static_prio = NICE_TO_PRIO(nice);
  4742. goto out_unlock;
  4743. }
  4744. on_rq = p->se.on_rq;
  4745. if (on_rq)
  4746. dequeue_task(rq, p, 0);
  4747. p->static_prio = NICE_TO_PRIO(nice);
  4748. set_load_weight(p);
  4749. old_prio = p->prio;
  4750. p->prio = effective_prio(p);
  4751. delta = p->prio - old_prio;
  4752. if (on_rq) {
  4753. enqueue_task(rq, p, 0);
  4754. /*
  4755. * If the task increased its priority or is running and
  4756. * lowered its priority, then reschedule its CPU:
  4757. */
  4758. if (delta < 0 || (delta > 0 && task_running(rq, p)))
  4759. resched_task(rq->curr);
  4760. }
  4761. out_unlock:
  4762. task_rq_unlock(rq, &flags);
  4763. }
  4764. EXPORT_SYMBOL(set_user_nice);
  4765. /*
  4766. * can_nice - check if a task can reduce its nice value
  4767. * @p: task
  4768. * @nice: nice value
  4769. */
  4770. int can_nice(const struct task_struct *p, const int nice)
  4771. {
  4772. /* convert nice value [19,-20] to rlimit style value [1,40] */
  4773. int nice_rlim = 20 - nice;
  4774. return (nice_rlim <= p->signal->rlim[RLIMIT_NICE].rlim_cur ||
  4775. capable(CAP_SYS_NICE));
  4776. }
  4777. #ifdef __ARCH_WANT_SYS_NICE
  4778. /*
  4779. * sys_nice - change the priority of the current process.
  4780. * @increment: priority increment
  4781. *
  4782. * sys_setpriority is a more generic, but much slower function that
  4783. * does similar things.
  4784. */
  4785. SYSCALL_DEFINE1(nice, int, increment)
  4786. {
  4787. long nice, retval;
  4788. /*
  4789. * Setpriority might change our priority at the same moment.
  4790. * We don't have to worry. Conceptually one call occurs first
  4791. * and we have a single winner.
  4792. */
  4793. if (increment < -40)
  4794. increment = -40;
  4795. if (increment > 40)
  4796. increment = 40;
  4797. nice = TASK_NICE(current) + increment;
  4798. if (nice < -20)
  4799. nice = -20;
  4800. if (nice > 19)
  4801. nice = 19;
  4802. if (increment < 0 && !can_nice(current, nice))
  4803. return -EPERM;
  4804. retval = security_task_setnice(current, nice);
  4805. if (retval)
  4806. return retval;
  4807. set_user_nice(current, nice);
  4808. return 0;
  4809. }
  4810. #endif
  4811. /**
  4812. * task_prio - return the priority value of a given task.
  4813. * @p: the task in question.
  4814. *
  4815. * This is the priority value as seen by users in /proc.
  4816. * RT tasks are offset by -200. Normal tasks are centered
  4817. * around 0, value goes from -16 to +15.
  4818. */
  4819. int task_prio(const struct task_struct *p)
  4820. {
  4821. return p->prio - MAX_RT_PRIO;
  4822. }
  4823. /**
  4824. * task_nice - return the nice value of a given task.
  4825. * @p: the task in question.
  4826. */
  4827. int task_nice(const struct task_struct *p)
  4828. {
  4829. return TASK_NICE(p);
  4830. }
  4831. EXPORT_SYMBOL(task_nice);
  4832. /**
  4833. * idle_cpu - is a given cpu idle currently?
  4834. * @cpu: the processor in question.
  4835. */
  4836. int idle_cpu(int cpu)
  4837. {
  4838. return cpu_curr(cpu) == cpu_rq(cpu)->idle;
  4839. }
  4840. /**
  4841. * idle_task - return the idle task for a given cpu.
  4842. * @cpu: the processor in question.
  4843. */
  4844. struct task_struct *idle_task(int cpu)
  4845. {
  4846. return cpu_rq(cpu)->idle;
  4847. }
  4848. /**
  4849. * find_process_by_pid - find a process with a matching PID value.
  4850. * @pid: the pid in question.
  4851. */
  4852. static struct task_struct *find_process_by_pid(pid_t pid)
  4853. {
  4854. return pid ? find_task_by_vpid(pid) : current;
  4855. }
  4856. /* Actually do priority change: must hold rq lock. */
  4857. static void
  4858. __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
  4859. {
  4860. BUG_ON(p->se.on_rq);
  4861. p->policy = policy;
  4862. switch (p->policy) {
  4863. case SCHED_NORMAL:
  4864. case SCHED_BATCH:
  4865. case SCHED_IDLE:
  4866. p->sched_class = &fair_sched_class;
  4867. break;
  4868. case SCHED_FIFO:
  4869. case SCHED_RR:
  4870. p->sched_class = &rt_sched_class;
  4871. break;
  4872. }
  4873. p->rt_priority = prio;
  4874. p->normal_prio = normal_prio(p);
  4875. /* we are holding p->pi_lock already */
  4876. p->prio = rt_mutex_getprio(p);
  4877. set_load_weight(p);
  4878. }
  4879. /*
  4880. * check the target process has a UID that matches the current process's
  4881. */
  4882. static bool check_same_owner(struct task_struct *p)
  4883. {
  4884. const struct cred *cred = current_cred(), *pcred;
  4885. bool match;
  4886. rcu_read_lock();
  4887. pcred = __task_cred(p);
  4888. match = (cred->euid == pcred->euid ||
  4889. cred->euid == pcred->uid);
  4890. rcu_read_unlock();
  4891. return match;
  4892. }
  4893. static int __sched_setscheduler(struct task_struct *p, int policy,
  4894. struct sched_param *param, bool user)
  4895. {
  4896. int retval, oldprio, oldpolicy = -1, on_rq, running;
  4897. unsigned long flags;
  4898. const struct sched_class *prev_class = p->sched_class;
  4899. struct rq *rq;
  4900. /* may grab non-irq protected spin_locks */
  4901. BUG_ON(in_interrupt());
  4902. recheck:
  4903. /* double check policy once rq lock held */
  4904. if (policy < 0)
  4905. policy = oldpolicy = p->policy;
  4906. else if (policy != SCHED_FIFO && policy != SCHED_RR &&
  4907. policy != SCHED_NORMAL && policy != SCHED_BATCH &&
  4908. policy != SCHED_IDLE)
  4909. return -EINVAL;
  4910. /*
  4911. * Valid priorities for SCHED_FIFO and SCHED_RR are
  4912. * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
  4913. * SCHED_BATCH and SCHED_IDLE is 0.
  4914. */
  4915. if (param->sched_priority < 0 ||
  4916. (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
  4917. (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
  4918. return -EINVAL;
  4919. if (rt_policy(policy) != (param->sched_priority != 0))
  4920. return -EINVAL;
  4921. /*
  4922. * Allow unprivileged RT tasks to decrease priority:
  4923. */
  4924. if (user && !capable(CAP_SYS_NICE)) {
  4925. if (rt_policy(policy)) {
  4926. unsigned long rlim_rtprio;
  4927. if (!lock_task_sighand(p, &flags))
  4928. return -ESRCH;
  4929. rlim_rtprio = p->signal->rlim[RLIMIT_RTPRIO].rlim_cur;
  4930. unlock_task_sighand(p, &flags);
  4931. /* can't set/change the rt policy */
  4932. if (policy != p->policy && !rlim_rtprio)
  4933. return -EPERM;
  4934. /* can't increase priority */
  4935. if (param->sched_priority > p->rt_priority &&
  4936. param->sched_priority > rlim_rtprio)
  4937. return -EPERM;
  4938. }
  4939. /*
  4940. * Like positive nice levels, dont allow tasks to
  4941. * move out of SCHED_IDLE either:
  4942. */
  4943. if (p->policy == SCHED_IDLE && policy != SCHED_IDLE)
  4944. return -EPERM;
  4945. /* can't change other user's priorities */
  4946. if (!check_same_owner(p))
  4947. return -EPERM;
  4948. }
  4949. if (user) {
  4950. #ifdef CONFIG_RT_GROUP_SCHED
  4951. /*
  4952. * Do not allow realtime tasks into groups that have no runtime
  4953. * assigned.
  4954. */
  4955. if (rt_bandwidth_enabled() && rt_policy(policy) &&
  4956. task_group(p)->rt_bandwidth.rt_runtime == 0)
  4957. return -EPERM;
  4958. #endif
  4959. retval = security_task_setscheduler(p, policy, param);
  4960. if (retval)
  4961. return retval;
  4962. }
  4963. /*
  4964. * make sure no PI-waiters arrive (or leave) while we are
  4965. * changing the priority of the task:
  4966. */
  4967. spin_lock_irqsave(&p->pi_lock, flags);
  4968. /*
  4969. * To be able to change p->policy safely, the apropriate
  4970. * runqueue lock must be held.
  4971. */
  4972. rq = __task_rq_lock(p);
  4973. /* recheck policy now with rq lock held */
  4974. if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  4975. policy = oldpolicy = -1;
  4976. __task_rq_unlock(rq);
  4977. spin_unlock_irqrestore(&p->pi_lock, flags);
  4978. goto recheck;
  4979. }
  4980. update_rq_clock(rq);
  4981. on_rq = p->se.on_rq;
  4982. running = task_current(rq, p);
  4983. if (on_rq)
  4984. deactivate_task(rq, p, 0);
  4985. if (running)
  4986. p->sched_class->put_prev_task(rq, p);
  4987. oldprio = p->prio;
  4988. __setscheduler(rq, p, policy, param->sched_priority);
  4989. if (running)
  4990. p->sched_class->set_curr_task(rq);
  4991. if (on_rq) {
  4992. activate_task(rq, p, 0);
  4993. check_class_changed(rq, p, prev_class, oldprio, running);
  4994. }
  4995. __task_rq_unlock(rq);
  4996. spin_unlock_irqrestore(&p->pi_lock, flags);
  4997. rt_mutex_adjust_pi(p);
  4998. return 0;
  4999. }
  5000. /**
  5001. * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
  5002. * @p: the task in question.
  5003. * @policy: new policy.
  5004. * @param: structure containing the new RT priority.
  5005. *
  5006. * NOTE that the task may be already dead.
  5007. */
  5008. int sched_setscheduler(struct task_struct *p, int policy,
  5009. struct sched_param *param)
  5010. {
  5011. return __sched_setscheduler(p, policy, param, true);
  5012. }
  5013. EXPORT_SYMBOL_GPL(sched_setscheduler);
  5014. /**
  5015. * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
  5016. * @p: the task in question.
  5017. * @policy: new policy.
  5018. * @param: structure containing the new RT priority.
  5019. *
  5020. * Just like sched_setscheduler, only don't bother checking if the
  5021. * current context has permission. For example, this is needed in
  5022. * stop_machine(): we create temporary high priority worker threads,
  5023. * but our caller might not have that capability.
  5024. */
  5025. int sched_setscheduler_nocheck(struct task_struct *p, int policy,
  5026. struct sched_param *param)
  5027. {
  5028. return __sched_setscheduler(p, policy, param, false);
  5029. }
  5030. static int
  5031. do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  5032. {
  5033. struct sched_param lparam;
  5034. struct task_struct *p;
  5035. int retval;
  5036. if (!param || pid < 0)
  5037. return -EINVAL;
  5038. if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  5039. return -EFAULT;
  5040. rcu_read_lock();
  5041. retval = -ESRCH;
  5042. p = find_process_by_pid(pid);
  5043. if (p != NULL)
  5044. retval = sched_setscheduler(p, policy, &lparam);
  5045. rcu_read_unlock();
  5046. return retval;
  5047. }
  5048. /**
  5049. * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  5050. * @pid: the pid in question.
  5051. * @policy: new policy.
  5052. * @param: structure containing the new RT priority.
  5053. */
  5054. SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
  5055. struct sched_param __user *, param)
  5056. {
  5057. /* negative values for policy are not valid */
  5058. if (policy < 0)
  5059. return -EINVAL;
  5060. return do_sched_setscheduler(pid, policy, param);
  5061. }
  5062. /**
  5063. * sys_sched_setparam - set/change the RT priority of a thread
  5064. * @pid: the pid in question.
  5065. * @param: structure containing the new RT priority.
  5066. */
  5067. SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
  5068. {
  5069. return do_sched_setscheduler(pid, -1, param);
  5070. }
  5071. /**
  5072. * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  5073. * @pid: the pid in question.
  5074. */
  5075. SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
  5076. {
  5077. struct task_struct *p;
  5078. int retval;
  5079. if (pid < 0)
  5080. return -EINVAL;
  5081. retval = -ESRCH;
  5082. read_lock(&tasklist_lock);
  5083. p = find_process_by_pid(pid);
  5084. if (p) {
  5085. retval = security_task_getscheduler(p);
  5086. if (!retval)
  5087. retval = p->policy;
  5088. }
  5089. read_unlock(&tasklist_lock);
  5090. return retval;
  5091. }
  5092. /**
  5093. * sys_sched_getscheduler - get the RT priority of a thread
  5094. * @pid: the pid in question.
  5095. * @param: structure containing the RT priority.
  5096. */
  5097. SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
  5098. {
  5099. struct sched_param lp;
  5100. struct task_struct *p;
  5101. int retval;
  5102. if (!param || pid < 0)
  5103. return -EINVAL;
  5104. read_lock(&tasklist_lock);
  5105. p = find_process_by_pid(pid);
  5106. retval = -ESRCH;
  5107. if (!p)
  5108. goto out_unlock;
  5109. retval = security_task_getscheduler(p);
  5110. if (retval)
  5111. goto out_unlock;
  5112. lp.sched_priority = p->rt_priority;
  5113. read_unlock(&tasklist_lock);
  5114. /*
  5115. * This one might sleep, we cannot do it with a spinlock held ...
  5116. */
  5117. retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  5118. return retval;
  5119. out_unlock:
  5120. read_unlock(&tasklist_lock);
  5121. return retval;
  5122. }
  5123. long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
  5124. {
  5125. cpumask_var_t cpus_allowed, new_mask;
  5126. struct task_struct *p;
  5127. int retval;
  5128. get_online_cpus();
  5129. read_lock(&tasklist_lock);
  5130. p = find_process_by_pid(pid);
  5131. if (!p) {
  5132. read_unlock(&tasklist_lock);
  5133. put_online_cpus();
  5134. return -ESRCH;
  5135. }
  5136. /*
  5137. * It is not safe to call set_cpus_allowed with the
  5138. * tasklist_lock held. We will bump the task_struct's
  5139. * usage count and then drop tasklist_lock.
  5140. */
  5141. get_task_struct(p);
  5142. read_unlock(&tasklist_lock);
  5143. if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
  5144. retval = -ENOMEM;
  5145. goto out_put_task;
  5146. }
  5147. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
  5148. retval = -ENOMEM;
  5149. goto out_free_cpus_allowed;
  5150. }
  5151. retval = -EPERM;
  5152. if (!check_same_owner(p) && !capable(CAP_SYS_NICE))
  5153. goto out_unlock;
  5154. retval = security_task_setscheduler(p, 0, NULL);
  5155. if (retval)
  5156. goto out_unlock;
  5157. cpuset_cpus_allowed(p, cpus_allowed);
  5158. cpumask_and(new_mask, in_mask, cpus_allowed);
  5159. again:
  5160. retval = set_cpus_allowed_ptr(p, new_mask);
  5161. if (!retval) {
  5162. cpuset_cpus_allowed(p, cpus_allowed);
  5163. if (!cpumask_subset(new_mask, cpus_allowed)) {
  5164. /*
  5165. * We must have raced with a concurrent cpuset
  5166. * update. Just reset the cpus_allowed to the
  5167. * cpuset's cpus_allowed
  5168. */
  5169. cpumask_copy(new_mask, cpus_allowed);
  5170. goto again;
  5171. }
  5172. }
  5173. out_unlock:
  5174. free_cpumask_var(new_mask);
  5175. out_free_cpus_allowed:
  5176. free_cpumask_var(cpus_allowed);
  5177. out_put_task:
  5178. put_task_struct(p);
  5179. put_online_cpus();
  5180. return retval;
  5181. }
  5182. static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  5183. struct cpumask *new_mask)
  5184. {
  5185. if (len < cpumask_size())
  5186. cpumask_clear(new_mask);
  5187. else if (len > cpumask_size())
  5188. len = cpumask_size();
  5189. return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  5190. }
  5191. /**
  5192. * sys_sched_setaffinity - set the cpu affinity of a process
  5193. * @pid: pid of the process
  5194. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  5195. * @user_mask_ptr: user-space pointer to the new cpu mask
  5196. */
  5197. SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
  5198. unsigned long __user *, user_mask_ptr)
  5199. {
  5200. cpumask_var_t new_mask;
  5201. int retval;
  5202. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  5203. return -ENOMEM;
  5204. retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
  5205. if (retval == 0)
  5206. retval = sched_setaffinity(pid, new_mask);
  5207. free_cpumask_var(new_mask);
  5208. return retval;
  5209. }
  5210. long sched_getaffinity(pid_t pid, struct cpumask *mask)
  5211. {
  5212. struct task_struct *p;
  5213. int retval;
  5214. get_online_cpus();
  5215. read_lock(&tasklist_lock);
  5216. retval = -ESRCH;
  5217. p = find_process_by_pid(pid);
  5218. if (!p)
  5219. goto out_unlock;
  5220. retval = security_task_getscheduler(p);
  5221. if (retval)
  5222. goto out_unlock;
  5223. cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
  5224. out_unlock:
  5225. read_unlock(&tasklist_lock);
  5226. put_online_cpus();
  5227. return retval;
  5228. }
  5229. /**
  5230. * sys_sched_getaffinity - get the cpu affinity of a process
  5231. * @pid: pid of the process
  5232. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  5233. * @user_mask_ptr: user-space pointer to hold the current cpu mask
  5234. */
  5235. SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
  5236. unsigned long __user *, user_mask_ptr)
  5237. {
  5238. int ret;
  5239. cpumask_var_t mask;
  5240. if (len < cpumask_size())
  5241. return -EINVAL;
  5242. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  5243. return -ENOMEM;
  5244. ret = sched_getaffinity(pid, mask);
  5245. if (ret == 0) {
  5246. if (copy_to_user(user_mask_ptr, mask, cpumask_size()))
  5247. ret = -EFAULT;
  5248. else
  5249. ret = cpumask_size();
  5250. }
  5251. free_cpumask_var(mask);
  5252. return ret;
  5253. }
  5254. /**
  5255. * sys_sched_yield - yield the current processor to other threads.
  5256. *
  5257. * This function yields the current CPU to other tasks. If there are no
  5258. * other threads running on this CPU then this function will return.
  5259. */
  5260. SYSCALL_DEFINE0(sched_yield)
  5261. {
  5262. struct rq *rq = this_rq_lock();
  5263. schedstat_inc(rq, yld_count);
  5264. current->sched_class->yield_task(rq);
  5265. /*
  5266. * Since we are going to call schedule() anyway, there's
  5267. * no need to preempt or enable interrupts:
  5268. */
  5269. __release(rq->lock);
  5270. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  5271. _raw_spin_unlock(&rq->lock);
  5272. preempt_enable_no_resched();
  5273. schedule();
  5274. return 0;
  5275. }
  5276. static void __cond_resched(void)
  5277. {
  5278. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  5279. __might_sleep(__FILE__, __LINE__);
  5280. #endif
  5281. /*
  5282. * The BKS might be reacquired before we have dropped
  5283. * PREEMPT_ACTIVE, which could trigger a second
  5284. * cond_resched() call.
  5285. */
  5286. do {
  5287. add_preempt_count(PREEMPT_ACTIVE);
  5288. schedule();
  5289. sub_preempt_count(PREEMPT_ACTIVE);
  5290. } while (need_resched());
  5291. }
  5292. int __sched _cond_resched(void)
  5293. {
  5294. if (need_resched() && !(preempt_count() & PREEMPT_ACTIVE) &&
  5295. system_state == SYSTEM_RUNNING) {
  5296. __cond_resched();
  5297. return 1;
  5298. }
  5299. return 0;
  5300. }
  5301. EXPORT_SYMBOL(_cond_resched);
  5302. /*
  5303. * cond_resched_lock() - if a reschedule is pending, drop the given lock,
  5304. * call schedule, and on return reacquire the lock.
  5305. *
  5306. * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  5307. * operations here to prevent schedule() from being called twice (once via
  5308. * spin_unlock(), once by hand).
  5309. */
  5310. int cond_resched_lock(spinlock_t *lock)
  5311. {
  5312. int resched = need_resched() && system_state == SYSTEM_RUNNING;
  5313. int ret = 0;
  5314. if (spin_needbreak(lock) || resched) {
  5315. spin_unlock(lock);
  5316. if (resched && need_resched())
  5317. __cond_resched();
  5318. else
  5319. cpu_relax();
  5320. ret = 1;
  5321. spin_lock(lock);
  5322. }
  5323. return ret;
  5324. }
  5325. EXPORT_SYMBOL(cond_resched_lock);
  5326. int __sched cond_resched_softirq(void)
  5327. {
  5328. BUG_ON(!in_softirq());
  5329. if (need_resched() && system_state == SYSTEM_RUNNING) {
  5330. local_bh_enable();
  5331. __cond_resched();
  5332. local_bh_disable();
  5333. return 1;
  5334. }
  5335. return 0;
  5336. }
  5337. EXPORT_SYMBOL(cond_resched_softirq);
  5338. /**
  5339. * yield - yield the current processor to other threads.
  5340. *
  5341. * This is a shortcut for kernel-space yielding - it marks the
  5342. * thread runnable and calls sys_sched_yield().
  5343. */
  5344. void __sched yield(void)
  5345. {
  5346. set_current_state(TASK_RUNNING);
  5347. sys_sched_yield();
  5348. }
  5349. EXPORT_SYMBOL(yield);
  5350. /*
  5351. * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  5352. * that process accounting knows that this is a task in IO wait state.
  5353. *
  5354. * But don't do that if it is a deliberate, throttling IO wait (this task
  5355. * has set its backing_dev_info: the queue against which it should throttle)
  5356. */
  5357. void __sched io_schedule(void)
  5358. {
  5359. struct rq *rq = &__raw_get_cpu_var(runqueues);
  5360. delayacct_blkio_start();
  5361. atomic_inc(&rq->nr_iowait);
  5362. schedule();
  5363. atomic_dec(&rq->nr_iowait);
  5364. delayacct_blkio_end();
  5365. }
  5366. EXPORT_SYMBOL(io_schedule);
  5367. long __sched io_schedule_timeout(long timeout)
  5368. {
  5369. struct rq *rq = &__raw_get_cpu_var(runqueues);
  5370. long ret;
  5371. delayacct_blkio_start();
  5372. atomic_inc(&rq->nr_iowait);
  5373. ret = schedule_timeout(timeout);
  5374. atomic_dec(&rq->nr_iowait);
  5375. delayacct_blkio_end();
  5376. return ret;
  5377. }
  5378. /**
  5379. * sys_sched_get_priority_max - return maximum RT priority.
  5380. * @policy: scheduling class.
  5381. *
  5382. * this syscall returns the maximum rt_priority that can be used
  5383. * by a given scheduling class.
  5384. */
  5385. SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
  5386. {
  5387. int ret = -EINVAL;
  5388. switch (policy) {
  5389. case SCHED_FIFO:
  5390. case SCHED_RR:
  5391. ret = MAX_USER_RT_PRIO-1;
  5392. break;
  5393. case SCHED_NORMAL:
  5394. case SCHED_BATCH:
  5395. case SCHED_IDLE:
  5396. ret = 0;
  5397. break;
  5398. }
  5399. return ret;
  5400. }
  5401. /**
  5402. * sys_sched_get_priority_min - return minimum RT priority.
  5403. * @policy: scheduling class.
  5404. *
  5405. * this syscall returns the minimum rt_priority that can be used
  5406. * by a given scheduling class.
  5407. */
  5408. SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
  5409. {
  5410. int ret = -EINVAL;
  5411. switch (policy) {
  5412. case SCHED_FIFO:
  5413. case SCHED_RR:
  5414. ret = 1;
  5415. break;
  5416. case SCHED_NORMAL:
  5417. case SCHED_BATCH:
  5418. case SCHED_IDLE:
  5419. ret = 0;
  5420. }
  5421. return ret;
  5422. }
  5423. /**
  5424. * sys_sched_rr_get_interval - return the default timeslice of a process.
  5425. * @pid: pid of the process.
  5426. * @interval: userspace pointer to the timeslice value.
  5427. *
  5428. * this syscall writes the default timeslice value of a given process
  5429. * into the user-space timespec buffer. A value of '0' means infinity.
  5430. */
  5431. SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
  5432. struct timespec __user *, interval)
  5433. {
  5434. struct task_struct *p;
  5435. unsigned int time_slice;
  5436. int retval;
  5437. struct timespec t;
  5438. if (pid < 0)
  5439. return -EINVAL;
  5440. retval = -ESRCH;
  5441. read_lock(&tasklist_lock);
  5442. p = find_process_by_pid(pid);
  5443. if (!p)
  5444. goto out_unlock;
  5445. retval = security_task_getscheduler(p);
  5446. if (retval)
  5447. goto out_unlock;
  5448. /*
  5449. * Time slice is 0 for SCHED_FIFO tasks and for SCHED_OTHER
  5450. * tasks that are on an otherwise idle runqueue:
  5451. */
  5452. time_slice = 0;
  5453. if (p->policy == SCHED_RR) {
  5454. time_slice = DEF_TIMESLICE;
  5455. } else if (p->policy != SCHED_FIFO) {
  5456. struct sched_entity *se = &p->se;
  5457. unsigned long flags;
  5458. struct rq *rq;
  5459. rq = task_rq_lock(p, &flags);
  5460. if (rq->cfs.load.weight)
  5461. time_slice = NS_TO_JIFFIES(sched_slice(&rq->cfs, se));
  5462. task_rq_unlock(rq, &flags);
  5463. }
  5464. read_unlock(&tasklist_lock);
  5465. jiffies_to_timespec(time_slice, &t);
  5466. retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  5467. return retval;
  5468. out_unlock:
  5469. read_unlock(&tasklist_lock);
  5470. return retval;
  5471. }
  5472. static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
  5473. void sched_show_task(struct task_struct *p)
  5474. {
  5475. unsigned long free = 0;
  5476. unsigned state;
  5477. state = p->state ? __ffs(p->state) + 1 : 0;
  5478. printk(KERN_INFO "%-13.13s %c", p->comm,
  5479. state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
  5480. #if BITS_PER_LONG == 32
  5481. if (state == TASK_RUNNING)
  5482. printk(KERN_CONT " running ");
  5483. else
  5484. printk(KERN_CONT " %08lx ", thread_saved_pc(p));
  5485. #else
  5486. if (state == TASK_RUNNING)
  5487. printk(KERN_CONT " running task ");
  5488. else
  5489. printk(KERN_CONT " %016lx ", thread_saved_pc(p));
  5490. #endif
  5491. #ifdef CONFIG_DEBUG_STACK_USAGE
  5492. free = stack_not_used(p);
  5493. #endif
  5494. printk(KERN_CONT "%5lu %5d %6d\n", free,
  5495. task_pid_nr(p), task_pid_nr(p->real_parent));
  5496. show_stack(p, NULL);
  5497. }
  5498. void show_state_filter(unsigned long state_filter)
  5499. {
  5500. struct task_struct *g, *p;
  5501. #if BITS_PER_LONG == 32
  5502. printk(KERN_INFO
  5503. " task PC stack pid father\n");
  5504. #else
  5505. printk(KERN_INFO
  5506. " task PC stack pid father\n");
  5507. #endif
  5508. read_lock(&tasklist_lock);
  5509. do_each_thread(g, p) {
  5510. /*
  5511. * reset the NMI-timeout, listing all files on a slow
  5512. * console might take alot of time:
  5513. */
  5514. touch_nmi_watchdog();
  5515. if (!state_filter || (p->state & state_filter))
  5516. sched_show_task(p);
  5517. } while_each_thread(g, p);
  5518. touch_all_softlockup_watchdogs();
  5519. #ifdef CONFIG_SCHED_DEBUG
  5520. sysrq_sched_debug_show();
  5521. #endif
  5522. read_unlock(&tasklist_lock);
  5523. /*
  5524. * Only show locks if all tasks are dumped:
  5525. */
  5526. if (state_filter == -1)
  5527. debug_show_all_locks();
  5528. }
  5529. void __cpuinit init_idle_bootup_task(struct task_struct *idle)
  5530. {
  5531. idle->sched_class = &idle_sched_class;
  5532. }
  5533. /**
  5534. * init_idle - set up an idle thread for a given CPU
  5535. * @idle: task in question
  5536. * @cpu: cpu the idle task belongs to
  5537. *
  5538. * NOTE: this function does not set the idle thread's NEED_RESCHED
  5539. * flag, to make booting more robust.
  5540. */
  5541. void __cpuinit init_idle(struct task_struct *idle, int cpu)
  5542. {
  5543. struct rq *rq = cpu_rq(cpu);
  5544. unsigned long flags;
  5545. spin_lock_irqsave(&rq->lock, flags);
  5546. __sched_fork(idle);
  5547. idle->se.exec_start = sched_clock();
  5548. idle->prio = idle->normal_prio = MAX_PRIO;
  5549. cpumask_copy(&idle->cpus_allowed, cpumask_of(cpu));
  5550. __set_task_cpu(idle, cpu);
  5551. rq->curr = rq->idle = idle;
  5552. #if defined(CONFIG_SMP) && defined(__ARCH_WANT_UNLOCKED_CTXSW)
  5553. idle->oncpu = 1;
  5554. #endif
  5555. spin_unlock_irqrestore(&rq->lock, flags);
  5556. /* Set the preempt count _outside_ the spinlocks! */
  5557. #if defined(CONFIG_PREEMPT)
  5558. task_thread_info(idle)->preempt_count = (idle->lock_depth >= 0);
  5559. #else
  5560. task_thread_info(idle)->preempt_count = 0;
  5561. #endif
  5562. /*
  5563. * The idle tasks have their own, simple scheduling class:
  5564. */
  5565. idle->sched_class = &idle_sched_class;
  5566. ftrace_graph_init_task(idle);
  5567. }
  5568. /*
  5569. * In a system that switches off the HZ timer nohz_cpu_mask
  5570. * indicates which cpus entered this state. This is used
  5571. * in the rcu update to wait only for active cpus. For system
  5572. * which do not switch off the HZ timer nohz_cpu_mask should
  5573. * always be CPU_BITS_NONE.
  5574. */
  5575. cpumask_var_t nohz_cpu_mask;
  5576. /*
  5577. * Increase the granularity value when there are more CPUs,
  5578. * because with more CPUs the 'effective latency' as visible
  5579. * to users decreases. But the relationship is not linear,
  5580. * so pick a second-best guess by going with the log2 of the
  5581. * number of CPUs.
  5582. *
  5583. * This idea comes from the SD scheduler of Con Kolivas:
  5584. */
  5585. static inline void sched_init_granularity(void)
  5586. {
  5587. unsigned int factor = 1 + ilog2(num_online_cpus());
  5588. const unsigned long limit = 200000000;
  5589. sysctl_sched_min_granularity *= factor;
  5590. if (sysctl_sched_min_granularity > limit)
  5591. sysctl_sched_min_granularity = limit;
  5592. sysctl_sched_latency *= factor;
  5593. if (sysctl_sched_latency > limit)
  5594. sysctl_sched_latency = limit;
  5595. sysctl_sched_wakeup_granularity *= factor;
  5596. sysctl_sched_shares_ratelimit *= factor;
  5597. }
  5598. #ifdef CONFIG_SMP
  5599. /*
  5600. * This is how migration works:
  5601. *
  5602. * 1) we queue a struct migration_req structure in the source CPU's
  5603. * runqueue and wake up that CPU's migration thread.
  5604. * 2) we down() the locked semaphore => thread blocks.
  5605. * 3) migration thread wakes up (implicitly it forces the migrated
  5606. * thread off the CPU)
  5607. * 4) it gets the migration request and checks whether the migrated
  5608. * task is still in the wrong runqueue.
  5609. * 5) if it's in the wrong runqueue then the migration thread removes
  5610. * it and puts it into the right queue.
  5611. * 6) migration thread up()s the semaphore.
  5612. * 7) we wake up and the migration is done.
  5613. */
  5614. /*
  5615. * Change a given task's CPU affinity. Migrate the thread to a
  5616. * proper CPU and schedule it away if the CPU it's executing on
  5617. * is removed from the allowed bitmask.
  5618. *
  5619. * NOTE: the caller must have a valid reference to the task, the
  5620. * task must not exit() & deallocate itself prematurely. The
  5621. * call is not atomic; no spinlocks may be held.
  5622. */
  5623. int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
  5624. {
  5625. struct migration_req req;
  5626. unsigned long flags;
  5627. struct rq *rq;
  5628. int ret = 0;
  5629. rq = task_rq_lock(p, &flags);
  5630. if (!cpumask_intersects(new_mask, cpu_online_mask)) {
  5631. ret = -EINVAL;
  5632. goto out;
  5633. }
  5634. if (unlikely((p->flags & PF_THREAD_BOUND) && p != current &&
  5635. !cpumask_equal(&p->cpus_allowed, new_mask))) {
  5636. ret = -EINVAL;
  5637. goto out;
  5638. }
  5639. if (p->sched_class->set_cpus_allowed)
  5640. p->sched_class->set_cpus_allowed(p, new_mask);
  5641. else {
  5642. cpumask_copy(&p->cpus_allowed, new_mask);
  5643. p->rt.nr_cpus_allowed = cpumask_weight(new_mask);
  5644. }
  5645. /* Can the task run on the task's current CPU? If so, we're done */
  5646. if (cpumask_test_cpu(task_cpu(p), new_mask))
  5647. goto out;
  5648. if (migrate_task(p, cpumask_any_and(cpu_online_mask, new_mask), &req)) {
  5649. /* Need help from migration thread: drop lock and wait. */
  5650. task_rq_unlock(rq, &flags);
  5651. wake_up_process(rq->migration_thread);
  5652. wait_for_completion(&req.done);
  5653. tlb_migrate_finish(p->mm);
  5654. return 0;
  5655. }
  5656. out:
  5657. task_rq_unlock(rq, &flags);
  5658. return ret;
  5659. }
  5660. EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
  5661. /*
  5662. * Move (not current) task off this cpu, onto dest cpu. We're doing
  5663. * this because either it can't run here any more (set_cpus_allowed()
  5664. * away from this CPU, or CPU going down), or because we're
  5665. * attempting to rebalance this task on exec (sched_exec).
  5666. *
  5667. * So we race with normal scheduler movements, but that's OK, as long
  5668. * as the task is no longer on this CPU.
  5669. *
  5670. * Returns non-zero if task was successfully migrated.
  5671. */
  5672. static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
  5673. {
  5674. struct rq *rq_dest, *rq_src;
  5675. int ret = 0, on_rq;
  5676. if (unlikely(!cpu_active(dest_cpu)))
  5677. return ret;
  5678. rq_src = cpu_rq(src_cpu);
  5679. rq_dest = cpu_rq(dest_cpu);
  5680. double_rq_lock(rq_src, rq_dest);
  5681. /* Already moved. */
  5682. if (task_cpu(p) != src_cpu)
  5683. goto done;
  5684. /* Affinity changed (again). */
  5685. if (!cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
  5686. goto fail;
  5687. on_rq = p->se.on_rq;
  5688. if (on_rq)
  5689. deactivate_task(rq_src, p, 0);
  5690. set_task_cpu(p, dest_cpu);
  5691. if (on_rq) {
  5692. activate_task(rq_dest, p, 0);
  5693. check_preempt_curr(rq_dest, p, 0);
  5694. }
  5695. done:
  5696. ret = 1;
  5697. fail:
  5698. double_rq_unlock(rq_src, rq_dest);
  5699. return ret;
  5700. }
  5701. /*
  5702. * migration_thread - this is a highprio system thread that performs
  5703. * thread migration by bumping thread off CPU then 'pushing' onto
  5704. * another runqueue.
  5705. */
  5706. static int migration_thread(void *data)
  5707. {
  5708. int cpu = (long)data;
  5709. struct rq *rq;
  5710. rq = cpu_rq(cpu);
  5711. BUG_ON(rq->migration_thread != current);
  5712. set_current_state(TASK_INTERRUPTIBLE);
  5713. while (!kthread_should_stop()) {
  5714. struct migration_req *req;
  5715. struct list_head *head;
  5716. spin_lock_irq(&rq->lock);
  5717. if (cpu_is_offline(cpu)) {
  5718. spin_unlock_irq(&rq->lock);
  5719. goto wait_to_die;
  5720. }
  5721. if (rq->active_balance) {
  5722. active_load_balance(rq, cpu);
  5723. rq->active_balance = 0;
  5724. }
  5725. head = &rq->migration_queue;
  5726. if (list_empty(head)) {
  5727. spin_unlock_irq(&rq->lock);
  5728. schedule();
  5729. set_current_state(TASK_INTERRUPTIBLE);
  5730. continue;
  5731. }
  5732. req = list_entry(head->next, struct migration_req, list);
  5733. list_del_init(head->next);
  5734. spin_unlock(&rq->lock);
  5735. __migrate_task(req->task, cpu, req->dest_cpu);
  5736. local_irq_enable();
  5737. complete(&req->done);
  5738. }
  5739. __set_current_state(TASK_RUNNING);
  5740. return 0;
  5741. wait_to_die:
  5742. /* Wait for kthread_stop */
  5743. set_current_state(TASK_INTERRUPTIBLE);
  5744. while (!kthread_should_stop()) {
  5745. schedule();
  5746. set_current_state(TASK_INTERRUPTIBLE);
  5747. }
  5748. __set_current_state(TASK_RUNNING);
  5749. return 0;
  5750. }
  5751. #ifdef CONFIG_HOTPLUG_CPU
  5752. static int __migrate_task_irq(struct task_struct *p, int src_cpu, int dest_cpu)
  5753. {
  5754. int ret;
  5755. local_irq_disable();
  5756. ret = __migrate_task(p, src_cpu, dest_cpu);
  5757. local_irq_enable();
  5758. return ret;
  5759. }
  5760. /*
  5761. * Figure out where task on dead CPU should go, use force if necessary.
  5762. */
  5763. static void move_task_off_dead_cpu(int dead_cpu, struct task_struct *p)
  5764. {
  5765. int dest_cpu;
  5766. const struct cpumask *nodemask = cpumask_of_node(cpu_to_node(dead_cpu));
  5767. again:
  5768. /* Look for allowed, online CPU in same node. */
  5769. for_each_cpu_and(dest_cpu, nodemask, cpu_online_mask)
  5770. if (cpumask_test_cpu(dest_cpu, &p->cpus_allowed))
  5771. goto move;
  5772. /* Any allowed, online CPU? */
  5773. dest_cpu = cpumask_any_and(&p->cpus_allowed, cpu_online_mask);
  5774. if (dest_cpu < nr_cpu_ids)
  5775. goto move;
  5776. /* No more Mr. Nice Guy. */
  5777. if (dest_cpu >= nr_cpu_ids) {
  5778. cpuset_cpus_allowed_locked(p, &p->cpus_allowed);
  5779. dest_cpu = cpumask_any_and(cpu_online_mask, &p->cpus_allowed);
  5780. /*
  5781. * Don't tell them about moving exiting tasks or
  5782. * kernel threads (both mm NULL), since they never
  5783. * leave kernel.
  5784. */
  5785. if (p->mm && printk_ratelimit()) {
  5786. printk(KERN_INFO "process %d (%s) no "
  5787. "longer affine to cpu%d\n",
  5788. task_pid_nr(p), p->comm, dead_cpu);
  5789. }
  5790. }
  5791. move:
  5792. /* It can have affinity changed while we were choosing. */
  5793. if (unlikely(!__migrate_task_irq(p, dead_cpu, dest_cpu)))
  5794. goto again;
  5795. }
  5796. /*
  5797. * While a dead CPU has no uninterruptible tasks queued at this point,
  5798. * it might still have a nonzero ->nr_uninterruptible counter, because
  5799. * for performance reasons the counter is not stricly tracking tasks to
  5800. * their home CPUs. So we just add the counter to another CPU's counter,
  5801. * to keep the global sum constant after CPU-down:
  5802. */
  5803. static void migrate_nr_uninterruptible(struct rq *rq_src)
  5804. {
  5805. struct rq *rq_dest = cpu_rq(cpumask_any(cpu_online_mask));
  5806. unsigned long flags;
  5807. local_irq_save(flags);
  5808. double_rq_lock(rq_src, rq_dest);
  5809. rq_dest->nr_uninterruptible += rq_src->nr_uninterruptible;
  5810. rq_src->nr_uninterruptible = 0;
  5811. double_rq_unlock(rq_src, rq_dest);
  5812. local_irq_restore(flags);
  5813. }
  5814. /* Run through task list and migrate tasks from the dead cpu. */
  5815. static void migrate_live_tasks(int src_cpu)
  5816. {
  5817. struct task_struct *p, *t;
  5818. read_lock(&tasklist_lock);
  5819. do_each_thread(t, p) {
  5820. if (p == current)
  5821. continue;
  5822. if (task_cpu(p) == src_cpu)
  5823. move_task_off_dead_cpu(src_cpu, p);
  5824. } while_each_thread(t, p);
  5825. read_unlock(&tasklist_lock);
  5826. }
  5827. /*
  5828. * Schedules idle task to be the next runnable task on current CPU.
  5829. * It does so by boosting its priority to highest possible.
  5830. * Used by CPU offline code.
  5831. */
  5832. void sched_idle_next(void)
  5833. {
  5834. int this_cpu = smp_processor_id();
  5835. struct rq *rq = cpu_rq(this_cpu);
  5836. struct task_struct *p = rq->idle;
  5837. unsigned long flags;
  5838. /* cpu has to be offline */
  5839. BUG_ON(cpu_online(this_cpu));
  5840. /*
  5841. * Strictly not necessary since rest of the CPUs are stopped by now
  5842. * and interrupts disabled on the current cpu.
  5843. */
  5844. spin_lock_irqsave(&rq->lock, flags);
  5845. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  5846. update_rq_clock(rq);
  5847. activate_task(rq, p, 0);
  5848. spin_unlock_irqrestore(&rq->lock, flags);
  5849. }
  5850. /*
  5851. * Ensures that the idle task is using init_mm right before its cpu goes
  5852. * offline.
  5853. */
  5854. void idle_task_exit(void)
  5855. {
  5856. struct mm_struct *mm = current->active_mm;
  5857. BUG_ON(cpu_online(smp_processor_id()));
  5858. if (mm != &init_mm)
  5859. switch_mm(mm, &init_mm, current);
  5860. mmdrop(mm);
  5861. }
  5862. /* called under rq->lock with disabled interrupts */
  5863. static void migrate_dead(unsigned int dead_cpu, struct task_struct *p)
  5864. {
  5865. struct rq *rq = cpu_rq(dead_cpu);
  5866. /* Must be exiting, otherwise would be on tasklist. */
  5867. BUG_ON(!p->exit_state);
  5868. /* Cannot have done final schedule yet: would have vanished. */
  5869. BUG_ON(p->state == TASK_DEAD);
  5870. get_task_struct(p);
  5871. /*
  5872. * Drop lock around migration; if someone else moves it,
  5873. * that's OK. No task can be added to this CPU, so iteration is
  5874. * fine.
  5875. */
  5876. spin_unlock_irq(&rq->lock);
  5877. move_task_off_dead_cpu(dead_cpu, p);
  5878. spin_lock_irq(&rq->lock);
  5879. put_task_struct(p);
  5880. }
  5881. /* release_task() removes task from tasklist, so we won't find dead tasks. */
  5882. static void migrate_dead_tasks(unsigned int dead_cpu)
  5883. {
  5884. struct rq *rq = cpu_rq(dead_cpu);
  5885. struct task_struct *next;
  5886. for ( ; ; ) {
  5887. if (!rq->nr_running)
  5888. break;
  5889. update_rq_clock(rq);
  5890. next = pick_next_task(rq);
  5891. if (!next)
  5892. break;
  5893. next->sched_class->put_prev_task(rq, next);
  5894. migrate_dead(dead_cpu, next);
  5895. }
  5896. }
  5897. #endif /* CONFIG_HOTPLUG_CPU */
  5898. #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
  5899. static struct ctl_table sd_ctl_dir[] = {
  5900. {
  5901. .procname = "sched_domain",
  5902. .mode = 0555,
  5903. },
  5904. {0, },
  5905. };
  5906. static struct ctl_table sd_ctl_root[] = {
  5907. {
  5908. .ctl_name = CTL_KERN,
  5909. .procname = "kernel",
  5910. .mode = 0555,
  5911. .child = sd_ctl_dir,
  5912. },
  5913. {0, },
  5914. };
  5915. static struct ctl_table *sd_alloc_ctl_entry(int n)
  5916. {
  5917. struct ctl_table *entry =
  5918. kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
  5919. return entry;
  5920. }
  5921. static void sd_free_ctl_entry(struct ctl_table **tablep)
  5922. {
  5923. struct ctl_table *entry;
  5924. /*
  5925. * In the intermediate directories, both the child directory and
  5926. * procname are dynamically allocated and could fail but the mode
  5927. * will always be set. In the lowest directory the names are
  5928. * static strings and all have proc handlers.
  5929. */
  5930. for (entry = *tablep; entry->mode; entry++) {
  5931. if (entry->child)
  5932. sd_free_ctl_entry(&entry->child);
  5933. if (entry->proc_handler == NULL)
  5934. kfree(entry->procname);
  5935. }
  5936. kfree(*tablep);
  5937. *tablep = NULL;
  5938. }
  5939. static void
  5940. set_table_entry(struct ctl_table *entry,
  5941. const char *procname, void *data, int maxlen,
  5942. mode_t mode, proc_handler *proc_handler)
  5943. {
  5944. entry->procname = procname;
  5945. entry->data = data;
  5946. entry->maxlen = maxlen;
  5947. entry->mode = mode;
  5948. entry->proc_handler = proc_handler;
  5949. }
  5950. static struct ctl_table *
  5951. sd_alloc_ctl_domain_table(struct sched_domain *sd)
  5952. {
  5953. struct ctl_table *table = sd_alloc_ctl_entry(13);
  5954. if (table == NULL)
  5955. return NULL;
  5956. set_table_entry(&table[0], "min_interval", &sd->min_interval,
  5957. sizeof(long), 0644, proc_doulongvec_minmax);
  5958. set_table_entry(&table[1], "max_interval", &sd->max_interval,
  5959. sizeof(long), 0644, proc_doulongvec_minmax);
  5960. set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
  5961. sizeof(int), 0644, proc_dointvec_minmax);
  5962. set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
  5963. sizeof(int), 0644, proc_dointvec_minmax);
  5964. set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
  5965. sizeof(int), 0644, proc_dointvec_minmax);
  5966. set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
  5967. sizeof(int), 0644, proc_dointvec_minmax);
  5968. set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
  5969. sizeof(int), 0644, proc_dointvec_minmax);
  5970. set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
  5971. sizeof(int), 0644, proc_dointvec_minmax);
  5972. set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
  5973. sizeof(int), 0644, proc_dointvec_minmax);
  5974. set_table_entry(&table[9], "cache_nice_tries",
  5975. &sd->cache_nice_tries,
  5976. sizeof(int), 0644, proc_dointvec_minmax);
  5977. set_table_entry(&table[10], "flags", &sd->flags,
  5978. sizeof(int), 0644, proc_dointvec_minmax);
  5979. set_table_entry(&table[11], "name", sd->name,
  5980. CORENAME_MAX_SIZE, 0444, proc_dostring);
  5981. /* &table[12] is terminator */
  5982. return table;
  5983. }
  5984. static ctl_table *sd_alloc_ctl_cpu_table(int cpu)
  5985. {
  5986. struct ctl_table *entry, *table;
  5987. struct sched_domain *sd;
  5988. int domain_num = 0, i;
  5989. char buf[32];
  5990. for_each_domain(cpu, sd)
  5991. domain_num++;
  5992. entry = table = sd_alloc_ctl_entry(domain_num + 1);
  5993. if (table == NULL)
  5994. return NULL;
  5995. i = 0;
  5996. for_each_domain(cpu, sd) {
  5997. snprintf(buf, 32, "domain%d", i);
  5998. entry->procname = kstrdup(buf, GFP_KERNEL);
  5999. entry->mode = 0555;
  6000. entry->child = sd_alloc_ctl_domain_table(sd);
  6001. entry++;
  6002. i++;
  6003. }
  6004. return table;
  6005. }
  6006. static struct ctl_table_header *sd_sysctl_header;
  6007. static void register_sched_domain_sysctl(void)
  6008. {
  6009. int i, cpu_num = num_online_cpus();
  6010. struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
  6011. char buf[32];
  6012. WARN_ON(sd_ctl_dir[0].child);
  6013. sd_ctl_dir[0].child = entry;
  6014. if (entry == NULL)
  6015. return;
  6016. for_each_online_cpu(i) {
  6017. snprintf(buf, 32, "cpu%d", i);
  6018. entry->procname = kstrdup(buf, GFP_KERNEL);
  6019. entry->mode = 0555;
  6020. entry->child = sd_alloc_ctl_cpu_table(i);
  6021. entry++;
  6022. }
  6023. WARN_ON(sd_sysctl_header);
  6024. sd_sysctl_header = register_sysctl_table(sd_ctl_root);
  6025. }
  6026. /* may be called multiple times per register */
  6027. static void unregister_sched_domain_sysctl(void)
  6028. {
  6029. if (sd_sysctl_header)
  6030. unregister_sysctl_table(sd_sysctl_header);
  6031. sd_sysctl_header = NULL;
  6032. if (sd_ctl_dir[0].child)
  6033. sd_free_ctl_entry(&sd_ctl_dir[0].child);
  6034. }
  6035. #else
  6036. static void register_sched_domain_sysctl(void)
  6037. {
  6038. }
  6039. static void unregister_sched_domain_sysctl(void)
  6040. {
  6041. }
  6042. #endif
  6043. static void set_rq_online(struct rq *rq)
  6044. {
  6045. if (!rq->online) {
  6046. const struct sched_class *class;
  6047. cpumask_set_cpu(rq->cpu, rq->rd->online);
  6048. rq->online = 1;
  6049. for_each_class(class) {
  6050. if (class->rq_online)
  6051. class->rq_online(rq);
  6052. }
  6053. }
  6054. }
  6055. static void set_rq_offline(struct rq *rq)
  6056. {
  6057. if (rq->online) {
  6058. const struct sched_class *class;
  6059. for_each_class(class) {
  6060. if (class->rq_offline)
  6061. class->rq_offline(rq);
  6062. }
  6063. cpumask_clear_cpu(rq->cpu, rq->rd->online);
  6064. rq->online = 0;
  6065. }
  6066. }
  6067. /*
  6068. * migration_call - callback that gets triggered when a CPU is added.
  6069. * Here we can start up the necessary migration thread for the new CPU.
  6070. */
  6071. static int __cpuinit
  6072. migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  6073. {
  6074. struct task_struct *p;
  6075. int cpu = (long)hcpu;
  6076. unsigned long flags;
  6077. struct rq *rq;
  6078. switch (action) {
  6079. case CPU_UP_PREPARE:
  6080. case CPU_UP_PREPARE_FROZEN:
  6081. p = kthread_create(migration_thread, hcpu, "migration/%d", cpu);
  6082. if (IS_ERR(p))
  6083. return NOTIFY_BAD;
  6084. kthread_bind(p, cpu);
  6085. /* Must be high prio: stop_machine expects to yield to it. */
  6086. rq = task_rq_lock(p, &flags);
  6087. __setscheduler(rq, p, SCHED_FIFO, MAX_RT_PRIO-1);
  6088. task_rq_unlock(rq, &flags);
  6089. cpu_rq(cpu)->migration_thread = p;
  6090. break;
  6091. case CPU_ONLINE:
  6092. case CPU_ONLINE_FROZEN:
  6093. /* Strictly unnecessary, as first user will wake it. */
  6094. wake_up_process(cpu_rq(cpu)->migration_thread);
  6095. /* Update our root-domain */
  6096. rq = cpu_rq(cpu);
  6097. spin_lock_irqsave(&rq->lock, flags);
  6098. if (rq->rd) {
  6099. BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  6100. set_rq_online(rq);
  6101. }
  6102. spin_unlock_irqrestore(&rq->lock, flags);
  6103. break;
  6104. #ifdef CONFIG_HOTPLUG_CPU
  6105. case CPU_UP_CANCELED:
  6106. case CPU_UP_CANCELED_FROZEN:
  6107. if (!cpu_rq(cpu)->migration_thread)
  6108. break;
  6109. /* Unbind it from offline cpu so it can run. Fall thru. */
  6110. kthread_bind(cpu_rq(cpu)->migration_thread,
  6111. cpumask_any(cpu_online_mask));
  6112. kthread_stop(cpu_rq(cpu)->migration_thread);
  6113. cpu_rq(cpu)->migration_thread = NULL;
  6114. break;
  6115. case CPU_DEAD:
  6116. case CPU_DEAD_FROZEN:
  6117. cpuset_lock(); /* around calls to cpuset_cpus_allowed_lock() */
  6118. migrate_live_tasks(cpu);
  6119. rq = cpu_rq(cpu);
  6120. kthread_stop(rq->migration_thread);
  6121. rq->migration_thread = NULL;
  6122. /* Idle task back to normal (off runqueue, low prio) */
  6123. spin_lock_irq(&rq->lock);
  6124. update_rq_clock(rq);
  6125. deactivate_task(rq, rq->idle, 0);
  6126. rq->idle->static_prio = MAX_PRIO;
  6127. __setscheduler(rq, rq->idle, SCHED_NORMAL, 0);
  6128. rq->idle->sched_class = &idle_sched_class;
  6129. migrate_dead_tasks(cpu);
  6130. spin_unlock_irq(&rq->lock);
  6131. cpuset_unlock();
  6132. migrate_nr_uninterruptible(rq);
  6133. BUG_ON(rq->nr_running != 0);
  6134. /*
  6135. * No need to migrate the tasks: it was best-effort if
  6136. * they didn't take sched_hotcpu_mutex. Just wake up
  6137. * the requestors.
  6138. */
  6139. spin_lock_irq(&rq->lock);
  6140. while (!list_empty(&rq->migration_queue)) {
  6141. struct migration_req *req;
  6142. req = list_entry(rq->migration_queue.next,
  6143. struct migration_req, list);
  6144. list_del_init(&req->list);
  6145. spin_unlock_irq(&rq->lock);
  6146. complete(&req->done);
  6147. spin_lock_irq(&rq->lock);
  6148. }
  6149. spin_unlock_irq(&rq->lock);
  6150. break;
  6151. case CPU_DYING:
  6152. case CPU_DYING_FROZEN:
  6153. /* Update our root-domain */
  6154. rq = cpu_rq(cpu);
  6155. spin_lock_irqsave(&rq->lock, flags);
  6156. if (rq->rd) {
  6157. BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  6158. set_rq_offline(rq);
  6159. }
  6160. spin_unlock_irqrestore(&rq->lock, flags);
  6161. break;
  6162. #endif
  6163. }
  6164. return NOTIFY_OK;
  6165. }
  6166. /* Register at highest priority so that task migration (migrate_all_tasks)
  6167. * happens before everything else.
  6168. */
  6169. static struct notifier_block __cpuinitdata migration_notifier = {
  6170. .notifier_call = migration_call,
  6171. .priority = 10
  6172. };
  6173. static int __init migration_init(void)
  6174. {
  6175. void *cpu = (void *)(long)smp_processor_id();
  6176. int err;
  6177. /* Start one for the boot CPU: */
  6178. err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  6179. BUG_ON(err == NOTIFY_BAD);
  6180. migration_call(&migration_notifier, CPU_ONLINE, cpu);
  6181. register_cpu_notifier(&migration_notifier);
  6182. return err;
  6183. }
  6184. early_initcall(migration_init);
  6185. #endif
  6186. #ifdef CONFIG_SMP
  6187. #ifdef CONFIG_SCHED_DEBUG
  6188. static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
  6189. struct cpumask *groupmask)
  6190. {
  6191. struct sched_group *group = sd->groups;
  6192. char str[256];
  6193. cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
  6194. cpumask_clear(groupmask);
  6195. printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
  6196. if (!(sd->flags & SD_LOAD_BALANCE)) {
  6197. printk("does not load-balance\n");
  6198. if (sd->parent)
  6199. printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
  6200. " has parent");
  6201. return -1;
  6202. }
  6203. printk(KERN_CONT "span %s level %s\n", str, sd->name);
  6204. if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  6205. printk(KERN_ERR "ERROR: domain->span does not contain "
  6206. "CPU%d\n", cpu);
  6207. }
  6208. if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
  6209. printk(KERN_ERR "ERROR: domain->groups does not contain"
  6210. " CPU%d\n", cpu);
  6211. }
  6212. printk(KERN_DEBUG "%*s groups:", level + 1, "");
  6213. do {
  6214. if (!group) {
  6215. printk("\n");
  6216. printk(KERN_ERR "ERROR: group is NULL\n");
  6217. break;
  6218. }
  6219. if (!group->__cpu_power) {
  6220. printk(KERN_CONT "\n");
  6221. printk(KERN_ERR "ERROR: domain->cpu_power not "
  6222. "set\n");
  6223. break;
  6224. }
  6225. if (!cpumask_weight(sched_group_cpus(group))) {
  6226. printk(KERN_CONT "\n");
  6227. printk(KERN_ERR "ERROR: empty group\n");
  6228. break;
  6229. }
  6230. if (cpumask_intersects(groupmask, sched_group_cpus(group))) {
  6231. printk(KERN_CONT "\n");
  6232. printk(KERN_ERR "ERROR: repeated CPUs\n");
  6233. break;
  6234. }
  6235. cpumask_or(groupmask, groupmask, sched_group_cpus(group));
  6236. cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
  6237. printk(KERN_CONT " %s", str);
  6238. group = group->next;
  6239. } while (group != sd->groups);
  6240. printk(KERN_CONT "\n");
  6241. if (!cpumask_equal(sched_domain_span(sd), groupmask))
  6242. printk(KERN_ERR "ERROR: groups don't span domain->span\n");
  6243. if (sd->parent &&
  6244. !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
  6245. printk(KERN_ERR "ERROR: parent span is not a superset "
  6246. "of domain->span\n");
  6247. return 0;
  6248. }
  6249. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  6250. {
  6251. cpumask_var_t groupmask;
  6252. int level = 0;
  6253. if (!sd) {
  6254. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  6255. return;
  6256. }
  6257. printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  6258. if (!alloc_cpumask_var(&groupmask, GFP_KERNEL)) {
  6259. printk(KERN_DEBUG "Cannot load-balance (out of memory)\n");
  6260. return;
  6261. }
  6262. for (;;) {
  6263. if (sched_domain_debug_one(sd, cpu, level, groupmask))
  6264. break;
  6265. level++;
  6266. sd = sd->parent;
  6267. if (!sd)
  6268. break;
  6269. }
  6270. free_cpumask_var(groupmask);
  6271. }
  6272. #else /* !CONFIG_SCHED_DEBUG */
  6273. # define sched_domain_debug(sd, cpu) do { } while (0)
  6274. #endif /* CONFIG_SCHED_DEBUG */
  6275. static int sd_degenerate(struct sched_domain *sd)
  6276. {
  6277. if (cpumask_weight(sched_domain_span(sd)) == 1)
  6278. return 1;
  6279. /* Following flags need at least 2 groups */
  6280. if (sd->flags & (SD_LOAD_BALANCE |
  6281. SD_BALANCE_NEWIDLE |
  6282. SD_BALANCE_FORK |
  6283. SD_BALANCE_EXEC |
  6284. SD_SHARE_CPUPOWER |
  6285. SD_SHARE_PKG_RESOURCES)) {
  6286. if (sd->groups != sd->groups->next)
  6287. return 0;
  6288. }
  6289. /* Following flags don't use groups */
  6290. if (sd->flags & (SD_WAKE_IDLE |
  6291. SD_WAKE_AFFINE |
  6292. SD_WAKE_BALANCE))
  6293. return 0;
  6294. return 1;
  6295. }
  6296. static int
  6297. sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  6298. {
  6299. unsigned long cflags = sd->flags, pflags = parent->flags;
  6300. if (sd_degenerate(parent))
  6301. return 1;
  6302. if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
  6303. return 0;
  6304. /* Does parent contain flags not in child? */
  6305. /* WAKE_BALANCE is a subset of WAKE_AFFINE */
  6306. if (cflags & SD_WAKE_AFFINE)
  6307. pflags &= ~SD_WAKE_BALANCE;
  6308. /* Flags needing groups don't count if only 1 group in parent */
  6309. if (parent->groups == parent->groups->next) {
  6310. pflags &= ~(SD_LOAD_BALANCE |
  6311. SD_BALANCE_NEWIDLE |
  6312. SD_BALANCE_FORK |
  6313. SD_BALANCE_EXEC |
  6314. SD_SHARE_CPUPOWER |
  6315. SD_SHARE_PKG_RESOURCES);
  6316. if (nr_node_ids == 1)
  6317. pflags &= ~SD_SERIALIZE;
  6318. }
  6319. if (~cflags & pflags)
  6320. return 0;
  6321. return 1;
  6322. }
  6323. static void free_rootdomain(struct root_domain *rd)
  6324. {
  6325. cpupri_cleanup(&rd->cpupri);
  6326. free_cpumask_var(rd->rto_mask);
  6327. free_cpumask_var(rd->online);
  6328. free_cpumask_var(rd->span);
  6329. kfree(rd);
  6330. }
  6331. static void rq_attach_root(struct rq *rq, struct root_domain *rd)
  6332. {
  6333. struct root_domain *old_rd = NULL;
  6334. unsigned long flags;
  6335. spin_lock_irqsave(&rq->lock, flags);
  6336. if (rq->rd) {
  6337. old_rd = rq->rd;
  6338. if (cpumask_test_cpu(rq->cpu, old_rd->online))
  6339. set_rq_offline(rq);
  6340. cpumask_clear_cpu(rq->cpu, old_rd->span);
  6341. /*
  6342. * If we dont want to free the old_rt yet then
  6343. * set old_rd to NULL to skip the freeing later
  6344. * in this function:
  6345. */
  6346. if (!atomic_dec_and_test(&old_rd->refcount))
  6347. old_rd = NULL;
  6348. }
  6349. atomic_inc(&rd->refcount);
  6350. rq->rd = rd;
  6351. cpumask_set_cpu(rq->cpu, rd->span);
  6352. if (cpumask_test_cpu(rq->cpu, cpu_online_mask))
  6353. set_rq_online(rq);
  6354. spin_unlock_irqrestore(&rq->lock, flags);
  6355. if (old_rd)
  6356. free_rootdomain(old_rd);
  6357. }
  6358. static int __init_refok init_rootdomain(struct root_domain *rd, bool bootmem)
  6359. {
  6360. memset(rd, 0, sizeof(*rd));
  6361. if (bootmem) {
  6362. alloc_bootmem_cpumask_var(&def_root_domain.span);
  6363. alloc_bootmem_cpumask_var(&def_root_domain.online);
  6364. alloc_bootmem_cpumask_var(&def_root_domain.rto_mask);
  6365. cpupri_init(&rd->cpupri, true);
  6366. return 0;
  6367. }
  6368. if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
  6369. goto out;
  6370. if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
  6371. goto free_span;
  6372. if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
  6373. goto free_online;
  6374. if (cpupri_init(&rd->cpupri, false) != 0)
  6375. goto free_rto_mask;
  6376. return 0;
  6377. free_rto_mask:
  6378. free_cpumask_var(rd->rto_mask);
  6379. free_online:
  6380. free_cpumask_var(rd->online);
  6381. free_span:
  6382. free_cpumask_var(rd->span);
  6383. out:
  6384. return -ENOMEM;
  6385. }
  6386. static void init_defrootdomain(void)
  6387. {
  6388. init_rootdomain(&def_root_domain, true);
  6389. atomic_set(&def_root_domain.refcount, 1);
  6390. }
  6391. static struct root_domain *alloc_rootdomain(void)
  6392. {
  6393. struct root_domain *rd;
  6394. rd = kmalloc(sizeof(*rd), GFP_KERNEL);
  6395. if (!rd)
  6396. return NULL;
  6397. if (init_rootdomain(rd, false) != 0) {
  6398. kfree(rd);
  6399. return NULL;
  6400. }
  6401. return rd;
  6402. }
  6403. /*
  6404. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  6405. * hold the hotplug lock.
  6406. */
  6407. static void
  6408. cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
  6409. {
  6410. struct rq *rq = cpu_rq(cpu);
  6411. struct sched_domain *tmp;
  6412. /* Remove the sched domains which do not contribute to scheduling. */
  6413. for (tmp = sd; tmp; ) {
  6414. struct sched_domain *parent = tmp->parent;
  6415. if (!parent)
  6416. break;
  6417. if (sd_parent_degenerate(tmp, parent)) {
  6418. tmp->parent = parent->parent;
  6419. if (parent->parent)
  6420. parent->parent->child = tmp;
  6421. } else
  6422. tmp = tmp->parent;
  6423. }
  6424. if (sd && sd_degenerate(sd)) {
  6425. sd = sd->parent;
  6426. if (sd)
  6427. sd->child = NULL;
  6428. }
  6429. sched_domain_debug(sd, cpu);
  6430. rq_attach_root(rq, rd);
  6431. rcu_assign_pointer(rq->sd, sd);
  6432. }
  6433. /* cpus with isolated domains */
  6434. static cpumask_var_t cpu_isolated_map;
  6435. /* Setup the mask of cpus configured for isolated domains */
  6436. static int __init isolated_cpu_setup(char *str)
  6437. {
  6438. cpulist_parse(str, cpu_isolated_map);
  6439. return 1;
  6440. }
  6441. __setup("isolcpus=", isolated_cpu_setup);
  6442. /*
  6443. * init_sched_build_groups takes the cpumask we wish to span, and a pointer
  6444. * to a function which identifies what group(along with sched group) a CPU
  6445. * belongs to. The return value of group_fn must be a >= 0 and < nr_cpu_ids
  6446. * (due to the fact that we keep track of groups covered with a struct cpumask).
  6447. *
  6448. * init_sched_build_groups will build a circular linked list of the groups
  6449. * covered by the given span, and will set each group's ->cpumask correctly,
  6450. * and ->cpu_power to 0.
  6451. */
  6452. static void
  6453. init_sched_build_groups(const struct cpumask *span,
  6454. const struct cpumask *cpu_map,
  6455. int (*group_fn)(int cpu, const struct cpumask *cpu_map,
  6456. struct sched_group **sg,
  6457. struct cpumask *tmpmask),
  6458. struct cpumask *covered, struct cpumask *tmpmask)
  6459. {
  6460. struct sched_group *first = NULL, *last = NULL;
  6461. int i;
  6462. cpumask_clear(covered);
  6463. for_each_cpu(i, span) {
  6464. struct sched_group *sg;
  6465. int group = group_fn(i, cpu_map, &sg, tmpmask);
  6466. int j;
  6467. if (cpumask_test_cpu(i, covered))
  6468. continue;
  6469. cpumask_clear(sched_group_cpus(sg));
  6470. sg->__cpu_power = 0;
  6471. for_each_cpu(j, span) {
  6472. if (group_fn(j, cpu_map, NULL, tmpmask) != group)
  6473. continue;
  6474. cpumask_set_cpu(j, covered);
  6475. cpumask_set_cpu(j, sched_group_cpus(sg));
  6476. }
  6477. if (!first)
  6478. first = sg;
  6479. if (last)
  6480. last->next = sg;
  6481. last = sg;
  6482. }
  6483. last->next = first;
  6484. }
  6485. #define SD_NODES_PER_DOMAIN 16
  6486. #ifdef CONFIG_NUMA
  6487. /**
  6488. * find_next_best_node - find the next node to include in a sched_domain
  6489. * @node: node whose sched_domain we're building
  6490. * @used_nodes: nodes already in the sched_domain
  6491. *
  6492. * Find the next node to include in a given scheduling domain. Simply
  6493. * finds the closest node not already in the @used_nodes map.
  6494. *
  6495. * Should use nodemask_t.
  6496. */
  6497. static int find_next_best_node(int node, nodemask_t *used_nodes)
  6498. {
  6499. int i, n, val, min_val, best_node = 0;
  6500. min_val = INT_MAX;
  6501. for (i = 0; i < nr_node_ids; i++) {
  6502. /* Start at @node */
  6503. n = (node + i) % nr_node_ids;
  6504. if (!nr_cpus_node(n))
  6505. continue;
  6506. /* Skip already used nodes */
  6507. if (node_isset(n, *used_nodes))
  6508. continue;
  6509. /* Simple min distance search */
  6510. val = node_distance(node, n);
  6511. if (val < min_val) {
  6512. min_val = val;
  6513. best_node = n;
  6514. }
  6515. }
  6516. node_set(best_node, *used_nodes);
  6517. return best_node;
  6518. }
  6519. /**
  6520. * sched_domain_node_span - get a cpumask for a node's sched_domain
  6521. * @node: node whose cpumask we're constructing
  6522. * @span: resulting cpumask
  6523. *
  6524. * Given a node, construct a good cpumask for its sched_domain to span. It
  6525. * should be one that prevents unnecessary balancing, but also spreads tasks
  6526. * out optimally.
  6527. */
  6528. static void sched_domain_node_span(int node, struct cpumask *span)
  6529. {
  6530. nodemask_t used_nodes;
  6531. int i;
  6532. cpumask_clear(span);
  6533. nodes_clear(used_nodes);
  6534. cpumask_or(span, span, cpumask_of_node(node));
  6535. node_set(node, used_nodes);
  6536. for (i = 1; i < SD_NODES_PER_DOMAIN; i++) {
  6537. int next_node = find_next_best_node(node, &used_nodes);
  6538. cpumask_or(span, span, cpumask_of_node(next_node));
  6539. }
  6540. }
  6541. #endif /* CONFIG_NUMA */
  6542. int sched_smt_power_savings = 0, sched_mc_power_savings = 0;
  6543. /*
  6544. * The cpus mask in sched_group and sched_domain hangs off the end.
  6545. * FIXME: use cpumask_var_t or dynamic percpu alloc to avoid wasting space
  6546. * for nr_cpu_ids < CONFIG_NR_CPUS.
  6547. */
  6548. struct static_sched_group {
  6549. struct sched_group sg;
  6550. DECLARE_BITMAP(cpus, CONFIG_NR_CPUS);
  6551. };
  6552. struct static_sched_domain {
  6553. struct sched_domain sd;
  6554. DECLARE_BITMAP(span, CONFIG_NR_CPUS);
  6555. };
  6556. /*
  6557. * SMT sched-domains:
  6558. */
  6559. #ifdef CONFIG_SCHED_SMT
  6560. static DEFINE_PER_CPU(struct static_sched_domain, cpu_domains);
  6561. static DEFINE_PER_CPU(struct static_sched_group, sched_group_cpus);
  6562. static int
  6563. cpu_to_cpu_group(int cpu, const struct cpumask *cpu_map,
  6564. struct sched_group **sg, struct cpumask *unused)
  6565. {
  6566. if (sg)
  6567. *sg = &per_cpu(sched_group_cpus, cpu).sg;
  6568. return cpu;
  6569. }
  6570. #endif /* CONFIG_SCHED_SMT */
  6571. /*
  6572. * multi-core sched-domains:
  6573. */
  6574. #ifdef CONFIG_SCHED_MC
  6575. static DEFINE_PER_CPU(struct static_sched_domain, core_domains);
  6576. static DEFINE_PER_CPU(struct static_sched_group, sched_group_core);
  6577. #endif /* CONFIG_SCHED_MC */
  6578. #if defined(CONFIG_SCHED_MC) && defined(CONFIG_SCHED_SMT)
  6579. static int
  6580. cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
  6581. struct sched_group **sg, struct cpumask *mask)
  6582. {
  6583. int group;
  6584. cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
  6585. group = cpumask_first(mask);
  6586. if (sg)
  6587. *sg = &per_cpu(sched_group_core, group).sg;
  6588. return group;
  6589. }
  6590. #elif defined(CONFIG_SCHED_MC)
  6591. static int
  6592. cpu_to_core_group(int cpu, const struct cpumask *cpu_map,
  6593. struct sched_group **sg, struct cpumask *unused)
  6594. {
  6595. if (sg)
  6596. *sg = &per_cpu(sched_group_core, cpu).sg;
  6597. return cpu;
  6598. }
  6599. #endif
  6600. static DEFINE_PER_CPU(struct static_sched_domain, phys_domains);
  6601. static DEFINE_PER_CPU(struct static_sched_group, sched_group_phys);
  6602. static int
  6603. cpu_to_phys_group(int cpu, const struct cpumask *cpu_map,
  6604. struct sched_group **sg, struct cpumask *mask)
  6605. {
  6606. int group;
  6607. #ifdef CONFIG_SCHED_MC
  6608. cpumask_and(mask, cpu_coregroup_mask(cpu), cpu_map);
  6609. group = cpumask_first(mask);
  6610. #elif defined(CONFIG_SCHED_SMT)
  6611. cpumask_and(mask, topology_thread_cpumask(cpu), cpu_map);
  6612. group = cpumask_first(mask);
  6613. #else
  6614. group = cpu;
  6615. #endif
  6616. if (sg)
  6617. *sg = &per_cpu(sched_group_phys, group).sg;
  6618. return group;
  6619. }
  6620. #ifdef CONFIG_NUMA
  6621. /*
  6622. * The init_sched_build_groups can't handle what we want to do with node
  6623. * groups, so roll our own. Now each node has its own list of groups which
  6624. * gets dynamically allocated.
  6625. */
  6626. static DEFINE_PER_CPU(struct static_sched_domain, node_domains);
  6627. static struct sched_group ***sched_group_nodes_bycpu;
  6628. static DEFINE_PER_CPU(struct static_sched_domain, allnodes_domains);
  6629. static DEFINE_PER_CPU(struct static_sched_group, sched_group_allnodes);
  6630. static int cpu_to_allnodes_group(int cpu, const struct cpumask *cpu_map,
  6631. struct sched_group **sg,
  6632. struct cpumask *nodemask)
  6633. {
  6634. int group;
  6635. cpumask_and(nodemask, cpumask_of_node(cpu_to_node(cpu)), cpu_map);
  6636. group = cpumask_first(nodemask);
  6637. if (sg)
  6638. *sg = &per_cpu(sched_group_allnodes, group).sg;
  6639. return group;
  6640. }
  6641. static void init_numa_sched_groups_power(struct sched_group *group_head)
  6642. {
  6643. struct sched_group *sg = group_head;
  6644. int j;
  6645. if (!sg)
  6646. return;
  6647. do {
  6648. for_each_cpu(j, sched_group_cpus(sg)) {
  6649. struct sched_domain *sd;
  6650. sd = &per_cpu(phys_domains, j).sd;
  6651. if (j != cpumask_first(sched_group_cpus(sd->groups))) {
  6652. /*
  6653. * Only add "power" once for each
  6654. * physical package.
  6655. */
  6656. continue;
  6657. }
  6658. sg_inc_cpu_power(sg, sd->groups->__cpu_power);
  6659. }
  6660. sg = sg->next;
  6661. } while (sg != group_head);
  6662. }
  6663. #endif /* CONFIG_NUMA */
  6664. #ifdef CONFIG_NUMA
  6665. /* Free memory allocated for various sched_group structures */
  6666. static void free_sched_groups(const struct cpumask *cpu_map,
  6667. struct cpumask *nodemask)
  6668. {
  6669. int cpu, i;
  6670. for_each_cpu(cpu, cpu_map) {
  6671. struct sched_group **sched_group_nodes
  6672. = sched_group_nodes_bycpu[cpu];
  6673. if (!sched_group_nodes)
  6674. continue;
  6675. for (i = 0; i < nr_node_ids; i++) {
  6676. struct sched_group *oldsg, *sg = sched_group_nodes[i];
  6677. cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
  6678. if (cpumask_empty(nodemask))
  6679. continue;
  6680. if (sg == NULL)
  6681. continue;
  6682. sg = sg->next;
  6683. next_sg:
  6684. oldsg = sg;
  6685. sg = sg->next;
  6686. kfree(oldsg);
  6687. if (oldsg != sched_group_nodes[i])
  6688. goto next_sg;
  6689. }
  6690. kfree(sched_group_nodes);
  6691. sched_group_nodes_bycpu[cpu] = NULL;
  6692. }
  6693. }
  6694. #else /* !CONFIG_NUMA */
  6695. static void free_sched_groups(const struct cpumask *cpu_map,
  6696. struct cpumask *nodemask)
  6697. {
  6698. }
  6699. #endif /* CONFIG_NUMA */
  6700. /*
  6701. * Initialize sched groups cpu_power.
  6702. *
  6703. * cpu_power indicates the capacity of sched group, which is used while
  6704. * distributing the load between different sched groups in a sched domain.
  6705. * Typically cpu_power for all the groups in a sched domain will be same unless
  6706. * there are asymmetries in the topology. If there are asymmetries, group
  6707. * having more cpu_power will pickup more load compared to the group having
  6708. * less cpu_power.
  6709. *
  6710. * cpu_power will be a multiple of SCHED_LOAD_SCALE. This multiple represents
  6711. * the maximum number of tasks a group can handle in the presence of other idle
  6712. * or lightly loaded groups in the same sched domain.
  6713. */
  6714. static void init_sched_groups_power(int cpu, struct sched_domain *sd)
  6715. {
  6716. struct sched_domain *child;
  6717. struct sched_group *group;
  6718. WARN_ON(!sd || !sd->groups);
  6719. if (cpu != cpumask_first(sched_group_cpus(sd->groups)))
  6720. return;
  6721. child = sd->child;
  6722. sd->groups->__cpu_power = 0;
  6723. /*
  6724. * For perf policy, if the groups in child domain share resources
  6725. * (for example cores sharing some portions of the cache hierarchy
  6726. * or SMT), then set this domain groups cpu_power such that each group
  6727. * can handle only one task, when there are other idle groups in the
  6728. * same sched domain.
  6729. */
  6730. if (!child || (!(sd->flags & SD_POWERSAVINGS_BALANCE) &&
  6731. (child->flags &
  6732. (SD_SHARE_CPUPOWER | SD_SHARE_PKG_RESOURCES)))) {
  6733. sg_inc_cpu_power(sd->groups, SCHED_LOAD_SCALE);
  6734. return;
  6735. }
  6736. /*
  6737. * add cpu_power of each child group to this groups cpu_power
  6738. */
  6739. group = child->groups;
  6740. do {
  6741. sg_inc_cpu_power(sd->groups, group->__cpu_power);
  6742. group = group->next;
  6743. } while (group != child->groups);
  6744. }
  6745. /*
  6746. * Initializers for schedule domains
  6747. * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
  6748. */
  6749. #ifdef CONFIG_SCHED_DEBUG
  6750. # define SD_INIT_NAME(sd, type) sd->name = #type
  6751. #else
  6752. # define SD_INIT_NAME(sd, type) do { } while (0)
  6753. #endif
  6754. #define SD_INIT(sd, type) sd_init_##type(sd)
  6755. #define SD_INIT_FUNC(type) \
  6756. static noinline void sd_init_##type(struct sched_domain *sd) \
  6757. { \
  6758. memset(sd, 0, sizeof(*sd)); \
  6759. *sd = SD_##type##_INIT; \
  6760. sd->level = SD_LV_##type; \
  6761. SD_INIT_NAME(sd, type); \
  6762. }
  6763. SD_INIT_FUNC(CPU)
  6764. #ifdef CONFIG_NUMA
  6765. SD_INIT_FUNC(ALLNODES)
  6766. SD_INIT_FUNC(NODE)
  6767. #endif
  6768. #ifdef CONFIG_SCHED_SMT
  6769. SD_INIT_FUNC(SIBLING)
  6770. #endif
  6771. #ifdef CONFIG_SCHED_MC
  6772. SD_INIT_FUNC(MC)
  6773. #endif
  6774. static int default_relax_domain_level = -1;
  6775. static int __init setup_relax_domain_level(char *str)
  6776. {
  6777. unsigned long val;
  6778. val = simple_strtoul(str, NULL, 0);
  6779. if (val < SD_LV_MAX)
  6780. default_relax_domain_level = val;
  6781. return 1;
  6782. }
  6783. __setup("relax_domain_level=", setup_relax_domain_level);
  6784. static void set_domain_attribute(struct sched_domain *sd,
  6785. struct sched_domain_attr *attr)
  6786. {
  6787. int request;
  6788. if (!attr || attr->relax_domain_level < 0) {
  6789. if (default_relax_domain_level < 0)
  6790. return;
  6791. else
  6792. request = default_relax_domain_level;
  6793. } else
  6794. request = attr->relax_domain_level;
  6795. if (request < sd->level) {
  6796. /* turn off idle balance on this domain */
  6797. sd->flags &= ~(SD_WAKE_IDLE|SD_BALANCE_NEWIDLE);
  6798. } else {
  6799. /* turn on idle balance on this domain */
  6800. sd->flags |= (SD_WAKE_IDLE_FAR|SD_BALANCE_NEWIDLE);
  6801. }
  6802. }
  6803. /*
  6804. * Build sched domains for a given set of cpus and attach the sched domains
  6805. * to the individual cpus
  6806. */
  6807. static int __build_sched_domains(const struct cpumask *cpu_map,
  6808. struct sched_domain_attr *attr)
  6809. {
  6810. int i, err = -ENOMEM;
  6811. struct root_domain *rd;
  6812. cpumask_var_t nodemask, this_sibling_map, this_core_map, send_covered,
  6813. tmpmask;
  6814. #ifdef CONFIG_NUMA
  6815. cpumask_var_t domainspan, covered, notcovered;
  6816. struct sched_group **sched_group_nodes = NULL;
  6817. int sd_allnodes = 0;
  6818. if (!alloc_cpumask_var(&domainspan, GFP_KERNEL))
  6819. goto out;
  6820. if (!alloc_cpumask_var(&covered, GFP_KERNEL))
  6821. goto free_domainspan;
  6822. if (!alloc_cpumask_var(&notcovered, GFP_KERNEL))
  6823. goto free_covered;
  6824. #endif
  6825. if (!alloc_cpumask_var(&nodemask, GFP_KERNEL))
  6826. goto free_notcovered;
  6827. if (!alloc_cpumask_var(&this_sibling_map, GFP_KERNEL))
  6828. goto free_nodemask;
  6829. if (!alloc_cpumask_var(&this_core_map, GFP_KERNEL))
  6830. goto free_this_sibling_map;
  6831. if (!alloc_cpumask_var(&send_covered, GFP_KERNEL))
  6832. goto free_this_core_map;
  6833. if (!alloc_cpumask_var(&tmpmask, GFP_KERNEL))
  6834. goto free_send_covered;
  6835. #ifdef CONFIG_NUMA
  6836. /*
  6837. * Allocate the per-node list of sched groups
  6838. */
  6839. sched_group_nodes = kcalloc(nr_node_ids, sizeof(struct sched_group *),
  6840. GFP_KERNEL);
  6841. if (!sched_group_nodes) {
  6842. printk(KERN_WARNING "Can not alloc sched group node list\n");
  6843. goto free_tmpmask;
  6844. }
  6845. #endif
  6846. rd = alloc_rootdomain();
  6847. if (!rd) {
  6848. printk(KERN_WARNING "Cannot alloc root domain\n");
  6849. goto free_sched_groups;
  6850. }
  6851. #ifdef CONFIG_NUMA
  6852. sched_group_nodes_bycpu[cpumask_first(cpu_map)] = sched_group_nodes;
  6853. #endif
  6854. /*
  6855. * Set up domains for cpus specified by the cpu_map.
  6856. */
  6857. for_each_cpu(i, cpu_map) {
  6858. struct sched_domain *sd = NULL, *p;
  6859. cpumask_and(nodemask, cpumask_of_node(cpu_to_node(i)), cpu_map);
  6860. #ifdef CONFIG_NUMA
  6861. if (cpumask_weight(cpu_map) >
  6862. SD_NODES_PER_DOMAIN*cpumask_weight(nodemask)) {
  6863. sd = &per_cpu(allnodes_domains, i).sd;
  6864. SD_INIT(sd, ALLNODES);
  6865. set_domain_attribute(sd, attr);
  6866. cpumask_copy(sched_domain_span(sd), cpu_map);
  6867. cpu_to_allnodes_group(i, cpu_map, &sd->groups, tmpmask);
  6868. p = sd;
  6869. sd_allnodes = 1;
  6870. } else
  6871. p = NULL;
  6872. sd = &per_cpu(node_domains, i).sd;
  6873. SD_INIT(sd, NODE);
  6874. set_domain_attribute(sd, attr);
  6875. sched_domain_node_span(cpu_to_node(i), sched_domain_span(sd));
  6876. sd->parent = p;
  6877. if (p)
  6878. p->child = sd;
  6879. cpumask_and(sched_domain_span(sd),
  6880. sched_domain_span(sd), cpu_map);
  6881. #endif
  6882. p = sd;
  6883. sd = &per_cpu(phys_domains, i).sd;
  6884. SD_INIT(sd, CPU);
  6885. set_domain_attribute(sd, attr);
  6886. cpumask_copy(sched_domain_span(sd), nodemask);
  6887. sd->parent = p;
  6888. if (p)
  6889. p->child = sd;
  6890. cpu_to_phys_group(i, cpu_map, &sd->groups, tmpmask);
  6891. #ifdef CONFIG_SCHED_MC
  6892. p = sd;
  6893. sd = &per_cpu(core_domains, i).sd;
  6894. SD_INIT(sd, MC);
  6895. set_domain_attribute(sd, attr);
  6896. cpumask_and(sched_domain_span(sd), cpu_map,
  6897. cpu_coregroup_mask(i));
  6898. sd->parent = p;
  6899. p->child = sd;
  6900. cpu_to_core_group(i, cpu_map, &sd->groups, tmpmask);
  6901. #endif
  6902. #ifdef CONFIG_SCHED_SMT
  6903. p = sd;
  6904. sd = &per_cpu(cpu_domains, i).sd;
  6905. SD_INIT(sd, SIBLING);
  6906. set_domain_attribute(sd, attr);
  6907. cpumask_and(sched_domain_span(sd),
  6908. topology_thread_cpumask(i), cpu_map);
  6909. sd->parent = p;
  6910. p->child = sd;
  6911. cpu_to_cpu_group(i, cpu_map, &sd->groups, tmpmask);
  6912. #endif
  6913. }
  6914. #ifdef CONFIG_SCHED_SMT
  6915. /* Set up CPU (sibling) groups */
  6916. for_each_cpu(i, cpu_map) {
  6917. cpumask_and(this_sibling_map,
  6918. topology_thread_cpumask(i), cpu_map);
  6919. if (i != cpumask_first(this_sibling_map))
  6920. continue;
  6921. init_sched_build_groups(this_sibling_map, cpu_map,
  6922. &cpu_to_cpu_group,
  6923. send_covered, tmpmask);
  6924. }
  6925. #endif
  6926. #ifdef CONFIG_SCHED_MC
  6927. /* Set up multi-core groups */
  6928. for_each_cpu(i, cpu_map) {
  6929. cpumask_and(this_core_map, cpu_coregroup_mask(i), cpu_map);
  6930. if (i != cpumask_first(this_core_map))
  6931. continue;
  6932. init_sched_build_groups(this_core_map, cpu_map,
  6933. &cpu_to_core_group,
  6934. send_covered, tmpmask);
  6935. }
  6936. #endif
  6937. /* Set up physical groups */
  6938. for (i = 0; i < nr_node_ids; i++) {
  6939. cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
  6940. if (cpumask_empty(nodemask))
  6941. continue;
  6942. init_sched_build_groups(nodemask, cpu_map,
  6943. &cpu_to_phys_group,
  6944. send_covered, tmpmask);
  6945. }
  6946. #ifdef CONFIG_NUMA
  6947. /* Set up node groups */
  6948. if (sd_allnodes) {
  6949. init_sched_build_groups(cpu_map, cpu_map,
  6950. &cpu_to_allnodes_group,
  6951. send_covered, tmpmask);
  6952. }
  6953. for (i = 0; i < nr_node_ids; i++) {
  6954. /* Set up node groups */
  6955. struct sched_group *sg, *prev;
  6956. int j;
  6957. cpumask_clear(covered);
  6958. cpumask_and(nodemask, cpumask_of_node(i), cpu_map);
  6959. if (cpumask_empty(nodemask)) {
  6960. sched_group_nodes[i] = NULL;
  6961. continue;
  6962. }
  6963. sched_domain_node_span(i, domainspan);
  6964. cpumask_and(domainspan, domainspan, cpu_map);
  6965. sg = kmalloc_node(sizeof(struct sched_group) + cpumask_size(),
  6966. GFP_KERNEL, i);
  6967. if (!sg) {
  6968. printk(KERN_WARNING "Can not alloc domain group for "
  6969. "node %d\n", i);
  6970. goto error;
  6971. }
  6972. sched_group_nodes[i] = sg;
  6973. for_each_cpu(j, nodemask) {
  6974. struct sched_domain *sd;
  6975. sd = &per_cpu(node_domains, j).sd;
  6976. sd->groups = sg;
  6977. }
  6978. sg->__cpu_power = 0;
  6979. cpumask_copy(sched_group_cpus(sg), nodemask);
  6980. sg->next = sg;
  6981. cpumask_or(covered, covered, nodemask);
  6982. prev = sg;
  6983. for (j = 0; j < nr_node_ids; j++) {
  6984. int n = (i + j) % nr_node_ids;
  6985. cpumask_complement(notcovered, covered);
  6986. cpumask_and(tmpmask, notcovered, cpu_map);
  6987. cpumask_and(tmpmask, tmpmask, domainspan);
  6988. if (cpumask_empty(tmpmask))
  6989. break;
  6990. cpumask_and(tmpmask, tmpmask, cpumask_of_node(n));
  6991. if (cpumask_empty(tmpmask))
  6992. continue;
  6993. sg = kmalloc_node(sizeof(struct sched_group) +
  6994. cpumask_size(),
  6995. GFP_KERNEL, i);
  6996. if (!sg) {
  6997. printk(KERN_WARNING
  6998. "Can not alloc domain group for node %d\n", j);
  6999. goto error;
  7000. }
  7001. sg->__cpu_power = 0;
  7002. cpumask_copy(sched_group_cpus(sg), tmpmask);
  7003. sg->next = prev->next;
  7004. cpumask_or(covered, covered, tmpmask);
  7005. prev->next = sg;
  7006. prev = sg;
  7007. }
  7008. }
  7009. #endif
  7010. /* Calculate CPU power for physical packages and nodes */
  7011. #ifdef CONFIG_SCHED_SMT
  7012. for_each_cpu(i, cpu_map) {
  7013. struct sched_domain *sd = &per_cpu(cpu_domains, i).sd;
  7014. init_sched_groups_power(i, sd);
  7015. }
  7016. #endif
  7017. #ifdef CONFIG_SCHED_MC
  7018. for_each_cpu(i, cpu_map) {
  7019. struct sched_domain *sd = &per_cpu(core_domains, i).sd;
  7020. init_sched_groups_power(i, sd);
  7021. }
  7022. #endif
  7023. for_each_cpu(i, cpu_map) {
  7024. struct sched_domain *sd = &per_cpu(phys_domains, i).sd;
  7025. init_sched_groups_power(i, sd);
  7026. }
  7027. #ifdef CONFIG_NUMA
  7028. for (i = 0; i < nr_node_ids; i++)
  7029. init_numa_sched_groups_power(sched_group_nodes[i]);
  7030. if (sd_allnodes) {
  7031. struct sched_group *sg;
  7032. cpu_to_allnodes_group(cpumask_first(cpu_map), cpu_map, &sg,
  7033. tmpmask);
  7034. init_numa_sched_groups_power(sg);
  7035. }
  7036. #endif
  7037. /* Attach the domains */
  7038. for_each_cpu(i, cpu_map) {
  7039. struct sched_domain *sd;
  7040. #ifdef CONFIG_SCHED_SMT
  7041. sd = &per_cpu(cpu_domains, i).sd;
  7042. #elif defined(CONFIG_SCHED_MC)
  7043. sd = &per_cpu(core_domains, i).sd;
  7044. #else
  7045. sd = &per_cpu(phys_domains, i).sd;
  7046. #endif
  7047. cpu_attach_domain(sd, rd, i);
  7048. }
  7049. err = 0;
  7050. free_tmpmask:
  7051. free_cpumask_var(tmpmask);
  7052. free_send_covered:
  7053. free_cpumask_var(send_covered);
  7054. free_this_core_map:
  7055. free_cpumask_var(this_core_map);
  7056. free_this_sibling_map:
  7057. free_cpumask_var(this_sibling_map);
  7058. free_nodemask:
  7059. free_cpumask_var(nodemask);
  7060. free_notcovered:
  7061. #ifdef CONFIG_NUMA
  7062. free_cpumask_var(notcovered);
  7063. free_covered:
  7064. free_cpumask_var(covered);
  7065. free_domainspan:
  7066. free_cpumask_var(domainspan);
  7067. out:
  7068. #endif
  7069. return err;
  7070. free_sched_groups:
  7071. #ifdef CONFIG_NUMA
  7072. kfree(sched_group_nodes);
  7073. #endif
  7074. goto free_tmpmask;
  7075. #ifdef CONFIG_NUMA
  7076. error:
  7077. free_sched_groups(cpu_map, tmpmask);
  7078. free_rootdomain(rd);
  7079. goto free_tmpmask;
  7080. #endif
  7081. }
  7082. static int build_sched_domains(const struct cpumask *cpu_map)
  7083. {
  7084. return __build_sched_domains(cpu_map, NULL);
  7085. }
  7086. static struct cpumask *doms_cur; /* current sched domains */
  7087. static int ndoms_cur; /* number of sched domains in 'doms_cur' */
  7088. static struct sched_domain_attr *dattr_cur;
  7089. /* attribues of custom domains in 'doms_cur' */
  7090. /*
  7091. * Special case: If a kmalloc of a doms_cur partition (array of
  7092. * cpumask) fails, then fallback to a single sched domain,
  7093. * as determined by the single cpumask fallback_doms.
  7094. */
  7095. static cpumask_var_t fallback_doms;
  7096. /*
  7097. * arch_update_cpu_topology lets virtualized architectures update the
  7098. * cpu core maps. It is supposed to return 1 if the topology changed
  7099. * or 0 if it stayed the same.
  7100. */
  7101. int __attribute__((weak)) arch_update_cpu_topology(void)
  7102. {
  7103. return 0;
  7104. }
  7105. /*
  7106. * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  7107. * For now this just excludes isolated cpus, but could be used to
  7108. * exclude other special cases in the future.
  7109. */
  7110. static int arch_init_sched_domains(const struct cpumask *cpu_map)
  7111. {
  7112. int err;
  7113. arch_update_cpu_topology();
  7114. ndoms_cur = 1;
  7115. doms_cur = kmalloc(cpumask_size(), GFP_KERNEL);
  7116. if (!doms_cur)
  7117. doms_cur = fallback_doms;
  7118. cpumask_andnot(doms_cur, cpu_map, cpu_isolated_map);
  7119. dattr_cur = NULL;
  7120. err = build_sched_domains(doms_cur);
  7121. register_sched_domain_sysctl();
  7122. return err;
  7123. }
  7124. static void arch_destroy_sched_domains(const struct cpumask *cpu_map,
  7125. struct cpumask *tmpmask)
  7126. {
  7127. free_sched_groups(cpu_map, tmpmask);
  7128. }
  7129. /*
  7130. * Detach sched domains from a group of cpus specified in cpu_map
  7131. * These cpus will now be attached to the NULL domain
  7132. */
  7133. static void detach_destroy_domains(const struct cpumask *cpu_map)
  7134. {
  7135. /* Save because hotplug lock held. */
  7136. static DECLARE_BITMAP(tmpmask, CONFIG_NR_CPUS);
  7137. int i;
  7138. for_each_cpu(i, cpu_map)
  7139. cpu_attach_domain(NULL, &def_root_domain, i);
  7140. synchronize_sched();
  7141. arch_destroy_sched_domains(cpu_map, to_cpumask(tmpmask));
  7142. }
  7143. /* handle null as "default" */
  7144. static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
  7145. struct sched_domain_attr *new, int idx_new)
  7146. {
  7147. struct sched_domain_attr tmp;
  7148. /* fast path */
  7149. if (!new && !cur)
  7150. return 1;
  7151. tmp = SD_ATTR_INIT;
  7152. return !memcmp(cur ? (cur + idx_cur) : &tmp,
  7153. new ? (new + idx_new) : &tmp,
  7154. sizeof(struct sched_domain_attr));
  7155. }
  7156. /*
  7157. * Partition sched domains as specified by the 'ndoms_new'
  7158. * cpumasks in the array doms_new[] of cpumasks. This compares
  7159. * doms_new[] to the current sched domain partitioning, doms_cur[].
  7160. * It destroys each deleted domain and builds each new domain.
  7161. *
  7162. * 'doms_new' is an array of cpumask's of length 'ndoms_new'.
  7163. * The masks don't intersect (don't overlap.) We should setup one
  7164. * sched domain for each mask. CPUs not in any of the cpumasks will
  7165. * not be load balanced. If the same cpumask appears both in the
  7166. * current 'doms_cur' domains and in the new 'doms_new', we can leave
  7167. * it as it is.
  7168. *
  7169. * The passed in 'doms_new' should be kmalloc'd. This routine takes
  7170. * ownership of it and will kfree it when done with it. If the caller
  7171. * failed the kmalloc call, then it can pass in doms_new == NULL &&
  7172. * ndoms_new == 1, and partition_sched_domains() will fallback to
  7173. * the single partition 'fallback_doms', it also forces the domains
  7174. * to be rebuilt.
  7175. *
  7176. * If doms_new == NULL it will be replaced with cpu_online_mask.
  7177. * ndoms_new == 0 is a special case for destroying existing domains,
  7178. * and it will not create the default domain.
  7179. *
  7180. * Call with hotplug lock held
  7181. */
  7182. /* FIXME: Change to struct cpumask *doms_new[] */
  7183. void partition_sched_domains(int ndoms_new, struct cpumask *doms_new,
  7184. struct sched_domain_attr *dattr_new)
  7185. {
  7186. int i, j, n;
  7187. int new_topology;
  7188. mutex_lock(&sched_domains_mutex);
  7189. /* always unregister in case we don't destroy any domains */
  7190. unregister_sched_domain_sysctl();
  7191. /* Let architecture update cpu core mappings. */
  7192. new_topology = arch_update_cpu_topology();
  7193. n = doms_new ? ndoms_new : 0;
  7194. /* Destroy deleted domains */
  7195. for (i = 0; i < ndoms_cur; i++) {
  7196. for (j = 0; j < n && !new_topology; j++) {
  7197. if (cpumask_equal(&doms_cur[i], &doms_new[j])
  7198. && dattrs_equal(dattr_cur, i, dattr_new, j))
  7199. goto match1;
  7200. }
  7201. /* no match - a current sched domain not in new doms_new[] */
  7202. detach_destroy_domains(doms_cur + i);
  7203. match1:
  7204. ;
  7205. }
  7206. if (doms_new == NULL) {
  7207. ndoms_cur = 0;
  7208. doms_new = fallback_doms;
  7209. cpumask_andnot(&doms_new[0], cpu_online_mask, cpu_isolated_map);
  7210. WARN_ON_ONCE(dattr_new);
  7211. }
  7212. /* Build new domains */
  7213. for (i = 0; i < ndoms_new; i++) {
  7214. for (j = 0; j < ndoms_cur && !new_topology; j++) {
  7215. if (cpumask_equal(&doms_new[i], &doms_cur[j])
  7216. && dattrs_equal(dattr_new, i, dattr_cur, j))
  7217. goto match2;
  7218. }
  7219. /* no match - add a new doms_new */
  7220. __build_sched_domains(doms_new + i,
  7221. dattr_new ? dattr_new + i : NULL);
  7222. match2:
  7223. ;
  7224. }
  7225. /* Remember the new sched domains */
  7226. if (doms_cur != fallback_doms)
  7227. kfree(doms_cur);
  7228. kfree(dattr_cur); /* kfree(NULL) is safe */
  7229. doms_cur = doms_new;
  7230. dattr_cur = dattr_new;
  7231. ndoms_cur = ndoms_new;
  7232. register_sched_domain_sysctl();
  7233. mutex_unlock(&sched_domains_mutex);
  7234. }
  7235. #if defined(CONFIG_SCHED_MC) || defined(CONFIG_SCHED_SMT)
  7236. static void arch_reinit_sched_domains(void)
  7237. {
  7238. get_online_cpus();
  7239. /* Destroy domains first to force the rebuild */
  7240. partition_sched_domains(0, NULL, NULL);
  7241. rebuild_sched_domains();
  7242. put_online_cpus();
  7243. }
  7244. static ssize_t sched_power_savings_store(const char *buf, size_t count, int smt)
  7245. {
  7246. unsigned int level = 0;
  7247. if (sscanf(buf, "%u", &level) != 1)
  7248. return -EINVAL;
  7249. /*
  7250. * level is always be positive so don't check for
  7251. * level < POWERSAVINGS_BALANCE_NONE which is 0
  7252. * What happens on 0 or 1 byte write,
  7253. * need to check for count as well?
  7254. */
  7255. if (level >= MAX_POWERSAVINGS_BALANCE_LEVELS)
  7256. return -EINVAL;
  7257. if (smt)
  7258. sched_smt_power_savings = level;
  7259. else
  7260. sched_mc_power_savings = level;
  7261. arch_reinit_sched_domains();
  7262. return count;
  7263. }
  7264. #ifdef CONFIG_SCHED_MC
  7265. static ssize_t sched_mc_power_savings_show(struct sysdev_class *class,
  7266. char *page)
  7267. {
  7268. return sprintf(page, "%u\n", sched_mc_power_savings);
  7269. }
  7270. static ssize_t sched_mc_power_savings_store(struct sysdev_class *class,
  7271. const char *buf, size_t count)
  7272. {
  7273. return sched_power_savings_store(buf, count, 0);
  7274. }
  7275. static SYSDEV_CLASS_ATTR(sched_mc_power_savings, 0644,
  7276. sched_mc_power_savings_show,
  7277. sched_mc_power_savings_store);
  7278. #endif
  7279. #ifdef CONFIG_SCHED_SMT
  7280. static ssize_t sched_smt_power_savings_show(struct sysdev_class *dev,
  7281. char *page)
  7282. {
  7283. return sprintf(page, "%u\n", sched_smt_power_savings);
  7284. }
  7285. static ssize_t sched_smt_power_savings_store(struct sysdev_class *dev,
  7286. const char *buf, size_t count)
  7287. {
  7288. return sched_power_savings_store(buf, count, 1);
  7289. }
  7290. static SYSDEV_CLASS_ATTR(sched_smt_power_savings, 0644,
  7291. sched_smt_power_savings_show,
  7292. sched_smt_power_savings_store);
  7293. #endif
  7294. int __init sched_create_sysfs_power_savings_entries(struct sysdev_class *cls)
  7295. {
  7296. int err = 0;
  7297. #ifdef CONFIG_SCHED_SMT
  7298. if (smt_capable())
  7299. err = sysfs_create_file(&cls->kset.kobj,
  7300. &attr_sched_smt_power_savings.attr);
  7301. #endif
  7302. #ifdef CONFIG_SCHED_MC
  7303. if (!err && mc_capable())
  7304. err = sysfs_create_file(&cls->kset.kobj,
  7305. &attr_sched_mc_power_savings.attr);
  7306. #endif
  7307. return err;
  7308. }
  7309. #endif /* CONFIG_SCHED_MC || CONFIG_SCHED_SMT */
  7310. #ifndef CONFIG_CPUSETS
  7311. /*
  7312. * Add online and remove offline CPUs from the scheduler domains.
  7313. * When cpusets are enabled they take over this function.
  7314. */
  7315. static int update_sched_domains(struct notifier_block *nfb,
  7316. unsigned long action, void *hcpu)
  7317. {
  7318. switch (action) {
  7319. case CPU_ONLINE:
  7320. case CPU_ONLINE_FROZEN:
  7321. case CPU_DEAD:
  7322. case CPU_DEAD_FROZEN:
  7323. partition_sched_domains(1, NULL, NULL);
  7324. return NOTIFY_OK;
  7325. default:
  7326. return NOTIFY_DONE;
  7327. }
  7328. }
  7329. #endif
  7330. static int update_runtime(struct notifier_block *nfb,
  7331. unsigned long action, void *hcpu)
  7332. {
  7333. int cpu = (int)(long)hcpu;
  7334. switch (action) {
  7335. case CPU_DOWN_PREPARE:
  7336. case CPU_DOWN_PREPARE_FROZEN:
  7337. disable_runtime(cpu_rq(cpu));
  7338. return NOTIFY_OK;
  7339. case CPU_DOWN_FAILED:
  7340. case CPU_DOWN_FAILED_FROZEN:
  7341. case CPU_ONLINE:
  7342. case CPU_ONLINE_FROZEN:
  7343. enable_runtime(cpu_rq(cpu));
  7344. return NOTIFY_OK;
  7345. default:
  7346. return NOTIFY_DONE;
  7347. }
  7348. }
  7349. void __init sched_init_smp(void)
  7350. {
  7351. cpumask_var_t non_isolated_cpus;
  7352. alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
  7353. #if defined(CONFIG_NUMA)
  7354. sched_group_nodes_bycpu = kzalloc(nr_cpu_ids * sizeof(void **),
  7355. GFP_KERNEL);
  7356. BUG_ON(sched_group_nodes_bycpu == NULL);
  7357. #endif
  7358. get_online_cpus();
  7359. mutex_lock(&sched_domains_mutex);
  7360. arch_init_sched_domains(cpu_online_mask);
  7361. cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
  7362. if (cpumask_empty(non_isolated_cpus))
  7363. cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
  7364. mutex_unlock(&sched_domains_mutex);
  7365. put_online_cpus();
  7366. #ifndef CONFIG_CPUSETS
  7367. /* XXX: Theoretical race here - CPU may be hotplugged now */
  7368. hotcpu_notifier(update_sched_domains, 0);
  7369. #endif
  7370. /* RT runtime code needs to handle some hotplug events */
  7371. hotcpu_notifier(update_runtime, 0);
  7372. init_hrtick();
  7373. /* Move init over to a non-isolated CPU */
  7374. if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
  7375. BUG();
  7376. sched_init_granularity();
  7377. free_cpumask_var(non_isolated_cpus);
  7378. alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
  7379. init_sched_rt_class();
  7380. }
  7381. #else
  7382. void __init sched_init_smp(void)
  7383. {
  7384. sched_init_granularity();
  7385. }
  7386. #endif /* CONFIG_SMP */
  7387. int in_sched_functions(unsigned long addr)
  7388. {
  7389. return in_lock_functions(addr) ||
  7390. (addr >= (unsigned long)__sched_text_start
  7391. && addr < (unsigned long)__sched_text_end);
  7392. }
  7393. static void init_cfs_rq(struct cfs_rq *cfs_rq, struct rq *rq)
  7394. {
  7395. cfs_rq->tasks_timeline = RB_ROOT;
  7396. INIT_LIST_HEAD(&cfs_rq->tasks);
  7397. #ifdef CONFIG_FAIR_GROUP_SCHED
  7398. cfs_rq->rq = rq;
  7399. #endif
  7400. cfs_rq->min_vruntime = (u64)(-(1LL << 20));
  7401. }
  7402. static void init_rt_rq(struct rt_rq *rt_rq, struct rq *rq)
  7403. {
  7404. struct rt_prio_array *array;
  7405. int i;
  7406. array = &rt_rq->active;
  7407. for (i = 0; i < MAX_RT_PRIO; i++) {
  7408. INIT_LIST_HEAD(array->queue + i);
  7409. __clear_bit(i, array->bitmap);
  7410. }
  7411. /* delimiter for bitsearch: */
  7412. __set_bit(MAX_RT_PRIO, array->bitmap);
  7413. #if defined CONFIG_SMP || defined CONFIG_RT_GROUP_SCHED
  7414. rt_rq->highest_prio.curr = MAX_RT_PRIO;
  7415. #ifdef CONFIG_SMP
  7416. rt_rq->highest_prio.next = MAX_RT_PRIO;
  7417. #endif
  7418. #endif
  7419. #ifdef CONFIG_SMP
  7420. rt_rq->rt_nr_migratory = 0;
  7421. rt_rq->overloaded = 0;
  7422. plist_head_init(&rq->rt.pushable_tasks, &rq->lock);
  7423. #endif
  7424. rt_rq->rt_time = 0;
  7425. rt_rq->rt_throttled = 0;
  7426. rt_rq->rt_runtime = 0;
  7427. spin_lock_init(&rt_rq->rt_runtime_lock);
  7428. #ifdef CONFIG_RT_GROUP_SCHED
  7429. rt_rq->rt_nr_boosted = 0;
  7430. rt_rq->rq = rq;
  7431. #endif
  7432. }
  7433. #ifdef CONFIG_FAIR_GROUP_SCHED
  7434. static void init_tg_cfs_entry(struct task_group *tg, struct cfs_rq *cfs_rq,
  7435. struct sched_entity *se, int cpu, int add,
  7436. struct sched_entity *parent)
  7437. {
  7438. struct rq *rq = cpu_rq(cpu);
  7439. tg->cfs_rq[cpu] = cfs_rq;
  7440. init_cfs_rq(cfs_rq, rq);
  7441. cfs_rq->tg = tg;
  7442. if (add)
  7443. list_add(&cfs_rq->leaf_cfs_rq_list, &rq->leaf_cfs_rq_list);
  7444. tg->se[cpu] = se;
  7445. /* se could be NULL for init_task_group */
  7446. if (!se)
  7447. return;
  7448. if (!parent)
  7449. se->cfs_rq = &rq->cfs;
  7450. else
  7451. se->cfs_rq = parent->my_q;
  7452. se->my_q = cfs_rq;
  7453. se->load.weight = tg->shares;
  7454. se->load.inv_weight = 0;
  7455. se->parent = parent;
  7456. }
  7457. #endif
  7458. #ifdef CONFIG_RT_GROUP_SCHED
  7459. static void init_tg_rt_entry(struct task_group *tg, struct rt_rq *rt_rq,
  7460. struct sched_rt_entity *rt_se, int cpu, int add,
  7461. struct sched_rt_entity *parent)
  7462. {
  7463. struct rq *rq = cpu_rq(cpu);
  7464. tg->rt_rq[cpu] = rt_rq;
  7465. init_rt_rq(rt_rq, rq);
  7466. rt_rq->tg = tg;
  7467. rt_rq->rt_se = rt_se;
  7468. rt_rq->rt_runtime = tg->rt_bandwidth.rt_runtime;
  7469. if (add)
  7470. list_add(&rt_rq->leaf_rt_rq_list, &rq->leaf_rt_rq_list);
  7471. tg->rt_se[cpu] = rt_se;
  7472. if (!rt_se)
  7473. return;
  7474. if (!parent)
  7475. rt_se->rt_rq = &rq->rt;
  7476. else
  7477. rt_se->rt_rq = parent->my_q;
  7478. rt_se->my_q = rt_rq;
  7479. rt_se->parent = parent;
  7480. INIT_LIST_HEAD(&rt_se->run_list);
  7481. }
  7482. #endif
  7483. void __init sched_init(void)
  7484. {
  7485. int i, j;
  7486. unsigned long alloc_size = 0, ptr;
  7487. #ifdef CONFIG_FAIR_GROUP_SCHED
  7488. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  7489. #endif
  7490. #ifdef CONFIG_RT_GROUP_SCHED
  7491. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  7492. #endif
  7493. #ifdef CONFIG_USER_SCHED
  7494. alloc_size *= 2;
  7495. #endif
  7496. #ifdef CONFIG_CPUMASK_OFFSTACK
  7497. alloc_size += num_possible_cpus() * cpumask_size();
  7498. #endif
  7499. /*
  7500. * As sched_init() is called before page_alloc is setup,
  7501. * we use alloc_bootmem().
  7502. */
  7503. if (alloc_size) {
  7504. ptr = (unsigned long)alloc_bootmem(alloc_size);
  7505. #ifdef CONFIG_FAIR_GROUP_SCHED
  7506. init_task_group.se = (struct sched_entity **)ptr;
  7507. ptr += nr_cpu_ids * sizeof(void **);
  7508. init_task_group.cfs_rq = (struct cfs_rq **)ptr;
  7509. ptr += nr_cpu_ids * sizeof(void **);
  7510. #ifdef CONFIG_USER_SCHED
  7511. root_task_group.se = (struct sched_entity **)ptr;
  7512. ptr += nr_cpu_ids * sizeof(void **);
  7513. root_task_group.cfs_rq = (struct cfs_rq **)ptr;
  7514. ptr += nr_cpu_ids * sizeof(void **);
  7515. #endif /* CONFIG_USER_SCHED */
  7516. #endif /* CONFIG_FAIR_GROUP_SCHED */
  7517. #ifdef CONFIG_RT_GROUP_SCHED
  7518. init_task_group.rt_se = (struct sched_rt_entity **)ptr;
  7519. ptr += nr_cpu_ids * sizeof(void **);
  7520. init_task_group.rt_rq = (struct rt_rq **)ptr;
  7521. ptr += nr_cpu_ids * sizeof(void **);
  7522. #ifdef CONFIG_USER_SCHED
  7523. root_task_group.rt_se = (struct sched_rt_entity **)ptr;
  7524. ptr += nr_cpu_ids * sizeof(void **);
  7525. root_task_group.rt_rq = (struct rt_rq **)ptr;
  7526. ptr += nr_cpu_ids * sizeof(void **);
  7527. #endif /* CONFIG_USER_SCHED */
  7528. #endif /* CONFIG_RT_GROUP_SCHED */
  7529. #ifdef CONFIG_CPUMASK_OFFSTACK
  7530. for_each_possible_cpu(i) {
  7531. per_cpu(load_balance_tmpmask, i) = (void *)ptr;
  7532. ptr += cpumask_size();
  7533. }
  7534. #endif /* CONFIG_CPUMASK_OFFSTACK */
  7535. }
  7536. #ifdef CONFIG_SMP
  7537. init_defrootdomain();
  7538. #endif
  7539. init_rt_bandwidth(&def_rt_bandwidth,
  7540. global_rt_period(), global_rt_runtime());
  7541. #ifdef CONFIG_RT_GROUP_SCHED
  7542. init_rt_bandwidth(&init_task_group.rt_bandwidth,
  7543. global_rt_period(), global_rt_runtime());
  7544. #ifdef CONFIG_USER_SCHED
  7545. init_rt_bandwidth(&root_task_group.rt_bandwidth,
  7546. global_rt_period(), RUNTIME_INF);
  7547. #endif /* CONFIG_USER_SCHED */
  7548. #endif /* CONFIG_RT_GROUP_SCHED */
  7549. #ifdef CONFIG_GROUP_SCHED
  7550. list_add(&init_task_group.list, &task_groups);
  7551. INIT_LIST_HEAD(&init_task_group.children);
  7552. #ifdef CONFIG_USER_SCHED
  7553. INIT_LIST_HEAD(&root_task_group.children);
  7554. init_task_group.parent = &root_task_group;
  7555. list_add(&init_task_group.siblings, &root_task_group.children);
  7556. #endif /* CONFIG_USER_SCHED */
  7557. #endif /* CONFIG_GROUP_SCHED */
  7558. for_each_possible_cpu(i) {
  7559. struct rq *rq;
  7560. rq = cpu_rq(i);
  7561. spin_lock_init(&rq->lock);
  7562. rq->nr_running = 0;
  7563. init_cfs_rq(&rq->cfs, rq);
  7564. init_rt_rq(&rq->rt, rq);
  7565. #ifdef CONFIG_FAIR_GROUP_SCHED
  7566. init_task_group.shares = init_task_group_load;
  7567. INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
  7568. #ifdef CONFIG_CGROUP_SCHED
  7569. /*
  7570. * How much cpu bandwidth does init_task_group get?
  7571. *
  7572. * In case of task-groups formed thr' the cgroup filesystem, it
  7573. * gets 100% of the cpu resources in the system. This overall
  7574. * system cpu resource is divided among the tasks of
  7575. * init_task_group and its child task-groups in a fair manner,
  7576. * based on each entity's (task or task-group's) weight
  7577. * (se->load.weight).
  7578. *
  7579. * In other words, if init_task_group has 10 tasks of weight
  7580. * 1024) and two child groups A0 and A1 (of weight 1024 each),
  7581. * then A0's share of the cpu resource is:
  7582. *
  7583. * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
  7584. *
  7585. * We achieve this by letting init_task_group's tasks sit
  7586. * directly in rq->cfs (i.e init_task_group->se[] = NULL).
  7587. */
  7588. init_tg_cfs_entry(&init_task_group, &rq->cfs, NULL, i, 1, NULL);
  7589. #elif defined CONFIG_USER_SCHED
  7590. root_task_group.shares = NICE_0_LOAD;
  7591. init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, 0, NULL);
  7592. /*
  7593. * In case of task-groups formed thr' the user id of tasks,
  7594. * init_task_group represents tasks belonging to root user.
  7595. * Hence it forms a sibling of all subsequent groups formed.
  7596. * In this case, init_task_group gets only a fraction of overall
  7597. * system cpu resource, based on the weight assigned to root
  7598. * user's cpu share (INIT_TASK_GROUP_LOAD). This is accomplished
  7599. * by letting tasks of init_task_group sit in a separate cfs_rq
  7600. * (init_cfs_rq) and having one entity represent this group of
  7601. * tasks in rq->cfs (i.e init_task_group->se[] != NULL).
  7602. */
  7603. init_tg_cfs_entry(&init_task_group,
  7604. &per_cpu(init_cfs_rq, i),
  7605. &per_cpu(init_sched_entity, i), i, 1,
  7606. root_task_group.se[i]);
  7607. #endif
  7608. #endif /* CONFIG_FAIR_GROUP_SCHED */
  7609. rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
  7610. #ifdef CONFIG_RT_GROUP_SCHED
  7611. INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
  7612. #ifdef CONFIG_CGROUP_SCHED
  7613. init_tg_rt_entry(&init_task_group, &rq->rt, NULL, i, 1, NULL);
  7614. #elif defined CONFIG_USER_SCHED
  7615. init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, 0, NULL);
  7616. init_tg_rt_entry(&init_task_group,
  7617. &per_cpu(init_rt_rq, i),
  7618. &per_cpu(init_sched_rt_entity, i), i, 1,
  7619. root_task_group.rt_se[i]);
  7620. #endif
  7621. #endif
  7622. for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
  7623. rq->cpu_load[j] = 0;
  7624. #ifdef CONFIG_SMP
  7625. rq->sd = NULL;
  7626. rq->rd = NULL;
  7627. rq->active_balance = 0;
  7628. rq->next_balance = jiffies;
  7629. rq->push_cpu = 0;
  7630. rq->cpu = i;
  7631. rq->online = 0;
  7632. rq->migration_thread = NULL;
  7633. INIT_LIST_HEAD(&rq->migration_queue);
  7634. rq_attach_root(rq, &def_root_domain);
  7635. #endif
  7636. init_rq_hrtick(rq);
  7637. atomic_set(&rq->nr_iowait, 0);
  7638. }
  7639. set_load_weight(&init_task);
  7640. #ifdef CONFIG_PREEMPT_NOTIFIERS
  7641. INIT_HLIST_HEAD(&init_task.preempt_notifiers);
  7642. #endif
  7643. #ifdef CONFIG_SMP
  7644. open_softirq(SCHED_SOFTIRQ, run_rebalance_domains);
  7645. #endif
  7646. #ifdef CONFIG_RT_MUTEXES
  7647. plist_head_init(&init_task.pi_waiters, &init_task.pi_lock);
  7648. #endif
  7649. /*
  7650. * The boot idle thread does lazy MMU switching as well:
  7651. */
  7652. atomic_inc(&init_mm.mm_count);
  7653. enter_lazy_tlb(&init_mm, current);
  7654. /*
  7655. * Make us the idle thread. Technically, schedule() should not be
  7656. * called from this thread, however somewhere below it might be,
  7657. * but because we are the idle thread, we just pick up running again
  7658. * when this runqueue becomes "idle".
  7659. */
  7660. init_idle(current, smp_processor_id());
  7661. /*
  7662. * During early bootup we pretend to be a normal task:
  7663. */
  7664. current->sched_class = &fair_sched_class;
  7665. /* Allocate the nohz_cpu_mask if CONFIG_CPUMASK_OFFSTACK */
  7666. alloc_bootmem_cpumask_var(&nohz_cpu_mask);
  7667. #ifdef CONFIG_SMP
  7668. #ifdef CONFIG_NO_HZ
  7669. alloc_bootmem_cpumask_var(&nohz.cpu_mask);
  7670. #endif
  7671. alloc_bootmem_cpumask_var(&cpu_isolated_map);
  7672. #endif /* SMP */
  7673. scheduler_running = 1;
  7674. }
  7675. #ifdef CONFIG_DEBUG_SPINLOCK_SLEEP
  7676. void __might_sleep(char *file, int line)
  7677. {
  7678. #ifdef in_atomic
  7679. static unsigned long prev_jiffy; /* ratelimiting */
  7680. if ((!in_atomic() && !irqs_disabled()) ||
  7681. system_state != SYSTEM_RUNNING || oops_in_progress)
  7682. return;
  7683. if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  7684. return;
  7685. prev_jiffy = jiffies;
  7686. printk(KERN_ERR
  7687. "BUG: sleeping function called from invalid context at %s:%d\n",
  7688. file, line);
  7689. printk(KERN_ERR
  7690. "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
  7691. in_atomic(), irqs_disabled(),
  7692. current->pid, current->comm);
  7693. debug_show_held_locks(current);
  7694. if (irqs_disabled())
  7695. print_irqtrace_events(current);
  7696. dump_stack();
  7697. #endif
  7698. }
  7699. EXPORT_SYMBOL(__might_sleep);
  7700. #endif
  7701. #ifdef CONFIG_MAGIC_SYSRQ
  7702. static void normalize_task(struct rq *rq, struct task_struct *p)
  7703. {
  7704. int on_rq;
  7705. update_rq_clock(rq);
  7706. on_rq = p->se.on_rq;
  7707. if (on_rq)
  7708. deactivate_task(rq, p, 0);
  7709. __setscheduler(rq, p, SCHED_NORMAL, 0);
  7710. if (on_rq) {
  7711. activate_task(rq, p, 0);
  7712. resched_task(rq->curr);
  7713. }
  7714. }
  7715. void normalize_rt_tasks(void)
  7716. {
  7717. struct task_struct *g, *p;
  7718. unsigned long flags;
  7719. struct rq *rq;
  7720. read_lock_irqsave(&tasklist_lock, flags);
  7721. do_each_thread(g, p) {
  7722. /*
  7723. * Only normalize user tasks:
  7724. */
  7725. if (!p->mm)
  7726. continue;
  7727. p->se.exec_start = 0;
  7728. #ifdef CONFIG_SCHEDSTATS
  7729. p->se.wait_start = 0;
  7730. p->se.sleep_start = 0;
  7731. p->se.block_start = 0;
  7732. #endif
  7733. if (!rt_task(p)) {
  7734. /*
  7735. * Renice negative nice level userspace
  7736. * tasks back to 0:
  7737. */
  7738. if (TASK_NICE(p) < 0 && p->mm)
  7739. set_user_nice(p, 0);
  7740. continue;
  7741. }
  7742. spin_lock(&p->pi_lock);
  7743. rq = __task_rq_lock(p);
  7744. normalize_task(rq, p);
  7745. __task_rq_unlock(rq);
  7746. spin_unlock(&p->pi_lock);
  7747. } while_each_thread(g, p);
  7748. read_unlock_irqrestore(&tasklist_lock, flags);
  7749. }
  7750. #endif /* CONFIG_MAGIC_SYSRQ */
  7751. #ifdef CONFIG_IA64
  7752. /*
  7753. * These functions are only useful for the IA64 MCA handling.
  7754. *
  7755. * They can only be called when the whole system has been
  7756. * stopped - every CPU needs to be quiescent, and no scheduling
  7757. * activity can take place. Using them for anything else would
  7758. * be a serious bug, and as a result, they aren't even visible
  7759. * under any other configuration.
  7760. */
  7761. /**
  7762. * curr_task - return the current task for a given cpu.
  7763. * @cpu: the processor in question.
  7764. *
  7765. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  7766. */
  7767. struct task_struct *curr_task(int cpu)
  7768. {
  7769. return cpu_curr(cpu);
  7770. }
  7771. /**
  7772. * set_curr_task - set the current task for a given cpu.
  7773. * @cpu: the processor in question.
  7774. * @p: the task pointer to set.
  7775. *
  7776. * Description: This function must only be used when non-maskable interrupts
  7777. * are serviced on a separate stack. It allows the architecture to switch the
  7778. * notion of the current task on a cpu in a non-blocking manner. This function
  7779. * must be called with all CPU's synchronized, and interrupts disabled, the
  7780. * and caller must save the original value of the current task (see
  7781. * curr_task() above) and restore that value before reenabling interrupts and
  7782. * re-starting the system.
  7783. *
  7784. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  7785. */
  7786. void set_curr_task(int cpu, struct task_struct *p)
  7787. {
  7788. cpu_curr(cpu) = p;
  7789. }
  7790. #endif
  7791. #ifdef CONFIG_FAIR_GROUP_SCHED
  7792. static void free_fair_sched_group(struct task_group *tg)
  7793. {
  7794. int i;
  7795. for_each_possible_cpu(i) {
  7796. if (tg->cfs_rq)
  7797. kfree(tg->cfs_rq[i]);
  7798. if (tg->se)
  7799. kfree(tg->se[i]);
  7800. }
  7801. kfree(tg->cfs_rq);
  7802. kfree(tg->se);
  7803. }
  7804. static
  7805. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  7806. {
  7807. struct cfs_rq *cfs_rq;
  7808. struct sched_entity *se;
  7809. struct rq *rq;
  7810. int i;
  7811. tg->cfs_rq = kzalloc(sizeof(cfs_rq) * nr_cpu_ids, GFP_KERNEL);
  7812. if (!tg->cfs_rq)
  7813. goto err;
  7814. tg->se = kzalloc(sizeof(se) * nr_cpu_ids, GFP_KERNEL);
  7815. if (!tg->se)
  7816. goto err;
  7817. tg->shares = NICE_0_LOAD;
  7818. for_each_possible_cpu(i) {
  7819. rq = cpu_rq(i);
  7820. cfs_rq = kzalloc_node(sizeof(struct cfs_rq),
  7821. GFP_KERNEL, cpu_to_node(i));
  7822. if (!cfs_rq)
  7823. goto err;
  7824. se = kzalloc_node(sizeof(struct sched_entity),
  7825. GFP_KERNEL, cpu_to_node(i));
  7826. if (!se)
  7827. goto err;
  7828. init_tg_cfs_entry(tg, cfs_rq, se, i, 0, parent->se[i]);
  7829. }
  7830. return 1;
  7831. err:
  7832. return 0;
  7833. }
  7834. static inline void register_fair_sched_group(struct task_group *tg, int cpu)
  7835. {
  7836. list_add_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list,
  7837. &cpu_rq(cpu)->leaf_cfs_rq_list);
  7838. }
  7839. static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
  7840. {
  7841. list_del_rcu(&tg->cfs_rq[cpu]->leaf_cfs_rq_list);
  7842. }
  7843. #else /* !CONFG_FAIR_GROUP_SCHED */
  7844. static inline void free_fair_sched_group(struct task_group *tg)
  7845. {
  7846. }
  7847. static inline
  7848. int alloc_fair_sched_group(struct task_group *tg, struct task_group *parent)
  7849. {
  7850. return 1;
  7851. }
  7852. static inline void register_fair_sched_group(struct task_group *tg, int cpu)
  7853. {
  7854. }
  7855. static inline void unregister_fair_sched_group(struct task_group *tg, int cpu)
  7856. {
  7857. }
  7858. #endif /* CONFIG_FAIR_GROUP_SCHED */
  7859. #ifdef CONFIG_RT_GROUP_SCHED
  7860. static void free_rt_sched_group(struct task_group *tg)
  7861. {
  7862. int i;
  7863. destroy_rt_bandwidth(&tg->rt_bandwidth);
  7864. for_each_possible_cpu(i) {
  7865. if (tg->rt_rq)
  7866. kfree(tg->rt_rq[i]);
  7867. if (tg->rt_se)
  7868. kfree(tg->rt_se[i]);
  7869. }
  7870. kfree(tg->rt_rq);
  7871. kfree(tg->rt_se);
  7872. }
  7873. static
  7874. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  7875. {
  7876. struct rt_rq *rt_rq;
  7877. struct sched_rt_entity *rt_se;
  7878. struct rq *rq;
  7879. int i;
  7880. tg->rt_rq = kzalloc(sizeof(rt_rq) * nr_cpu_ids, GFP_KERNEL);
  7881. if (!tg->rt_rq)
  7882. goto err;
  7883. tg->rt_se = kzalloc(sizeof(rt_se) * nr_cpu_ids, GFP_KERNEL);
  7884. if (!tg->rt_se)
  7885. goto err;
  7886. init_rt_bandwidth(&tg->rt_bandwidth,
  7887. ktime_to_ns(def_rt_bandwidth.rt_period), 0);
  7888. for_each_possible_cpu(i) {
  7889. rq = cpu_rq(i);
  7890. rt_rq = kzalloc_node(sizeof(struct rt_rq),
  7891. GFP_KERNEL, cpu_to_node(i));
  7892. if (!rt_rq)
  7893. goto err;
  7894. rt_se = kzalloc_node(sizeof(struct sched_rt_entity),
  7895. GFP_KERNEL, cpu_to_node(i));
  7896. if (!rt_se)
  7897. goto err;
  7898. init_tg_rt_entry(tg, rt_rq, rt_se, i, 0, parent->rt_se[i]);
  7899. }
  7900. return 1;
  7901. err:
  7902. return 0;
  7903. }
  7904. static inline void register_rt_sched_group(struct task_group *tg, int cpu)
  7905. {
  7906. list_add_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list,
  7907. &cpu_rq(cpu)->leaf_rt_rq_list);
  7908. }
  7909. static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
  7910. {
  7911. list_del_rcu(&tg->rt_rq[cpu]->leaf_rt_rq_list);
  7912. }
  7913. #else /* !CONFIG_RT_GROUP_SCHED */
  7914. static inline void free_rt_sched_group(struct task_group *tg)
  7915. {
  7916. }
  7917. static inline
  7918. int alloc_rt_sched_group(struct task_group *tg, struct task_group *parent)
  7919. {
  7920. return 1;
  7921. }
  7922. static inline void register_rt_sched_group(struct task_group *tg, int cpu)
  7923. {
  7924. }
  7925. static inline void unregister_rt_sched_group(struct task_group *tg, int cpu)
  7926. {
  7927. }
  7928. #endif /* CONFIG_RT_GROUP_SCHED */
  7929. #ifdef CONFIG_GROUP_SCHED
  7930. static void free_sched_group(struct task_group *tg)
  7931. {
  7932. free_fair_sched_group(tg);
  7933. free_rt_sched_group(tg);
  7934. kfree(tg);
  7935. }
  7936. /* allocate runqueue etc for a new task group */
  7937. struct task_group *sched_create_group(struct task_group *parent)
  7938. {
  7939. struct task_group *tg;
  7940. unsigned long flags;
  7941. int i;
  7942. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  7943. if (!tg)
  7944. return ERR_PTR(-ENOMEM);
  7945. if (!alloc_fair_sched_group(tg, parent))
  7946. goto err;
  7947. if (!alloc_rt_sched_group(tg, parent))
  7948. goto err;
  7949. spin_lock_irqsave(&task_group_lock, flags);
  7950. for_each_possible_cpu(i) {
  7951. register_fair_sched_group(tg, i);
  7952. register_rt_sched_group(tg, i);
  7953. }
  7954. list_add_rcu(&tg->list, &task_groups);
  7955. WARN_ON(!parent); /* root should already exist */
  7956. tg->parent = parent;
  7957. INIT_LIST_HEAD(&tg->children);
  7958. list_add_rcu(&tg->siblings, &parent->children);
  7959. spin_unlock_irqrestore(&task_group_lock, flags);
  7960. return tg;
  7961. err:
  7962. free_sched_group(tg);
  7963. return ERR_PTR(-ENOMEM);
  7964. }
  7965. /* rcu callback to free various structures associated with a task group */
  7966. static void free_sched_group_rcu(struct rcu_head *rhp)
  7967. {
  7968. /* now it should be safe to free those cfs_rqs */
  7969. free_sched_group(container_of(rhp, struct task_group, rcu));
  7970. }
  7971. /* Destroy runqueue etc associated with a task group */
  7972. void sched_destroy_group(struct task_group *tg)
  7973. {
  7974. unsigned long flags;
  7975. int i;
  7976. spin_lock_irqsave(&task_group_lock, flags);
  7977. for_each_possible_cpu(i) {
  7978. unregister_fair_sched_group(tg, i);
  7979. unregister_rt_sched_group(tg, i);
  7980. }
  7981. list_del_rcu(&tg->list);
  7982. list_del_rcu(&tg->siblings);
  7983. spin_unlock_irqrestore(&task_group_lock, flags);
  7984. /* wait for possible concurrent references to cfs_rqs complete */
  7985. call_rcu(&tg->rcu, free_sched_group_rcu);
  7986. }
  7987. /* change task's runqueue when it moves between groups.
  7988. * The caller of this function should have put the task in its new group
  7989. * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
  7990. * reflect its new group.
  7991. */
  7992. void sched_move_task(struct task_struct *tsk)
  7993. {
  7994. int on_rq, running;
  7995. unsigned long flags;
  7996. struct rq *rq;
  7997. rq = task_rq_lock(tsk, &flags);
  7998. update_rq_clock(rq);
  7999. running = task_current(rq, tsk);
  8000. on_rq = tsk->se.on_rq;
  8001. if (on_rq)
  8002. dequeue_task(rq, tsk, 0);
  8003. if (unlikely(running))
  8004. tsk->sched_class->put_prev_task(rq, tsk);
  8005. set_task_rq(tsk, task_cpu(tsk));
  8006. #ifdef CONFIG_FAIR_GROUP_SCHED
  8007. if (tsk->sched_class->moved_group)
  8008. tsk->sched_class->moved_group(tsk);
  8009. #endif
  8010. if (unlikely(running))
  8011. tsk->sched_class->set_curr_task(rq);
  8012. if (on_rq)
  8013. enqueue_task(rq, tsk, 0);
  8014. task_rq_unlock(rq, &flags);
  8015. }
  8016. #endif /* CONFIG_GROUP_SCHED */
  8017. #ifdef CONFIG_FAIR_GROUP_SCHED
  8018. static void __set_se_shares(struct sched_entity *se, unsigned long shares)
  8019. {
  8020. struct cfs_rq *cfs_rq = se->cfs_rq;
  8021. int on_rq;
  8022. on_rq = se->on_rq;
  8023. if (on_rq)
  8024. dequeue_entity(cfs_rq, se, 0);
  8025. se->load.weight = shares;
  8026. se->load.inv_weight = 0;
  8027. if (on_rq)
  8028. enqueue_entity(cfs_rq, se, 0);
  8029. }
  8030. static void set_se_shares(struct sched_entity *se, unsigned long shares)
  8031. {
  8032. struct cfs_rq *cfs_rq = se->cfs_rq;
  8033. struct rq *rq = cfs_rq->rq;
  8034. unsigned long flags;
  8035. spin_lock_irqsave(&rq->lock, flags);
  8036. __set_se_shares(se, shares);
  8037. spin_unlock_irqrestore(&rq->lock, flags);
  8038. }
  8039. static DEFINE_MUTEX(shares_mutex);
  8040. int sched_group_set_shares(struct task_group *tg, unsigned long shares)
  8041. {
  8042. int i;
  8043. unsigned long flags;
  8044. /*
  8045. * We can't change the weight of the root cgroup.
  8046. */
  8047. if (!tg->se[0])
  8048. return -EINVAL;
  8049. if (shares < MIN_SHARES)
  8050. shares = MIN_SHARES;
  8051. else if (shares > MAX_SHARES)
  8052. shares = MAX_SHARES;
  8053. mutex_lock(&shares_mutex);
  8054. if (tg->shares == shares)
  8055. goto done;
  8056. spin_lock_irqsave(&task_group_lock, flags);
  8057. for_each_possible_cpu(i)
  8058. unregister_fair_sched_group(tg, i);
  8059. list_del_rcu(&tg->siblings);
  8060. spin_unlock_irqrestore(&task_group_lock, flags);
  8061. /* wait for any ongoing reference to this group to finish */
  8062. synchronize_sched();
  8063. /*
  8064. * Now we are free to modify the group's share on each cpu
  8065. * w/o tripping rebalance_share or load_balance_fair.
  8066. */
  8067. tg->shares = shares;
  8068. for_each_possible_cpu(i) {
  8069. /*
  8070. * force a rebalance
  8071. */
  8072. cfs_rq_set_shares(tg->cfs_rq[i], 0);
  8073. set_se_shares(tg->se[i], shares);
  8074. }
  8075. /*
  8076. * Enable load balance activity on this group, by inserting it back on
  8077. * each cpu's rq->leaf_cfs_rq_list.
  8078. */
  8079. spin_lock_irqsave(&task_group_lock, flags);
  8080. for_each_possible_cpu(i)
  8081. register_fair_sched_group(tg, i);
  8082. list_add_rcu(&tg->siblings, &tg->parent->children);
  8083. spin_unlock_irqrestore(&task_group_lock, flags);
  8084. done:
  8085. mutex_unlock(&shares_mutex);
  8086. return 0;
  8087. }
  8088. unsigned long sched_group_shares(struct task_group *tg)
  8089. {
  8090. return tg->shares;
  8091. }
  8092. #endif
  8093. #ifdef CONFIG_RT_GROUP_SCHED
  8094. /*
  8095. * Ensure that the real time constraints are schedulable.
  8096. */
  8097. static DEFINE_MUTEX(rt_constraints_mutex);
  8098. static unsigned long to_ratio(u64 period, u64 runtime)
  8099. {
  8100. if (runtime == RUNTIME_INF)
  8101. return 1ULL << 20;
  8102. return div64_u64(runtime << 20, period);
  8103. }
  8104. /* Must be called with tasklist_lock held */
  8105. static inline int tg_has_rt_tasks(struct task_group *tg)
  8106. {
  8107. struct task_struct *g, *p;
  8108. do_each_thread(g, p) {
  8109. if (rt_task(p) && rt_rq_of_se(&p->rt)->tg == tg)
  8110. return 1;
  8111. } while_each_thread(g, p);
  8112. return 0;
  8113. }
  8114. struct rt_schedulable_data {
  8115. struct task_group *tg;
  8116. u64 rt_period;
  8117. u64 rt_runtime;
  8118. };
  8119. static int tg_schedulable(struct task_group *tg, void *data)
  8120. {
  8121. struct rt_schedulable_data *d = data;
  8122. struct task_group *child;
  8123. unsigned long total, sum = 0;
  8124. u64 period, runtime;
  8125. period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  8126. runtime = tg->rt_bandwidth.rt_runtime;
  8127. if (tg == d->tg) {
  8128. period = d->rt_period;
  8129. runtime = d->rt_runtime;
  8130. }
  8131. #ifdef CONFIG_USER_SCHED
  8132. if (tg == &root_task_group) {
  8133. period = global_rt_period();
  8134. runtime = global_rt_runtime();
  8135. }
  8136. #endif
  8137. /*
  8138. * Cannot have more runtime than the period.
  8139. */
  8140. if (runtime > period && runtime != RUNTIME_INF)
  8141. return -EINVAL;
  8142. /*
  8143. * Ensure we don't starve existing RT tasks.
  8144. */
  8145. if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
  8146. return -EBUSY;
  8147. total = to_ratio(period, runtime);
  8148. /*
  8149. * Nobody can have more than the global setting allows.
  8150. */
  8151. if (total > to_ratio(global_rt_period(), global_rt_runtime()))
  8152. return -EINVAL;
  8153. /*
  8154. * The sum of our children's runtime should not exceed our own.
  8155. */
  8156. list_for_each_entry_rcu(child, &tg->children, siblings) {
  8157. period = ktime_to_ns(child->rt_bandwidth.rt_period);
  8158. runtime = child->rt_bandwidth.rt_runtime;
  8159. if (child == d->tg) {
  8160. period = d->rt_period;
  8161. runtime = d->rt_runtime;
  8162. }
  8163. sum += to_ratio(period, runtime);
  8164. }
  8165. if (sum > total)
  8166. return -EINVAL;
  8167. return 0;
  8168. }
  8169. static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
  8170. {
  8171. struct rt_schedulable_data data = {
  8172. .tg = tg,
  8173. .rt_period = period,
  8174. .rt_runtime = runtime,
  8175. };
  8176. return walk_tg_tree(tg_schedulable, tg_nop, &data);
  8177. }
  8178. static int tg_set_bandwidth(struct task_group *tg,
  8179. u64 rt_period, u64 rt_runtime)
  8180. {
  8181. int i, err = 0;
  8182. mutex_lock(&rt_constraints_mutex);
  8183. read_lock(&tasklist_lock);
  8184. err = __rt_schedulable(tg, rt_period, rt_runtime);
  8185. if (err)
  8186. goto unlock;
  8187. spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  8188. tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
  8189. tg->rt_bandwidth.rt_runtime = rt_runtime;
  8190. for_each_possible_cpu(i) {
  8191. struct rt_rq *rt_rq = tg->rt_rq[i];
  8192. spin_lock(&rt_rq->rt_runtime_lock);
  8193. rt_rq->rt_runtime = rt_runtime;
  8194. spin_unlock(&rt_rq->rt_runtime_lock);
  8195. }
  8196. spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  8197. unlock:
  8198. read_unlock(&tasklist_lock);
  8199. mutex_unlock(&rt_constraints_mutex);
  8200. return err;
  8201. }
  8202. int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
  8203. {
  8204. u64 rt_runtime, rt_period;
  8205. rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  8206. rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
  8207. if (rt_runtime_us < 0)
  8208. rt_runtime = RUNTIME_INF;
  8209. return tg_set_bandwidth(tg, rt_period, rt_runtime);
  8210. }
  8211. long sched_group_rt_runtime(struct task_group *tg)
  8212. {
  8213. u64 rt_runtime_us;
  8214. if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
  8215. return -1;
  8216. rt_runtime_us = tg->rt_bandwidth.rt_runtime;
  8217. do_div(rt_runtime_us, NSEC_PER_USEC);
  8218. return rt_runtime_us;
  8219. }
  8220. int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
  8221. {
  8222. u64 rt_runtime, rt_period;
  8223. rt_period = (u64)rt_period_us * NSEC_PER_USEC;
  8224. rt_runtime = tg->rt_bandwidth.rt_runtime;
  8225. if (rt_period == 0)
  8226. return -EINVAL;
  8227. return tg_set_bandwidth(tg, rt_period, rt_runtime);
  8228. }
  8229. long sched_group_rt_period(struct task_group *tg)
  8230. {
  8231. u64 rt_period_us;
  8232. rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
  8233. do_div(rt_period_us, NSEC_PER_USEC);
  8234. return rt_period_us;
  8235. }
  8236. static int sched_rt_global_constraints(void)
  8237. {
  8238. u64 runtime, period;
  8239. int ret = 0;
  8240. if (sysctl_sched_rt_period <= 0)
  8241. return -EINVAL;
  8242. runtime = global_rt_runtime();
  8243. period = global_rt_period();
  8244. /*
  8245. * Sanity check on the sysctl variables.
  8246. */
  8247. if (runtime > period && runtime != RUNTIME_INF)
  8248. return -EINVAL;
  8249. mutex_lock(&rt_constraints_mutex);
  8250. read_lock(&tasklist_lock);
  8251. ret = __rt_schedulable(NULL, 0, 0);
  8252. read_unlock(&tasklist_lock);
  8253. mutex_unlock(&rt_constraints_mutex);
  8254. return ret;
  8255. }
  8256. int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
  8257. {
  8258. /* Don't accept realtime tasks when there is no way for them to run */
  8259. if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
  8260. return 0;
  8261. return 1;
  8262. }
  8263. #else /* !CONFIG_RT_GROUP_SCHED */
  8264. static int sched_rt_global_constraints(void)
  8265. {
  8266. unsigned long flags;
  8267. int i;
  8268. if (sysctl_sched_rt_period <= 0)
  8269. return -EINVAL;
  8270. spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
  8271. for_each_possible_cpu(i) {
  8272. struct rt_rq *rt_rq = &cpu_rq(i)->rt;
  8273. spin_lock(&rt_rq->rt_runtime_lock);
  8274. rt_rq->rt_runtime = global_rt_runtime();
  8275. spin_unlock(&rt_rq->rt_runtime_lock);
  8276. }
  8277. spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
  8278. return 0;
  8279. }
  8280. #endif /* CONFIG_RT_GROUP_SCHED */
  8281. int sched_rt_handler(struct ctl_table *table, int write,
  8282. struct file *filp, void __user *buffer, size_t *lenp,
  8283. loff_t *ppos)
  8284. {
  8285. int ret;
  8286. int old_period, old_runtime;
  8287. static DEFINE_MUTEX(mutex);
  8288. mutex_lock(&mutex);
  8289. old_period = sysctl_sched_rt_period;
  8290. old_runtime = sysctl_sched_rt_runtime;
  8291. ret = proc_dointvec(table, write, filp, buffer, lenp, ppos);
  8292. if (!ret && write) {
  8293. ret = sched_rt_global_constraints();
  8294. if (ret) {
  8295. sysctl_sched_rt_period = old_period;
  8296. sysctl_sched_rt_runtime = old_runtime;
  8297. } else {
  8298. def_rt_bandwidth.rt_runtime = global_rt_runtime();
  8299. def_rt_bandwidth.rt_period =
  8300. ns_to_ktime(global_rt_period());
  8301. }
  8302. }
  8303. mutex_unlock(&mutex);
  8304. return ret;
  8305. }
  8306. #ifdef CONFIG_CGROUP_SCHED
  8307. /* return corresponding task_group object of a cgroup */
  8308. static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
  8309. {
  8310. return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
  8311. struct task_group, css);
  8312. }
  8313. static struct cgroup_subsys_state *
  8314. cpu_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8315. {
  8316. struct task_group *tg, *parent;
  8317. if (!cgrp->parent) {
  8318. /* This is early initialization for the top cgroup */
  8319. return &init_task_group.css;
  8320. }
  8321. parent = cgroup_tg(cgrp->parent);
  8322. tg = sched_create_group(parent);
  8323. if (IS_ERR(tg))
  8324. return ERR_PTR(-ENOMEM);
  8325. return &tg->css;
  8326. }
  8327. static void
  8328. cpu_cgroup_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8329. {
  8330. struct task_group *tg = cgroup_tg(cgrp);
  8331. sched_destroy_group(tg);
  8332. }
  8333. static int
  8334. cpu_cgroup_can_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  8335. struct task_struct *tsk)
  8336. {
  8337. #ifdef CONFIG_RT_GROUP_SCHED
  8338. if (!sched_rt_can_attach(cgroup_tg(cgrp), tsk))
  8339. return -EINVAL;
  8340. #else
  8341. /* We don't support RT-tasks being in separate groups */
  8342. if (tsk->sched_class != &fair_sched_class)
  8343. return -EINVAL;
  8344. #endif
  8345. return 0;
  8346. }
  8347. static void
  8348. cpu_cgroup_attach(struct cgroup_subsys *ss, struct cgroup *cgrp,
  8349. struct cgroup *old_cont, struct task_struct *tsk)
  8350. {
  8351. sched_move_task(tsk);
  8352. }
  8353. #ifdef CONFIG_FAIR_GROUP_SCHED
  8354. static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
  8355. u64 shareval)
  8356. {
  8357. return sched_group_set_shares(cgroup_tg(cgrp), shareval);
  8358. }
  8359. static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
  8360. {
  8361. struct task_group *tg = cgroup_tg(cgrp);
  8362. return (u64) tg->shares;
  8363. }
  8364. #endif /* CONFIG_FAIR_GROUP_SCHED */
  8365. #ifdef CONFIG_RT_GROUP_SCHED
  8366. static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
  8367. s64 val)
  8368. {
  8369. return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
  8370. }
  8371. static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
  8372. {
  8373. return sched_group_rt_runtime(cgroup_tg(cgrp));
  8374. }
  8375. static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
  8376. u64 rt_period_us)
  8377. {
  8378. return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
  8379. }
  8380. static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
  8381. {
  8382. return sched_group_rt_period(cgroup_tg(cgrp));
  8383. }
  8384. #endif /* CONFIG_RT_GROUP_SCHED */
  8385. static struct cftype cpu_files[] = {
  8386. #ifdef CONFIG_FAIR_GROUP_SCHED
  8387. {
  8388. .name = "shares",
  8389. .read_u64 = cpu_shares_read_u64,
  8390. .write_u64 = cpu_shares_write_u64,
  8391. },
  8392. #endif
  8393. #ifdef CONFIG_RT_GROUP_SCHED
  8394. {
  8395. .name = "rt_runtime_us",
  8396. .read_s64 = cpu_rt_runtime_read,
  8397. .write_s64 = cpu_rt_runtime_write,
  8398. },
  8399. {
  8400. .name = "rt_period_us",
  8401. .read_u64 = cpu_rt_period_read_uint,
  8402. .write_u64 = cpu_rt_period_write_uint,
  8403. },
  8404. #endif
  8405. };
  8406. static int cpu_cgroup_populate(struct cgroup_subsys *ss, struct cgroup *cont)
  8407. {
  8408. return cgroup_add_files(cont, ss, cpu_files, ARRAY_SIZE(cpu_files));
  8409. }
  8410. struct cgroup_subsys cpu_cgroup_subsys = {
  8411. .name = "cpu",
  8412. .create = cpu_cgroup_create,
  8413. .destroy = cpu_cgroup_destroy,
  8414. .can_attach = cpu_cgroup_can_attach,
  8415. .attach = cpu_cgroup_attach,
  8416. .populate = cpu_cgroup_populate,
  8417. .subsys_id = cpu_cgroup_subsys_id,
  8418. .early_init = 1,
  8419. };
  8420. #endif /* CONFIG_CGROUP_SCHED */
  8421. #ifdef CONFIG_CGROUP_CPUACCT
  8422. /*
  8423. * CPU accounting code for task groups.
  8424. *
  8425. * Based on the work by Paul Menage (menage@google.com) and Balbir Singh
  8426. * (balbir@in.ibm.com).
  8427. */
  8428. /* track cpu usage of a group of tasks and its child groups */
  8429. struct cpuacct {
  8430. struct cgroup_subsys_state css;
  8431. /* cpuusage holds pointer to a u64-type object on every cpu */
  8432. u64 *cpuusage;
  8433. struct cpuacct *parent;
  8434. };
  8435. struct cgroup_subsys cpuacct_subsys;
  8436. /* return cpu accounting group corresponding to this container */
  8437. static inline struct cpuacct *cgroup_ca(struct cgroup *cgrp)
  8438. {
  8439. return container_of(cgroup_subsys_state(cgrp, cpuacct_subsys_id),
  8440. struct cpuacct, css);
  8441. }
  8442. /* return cpu accounting group to which this task belongs */
  8443. static inline struct cpuacct *task_ca(struct task_struct *tsk)
  8444. {
  8445. return container_of(task_subsys_state(tsk, cpuacct_subsys_id),
  8446. struct cpuacct, css);
  8447. }
  8448. /* create a new cpu accounting group */
  8449. static struct cgroup_subsys_state *cpuacct_create(
  8450. struct cgroup_subsys *ss, struct cgroup *cgrp)
  8451. {
  8452. struct cpuacct *ca = kzalloc(sizeof(*ca), GFP_KERNEL);
  8453. if (!ca)
  8454. return ERR_PTR(-ENOMEM);
  8455. ca->cpuusage = alloc_percpu(u64);
  8456. if (!ca->cpuusage) {
  8457. kfree(ca);
  8458. return ERR_PTR(-ENOMEM);
  8459. }
  8460. if (cgrp->parent)
  8461. ca->parent = cgroup_ca(cgrp->parent);
  8462. return &ca->css;
  8463. }
  8464. /* destroy an existing cpu accounting group */
  8465. static void
  8466. cpuacct_destroy(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8467. {
  8468. struct cpuacct *ca = cgroup_ca(cgrp);
  8469. free_percpu(ca->cpuusage);
  8470. kfree(ca);
  8471. }
  8472. static u64 cpuacct_cpuusage_read(struct cpuacct *ca, int cpu)
  8473. {
  8474. u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  8475. u64 data;
  8476. #ifndef CONFIG_64BIT
  8477. /*
  8478. * Take rq->lock to make 64-bit read safe on 32-bit platforms.
  8479. */
  8480. spin_lock_irq(&cpu_rq(cpu)->lock);
  8481. data = *cpuusage;
  8482. spin_unlock_irq(&cpu_rq(cpu)->lock);
  8483. #else
  8484. data = *cpuusage;
  8485. #endif
  8486. return data;
  8487. }
  8488. static void cpuacct_cpuusage_write(struct cpuacct *ca, int cpu, u64 val)
  8489. {
  8490. u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  8491. #ifndef CONFIG_64BIT
  8492. /*
  8493. * Take rq->lock to make 64-bit write safe on 32-bit platforms.
  8494. */
  8495. spin_lock_irq(&cpu_rq(cpu)->lock);
  8496. *cpuusage = val;
  8497. spin_unlock_irq(&cpu_rq(cpu)->lock);
  8498. #else
  8499. *cpuusage = val;
  8500. #endif
  8501. }
  8502. /* return total cpu usage (in nanoseconds) of a group */
  8503. static u64 cpuusage_read(struct cgroup *cgrp, struct cftype *cft)
  8504. {
  8505. struct cpuacct *ca = cgroup_ca(cgrp);
  8506. u64 totalcpuusage = 0;
  8507. int i;
  8508. for_each_present_cpu(i)
  8509. totalcpuusage += cpuacct_cpuusage_read(ca, i);
  8510. return totalcpuusage;
  8511. }
  8512. static int cpuusage_write(struct cgroup *cgrp, struct cftype *cftype,
  8513. u64 reset)
  8514. {
  8515. struct cpuacct *ca = cgroup_ca(cgrp);
  8516. int err = 0;
  8517. int i;
  8518. if (reset) {
  8519. err = -EINVAL;
  8520. goto out;
  8521. }
  8522. for_each_present_cpu(i)
  8523. cpuacct_cpuusage_write(ca, i, 0);
  8524. out:
  8525. return err;
  8526. }
  8527. static int cpuacct_percpu_seq_read(struct cgroup *cgroup, struct cftype *cft,
  8528. struct seq_file *m)
  8529. {
  8530. struct cpuacct *ca = cgroup_ca(cgroup);
  8531. u64 percpu;
  8532. int i;
  8533. for_each_present_cpu(i) {
  8534. percpu = cpuacct_cpuusage_read(ca, i);
  8535. seq_printf(m, "%llu ", (unsigned long long) percpu);
  8536. }
  8537. seq_printf(m, "\n");
  8538. return 0;
  8539. }
  8540. static struct cftype files[] = {
  8541. {
  8542. .name = "usage",
  8543. .read_u64 = cpuusage_read,
  8544. .write_u64 = cpuusage_write,
  8545. },
  8546. {
  8547. .name = "usage_percpu",
  8548. .read_seq_string = cpuacct_percpu_seq_read,
  8549. },
  8550. };
  8551. static int cpuacct_populate(struct cgroup_subsys *ss, struct cgroup *cgrp)
  8552. {
  8553. return cgroup_add_files(cgrp, ss, files, ARRAY_SIZE(files));
  8554. }
  8555. /*
  8556. * charge this task's execution time to its accounting group.
  8557. *
  8558. * called with rq->lock held.
  8559. */
  8560. static void cpuacct_charge(struct task_struct *tsk, u64 cputime)
  8561. {
  8562. struct cpuacct *ca;
  8563. int cpu;
  8564. if (unlikely(!cpuacct_subsys.active))
  8565. return;
  8566. cpu = task_cpu(tsk);
  8567. ca = task_ca(tsk);
  8568. for (; ca; ca = ca->parent) {
  8569. u64 *cpuusage = per_cpu_ptr(ca->cpuusage, cpu);
  8570. *cpuusage += cputime;
  8571. }
  8572. }
  8573. struct cgroup_subsys cpuacct_subsys = {
  8574. .name = "cpuacct",
  8575. .create = cpuacct_create,
  8576. .destroy = cpuacct_destroy,
  8577. .populate = cpuacct_populate,
  8578. .subsys_id = cpuacct_subsys_id,
  8579. };
  8580. #endif /* CONFIG_CGROUP_CPUACCT */