sched.c 232 KB

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