sched.c 267 KB

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