core.c 179 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508
  1. /*
  2. * kernel/sched/core.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 <asm/mmu_context.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/capability.h>
  37. #include <linux/completion.h>
  38. #include <linux/kernel_stat.h>
  39. #include <linux/debug_locks.h>
  40. #include <linux/perf_event.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/proc_fs.h>
  57. #include <linux/seq_file.h>
  58. #include <linux/sysctl.h>
  59. #include <linux/syscalls.h>
  60. #include <linux/times.h>
  61. #include <linux/tsacct_kern.h>
  62. #include <linux/kprobes.h>
  63. #include <linux/delayacct.h>
  64. #include <linux/unistd.h>
  65. #include <linux/pagemap.h>
  66. #include <linux/hrtimer.h>
  67. #include <linux/tick.h>
  68. #include <linux/debugfs.h>
  69. #include <linux/ctype.h>
  70. #include <linux/ftrace.h>
  71. #include <linux/slab.h>
  72. #include <linux/init_task.h>
  73. #include <linux/binfmts.h>
  74. #include <linux/context_tracking.h>
  75. #include <asm/switch_to.h>
  76. #include <asm/tlb.h>
  77. #include <asm/irq_regs.h>
  78. #include <asm/mutex.h>
  79. #ifdef CONFIG_PARAVIRT
  80. #include <asm/paravirt.h>
  81. #endif
  82. #include "sched.h"
  83. #include "../workqueue_internal.h"
  84. #include "../smpboot.h"
  85. #define CREATE_TRACE_POINTS
  86. #include <trace/events/sched.h>
  87. void start_bandwidth_timer(struct hrtimer *period_timer, ktime_t period)
  88. {
  89. unsigned long delta;
  90. ktime_t soft, hard, now;
  91. for (;;) {
  92. if (hrtimer_active(period_timer))
  93. break;
  94. now = hrtimer_cb_get_time(period_timer);
  95. hrtimer_forward(period_timer, now, period);
  96. soft = hrtimer_get_softexpires(period_timer);
  97. hard = hrtimer_get_expires(period_timer);
  98. delta = ktime_to_ns(ktime_sub(hard, soft));
  99. __hrtimer_start_range_ns(period_timer, soft, delta,
  100. HRTIMER_MODE_ABS_PINNED, 0);
  101. }
  102. }
  103. DEFINE_MUTEX(sched_domains_mutex);
  104. DEFINE_PER_CPU_SHARED_ALIGNED(struct rq, runqueues);
  105. static void update_rq_clock_task(struct rq *rq, s64 delta);
  106. void update_rq_clock(struct rq *rq)
  107. {
  108. s64 delta;
  109. if (rq->skip_clock_update > 0)
  110. return;
  111. delta = sched_clock_cpu(cpu_of(rq)) - rq->clock;
  112. rq->clock += delta;
  113. update_rq_clock_task(rq, delta);
  114. }
  115. /*
  116. * Debugging: various feature bits
  117. */
  118. #define SCHED_FEAT(name, enabled) \
  119. (1UL << __SCHED_FEAT_##name) * enabled |
  120. const_debug unsigned int sysctl_sched_features =
  121. #include "features.h"
  122. 0;
  123. #undef SCHED_FEAT
  124. #ifdef CONFIG_SCHED_DEBUG
  125. #define SCHED_FEAT(name, enabled) \
  126. #name ,
  127. static const char * const sched_feat_names[] = {
  128. #include "features.h"
  129. };
  130. #undef SCHED_FEAT
  131. static int sched_feat_show(struct seq_file *m, void *v)
  132. {
  133. int i;
  134. for (i = 0; i < __SCHED_FEAT_NR; i++) {
  135. if (!(sysctl_sched_features & (1UL << i)))
  136. seq_puts(m, "NO_");
  137. seq_printf(m, "%s ", sched_feat_names[i]);
  138. }
  139. seq_puts(m, "\n");
  140. return 0;
  141. }
  142. #ifdef HAVE_JUMP_LABEL
  143. #define jump_label_key__true STATIC_KEY_INIT_TRUE
  144. #define jump_label_key__false STATIC_KEY_INIT_FALSE
  145. #define SCHED_FEAT(name, enabled) \
  146. jump_label_key__##enabled ,
  147. struct static_key sched_feat_keys[__SCHED_FEAT_NR] = {
  148. #include "features.h"
  149. };
  150. #undef SCHED_FEAT
  151. static void sched_feat_disable(int i)
  152. {
  153. if (static_key_enabled(&sched_feat_keys[i]))
  154. static_key_slow_dec(&sched_feat_keys[i]);
  155. }
  156. static void sched_feat_enable(int i)
  157. {
  158. if (!static_key_enabled(&sched_feat_keys[i]))
  159. static_key_slow_inc(&sched_feat_keys[i]);
  160. }
  161. #else
  162. static void sched_feat_disable(int i) { };
  163. static void sched_feat_enable(int i) { };
  164. #endif /* HAVE_JUMP_LABEL */
  165. static int sched_feat_set(char *cmp)
  166. {
  167. int i;
  168. int neg = 0;
  169. if (strncmp(cmp, "NO_", 3) == 0) {
  170. neg = 1;
  171. cmp += 3;
  172. }
  173. for (i = 0; i < __SCHED_FEAT_NR; i++) {
  174. if (strcmp(cmp, sched_feat_names[i]) == 0) {
  175. if (neg) {
  176. sysctl_sched_features &= ~(1UL << i);
  177. sched_feat_disable(i);
  178. } else {
  179. sysctl_sched_features |= (1UL << i);
  180. sched_feat_enable(i);
  181. }
  182. break;
  183. }
  184. }
  185. return i;
  186. }
  187. static ssize_t
  188. sched_feat_write(struct file *filp, const char __user *ubuf,
  189. size_t cnt, loff_t *ppos)
  190. {
  191. char buf[64];
  192. char *cmp;
  193. int i;
  194. if (cnt > 63)
  195. cnt = 63;
  196. if (copy_from_user(&buf, ubuf, cnt))
  197. return -EFAULT;
  198. buf[cnt] = 0;
  199. cmp = strstrip(buf);
  200. i = sched_feat_set(cmp);
  201. if (i == __SCHED_FEAT_NR)
  202. return -EINVAL;
  203. *ppos += cnt;
  204. return cnt;
  205. }
  206. static int sched_feat_open(struct inode *inode, struct file *filp)
  207. {
  208. return single_open(filp, sched_feat_show, NULL);
  209. }
  210. static const struct file_operations sched_feat_fops = {
  211. .open = sched_feat_open,
  212. .write = sched_feat_write,
  213. .read = seq_read,
  214. .llseek = seq_lseek,
  215. .release = single_release,
  216. };
  217. static __init int sched_init_debug(void)
  218. {
  219. debugfs_create_file("sched_features", 0644, NULL, NULL,
  220. &sched_feat_fops);
  221. return 0;
  222. }
  223. late_initcall(sched_init_debug);
  224. #endif /* CONFIG_SCHED_DEBUG */
  225. /*
  226. * Number of tasks to iterate in a single balance run.
  227. * Limited because this is done with IRQs disabled.
  228. */
  229. const_debug unsigned int sysctl_sched_nr_migrate = 32;
  230. /*
  231. * period over which we average the RT time consumption, measured
  232. * in ms.
  233. *
  234. * default: 1s
  235. */
  236. const_debug unsigned int sysctl_sched_time_avg = MSEC_PER_SEC;
  237. /*
  238. * period over which we measure -rt task cpu usage in us.
  239. * default: 1s
  240. */
  241. unsigned int sysctl_sched_rt_period = 1000000;
  242. __read_mostly int scheduler_running;
  243. /*
  244. * part of the period that we allow rt tasks to run in us.
  245. * default: 0.95s
  246. */
  247. int sysctl_sched_rt_runtime = 950000;
  248. /*
  249. * __task_rq_lock - lock the rq @p resides on.
  250. */
  251. static inline struct rq *__task_rq_lock(struct task_struct *p)
  252. __acquires(rq->lock)
  253. {
  254. struct rq *rq;
  255. lockdep_assert_held(&p->pi_lock);
  256. for (;;) {
  257. rq = task_rq(p);
  258. raw_spin_lock(&rq->lock);
  259. if (likely(rq == task_rq(p)))
  260. return rq;
  261. raw_spin_unlock(&rq->lock);
  262. }
  263. }
  264. /*
  265. * task_rq_lock - lock p->pi_lock and lock the rq @p resides on.
  266. */
  267. static struct rq *task_rq_lock(struct task_struct *p, unsigned long *flags)
  268. __acquires(p->pi_lock)
  269. __acquires(rq->lock)
  270. {
  271. struct rq *rq;
  272. for (;;) {
  273. raw_spin_lock_irqsave(&p->pi_lock, *flags);
  274. rq = task_rq(p);
  275. raw_spin_lock(&rq->lock);
  276. if (likely(rq == task_rq(p)))
  277. return rq;
  278. raw_spin_unlock(&rq->lock);
  279. raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
  280. }
  281. }
  282. static void __task_rq_unlock(struct rq *rq)
  283. __releases(rq->lock)
  284. {
  285. raw_spin_unlock(&rq->lock);
  286. }
  287. static inline void
  288. task_rq_unlock(struct rq *rq, struct task_struct *p, unsigned long *flags)
  289. __releases(rq->lock)
  290. __releases(p->pi_lock)
  291. {
  292. raw_spin_unlock(&rq->lock);
  293. raw_spin_unlock_irqrestore(&p->pi_lock, *flags);
  294. }
  295. /*
  296. * this_rq_lock - lock this runqueue and disable interrupts.
  297. */
  298. static struct rq *this_rq_lock(void)
  299. __acquires(rq->lock)
  300. {
  301. struct rq *rq;
  302. local_irq_disable();
  303. rq = this_rq();
  304. raw_spin_lock(&rq->lock);
  305. return rq;
  306. }
  307. #ifdef CONFIG_SCHED_HRTICK
  308. /*
  309. * Use HR-timers to deliver accurate preemption points.
  310. *
  311. * Its all a bit involved since we cannot program an hrt while holding the
  312. * rq->lock. So what we do is store a state in in rq->hrtick_* and ask for a
  313. * reschedule event.
  314. *
  315. * When we get rescheduled we reprogram the hrtick_timer outside of the
  316. * rq->lock.
  317. */
  318. static void hrtick_clear(struct rq *rq)
  319. {
  320. if (hrtimer_active(&rq->hrtick_timer))
  321. hrtimer_cancel(&rq->hrtick_timer);
  322. }
  323. /*
  324. * High-resolution timer tick.
  325. * Runs from hardirq context with interrupts disabled.
  326. */
  327. static enum hrtimer_restart hrtick(struct hrtimer *timer)
  328. {
  329. struct rq *rq = container_of(timer, struct rq, hrtick_timer);
  330. WARN_ON_ONCE(cpu_of(rq) != smp_processor_id());
  331. raw_spin_lock(&rq->lock);
  332. update_rq_clock(rq);
  333. rq->curr->sched_class->task_tick(rq, rq->curr, 1);
  334. raw_spin_unlock(&rq->lock);
  335. return HRTIMER_NORESTART;
  336. }
  337. #ifdef CONFIG_SMP
  338. /*
  339. * called from hardirq (IPI) context
  340. */
  341. static void __hrtick_start(void *arg)
  342. {
  343. struct rq *rq = arg;
  344. raw_spin_lock(&rq->lock);
  345. hrtimer_restart(&rq->hrtick_timer);
  346. rq->hrtick_csd_pending = 0;
  347. raw_spin_unlock(&rq->lock);
  348. }
  349. /*
  350. * Called to set the hrtick timer state.
  351. *
  352. * called with rq->lock held and irqs disabled
  353. */
  354. void hrtick_start(struct rq *rq, u64 delay)
  355. {
  356. struct hrtimer *timer = &rq->hrtick_timer;
  357. ktime_t time = ktime_add_ns(timer->base->get_time(), delay);
  358. hrtimer_set_expires(timer, time);
  359. if (rq == this_rq()) {
  360. hrtimer_restart(timer);
  361. } else if (!rq->hrtick_csd_pending) {
  362. __smp_call_function_single(cpu_of(rq), &rq->hrtick_csd, 0);
  363. rq->hrtick_csd_pending = 1;
  364. }
  365. }
  366. static int
  367. hotplug_hrtick(struct notifier_block *nfb, unsigned long action, void *hcpu)
  368. {
  369. int cpu = (int)(long)hcpu;
  370. switch (action) {
  371. case CPU_UP_CANCELED:
  372. case CPU_UP_CANCELED_FROZEN:
  373. case CPU_DOWN_PREPARE:
  374. case CPU_DOWN_PREPARE_FROZEN:
  375. case CPU_DEAD:
  376. case CPU_DEAD_FROZEN:
  377. hrtick_clear(cpu_rq(cpu));
  378. return NOTIFY_OK;
  379. }
  380. return NOTIFY_DONE;
  381. }
  382. static __init void init_hrtick(void)
  383. {
  384. hotcpu_notifier(hotplug_hrtick, 0);
  385. }
  386. #else
  387. /*
  388. * Called to set the hrtick timer state.
  389. *
  390. * called with rq->lock held and irqs disabled
  391. */
  392. void hrtick_start(struct rq *rq, u64 delay)
  393. {
  394. __hrtimer_start_range_ns(&rq->hrtick_timer, ns_to_ktime(delay), 0,
  395. HRTIMER_MODE_REL_PINNED, 0);
  396. }
  397. static inline void init_hrtick(void)
  398. {
  399. }
  400. #endif /* CONFIG_SMP */
  401. static void init_rq_hrtick(struct rq *rq)
  402. {
  403. #ifdef CONFIG_SMP
  404. rq->hrtick_csd_pending = 0;
  405. rq->hrtick_csd.flags = 0;
  406. rq->hrtick_csd.func = __hrtick_start;
  407. rq->hrtick_csd.info = rq;
  408. #endif
  409. hrtimer_init(&rq->hrtick_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  410. rq->hrtick_timer.function = hrtick;
  411. }
  412. #else /* CONFIG_SCHED_HRTICK */
  413. static inline void hrtick_clear(struct rq *rq)
  414. {
  415. }
  416. static inline void init_rq_hrtick(struct rq *rq)
  417. {
  418. }
  419. static inline void init_hrtick(void)
  420. {
  421. }
  422. #endif /* CONFIG_SCHED_HRTICK */
  423. /*
  424. * resched_task - mark a task 'to be rescheduled now'.
  425. *
  426. * On UP this means the setting of the need_resched flag, on SMP it
  427. * might also involve a cross-CPU call to trigger the scheduler on
  428. * the target CPU.
  429. */
  430. #ifdef CONFIG_SMP
  431. void resched_task(struct task_struct *p)
  432. {
  433. int cpu;
  434. assert_raw_spin_locked(&task_rq(p)->lock);
  435. if (test_tsk_need_resched(p))
  436. return;
  437. set_tsk_need_resched(p);
  438. cpu = task_cpu(p);
  439. if (cpu == smp_processor_id())
  440. return;
  441. /* NEED_RESCHED must be visible before we test polling */
  442. smp_mb();
  443. if (!tsk_is_polling(p))
  444. smp_send_reschedule(cpu);
  445. }
  446. void resched_cpu(int cpu)
  447. {
  448. struct rq *rq = cpu_rq(cpu);
  449. unsigned long flags;
  450. if (!raw_spin_trylock_irqsave(&rq->lock, flags))
  451. return;
  452. resched_task(cpu_curr(cpu));
  453. raw_spin_unlock_irqrestore(&rq->lock, flags);
  454. }
  455. #ifdef CONFIG_NO_HZ_COMMON
  456. /*
  457. * In the semi idle case, use the nearest busy cpu for migrating timers
  458. * from an idle cpu. This is good for power-savings.
  459. *
  460. * We don't do similar optimization for completely idle system, as
  461. * selecting an idle cpu will add more delays to the timers than intended
  462. * (as that cpu's timer base may not be uptodate wrt jiffies etc).
  463. */
  464. int get_nohz_timer_target(void)
  465. {
  466. int cpu = smp_processor_id();
  467. int i;
  468. struct sched_domain *sd;
  469. rcu_read_lock();
  470. for_each_domain(cpu, sd) {
  471. for_each_cpu(i, sched_domain_span(sd)) {
  472. if (!idle_cpu(i)) {
  473. cpu = i;
  474. goto unlock;
  475. }
  476. }
  477. }
  478. unlock:
  479. rcu_read_unlock();
  480. return cpu;
  481. }
  482. /*
  483. * When add_timer_on() enqueues a timer into the timer wheel of an
  484. * idle CPU then this timer might expire before the next timer event
  485. * which is scheduled to wake up that CPU. In case of a completely
  486. * idle system the next event might even be infinite time into the
  487. * future. wake_up_idle_cpu() ensures that the CPU is woken up and
  488. * leaves the inner idle loop so the newly added timer is taken into
  489. * account when the CPU goes back to idle and evaluates the timer
  490. * wheel for the next timer event.
  491. */
  492. static void wake_up_idle_cpu(int cpu)
  493. {
  494. struct rq *rq = cpu_rq(cpu);
  495. if (cpu == smp_processor_id())
  496. return;
  497. /*
  498. * This is safe, as this function is called with the timer
  499. * wheel base lock of (cpu) held. When the CPU is on the way
  500. * to idle and has not yet set rq->curr to idle then it will
  501. * be serialized on the timer wheel base lock and take the new
  502. * timer into account automatically.
  503. */
  504. if (rq->curr != rq->idle)
  505. return;
  506. /*
  507. * We can set TIF_RESCHED on the idle task of the other CPU
  508. * lockless. The worst case is that the other CPU runs the
  509. * idle task through an additional NOOP schedule()
  510. */
  511. set_tsk_need_resched(rq->idle);
  512. /* NEED_RESCHED must be visible before we test polling */
  513. smp_mb();
  514. if (!tsk_is_polling(rq->idle))
  515. smp_send_reschedule(cpu);
  516. }
  517. static bool wake_up_full_nohz_cpu(int cpu)
  518. {
  519. if (tick_nohz_full_cpu(cpu)) {
  520. if (cpu != smp_processor_id() ||
  521. tick_nohz_tick_stopped())
  522. smp_send_reschedule(cpu);
  523. return true;
  524. }
  525. return false;
  526. }
  527. void wake_up_nohz_cpu(int cpu)
  528. {
  529. if (!wake_up_full_nohz_cpu(cpu))
  530. wake_up_idle_cpu(cpu);
  531. }
  532. static inline bool got_nohz_idle_kick(void)
  533. {
  534. int cpu = smp_processor_id();
  535. if (!test_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu)))
  536. return false;
  537. if (idle_cpu(cpu) && !need_resched())
  538. return true;
  539. /*
  540. * We can't run Idle Load Balance on this CPU for this time so we
  541. * cancel it and clear NOHZ_BALANCE_KICK
  542. */
  543. clear_bit(NOHZ_BALANCE_KICK, nohz_flags(cpu));
  544. return false;
  545. }
  546. #else /* CONFIG_NO_HZ_COMMON */
  547. static inline bool got_nohz_idle_kick(void)
  548. {
  549. return false;
  550. }
  551. #endif /* CONFIG_NO_HZ_COMMON */
  552. #ifdef CONFIG_NO_HZ_FULL
  553. bool sched_can_stop_tick(void)
  554. {
  555. struct rq *rq;
  556. rq = this_rq();
  557. /* Make sure rq->nr_running update is visible after the IPI */
  558. smp_rmb();
  559. /* More than one running task need preemption */
  560. if (rq->nr_running > 1)
  561. return false;
  562. return true;
  563. }
  564. #endif /* CONFIG_NO_HZ_FULL */
  565. void sched_avg_update(struct rq *rq)
  566. {
  567. s64 period = sched_avg_period();
  568. while ((s64)(rq_clock(rq) - rq->age_stamp) > period) {
  569. /*
  570. * Inline assembly required to prevent the compiler
  571. * optimising this loop into a divmod call.
  572. * See __iter_div_u64_rem() for another example of this.
  573. */
  574. asm("" : "+rm" (rq->age_stamp));
  575. rq->age_stamp += period;
  576. rq->rt_avg /= 2;
  577. }
  578. }
  579. #else /* !CONFIG_SMP */
  580. void resched_task(struct task_struct *p)
  581. {
  582. assert_raw_spin_locked(&task_rq(p)->lock);
  583. set_tsk_need_resched(p);
  584. }
  585. #endif /* CONFIG_SMP */
  586. #if defined(CONFIG_RT_GROUP_SCHED) || (defined(CONFIG_FAIR_GROUP_SCHED) && \
  587. (defined(CONFIG_SMP) || defined(CONFIG_CFS_BANDWIDTH)))
  588. /*
  589. * Iterate task_group tree rooted at *from, calling @down when first entering a
  590. * node and @up when leaving it for the final time.
  591. *
  592. * Caller must hold rcu_lock or sufficient equivalent.
  593. */
  594. int walk_tg_tree_from(struct task_group *from,
  595. tg_visitor down, tg_visitor up, void *data)
  596. {
  597. struct task_group *parent, *child;
  598. int ret;
  599. parent = from;
  600. down:
  601. ret = (*down)(parent, data);
  602. if (ret)
  603. goto out;
  604. list_for_each_entry_rcu(child, &parent->children, siblings) {
  605. parent = child;
  606. goto down;
  607. up:
  608. continue;
  609. }
  610. ret = (*up)(parent, data);
  611. if (ret || parent == from)
  612. goto out;
  613. child = parent;
  614. parent = parent->parent;
  615. if (parent)
  616. goto up;
  617. out:
  618. return ret;
  619. }
  620. int tg_nop(struct task_group *tg, void *data)
  621. {
  622. return 0;
  623. }
  624. #endif
  625. static void set_load_weight(struct task_struct *p)
  626. {
  627. int prio = p->static_prio - MAX_RT_PRIO;
  628. struct load_weight *load = &p->se.load;
  629. /*
  630. * SCHED_IDLE tasks get minimal weight:
  631. */
  632. if (p->policy == SCHED_IDLE) {
  633. load->weight = scale_load(WEIGHT_IDLEPRIO);
  634. load->inv_weight = WMULT_IDLEPRIO;
  635. return;
  636. }
  637. load->weight = scale_load(prio_to_weight[prio]);
  638. load->inv_weight = prio_to_wmult[prio];
  639. }
  640. static void enqueue_task(struct rq *rq, struct task_struct *p, int flags)
  641. {
  642. update_rq_clock(rq);
  643. sched_info_queued(p);
  644. p->sched_class->enqueue_task(rq, p, flags);
  645. }
  646. static void dequeue_task(struct rq *rq, struct task_struct *p, int flags)
  647. {
  648. update_rq_clock(rq);
  649. sched_info_dequeued(p);
  650. p->sched_class->dequeue_task(rq, p, flags);
  651. }
  652. void activate_task(struct rq *rq, struct task_struct *p, int flags)
  653. {
  654. if (task_contributes_to_load(p))
  655. rq->nr_uninterruptible--;
  656. enqueue_task(rq, p, flags);
  657. }
  658. void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
  659. {
  660. if (task_contributes_to_load(p))
  661. rq->nr_uninterruptible++;
  662. dequeue_task(rq, p, flags);
  663. }
  664. static void update_rq_clock_task(struct rq *rq, s64 delta)
  665. {
  666. /*
  667. * In theory, the compile should just see 0 here, and optimize out the call
  668. * to sched_rt_avg_update. But I don't trust it...
  669. */
  670. #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
  671. s64 steal = 0, irq_delta = 0;
  672. #endif
  673. #ifdef CONFIG_IRQ_TIME_ACCOUNTING
  674. irq_delta = irq_time_read(cpu_of(rq)) - rq->prev_irq_time;
  675. /*
  676. * Since irq_time is only updated on {soft,}irq_exit, we might run into
  677. * this case when a previous update_rq_clock() happened inside a
  678. * {soft,}irq region.
  679. *
  680. * When this happens, we stop ->clock_task and only update the
  681. * prev_irq_time stamp to account for the part that fit, so that a next
  682. * update will consume the rest. This ensures ->clock_task is
  683. * monotonic.
  684. *
  685. * It does however cause some slight miss-attribution of {soft,}irq
  686. * time, a more accurate solution would be to update the irq_time using
  687. * the current rq->clock timestamp, except that would require using
  688. * atomic ops.
  689. */
  690. if (irq_delta > delta)
  691. irq_delta = delta;
  692. rq->prev_irq_time += irq_delta;
  693. delta -= irq_delta;
  694. #endif
  695. #ifdef CONFIG_PARAVIRT_TIME_ACCOUNTING
  696. if (static_key_false((&paravirt_steal_rq_enabled))) {
  697. u64 st;
  698. steal = paravirt_steal_clock(cpu_of(rq));
  699. steal -= rq->prev_steal_time_rq;
  700. if (unlikely(steal > delta))
  701. steal = delta;
  702. st = steal_ticks(steal);
  703. steal = st * TICK_NSEC;
  704. rq->prev_steal_time_rq += steal;
  705. delta -= steal;
  706. }
  707. #endif
  708. rq->clock_task += delta;
  709. #if defined(CONFIG_IRQ_TIME_ACCOUNTING) || defined(CONFIG_PARAVIRT_TIME_ACCOUNTING)
  710. if ((irq_delta + steal) && sched_feat(NONTASK_POWER))
  711. sched_rt_avg_update(rq, irq_delta + steal);
  712. #endif
  713. }
  714. void sched_set_stop_task(int cpu, struct task_struct *stop)
  715. {
  716. struct sched_param param = { .sched_priority = MAX_RT_PRIO - 1 };
  717. struct task_struct *old_stop = cpu_rq(cpu)->stop;
  718. if (stop) {
  719. /*
  720. * Make it appear like a SCHED_FIFO task, its something
  721. * userspace knows about and won't get confused about.
  722. *
  723. * Also, it will make PI more or less work without too
  724. * much confusion -- but then, stop work should not
  725. * rely on PI working anyway.
  726. */
  727. sched_setscheduler_nocheck(stop, SCHED_FIFO, &param);
  728. stop->sched_class = &stop_sched_class;
  729. }
  730. cpu_rq(cpu)->stop = stop;
  731. if (old_stop) {
  732. /*
  733. * Reset it back to a normal scheduling class so that
  734. * it can die in pieces.
  735. */
  736. old_stop->sched_class = &rt_sched_class;
  737. }
  738. }
  739. /*
  740. * __normal_prio - return the priority that is based on the static prio
  741. */
  742. static inline int __normal_prio(struct task_struct *p)
  743. {
  744. return p->static_prio;
  745. }
  746. /*
  747. * Calculate the expected normal priority: i.e. priority
  748. * without taking RT-inheritance into account. Might be
  749. * boosted by interactivity modifiers. Changes upon fork,
  750. * setprio syscalls, and whenever the interactivity
  751. * estimator recalculates.
  752. */
  753. static inline int normal_prio(struct task_struct *p)
  754. {
  755. int prio;
  756. if (task_has_rt_policy(p))
  757. prio = MAX_RT_PRIO-1 - p->rt_priority;
  758. else
  759. prio = __normal_prio(p);
  760. return prio;
  761. }
  762. /*
  763. * Calculate the current priority, i.e. the priority
  764. * taken into account by the scheduler. This value might
  765. * be boosted by RT tasks, or might be boosted by
  766. * interactivity modifiers. Will be RT if the task got
  767. * RT-boosted. If not then it returns p->normal_prio.
  768. */
  769. static int effective_prio(struct task_struct *p)
  770. {
  771. p->normal_prio = normal_prio(p);
  772. /*
  773. * If we are RT tasks or we were boosted to RT priority,
  774. * keep the priority unchanged. Otherwise, update priority
  775. * to the normal priority:
  776. */
  777. if (!rt_prio(p->prio))
  778. return p->normal_prio;
  779. return p->prio;
  780. }
  781. /**
  782. * task_curr - is this task currently executing on a CPU?
  783. * @p: the task in question.
  784. */
  785. inline int task_curr(const struct task_struct *p)
  786. {
  787. return cpu_curr(task_cpu(p)) == p;
  788. }
  789. static inline void check_class_changed(struct rq *rq, struct task_struct *p,
  790. const struct sched_class *prev_class,
  791. int oldprio)
  792. {
  793. if (prev_class != p->sched_class) {
  794. if (prev_class->switched_from)
  795. prev_class->switched_from(rq, p);
  796. p->sched_class->switched_to(rq, p);
  797. } else if (oldprio != p->prio)
  798. p->sched_class->prio_changed(rq, p, oldprio);
  799. }
  800. void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
  801. {
  802. const struct sched_class *class;
  803. if (p->sched_class == rq->curr->sched_class) {
  804. rq->curr->sched_class->check_preempt_curr(rq, p, flags);
  805. } else {
  806. for_each_class(class) {
  807. if (class == rq->curr->sched_class)
  808. break;
  809. if (class == p->sched_class) {
  810. resched_task(rq->curr);
  811. break;
  812. }
  813. }
  814. }
  815. /*
  816. * A queue event has occurred, and we're going to schedule. In
  817. * this case, we can save a useless back to back clock update.
  818. */
  819. if (rq->curr->on_rq && test_tsk_need_resched(rq->curr))
  820. rq->skip_clock_update = 1;
  821. }
  822. static ATOMIC_NOTIFIER_HEAD(task_migration_notifier);
  823. void register_task_migration_notifier(struct notifier_block *n)
  824. {
  825. atomic_notifier_chain_register(&task_migration_notifier, n);
  826. }
  827. #ifdef CONFIG_SMP
  828. void set_task_cpu(struct task_struct *p, unsigned int new_cpu)
  829. {
  830. #ifdef CONFIG_SCHED_DEBUG
  831. /*
  832. * We should never call set_task_cpu() on a blocked task,
  833. * ttwu() will sort out the placement.
  834. */
  835. WARN_ON_ONCE(p->state != TASK_RUNNING && p->state != TASK_WAKING &&
  836. !(task_thread_info(p)->preempt_count & PREEMPT_ACTIVE));
  837. #ifdef CONFIG_LOCKDEP
  838. /*
  839. * The caller should hold either p->pi_lock or rq->lock, when changing
  840. * a task's CPU. ->pi_lock for waking tasks, rq->lock for runnable tasks.
  841. *
  842. * sched_move_task() holds both and thus holding either pins the cgroup,
  843. * see task_group().
  844. *
  845. * Furthermore, all task_rq users should acquire both locks, see
  846. * task_rq_lock().
  847. */
  848. WARN_ON_ONCE(debug_locks && !(lockdep_is_held(&p->pi_lock) ||
  849. lockdep_is_held(&task_rq(p)->lock)));
  850. #endif
  851. #endif
  852. trace_sched_migrate_task(p, new_cpu);
  853. if (task_cpu(p) != new_cpu) {
  854. struct task_migration_notifier tmn;
  855. if (p->sched_class->migrate_task_rq)
  856. p->sched_class->migrate_task_rq(p, new_cpu);
  857. p->se.nr_migrations++;
  858. perf_sw_event(PERF_COUNT_SW_CPU_MIGRATIONS, 1, NULL, 0);
  859. tmn.task = p;
  860. tmn.from_cpu = task_cpu(p);
  861. tmn.to_cpu = new_cpu;
  862. atomic_notifier_call_chain(&task_migration_notifier, 0, &tmn);
  863. }
  864. __set_task_cpu(p, new_cpu);
  865. }
  866. struct migration_arg {
  867. struct task_struct *task;
  868. int dest_cpu;
  869. };
  870. static int migration_cpu_stop(void *data);
  871. /*
  872. * wait_task_inactive - wait for a thread to unschedule.
  873. *
  874. * If @match_state is nonzero, it's the @p->state value just checked and
  875. * not expected to change. If it changes, i.e. @p might have woken up,
  876. * then return zero. When we succeed in waiting for @p to be off its CPU,
  877. * we return a positive number (its total switch count). If a second call
  878. * a short while later returns the same number, the caller can be sure that
  879. * @p has remained unscheduled the whole time.
  880. *
  881. * The caller must ensure that the task *will* unschedule sometime soon,
  882. * else this function might spin for a *long* time. This function can't
  883. * be called with interrupts off, or it may introduce deadlock with
  884. * smp_call_function() if an IPI is sent by the same process we are
  885. * waiting to become inactive.
  886. */
  887. unsigned long wait_task_inactive(struct task_struct *p, long match_state)
  888. {
  889. unsigned long flags;
  890. int running, on_rq;
  891. unsigned long ncsw;
  892. struct rq *rq;
  893. for (;;) {
  894. /*
  895. * We do the initial early heuristics without holding
  896. * any task-queue locks at all. We'll only try to get
  897. * the runqueue lock when things look like they will
  898. * work out!
  899. */
  900. rq = task_rq(p);
  901. /*
  902. * If the task is actively running on another CPU
  903. * still, just relax and busy-wait without holding
  904. * any locks.
  905. *
  906. * NOTE! Since we don't hold any locks, it's not
  907. * even sure that "rq" stays as the right runqueue!
  908. * But we don't care, since "task_running()" will
  909. * return false if the runqueue has changed and p
  910. * is actually now running somewhere else!
  911. */
  912. while (task_running(rq, p)) {
  913. if (match_state && unlikely(p->state != match_state))
  914. return 0;
  915. cpu_relax();
  916. }
  917. /*
  918. * Ok, time to look more closely! We need the rq
  919. * lock now, to be *sure*. If we're wrong, we'll
  920. * just go back and repeat.
  921. */
  922. rq = task_rq_lock(p, &flags);
  923. trace_sched_wait_task(p);
  924. running = task_running(rq, p);
  925. on_rq = p->on_rq;
  926. ncsw = 0;
  927. if (!match_state || p->state == match_state)
  928. ncsw = p->nvcsw | LONG_MIN; /* sets MSB */
  929. task_rq_unlock(rq, p, &flags);
  930. /*
  931. * If it changed from the expected state, bail out now.
  932. */
  933. if (unlikely(!ncsw))
  934. break;
  935. /*
  936. * Was it really running after all now that we
  937. * checked with the proper locks actually held?
  938. *
  939. * Oops. Go back and try again..
  940. */
  941. if (unlikely(running)) {
  942. cpu_relax();
  943. continue;
  944. }
  945. /*
  946. * It's not enough that it's not actively running,
  947. * it must be off the runqueue _entirely_, and not
  948. * preempted!
  949. *
  950. * So if it was still runnable (but just not actively
  951. * running right now), it's preempted, and we should
  952. * yield - it could be a while.
  953. */
  954. if (unlikely(on_rq)) {
  955. ktime_t to = ktime_set(0, NSEC_PER_SEC/HZ);
  956. set_current_state(TASK_UNINTERRUPTIBLE);
  957. schedule_hrtimeout(&to, HRTIMER_MODE_REL);
  958. continue;
  959. }
  960. /*
  961. * Ahh, all good. It wasn't running, and it wasn't
  962. * runnable, which means that it will never become
  963. * running in the future either. We're all done!
  964. */
  965. break;
  966. }
  967. return ncsw;
  968. }
  969. /***
  970. * kick_process - kick a running thread to enter/exit the kernel
  971. * @p: the to-be-kicked thread
  972. *
  973. * Cause a process which is running on another CPU to enter
  974. * kernel-mode, without any delay. (to get signals handled.)
  975. *
  976. * NOTE: this function doesn't have to take the runqueue lock,
  977. * because all it wants to ensure is that the remote task enters
  978. * the kernel. If the IPI races and the task has been migrated
  979. * to another CPU then no harm is done and the purpose has been
  980. * achieved as well.
  981. */
  982. void kick_process(struct task_struct *p)
  983. {
  984. int cpu;
  985. preempt_disable();
  986. cpu = task_cpu(p);
  987. if ((cpu != smp_processor_id()) && task_curr(p))
  988. smp_send_reschedule(cpu);
  989. preempt_enable();
  990. }
  991. EXPORT_SYMBOL_GPL(kick_process);
  992. #endif /* CONFIG_SMP */
  993. #ifdef CONFIG_SMP
  994. /*
  995. * ->cpus_allowed is protected by both rq->lock and p->pi_lock
  996. */
  997. static int select_fallback_rq(int cpu, struct task_struct *p)
  998. {
  999. int nid = cpu_to_node(cpu);
  1000. const struct cpumask *nodemask = NULL;
  1001. enum { cpuset, possible, fail } state = cpuset;
  1002. int dest_cpu;
  1003. /*
  1004. * If the node that the cpu is on has been offlined, cpu_to_node()
  1005. * will return -1. There is no cpu on the node, and we should
  1006. * select the cpu on the other node.
  1007. */
  1008. if (nid != -1) {
  1009. nodemask = cpumask_of_node(nid);
  1010. /* Look for allowed, online CPU in same node. */
  1011. for_each_cpu(dest_cpu, nodemask) {
  1012. if (!cpu_online(dest_cpu))
  1013. continue;
  1014. if (!cpu_active(dest_cpu))
  1015. continue;
  1016. if (cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
  1017. return dest_cpu;
  1018. }
  1019. }
  1020. for (;;) {
  1021. /* Any allowed, online CPU? */
  1022. for_each_cpu(dest_cpu, tsk_cpus_allowed(p)) {
  1023. if (!cpu_online(dest_cpu))
  1024. continue;
  1025. if (!cpu_active(dest_cpu))
  1026. continue;
  1027. goto out;
  1028. }
  1029. switch (state) {
  1030. case cpuset:
  1031. /* No more Mr. Nice Guy. */
  1032. cpuset_cpus_allowed_fallback(p);
  1033. state = possible;
  1034. break;
  1035. case possible:
  1036. do_set_cpus_allowed(p, cpu_possible_mask);
  1037. state = fail;
  1038. break;
  1039. case fail:
  1040. BUG();
  1041. break;
  1042. }
  1043. }
  1044. out:
  1045. if (state != cpuset) {
  1046. /*
  1047. * Don't tell them about moving exiting tasks or
  1048. * kernel threads (both mm NULL), since they never
  1049. * leave kernel.
  1050. */
  1051. if (p->mm && printk_ratelimit()) {
  1052. printk_sched("process %d (%s) no longer affine to cpu%d\n",
  1053. task_pid_nr(p), p->comm, cpu);
  1054. }
  1055. }
  1056. return dest_cpu;
  1057. }
  1058. /*
  1059. * The caller (fork, wakeup) owns p->pi_lock, ->cpus_allowed is stable.
  1060. */
  1061. static inline
  1062. int select_task_rq(struct task_struct *p, int sd_flags, int wake_flags)
  1063. {
  1064. int cpu = p->sched_class->select_task_rq(p, sd_flags, wake_flags);
  1065. /*
  1066. * In order not to call set_task_cpu() on a blocking task we need
  1067. * to rely on ttwu() to place the task on a valid ->cpus_allowed
  1068. * cpu.
  1069. *
  1070. * Since this is common to all placement strategies, this lives here.
  1071. *
  1072. * [ this allows ->select_task() to simply return task_cpu(p) and
  1073. * not worry about this generic constraint ]
  1074. */
  1075. if (unlikely(!cpumask_test_cpu(cpu, tsk_cpus_allowed(p)) ||
  1076. !cpu_online(cpu)))
  1077. cpu = select_fallback_rq(task_cpu(p), p);
  1078. return cpu;
  1079. }
  1080. static void update_avg(u64 *avg, u64 sample)
  1081. {
  1082. s64 diff = sample - *avg;
  1083. *avg += diff >> 3;
  1084. }
  1085. #endif
  1086. static void
  1087. ttwu_stat(struct task_struct *p, int cpu, int wake_flags)
  1088. {
  1089. #ifdef CONFIG_SCHEDSTATS
  1090. struct rq *rq = this_rq();
  1091. #ifdef CONFIG_SMP
  1092. int this_cpu = smp_processor_id();
  1093. if (cpu == this_cpu) {
  1094. schedstat_inc(rq, ttwu_local);
  1095. schedstat_inc(p, se.statistics.nr_wakeups_local);
  1096. } else {
  1097. struct sched_domain *sd;
  1098. schedstat_inc(p, se.statistics.nr_wakeups_remote);
  1099. rcu_read_lock();
  1100. for_each_domain(this_cpu, sd) {
  1101. if (cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  1102. schedstat_inc(sd, ttwu_wake_remote);
  1103. break;
  1104. }
  1105. }
  1106. rcu_read_unlock();
  1107. }
  1108. if (wake_flags & WF_MIGRATED)
  1109. schedstat_inc(p, se.statistics.nr_wakeups_migrate);
  1110. #endif /* CONFIG_SMP */
  1111. schedstat_inc(rq, ttwu_count);
  1112. schedstat_inc(p, se.statistics.nr_wakeups);
  1113. if (wake_flags & WF_SYNC)
  1114. schedstat_inc(p, se.statistics.nr_wakeups_sync);
  1115. #endif /* CONFIG_SCHEDSTATS */
  1116. }
  1117. static void ttwu_activate(struct rq *rq, struct task_struct *p, int en_flags)
  1118. {
  1119. activate_task(rq, p, en_flags);
  1120. p->on_rq = 1;
  1121. /* if a worker is waking up, notify workqueue */
  1122. if (p->flags & PF_WQ_WORKER)
  1123. wq_worker_waking_up(p, cpu_of(rq));
  1124. }
  1125. /*
  1126. * Mark the task runnable and perform wakeup-preemption.
  1127. */
  1128. static void
  1129. ttwu_do_wakeup(struct rq *rq, struct task_struct *p, int wake_flags)
  1130. {
  1131. check_preempt_curr(rq, p, wake_flags);
  1132. trace_sched_wakeup(p, true);
  1133. p->state = TASK_RUNNING;
  1134. #ifdef CONFIG_SMP
  1135. if (p->sched_class->task_woken)
  1136. p->sched_class->task_woken(rq, p);
  1137. if (rq->idle_stamp) {
  1138. u64 delta = rq_clock(rq) - rq->idle_stamp;
  1139. u64 max = 2*sysctl_sched_migration_cost;
  1140. if (delta > max)
  1141. rq->avg_idle = max;
  1142. else
  1143. update_avg(&rq->avg_idle, delta);
  1144. rq->idle_stamp = 0;
  1145. }
  1146. #endif
  1147. }
  1148. static void
  1149. ttwu_do_activate(struct rq *rq, struct task_struct *p, int wake_flags)
  1150. {
  1151. #ifdef CONFIG_SMP
  1152. if (p->sched_contributes_to_load)
  1153. rq->nr_uninterruptible--;
  1154. #endif
  1155. ttwu_activate(rq, p, ENQUEUE_WAKEUP | ENQUEUE_WAKING);
  1156. ttwu_do_wakeup(rq, p, wake_flags);
  1157. }
  1158. /*
  1159. * Called in case the task @p isn't fully descheduled from its runqueue,
  1160. * in this case we must do a remote wakeup. Its a 'light' wakeup though,
  1161. * since all we need to do is flip p->state to TASK_RUNNING, since
  1162. * the task is still ->on_rq.
  1163. */
  1164. static int ttwu_remote(struct task_struct *p, int wake_flags)
  1165. {
  1166. struct rq *rq;
  1167. int ret = 0;
  1168. rq = __task_rq_lock(p);
  1169. if (p->on_rq) {
  1170. /* check_preempt_curr() may use rq clock */
  1171. update_rq_clock(rq);
  1172. ttwu_do_wakeup(rq, p, wake_flags);
  1173. ret = 1;
  1174. }
  1175. __task_rq_unlock(rq);
  1176. return ret;
  1177. }
  1178. #ifdef CONFIG_SMP
  1179. static void sched_ttwu_pending(void)
  1180. {
  1181. struct rq *rq = this_rq();
  1182. struct llist_node *llist = llist_del_all(&rq->wake_list);
  1183. struct task_struct *p;
  1184. raw_spin_lock(&rq->lock);
  1185. while (llist) {
  1186. p = llist_entry(llist, struct task_struct, wake_entry);
  1187. llist = llist_next(llist);
  1188. ttwu_do_activate(rq, p, 0);
  1189. }
  1190. raw_spin_unlock(&rq->lock);
  1191. }
  1192. void scheduler_ipi(void)
  1193. {
  1194. if (llist_empty(&this_rq()->wake_list)
  1195. && !tick_nohz_full_cpu(smp_processor_id())
  1196. && !got_nohz_idle_kick())
  1197. return;
  1198. /*
  1199. * Not all reschedule IPI handlers call irq_enter/irq_exit, since
  1200. * traditionally all their work was done from the interrupt return
  1201. * path. Now that we actually do some work, we need to make sure
  1202. * we do call them.
  1203. *
  1204. * Some archs already do call them, luckily irq_enter/exit nest
  1205. * properly.
  1206. *
  1207. * Arguably we should visit all archs and update all handlers,
  1208. * however a fair share of IPIs are still resched only so this would
  1209. * somewhat pessimize the simple resched case.
  1210. */
  1211. irq_enter();
  1212. tick_nohz_full_check();
  1213. sched_ttwu_pending();
  1214. /*
  1215. * Check if someone kicked us for doing the nohz idle load balance.
  1216. */
  1217. if (unlikely(got_nohz_idle_kick())) {
  1218. this_rq()->idle_balance = 1;
  1219. raise_softirq_irqoff(SCHED_SOFTIRQ);
  1220. }
  1221. irq_exit();
  1222. }
  1223. static void ttwu_queue_remote(struct task_struct *p, int cpu)
  1224. {
  1225. if (llist_add(&p->wake_entry, &cpu_rq(cpu)->wake_list))
  1226. smp_send_reschedule(cpu);
  1227. }
  1228. bool cpus_share_cache(int this_cpu, int that_cpu)
  1229. {
  1230. return per_cpu(sd_llc_id, this_cpu) == per_cpu(sd_llc_id, that_cpu);
  1231. }
  1232. #endif /* CONFIG_SMP */
  1233. static void ttwu_queue(struct task_struct *p, int cpu)
  1234. {
  1235. struct rq *rq = cpu_rq(cpu);
  1236. #if defined(CONFIG_SMP)
  1237. if (sched_feat(TTWU_QUEUE) && !cpus_share_cache(smp_processor_id(), cpu)) {
  1238. sched_clock_cpu(cpu); /* sync clocks x-cpu */
  1239. ttwu_queue_remote(p, cpu);
  1240. return;
  1241. }
  1242. #endif
  1243. raw_spin_lock(&rq->lock);
  1244. ttwu_do_activate(rq, p, 0);
  1245. raw_spin_unlock(&rq->lock);
  1246. }
  1247. /**
  1248. * try_to_wake_up - wake up a thread
  1249. * @p: the thread to be awakened
  1250. * @state: the mask of task states that can be woken
  1251. * @wake_flags: wake modifier flags (WF_*)
  1252. *
  1253. * Put it on the run-queue if it's not already there. The "current"
  1254. * thread is always on the run-queue (except when the actual
  1255. * re-schedule is in progress), and as such you're allowed to do
  1256. * the simpler "current->state = TASK_RUNNING" to mark yourself
  1257. * runnable without the overhead of this.
  1258. *
  1259. * Returns %true if @p was woken up, %false if it was already running
  1260. * or @state didn't match @p's state.
  1261. */
  1262. static int
  1263. try_to_wake_up(struct task_struct *p, unsigned int state, int wake_flags)
  1264. {
  1265. unsigned long flags;
  1266. int cpu, success = 0;
  1267. smp_wmb();
  1268. raw_spin_lock_irqsave(&p->pi_lock, flags);
  1269. if (!(p->state & state))
  1270. goto out;
  1271. success = 1; /* we're going to change ->state */
  1272. cpu = task_cpu(p);
  1273. if (p->on_rq && ttwu_remote(p, wake_flags))
  1274. goto stat;
  1275. #ifdef CONFIG_SMP
  1276. /*
  1277. * If the owning (remote) cpu is still in the middle of schedule() with
  1278. * this task as prev, wait until its done referencing the task.
  1279. */
  1280. while (p->on_cpu)
  1281. cpu_relax();
  1282. /*
  1283. * Pairs with the smp_wmb() in finish_lock_switch().
  1284. */
  1285. smp_rmb();
  1286. p->sched_contributes_to_load = !!task_contributes_to_load(p);
  1287. p->state = TASK_WAKING;
  1288. if (p->sched_class->task_waking)
  1289. p->sched_class->task_waking(p);
  1290. cpu = select_task_rq(p, SD_BALANCE_WAKE, wake_flags);
  1291. if (task_cpu(p) != cpu) {
  1292. wake_flags |= WF_MIGRATED;
  1293. set_task_cpu(p, cpu);
  1294. }
  1295. #endif /* CONFIG_SMP */
  1296. ttwu_queue(p, cpu);
  1297. stat:
  1298. ttwu_stat(p, cpu, wake_flags);
  1299. out:
  1300. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  1301. return success;
  1302. }
  1303. /**
  1304. * try_to_wake_up_local - try to wake up a local task with rq lock held
  1305. * @p: the thread to be awakened
  1306. *
  1307. * Put @p on the run-queue if it's not already there. The caller must
  1308. * ensure that this_rq() is locked, @p is bound to this_rq() and not
  1309. * the current task.
  1310. */
  1311. static void try_to_wake_up_local(struct task_struct *p)
  1312. {
  1313. struct rq *rq = task_rq(p);
  1314. if (WARN_ON_ONCE(rq != this_rq()) ||
  1315. WARN_ON_ONCE(p == current))
  1316. return;
  1317. lockdep_assert_held(&rq->lock);
  1318. if (!raw_spin_trylock(&p->pi_lock)) {
  1319. raw_spin_unlock(&rq->lock);
  1320. raw_spin_lock(&p->pi_lock);
  1321. raw_spin_lock(&rq->lock);
  1322. }
  1323. if (!(p->state & TASK_NORMAL))
  1324. goto out;
  1325. if (!p->on_rq)
  1326. ttwu_activate(rq, p, ENQUEUE_WAKEUP);
  1327. ttwu_do_wakeup(rq, p, 0);
  1328. ttwu_stat(p, smp_processor_id(), 0);
  1329. out:
  1330. raw_spin_unlock(&p->pi_lock);
  1331. }
  1332. /**
  1333. * wake_up_process - Wake up a specific process
  1334. * @p: The process to be woken up.
  1335. *
  1336. * Attempt to wake up the nominated process and move it to the set of runnable
  1337. * processes. Returns 1 if the process was woken up, 0 if it was already
  1338. * running.
  1339. *
  1340. * It may be assumed that this function implies a write memory barrier before
  1341. * changing the task state if and only if any tasks are woken up.
  1342. */
  1343. int wake_up_process(struct task_struct *p)
  1344. {
  1345. WARN_ON(task_is_stopped_or_traced(p));
  1346. return try_to_wake_up(p, TASK_NORMAL, 0);
  1347. }
  1348. EXPORT_SYMBOL(wake_up_process);
  1349. int wake_up_state(struct task_struct *p, unsigned int state)
  1350. {
  1351. return try_to_wake_up(p, state, 0);
  1352. }
  1353. /*
  1354. * Perform scheduler related setup for a newly forked process p.
  1355. * p is forked by current.
  1356. *
  1357. * __sched_fork() is basic setup used by init_idle() too:
  1358. */
  1359. static void __sched_fork(struct task_struct *p)
  1360. {
  1361. p->on_rq = 0;
  1362. p->se.on_rq = 0;
  1363. p->se.exec_start = 0;
  1364. p->se.sum_exec_runtime = 0;
  1365. p->se.prev_sum_exec_runtime = 0;
  1366. p->se.nr_migrations = 0;
  1367. p->se.vruntime = 0;
  1368. INIT_LIST_HEAD(&p->se.group_node);
  1369. #ifdef CONFIG_SCHEDSTATS
  1370. memset(&p->se.statistics, 0, sizeof(p->se.statistics));
  1371. #endif
  1372. INIT_LIST_HEAD(&p->rt.run_list);
  1373. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1374. INIT_HLIST_HEAD(&p->preempt_notifiers);
  1375. #endif
  1376. #ifdef CONFIG_NUMA_BALANCING
  1377. if (p->mm && atomic_read(&p->mm->mm_users) == 1) {
  1378. p->mm->numa_next_scan = jiffies;
  1379. p->mm->numa_next_reset = jiffies;
  1380. p->mm->numa_scan_seq = 0;
  1381. }
  1382. p->node_stamp = 0ULL;
  1383. p->numa_scan_seq = p->mm ? p->mm->numa_scan_seq : 0;
  1384. p->numa_migrate_seq = p->mm ? p->mm->numa_scan_seq - 1 : 0;
  1385. p->numa_scan_period = sysctl_numa_balancing_scan_delay;
  1386. p->numa_work.next = &p->numa_work;
  1387. #endif /* CONFIG_NUMA_BALANCING */
  1388. }
  1389. #ifdef CONFIG_NUMA_BALANCING
  1390. #ifdef CONFIG_SCHED_DEBUG
  1391. void set_numabalancing_state(bool enabled)
  1392. {
  1393. if (enabled)
  1394. sched_feat_set("NUMA");
  1395. else
  1396. sched_feat_set("NO_NUMA");
  1397. }
  1398. #else
  1399. __read_mostly bool numabalancing_enabled;
  1400. void set_numabalancing_state(bool enabled)
  1401. {
  1402. numabalancing_enabled = enabled;
  1403. }
  1404. #endif /* CONFIG_SCHED_DEBUG */
  1405. #endif /* CONFIG_NUMA_BALANCING */
  1406. /*
  1407. * fork()/clone()-time setup:
  1408. */
  1409. void sched_fork(struct task_struct *p)
  1410. {
  1411. unsigned long flags;
  1412. int cpu = get_cpu();
  1413. __sched_fork(p);
  1414. /*
  1415. * We mark the process as running here. This guarantees that
  1416. * nobody will actually run it, and a signal or other external
  1417. * event cannot wake it up and insert it on the runqueue either.
  1418. */
  1419. p->state = TASK_RUNNING;
  1420. /*
  1421. * Make sure we do not leak PI boosting priority to the child.
  1422. */
  1423. p->prio = current->normal_prio;
  1424. /*
  1425. * Revert to default priority/policy on fork if requested.
  1426. */
  1427. if (unlikely(p->sched_reset_on_fork)) {
  1428. if (task_has_rt_policy(p)) {
  1429. p->policy = SCHED_NORMAL;
  1430. p->static_prio = NICE_TO_PRIO(0);
  1431. p->rt_priority = 0;
  1432. } else if (PRIO_TO_NICE(p->static_prio) < 0)
  1433. p->static_prio = NICE_TO_PRIO(0);
  1434. p->prio = p->normal_prio = __normal_prio(p);
  1435. set_load_weight(p);
  1436. /*
  1437. * We don't need the reset flag anymore after the fork. It has
  1438. * fulfilled its duty:
  1439. */
  1440. p->sched_reset_on_fork = 0;
  1441. }
  1442. if (!rt_prio(p->prio))
  1443. p->sched_class = &fair_sched_class;
  1444. if (p->sched_class->task_fork)
  1445. p->sched_class->task_fork(p);
  1446. /*
  1447. * The child is not yet in the pid-hash so no cgroup attach races,
  1448. * and the cgroup is pinned to this child due to cgroup_fork()
  1449. * is ran before sched_fork().
  1450. *
  1451. * Silence PROVE_RCU.
  1452. */
  1453. raw_spin_lock_irqsave(&p->pi_lock, flags);
  1454. set_task_cpu(p, cpu);
  1455. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  1456. #if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
  1457. if (likely(sched_info_on()))
  1458. memset(&p->sched_info, 0, sizeof(p->sched_info));
  1459. #endif
  1460. #if defined(CONFIG_SMP)
  1461. p->on_cpu = 0;
  1462. #endif
  1463. #ifdef CONFIG_PREEMPT_COUNT
  1464. /* Want to start with kernel preemption disabled. */
  1465. task_thread_info(p)->preempt_count = 1;
  1466. #endif
  1467. #ifdef CONFIG_SMP
  1468. plist_node_init(&p->pushable_tasks, MAX_PRIO);
  1469. #endif
  1470. put_cpu();
  1471. }
  1472. /*
  1473. * wake_up_new_task - wake up a newly created task for the first time.
  1474. *
  1475. * This function will do some initial scheduler statistics housekeeping
  1476. * that must be done for every newly created context, then puts the task
  1477. * on the runqueue and wakes it.
  1478. */
  1479. void wake_up_new_task(struct task_struct *p)
  1480. {
  1481. unsigned long flags;
  1482. struct rq *rq;
  1483. raw_spin_lock_irqsave(&p->pi_lock, flags);
  1484. #ifdef CONFIG_SMP
  1485. /*
  1486. * Fork balancing, do it here and not earlier because:
  1487. * - cpus_allowed can change in the fork path
  1488. * - any previously selected cpu might disappear through hotplug
  1489. */
  1490. set_task_cpu(p, select_task_rq(p, SD_BALANCE_FORK, 0));
  1491. #endif
  1492. /* Initialize new task's runnable average */
  1493. init_task_runnable_average(p);
  1494. rq = __task_rq_lock(p);
  1495. activate_task(rq, p, 0);
  1496. p->on_rq = 1;
  1497. trace_sched_wakeup_new(p, true);
  1498. check_preempt_curr(rq, p, WF_FORK);
  1499. #ifdef CONFIG_SMP
  1500. if (p->sched_class->task_woken)
  1501. p->sched_class->task_woken(rq, p);
  1502. #endif
  1503. task_rq_unlock(rq, p, &flags);
  1504. }
  1505. #ifdef CONFIG_PREEMPT_NOTIFIERS
  1506. /**
  1507. * preempt_notifier_register - tell me when current is being preempted & rescheduled
  1508. * @notifier: notifier struct to register
  1509. */
  1510. void preempt_notifier_register(struct preempt_notifier *notifier)
  1511. {
  1512. hlist_add_head(&notifier->link, &current->preempt_notifiers);
  1513. }
  1514. EXPORT_SYMBOL_GPL(preempt_notifier_register);
  1515. /**
  1516. * preempt_notifier_unregister - no longer interested in preemption notifications
  1517. * @notifier: notifier struct to unregister
  1518. *
  1519. * This is safe to call from within a preemption notifier.
  1520. */
  1521. void preempt_notifier_unregister(struct preempt_notifier *notifier)
  1522. {
  1523. hlist_del(&notifier->link);
  1524. }
  1525. EXPORT_SYMBOL_GPL(preempt_notifier_unregister);
  1526. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1527. {
  1528. struct preempt_notifier *notifier;
  1529. hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
  1530. notifier->ops->sched_in(notifier, raw_smp_processor_id());
  1531. }
  1532. static void
  1533. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1534. struct task_struct *next)
  1535. {
  1536. struct preempt_notifier *notifier;
  1537. hlist_for_each_entry(notifier, &curr->preempt_notifiers, link)
  1538. notifier->ops->sched_out(notifier, next);
  1539. }
  1540. #else /* !CONFIG_PREEMPT_NOTIFIERS */
  1541. static void fire_sched_in_preempt_notifiers(struct task_struct *curr)
  1542. {
  1543. }
  1544. static void
  1545. fire_sched_out_preempt_notifiers(struct task_struct *curr,
  1546. struct task_struct *next)
  1547. {
  1548. }
  1549. #endif /* CONFIG_PREEMPT_NOTIFIERS */
  1550. /**
  1551. * prepare_task_switch - prepare to switch tasks
  1552. * @rq: the runqueue preparing to switch
  1553. * @prev: the current task that is being switched out
  1554. * @next: the task we are going to switch to.
  1555. *
  1556. * This is called with the rq lock held and interrupts off. It must
  1557. * be paired with a subsequent finish_task_switch after the context
  1558. * switch.
  1559. *
  1560. * prepare_task_switch sets up locking and calls architecture specific
  1561. * hooks.
  1562. */
  1563. static inline void
  1564. prepare_task_switch(struct rq *rq, struct task_struct *prev,
  1565. struct task_struct *next)
  1566. {
  1567. trace_sched_switch(prev, next);
  1568. sched_info_switch(prev, next);
  1569. perf_event_task_sched_out(prev, next);
  1570. fire_sched_out_preempt_notifiers(prev, next);
  1571. prepare_lock_switch(rq, next);
  1572. prepare_arch_switch(next);
  1573. }
  1574. /**
  1575. * finish_task_switch - clean up after a task-switch
  1576. * @rq: runqueue associated with task-switch
  1577. * @prev: the thread we just switched away from.
  1578. *
  1579. * finish_task_switch must be called after the context switch, paired
  1580. * with a prepare_task_switch call before the context switch.
  1581. * finish_task_switch will reconcile locking set up by prepare_task_switch,
  1582. * and do any other architecture-specific cleanup actions.
  1583. *
  1584. * Note that we may have delayed dropping an mm in context_switch(). If
  1585. * so, we finish that here outside of the runqueue lock. (Doing it
  1586. * with the lock held can cause deadlocks; see schedule() for
  1587. * details.)
  1588. */
  1589. static void finish_task_switch(struct rq *rq, struct task_struct *prev)
  1590. __releases(rq->lock)
  1591. {
  1592. struct mm_struct *mm = rq->prev_mm;
  1593. long prev_state;
  1594. rq->prev_mm = NULL;
  1595. /*
  1596. * A task struct has one reference for the use as "current".
  1597. * If a task dies, then it sets TASK_DEAD in tsk->state and calls
  1598. * schedule one last time. The schedule call will never return, and
  1599. * the scheduled task must drop that reference.
  1600. * The test for TASK_DEAD must occur while the runqueue locks are
  1601. * still held, otherwise prev could be scheduled on another cpu, die
  1602. * there before we look at prev->state, and then the reference would
  1603. * be dropped twice.
  1604. * Manfred Spraul <manfred@colorfullife.com>
  1605. */
  1606. prev_state = prev->state;
  1607. vtime_task_switch(prev);
  1608. finish_arch_switch(prev);
  1609. perf_event_task_sched_in(prev, current);
  1610. finish_lock_switch(rq, prev);
  1611. finish_arch_post_lock_switch();
  1612. fire_sched_in_preempt_notifiers(current);
  1613. if (mm)
  1614. mmdrop(mm);
  1615. if (unlikely(prev_state == TASK_DEAD)) {
  1616. /*
  1617. * Remove function-return probe instances associated with this
  1618. * task and put them back on the free list.
  1619. */
  1620. kprobe_flush_task(prev);
  1621. put_task_struct(prev);
  1622. }
  1623. tick_nohz_task_switch(current);
  1624. }
  1625. #ifdef CONFIG_SMP
  1626. /* assumes rq->lock is held */
  1627. static inline void pre_schedule(struct rq *rq, struct task_struct *prev)
  1628. {
  1629. if (prev->sched_class->pre_schedule)
  1630. prev->sched_class->pre_schedule(rq, prev);
  1631. }
  1632. /* rq->lock is NOT held, but preemption is disabled */
  1633. static inline void post_schedule(struct rq *rq)
  1634. {
  1635. if (rq->post_schedule) {
  1636. unsigned long flags;
  1637. raw_spin_lock_irqsave(&rq->lock, flags);
  1638. if (rq->curr->sched_class->post_schedule)
  1639. rq->curr->sched_class->post_schedule(rq);
  1640. raw_spin_unlock_irqrestore(&rq->lock, flags);
  1641. rq->post_schedule = 0;
  1642. }
  1643. }
  1644. #else
  1645. static inline void pre_schedule(struct rq *rq, struct task_struct *p)
  1646. {
  1647. }
  1648. static inline void post_schedule(struct rq *rq)
  1649. {
  1650. }
  1651. #endif
  1652. /**
  1653. * schedule_tail - first thing a freshly forked thread must call.
  1654. * @prev: the thread we just switched away from.
  1655. */
  1656. asmlinkage void schedule_tail(struct task_struct *prev)
  1657. __releases(rq->lock)
  1658. {
  1659. struct rq *rq = this_rq();
  1660. finish_task_switch(rq, prev);
  1661. /*
  1662. * FIXME: do we need to worry about rq being invalidated by the
  1663. * task_switch?
  1664. */
  1665. post_schedule(rq);
  1666. #ifdef __ARCH_WANT_UNLOCKED_CTXSW
  1667. /* In this case, finish_task_switch does not reenable preemption */
  1668. preempt_enable();
  1669. #endif
  1670. if (current->set_child_tid)
  1671. put_user(task_pid_vnr(current), current->set_child_tid);
  1672. }
  1673. /*
  1674. * context_switch - switch to the new MM and the new
  1675. * thread's register state.
  1676. */
  1677. static inline void
  1678. context_switch(struct rq *rq, struct task_struct *prev,
  1679. struct task_struct *next)
  1680. {
  1681. struct mm_struct *mm, *oldmm;
  1682. prepare_task_switch(rq, prev, next);
  1683. mm = next->mm;
  1684. oldmm = prev->active_mm;
  1685. /*
  1686. * For paravirt, this is coupled with an exit in switch_to to
  1687. * combine the page table reload and the switch backend into
  1688. * one hypercall.
  1689. */
  1690. arch_start_context_switch(prev);
  1691. if (!mm) {
  1692. next->active_mm = oldmm;
  1693. atomic_inc(&oldmm->mm_count);
  1694. enter_lazy_tlb(oldmm, next);
  1695. } else
  1696. switch_mm(oldmm, mm, next);
  1697. if (!prev->mm) {
  1698. prev->active_mm = NULL;
  1699. rq->prev_mm = oldmm;
  1700. }
  1701. /*
  1702. * Since the runqueue lock will be released by the next
  1703. * task (which is an invalid locking op but in the case
  1704. * of the scheduler it's an obvious special-case), so we
  1705. * do an early lockdep release here:
  1706. */
  1707. #ifndef __ARCH_WANT_UNLOCKED_CTXSW
  1708. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  1709. #endif
  1710. context_tracking_task_switch(prev, next);
  1711. /* Here we just switch the register state and the stack. */
  1712. switch_to(prev, next, prev);
  1713. barrier();
  1714. /*
  1715. * this_rq must be evaluated again because prev may have moved
  1716. * CPUs since it called schedule(), thus the 'rq' on its stack
  1717. * frame will be invalid.
  1718. */
  1719. finish_task_switch(this_rq(), prev);
  1720. }
  1721. /*
  1722. * nr_running and nr_context_switches:
  1723. *
  1724. * externally visible scheduler statistics: current number of runnable
  1725. * threads, total number of context switches performed since bootup.
  1726. */
  1727. unsigned long nr_running(void)
  1728. {
  1729. unsigned long i, sum = 0;
  1730. for_each_online_cpu(i)
  1731. sum += cpu_rq(i)->nr_running;
  1732. return sum;
  1733. }
  1734. unsigned long long nr_context_switches(void)
  1735. {
  1736. int i;
  1737. unsigned long long sum = 0;
  1738. for_each_possible_cpu(i)
  1739. sum += cpu_rq(i)->nr_switches;
  1740. return sum;
  1741. }
  1742. unsigned long nr_iowait(void)
  1743. {
  1744. unsigned long i, sum = 0;
  1745. for_each_possible_cpu(i)
  1746. sum += atomic_read(&cpu_rq(i)->nr_iowait);
  1747. return sum;
  1748. }
  1749. unsigned long nr_iowait_cpu(int cpu)
  1750. {
  1751. struct rq *this = cpu_rq(cpu);
  1752. return atomic_read(&this->nr_iowait);
  1753. }
  1754. #ifdef CONFIG_SMP
  1755. /*
  1756. * sched_exec - execve() is a valuable balancing opportunity, because at
  1757. * this point the task has the smallest effective memory and cache footprint.
  1758. */
  1759. void sched_exec(void)
  1760. {
  1761. struct task_struct *p = current;
  1762. unsigned long flags;
  1763. int dest_cpu;
  1764. raw_spin_lock_irqsave(&p->pi_lock, flags);
  1765. dest_cpu = p->sched_class->select_task_rq(p, SD_BALANCE_EXEC, 0);
  1766. if (dest_cpu == smp_processor_id())
  1767. goto unlock;
  1768. if (likely(cpu_active(dest_cpu))) {
  1769. struct migration_arg arg = { p, dest_cpu };
  1770. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  1771. stop_one_cpu(task_cpu(p), migration_cpu_stop, &arg);
  1772. return;
  1773. }
  1774. unlock:
  1775. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  1776. }
  1777. #endif
  1778. DEFINE_PER_CPU(struct kernel_stat, kstat);
  1779. DEFINE_PER_CPU(struct kernel_cpustat, kernel_cpustat);
  1780. EXPORT_PER_CPU_SYMBOL(kstat);
  1781. EXPORT_PER_CPU_SYMBOL(kernel_cpustat);
  1782. /*
  1783. * Return any ns on the sched_clock that have not yet been accounted in
  1784. * @p in case that task is currently running.
  1785. *
  1786. * Called with task_rq_lock() held on @rq.
  1787. */
  1788. static u64 do_task_delta_exec(struct task_struct *p, struct rq *rq)
  1789. {
  1790. u64 ns = 0;
  1791. if (task_current(rq, p)) {
  1792. update_rq_clock(rq);
  1793. ns = rq_clock_task(rq) - p->se.exec_start;
  1794. if ((s64)ns < 0)
  1795. ns = 0;
  1796. }
  1797. return ns;
  1798. }
  1799. unsigned long long task_delta_exec(struct task_struct *p)
  1800. {
  1801. unsigned long flags;
  1802. struct rq *rq;
  1803. u64 ns = 0;
  1804. rq = task_rq_lock(p, &flags);
  1805. ns = do_task_delta_exec(p, rq);
  1806. task_rq_unlock(rq, p, &flags);
  1807. return ns;
  1808. }
  1809. /*
  1810. * Return accounted runtime for the task.
  1811. * In case the task is currently running, return the runtime plus current's
  1812. * pending runtime that have not been accounted yet.
  1813. */
  1814. unsigned long long task_sched_runtime(struct task_struct *p)
  1815. {
  1816. unsigned long flags;
  1817. struct rq *rq;
  1818. u64 ns = 0;
  1819. rq = task_rq_lock(p, &flags);
  1820. ns = p->se.sum_exec_runtime + do_task_delta_exec(p, rq);
  1821. task_rq_unlock(rq, p, &flags);
  1822. return ns;
  1823. }
  1824. /*
  1825. * This function gets called by the timer code, with HZ frequency.
  1826. * We call it with interrupts disabled.
  1827. */
  1828. void scheduler_tick(void)
  1829. {
  1830. int cpu = smp_processor_id();
  1831. struct rq *rq = cpu_rq(cpu);
  1832. struct task_struct *curr = rq->curr;
  1833. sched_clock_tick();
  1834. raw_spin_lock(&rq->lock);
  1835. update_rq_clock(rq);
  1836. curr->sched_class->task_tick(rq, curr, 0);
  1837. update_cpu_load_active(rq);
  1838. raw_spin_unlock(&rq->lock);
  1839. perf_event_task_tick();
  1840. #ifdef CONFIG_SMP
  1841. rq->idle_balance = idle_cpu(cpu);
  1842. trigger_load_balance(rq, cpu);
  1843. #endif
  1844. rq_last_tick_reset(rq);
  1845. }
  1846. #ifdef CONFIG_NO_HZ_FULL
  1847. /**
  1848. * scheduler_tick_max_deferment
  1849. *
  1850. * Keep at least one tick per second when a single
  1851. * active task is running because the scheduler doesn't
  1852. * yet completely support full dynticks environment.
  1853. *
  1854. * This makes sure that uptime, CFS vruntime, load
  1855. * balancing, etc... continue to move forward, even
  1856. * with a very low granularity.
  1857. */
  1858. u64 scheduler_tick_max_deferment(void)
  1859. {
  1860. struct rq *rq = this_rq();
  1861. unsigned long next, now = ACCESS_ONCE(jiffies);
  1862. next = rq->last_sched_tick + HZ;
  1863. if (time_before_eq(next, now))
  1864. return 0;
  1865. return jiffies_to_usecs(next - now) * NSEC_PER_USEC;
  1866. }
  1867. #endif
  1868. notrace unsigned long get_parent_ip(unsigned long addr)
  1869. {
  1870. if (in_lock_functions(addr)) {
  1871. addr = CALLER_ADDR2;
  1872. if (in_lock_functions(addr))
  1873. addr = CALLER_ADDR3;
  1874. }
  1875. return addr;
  1876. }
  1877. #if defined(CONFIG_PREEMPT) && (defined(CONFIG_DEBUG_PREEMPT) || \
  1878. defined(CONFIG_PREEMPT_TRACER))
  1879. void __kprobes add_preempt_count(int val)
  1880. {
  1881. #ifdef CONFIG_DEBUG_PREEMPT
  1882. /*
  1883. * Underflow?
  1884. */
  1885. if (DEBUG_LOCKS_WARN_ON((preempt_count() < 0)))
  1886. return;
  1887. #endif
  1888. preempt_count() += val;
  1889. #ifdef CONFIG_DEBUG_PREEMPT
  1890. /*
  1891. * Spinlock count overflowing soon?
  1892. */
  1893. DEBUG_LOCKS_WARN_ON((preempt_count() & PREEMPT_MASK) >=
  1894. PREEMPT_MASK - 10);
  1895. #endif
  1896. if (preempt_count() == val)
  1897. trace_preempt_off(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  1898. }
  1899. EXPORT_SYMBOL(add_preempt_count);
  1900. void __kprobes sub_preempt_count(int val)
  1901. {
  1902. #ifdef CONFIG_DEBUG_PREEMPT
  1903. /*
  1904. * Underflow?
  1905. */
  1906. if (DEBUG_LOCKS_WARN_ON(val > preempt_count()))
  1907. return;
  1908. /*
  1909. * Is the spinlock portion underflowing?
  1910. */
  1911. if (DEBUG_LOCKS_WARN_ON((val < PREEMPT_MASK) &&
  1912. !(preempt_count() & PREEMPT_MASK)))
  1913. return;
  1914. #endif
  1915. if (preempt_count() == val)
  1916. trace_preempt_on(CALLER_ADDR0, get_parent_ip(CALLER_ADDR1));
  1917. preempt_count() -= val;
  1918. }
  1919. EXPORT_SYMBOL(sub_preempt_count);
  1920. #endif
  1921. /*
  1922. * Print scheduling while atomic bug:
  1923. */
  1924. static noinline void __schedule_bug(struct task_struct *prev)
  1925. {
  1926. if (oops_in_progress)
  1927. return;
  1928. printk(KERN_ERR "BUG: scheduling while atomic: %s/%d/0x%08x\n",
  1929. prev->comm, prev->pid, preempt_count());
  1930. debug_show_held_locks(prev);
  1931. print_modules();
  1932. if (irqs_disabled())
  1933. print_irqtrace_events(prev);
  1934. dump_stack();
  1935. add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
  1936. }
  1937. /*
  1938. * Various schedule()-time debugging checks and statistics:
  1939. */
  1940. static inline void schedule_debug(struct task_struct *prev)
  1941. {
  1942. /*
  1943. * Test if we are atomic. Since do_exit() needs to call into
  1944. * schedule() atomically, we ignore that path for now.
  1945. * Otherwise, whine if we are scheduling when we should not be.
  1946. */
  1947. if (unlikely(in_atomic_preempt_off() && !prev->exit_state))
  1948. __schedule_bug(prev);
  1949. rcu_sleep_check();
  1950. profile_hit(SCHED_PROFILING, __builtin_return_address(0));
  1951. schedstat_inc(this_rq(), sched_count);
  1952. }
  1953. static void put_prev_task(struct rq *rq, struct task_struct *prev)
  1954. {
  1955. if (prev->on_rq || rq->skip_clock_update < 0)
  1956. update_rq_clock(rq);
  1957. prev->sched_class->put_prev_task(rq, prev);
  1958. }
  1959. /*
  1960. * Pick up the highest-prio task:
  1961. */
  1962. static inline struct task_struct *
  1963. pick_next_task(struct rq *rq)
  1964. {
  1965. const struct sched_class *class;
  1966. struct task_struct *p;
  1967. /*
  1968. * Optimization: we know that if all tasks are in
  1969. * the fair class we can call that function directly:
  1970. */
  1971. if (likely(rq->nr_running == rq->cfs.h_nr_running)) {
  1972. p = fair_sched_class.pick_next_task(rq);
  1973. if (likely(p))
  1974. return p;
  1975. }
  1976. for_each_class(class) {
  1977. p = class->pick_next_task(rq);
  1978. if (p)
  1979. return p;
  1980. }
  1981. BUG(); /* the idle class will always have a runnable task */
  1982. }
  1983. /*
  1984. * __schedule() is the main scheduler function.
  1985. *
  1986. * The main means of driving the scheduler and thus entering this function are:
  1987. *
  1988. * 1. Explicit blocking: mutex, semaphore, waitqueue, etc.
  1989. *
  1990. * 2. TIF_NEED_RESCHED flag is checked on interrupt and userspace return
  1991. * paths. For example, see arch/x86/entry_64.S.
  1992. *
  1993. * To drive preemption between tasks, the scheduler sets the flag in timer
  1994. * interrupt handler scheduler_tick().
  1995. *
  1996. * 3. Wakeups don't really cause entry into schedule(). They add a
  1997. * task to the run-queue and that's it.
  1998. *
  1999. * Now, if the new task added to the run-queue preempts the current
  2000. * task, then the wakeup sets TIF_NEED_RESCHED and schedule() gets
  2001. * called on the nearest possible occasion:
  2002. *
  2003. * - If the kernel is preemptible (CONFIG_PREEMPT=y):
  2004. *
  2005. * - in syscall or exception context, at the next outmost
  2006. * preempt_enable(). (this might be as soon as the wake_up()'s
  2007. * spin_unlock()!)
  2008. *
  2009. * - in IRQ context, return from interrupt-handler to
  2010. * preemptible context
  2011. *
  2012. * - If the kernel is not preemptible (CONFIG_PREEMPT is not set)
  2013. * then at the next:
  2014. *
  2015. * - cond_resched() call
  2016. * - explicit schedule() call
  2017. * - return from syscall or exception to user-space
  2018. * - return from interrupt-handler to user-space
  2019. */
  2020. static void __sched __schedule(void)
  2021. {
  2022. struct task_struct *prev, *next;
  2023. unsigned long *switch_count;
  2024. struct rq *rq;
  2025. int cpu;
  2026. need_resched:
  2027. preempt_disable();
  2028. cpu = smp_processor_id();
  2029. rq = cpu_rq(cpu);
  2030. rcu_note_context_switch(cpu);
  2031. prev = rq->curr;
  2032. schedule_debug(prev);
  2033. if (sched_feat(HRTICK))
  2034. hrtick_clear(rq);
  2035. raw_spin_lock_irq(&rq->lock);
  2036. switch_count = &prev->nivcsw;
  2037. if (prev->state && !(preempt_count() & PREEMPT_ACTIVE)) {
  2038. if (unlikely(signal_pending_state(prev->state, prev))) {
  2039. prev->state = TASK_RUNNING;
  2040. } else {
  2041. deactivate_task(rq, prev, DEQUEUE_SLEEP);
  2042. prev->on_rq = 0;
  2043. /*
  2044. * If a worker went to sleep, notify and ask workqueue
  2045. * whether it wants to wake up a task to maintain
  2046. * concurrency.
  2047. */
  2048. if (prev->flags & PF_WQ_WORKER) {
  2049. struct task_struct *to_wakeup;
  2050. to_wakeup = wq_worker_sleeping(prev, cpu);
  2051. if (to_wakeup)
  2052. try_to_wake_up_local(to_wakeup);
  2053. }
  2054. }
  2055. switch_count = &prev->nvcsw;
  2056. }
  2057. pre_schedule(rq, prev);
  2058. if (unlikely(!rq->nr_running))
  2059. idle_balance(cpu, rq);
  2060. put_prev_task(rq, prev);
  2061. next = pick_next_task(rq);
  2062. clear_tsk_need_resched(prev);
  2063. rq->skip_clock_update = 0;
  2064. if (likely(prev != next)) {
  2065. rq->nr_switches++;
  2066. rq->curr = next;
  2067. ++*switch_count;
  2068. context_switch(rq, prev, next); /* unlocks the rq */
  2069. /*
  2070. * The context switch have flipped the stack from under us
  2071. * and restored the local variables which were saved when
  2072. * this task called schedule() in the past. prev == current
  2073. * is still correct, but it can be moved to another cpu/rq.
  2074. */
  2075. cpu = smp_processor_id();
  2076. rq = cpu_rq(cpu);
  2077. } else
  2078. raw_spin_unlock_irq(&rq->lock);
  2079. post_schedule(rq);
  2080. sched_preempt_enable_no_resched();
  2081. if (need_resched())
  2082. goto need_resched;
  2083. }
  2084. static inline void sched_submit_work(struct task_struct *tsk)
  2085. {
  2086. if (!tsk->state || tsk_is_pi_blocked(tsk))
  2087. return;
  2088. /*
  2089. * If we are going to sleep and we have plugged IO queued,
  2090. * make sure to submit it to avoid deadlocks.
  2091. */
  2092. if (blk_needs_flush_plug(tsk))
  2093. blk_schedule_flush_plug(tsk);
  2094. }
  2095. asmlinkage void __sched schedule(void)
  2096. {
  2097. struct task_struct *tsk = current;
  2098. sched_submit_work(tsk);
  2099. __schedule();
  2100. }
  2101. EXPORT_SYMBOL(schedule);
  2102. #ifdef CONFIG_CONTEXT_TRACKING
  2103. asmlinkage void __sched schedule_user(void)
  2104. {
  2105. /*
  2106. * If we come here after a random call to set_need_resched(),
  2107. * or we have been woken up remotely but the IPI has not yet arrived,
  2108. * we haven't yet exited the RCU idle mode. Do it here manually until
  2109. * we find a better solution.
  2110. */
  2111. user_exit();
  2112. schedule();
  2113. user_enter();
  2114. }
  2115. #endif
  2116. /**
  2117. * schedule_preempt_disabled - called with preemption disabled
  2118. *
  2119. * Returns with preemption disabled. Note: preempt_count must be 1
  2120. */
  2121. void __sched schedule_preempt_disabled(void)
  2122. {
  2123. sched_preempt_enable_no_resched();
  2124. schedule();
  2125. preempt_disable();
  2126. }
  2127. #ifdef CONFIG_PREEMPT
  2128. /*
  2129. * this is the entry point to schedule() from in-kernel preemption
  2130. * off of preempt_enable. Kernel preemptions off return from interrupt
  2131. * occur there and call schedule directly.
  2132. */
  2133. asmlinkage void __sched notrace preempt_schedule(void)
  2134. {
  2135. struct thread_info *ti = current_thread_info();
  2136. /*
  2137. * If there is a non-zero preempt_count or interrupts are disabled,
  2138. * we do not want to preempt the current task. Just return..
  2139. */
  2140. if (likely(ti->preempt_count || irqs_disabled()))
  2141. return;
  2142. do {
  2143. add_preempt_count_notrace(PREEMPT_ACTIVE);
  2144. __schedule();
  2145. sub_preempt_count_notrace(PREEMPT_ACTIVE);
  2146. /*
  2147. * Check again in case we missed a preemption opportunity
  2148. * between schedule and now.
  2149. */
  2150. barrier();
  2151. } while (need_resched());
  2152. }
  2153. EXPORT_SYMBOL(preempt_schedule);
  2154. /*
  2155. * this is the entry point to schedule() from kernel preemption
  2156. * off of irq context.
  2157. * Note, that this is called and return with irqs disabled. This will
  2158. * protect us against recursive calling from irq.
  2159. */
  2160. asmlinkage void __sched preempt_schedule_irq(void)
  2161. {
  2162. struct thread_info *ti = current_thread_info();
  2163. enum ctx_state prev_state;
  2164. /* Catch callers which need to be fixed */
  2165. BUG_ON(ti->preempt_count || !irqs_disabled());
  2166. prev_state = exception_enter();
  2167. do {
  2168. add_preempt_count(PREEMPT_ACTIVE);
  2169. local_irq_enable();
  2170. __schedule();
  2171. local_irq_disable();
  2172. sub_preempt_count(PREEMPT_ACTIVE);
  2173. /*
  2174. * Check again in case we missed a preemption opportunity
  2175. * between schedule and now.
  2176. */
  2177. barrier();
  2178. } while (need_resched());
  2179. exception_exit(prev_state);
  2180. }
  2181. #endif /* CONFIG_PREEMPT */
  2182. int default_wake_function(wait_queue_t *curr, unsigned mode, int wake_flags,
  2183. void *key)
  2184. {
  2185. return try_to_wake_up(curr->private, mode, wake_flags);
  2186. }
  2187. EXPORT_SYMBOL(default_wake_function);
  2188. /*
  2189. * The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just
  2190. * wake everything up. If it's an exclusive wakeup (nr_exclusive == small +ve
  2191. * number) then we wake all the non-exclusive tasks and one exclusive task.
  2192. *
  2193. * There are circumstances in which we can try to wake a task which has already
  2194. * started to run but is not in state TASK_RUNNING. try_to_wake_up() returns
  2195. * zero in this (rare) case, and we handle it by continuing to scan the queue.
  2196. */
  2197. static void __wake_up_common(wait_queue_head_t *q, unsigned int mode,
  2198. int nr_exclusive, int wake_flags, void *key)
  2199. {
  2200. wait_queue_t *curr, *next;
  2201. list_for_each_entry_safe(curr, next, &q->task_list, task_list) {
  2202. unsigned flags = curr->flags;
  2203. if (curr->func(curr, mode, wake_flags, key) &&
  2204. (flags & WQ_FLAG_EXCLUSIVE) && !--nr_exclusive)
  2205. break;
  2206. }
  2207. }
  2208. /**
  2209. * __wake_up - wake up threads blocked on a waitqueue.
  2210. * @q: the waitqueue
  2211. * @mode: which threads
  2212. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  2213. * @key: is directly passed to the wakeup function
  2214. *
  2215. * It may be assumed that this function implies a write memory barrier before
  2216. * changing the task state if and only if any tasks are woken up.
  2217. */
  2218. void __wake_up(wait_queue_head_t *q, unsigned int mode,
  2219. int nr_exclusive, void *key)
  2220. {
  2221. unsigned long flags;
  2222. spin_lock_irqsave(&q->lock, flags);
  2223. __wake_up_common(q, mode, nr_exclusive, 0, key);
  2224. spin_unlock_irqrestore(&q->lock, flags);
  2225. }
  2226. EXPORT_SYMBOL(__wake_up);
  2227. /*
  2228. * Same as __wake_up but called with the spinlock in wait_queue_head_t held.
  2229. */
  2230. void __wake_up_locked(wait_queue_head_t *q, unsigned int mode, int nr)
  2231. {
  2232. __wake_up_common(q, mode, nr, 0, NULL);
  2233. }
  2234. EXPORT_SYMBOL_GPL(__wake_up_locked);
  2235. void __wake_up_locked_key(wait_queue_head_t *q, unsigned int mode, void *key)
  2236. {
  2237. __wake_up_common(q, mode, 1, 0, key);
  2238. }
  2239. EXPORT_SYMBOL_GPL(__wake_up_locked_key);
  2240. /**
  2241. * __wake_up_sync_key - wake up threads blocked on a waitqueue.
  2242. * @q: the waitqueue
  2243. * @mode: which threads
  2244. * @nr_exclusive: how many wake-one or wake-many threads to wake up
  2245. * @key: opaque value to be passed to wakeup targets
  2246. *
  2247. * The sync wakeup differs that the waker knows that it will schedule
  2248. * away soon, so while the target thread will be woken up, it will not
  2249. * be migrated to another CPU - ie. the two threads are 'synchronized'
  2250. * with each other. This can prevent needless bouncing between CPUs.
  2251. *
  2252. * On UP it can prevent extra preemption.
  2253. *
  2254. * It may be assumed that this function implies a write memory barrier before
  2255. * changing the task state if and only if any tasks are woken up.
  2256. */
  2257. void __wake_up_sync_key(wait_queue_head_t *q, unsigned int mode,
  2258. int nr_exclusive, void *key)
  2259. {
  2260. unsigned long flags;
  2261. int wake_flags = WF_SYNC;
  2262. if (unlikely(!q))
  2263. return;
  2264. if (unlikely(!nr_exclusive))
  2265. wake_flags = 0;
  2266. spin_lock_irqsave(&q->lock, flags);
  2267. __wake_up_common(q, mode, nr_exclusive, wake_flags, key);
  2268. spin_unlock_irqrestore(&q->lock, flags);
  2269. }
  2270. EXPORT_SYMBOL_GPL(__wake_up_sync_key);
  2271. /*
  2272. * __wake_up_sync - see __wake_up_sync_key()
  2273. */
  2274. void __wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr_exclusive)
  2275. {
  2276. __wake_up_sync_key(q, mode, nr_exclusive, NULL);
  2277. }
  2278. EXPORT_SYMBOL_GPL(__wake_up_sync); /* For internal use only */
  2279. /**
  2280. * complete: - signals a single thread waiting on this completion
  2281. * @x: holds the state of this particular completion
  2282. *
  2283. * This will wake up a single thread waiting on this completion. Threads will be
  2284. * awakened in the same order in which they were queued.
  2285. *
  2286. * See also complete_all(), wait_for_completion() and related routines.
  2287. *
  2288. * It may be assumed that this function implies a write memory barrier before
  2289. * changing the task state if and only if any tasks are woken up.
  2290. */
  2291. void complete(struct completion *x)
  2292. {
  2293. unsigned long flags;
  2294. spin_lock_irqsave(&x->wait.lock, flags);
  2295. x->done++;
  2296. __wake_up_common(&x->wait, TASK_NORMAL, 1, 0, NULL);
  2297. spin_unlock_irqrestore(&x->wait.lock, flags);
  2298. }
  2299. EXPORT_SYMBOL(complete);
  2300. /**
  2301. * complete_all: - signals all threads waiting on this completion
  2302. * @x: holds the state of this particular completion
  2303. *
  2304. * This will wake up all threads waiting on this particular completion event.
  2305. *
  2306. * It may be assumed that this function implies a write memory barrier before
  2307. * changing the task state if and only if any tasks are woken up.
  2308. */
  2309. void complete_all(struct completion *x)
  2310. {
  2311. unsigned long flags;
  2312. spin_lock_irqsave(&x->wait.lock, flags);
  2313. x->done += UINT_MAX/2;
  2314. __wake_up_common(&x->wait, TASK_NORMAL, 0, 0, NULL);
  2315. spin_unlock_irqrestore(&x->wait.lock, flags);
  2316. }
  2317. EXPORT_SYMBOL(complete_all);
  2318. static inline long __sched
  2319. do_wait_for_common(struct completion *x,
  2320. long (*action)(long), long timeout, int state)
  2321. {
  2322. if (!x->done) {
  2323. DECLARE_WAITQUEUE(wait, current);
  2324. __add_wait_queue_tail_exclusive(&x->wait, &wait);
  2325. do {
  2326. if (signal_pending_state(state, current)) {
  2327. timeout = -ERESTARTSYS;
  2328. break;
  2329. }
  2330. __set_current_state(state);
  2331. spin_unlock_irq(&x->wait.lock);
  2332. timeout = action(timeout);
  2333. spin_lock_irq(&x->wait.lock);
  2334. } while (!x->done && timeout);
  2335. __remove_wait_queue(&x->wait, &wait);
  2336. if (!x->done)
  2337. return timeout;
  2338. }
  2339. x->done--;
  2340. return timeout ?: 1;
  2341. }
  2342. static inline long __sched
  2343. __wait_for_common(struct completion *x,
  2344. long (*action)(long), long timeout, int state)
  2345. {
  2346. might_sleep();
  2347. spin_lock_irq(&x->wait.lock);
  2348. timeout = do_wait_for_common(x, action, timeout, state);
  2349. spin_unlock_irq(&x->wait.lock);
  2350. return timeout;
  2351. }
  2352. static long __sched
  2353. wait_for_common(struct completion *x, long timeout, int state)
  2354. {
  2355. return __wait_for_common(x, schedule_timeout, timeout, state);
  2356. }
  2357. static long __sched
  2358. wait_for_common_io(struct completion *x, long timeout, int state)
  2359. {
  2360. return __wait_for_common(x, io_schedule_timeout, timeout, state);
  2361. }
  2362. /**
  2363. * wait_for_completion: - waits for completion of a task
  2364. * @x: holds the state of this particular completion
  2365. *
  2366. * This waits to be signaled for completion of a specific task. It is NOT
  2367. * interruptible and there is no timeout.
  2368. *
  2369. * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
  2370. * and interrupt capability. Also see complete().
  2371. */
  2372. void __sched wait_for_completion(struct completion *x)
  2373. {
  2374. wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
  2375. }
  2376. EXPORT_SYMBOL(wait_for_completion);
  2377. /**
  2378. * wait_for_completion_timeout: - waits for completion of a task (w/timeout)
  2379. * @x: holds the state of this particular completion
  2380. * @timeout: timeout value in jiffies
  2381. *
  2382. * This waits for either a completion of a specific task to be signaled or for a
  2383. * specified timeout to expire. The timeout is in jiffies. It is not
  2384. * interruptible.
  2385. *
  2386. * The return value is 0 if timed out, and positive (at least 1, or number of
  2387. * jiffies left till timeout) if completed.
  2388. */
  2389. unsigned long __sched
  2390. wait_for_completion_timeout(struct completion *x, unsigned long timeout)
  2391. {
  2392. return wait_for_common(x, timeout, TASK_UNINTERRUPTIBLE);
  2393. }
  2394. EXPORT_SYMBOL(wait_for_completion_timeout);
  2395. /**
  2396. * wait_for_completion_io: - waits for completion of a task
  2397. * @x: holds the state of this particular completion
  2398. *
  2399. * This waits to be signaled for completion of a specific task. It is NOT
  2400. * interruptible and there is no timeout. The caller is accounted as waiting
  2401. * for IO.
  2402. */
  2403. void __sched wait_for_completion_io(struct completion *x)
  2404. {
  2405. wait_for_common_io(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
  2406. }
  2407. EXPORT_SYMBOL(wait_for_completion_io);
  2408. /**
  2409. * wait_for_completion_io_timeout: - waits for completion of a task (w/timeout)
  2410. * @x: holds the state of this particular completion
  2411. * @timeout: timeout value in jiffies
  2412. *
  2413. * This waits for either a completion of a specific task to be signaled or for a
  2414. * specified timeout to expire. The timeout is in jiffies. It is not
  2415. * interruptible. The caller is accounted as waiting for IO.
  2416. *
  2417. * The return value is 0 if timed out, and positive (at least 1, or number of
  2418. * jiffies left till timeout) if completed.
  2419. */
  2420. unsigned long __sched
  2421. wait_for_completion_io_timeout(struct completion *x, unsigned long timeout)
  2422. {
  2423. return wait_for_common_io(x, timeout, TASK_UNINTERRUPTIBLE);
  2424. }
  2425. EXPORT_SYMBOL(wait_for_completion_io_timeout);
  2426. /**
  2427. * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
  2428. * @x: holds the state of this particular completion
  2429. *
  2430. * This waits for completion of a specific task to be signaled. It is
  2431. * interruptible.
  2432. *
  2433. * The return value is -ERESTARTSYS if interrupted, 0 if completed.
  2434. */
  2435. int __sched wait_for_completion_interruptible(struct completion *x)
  2436. {
  2437. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
  2438. if (t == -ERESTARTSYS)
  2439. return t;
  2440. return 0;
  2441. }
  2442. EXPORT_SYMBOL(wait_for_completion_interruptible);
  2443. /**
  2444. * wait_for_completion_interruptible_timeout: - waits for completion (w/(to,intr))
  2445. * @x: holds the state of this particular completion
  2446. * @timeout: timeout value in jiffies
  2447. *
  2448. * This waits for either a completion of a specific task to be signaled or for a
  2449. * specified timeout to expire. It is interruptible. The timeout is in jiffies.
  2450. *
  2451. * The return value is -ERESTARTSYS if interrupted, 0 if timed out,
  2452. * positive (at least 1, or number of jiffies left till timeout) if completed.
  2453. */
  2454. long __sched
  2455. wait_for_completion_interruptible_timeout(struct completion *x,
  2456. unsigned long timeout)
  2457. {
  2458. return wait_for_common(x, timeout, TASK_INTERRUPTIBLE);
  2459. }
  2460. EXPORT_SYMBOL(wait_for_completion_interruptible_timeout);
  2461. /**
  2462. * wait_for_completion_killable: - waits for completion of a task (killable)
  2463. * @x: holds the state of this particular completion
  2464. *
  2465. * This waits to be signaled for completion of a specific task. It can be
  2466. * interrupted by a kill signal.
  2467. *
  2468. * The return value is -ERESTARTSYS if interrupted, 0 if completed.
  2469. */
  2470. int __sched wait_for_completion_killable(struct completion *x)
  2471. {
  2472. long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_KILLABLE);
  2473. if (t == -ERESTARTSYS)
  2474. return t;
  2475. return 0;
  2476. }
  2477. EXPORT_SYMBOL(wait_for_completion_killable);
  2478. /**
  2479. * wait_for_completion_killable_timeout: - waits for completion of a task (w/(to,killable))
  2480. * @x: holds the state of this particular completion
  2481. * @timeout: timeout value in jiffies
  2482. *
  2483. * This waits for either a completion of a specific task to be
  2484. * signaled or for a specified timeout to expire. It can be
  2485. * interrupted by a kill signal. The timeout is in jiffies.
  2486. *
  2487. * The return value is -ERESTARTSYS if interrupted, 0 if timed out,
  2488. * positive (at least 1, or number of jiffies left till timeout) if completed.
  2489. */
  2490. long __sched
  2491. wait_for_completion_killable_timeout(struct completion *x,
  2492. unsigned long timeout)
  2493. {
  2494. return wait_for_common(x, timeout, TASK_KILLABLE);
  2495. }
  2496. EXPORT_SYMBOL(wait_for_completion_killable_timeout);
  2497. /**
  2498. * try_wait_for_completion - try to decrement a completion without blocking
  2499. * @x: completion structure
  2500. *
  2501. * Returns: 0 if a decrement cannot be done without blocking
  2502. * 1 if a decrement succeeded.
  2503. *
  2504. * If a completion is being used as a counting completion,
  2505. * attempt to decrement the counter without blocking. This
  2506. * enables us to avoid waiting if the resource the completion
  2507. * is protecting is not available.
  2508. */
  2509. bool try_wait_for_completion(struct completion *x)
  2510. {
  2511. unsigned long flags;
  2512. int ret = 1;
  2513. spin_lock_irqsave(&x->wait.lock, flags);
  2514. if (!x->done)
  2515. ret = 0;
  2516. else
  2517. x->done--;
  2518. spin_unlock_irqrestore(&x->wait.lock, flags);
  2519. return ret;
  2520. }
  2521. EXPORT_SYMBOL(try_wait_for_completion);
  2522. /**
  2523. * completion_done - Test to see if a completion has any waiters
  2524. * @x: completion structure
  2525. *
  2526. * Returns: 0 if there are waiters (wait_for_completion() in progress)
  2527. * 1 if there are no waiters.
  2528. *
  2529. */
  2530. bool completion_done(struct completion *x)
  2531. {
  2532. unsigned long flags;
  2533. int ret = 1;
  2534. spin_lock_irqsave(&x->wait.lock, flags);
  2535. if (!x->done)
  2536. ret = 0;
  2537. spin_unlock_irqrestore(&x->wait.lock, flags);
  2538. return ret;
  2539. }
  2540. EXPORT_SYMBOL(completion_done);
  2541. static long __sched
  2542. sleep_on_common(wait_queue_head_t *q, int state, long timeout)
  2543. {
  2544. unsigned long flags;
  2545. wait_queue_t wait;
  2546. init_waitqueue_entry(&wait, current);
  2547. __set_current_state(state);
  2548. spin_lock_irqsave(&q->lock, flags);
  2549. __add_wait_queue(q, &wait);
  2550. spin_unlock(&q->lock);
  2551. timeout = schedule_timeout(timeout);
  2552. spin_lock_irq(&q->lock);
  2553. __remove_wait_queue(q, &wait);
  2554. spin_unlock_irqrestore(&q->lock, flags);
  2555. return timeout;
  2556. }
  2557. void __sched interruptible_sleep_on(wait_queue_head_t *q)
  2558. {
  2559. sleep_on_common(q, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  2560. }
  2561. EXPORT_SYMBOL(interruptible_sleep_on);
  2562. long __sched
  2563. interruptible_sleep_on_timeout(wait_queue_head_t *q, long timeout)
  2564. {
  2565. return sleep_on_common(q, TASK_INTERRUPTIBLE, timeout);
  2566. }
  2567. EXPORT_SYMBOL(interruptible_sleep_on_timeout);
  2568. void __sched sleep_on(wait_queue_head_t *q)
  2569. {
  2570. sleep_on_common(q, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
  2571. }
  2572. EXPORT_SYMBOL(sleep_on);
  2573. long __sched sleep_on_timeout(wait_queue_head_t *q, long timeout)
  2574. {
  2575. return sleep_on_common(q, TASK_UNINTERRUPTIBLE, timeout);
  2576. }
  2577. EXPORT_SYMBOL(sleep_on_timeout);
  2578. #ifdef CONFIG_RT_MUTEXES
  2579. /*
  2580. * rt_mutex_setprio - set the current priority of a task
  2581. * @p: task
  2582. * @prio: prio value (kernel-internal form)
  2583. *
  2584. * This function changes the 'effective' priority of a task. It does
  2585. * not touch ->normal_prio like __setscheduler().
  2586. *
  2587. * Used by the rt_mutex code to implement priority inheritance logic.
  2588. */
  2589. void rt_mutex_setprio(struct task_struct *p, int prio)
  2590. {
  2591. int oldprio, on_rq, running;
  2592. struct rq *rq;
  2593. const struct sched_class *prev_class;
  2594. BUG_ON(prio < 0 || prio > MAX_PRIO);
  2595. rq = __task_rq_lock(p);
  2596. /*
  2597. * Idle task boosting is a nono in general. There is one
  2598. * exception, when PREEMPT_RT and NOHZ is active:
  2599. *
  2600. * The idle task calls get_next_timer_interrupt() and holds
  2601. * the timer wheel base->lock on the CPU and another CPU wants
  2602. * to access the timer (probably to cancel it). We can safely
  2603. * ignore the boosting request, as the idle CPU runs this code
  2604. * with interrupts disabled and will complete the lock
  2605. * protected section without being interrupted. So there is no
  2606. * real need to boost.
  2607. */
  2608. if (unlikely(p == rq->idle)) {
  2609. WARN_ON(p != rq->curr);
  2610. WARN_ON(p->pi_blocked_on);
  2611. goto out_unlock;
  2612. }
  2613. trace_sched_pi_setprio(p, prio);
  2614. oldprio = p->prio;
  2615. prev_class = p->sched_class;
  2616. on_rq = p->on_rq;
  2617. running = task_current(rq, p);
  2618. if (on_rq)
  2619. dequeue_task(rq, p, 0);
  2620. if (running)
  2621. p->sched_class->put_prev_task(rq, p);
  2622. if (rt_prio(prio))
  2623. p->sched_class = &rt_sched_class;
  2624. else
  2625. p->sched_class = &fair_sched_class;
  2626. p->prio = prio;
  2627. if (running)
  2628. p->sched_class->set_curr_task(rq);
  2629. if (on_rq)
  2630. enqueue_task(rq, p, oldprio < prio ? ENQUEUE_HEAD : 0);
  2631. check_class_changed(rq, p, prev_class, oldprio);
  2632. out_unlock:
  2633. __task_rq_unlock(rq);
  2634. }
  2635. #endif
  2636. void set_user_nice(struct task_struct *p, long nice)
  2637. {
  2638. int old_prio, delta, on_rq;
  2639. unsigned long flags;
  2640. struct rq *rq;
  2641. if (TASK_NICE(p) == nice || nice < -20 || nice > 19)
  2642. return;
  2643. /*
  2644. * We have to be careful, if called from sys_setpriority(),
  2645. * the task might be in the middle of scheduling on another CPU.
  2646. */
  2647. rq = task_rq_lock(p, &flags);
  2648. /*
  2649. * The RT priorities are set via sched_setscheduler(), but we still
  2650. * allow the 'normal' nice value to be set - but as expected
  2651. * it wont have any effect on scheduling until the task is
  2652. * SCHED_FIFO/SCHED_RR:
  2653. */
  2654. if (task_has_rt_policy(p)) {
  2655. p->static_prio = NICE_TO_PRIO(nice);
  2656. goto out_unlock;
  2657. }
  2658. on_rq = p->on_rq;
  2659. if (on_rq)
  2660. dequeue_task(rq, p, 0);
  2661. p->static_prio = NICE_TO_PRIO(nice);
  2662. set_load_weight(p);
  2663. old_prio = p->prio;
  2664. p->prio = effective_prio(p);
  2665. delta = p->prio - old_prio;
  2666. if (on_rq) {
  2667. enqueue_task(rq, p, 0);
  2668. /*
  2669. * If the task increased its priority or is running and
  2670. * lowered its priority, then reschedule its CPU:
  2671. */
  2672. if (delta < 0 || (delta > 0 && task_running(rq, p)))
  2673. resched_task(rq->curr);
  2674. }
  2675. out_unlock:
  2676. task_rq_unlock(rq, p, &flags);
  2677. }
  2678. EXPORT_SYMBOL(set_user_nice);
  2679. /*
  2680. * can_nice - check if a task can reduce its nice value
  2681. * @p: task
  2682. * @nice: nice value
  2683. */
  2684. int can_nice(const struct task_struct *p, const int nice)
  2685. {
  2686. /* convert nice value [19,-20] to rlimit style value [1,40] */
  2687. int nice_rlim = 20 - nice;
  2688. return (nice_rlim <= task_rlimit(p, RLIMIT_NICE) ||
  2689. capable(CAP_SYS_NICE));
  2690. }
  2691. #ifdef __ARCH_WANT_SYS_NICE
  2692. /*
  2693. * sys_nice - change the priority of the current process.
  2694. * @increment: priority increment
  2695. *
  2696. * sys_setpriority is a more generic, but much slower function that
  2697. * does similar things.
  2698. */
  2699. SYSCALL_DEFINE1(nice, int, increment)
  2700. {
  2701. long nice, retval;
  2702. /*
  2703. * Setpriority might change our priority at the same moment.
  2704. * We don't have to worry. Conceptually one call occurs first
  2705. * and we have a single winner.
  2706. */
  2707. if (increment < -40)
  2708. increment = -40;
  2709. if (increment > 40)
  2710. increment = 40;
  2711. nice = TASK_NICE(current) + increment;
  2712. if (nice < -20)
  2713. nice = -20;
  2714. if (nice > 19)
  2715. nice = 19;
  2716. if (increment < 0 && !can_nice(current, nice))
  2717. return -EPERM;
  2718. retval = security_task_setnice(current, nice);
  2719. if (retval)
  2720. return retval;
  2721. set_user_nice(current, nice);
  2722. return 0;
  2723. }
  2724. #endif
  2725. /**
  2726. * task_prio - return the priority value of a given task.
  2727. * @p: the task in question.
  2728. *
  2729. * This is the priority value as seen by users in /proc.
  2730. * RT tasks are offset by -200. Normal tasks are centered
  2731. * around 0, value goes from -16 to +15.
  2732. */
  2733. int task_prio(const struct task_struct *p)
  2734. {
  2735. return p->prio - MAX_RT_PRIO;
  2736. }
  2737. /**
  2738. * task_nice - return the nice value of a given task.
  2739. * @p: the task in question.
  2740. */
  2741. int task_nice(const struct task_struct *p)
  2742. {
  2743. return TASK_NICE(p);
  2744. }
  2745. EXPORT_SYMBOL(task_nice);
  2746. /**
  2747. * idle_cpu - is a given cpu idle currently?
  2748. * @cpu: the processor in question.
  2749. */
  2750. int idle_cpu(int cpu)
  2751. {
  2752. struct rq *rq = cpu_rq(cpu);
  2753. if (rq->curr != rq->idle)
  2754. return 0;
  2755. if (rq->nr_running)
  2756. return 0;
  2757. #ifdef CONFIG_SMP
  2758. if (!llist_empty(&rq->wake_list))
  2759. return 0;
  2760. #endif
  2761. return 1;
  2762. }
  2763. /**
  2764. * idle_task - return the idle task for a given cpu.
  2765. * @cpu: the processor in question.
  2766. */
  2767. struct task_struct *idle_task(int cpu)
  2768. {
  2769. return cpu_rq(cpu)->idle;
  2770. }
  2771. /**
  2772. * find_process_by_pid - find a process with a matching PID value.
  2773. * @pid: the pid in question.
  2774. */
  2775. static struct task_struct *find_process_by_pid(pid_t pid)
  2776. {
  2777. return pid ? find_task_by_vpid(pid) : current;
  2778. }
  2779. /* Actually do priority change: must hold rq lock. */
  2780. static void
  2781. __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
  2782. {
  2783. p->policy = policy;
  2784. p->rt_priority = prio;
  2785. p->normal_prio = normal_prio(p);
  2786. /* we are holding p->pi_lock already */
  2787. p->prio = rt_mutex_getprio(p);
  2788. if (rt_prio(p->prio))
  2789. p->sched_class = &rt_sched_class;
  2790. else
  2791. p->sched_class = &fair_sched_class;
  2792. set_load_weight(p);
  2793. }
  2794. /*
  2795. * check the target process has a UID that matches the current process's
  2796. */
  2797. static bool check_same_owner(struct task_struct *p)
  2798. {
  2799. const struct cred *cred = current_cred(), *pcred;
  2800. bool match;
  2801. rcu_read_lock();
  2802. pcred = __task_cred(p);
  2803. match = (uid_eq(cred->euid, pcred->euid) ||
  2804. uid_eq(cred->euid, pcred->uid));
  2805. rcu_read_unlock();
  2806. return match;
  2807. }
  2808. static int __sched_setscheduler(struct task_struct *p, int policy,
  2809. const struct sched_param *param, bool user)
  2810. {
  2811. int retval, oldprio, oldpolicy = -1, on_rq, running;
  2812. unsigned long flags;
  2813. const struct sched_class *prev_class;
  2814. struct rq *rq;
  2815. int reset_on_fork;
  2816. /* may grab non-irq protected spin_locks */
  2817. BUG_ON(in_interrupt());
  2818. recheck:
  2819. /* double check policy once rq lock held */
  2820. if (policy < 0) {
  2821. reset_on_fork = p->sched_reset_on_fork;
  2822. policy = oldpolicy = p->policy;
  2823. } else {
  2824. reset_on_fork = !!(policy & SCHED_RESET_ON_FORK);
  2825. policy &= ~SCHED_RESET_ON_FORK;
  2826. if (policy != SCHED_FIFO && policy != SCHED_RR &&
  2827. policy != SCHED_NORMAL && policy != SCHED_BATCH &&
  2828. policy != SCHED_IDLE)
  2829. return -EINVAL;
  2830. }
  2831. /*
  2832. * Valid priorities for SCHED_FIFO and SCHED_RR are
  2833. * 1..MAX_USER_RT_PRIO-1, valid priority for SCHED_NORMAL,
  2834. * SCHED_BATCH and SCHED_IDLE is 0.
  2835. */
  2836. if (param->sched_priority < 0 ||
  2837. (p->mm && param->sched_priority > MAX_USER_RT_PRIO-1) ||
  2838. (!p->mm && param->sched_priority > MAX_RT_PRIO-1))
  2839. return -EINVAL;
  2840. if (rt_policy(policy) != (param->sched_priority != 0))
  2841. return -EINVAL;
  2842. /*
  2843. * Allow unprivileged RT tasks to decrease priority:
  2844. */
  2845. if (user && !capable(CAP_SYS_NICE)) {
  2846. if (rt_policy(policy)) {
  2847. unsigned long rlim_rtprio =
  2848. task_rlimit(p, RLIMIT_RTPRIO);
  2849. /* can't set/change the rt policy */
  2850. if (policy != p->policy && !rlim_rtprio)
  2851. return -EPERM;
  2852. /* can't increase priority */
  2853. if (param->sched_priority > p->rt_priority &&
  2854. param->sched_priority > rlim_rtprio)
  2855. return -EPERM;
  2856. }
  2857. /*
  2858. * Treat SCHED_IDLE as nice 20. Only allow a switch to
  2859. * SCHED_NORMAL if the RLIMIT_NICE would normally permit it.
  2860. */
  2861. if (p->policy == SCHED_IDLE && policy != SCHED_IDLE) {
  2862. if (!can_nice(p, TASK_NICE(p)))
  2863. return -EPERM;
  2864. }
  2865. /* can't change other user's priorities */
  2866. if (!check_same_owner(p))
  2867. return -EPERM;
  2868. /* Normal users shall not reset the sched_reset_on_fork flag */
  2869. if (p->sched_reset_on_fork && !reset_on_fork)
  2870. return -EPERM;
  2871. }
  2872. if (user) {
  2873. retval = security_task_setscheduler(p);
  2874. if (retval)
  2875. return retval;
  2876. }
  2877. /*
  2878. * make sure no PI-waiters arrive (or leave) while we are
  2879. * changing the priority of the task:
  2880. *
  2881. * To be able to change p->policy safely, the appropriate
  2882. * runqueue lock must be held.
  2883. */
  2884. rq = task_rq_lock(p, &flags);
  2885. /*
  2886. * Changing the policy of the stop threads its a very bad idea
  2887. */
  2888. if (p == rq->stop) {
  2889. task_rq_unlock(rq, p, &flags);
  2890. return -EINVAL;
  2891. }
  2892. /*
  2893. * If not changing anything there's no need to proceed further:
  2894. */
  2895. if (unlikely(policy == p->policy && (!rt_policy(policy) ||
  2896. param->sched_priority == p->rt_priority))) {
  2897. task_rq_unlock(rq, p, &flags);
  2898. return 0;
  2899. }
  2900. #ifdef CONFIG_RT_GROUP_SCHED
  2901. if (user) {
  2902. /*
  2903. * Do not allow realtime tasks into groups that have no runtime
  2904. * assigned.
  2905. */
  2906. if (rt_bandwidth_enabled() && rt_policy(policy) &&
  2907. task_group(p)->rt_bandwidth.rt_runtime == 0 &&
  2908. !task_group_is_autogroup(task_group(p))) {
  2909. task_rq_unlock(rq, p, &flags);
  2910. return -EPERM;
  2911. }
  2912. }
  2913. #endif
  2914. /* recheck policy now with rq lock held */
  2915. if (unlikely(oldpolicy != -1 && oldpolicy != p->policy)) {
  2916. policy = oldpolicy = -1;
  2917. task_rq_unlock(rq, p, &flags);
  2918. goto recheck;
  2919. }
  2920. on_rq = p->on_rq;
  2921. running = task_current(rq, p);
  2922. if (on_rq)
  2923. dequeue_task(rq, p, 0);
  2924. if (running)
  2925. p->sched_class->put_prev_task(rq, p);
  2926. p->sched_reset_on_fork = reset_on_fork;
  2927. oldprio = p->prio;
  2928. prev_class = p->sched_class;
  2929. __setscheduler(rq, p, policy, param->sched_priority);
  2930. if (running)
  2931. p->sched_class->set_curr_task(rq);
  2932. if (on_rq)
  2933. enqueue_task(rq, p, 0);
  2934. check_class_changed(rq, p, prev_class, oldprio);
  2935. task_rq_unlock(rq, p, &flags);
  2936. rt_mutex_adjust_pi(p);
  2937. return 0;
  2938. }
  2939. /**
  2940. * sched_setscheduler - change the scheduling policy and/or RT priority of a thread.
  2941. * @p: the task in question.
  2942. * @policy: new policy.
  2943. * @param: structure containing the new RT priority.
  2944. *
  2945. * NOTE that the task may be already dead.
  2946. */
  2947. int sched_setscheduler(struct task_struct *p, int policy,
  2948. const struct sched_param *param)
  2949. {
  2950. return __sched_setscheduler(p, policy, param, true);
  2951. }
  2952. EXPORT_SYMBOL_GPL(sched_setscheduler);
  2953. /**
  2954. * sched_setscheduler_nocheck - change the scheduling policy and/or RT priority of a thread from kernelspace.
  2955. * @p: the task in question.
  2956. * @policy: new policy.
  2957. * @param: structure containing the new RT priority.
  2958. *
  2959. * Just like sched_setscheduler, only don't bother checking if the
  2960. * current context has permission. For example, this is needed in
  2961. * stop_machine(): we create temporary high priority worker threads,
  2962. * but our caller might not have that capability.
  2963. */
  2964. int sched_setscheduler_nocheck(struct task_struct *p, int policy,
  2965. const struct sched_param *param)
  2966. {
  2967. return __sched_setscheduler(p, policy, param, false);
  2968. }
  2969. static int
  2970. do_sched_setscheduler(pid_t pid, int policy, struct sched_param __user *param)
  2971. {
  2972. struct sched_param lparam;
  2973. struct task_struct *p;
  2974. int retval;
  2975. if (!param || pid < 0)
  2976. return -EINVAL;
  2977. if (copy_from_user(&lparam, param, sizeof(struct sched_param)))
  2978. return -EFAULT;
  2979. rcu_read_lock();
  2980. retval = -ESRCH;
  2981. p = find_process_by_pid(pid);
  2982. if (p != NULL)
  2983. retval = sched_setscheduler(p, policy, &lparam);
  2984. rcu_read_unlock();
  2985. return retval;
  2986. }
  2987. /**
  2988. * sys_sched_setscheduler - set/change the scheduler policy and RT priority
  2989. * @pid: the pid in question.
  2990. * @policy: new policy.
  2991. * @param: structure containing the new RT priority.
  2992. */
  2993. SYSCALL_DEFINE3(sched_setscheduler, pid_t, pid, int, policy,
  2994. struct sched_param __user *, param)
  2995. {
  2996. /* negative values for policy are not valid */
  2997. if (policy < 0)
  2998. return -EINVAL;
  2999. return do_sched_setscheduler(pid, policy, param);
  3000. }
  3001. /**
  3002. * sys_sched_setparam - set/change the RT priority of a thread
  3003. * @pid: the pid in question.
  3004. * @param: structure containing the new RT priority.
  3005. */
  3006. SYSCALL_DEFINE2(sched_setparam, pid_t, pid, struct sched_param __user *, param)
  3007. {
  3008. return do_sched_setscheduler(pid, -1, param);
  3009. }
  3010. /**
  3011. * sys_sched_getscheduler - get the policy (scheduling class) of a thread
  3012. * @pid: the pid in question.
  3013. */
  3014. SYSCALL_DEFINE1(sched_getscheduler, pid_t, pid)
  3015. {
  3016. struct task_struct *p;
  3017. int retval;
  3018. if (pid < 0)
  3019. return -EINVAL;
  3020. retval = -ESRCH;
  3021. rcu_read_lock();
  3022. p = find_process_by_pid(pid);
  3023. if (p) {
  3024. retval = security_task_getscheduler(p);
  3025. if (!retval)
  3026. retval = p->policy
  3027. | (p->sched_reset_on_fork ? SCHED_RESET_ON_FORK : 0);
  3028. }
  3029. rcu_read_unlock();
  3030. return retval;
  3031. }
  3032. /**
  3033. * sys_sched_getparam - get the RT priority of a thread
  3034. * @pid: the pid in question.
  3035. * @param: structure containing the RT priority.
  3036. */
  3037. SYSCALL_DEFINE2(sched_getparam, pid_t, pid, struct sched_param __user *, param)
  3038. {
  3039. struct sched_param lp;
  3040. struct task_struct *p;
  3041. int retval;
  3042. if (!param || pid < 0)
  3043. return -EINVAL;
  3044. rcu_read_lock();
  3045. p = find_process_by_pid(pid);
  3046. retval = -ESRCH;
  3047. if (!p)
  3048. goto out_unlock;
  3049. retval = security_task_getscheduler(p);
  3050. if (retval)
  3051. goto out_unlock;
  3052. lp.sched_priority = p->rt_priority;
  3053. rcu_read_unlock();
  3054. /*
  3055. * This one might sleep, we cannot do it with a spinlock held ...
  3056. */
  3057. retval = copy_to_user(param, &lp, sizeof(*param)) ? -EFAULT : 0;
  3058. return retval;
  3059. out_unlock:
  3060. rcu_read_unlock();
  3061. return retval;
  3062. }
  3063. long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
  3064. {
  3065. cpumask_var_t cpus_allowed, new_mask;
  3066. struct task_struct *p;
  3067. int retval;
  3068. get_online_cpus();
  3069. rcu_read_lock();
  3070. p = find_process_by_pid(pid);
  3071. if (!p) {
  3072. rcu_read_unlock();
  3073. put_online_cpus();
  3074. return -ESRCH;
  3075. }
  3076. /* Prevent p going away */
  3077. get_task_struct(p);
  3078. rcu_read_unlock();
  3079. if (p->flags & PF_NO_SETAFFINITY) {
  3080. retval = -EINVAL;
  3081. goto out_put_task;
  3082. }
  3083. if (!alloc_cpumask_var(&cpus_allowed, GFP_KERNEL)) {
  3084. retval = -ENOMEM;
  3085. goto out_put_task;
  3086. }
  3087. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL)) {
  3088. retval = -ENOMEM;
  3089. goto out_free_cpus_allowed;
  3090. }
  3091. retval = -EPERM;
  3092. if (!check_same_owner(p)) {
  3093. rcu_read_lock();
  3094. if (!ns_capable(__task_cred(p)->user_ns, CAP_SYS_NICE)) {
  3095. rcu_read_unlock();
  3096. goto out_unlock;
  3097. }
  3098. rcu_read_unlock();
  3099. }
  3100. retval = security_task_setscheduler(p);
  3101. if (retval)
  3102. goto out_unlock;
  3103. cpuset_cpus_allowed(p, cpus_allowed);
  3104. cpumask_and(new_mask, in_mask, cpus_allowed);
  3105. again:
  3106. retval = set_cpus_allowed_ptr(p, new_mask);
  3107. if (!retval) {
  3108. cpuset_cpus_allowed(p, cpus_allowed);
  3109. if (!cpumask_subset(new_mask, cpus_allowed)) {
  3110. /*
  3111. * We must have raced with a concurrent cpuset
  3112. * update. Just reset the cpus_allowed to the
  3113. * cpuset's cpus_allowed
  3114. */
  3115. cpumask_copy(new_mask, cpus_allowed);
  3116. goto again;
  3117. }
  3118. }
  3119. out_unlock:
  3120. free_cpumask_var(new_mask);
  3121. out_free_cpus_allowed:
  3122. free_cpumask_var(cpus_allowed);
  3123. out_put_task:
  3124. put_task_struct(p);
  3125. put_online_cpus();
  3126. return retval;
  3127. }
  3128. static int get_user_cpu_mask(unsigned long __user *user_mask_ptr, unsigned len,
  3129. struct cpumask *new_mask)
  3130. {
  3131. if (len < cpumask_size())
  3132. cpumask_clear(new_mask);
  3133. else if (len > cpumask_size())
  3134. len = cpumask_size();
  3135. return copy_from_user(new_mask, user_mask_ptr, len) ? -EFAULT : 0;
  3136. }
  3137. /**
  3138. * sys_sched_setaffinity - set the cpu affinity of a process
  3139. * @pid: pid of the process
  3140. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3141. * @user_mask_ptr: user-space pointer to the new cpu mask
  3142. */
  3143. SYSCALL_DEFINE3(sched_setaffinity, pid_t, pid, unsigned int, len,
  3144. unsigned long __user *, user_mask_ptr)
  3145. {
  3146. cpumask_var_t new_mask;
  3147. int retval;
  3148. if (!alloc_cpumask_var(&new_mask, GFP_KERNEL))
  3149. return -ENOMEM;
  3150. retval = get_user_cpu_mask(user_mask_ptr, len, new_mask);
  3151. if (retval == 0)
  3152. retval = sched_setaffinity(pid, new_mask);
  3153. free_cpumask_var(new_mask);
  3154. return retval;
  3155. }
  3156. long sched_getaffinity(pid_t pid, struct cpumask *mask)
  3157. {
  3158. struct task_struct *p;
  3159. unsigned long flags;
  3160. int retval;
  3161. get_online_cpus();
  3162. rcu_read_lock();
  3163. retval = -ESRCH;
  3164. p = find_process_by_pid(pid);
  3165. if (!p)
  3166. goto out_unlock;
  3167. retval = security_task_getscheduler(p);
  3168. if (retval)
  3169. goto out_unlock;
  3170. raw_spin_lock_irqsave(&p->pi_lock, flags);
  3171. cpumask_and(mask, &p->cpus_allowed, cpu_online_mask);
  3172. raw_spin_unlock_irqrestore(&p->pi_lock, flags);
  3173. out_unlock:
  3174. rcu_read_unlock();
  3175. put_online_cpus();
  3176. return retval;
  3177. }
  3178. /**
  3179. * sys_sched_getaffinity - get the cpu affinity of a process
  3180. * @pid: pid of the process
  3181. * @len: length in bytes of the bitmask pointed to by user_mask_ptr
  3182. * @user_mask_ptr: user-space pointer to hold the current cpu mask
  3183. */
  3184. SYSCALL_DEFINE3(sched_getaffinity, pid_t, pid, unsigned int, len,
  3185. unsigned long __user *, user_mask_ptr)
  3186. {
  3187. int ret;
  3188. cpumask_var_t mask;
  3189. if ((len * BITS_PER_BYTE) < nr_cpu_ids)
  3190. return -EINVAL;
  3191. if (len & (sizeof(unsigned long)-1))
  3192. return -EINVAL;
  3193. if (!alloc_cpumask_var(&mask, GFP_KERNEL))
  3194. return -ENOMEM;
  3195. ret = sched_getaffinity(pid, mask);
  3196. if (ret == 0) {
  3197. size_t retlen = min_t(size_t, len, cpumask_size());
  3198. if (copy_to_user(user_mask_ptr, mask, retlen))
  3199. ret = -EFAULT;
  3200. else
  3201. ret = retlen;
  3202. }
  3203. free_cpumask_var(mask);
  3204. return ret;
  3205. }
  3206. /**
  3207. * sys_sched_yield - yield the current processor to other threads.
  3208. *
  3209. * This function yields the current CPU to other tasks. If there are no
  3210. * other threads running on this CPU then this function will return.
  3211. */
  3212. SYSCALL_DEFINE0(sched_yield)
  3213. {
  3214. struct rq *rq = this_rq_lock();
  3215. schedstat_inc(rq, yld_count);
  3216. current->sched_class->yield_task(rq);
  3217. /*
  3218. * Since we are going to call schedule() anyway, there's
  3219. * no need to preempt or enable interrupts:
  3220. */
  3221. __release(rq->lock);
  3222. spin_release(&rq->lock.dep_map, 1, _THIS_IP_);
  3223. do_raw_spin_unlock(&rq->lock);
  3224. sched_preempt_enable_no_resched();
  3225. schedule();
  3226. return 0;
  3227. }
  3228. static inline int should_resched(void)
  3229. {
  3230. return need_resched() && !(preempt_count() & PREEMPT_ACTIVE);
  3231. }
  3232. static void __cond_resched(void)
  3233. {
  3234. add_preempt_count(PREEMPT_ACTIVE);
  3235. __schedule();
  3236. sub_preempt_count(PREEMPT_ACTIVE);
  3237. }
  3238. int __sched _cond_resched(void)
  3239. {
  3240. if (should_resched()) {
  3241. __cond_resched();
  3242. return 1;
  3243. }
  3244. return 0;
  3245. }
  3246. EXPORT_SYMBOL(_cond_resched);
  3247. /*
  3248. * __cond_resched_lock() - if a reschedule is pending, drop the given lock,
  3249. * call schedule, and on return reacquire the lock.
  3250. *
  3251. * This works OK both with and without CONFIG_PREEMPT. We do strange low-level
  3252. * operations here to prevent schedule() from being called twice (once via
  3253. * spin_unlock(), once by hand).
  3254. */
  3255. int __cond_resched_lock(spinlock_t *lock)
  3256. {
  3257. int resched = should_resched();
  3258. int ret = 0;
  3259. lockdep_assert_held(lock);
  3260. if (spin_needbreak(lock) || resched) {
  3261. spin_unlock(lock);
  3262. if (resched)
  3263. __cond_resched();
  3264. else
  3265. cpu_relax();
  3266. ret = 1;
  3267. spin_lock(lock);
  3268. }
  3269. return ret;
  3270. }
  3271. EXPORT_SYMBOL(__cond_resched_lock);
  3272. int __sched __cond_resched_softirq(void)
  3273. {
  3274. BUG_ON(!in_softirq());
  3275. if (should_resched()) {
  3276. local_bh_enable();
  3277. __cond_resched();
  3278. local_bh_disable();
  3279. return 1;
  3280. }
  3281. return 0;
  3282. }
  3283. EXPORT_SYMBOL(__cond_resched_softirq);
  3284. /**
  3285. * yield - yield the current processor to other threads.
  3286. *
  3287. * Do not ever use this function, there's a 99% chance you're doing it wrong.
  3288. *
  3289. * The scheduler is at all times free to pick the calling task as the most
  3290. * eligible task to run, if removing the yield() call from your code breaks
  3291. * it, its already broken.
  3292. *
  3293. * Typical broken usage is:
  3294. *
  3295. * while (!event)
  3296. * yield();
  3297. *
  3298. * where one assumes that yield() will let 'the other' process run that will
  3299. * make event true. If the current task is a SCHED_FIFO task that will never
  3300. * happen. Never use yield() as a progress guarantee!!
  3301. *
  3302. * If you want to use yield() to wait for something, use wait_event().
  3303. * If you want to use yield() to be 'nice' for others, use cond_resched().
  3304. * If you still want to use yield(), do not!
  3305. */
  3306. void __sched yield(void)
  3307. {
  3308. set_current_state(TASK_RUNNING);
  3309. sys_sched_yield();
  3310. }
  3311. EXPORT_SYMBOL(yield);
  3312. /**
  3313. * yield_to - yield the current processor to another thread in
  3314. * your thread group, or accelerate that thread toward the
  3315. * processor it's on.
  3316. * @p: target task
  3317. * @preempt: whether task preemption is allowed or not
  3318. *
  3319. * It's the caller's job to ensure that the target task struct
  3320. * can't go away on us before we can do any checks.
  3321. *
  3322. * Returns:
  3323. * true (>0) if we indeed boosted the target task.
  3324. * false (0) if we failed to boost the target.
  3325. * -ESRCH if there's no task to yield to.
  3326. */
  3327. bool __sched yield_to(struct task_struct *p, bool preempt)
  3328. {
  3329. struct task_struct *curr = current;
  3330. struct rq *rq, *p_rq;
  3331. unsigned long flags;
  3332. int yielded = 0;
  3333. local_irq_save(flags);
  3334. rq = this_rq();
  3335. again:
  3336. p_rq = task_rq(p);
  3337. /*
  3338. * If we're the only runnable task on the rq and target rq also
  3339. * has only one task, there's absolutely no point in yielding.
  3340. */
  3341. if (rq->nr_running == 1 && p_rq->nr_running == 1) {
  3342. yielded = -ESRCH;
  3343. goto out_irq;
  3344. }
  3345. double_rq_lock(rq, p_rq);
  3346. while (task_rq(p) != p_rq) {
  3347. double_rq_unlock(rq, p_rq);
  3348. goto again;
  3349. }
  3350. if (!curr->sched_class->yield_to_task)
  3351. goto out_unlock;
  3352. if (curr->sched_class != p->sched_class)
  3353. goto out_unlock;
  3354. if (task_running(p_rq, p) || p->state)
  3355. goto out_unlock;
  3356. yielded = curr->sched_class->yield_to_task(rq, p, preempt);
  3357. if (yielded) {
  3358. schedstat_inc(rq, yld_count);
  3359. /*
  3360. * Make p's CPU reschedule; pick_next_entity takes care of
  3361. * fairness.
  3362. */
  3363. if (preempt && rq != p_rq)
  3364. resched_task(p_rq->curr);
  3365. }
  3366. out_unlock:
  3367. double_rq_unlock(rq, p_rq);
  3368. out_irq:
  3369. local_irq_restore(flags);
  3370. if (yielded > 0)
  3371. schedule();
  3372. return yielded;
  3373. }
  3374. EXPORT_SYMBOL_GPL(yield_to);
  3375. /*
  3376. * This task is about to go to sleep on IO. Increment rq->nr_iowait so
  3377. * that process accounting knows that this is a task in IO wait state.
  3378. */
  3379. void __sched io_schedule(void)
  3380. {
  3381. struct rq *rq = raw_rq();
  3382. delayacct_blkio_start();
  3383. atomic_inc(&rq->nr_iowait);
  3384. blk_flush_plug(current);
  3385. current->in_iowait = 1;
  3386. schedule();
  3387. current->in_iowait = 0;
  3388. atomic_dec(&rq->nr_iowait);
  3389. delayacct_blkio_end();
  3390. }
  3391. EXPORT_SYMBOL(io_schedule);
  3392. long __sched io_schedule_timeout(long timeout)
  3393. {
  3394. struct rq *rq = raw_rq();
  3395. long ret;
  3396. delayacct_blkio_start();
  3397. atomic_inc(&rq->nr_iowait);
  3398. blk_flush_plug(current);
  3399. current->in_iowait = 1;
  3400. ret = schedule_timeout(timeout);
  3401. current->in_iowait = 0;
  3402. atomic_dec(&rq->nr_iowait);
  3403. delayacct_blkio_end();
  3404. return ret;
  3405. }
  3406. /**
  3407. * sys_sched_get_priority_max - return maximum RT priority.
  3408. * @policy: scheduling class.
  3409. *
  3410. * this syscall returns the maximum rt_priority that can be used
  3411. * by a given scheduling class.
  3412. */
  3413. SYSCALL_DEFINE1(sched_get_priority_max, int, policy)
  3414. {
  3415. int ret = -EINVAL;
  3416. switch (policy) {
  3417. case SCHED_FIFO:
  3418. case SCHED_RR:
  3419. ret = MAX_USER_RT_PRIO-1;
  3420. break;
  3421. case SCHED_NORMAL:
  3422. case SCHED_BATCH:
  3423. case SCHED_IDLE:
  3424. ret = 0;
  3425. break;
  3426. }
  3427. return ret;
  3428. }
  3429. /**
  3430. * sys_sched_get_priority_min - return minimum RT priority.
  3431. * @policy: scheduling class.
  3432. *
  3433. * this syscall returns the minimum rt_priority that can be used
  3434. * by a given scheduling class.
  3435. */
  3436. SYSCALL_DEFINE1(sched_get_priority_min, int, policy)
  3437. {
  3438. int ret = -EINVAL;
  3439. switch (policy) {
  3440. case SCHED_FIFO:
  3441. case SCHED_RR:
  3442. ret = 1;
  3443. break;
  3444. case SCHED_NORMAL:
  3445. case SCHED_BATCH:
  3446. case SCHED_IDLE:
  3447. ret = 0;
  3448. }
  3449. return ret;
  3450. }
  3451. /**
  3452. * sys_sched_rr_get_interval - return the default timeslice of a process.
  3453. * @pid: pid of the process.
  3454. * @interval: userspace pointer to the timeslice value.
  3455. *
  3456. * this syscall writes the default timeslice value of a given process
  3457. * into the user-space timespec buffer. A value of '0' means infinity.
  3458. */
  3459. SYSCALL_DEFINE2(sched_rr_get_interval, pid_t, pid,
  3460. struct timespec __user *, interval)
  3461. {
  3462. struct task_struct *p;
  3463. unsigned int time_slice;
  3464. unsigned long flags;
  3465. struct rq *rq;
  3466. int retval;
  3467. struct timespec t;
  3468. if (pid < 0)
  3469. return -EINVAL;
  3470. retval = -ESRCH;
  3471. rcu_read_lock();
  3472. p = find_process_by_pid(pid);
  3473. if (!p)
  3474. goto out_unlock;
  3475. retval = security_task_getscheduler(p);
  3476. if (retval)
  3477. goto out_unlock;
  3478. rq = task_rq_lock(p, &flags);
  3479. time_slice = p->sched_class->get_rr_interval(rq, p);
  3480. task_rq_unlock(rq, p, &flags);
  3481. rcu_read_unlock();
  3482. jiffies_to_timespec(time_slice, &t);
  3483. retval = copy_to_user(interval, &t, sizeof(t)) ? -EFAULT : 0;
  3484. return retval;
  3485. out_unlock:
  3486. rcu_read_unlock();
  3487. return retval;
  3488. }
  3489. static const char stat_nam[] = TASK_STATE_TO_CHAR_STR;
  3490. void sched_show_task(struct task_struct *p)
  3491. {
  3492. unsigned long free = 0;
  3493. int ppid;
  3494. unsigned state;
  3495. state = p->state ? __ffs(p->state) + 1 : 0;
  3496. printk(KERN_INFO "%-15.15s %c", p->comm,
  3497. state < sizeof(stat_nam) - 1 ? stat_nam[state] : '?');
  3498. #if BITS_PER_LONG == 32
  3499. if (state == TASK_RUNNING)
  3500. printk(KERN_CONT " running ");
  3501. else
  3502. printk(KERN_CONT " %08lx ", thread_saved_pc(p));
  3503. #else
  3504. if (state == TASK_RUNNING)
  3505. printk(KERN_CONT " running task ");
  3506. else
  3507. printk(KERN_CONT " %016lx ", thread_saved_pc(p));
  3508. #endif
  3509. #ifdef CONFIG_DEBUG_STACK_USAGE
  3510. free = stack_not_used(p);
  3511. #endif
  3512. rcu_read_lock();
  3513. ppid = task_pid_nr(rcu_dereference(p->real_parent));
  3514. rcu_read_unlock();
  3515. printk(KERN_CONT "%5lu %5d %6d 0x%08lx\n", free,
  3516. task_pid_nr(p), ppid,
  3517. (unsigned long)task_thread_info(p)->flags);
  3518. print_worker_info(KERN_INFO, p);
  3519. show_stack(p, NULL);
  3520. }
  3521. void show_state_filter(unsigned long state_filter)
  3522. {
  3523. struct task_struct *g, *p;
  3524. #if BITS_PER_LONG == 32
  3525. printk(KERN_INFO
  3526. " task PC stack pid father\n");
  3527. #else
  3528. printk(KERN_INFO
  3529. " task PC stack pid father\n");
  3530. #endif
  3531. rcu_read_lock();
  3532. do_each_thread(g, p) {
  3533. /*
  3534. * reset the NMI-timeout, listing all files on a slow
  3535. * console might take a lot of time:
  3536. */
  3537. touch_nmi_watchdog();
  3538. if (!state_filter || (p->state & state_filter))
  3539. sched_show_task(p);
  3540. } while_each_thread(g, p);
  3541. touch_all_softlockup_watchdogs();
  3542. #ifdef CONFIG_SCHED_DEBUG
  3543. sysrq_sched_debug_show();
  3544. #endif
  3545. rcu_read_unlock();
  3546. /*
  3547. * Only show locks if all tasks are dumped:
  3548. */
  3549. if (!state_filter)
  3550. debug_show_all_locks();
  3551. }
  3552. void __cpuinit init_idle_bootup_task(struct task_struct *idle)
  3553. {
  3554. idle->sched_class = &idle_sched_class;
  3555. }
  3556. /**
  3557. * init_idle - set up an idle thread for a given CPU
  3558. * @idle: task in question
  3559. * @cpu: cpu the idle task belongs to
  3560. *
  3561. * NOTE: this function does not set the idle thread's NEED_RESCHED
  3562. * flag, to make booting more robust.
  3563. */
  3564. void __cpuinit init_idle(struct task_struct *idle, int cpu)
  3565. {
  3566. struct rq *rq = cpu_rq(cpu);
  3567. unsigned long flags;
  3568. raw_spin_lock_irqsave(&rq->lock, flags);
  3569. __sched_fork(idle);
  3570. idle->state = TASK_RUNNING;
  3571. idle->se.exec_start = sched_clock();
  3572. do_set_cpus_allowed(idle, cpumask_of(cpu));
  3573. /*
  3574. * We're having a chicken and egg problem, even though we are
  3575. * holding rq->lock, the cpu isn't yet set to this cpu so the
  3576. * lockdep check in task_group() will fail.
  3577. *
  3578. * Similar case to sched_fork(). / Alternatively we could
  3579. * use task_rq_lock() here and obtain the other rq->lock.
  3580. *
  3581. * Silence PROVE_RCU
  3582. */
  3583. rcu_read_lock();
  3584. __set_task_cpu(idle, cpu);
  3585. rcu_read_unlock();
  3586. rq->curr = rq->idle = idle;
  3587. #if defined(CONFIG_SMP)
  3588. idle->on_cpu = 1;
  3589. #endif
  3590. raw_spin_unlock_irqrestore(&rq->lock, flags);
  3591. /* Set the preempt count _outside_ the spinlocks! */
  3592. task_thread_info(idle)->preempt_count = 0;
  3593. /*
  3594. * The idle tasks have their own, simple scheduling class:
  3595. */
  3596. idle->sched_class = &idle_sched_class;
  3597. ftrace_graph_init_idle_task(idle, cpu);
  3598. vtime_init_idle(idle, cpu);
  3599. #if defined(CONFIG_SMP)
  3600. sprintf(idle->comm, "%s/%d", INIT_TASK_COMM, cpu);
  3601. #endif
  3602. }
  3603. #ifdef CONFIG_SMP
  3604. void do_set_cpus_allowed(struct task_struct *p, const struct cpumask *new_mask)
  3605. {
  3606. if (p->sched_class && p->sched_class->set_cpus_allowed)
  3607. p->sched_class->set_cpus_allowed(p, new_mask);
  3608. cpumask_copy(&p->cpus_allowed, new_mask);
  3609. p->nr_cpus_allowed = cpumask_weight(new_mask);
  3610. }
  3611. /*
  3612. * This is how migration works:
  3613. *
  3614. * 1) we invoke migration_cpu_stop() on the target CPU using
  3615. * stop_one_cpu().
  3616. * 2) stopper starts to run (implicitly forcing the migrated thread
  3617. * off the CPU)
  3618. * 3) it checks whether the migrated task is still in the wrong runqueue.
  3619. * 4) if it's in the wrong runqueue then the migration thread removes
  3620. * it and puts it into the right queue.
  3621. * 5) stopper completes and stop_one_cpu() returns and the migration
  3622. * is done.
  3623. */
  3624. /*
  3625. * Change a given task's CPU affinity. Migrate the thread to a
  3626. * proper CPU and schedule it away if the CPU it's executing on
  3627. * is removed from the allowed bitmask.
  3628. *
  3629. * NOTE: the caller must have a valid reference to the task, the
  3630. * task must not exit() & deallocate itself prematurely. The
  3631. * call is not atomic; no spinlocks may be held.
  3632. */
  3633. int set_cpus_allowed_ptr(struct task_struct *p, const struct cpumask *new_mask)
  3634. {
  3635. unsigned long flags;
  3636. struct rq *rq;
  3637. unsigned int dest_cpu;
  3638. int ret = 0;
  3639. rq = task_rq_lock(p, &flags);
  3640. if (cpumask_equal(&p->cpus_allowed, new_mask))
  3641. goto out;
  3642. if (!cpumask_intersects(new_mask, cpu_active_mask)) {
  3643. ret = -EINVAL;
  3644. goto out;
  3645. }
  3646. do_set_cpus_allowed(p, new_mask);
  3647. /* Can the task run on the task's current CPU? If so, we're done */
  3648. if (cpumask_test_cpu(task_cpu(p), new_mask))
  3649. goto out;
  3650. dest_cpu = cpumask_any_and(cpu_active_mask, new_mask);
  3651. if (p->on_rq) {
  3652. struct migration_arg arg = { p, dest_cpu };
  3653. /* Need help from migration thread: drop lock and wait. */
  3654. task_rq_unlock(rq, p, &flags);
  3655. stop_one_cpu(cpu_of(rq), migration_cpu_stop, &arg);
  3656. tlb_migrate_finish(p->mm);
  3657. return 0;
  3658. }
  3659. out:
  3660. task_rq_unlock(rq, p, &flags);
  3661. return ret;
  3662. }
  3663. EXPORT_SYMBOL_GPL(set_cpus_allowed_ptr);
  3664. /*
  3665. * Move (not current) task off this cpu, onto dest cpu. We're doing
  3666. * this because either it can't run here any more (set_cpus_allowed()
  3667. * away from this CPU, or CPU going down), or because we're
  3668. * attempting to rebalance this task on exec (sched_exec).
  3669. *
  3670. * So we race with normal scheduler movements, but that's OK, as long
  3671. * as the task is no longer on this CPU.
  3672. *
  3673. * Returns non-zero if task was successfully migrated.
  3674. */
  3675. static int __migrate_task(struct task_struct *p, int src_cpu, int dest_cpu)
  3676. {
  3677. struct rq *rq_dest, *rq_src;
  3678. int ret = 0;
  3679. if (unlikely(!cpu_active(dest_cpu)))
  3680. return ret;
  3681. rq_src = cpu_rq(src_cpu);
  3682. rq_dest = cpu_rq(dest_cpu);
  3683. raw_spin_lock(&p->pi_lock);
  3684. double_rq_lock(rq_src, rq_dest);
  3685. /* Already moved. */
  3686. if (task_cpu(p) != src_cpu)
  3687. goto done;
  3688. /* Affinity changed (again). */
  3689. if (!cpumask_test_cpu(dest_cpu, tsk_cpus_allowed(p)))
  3690. goto fail;
  3691. /*
  3692. * If we're not on a rq, the next wake-up will ensure we're
  3693. * placed properly.
  3694. */
  3695. if (p->on_rq) {
  3696. dequeue_task(rq_src, p, 0);
  3697. set_task_cpu(p, dest_cpu);
  3698. enqueue_task(rq_dest, p, 0);
  3699. check_preempt_curr(rq_dest, p, 0);
  3700. }
  3701. done:
  3702. ret = 1;
  3703. fail:
  3704. double_rq_unlock(rq_src, rq_dest);
  3705. raw_spin_unlock(&p->pi_lock);
  3706. return ret;
  3707. }
  3708. /*
  3709. * migration_cpu_stop - this will be executed by a highprio stopper thread
  3710. * and performs thread migration by bumping thread off CPU then
  3711. * 'pushing' onto another runqueue.
  3712. */
  3713. static int migration_cpu_stop(void *data)
  3714. {
  3715. struct migration_arg *arg = data;
  3716. /*
  3717. * The original target cpu might have gone down and we might
  3718. * be on another cpu but it doesn't matter.
  3719. */
  3720. local_irq_disable();
  3721. __migrate_task(arg->task, raw_smp_processor_id(), arg->dest_cpu);
  3722. local_irq_enable();
  3723. return 0;
  3724. }
  3725. #ifdef CONFIG_HOTPLUG_CPU
  3726. /*
  3727. * Ensures that the idle task is using init_mm right before its cpu goes
  3728. * offline.
  3729. */
  3730. void idle_task_exit(void)
  3731. {
  3732. struct mm_struct *mm = current->active_mm;
  3733. BUG_ON(cpu_online(smp_processor_id()));
  3734. if (mm != &init_mm)
  3735. switch_mm(mm, &init_mm, current);
  3736. mmdrop(mm);
  3737. }
  3738. /*
  3739. * Since this CPU is going 'away' for a while, fold any nr_active delta
  3740. * we might have. Assumes we're called after migrate_tasks() so that the
  3741. * nr_active count is stable.
  3742. *
  3743. * Also see the comment "Global load-average calculations".
  3744. */
  3745. static void calc_load_migrate(struct rq *rq)
  3746. {
  3747. long delta = calc_load_fold_active(rq);
  3748. if (delta)
  3749. atomic_long_add(delta, &calc_load_tasks);
  3750. }
  3751. /*
  3752. * Migrate all tasks from the rq, sleeping tasks will be migrated by
  3753. * try_to_wake_up()->select_task_rq().
  3754. *
  3755. * Called with rq->lock held even though we'er in stop_machine() and
  3756. * there's no concurrency possible, we hold the required locks anyway
  3757. * because of lock validation efforts.
  3758. */
  3759. static void migrate_tasks(unsigned int dead_cpu)
  3760. {
  3761. struct rq *rq = cpu_rq(dead_cpu);
  3762. struct task_struct *next, *stop = rq->stop;
  3763. int dest_cpu;
  3764. /*
  3765. * Fudge the rq selection such that the below task selection loop
  3766. * doesn't get stuck on the currently eligible stop task.
  3767. *
  3768. * We're currently inside stop_machine() and the rq is either stuck
  3769. * in the stop_machine_cpu_stop() loop, or we're executing this code,
  3770. * either way we should never end up calling schedule() until we're
  3771. * done here.
  3772. */
  3773. rq->stop = NULL;
  3774. /*
  3775. * put_prev_task() and pick_next_task() sched
  3776. * class method both need to have an up-to-date
  3777. * value of rq->clock[_task]
  3778. */
  3779. update_rq_clock(rq);
  3780. for ( ; ; ) {
  3781. /*
  3782. * There's this thread running, bail when that's the only
  3783. * remaining thread.
  3784. */
  3785. if (rq->nr_running == 1)
  3786. break;
  3787. next = pick_next_task(rq);
  3788. BUG_ON(!next);
  3789. next->sched_class->put_prev_task(rq, next);
  3790. /* Find suitable destination for @next, with force if needed. */
  3791. dest_cpu = select_fallback_rq(dead_cpu, next);
  3792. raw_spin_unlock(&rq->lock);
  3793. __migrate_task(next, dead_cpu, dest_cpu);
  3794. raw_spin_lock(&rq->lock);
  3795. }
  3796. rq->stop = stop;
  3797. }
  3798. #endif /* CONFIG_HOTPLUG_CPU */
  3799. #if defined(CONFIG_SCHED_DEBUG) && defined(CONFIG_SYSCTL)
  3800. static struct ctl_table sd_ctl_dir[] = {
  3801. {
  3802. .procname = "sched_domain",
  3803. .mode = 0555,
  3804. },
  3805. {}
  3806. };
  3807. static struct ctl_table sd_ctl_root[] = {
  3808. {
  3809. .procname = "kernel",
  3810. .mode = 0555,
  3811. .child = sd_ctl_dir,
  3812. },
  3813. {}
  3814. };
  3815. static struct ctl_table *sd_alloc_ctl_entry(int n)
  3816. {
  3817. struct ctl_table *entry =
  3818. kcalloc(n, sizeof(struct ctl_table), GFP_KERNEL);
  3819. return entry;
  3820. }
  3821. static void sd_free_ctl_entry(struct ctl_table **tablep)
  3822. {
  3823. struct ctl_table *entry;
  3824. /*
  3825. * In the intermediate directories, both the child directory and
  3826. * procname are dynamically allocated and could fail but the mode
  3827. * will always be set. In the lowest directory the names are
  3828. * static strings and all have proc handlers.
  3829. */
  3830. for (entry = *tablep; entry->mode; entry++) {
  3831. if (entry->child)
  3832. sd_free_ctl_entry(&entry->child);
  3833. if (entry->proc_handler == NULL)
  3834. kfree(entry->procname);
  3835. }
  3836. kfree(*tablep);
  3837. *tablep = NULL;
  3838. }
  3839. static int min_load_idx = 0;
  3840. static int max_load_idx = CPU_LOAD_IDX_MAX-1;
  3841. static void
  3842. set_table_entry(struct ctl_table *entry,
  3843. const char *procname, void *data, int maxlen,
  3844. umode_t mode, proc_handler *proc_handler,
  3845. bool load_idx)
  3846. {
  3847. entry->procname = procname;
  3848. entry->data = data;
  3849. entry->maxlen = maxlen;
  3850. entry->mode = mode;
  3851. entry->proc_handler = proc_handler;
  3852. if (load_idx) {
  3853. entry->extra1 = &min_load_idx;
  3854. entry->extra2 = &max_load_idx;
  3855. }
  3856. }
  3857. static struct ctl_table *
  3858. sd_alloc_ctl_domain_table(struct sched_domain *sd)
  3859. {
  3860. struct ctl_table *table = sd_alloc_ctl_entry(13);
  3861. if (table == NULL)
  3862. return NULL;
  3863. set_table_entry(&table[0], "min_interval", &sd->min_interval,
  3864. sizeof(long), 0644, proc_doulongvec_minmax, false);
  3865. set_table_entry(&table[1], "max_interval", &sd->max_interval,
  3866. sizeof(long), 0644, proc_doulongvec_minmax, false);
  3867. set_table_entry(&table[2], "busy_idx", &sd->busy_idx,
  3868. sizeof(int), 0644, proc_dointvec_minmax, true);
  3869. set_table_entry(&table[3], "idle_idx", &sd->idle_idx,
  3870. sizeof(int), 0644, proc_dointvec_minmax, true);
  3871. set_table_entry(&table[4], "newidle_idx", &sd->newidle_idx,
  3872. sizeof(int), 0644, proc_dointvec_minmax, true);
  3873. set_table_entry(&table[5], "wake_idx", &sd->wake_idx,
  3874. sizeof(int), 0644, proc_dointvec_minmax, true);
  3875. set_table_entry(&table[6], "forkexec_idx", &sd->forkexec_idx,
  3876. sizeof(int), 0644, proc_dointvec_minmax, true);
  3877. set_table_entry(&table[7], "busy_factor", &sd->busy_factor,
  3878. sizeof(int), 0644, proc_dointvec_minmax, false);
  3879. set_table_entry(&table[8], "imbalance_pct", &sd->imbalance_pct,
  3880. sizeof(int), 0644, proc_dointvec_minmax, false);
  3881. set_table_entry(&table[9], "cache_nice_tries",
  3882. &sd->cache_nice_tries,
  3883. sizeof(int), 0644, proc_dointvec_minmax, false);
  3884. set_table_entry(&table[10], "flags", &sd->flags,
  3885. sizeof(int), 0644, proc_dointvec_minmax, false);
  3886. set_table_entry(&table[11], "name", sd->name,
  3887. CORENAME_MAX_SIZE, 0444, proc_dostring, false);
  3888. /* &table[12] is terminator */
  3889. return table;
  3890. }
  3891. static struct ctl_table *sd_alloc_ctl_cpu_table(int cpu)
  3892. {
  3893. struct ctl_table *entry, *table;
  3894. struct sched_domain *sd;
  3895. int domain_num = 0, i;
  3896. char buf[32];
  3897. for_each_domain(cpu, sd)
  3898. domain_num++;
  3899. entry = table = sd_alloc_ctl_entry(domain_num + 1);
  3900. if (table == NULL)
  3901. return NULL;
  3902. i = 0;
  3903. for_each_domain(cpu, sd) {
  3904. snprintf(buf, 32, "domain%d", i);
  3905. entry->procname = kstrdup(buf, GFP_KERNEL);
  3906. entry->mode = 0555;
  3907. entry->child = sd_alloc_ctl_domain_table(sd);
  3908. entry++;
  3909. i++;
  3910. }
  3911. return table;
  3912. }
  3913. static struct ctl_table_header *sd_sysctl_header;
  3914. static void register_sched_domain_sysctl(void)
  3915. {
  3916. int i, cpu_num = num_possible_cpus();
  3917. struct ctl_table *entry = sd_alloc_ctl_entry(cpu_num + 1);
  3918. char buf[32];
  3919. WARN_ON(sd_ctl_dir[0].child);
  3920. sd_ctl_dir[0].child = entry;
  3921. if (entry == NULL)
  3922. return;
  3923. for_each_possible_cpu(i) {
  3924. snprintf(buf, 32, "cpu%d", i);
  3925. entry->procname = kstrdup(buf, GFP_KERNEL);
  3926. entry->mode = 0555;
  3927. entry->child = sd_alloc_ctl_cpu_table(i);
  3928. entry++;
  3929. }
  3930. WARN_ON(sd_sysctl_header);
  3931. sd_sysctl_header = register_sysctl_table(sd_ctl_root);
  3932. }
  3933. /* may be called multiple times per register */
  3934. static void unregister_sched_domain_sysctl(void)
  3935. {
  3936. if (sd_sysctl_header)
  3937. unregister_sysctl_table(sd_sysctl_header);
  3938. sd_sysctl_header = NULL;
  3939. if (sd_ctl_dir[0].child)
  3940. sd_free_ctl_entry(&sd_ctl_dir[0].child);
  3941. }
  3942. #else
  3943. static void register_sched_domain_sysctl(void)
  3944. {
  3945. }
  3946. static void unregister_sched_domain_sysctl(void)
  3947. {
  3948. }
  3949. #endif
  3950. static void set_rq_online(struct rq *rq)
  3951. {
  3952. if (!rq->online) {
  3953. const struct sched_class *class;
  3954. cpumask_set_cpu(rq->cpu, rq->rd->online);
  3955. rq->online = 1;
  3956. for_each_class(class) {
  3957. if (class->rq_online)
  3958. class->rq_online(rq);
  3959. }
  3960. }
  3961. }
  3962. static void set_rq_offline(struct rq *rq)
  3963. {
  3964. if (rq->online) {
  3965. const struct sched_class *class;
  3966. for_each_class(class) {
  3967. if (class->rq_offline)
  3968. class->rq_offline(rq);
  3969. }
  3970. cpumask_clear_cpu(rq->cpu, rq->rd->online);
  3971. rq->online = 0;
  3972. }
  3973. }
  3974. /*
  3975. * migration_call - callback that gets triggered when a CPU is added.
  3976. * Here we can start up the necessary migration thread for the new CPU.
  3977. */
  3978. static int __cpuinit
  3979. migration_call(struct notifier_block *nfb, unsigned long action, void *hcpu)
  3980. {
  3981. int cpu = (long)hcpu;
  3982. unsigned long flags;
  3983. struct rq *rq = cpu_rq(cpu);
  3984. switch (action & ~CPU_TASKS_FROZEN) {
  3985. case CPU_UP_PREPARE:
  3986. rq->calc_load_update = calc_load_update;
  3987. break;
  3988. case CPU_ONLINE:
  3989. /* Update our root-domain */
  3990. raw_spin_lock_irqsave(&rq->lock, flags);
  3991. if (rq->rd) {
  3992. BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  3993. set_rq_online(rq);
  3994. }
  3995. raw_spin_unlock_irqrestore(&rq->lock, flags);
  3996. break;
  3997. #ifdef CONFIG_HOTPLUG_CPU
  3998. case CPU_DYING:
  3999. sched_ttwu_pending();
  4000. /* Update our root-domain */
  4001. raw_spin_lock_irqsave(&rq->lock, flags);
  4002. if (rq->rd) {
  4003. BUG_ON(!cpumask_test_cpu(cpu, rq->rd->span));
  4004. set_rq_offline(rq);
  4005. }
  4006. migrate_tasks(cpu);
  4007. BUG_ON(rq->nr_running != 1); /* the migration thread */
  4008. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4009. break;
  4010. case CPU_DEAD:
  4011. calc_load_migrate(rq);
  4012. break;
  4013. #endif
  4014. }
  4015. update_max_interval();
  4016. return NOTIFY_OK;
  4017. }
  4018. /*
  4019. * Register at high priority so that task migration (migrate_all_tasks)
  4020. * happens before everything else. This has to be lower priority than
  4021. * the notifier in the perf_event subsystem, though.
  4022. */
  4023. static struct notifier_block __cpuinitdata migration_notifier = {
  4024. .notifier_call = migration_call,
  4025. .priority = CPU_PRI_MIGRATION,
  4026. };
  4027. static int __cpuinit sched_cpu_active(struct notifier_block *nfb,
  4028. unsigned long action, void *hcpu)
  4029. {
  4030. switch (action & ~CPU_TASKS_FROZEN) {
  4031. case CPU_STARTING:
  4032. case CPU_DOWN_FAILED:
  4033. set_cpu_active((long)hcpu, true);
  4034. return NOTIFY_OK;
  4035. default:
  4036. return NOTIFY_DONE;
  4037. }
  4038. }
  4039. static int __cpuinit sched_cpu_inactive(struct notifier_block *nfb,
  4040. unsigned long action, void *hcpu)
  4041. {
  4042. switch (action & ~CPU_TASKS_FROZEN) {
  4043. case CPU_DOWN_PREPARE:
  4044. set_cpu_active((long)hcpu, false);
  4045. return NOTIFY_OK;
  4046. default:
  4047. return NOTIFY_DONE;
  4048. }
  4049. }
  4050. static int __init migration_init(void)
  4051. {
  4052. void *cpu = (void *)(long)smp_processor_id();
  4053. int err;
  4054. /* Initialize migration for the boot CPU */
  4055. err = migration_call(&migration_notifier, CPU_UP_PREPARE, cpu);
  4056. BUG_ON(err == NOTIFY_BAD);
  4057. migration_call(&migration_notifier, CPU_ONLINE, cpu);
  4058. register_cpu_notifier(&migration_notifier);
  4059. /* Register cpu active notifiers */
  4060. cpu_notifier(sched_cpu_active, CPU_PRI_SCHED_ACTIVE);
  4061. cpu_notifier(sched_cpu_inactive, CPU_PRI_SCHED_INACTIVE);
  4062. return 0;
  4063. }
  4064. early_initcall(migration_init);
  4065. #endif
  4066. #ifdef CONFIG_SMP
  4067. static cpumask_var_t sched_domains_tmpmask; /* sched_domains_mutex */
  4068. #ifdef CONFIG_SCHED_DEBUG
  4069. static __read_mostly int sched_debug_enabled;
  4070. static int __init sched_debug_setup(char *str)
  4071. {
  4072. sched_debug_enabled = 1;
  4073. return 0;
  4074. }
  4075. early_param("sched_debug", sched_debug_setup);
  4076. static inline bool sched_debug(void)
  4077. {
  4078. return sched_debug_enabled;
  4079. }
  4080. static int sched_domain_debug_one(struct sched_domain *sd, int cpu, int level,
  4081. struct cpumask *groupmask)
  4082. {
  4083. struct sched_group *group = sd->groups;
  4084. char str[256];
  4085. cpulist_scnprintf(str, sizeof(str), sched_domain_span(sd));
  4086. cpumask_clear(groupmask);
  4087. printk(KERN_DEBUG "%*s domain %d: ", level, "", level);
  4088. if (!(sd->flags & SD_LOAD_BALANCE)) {
  4089. printk("does not load-balance\n");
  4090. if (sd->parent)
  4091. printk(KERN_ERR "ERROR: !SD_LOAD_BALANCE domain"
  4092. " has parent");
  4093. return -1;
  4094. }
  4095. printk(KERN_CONT "span %s level %s\n", str, sd->name);
  4096. if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) {
  4097. printk(KERN_ERR "ERROR: domain->span does not contain "
  4098. "CPU%d\n", cpu);
  4099. }
  4100. if (!cpumask_test_cpu(cpu, sched_group_cpus(group))) {
  4101. printk(KERN_ERR "ERROR: domain->groups does not contain"
  4102. " CPU%d\n", cpu);
  4103. }
  4104. printk(KERN_DEBUG "%*s groups:", level + 1, "");
  4105. do {
  4106. if (!group) {
  4107. printk("\n");
  4108. printk(KERN_ERR "ERROR: group is NULL\n");
  4109. break;
  4110. }
  4111. /*
  4112. * Even though we initialize ->power to something semi-sane,
  4113. * we leave power_orig unset. This allows us to detect if
  4114. * domain iteration is still funny without causing /0 traps.
  4115. */
  4116. if (!group->sgp->power_orig) {
  4117. printk(KERN_CONT "\n");
  4118. printk(KERN_ERR "ERROR: domain->cpu_power not "
  4119. "set\n");
  4120. break;
  4121. }
  4122. if (!cpumask_weight(sched_group_cpus(group))) {
  4123. printk(KERN_CONT "\n");
  4124. printk(KERN_ERR "ERROR: empty group\n");
  4125. break;
  4126. }
  4127. if (!(sd->flags & SD_OVERLAP) &&
  4128. cpumask_intersects(groupmask, sched_group_cpus(group))) {
  4129. printk(KERN_CONT "\n");
  4130. printk(KERN_ERR "ERROR: repeated CPUs\n");
  4131. break;
  4132. }
  4133. cpumask_or(groupmask, groupmask, sched_group_cpus(group));
  4134. cpulist_scnprintf(str, sizeof(str), sched_group_cpus(group));
  4135. printk(KERN_CONT " %s", str);
  4136. if (group->sgp->power != SCHED_POWER_SCALE) {
  4137. printk(KERN_CONT " (cpu_power = %d)",
  4138. group->sgp->power);
  4139. }
  4140. group = group->next;
  4141. } while (group != sd->groups);
  4142. printk(KERN_CONT "\n");
  4143. if (!cpumask_equal(sched_domain_span(sd), groupmask))
  4144. printk(KERN_ERR "ERROR: groups don't span domain->span\n");
  4145. if (sd->parent &&
  4146. !cpumask_subset(groupmask, sched_domain_span(sd->parent)))
  4147. printk(KERN_ERR "ERROR: parent span is not a superset "
  4148. "of domain->span\n");
  4149. return 0;
  4150. }
  4151. static void sched_domain_debug(struct sched_domain *sd, int cpu)
  4152. {
  4153. int level = 0;
  4154. if (!sched_debug_enabled)
  4155. return;
  4156. if (!sd) {
  4157. printk(KERN_DEBUG "CPU%d attaching NULL sched-domain.\n", cpu);
  4158. return;
  4159. }
  4160. printk(KERN_DEBUG "CPU%d attaching sched-domain:\n", cpu);
  4161. for (;;) {
  4162. if (sched_domain_debug_one(sd, cpu, level, sched_domains_tmpmask))
  4163. break;
  4164. level++;
  4165. sd = sd->parent;
  4166. if (!sd)
  4167. break;
  4168. }
  4169. }
  4170. #else /* !CONFIG_SCHED_DEBUG */
  4171. # define sched_domain_debug(sd, cpu) do { } while (0)
  4172. static inline bool sched_debug(void)
  4173. {
  4174. return false;
  4175. }
  4176. #endif /* CONFIG_SCHED_DEBUG */
  4177. static int sd_degenerate(struct sched_domain *sd)
  4178. {
  4179. if (cpumask_weight(sched_domain_span(sd)) == 1)
  4180. return 1;
  4181. /* Following flags need at least 2 groups */
  4182. if (sd->flags & (SD_LOAD_BALANCE |
  4183. SD_BALANCE_NEWIDLE |
  4184. SD_BALANCE_FORK |
  4185. SD_BALANCE_EXEC |
  4186. SD_SHARE_CPUPOWER |
  4187. SD_SHARE_PKG_RESOURCES)) {
  4188. if (sd->groups != sd->groups->next)
  4189. return 0;
  4190. }
  4191. /* Following flags don't use groups */
  4192. if (sd->flags & (SD_WAKE_AFFINE))
  4193. return 0;
  4194. return 1;
  4195. }
  4196. static int
  4197. sd_parent_degenerate(struct sched_domain *sd, struct sched_domain *parent)
  4198. {
  4199. unsigned long cflags = sd->flags, pflags = parent->flags;
  4200. if (sd_degenerate(parent))
  4201. return 1;
  4202. if (!cpumask_equal(sched_domain_span(sd), sched_domain_span(parent)))
  4203. return 0;
  4204. /* Flags needing groups don't count if only 1 group in parent */
  4205. if (parent->groups == parent->groups->next) {
  4206. pflags &= ~(SD_LOAD_BALANCE |
  4207. SD_BALANCE_NEWIDLE |
  4208. SD_BALANCE_FORK |
  4209. SD_BALANCE_EXEC |
  4210. SD_SHARE_CPUPOWER |
  4211. SD_SHARE_PKG_RESOURCES);
  4212. if (nr_node_ids == 1)
  4213. pflags &= ~SD_SERIALIZE;
  4214. }
  4215. if (~cflags & pflags)
  4216. return 0;
  4217. return 1;
  4218. }
  4219. static void free_rootdomain(struct rcu_head *rcu)
  4220. {
  4221. struct root_domain *rd = container_of(rcu, struct root_domain, rcu);
  4222. cpupri_cleanup(&rd->cpupri);
  4223. free_cpumask_var(rd->rto_mask);
  4224. free_cpumask_var(rd->online);
  4225. free_cpumask_var(rd->span);
  4226. kfree(rd);
  4227. }
  4228. static void rq_attach_root(struct rq *rq, struct root_domain *rd)
  4229. {
  4230. struct root_domain *old_rd = NULL;
  4231. unsigned long flags;
  4232. raw_spin_lock_irqsave(&rq->lock, flags);
  4233. if (rq->rd) {
  4234. old_rd = rq->rd;
  4235. if (cpumask_test_cpu(rq->cpu, old_rd->online))
  4236. set_rq_offline(rq);
  4237. cpumask_clear_cpu(rq->cpu, old_rd->span);
  4238. /*
  4239. * If we dont want to free the old_rt yet then
  4240. * set old_rd to NULL to skip the freeing later
  4241. * in this function:
  4242. */
  4243. if (!atomic_dec_and_test(&old_rd->refcount))
  4244. old_rd = NULL;
  4245. }
  4246. atomic_inc(&rd->refcount);
  4247. rq->rd = rd;
  4248. cpumask_set_cpu(rq->cpu, rd->span);
  4249. if (cpumask_test_cpu(rq->cpu, cpu_active_mask))
  4250. set_rq_online(rq);
  4251. raw_spin_unlock_irqrestore(&rq->lock, flags);
  4252. if (old_rd)
  4253. call_rcu_sched(&old_rd->rcu, free_rootdomain);
  4254. }
  4255. static int init_rootdomain(struct root_domain *rd)
  4256. {
  4257. memset(rd, 0, sizeof(*rd));
  4258. if (!alloc_cpumask_var(&rd->span, GFP_KERNEL))
  4259. goto out;
  4260. if (!alloc_cpumask_var(&rd->online, GFP_KERNEL))
  4261. goto free_span;
  4262. if (!alloc_cpumask_var(&rd->rto_mask, GFP_KERNEL))
  4263. goto free_online;
  4264. if (cpupri_init(&rd->cpupri) != 0)
  4265. goto free_rto_mask;
  4266. return 0;
  4267. free_rto_mask:
  4268. free_cpumask_var(rd->rto_mask);
  4269. free_online:
  4270. free_cpumask_var(rd->online);
  4271. free_span:
  4272. free_cpumask_var(rd->span);
  4273. out:
  4274. return -ENOMEM;
  4275. }
  4276. /*
  4277. * By default the system creates a single root-domain with all cpus as
  4278. * members (mimicking the global state we have today).
  4279. */
  4280. struct root_domain def_root_domain;
  4281. static void init_defrootdomain(void)
  4282. {
  4283. init_rootdomain(&def_root_domain);
  4284. atomic_set(&def_root_domain.refcount, 1);
  4285. }
  4286. static struct root_domain *alloc_rootdomain(void)
  4287. {
  4288. struct root_domain *rd;
  4289. rd = kmalloc(sizeof(*rd), GFP_KERNEL);
  4290. if (!rd)
  4291. return NULL;
  4292. if (init_rootdomain(rd) != 0) {
  4293. kfree(rd);
  4294. return NULL;
  4295. }
  4296. return rd;
  4297. }
  4298. static void free_sched_groups(struct sched_group *sg, int free_sgp)
  4299. {
  4300. struct sched_group *tmp, *first;
  4301. if (!sg)
  4302. return;
  4303. first = sg;
  4304. do {
  4305. tmp = sg->next;
  4306. if (free_sgp && atomic_dec_and_test(&sg->sgp->ref))
  4307. kfree(sg->sgp);
  4308. kfree(sg);
  4309. sg = tmp;
  4310. } while (sg != first);
  4311. }
  4312. static void free_sched_domain(struct rcu_head *rcu)
  4313. {
  4314. struct sched_domain *sd = container_of(rcu, struct sched_domain, rcu);
  4315. /*
  4316. * If its an overlapping domain it has private groups, iterate and
  4317. * nuke them all.
  4318. */
  4319. if (sd->flags & SD_OVERLAP) {
  4320. free_sched_groups(sd->groups, 1);
  4321. } else if (atomic_dec_and_test(&sd->groups->ref)) {
  4322. kfree(sd->groups->sgp);
  4323. kfree(sd->groups);
  4324. }
  4325. kfree(sd);
  4326. }
  4327. static void destroy_sched_domain(struct sched_domain *sd, int cpu)
  4328. {
  4329. call_rcu(&sd->rcu, free_sched_domain);
  4330. }
  4331. static void destroy_sched_domains(struct sched_domain *sd, int cpu)
  4332. {
  4333. for (; sd; sd = sd->parent)
  4334. destroy_sched_domain(sd, cpu);
  4335. }
  4336. /*
  4337. * Keep a special pointer to the highest sched_domain that has
  4338. * SD_SHARE_PKG_RESOURCE set (Last Level Cache Domain) for this
  4339. * allows us to avoid some pointer chasing select_idle_sibling().
  4340. *
  4341. * Also keep a unique ID per domain (we use the first cpu number in
  4342. * the cpumask of the domain), this allows us to quickly tell if
  4343. * two cpus are in the same cache domain, see cpus_share_cache().
  4344. */
  4345. DEFINE_PER_CPU(struct sched_domain *, sd_llc);
  4346. DEFINE_PER_CPU(int, sd_llc_id);
  4347. static void update_top_cache_domain(int cpu)
  4348. {
  4349. struct sched_domain *sd;
  4350. int id = cpu;
  4351. sd = highest_flag_domain(cpu, SD_SHARE_PKG_RESOURCES);
  4352. if (sd)
  4353. id = cpumask_first(sched_domain_span(sd));
  4354. rcu_assign_pointer(per_cpu(sd_llc, cpu), sd);
  4355. per_cpu(sd_llc_id, cpu) = id;
  4356. }
  4357. /*
  4358. * Attach the domain 'sd' to 'cpu' as its base domain. Callers must
  4359. * hold the hotplug lock.
  4360. */
  4361. static void
  4362. cpu_attach_domain(struct sched_domain *sd, struct root_domain *rd, int cpu)
  4363. {
  4364. struct rq *rq = cpu_rq(cpu);
  4365. struct sched_domain *tmp;
  4366. /* Remove the sched domains which do not contribute to scheduling. */
  4367. for (tmp = sd; tmp; ) {
  4368. struct sched_domain *parent = tmp->parent;
  4369. if (!parent)
  4370. break;
  4371. if (sd_parent_degenerate(tmp, parent)) {
  4372. tmp->parent = parent->parent;
  4373. if (parent->parent)
  4374. parent->parent->child = tmp;
  4375. destroy_sched_domain(parent, cpu);
  4376. } else
  4377. tmp = tmp->parent;
  4378. }
  4379. if (sd && sd_degenerate(sd)) {
  4380. tmp = sd;
  4381. sd = sd->parent;
  4382. destroy_sched_domain(tmp, cpu);
  4383. if (sd)
  4384. sd->child = NULL;
  4385. }
  4386. sched_domain_debug(sd, cpu);
  4387. rq_attach_root(rq, rd);
  4388. tmp = rq->sd;
  4389. rcu_assign_pointer(rq->sd, sd);
  4390. destroy_sched_domains(tmp, cpu);
  4391. update_top_cache_domain(cpu);
  4392. }
  4393. /* cpus with isolated domains */
  4394. static cpumask_var_t cpu_isolated_map;
  4395. /* Setup the mask of cpus configured for isolated domains */
  4396. static int __init isolated_cpu_setup(char *str)
  4397. {
  4398. alloc_bootmem_cpumask_var(&cpu_isolated_map);
  4399. cpulist_parse(str, cpu_isolated_map);
  4400. return 1;
  4401. }
  4402. __setup("isolcpus=", isolated_cpu_setup);
  4403. static const struct cpumask *cpu_cpu_mask(int cpu)
  4404. {
  4405. return cpumask_of_node(cpu_to_node(cpu));
  4406. }
  4407. struct sd_data {
  4408. struct sched_domain **__percpu sd;
  4409. struct sched_group **__percpu sg;
  4410. struct sched_group_power **__percpu sgp;
  4411. };
  4412. struct s_data {
  4413. struct sched_domain ** __percpu sd;
  4414. struct root_domain *rd;
  4415. };
  4416. enum s_alloc {
  4417. sa_rootdomain,
  4418. sa_sd,
  4419. sa_sd_storage,
  4420. sa_none,
  4421. };
  4422. struct sched_domain_topology_level;
  4423. typedef struct sched_domain *(*sched_domain_init_f)(struct sched_domain_topology_level *tl, int cpu);
  4424. typedef const struct cpumask *(*sched_domain_mask_f)(int cpu);
  4425. #define SDTL_OVERLAP 0x01
  4426. struct sched_domain_topology_level {
  4427. sched_domain_init_f init;
  4428. sched_domain_mask_f mask;
  4429. int flags;
  4430. int numa_level;
  4431. struct sd_data data;
  4432. };
  4433. /*
  4434. * Build an iteration mask that can exclude certain CPUs from the upwards
  4435. * domain traversal.
  4436. *
  4437. * Asymmetric node setups can result in situations where the domain tree is of
  4438. * unequal depth, make sure to skip domains that already cover the entire
  4439. * range.
  4440. *
  4441. * In that case build_sched_domains() will have terminated the iteration early
  4442. * and our sibling sd spans will be empty. Domains should always include the
  4443. * cpu they're built on, so check that.
  4444. *
  4445. */
  4446. static void build_group_mask(struct sched_domain *sd, struct sched_group *sg)
  4447. {
  4448. const struct cpumask *span = sched_domain_span(sd);
  4449. struct sd_data *sdd = sd->private;
  4450. struct sched_domain *sibling;
  4451. int i;
  4452. for_each_cpu(i, span) {
  4453. sibling = *per_cpu_ptr(sdd->sd, i);
  4454. if (!cpumask_test_cpu(i, sched_domain_span(sibling)))
  4455. continue;
  4456. cpumask_set_cpu(i, sched_group_mask(sg));
  4457. }
  4458. }
  4459. /*
  4460. * Return the canonical balance cpu for this group, this is the first cpu
  4461. * of this group that's also in the iteration mask.
  4462. */
  4463. int group_balance_cpu(struct sched_group *sg)
  4464. {
  4465. return cpumask_first_and(sched_group_cpus(sg), sched_group_mask(sg));
  4466. }
  4467. static int
  4468. build_overlap_sched_groups(struct sched_domain *sd, int cpu)
  4469. {
  4470. struct sched_group *first = NULL, *last = NULL, *groups = NULL, *sg;
  4471. const struct cpumask *span = sched_domain_span(sd);
  4472. struct cpumask *covered = sched_domains_tmpmask;
  4473. struct sd_data *sdd = sd->private;
  4474. struct sched_domain *child;
  4475. int i;
  4476. cpumask_clear(covered);
  4477. for_each_cpu(i, span) {
  4478. struct cpumask *sg_span;
  4479. if (cpumask_test_cpu(i, covered))
  4480. continue;
  4481. child = *per_cpu_ptr(sdd->sd, i);
  4482. /* See the comment near build_group_mask(). */
  4483. if (!cpumask_test_cpu(i, sched_domain_span(child)))
  4484. continue;
  4485. sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
  4486. GFP_KERNEL, cpu_to_node(cpu));
  4487. if (!sg)
  4488. goto fail;
  4489. sg_span = sched_group_cpus(sg);
  4490. if (child->child) {
  4491. child = child->child;
  4492. cpumask_copy(sg_span, sched_domain_span(child));
  4493. } else
  4494. cpumask_set_cpu(i, sg_span);
  4495. cpumask_or(covered, covered, sg_span);
  4496. sg->sgp = *per_cpu_ptr(sdd->sgp, i);
  4497. if (atomic_inc_return(&sg->sgp->ref) == 1)
  4498. build_group_mask(sd, sg);
  4499. /*
  4500. * Initialize sgp->power such that even if we mess up the
  4501. * domains and no possible iteration will get us here, we won't
  4502. * die on a /0 trap.
  4503. */
  4504. sg->sgp->power = SCHED_POWER_SCALE * cpumask_weight(sg_span);
  4505. /*
  4506. * Make sure the first group of this domain contains the
  4507. * canonical balance cpu. Otherwise the sched_domain iteration
  4508. * breaks. See update_sg_lb_stats().
  4509. */
  4510. if ((!groups && cpumask_test_cpu(cpu, sg_span)) ||
  4511. group_balance_cpu(sg) == cpu)
  4512. groups = sg;
  4513. if (!first)
  4514. first = sg;
  4515. if (last)
  4516. last->next = sg;
  4517. last = sg;
  4518. last->next = first;
  4519. }
  4520. sd->groups = groups;
  4521. return 0;
  4522. fail:
  4523. free_sched_groups(first, 0);
  4524. return -ENOMEM;
  4525. }
  4526. static int get_group(int cpu, struct sd_data *sdd, struct sched_group **sg)
  4527. {
  4528. struct sched_domain *sd = *per_cpu_ptr(sdd->sd, cpu);
  4529. struct sched_domain *child = sd->child;
  4530. if (child)
  4531. cpu = cpumask_first(sched_domain_span(child));
  4532. if (sg) {
  4533. *sg = *per_cpu_ptr(sdd->sg, cpu);
  4534. (*sg)->sgp = *per_cpu_ptr(sdd->sgp, cpu);
  4535. atomic_set(&(*sg)->sgp->ref, 1); /* for claim_allocations */
  4536. }
  4537. return cpu;
  4538. }
  4539. /*
  4540. * build_sched_groups will build a circular linked list of the groups
  4541. * covered by the given span, and will set each group's ->cpumask correctly,
  4542. * and ->cpu_power to 0.
  4543. *
  4544. * Assumes the sched_domain tree is fully constructed
  4545. */
  4546. static int
  4547. build_sched_groups(struct sched_domain *sd, int cpu)
  4548. {
  4549. struct sched_group *first = NULL, *last = NULL;
  4550. struct sd_data *sdd = sd->private;
  4551. const struct cpumask *span = sched_domain_span(sd);
  4552. struct cpumask *covered;
  4553. int i;
  4554. get_group(cpu, sdd, &sd->groups);
  4555. atomic_inc(&sd->groups->ref);
  4556. if (cpu != cpumask_first(span))
  4557. return 0;
  4558. lockdep_assert_held(&sched_domains_mutex);
  4559. covered = sched_domains_tmpmask;
  4560. cpumask_clear(covered);
  4561. for_each_cpu(i, span) {
  4562. struct sched_group *sg;
  4563. int group, j;
  4564. if (cpumask_test_cpu(i, covered))
  4565. continue;
  4566. group = get_group(i, sdd, &sg);
  4567. cpumask_clear(sched_group_cpus(sg));
  4568. sg->sgp->power = 0;
  4569. cpumask_setall(sched_group_mask(sg));
  4570. for_each_cpu(j, span) {
  4571. if (get_group(j, sdd, NULL) != group)
  4572. continue;
  4573. cpumask_set_cpu(j, covered);
  4574. cpumask_set_cpu(j, sched_group_cpus(sg));
  4575. }
  4576. if (!first)
  4577. first = sg;
  4578. if (last)
  4579. last->next = sg;
  4580. last = sg;
  4581. }
  4582. last->next = first;
  4583. return 0;
  4584. }
  4585. /*
  4586. * Initialize sched groups cpu_power.
  4587. *
  4588. * cpu_power indicates the capacity of sched group, which is used while
  4589. * distributing the load between different sched groups in a sched domain.
  4590. * Typically cpu_power for all the groups in a sched domain will be same unless
  4591. * there are asymmetries in the topology. If there are asymmetries, group
  4592. * having more cpu_power will pickup more load compared to the group having
  4593. * less cpu_power.
  4594. */
  4595. static void init_sched_groups_power(int cpu, struct sched_domain *sd)
  4596. {
  4597. struct sched_group *sg = sd->groups;
  4598. WARN_ON(!sg);
  4599. do {
  4600. sg->group_weight = cpumask_weight(sched_group_cpus(sg));
  4601. sg = sg->next;
  4602. } while (sg != sd->groups);
  4603. if (cpu != group_balance_cpu(sg))
  4604. return;
  4605. update_group_power(sd, cpu);
  4606. atomic_set(&sg->sgp->nr_busy_cpus, sg->group_weight);
  4607. }
  4608. int __weak arch_sd_sibling_asym_packing(void)
  4609. {
  4610. return 0*SD_ASYM_PACKING;
  4611. }
  4612. /*
  4613. * Initializers for schedule domains
  4614. * Non-inlined to reduce accumulated stack pressure in build_sched_domains()
  4615. */
  4616. #ifdef CONFIG_SCHED_DEBUG
  4617. # define SD_INIT_NAME(sd, type) sd->name = #type
  4618. #else
  4619. # define SD_INIT_NAME(sd, type) do { } while (0)
  4620. #endif
  4621. #define SD_INIT_FUNC(type) \
  4622. static noinline struct sched_domain * \
  4623. sd_init_##type(struct sched_domain_topology_level *tl, int cpu) \
  4624. { \
  4625. struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu); \
  4626. *sd = SD_##type##_INIT; \
  4627. SD_INIT_NAME(sd, type); \
  4628. sd->private = &tl->data; \
  4629. return sd; \
  4630. }
  4631. SD_INIT_FUNC(CPU)
  4632. #ifdef CONFIG_SCHED_SMT
  4633. SD_INIT_FUNC(SIBLING)
  4634. #endif
  4635. #ifdef CONFIG_SCHED_MC
  4636. SD_INIT_FUNC(MC)
  4637. #endif
  4638. #ifdef CONFIG_SCHED_BOOK
  4639. SD_INIT_FUNC(BOOK)
  4640. #endif
  4641. static int default_relax_domain_level = -1;
  4642. int sched_domain_level_max;
  4643. static int __init setup_relax_domain_level(char *str)
  4644. {
  4645. if (kstrtoint(str, 0, &default_relax_domain_level))
  4646. pr_warn("Unable to set relax_domain_level\n");
  4647. return 1;
  4648. }
  4649. __setup("relax_domain_level=", setup_relax_domain_level);
  4650. static void set_domain_attribute(struct sched_domain *sd,
  4651. struct sched_domain_attr *attr)
  4652. {
  4653. int request;
  4654. if (!attr || attr->relax_domain_level < 0) {
  4655. if (default_relax_domain_level < 0)
  4656. return;
  4657. else
  4658. request = default_relax_domain_level;
  4659. } else
  4660. request = attr->relax_domain_level;
  4661. if (request < sd->level) {
  4662. /* turn off idle balance on this domain */
  4663. sd->flags &= ~(SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
  4664. } else {
  4665. /* turn on idle balance on this domain */
  4666. sd->flags |= (SD_BALANCE_WAKE|SD_BALANCE_NEWIDLE);
  4667. }
  4668. }
  4669. static void __sdt_free(const struct cpumask *cpu_map);
  4670. static int __sdt_alloc(const struct cpumask *cpu_map);
  4671. static void __free_domain_allocs(struct s_data *d, enum s_alloc what,
  4672. const struct cpumask *cpu_map)
  4673. {
  4674. switch (what) {
  4675. case sa_rootdomain:
  4676. if (!atomic_read(&d->rd->refcount))
  4677. free_rootdomain(&d->rd->rcu); /* fall through */
  4678. case sa_sd:
  4679. free_percpu(d->sd); /* fall through */
  4680. case sa_sd_storage:
  4681. __sdt_free(cpu_map); /* fall through */
  4682. case sa_none:
  4683. break;
  4684. }
  4685. }
  4686. static enum s_alloc __visit_domain_allocation_hell(struct s_data *d,
  4687. const struct cpumask *cpu_map)
  4688. {
  4689. memset(d, 0, sizeof(*d));
  4690. if (__sdt_alloc(cpu_map))
  4691. return sa_sd_storage;
  4692. d->sd = alloc_percpu(struct sched_domain *);
  4693. if (!d->sd)
  4694. return sa_sd_storage;
  4695. d->rd = alloc_rootdomain();
  4696. if (!d->rd)
  4697. return sa_sd;
  4698. return sa_rootdomain;
  4699. }
  4700. /*
  4701. * NULL the sd_data elements we've used to build the sched_domain and
  4702. * sched_group structure so that the subsequent __free_domain_allocs()
  4703. * will not free the data we're using.
  4704. */
  4705. static void claim_allocations(int cpu, struct sched_domain *sd)
  4706. {
  4707. struct sd_data *sdd = sd->private;
  4708. WARN_ON_ONCE(*per_cpu_ptr(sdd->sd, cpu) != sd);
  4709. *per_cpu_ptr(sdd->sd, cpu) = NULL;
  4710. if (atomic_read(&(*per_cpu_ptr(sdd->sg, cpu))->ref))
  4711. *per_cpu_ptr(sdd->sg, cpu) = NULL;
  4712. if (atomic_read(&(*per_cpu_ptr(sdd->sgp, cpu))->ref))
  4713. *per_cpu_ptr(sdd->sgp, cpu) = NULL;
  4714. }
  4715. #ifdef CONFIG_SCHED_SMT
  4716. static const struct cpumask *cpu_smt_mask(int cpu)
  4717. {
  4718. return topology_thread_cpumask(cpu);
  4719. }
  4720. #endif
  4721. /*
  4722. * Topology list, bottom-up.
  4723. */
  4724. static struct sched_domain_topology_level default_topology[] = {
  4725. #ifdef CONFIG_SCHED_SMT
  4726. { sd_init_SIBLING, cpu_smt_mask, },
  4727. #endif
  4728. #ifdef CONFIG_SCHED_MC
  4729. { sd_init_MC, cpu_coregroup_mask, },
  4730. #endif
  4731. #ifdef CONFIG_SCHED_BOOK
  4732. { sd_init_BOOK, cpu_book_mask, },
  4733. #endif
  4734. { sd_init_CPU, cpu_cpu_mask, },
  4735. { NULL, },
  4736. };
  4737. static struct sched_domain_topology_level *sched_domain_topology = default_topology;
  4738. #define for_each_sd_topology(tl) \
  4739. for (tl = sched_domain_topology; tl->init; tl++)
  4740. #ifdef CONFIG_NUMA
  4741. static int sched_domains_numa_levels;
  4742. static int *sched_domains_numa_distance;
  4743. static struct cpumask ***sched_domains_numa_masks;
  4744. static int sched_domains_curr_level;
  4745. static inline int sd_local_flags(int level)
  4746. {
  4747. if (sched_domains_numa_distance[level] > RECLAIM_DISTANCE)
  4748. return 0;
  4749. return SD_BALANCE_EXEC | SD_BALANCE_FORK | SD_WAKE_AFFINE;
  4750. }
  4751. static struct sched_domain *
  4752. sd_numa_init(struct sched_domain_topology_level *tl, int cpu)
  4753. {
  4754. struct sched_domain *sd = *per_cpu_ptr(tl->data.sd, cpu);
  4755. int level = tl->numa_level;
  4756. int sd_weight = cpumask_weight(
  4757. sched_domains_numa_masks[level][cpu_to_node(cpu)]);
  4758. *sd = (struct sched_domain){
  4759. .min_interval = sd_weight,
  4760. .max_interval = 2*sd_weight,
  4761. .busy_factor = 32,
  4762. .imbalance_pct = 125,
  4763. .cache_nice_tries = 2,
  4764. .busy_idx = 3,
  4765. .idle_idx = 2,
  4766. .newidle_idx = 0,
  4767. .wake_idx = 0,
  4768. .forkexec_idx = 0,
  4769. .flags = 1*SD_LOAD_BALANCE
  4770. | 1*SD_BALANCE_NEWIDLE
  4771. | 0*SD_BALANCE_EXEC
  4772. | 0*SD_BALANCE_FORK
  4773. | 0*SD_BALANCE_WAKE
  4774. | 0*SD_WAKE_AFFINE
  4775. | 0*SD_SHARE_CPUPOWER
  4776. | 0*SD_SHARE_PKG_RESOURCES
  4777. | 1*SD_SERIALIZE
  4778. | 0*SD_PREFER_SIBLING
  4779. | sd_local_flags(level)
  4780. ,
  4781. .last_balance = jiffies,
  4782. .balance_interval = sd_weight,
  4783. };
  4784. SD_INIT_NAME(sd, NUMA);
  4785. sd->private = &tl->data;
  4786. /*
  4787. * Ugly hack to pass state to sd_numa_mask()...
  4788. */
  4789. sched_domains_curr_level = tl->numa_level;
  4790. return sd;
  4791. }
  4792. static const struct cpumask *sd_numa_mask(int cpu)
  4793. {
  4794. return sched_domains_numa_masks[sched_domains_curr_level][cpu_to_node(cpu)];
  4795. }
  4796. static void sched_numa_warn(const char *str)
  4797. {
  4798. static int done = false;
  4799. int i,j;
  4800. if (done)
  4801. return;
  4802. done = true;
  4803. printk(KERN_WARNING "ERROR: %s\n\n", str);
  4804. for (i = 0; i < nr_node_ids; i++) {
  4805. printk(KERN_WARNING " ");
  4806. for (j = 0; j < nr_node_ids; j++)
  4807. printk(KERN_CONT "%02d ", node_distance(i,j));
  4808. printk(KERN_CONT "\n");
  4809. }
  4810. printk(KERN_WARNING "\n");
  4811. }
  4812. static bool find_numa_distance(int distance)
  4813. {
  4814. int i;
  4815. if (distance == node_distance(0, 0))
  4816. return true;
  4817. for (i = 0; i < sched_domains_numa_levels; i++) {
  4818. if (sched_domains_numa_distance[i] == distance)
  4819. return true;
  4820. }
  4821. return false;
  4822. }
  4823. static void sched_init_numa(void)
  4824. {
  4825. int next_distance, curr_distance = node_distance(0, 0);
  4826. struct sched_domain_topology_level *tl;
  4827. int level = 0;
  4828. int i, j, k;
  4829. sched_domains_numa_distance = kzalloc(sizeof(int) * nr_node_ids, GFP_KERNEL);
  4830. if (!sched_domains_numa_distance)
  4831. return;
  4832. /*
  4833. * O(nr_nodes^2) deduplicating selection sort -- in order to find the
  4834. * unique distances in the node_distance() table.
  4835. *
  4836. * Assumes node_distance(0,j) includes all distances in
  4837. * node_distance(i,j) in order to avoid cubic time.
  4838. */
  4839. next_distance = curr_distance;
  4840. for (i = 0; i < nr_node_ids; i++) {
  4841. for (j = 0; j < nr_node_ids; j++) {
  4842. for (k = 0; k < nr_node_ids; k++) {
  4843. int distance = node_distance(i, k);
  4844. if (distance > curr_distance &&
  4845. (distance < next_distance ||
  4846. next_distance == curr_distance))
  4847. next_distance = distance;
  4848. /*
  4849. * While not a strong assumption it would be nice to know
  4850. * about cases where if node A is connected to B, B is not
  4851. * equally connected to A.
  4852. */
  4853. if (sched_debug() && node_distance(k, i) != distance)
  4854. sched_numa_warn("Node-distance not symmetric");
  4855. if (sched_debug() && i && !find_numa_distance(distance))
  4856. sched_numa_warn("Node-0 not representative");
  4857. }
  4858. if (next_distance != curr_distance) {
  4859. sched_domains_numa_distance[level++] = next_distance;
  4860. sched_domains_numa_levels = level;
  4861. curr_distance = next_distance;
  4862. } else break;
  4863. }
  4864. /*
  4865. * In case of sched_debug() we verify the above assumption.
  4866. */
  4867. if (!sched_debug())
  4868. break;
  4869. }
  4870. /*
  4871. * 'level' contains the number of unique distances, excluding the
  4872. * identity distance node_distance(i,i).
  4873. *
  4874. * The sched_domains_numa_distance[] array includes the actual distance
  4875. * numbers.
  4876. */
  4877. /*
  4878. * Here, we should temporarily reset sched_domains_numa_levels to 0.
  4879. * If it fails to allocate memory for array sched_domains_numa_masks[][],
  4880. * the array will contain less then 'level' members. This could be
  4881. * dangerous when we use it to iterate array sched_domains_numa_masks[][]
  4882. * in other functions.
  4883. *
  4884. * We reset it to 'level' at the end of this function.
  4885. */
  4886. sched_domains_numa_levels = 0;
  4887. sched_domains_numa_masks = kzalloc(sizeof(void *) * level, GFP_KERNEL);
  4888. if (!sched_domains_numa_masks)
  4889. return;
  4890. /*
  4891. * Now for each level, construct a mask per node which contains all
  4892. * cpus of nodes that are that many hops away from us.
  4893. */
  4894. for (i = 0; i < level; i++) {
  4895. sched_domains_numa_masks[i] =
  4896. kzalloc(nr_node_ids * sizeof(void *), GFP_KERNEL);
  4897. if (!sched_domains_numa_masks[i])
  4898. return;
  4899. for (j = 0; j < nr_node_ids; j++) {
  4900. struct cpumask *mask = kzalloc(cpumask_size(), GFP_KERNEL);
  4901. if (!mask)
  4902. return;
  4903. sched_domains_numa_masks[i][j] = mask;
  4904. for (k = 0; k < nr_node_ids; k++) {
  4905. if (node_distance(j, k) > sched_domains_numa_distance[i])
  4906. continue;
  4907. cpumask_or(mask, mask, cpumask_of_node(k));
  4908. }
  4909. }
  4910. }
  4911. tl = kzalloc((ARRAY_SIZE(default_topology) + level) *
  4912. sizeof(struct sched_domain_topology_level), GFP_KERNEL);
  4913. if (!tl)
  4914. return;
  4915. /*
  4916. * Copy the default topology bits..
  4917. */
  4918. for (i = 0; default_topology[i].init; i++)
  4919. tl[i] = default_topology[i];
  4920. /*
  4921. * .. and append 'j' levels of NUMA goodness.
  4922. */
  4923. for (j = 0; j < level; i++, j++) {
  4924. tl[i] = (struct sched_domain_topology_level){
  4925. .init = sd_numa_init,
  4926. .mask = sd_numa_mask,
  4927. .flags = SDTL_OVERLAP,
  4928. .numa_level = j,
  4929. };
  4930. }
  4931. sched_domain_topology = tl;
  4932. sched_domains_numa_levels = level;
  4933. }
  4934. static void sched_domains_numa_masks_set(int cpu)
  4935. {
  4936. int i, j;
  4937. int node = cpu_to_node(cpu);
  4938. for (i = 0; i < sched_domains_numa_levels; i++) {
  4939. for (j = 0; j < nr_node_ids; j++) {
  4940. if (node_distance(j, node) <= sched_domains_numa_distance[i])
  4941. cpumask_set_cpu(cpu, sched_domains_numa_masks[i][j]);
  4942. }
  4943. }
  4944. }
  4945. static void sched_domains_numa_masks_clear(int cpu)
  4946. {
  4947. int i, j;
  4948. for (i = 0; i < sched_domains_numa_levels; i++) {
  4949. for (j = 0; j < nr_node_ids; j++)
  4950. cpumask_clear_cpu(cpu, sched_domains_numa_masks[i][j]);
  4951. }
  4952. }
  4953. /*
  4954. * Update sched_domains_numa_masks[level][node] array when new cpus
  4955. * are onlined.
  4956. */
  4957. static int sched_domains_numa_masks_update(struct notifier_block *nfb,
  4958. unsigned long action,
  4959. void *hcpu)
  4960. {
  4961. int cpu = (long)hcpu;
  4962. switch (action & ~CPU_TASKS_FROZEN) {
  4963. case CPU_ONLINE:
  4964. sched_domains_numa_masks_set(cpu);
  4965. break;
  4966. case CPU_DEAD:
  4967. sched_domains_numa_masks_clear(cpu);
  4968. break;
  4969. default:
  4970. return NOTIFY_DONE;
  4971. }
  4972. return NOTIFY_OK;
  4973. }
  4974. #else
  4975. static inline void sched_init_numa(void)
  4976. {
  4977. }
  4978. static int sched_domains_numa_masks_update(struct notifier_block *nfb,
  4979. unsigned long action,
  4980. void *hcpu)
  4981. {
  4982. return 0;
  4983. }
  4984. #endif /* CONFIG_NUMA */
  4985. static int __sdt_alloc(const struct cpumask *cpu_map)
  4986. {
  4987. struct sched_domain_topology_level *tl;
  4988. int j;
  4989. for_each_sd_topology(tl) {
  4990. struct sd_data *sdd = &tl->data;
  4991. sdd->sd = alloc_percpu(struct sched_domain *);
  4992. if (!sdd->sd)
  4993. return -ENOMEM;
  4994. sdd->sg = alloc_percpu(struct sched_group *);
  4995. if (!sdd->sg)
  4996. return -ENOMEM;
  4997. sdd->sgp = alloc_percpu(struct sched_group_power *);
  4998. if (!sdd->sgp)
  4999. return -ENOMEM;
  5000. for_each_cpu(j, cpu_map) {
  5001. struct sched_domain *sd;
  5002. struct sched_group *sg;
  5003. struct sched_group_power *sgp;
  5004. sd = kzalloc_node(sizeof(struct sched_domain) + cpumask_size(),
  5005. GFP_KERNEL, cpu_to_node(j));
  5006. if (!sd)
  5007. return -ENOMEM;
  5008. *per_cpu_ptr(sdd->sd, j) = sd;
  5009. sg = kzalloc_node(sizeof(struct sched_group) + cpumask_size(),
  5010. GFP_KERNEL, cpu_to_node(j));
  5011. if (!sg)
  5012. return -ENOMEM;
  5013. sg->next = sg;
  5014. *per_cpu_ptr(sdd->sg, j) = sg;
  5015. sgp = kzalloc_node(sizeof(struct sched_group_power) + cpumask_size(),
  5016. GFP_KERNEL, cpu_to_node(j));
  5017. if (!sgp)
  5018. return -ENOMEM;
  5019. *per_cpu_ptr(sdd->sgp, j) = sgp;
  5020. }
  5021. }
  5022. return 0;
  5023. }
  5024. static void __sdt_free(const struct cpumask *cpu_map)
  5025. {
  5026. struct sched_domain_topology_level *tl;
  5027. int j;
  5028. for_each_sd_topology(tl) {
  5029. struct sd_data *sdd = &tl->data;
  5030. for_each_cpu(j, cpu_map) {
  5031. struct sched_domain *sd;
  5032. if (sdd->sd) {
  5033. sd = *per_cpu_ptr(sdd->sd, j);
  5034. if (sd && (sd->flags & SD_OVERLAP))
  5035. free_sched_groups(sd->groups, 0);
  5036. kfree(*per_cpu_ptr(sdd->sd, j));
  5037. }
  5038. if (sdd->sg)
  5039. kfree(*per_cpu_ptr(sdd->sg, j));
  5040. if (sdd->sgp)
  5041. kfree(*per_cpu_ptr(sdd->sgp, j));
  5042. }
  5043. free_percpu(sdd->sd);
  5044. sdd->sd = NULL;
  5045. free_percpu(sdd->sg);
  5046. sdd->sg = NULL;
  5047. free_percpu(sdd->sgp);
  5048. sdd->sgp = NULL;
  5049. }
  5050. }
  5051. struct sched_domain *build_sched_domain(struct sched_domain_topology_level *tl,
  5052. const struct cpumask *cpu_map, struct sched_domain_attr *attr,
  5053. struct sched_domain *child, int cpu)
  5054. {
  5055. struct sched_domain *sd = tl->init(tl, cpu);
  5056. if (!sd)
  5057. return child;
  5058. cpumask_and(sched_domain_span(sd), cpu_map, tl->mask(cpu));
  5059. if (child) {
  5060. sd->level = child->level + 1;
  5061. sched_domain_level_max = max(sched_domain_level_max, sd->level);
  5062. child->parent = sd;
  5063. sd->child = child;
  5064. }
  5065. set_domain_attribute(sd, attr);
  5066. return sd;
  5067. }
  5068. /*
  5069. * Build sched domains for a given set of cpus and attach the sched domains
  5070. * to the individual cpus
  5071. */
  5072. static int build_sched_domains(const struct cpumask *cpu_map,
  5073. struct sched_domain_attr *attr)
  5074. {
  5075. enum s_alloc alloc_state;
  5076. struct sched_domain *sd;
  5077. struct s_data d;
  5078. int i, ret = -ENOMEM;
  5079. alloc_state = __visit_domain_allocation_hell(&d, cpu_map);
  5080. if (alloc_state != sa_rootdomain)
  5081. goto error;
  5082. /* Set up domains for cpus specified by the cpu_map. */
  5083. for_each_cpu(i, cpu_map) {
  5084. struct sched_domain_topology_level *tl;
  5085. sd = NULL;
  5086. for_each_sd_topology(tl) {
  5087. sd = build_sched_domain(tl, cpu_map, attr, sd, i);
  5088. if (tl == sched_domain_topology)
  5089. *per_cpu_ptr(d.sd, i) = sd;
  5090. if (tl->flags & SDTL_OVERLAP || sched_feat(FORCE_SD_OVERLAP))
  5091. sd->flags |= SD_OVERLAP;
  5092. if (cpumask_equal(cpu_map, sched_domain_span(sd)))
  5093. break;
  5094. }
  5095. }
  5096. /* Build the groups for the domains */
  5097. for_each_cpu(i, cpu_map) {
  5098. for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
  5099. sd->span_weight = cpumask_weight(sched_domain_span(sd));
  5100. if (sd->flags & SD_OVERLAP) {
  5101. if (build_overlap_sched_groups(sd, i))
  5102. goto error;
  5103. } else {
  5104. if (build_sched_groups(sd, i))
  5105. goto error;
  5106. }
  5107. }
  5108. }
  5109. /* Calculate CPU power for physical packages and nodes */
  5110. for (i = nr_cpumask_bits-1; i >= 0; i--) {
  5111. if (!cpumask_test_cpu(i, cpu_map))
  5112. continue;
  5113. for (sd = *per_cpu_ptr(d.sd, i); sd; sd = sd->parent) {
  5114. claim_allocations(i, sd);
  5115. init_sched_groups_power(i, sd);
  5116. }
  5117. }
  5118. /* Attach the domains */
  5119. rcu_read_lock();
  5120. for_each_cpu(i, cpu_map) {
  5121. sd = *per_cpu_ptr(d.sd, i);
  5122. cpu_attach_domain(sd, d.rd, i);
  5123. }
  5124. rcu_read_unlock();
  5125. ret = 0;
  5126. error:
  5127. __free_domain_allocs(&d, alloc_state, cpu_map);
  5128. return ret;
  5129. }
  5130. static cpumask_var_t *doms_cur; /* current sched domains */
  5131. static int ndoms_cur; /* number of sched domains in 'doms_cur' */
  5132. static struct sched_domain_attr *dattr_cur;
  5133. /* attribues of custom domains in 'doms_cur' */
  5134. /*
  5135. * Special case: If a kmalloc of a doms_cur partition (array of
  5136. * cpumask) fails, then fallback to a single sched domain,
  5137. * as determined by the single cpumask fallback_doms.
  5138. */
  5139. static cpumask_var_t fallback_doms;
  5140. /*
  5141. * arch_update_cpu_topology lets virtualized architectures update the
  5142. * cpu core maps. It is supposed to return 1 if the topology changed
  5143. * or 0 if it stayed the same.
  5144. */
  5145. int __attribute__((weak)) arch_update_cpu_topology(void)
  5146. {
  5147. return 0;
  5148. }
  5149. cpumask_var_t *alloc_sched_domains(unsigned int ndoms)
  5150. {
  5151. int i;
  5152. cpumask_var_t *doms;
  5153. doms = kmalloc(sizeof(*doms) * ndoms, GFP_KERNEL);
  5154. if (!doms)
  5155. return NULL;
  5156. for (i = 0; i < ndoms; i++) {
  5157. if (!alloc_cpumask_var(&doms[i], GFP_KERNEL)) {
  5158. free_sched_domains(doms, i);
  5159. return NULL;
  5160. }
  5161. }
  5162. return doms;
  5163. }
  5164. void free_sched_domains(cpumask_var_t doms[], unsigned int ndoms)
  5165. {
  5166. unsigned int i;
  5167. for (i = 0; i < ndoms; i++)
  5168. free_cpumask_var(doms[i]);
  5169. kfree(doms);
  5170. }
  5171. /*
  5172. * Set up scheduler domains and groups. Callers must hold the hotplug lock.
  5173. * For now this just excludes isolated cpus, but could be used to
  5174. * exclude other special cases in the future.
  5175. */
  5176. static int init_sched_domains(const struct cpumask *cpu_map)
  5177. {
  5178. int err;
  5179. arch_update_cpu_topology();
  5180. ndoms_cur = 1;
  5181. doms_cur = alloc_sched_domains(ndoms_cur);
  5182. if (!doms_cur)
  5183. doms_cur = &fallback_doms;
  5184. cpumask_andnot(doms_cur[0], cpu_map, cpu_isolated_map);
  5185. err = build_sched_domains(doms_cur[0], NULL);
  5186. register_sched_domain_sysctl();
  5187. return err;
  5188. }
  5189. /*
  5190. * Detach sched domains from a group of cpus specified in cpu_map
  5191. * These cpus will now be attached to the NULL domain
  5192. */
  5193. static void detach_destroy_domains(const struct cpumask *cpu_map)
  5194. {
  5195. int i;
  5196. rcu_read_lock();
  5197. for_each_cpu(i, cpu_map)
  5198. cpu_attach_domain(NULL, &def_root_domain, i);
  5199. rcu_read_unlock();
  5200. }
  5201. /* handle null as "default" */
  5202. static int dattrs_equal(struct sched_domain_attr *cur, int idx_cur,
  5203. struct sched_domain_attr *new, int idx_new)
  5204. {
  5205. struct sched_domain_attr tmp;
  5206. /* fast path */
  5207. if (!new && !cur)
  5208. return 1;
  5209. tmp = SD_ATTR_INIT;
  5210. return !memcmp(cur ? (cur + idx_cur) : &tmp,
  5211. new ? (new + idx_new) : &tmp,
  5212. sizeof(struct sched_domain_attr));
  5213. }
  5214. /*
  5215. * Partition sched domains as specified by the 'ndoms_new'
  5216. * cpumasks in the array doms_new[] of cpumasks. This compares
  5217. * doms_new[] to the current sched domain partitioning, doms_cur[].
  5218. * It destroys each deleted domain and builds each new domain.
  5219. *
  5220. * 'doms_new' is an array of cpumask_var_t's of length 'ndoms_new'.
  5221. * The masks don't intersect (don't overlap.) We should setup one
  5222. * sched domain for each mask. CPUs not in any of the cpumasks will
  5223. * not be load balanced. If the same cpumask appears both in the
  5224. * current 'doms_cur' domains and in the new 'doms_new', we can leave
  5225. * it as it is.
  5226. *
  5227. * The passed in 'doms_new' should be allocated using
  5228. * alloc_sched_domains. This routine takes ownership of it and will
  5229. * free_sched_domains it when done with it. If the caller failed the
  5230. * alloc call, then it can pass in doms_new == NULL && ndoms_new == 1,
  5231. * and partition_sched_domains() will fallback to the single partition
  5232. * 'fallback_doms', it also forces the domains to be rebuilt.
  5233. *
  5234. * If doms_new == NULL it will be replaced with cpu_online_mask.
  5235. * ndoms_new == 0 is a special case for destroying existing domains,
  5236. * and it will not create the default domain.
  5237. *
  5238. * Call with hotplug lock held
  5239. */
  5240. void partition_sched_domains(int ndoms_new, cpumask_var_t doms_new[],
  5241. struct sched_domain_attr *dattr_new)
  5242. {
  5243. int i, j, n;
  5244. int new_topology;
  5245. mutex_lock(&sched_domains_mutex);
  5246. /* always unregister in case we don't destroy any domains */
  5247. unregister_sched_domain_sysctl();
  5248. /* Let architecture update cpu core mappings. */
  5249. new_topology = arch_update_cpu_topology();
  5250. n = doms_new ? ndoms_new : 0;
  5251. /* Destroy deleted domains */
  5252. for (i = 0; i < ndoms_cur; i++) {
  5253. for (j = 0; j < n && !new_topology; j++) {
  5254. if (cpumask_equal(doms_cur[i], doms_new[j])
  5255. && dattrs_equal(dattr_cur, i, dattr_new, j))
  5256. goto match1;
  5257. }
  5258. /* no match - a current sched domain not in new doms_new[] */
  5259. detach_destroy_domains(doms_cur[i]);
  5260. match1:
  5261. ;
  5262. }
  5263. if (doms_new == NULL) {
  5264. ndoms_cur = 0;
  5265. doms_new = &fallback_doms;
  5266. cpumask_andnot(doms_new[0], cpu_active_mask, cpu_isolated_map);
  5267. WARN_ON_ONCE(dattr_new);
  5268. }
  5269. /* Build new domains */
  5270. for (i = 0; i < ndoms_new; i++) {
  5271. for (j = 0; j < ndoms_cur && !new_topology; j++) {
  5272. if (cpumask_equal(doms_new[i], doms_cur[j])
  5273. && dattrs_equal(dattr_new, i, dattr_cur, j))
  5274. goto match2;
  5275. }
  5276. /* no match - add a new doms_new */
  5277. build_sched_domains(doms_new[i], dattr_new ? dattr_new + i : NULL);
  5278. match2:
  5279. ;
  5280. }
  5281. /* Remember the new sched domains */
  5282. if (doms_cur != &fallback_doms)
  5283. free_sched_domains(doms_cur, ndoms_cur);
  5284. kfree(dattr_cur); /* kfree(NULL) is safe */
  5285. doms_cur = doms_new;
  5286. dattr_cur = dattr_new;
  5287. ndoms_cur = ndoms_new;
  5288. register_sched_domain_sysctl();
  5289. mutex_unlock(&sched_domains_mutex);
  5290. }
  5291. static int num_cpus_frozen; /* used to mark begin/end of suspend/resume */
  5292. /*
  5293. * Update cpusets according to cpu_active mask. If cpusets are
  5294. * disabled, cpuset_update_active_cpus() becomes a simple wrapper
  5295. * around partition_sched_domains().
  5296. *
  5297. * If we come here as part of a suspend/resume, don't touch cpusets because we
  5298. * want to restore it back to its original state upon resume anyway.
  5299. */
  5300. static int cpuset_cpu_active(struct notifier_block *nfb, unsigned long action,
  5301. void *hcpu)
  5302. {
  5303. switch (action) {
  5304. case CPU_ONLINE_FROZEN:
  5305. case CPU_DOWN_FAILED_FROZEN:
  5306. /*
  5307. * num_cpus_frozen tracks how many CPUs are involved in suspend
  5308. * resume sequence. As long as this is not the last online
  5309. * operation in the resume sequence, just build a single sched
  5310. * domain, ignoring cpusets.
  5311. */
  5312. num_cpus_frozen--;
  5313. if (likely(num_cpus_frozen)) {
  5314. partition_sched_domains(1, NULL, NULL);
  5315. break;
  5316. }
  5317. /*
  5318. * This is the last CPU online operation. So fall through and
  5319. * restore the original sched domains by considering the
  5320. * cpuset configurations.
  5321. */
  5322. case CPU_ONLINE:
  5323. case CPU_DOWN_FAILED:
  5324. cpuset_update_active_cpus(true);
  5325. break;
  5326. default:
  5327. return NOTIFY_DONE;
  5328. }
  5329. return NOTIFY_OK;
  5330. }
  5331. static int cpuset_cpu_inactive(struct notifier_block *nfb, unsigned long action,
  5332. void *hcpu)
  5333. {
  5334. switch (action) {
  5335. case CPU_DOWN_PREPARE:
  5336. cpuset_update_active_cpus(false);
  5337. break;
  5338. case CPU_DOWN_PREPARE_FROZEN:
  5339. num_cpus_frozen++;
  5340. partition_sched_domains(1, NULL, NULL);
  5341. break;
  5342. default:
  5343. return NOTIFY_DONE;
  5344. }
  5345. return NOTIFY_OK;
  5346. }
  5347. void __init sched_init_smp(void)
  5348. {
  5349. cpumask_var_t non_isolated_cpus;
  5350. alloc_cpumask_var(&non_isolated_cpus, GFP_KERNEL);
  5351. alloc_cpumask_var(&fallback_doms, GFP_KERNEL);
  5352. sched_init_numa();
  5353. get_online_cpus();
  5354. mutex_lock(&sched_domains_mutex);
  5355. init_sched_domains(cpu_active_mask);
  5356. cpumask_andnot(non_isolated_cpus, cpu_possible_mask, cpu_isolated_map);
  5357. if (cpumask_empty(non_isolated_cpus))
  5358. cpumask_set_cpu(smp_processor_id(), non_isolated_cpus);
  5359. mutex_unlock(&sched_domains_mutex);
  5360. put_online_cpus();
  5361. hotcpu_notifier(sched_domains_numa_masks_update, CPU_PRI_SCHED_ACTIVE);
  5362. hotcpu_notifier(cpuset_cpu_active, CPU_PRI_CPUSET_ACTIVE);
  5363. hotcpu_notifier(cpuset_cpu_inactive, CPU_PRI_CPUSET_INACTIVE);
  5364. init_hrtick();
  5365. /* Move init over to a non-isolated CPU */
  5366. if (set_cpus_allowed_ptr(current, non_isolated_cpus) < 0)
  5367. BUG();
  5368. sched_init_granularity();
  5369. free_cpumask_var(non_isolated_cpus);
  5370. init_sched_rt_class();
  5371. }
  5372. #else
  5373. void __init sched_init_smp(void)
  5374. {
  5375. sched_init_granularity();
  5376. }
  5377. #endif /* CONFIG_SMP */
  5378. const_debug unsigned int sysctl_timer_migration = 1;
  5379. int in_sched_functions(unsigned long addr)
  5380. {
  5381. return in_lock_functions(addr) ||
  5382. (addr >= (unsigned long)__sched_text_start
  5383. && addr < (unsigned long)__sched_text_end);
  5384. }
  5385. #ifdef CONFIG_CGROUP_SCHED
  5386. /*
  5387. * Default task group.
  5388. * Every task in system belongs to this group at bootup.
  5389. */
  5390. struct task_group root_task_group;
  5391. LIST_HEAD(task_groups);
  5392. #endif
  5393. DECLARE_PER_CPU(cpumask_var_t, load_balance_mask);
  5394. void __init sched_init(void)
  5395. {
  5396. int i, j;
  5397. unsigned long alloc_size = 0, ptr;
  5398. #ifdef CONFIG_FAIR_GROUP_SCHED
  5399. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  5400. #endif
  5401. #ifdef CONFIG_RT_GROUP_SCHED
  5402. alloc_size += 2 * nr_cpu_ids * sizeof(void **);
  5403. #endif
  5404. #ifdef CONFIG_CPUMASK_OFFSTACK
  5405. alloc_size += num_possible_cpus() * cpumask_size();
  5406. #endif
  5407. if (alloc_size) {
  5408. ptr = (unsigned long)kzalloc(alloc_size, GFP_NOWAIT);
  5409. #ifdef CONFIG_FAIR_GROUP_SCHED
  5410. root_task_group.se = (struct sched_entity **)ptr;
  5411. ptr += nr_cpu_ids * sizeof(void **);
  5412. root_task_group.cfs_rq = (struct cfs_rq **)ptr;
  5413. ptr += nr_cpu_ids * sizeof(void **);
  5414. #endif /* CONFIG_FAIR_GROUP_SCHED */
  5415. #ifdef CONFIG_RT_GROUP_SCHED
  5416. root_task_group.rt_se = (struct sched_rt_entity **)ptr;
  5417. ptr += nr_cpu_ids * sizeof(void **);
  5418. root_task_group.rt_rq = (struct rt_rq **)ptr;
  5419. ptr += nr_cpu_ids * sizeof(void **);
  5420. #endif /* CONFIG_RT_GROUP_SCHED */
  5421. #ifdef CONFIG_CPUMASK_OFFSTACK
  5422. for_each_possible_cpu(i) {
  5423. per_cpu(load_balance_mask, i) = (void *)ptr;
  5424. ptr += cpumask_size();
  5425. }
  5426. #endif /* CONFIG_CPUMASK_OFFSTACK */
  5427. }
  5428. #ifdef CONFIG_SMP
  5429. init_defrootdomain();
  5430. #endif
  5431. init_rt_bandwidth(&def_rt_bandwidth,
  5432. global_rt_period(), global_rt_runtime());
  5433. #ifdef CONFIG_RT_GROUP_SCHED
  5434. init_rt_bandwidth(&root_task_group.rt_bandwidth,
  5435. global_rt_period(), global_rt_runtime());
  5436. #endif /* CONFIG_RT_GROUP_SCHED */
  5437. #ifdef CONFIG_CGROUP_SCHED
  5438. list_add(&root_task_group.list, &task_groups);
  5439. INIT_LIST_HEAD(&root_task_group.children);
  5440. INIT_LIST_HEAD(&root_task_group.siblings);
  5441. autogroup_init(&init_task);
  5442. #endif /* CONFIG_CGROUP_SCHED */
  5443. for_each_possible_cpu(i) {
  5444. struct rq *rq;
  5445. rq = cpu_rq(i);
  5446. raw_spin_lock_init(&rq->lock);
  5447. rq->nr_running = 0;
  5448. rq->calc_load_active = 0;
  5449. rq->calc_load_update = jiffies + LOAD_FREQ;
  5450. init_cfs_rq(&rq->cfs);
  5451. init_rt_rq(&rq->rt, rq);
  5452. #ifdef CONFIG_FAIR_GROUP_SCHED
  5453. root_task_group.shares = ROOT_TASK_GROUP_LOAD;
  5454. INIT_LIST_HEAD(&rq->leaf_cfs_rq_list);
  5455. /*
  5456. * How much cpu bandwidth does root_task_group get?
  5457. *
  5458. * In case of task-groups formed thr' the cgroup filesystem, it
  5459. * gets 100% of the cpu resources in the system. This overall
  5460. * system cpu resource is divided among the tasks of
  5461. * root_task_group and its child task-groups in a fair manner,
  5462. * based on each entity's (task or task-group's) weight
  5463. * (se->load.weight).
  5464. *
  5465. * In other words, if root_task_group has 10 tasks of weight
  5466. * 1024) and two child groups A0 and A1 (of weight 1024 each),
  5467. * then A0's share of the cpu resource is:
  5468. *
  5469. * A0's bandwidth = 1024 / (10*1024 + 1024 + 1024) = 8.33%
  5470. *
  5471. * We achieve this by letting root_task_group's tasks sit
  5472. * directly in rq->cfs (i.e root_task_group->se[] = NULL).
  5473. */
  5474. init_cfs_bandwidth(&root_task_group.cfs_bandwidth);
  5475. init_tg_cfs_entry(&root_task_group, &rq->cfs, NULL, i, NULL);
  5476. #endif /* CONFIG_FAIR_GROUP_SCHED */
  5477. rq->rt.rt_runtime = def_rt_bandwidth.rt_runtime;
  5478. #ifdef CONFIG_RT_GROUP_SCHED
  5479. INIT_LIST_HEAD(&rq->leaf_rt_rq_list);
  5480. init_tg_rt_entry(&root_task_group, &rq->rt, NULL, i, NULL);
  5481. #endif
  5482. for (j = 0; j < CPU_LOAD_IDX_MAX; j++)
  5483. rq->cpu_load[j] = 0;
  5484. rq->last_load_update_tick = jiffies;
  5485. #ifdef CONFIG_SMP
  5486. rq->sd = NULL;
  5487. rq->rd = NULL;
  5488. rq->cpu_power = SCHED_POWER_SCALE;
  5489. rq->post_schedule = 0;
  5490. rq->active_balance = 0;
  5491. rq->next_balance = jiffies;
  5492. rq->push_cpu = 0;
  5493. rq->cpu = i;
  5494. rq->online = 0;
  5495. rq->idle_stamp = 0;
  5496. rq->avg_idle = 2*sysctl_sched_migration_cost;
  5497. INIT_LIST_HEAD(&rq->cfs_tasks);
  5498. rq_attach_root(rq, &def_root_domain);
  5499. #ifdef CONFIG_NO_HZ_COMMON
  5500. rq->nohz_flags = 0;
  5501. #endif
  5502. #ifdef CONFIG_NO_HZ_FULL
  5503. rq->last_sched_tick = 0;
  5504. #endif
  5505. #endif
  5506. init_rq_hrtick(rq);
  5507. atomic_set(&rq->nr_iowait, 0);
  5508. }
  5509. set_load_weight(&init_task);
  5510. #ifdef CONFIG_PREEMPT_NOTIFIERS
  5511. INIT_HLIST_HEAD(&init_task.preempt_notifiers);
  5512. #endif
  5513. #ifdef CONFIG_RT_MUTEXES
  5514. plist_head_init(&init_task.pi_waiters);
  5515. #endif
  5516. /*
  5517. * The boot idle thread does lazy MMU switching as well:
  5518. */
  5519. atomic_inc(&init_mm.mm_count);
  5520. enter_lazy_tlb(&init_mm, current);
  5521. /*
  5522. * Make us the idle thread. Technically, schedule() should not be
  5523. * called from this thread, however somewhere below it might be,
  5524. * but because we are the idle thread, we just pick up running again
  5525. * when this runqueue becomes "idle".
  5526. */
  5527. init_idle(current, smp_processor_id());
  5528. calc_load_update = jiffies + LOAD_FREQ;
  5529. /*
  5530. * During early bootup we pretend to be a normal task:
  5531. */
  5532. current->sched_class = &fair_sched_class;
  5533. #ifdef CONFIG_SMP
  5534. zalloc_cpumask_var(&sched_domains_tmpmask, GFP_NOWAIT);
  5535. /* May be allocated at isolcpus cmdline parse time */
  5536. if (cpu_isolated_map == NULL)
  5537. zalloc_cpumask_var(&cpu_isolated_map, GFP_NOWAIT);
  5538. idle_thread_set_boot_cpu();
  5539. #endif
  5540. init_sched_fair_class();
  5541. scheduler_running = 1;
  5542. }
  5543. #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
  5544. static inline int preempt_count_equals(int preempt_offset)
  5545. {
  5546. int nested = (preempt_count() & ~PREEMPT_ACTIVE) + rcu_preempt_depth();
  5547. return (nested == preempt_offset);
  5548. }
  5549. void __might_sleep(const char *file, int line, int preempt_offset)
  5550. {
  5551. static unsigned long prev_jiffy; /* ratelimiting */
  5552. rcu_sleep_check(); /* WARN_ON_ONCE() by default, no rate limit reqd. */
  5553. if ((preempt_count_equals(preempt_offset) && !irqs_disabled()) ||
  5554. system_state != SYSTEM_RUNNING || oops_in_progress)
  5555. return;
  5556. if (time_before(jiffies, prev_jiffy + HZ) && prev_jiffy)
  5557. return;
  5558. prev_jiffy = jiffies;
  5559. printk(KERN_ERR
  5560. "BUG: sleeping function called from invalid context at %s:%d\n",
  5561. file, line);
  5562. printk(KERN_ERR
  5563. "in_atomic(): %d, irqs_disabled(): %d, pid: %d, name: %s\n",
  5564. in_atomic(), irqs_disabled(),
  5565. current->pid, current->comm);
  5566. debug_show_held_locks(current);
  5567. if (irqs_disabled())
  5568. print_irqtrace_events(current);
  5569. dump_stack();
  5570. }
  5571. EXPORT_SYMBOL(__might_sleep);
  5572. #endif
  5573. #ifdef CONFIG_MAGIC_SYSRQ
  5574. static void normalize_task(struct rq *rq, struct task_struct *p)
  5575. {
  5576. const struct sched_class *prev_class = p->sched_class;
  5577. int old_prio = p->prio;
  5578. int on_rq;
  5579. on_rq = p->on_rq;
  5580. if (on_rq)
  5581. dequeue_task(rq, p, 0);
  5582. __setscheduler(rq, p, SCHED_NORMAL, 0);
  5583. if (on_rq) {
  5584. enqueue_task(rq, p, 0);
  5585. resched_task(rq->curr);
  5586. }
  5587. check_class_changed(rq, p, prev_class, old_prio);
  5588. }
  5589. void normalize_rt_tasks(void)
  5590. {
  5591. struct task_struct *g, *p;
  5592. unsigned long flags;
  5593. struct rq *rq;
  5594. read_lock_irqsave(&tasklist_lock, flags);
  5595. do_each_thread(g, p) {
  5596. /*
  5597. * Only normalize user tasks:
  5598. */
  5599. if (!p->mm)
  5600. continue;
  5601. p->se.exec_start = 0;
  5602. #ifdef CONFIG_SCHEDSTATS
  5603. p->se.statistics.wait_start = 0;
  5604. p->se.statistics.sleep_start = 0;
  5605. p->se.statistics.block_start = 0;
  5606. #endif
  5607. if (!rt_task(p)) {
  5608. /*
  5609. * Renice negative nice level userspace
  5610. * tasks back to 0:
  5611. */
  5612. if (TASK_NICE(p) < 0 && p->mm)
  5613. set_user_nice(p, 0);
  5614. continue;
  5615. }
  5616. raw_spin_lock(&p->pi_lock);
  5617. rq = __task_rq_lock(p);
  5618. normalize_task(rq, p);
  5619. __task_rq_unlock(rq);
  5620. raw_spin_unlock(&p->pi_lock);
  5621. } while_each_thread(g, p);
  5622. read_unlock_irqrestore(&tasklist_lock, flags);
  5623. }
  5624. #endif /* CONFIG_MAGIC_SYSRQ */
  5625. #if defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB)
  5626. /*
  5627. * These functions are only useful for the IA64 MCA handling, or kdb.
  5628. *
  5629. * They can only be called when the whole system has been
  5630. * stopped - every CPU needs to be quiescent, and no scheduling
  5631. * activity can take place. Using them for anything else would
  5632. * be a serious bug, and as a result, they aren't even visible
  5633. * under any other configuration.
  5634. */
  5635. /**
  5636. * curr_task - return the current task for a given cpu.
  5637. * @cpu: the processor in question.
  5638. *
  5639. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  5640. */
  5641. struct task_struct *curr_task(int cpu)
  5642. {
  5643. return cpu_curr(cpu);
  5644. }
  5645. #endif /* defined(CONFIG_IA64) || defined(CONFIG_KGDB_KDB) */
  5646. #ifdef CONFIG_IA64
  5647. /**
  5648. * set_curr_task - set the current task for a given cpu.
  5649. * @cpu: the processor in question.
  5650. * @p: the task pointer to set.
  5651. *
  5652. * Description: This function must only be used when non-maskable interrupts
  5653. * are serviced on a separate stack. It allows the architecture to switch the
  5654. * notion of the current task on a cpu in a non-blocking manner. This function
  5655. * must be called with all CPU's synchronized, and interrupts disabled, the
  5656. * and caller must save the original value of the current task (see
  5657. * curr_task() above) and restore that value before reenabling interrupts and
  5658. * re-starting the system.
  5659. *
  5660. * ONLY VALID WHEN THE WHOLE SYSTEM IS STOPPED!
  5661. */
  5662. void set_curr_task(int cpu, struct task_struct *p)
  5663. {
  5664. cpu_curr(cpu) = p;
  5665. }
  5666. #endif
  5667. #ifdef CONFIG_CGROUP_SCHED
  5668. /* task_group_lock serializes the addition/removal of task groups */
  5669. static DEFINE_SPINLOCK(task_group_lock);
  5670. static void free_sched_group(struct task_group *tg)
  5671. {
  5672. free_fair_sched_group(tg);
  5673. free_rt_sched_group(tg);
  5674. autogroup_free(tg);
  5675. kfree(tg);
  5676. }
  5677. /* allocate runqueue etc for a new task group */
  5678. struct task_group *sched_create_group(struct task_group *parent)
  5679. {
  5680. struct task_group *tg;
  5681. tg = kzalloc(sizeof(*tg), GFP_KERNEL);
  5682. if (!tg)
  5683. return ERR_PTR(-ENOMEM);
  5684. if (!alloc_fair_sched_group(tg, parent))
  5685. goto err;
  5686. if (!alloc_rt_sched_group(tg, parent))
  5687. goto err;
  5688. return tg;
  5689. err:
  5690. free_sched_group(tg);
  5691. return ERR_PTR(-ENOMEM);
  5692. }
  5693. void sched_online_group(struct task_group *tg, struct task_group *parent)
  5694. {
  5695. unsigned long flags;
  5696. spin_lock_irqsave(&task_group_lock, flags);
  5697. list_add_rcu(&tg->list, &task_groups);
  5698. WARN_ON(!parent); /* root should already exist */
  5699. tg->parent = parent;
  5700. INIT_LIST_HEAD(&tg->children);
  5701. list_add_rcu(&tg->siblings, &parent->children);
  5702. spin_unlock_irqrestore(&task_group_lock, flags);
  5703. }
  5704. /* rcu callback to free various structures associated with a task group */
  5705. static void free_sched_group_rcu(struct rcu_head *rhp)
  5706. {
  5707. /* now it should be safe to free those cfs_rqs */
  5708. free_sched_group(container_of(rhp, struct task_group, rcu));
  5709. }
  5710. /* Destroy runqueue etc associated with a task group */
  5711. void sched_destroy_group(struct task_group *tg)
  5712. {
  5713. /* wait for possible concurrent references to cfs_rqs complete */
  5714. call_rcu(&tg->rcu, free_sched_group_rcu);
  5715. }
  5716. void sched_offline_group(struct task_group *tg)
  5717. {
  5718. unsigned long flags;
  5719. int i;
  5720. /* end participation in shares distribution */
  5721. for_each_possible_cpu(i)
  5722. unregister_fair_sched_group(tg, i);
  5723. spin_lock_irqsave(&task_group_lock, flags);
  5724. list_del_rcu(&tg->list);
  5725. list_del_rcu(&tg->siblings);
  5726. spin_unlock_irqrestore(&task_group_lock, flags);
  5727. }
  5728. /* change task's runqueue when it moves between groups.
  5729. * The caller of this function should have put the task in its new group
  5730. * by now. This function just updates tsk->se.cfs_rq and tsk->se.parent to
  5731. * reflect its new group.
  5732. */
  5733. void sched_move_task(struct task_struct *tsk)
  5734. {
  5735. struct task_group *tg;
  5736. int on_rq, running;
  5737. unsigned long flags;
  5738. struct rq *rq;
  5739. rq = task_rq_lock(tsk, &flags);
  5740. running = task_current(rq, tsk);
  5741. on_rq = tsk->on_rq;
  5742. if (on_rq)
  5743. dequeue_task(rq, tsk, 0);
  5744. if (unlikely(running))
  5745. tsk->sched_class->put_prev_task(rq, tsk);
  5746. tg = container_of(task_subsys_state_check(tsk, cpu_cgroup_subsys_id,
  5747. lockdep_is_held(&tsk->sighand->siglock)),
  5748. struct task_group, css);
  5749. tg = autogroup_task_group(tsk, tg);
  5750. tsk->sched_task_group = tg;
  5751. #ifdef CONFIG_FAIR_GROUP_SCHED
  5752. if (tsk->sched_class->task_move_group)
  5753. tsk->sched_class->task_move_group(tsk, on_rq);
  5754. else
  5755. #endif
  5756. set_task_rq(tsk, task_cpu(tsk));
  5757. if (unlikely(running))
  5758. tsk->sched_class->set_curr_task(rq);
  5759. if (on_rq)
  5760. enqueue_task(rq, tsk, 0);
  5761. task_rq_unlock(rq, tsk, &flags);
  5762. }
  5763. #endif /* CONFIG_CGROUP_SCHED */
  5764. #if defined(CONFIG_RT_GROUP_SCHED) || defined(CONFIG_CFS_BANDWIDTH)
  5765. static unsigned long to_ratio(u64 period, u64 runtime)
  5766. {
  5767. if (runtime == RUNTIME_INF)
  5768. return 1ULL << 20;
  5769. return div64_u64(runtime << 20, period);
  5770. }
  5771. #endif
  5772. #ifdef CONFIG_RT_GROUP_SCHED
  5773. /*
  5774. * Ensure that the real time constraints are schedulable.
  5775. */
  5776. static DEFINE_MUTEX(rt_constraints_mutex);
  5777. /* Must be called with tasklist_lock held */
  5778. static inline int tg_has_rt_tasks(struct task_group *tg)
  5779. {
  5780. struct task_struct *g, *p;
  5781. do_each_thread(g, p) {
  5782. if (rt_task(p) && task_rq(p)->rt.tg == tg)
  5783. return 1;
  5784. } while_each_thread(g, p);
  5785. return 0;
  5786. }
  5787. struct rt_schedulable_data {
  5788. struct task_group *tg;
  5789. u64 rt_period;
  5790. u64 rt_runtime;
  5791. };
  5792. static int tg_rt_schedulable(struct task_group *tg, void *data)
  5793. {
  5794. struct rt_schedulable_data *d = data;
  5795. struct task_group *child;
  5796. unsigned long total, sum = 0;
  5797. u64 period, runtime;
  5798. period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  5799. runtime = tg->rt_bandwidth.rt_runtime;
  5800. if (tg == d->tg) {
  5801. period = d->rt_period;
  5802. runtime = d->rt_runtime;
  5803. }
  5804. /*
  5805. * Cannot have more runtime than the period.
  5806. */
  5807. if (runtime > period && runtime != RUNTIME_INF)
  5808. return -EINVAL;
  5809. /*
  5810. * Ensure we don't starve existing RT tasks.
  5811. */
  5812. if (rt_bandwidth_enabled() && !runtime && tg_has_rt_tasks(tg))
  5813. return -EBUSY;
  5814. total = to_ratio(period, runtime);
  5815. /*
  5816. * Nobody can have more than the global setting allows.
  5817. */
  5818. if (total > to_ratio(global_rt_period(), global_rt_runtime()))
  5819. return -EINVAL;
  5820. /*
  5821. * The sum of our children's runtime should not exceed our own.
  5822. */
  5823. list_for_each_entry_rcu(child, &tg->children, siblings) {
  5824. period = ktime_to_ns(child->rt_bandwidth.rt_period);
  5825. runtime = child->rt_bandwidth.rt_runtime;
  5826. if (child == d->tg) {
  5827. period = d->rt_period;
  5828. runtime = d->rt_runtime;
  5829. }
  5830. sum += to_ratio(period, runtime);
  5831. }
  5832. if (sum > total)
  5833. return -EINVAL;
  5834. return 0;
  5835. }
  5836. static int __rt_schedulable(struct task_group *tg, u64 period, u64 runtime)
  5837. {
  5838. int ret;
  5839. struct rt_schedulable_data data = {
  5840. .tg = tg,
  5841. .rt_period = period,
  5842. .rt_runtime = runtime,
  5843. };
  5844. rcu_read_lock();
  5845. ret = walk_tg_tree(tg_rt_schedulable, tg_nop, &data);
  5846. rcu_read_unlock();
  5847. return ret;
  5848. }
  5849. static int tg_set_rt_bandwidth(struct task_group *tg,
  5850. u64 rt_period, u64 rt_runtime)
  5851. {
  5852. int i, err = 0;
  5853. mutex_lock(&rt_constraints_mutex);
  5854. read_lock(&tasklist_lock);
  5855. err = __rt_schedulable(tg, rt_period, rt_runtime);
  5856. if (err)
  5857. goto unlock;
  5858. raw_spin_lock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  5859. tg->rt_bandwidth.rt_period = ns_to_ktime(rt_period);
  5860. tg->rt_bandwidth.rt_runtime = rt_runtime;
  5861. for_each_possible_cpu(i) {
  5862. struct rt_rq *rt_rq = tg->rt_rq[i];
  5863. raw_spin_lock(&rt_rq->rt_runtime_lock);
  5864. rt_rq->rt_runtime = rt_runtime;
  5865. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  5866. }
  5867. raw_spin_unlock_irq(&tg->rt_bandwidth.rt_runtime_lock);
  5868. unlock:
  5869. read_unlock(&tasklist_lock);
  5870. mutex_unlock(&rt_constraints_mutex);
  5871. return err;
  5872. }
  5873. static int sched_group_set_rt_runtime(struct task_group *tg, long rt_runtime_us)
  5874. {
  5875. u64 rt_runtime, rt_period;
  5876. rt_period = ktime_to_ns(tg->rt_bandwidth.rt_period);
  5877. rt_runtime = (u64)rt_runtime_us * NSEC_PER_USEC;
  5878. if (rt_runtime_us < 0)
  5879. rt_runtime = RUNTIME_INF;
  5880. return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
  5881. }
  5882. static long sched_group_rt_runtime(struct task_group *tg)
  5883. {
  5884. u64 rt_runtime_us;
  5885. if (tg->rt_bandwidth.rt_runtime == RUNTIME_INF)
  5886. return -1;
  5887. rt_runtime_us = tg->rt_bandwidth.rt_runtime;
  5888. do_div(rt_runtime_us, NSEC_PER_USEC);
  5889. return rt_runtime_us;
  5890. }
  5891. static int sched_group_set_rt_period(struct task_group *tg, long rt_period_us)
  5892. {
  5893. u64 rt_runtime, rt_period;
  5894. rt_period = (u64)rt_period_us * NSEC_PER_USEC;
  5895. rt_runtime = tg->rt_bandwidth.rt_runtime;
  5896. if (rt_period == 0)
  5897. return -EINVAL;
  5898. return tg_set_rt_bandwidth(tg, rt_period, rt_runtime);
  5899. }
  5900. static long sched_group_rt_period(struct task_group *tg)
  5901. {
  5902. u64 rt_period_us;
  5903. rt_period_us = ktime_to_ns(tg->rt_bandwidth.rt_period);
  5904. do_div(rt_period_us, NSEC_PER_USEC);
  5905. return rt_period_us;
  5906. }
  5907. static int sched_rt_global_constraints(void)
  5908. {
  5909. u64 runtime, period;
  5910. int ret = 0;
  5911. if (sysctl_sched_rt_period <= 0)
  5912. return -EINVAL;
  5913. runtime = global_rt_runtime();
  5914. period = global_rt_period();
  5915. /*
  5916. * Sanity check on the sysctl variables.
  5917. */
  5918. if (runtime > period && runtime != RUNTIME_INF)
  5919. return -EINVAL;
  5920. mutex_lock(&rt_constraints_mutex);
  5921. read_lock(&tasklist_lock);
  5922. ret = __rt_schedulable(NULL, 0, 0);
  5923. read_unlock(&tasklist_lock);
  5924. mutex_unlock(&rt_constraints_mutex);
  5925. return ret;
  5926. }
  5927. static int sched_rt_can_attach(struct task_group *tg, struct task_struct *tsk)
  5928. {
  5929. /* Don't accept realtime tasks when there is no way for them to run */
  5930. if (rt_task(tsk) && tg->rt_bandwidth.rt_runtime == 0)
  5931. return 0;
  5932. return 1;
  5933. }
  5934. #else /* !CONFIG_RT_GROUP_SCHED */
  5935. static int sched_rt_global_constraints(void)
  5936. {
  5937. unsigned long flags;
  5938. int i;
  5939. if (sysctl_sched_rt_period <= 0)
  5940. return -EINVAL;
  5941. /*
  5942. * There's always some RT tasks in the root group
  5943. * -- migration, kstopmachine etc..
  5944. */
  5945. if (sysctl_sched_rt_runtime == 0)
  5946. return -EBUSY;
  5947. raw_spin_lock_irqsave(&def_rt_bandwidth.rt_runtime_lock, flags);
  5948. for_each_possible_cpu(i) {
  5949. struct rt_rq *rt_rq = &cpu_rq(i)->rt;
  5950. raw_spin_lock(&rt_rq->rt_runtime_lock);
  5951. rt_rq->rt_runtime = global_rt_runtime();
  5952. raw_spin_unlock(&rt_rq->rt_runtime_lock);
  5953. }
  5954. raw_spin_unlock_irqrestore(&def_rt_bandwidth.rt_runtime_lock, flags);
  5955. return 0;
  5956. }
  5957. #endif /* CONFIG_RT_GROUP_SCHED */
  5958. int sched_rr_handler(struct ctl_table *table, int write,
  5959. void __user *buffer, size_t *lenp,
  5960. loff_t *ppos)
  5961. {
  5962. int ret;
  5963. static DEFINE_MUTEX(mutex);
  5964. mutex_lock(&mutex);
  5965. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  5966. /* make sure that internally we keep jiffies */
  5967. /* also, writing zero resets timeslice to default */
  5968. if (!ret && write) {
  5969. sched_rr_timeslice = sched_rr_timeslice <= 0 ?
  5970. RR_TIMESLICE : msecs_to_jiffies(sched_rr_timeslice);
  5971. }
  5972. mutex_unlock(&mutex);
  5973. return ret;
  5974. }
  5975. int sched_rt_handler(struct ctl_table *table, int write,
  5976. void __user *buffer, size_t *lenp,
  5977. loff_t *ppos)
  5978. {
  5979. int ret;
  5980. int old_period, old_runtime;
  5981. static DEFINE_MUTEX(mutex);
  5982. mutex_lock(&mutex);
  5983. old_period = sysctl_sched_rt_period;
  5984. old_runtime = sysctl_sched_rt_runtime;
  5985. ret = proc_dointvec(table, write, buffer, lenp, ppos);
  5986. if (!ret && write) {
  5987. ret = sched_rt_global_constraints();
  5988. if (ret) {
  5989. sysctl_sched_rt_period = old_period;
  5990. sysctl_sched_rt_runtime = old_runtime;
  5991. } else {
  5992. def_rt_bandwidth.rt_runtime = global_rt_runtime();
  5993. def_rt_bandwidth.rt_period =
  5994. ns_to_ktime(global_rt_period());
  5995. }
  5996. }
  5997. mutex_unlock(&mutex);
  5998. return ret;
  5999. }
  6000. #ifdef CONFIG_CGROUP_SCHED
  6001. /* return corresponding task_group object of a cgroup */
  6002. static inline struct task_group *cgroup_tg(struct cgroup *cgrp)
  6003. {
  6004. return container_of(cgroup_subsys_state(cgrp, cpu_cgroup_subsys_id),
  6005. struct task_group, css);
  6006. }
  6007. static struct cgroup_subsys_state *cpu_cgroup_css_alloc(struct cgroup *cgrp)
  6008. {
  6009. struct task_group *tg, *parent;
  6010. if (!cgrp->parent) {
  6011. /* This is early initialization for the top cgroup */
  6012. return &root_task_group.css;
  6013. }
  6014. parent = cgroup_tg(cgrp->parent);
  6015. tg = sched_create_group(parent);
  6016. if (IS_ERR(tg))
  6017. return ERR_PTR(-ENOMEM);
  6018. return &tg->css;
  6019. }
  6020. static int cpu_cgroup_css_online(struct cgroup *cgrp)
  6021. {
  6022. struct task_group *tg = cgroup_tg(cgrp);
  6023. struct task_group *parent;
  6024. if (!cgrp->parent)
  6025. return 0;
  6026. parent = cgroup_tg(cgrp->parent);
  6027. sched_online_group(tg, parent);
  6028. return 0;
  6029. }
  6030. static void cpu_cgroup_css_free(struct cgroup *cgrp)
  6031. {
  6032. struct task_group *tg = cgroup_tg(cgrp);
  6033. sched_destroy_group(tg);
  6034. }
  6035. static void cpu_cgroup_css_offline(struct cgroup *cgrp)
  6036. {
  6037. struct task_group *tg = cgroup_tg(cgrp);
  6038. sched_offline_group(tg);
  6039. }
  6040. static int cpu_cgroup_can_attach(struct cgroup *cgrp,
  6041. struct cgroup_taskset *tset)
  6042. {
  6043. struct task_struct *task;
  6044. cgroup_taskset_for_each(task, cgrp, tset) {
  6045. #ifdef CONFIG_RT_GROUP_SCHED
  6046. if (!sched_rt_can_attach(cgroup_tg(cgrp), task))
  6047. return -EINVAL;
  6048. #else
  6049. /* We don't support RT-tasks being in separate groups */
  6050. if (task->sched_class != &fair_sched_class)
  6051. return -EINVAL;
  6052. #endif
  6053. }
  6054. return 0;
  6055. }
  6056. static void cpu_cgroup_attach(struct cgroup *cgrp,
  6057. struct cgroup_taskset *tset)
  6058. {
  6059. struct task_struct *task;
  6060. cgroup_taskset_for_each(task, cgrp, tset)
  6061. sched_move_task(task);
  6062. }
  6063. static void
  6064. cpu_cgroup_exit(struct cgroup *cgrp, struct cgroup *old_cgrp,
  6065. struct task_struct *task)
  6066. {
  6067. /*
  6068. * cgroup_exit() is called in the copy_process() failure path.
  6069. * Ignore this case since the task hasn't ran yet, this avoids
  6070. * trying to poke a half freed task state from generic code.
  6071. */
  6072. if (!(task->flags & PF_EXITING))
  6073. return;
  6074. sched_move_task(task);
  6075. }
  6076. #ifdef CONFIG_FAIR_GROUP_SCHED
  6077. static int cpu_shares_write_u64(struct cgroup *cgrp, struct cftype *cftype,
  6078. u64 shareval)
  6079. {
  6080. return sched_group_set_shares(cgroup_tg(cgrp), scale_load(shareval));
  6081. }
  6082. static u64 cpu_shares_read_u64(struct cgroup *cgrp, struct cftype *cft)
  6083. {
  6084. struct task_group *tg = cgroup_tg(cgrp);
  6085. return (u64) scale_load_down(tg->shares);
  6086. }
  6087. #ifdef CONFIG_CFS_BANDWIDTH
  6088. static DEFINE_MUTEX(cfs_constraints_mutex);
  6089. const u64 max_cfs_quota_period = 1 * NSEC_PER_SEC; /* 1s */
  6090. const u64 min_cfs_quota_period = 1 * NSEC_PER_MSEC; /* 1ms */
  6091. static int __cfs_schedulable(struct task_group *tg, u64 period, u64 runtime);
  6092. static int tg_set_cfs_bandwidth(struct task_group *tg, u64 period, u64 quota)
  6093. {
  6094. int i, ret = 0, runtime_enabled, runtime_was_enabled;
  6095. struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
  6096. if (tg == &root_task_group)
  6097. return -EINVAL;
  6098. /*
  6099. * Ensure we have at some amount of bandwidth every period. This is
  6100. * to prevent reaching a state of large arrears when throttled via
  6101. * entity_tick() resulting in prolonged exit starvation.
  6102. */
  6103. if (quota < min_cfs_quota_period || period < min_cfs_quota_period)
  6104. return -EINVAL;
  6105. /*
  6106. * Likewise, bound things on the otherside by preventing insane quota
  6107. * periods. This also allows us to normalize in computing quota
  6108. * feasibility.
  6109. */
  6110. if (period > max_cfs_quota_period)
  6111. return -EINVAL;
  6112. mutex_lock(&cfs_constraints_mutex);
  6113. ret = __cfs_schedulable(tg, period, quota);
  6114. if (ret)
  6115. goto out_unlock;
  6116. runtime_enabled = quota != RUNTIME_INF;
  6117. runtime_was_enabled = cfs_b->quota != RUNTIME_INF;
  6118. account_cfs_bandwidth_used(runtime_enabled, runtime_was_enabled);
  6119. raw_spin_lock_irq(&cfs_b->lock);
  6120. cfs_b->period = ns_to_ktime(period);
  6121. cfs_b->quota = quota;
  6122. __refill_cfs_bandwidth_runtime(cfs_b);
  6123. /* restart the period timer (if active) to handle new period expiry */
  6124. if (runtime_enabled && cfs_b->timer_active) {
  6125. /* force a reprogram */
  6126. cfs_b->timer_active = 0;
  6127. __start_cfs_bandwidth(cfs_b);
  6128. }
  6129. raw_spin_unlock_irq(&cfs_b->lock);
  6130. for_each_possible_cpu(i) {
  6131. struct cfs_rq *cfs_rq = tg->cfs_rq[i];
  6132. struct rq *rq = cfs_rq->rq;
  6133. raw_spin_lock_irq(&rq->lock);
  6134. cfs_rq->runtime_enabled = runtime_enabled;
  6135. cfs_rq->runtime_remaining = 0;
  6136. if (cfs_rq->throttled)
  6137. unthrottle_cfs_rq(cfs_rq);
  6138. raw_spin_unlock_irq(&rq->lock);
  6139. }
  6140. out_unlock:
  6141. mutex_unlock(&cfs_constraints_mutex);
  6142. return ret;
  6143. }
  6144. int tg_set_cfs_quota(struct task_group *tg, long cfs_quota_us)
  6145. {
  6146. u64 quota, period;
  6147. period = ktime_to_ns(tg->cfs_bandwidth.period);
  6148. if (cfs_quota_us < 0)
  6149. quota = RUNTIME_INF;
  6150. else
  6151. quota = (u64)cfs_quota_us * NSEC_PER_USEC;
  6152. return tg_set_cfs_bandwidth(tg, period, quota);
  6153. }
  6154. long tg_get_cfs_quota(struct task_group *tg)
  6155. {
  6156. u64 quota_us;
  6157. if (tg->cfs_bandwidth.quota == RUNTIME_INF)
  6158. return -1;
  6159. quota_us = tg->cfs_bandwidth.quota;
  6160. do_div(quota_us, NSEC_PER_USEC);
  6161. return quota_us;
  6162. }
  6163. int tg_set_cfs_period(struct task_group *tg, long cfs_period_us)
  6164. {
  6165. u64 quota, period;
  6166. period = (u64)cfs_period_us * NSEC_PER_USEC;
  6167. quota = tg->cfs_bandwidth.quota;
  6168. return tg_set_cfs_bandwidth(tg, period, quota);
  6169. }
  6170. long tg_get_cfs_period(struct task_group *tg)
  6171. {
  6172. u64 cfs_period_us;
  6173. cfs_period_us = ktime_to_ns(tg->cfs_bandwidth.period);
  6174. do_div(cfs_period_us, NSEC_PER_USEC);
  6175. return cfs_period_us;
  6176. }
  6177. static s64 cpu_cfs_quota_read_s64(struct cgroup *cgrp, struct cftype *cft)
  6178. {
  6179. return tg_get_cfs_quota(cgroup_tg(cgrp));
  6180. }
  6181. static int cpu_cfs_quota_write_s64(struct cgroup *cgrp, struct cftype *cftype,
  6182. s64 cfs_quota_us)
  6183. {
  6184. return tg_set_cfs_quota(cgroup_tg(cgrp), cfs_quota_us);
  6185. }
  6186. static u64 cpu_cfs_period_read_u64(struct cgroup *cgrp, struct cftype *cft)
  6187. {
  6188. return tg_get_cfs_period(cgroup_tg(cgrp));
  6189. }
  6190. static int cpu_cfs_period_write_u64(struct cgroup *cgrp, struct cftype *cftype,
  6191. u64 cfs_period_us)
  6192. {
  6193. return tg_set_cfs_period(cgroup_tg(cgrp), cfs_period_us);
  6194. }
  6195. struct cfs_schedulable_data {
  6196. struct task_group *tg;
  6197. u64 period, quota;
  6198. };
  6199. /*
  6200. * normalize group quota/period to be quota/max_period
  6201. * note: units are usecs
  6202. */
  6203. static u64 normalize_cfs_quota(struct task_group *tg,
  6204. struct cfs_schedulable_data *d)
  6205. {
  6206. u64 quota, period;
  6207. if (tg == d->tg) {
  6208. period = d->period;
  6209. quota = d->quota;
  6210. } else {
  6211. period = tg_get_cfs_period(tg);
  6212. quota = tg_get_cfs_quota(tg);
  6213. }
  6214. /* note: these should typically be equivalent */
  6215. if (quota == RUNTIME_INF || quota == -1)
  6216. return RUNTIME_INF;
  6217. return to_ratio(period, quota);
  6218. }
  6219. static int tg_cfs_schedulable_down(struct task_group *tg, void *data)
  6220. {
  6221. struct cfs_schedulable_data *d = data;
  6222. struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
  6223. s64 quota = 0, parent_quota = -1;
  6224. if (!tg->parent) {
  6225. quota = RUNTIME_INF;
  6226. } else {
  6227. struct cfs_bandwidth *parent_b = &tg->parent->cfs_bandwidth;
  6228. quota = normalize_cfs_quota(tg, d);
  6229. parent_quota = parent_b->hierarchal_quota;
  6230. /*
  6231. * ensure max(child_quota) <= parent_quota, inherit when no
  6232. * limit is set
  6233. */
  6234. if (quota == RUNTIME_INF)
  6235. quota = parent_quota;
  6236. else if (parent_quota != RUNTIME_INF && quota > parent_quota)
  6237. return -EINVAL;
  6238. }
  6239. cfs_b->hierarchal_quota = quota;
  6240. return 0;
  6241. }
  6242. static int __cfs_schedulable(struct task_group *tg, u64 period, u64 quota)
  6243. {
  6244. int ret;
  6245. struct cfs_schedulable_data data = {
  6246. .tg = tg,
  6247. .period = period,
  6248. .quota = quota,
  6249. };
  6250. if (quota != RUNTIME_INF) {
  6251. do_div(data.period, NSEC_PER_USEC);
  6252. do_div(data.quota, NSEC_PER_USEC);
  6253. }
  6254. rcu_read_lock();
  6255. ret = walk_tg_tree(tg_cfs_schedulable_down, tg_nop, &data);
  6256. rcu_read_unlock();
  6257. return ret;
  6258. }
  6259. static int cpu_stats_show(struct cgroup *cgrp, struct cftype *cft,
  6260. struct cgroup_map_cb *cb)
  6261. {
  6262. struct task_group *tg = cgroup_tg(cgrp);
  6263. struct cfs_bandwidth *cfs_b = &tg->cfs_bandwidth;
  6264. cb->fill(cb, "nr_periods", cfs_b->nr_periods);
  6265. cb->fill(cb, "nr_throttled", cfs_b->nr_throttled);
  6266. cb->fill(cb, "throttled_time", cfs_b->throttled_time);
  6267. return 0;
  6268. }
  6269. #endif /* CONFIG_CFS_BANDWIDTH */
  6270. #endif /* CONFIG_FAIR_GROUP_SCHED */
  6271. #ifdef CONFIG_RT_GROUP_SCHED
  6272. static int cpu_rt_runtime_write(struct cgroup *cgrp, struct cftype *cft,
  6273. s64 val)
  6274. {
  6275. return sched_group_set_rt_runtime(cgroup_tg(cgrp), val);
  6276. }
  6277. static s64 cpu_rt_runtime_read(struct cgroup *cgrp, struct cftype *cft)
  6278. {
  6279. return sched_group_rt_runtime(cgroup_tg(cgrp));
  6280. }
  6281. static int cpu_rt_period_write_uint(struct cgroup *cgrp, struct cftype *cftype,
  6282. u64 rt_period_us)
  6283. {
  6284. return sched_group_set_rt_period(cgroup_tg(cgrp), rt_period_us);
  6285. }
  6286. static u64 cpu_rt_period_read_uint(struct cgroup *cgrp, struct cftype *cft)
  6287. {
  6288. return sched_group_rt_period(cgroup_tg(cgrp));
  6289. }
  6290. #endif /* CONFIG_RT_GROUP_SCHED */
  6291. static struct cftype cpu_files[] = {
  6292. #ifdef CONFIG_FAIR_GROUP_SCHED
  6293. {
  6294. .name = "shares",
  6295. .read_u64 = cpu_shares_read_u64,
  6296. .write_u64 = cpu_shares_write_u64,
  6297. },
  6298. #endif
  6299. #ifdef CONFIG_CFS_BANDWIDTH
  6300. {
  6301. .name = "cfs_quota_us",
  6302. .read_s64 = cpu_cfs_quota_read_s64,
  6303. .write_s64 = cpu_cfs_quota_write_s64,
  6304. },
  6305. {
  6306. .name = "cfs_period_us",
  6307. .read_u64 = cpu_cfs_period_read_u64,
  6308. .write_u64 = cpu_cfs_period_write_u64,
  6309. },
  6310. {
  6311. .name = "stat",
  6312. .read_map = cpu_stats_show,
  6313. },
  6314. #endif
  6315. #ifdef CONFIG_RT_GROUP_SCHED
  6316. {
  6317. .name = "rt_runtime_us",
  6318. .read_s64 = cpu_rt_runtime_read,
  6319. .write_s64 = cpu_rt_runtime_write,
  6320. },
  6321. {
  6322. .name = "rt_period_us",
  6323. .read_u64 = cpu_rt_period_read_uint,
  6324. .write_u64 = cpu_rt_period_write_uint,
  6325. },
  6326. #endif
  6327. { } /* terminate */
  6328. };
  6329. struct cgroup_subsys cpu_cgroup_subsys = {
  6330. .name = "cpu",
  6331. .css_alloc = cpu_cgroup_css_alloc,
  6332. .css_free = cpu_cgroup_css_free,
  6333. .css_online = cpu_cgroup_css_online,
  6334. .css_offline = cpu_cgroup_css_offline,
  6335. .can_attach = cpu_cgroup_can_attach,
  6336. .attach = cpu_cgroup_attach,
  6337. .exit = cpu_cgroup_exit,
  6338. .subsys_id = cpu_cgroup_subsys_id,
  6339. .base_cftypes = cpu_files,
  6340. .early_init = 1,
  6341. };
  6342. #endif /* CONFIG_CGROUP_SCHED */
  6343. void dump_cpu_task(int cpu)
  6344. {
  6345. pr_info("Task dump for CPU %d:\n", cpu);
  6346. sched_show_task(cpu_curr(cpu));
  6347. }