core.c 190 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195
  1. /*
  2. * Performance events core code:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2011 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2011 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  7. * Copyright © 2009 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
  8. *
  9. * For licensing details see kernel-base/COPYING
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/mm.h>
  13. #include <linux/cpu.h>
  14. #include <linux/smp.h>
  15. #include <linux/idr.h>
  16. #include <linux/file.h>
  17. #include <linux/poll.h>
  18. #include <linux/slab.h>
  19. #include <linux/hash.h>
  20. #include <linux/tick.h>
  21. #include <linux/sysfs.h>
  22. #include <linux/dcache.h>
  23. #include <linux/percpu.h>
  24. #include <linux/ptrace.h>
  25. #include <linux/reboot.h>
  26. #include <linux/vmstat.h>
  27. #include <linux/device.h>
  28. #include <linux/export.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/hardirq.h>
  31. #include <linux/rculist.h>
  32. #include <linux/uaccess.h>
  33. #include <linux/syscalls.h>
  34. #include <linux/anon_inodes.h>
  35. #include <linux/kernel_stat.h>
  36. #include <linux/perf_event.h>
  37. #include <linux/ftrace_event.h>
  38. #include <linux/hw_breakpoint.h>
  39. #include <linux/mm_types.h>
  40. #include <linux/cgroup.h>
  41. #include <linux/module.h>
  42. #include <linux/mman.h>
  43. #include <linux/compat.h>
  44. #include "internal.h"
  45. #include <asm/irq_regs.h>
  46. struct remote_function_call {
  47. struct task_struct *p;
  48. int (*func)(void *info);
  49. void *info;
  50. int ret;
  51. };
  52. static void remote_function(void *data)
  53. {
  54. struct remote_function_call *tfc = data;
  55. struct task_struct *p = tfc->p;
  56. if (p) {
  57. tfc->ret = -EAGAIN;
  58. if (task_cpu(p) != smp_processor_id() || !task_curr(p))
  59. return;
  60. }
  61. tfc->ret = tfc->func(tfc->info);
  62. }
  63. /**
  64. * task_function_call - call a function on the cpu on which a task runs
  65. * @p: the task to evaluate
  66. * @func: the function to be called
  67. * @info: the function call argument
  68. *
  69. * Calls the function @func when the task is currently running. This might
  70. * be on the current CPU, which just calls the function directly
  71. *
  72. * returns: @func return value, or
  73. * -ESRCH - when the process isn't running
  74. * -EAGAIN - when the process moved away
  75. */
  76. static int
  77. task_function_call(struct task_struct *p, int (*func) (void *info), void *info)
  78. {
  79. struct remote_function_call data = {
  80. .p = p,
  81. .func = func,
  82. .info = info,
  83. .ret = -ESRCH, /* No such (running) process */
  84. };
  85. if (task_curr(p))
  86. smp_call_function_single(task_cpu(p), remote_function, &data, 1);
  87. return data.ret;
  88. }
  89. /**
  90. * cpu_function_call - call a function on the cpu
  91. * @func: the function to be called
  92. * @info: the function call argument
  93. *
  94. * Calls the function @func on the remote cpu.
  95. *
  96. * returns: @func return value or -ENXIO when the cpu is offline
  97. */
  98. static int cpu_function_call(int cpu, int (*func) (void *info), void *info)
  99. {
  100. struct remote_function_call data = {
  101. .p = NULL,
  102. .func = func,
  103. .info = info,
  104. .ret = -ENXIO, /* No such CPU */
  105. };
  106. smp_call_function_single(cpu, remote_function, &data, 1);
  107. return data.ret;
  108. }
  109. #define PERF_FLAG_ALL (PERF_FLAG_FD_NO_GROUP |\
  110. PERF_FLAG_FD_OUTPUT |\
  111. PERF_FLAG_PID_CGROUP |\
  112. PERF_FLAG_FD_CLOEXEC)
  113. /*
  114. * branch priv levels that need permission checks
  115. */
  116. #define PERF_SAMPLE_BRANCH_PERM_PLM \
  117. (PERF_SAMPLE_BRANCH_KERNEL |\
  118. PERF_SAMPLE_BRANCH_HV)
  119. enum event_type_t {
  120. EVENT_FLEXIBLE = 0x1,
  121. EVENT_PINNED = 0x2,
  122. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  123. };
  124. /*
  125. * perf_sched_events : >0 events exist
  126. * perf_cgroup_events: >0 per-cpu cgroup events exist on this cpu
  127. */
  128. struct static_key_deferred perf_sched_events __read_mostly;
  129. static DEFINE_PER_CPU(atomic_t, perf_cgroup_events);
  130. static DEFINE_PER_CPU(atomic_t, perf_branch_stack_events);
  131. static atomic_t nr_mmap_events __read_mostly;
  132. static atomic_t nr_comm_events __read_mostly;
  133. static atomic_t nr_task_events __read_mostly;
  134. static atomic_t nr_freq_events __read_mostly;
  135. static LIST_HEAD(pmus);
  136. static DEFINE_MUTEX(pmus_lock);
  137. static struct srcu_struct pmus_srcu;
  138. /*
  139. * perf event paranoia level:
  140. * -1 - not paranoid at all
  141. * 0 - disallow raw tracepoint access for unpriv
  142. * 1 - disallow cpu events for unpriv
  143. * 2 - disallow kernel profiling for unpriv
  144. */
  145. int sysctl_perf_event_paranoid __read_mostly = 1;
  146. /* Minimum for 512 kiB + 1 user control page */
  147. int sysctl_perf_event_mlock __read_mostly = 512 + (PAGE_SIZE / 1024); /* 'free' kiB per user */
  148. /*
  149. * max perf event sample rate
  150. */
  151. #define DEFAULT_MAX_SAMPLE_RATE 100000
  152. #define DEFAULT_SAMPLE_PERIOD_NS (NSEC_PER_SEC / DEFAULT_MAX_SAMPLE_RATE)
  153. #define DEFAULT_CPU_TIME_MAX_PERCENT 25
  154. int sysctl_perf_event_sample_rate __read_mostly = DEFAULT_MAX_SAMPLE_RATE;
  155. static int max_samples_per_tick __read_mostly = DIV_ROUND_UP(DEFAULT_MAX_SAMPLE_RATE, HZ);
  156. static int perf_sample_period_ns __read_mostly = DEFAULT_SAMPLE_PERIOD_NS;
  157. static int perf_sample_allowed_ns __read_mostly =
  158. DEFAULT_SAMPLE_PERIOD_NS * DEFAULT_CPU_TIME_MAX_PERCENT / 100;
  159. void update_perf_cpu_limits(void)
  160. {
  161. u64 tmp = perf_sample_period_ns;
  162. tmp *= sysctl_perf_cpu_time_max_percent;
  163. do_div(tmp, 100);
  164. ACCESS_ONCE(perf_sample_allowed_ns) = tmp;
  165. }
  166. static int perf_rotate_context(struct perf_cpu_context *cpuctx);
  167. int perf_proc_update_handler(struct ctl_table *table, int write,
  168. void __user *buffer, size_t *lenp,
  169. loff_t *ppos)
  170. {
  171. int ret = proc_dointvec_minmax(table, write, buffer, lenp, ppos);
  172. if (ret || !write)
  173. return ret;
  174. max_samples_per_tick = DIV_ROUND_UP(sysctl_perf_event_sample_rate, HZ);
  175. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  176. update_perf_cpu_limits();
  177. return 0;
  178. }
  179. int sysctl_perf_cpu_time_max_percent __read_mostly = DEFAULT_CPU_TIME_MAX_PERCENT;
  180. int perf_cpu_time_max_percent_handler(struct ctl_table *table, int write,
  181. void __user *buffer, size_t *lenp,
  182. loff_t *ppos)
  183. {
  184. int ret = proc_dointvec(table, write, buffer, lenp, ppos);
  185. if (ret || !write)
  186. return ret;
  187. update_perf_cpu_limits();
  188. return 0;
  189. }
  190. /*
  191. * perf samples are done in some very critical code paths (NMIs).
  192. * If they take too much CPU time, the system can lock up and not
  193. * get any real work done. This will drop the sample rate when
  194. * we detect that events are taking too long.
  195. */
  196. #define NR_ACCUMULATED_SAMPLES 128
  197. static DEFINE_PER_CPU(u64, running_sample_length);
  198. static void perf_duration_warn(struct irq_work *w)
  199. {
  200. u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
  201. u64 avg_local_sample_len;
  202. u64 local_samples_len;
  203. local_samples_len = __get_cpu_var(running_sample_length);
  204. avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
  205. printk_ratelimited(KERN_WARNING
  206. "perf interrupt took too long (%lld > %lld), lowering "
  207. "kernel.perf_event_max_sample_rate to %d\n",
  208. avg_local_sample_len, allowed_ns >> 1,
  209. sysctl_perf_event_sample_rate);
  210. }
  211. static DEFINE_IRQ_WORK(perf_duration_work, perf_duration_warn);
  212. void perf_sample_event_took(u64 sample_len_ns)
  213. {
  214. u64 allowed_ns = ACCESS_ONCE(perf_sample_allowed_ns);
  215. u64 avg_local_sample_len;
  216. u64 local_samples_len;
  217. if (allowed_ns == 0)
  218. return;
  219. /* decay the counter by 1 average sample */
  220. local_samples_len = __get_cpu_var(running_sample_length);
  221. local_samples_len -= local_samples_len/NR_ACCUMULATED_SAMPLES;
  222. local_samples_len += sample_len_ns;
  223. __get_cpu_var(running_sample_length) = local_samples_len;
  224. /*
  225. * note: this will be biased artifically low until we have
  226. * seen NR_ACCUMULATED_SAMPLES. Doing it this way keeps us
  227. * from having to maintain a count.
  228. */
  229. avg_local_sample_len = local_samples_len/NR_ACCUMULATED_SAMPLES;
  230. if (avg_local_sample_len <= allowed_ns)
  231. return;
  232. if (max_samples_per_tick <= 1)
  233. return;
  234. max_samples_per_tick = DIV_ROUND_UP(max_samples_per_tick, 2);
  235. sysctl_perf_event_sample_rate = max_samples_per_tick * HZ;
  236. perf_sample_period_ns = NSEC_PER_SEC / sysctl_perf_event_sample_rate;
  237. update_perf_cpu_limits();
  238. if (!irq_work_queue(&perf_duration_work)) {
  239. early_printk("perf interrupt took too long (%lld > %lld), lowering "
  240. "kernel.perf_event_max_sample_rate to %d\n",
  241. avg_local_sample_len, allowed_ns >> 1,
  242. sysctl_perf_event_sample_rate);
  243. }
  244. }
  245. static atomic64_t perf_event_id;
  246. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  247. enum event_type_t event_type);
  248. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  249. enum event_type_t event_type,
  250. struct task_struct *task);
  251. static void update_context_time(struct perf_event_context *ctx);
  252. static u64 perf_event_time(struct perf_event *event);
  253. void __weak perf_event_print_debug(void) { }
  254. extern __weak const char *perf_pmu_name(void)
  255. {
  256. return "pmu";
  257. }
  258. static inline u64 perf_clock(void)
  259. {
  260. return local_clock();
  261. }
  262. static inline struct perf_cpu_context *
  263. __get_cpu_context(struct perf_event_context *ctx)
  264. {
  265. return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  266. }
  267. static void perf_ctx_lock(struct perf_cpu_context *cpuctx,
  268. struct perf_event_context *ctx)
  269. {
  270. raw_spin_lock(&cpuctx->ctx.lock);
  271. if (ctx)
  272. raw_spin_lock(&ctx->lock);
  273. }
  274. static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
  275. struct perf_event_context *ctx)
  276. {
  277. if (ctx)
  278. raw_spin_unlock(&ctx->lock);
  279. raw_spin_unlock(&cpuctx->ctx.lock);
  280. }
  281. #ifdef CONFIG_CGROUP_PERF
  282. /*
  283. * perf_cgroup_info keeps track of time_enabled for a cgroup.
  284. * This is a per-cpu dynamically allocated data structure.
  285. */
  286. struct perf_cgroup_info {
  287. u64 time;
  288. u64 timestamp;
  289. };
  290. struct perf_cgroup {
  291. struct cgroup_subsys_state css;
  292. struct perf_cgroup_info __percpu *info;
  293. };
  294. /*
  295. * Must ensure cgroup is pinned (css_get) before calling
  296. * this function. In other words, we cannot call this function
  297. * if there is no cgroup event for the current CPU context.
  298. */
  299. static inline struct perf_cgroup *
  300. perf_cgroup_from_task(struct task_struct *task)
  301. {
  302. return container_of(task_css(task, perf_event_cgrp_id),
  303. struct perf_cgroup, css);
  304. }
  305. static inline bool
  306. perf_cgroup_match(struct perf_event *event)
  307. {
  308. struct perf_event_context *ctx = event->ctx;
  309. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  310. /* @event doesn't care about cgroup */
  311. if (!event->cgrp)
  312. return true;
  313. /* wants specific cgroup scope but @cpuctx isn't associated with any */
  314. if (!cpuctx->cgrp)
  315. return false;
  316. /*
  317. * Cgroup scoping is recursive. An event enabled for a cgroup is
  318. * also enabled for all its descendant cgroups. If @cpuctx's
  319. * cgroup is a descendant of @event's (the test covers identity
  320. * case), it's a match.
  321. */
  322. return cgroup_is_descendant(cpuctx->cgrp->css.cgroup,
  323. event->cgrp->css.cgroup);
  324. }
  325. static inline void perf_put_cgroup(struct perf_event *event)
  326. {
  327. css_put(&event->cgrp->css);
  328. }
  329. static inline void perf_detach_cgroup(struct perf_event *event)
  330. {
  331. perf_put_cgroup(event);
  332. event->cgrp = NULL;
  333. }
  334. static inline int is_cgroup_event(struct perf_event *event)
  335. {
  336. return event->cgrp != NULL;
  337. }
  338. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  339. {
  340. struct perf_cgroup_info *t;
  341. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  342. return t->time;
  343. }
  344. static inline void __update_cgrp_time(struct perf_cgroup *cgrp)
  345. {
  346. struct perf_cgroup_info *info;
  347. u64 now;
  348. now = perf_clock();
  349. info = this_cpu_ptr(cgrp->info);
  350. info->time += now - info->timestamp;
  351. info->timestamp = now;
  352. }
  353. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  354. {
  355. struct perf_cgroup *cgrp_out = cpuctx->cgrp;
  356. if (cgrp_out)
  357. __update_cgrp_time(cgrp_out);
  358. }
  359. static inline void update_cgrp_time_from_event(struct perf_event *event)
  360. {
  361. struct perf_cgroup *cgrp;
  362. /*
  363. * ensure we access cgroup data only when needed and
  364. * when we know the cgroup is pinned (css_get)
  365. */
  366. if (!is_cgroup_event(event))
  367. return;
  368. cgrp = perf_cgroup_from_task(current);
  369. /*
  370. * Do not update time when cgroup is not active
  371. */
  372. if (cgrp == event->cgrp)
  373. __update_cgrp_time(event->cgrp);
  374. }
  375. static inline void
  376. perf_cgroup_set_timestamp(struct task_struct *task,
  377. struct perf_event_context *ctx)
  378. {
  379. struct perf_cgroup *cgrp;
  380. struct perf_cgroup_info *info;
  381. /*
  382. * ctx->lock held by caller
  383. * ensure we do not access cgroup data
  384. * unless we have the cgroup pinned (css_get)
  385. */
  386. if (!task || !ctx->nr_cgroups)
  387. return;
  388. cgrp = perf_cgroup_from_task(task);
  389. info = this_cpu_ptr(cgrp->info);
  390. info->timestamp = ctx->timestamp;
  391. }
  392. #define PERF_CGROUP_SWOUT 0x1 /* cgroup switch out every event */
  393. #define PERF_CGROUP_SWIN 0x2 /* cgroup switch in events based on task */
  394. /*
  395. * reschedule events based on the cgroup constraint of task.
  396. *
  397. * mode SWOUT : schedule out everything
  398. * mode SWIN : schedule in based on cgroup for next
  399. */
  400. void perf_cgroup_switch(struct task_struct *task, int mode)
  401. {
  402. struct perf_cpu_context *cpuctx;
  403. struct pmu *pmu;
  404. unsigned long flags;
  405. /*
  406. * disable interrupts to avoid geting nr_cgroup
  407. * changes via __perf_event_disable(). Also
  408. * avoids preemption.
  409. */
  410. local_irq_save(flags);
  411. /*
  412. * we reschedule only in the presence of cgroup
  413. * constrained events.
  414. */
  415. rcu_read_lock();
  416. list_for_each_entry_rcu(pmu, &pmus, entry) {
  417. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  418. if (cpuctx->unique_pmu != pmu)
  419. continue; /* ensure we process each cpuctx once */
  420. /*
  421. * perf_cgroup_events says at least one
  422. * context on this CPU has cgroup events.
  423. *
  424. * ctx->nr_cgroups reports the number of cgroup
  425. * events for a context.
  426. */
  427. if (cpuctx->ctx.nr_cgroups > 0) {
  428. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  429. perf_pmu_disable(cpuctx->ctx.pmu);
  430. if (mode & PERF_CGROUP_SWOUT) {
  431. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  432. /*
  433. * must not be done before ctxswout due
  434. * to event_filter_match() in event_sched_out()
  435. */
  436. cpuctx->cgrp = NULL;
  437. }
  438. if (mode & PERF_CGROUP_SWIN) {
  439. WARN_ON_ONCE(cpuctx->cgrp);
  440. /*
  441. * set cgrp before ctxsw in to allow
  442. * event_filter_match() to not have to pass
  443. * task around
  444. */
  445. cpuctx->cgrp = perf_cgroup_from_task(task);
  446. cpu_ctx_sched_in(cpuctx, EVENT_ALL, task);
  447. }
  448. perf_pmu_enable(cpuctx->ctx.pmu);
  449. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  450. }
  451. }
  452. rcu_read_unlock();
  453. local_irq_restore(flags);
  454. }
  455. static inline void perf_cgroup_sched_out(struct task_struct *task,
  456. struct task_struct *next)
  457. {
  458. struct perf_cgroup *cgrp1;
  459. struct perf_cgroup *cgrp2 = NULL;
  460. /*
  461. * we come here when we know perf_cgroup_events > 0
  462. */
  463. cgrp1 = perf_cgroup_from_task(task);
  464. /*
  465. * next is NULL when called from perf_event_enable_on_exec()
  466. * that will systematically cause a cgroup_switch()
  467. */
  468. if (next)
  469. cgrp2 = perf_cgroup_from_task(next);
  470. /*
  471. * only schedule out current cgroup events if we know
  472. * that we are switching to a different cgroup. Otherwise,
  473. * do no touch the cgroup events.
  474. */
  475. if (cgrp1 != cgrp2)
  476. perf_cgroup_switch(task, PERF_CGROUP_SWOUT);
  477. }
  478. static inline void perf_cgroup_sched_in(struct task_struct *prev,
  479. struct task_struct *task)
  480. {
  481. struct perf_cgroup *cgrp1;
  482. struct perf_cgroup *cgrp2 = NULL;
  483. /*
  484. * we come here when we know perf_cgroup_events > 0
  485. */
  486. cgrp1 = perf_cgroup_from_task(task);
  487. /* prev can never be NULL */
  488. cgrp2 = perf_cgroup_from_task(prev);
  489. /*
  490. * only need to schedule in cgroup events if we are changing
  491. * cgroup during ctxsw. Cgroup events were not scheduled
  492. * out of ctxsw out if that was not the case.
  493. */
  494. if (cgrp1 != cgrp2)
  495. perf_cgroup_switch(task, PERF_CGROUP_SWIN);
  496. }
  497. static inline int perf_cgroup_connect(int fd, struct perf_event *event,
  498. struct perf_event_attr *attr,
  499. struct perf_event *group_leader)
  500. {
  501. struct perf_cgroup *cgrp;
  502. struct cgroup_subsys_state *css;
  503. struct fd f = fdget(fd);
  504. int ret = 0;
  505. if (!f.file)
  506. return -EBADF;
  507. css = css_tryget_online_from_dir(f.file->f_dentry,
  508. &perf_event_cgrp_subsys);
  509. if (IS_ERR(css)) {
  510. ret = PTR_ERR(css);
  511. goto out;
  512. }
  513. cgrp = container_of(css, struct perf_cgroup, css);
  514. event->cgrp = cgrp;
  515. /*
  516. * all events in a group must monitor
  517. * the same cgroup because a task belongs
  518. * to only one perf cgroup at a time
  519. */
  520. if (group_leader && group_leader->cgrp != cgrp) {
  521. perf_detach_cgroup(event);
  522. ret = -EINVAL;
  523. }
  524. out:
  525. fdput(f);
  526. return ret;
  527. }
  528. static inline void
  529. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  530. {
  531. struct perf_cgroup_info *t;
  532. t = per_cpu_ptr(event->cgrp->info, event->cpu);
  533. event->shadow_ctx_time = now - t->timestamp;
  534. }
  535. static inline void
  536. perf_cgroup_defer_enabled(struct perf_event *event)
  537. {
  538. /*
  539. * when the current task's perf cgroup does not match
  540. * the event's, we need to remember to call the
  541. * perf_mark_enable() function the first time a task with
  542. * a matching perf cgroup is scheduled in.
  543. */
  544. if (is_cgroup_event(event) && !perf_cgroup_match(event))
  545. event->cgrp_defer_enabled = 1;
  546. }
  547. static inline void
  548. perf_cgroup_mark_enabled(struct perf_event *event,
  549. struct perf_event_context *ctx)
  550. {
  551. struct perf_event *sub;
  552. u64 tstamp = perf_event_time(event);
  553. if (!event->cgrp_defer_enabled)
  554. return;
  555. event->cgrp_defer_enabled = 0;
  556. event->tstamp_enabled = tstamp - event->total_time_enabled;
  557. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  558. if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
  559. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  560. sub->cgrp_defer_enabled = 0;
  561. }
  562. }
  563. }
  564. #else /* !CONFIG_CGROUP_PERF */
  565. static inline bool
  566. perf_cgroup_match(struct perf_event *event)
  567. {
  568. return true;
  569. }
  570. static inline void perf_detach_cgroup(struct perf_event *event)
  571. {}
  572. static inline int is_cgroup_event(struct perf_event *event)
  573. {
  574. return 0;
  575. }
  576. static inline u64 perf_cgroup_event_cgrp_time(struct perf_event *event)
  577. {
  578. return 0;
  579. }
  580. static inline void update_cgrp_time_from_event(struct perf_event *event)
  581. {
  582. }
  583. static inline void update_cgrp_time_from_cpuctx(struct perf_cpu_context *cpuctx)
  584. {
  585. }
  586. static inline void perf_cgroup_sched_out(struct task_struct *task,
  587. struct task_struct *next)
  588. {
  589. }
  590. static inline void perf_cgroup_sched_in(struct task_struct *prev,
  591. struct task_struct *task)
  592. {
  593. }
  594. static inline int perf_cgroup_connect(pid_t pid, struct perf_event *event,
  595. struct perf_event_attr *attr,
  596. struct perf_event *group_leader)
  597. {
  598. return -EINVAL;
  599. }
  600. static inline void
  601. perf_cgroup_set_timestamp(struct task_struct *task,
  602. struct perf_event_context *ctx)
  603. {
  604. }
  605. void
  606. perf_cgroup_switch(struct task_struct *task, struct task_struct *next)
  607. {
  608. }
  609. static inline void
  610. perf_cgroup_set_shadow_time(struct perf_event *event, u64 now)
  611. {
  612. }
  613. static inline u64 perf_cgroup_event_time(struct perf_event *event)
  614. {
  615. return 0;
  616. }
  617. static inline void
  618. perf_cgroup_defer_enabled(struct perf_event *event)
  619. {
  620. }
  621. static inline void
  622. perf_cgroup_mark_enabled(struct perf_event *event,
  623. struct perf_event_context *ctx)
  624. {
  625. }
  626. #endif
  627. /*
  628. * set default to be dependent on timer tick just
  629. * like original code
  630. */
  631. #define PERF_CPU_HRTIMER (1000 / HZ)
  632. /*
  633. * function must be called with interrupts disbled
  634. */
  635. static enum hrtimer_restart perf_cpu_hrtimer_handler(struct hrtimer *hr)
  636. {
  637. struct perf_cpu_context *cpuctx;
  638. enum hrtimer_restart ret = HRTIMER_NORESTART;
  639. int rotations = 0;
  640. WARN_ON(!irqs_disabled());
  641. cpuctx = container_of(hr, struct perf_cpu_context, hrtimer);
  642. rotations = perf_rotate_context(cpuctx);
  643. /*
  644. * arm timer if needed
  645. */
  646. if (rotations) {
  647. hrtimer_forward_now(hr, cpuctx->hrtimer_interval);
  648. ret = HRTIMER_RESTART;
  649. }
  650. return ret;
  651. }
  652. /* CPU is going down */
  653. void perf_cpu_hrtimer_cancel(int cpu)
  654. {
  655. struct perf_cpu_context *cpuctx;
  656. struct pmu *pmu;
  657. unsigned long flags;
  658. if (WARN_ON(cpu != smp_processor_id()))
  659. return;
  660. local_irq_save(flags);
  661. rcu_read_lock();
  662. list_for_each_entry_rcu(pmu, &pmus, entry) {
  663. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  664. if (pmu->task_ctx_nr == perf_sw_context)
  665. continue;
  666. hrtimer_cancel(&cpuctx->hrtimer);
  667. }
  668. rcu_read_unlock();
  669. local_irq_restore(flags);
  670. }
  671. static void __perf_cpu_hrtimer_init(struct perf_cpu_context *cpuctx, int cpu)
  672. {
  673. struct hrtimer *hr = &cpuctx->hrtimer;
  674. struct pmu *pmu = cpuctx->ctx.pmu;
  675. int timer;
  676. /* no multiplexing needed for SW PMU */
  677. if (pmu->task_ctx_nr == perf_sw_context)
  678. return;
  679. /*
  680. * check default is sane, if not set then force to
  681. * default interval (1/tick)
  682. */
  683. timer = pmu->hrtimer_interval_ms;
  684. if (timer < 1)
  685. timer = pmu->hrtimer_interval_ms = PERF_CPU_HRTIMER;
  686. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
  687. hrtimer_init(hr, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
  688. hr->function = perf_cpu_hrtimer_handler;
  689. }
  690. static void perf_cpu_hrtimer_restart(struct perf_cpu_context *cpuctx)
  691. {
  692. struct hrtimer *hr = &cpuctx->hrtimer;
  693. struct pmu *pmu = cpuctx->ctx.pmu;
  694. /* not for SW PMU */
  695. if (pmu->task_ctx_nr == perf_sw_context)
  696. return;
  697. if (hrtimer_active(hr))
  698. return;
  699. if (!hrtimer_callback_running(hr))
  700. __hrtimer_start_range_ns(hr, cpuctx->hrtimer_interval,
  701. 0, HRTIMER_MODE_REL_PINNED, 0);
  702. }
  703. void perf_pmu_disable(struct pmu *pmu)
  704. {
  705. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  706. if (!(*count)++)
  707. pmu->pmu_disable(pmu);
  708. }
  709. void perf_pmu_enable(struct pmu *pmu)
  710. {
  711. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  712. if (!--(*count))
  713. pmu->pmu_enable(pmu);
  714. }
  715. static DEFINE_PER_CPU(struct list_head, rotation_list);
  716. /*
  717. * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
  718. * because they're strictly cpu affine and rotate_start is called with IRQs
  719. * disabled, while rotate_context is called from IRQ context.
  720. */
  721. static void perf_pmu_rotate_start(struct pmu *pmu)
  722. {
  723. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  724. struct list_head *head = &__get_cpu_var(rotation_list);
  725. WARN_ON(!irqs_disabled());
  726. if (list_empty(&cpuctx->rotation_list))
  727. list_add(&cpuctx->rotation_list, head);
  728. }
  729. static void get_ctx(struct perf_event_context *ctx)
  730. {
  731. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  732. }
  733. static void put_ctx(struct perf_event_context *ctx)
  734. {
  735. if (atomic_dec_and_test(&ctx->refcount)) {
  736. if (ctx->parent_ctx)
  737. put_ctx(ctx->parent_ctx);
  738. if (ctx->task)
  739. put_task_struct(ctx->task);
  740. kfree_rcu(ctx, rcu_head);
  741. }
  742. }
  743. static void unclone_ctx(struct perf_event_context *ctx)
  744. {
  745. if (ctx->parent_ctx) {
  746. put_ctx(ctx->parent_ctx);
  747. ctx->parent_ctx = NULL;
  748. }
  749. ctx->generation++;
  750. }
  751. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  752. {
  753. /*
  754. * only top level events have the pid namespace they were created in
  755. */
  756. if (event->parent)
  757. event = event->parent;
  758. return task_tgid_nr_ns(p, event->ns);
  759. }
  760. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  761. {
  762. /*
  763. * only top level events have the pid namespace they were created in
  764. */
  765. if (event->parent)
  766. event = event->parent;
  767. return task_pid_nr_ns(p, event->ns);
  768. }
  769. /*
  770. * If we inherit events we want to return the parent event id
  771. * to userspace.
  772. */
  773. static u64 primary_event_id(struct perf_event *event)
  774. {
  775. u64 id = event->id;
  776. if (event->parent)
  777. id = event->parent->id;
  778. return id;
  779. }
  780. /*
  781. * Get the perf_event_context for a task and lock it.
  782. * This has to cope with with the fact that until it is locked,
  783. * the context could get moved to another task.
  784. */
  785. static struct perf_event_context *
  786. perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
  787. {
  788. struct perf_event_context *ctx;
  789. retry:
  790. /*
  791. * One of the few rules of preemptible RCU is that one cannot do
  792. * rcu_read_unlock() while holding a scheduler (or nested) lock when
  793. * part of the read side critical section was preemptible -- see
  794. * rcu_read_unlock_special().
  795. *
  796. * Since ctx->lock nests under rq->lock we must ensure the entire read
  797. * side critical section is non-preemptible.
  798. */
  799. preempt_disable();
  800. rcu_read_lock();
  801. ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
  802. if (ctx) {
  803. /*
  804. * If this context is a clone of another, it might
  805. * get swapped for another underneath us by
  806. * perf_event_task_sched_out, though the
  807. * rcu_read_lock() protects us from any context
  808. * getting freed. Lock the context and check if it
  809. * got swapped before we could get the lock, and retry
  810. * if so. If we locked the right context, then it
  811. * can't get swapped on us any more.
  812. */
  813. raw_spin_lock_irqsave(&ctx->lock, *flags);
  814. if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
  815. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  816. rcu_read_unlock();
  817. preempt_enable();
  818. goto retry;
  819. }
  820. if (!atomic_inc_not_zero(&ctx->refcount)) {
  821. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  822. ctx = NULL;
  823. }
  824. }
  825. rcu_read_unlock();
  826. preempt_enable();
  827. return ctx;
  828. }
  829. /*
  830. * Get the context for a task and increment its pin_count so it
  831. * can't get swapped to another task. This also increments its
  832. * reference count so that the context can't get freed.
  833. */
  834. static struct perf_event_context *
  835. perf_pin_task_context(struct task_struct *task, int ctxn)
  836. {
  837. struct perf_event_context *ctx;
  838. unsigned long flags;
  839. ctx = perf_lock_task_context(task, ctxn, &flags);
  840. if (ctx) {
  841. ++ctx->pin_count;
  842. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  843. }
  844. return ctx;
  845. }
  846. static void perf_unpin_context(struct perf_event_context *ctx)
  847. {
  848. unsigned long flags;
  849. raw_spin_lock_irqsave(&ctx->lock, flags);
  850. --ctx->pin_count;
  851. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  852. }
  853. /*
  854. * Update the record of the current time in a context.
  855. */
  856. static void update_context_time(struct perf_event_context *ctx)
  857. {
  858. u64 now = perf_clock();
  859. ctx->time += now - ctx->timestamp;
  860. ctx->timestamp = now;
  861. }
  862. static u64 perf_event_time(struct perf_event *event)
  863. {
  864. struct perf_event_context *ctx = event->ctx;
  865. if (is_cgroup_event(event))
  866. return perf_cgroup_event_time(event);
  867. return ctx ? ctx->time : 0;
  868. }
  869. /*
  870. * Update the total_time_enabled and total_time_running fields for a event.
  871. * The caller of this function needs to hold the ctx->lock.
  872. */
  873. static void update_event_times(struct perf_event *event)
  874. {
  875. struct perf_event_context *ctx = event->ctx;
  876. u64 run_end;
  877. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  878. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  879. return;
  880. /*
  881. * in cgroup mode, time_enabled represents
  882. * the time the event was enabled AND active
  883. * tasks were in the monitored cgroup. This is
  884. * independent of the activity of the context as
  885. * there may be a mix of cgroup and non-cgroup events.
  886. *
  887. * That is why we treat cgroup events differently
  888. * here.
  889. */
  890. if (is_cgroup_event(event))
  891. run_end = perf_cgroup_event_time(event);
  892. else if (ctx->is_active)
  893. run_end = ctx->time;
  894. else
  895. run_end = event->tstamp_stopped;
  896. event->total_time_enabled = run_end - event->tstamp_enabled;
  897. if (event->state == PERF_EVENT_STATE_INACTIVE)
  898. run_end = event->tstamp_stopped;
  899. else
  900. run_end = perf_event_time(event);
  901. event->total_time_running = run_end - event->tstamp_running;
  902. }
  903. /*
  904. * Update total_time_enabled and total_time_running for all events in a group.
  905. */
  906. static void update_group_times(struct perf_event *leader)
  907. {
  908. struct perf_event *event;
  909. update_event_times(leader);
  910. list_for_each_entry(event, &leader->sibling_list, group_entry)
  911. update_event_times(event);
  912. }
  913. static struct list_head *
  914. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  915. {
  916. if (event->attr.pinned)
  917. return &ctx->pinned_groups;
  918. else
  919. return &ctx->flexible_groups;
  920. }
  921. /*
  922. * Add a event from the lists for its context.
  923. * Must be called with ctx->mutex and ctx->lock held.
  924. */
  925. static void
  926. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  927. {
  928. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  929. event->attach_state |= PERF_ATTACH_CONTEXT;
  930. /*
  931. * If we're a stand alone event or group leader, we go to the context
  932. * list, group events are kept attached to the group so that
  933. * perf_group_detach can, at all times, locate all siblings.
  934. */
  935. if (event->group_leader == event) {
  936. struct list_head *list;
  937. if (is_software_event(event))
  938. event->group_flags |= PERF_GROUP_SOFTWARE;
  939. list = ctx_group_list(event, ctx);
  940. list_add_tail(&event->group_entry, list);
  941. }
  942. if (is_cgroup_event(event))
  943. ctx->nr_cgroups++;
  944. if (has_branch_stack(event))
  945. ctx->nr_branch_stack++;
  946. list_add_rcu(&event->event_entry, &ctx->event_list);
  947. if (!ctx->nr_events)
  948. perf_pmu_rotate_start(ctx->pmu);
  949. ctx->nr_events++;
  950. if (event->attr.inherit_stat)
  951. ctx->nr_stat++;
  952. ctx->generation++;
  953. }
  954. /*
  955. * Initialize event state based on the perf_event_attr::disabled.
  956. */
  957. static inline void perf_event__state_init(struct perf_event *event)
  958. {
  959. event->state = event->attr.disabled ? PERF_EVENT_STATE_OFF :
  960. PERF_EVENT_STATE_INACTIVE;
  961. }
  962. /*
  963. * Called at perf_event creation and when events are attached/detached from a
  964. * group.
  965. */
  966. static void perf_event__read_size(struct perf_event *event)
  967. {
  968. int entry = sizeof(u64); /* value */
  969. int size = 0;
  970. int nr = 1;
  971. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  972. size += sizeof(u64);
  973. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  974. size += sizeof(u64);
  975. if (event->attr.read_format & PERF_FORMAT_ID)
  976. entry += sizeof(u64);
  977. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  978. nr += event->group_leader->nr_siblings;
  979. size += sizeof(u64);
  980. }
  981. size += entry * nr;
  982. event->read_size = size;
  983. }
  984. static void perf_event__header_size(struct perf_event *event)
  985. {
  986. struct perf_sample_data *data;
  987. u64 sample_type = event->attr.sample_type;
  988. u16 size = 0;
  989. perf_event__read_size(event);
  990. if (sample_type & PERF_SAMPLE_IP)
  991. size += sizeof(data->ip);
  992. if (sample_type & PERF_SAMPLE_ADDR)
  993. size += sizeof(data->addr);
  994. if (sample_type & PERF_SAMPLE_PERIOD)
  995. size += sizeof(data->period);
  996. if (sample_type & PERF_SAMPLE_WEIGHT)
  997. size += sizeof(data->weight);
  998. if (sample_type & PERF_SAMPLE_READ)
  999. size += event->read_size;
  1000. if (sample_type & PERF_SAMPLE_DATA_SRC)
  1001. size += sizeof(data->data_src.val);
  1002. if (sample_type & PERF_SAMPLE_TRANSACTION)
  1003. size += sizeof(data->txn);
  1004. event->header_size = size;
  1005. }
  1006. static void perf_event__id_header_size(struct perf_event *event)
  1007. {
  1008. struct perf_sample_data *data;
  1009. u64 sample_type = event->attr.sample_type;
  1010. u16 size = 0;
  1011. if (sample_type & PERF_SAMPLE_TID)
  1012. size += sizeof(data->tid_entry);
  1013. if (sample_type & PERF_SAMPLE_TIME)
  1014. size += sizeof(data->time);
  1015. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  1016. size += sizeof(data->id);
  1017. if (sample_type & PERF_SAMPLE_ID)
  1018. size += sizeof(data->id);
  1019. if (sample_type & PERF_SAMPLE_STREAM_ID)
  1020. size += sizeof(data->stream_id);
  1021. if (sample_type & PERF_SAMPLE_CPU)
  1022. size += sizeof(data->cpu_entry);
  1023. event->id_header_size = size;
  1024. }
  1025. static void perf_group_attach(struct perf_event *event)
  1026. {
  1027. struct perf_event *group_leader = event->group_leader, *pos;
  1028. /*
  1029. * We can have double attach due to group movement in perf_event_open.
  1030. */
  1031. if (event->attach_state & PERF_ATTACH_GROUP)
  1032. return;
  1033. event->attach_state |= PERF_ATTACH_GROUP;
  1034. if (group_leader == event)
  1035. return;
  1036. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  1037. !is_software_event(event))
  1038. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  1039. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  1040. group_leader->nr_siblings++;
  1041. perf_event__header_size(group_leader);
  1042. list_for_each_entry(pos, &group_leader->sibling_list, group_entry)
  1043. perf_event__header_size(pos);
  1044. }
  1045. /*
  1046. * Remove a event from the lists for its context.
  1047. * Must be called with ctx->mutex and ctx->lock held.
  1048. */
  1049. static void
  1050. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  1051. {
  1052. struct perf_cpu_context *cpuctx;
  1053. /*
  1054. * We can have double detach due to exit/hot-unplug + close.
  1055. */
  1056. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  1057. return;
  1058. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  1059. if (is_cgroup_event(event)) {
  1060. ctx->nr_cgroups--;
  1061. cpuctx = __get_cpu_context(ctx);
  1062. /*
  1063. * if there are no more cgroup events
  1064. * then cler cgrp to avoid stale pointer
  1065. * in update_cgrp_time_from_cpuctx()
  1066. */
  1067. if (!ctx->nr_cgroups)
  1068. cpuctx->cgrp = NULL;
  1069. }
  1070. if (has_branch_stack(event))
  1071. ctx->nr_branch_stack--;
  1072. ctx->nr_events--;
  1073. if (event->attr.inherit_stat)
  1074. ctx->nr_stat--;
  1075. list_del_rcu(&event->event_entry);
  1076. if (event->group_leader == event)
  1077. list_del_init(&event->group_entry);
  1078. update_group_times(event);
  1079. /*
  1080. * If event was in error state, then keep it
  1081. * that way, otherwise bogus counts will be
  1082. * returned on read(). The only way to get out
  1083. * of error state is by explicit re-enabling
  1084. * of the event
  1085. */
  1086. if (event->state > PERF_EVENT_STATE_OFF)
  1087. event->state = PERF_EVENT_STATE_OFF;
  1088. ctx->generation++;
  1089. }
  1090. static void perf_group_detach(struct perf_event *event)
  1091. {
  1092. struct perf_event *sibling, *tmp;
  1093. struct list_head *list = NULL;
  1094. /*
  1095. * We can have double detach due to exit/hot-unplug + close.
  1096. */
  1097. if (!(event->attach_state & PERF_ATTACH_GROUP))
  1098. return;
  1099. event->attach_state &= ~PERF_ATTACH_GROUP;
  1100. /*
  1101. * If this is a sibling, remove it from its group.
  1102. */
  1103. if (event->group_leader != event) {
  1104. list_del_init(&event->group_entry);
  1105. event->group_leader->nr_siblings--;
  1106. goto out;
  1107. }
  1108. if (!list_empty(&event->group_entry))
  1109. list = &event->group_entry;
  1110. /*
  1111. * If this was a group event with sibling events then
  1112. * upgrade the siblings to singleton events by adding them
  1113. * to whatever list we are on.
  1114. */
  1115. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  1116. if (list)
  1117. list_move_tail(&sibling->group_entry, list);
  1118. sibling->group_leader = sibling;
  1119. /* Inherit group flags from the previous leader */
  1120. sibling->group_flags = event->group_flags;
  1121. }
  1122. out:
  1123. perf_event__header_size(event->group_leader);
  1124. list_for_each_entry(tmp, &event->group_leader->sibling_list, group_entry)
  1125. perf_event__header_size(tmp);
  1126. }
  1127. static inline int
  1128. event_filter_match(struct perf_event *event)
  1129. {
  1130. return (event->cpu == -1 || event->cpu == smp_processor_id())
  1131. && perf_cgroup_match(event);
  1132. }
  1133. static void
  1134. event_sched_out(struct perf_event *event,
  1135. struct perf_cpu_context *cpuctx,
  1136. struct perf_event_context *ctx)
  1137. {
  1138. u64 tstamp = perf_event_time(event);
  1139. u64 delta;
  1140. /*
  1141. * An event which could not be activated because of
  1142. * filter mismatch still needs to have its timings
  1143. * maintained, otherwise bogus information is return
  1144. * via read() for time_enabled, time_running:
  1145. */
  1146. if (event->state == PERF_EVENT_STATE_INACTIVE
  1147. && !event_filter_match(event)) {
  1148. delta = tstamp - event->tstamp_stopped;
  1149. event->tstamp_running += delta;
  1150. event->tstamp_stopped = tstamp;
  1151. }
  1152. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1153. return;
  1154. perf_pmu_disable(event->pmu);
  1155. event->state = PERF_EVENT_STATE_INACTIVE;
  1156. if (event->pending_disable) {
  1157. event->pending_disable = 0;
  1158. event->state = PERF_EVENT_STATE_OFF;
  1159. }
  1160. event->tstamp_stopped = tstamp;
  1161. event->pmu->del(event, 0);
  1162. event->oncpu = -1;
  1163. if (!is_software_event(event))
  1164. cpuctx->active_oncpu--;
  1165. ctx->nr_active--;
  1166. if (event->attr.freq && event->attr.sample_freq)
  1167. ctx->nr_freq--;
  1168. if (event->attr.exclusive || !cpuctx->active_oncpu)
  1169. cpuctx->exclusive = 0;
  1170. perf_pmu_enable(event->pmu);
  1171. }
  1172. static void
  1173. group_sched_out(struct perf_event *group_event,
  1174. struct perf_cpu_context *cpuctx,
  1175. struct perf_event_context *ctx)
  1176. {
  1177. struct perf_event *event;
  1178. int state = group_event->state;
  1179. event_sched_out(group_event, cpuctx, ctx);
  1180. /*
  1181. * Schedule out siblings (if any):
  1182. */
  1183. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  1184. event_sched_out(event, cpuctx, ctx);
  1185. if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
  1186. cpuctx->exclusive = 0;
  1187. }
  1188. struct remove_event {
  1189. struct perf_event *event;
  1190. bool detach_group;
  1191. };
  1192. /*
  1193. * Cross CPU call to remove a performance event
  1194. *
  1195. * We disable the event on the hardware level first. After that we
  1196. * remove it from the context list.
  1197. */
  1198. static int __perf_remove_from_context(void *info)
  1199. {
  1200. struct remove_event *re = info;
  1201. struct perf_event *event = re->event;
  1202. struct perf_event_context *ctx = event->ctx;
  1203. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1204. raw_spin_lock(&ctx->lock);
  1205. event_sched_out(event, cpuctx, ctx);
  1206. if (re->detach_group)
  1207. perf_group_detach(event);
  1208. list_del_event(event, ctx);
  1209. if (!ctx->nr_events && cpuctx->task_ctx == ctx) {
  1210. ctx->is_active = 0;
  1211. cpuctx->task_ctx = NULL;
  1212. }
  1213. raw_spin_unlock(&ctx->lock);
  1214. return 0;
  1215. }
  1216. /*
  1217. * Remove the event from a task's (or a CPU's) list of events.
  1218. *
  1219. * CPU events are removed with a smp call. For task events we only
  1220. * call when the task is on a CPU.
  1221. *
  1222. * If event->ctx is a cloned context, callers must make sure that
  1223. * every task struct that event->ctx->task could possibly point to
  1224. * remains valid. This is OK when called from perf_release since
  1225. * that only calls us on the top-level context, which can't be a clone.
  1226. * When called from perf_event_exit_task, it's OK because the
  1227. * context has been detached from its task.
  1228. */
  1229. static void perf_remove_from_context(struct perf_event *event, bool detach_group)
  1230. {
  1231. struct perf_event_context *ctx = event->ctx;
  1232. struct task_struct *task = ctx->task;
  1233. struct remove_event re = {
  1234. .event = event,
  1235. .detach_group = detach_group,
  1236. };
  1237. lockdep_assert_held(&ctx->mutex);
  1238. if (!task) {
  1239. /*
  1240. * Per cpu events are removed via an smp call and
  1241. * the removal is always successful.
  1242. */
  1243. cpu_function_call(event->cpu, __perf_remove_from_context, &re);
  1244. return;
  1245. }
  1246. retry:
  1247. if (!task_function_call(task, __perf_remove_from_context, &re))
  1248. return;
  1249. raw_spin_lock_irq(&ctx->lock);
  1250. /*
  1251. * If we failed to find a running task, but find the context active now
  1252. * that we've acquired the ctx->lock, retry.
  1253. */
  1254. if (ctx->is_active) {
  1255. raw_spin_unlock_irq(&ctx->lock);
  1256. goto retry;
  1257. }
  1258. /*
  1259. * Since the task isn't running, its safe to remove the event, us
  1260. * holding the ctx->lock ensures the task won't get scheduled in.
  1261. */
  1262. if (detach_group)
  1263. perf_group_detach(event);
  1264. list_del_event(event, ctx);
  1265. raw_spin_unlock_irq(&ctx->lock);
  1266. }
  1267. /*
  1268. * Cross CPU call to disable a performance event
  1269. */
  1270. int __perf_event_disable(void *info)
  1271. {
  1272. struct perf_event *event = info;
  1273. struct perf_event_context *ctx = event->ctx;
  1274. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1275. /*
  1276. * If this is a per-task event, need to check whether this
  1277. * event's task is the current task on this cpu.
  1278. *
  1279. * Can trigger due to concurrent perf_event_context_sched_out()
  1280. * flipping contexts around.
  1281. */
  1282. if (ctx->task && cpuctx->task_ctx != ctx)
  1283. return -EINVAL;
  1284. raw_spin_lock(&ctx->lock);
  1285. /*
  1286. * If the event is on, turn it off.
  1287. * If it is in error state, leave it in error state.
  1288. */
  1289. if (event->state >= PERF_EVENT_STATE_INACTIVE) {
  1290. update_context_time(ctx);
  1291. update_cgrp_time_from_event(event);
  1292. update_group_times(event);
  1293. if (event == event->group_leader)
  1294. group_sched_out(event, cpuctx, ctx);
  1295. else
  1296. event_sched_out(event, cpuctx, ctx);
  1297. event->state = PERF_EVENT_STATE_OFF;
  1298. }
  1299. raw_spin_unlock(&ctx->lock);
  1300. return 0;
  1301. }
  1302. /*
  1303. * Disable a event.
  1304. *
  1305. * If event->ctx is a cloned context, callers must make sure that
  1306. * every task struct that event->ctx->task could possibly point to
  1307. * remains valid. This condition is satisifed when called through
  1308. * perf_event_for_each_child or perf_event_for_each because they
  1309. * hold the top-level event's child_mutex, so any descendant that
  1310. * goes to exit will block in sync_child_event.
  1311. * When called from perf_pending_event it's OK because event->ctx
  1312. * is the current context on this CPU and preemption is disabled,
  1313. * hence we can't get into perf_event_task_sched_out for this context.
  1314. */
  1315. void perf_event_disable(struct perf_event *event)
  1316. {
  1317. struct perf_event_context *ctx = event->ctx;
  1318. struct task_struct *task = ctx->task;
  1319. if (!task) {
  1320. /*
  1321. * Disable the event on the cpu that it's on
  1322. */
  1323. cpu_function_call(event->cpu, __perf_event_disable, event);
  1324. return;
  1325. }
  1326. retry:
  1327. if (!task_function_call(task, __perf_event_disable, event))
  1328. return;
  1329. raw_spin_lock_irq(&ctx->lock);
  1330. /*
  1331. * If the event is still active, we need to retry the cross-call.
  1332. */
  1333. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1334. raw_spin_unlock_irq(&ctx->lock);
  1335. /*
  1336. * Reload the task pointer, it might have been changed by
  1337. * a concurrent perf_event_context_sched_out().
  1338. */
  1339. task = ctx->task;
  1340. goto retry;
  1341. }
  1342. /*
  1343. * Since we have the lock this context can't be scheduled
  1344. * in, so we can change the state safely.
  1345. */
  1346. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1347. update_group_times(event);
  1348. event->state = PERF_EVENT_STATE_OFF;
  1349. }
  1350. raw_spin_unlock_irq(&ctx->lock);
  1351. }
  1352. EXPORT_SYMBOL_GPL(perf_event_disable);
  1353. static void perf_set_shadow_time(struct perf_event *event,
  1354. struct perf_event_context *ctx,
  1355. u64 tstamp)
  1356. {
  1357. /*
  1358. * use the correct time source for the time snapshot
  1359. *
  1360. * We could get by without this by leveraging the
  1361. * fact that to get to this function, the caller
  1362. * has most likely already called update_context_time()
  1363. * and update_cgrp_time_xx() and thus both timestamp
  1364. * are identical (or very close). Given that tstamp is,
  1365. * already adjusted for cgroup, we could say that:
  1366. * tstamp - ctx->timestamp
  1367. * is equivalent to
  1368. * tstamp - cgrp->timestamp.
  1369. *
  1370. * Then, in perf_output_read(), the calculation would
  1371. * work with no changes because:
  1372. * - event is guaranteed scheduled in
  1373. * - no scheduled out in between
  1374. * - thus the timestamp would be the same
  1375. *
  1376. * But this is a bit hairy.
  1377. *
  1378. * So instead, we have an explicit cgroup call to remain
  1379. * within the time time source all along. We believe it
  1380. * is cleaner and simpler to understand.
  1381. */
  1382. if (is_cgroup_event(event))
  1383. perf_cgroup_set_shadow_time(event, tstamp);
  1384. else
  1385. event->shadow_ctx_time = tstamp - ctx->timestamp;
  1386. }
  1387. #define MAX_INTERRUPTS (~0ULL)
  1388. static void perf_log_throttle(struct perf_event *event, int enable);
  1389. static int
  1390. event_sched_in(struct perf_event *event,
  1391. struct perf_cpu_context *cpuctx,
  1392. struct perf_event_context *ctx)
  1393. {
  1394. u64 tstamp = perf_event_time(event);
  1395. int ret = 0;
  1396. lockdep_assert_held(&ctx->lock);
  1397. if (event->state <= PERF_EVENT_STATE_OFF)
  1398. return 0;
  1399. event->state = PERF_EVENT_STATE_ACTIVE;
  1400. event->oncpu = smp_processor_id();
  1401. /*
  1402. * Unthrottle events, since we scheduled we might have missed several
  1403. * ticks already, also for a heavily scheduling task there is little
  1404. * guarantee it'll get a tick in a timely manner.
  1405. */
  1406. if (unlikely(event->hw.interrupts == MAX_INTERRUPTS)) {
  1407. perf_log_throttle(event, 1);
  1408. event->hw.interrupts = 0;
  1409. }
  1410. /*
  1411. * The new state must be visible before we turn it on in the hardware:
  1412. */
  1413. smp_wmb();
  1414. perf_pmu_disable(event->pmu);
  1415. if (event->pmu->add(event, PERF_EF_START)) {
  1416. event->state = PERF_EVENT_STATE_INACTIVE;
  1417. event->oncpu = -1;
  1418. ret = -EAGAIN;
  1419. goto out;
  1420. }
  1421. event->tstamp_running += tstamp - event->tstamp_stopped;
  1422. perf_set_shadow_time(event, ctx, tstamp);
  1423. if (!is_software_event(event))
  1424. cpuctx->active_oncpu++;
  1425. ctx->nr_active++;
  1426. if (event->attr.freq && event->attr.sample_freq)
  1427. ctx->nr_freq++;
  1428. if (event->attr.exclusive)
  1429. cpuctx->exclusive = 1;
  1430. out:
  1431. perf_pmu_enable(event->pmu);
  1432. return ret;
  1433. }
  1434. static int
  1435. group_sched_in(struct perf_event *group_event,
  1436. struct perf_cpu_context *cpuctx,
  1437. struct perf_event_context *ctx)
  1438. {
  1439. struct perf_event *event, *partial_group = NULL;
  1440. struct pmu *pmu = ctx->pmu;
  1441. u64 now = ctx->time;
  1442. bool simulate = false;
  1443. if (group_event->state == PERF_EVENT_STATE_OFF)
  1444. return 0;
  1445. pmu->start_txn(pmu);
  1446. if (event_sched_in(group_event, cpuctx, ctx)) {
  1447. pmu->cancel_txn(pmu);
  1448. perf_cpu_hrtimer_restart(cpuctx);
  1449. return -EAGAIN;
  1450. }
  1451. /*
  1452. * Schedule in siblings as one group (if any):
  1453. */
  1454. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1455. if (event_sched_in(event, cpuctx, ctx)) {
  1456. partial_group = event;
  1457. goto group_error;
  1458. }
  1459. }
  1460. if (!pmu->commit_txn(pmu))
  1461. return 0;
  1462. group_error:
  1463. /*
  1464. * Groups can be scheduled in as one unit only, so undo any
  1465. * partial group before returning:
  1466. * The events up to the failed event are scheduled out normally,
  1467. * tstamp_stopped will be updated.
  1468. *
  1469. * The failed events and the remaining siblings need to have
  1470. * their timings updated as if they had gone thru event_sched_in()
  1471. * and event_sched_out(). This is required to get consistent timings
  1472. * across the group. This also takes care of the case where the group
  1473. * could never be scheduled by ensuring tstamp_stopped is set to mark
  1474. * the time the event was actually stopped, such that time delta
  1475. * calculation in update_event_times() is correct.
  1476. */
  1477. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  1478. if (event == partial_group)
  1479. simulate = true;
  1480. if (simulate) {
  1481. event->tstamp_running += now - event->tstamp_stopped;
  1482. event->tstamp_stopped = now;
  1483. } else {
  1484. event_sched_out(event, cpuctx, ctx);
  1485. }
  1486. }
  1487. event_sched_out(group_event, cpuctx, ctx);
  1488. pmu->cancel_txn(pmu);
  1489. perf_cpu_hrtimer_restart(cpuctx);
  1490. return -EAGAIN;
  1491. }
  1492. /*
  1493. * Work out whether we can put this event group on the CPU now.
  1494. */
  1495. static int group_can_go_on(struct perf_event *event,
  1496. struct perf_cpu_context *cpuctx,
  1497. int can_add_hw)
  1498. {
  1499. /*
  1500. * Groups consisting entirely of software events can always go on.
  1501. */
  1502. if (event->group_flags & PERF_GROUP_SOFTWARE)
  1503. return 1;
  1504. /*
  1505. * If an exclusive group is already on, no other hardware
  1506. * events can go on.
  1507. */
  1508. if (cpuctx->exclusive)
  1509. return 0;
  1510. /*
  1511. * If this group is exclusive and there are already
  1512. * events on the CPU, it can't go on.
  1513. */
  1514. if (event->attr.exclusive && cpuctx->active_oncpu)
  1515. return 0;
  1516. /*
  1517. * Otherwise, try to add it if all previous groups were able
  1518. * to go on.
  1519. */
  1520. return can_add_hw;
  1521. }
  1522. static void add_event_to_ctx(struct perf_event *event,
  1523. struct perf_event_context *ctx)
  1524. {
  1525. u64 tstamp = perf_event_time(event);
  1526. list_add_event(event, ctx);
  1527. perf_group_attach(event);
  1528. event->tstamp_enabled = tstamp;
  1529. event->tstamp_running = tstamp;
  1530. event->tstamp_stopped = tstamp;
  1531. }
  1532. static void task_ctx_sched_out(struct perf_event_context *ctx);
  1533. static void
  1534. ctx_sched_in(struct perf_event_context *ctx,
  1535. struct perf_cpu_context *cpuctx,
  1536. enum event_type_t event_type,
  1537. struct task_struct *task);
  1538. static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
  1539. struct perf_event_context *ctx,
  1540. struct task_struct *task)
  1541. {
  1542. cpu_ctx_sched_in(cpuctx, EVENT_PINNED, task);
  1543. if (ctx)
  1544. ctx_sched_in(ctx, cpuctx, EVENT_PINNED, task);
  1545. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE, task);
  1546. if (ctx)
  1547. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE, task);
  1548. }
  1549. /*
  1550. * Cross CPU call to install and enable a performance event
  1551. *
  1552. * Must be called with ctx->mutex held
  1553. */
  1554. static int __perf_install_in_context(void *info)
  1555. {
  1556. struct perf_event *event = info;
  1557. struct perf_event_context *ctx = event->ctx;
  1558. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1559. struct perf_event_context *task_ctx = cpuctx->task_ctx;
  1560. struct task_struct *task = current;
  1561. perf_ctx_lock(cpuctx, task_ctx);
  1562. perf_pmu_disable(cpuctx->ctx.pmu);
  1563. /*
  1564. * If there was an active task_ctx schedule it out.
  1565. */
  1566. if (task_ctx)
  1567. task_ctx_sched_out(task_ctx);
  1568. /*
  1569. * If the context we're installing events in is not the
  1570. * active task_ctx, flip them.
  1571. */
  1572. if (ctx->task && task_ctx != ctx) {
  1573. if (task_ctx)
  1574. raw_spin_unlock(&task_ctx->lock);
  1575. raw_spin_lock(&ctx->lock);
  1576. task_ctx = ctx;
  1577. }
  1578. if (task_ctx) {
  1579. cpuctx->task_ctx = task_ctx;
  1580. task = task_ctx->task;
  1581. }
  1582. cpu_ctx_sched_out(cpuctx, EVENT_ALL);
  1583. update_context_time(ctx);
  1584. /*
  1585. * update cgrp time only if current cgrp
  1586. * matches event->cgrp. Must be done before
  1587. * calling add_event_to_ctx()
  1588. */
  1589. update_cgrp_time_from_event(event);
  1590. add_event_to_ctx(event, ctx);
  1591. /*
  1592. * Schedule everything back in
  1593. */
  1594. perf_event_sched_in(cpuctx, task_ctx, task);
  1595. perf_pmu_enable(cpuctx->ctx.pmu);
  1596. perf_ctx_unlock(cpuctx, task_ctx);
  1597. return 0;
  1598. }
  1599. /*
  1600. * Attach a performance event to a context
  1601. *
  1602. * First we add the event to the list with the hardware enable bit
  1603. * in event->hw_config cleared.
  1604. *
  1605. * If the event is attached to a task which is on a CPU we use a smp
  1606. * call to enable it in the task context. The task might have been
  1607. * scheduled away, but we check this in the smp call again.
  1608. */
  1609. static void
  1610. perf_install_in_context(struct perf_event_context *ctx,
  1611. struct perf_event *event,
  1612. int cpu)
  1613. {
  1614. struct task_struct *task = ctx->task;
  1615. lockdep_assert_held(&ctx->mutex);
  1616. event->ctx = ctx;
  1617. if (event->cpu != -1)
  1618. event->cpu = cpu;
  1619. if (!task) {
  1620. /*
  1621. * Per cpu events are installed via an smp call and
  1622. * the install is always successful.
  1623. */
  1624. cpu_function_call(cpu, __perf_install_in_context, event);
  1625. return;
  1626. }
  1627. retry:
  1628. if (!task_function_call(task, __perf_install_in_context, event))
  1629. return;
  1630. raw_spin_lock_irq(&ctx->lock);
  1631. /*
  1632. * If we failed to find a running task, but find the context active now
  1633. * that we've acquired the ctx->lock, retry.
  1634. */
  1635. if (ctx->is_active) {
  1636. raw_spin_unlock_irq(&ctx->lock);
  1637. goto retry;
  1638. }
  1639. /*
  1640. * Since the task isn't running, its safe to add the event, us holding
  1641. * the ctx->lock ensures the task won't get scheduled in.
  1642. */
  1643. add_event_to_ctx(event, ctx);
  1644. raw_spin_unlock_irq(&ctx->lock);
  1645. }
  1646. /*
  1647. * Put a event into inactive state and update time fields.
  1648. * Enabling the leader of a group effectively enables all
  1649. * the group members that aren't explicitly disabled, so we
  1650. * have to update their ->tstamp_enabled also.
  1651. * Note: this works for group members as well as group leaders
  1652. * since the non-leader members' sibling_lists will be empty.
  1653. */
  1654. static void __perf_event_mark_enabled(struct perf_event *event)
  1655. {
  1656. struct perf_event *sub;
  1657. u64 tstamp = perf_event_time(event);
  1658. event->state = PERF_EVENT_STATE_INACTIVE;
  1659. event->tstamp_enabled = tstamp - event->total_time_enabled;
  1660. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  1661. if (sub->state >= PERF_EVENT_STATE_INACTIVE)
  1662. sub->tstamp_enabled = tstamp - sub->total_time_enabled;
  1663. }
  1664. }
  1665. /*
  1666. * Cross CPU call to enable a performance event
  1667. */
  1668. static int __perf_event_enable(void *info)
  1669. {
  1670. struct perf_event *event = info;
  1671. struct perf_event_context *ctx = event->ctx;
  1672. struct perf_event *leader = event->group_leader;
  1673. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1674. int err;
  1675. /*
  1676. * There's a time window between 'ctx->is_active' check
  1677. * in perf_event_enable function and this place having:
  1678. * - IRQs on
  1679. * - ctx->lock unlocked
  1680. *
  1681. * where the task could be killed and 'ctx' deactivated
  1682. * by perf_event_exit_task.
  1683. */
  1684. if (!ctx->is_active)
  1685. return -EINVAL;
  1686. raw_spin_lock(&ctx->lock);
  1687. update_context_time(ctx);
  1688. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1689. goto unlock;
  1690. /*
  1691. * set current task's cgroup time reference point
  1692. */
  1693. perf_cgroup_set_timestamp(current, ctx);
  1694. __perf_event_mark_enabled(event);
  1695. if (!event_filter_match(event)) {
  1696. if (is_cgroup_event(event))
  1697. perf_cgroup_defer_enabled(event);
  1698. goto unlock;
  1699. }
  1700. /*
  1701. * If the event is in a group and isn't the group leader,
  1702. * then don't put it on unless the group is on.
  1703. */
  1704. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  1705. goto unlock;
  1706. if (!group_can_go_on(event, cpuctx, 1)) {
  1707. err = -EEXIST;
  1708. } else {
  1709. if (event == leader)
  1710. err = group_sched_in(event, cpuctx, ctx);
  1711. else
  1712. err = event_sched_in(event, cpuctx, ctx);
  1713. }
  1714. if (err) {
  1715. /*
  1716. * If this event can't go on and it's part of a
  1717. * group, then the whole group has to come off.
  1718. */
  1719. if (leader != event) {
  1720. group_sched_out(leader, cpuctx, ctx);
  1721. perf_cpu_hrtimer_restart(cpuctx);
  1722. }
  1723. if (leader->attr.pinned) {
  1724. update_group_times(leader);
  1725. leader->state = PERF_EVENT_STATE_ERROR;
  1726. }
  1727. }
  1728. unlock:
  1729. raw_spin_unlock(&ctx->lock);
  1730. return 0;
  1731. }
  1732. /*
  1733. * Enable a event.
  1734. *
  1735. * If event->ctx is a cloned context, callers must make sure that
  1736. * every task struct that event->ctx->task could possibly point to
  1737. * remains valid. This condition is satisfied when called through
  1738. * perf_event_for_each_child or perf_event_for_each as described
  1739. * for perf_event_disable.
  1740. */
  1741. void perf_event_enable(struct perf_event *event)
  1742. {
  1743. struct perf_event_context *ctx = event->ctx;
  1744. struct task_struct *task = ctx->task;
  1745. if (!task) {
  1746. /*
  1747. * Enable the event on the cpu that it's on
  1748. */
  1749. cpu_function_call(event->cpu, __perf_event_enable, event);
  1750. return;
  1751. }
  1752. raw_spin_lock_irq(&ctx->lock);
  1753. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1754. goto out;
  1755. /*
  1756. * If the event is in error state, clear that first.
  1757. * That way, if we see the event in error state below, we
  1758. * know that it has gone back into error state, as distinct
  1759. * from the task having been scheduled away before the
  1760. * cross-call arrived.
  1761. */
  1762. if (event->state == PERF_EVENT_STATE_ERROR)
  1763. event->state = PERF_EVENT_STATE_OFF;
  1764. retry:
  1765. if (!ctx->is_active) {
  1766. __perf_event_mark_enabled(event);
  1767. goto out;
  1768. }
  1769. raw_spin_unlock_irq(&ctx->lock);
  1770. if (!task_function_call(task, __perf_event_enable, event))
  1771. return;
  1772. raw_spin_lock_irq(&ctx->lock);
  1773. /*
  1774. * If the context is active and the event is still off,
  1775. * we need to retry the cross-call.
  1776. */
  1777. if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF) {
  1778. /*
  1779. * task could have been flipped by a concurrent
  1780. * perf_event_context_sched_out()
  1781. */
  1782. task = ctx->task;
  1783. goto retry;
  1784. }
  1785. out:
  1786. raw_spin_unlock_irq(&ctx->lock);
  1787. }
  1788. EXPORT_SYMBOL_GPL(perf_event_enable);
  1789. int perf_event_refresh(struct perf_event *event, int refresh)
  1790. {
  1791. /*
  1792. * not supported on inherited events
  1793. */
  1794. if (event->attr.inherit || !is_sampling_event(event))
  1795. return -EINVAL;
  1796. atomic_add(refresh, &event->event_limit);
  1797. perf_event_enable(event);
  1798. return 0;
  1799. }
  1800. EXPORT_SYMBOL_GPL(perf_event_refresh);
  1801. static void ctx_sched_out(struct perf_event_context *ctx,
  1802. struct perf_cpu_context *cpuctx,
  1803. enum event_type_t event_type)
  1804. {
  1805. struct perf_event *event;
  1806. int is_active = ctx->is_active;
  1807. ctx->is_active &= ~event_type;
  1808. if (likely(!ctx->nr_events))
  1809. return;
  1810. update_context_time(ctx);
  1811. update_cgrp_time_from_cpuctx(cpuctx);
  1812. if (!ctx->nr_active)
  1813. return;
  1814. perf_pmu_disable(ctx->pmu);
  1815. if ((is_active & EVENT_PINNED) && (event_type & EVENT_PINNED)) {
  1816. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  1817. group_sched_out(event, cpuctx, ctx);
  1818. }
  1819. if ((is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE)) {
  1820. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  1821. group_sched_out(event, cpuctx, ctx);
  1822. }
  1823. perf_pmu_enable(ctx->pmu);
  1824. }
  1825. /*
  1826. * Test whether two contexts are equivalent, i.e. whether they have both been
  1827. * cloned from the same version of the same context.
  1828. *
  1829. * Equivalence is measured using a generation number in the context that is
  1830. * incremented on each modification to it; see unclone_ctx(), list_add_event()
  1831. * and list_del_event().
  1832. */
  1833. static int context_equiv(struct perf_event_context *ctx1,
  1834. struct perf_event_context *ctx2)
  1835. {
  1836. /* Pinning disables the swap optimization */
  1837. if (ctx1->pin_count || ctx2->pin_count)
  1838. return 0;
  1839. /* If ctx1 is the parent of ctx2 */
  1840. if (ctx1 == ctx2->parent_ctx && ctx1->generation == ctx2->parent_gen)
  1841. return 1;
  1842. /* If ctx2 is the parent of ctx1 */
  1843. if (ctx1->parent_ctx == ctx2 && ctx1->parent_gen == ctx2->generation)
  1844. return 1;
  1845. /*
  1846. * If ctx1 and ctx2 have the same parent; we flatten the parent
  1847. * hierarchy, see perf_event_init_context().
  1848. */
  1849. if (ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx &&
  1850. ctx1->parent_gen == ctx2->parent_gen)
  1851. return 1;
  1852. /* Unmatched */
  1853. return 0;
  1854. }
  1855. static void __perf_event_sync_stat(struct perf_event *event,
  1856. struct perf_event *next_event)
  1857. {
  1858. u64 value;
  1859. if (!event->attr.inherit_stat)
  1860. return;
  1861. /*
  1862. * Update the event value, we cannot use perf_event_read()
  1863. * because we're in the middle of a context switch and have IRQs
  1864. * disabled, which upsets smp_call_function_single(), however
  1865. * we know the event must be on the current CPU, therefore we
  1866. * don't need to use it.
  1867. */
  1868. switch (event->state) {
  1869. case PERF_EVENT_STATE_ACTIVE:
  1870. event->pmu->read(event);
  1871. /* fall-through */
  1872. case PERF_EVENT_STATE_INACTIVE:
  1873. update_event_times(event);
  1874. break;
  1875. default:
  1876. break;
  1877. }
  1878. /*
  1879. * In order to keep per-task stats reliable we need to flip the event
  1880. * values when we flip the contexts.
  1881. */
  1882. value = local64_read(&next_event->count);
  1883. value = local64_xchg(&event->count, value);
  1884. local64_set(&next_event->count, value);
  1885. swap(event->total_time_enabled, next_event->total_time_enabled);
  1886. swap(event->total_time_running, next_event->total_time_running);
  1887. /*
  1888. * Since we swizzled the values, update the user visible data too.
  1889. */
  1890. perf_event_update_userpage(event);
  1891. perf_event_update_userpage(next_event);
  1892. }
  1893. static void perf_event_sync_stat(struct perf_event_context *ctx,
  1894. struct perf_event_context *next_ctx)
  1895. {
  1896. struct perf_event *event, *next_event;
  1897. if (!ctx->nr_stat)
  1898. return;
  1899. update_context_time(ctx);
  1900. event = list_first_entry(&ctx->event_list,
  1901. struct perf_event, event_entry);
  1902. next_event = list_first_entry(&next_ctx->event_list,
  1903. struct perf_event, event_entry);
  1904. while (&event->event_entry != &ctx->event_list &&
  1905. &next_event->event_entry != &next_ctx->event_list) {
  1906. __perf_event_sync_stat(event, next_event);
  1907. event = list_next_entry(event, event_entry);
  1908. next_event = list_next_entry(next_event, event_entry);
  1909. }
  1910. }
  1911. static void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  1912. struct task_struct *next)
  1913. {
  1914. struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
  1915. struct perf_event_context *next_ctx;
  1916. struct perf_event_context *parent, *next_parent;
  1917. struct perf_cpu_context *cpuctx;
  1918. int do_switch = 1;
  1919. if (likely(!ctx))
  1920. return;
  1921. cpuctx = __get_cpu_context(ctx);
  1922. if (!cpuctx->task_ctx)
  1923. return;
  1924. rcu_read_lock();
  1925. next_ctx = next->perf_event_ctxp[ctxn];
  1926. if (!next_ctx)
  1927. goto unlock;
  1928. parent = rcu_dereference(ctx->parent_ctx);
  1929. next_parent = rcu_dereference(next_ctx->parent_ctx);
  1930. /* If neither context have a parent context; they cannot be clones. */
  1931. if (!parent || !next_parent)
  1932. goto unlock;
  1933. if (next_parent == ctx || next_ctx == parent || next_parent == parent) {
  1934. /*
  1935. * Looks like the two contexts are clones, so we might be
  1936. * able to optimize the context switch. We lock both
  1937. * contexts and check that they are clones under the
  1938. * lock (including re-checking that neither has been
  1939. * uncloned in the meantime). It doesn't matter which
  1940. * order we take the locks because no other cpu could
  1941. * be trying to lock both of these tasks.
  1942. */
  1943. raw_spin_lock(&ctx->lock);
  1944. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  1945. if (context_equiv(ctx, next_ctx)) {
  1946. /*
  1947. * XXX do we need a memory barrier of sorts
  1948. * wrt to rcu_dereference() of perf_event_ctxp
  1949. */
  1950. task->perf_event_ctxp[ctxn] = next_ctx;
  1951. next->perf_event_ctxp[ctxn] = ctx;
  1952. ctx->task = next;
  1953. next_ctx->task = task;
  1954. do_switch = 0;
  1955. perf_event_sync_stat(ctx, next_ctx);
  1956. }
  1957. raw_spin_unlock(&next_ctx->lock);
  1958. raw_spin_unlock(&ctx->lock);
  1959. }
  1960. unlock:
  1961. rcu_read_unlock();
  1962. if (do_switch) {
  1963. raw_spin_lock(&ctx->lock);
  1964. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  1965. cpuctx->task_ctx = NULL;
  1966. raw_spin_unlock(&ctx->lock);
  1967. }
  1968. }
  1969. #define for_each_task_context_nr(ctxn) \
  1970. for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  1971. /*
  1972. * Called from scheduler to remove the events of the current task,
  1973. * with interrupts disabled.
  1974. *
  1975. * We stop each event and update the event value in event->count.
  1976. *
  1977. * This does not protect us against NMI, but disable()
  1978. * sets the disabled bit in the control field of event _before_
  1979. * accessing the event control register. If a NMI hits, then it will
  1980. * not restart the event.
  1981. */
  1982. void __perf_event_task_sched_out(struct task_struct *task,
  1983. struct task_struct *next)
  1984. {
  1985. int ctxn;
  1986. for_each_task_context_nr(ctxn)
  1987. perf_event_context_sched_out(task, ctxn, next);
  1988. /*
  1989. * if cgroup events exist on this CPU, then we need
  1990. * to check if we have to switch out PMU state.
  1991. * cgroup event are system-wide mode only
  1992. */
  1993. if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
  1994. perf_cgroup_sched_out(task, next);
  1995. }
  1996. static void task_ctx_sched_out(struct perf_event_context *ctx)
  1997. {
  1998. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1999. if (!cpuctx->task_ctx)
  2000. return;
  2001. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  2002. return;
  2003. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  2004. cpuctx->task_ctx = NULL;
  2005. }
  2006. /*
  2007. * Called with IRQs disabled
  2008. */
  2009. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  2010. enum event_type_t event_type)
  2011. {
  2012. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  2013. }
  2014. static void
  2015. ctx_pinned_sched_in(struct perf_event_context *ctx,
  2016. struct perf_cpu_context *cpuctx)
  2017. {
  2018. struct perf_event *event;
  2019. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  2020. if (event->state <= PERF_EVENT_STATE_OFF)
  2021. continue;
  2022. if (!event_filter_match(event))
  2023. continue;
  2024. /* may need to reset tstamp_enabled */
  2025. if (is_cgroup_event(event))
  2026. perf_cgroup_mark_enabled(event, ctx);
  2027. if (group_can_go_on(event, cpuctx, 1))
  2028. group_sched_in(event, cpuctx, ctx);
  2029. /*
  2030. * If this pinned group hasn't been scheduled,
  2031. * put it in error state.
  2032. */
  2033. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2034. update_group_times(event);
  2035. event->state = PERF_EVENT_STATE_ERROR;
  2036. }
  2037. }
  2038. }
  2039. static void
  2040. ctx_flexible_sched_in(struct perf_event_context *ctx,
  2041. struct perf_cpu_context *cpuctx)
  2042. {
  2043. struct perf_event *event;
  2044. int can_add_hw = 1;
  2045. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  2046. /* Ignore events in OFF or ERROR state */
  2047. if (event->state <= PERF_EVENT_STATE_OFF)
  2048. continue;
  2049. /*
  2050. * Listen to the 'cpu' scheduling filter constraint
  2051. * of events:
  2052. */
  2053. if (!event_filter_match(event))
  2054. continue;
  2055. /* may need to reset tstamp_enabled */
  2056. if (is_cgroup_event(event))
  2057. perf_cgroup_mark_enabled(event, ctx);
  2058. if (group_can_go_on(event, cpuctx, can_add_hw)) {
  2059. if (group_sched_in(event, cpuctx, ctx))
  2060. can_add_hw = 0;
  2061. }
  2062. }
  2063. }
  2064. static void
  2065. ctx_sched_in(struct perf_event_context *ctx,
  2066. struct perf_cpu_context *cpuctx,
  2067. enum event_type_t event_type,
  2068. struct task_struct *task)
  2069. {
  2070. u64 now;
  2071. int is_active = ctx->is_active;
  2072. ctx->is_active |= event_type;
  2073. if (likely(!ctx->nr_events))
  2074. return;
  2075. now = perf_clock();
  2076. ctx->timestamp = now;
  2077. perf_cgroup_set_timestamp(task, ctx);
  2078. /*
  2079. * First go through the list and put on any pinned groups
  2080. * in order to give them the best chance of going on.
  2081. */
  2082. if (!(is_active & EVENT_PINNED) && (event_type & EVENT_PINNED))
  2083. ctx_pinned_sched_in(ctx, cpuctx);
  2084. /* Then walk through the lower prio flexible groups */
  2085. if (!(is_active & EVENT_FLEXIBLE) && (event_type & EVENT_FLEXIBLE))
  2086. ctx_flexible_sched_in(ctx, cpuctx);
  2087. }
  2088. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  2089. enum event_type_t event_type,
  2090. struct task_struct *task)
  2091. {
  2092. struct perf_event_context *ctx = &cpuctx->ctx;
  2093. ctx_sched_in(ctx, cpuctx, event_type, task);
  2094. }
  2095. static void perf_event_context_sched_in(struct perf_event_context *ctx,
  2096. struct task_struct *task)
  2097. {
  2098. struct perf_cpu_context *cpuctx;
  2099. cpuctx = __get_cpu_context(ctx);
  2100. if (cpuctx->task_ctx == ctx)
  2101. return;
  2102. perf_ctx_lock(cpuctx, ctx);
  2103. perf_pmu_disable(ctx->pmu);
  2104. /*
  2105. * We want to keep the following priority order:
  2106. * cpu pinned (that don't need to move), task pinned,
  2107. * cpu flexible, task flexible.
  2108. */
  2109. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2110. if (ctx->nr_events)
  2111. cpuctx->task_ctx = ctx;
  2112. perf_event_sched_in(cpuctx, cpuctx->task_ctx, task);
  2113. perf_pmu_enable(ctx->pmu);
  2114. perf_ctx_unlock(cpuctx, ctx);
  2115. /*
  2116. * Since these rotations are per-cpu, we need to ensure the
  2117. * cpu-context we got scheduled on is actually rotating.
  2118. */
  2119. perf_pmu_rotate_start(ctx->pmu);
  2120. }
  2121. /*
  2122. * When sampling the branck stack in system-wide, it may be necessary
  2123. * to flush the stack on context switch. This happens when the branch
  2124. * stack does not tag its entries with the pid of the current task.
  2125. * Otherwise it becomes impossible to associate a branch entry with a
  2126. * task. This ambiguity is more likely to appear when the branch stack
  2127. * supports priv level filtering and the user sets it to monitor only
  2128. * at the user level (which could be a useful measurement in system-wide
  2129. * mode). In that case, the risk is high of having a branch stack with
  2130. * branch from multiple tasks. Flushing may mean dropping the existing
  2131. * entries or stashing them somewhere in the PMU specific code layer.
  2132. *
  2133. * This function provides the context switch callback to the lower code
  2134. * layer. It is invoked ONLY when there is at least one system-wide context
  2135. * with at least one active event using taken branch sampling.
  2136. */
  2137. static void perf_branch_stack_sched_in(struct task_struct *prev,
  2138. struct task_struct *task)
  2139. {
  2140. struct perf_cpu_context *cpuctx;
  2141. struct pmu *pmu;
  2142. unsigned long flags;
  2143. /* no need to flush branch stack if not changing task */
  2144. if (prev == task)
  2145. return;
  2146. local_irq_save(flags);
  2147. rcu_read_lock();
  2148. list_for_each_entry_rcu(pmu, &pmus, entry) {
  2149. cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  2150. /*
  2151. * check if the context has at least one
  2152. * event using PERF_SAMPLE_BRANCH_STACK
  2153. */
  2154. if (cpuctx->ctx.nr_branch_stack > 0
  2155. && pmu->flush_branch_stack) {
  2156. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2157. perf_pmu_disable(pmu);
  2158. pmu->flush_branch_stack();
  2159. perf_pmu_enable(pmu);
  2160. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2161. }
  2162. }
  2163. rcu_read_unlock();
  2164. local_irq_restore(flags);
  2165. }
  2166. /*
  2167. * Called from scheduler to add the events of the current task
  2168. * with interrupts disabled.
  2169. *
  2170. * We restore the event value and then enable it.
  2171. *
  2172. * This does not protect us against NMI, but enable()
  2173. * sets the enabled bit in the control field of event _before_
  2174. * accessing the event control register. If a NMI hits, then it will
  2175. * keep the event running.
  2176. */
  2177. void __perf_event_task_sched_in(struct task_struct *prev,
  2178. struct task_struct *task)
  2179. {
  2180. struct perf_event_context *ctx;
  2181. int ctxn;
  2182. for_each_task_context_nr(ctxn) {
  2183. ctx = task->perf_event_ctxp[ctxn];
  2184. if (likely(!ctx))
  2185. continue;
  2186. perf_event_context_sched_in(ctx, task);
  2187. }
  2188. /*
  2189. * if cgroup events exist on this CPU, then we need
  2190. * to check if we have to switch in PMU state.
  2191. * cgroup event are system-wide mode only
  2192. */
  2193. if (atomic_read(&__get_cpu_var(perf_cgroup_events)))
  2194. perf_cgroup_sched_in(prev, task);
  2195. /* check for system-wide branch_stack events */
  2196. if (atomic_read(&__get_cpu_var(perf_branch_stack_events)))
  2197. perf_branch_stack_sched_in(prev, task);
  2198. }
  2199. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  2200. {
  2201. u64 frequency = event->attr.sample_freq;
  2202. u64 sec = NSEC_PER_SEC;
  2203. u64 divisor, dividend;
  2204. int count_fls, nsec_fls, frequency_fls, sec_fls;
  2205. count_fls = fls64(count);
  2206. nsec_fls = fls64(nsec);
  2207. frequency_fls = fls64(frequency);
  2208. sec_fls = 30;
  2209. /*
  2210. * We got @count in @nsec, with a target of sample_freq HZ
  2211. * the target period becomes:
  2212. *
  2213. * @count * 10^9
  2214. * period = -------------------
  2215. * @nsec * sample_freq
  2216. *
  2217. */
  2218. /*
  2219. * Reduce accuracy by one bit such that @a and @b converge
  2220. * to a similar magnitude.
  2221. */
  2222. #define REDUCE_FLS(a, b) \
  2223. do { \
  2224. if (a##_fls > b##_fls) { \
  2225. a >>= 1; \
  2226. a##_fls--; \
  2227. } else { \
  2228. b >>= 1; \
  2229. b##_fls--; \
  2230. } \
  2231. } while (0)
  2232. /*
  2233. * Reduce accuracy until either term fits in a u64, then proceed with
  2234. * the other, so that finally we can do a u64/u64 division.
  2235. */
  2236. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  2237. REDUCE_FLS(nsec, frequency);
  2238. REDUCE_FLS(sec, count);
  2239. }
  2240. if (count_fls + sec_fls > 64) {
  2241. divisor = nsec * frequency;
  2242. while (count_fls + sec_fls > 64) {
  2243. REDUCE_FLS(count, sec);
  2244. divisor >>= 1;
  2245. }
  2246. dividend = count * sec;
  2247. } else {
  2248. dividend = count * sec;
  2249. while (nsec_fls + frequency_fls > 64) {
  2250. REDUCE_FLS(nsec, frequency);
  2251. dividend >>= 1;
  2252. }
  2253. divisor = nsec * frequency;
  2254. }
  2255. if (!divisor)
  2256. return dividend;
  2257. return div64_u64(dividend, divisor);
  2258. }
  2259. static DEFINE_PER_CPU(int, perf_throttled_count);
  2260. static DEFINE_PER_CPU(u64, perf_throttled_seq);
  2261. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count, bool disable)
  2262. {
  2263. struct hw_perf_event *hwc = &event->hw;
  2264. s64 period, sample_period;
  2265. s64 delta;
  2266. period = perf_calculate_period(event, nsec, count);
  2267. delta = (s64)(period - hwc->sample_period);
  2268. delta = (delta + 7) / 8; /* low pass filter */
  2269. sample_period = hwc->sample_period + delta;
  2270. if (!sample_period)
  2271. sample_period = 1;
  2272. hwc->sample_period = sample_period;
  2273. if (local64_read(&hwc->period_left) > 8*sample_period) {
  2274. if (disable)
  2275. event->pmu->stop(event, PERF_EF_UPDATE);
  2276. local64_set(&hwc->period_left, 0);
  2277. if (disable)
  2278. event->pmu->start(event, PERF_EF_RELOAD);
  2279. }
  2280. }
  2281. /*
  2282. * combine freq adjustment with unthrottling to avoid two passes over the
  2283. * events. At the same time, make sure, having freq events does not change
  2284. * the rate of unthrottling as that would introduce bias.
  2285. */
  2286. static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
  2287. int needs_unthr)
  2288. {
  2289. struct perf_event *event;
  2290. struct hw_perf_event *hwc;
  2291. u64 now, period = TICK_NSEC;
  2292. s64 delta;
  2293. /*
  2294. * only need to iterate over all events iff:
  2295. * - context have events in frequency mode (needs freq adjust)
  2296. * - there are events to unthrottle on this cpu
  2297. */
  2298. if (!(ctx->nr_freq || needs_unthr))
  2299. return;
  2300. raw_spin_lock(&ctx->lock);
  2301. perf_pmu_disable(ctx->pmu);
  2302. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2303. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2304. continue;
  2305. if (!event_filter_match(event))
  2306. continue;
  2307. perf_pmu_disable(event->pmu);
  2308. hwc = &event->hw;
  2309. if (hwc->interrupts == MAX_INTERRUPTS) {
  2310. hwc->interrupts = 0;
  2311. perf_log_throttle(event, 1);
  2312. event->pmu->start(event, 0);
  2313. }
  2314. if (!event->attr.freq || !event->attr.sample_freq)
  2315. goto next;
  2316. /*
  2317. * stop the event and update event->count
  2318. */
  2319. event->pmu->stop(event, PERF_EF_UPDATE);
  2320. now = local64_read(&event->count);
  2321. delta = now - hwc->freq_count_stamp;
  2322. hwc->freq_count_stamp = now;
  2323. /*
  2324. * restart the event
  2325. * reload only if value has changed
  2326. * we have stopped the event so tell that
  2327. * to perf_adjust_period() to avoid stopping it
  2328. * twice.
  2329. */
  2330. if (delta > 0)
  2331. perf_adjust_period(event, period, delta, false);
  2332. event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
  2333. next:
  2334. perf_pmu_enable(event->pmu);
  2335. }
  2336. perf_pmu_enable(ctx->pmu);
  2337. raw_spin_unlock(&ctx->lock);
  2338. }
  2339. /*
  2340. * Round-robin a context's events:
  2341. */
  2342. static void rotate_ctx(struct perf_event_context *ctx)
  2343. {
  2344. /*
  2345. * Rotate the first entry last of non-pinned groups. Rotation might be
  2346. * disabled by the inheritance code.
  2347. */
  2348. if (!ctx->rotate_disable)
  2349. list_rotate_left(&ctx->flexible_groups);
  2350. }
  2351. /*
  2352. * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
  2353. * because they're strictly cpu affine and rotate_start is called with IRQs
  2354. * disabled, while rotate_context is called from IRQ context.
  2355. */
  2356. static int perf_rotate_context(struct perf_cpu_context *cpuctx)
  2357. {
  2358. struct perf_event_context *ctx = NULL;
  2359. int rotate = 0, remove = 1;
  2360. if (cpuctx->ctx.nr_events) {
  2361. remove = 0;
  2362. if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  2363. rotate = 1;
  2364. }
  2365. ctx = cpuctx->task_ctx;
  2366. if (ctx && ctx->nr_events) {
  2367. remove = 0;
  2368. if (ctx->nr_events != ctx->nr_active)
  2369. rotate = 1;
  2370. }
  2371. if (!rotate)
  2372. goto done;
  2373. perf_ctx_lock(cpuctx, cpuctx->task_ctx);
  2374. perf_pmu_disable(cpuctx->ctx.pmu);
  2375. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  2376. if (ctx)
  2377. ctx_sched_out(ctx, cpuctx, EVENT_FLEXIBLE);
  2378. rotate_ctx(&cpuctx->ctx);
  2379. if (ctx)
  2380. rotate_ctx(ctx);
  2381. perf_event_sched_in(cpuctx, ctx, current);
  2382. perf_pmu_enable(cpuctx->ctx.pmu);
  2383. perf_ctx_unlock(cpuctx, cpuctx->task_ctx);
  2384. done:
  2385. if (remove)
  2386. list_del_init(&cpuctx->rotation_list);
  2387. return rotate;
  2388. }
  2389. #ifdef CONFIG_NO_HZ_FULL
  2390. bool perf_event_can_stop_tick(void)
  2391. {
  2392. if (atomic_read(&nr_freq_events) ||
  2393. __this_cpu_read(perf_throttled_count))
  2394. return false;
  2395. else
  2396. return true;
  2397. }
  2398. #endif
  2399. void perf_event_task_tick(void)
  2400. {
  2401. struct list_head *head = &__get_cpu_var(rotation_list);
  2402. struct perf_cpu_context *cpuctx, *tmp;
  2403. struct perf_event_context *ctx;
  2404. int throttled;
  2405. WARN_ON(!irqs_disabled());
  2406. __this_cpu_inc(perf_throttled_seq);
  2407. throttled = __this_cpu_xchg(perf_throttled_count, 0);
  2408. list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
  2409. ctx = &cpuctx->ctx;
  2410. perf_adjust_freq_unthr_context(ctx, throttled);
  2411. ctx = cpuctx->task_ctx;
  2412. if (ctx)
  2413. perf_adjust_freq_unthr_context(ctx, throttled);
  2414. }
  2415. }
  2416. static int event_enable_on_exec(struct perf_event *event,
  2417. struct perf_event_context *ctx)
  2418. {
  2419. if (!event->attr.enable_on_exec)
  2420. return 0;
  2421. event->attr.enable_on_exec = 0;
  2422. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  2423. return 0;
  2424. __perf_event_mark_enabled(event);
  2425. return 1;
  2426. }
  2427. /*
  2428. * Enable all of a task's events that have been marked enable-on-exec.
  2429. * This expects task == current.
  2430. */
  2431. static void perf_event_enable_on_exec(struct perf_event_context *ctx)
  2432. {
  2433. struct perf_event *event;
  2434. unsigned long flags;
  2435. int enabled = 0;
  2436. int ret;
  2437. local_irq_save(flags);
  2438. if (!ctx || !ctx->nr_events)
  2439. goto out;
  2440. /*
  2441. * We must ctxsw out cgroup events to avoid conflict
  2442. * when invoking perf_task_event_sched_in() later on
  2443. * in this function. Otherwise we end up trying to
  2444. * ctxswin cgroup events which are already scheduled
  2445. * in.
  2446. */
  2447. perf_cgroup_sched_out(current, NULL);
  2448. raw_spin_lock(&ctx->lock);
  2449. task_ctx_sched_out(ctx);
  2450. list_for_each_entry(event, &ctx->event_list, event_entry) {
  2451. ret = event_enable_on_exec(event, ctx);
  2452. if (ret)
  2453. enabled = 1;
  2454. }
  2455. /*
  2456. * Unclone this context if we enabled any event.
  2457. */
  2458. if (enabled)
  2459. unclone_ctx(ctx);
  2460. raw_spin_unlock(&ctx->lock);
  2461. /*
  2462. * Also calls ctxswin for cgroup events, if any:
  2463. */
  2464. perf_event_context_sched_in(ctx, ctx->task);
  2465. out:
  2466. local_irq_restore(flags);
  2467. }
  2468. void perf_event_exec(void)
  2469. {
  2470. struct perf_event_context *ctx;
  2471. int ctxn;
  2472. rcu_read_lock();
  2473. for_each_task_context_nr(ctxn) {
  2474. ctx = current->perf_event_ctxp[ctxn];
  2475. if (!ctx)
  2476. continue;
  2477. perf_event_enable_on_exec(ctx);
  2478. }
  2479. rcu_read_unlock();
  2480. }
  2481. /*
  2482. * Cross CPU call to read the hardware event
  2483. */
  2484. static void __perf_event_read(void *info)
  2485. {
  2486. struct perf_event *event = info;
  2487. struct perf_event_context *ctx = event->ctx;
  2488. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  2489. /*
  2490. * If this is a task context, we need to check whether it is
  2491. * the current task context of this cpu. If not it has been
  2492. * scheduled out before the smp call arrived. In that case
  2493. * event->count would have been updated to a recent sample
  2494. * when the event was scheduled out.
  2495. */
  2496. if (ctx->task && cpuctx->task_ctx != ctx)
  2497. return;
  2498. raw_spin_lock(&ctx->lock);
  2499. if (ctx->is_active) {
  2500. update_context_time(ctx);
  2501. update_cgrp_time_from_event(event);
  2502. }
  2503. update_event_times(event);
  2504. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2505. event->pmu->read(event);
  2506. raw_spin_unlock(&ctx->lock);
  2507. }
  2508. static inline u64 perf_event_count(struct perf_event *event)
  2509. {
  2510. return local64_read(&event->count) + atomic64_read(&event->child_count);
  2511. }
  2512. static u64 perf_event_read(struct perf_event *event)
  2513. {
  2514. /*
  2515. * If event is enabled and currently active on a CPU, update the
  2516. * value in the event structure:
  2517. */
  2518. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  2519. smp_call_function_single(event->oncpu,
  2520. __perf_event_read, event, 1);
  2521. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  2522. struct perf_event_context *ctx = event->ctx;
  2523. unsigned long flags;
  2524. raw_spin_lock_irqsave(&ctx->lock, flags);
  2525. /*
  2526. * may read while context is not active
  2527. * (e.g., thread is blocked), in that case
  2528. * we cannot update context time
  2529. */
  2530. if (ctx->is_active) {
  2531. update_context_time(ctx);
  2532. update_cgrp_time_from_event(event);
  2533. }
  2534. update_event_times(event);
  2535. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2536. }
  2537. return perf_event_count(event);
  2538. }
  2539. /*
  2540. * Initialize the perf_event context in a task_struct:
  2541. */
  2542. static void __perf_event_init_context(struct perf_event_context *ctx)
  2543. {
  2544. raw_spin_lock_init(&ctx->lock);
  2545. mutex_init(&ctx->mutex);
  2546. INIT_LIST_HEAD(&ctx->pinned_groups);
  2547. INIT_LIST_HEAD(&ctx->flexible_groups);
  2548. INIT_LIST_HEAD(&ctx->event_list);
  2549. atomic_set(&ctx->refcount, 1);
  2550. }
  2551. static struct perf_event_context *
  2552. alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  2553. {
  2554. struct perf_event_context *ctx;
  2555. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  2556. if (!ctx)
  2557. return NULL;
  2558. __perf_event_init_context(ctx);
  2559. if (task) {
  2560. ctx->task = task;
  2561. get_task_struct(task);
  2562. }
  2563. ctx->pmu = pmu;
  2564. return ctx;
  2565. }
  2566. static struct task_struct *
  2567. find_lively_task_by_vpid(pid_t vpid)
  2568. {
  2569. struct task_struct *task;
  2570. int err;
  2571. rcu_read_lock();
  2572. if (!vpid)
  2573. task = current;
  2574. else
  2575. task = find_task_by_vpid(vpid);
  2576. if (task)
  2577. get_task_struct(task);
  2578. rcu_read_unlock();
  2579. if (!task)
  2580. return ERR_PTR(-ESRCH);
  2581. /* Reuse ptrace permission checks for now. */
  2582. err = -EACCES;
  2583. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  2584. goto errout;
  2585. return task;
  2586. errout:
  2587. put_task_struct(task);
  2588. return ERR_PTR(err);
  2589. }
  2590. /*
  2591. * Returns a matching context with refcount and pincount.
  2592. */
  2593. static struct perf_event_context *
  2594. find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
  2595. {
  2596. struct perf_event_context *ctx;
  2597. struct perf_cpu_context *cpuctx;
  2598. unsigned long flags;
  2599. int ctxn, err;
  2600. if (!task) {
  2601. /* Must be root to operate on a CPU event: */
  2602. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  2603. return ERR_PTR(-EACCES);
  2604. /*
  2605. * We could be clever and allow to attach a event to an
  2606. * offline CPU and activate it when the CPU comes up, but
  2607. * that's for later.
  2608. */
  2609. if (!cpu_online(cpu))
  2610. return ERR_PTR(-ENODEV);
  2611. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  2612. ctx = &cpuctx->ctx;
  2613. get_ctx(ctx);
  2614. ++ctx->pin_count;
  2615. return ctx;
  2616. }
  2617. err = -EINVAL;
  2618. ctxn = pmu->task_ctx_nr;
  2619. if (ctxn < 0)
  2620. goto errout;
  2621. retry:
  2622. ctx = perf_lock_task_context(task, ctxn, &flags);
  2623. if (ctx) {
  2624. unclone_ctx(ctx);
  2625. ++ctx->pin_count;
  2626. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  2627. } else {
  2628. ctx = alloc_perf_context(pmu, task);
  2629. err = -ENOMEM;
  2630. if (!ctx)
  2631. goto errout;
  2632. err = 0;
  2633. mutex_lock(&task->perf_event_mutex);
  2634. /*
  2635. * If it has already passed perf_event_exit_task().
  2636. * we must see PF_EXITING, it takes this mutex too.
  2637. */
  2638. if (task->flags & PF_EXITING)
  2639. err = -ESRCH;
  2640. else if (task->perf_event_ctxp[ctxn])
  2641. err = -EAGAIN;
  2642. else {
  2643. get_ctx(ctx);
  2644. ++ctx->pin_count;
  2645. rcu_assign_pointer(task->perf_event_ctxp[ctxn], ctx);
  2646. }
  2647. mutex_unlock(&task->perf_event_mutex);
  2648. if (unlikely(err)) {
  2649. put_ctx(ctx);
  2650. if (err == -EAGAIN)
  2651. goto retry;
  2652. goto errout;
  2653. }
  2654. }
  2655. return ctx;
  2656. errout:
  2657. return ERR_PTR(err);
  2658. }
  2659. static void perf_event_free_filter(struct perf_event *event);
  2660. static void free_event_rcu(struct rcu_head *head)
  2661. {
  2662. struct perf_event *event;
  2663. event = container_of(head, struct perf_event, rcu_head);
  2664. if (event->ns)
  2665. put_pid_ns(event->ns);
  2666. perf_event_free_filter(event);
  2667. kfree(event);
  2668. }
  2669. static void ring_buffer_put(struct ring_buffer *rb);
  2670. static void ring_buffer_attach(struct perf_event *event,
  2671. struct ring_buffer *rb);
  2672. static void unaccount_event_cpu(struct perf_event *event, int cpu)
  2673. {
  2674. if (event->parent)
  2675. return;
  2676. if (has_branch_stack(event)) {
  2677. if (!(event->attach_state & PERF_ATTACH_TASK))
  2678. atomic_dec(&per_cpu(perf_branch_stack_events, cpu));
  2679. }
  2680. if (is_cgroup_event(event))
  2681. atomic_dec(&per_cpu(perf_cgroup_events, cpu));
  2682. }
  2683. static void unaccount_event(struct perf_event *event)
  2684. {
  2685. if (event->parent)
  2686. return;
  2687. if (event->attach_state & PERF_ATTACH_TASK)
  2688. static_key_slow_dec_deferred(&perf_sched_events);
  2689. if (event->attr.mmap || event->attr.mmap_data)
  2690. atomic_dec(&nr_mmap_events);
  2691. if (event->attr.comm)
  2692. atomic_dec(&nr_comm_events);
  2693. if (event->attr.task)
  2694. atomic_dec(&nr_task_events);
  2695. if (event->attr.freq)
  2696. atomic_dec(&nr_freq_events);
  2697. if (is_cgroup_event(event))
  2698. static_key_slow_dec_deferred(&perf_sched_events);
  2699. if (has_branch_stack(event))
  2700. static_key_slow_dec_deferred(&perf_sched_events);
  2701. unaccount_event_cpu(event, event->cpu);
  2702. }
  2703. static void __free_event(struct perf_event *event)
  2704. {
  2705. if (!event->parent) {
  2706. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  2707. put_callchain_buffers();
  2708. }
  2709. if (event->destroy)
  2710. event->destroy(event);
  2711. if (event->ctx)
  2712. put_ctx(event->ctx);
  2713. if (event->pmu)
  2714. module_put(event->pmu->module);
  2715. call_rcu(&event->rcu_head, free_event_rcu);
  2716. }
  2717. static void _free_event(struct perf_event *event)
  2718. {
  2719. irq_work_sync(&event->pending);
  2720. unaccount_event(event);
  2721. if (event->rb) {
  2722. /*
  2723. * Can happen when we close an event with re-directed output.
  2724. *
  2725. * Since we have a 0 refcount, perf_mmap_close() will skip
  2726. * over us; possibly making our ring_buffer_put() the last.
  2727. */
  2728. mutex_lock(&event->mmap_mutex);
  2729. ring_buffer_attach(event, NULL);
  2730. mutex_unlock(&event->mmap_mutex);
  2731. }
  2732. if (is_cgroup_event(event))
  2733. perf_detach_cgroup(event);
  2734. __free_event(event);
  2735. }
  2736. /*
  2737. * Used to free events which have a known refcount of 1, such as in error paths
  2738. * where the event isn't exposed yet and inherited events.
  2739. */
  2740. static void free_event(struct perf_event *event)
  2741. {
  2742. if (WARN(atomic_long_cmpxchg(&event->refcount, 1, 0) != 1,
  2743. "unexpected event refcount: %ld; ptr=%p\n",
  2744. atomic_long_read(&event->refcount), event)) {
  2745. /* leak to avoid use-after-free */
  2746. return;
  2747. }
  2748. _free_event(event);
  2749. }
  2750. /*
  2751. * Called when the last reference to the file is gone.
  2752. */
  2753. static void put_event(struct perf_event *event)
  2754. {
  2755. struct perf_event_context *ctx = event->ctx;
  2756. struct task_struct *owner;
  2757. if (!atomic_long_dec_and_test(&event->refcount))
  2758. return;
  2759. rcu_read_lock();
  2760. owner = ACCESS_ONCE(event->owner);
  2761. /*
  2762. * Matches the smp_wmb() in perf_event_exit_task(). If we observe
  2763. * !owner it means the list deletion is complete and we can indeed
  2764. * free this event, otherwise we need to serialize on
  2765. * owner->perf_event_mutex.
  2766. */
  2767. smp_read_barrier_depends();
  2768. if (owner) {
  2769. /*
  2770. * Since delayed_put_task_struct() also drops the last
  2771. * task reference we can safely take a new reference
  2772. * while holding the rcu_read_lock().
  2773. */
  2774. get_task_struct(owner);
  2775. }
  2776. rcu_read_unlock();
  2777. if (owner) {
  2778. mutex_lock(&owner->perf_event_mutex);
  2779. /*
  2780. * We have to re-check the event->owner field, if it is cleared
  2781. * we raced with perf_event_exit_task(), acquiring the mutex
  2782. * ensured they're done, and we can proceed with freeing the
  2783. * event.
  2784. */
  2785. if (event->owner)
  2786. list_del_init(&event->owner_entry);
  2787. mutex_unlock(&owner->perf_event_mutex);
  2788. put_task_struct(owner);
  2789. }
  2790. WARN_ON_ONCE(ctx->parent_ctx);
  2791. /*
  2792. * There are two ways this annotation is useful:
  2793. *
  2794. * 1) there is a lock recursion from perf_event_exit_task
  2795. * see the comment there.
  2796. *
  2797. * 2) there is a lock-inversion with mmap_sem through
  2798. * perf_event_read_group(), which takes faults while
  2799. * holding ctx->mutex, however this is called after
  2800. * the last filedesc died, so there is no possibility
  2801. * to trigger the AB-BA case.
  2802. */
  2803. mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
  2804. perf_remove_from_context(event, true);
  2805. mutex_unlock(&ctx->mutex);
  2806. _free_event(event);
  2807. }
  2808. int perf_event_release_kernel(struct perf_event *event)
  2809. {
  2810. put_event(event);
  2811. return 0;
  2812. }
  2813. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  2814. static int perf_release(struct inode *inode, struct file *file)
  2815. {
  2816. put_event(file->private_data);
  2817. return 0;
  2818. }
  2819. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  2820. {
  2821. struct perf_event *child;
  2822. u64 total = 0;
  2823. *enabled = 0;
  2824. *running = 0;
  2825. mutex_lock(&event->child_mutex);
  2826. total += perf_event_read(event);
  2827. *enabled += event->total_time_enabled +
  2828. atomic64_read(&event->child_total_time_enabled);
  2829. *running += event->total_time_running +
  2830. atomic64_read(&event->child_total_time_running);
  2831. list_for_each_entry(child, &event->child_list, child_list) {
  2832. total += perf_event_read(child);
  2833. *enabled += child->total_time_enabled;
  2834. *running += child->total_time_running;
  2835. }
  2836. mutex_unlock(&event->child_mutex);
  2837. return total;
  2838. }
  2839. EXPORT_SYMBOL_GPL(perf_event_read_value);
  2840. static int perf_event_read_group(struct perf_event *event,
  2841. u64 read_format, char __user *buf)
  2842. {
  2843. struct perf_event *leader = event->group_leader, *sub;
  2844. int n = 0, size = 0, ret = -EFAULT;
  2845. struct perf_event_context *ctx = leader->ctx;
  2846. u64 values[5];
  2847. u64 count, enabled, running;
  2848. mutex_lock(&ctx->mutex);
  2849. count = perf_event_read_value(leader, &enabled, &running);
  2850. values[n++] = 1 + leader->nr_siblings;
  2851. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2852. values[n++] = enabled;
  2853. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2854. values[n++] = running;
  2855. values[n++] = count;
  2856. if (read_format & PERF_FORMAT_ID)
  2857. values[n++] = primary_event_id(leader);
  2858. size = n * sizeof(u64);
  2859. if (copy_to_user(buf, values, size))
  2860. goto unlock;
  2861. ret = size;
  2862. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2863. n = 0;
  2864. values[n++] = perf_event_read_value(sub, &enabled, &running);
  2865. if (read_format & PERF_FORMAT_ID)
  2866. values[n++] = primary_event_id(sub);
  2867. size = n * sizeof(u64);
  2868. if (copy_to_user(buf + ret, values, size)) {
  2869. ret = -EFAULT;
  2870. goto unlock;
  2871. }
  2872. ret += size;
  2873. }
  2874. unlock:
  2875. mutex_unlock(&ctx->mutex);
  2876. return ret;
  2877. }
  2878. static int perf_event_read_one(struct perf_event *event,
  2879. u64 read_format, char __user *buf)
  2880. {
  2881. u64 enabled, running;
  2882. u64 values[4];
  2883. int n = 0;
  2884. values[n++] = perf_event_read_value(event, &enabled, &running);
  2885. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2886. values[n++] = enabled;
  2887. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2888. values[n++] = running;
  2889. if (read_format & PERF_FORMAT_ID)
  2890. values[n++] = primary_event_id(event);
  2891. if (copy_to_user(buf, values, n * sizeof(u64)))
  2892. return -EFAULT;
  2893. return n * sizeof(u64);
  2894. }
  2895. /*
  2896. * Read the performance event - simple non blocking version for now
  2897. */
  2898. static ssize_t
  2899. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  2900. {
  2901. u64 read_format = event->attr.read_format;
  2902. int ret;
  2903. /*
  2904. * Return end-of-file for a read on a event that is in
  2905. * error state (i.e. because it was pinned but it couldn't be
  2906. * scheduled on to the CPU at some point).
  2907. */
  2908. if (event->state == PERF_EVENT_STATE_ERROR)
  2909. return 0;
  2910. if (count < event->read_size)
  2911. return -ENOSPC;
  2912. WARN_ON_ONCE(event->ctx->parent_ctx);
  2913. if (read_format & PERF_FORMAT_GROUP)
  2914. ret = perf_event_read_group(event, read_format, buf);
  2915. else
  2916. ret = perf_event_read_one(event, read_format, buf);
  2917. return ret;
  2918. }
  2919. static ssize_t
  2920. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  2921. {
  2922. struct perf_event *event = file->private_data;
  2923. return perf_read_hw(event, buf, count);
  2924. }
  2925. static unsigned int perf_poll(struct file *file, poll_table *wait)
  2926. {
  2927. struct perf_event *event = file->private_data;
  2928. struct ring_buffer *rb;
  2929. unsigned int events = POLL_HUP;
  2930. /*
  2931. * Pin the event->rb by taking event->mmap_mutex; otherwise
  2932. * perf_event_set_output() can swizzle our rb and make us miss wakeups.
  2933. */
  2934. mutex_lock(&event->mmap_mutex);
  2935. rb = event->rb;
  2936. if (rb)
  2937. events = atomic_xchg(&rb->poll, 0);
  2938. mutex_unlock(&event->mmap_mutex);
  2939. poll_wait(file, &event->waitq, wait);
  2940. return events;
  2941. }
  2942. static void perf_event_reset(struct perf_event *event)
  2943. {
  2944. (void)perf_event_read(event);
  2945. local64_set(&event->count, 0);
  2946. perf_event_update_userpage(event);
  2947. }
  2948. /*
  2949. * Holding the top-level event's child_mutex means that any
  2950. * descendant process that has inherited this event will block
  2951. * in sync_child_event if it goes to exit, thus satisfying the
  2952. * task existence requirements of perf_event_enable/disable.
  2953. */
  2954. static void perf_event_for_each_child(struct perf_event *event,
  2955. void (*func)(struct perf_event *))
  2956. {
  2957. struct perf_event *child;
  2958. WARN_ON_ONCE(event->ctx->parent_ctx);
  2959. mutex_lock(&event->child_mutex);
  2960. func(event);
  2961. list_for_each_entry(child, &event->child_list, child_list)
  2962. func(child);
  2963. mutex_unlock(&event->child_mutex);
  2964. }
  2965. static void perf_event_for_each(struct perf_event *event,
  2966. void (*func)(struct perf_event *))
  2967. {
  2968. struct perf_event_context *ctx = event->ctx;
  2969. struct perf_event *sibling;
  2970. WARN_ON_ONCE(ctx->parent_ctx);
  2971. mutex_lock(&ctx->mutex);
  2972. event = event->group_leader;
  2973. perf_event_for_each_child(event, func);
  2974. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  2975. perf_event_for_each_child(sibling, func);
  2976. mutex_unlock(&ctx->mutex);
  2977. }
  2978. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  2979. {
  2980. struct perf_event_context *ctx = event->ctx;
  2981. int ret = 0, active;
  2982. u64 value;
  2983. if (!is_sampling_event(event))
  2984. return -EINVAL;
  2985. if (copy_from_user(&value, arg, sizeof(value)))
  2986. return -EFAULT;
  2987. if (!value)
  2988. return -EINVAL;
  2989. raw_spin_lock_irq(&ctx->lock);
  2990. if (event->attr.freq) {
  2991. if (value > sysctl_perf_event_sample_rate) {
  2992. ret = -EINVAL;
  2993. goto unlock;
  2994. }
  2995. event->attr.sample_freq = value;
  2996. } else {
  2997. event->attr.sample_period = value;
  2998. event->hw.sample_period = value;
  2999. }
  3000. active = (event->state == PERF_EVENT_STATE_ACTIVE);
  3001. if (active) {
  3002. perf_pmu_disable(ctx->pmu);
  3003. event->pmu->stop(event, PERF_EF_UPDATE);
  3004. }
  3005. local64_set(&event->hw.period_left, 0);
  3006. if (active) {
  3007. event->pmu->start(event, PERF_EF_RELOAD);
  3008. perf_pmu_enable(ctx->pmu);
  3009. }
  3010. unlock:
  3011. raw_spin_unlock_irq(&ctx->lock);
  3012. return ret;
  3013. }
  3014. static const struct file_operations perf_fops;
  3015. static inline int perf_fget_light(int fd, struct fd *p)
  3016. {
  3017. struct fd f = fdget(fd);
  3018. if (!f.file)
  3019. return -EBADF;
  3020. if (f.file->f_op != &perf_fops) {
  3021. fdput(f);
  3022. return -EBADF;
  3023. }
  3024. *p = f;
  3025. return 0;
  3026. }
  3027. static int perf_event_set_output(struct perf_event *event,
  3028. struct perf_event *output_event);
  3029. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  3030. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  3031. {
  3032. struct perf_event *event = file->private_data;
  3033. void (*func)(struct perf_event *);
  3034. u32 flags = arg;
  3035. switch (cmd) {
  3036. case PERF_EVENT_IOC_ENABLE:
  3037. func = perf_event_enable;
  3038. break;
  3039. case PERF_EVENT_IOC_DISABLE:
  3040. func = perf_event_disable;
  3041. break;
  3042. case PERF_EVENT_IOC_RESET:
  3043. func = perf_event_reset;
  3044. break;
  3045. case PERF_EVENT_IOC_REFRESH:
  3046. return perf_event_refresh(event, arg);
  3047. case PERF_EVENT_IOC_PERIOD:
  3048. return perf_event_period(event, (u64 __user *)arg);
  3049. case PERF_EVENT_IOC_ID:
  3050. {
  3051. u64 id = primary_event_id(event);
  3052. if (copy_to_user((void __user *)arg, &id, sizeof(id)))
  3053. return -EFAULT;
  3054. return 0;
  3055. }
  3056. case PERF_EVENT_IOC_SET_OUTPUT:
  3057. {
  3058. int ret;
  3059. if (arg != -1) {
  3060. struct perf_event *output_event;
  3061. struct fd output;
  3062. ret = perf_fget_light(arg, &output);
  3063. if (ret)
  3064. return ret;
  3065. output_event = output.file->private_data;
  3066. ret = perf_event_set_output(event, output_event);
  3067. fdput(output);
  3068. } else {
  3069. ret = perf_event_set_output(event, NULL);
  3070. }
  3071. return ret;
  3072. }
  3073. case PERF_EVENT_IOC_SET_FILTER:
  3074. return perf_event_set_filter(event, (void __user *)arg);
  3075. default:
  3076. return -ENOTTY;
  3077. }
  3078. if (flags & PERF_IOC_FLAG_GROUP)
  3079. perf_event_for_each(event, func);
  3080. else
  3081. perf_event_for_each_child(event, func);
  3082. return 0;
  3083. }
  3084. #ifdef CONFIG_COMPAT
  3085. static long perf_compat_ioctl(struct file *file, unsigned int cmd,
  3086. unsigned long arg)
  3087. {
  3088. switch (_IOC_NR(cmd)) {
  3089. case _IOC_NR(PERF_EVENT_IOC_SET_FILTER):
  3090. case _IOC_NR(PERF_EVENT_IOC_ID):
  3091. /* Fix up pointer size (usually 4 -> 8 in 32-on-64-bit case */
  3092. if (_IOC_SIZE(cmd) == sizeof(compat_uptr_t)) {
  3093. cmd &= ~IOCSIZE_MASK;
  3094. cmd |= sizeof(void *) << IOCSIZE_SHIFT;
  3095. }
  3096. break;
  3097. }
  3098. return perf_ioctl(file, cmd, arg);
  3099. }
  3100. #else
  3101. # define perf_compat_ioctl NULL
  3102. #endif
  3103. int perf_event_task_enable(void)
  3104. {
  3105. struct perf_event *event;
  3106. mutex_lock(&current->perf_event_mutex);
  3107. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  3108. perf_event_for_each_child(event, perf_event_enable);
  3109. mutex_unlock(&current->perf_event_mutex);
  3110. return 0;
  3111. }
  3112. int perf_event_task_disable(void)
  3113. {
  3114. struct perf_event *event;
  3115. mutex_lock(&current->perf_event_mutex);
  3116. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  3117. perf_event_for_each_child(event, perf_event_disable);
  3118. mutex_unlock(&current->perf_event_mutex);
  3119. return 0;
  3120. }
  3121. static int perf_event_index(struct perf_event *event)
  3122. {
  3123. if (event->hw.state & PERF_HES_STOPPED)
  3124. return 0;
  3125. if (event->state != PERF_EVENT_STATE_ACTIVE)
  3126. return 0;
  3127. return event->pmu->event_idx(event);
  3128. }
  3129. static void calc_timer_values(struct perf_event *event,
  3130. u64 *now,
  3131. u64 *enabled,
  3132. u64 *running)
  3133. {
  3134. u64 ctx_time;
  3135. *now = perf_clock();
  3136. ctx_time = event->shadow_ctx_time + *now;
  3137. *enabled = ctx_time - event->tstamp_enabled;
  3138. *running = ctx_time - event->tstamp_running;
  3139. }
  3140. static void perf_event_init_userpage(struct perf_event *event)
  3141. {
  3142. struct perf_event_mmap_page *userpg;
  3143. struct ring_buffer *rb;
  3144. rcu_read_lock();
  3145. rb = rcu_dereference(event->rb);
  3146. if (!rb)
  3147. goto unlock;
  3148. userpg = rb->user_page;
  3149. /* Allow new userspace to detect that bit 0 is deprecated */
  3150. userpg->cap_bit0_is_deprecated = 1;
  3151. userpg->size = offsetof(struct perf_event_mmap_page, __reserved);
  3152. unlock:
  3153. rcu_read_unlock();
  3154. }
  3155. void __weak arch_perf_update_userpage(struct perf_event_mmap_page *userpg, u64 now)
  3156. {
  3157. }
  3158. /*
  3159. * Callers need to ensure there can be no nesting of this function, otherwise
  3160. * the seqlock logic goes bad. We can not serialize this because the arch
  3161. * code calls this from NMI context.
  3162. */
  3163. void perf_event_update_userpage(struct perf_event *event)
  3164. {
  3165. struct perf_event_mmap_page *userpg;
  3166. struct ring_buffer *rb;
  3167. u64 enabled, running, now;
  3168. rcu_read_lock();
  3169. rb = rcu_dereference(event->rb);
  3170. if (!rb)
  3171. goto unlock;
  3172. /*
  3173. * compute total_time_enabled, total_time_running
  3174. * based on snapshot values taken when the event
  3175. * was last scheduled in.
  3176. *
  3177. * we cannot simply called update_context_time()
  3178. * because of locking issue as we can be called in
  3179. * NMI context
  3180. */
  3181. calc_timer_values(event, &now, &enabled, &running);
  3182. userpg = rb->user_page;
  3183. /*
  3184. * Disable preemption so as to not let the corresponding user-space
  3185. * spin too long if we get preempted.
  3186. */
  3187. preempt_disable();
  3188. ++userpg->lock;
  3189. barrier();
  3190. userpg->index = perf_event_index(event);
  3191. userpg->offset = perf_event_count(event);
  3192. if (userpg->index)
  3193. userpg->offset -= local64_read(&event->hw.prev_count);
  3194. userpg->time_enabled = enabled +
  3195. atomic64_read(&event->child_total_time_enabled);
  3196. userpg->time_running = running +
  3197. atomic64_read(&event->child_total_time_running);
  3198. arch_perf_update_userpage(userpg, now);
  3199. barrier();
  3200. ++userpg->lock;
  3201. preempt_enable();
  3202. unlock:
  3203. rcu_read_unlock();
  3204. }
  3205. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  3206. {
  3207. struct perf_event *event = vma->vm_file->private_data;
  3208. struct ring_buffer *rb;
  3209. int ret = VM_FAULT_SIGBUS;
  3210. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  3211. if (vmf->pgoff == 0)
  3212. ret = 0;
  3213. return ret;
  3214. }
  3215. rcu_read_lock();
  3216. rb = rcu_dereference(event->rb);
  3217. if (!rb)
  3218. goto unlock;
  3219. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  3220. goto unlock;
  3221. vmf->page = perf_mmap_to_page(rb, vmf->pgoff);
  3222. if (!vmf->page)
  3223. goto unlock;
  3224. get_page(vmf->page);
  3225. vmf->page->mapping = vma->vm_file->f_mapping;
  3226. vmf->page->index = vmf->pgoff;
  3227. ret = 0;
  3228. unlock:
  3229. rcu_read_unlock();
  3230. return ret;
  3231. }
  3232. static void ring_buffer_attach(struct perf_event *event,
  3233. struct ring_buffer *rb)
  3234. {
  3235. struct ring_buffer *old_rb = NULL;
  3236. unsigned long flags;
  3237. if (event->rb) {
  3238. /*
  3239. * Should be impossible, we set this when removing
  3240. * event->rb_entry and wait/clear when adding event->rb_entry.
  3241. */
  3242. WARN_ON_ONCE(event->rcu_pending);
  3243. old_rb = event->rb;
  3244. event->rcu_batches = get_state_synchronize_rcu();
  3245. event->rcu_pending = 1;
  3246. spin_lock_irqsave(&old_rb->event_lock, flags);
  3247. list_del_rcu(&event->rb_entry);
  3248. spin_unlock_irqrestore(&old_rb->event_lock, flags);
  3249. }
  3250. if (event->rcu_pending && rb) {
  3251. cond_synchronize_rcu(event->rcu_batches);
  3252. event->rcu_pending = 0;
  3253. }
  3254. if (rb) {
  3255. spin_lock_irqsave(&rb->event_lock, flags);
  3256. list_add_rcu(&event->rb_entry, &rb->event_list);
  3257. spin_unlock_irqrestore(&rb->event_lock, flags);
  3258. }
  3259. rcu_assign_pointer(event->rb, rb);
  3260. if (old_rb) {
  3261. ring_buffer_put(old_rb);
  3262. /*
  3263. * Since we detached before setting the new rb, so that we
  3264. * could attach the new rb, we could have missed a wakeup.
  3265. * Provide it now.
  3266. */
  3267. wake_up_all(&event->waitq);
  3268. }
  3269. }
  3270. static void ring_buffer_wakeup(struct perf_event *event)
  3271. {
  3272. struct ring_buffer *rb;
  3273. rcu_read_lock();
  3274. rb = rcu_dereference(event->rb);
  3275. if (rb) {
  3276. list_for_each_entry_rcu(event, &rb->event_list, rb_entry)
  3277. wake_up_all(&event->waitq);
  3278. }
  3279. rcu_read_unlock();
  3280. }
  3281. static void rb_free_rcu(struct rcu_head *rcu_head)
  3282. {
  3283. struct ring_buffer *rb;
  3284. rb = container_of(rcu_head, struct ring_buffer, rcu_head);
  3285. rb_free(rb);
  3286. }
  3287. static struct ring_buffer *ring_buffer_get(struct perf_event *event)
  3288. {
  3289. struct ring_buffer *rb;
  3290. rcu_read_lock();
  3291. rb = rcu_dereference(event->rb);
  3292. if (rb) {
  3293. if (!atomic_inc_not_zero(&rb->refcount))
  3294. rb = NULL;
  3295. }
  3296. rcu_read_unlock();
  3297. return rb;
  3298. }
  3299. static void ring_buffer_put(struct ring_buffer *rb)
  3300. {
  3301. if (!atomic_dec_and_test(&rb->refcount))
  3302. return;
  3303. WARN_ON_ONCE(!list_empty(&rb->event_list));
  3304. call_rcu(&rb->rcu_head, rb_free_rcu);
  3305. }
  3306. static void perf_mmap_open(struct vm_area_struct *vma)
  3307. {
  3308. struct perf_event *event = vma->vm_file->private_data;
  3309. atomic_inc(&event->mmap_count);
  3310. atomic_inc(&event->rb->mmap_count);
  3311. }
  3312. /*
  3313. * A buffer can be mmap()ed multiple times; either directly through the same
  3314. * event, or through other events by use of perf_event_set_output().
  3315. *
  3316. * In order to undo the VM accounting done by perf_mmap() we need to destroy
  3317. * the buffer here, where we still have a VM context. This means we need
  3318. * to detach all events redirecting to us.
  3319. */
  3320. static void perf_mmap_close(struct vm_area_struct *vma)
  3321. {
  3322. struct perf_event *event = vma->vm_file->private_data;
  3323. struct ring_buffer *rb = ring_buffer_get(event);
  3324. struct user_struct *mmap_user = rb->mmap_user;
  3325. int mmap_locked = rb->mmap_locked;
  3326. unsigned long size = perf_data_size(rb);
  3327. atomic_dec(&rb->mmap_count);
  3328. if (!atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex))
  3329. goto out_put;
  3330. ring_buffer_attach(event, NULL);
  3331. mutex_unlock(&event->mmap_mutex);
  3332. /* If there's still other mmap()s of this buffer, we're done. */
  3333. if (atomic_read(&rb->mmap_count))
  3334. goto out_put;
  3335. /*
  3336. * No other mmap()s, detach from all other events that might redirect
  3337. * into the now unreachable buffer. Somewhat complicated by the
  3338. * fact that rb::event_lock otherwise nests inside mmap_mutex.
  3339. */
  3340. again:
  3341. rcu_read_lock();
  3342. list_for_each_entry_rcu(event, &rb->event_list, rb_entry) {
  3343. if (!atomic_long_inc_not_zero(&event->refcount)) {
  3344. /*
  3345. * This event is en-route to free_event() which will
  3346. * detach it and remove it from the list.
  3347. */
  3348. continue;
  3349. }
  3350. rcu_read_unlock();
  3351. mutex_lock(&event->mmap_mutex);
  3352. /*
  3353. * Check we didn't race with perf_event_set_output() which can
  3354. * swizzle the rb from under us while we were waiting to
  3355. * acquire mmap_mutex.
  3356. *
  3357. * If we find a different rb; ignore this event, a next
  3358. * iteration will no longer find it on the list. We have to
  3359. * still restart the iteration to make sure we're not now
  3360. * iterating the wrong list.
  3361. */
  3362. if (event->rb == rb)
  3363. ring_buffer_attach(event, NULL);
  3364. mutex_unlock(&event->mmap_mutex);
  3365. put_event(event);
  3366. /*
  3367. * Restart the iteration; either we're on the wrong list or
  3368. * destroyed its integrity by doing a deletion.
  3369. */
  3370. goto again;
  3371. }
  3372. rcu_read_unlock();
  3373. /*
  3374. * It could be there's still a few 0-ref events on the list; they'll
  3375. * get cleaned up by free_event() -- they'll also still have their
  3376. * ref on the rb and will free it whenever they are done with it.
  3377. *
  3378. * Aside from that, this buffer is 'fully' detached and unmapped,
  3379. * undo the VM accounting.
  3380. */
  3381. atomic_long_sub((size >> PAGE_SHIFT) + 1, &mmap_user->locked_vm);
  3382. vma->vm_mm->pinned_vm -= mmap_locked;
  3383. free_uid(mmap_user);
  3384. out_put:
  3385. ring_buffer_put(rb); /* could be last */
  3386. }
  3387. static const struct vm_operations_struct perf_mmap_vmops = {
  3388. .open = perf_mmap_open,
  3389. .close = perf_mmap_close,
  3390. .fault = perf_mmap_fault,
  3391. .page_mkwrite = perf_mmap_fault,
  3392. };
  3393. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  3394. {
  3395. struct perf_event *event = file->private_data;
  3396. unsigned long user_locked, user_lock_limit;
  3397. struct user_struct *user = current_user();
  3398. unsigned long locked, lock_limit;
  3399. struct ring_buffer *rb;
  3400. unsigned long vma_size;
  3401. unsigned long nr_pages;
  3402. long user_extra, extra;
  3403. int ret = 0, flags = 0;
  3404. /*
  3405. * Don't allow mmap() of inherited per-task counters. This would
  3406. * create a performance issue due to all children writing to the
  3407. * same rb.
  3408. */
  3409. if (event->cpu == -1 && event->attr.inherit)
  3410. return -EINVAL;
  3411. if (!(vma->vm_flags & VM_SHARED))
  3412. return -EINVAL;
  3413. vma_size = vma->vm_end - vma->vm_start;
  3414. nr_pages = (vma_size / PAGE_SIZE) - 1;
  3415. /*
  3416. * If we have rb pages ensure they're a power-of-two number, so we
  3417. * can do bitmasks instead of modulo.
  3418. */
  3419. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  3420. return -EINVAL;
  3421. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  3422. return -EINVAL;
  3423. if (vma->vm_pgoff != 0)
  3424. return -EINVAL;
  3425. WARN_ON_ONCE(event->ctx->parent_ctx);
  3426. again:
  3427. mutex_lock(&event->mmap_mutex);
  3428. if (event->rb) {
  3429. if (event->rb->nr_pages != nr_pages) {
  3430. ret = -EINVAL;
  3431. goto unlock;
  3432. }
  3433. if (!atomic_inc_not_zero(&event->rb->mmap_count)) {
  3434. /*
  3435. * Raced against perf_mmap_close() through
  3436. * perf_event_set_output(). Try again, hope for better
  3437. * luck.
  3438. */
  3439. mutex_unlock(&event->mmap_mutex);
  3440. goto again;
  3441. }
  3442. goto unlock;
  3443. }
  3444. user_extra = nr_pages + 1;
  3445. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  3446. /*
  3447. * Increase the limit linearly with more CPUs:
  3448. */
  3449. user_lock_limit *= num_online_cpus();
  3450. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  3451. extra = 0;
  3452. if (user_locked > user_lock_limit)
  3453. extra = user_locked - user_lock_limit;
  3454. lock_limit = rlimit(RLIMIT_MEMLOCK);
  3455. lock_limit >>= PAGE_SHIFT;
  3456. locked = vma->vm_mm->pinned_vm + extra;
  3457. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  3458. !capable(CAP_IPC_LOCK)) {
  3459. ret = -EPERM;
  3460. goto unlock;
  3461. }
  3462. WARN_ON(event->rb);
  3463. if (vma->vm_flags & VM_WRITE)
  3464. flags |= RING_BUFFER_WRITABLE;
  3465. rb = rb_alloc(nr_pages,
  3466. event->attr.watermark ? event->attr.wakeup_watermark : 0,
  3467. event->cpu, flags);
  3468. if (!rb) {
  3469. ret = -ENOMEM;
  3470. goto unlock;
  3471. }
  3472. atomic_set(&rb->mmap_count, 1);
  3473. rb->mmap_locked = extra;
  3474. rb->mmap_user = get_current_user();
  3475. atomic_long_add(user_extra, &user->locked_vm);
  3476. vma->vm_mm->pinned_vm += extra;
  3477. ring_buffer_attach(event, rb);
  3478. perf_event_init_userpage(event);
  3479. perf_event_update_userpage(event);
  3480. unlock:
  3481. if (!ret)
  3482. atomic_inc(&event->mmap_count);
  3483. mutex_unlock(&event->mmap_mutex);
  3484. /*
  3485. * Since pinned accounting is per vm we cannot allow fork() to copy our
  3486. * vma.
  3487. */
  3488. vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND | VM_DONTDUMP;
  3489. vma->vm_ops = &perf_mmap_vmops;
  3490. return ret;
  3491. }
  3492. static int perf_fasync(int fd, struct file *filp, int on)
  3493. {
  3494. struct inode *inode = file_inode(filp);
  3495. struct perf_event *event = filp->private_data;
  3496. int retval;
  3497. mutex_lock(&inode->i_mutex);
  3498. retval = fasync_helper(fd, filp, on, &event->fasync);
  3499. mutex_unlock(&inode->i_mutex);
  3500. if (retval < 0)
  3501. return retval;
  3502. return 0;
  3503. }
  3504. static const struct file_operations perf_fops = {
  3505. .llseek = no_llseek,
  3506. .release = perf_release,
  3507. .read = perf_read,
  3508. .poll = perf_poll,
  3509. .unlocked_ioctl = perf_ioctl,
  3510. .compat_ioctl = perf_compat_ioctl,
  3511. .mmap = perf_mmap,
  3512. .fasync = perf_fasync,
  3513. };
  3514. /*
  3515. * Perf event wakeup
  3516. *
  3517. * If there's data, ensure we set the poll() state and publish everything
  3518. * to user-space before waking everybody up.
  3519. */
  3520. void perf_event_wakeup(struct perf_event *event)
  3521. {
  3522. ring_buffer_wakeup(event);
  3523. if (event->pending_kill) {
  3524. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  3525. event->pending_kill = 0;
  3526. }
  3527. }
  3528. static void perf_pending_event(struct irq_work *entry)
  3529. {
  3530. struct perf_event *event = container_of(entry,
  3531. struct perf_event, pending);
  3532. if (event->pending_disable) {
  3533. event->pending_disable = 0;
  3534. __perf_event_disable(event);
  3535. }
  3536. if (event->pending_wakeup) {
  3537. event->pending_wakeup = 0;
  3538. perf_event_wakeup(event);
  3539. }
  3540. }
  3541. /*
  3542. * We assume there is only KVM supporting the callbacks.
  3543. * Later on, we might change it to a list if there is
  3544. * another virtualization implementation supporting the callbacks.
  3545. */
  3546. struct perf_guest_info_callbacks *perf_guest_cbs;
  3547. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3548. {
  3549. perf_guest_cbs = cbs;
  3550. return 0;
  3551. }
  3552. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  3553. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  3554. {
  3555. perf_guest_cbs = NULL;
  3556. return 0;
  3557. }
  3558. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  3559. static void
  3560. perf_output_sample_regs(struct perf_output_handle *handle,
  3561. struct pt_regs *regs, u64 mask)
  3562. {
  3563. int bit;
  3564. for_each_set_bit(bit, (const unsigned long *) &mask,
  3565. sizeof(mask) * BITS_PER_BYTE) {
  3566. u64 val;
  3567. val = perf_reg_value(regs, bit);
  3568. perf_output_put(handle, val);
  3569. }
  3570. }
  3571. static void perf_sample_regs_user(struct perf_regs_user *regs_user,
  3572. struct pt_regs *regs)
  3573. {
  3574. if (!user_mode(regs)) {
  3575. if (current->mm)
  3576. regs = task_pt_regs(current);
  3577. else
  3578. regs = NULL;
  3579. }
  3580. if (regs) {
  3581. regs_user->regs = regs;
  3582. regs_user->abi = perf_reg_abi(current);
  3583. }
  3584. }
  3585. /*
  3586. * Get remaining task size from user stack pointer.
  3587. *
  3588. * It'd be better to take stack vma map and limit this more
  3589. * precisly, but there's no way to get it safely under interrupt,
  3590. * so using TASK_SIZE as limit.
  3591. */
  3592. static u64 perf_ustack_task_size(struct pt_regs *regs)
  3593. {
  3594. unsigned long addr = perf_user_stack_pointer(regs);
  3595. if (!addr || addr >= TASK_SIZE)
  3596. return 0;
  3597. return TASK_SIZE - addr;
  3598. }
  3599. static u16
  3600. perf_sample_ustack_size(u16 stack_size, u16 header_size,
  3601. struct pt_regs *regs)
  3602. {
  3603. u64 task_size;
  3604. /* No regs, no stack pointer, no dump. */
  3605. if (!regs)
  3606. return 0;
  3607. /*
  3608. * Check if we fit in with the requested stack size into the:
  3609. * - TASK_SIZE
  3610. * If we don't, we limit the size to the TASK_SIZE.
  3611. *
  3612. * - remaining sample size
  3613. * If we don't, we customize the stack size to
  3614. * fit in to the remaining sample size.
  3615. */
  3616. task_size = min((u64) USHRT_MAX, perf_ustack_task_size(regs));
  3617. stack_size = min(stack_size, (u16) task_size);
  3618. /* Current header size plus static size and dynamic size. */
  3619. header_size += 2 * sizeof(u64);
  3620. /* Do we fit in with the current stack dump size? */
  3621. if ((u16) (header_size + stack_size) < header_size) {
  3622. /*
  3623. * If we overflow the maximum size for the sample,
  3624. * we customize the stack dump size to fit in.
  3625. */
  3626. stack_size = USHRT_MAX - header_size - sizeof(u64);
  3627. stack_size = round_up(stack_size, sizeof(u64));
  3628. }
  3629. return stack_size;
  3630. }
  3631. static void
  3632. perf_output_sample_ustack(struct perf_output_handle *handle, u64 dump_size,
  3633. struct pt_regs *regs)
  3634. {
  3635. /* Case of a kernel thread, nothing to dump */
  3636. if (!regs) {
  3637. u64 size = 0;
  3638. perf_output_put(handle, size);
  3639. } else {
  3640. unsigned long sp;
  3641. unsigned int rem;
  3642. u64 dyn_size;
  3643. /*
  3644. * We dump:
  3645. * static size
  3646. * - the size requested by user or the best one we can fit
  3647. * in to the sample max size
  3648. * data
  3649. * - user stack dump data
  3650. * dynamic size
  3651. * - the actual dumped size
  3652. */
  3653. /* Static size. */
  3654. perf_output_put(handle, dump_size);
  3655. /* Data. */
  3656. sp = perf_user_stack_pointer(regs);
  3657. rem = __output_copy_user(handle, (void *) sp, dump_size);
  3658. dyn_size = dump_size - rem;
  3659. perf_output_skip(handle, rem);
  3660. /* Dynamic size. */
  3661. perf_output_put(handle, dyn_size);
  3662. }
  3663. }
  3664. static void __perf_event_header__init_id(struct perf_event_header *header,
  3665. struct perf_sample_data *data,
  3666. struct perf_event *event)
  3667. {
  3668. u64 sample_type = event->attr.sample_type;
  3669. data->type = sample_type;
  3670. header->size += event->id_header_size;
  3671. if (sample_type & PERF_SAMPLE_TID) {
  3672. /* namespace issues */
  3673. data->tid_entry.pid = perf_event_pid(event, current);
  3674. data->tid_entry.tid = perf_event_tid(event, current);
  3675. }
  3676. if (sample_type & PERF_SAMPLE_TIME)
  3677. data->time = perf_clock();
  3678. if (sample_type & (PERF_SAMPLE_ID | PERF_SAMPLE_IDENTIFIER))
  3679. data->id = primary_event_id(event);
  3680. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3681. data->stream_id = event->id;
  3682. if (sample_type & PERF_SAMPLE_CPU) {
  3683. data->cpu_entry.cpu = raw_smp_processor_id();
  3684. data->cpu_entry.reserved = 0;
  3685. }
  3686. }
  3687. void perf_event_header__init_id(struct perf_event_header *header,
  3688. struct perf_sample_data *data,
  3689. struct perf_event *event)
  3690. {
  3691. if (event->attr.sample_id_all)
  3692. __perf_event_header__init_id(header, data, event);
  3693. }
  3694. static void __perf_event__output_id_sample(struct perf_output_handle *handle,
  3695. struct perf_sample_data *data)
  3696. {
  3697. u64 sample_type = data->type;
  3698. if (sample_type & PERF_SAMPLE_TID)
  3699. perf_output_put(handle, data->tid_entry);
  3700. if (sample_type & PERF_SAMPLE_TIME)
  3701. perf_output_put(handle, data->time);
  3702. if (sample_type & PERF_SAMPLE_ID)
  3703. perf_output_put(handle, data->id);
  3704. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3705. perf_output_put(handle, data->stream_id);
  3706. if (sample_type & PERF_SAMPLE_CPU)
  3707. perf_output_put(handle, data->cpu_entry);
  3708. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  3709. perf_output_put(handle, data->id);
  3710. }
  3711. void perf_event__output_id_sample(struct perf_event *event,
  3712. struct perf_output_handle *handle,
  3713. struct perf_sample_data *sample)
  3714. {
  3715. if (event->attr.sample_id_all)
  3716. __perf_event__output_id_sample(handle, sample);
  3717. }
  3718. static void perf_output_read_one(struct perf_output_handle *handle,
  3719. struct perf_event *event,
  3720. u64 enabled, u64 running)
  3721. {
  3722. u64 read_format = event->attr.read_format;
  3723. u64 values[4];
  3724. int n = 0;
  3725. values[n++] = perf_event_count(event);
  3726. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  3727. values[n++] = enabled +
  3728. atomic64_read(&event->child_total_time_enabled);
  3729. }
  3730. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  3731. values[n++] = running +
  3732. atomic64_read(&event->child_total_time_running);
  3733. }
  3734. if (read_format & PERF_FORMAT_ID)
  3735. values[n++] = primary_event_id(event);
  3736. __output_copy(handle, values, n * sizeof(u64));
  3737. }
  3738. /*
  3739. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  3740. */
  3741. static void perf_output_read_group(struct perf_output_handle *handle,
  3742. struct perf_event *event,
  3743. u64 enabled, u64 running)
  3744. {
  3745. struct perf_event *leader = event->group_leader, *sub;
  3746. u64 read_format = event->attr.read_format;
  3747. u64 values[5];
  3748. int n = 0;
  3749. values[n++] = 1 + leader->nr_siblings;
  3750. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  3751. values[n++] = enabled;
  3752. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  3753. values[n++] = running;
  3754. if (leader != event)
  3755. leader->pmu->read(leader);
  3756. values[n++] = perf_event_count(leader);
  3757. if (read_format & PERF_FORMAT_ID)
  3758. values[n++] = primary_event_id(leader);
  3759. __output_copy(handle, values, n * sizeof(u64));
  3760. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  3761. n = 0;
  3762. if ((sub != event) &&
  3763. (sub->state == PERF_EVENT_STATE_ACTIVE))
  3764. sub->pmu->read(sub);
  3765. values[n++] = perf_event_count(sub);
  3766. if (read_format & PERF_FORMAT_ID)
  3767. values[n++] = primary_event_id(sub);
  3768. __output_copy(handle, values, n * sizeof(u64));
  3769. }
  3770. }
  3771. #define PERF_FORMAT_TOTAL_TIMES (PERF_FORMAT_TOTAL_TIME_ENABLED|\
  3772. PERF_FORMAT_TOTAL_TIME_RUNNING)
  3773. static void perf_output_read(struct perf_output_handle *handle,
  3774. struct perf_event *event)
  3775. {
  3776. u64 enabled = 0, running = 0, now;
  3777. u64 read_format = event->attr.read_format;
  3778. /*
  3779. * compute total_time_enabled, total_time_running
  3780. * based on snapshot values taken when the event
  3781. * was last scheduled in.
  3782. *
  3783. * we cannot simply called update_context_time()
  3784. * because of locking issue as we are called in
  3785. * NMI context
  3786. */
  3787. if (read_format & PERF_FORMAT_TOTAL_TIMES)
  3788. calc_timer_values(event, &now, &enabled, &running);
  3789. if (event->attr.read_format & PERF_FORMAT_GROUP)
  3790. perf_output_read_group(handle, event, enabled, running);
  3791. else
  3792. perf_output_read_one(handle, event, enabled, running);
  3793. }
  3794. void perf_output_sample(struct perf_output_handle *handle,
  3795. struct perf_event_header *header,
  3796. struct perf_sample_data *data,
  3797. struct perf_event *event)
  3798. {
  3799. u64 sample_type = data->type;
  3800. perf_output_put(handle, *header);
  3801. if (sample_type & PERF_SAMPLE_IDENTIFIER)
  3802. perf_output_put(handle, data->id);
  3803. if (sample_type & PERF_SAMPLE_IP)
  3804. perf_output_put(handle, data->ip);
  3805. if (sample_type & PERF_SAMPLE_TID)
  3806. perf_output_put(handle, data->tid_entry);
  3807. if (sample_type & PERF_SAMPLE_TIME)
  3808. perf_output_put(handle, data->time);
  3809. if (sample_type & PERF_SAMPLE_ADDR)
  3810. perf_output_put(handle, data->addr);
  3811. if (sample_type & PERF_SAMPLE_ID)
  3812. perf_output_put(handle, data->id);
  3813. if (sample_type & PERF_SAMPLE_STREAM_ID)
  3814. perf_output_put(handle, data->stream_id);
  3815. if (sample_type & PERF_SAMPLE_CPU)
  3816. perf_output_put(handle, data->cpu_entry);
  3817. if (sample_type & PERF_SAMPLE_PERIOD)
  3818. perf_output_put(handle, data->period);
  3819. if (sample_type & PERF_SAMPLE_READ)
  3820. perf_output_read(handle, event);
  3821. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3822. if (data->callchain) {
  3823. int size = 1;
  3824. if (data->callchain)
  3825. size += data->callchain->nr;
  3826. size *= sizeof(u64);
  3827. __output_copy(handle, data->callchain, size);
  3828. } else {
  3829. u64 nr = 0;
  3830. perf_output_put(handle, nr);
  3831. }
  3832. }
  3833. if (sample_type & PERF_SAMPLE_RAW) {
  3834. if (data->raw) {
  3835. perf_output_put(handle, data->raw->size);
  3836. __output_copy(handle, data->raw->data,
  3837. data->raw->size);
  3838. } else {
  3839. struct {
  3840. u32 size;
  3841. u32 data;
  3842. } raw = {
  3843. .size = sizeof(u32),
  3844. .data = 0,
  3845. };
  3846. perf_output_put(handle, raw);
  3847. }
  3848. }
  3849. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  3850. if (data->br_stack) {
  3851. size_t size;
  3852. size = data->br_stack->nr
  3853. * sizeof(struct perf_branch_entry);
  3854. perf_output_put(handle, data->br_stack->nr);
  3855. perf_output_copy(handle, data->br_stack->entries, size);
  3856. } else {
  3857. /*
  3858. * we always store at least the value of nr
  3859. */
  3860. u64 nr = 0;
  3861. perf_output_put(handle, nr);
  3862. }
  3863. }
  3864. if (sample_type & PERF_SAMPLE_REGS_USER) {
  3865. u64 abi = data->regs_user.abi;
  3866. /*
  3867. * If there are no regs to dump, notice it through
  3868. * first u64 being zero (PERF_SAMPLE_REGS_ABI_NONE).
  3869. */
  3870. perf_output_put(handle, abi);
  3871. if (abi) {
  3872. u64 mask = event->attr.sample_regs_user;
  3873. perf_output_sample_regs(handle,
  3874. data->regs_user.regs,
  3875. mask);
  3876. }
  3877. }
  3878. if (sample_type & PERF_SAMPLE_STACK_USER) {
  3879. perf_output_sample_ustack(handle,
  3880. data->stack_user_size,
  3881. data->regs_user.regs);
  3882. }
  3883. if (sample_type & PERF_SAMPLE_WEIGHT)
  3884. perf_output_put(handle, data->weight);
  3885. if (sample_type & PERF_SAMPLE_DATA_SRC)
  3886. perf_output_put(handle, data->data_src.val);
  3887. if (sample_type & PERF_SAMPLE_TRANSACTION)
  3888. perf_output_put(handle, data->txn);
  3889. if (!event->attr.watermark) {
  3890. int wakeup_events = event->attr.wakeup_events;
  3891. if (wakeup_events) {
  3892. struct ring_buffer *rb = handle->rb;
  3893. int events = local_inc_return(&rb->events);
  3894. if (events >= wakeup_events) {
  3895. local_sub(wakeup_events, &rb->events);
  3896. local_inc(&rb->wakeup);
  3897. }
  3898. }
  3899. }
  3900. }
  3901. void perf_prepare_sample(struct perf_event_header *header,
  3902. struct perf_sample_data *data,
  3903. struct perf_event *event,
  3904. struct pt_regs *regs)
  3905. {
  3906. u64 sample_type = event->attr.sample_type;
  3907. header->type = PERF_RECORD_SAMPLE;
  3908. header->size = sizeof(*header) + event->header_size;
  3909. header->misc = 0;
  3910. header->misc |= perf_misc_flags(regs);
  3911. __perf_event_header__init_id(header, data, event);
  3912. if (sample_type & PERF_SAMPLE_IP)
  3913. data->ip = perf_instruction_pointer(regs);
  3914. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  3915. int size = 1;
  3916. data->callchain = perf_callchain(event, regs);
  3917. if (data->callchain)
  3918. size += data->callchain->nr;
  3919. header->size += size * sizeof(u64);
  3920. }
  3921. if (sample_type & PERF_SAMPLE_RAW) {
  3922. int size = sizeof(u32);
  3923. if (data->raw)
  3924. size += data->raw->size;
  3925. else
  3926. size += sizeof(u32);
  3927. WARN_ON_ONCE(size & (sizeof(u64)-1));
  3928. header->size += size;
  3929. }
  3930. if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
  3931. int size = sizeof(u64); /* nr */
  3932. if (data->br_stack) {
  3933. size += data->br_stack->nr
  3934. * sizeof(struct perf_branch_entry);
  3935. }
  3936. header->size += size;
  3937. }
  3938. if (sample_type & PERF_SAMPLE_REGS_USER) {
  3939. /* regs dump ABI info */
  3940. int size = sizeof(u64);
  3941. perf_sample_regs_user(&data->regs_user, regs);
  3942. if (data->regs_user.regs) {
  3943. u64 mask = event->attr.sample_regs_user;
  3944. size += hweight64(mask) * sizeof(u64);
  3945. }
  3946. header->size += size;
  3947. }
  3948. if (sample_type & PERF_SAMPLE_STACK_USER) {
  3949. /*
  3950. * Either we need PERF_SAMPLE_STACK_USER bit to be allways
  3951. * processed as the last one or have additional check added
  3952. * in case new sample type is added, because we could eat
  3953. * up the rest of the sample size.
  3954. */
  3955. struct perf_regs_user *uregs = &data->regs_user;
  3956. u16 stack_size = event->attr.sample_stack_user;
  3957. u16 size = sizeof(u64);
  3958. if (!uregs->abi)
  3959. perf_sample_regs_user(uregs, regs);
  3960. stack_size = perf_sample_ustack_size(stack_size, header->size,
  3961. uregs->regs);
  3962. /*
  3963. * If there is something to dump, add space for the dump
  3964. * itself and for the field that tells the dynamic size,
  3965. * which is how many have been actually dumped.
  3966. */
  3967. if (stack_size)
  3968. size += sizeof(u64) + stack_size;
  3969. data->stack_user_size = stack_size;
  3970. header->size += size;
  3971. }
  3972. }
  3973. static void perf_event_output(struct perf_event *event,
  3974. struct perf_sample_data *data,
  3975. struct pt_regs *regs)
  3976. {
  3977. struct perf_output_handle handle;
  3978. struct perf_event_header header;
  3979. /* protect the callchain buffers */
  3980. rcu_read_lock();
  3981. perf_prepare_sample(&header, data, event, regs);
  3982. if (perf_output_begin(&handle, event, header.size))
  3983. goto exit;
  3984. perf_output_sample(&handle, &header, data, event);
  3985. perf_output_end(&handle);
  3986. exit:
  3987. rcu_read_unlock();
  3988. }
  3989. /*
  3990. * read event_id
  3991. */
  3992. struct perf_read_event {
  3993. struct perf_event_header header;
  3994. u32 pid;
  3995. u32 tid;
  3996. };
  3997. static void
  3998. perf_event_read_event(struct perf_event *event,
  3999. struct task_struct *task)
  4000. {
  4001. struct perf_output_handle handle;
  4002. struct perf_sample_data sample;
  4003. struct perf_read_event read_event = {
  4004. .header = {
  4005. .type = PERF_RECORD_READ,
  4006. .misc = 0,
  4007. .size = sizeof(read_event) + event->read_size,
  4008. },
  4009. .pid = perf_event_pid(event, task),
  4010. .tid = perf_event_tid(event, task),
  4011. };
  4012. int ret;
  4013. perf_event_header__init_id(&read_event.header, &sample, event);
  4014. ret = perf_output_begin(&handle, event, read_event.header.size);
  4015. if (ret)
  4016. return;
  4017. perf_output_put(&handle, read_event);
  4018. perf_output_read(&handle, event);
  4019. perf_event__output_id_sample(event, &handle, &sample);
  4020. perf_output_end(&handle);
  4021. }
  4022. typedef void (perf_event_aux_output_cb)(struct perf_event *event, void *data);
  4023. static void
  4024. perf_event_aux_ctx(struct perf_event_context *ctx,
  4025. perf_event_aux_output_cb output,
  4026. void *data)
  4027. {
  4028. struct perf_event *event;
  4029. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  4030. if (event->state < PERF_EVENT_STATE_INACTIVE)
  4031. continue;
  4032. if (!event_filter_match(event))
  4033. continue;
  4034. output(event, data);
  4035. }
  4036. }
  4037. static void
  4038. perf_event_aux(perf_event_aux_output_cb output, void *data,
  4039. struct perf_event_context *task_ctx)
  4040. {
  4041. struct perf_cpu_context *cpuctx;
  4042. struct perf_event_context *ctx;
  4043. struct pmu *pmu;
  4044. int ctxn;
  4045. rcu_read_lock();
  4046. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4047. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  4048. if (cpuctx->unique_pmu != pmu)
  4049. goto next;
  4050. perf_event_aux_ctx(&cpuctx->ctx, output, data);
  4051. if (task_ctx)
  4052. goto next;
  4053. ctxn = pmu->task_ctx_nr;
  4054. if (ctxn < 0)
  4055. goto next;
  4056. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  4057. if (ctx)
  4058. perf_event_aux_ctx(ctx, output, data);
  4059. next:
  4060. put_cpu_ptr(pmu->pmu_cpu_context);
  4061. }
  4062. if (task_ctx) {
  4063. preempt_disable();
  4064. perf_event_aux_ctx(task_ctx, output, data);
  4065. preempt_enable();
  4066. }
  4067. rcu_read_unlock();
  4068. }
  4069. /*
  4070. * task tracking -- fork/exit
  4071. *
  4072. * enabled by: attr.comm | attr.mmap | attr.mmap2 | attr.mmap_data | attr.task
  4073. */
  4074. struct perf_task_event {
  4075. struct task_struct *task;
  4076. struct perf_event_context *task_ctx;
  4077. struct {
  4078. struct perf_event_header header;
  4079. u32 pid;
  4080. u32 ppid;
  4081. u32 tid;
  4082. u32 ptid;
  4083. u64 time;
  4084. } event_id;
  4085. };
  4086. static int perf_event_task_match(struct perf_event *event)
  4087. {
  4088. return event->attr.comm || event->attr.mmap ||
  4089. event->attr.mmap2 || event->attr.mmap_data ||
  4090. event->attr.task;
  4091. }
  4092. static void perf_event_task_output(struct perf_event *event,
  4093. void *data)
  4094. {
  4095. struct perf_task_event *task_event = data;
  4096. struct perf_output_handle handle;
  4097. struct perf_sample_data sample;
  4098. struct task_struct *task = task_event->task;
  4099. int ret, size = task_event->event_id.header.size;
  4100. if (!perf_event_task_match(event))
  4101. return;
  4102. perf_event_header__init_id(&task_event->event_id.header, &sample, event);
  4103. ret = perf_output_begin(&handle, event,
  4104. task_event->event_id.header.size);
  4105. if (ret)
  4106. goto out;
  4107. task_event->event_id.pid = perf_event_pid(event, task);
  4108. task_event->event_id.ppid = perf_event_pid(event, current);
  4109. task_event->event_id.tid = perf_event_tid(event, task);
  4110. task_event->event_id.ptid = perf_event_tid(event, current);
  4111. perf_output_put(&handle, task_event->event_id);
  4112. perf_event__output_id_sample(event, &handle, &sample);
  4113. perf_output_end(&handle);
  4114. out:
  4115. task_event->event_id.header.size = size;
  4116. }
  4117. static void perf_event_task(struct task_struct *task,
  4118. struct perf_event_context *task_ctx,
  4119. int new)
  4120. {
  4121. struct perf_task_event task_event;
  4122. if (!atomic_read(&nr_comm_events) &&
  4123. !atomic_read(&nr_mmap_events) &&
  4124. !atomic_read(&nr_task_events))
  4125. return;
  4126. task_event = (struct perf_task_event){
  4127. .task = task,
  4128. .task_ctx = task_ctx,
  4129. .event_id = {
  4130. .header = {
  4131. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  4132. .misc = 0,
  4133. .size = sizeof(task_event.event_id),
  4134. },
  4135. /* .pid */
  4136. /* .ppid */
  4137. /* .tid */
  4138. /* .ptid */
  4139. .time = perf_clock(),
  4140. },
  4141. };
  4142. perf_event_aux(perf_event_task_output,
  4143. &task_event,
  4144. task_ctx);
  4145. }
  4146. void perf_event_fork(struct task_struct *task)
  4147. {
  4148. perf_event_task(task, NULL, 1);
  4149. }
  4150. /*
  4151. * comm tracking
  4152. */
  4153. struct perf_comm_event {
  4154. struct task_struct *task;
  4155. char *comm;
  4156. int comm_size;
  4157. struct {
  4158. struct perf_event_header header;
  4159. u32 pid;
  4160. u32 tid;
  4161. } event_id;
  4162. };
  4163. static int perf_event_comm_match(struct perf_event *event)
  4164. {
  4165. return event->attr.comm;
  4166. }
  4167. static void perf_event_comm_output(struct perf_event *event,
  4168. void *data)
  4169. {
  4170. struct perf_comm_event *comm_event = data;
  4171. struct perf_output_handle handle;
  4172. struct perf_sample_data sample;
  4173. int size = comm_event->event_id.header.size;
  4174. int ret;
  4175. if (!perf_event_comm_match(event))
  4176. return;
  4177. perf_event_header__init_id(&comm_event->event_id.header, &sample, event);
  4178. ret = perf_output_begin(&handle, event,
  4179. comm_event->event_id.header.size);
  4180. if (ret)
  4181. goto out;
  4182. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  4183. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  4184. perf_output_put(&handle, comm_event->event_id);
  4185. __output_copy(&handle, comm_event->comm,
  4186. comm_event->comm_size);
  4187. perf_event__output_id_sample(event, &handle, &sample);
  4188. perf_output_end(&handle);
  4189. out:
  4190. comm_event->event_id.header.size = size;
  4191. }
  4192. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  4193. {
  4194. char comm[TASK_COMM_LEN];
  4195. unsigned int size;
  4196. memset(comm, 0, sizeof(comm));
  4197. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  4198. size = ALIGN(strlen(comm)+1, sizeof(u64));
  4199. comm_event->comm = comm;
  4200. comm_event->comm_size = size;
  4201. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  4202. perf_event_aux(perf_event_comm_output,
  4203. comm_event,
  4204. NULL);
  4205. }
  4206. void perf_event_comm(struct task_struct *task, bool exec)
  4207. {
  4208. struct perf_comm_event comm_event;
  4209. if (!atomic_read(&nr_comm_events))
  4210. return;
  4211. comm_event = (struct perf_comm_event){
  4212. .task = task,
  4213. /* .comm */
  4214. /* .comm_size */
  4215. .event_id = {
  4216. .header = {
  4217. .type = PERF_RECORD_COMM,
  4218. .misc = exec ? PERF_RECORD_MISC_COMM_EXEC : 0,
  4219. /* .size */
  4220. },
  4221. /* .pid */
  4222. /* .tid */
  4223. },
  4224. };
  4225. perf_event_comm_event(&comm_event);
  4226. }
  4227. /*
  4228. * mmap tracking
  4229. */
  4230. struct perf_mmap_event {
  4231. struct vm_area_struct *vma;
  4232. const char *file_name;
  4233. int file_size;
  4234. int maj, min;
  4235. u64 ino;
  4236. u64 ino_generation;
  4237. u32 prot, flags;
  4238. struct {
  4239. struct perf_event_header header;
  4240. u32 pid;
  4241. u32 tid;
  4242. u64 start;
  4243. u64 len;
  4244. u64 pgoff;
  4245. } event_id;
  4246. };
  4247. static int perf_event_mmap_match(struct perf_event *event,
  4248. void *data)
  4249. {
  4250. struct perf_mmap_event *mmap_event = data;
  4251. struct vm_area_struct *vma = mmap_event->vma;
  4252. int executable = vma->vm_flags & VM_EXEC;
  4253. return (!executable && event->attr.mmap_data) ||
  4254. (executable && (event->attr.mmap || event->attr.mmap2));
  4255. }
  4256. static void perf_event_mmap_output(struct perf_event *event,
  4257. void *data)
  4258. {
  4259. struct perf_mmap_event *mmap_event = data;
  4260. struct perf_output_handle handle;
  4261. struct perf_sample_data sample;
  4262. int size = mmap_event->event_id.header.size;
  4263. int ret;
  4264. if (!perf_event_mmap_match(event, data))
  4265. return;
  4266. if (event->attr.mmap2) {
  4267. mmap_event->event_id.header.type = PERF_RECORD_MMAP2;
  4268. mmap_event->event_id.header.size += sizeof(mmap_event->maj);
  4269. mmap_event->event_id.header.size += sizeof(mmap_event->min);
  4270. mmap_event->event_id.header.size += sizeof(mmap_event->ino);
  4271. mmap_event->event_id.header.size += sizeof(mmap_event->ino_generation);
  4272. mmap_event->event_id.header.size += sizeof(mmap_event->prot);
  4273. mmap_event->event_id.header.size += sizeof(mmap_event->flags);
  4274. }
  4275. perf_event_header__init_id(&mmap_event->event_id.header, &sample, event);
  4276. ret = perf_output_begin(&handle, event,
  4277. mmap_event->event_id.header.size);
  4278. if (ret)
  4279. goto out;
  4280. mmap_event->event_id.pid = perf_event_pid(event, current);
  4281. mmap_event->event_id.tid = perf_event_tid(event, current);
  4282. perf_output_put(&handle, mmap_event->event_id);
  4283. if (event->attr.mmap2) {
  4284. perf_output_put(&handle, mmap_event->maj);
  4285. perf_output_put(&handle, mmap_event->min);
  4286. perf_output_put(&handle, mmap_event->ino);
  4287. perf_output_put(&handle, mmap_event->ino_generation);
  4288. perf_output_put(&handle, mmap_event->prot);
  4289. perf_output_put(&handle, mmap_event->flags);
  4290. }
  4291. __output_copy(&handle, mmap_event->file_name,
  4292. mmap_event->file_size);
  4293. perf_event__output_id_sample(event, &handle, &sample);
  4294. perf_output_end(&handle);
  4295. out:
  4296. mmap_event->event_id.header.size = size;
  4297. }
  4298. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  4299. {
  4300. struct vm_area_struct *vma = mmap_event->vma;
  4301. struct file *file = vma->vm_file;
  4302. int maj = 0, min = 0;
  4303. u64 ino = 0, gen = 0;
  4304. u32 prot = 0, flags = 0;
  4305. unsigned int size;
  4306. char tmp[16];
  4307. char *buf = NULL;
  4308. char *name;
  4309. if (file) {
  4310. struct inode *inode;
  4311. dev_t dev;
  4312. buf = kmalloc(PATH_MAX, GFP_KERNEL);
  4313. if (!buf) {
  4314. name = "//enomem";
  4315. goto cpy_name;
  4316. }
  4317. /*
  4318. * d_path() works from the end of the rb backwards, so we
  4319. * need to add enough zero bytes after the string to handle
  4320. * the 64bit alignment we do later.
  4321. */
  4322. name = d_path(&file->f_path, buf, PATH_MAX - sizeof(u64));
  4323. if (IS_ERR(name)) {
  4324. name = "//toolong";
  4325. goto cpy_name;
  4326. }
  4327. inode = file_inode(vma->vm_file);
  4328. dev = inode->i_sb->s_dev;
  4329. ino = inode->i_ino;
  4330. gen = inode->i_generation;
  4331. maj = MAJOR(dev);
  4332. min = MINOR(dev);
  4333. if (vma->vm_flags & VM_READ)
  4334. prot |= PROT_READ;
  4335. if (vma->vm_flags & VM_WRITE)
  4336. prot |= PROT_WRITE;
  4337. if (vma->vm_flags & VM_EXEC)
  4338. prot |= PROT_EXEC;
  4339. if (vma->vm_flags & VM_MAYSHARE)
  4340. flags = MAP_SHARED;
  4341. else
  4342. flags = MAP_PRIVATE;
  4343. if (vma->vm_flags & VM_DENYWRITE)
  4344. flags |= MAP_DENYWRITE;
  4345. if (vma->vm_flags & VM_MAYEXEC)
  4346. flags |= MAP_EXECUTABLE;
  4347. if (vma->vm_flags & VM_LOCKED)
  4348. flags |= MAP_LOCKED;
  4349. if (vma->vm_flags & VM_HUGETLB)
  4350. flags |= MAP_HUGETLB;
  4351. goto got_name;
  4352. } else {
  4353. if (vma->vm_ops && vma->vm_ops->name) {
  4354. name = (char *) vma->vm_ops->name(vma);
  4355. if (name)
  4356. goto cpy_name;
  4357. }
  4358. name = (char *)arch_vma_name(vma);
  4359. if (name)
  4360. goto cpy_name;
  4361. if (vma->vm_start <= vma->vm_mm->start_brk &&
  4362. vma->vm_end >= vma->vm_mm->brk) {
  4363. name = "[heap]";
  4364. goto cpy_name;
  4365. }
  4366. if (vma->vm_start <= vma->vm_mm->start_stack &&
  4367. vma->vm_end >= vma->vm_mm->start_stack) {
  4368. name = "[stack]";
  4369. goto cpy_name;
  4370. }
  4371. name = "//anon";
  4372. goto cpy_name;
  4373. }
  4374. cpy_name:
  4375. strlcpy(tmp, name, sizeof(tmp));
  4376. name = tmp;
  4377. got_name:
  4378. /*
  4379. * Since our buffer works in 8 byte units we need to align our string
  4380. * size to a multiple of 8. However, we must guarantee the tail end is
  4381. * zero'd out to avoid leaking random bits to userspace.
  4382. */
  4383. size = strlen(name)+1;
  4384. while (!IS_ALIGNED(size, sizeof(u64)))
  4385. name[size++] = '\0';
  4386. mmap_event->file_name = name;
  4387. mmap_event->file_size = size;
  4388. mmap_event->maj = maj;
  4389. mmap_event->min = min;
  4390. mmap_event->ino = ino;
  4391. mmap_event->ino_generation = gen;
  4392. mmap_event->prot = prot;
  4393. mmap_event->flags = flags;
  4394. if (!(vma->vm_flags & VM_EXEC))
  4395. mmap_event->event_id.header.misc |= PERF_RECORD_MISC_MMAP_DATA;
  4396. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  4397. perf_event_aux(perf_event_mmap_output,
  4398. mmap_event,
  4399. NULL);
  4400. kfree(buf);
  4401. }
  4402. void perf_event_mmap(struct vm_area_struct *vma)
  4403. {
  4404. struct perf_mmap_event mmap_event;
  4405. if (!atomic_read(&nr_mmap_events))
  4406. return;
  4407. mmap_event = (struct perf_mmap_event){
  4408. .vma = vma,
  4409. /* .file_name */
  4410. /* .file_size */
  4411. .event_id = {
  4412. .header = {
  4413. .type = PERF_RECORD_MMAP,
  4414. .misc = PERF_RECORD_MISC_USER,
  4415. /* .size */
  4416. },
  4417. /* .pid */
  4418. /* .tid */
  4419. .start = vma->vm_start,
  4420. .len = vma->vm_end - vma->vm_start,
  4421. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  4422. },
  4423. /* .maj (attr_mmap2 only) */
  4424. /* .min (attr_mmap2 only) */
  4425. /* .ino (attr_mmap2 only) */
  4426. /* .ino_generation (attr_mmap2 only) */
  4427. /* .prot (attr_mmap2 only) */
  4428. /* .flags (attr_mmap2 only) */
  4429. };
  4430. perf_event_mmap_event(&mmap_event);
  4431. }
  4432. /*
  4433. * IRQ throttle logging
  4434. */
  4435. static void perf_log_throttle(struct perf_event *event, int enable)
  4436. {
  4437. struct perf_output_handle handle;
  4438. struct perf_sample_data sample;
  4439. int ret;
  4440. struct {
  4441. struct perf_event_header header;
  4442. u64 time;
  4443. u64 id;
  4444. u64 stream_id;
  4445. } throttle_event = {
  4446. .header = {
  4447. .type = PERF_RECORD_THROTTLE,
  4448. .misc = 0,
  4449. .size = sizeof(throttle_event),
  4450. },
  4451. .time = perf_clock(),
  4452. .id = primary_event_id(event),
  4453. .stream_id = event->id,
  4454. };
  4455. if (enable)
  4456. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  4457. perf_event_header__init_id(&throttle_event.header, &sample, event);
  4458. ret = perf_output_begin(&handle, event,
  4459. throttle_event.header.size);
  4460. if (ret)
  4461. return;
  4462. perf_output_put(&handle, throttle_event);
  4463. perf_event__output_id_sample(event, &handle, &sample);
  4464. perf_output_end(&handle);
  4465. }
  4466. /*
  4467. * Generic event overflow handling, sampling.
  4468. */
  4469. static int __perf_event_overflow(struct perf_event *event,
  4470. int throttle, struct perf_sample_data *data,
  4471. struct pt_regs *regs)
  4472. {
  4473. int events = atomic_read(&event->event_limit);
  4474. struct hw_perf_event *hwc = &event->hw;
  4475. u64 seq;
  4476. int ret = 0;
  4477. /*
  4478. * Non-sampling counters might still use the PMI to fold short
  4479. * hardware counters, ignore those.
  4480. */
  4481. if (unlikely(!is_sampling_event(event)))
  4482. return 0;
  4483. seq = __this_cpu_read(perf_throttled_seq);
  4484. if (seq != hwc->interrupts_seq) {
  4485. hwc->interrupts_seq = seq;
  4486. hwc->interrupts = 1;
  4487. } else {
  4488. hwc->interrupts++;
  4489. if (unlikely(throttle
  4490. && hwc->interrupts >= max_samples_per_tick)) {
  4491. __this_cpu_inc(perf_throttled_count);
  4492. hwc->interrupts = MAX_INTERRUPTS;
  4493. perf_log_throttle(event, 0);
  4494. tick_nohz_full_kick();
  4495. ret = 1;
  4496. }
  4497. }
  4498. if (event->attr.freq) {
  4499. u64 now = perf_clock();
  4500. s64 delta = now - hwc->freq_time_stamp;
  4501. hwc->freq_time_stamp = now;
  4502. if (delta > 0 && delta < 2*TICK_NSEC)
  4503. perf_adjust_period(event, delta, hwc->last_period, true);
  4504. }
  4505. /*
  4506. * XXX event_limit might not quite work as expected on inherited
  4507. * events
  4508. */
  4509. event->pending_kill = POLL_IN;
  4510. if (events && atomic_dec_and_test(&event->event_limit)) {
  4511. ret = 1;
  4512. event->pending_kill = POLL_HUP;
  4513. event->pending_disable = 1;
  4514. irq_work_queue(&event->pending);
  4515. }
  4516. if (event->overflow_handler)
  4517. event->overflow_handler(event, data, regs);
  4518. else
  4519. perf_event_output(event, data, regs);
  4520. if (event->fasync && event->pending_kill) {
  4521. event->pending_wakeup = 1;
  4522. irq_work_queue(&event->pending);
  4523. }
  4524. return ret;
  4525. }
  4526. int perf_event_overflow(struct perf_event *event,
  4527. struct perf_sample_data *data,
  4528. struct pt_regs *regs)
  4529. {
  4530. return __perf_event_overflow(event, 1, data, regs);
  4531. }
  4532. /*
  4533. * Generic software event infrastructure
  4534. */
  4535. struct swevent_htable {
  4536. struct swevent_hlist *swevent_hlist;
  4537. struct mutex hlist_mutex;
  4538. int hlist_refcount;
  4539. /* Recursion avoidance in each contexts */
  4540. int recursion[PERF_NR_CONTEXTS];
  4541. /* Keeps track of cpu being initialized/exited */
  4542. bool online;
  4543. };
  4544. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  4545. /*
  4546. * We directly increment event->count and keep a second value in
  4547. * event->hw.period_left to count intervals. This period event
  4548. * is kept in the range [-sample_period, 0] so that we can use the
  4549. * sign as trigger.
  4550. */
  4551. u64 perf_swevent_set_period(struct perf_event *event)
  4552. {
  4553. struct hw_perf_event *hwc = &event->hw;
  4554. u64 period = hwc->last_period;
  4555. u64 nr, offset;
  4556. s64 old, val;
  4557. hwc->last_period = hwc->sample_period;
  4558. again:
  4559. old = val = local64_read(&hwc->period_left);
  4560. if (val < 0)
  4561. return 0;
  4562. nr = div64_u64(period + val, period);
  4563. offset = nr * period;
  4564. val -= offset;
  4565. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  4566. goto again;
  4567. return nr;
  4568. }
  4569. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  4570. struct perf_sample_data *data,
  4571. struct pt_regs *regs)
  4572. {
  4573. struct hw_perf_event *hwc = &event->hw;
  4574. int throttle = 0;
  4575. if (!overflow)
  4576. overflow = perf_swevent_set_period(event);
  4577. if (hwc->interrupts == MAX_INTERRUPTS)
  4578. return;
  4579. for (; overflow; overflow--) {
  4580. if (__perf_event_overflow(event, throttle,
  4581. data, regs)) {
  4582. /*
  4583. * We inhibit the overflow from happening when
  4584. * hwc->interrupts == MAX_INTERRUPTS.
  4585. */
  4586. break;
  4587. }
  4588. throttle = 1;
  4589. }
  4590. }
  4591. static void perf_swevent_event(struct perf_event *event, u64 nr,
  4592. struct perf_sample_data *data,
  4593. struct pt_regs *regs)
  4594. {
  4595. struct hw_perf_event *hwc = &event->hw;
  4596. local64_add(nr, &event->count);
  4597. if (!regs)
  4598. return;
  4599. if (!is_sampling_event(event))
  4600. return;
  4601. if ((event->attr.sample_type & PERF_SAMPLE_PERIOD) && !event->attr.freq) {
  4602. data->period = nr;
  4603. return perf_swevent_overflow(event, 1, data, regs);
  4604. } else
  4605. data->period = event->hw.last_period;
  4606. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  4607. return perf_swevent_overflow(event, 1, data, regs);
  4608. if (local64_add_negative(nr, &hwc->period_left))
  4609. return;
  4610. perf_swevent_overflow(event, 0, data, regs);
  4611. }
  4612. static int perf_exclude_event(struct perf_event *event,
  4613. struct pt_regs *regs)
  4614. {
  4615. if (event->hw.state & PERF_HES_STOPPED)
  4616. return 1;
  4617. if (regs) {
  4618. if (event->attr.exclude_user && user_mode(regs))
  4619. return 1;
  4620. if (event->attr.exclude_kernel && !user_mode(regs))
  4621. return 1;
  4622. }
  4623. return 0;
  4624. }
  4625. static int perf_swevent_match(struct perf_event *event,
  4626. enum perf_type_id type,
  4627. u32 event_id,
  4628. struct perf_sample_data *data,
  4629. struct pt_regs *regs)
  4630. {
  4631. if (event->attr.type != type)
  4632. return 0;
  4633. if (event->attr.config != event_id)
  4634. return 0;
  4635. if (perf_exclude_event(event, regs))
  4636. return 0;
  4637. return 1;
  4638. }
  4639. static inline u64 swevent_hash(u64 type, u32 event_id)
  4640. {
  4641. u64 val = event_id | (type << 32);
  4642. return hash_64(val, SWEVENT_HLIST_BITS);
  4643. }
  4644. static inline struct hlist_head *
  4645. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  4646. {
  4647. u64 hash = swevent_hash(type, event_id);
  4648. return &hlist->heads[hash];
  4649. }
  4650. /* For the read side: events when they trigger */
  4651. static inline struct hlist_head *
  4652. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  4653. {
  4654. struct swevent_hlist *hlist;
  4655. hlist = rcu_dereference(swhash->swevent_hlist);
  4656. if (!hlist)
  4657. return NULL;
  4658. return __find_swevent_head(hlist, type, event_id);
  4659. }
  4660. /* For the event head insertion and removal in the hlist */
  4661. static inline struct hlist_head *
  4662. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  4663. {
  4664. struct swevent_hlist *hlist;
  4665. u32 event_id = event->attr.config;
  4666. u64 type = event->attr.type;
  4667. /*
  4668. * Event scheduling is always serialized against hlist allocation
  4669. * and release. Which makes the protected version suitable here.
  4670. * The context lock guarantees that.
  4671. */
  4672. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  4673. lockdep_is_held(&event->ctx->lock));
  4674. if (!hlist)
  4675. return NULL;
  4676. return __find_swevent_head(hlist, type, event_id);
  4677. }
  4678. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  4679. u64 nr,
  4680. struct perf_sample_data *data,
  4681. struct pt_regs *regs)
  4682. {
  4683. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  4684. struct perf_event *event;
  4685. struct hlist_head *head;
  4686. rcu_read_lock();
  4687. head = find_swevent_head_rcu(swhash, type, event_id);
  4688. if (!head)
  4689. goto end;
  4690. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  4691. if (perf_swevent_match(event, type, event_id, data, regs))
  4692. perf_swevent_event(event, nr, data, regs);
  4693. }
  4694. end:
  4695. rcu_read_unlock();
  4696. }
  4697. int perf_swevent_get_recursion_context(void)
  4698. {
  4699. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  4700. return get_recursion_context(swhash->recursion);
  4701. }
  4702. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  4703. inline void perf_swevent_put_recursion_context(int rctx)
  4704. {
  4705. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  4706. put_recursion_context(swhash->recursion, rctx);
  4707. }
  4708. void __perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
  4709. {
  4710. struct perf_sample_data data;
  4711. int rctx;
  4712. preempt_disable_notrace();
  4713. rctx = perf_swevent_get_recursion_context();
  4714. if (rctx < 0)
  4715. return;
  4716. perf_sample_data_init(&data, addr, 0);
  4717. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, &data, regs);
  4718. perf_swevent_put_recursion_context(rctx);
  4719. preempt_enable_notrace();
  4720. }
  4721. static void perf_swevent_read(struct perf_event *event)
  4722. {
  4723. }
  4724. static int perf_swevent_add(struct perf_event *event, int flags)
  4725. {
  4726. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  4727. struct hw_perf_event *hwc = &event->hw;
  4728. struct hlist_head *head;
  4729. if (is_sampling_event(event)) {
  4730. hwc->last_period = hwc->sample_period;
  4731. perf_swevent_set_period(event);
  4732. }
  4733. hwc->state = !(flags & PERF_EF_START);
  4734. head = find_swevent_head(swhash, event);
  4735. if (!head) {
  4736. /*
  4737. * We can race with cpu hotplug code. Do not
  4738. * WARN if the cpu just got unplugged.
  4739. */
  4740. WARN_ON_ONCE(swhash->online);
  4741. return -EINVAL;
  4742. }
  4743. hlist_add_head_rcu(&event->hlist_entry, head);
  4744. return 0;
  4745. }
  4746. static void perf_swevent_del(struct perf_event *event, int flags)
  4747. {
  4748. hlist_del_rcu(&event->hlist_entry);
  4749. }
  4750. static void perf_swevent_start(struct perf_event *event, int flags)
  4751. {
  4752. event->hw.state = 0;
  4753. }
  4754. static void perf_swevent_stop(struct perf_event *event, int flags)
  4755. {
  4756. event->hw.state = PERF_HES_STOPPED;
  4757. }
  4758. /* Deref the hlist from the update side */
  4759. static inline struct swevent_hlist *
  4760. swevent_hlist_deref(struct swevent_htable *swhash)
  4761. {
  4762. return rcu_dereference_protected(swhash->swevent_hlist,
  4763. lockdep_is_held(&swhash->hlist_mutex));
  4764. }
  4765. static void swevent_hlist_release(struct swevent_htable *swhash)
  4766. {
  4767. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  4768. if (!hlist)
  4769. return;
  4770. rcu_assign_pointer(swhash->swevent_hlist, NULL);
  4771. kfree_rcu(hlist, rcu_head);
  4772. }
  4773. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  4774. {
  4775. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  4776. mutex_lock(&swhash->hlist_mutex);
  4777. if (!--swhash->hlist_refcount)
  4778. swevent_hlist_release(swhash);
  4779. mutex_unlock(&swhash->hlist_mutex);
  4780. }
  4781. static void swevent_hlist_put(struct perf_event *event)
  4782. {
  4783. int cpu;
  4784. for_each_possible_cpu(cpu)
  4785. swevent_hlist_put_cpu(event, cpu);
  4786. }
  4787. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  4788. {
  4789. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  4790. int err = 0;
  4791. mutex_lock(&swhash->hlist_mutex);
  4792. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  4793. struct swevent_hlist *hlist;
  4794. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  4795. if (!hlist) {
  4796. err = -ENOMEM;
  4797. goto exit;
  4798. }
  4799. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  4800. }
  4801. swhash->hlist_refcount++;
  4802. exit:
  4803. mutex_unlock(&swhash->hlist_mutex);
  4804. return err;
  4805. }
  4806. static int swevent_hlist_get(struct perf_event *event)
  4807. {
  4808. int err;
  4809. int cpu, failed_cpu;
  4810. get_online_cpus();
  4811. for_each_possible_cpu(cpu) {
  4812. err = swevent_hlist_get_cpu(event, cpu);
  4813. if (err) {
  4814. failed_cpu = cpu;
  4815. goto fail;
  4816. }
  4817. }
  4818. put_online_cpus();
  4819. return 0;
  4820. fail:
  4821. for_each_possible_cpu(cpu) {
  4822. if (cpu == failed_cpu)
  4823. break;
  4824. swevent_hlist_put_cpu(event, cpu);
  4825. }
  4826. put_online_cpus();
  4827. return err;
  4828. }
  4829. struct static_key perf_swevent_enabled[PERF_COUNT_SW_MAX];
  4830. static void sw_perf_event_destroy(struct perf_event *event)
  4831. {
  4832. u64 event_id = event->attr.config;
  4833. WARN_ON(event->parent);
  4834. static_key_slow_dec(&perf_swevent_enabled[event_id]);
  4835. swevent_hlist_put(event);
  4836. }
  4837. static int perf_swevent_init(struct perf_event *event)
  4838. {
  4839. u64 event_id = event->attr.config;
  4840. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4841. return -ENOENT;
  4842. /*
  4843. * no branch sampling for software events
  4844. */
  4845. if (has_branch_stack(event))
  4846. return -EOPNOTSUPP;
  4847. switch (event_id) {
  4848. case PERF_COUNT_SW_CPU_CLOCK:
  4849. case PERF_COUNT_SW_TASK_CLOCK:
  4850. return -ENOENT;
  4851. default:
  4852. break;
  4853. }
  4854. if (event_id >= PERF_COUNT_SW_MAX)
  4855. return -ENOENT;
  4856. if (!event->parent) {
  4857. int err;
  4858. err = swevent_hlist_get(event);
  4859. if (err)
  4860. return err;
  4861. static_key_slow_inc(&perf_swevent_enabled[event_id]);
  4862. event->destroy = sw_perf_event_destroy;
  4863. }
  4864. return 0;
  4865. }
  4866. static int perf_swevent_event_idx(struct perf_event *event)
  4867. {
  4868. return 0;
  4869. }
  4870. static struct pmu perf_swevent = {
  4871. .task_ctx_nr = perf_sw_context,
  4872. .event_init = perf_swevent_init,
  4873. .add = perf_swevent_add,
  4874. .del = perf_swevent_del,
  4875. .start = perf_swevent_start,
  4876. .stop = perf_swevent_stop,
  4877. .read = perf_swevent_read,
  4878. .event_idx = perf_swevent_event_idx,
  4879. };
  4880. #ifdef CONFIG_EVENT_TRACING
  4881. static int perf_tp_filter_match(struct perf_event *event,
  4882. struct perf_sample_data *data)
  4883. {
  4884. void *record = data->raw->data;
  4885. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  4886. return 1;
  4887. return 0;
  4888. }
  4889. static int perf_tp_event_match(struct perf_event *event,
  4890. struct perf_sample_data *data,
  4891. struct pt_regs *regs)
  4892. {
  4893. if (event->hw.state & PERF_HES_STOPPED)
  4894. return 0;
  4895. /*
  4896. * All tracepoints are from kernel-space.
  4897. */
  4898. if (event->attr.exclude_kernel)
  4899. return 0;
  4900. if (!perf_tp_filter_match(event, data))
  4901. return 0;
  4902. return 1;
  4903. }
  4904. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  4905. struct pt_regs *regs, struct hlist_head *head, int rctx,
  4906. struct task_struct *task)
  4907. {
  4908. struct perf_sample_data data;
  4909. struct perf_event *event;
  4910. struct perf_raw_record raw = {
  4911. .size = entry_size,
  4912. .data = record,
  4913. };
  4914. perf_sample_data_init(&data, addr, 0);
  4915. data.raw = &raw;
  4916. hlist_for_each_entry_rcu(event, head, hlist_entry) {
  4917. if (perf_tp_event_match(event, &data, regs))
  4918. perf_swevent_event(event, count, &data, regs);
  4919. }
  4920. /*
  4921. * If we got specified a target task, also iterate its context and
  4922. * deliver this event there too.
  4923. */
  4924. if (task && task != current) {
  4925. struct perf_event_context *ctx;
  4926. struct trace_entry *entry = record;
  4927. rcu_read_lock();
  4928. ctx = rcu_dereference(task->perf_event_ctxp[perf_sw_context]);
  4929. if (!ctx)
  4930. goto unlock;
  4931. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  4932. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4933. continue;
  4934. if (event->attr.config != entry->type)
  4935. continue;
  4936. if (perf_tp_event_match(event, &data, regs))
  4937. perf_swevent_event(event, count, &data, regs);
  4938. }
  4939. unlock:
  4940. rcu_read_unlock();
  4941. }
  4942. perf_swevent_put_recursion_context(rctx);
  4943. }
  4944. EXPORT_SYMBOL_GPL(perf_tp_event);
  4945. static void tp_perf_event_destroy(struct perf_event *event)
  4946. {
  4947. perf_trace_destroy(event);
  4948. }
  4949. static int perf_tp_event_init(struct perf_event *event)
  4950. {
  4951. int err;
  4952. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4953. return -ENOENT;
  4954. /*
  4955. * no branch sampling for tracepoint events
  4956. */
  4957. if (has_branch_stack(event))
  4958. return -EOPNOTSUPP;
  4959. err = perf_trace_init(event);
  4960. if (err)
  4961. return err;
  4962. event->destroy = tp_perf_event_destroy;
  4963. return 0;
  4964. }
  4965. static struct pmu perf_tracepoint = {
  4966. .task_ctx_nr = perf_sw_context,
  4967. .event_init = perf_tp_event_init,
  4968. .add = perf_trace_add,
  4969. .del = perf_trace_del,
  4970. .start = perf_swevent_start,
  4971. .stop = perf_swevent_stop,
  4972. .read = perf_swevent_read,
  4973. .event_idx = perf_swevent_event_idx,
  4974. };
  4975. static inline void perf_tp_register(void)
  4976. {
  4977. perf_pmu_register(&perf_tracepoint, "tracepoint", PERF_TYPE_TRACEPOINT);
  4978. }
  4979. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  4980. {
  4981. char *filter_str;
  4982. int ret;
  4983. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  4984. return -EINVAL;
  4985. filter_str = strndup_user(arg, PAGE_SIZE);
  4986. if (IS_ERR(filter_str))
  4987. return PTR_ERR(filter_str);
  4988. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  4989. kfree(filter_str);
  4990. return ret;
  4991. }
  4992. static void perf_event_free_filter(struct perf_event *event)
  4993. {
  4994. ftrace_profile_free_filter(event);
  4995. }
  4996. #else
  4997. static inline void perf_tp_register(void)
  4998. {
  4999. }
  5000. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  5001. {
  5002. return -ENOENT;
  5003. }
  5004. static void perf_event_free_filter(struct perf_event *event)
  5005. {
  5006. }
  5007. #endif /* CONFIG_EVENT_TRACING */
  5008. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  5009. void perf_bp_event(struct perf_event *bp, void *data)
  5010. {
  5011. struct perf_sample_data sample;
  5012. struct pt_regs *regs = data;
  5013. perf_sample_data_init(&sample, bp->attr.bp_addr, 0);
  5014. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  5015. perf_swevent_event(bp, 1, &sample, regs);
  5016. }
  5017. #endif
  5018. /*
  5019. * hrtimer based swevent callback
  5020. */
  5021. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  5022. {
  5023. enum hrtimer_restart ret = HRTIMER_RESTART;
  5024. struct perf_sample_data data;
  5025. struct pt_regs *regs;
  5026. struct perf_event *event;
  5027. u64 period;
  5028. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  5029. if (event->state != PERF_EVENT_STATE_ACTIVE)
  5030. return HRTIMER_NORESTART;
  5031. event->pmu->read(event);
  5032. perf_sample_data_init(&data, 0, event->hw.last_period);
  5033. regs = get_irq_regs();
  5034. if (regs && !perf_exclude_event(event, regs)) {
  5035. if (!(event->attr.exclude_idle && is_idle_task(current)))
  5036. if (__perf_event_overflow(event, 1, &data, regs))
  5037. ret = HRTIMER_NORESTART;
  5038. }
  5039. period = max_t(u64, 10000, event->hw.sample_period);
  5040. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  5041. return ret;
  5042. }
  5043. static void perf_swevent_start_hrtimer(struct perf_event *event)
  5044. {
  5045. struct hw_perf_event *hwc = &event->hw;
  5046. s64 period;
  5047. if (!is_sampling_event(event))
  5048. return;
  5049. period = local64_read(&hwc->period_left);
  5050. if (period) {
  5051. if (period < 0)
  5052. period = 10000;
  5053. local64_set(&hwc->period_left, 0);
  5054. } else {
  5055. period = max_t(u64, 10000, hwc->sample_period);
  5056. }
  5057. __hrtimer_start_range_ns(&hwc->hrtimer,
  5058. ns_to_ktime(period), 0,
  5059. HRTIMER_MODE_REL_PINNED, 0);
  5060. }
  5061. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  5062. {
  5063. struct hw_perf_event *hwc = &event->hw;
  5064. if (is_sampling_event(event)) {
  5065. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  5066. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  5067. hrtimer_cancel(&hwc->hrtimer);
  5068. }
  5069. }
  5070. static void perf_swevent_init_hrtimer(struct perf_event *event)
  5071. {
  5072. struct hw_perf_event *hwc = &event->hw;
  5073. if (!is_sampling_event(event))
  5074. return;
  5075. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  5076. hwc->hrtimer.function = perf_swevent_hrtimer;
  5077. /*
  5078. * Since hrtimers have a fixed rate, we can do a static freq->period
  5079. * mapping and avoid the whole period adjust feedback stuff.
  5080. */
  5081. if (event->attr.freq) {
  5082. long freq = event->attr.sample_freq;
  5083. event->attr.sample_period = NSEC_PER_SEC / freq;
  5084. hwc->sample_period = event->attr.sample_period;
  5085. local64_set(&hwc->period_left, hwc->sample_period);
  5086. hwc->last_period = hwc->sample_period;
  5087. event->attr.freq = 0;
  5088. }
  5089. }
  5090. /*
  5091. * Software event: cpu wall time clock
  5092. */
  5093. static void cpu_clock_event_update(struct perf_event *event)
  5094. {
  5095. s64 prev;
  5096. u64 now;
  5097. now = local_clock();
  5098. prev = local64_xchg(&event->hw.prev_count, now);
  5099. local64_add(now - prev, &event->count);
  5100. }
  5101. static void cpu_clock_event_start(struct perf_event *event, int flags)
  5102. {
  5103. local64_set(&event->hw.prev_count, local_clock());
  5104. perf_swevent_start_hrtimer(event);
  5105. }
  5106. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  5107. {
  5108. perf_swevent_cancel_hrtimer(event);
  5109. cpu_clock_event_update(event);
  5110. }
  5111. static int cpu_clock_event_add(struct perf_event *event, int flags)
  5112. {
  5113. if (flags & PERF_EF_START)
  5114. cpu_clock_event_start(event, flags);
  5115. return 0;
  5116. }
  5117. static void cpu_clock_event_del(struct perf_event *event, int flags)
  5118. {
  5119. cpu_clock_event_stop(event, flags);
  5120. }
  5121. static void cpu_clock_event_read(struct perf_event *event)
  5122. {
  5123. cpu_clock_event_update(event);
  5124. }
  5125. static int cpu_clock_event_init(struct perf_event *event)
  5126. {
  5127. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5128. return -ENOENT;
  5129. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  5130. return -ENOENT;
  5131. /*
  5132. * no branch sampling for software events
  5133. */
  5134. if (has_branch_stack(event))
  5135. return -EOPNOTSUPP;
  5136. perf_swevent_init_hrtimer(event);
  5137. return 0;
  5138. }
  5139. static struct pmu perf_cpu_clock = {
  5140. .task_ctx_nr = perf_sw_context,
  5141. .event_init = cpu_clock_event_init,
  5142. .add = cpu_clock_event_add,
  5143. .del = cpu_clock_event_del,
  5144. .start = cpu_clock_event_start,
  5145. .stop = cpu_clock_event_stop,
  5146. .read = cpu_clock_event_read,
  5147. .event_idx = perf_swevent_event_idx,
  5148. };
  5149. /*
  5150. * Software event: task time clock
  5151. */
  5152. static void task_clock_event_update(struct perf_event *event, u64 now)
  5153. {
  5154. u64 prev;
  5155. s64 delta;
  5156. prev = local64_xchg(&event->hw.prev_count, now);
  5157. delta = now - prev;
  5158. local64_add(delta, &event->count);
  5159. }
  5160. static void task_clock_event_start(struct perf_event *event, int flags)
  5161. {
  5162. local64_set(&event->hw.prev_count, event->ctx->time);
  5163. perf_swevent_start_hrtimer(event);
  5164. }
  5165. static void task_clock_event_stop(struct perf_event *event, int flags)
  5166. {
  5167. perf_swevent_cancel_hrtimer(event);
  5168. task_clock_event_update(event, event->ctx->time);
  5169. }
  5170. static int task_clock_event_add(struct perf_event *event, int flags)
  5171. {
  5172. if (flags & PERF_EF_START)
  5173. task_clock_event_start(event, flags);
  5174. return 0;
  5175. }
  5176. static void task_clock_event_del(struct perf_event *event, int flags)
  5177. {
  5178. task_clock_event_stop(event, PERF_EF_UPDATE);
  5179. }
  5180. static void task_clock_event_read(struct perf_event *event)
  5181. {
  5182. u64 now = perf_clock();
  5183. u64 delta = now - event->ctx->timestamp;
  5184. u64 time = event->ctx->time + delta;
  5185. task_clock_event_update(event, time);
  5186. }
  5187. static int task_clock_event_init(struct perf_event *event)
  5188. {
  5189. if (event->attr.type != PERF_TYPE_SOFTWARE)
  5190. return -ENOENT;
  5191. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  5192. return -ENOENT;
  5193. /*
  5194. * no branch sampling for software events
  5195. */
  5196. if (has_branch_stack(event))
  5197. return -EOPNOTSUPP;
  5198. perf_swevent_init_hrtimer(event);
  5199. return 0;
  5200. }
  5201. static struct pmu perf_task_clock = {
  5202. .task_ctx_nr = perf_sw_context,
  5203. .event_init = task_clock_event_init,
  5204. .add = task_clock_event_add,
  5205. .del = task_clock_event_del,
  5206. .start = task_clock_event_start,
  5207. .stop = task_clock_event_stop,
  5208. .read = task_clock_event_read,
  5209. .event_idx = perf_swevent_event_idx,
  5210. };
  5211. static void perf_pmu_nop_void(struct pmu *pmu)
  5212. {
  5213. }
  5214. static int perf_pmu_nop_int(struct pmu *pmu)
  5215. {
  5216. return 0;
  5217. }
  5218. static void perf_pmu_start_txn(struct pmu *pmu)
  5219. {
  5220. perf_pmu_disable(pmu);
  5221. }
  5222. static int perf_pmu_commit_txn(struct pmu *pmu)
  5223. {
  5224. perf_pmu_enable(pmu);
  5225. return 0;
  5226. }
  5227. static void perf_pmu_cancel_txn(struct pmu *pmu)
  5228. {
  5229. perf_pmu_enable(pmu);
  5230. }
  5231. static int perf_event_idx_default(struct perf_event *event)
  5232. {
  5233. return event->hw.idx + 1;
  5234. }
  5235. /*
  5236. * Ensures all contexts with the same task_ctx_nr have the same
  5237. * pmu_cpu_context too.
  5238. */
  5239. static struct perf_cpu_context __percpu *find_pmu_context(int ctxn)
  5240. {
  5241. struct pmu *pmu;
  5242. if (ctxn < 0)
  5243. return NULL;
  5244. list_for_each_entry(pmu, &pmus, entry) {
  5245. if (pmu->task_ctx_nr == ctxn)
  5246. return pmu->pmu_cpu_context;
  5247. }
  5248. return NULL;
  5249. }
  5250. static void update_pmu_context(struct pmu *pmu, struct pmu *old_pmu)
  5251. {
  5252. int cpu;
  5253. for_each_possible_cpu(cpu) {
  5254. struct perf_cpu_context *cpuctx;
  5255. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5256. if (cpuctx->unique_pmu == old_pmu)
  5257. cpuctx->unique_pmu = pmu;
  5258. }
  5259. }
  5260. static void free_pmu_context(struct pmu *pmu)
  5261. {
  5262. struct pmu *i;
  5263. mutex_lock(&pmus_lock);
  5264. /*
  5265. * Like a real lame refcount.
  5266. */
  5267. list_for_each_entry(i, &pmus, entry) {
  5268. if (i->pmu_cpu_context == pmu->pmu_cpu_context) {
  5269. update_pmu_context(i, pmu);
  5270. goto out;
  5271. }
  5272. }
  5273. free_percpu(pmu->pmu_cpu_context);
  5274. out:
  5275. mutex_unlock(&pmus_lock);
  5276. }
  5277. static struct idr pmu_idr;
  5278. static ssize_t
  5279. type_show(struct device *dev, struct device_attribute *attr, char *page)
  5280. {
  5281. struct pmu *pmu = dev_get_drvdata(dev);
  5282. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->type);
  5283. }
  5284. static DEVICE_ATTR_RO(type);
  5285. static ssize_t
  5286. perf_event_mux_interval_ms_show(struct device *dev,
  5287. struct device_attribute *attr,
  5288. char *page)
  5289. {
  5290. struct pmu *pmu = dev_get_drvdata(dev);
  5291. return snprintf(page, PAGE_SIZE-1, "%d\n", pmu->hrtimer_interval_ms);
  5292. }
  5293. static ssize_t
  5294. perf_event_mux_interval_ms_store(struct device *dev,
  5295. struct device_attribute *attr,
  5296. const char *buf, size_t count)
  5297. {
  5298. struct pmu *pmu = dev_get_drvdata(dev);
  5299. int timer, cpu, ret;
  5300. ret = kstrtoint(buf, 0, &timer);
  5301. if (ret)
  5302. return ret;
  5303. if (timer < 1)
  5304. return -EINVAL;
  5305. /* same value, noting to do */
  5306. if (timer == pmu->hrtimer_interval_ms)
  5307. return count;
  5308. pmu->hrtimer_interval_ms = timer;
  5309. /* update all cpuctx for this PMU */
  5310. for_each_possible_cpu(cpu) {
  5311. struct perf_cpu_context *cpuctx;
  5312. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5313. cpuctx->hrtimer_interval = ns_to_ktime(NSEC_PER_MSEC * timer);
  5314. if (hrtimer_active(&cpuctx->hrtimer))
  5315. hrtimer_forward_now(&cpuctx->hrtimer, cpuctx->hrtimer_interval);
  5316. }
  5317. return count;
  5318. }
  5319. static DEVICE_ATTR_RW(perf_event_mux_interval_ms);
  5320. static struct attribute *pmu_dev_attrs[] = {
  5321. &dev_attr_type.attr,
  5322. &dev_attr_perf_event_mux_interval_ms.attr,
  5323. NULL,
  5324. };
  5325. ATTRIBUTE_GROUPS(pmu_dev);
  5326. static int pmu_bus_running;
  5327. static struct bus_type pmu_bus = {
  5328. .name = "event_source",
  5329. .dev_groups = pmu_dev_groups,
  5330. };
  5331. static void pmu_dev_release(struct device *dev)
  5332. {
  5333. kfree(dev);
  5334. }
  5335. static int pmu_dev_alloc(struct pmu *pmu)
  5336. {
  5337. int ret = -ENOMEM;
  5338. pmu->dev = kzalloc(sizeof(struct device), GFP_KERNEL);
  5339. if (!pmu->dev)
  5340. goto out;
  5341. pmu->dev->groups = pmu->attr_groups;
  5342. device_initialize(pmu->dev);
  5343. ret = dev_set_name(pmu->dev, "%s", pmu->name);
  5344. if (ret)
  5345. goto free_dev;
  5346. dev_set_drvdata(pmu->dev, pmu);
  5347. pmu->dev->bus = &pmu_bus;
  5348. pmu->dev->release = pmu_dev_release;
  5349. ret = device_add(pmu->dev);
  5350. if (ret)
  5351. goto free_dev;
  5352. out:
  5353. return ret;
  5354. free_dev:
  5355. put_device(pmu->dev);
  5356. goto out;
  5357. }
  5358. static struct lock_class_key cpuctx_mutex;
  5359. static struct lock_class_key cpuctx_lock;
  5360. int perf_pmu_register(struct pmu *pmu, const char *name, int type)
  5361. {
  5362. int cpu, ret;
  5363. mutex_lock(&pmus_lock);
  5364. ret = -ENOMEM;
  5365. pmu->pmu_disable_count = alloc_percpu(int);
  5366. if (!pmu->pmu_disable_count)
  5367. goto unlock;
  5368. pmu->type = -1;
  5369. if (!name)
  5370. goto skip_type;
  5371. pmu->name = name;
  5372. if (type < 0) {
  5373. type = idr_alloc(&pmu_idr, pmu, PERF_TYPE_MAX, 0, GFP_KERNEL);
  5374. if (type < 0) {
  5375. ret = type;
  5376. goto free_pdc;
  5377. }
  5378. }
  5379. pmu->type = type;
  5380. if (pmu_bus_running) {
  5381. ret = pmu_dev_alloc(pmu);
  5382. if (ret)
  5383. goto free_idr;
  5384. }
  5385. skip_type:
  5386. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  5387. if (pmu->pmu_cpu_context)
  5388. goto got_cpu_context;
  5389. ret = -ENOMEM;
  5390. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  5391. if (!pmu->pmu_cpu_context)
  5392. goto free_dev;
  5393. for_each_possible_cpu(cpu) {
  5394. struct perf_cpu_context *cpuctx;
  5395. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  5396. __perf_event_init_context(&cpuctx->ctx);
  5397. lockdep_set_class(&cpuctx->ctx.mutex, &cpuctx_mutex);
  5398. lockdep_set_class(&cpuctx->ctx.lock, &cpuctx_lock);
  5399. cpuctx->ctx.type = cpu_context;
  5400. cpuctx->ctx.pmu = pmu;
  5401. __perf_cpu_hrtimer_init(cpuctx, cpu);
  5402. INIT_LIST_HEAD(&cpuctx->rotation_list);
  5403. cpuctx->unique_pmu = pmu;
  5404. }
  5405. got_cpu_context:
  5406. if (!pmu->start_txn) {
  5407. if (pmu->pmu_enable) {
  5408. /*
  5409. * If we have pmu_enable/pmu_disable calls, install
  5410. * transaction stubs that use that to try and batch
  5411. * hardware accesses.
  5412. */
  5413. pmu->start_txn = perf_pmu_start_txn;
  5414. pmu->commit_txn = perf_pmu_commit_txn;
  5415. pmu->cancel_txn = perf_pmu_cancel_txn;
  5416. } else {
  5417. pmu->start_txn = perf_pmu_nop_void;
  5418. pmu->commit_txn = perf_pmu_nop_int;
  5419. pmu->cancel_txn = perf_pmu_nop_void;
  5420. }
  5421. }
  5422. if (!pmu->pmu_enable) {
  5423. pmu->pmu_enable = perf_pmu_nop_void;
  5424. pmu->pmu_disable = perf_pmu_nop_void;
  5425. }
  5426. if (!pmu->event_idx)
  5427. pmu->event_idx = perf_event_idx_default;
  5428. list_add_rcu(&pmu->entry, &pmus);
  5429. ret = 0;
  5430. unlock:
  5431. mutex_unlock(&pmus_lock);
  5432. return ret;
  5433. free_dev:
  5434. device_del(pmu->dev);
  5435. put_device(pmu->dev);
  5436. free_idr:
  5437. if (pmu->type >= PERF_TYPE_MAX)
  5438. idr_remove(&pmu_idr, pmu->type);
  5439. free_pdc:
  5440. free_percpu(pmu->pmu_disable_count);
  5441. goto unlock;
  5442. }
  5443. EXPORT_SYMBOL_GPL(perf_pmu_register);
  5444. void perf_pmu_unregister(struct pmu *pmu)
  5445. {
  5446. mutex_lock(&pmus_lock);
  5447. list_del_rcu(&pmu->entry);
  5448. mutex_unlock(&pmus_lock);
  5449. /*
  5450. * We dereference the pmu list under both SRCU and regular RCU, so
  5451. * synchronize against both of those.
  5452. */
  5453. synchronize_srcu(&pmus_srcu);
  5454. synchronize_rcu();
  5455. free_percpu(pmu->pmu_disable_count);
  5456. if (pmu->type >= PERF_TYPE_MAX)
  5457. idr_remove(&pmu_idr, pmu->type);
  5458. device_del(pmu->dev);
  5459. put_device(pmu->dev);
  5460. free_pmu_context(pmu);
  5461. }
  5462. EXPORT_SYMBOL_GPL(perf_pmu_unregister);
  5463. struct pmu *perf_init_event(struct perf_event *event)
  5464. {
  5465. struct pmu *pmu = NULL;
  5466. int idx;
  5467. int ret;
  5468. idx = srcu_read_lock(&pmus_srcu);
  5469. rcu_read_lock();
  5470. pmu = idr_find(&pmu_idr, event->attr.type);
  5471. rcu_read_unlock();
  5472. if (pmu) {
  5473. if (!try_module_get(pmu->module)) {
  5474. pmu = ERR_PTR(-ENODEV);
  5475. goto unlock;
  5476. }
  5477. event->pmu = pmu;
  5478. ret = pmu->event_init(event);
  5479. if (ret)
  5480. pmu = ERR_PTR(ret);
  5481. goto unlock;
  5482. }
  5483. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5484. if (!try_module_get(pmu->module)) {
  5485. pmu = ERR_PTR(-ENODEV);
  5486. goto unlock;
  5487. }
  5488. event->pmu = pmu;
  5489. ret = pmu->event_init(event);
  5490. if (!ret)
  5491. goto unlock;
  5492. if (ret != -ENOENT) {
  5493. pmu = ERR_PTR(ret);
  5494. goto unlock;
  5495. }
  5496. }
  5497. pmu = ERR_PTR(-ENOENT);
  5498. unlock:
  5499. srcu_read_unlock(&pmus_srcu, idx);
  5500. return pmu;
  5501. }
  5502. static void account_event_cpu(struct perf_event *event, int cpu)
  5503. {
  5504. if (event->parent)
  5505. return;
  5506. if (has_branch_stack(event)) {
  5507. if (!(event->attach_state & PERF_ATTACH_TASK))
  5508. atomic_inc(&per_cpu(perf_branch_stack_events, cpu));
  5509. }
  5510. if (is_cgroup_event(event))
  5511. atomic_inc(&per_cpu(perf_cgroup_events, cpu));
  5512. }
  5513. static void account_event(struct perf_event *event)
  5514. {
  5515. if (event->parent)
  5516. return;
  5517. if (event->attach_state & PERF_ATTACH_TASK)
  5518. static_key_slow_inc(&perf_sched_events.key);
  5519. if (event->attr.mmap || event->attr.mmap_data)
  5520. atomic_inc(&nr_mmap_events);
  5521. if (event->attr.comm)
  5522. atomic_inc(&nr_comm_events);
  5523. if (event->attr.task)
  5524. atomic_inc(&nr_task_events);
  5525. if (event->attr.freq) {
  5526. if (atomic_inc_return(&nr_freq_events) == 1)
  5527. tick_nohz_full_kick_all();
  5528. }
  5529. if (has_branch_stack(event))
  5530. static_key_slow_inc(&perf_sched_events.key);
  5531. if (is_cgroup_event(event))
  5532. static_key_slow_inc(&perf_sched_events.key);
  5533. account_event_cpu(event, event->cpu);
  5534. }
  5535. /*
  5536. * Allocate and initialize a event structure
  5537. */
  5538. static struct perf_event *
  5539. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  5540. struct task_struct *task,
  5541. struct perf_event *group_leader,
  5542. struct perf_event *parent_event,
  5543. perf_overflow_handler_t overflow_handler,
  5544. void *context)
  5545. {
  5546. struct pmu *pmu;
  5547. struct perf_event *event;
  5548. struct hw_perf_event *hwc;
  5549. long err = -EINVAL;
  5550. if ((unsigned)cpu >= nr_cpu_ids) {
  5551. if (!task || cpu != -1)
  5552. return ERR_PTR(-EINVAL);
  5553. }
  5554. event = kzalloc(sizeof(*event), GFP_KERNEL);
  5555. if (!event)
  5556. return ERR_PTR(-ENOMEM);
  5557. /*
  5558. * Single events are their own group leaders, with an
  5559. * empty sibling list:
  5560. */
  5561. if (!group_leader)
  5562. group_leader = event;
  5563. mutex_init(&event->child_mutex);
  5564. INIT_LIST_HEAD(&event->child_list);
  5565. INIT_LIST_HEAD(&event->group_entry);
  5566. INIT_LIST_HEAD(&event->event_entry);
  5567. INIT_LIST_HEAD(&event->sibling_list);
  5568. INIT_LIST_HEAD(&event->rb_entry);
  5569. INIT_LIST_HEAD(&event->active_entry);
  5570. INIT_HLIST_NODE(&event->hlist_entry);
  5571. init_waitqueue_head(&event->waitq);
  5572. init_irq_work(&event->pending, perf_pending_event);
  5573. mutex_init(&event->mmap_mutex);
  5574. atomic_long_set(&event->refcount, 1);
  5575. event->cpu = cpu;
  5576. event->attr = *attr;
  5577. event->group_leader = group_leader;
  5578. event->pmu = NULL;
  5579. event->oncpu = -1;
  5580. event->parent = parent_event;
  5581. event->ns = get_pid_ns(task_active_pid_ns(current));
  5582. event->id = atomic64_inc_return(&perf_event_id);
  5583. event->state = PERF_EVENT_STATE_INACTIVE;
  5584. if (task) {
  5585. event->attach_state = PERF_ATTACH_TASK;
  5586. if (attr->type == PERF_TYPE_TRACEPOINT)
  5587. event->hw.tp_target = task;
  5588. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  5589. /*
  5590. * hw_breakpoint is a bit difficult here..
  5591. */
  5592. else if (attr->type == PERF_TYPE_BREAKPOINT)
  5593. event->hw.bp_target = task;
  5594. #endif
  5595. }
  5596. if (!overflow_handler && parent_event) {
  5597. overflow_handler = parent_event->overflow_handler;
  5598. context = parent_event->overflow_handler_context;
  5599. }
  5600. event->overflow_handler = overflow_handler;
  5601. event->overflow_handler_context = context;
  5602. perf_event__state_init(event);
  5603. pmu = NULL;
  5604. hwc = &event->hw;
  5605. hwc->sample_period = attr->sample_period;
  5606. if (attr->freq && attr->sample_freq)
  5607. hwc->sample_period = 1;
  5608. hwc->last_period = hwc->sample_period;
  5609. local64_set(&hwc->period_left, hwc->sample_period);
  5610. /*
  5611. * we currently do not support PERF_FORMAT_GROUP on inherited events
  5612. */
  5613. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  5614. goto err_ns;
  5615. pmu = perf_init_event(event);
  5616. if (!pmu)
  5617. goto err_ns;
  5618. else if (IS_ERR(pmu)) {
  5619. err = PTR_ERR(pmu);
  5620. goto err_ns;
  5621. }
  5622. if (!event->parent) {
  5623. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  5624. err = get_callchain_buffers();
  5625. if (err)
  5626. goto err_pmu;
  5627. }
  5628. }
  5629. return event;
  5630. err_pmu:
  5631. if (event->destroy)
  5632. event->destroy(event);
  5633. module_put(pmu->module);
  5634. err_ns:
  5635. if (event->ns)
  5636. put_pid_ns(event->ns);
  5637. kfree(event);
  5638. return ERR_PTR(err);
  5639. }
  5640. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  5641. struct perf_event_attr *attr)
  5642. {
  5643. u32 size;
  5644. int ret;
  5645. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  5646. return -EFAULT;
  5647. /*
  5648. * zero the full structure, so that a short copy will be nice.
  5649. */
  5650. memset(attr, 0, sizeof(*attr));
  5651. ret = get_user(size, &uattr->size);
  5652. if (ret)
  5653. return ret;
  5654. if (size > PAGE_SIZE) /* silly large */
  5655. goto err_size;
  5656. if (!size) /* abi compat */
  5657. size = PERF_ATTR_SIZE_VER0;
  5658. if (size < PERF_ATTR_SIZE_VER0)
  5659. goto err_size;
  5660. /*
  5661. * If we're handed a bigger struct than we know of,
  5662. * ensure all the unknown bits are 0 - i.e. new
  5663. * user-space does not rely on any kernel feature
  5664. * extensions we dont know about yet.
  5665. */
  5666. if (size > sizeof(*attr)) {
  5667. unsigned char __user *addr;
  5668. unsigned char __user *end;
  5669. unsigned char val;
  5670. addr = (void __user *)uattr + sizeof(*attr);
  5671. end = (void __user *)uattr + size;
  5672. for (; addr < end; addr++) {
  5673. ret = get_user(val, addr);
  5674. if (ret)
  5675. return ret;
  5676. if (val)
  5677. goto err_size;
  5678. }
  5679. size = sizeof(*attr);
  5680. }
  5681. ret = copy_from_user(attr, uattr, size);
  5682. if (ret)
  5683. return -EFAULT;
  5684. if (attr->__reserved_1)
  5685. return -EINVAL;
  5686. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  5687. return -EINVAL;
  5688. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  5689. return -EINVAL;
  5690. if (attr->sample_type & PERF_SAMPLE_BRANCH_STACK) {
  5691. u64 mask = attr->branch_sample_type;
  5692. /* only using defined bits */
  5693. if (mask & ~(PERF_SAMPLE_BRANCH_MAX-1))
  5694. return -EINVAL;
  5695. /* at least one branch bit must be set */
  5696. if (!(mask & ~PERF_SAMPLE_BRANCH_PLM_ALL))
  5697. return -EINVAL;
  5698. /* propagate priv level, when not set for branch */
  5699. if (!(mask & PERF_SAMPLE_BRANCH_PLM_ALL)) {
  5700. /* exclude_kernel checked on syscall entry */
  5701. if (!attr->exclude_kernel)
  5702. mask |= PERF_SAMPLE_BRANCH_KERNEL;
  5703. if (!attr->exclude_user)
  5704. mask |= PERF_SAMPLE_BRANCH_USER;
  5705. if (!attr->exclude_hv)
  5706. mask |= PERF_SAMPLE_BRANCH_HV;
  5707. /*
  5708. * adjust user setting (for HW filter setup)
  5709. */
  5710. attr->branch_sample_type = mask;
  5711. }
  5712. /* privileged levels capture (kernel, hv): check permissions */
  5713. if ((mask & PERF_SAMPLE_BRANCH_PERM_PLM)
  5714. && perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  5715. return -EACCES;
  5716. }
  5717. if (attr->sample_type & PERF_SAMPLE_REGS_USER) {
  5718. ret = perf_reg_validate(attr->sample_regs_user);
  5719. if (ret)
  5720. return ret;
  5721. }
  5722. if (attr->sample_type & PERF_SAMPLE_STACK_USER) {
  5723. if (!arch_perf_have_user_stack_dump())
  5724. return -ENOSYS;
  5725. /*
  5726. * We have __u32 type for the size, but so far
  5727. * we can only use __u16 as maximum due to the
  5728. * __u16 sample size limit.
  5729. */
  5730. if (attr->sample_stack_user >= USHRT_MAX)
  5731. ret = -EINVAL;
  5732. else if (!IS_ALIGNED(attr->sample_stack_user, sizeof(u64)))
  5733. ret = -EINVAL;
  5734. }
  5735. out:
  5736. return ret;
  5737. err_size:
  5738. put_user(sizeof(*attr), &uattr->size);
  5739. ret = -E2BIG;
  5740. goto out;
  5741. }
  5742. static int
  5743. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  5744. {
  5745. struct ring_buffer *rb = NULL;
  5746. int ret = -EINVAL;
  5747. if (!output_event)
  5748. goto set;
  5749. /* don't allow circular references */
  5750. if (event == output_event)
  5751. goto out;
  5752. /*
  5753. * Don't allow cross-cpu buffers
  5754. */
  5755. if (output_event->cpu != event->cpu)
  5756. goto out;
  5757. /*
  5758. * If its not a per-cpu rb, it must be the same task.
  5759. */
  5760. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  5761. goto out;
  5762. set:
  5763. mutex_lock(&event->mmap_mutex);
  5764. /* Can't redirect output if we've got an active mmap() */
  5765. if (atomic_read(&event->mmap_count))
  5766. goto unlock;
  5767. if (output_event) {
  5768. /* get the rb we want to redirect to */
  5769. rb = ring_buffer_get(output_event);
  5770. if (!rb)
  5771. goto unlock;
  5772. }
  5773. ring_buffer_attach(event, rb);
  5774. ret = 0;
  5775. unlock:
  5776. mutex_unlock(&event->mmap_mutex);
  5777. out:
  5778. return ret;
  5779. }
  5780. /**
  5781. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  5782. *
  5783. * @attr_uptr: event_id type attributes for monitoring/sampling
  5784. * @pid: target pid
  5785. * @cpu: target cpu
  5786. * @group_fd: group leader event fd
  5787. */
  5788. SYSCALL_DEFINE5(perf_event_open,
  5789. struct perf_event_attr __user *, attr_uptr,
  5790. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  5791. {
  5792. struct perf_event *group_leader = NULL, *output_event = NULL;
  5793. struct perf_event *event, *sibling;
  5794. struct perf_event_attr attr;
  5795. struct perf_event_context *ctx;
  5796. struct file *event_file = NULL;
  5797. struct fd group = {NULL, 0};
  5798. struct task_struct *task = NULL;
  5799. struct pmu *pmu;
  5800. int event_fd;
  5801. int move_group = 0;
  5802. int err;
  5803. int f_flags = O_RDWR;
  5804. /* for future expandability... */
  5805. if (flags & ~PERF_FLAG_ALL)
  5806. return -EINVAL;
  5807. err = perf_copy_attr(attr_uptr, &attr);
  5808. if (err)
  5809. return err;
  5810. if (!attr.exclude_kernel) {
  5811. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  5812. return -EACCES;
  5813. }
  5814. if (attr.freq) {
  5815. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  5816. return -EINVAL;
  5817. } else {
  5818. if (attr.sample_period & (1ULL << 63))
  5819. return -EINVAL;
  5820. }
  5821. /*
  5822. * In cgroup mode, the pid argument is used to pass the fd
  5823. * opened to the cgroup directory in cgroupfs. The cpu argument
  5824. * designates the cpu on which to monitor threads from that
  5825. * cgroup.
  5826. */
  5827. if ((flags & PERF_FLAG_PID_CGROUP) && (pid == -1 || cpu == -1))
  5828. return -EINVAL;
  5829. if (flags & PERF_FLAG_FD_CLOEXEC)
  5830. f_flags |= O_CLOEXEC;
  5831. event_fd = get_unused_fd_flags(f_flags);
  5832. if (event_fd < 0)
  5833. return event_fd;
  5834. if (group_fd != -1) {
  5835. err = perf_fget_light(group_fd, &group);
  5836. if (err)
  5837. goto err_fd;
  5838. group_leader = group.file->private_data;
  5839. if (flags & PERF_FLAG_FD_OUTPUT)
  5840. output_event = group_leader;
  5841. if (flags & PERF_FLAG_FD_NO_GROUP)
  5842. group_leader = NULL;
  5843. }
  5844. if (pid != -1 && !(flags & PERF_FLAG_PID_CGROUP)) {
  5845. task = find_lively_task_by_vpid(pid);
  5846. if (IS_ERR(task)) {
  5847. err = PTR_ERR(task);
  5848. goto err_group_fd;
  5849. }
  5850. }
  5851. if (task && group_leader &&
  5852. group_leader->attr.inherit != attr.inherit) {
  5853. err = -EINVAL;
  5854. goto err_task;
  5855. }
  5856. get_online_cpus();
  5857. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL,
  5858. NULL, NULL);
  5859. if (IS_ERR(event)) {
  5860. err = PTR_ERR(event);
  5861. goto err_cpus;
  5862. }
  5863. if (flags & PERF_FLAG_PID_CGROUP) {
  5864. err = perf_cgroup_connect(pid, event, &attr, group_leader);
  5865. if (err) {
  5866. __free_event(event);
  5867. goto err_cpus;
  5868. }
  5869. }
  5870. if (is_sampling_event(event)) {
  5871. if (event->pmu->capabilities & PERF_PMU_CAP_NO_INTERRUPT) {
  5872. err = -ENOTSUPP;
  5873. goto err_alloc;
  5874. }
  5875. }
  5876. account_event(event);
  5877. /*
  5878. * Special case software events and allow them to be part of
  5879. * any hardware group.
  5880. */
  5881. pmu = event->pmu;
  5882. if (group_leader &&
  5883. (is_software_event(event) != is_software_event(group_leader))) {
  5884. if (is_software_event(event)) {
  5885. /*
  5886. * If event and group_leader are not both a software
  5887. * event, and event is, then group leader is not.
  5888. *
  5889. * Allow the addition of software events to !software
  5890. * groups, this is safe because software events never
  5891. * fail to schedule.
  5892. */
  5893. pmu = group_leader->pmu;
  5894. } else if (is_software_event(group_leader) &&
  5895. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  5896. /*
  5897. * In case the group is a pure software group, and we
  5898. * try to add a hardware event, move the whole group to
  5899. * the hardware context.
  5900. */
  5901. move_group = 1;
  5902. }
  5903. }
  5904. /*
  5905. * Get the target context (task or percpu):
  5906. */
  5907. ctx = find_get_context(pmu, task, event->cpu);
  5908. if (IS_ERR(ctx)) {
  5909. err = PTR_ERR(ctx);
  5910. goto err_alloc;
  5911. }
  5912. if (task) {
  5913. put_task_struct(task);
  5914. task = NULL;
  5915. }
  5916. /*
  5917. * Look up the group leader (we will attach this event to it):
  5918. */
  5919. if (group_leader) {
  5920. err = -EINVAL;
  5921. /*
  5922. * Do not allow a recursive hierarchy (this new sibling
  5923. * becoming part of another group-sibling):
  5924. */
  5925. if (group_leader->group_leader != group_leader)
  5926. goto err_context;
  5927. /*
  5928. * Do not allow to attach to a group in a different
  5929. * task or CPU context:
  5930. */
  5931. if (move_group) {
  5932. if (group_leader->ctx->type != ctx->type)
  5933. goto err_context;
  5934. } else {
  5935. if (group_leader->ctx != ctx)
  5936. goto err_context;
  5937. }
  5938. /*
  5939. * Only a group leader can be exclusive or pinned
  5940. */
  5941. if (attr.exclusive || attr.pinned)
  5942. goto err_context;
  5943. }
  5944. if (output_event) {
  5945. err = perf_event_set_output(event, output_event);
  5946. if (err)
  5947. goto err_context;
  5948. }
  5949. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event,
  5950. f_flags);
  5951. if (IS_ERR(event_file)) {
  5952. err = PTR_ERR(event_file);
  5953. goto err_context;
  5954. }
  5955. if (move_group) {
  5956. struct perf_event_context *gctx = group_leader->ctx;
  5957. mutex_lock(&gctx->mutex);
  5958. perf_remove_from_context(group_leader, false);
  5959. /*
  5960. * Removing from the context ends up with disabled
  5961. * event. What we want here is event in the initial
  5962. * startup state, ready to be add into new context.
  5963. */
  5964. perf_event__state_init(group_leader);
  5965. list_for_each_entry(sibling, &group_leader->sibling_list,
  5966. group_entry) {
  5967. perf_remove_from_context(sibling, false);
  5968. perf_event__state_init(sibling);
  5969. put_ctx(gctx);
  5970. }
  5971. mutex_unlock(&gctx->mutex);
  5972. put_ctx(gctx);
  5973. }
  5974. WARN_ON_ONCE(ctx->parent_ctx);
  5975. mutex_lock(&ctx->mutex);
  5976. if (move_group) {
  5977. synchronize_rcu();
  5978. perf_install_in_context(ctx, group_leader, event->cpu);
  5979. get_ctx(ctx);
  5980. list_for_each_entry(sibling, &group_leader->sibling_list,
  5981. group_entry) {
  5982. perf_install_in_context(ctx, sibling, event->cpu);
  5983. get_ctx(ctx);
  5984. }
  5985. }
  5986. perf_install_in_context(ctx, event, event->cpu);
  5987. perf_unpin_context(ctx);
  5988. mutex_unlock(&ctx->mutex);
  5989. put_online_cpus();
  5990. event->owner = current;
  5991. mutex_lock(&current->perf_event_mutex);
  5992. list_add_tail(&event->owner_entry, &current->perf_event_list);
  5993. mutex_unlock(&current->perf_event_mutex);
  5994. /*
  5995. * Precalculate sample_data sizes
  5996. */
  5997. perf_event__header_size(event);
  5998. perf_event__id_header_size(event);
  5999. /*
  6000. * Drop the reference on the group_event after placing the
  6001. * new event on the sibling_list. This ensures destruction
  6002. * of the group leader will find the pointer to itself in
  6003. * perf_group_detach().
  6004. */
  6005. fdput(group);
  6006. fd_install(event_fd, event_file);
  6007. return event_fd;
  6008. err_context:
  6009. perf_unpin_context(ctx);
  6010. put_ctx(ctx);
  6011. err_alloc:
  6012. free_event(event);
  6013. err_cpus:
  6014. put_online_cpus();
  6015. err_task:
  6016. if (task)
  6017. put_task_struct(task);
  6018. err_group_fd:
  6019. fdput(group);
  6020. err_fd:
  6021. put_unused_fd(event_fd);
  6022. return err;
  6023. }
  6024. /**
  6025. * perf_event_create_kernel_counter
  6026. *
  6027. * @attr: attributes of the counter to create
  6028. * @cpu: cpu in which the counter is bound
  6029. * @task: task to profile (NULL for percpu)
  6030. */
  6031. struct perf_event *
  6032. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  6033. struct task_struct *task,
  6034. perf_overflow_handler_t overflow_handler,
  6035. void *context)
  6036. {
  6037. struct perf_event_context *ctx;
  6038. struct perf_event *event;
  6039. int err;
  6040. /*
  6041. * Get the target context (task or percpu):
  6042. */
  6043. event = perf_event_alloc(attr, cpu, task, NULL, NULL,
  6044. overflow_handler, context);
  6045. if (IS_ERR(event)) {
  6046. err = PTR_ERR(event);
  6047. goto err;
  6048. }
  6049. account_event(event);
  6050. ctx = find_get_context(event->pmu, task, cpu);
  6051. if (IS_ERR(ctx)) {
  6052. err = PTR_ERR(ctx);
  6053. goto err_free;
  6054. }
  6055. WARN_ON_ONCE(ctx->parent_ctx);
  6056. mutex_lock(&ctx->mutex);
  6057. perf_install_in_context(ctx, event, cpu);
  6058. perf_unpin_context(ctx);
  6059. mutex_unlock(&ctx->mutex);
  6060. return event;
  6061. err_free:
  6062. free_event(event);
  6063. err:
  6064. return ERR_PTR(err);
  6065. }
  6066. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  6067. void perf_pmu_migrate_context(struct pmu *pmu, int src_cpu, int dst_cpu)
  6068. {
  6069. struct perf_event_context *src_ctx;
  6070. struct perf_event_context *dst_ctx;
  6071. struct perf_event *event, *tmp;
  6072. LIST_HEAD(events);
  6073. src_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, src_cpu)->ctx;
  6074. dst_ctx = &per_cpu_ptr(pmu->pmu_cpu_context, dst_cpu)->ctx;
  6075. mutex_lock(&src_ctx->mutex);
  6076. list_for_each_entry_safe(event, tmp, &src_ctx->event_list,
  6077. event_entry) {
  6078. perf_remove_from_context(event, false);
  6079. unaccount_event_cpu(event, src_cpu);
  6080. put_ctx(src_ctx);
  6081. list_add(&event->migrate_entry, &events);
  6082. }
  6083. mutex_unlock(&src_ctx->mutex);
  6084. synchronize_rcu();
  6085. mutex_lock(&dst_ctx->mutex);
  6086. list_for_each_entry_safe(event, tmp, &events, migrate_entry) {
  6087. list_del(&event->migrate_entry);
  6088. if (event->state >= PERF_EVENT_STATE_OFF)
  6089. event->state = PERF_EVENT_STATE_INACTIVE;
  6090. account_event_cpu(event, dst_cpu);
  6091. perf_install_in_context(dst_ctx, event, dst_cpu);
  6092. get_ctx(dst_ctx);
  6093. }
  6094. mutex_unlock(&dst_ctx->mutex);
  6095. }
  6096. EXPORT_SYMBOL_GPL(perf_pmu_migrate_context);
  6097. static void sync_child_event(struct perf_event *child_event,
  6098. struct task_struct *child)
  6099. {
  6100. struct perf_event *parent_event = child_event->parent;
  6101. u64 child_val;
  6102. if (child_event->attr.inherit_stat)
  6103. perf_event_read_event(child_event, child);
  6104. child_val = perf_event_count(child_event);
  6105. /*
  6106. * Add back the child's count to the parent's count:
  6107. */
  6108. atomic64_add(child_val, &parent_event->child_count);
  6109. atomic64_add(child_event->total_time_enabled,
  6110. &parent_event->child_total_time_enabled);
  6111. atomic64_add(child_event->total_time_running,
  6112. &parent_event->child_total_time_running);
  6113. /*
  6114. * Remove this event from the parent's list
  6115. */
  6116. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  6117. mutex_lock(&parent_event->child_mutex);
  6118. list_del_init(&child_event->child_list);
  6119. mutex_unlock(&parent_event->child_mutex);
  6120. /*
  6121. * Release the parent event, if this was the last
  6122. * reference to it.
  6123. */
  6124. put_event(parent_event);
  6125. }
  6126. static void
  6127. __perf_event_exit_task(struct perf_event *child_event,
  6128. struct perf_event_context *child_ctx,
  6129. struct task_struct *child)
  6130. {
  6131. /*
  6132. * Do not destroy the 'original' grouping; because of the context
  6133. * switch optimization the original events could've ended up in a
  6134. * random child task.
  6135. *
  6136. * If we were to destroy the original group, all group related
  6137. * operations would cease to function properly after this random
  6138. * child dies.
  6139. *
  6140. * Do destroy all inherited groups, we don't care about those
  6141. * and being thorough is better.
  6142. */
  6143. perf_remove_from_context(child_event, !!child_event->parent);
  6144. /*
  6145. * It can happen that the parent exits first, and has events
  6146. * that are still around due to the child reference. These
  6147. * events need to be zapped.
  6148. */
  6149. if (child_event->parent) {
  6150. sync_child_event(child_event, child);
  6151. free_event(child_event);
  6152. }
  6153. }
  6154. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  6155. {
  6156. struct perf_event *child_event, *next;
  6157. struct perf_event_context *child_ctx, *parent_ctx;
  6158. unsigned long flags;
  6159. if (likely(!child->perf_event_ctxp[ctxn])) {
  6160. perf_event_task(child, NULL, 0);
  6161. return;
  6162. }
  6163. local_irq_save(flags);
  6164. /*
  6165. * We can't reschedule here because interrupts are disabled,
  6166. * and either child is current or it is a task that can't be
  6167. * scheduled, so we are now safe from rescheduling changing
  6168. * our context.
  6169. */
  6170. child_ctx = rcu_dereference_raw(child->perf_event_ctxp[ctxn]);
  6171. /*
  6172. * Take the context lock here so that if find_get_context is
  6173. * reading child->perf_event_ctxp, we wait until it has
  6174. * incremented the context's refcount before we do put_ctx below.
  6175. */
  6176. raw_spin_lock(&child_ctx->lock);
  6177. task_ctx_sched_out(child_ctx);
  6178. child->perf_event_ctxp[ctxn] = NULL;
  6179. /*
  6180. * In order to avoid freeing: child_ctx->parent_ctx->task
  6181. * under perf_event_context::lock, grab another reference.
  6182. */
  6183. parent_ctx = child_ctx->parent_ctx;
  6184. if (parent_ctx)
  6185. get_ctx(parent_ctx);
  6186. /*
  6187. * If this context is a clone; unclone it so it can't get
  6188. * swapped to another process while we're removing all
  6189. * the events from it.
  6190. */
  6191. unclone_ctx(child_ctx);
  6192. update_context_time(child_ctx);
  6193. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  6194. /*
  6195. * Now that we no longer hold perf_event_context::lock, drop
  6196. * our extra child_ctx->parent_ctx reference.
  6197. */
  6198. if (parent_ctx)
  6199. put_ctx(parent_ctx);
  6200. /*
  6201. * Report the task dead after unscheduling the events so that we
  6202. * won't get any samples after PERF_RECORD_EXIT. We can however still
  6203. * get a few PERF_RECORD_READ events.
  6204. */
  6205. perf_event_task(child, child_ctx, 0);
  6206. /*
  6207. * We can recurse on the same lock type through:
  6208. *
  6209. * __perf_event_exit_task()
  6210. * sync_child_event()
  6211. * put_event()
  6212. * mutex_lock(&ctx->mutex)
  6213. *
  6214. * But since its the parent context it won't be the same instance.
  6215. */
  6216. mutex_lock(&child_ctx->mutex);
  6217. list_for_each_entry_safe(child_event, next, &child_ctx->event_list, event_entry)
  6218. __perf_event_exit_task(child_event, child_ctx, child);
  6219. mutex_unlock(&child_ctx->mutex);
  6220. put_ctx(child_ctx);
  6221. }
  6222. /*
  6223. * When a child task exits, feed back event values to parent events.
  6224. */
  6225. void perf_event_exit_task(struct task_struct *child)
  6226. {
  6227. struct perf_event *event, *tmp;
  6228. int ctxn;
  6229. mutex_lock(&child->perf_event_mutex);
  6230. list_for_each_entry_safe(event, tmp, &child->perf_event_list,
  6231. owner_entry) {
  6232. list_del_init(&event->owner_entry);
  6233. /*
  6234. * Ensure the list deletion is visible before we clear
  6235. * the owner, closes a race against perf_release() where
  6236. * we need to serialize on the owner->perf_event_mutex.
  6237. */
  6238. smp_wmb();
  6239. event->owner = NULL;
  6240. }
  6241. mutex_unlock(&child->perf_event_mutex);
  6242. for_each_task_context_nr(ctxn)
  6243. perf_event_exit_task_context(child, ctxn);
  6244. }
  6245. static void perf_free_event(struct perf_event *event,
  6246. struct perf_event_context *ctx)
  6247. {
  6248. struct perf_event *parent = event->parent;
  6249. if (WARN_ON_ONCE(!parent))
  6250. return;
  6251. mutex_lock(&parent->child_mutex);
  6252. list_del_init(&event->child_list);
  6253. mutex_unlock(&parent->child_mutex);
  6254. put_event(parent);
  6255. perf_group_detach(event);
  6256. list_del_event(event, ctx);
  6257. free_event(event);
  6258. }
  6259. /*
  6260. * free an unexposed, unused context as created by inheritance by
  6261. * perf_event_init_task below, used by fork() in case of fail.
  6262. */
  6263. void perf_event_free_task(struct task_struct *task)
  6264. {
  6265. struct perf_event_context *ctx;
  6266. struct perf_event *event, *tmp;
  6267. int ctxn;
  6268. for_each_task_context_nr(ctxn) {
  6269. ctx = task->perf_event_ctxp[ctxn];
  6270. if (!ctx)
  6271. continue;
  6272. mutex_lock(&ctx->mutex);
  6273. again:
  6274. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  6275. group_entry)
  6276. perf_free_event(event, ctx);
  6277. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  6278. group_entry)
  6279. perf_free_event(event, ctx);
  6280. if (!list_empty(&ctx->pinned_groups) ||
  6281. !list_empty(&ctx->flexible_groups))
  6282. goto again;
  6283. mutex_unlock(&ctx->mutex);
  6284. put_ctx(ctx);
  6285. }
  6286. }
  6287. void perf_event_delayed_put(struct task_struct *task)
  6288. {
  6289. int ctxn;
  6290. for_each_task_context_nr(ctxn)
  6291. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  6292. }
  6293. /*
  6294. * inherit a event from parent task to child task:
  6295. */
  6296. static struct perf_event *
  6297. inherit_event(struct perf_event *parent_event,
  6298. struct task_struct *parent,
  6299. struct perf_event_context *parent_ctx,
  6300. struct task_struct *child,
  6301. struct perf_event *group_leader,
  6302. struct perf_event_context *child_ctx)
  6303. {
  6304. struct perf_event *child_event;
  6305. unsigned long flags;
  6306. /*
  6307. * Instead of creating recursive hierarchies of events,
  6308. * we link inherited events back to the original parent,
  6309. * which has a filp for sure, which we use as the reference
  6310. * count:
  6311. */
  6312. if (parent_event->parent)
  6313. parent_event = parent_event->parent;
  6314. child_event = perf_event_alloc(&parent_event->attr,
  6315. parent_event->cpu,
  6316. child,
  6317. group_leader, parent_event,
  6318. NULL, NULL);
  6319. if (IS_ERR(child_event))
  6320. return child_event;
  6321. if (!atomic_long_inc_not_zero(&parent_event->refcount)) {
  6322. free_event(child_event);
  6323. return NULL;
  6324. }
  6325. get_ctx(child_ctx);
  6326. /*
  6327. * Make the child state follow the state of the parent event,
  6328. * not its attr.disabled bit. We hold the parent's mutex,
  6329. * so we won't race with perf_event_{en, dis}able_family.
  6330. */
  6331. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  6332. child_event->state = PERF_EVENT_STATE_INACTIVE;
  6333. else
  6334. child_event->state = PERF_EVENT_STATE_OFF;
  6335. if (parent_event->attr.freq) {
  6336. u64 sample_period = parent_event->hw.sample_period;
  6337. struct hw_perf_event *hwc = &child_event->hw;
  6338. hwc->sample_period = sample_period;
  6339. hwc->last_period = sample_period;
  6340. local64_set(&hwc->period_left, sample_period);
  6341. }
  6342. child_event->ctx = child_ctx;
  6343. child_event->overflow_handler = parent_event->overflow_handler;
  6344. child_event->overflow_handler_context
  6345. = parent_event->overflow_handler_context;
  6346. /*
  6347. * Precalculate sample_data sizes
  6348. */
  6349. perf_event__header_size(child_event);
  6350. perf_event__id_header_size(child_event);
  6351. /*
  6352. * Link it up in the child's context:
  6353. */
  6354. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  6355. add_event_to_ctx(child_event, child_ctx);
  6356. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  6357. /*
  6358. * Link this into the parent event's child list
  6359. */
  6360. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  6361. mutex_lock(&parent_event->child_mutex);
  6362. list_add_tail(&child_event->child_list, &parent_event->child_list);
  6363. mutex_unlock(&parent_event->child_mutex);
  6364. return child_event;
  6365. }
  6366. static int inherit_group(struct perf_event *parent_event,
  6367. struct task_struct *parent,
  6368. struct perf_event_context *parent_ctx,
  6369. struct task_struct *child,
  6370. struct perf_event_context *child_ctx)
  6371. {
  6372. struct perf_event *leader;
  6373. struct perf_event *sub;
  6374. struct perf_event *child_ctr;
  6375. leader = inherit_event(parent_event, parent, parent_ctx,
  6376. child, NULL, child_ctx);
  6377. if (IS_ERR(leader))
  6378. return PTR_ERR(leader);
  6379. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  6380. child_ctr = inherit_event(sub, parent, parent_ctx,
  6381. child, leader, child_ctx);
  6382. if (IS_ERR(child_ctr))
  6383. return PTR_ERR(child_ctr);
  6384. }
  6385. return 0;
  6386. }
  6387. static int
  6388. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  6389. struct perf_event_context *parent_ctx,
  6390. struct task_struct *child, int ctxn,
  6391. int *inherited_all)
  6392. {
  6393. int ret;
  6394. struct perf_event_context *child_ctx;
  6395. if (!event->attr.inherit) {
  6396. *inherited_all = 0;
  6397. return 0;
  6398. }
  6399. child_ctx = child->perf_event_ctxp[ctxn];
  6400. if (!child_ctx) {
  6401. /*
  6402. * This is executed from the parent task context, so
  6403. * inherit events that have been marked for cloning.
  6404. * First allocate and initialize a context for the
  6405. * child.
  6406. */
  6407. child_ctx = alloc_perf_context(parent_ctx->pmu, child);
  6408. if (!child_ctx)
  6409. return -ENOMEM;
  6410. child->perf_event_ctxp[ctxn] = child_ctx;
  6411. }
  6412. ret = inherit_group(event, parent, parent_ctx,
  6413. child, child_ctx);
  6414. if (ret)
  6415. *inherited_all = 0;
  6416. return ret;
  6417. }
  6418. /*
  6419. * Initialize the perf_event context in task_struct
  6420. */
  6421. static int perf_event_init_context(struct task_struct *child, int ctxn)
  6422. {
  6423. struct perf_event_context *child_ctx, *parent_ctx;
  6424. struct perf_event_context *cloned_ctx;
  6425. struct perf_event *event;
  6426. struct task_struct *parent = current;
  6427. int inherited_all = 1;
  6428. unsigned long flags;
  6429. int ret = 0;
  6430. if (likely(!parent->perf_event_ctxp[ctxn]))
  6431. return 0;
  6432. /*
  6433. * If the parent's context is a clone, pin it so it won't get
  6434. * swapped under us.
  6435. */
  6436. parent_ctx = perf_pin_task_context(parent, ctxn);
  6437. if (!parent_ctx)
  6438. return 0;
  6439. /*
  6440. * No need to check if parent_ctx != NULL here; since we saw
  6441. * it non-NULL earlier, the only reason for it to become NULL
  6442. * is if we exit, and since we're currently in the middle of
  6443. * a fork we can't be exiting at the same time.
  6444. */
  6445. /*
  6446. * Lock the parent list. No need to lock the child - not PID
  6447. * hashed yet and not running, so nobody can access it.
  6448. */
  6449. mutex_lock(&parent_ctx->mutex);
  6450. /*
  6451. * We dont have to disable NMIs - we are only looking at
  6452. * the list, not manipulating it:
  6453. */
  6454. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  6455. ret = inherit_task_group(event, parent, parent_ctx,
  6456. child, ctxn, &inherited_all);
  6457. if (ret)
  6458. break;
  6459. }
  6460. /*
  6461. * We can't hold ctx->lock when iterating the ->flexible_group list due
  6462. * to allocations, but we need to prevent rotation because
  6463. * rotate_ctx() will change the list from interrupt context.
  6464. */
  6465. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  6466. parent_ctx->rotate_disable = 1;
  6467. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  6468. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  6469. ret = inherit_task_group(event, parent, parent_ctx,
  6470. child, ctxn, &inherited_all);
  6471. if (ret)
  6472. break;
  6473. }
  6474. raw_spin_lock_irqsave(&parent_ctx->lock, flags);
  6475. parent_ctx->rotate_disable = 0;
  6476. child_ctx = child->perf_event_ctxp[ctxn];
  6477. if (child_ctx && inherited_all) {
  6478. /*
  6479. * Mark the child context as a clone of the parent
  6480. * context, or of whatever the parent is a clone of.
  6481. *
  6482. * Note that if the parent is a clone, the holding of
  6483. * parent_ctx->lock avoids it from being uncloned.
  6484. */
  6485. cloned_ctx = parent_ctx->parent_ctx;
  6486. if (cloned_ctx) {
  6487. child_ctx->parent_ctx = cloned_ctx;
  6488. child_ctx->parent_gen = parent_ctx->parent_gen;
  6489. } else {
  6490. child_ctx->parent_ctx = parent_ctx;
  6491. child_ctx->parent_gen = parent_ctx->generation;
  6492. }
  6493. get_ctx(child_ctx->parent_ctx);
  6494. }
  6495. raw_spin_unlock_irqrestore(&parent_ctx->lock, flags);
  6496. mutex_unlock(&parent_ctx->mutex);
  6497. perf_unpin_context(parent_ctx);
  6498. put_ctx(parent_ctx);
  6499. return ret;
  6500. }
  6501. /*
  6502. * Initialize the perf_event context in task_struct
  6503. */
  6504. int perf_event_init_task(struct task_struct *child)
  6505. {
  6506. int ctxn, ret;
  6507. memset(child->perf_event_ctxp, 0, sizeof(child->perf_event_ctxp));
  6508. mutex_init(&child->perf_event_mutex);
  6509. INIT_LIST_HEAD(&child->perf_event_list);
  6510. for_each_task_context_nr(ctxn) {
  6511. ret = perf_event_init_context(child, ctxn);
  6512. if (ret)
  6513. return ret;
  6514. }
  6515. return 0;
  6516. }
  6517. static void __init perf_event_init_all_cpus(void)
  6518. {
  6519. struct swevent_htable *swhash;
  6520. int cpu;
  6521. for_each_possible_cpu(cpu) {
  6522. swhash = &per_cpu(swevent_htable, cpu);
  6523. mutex_init(&swhash->hlist_mutex);
  6524. INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
  6525. }
  6526. }
  6527. static void perf_event_init_cpu(int cpu)
  6528. {
  6529. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  6530. mutex_lock(&swhash->hlist_mutex);
  6531. swhash->online = true;
  6532. if (swhash->hlist_refcount > 0) {
  6533. struct swevent_hlist *hlist;
  6534. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  6535. WARN_ON(!hlist);
  6536. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  6537. }
  6538. mutex_unlock(&swhash->hlist_mutex);
  6539. }
  6540. #if defined CONFIG_HOTPLUG_CPU || defined CONFIG_KEXEC
  6541. static void perf_pmu_rotate_stop(struct pmu *pmu)
  6542. {
  6543. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  6544. WARN_ON(!irqs_disabled());
  6545. list_del_init(&cpuctx->rotation_list);
  6546. }
  6547. static void __perf_event_exit_context(void *__info)
  6548. {
  6549. struct remove_event re = { .detach_group = false };
  6550. struct perf_event_context *ctx = __info;
  6551. perf_pmu_rotate_stop(ctx->pmu);
  6552. rcu_read_lock();
  6553. list_for_each_entry_rcu(re.event, &ctx->event_list, event_entry)
  6554. __perf_remove_from_context(&re);
  6555. rcu_read_unlock();
  6556. }
  6557. static void perf_event_exit_cpu_context(int cpu)
  6558. {
  6559. struct perf_event_context *ctx;
  6560. struct pmu *pmu;
  6561. int idx;
  6562. idx = srcu_read_lock(&pmus_srcu);
  6563. list_for_each_entry_rcu(pmu, &pmus, entry) {
  6564. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  6565. mutex_lock(&ctx->mutex);
  6566. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  6567. mutex_unlock(&ctx->mutex);
  6568. }
  6569. srcu_read_unlock(&pmus_srcu, idx);
  6570. }
  6571. static void perf_event_exit_cpu(int cpu)
  6572. {
  6573. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  6574. perf_event_exit_cpu_context(cpu);
  6575. mutex_lock(&swhash->hlist_mutex);
  6576. swhash->online = false;
  6577. swevent_hlist_release(swhash);
  6578. mutex_unlock(&swhash->hlist_mutex);
  6579. }
  6580. #else
  6581. static inline void perf_event_exit_cpu(int cpu) { }
  6582. #endif
  6583. static int
  6584. perf_reboot(struct notifier_block *notifier, unsigned long val, void *v)
  6585. {
  6586. int cpu;
  6587. for_each_online_cpu(cpu)
  6588. perf_event_exit_cpu(cpu);
  6589. return NOTIFY_OK;
  6590. }
  6591. /*
  6592. * Run the perf reboot notifier at the very last possible moment so that
  6593. * the generic watchdog code runs as long as possible.
  6594. */
  6595. static struct notifier_block perf_reboot_notifier = {
  6596. .notifier_call = perf_reboot,
  6597. .priority = INT_MIN,
  6598. };
  6599. static int
  6600. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  6601. {
  6602. unsigned int cpu = (long)hcpu;
  6603. switch (action & ~CPU_TASKS_FROZEN) {
  6604. case CPU_UP_PREPARE:
  6605. case CPU_DOWN_FAILED:
  6606. perf_event_init_cpu(cpu);
  6607. break;
  6608. case CPU_UP_CANCELED:
  6609. case CPU_DOWN_PREPARE:
  6610. perf_event_exit_cpu(cpu);
  6611. break;
  6612. default:
  6613. break;
  6614. }
  6615. return NOTIFY_OK;
  6616. }
  6617. void __init perf_event_init(void)
  6618. {
  6619. int ret;
  6620. idr_init(&pmu_idr);
  6621. perf_event_init_all_cpus();
  6622. init_srcu_struct(&pmus_srcu);
  6623. perf_pmu_register(&perf_swevent, "software", PERF_TYPE_SOFTWARE);
  6624. perf_pmu_register(&perf_cpu_clock, NULL, -1);
  6625. perf_pmu_register(&perf_task_clock, NULL, -1);
  6626. perf_tp_register();
  6627. perf_cpu_notifier(perf_cpu_notify);
  6628. register_reboot_notifier(&perf_reboot_notifier);
  6629. ret = init_hw_breakpoint();
  6630. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  6631. /* do not patch jump label more than once per second */
  6632. jump_label_rate_limit(&perf_sched_events, HZ);
  6633. /*
  6634. * Build time assertion that we keep the data_head at the intended
  6635. * location. IOW, validation we got the __reserved[] size right.
  6636. */
  6637. BUILD_BUG_ON((offsetof(struct perf_event_mmap_page, data_head))
  6638. != 1024);
  6639. }
  6640. static int __init perf_event_sysfs_init(void)
  6641. {
  6642. struct pmu *pmu;
  6643. int ret;
  6644. mutex_lock(&pmus_lock);
  6645. ret = bus_register(&pmu_bus);
  6646. if (ret)
  6647. goto unlock;
  6648. list_for_each_entry(pmu, &pmus, entry) {
  6649. if (!pmu->name || pmu->type < 0)
  6650. continue;
  6651. ret = pmu_dev_alloc(pmu);
  6652. WARN(ret, "Failed to register pmu: %s, reason %d\n", pmu->name, ret);
  6653. }
  6654. pmu_bus_running = 1;
  6655. ret = 0;
  6656. unlock:
  6657. mutex_unlock(&pmus_lock);
  6658. return ret;
  6659. }
  6660. device_initcall(perf_event_sysfs_init);
  6661. #ifdef CONFIG_CGROUP_PERF
  6662. static struct cgroup_subsys_state *
  6663. perf_cgroup_css_alloc(struct cgroup_subsys_state *parent_css)
  6664. {
  6665. struct perf_cgroup *jc;
  6666. jc = kzalloc(sizeof(*jc), GFP_KERNEL);
  6667. if (!jc)
  6668. return ERR_PTR(-ENOMEM);
  6669. jc->info = alloc_percpu(struct perf_cgroup_info);
  6670. if (!jc->info) {
  6671. kfree(jc);
  6672. return ERR_PTR(-ENOMEM);
  6673. }
  6674. return &jc->css;
  6675. }
  6676. static void perf_cgroup_css_free(struct cgroup_subsys_state *css)
  6677. {
  6678. struct perf_cgroup *jc = container_of(css, struct perf_cgroup, css);
  6679. free_percpu(jc->info);
  6680. kfree(jc);
  6681. }
  6682. static int __perf_cgroup_move(void *info)
  6683. {
  6684. struct task_struct *task = info;
  6685. perf_cgroup_switch(task, PERF_CGROUP_SWOUT | PERF_CGROUP_SWIN);
  6686. return 0;
  6687. }
  6688. static void perf_cgroup_attach(struct cgroup_subsys_state *css,
  6689. struct cgroup_taskset *tset)
  6690. {
  6691. struct task_struct *task;
  6692. cgroup_taskset_for_each(task, tset)
  6693. task_function_call(task, __perf_cgroup_move, task);
  6694. }
  6695. static void perf_cgroup_exit(struct cgroup_subsys_state *css,
  6696. struct cgroup_subsys_state *old_css,
  6697. struct task_struct *task)
  6698. {
  6699. /*
  6700. * cgroup_exit() is called in the copy_process() failure path.
  6701. * Ignore this case since the task hasn't ran yet, this avoids
  6702. * trying to poke a half freed task state from generic code.
  6703. */
  6704. if (!(task->flags & PF_EXITING))
  6705. return;
  6706. task_function_call(task, __perf_cgroup_move, task);
  6707. }
  6708. struct cgroup_subsys perf_event_cgrp_subsys = {
  6709. .css_alloc = perf_cgroup_css_alloc,
  6710. .css_free = perf_cgroup_css_free,
  6711. .exit = perf_cgroup_exit,
  6712. .attach = perf_cgroup_attach,
  6713. };
  6714. #endif /* CONFIG_CGROUP_PERF */