perf_event.c 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311
  1. /*
  2. * Performance events core code:
  3. *
  4. * Copyright (C) 2008 Thomas Gleixner <tglx@linutronix.de>
  5. * Copyright (C) 2008-2009 Red Hat, Inc., Ingo Molnar
  6. * Copyright (C) 2008-2009 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/file.h>
  16. #include <linux/poll.h>
  17. #include <linux/slab.h>
  18. #include <linux/hash.h>
  19. #include <linux/sysfs.h>
  20. #include <linux/dcache.h>
  21. #include <linux/percpu.h>
  22. #include <linux/ptrace.h>
  23. #include <linux/vmstat.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/hardirq.h>
  26. #include <linux/rculist.h>
  27. #include <linux/uaccess.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/anon_inodes.h>
  30. #include <linux/kernel_stat.h>
  31. #include <linux/perf_event.h>
  32. #include <linux/ftrace_event.h>
  33. #include <linux/hw_breakpoint.h>
  34. #include <asm/irq_regs.h>
  35. atomic_t perf_task_events __read_mostly;
  36. static atomic_t nr_mmap_events __read_mostly;
  37. static atomic_t nr_comm_events __read_mostly;
  38. static atomic_t nr_task_events __read_mostly;
  39. static LIST_HEAD(pmus);
  40. static DEFINE_MUTEX(pmus_lock);
  41. static struct srcu_struct pmus_srcu;
  42. /*
  43. * perf event paranoia level:
  44. * -1 - not paranoid at all
  45. * 0 - disallow raw tracepoint access for unpriv
  46. * 1 - disallow cpu events for unpriv
  47. * 2 - disallow kernel profiling for unpriv
  48. */
  49. int sysctl_perf_event_paranoid __read_mostly = 1;
  50. int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
  51. /*
  52. * max perf event sample rate
  53. */
  54. int sysctl_perf_event_sample_rate __read_mostly = 100000;
  55. static atomic64_t perf_event_id;
  56. void __weak perf_event_print_debug(void) { }
  57. extern __weak const char *perf_pmu_name(void)
  58. {
  59. return "pmu";
  60. }
  61. void perf_pmu_disable(struct pmu *pmu)
  62. {
  63. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  64. if (!(*count)++)
  65. pmu->pmu_disable(pmu);
  66. }
  67. void perf_pmu_enable(struct pmu *pmu)
  68. {
  69. int *count = this_cpu_ptr(pmu->pmu_disable_count);
  70. if (!--(*count))
  71. pmu->pmu_enable(pmu);
  72. }
  73. static DEFINE_PER_CPU(struct list_head, rotation_list);
  74. /*
  75. * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
  76. * because they're strictly cpu affine and rotate_start is called with IRQs
  77. * disabled, while rotate_context is called from IRQ context.
  78. */
  79. static void perf_pmu_rotate_start(struct pmu *pmu)
  80. {
  81. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  82. struct list_head *head = &__get_cpu_var(rotation_list);
  83. WARN_ON(!irqs_disabled());
  84. if (list_empty(&cpuctx->rotation_list))
  85. list_add(&cpuctx->rotation_list, head);
  86. }
  87. static void get_ctx(struct perf_event_context *ctx)
  88. {
  89. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  90. }
  91. static void free_ctx(struct rcu_head *head)
  92. {
  93. struct perf_event_context *ctx;
  94. ctx = container_of(head, struct perf_event_context, rcu_head);
  95. kfree(ctx);
  96. }
  97. static void put_ctx(struct perf_event_context *ctx)
  98. {
  99. if (atomic_dec_and_test(&ctx->refcount)) {
  100. if (ctx->parent_ctx)
  101. put_ctx(ctx->parent_ctx);
  102. if (ctx->task)
  103. put_task_struct(ctx->task);
  104. call_rcu(&ctx->rcu_head, free_ctx);
  105. }
  106. }
  107. static void unclone_ctx(struct perf_event_context *ctx)
  108. {
  109. if (ctx->parent_ctx) {
  110. put_ctx(ctx->parent_ctx);
  111. ctx->parent_ctx = NULL;
  112. }
  113. }
  114. /*
  115. * If we inherit events we want to return the parent event id
  116. * to userspace.
  117. */
  118. static u64 primary_event_id(struct perf_event *event)
  119. {
  120. u64 id = event->id;
  121. if (event->parent)
  122. id = event->parent->id;
  123. return id;
  124. }
  125. /*
  126. * Get the perf_event_context for a task and lock it.
  127. * This has to cope with with the fact that until it is locked,
  128. * the context could get moved to another task.
  129. */
  130. static struct perf_event_context *
  131. perf_lock_task_context(struct task_struct *task, int ctxn, unsigned long *flags)
  132. {
  133. struct perf_event_context *ctx;
  134. rcu_read_lock();
  135. retry:
  136. ctx = rcu_dereference(task->perf_event_ctxp[ctxn]);
  137. if (ctx) {
  138. /*
  139. * If this context is a clone of another, it might
  140. * get swapped for another underneath us by
  141. * perf_event_task_sched_out, though the
  142. * rcu_read_lock() protects us from any context
  143. * getting freed. Lock the context and check if it
  144. * got swapped before we could get the lock, and retry
  145. * if so. If we locked the right context, then it
  146. * can't get swapped on us any more.
  147. */
  148. raw_spin_lock_irqsave(&ctx->lock, *flags);
  149. if (ctx != rcu_dereference(task->perf_event_ctxp[ctxn])) {
  150. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  151. goto retry;
  152. }
  153. if (!atomic_inc_not_zero(&ctx->refcount)) {
  154. raw_spin_unlock_irqrestore(&ctx->lock, *flags);
  155. ctx = NULL;
  156. }
  157. }
  158. rcu_read_unlock();
  159. return ctx;
  160. }
  161. /*
  162. * Get the context for a task and increment its pin_count so it
  163. * can't get swapped to another task. This also increments its
  164. * reference count so that the context can't get freed.
  165. */
  166. static struct perf_event_context *
  167. perf_pin_task_context(struct task_struct *task, int ctxn)
  168. {
  169. struct perf_event_context *ctx;
  170. unsigned long flags;
  171. ctx = perf_lock_task_context(task, ctxn, &flags);
  172. if (ctx) {
  173. ++ctx->pin_count;
  174. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  175. }
  176. return ctx;
  177. }
  178. static void perf_unpin_context(struct perf_event_context *ctx)
  179. {
  180. unsigned long flags;
  181. raw_spin_lock_irqsave(&ctx->lock, flags);
  182. --ctx->pin_count;
  183. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  184. put_ctx(ctx);
  185. }
  186. static inline u64 perf_clock(void)
  187. {
  188. return local_clock();
  189. }
  190. /*
  191. * Update the record of the current time in a context.
  192. */
  193. static void update_context_time(struct perf_event_context *ctx)
  194. {
  195. u64 now = perf_clock();
  196. ctx->time += now - ctx->timestamp;
  197. ctx->timestamp = now;
  198. }
  199. /*
  200. * Update the total_time_enabled and total_time_running fields for a event.
  201. */
  202. static void update_event_times(struct perf_event *event)
  203. {
  204. struct perf_event_context *ctx = event->ctx;
  205. u64 run_end;
  206. if (event->state < PERF_EVENT_STATE_INACTIVE ||
  207. event->group_leader->state < PERF_EVENT_STATE_INACTIVE)
  208. return;
  209. if (ctx->is_active)
  210. run_end = ctx->time;
  211. else
  212. run_end = event->tstamp_stopped;
  213. event->total_time_enabled = run_end - event->tstamp_enabled;
  214. if (event->state == PERF_EVENT_STATE_INACTIVE)
  215. run_end = event->tstamp_stopped;
  216. else
  217. run_end = ctx->time;
  218. event->total_time_running = run_end - event->tstamp_running;
  219. }
  220. /*
  221. * Update total_time_enabled and total_time_running for all events in a group.
  222. */
  223. static void update_group_times(struct perf_event *leader)
  224. {
  225. struct perf_event *event;
  226. update_event_times(leader);
  227. list_for_each_entry(event, &leader->sibling_list, group_entry)
  228. update_event_times(event);
  229. }
  230. static struct list_head *
  231. ctx_group_list(struct perf_event *event, struct perf_event_context *ctx)
  232. {
  233. if (event->attr.pinned)
  234. return &ctx->pinned_groups;
  235. else
  236. return &ctx->flexible_groups;
  237. }
  238. /*
  239. * Add a event from the lists for its context.
  240. * Must be called with ctx->mutex and ctx->lock held.
  241. */
  242. static void
  243. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  244. {
  245. WARN_ON_ONCE(event->attach_state & PERF_ATTACH_CONTEXT);
  246. event->attach_state |= PERF_ATTACH_CONTEXT;
  247. /*
  248. * If we're a stand alone event or group leader, we go to the context
  249. * list, group events are kept attached to the group so that
  250. * perf_group_detach can, at all times, locate all siblings.
  251. */
  252. if (event->group_leader == event) {
  253. struct list_head *list;
  254. if (is_software_event(event))
  255. event->group_flags |= PERF_GROUP_SOFTWARE;
  256. list = ctx_group_list(event, ctx);
  257. list_add_tail(&event->group_entry, list);
  258. }
  259. list_add_rcu(&event->event_entry, &ctx->event_list);
  260. if (!ctx->nr_events)
  261. perf_pmu_rotate_start(ctx->pmu);
  262. ctx->nr_events++;
  263. if (event->attr.inherit_stat)
  264. ctx->nr_stat++;
  265. }
  266. static void perf_group_attach(struct perf_event *event)
  267. {
  268. struct perf_event *group_leader = event->group_leader;
  269. /*
  270. * We can have double attach due to group movement in perf_event_open.
  271. */
  272. if (event->attach_state & PERF_ATTACH_GROUP)
  273. return;
  274. event->attach_state |= PERF_ATTACH_GROUP;
  275. if (group_leader == event)
  276. return;
  277. if (group_leader->group_flags & PERF_GROUP_SOFTWARE &&
  278. !is_software_event(event))
  279. group_leader->group_flags &= ~PERF_GROUP_SOFTWARE;
  280. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  281. group_leader->nr_siblings++;
  282. }
  283. /*
  284. * Remove a event from the lists for its context.
  285. * Must be called with ctx->mutex and ctx->lock held.
  286. */
  287. static void
  288. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  289. {
  290. /*
  291. * We can have double detach due to exit/hot-unplug + close.
  292. */
  293. if (!(event->attach_state & PERF_ATTACH_CONTEXT))
  294. return;
  295. event->attach_state &= ~PERF_ATTACH_CONTEXT;
  296. ctx->nr_events--;
  297. if (event->attr.inherit_stat)
  298. ctx->nr_stat--;
  299. list_del_rcu(&event->event_entry);
  300. if (event->group_leader == event)
  301. list_del_init(&event->group_entry);
  302. update_group_times(event);
  303. /*
  304. * If event was in error state, then keep it
  305. * that way, otherwise bogus counts will be
  306. * returned on read(). The only way to get out
  307. * of error state is by explicit re-enabling
  308. * of the event
  309. */
  310. if (event->state > PERF_EVENT_STATE_OFF)
  311. event->state = PERF_EVENT_STATE_OFF;
  312. }
  313. static void perf_group_detach(struct perf_event *event)
  314. {
  315. struct perf_event *sibling, *tmp;
  316. struct list_head *list = NULL;
  317. /*
  318. * We can have double detach due to exit/hot-unplug + close.
  319. */
  320. if (!(event->attach_state & PERF_ATTACH_GROUP))
  321. return;
  322. event->attach_state &= ~PERF_ATTACH_GROUP;
  323. /*
  324. * If this is a sibling, remove it from its group.
  325. */
  326. if (event->group_leader != event) {
  327. list_del_init(&event->group_entry);
  328. event->group_leader->nr_siblings--;
  329. return;
  330. }
  331. if (!list_empty(&event->group_entry))
  332. list = &event->group_entry;
  333. /*
  334. * If this was a group event with sibling events then
  335. * upgrade the siblings to singleton events by adding them
  336. * to whatever list we are on.
  337. */
  338. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  339. if (list)
  340. list_move_tail(&sibling->group_entry, list);
  341. sibling->group_leader = sibling;
  342. /* Inherit group flags from the previous leader */
  343. sibling->group_flags = event->group_flags;
  344. }
  345. }
  346. static inline int
  347. event_filter_match(struct perf_event *event)
  348. {
  349. return event->cpu == -1 || event->cpu == smp_processor_id();
  350. }
  351. static void
  352. event_sched_out(struct perf_event *event,
  353. struct perf_cpu_context *cpuctx,
  354. struct perf_event_context *ctx)
  355. {
  356. u64 delta;
  357. /*
  358. * An event which could not be activated because of
  359. * filter mismatch still needs to have its timings
  360. * maintained, otherwise bogus information is return
  361. * via read() for time_enabled, time_running:
  362. */
  363. if (event->state == PERF_EVENT_STATE_INACTIVE
  364. && !event_filter_match(event)) {
  365. delta = ctx->time - event->tstamp_stopped;
  366. event->tstamp_running += delta;
  367. event->tstamp_stopped = ctx->time;
  368. }
  369. if (event->state != PERF_EVENT_STATE_ACTIVE)
  370. return;
  371. event->state = PERF_EVENT_STATE_INACTIVE;
  372. if (event->pending_disable) {
  373. event->pending_disable = 0;
  374. event->state = PERF_EVENT_STATE_OFF;
  375. }
  376. event->tstamp_stopped = ctx->time;
  377. event->pmu->del(event, 0);
  378. event->oncpu = -1;
  379. if (!is_software_event(event))
  380. cpuctx->active_oncpu--;
  381. ctx->nr_active--;
  382. if (event->attr.exclusive || !cpuctx->active_oncpu)
  383. cpuctx->exclusive = 0;
  384. }
  385. static void
  386. group_sched_out(struct perf_event *group_event,
  387. struct perf_cpu_context *cpuctx,
  388. struct perf_event_context *ctx)
  389. {
  390. struct perf_event *event;
  391. int state = group_event->state;
  392. event_sched_out(group_event, cpuctx, ctx);
  393. /*
  394. * Schedule out siblings (if any):
  395. */
  396. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  397. event_sched_out(event, cpuctx, ctx);
  398. if (state == PERF_EVENT_STATE_ACTIVE && group_event->attr.exclusive)
  399. cpuctx->exclusive = 0;
  400. }
  401. static inline struct perf_cpu_context *
  402. __get_cpu_context(struct perf_event_context *ctx)
  403. {
  404. return this_cpu_ptr(ctx->pmu->pmu_cpu_context);
  405. }
  406. /*
  407. * Cross CPU call to remove a performance event
  408. *
  409. * We disable the event on the hardware level first. After that we
  410. * remove it from the context list.
  411. */
  412. static void __perf_event_remove_from_context(void *info)
  413. {
  414. struct perf_event *event = info;
  415. struct perf_event_context *ctx = event->ctx;
  416. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  417. /*
  418. * If this is a task context, we need to check whether it is
  419. * the current task context of this cpu. If not it has been
  420. * scheduled out before the smp call arrived.
  421. */
  422. if (ctx->task && cpuctx->task_ctx != ctx)
  423. return;
  424. raw_spin_lock(&ctx->lock);
  425. event_sched_out(event, cpuctx, ctx);
  426. list_del_event(event, ctx);
  427. raw_spin_unlock(&ctx->lock);
  428. }
  429. /*
  430. * Remove the event from a task's (or a CPU's) list of events.
  431. *
  432. * Must be called with ctx->mutex held.
  433. *
  434. * CPU events are removed with a smp call. For task events we only
  435. * call when the task is on a CPU.
  436. *
  437. * If event->ctx is a cloned context, callers must make sure that
  438. * every task struct that event->ctx->task could possibly point to
  439. * remains valid. This is OK when called from perf_release since
  440. * that only calls us on the top-level context, which can't be a clone.
  441. * When called from perf_event_exit_task, it's OK because the
  442. * context has been detached from its task.
  443. */
  444. static void perf_event_remove_from_context(struct perf_event *event)
  445. {
  446. struct perf_event_context *ctx = event->ctx;
  447. struct task_struct *task = ctx->task;
  448. if (!task) {
  449. /*
  450. * Per cpu events are removed via an smp call and
  451. * the removal is always successful.
  452. */
  453. smp_call_function_single(event->cpu,
  454. __perf_event_remove_from_context,
  455. event, 1);
  456. return;
  457. }
  458. retry:
  459. task_oncpu_function_call(task, __perf_event_remove_from_context,
  460. event);
  461. raw_spin_lock_irq(&ctx->lock);
  462. /*
  463. * If the context is active we need to retry the smp call.
  464. */
  465. if (ctx->nr_active && !list_empty(&event->group_entry)) {
  466. raw_spin_unlock_irq(&ctx->lock);
  467. goto retry;
  468. }
  469. /*
  470. * The lock prevents that this context is scheduled in so we
  471. * can remove the event safely, if the call above did not
  472. * succeed.
  473. */
  474. if (!list_empty(&event->group_entry))
  475. list_del_event(event, ctx);
  476. raw_spin_unlock_irq(&ctx->lock);
  477. }
  478. /*
  479. * Cross CPU call to disable a performance event
  480. */
  481. static void __perf_event_disable(void *info)
  482. {
  483. struct perf_event *event = info;
  484. struct perf_event_context *ctx = event->ctx;
  485. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  486. /*
  487. * If this is a per-task event, need to check whether this
  488. * event's task is the current task on this cpu.
  489. */
  490. if (ctx->task && cpuctx->task_ctx != ctx)
  491. return;
  492. raw_spin_lock(&ctx->lock);
  493. /*
  494. * If the event is on, turn it off.
  495. * If it is in error state, leave it in error state.
  496. */
  497. if (event->state >= PERF_EVENT_STATE_INACTIVE) {
  498. update_context_time(ctx);
  499. update_group_times(event);
  500. if (event == event->group_leader)
  501. group_sched_out(event, cpuctx, ctx);
  502. else
  503. event_sched_out(event, cpuctx, ctx);
  504. event->state = PERF_EVENT_STATE_OFF;
  505. }
  506. raw_spin_unlock(&ctx->lock);
  507. }
  508. /*
  509. * Disable a event.
  510. *
  511. * If event->ctx is a cloned context, callers must make sure that
  512. * every task struct that event->ctx->task could possibly point to
  513. * remains valid. This condition is satisifed when called through
  514. * perf_event_for_each_child or perf_event_for_each because they
  515. * hold the top-level event's child_mutex, so any descendant that
  516. * goes to exit will block in sync_child_event.
  517. * When called from perf_pending_event it's OK because event->ctx
  518. * is the current context on this CPU and preemption is disabled,
  519. * hence we can't get into perf_event_task_sched_out for this context.
  520. */
  521. void perf_event_disable(struct perf_event *event)
  522. {
  523. struct perf_event_context *ctx = event->ctx;
  524. struct task_struct *task = ctx->task;
  525. if (!task) {
  526. /*
  527. * Disable the event on the cpu that it's on
  528. */
  529. smp_call_function_single(event->cpu, __perf_event_disable,
  530. event, 1);
  531. return;
  532. }
  533. retry:
  534. task_oncpu_function_call(task, __perf_event_disable, event);
  535. raw_spin_lock_irq(&ctx->lock);
  536. /*
  537. * If the event is still active, we need to retry the cross-call.
  538. */
  539. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  540. raw_spin_unlock_irq(&ctx->lock);
  541. goto retry;
  542. }
  543. /*
  544. * Since we have the lock this context can't be scheduled
  545. * in, so we can change the state safely.
  546. */
  547. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  548. update_group_times(event);
  549. event->state = PERF_EVENT_STATE_OFF;
  550. }
  551. raw_spin_unlock_irq(&ctx->lock);
  552. }
  553. static int
  554. event_sched_in(struct perf_event *event,
  555. struct perf_cpu_context *cpuctx,
  556. struct perf_event_context *ctx)
  557. {
  558. if (event->state <= PERF_EVENT_STATE_OFF)
  559. return 0;
  560. event->state = PERF_EVENT_STATE_ACTIVE;
  561. event->oncpu = smp_processor_id();
  562. /*
  563. * The new state must be visible before we turn it on in the hardware:
  564. */
  565. smp_wmb();
  566. if (event->pmu->add(event, PERF_EF_START)) {
  567. event->state = PERF_EVENT_STATE_INACTIVE;
  568. event->oncpu = -1;
  569. return -EAGAIN;
  570. }
  571. event->tstamp_running += ctx->time - event->tstamp_stopped;
  572. if (!is_software_event(event))
  573. cpuctx->active_oncpu++;
  574. ctx->nr_active++;
  575. if (event->attr.exclusive)
  576. cpuctx->exclusive = 1;
  577. return 0;
  578. }
  579. static int
  580. group_sched_in(struct perf_event *group_event,
  581. struct perf_cpu_context *cpuctx,
  582. struct perf_event_context *ctx)
  583. {
  584. struct perf_event *event, *partial_group = NULL;
  585. struct pmu *pmu = group_event->pmu;
  586. u64 now = ctx->time;
  587. bool simulate = false;
  588. if (group_event->state == PERF_EVENT_STATE_OFF)
  589. return 0;
  590. pmu->start_txn(pmu);
  591. if (event_sched_in(group_event, cpuctx, ctx)) {
  592. pmu->cancel_txn(pmu);
  593. return -EAGAIN;
  594. }
  595. /*
  596. * Schedule in siblings as one group (if any):
  597. */
  598. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  599. if (event_sched_in(event, cpuctx, ctx)) {
  600. partial_group = event;
  601. goto group_error;
  602. }
  603. }
  604. if (!pmu->commit_txn(pmu))
  605. return 0;
  606. group_error:
  607. /*
  608. * Groups can be scheduled in as one unit only, so undo any
  609. * partial group before returning:
  610. * The events up to the failed event are scheduled out normally,
  611. * tstamp_stopped will be updated.
  612. *
  613. * The failed events and the remaining siblings need to have
  614. * their timings updated as if they had gone thru event_sched_in()
  615. * and event_sched_out(). This is required to get consistent timings
  616. * across the group. This also takes care of the case where the group
  617. * could never be scheduled by ensuring tstamp_stopped is set to mark
  618. * the time the event was actually stopped, such that time delta
  619. * calculation in update_event_times() is correct.
  620. */
  621. list_for_each_entry(event, &group_event->sibling_list, group_entry) {
  622. if (event == partial_group)
  623. simulate = true;
  624. if (simulate) {
  625. event->tstamp_running += now - event->tstamp_stopped;
  626. event->tstamp_stopped = now;
  627. } else {
  628. event_sched_out(event, cpuctx, ctx);
  629. }
  630. }
  631. event_sched_out(group_event, cpuctx, ctx);
  632. pmu->cancel_txn(pmu);
  633. return -EAGAIN;
  634. }
  635. /*
  636. * Work out whether we can put this event group on the CPU now.
  637. */
  638. static int group_can_go_on(struct perf_event *event,
  639. struct perf_cpu_context *cpuctx,
  640. int can_add_hw)
  641. {
  642. /*
  643. * Groups consisting entirely of software events can always go on.
  644. */
  645. if (event->group_flags & PERF_GROUP_SOFTWARE)
  646. return 1;
  647. /*
  648. * If an exclusive group is already on, no other hardware
  649. * events can go on.
  650. */
  651. if (cpuctx->exclusive)
  652. return 0;
  653. /*
  654. * If this group is exclusive and there are already
  655. * events on the CPU, it can't go on.
  656. */
  657. if (event->attr.exclusive && cpuctx->active_oncpu)
  658. return 0;
  659. /*
  660. * Otherwise, try to add it if all previous groups were able
  661. * to go on.
  662. */
  663. return can_add_hw;
  664. }
  665. static void add_event_to_ctx(struct perf_event *event,
  666. struct perf_event_context *ctx)
  667. {
  668. list_add_event(event, ctx);
  669. perf_group_attach(event);
  670. event->tstamp_enabled = ctx->time;
  671. event->tstamp_running = ctx->time;
  672. event->tstamp_stopped = ctx->time;
  673. }
  674. /*
  675. * Cross CPU call to install and enable a performance event
  676. *
  677. * Must be called with ctx->mutex held
  678. */
  679. static void __perf_install_in_context(void *info)
  680. {
  681. struct perf_event *event = info;
  682. struct perf_event_context *ctx = event->ctx;
  683. struct perf_event *leader = event->group_leader;
  684. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  685. int err;
  686. /*
  687. * If this is a task context, we need to check whether it is
  688. * the current task context of this cpu. If not it has been
  689. * scheduled out before the smp call arrived.
  690. * Or possibly this is the right context but it isn't
  691. * on this cpu because it had no events.
  692. */
  693. if (ctx->task && cpuctx->task_ctx != ctx) {
  694. if (cpuctx->task_ctx || ctx->task != current)
  695. return;
  696. cpuctx->task_ctx = ctx;
  697. }
  698. raw_spin_lock(&ctx->lock);
  699. ctx->is_active = 1;
  700. update_context_time(ctx);
  701. add_event_to_ctx(event, ctx);
  702. if (event->cpu != -1 && event->cpu != smp_processor_id())
  703. goto unlock;
  704. /*
  705. * Don't put the event on if it is disabled or if
  706. * it is in a group and the group isn't on.
  707. */
  708. if (event->state != PERF_EVENT_STATE_INACTIVE ||
  709. (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE))
  710. goto unlock;
  711. /*
  712. * An exclusive event can't go on if there are already active
  713. * hardware events, and no hardware event can go on if there
  714. * is already an exclusive event on.
  715. */
  716. if (!group_can_go_on(event, cpuctx, 1))
  717. err = -EEXIST;
  718. else
  719. err = event_sched_in(event, cpuctx, ctx);
  720. if (err) {
  721. /*
  722. * This event couldn't go on. If it is in a group
  723. * then we have to pull the whole group off.
  724. * If the event group is pinned then put it in error state.
  725. */
  726. if (leader != event)
  727. group_sched_out(leader, cpuctx, ctx);
  728. if (leader->attr.pinned) {
  729. update_group_times(leader);
  730. leader->state = PERF_EVENT_STATE_ERROR;
  731. }
  732. }
  733. unlock:
  734. raw_spin_unlock(&ctx->lock);
  735. }
  736. /*
  737. * Attach a performance event to a context
  738. *
  739. * First we add the event to the list with the hardware enable bit
  740. * in event->hw_config cleared.
  741. *
  742. * If the event is attached to a task which is on a CPU we use a smp
  743. * call to enable it in the task context. The task might have been
  744. * scheduled away, but we check this in the smp call again.
  745. *
  746. * Must be called with ctx->mutex held.
  747. */
  748. static void
  749. perf_install_in_context(struct perf_event_context *ctx,
  750. struct perf_event *event,
  751. int cpu)
  752. {
  753. struct task_struct *task = ctx->task;
  754. event->ctx = ctx;
  755. if (!task) {
  756. /*
  757. * Per cpu events are installed via an smp call and
  758. * the install is always successful.
  759. */
  760. smp_call_function_single(cpu, __perf_install_in_context,
  761. event, 1);
  762. return;
  763. }
  764. retry:
  765. task_oncpu_function_call(task, __perf_install_in_context,
  766. event);
  767. raw_spin_lock_irq(&ctx->lock);
  768. /*
  769. * we need to retry the smp call.
  770. */
  771. if (ctx->is_active && list_empty(&event->group_entry)) {
  772. raw_spin_unlock_irq(&ctx->lock);
  773. goto retry;
  774. }
  775. /*
  776. * The lock prevents that this context is scheduled in so we
  777. * can add the event safely, if it the call above did not
  778. * succeed.
  779. */
  780. if (list_empty(&event->group_entry))
  781. add_event_to_ctx(event, ctx);
  782. raw_spin_unlock_irq(&ctx->lock);
  783. }
  784. /*
  785. * Put a event into inactive state and update time fields.
  786. * Enabling the leader of a group effectively enables all
  787. * the group members that aren't explicitly disabled, so we
  788. * have to update their ->tstamp_enabled also.
  789. * Note: this works for group members as well as group leaders
  790. * since the non-leader members' sibling_lists will be empty.
  791. */
  792. static void __perf_event_mark_enabled(struct perf_event *event,
  793. struct perf_event_context *ctx)
  794. {
  795. struct perf_event *sub;
  796. event->state = PERF_EVENT_STATE_INACTIVE;
  797. event->tstamp_enabled = ctx->time - event->total_time_enabled;
  798. list_for_each_entry(sub, &event->sibling_list, group_entry) {
  799. if (sub->state >= PERF_EVENT_STATE_INACTIVE) {
  800. sub->tstamp_enabled =
  801. ctx->time - sub->total_time_enabled;
  802. }
  803. }
  804. }
  805. /*
  806. * Cross CPU call to enable a performance event
  807. */
  808. static void __perf_event_enable(void *info)
  809. {
  810. struct perf_event *event = info;
  811. struct perf_event_context *ctx = event->ctx;
  812. struct perf_event *leader = event->group_leader;
  813. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  814. int err;
  815. /*
  816. * If this is a per-task event, need to check whether this
  817. * event's task is the current task on this cpu.
  818. */
  819. if (ctx->task && cpuctx->task_ctx != ctx) {
  820. if (cpuctx->task_ctx || ctx->task != current)
  821. return;
  822. cpuctx->task_ctx = ctx;
  823. }
  824. raw_spin_lock(&ctx->lock);
  825. ctx->is_active = 1;
  826. update_context_time(ctx);
  827. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  828. goto unlock;
  829. __perf_event_mark_enabled(event, ctx);
  830. if (event->cpu != -1 && event->cpu != smp_processor_id())
  831. goto unlock;
  832. /*
  833. * If the event is in a group and isn't the group leader,
  834. * then don't put it on unless the group is on.
  835. */
  836. if (leader != event && leader->state != PERF_EVENT_STATE_ACTIVE)
  837. goto unlock;
  838. if (!group_can_go_on(event, cpuctx, 1)) {
  839. err = -EEXIST;
  840. } else {
  841. if (event == leader)
  842. err = group_sched_in(event, cpuctx, ctx);
  843. else
  844. err = event_sched_in(event, cpuctx, ctx);
  845. }
  846. if (err) {
  847. /*
  848. * If this event can't go on and it's part of a
  849. * group, then the whole group has to come off.
  850. */
  851. if (leader != event)
  852. group_sched_out(leader, cpuctx, ctx);
  853. if (leader->attr.pinned) {
  854. update_group_times(leader);
  855. leader->state = PERF_EVENT_STATE_ERROR;
  856. }
  857. }
  858. unlock:
  859. raw_spin_unlock(&ctx->lock);
  860. }
  861. /*
  862. * Enable a event.
  863. *
  864. * If event->ctx is a cloned context, callers must make sure that
  865. * every task struct that event->ctx->task could possibly point to
  866. * remains valid. This condition is satisfied when called through
  867. * perf_event_for_each_child or perf_event_for_each as described
  868. * for perf_event_disable.
  869. */
  870. void perf_event_enable(struct perf_event *event)
  871. {
  872. struct perf_event_context *ctx = event->ctx;
  873. struct task_struct *task = ctx->task;
  874. if (!task) {
  875. /*
  876. * Enable the event on the cpu that it's on
  877. */
  878. smp_call_function_single(event->cpu, __perf_event_enable,
  879. event, 1);
  880. return;
  881. }
  882. raw_spin_lock_irq(&ctx->lock);
  883. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  884. goto out;
  885. /*
  886. * If the event is in error state, clear that first.
  887. * That way, if we see the event in error state below, we
  888. * know that it has gone back into error state, as distinct
  889. * from the task having been scheduled away before the
  890. * cross-call arrived.
  891. */
  892. if (event->state == PERF_EVENT_STATE_ERROR)
  893. event->state = PERF_EVENT_STATE_OFF;
  894. retry:
  895. raw_spin_unlock_irq(&ctx->lock);
  896. task_oncpu_function_call(task, __perf_event_enable, event);
  897. raw_spin_lock_irq(&ctx->lock);
  898. /*
  899. * If the context is active and the event is still off,
  900. * we need to retry the cross-call.
  901. */
  902. if (ctx->is_active && event->state == PERF_EVENT_STATE_OFF)
  903. goto retry;
  904. /*
  905. * Since we have the lock this context can't be scheduled
  906. * in, so we can change the state safely.
  907. */
  908. if (event->state == PERF_EVENT_STATE_OFF)
  909. __perf_event_mark_enabled(event, ctx);
  910. out:
  911. raw_spin_unlock_irq(&ctx->lock);
  912. }
  913. static int perf_event_refresh(struct perf_event *event, int refresh)
  914. {
  915. /*
  916. * not supported on inherited events
  917. */
  918. if (event->attr.inherit)
  919. return -EINVAL;
  920. atomic_add(refresh, &event->event_limit);
  921. perf_event_enable(event);
  922. return 0;
  923. }
  924. enum event_type_t {
  925. EVENT_FLEXIBLE = 0x1,
  926. EVENT_PINNED = 0x2,
  927. EVENT_ALL = EVENT_FLEXIBLE | EVENT_PINNED,
  928. };
  929. static void ctx_sched_out(struct perf_event_context *ctx,
  930. struct perf_cpu_context *cpuctx,
  931. enum event_type_t event_type)
  932. {
  933. struct perf_event *event;
  934. raw_spin_lock(&ctx->lock);
  935. perf_pmu_disable(ctx->pmu);
  936. ctx->is_active = 0;
  937. if (likely(!ctx->nr_events))
  938. goto out;
  939. update_context_time(ctx);
  940. if (!ctx->nr_active)
  941. goto out;
  942. if (event_type & EVENT_PINNED) {
  943. list_for_each_entry(event, &ctx->pinned_groups, group_entry)
  944. group_sched_out(event, cpuctx, ctx);
  945. }
  946. if (event_type & EVENT_FLEXIBLE) {
  947. list_for_each_entry(event, &ctx->flexible_groups, group_entry)
  948. group_sched_out(event, cpuctx, ctx);
  949. }
  950. out:
  951. perf_pmu_enable(ctx->pmu);
  952. raw_spin_unlock(&ctx->lock);
  953. }
  954. /*
  955. * Test whether two contexts are equivalent, i.e. whether they
  956. * have both been cloned from the same version of the same context
  957. * and they both have the same number of enabled events.
  958. * If the number of enabled events is the same, then the set
  959. * of enabled events should be the same, because these are both
  960. * inherited contexts, therefore we can't access individual events
  961. * in them directly with an fd; we can only enable/disable all
  962. * events via prctl, or enable/disable all events in a family
  963. * via ioctl, which will have the same effect on both contexts.
  964. */
  965. static int context_equiv(struct perf_event_context *ctx1,
  966. struct perf_event_context *ctx2)
  967. {
  968. return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
  969. && ctx1->parent_gen == ctx2->parent_gen
  970. && !ctx1->pin_count && !ctx2->pin_count;
  971. }
  972. static void __perf_event_sync_stat(struct perf_event *event,
  973. struct perf_event *next_event)
  974. {
  975. u64 value;
  976. if (!event->attr.inherit_stat)
  977. return;
  978. /*
  979. * Update the event value, we cannot use perf_event_read()
  980. * because we're in the middle of a context switch and have IRQs
  981. * disabled, which upsets smp_call_function_single(), however
  982. * we know the event must be on the current CPU, therefore we
  983. * don't need to use it.
  984. */
  985. switch (event->state) {
  986. case PERF_EVENT_STATE_ACTIVE:
  987. event->pmu->read(event);
  988. /* fall-through */
  989. case PERF_EVENT_STATE_INACTIVE:
  990. update_event_times(event);
  991. break;
  992. default:
  993. break;
  994. }
  995. /*
  996. * In order to keep per-task stats reliable we need to flip the event
  997. * values when we flip the contexts.
  998. */
  999. value = local64_read(&next_event->count);
  1000. value = local64_xchg(&event->count, value);
  1001. local64_set(&next_event->count, value);
  1002. swap(event->total_time_enabled, next_event->total_time_enabled);
  1003. swap(event->total_time_running, next_event->total_time_running);
  1004. /*
  1005. * Since we swizzled the values, update the user visible data too.
  1006. */
  1007. perf_event_update_userpage(event);
  1008. perf_event_update_userpage(next_event);
  1009. }
  1010. #define list_next_entry(pos, member) \
  1011. list_entry(pos->member.next, typeof(*pos), member)
  1012. static void perf_event_sync_stat(struct perf_event_context *ctx,
  1013. struct perf_event_context *next_ctx)
  1014. {
  1015. struct perf_event *event, *next_event;
  1016. if (!ctx->nr_stat)
  1017. return;
  1018. update_context_time(ctx);
  1019. event = list_first_entry(&ctx->event_list,
  1020. struct perf_event, event_entry);
  1021. next_event = list_first_entry(&next_ctx->event_list,
  1022. struct perf_event, event_entry);
  1023. while (&event->event_entry != &ctx->event_list &&
  1024. &next_event->event_entry != &next_ctx->event_list) {
  1025. __perf_event_sync_stat(event, next_event);
  1026. event = list_next_entry(event, event_entry);
  1027. next_event = list_next_entry(next_event, event_entry);
  1028. }
  1029. }
  1030. void perf_event_context_sched_out(struct task_struct *task, int ctxn,
  1031. struct task_struct *next)
  1032. {
  1033. struct perf_event_context *ctx = task->perf_event_ctxp[ctxn];
  1034. struct perf_event_context *next_ctx;
  1035. struct perf_event_context *parent;
  1036. struct perf_cpu_context *cpuctx;
  1037. int do_switch = 1;
  1038. if (likely(!ctx))
  1039. return;
  1040. cpuctx = __get_cpu_context(ctx);
  1041. if (!cpuctx->task_ctx)
  1042. return;
  1043. rcu_read_lock();
  1044. parent = rcu_dereference(ctx->parent_ctx);
  1045. next_ctx = next->perf_event_ctxp[ctxn];
  1046. if (parent && next_ctx &&
  1047. rcu_dereference(next_ctx->parent_ctx) == parent) {
  1048. /*
  1049. * Looks like the two contexts are clones, so we might be
  1050. * able to optimize the context switch. We lock both
  1051. * contexts and check that they are clones under the
  1052. * lock (including re-checking that neither has been
  1053. * uncloned in the meantime). It doesn't matter which
  1054. * order we take the locks because no other cpu could
  1055. * be trying to lock both of these tasks.
  1056. */
  1057. raw_spin_lock(&ctx->lock);
  1058. raw_spin_lock_nested(&next_ctx->lock, SINGLE_DEPTH_NESTING);
  1059. if (context_equiv(ctx, next_ctx)) {
  1060. /*
  1061. * XXX do we need a memory barrier of sorts
  1062. * wrt to rcu_dereference() of perf_event_ctxp
  1063. */
  1064. task->perf_event_ctxp[ctxn] = next_ctx;
  1065. next->perf_event_ctxp[ctxn] = ctx;
  1066. ctx->task = next;
  1067. next_ctx->task = task;
  1068. do_switch = 0;
  1069. perf_event_sync_stat(ctx, next_ctx);
  1070. }
  1071. raw_spin_unlock(&next_ctx->lock);
  1072. raw_spin_unlock(&ctx->lock);
  1073. }
  1074. rcu_read_unlock();
  1075. if (do_switch) {
  1076. ctx_sched_out(ctx, cpuctx, EVENT_ALL);
  1077. cpuctx->task_ctx = NULL;
  1078. }
  1079. }
  1080. #define for_each_task_context_nr(ctxn) \
  1081. for ((ctxn) = 0; (ctxn) < perf_nr_task_contexts; (ctxn)++)
  1082. /*
  1083. * Called from scheduler to remove the events of the current task,
  1084. * with interrupts disabled.
  1085. *
  1086. * We stop each event and update the event value in event->count.
  1087. *
  1088. * This does not protect us against NMI, but disable()
  1089. * sets the disabled bit in the control field of event _before_
  1090. * accessing the event control register. If a NMI hits, then it will
  1091. * not restart the event.
  1092. */
  1093. void __perf_event_task_sched_out(struct task_struct *task,
  1094. struct task_struct *next)
  1095. {
  1096. int ctxn;
  1097. perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, NULL, 0);
  1098. for_each_task_context_nr(ctxn)
  1099. perf_event_context_sched_out(task, ctxn, next);
  1100. }
  1101. static void task_ctx_sched_out(struct perf_event_context *ctx,
  1102. enum event_type_t event_type)
  1103. {
  1104. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1105. if (!cpuctx->task_ctx)
  1106. return;
  1107. if (WARN_ON_ONCE(ctx != cpuctx->task_ctx))
  1108. return;
  1109. ctx_sched_out(ctx, cpuctx, event_type);
  1110. cpuctx->task_ctx = NULL;
  1111. }
  1112. /*
  1113. * Called with IRQs disabled
  1114. */
  1115. static void cpu_ctx_sched_out(struct perf_cpu_context *cpuctx,
  1116. enum event_type_t event_type)
  1117. {
  1118. ctx_sched_out(&cpuctx->ctx, cpuctx, event_type);
  1119. }
  1120. static void
  1121. ctx_pinned_sched_in(struct perf_event_context *ctx,
  1122. struct perf_cpu_context *cpuctx)
  1123. {
  1124. struct perf_event *event;
  1125. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1126. if (event->state <= PERF_EVENT_STATE_OFF)
  1127. continue;
  1128. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1129. continue;
  1130. if (group_can_go_on(event, cpuctx, 1))
  1131. group_sched_in(event, cpuctx, ctx);
  1132. /*
  1133. * If this pinned group hasn't been scheduled,
  1134. * put it in error state.
  1135. */
  1136. if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1137. update_group_times(event);
  1138. event->state = PERF_EVENT_STATE_ERROR;
  1139. }
  1140. }
  1141. }
  1142. static void
  1143. ctx_flexible_sched_in(struct perf_event_context *ctx,
  1144. struct perf_cpu_context *cpuctx)
  1145. {
  1146. struct perf_event *event;
  1147. int can_add_hw = 1;
  1148. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1149. /* Ignore events in OFF or ERROR state */
  1150. if (event->state <= PERF_EVENT_STATE_OFF)
  1151. continue;
  1152. /*
  1153. * Listen to the 'cpu' scheduling filter constraint
  1154. * of events:
  1155. */
  1156. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1157. continue;
  1158. if (group_can_go_on(event, cpuctx, can_add_hw)) {
  1159. if (group_sched_in(event, cpuctx, ctx))
  1160. can_add_hw = 0;
  1161. }
  1162. }
  1163. }
  1164. static void
  1165. ctx_sched_in(struct perf_event_context *ctx,
  1166. struct perf_cpu_context *cpuctx,
  1167. enum event_type_t event_type)
  1168. {
  1169. raw_spin_lock(&ctx->lock);
  1170. ctx->is_active = 1;
  1171. if (likely(!ctx->nr_events))
  1172. goto out;
  1173. ctx->timestamp = perf_clock();
  1174. /*
  1175. * First go through the list and put on any pinned groups
  1176. * in order to give them the best chance of going on.
  1177. */
  1178. if (event_type & EVENT_PINNED)
  1179. ctx_pinned_sched_in(ctx, cpuctx);
  1180. /* Then walk through the lower prio flexible groups */
  1181. if (event_type & EVENT_FLEXIBLE)
  1182. ctx_flexible_sched_in(ctx, cpuctx);
  1183. out:
  1184. raw_spin_unlock(&ctx->lock);
  1185. }
  1186. static void cpu_ctx_sched_in(struct perf_cpu_context *cpuctx,
  1187. enum event_type_t event_type)
  1188. {
  1189. struct perf_event_context *ctx = &cpuctx->ctx;
  1190. ctx_sched_in(ctx, cpuctx, event_type);
  1191. }
  1192. static void task_ctx_sched_in(struct perf_event_context *ctx,
  1193. enum event_type_t event_type)
  1194. {
  1195. struct perf_cpu_context *cpuctx;
  1196. cpuctx = __get_cpu_context(ctx);
  1197. if (cpuctx->task_ctx == ctx)
  1198. return;
  1199. ctx_sched_in(ctx, cpuctx, event_type);
  1200. cpuctx->task_ctx = ctx;
  1201. }
  1202. void perf_event_context_sched_in(struct perf_event_context *ctx)
  1203. {
  1204. struct perf_cpu_context *cpuctx;
  1205. cpuctx = __get_cpu_context(ctx);
  1206. if (cpuctx->task_ctx == ctx)
  1207. return;
  1208. perf_pmu_disable(ctx->pmu);
  1209. /*
  1210. * We want to keep the following priority order:
  1211. * cpu pinned (that don't need to move), task pinned,
  1212. * cpu flexible, task flexible.
  1213. */
  1214. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1215. ctx_sched_in(ctx, cpuctx, EVENT_PINNED);
  1216. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1217. ctx_sched_in(ctx, cpuctx, EVENT_FLEXIBLE);
  1218. cpuctx->task_ctx = ctx;
  1219. /*
  1220. * Since these rotations are per-cpu, we need to ensure the
  1221. * cpu-context we got scheduled on is actually rotating.
  1222. */
  1223. perf_pmu_rotate_start(ctx->pmu);
  1224. perf_pmu_enable(ctx->pmu);
  1225. }
  1226. /*
  1227. * Called from scheduler to add the events of the current task
  1228. * with interrupts disabled.
  1229. *
  1230. * We restore the event value and then enable it.
  1231. *
  1232. * This does not protect us against NMI, but enable()
  1233. * sets the enabled bit in the control field of event _before_
  1234. * accessing the event control register. If a NMI hits, then it will
  1235. * keep the event running.
  1236. */
  1237. void __perf_event_task_sched_in(struct task_struct *task)
  1238. {
  1239. struct perf_event_context *ctx;
  1240. int ctxn;
  1241. for_each_task_context_nr(ctxn) {
  1242. ctx = task->perf_event_ctxp[ctxn];
  1243. if (likely(!ctx))
  1244. continue;
  1245. perf_event_context_sched_in(ctx);
  1246. }
  1247. }
  1248. #define MAX_INTERRUPTS (~0ULL)
  1249. static void perf_log_throttle(struct perf_event *event, int enable);
  1250. static u64 perf_calculate_period(struct perf_event *event, u64 nsec, u64 count)
  1251. {
  1252. u64 frequency = event->attr.sample_freq;
  1253. u64 sec = NSEC_PER_SEC;
  1254. u64 divisor, dividend;
  1255. int count_fls, nsec_fls, frequency_fls, sec_fls;
  1256. count_fls = fls64(count);
  1257. nsec_fls = fls64(nsec);
  1258. frequency_fls = fls64(frequency);
  1259. sec_fls = 30;
  1260. /*
  1261. * We got @count in @nsec, with a target of sample_freq HZ
  1262. * the target period becomes:
  1263. *
  1264. * @count * 10^9
  1265. * period = -------------------
  1266. * @nsec * sample_freq
  1267. *
  1268. */
  1269. /*
  1270. * Reduce accuracy by one bit such that @a and @b converge
  1271. * to a similar magnitude.
  1272. */
  1273. #define REDUCE_FLS(a, b) \
  1274. do { \
  1275. if (a##_fls > b##_fls) { \
  1276. a >>= 1; \
  1277. a##_fls--; \
  1278. } else { \
  1279. b >>= 1; \
  1280. b##_fls--; \
  1281. } \
  1282. } while (0)
  1283. /*
  1284. * Reduce accuracy until either term fits in a u64, then proceed with
  1285. * the other, so that finally we can do a u64/u64 division.
  1286. */
  1287. while (count_fls + sec_fls > 64 && nsec_fls + frequency_fls > 64) {
  1288. REDUCE_FLS(nsec, frequency);
  1289. REDUCE_FLS(sec, count);
  1290. }
  1291. if (count_fls + sec_fls > 64) {
  1292. divisor = nsec * frequency;
  1293. while (count_fls + sec_fls > 64) {
  1294. REDUCE_FLS(count, sec);
  1295. divisor >>= 1;
  1296. }
  1297. dividend = count * sec;
  1298. } else {
  1299. dividend = count * sec;
  1300. while (nsec_fls + frequency_fls > 64) {
  1301. REDUCE_FLS(nsec, frequency);
  1302. dividend >>= 1;
  1303. }
  1304. divisor = nsec * frequency;
  1305. }
  1306. if (!divisor)
  1307. return dividend;
  1308. return div64_u64(dividend, divisor);
  1309. }
  1310. static void perf_adjust_period(struct perf_event *event, u64 nsec, u64 count)
  1311. {
  1312. struct hw_perf_event *hwc = &event->hw;
  1313. s64 period, sample_period;
  1314. s64 delta;
  1315. period = perf_calculate_period(event, nsec, count);
  1316. delta = (s64)(period - hwc->sample_period);
  1317. delta = (delta + 7) / 8; /* low pass filter */
  1318. sample_period = hwc->sample_period + delta;
  1319. if (!sample_period)
  1320. sample_period = 1;
  1321. hwc->sample_period = sample_period;
  1322. if (local64_read(&hwc->period_left) > 8*sample_period) {
  1323. event->pmu->stop(event, PERF_EF_UPDATE);
  1324. local64_set(&hwc->period_left, 0);
  1325. event->pmu->start(event, PERF_EF_RELOAD);
  1326. }
  1327. }
  1328. static void perf_ctx_adjust_freq(struct perf_event_context *ctx, u64 period)
  1329. {
  1330. struct perf_event *event;
  1331. struct hw_perf_event *hwc;
  1332. u64 interrupts, now;
  1333. s64 delta;
  1334. raw_spin_lock(&ctx->lock);
  1335. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  1336. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1337. continue;
  1338. if (event->cpu != -1 && event->cpu != smp_processor_id())
  1339. continue;
  1340. hwc = &event->hw;
  1341. interrupts = hwc->interrupts;
  1342. hwc->interrupts = 0;
  1343. /*
  1344. * unthrottle events on the tick
  1345. */
  1346. if (interrupts == MAX_INTERRUPTS) {
  1347. perf_log_throttle(event, 1);
  1348. event->pmu->start(event, 0);
  1349. }
  1350. if (!event->attr.freq || !event->attr.sample_freq)
  1351. continue;
  1352. event->pmu->read(event);
  1353. now = local64_read(&event->count);
  1354. delta = now - hwc->freq_count_stamp;
  1355. hwc->freq_count_stamp = now;
  1356. if (delta > 0)
  1357. perf_adjust_period(event, period, delta);
  1358. }
  1359. raw_spin_unlock(&ctx->lock);
  1360. }
  1361. /*
  1362. * Round-robin a context's events:
  1363. */
  1364. static void rotate_ctx(struct perf_event_context *ctx)
  1365. {
  1366. raw_spin_lock(&ctx->lock);
  1367. /* Rotate the first entry last of non-pinned groups */
  1368. list_rotate_left(&ctx->flexible_groups);
  1369. raw_spin_unlock(&ctx->lock);
  1370. }
  1371. /*
  1372. * perf_pmu_rotate_start() and perf_rotate_context() are fully serialized
  1373. * because they're strictly cpu affine and rotate_start is called with IRQs
  1374. * disabled, while rotate_context is called from IRQ context.
  1375. */
  1376. static void perf_rotate_context(struct perf_cpu_context *cpuctx)
  1377. {
  1378. u64 interval = (u64)cpuctx->jiffies_interval * TICK_NSEC;
  1379. struct perf_event_context *ctx = NULL;
  1380. int rotate = 0, remove = 1;
  1381. if (cpuctx->ctx.nr_events) {
  1382. remove = 0;
  1383. if (cpuctx->ctx.nr_events != cpuctx->ctx.nr_active)
  1384. rotate = 1;
  1385. }
  1386. ctx = cpuctx->task_ctx;
  1387. if (ctx && ctx->nr_events) {
  1388. remove = 0;
  1389. if (ctx->nr_events != ctx->nr_active)
  1390. rotate = 1;
  1391. }
  1392. perf_pmu_disable(cpuctx->ctx.pmu);
  1393. perf_ctx_adjust_freq(&cpuctx->ctx, interval);
  1394. if (ctx)
  1395. perf_ctx_adjust_freq(ctx, interval);
  1396. if (!rotate)
  1397. goto done;
  1398. cpu_ctx_sched_out(cpuctx, EVENT_FLEXIBLE);
  1399. if (ctx)
  1400. task_ctx_sched_out(ctx, EVENT_FLEXIBLE);
  1401. rotate_ctx(&cpuctx->ctx);
  1402. if (ctx)
  1403. rotate_ctx(ctx);
  1404. cpu_ctx_sched_in(cpuctx, EVENT_FLEXIBLE);
  1405. if (ctx)
  1406. task_ctx_sched_in(ctx, EVENT_FLEXIBLE);
  1407. done:
  1408. if (remove)
  1409. list_del_init(&cpuctx->rotation_list);
  1410. perf_pmu_enable(cpuctx->ctx.pmu);
  1411. }
  1412. void perf_event_task_tick(void)
  1413. {
  1414. struct list_head *head = &__get_cpu_var(rotation_list);
  1415. struct perf_cpu_context *cpuctx, *tmp;
  1416. WARN_ON(!irqs_disabled());
  1417. list_for_each_entry_safe(cpuctx, tmp, head, rotation_list) {
  1418. if (cpuctx->jiffies_interval == 1 ||
  1419. !(jiffies % cpuctx->jiffies_interval))
  1420. perf_rotate_context(cpuctx);
  1421. }
  1422. }
  1423. static int event_enable_on_exec(struct perf_event *event,
  1424. struct perf_event_context *ctx)
  1425. {
  1426. if (!event->attr.enable_on_exec)
  1427. return 0;
  1428. event->attr.enable_on_exec = 0;
  1429. if (event->state >= PERF_EVENT_STATE_INACTIVE)
  1430. return 0;
  1431. __perf_event_mark_enabled(event, ctx);
  1432. return 1;
  1433. }
  1434. /*
  1435. * Enable all of a task's events that have been marked enable-on-exec.
  1436. * This expects task == current.
  1437. */
  1438. static void perf_event_enable_on_exec(struct perf_event_context *ctx)
  1439. {
  1440. struct perf_event *event;
  1441. unsigned long flags;
  1442. int enabled = 0;
  1443. int ret;
  1444. local_irq_save(flags);
  1445. if (!ctx || !ctx->nr_events)
  1446. goto out;
  1447. task_ctx_sched_out(ctx, EVENT_ALL);
  1448. raw_spin_lock(&ctx->lock);
  1449. list_for_each_entry(event, &ctx->pinned_groups, group_entry) {
  1450. ret = event_enable_on_exec(event, ctx);
  1451. if (ret)
  1452. enabled = 1;
  1453. }
  1454. list_for_each_entry(event, &ctx->flexible_groups, group_entry) {
  1455. ret = event_enable_on_exec(event, ctx);
  1456. if (ret)
  1457. enabled = 1;
  1458. }
  1459. /*
  1460. * Unclone this context if we enabled any event.
  1461. */
  1462. if (enabled)
  1463. unclone_ctx(ctx);
  1464. raw_spin_unlock(&ctx->lock);
  1465. perf_event_context_sched_in(ctx);
  1466. out:
  1467. local_irq_restore(flags);
  1468. }
  1469. /*
  1470. * Cross CPU call to read the hardware event
  1471. */
  1472. static void __perf_event_read(void *info)
  1473. {
  1474. struct perf_event *event = info;
  1475. struct perf_event_context *ctx = event->ctx;
  1476. struct perf_cpu_context *cpuctx = __get_cpu_context(ctx);
  1477. /*
  1478. * If this is a task context, we need to check whether it is
  1479. * the current task context of this cpu. If not it has been
  1480. * scheduled out before the smp call arrived. In that case
  1481. * event->count would have been updated to a recent sample
  1482. * when the event was scheduled out.
  1483. */
  1484. if (ctx->task && cpuctx->task_ctx != ctx)
  1485. return;
  1486. raw_spin_lock(&ctx->lock);
  1487. update_context_time(ctx);
  1488. update_event_times(event);
  1489. raw_spin_unlock(&ctx->lock);
  1490. event->pmu->read(event);
  1491. }
  1492. static inline u64 perf_event_count(struct perf_event *event)
  1493. {
  1494. return local64_read(&event->count) + atomic64_read(&event->child_count);
  1495. }
  1496. static u64 perf_event_read(struct perf_event *event)
  1497. {
  1498. /*
  1499. * If event is enabled and currently active on a CPU, update the
  1500. * value in the event structure:
  1501. */
  1502. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1503. smp_call_function_single(event->oncpu,
  1504. __perf_event_read, event, 1);
  1505. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1506. struct perf_event_context *ctx = event->ctx;
  1507. unsigned long flags;
  1508. raw_spin_lock_irqsave(&ctx->lock, flags);
  1509. /*
  1510. * may read while context is not active
  1511. * (e.g., thread is blocked), in that case
  1512. * we cannot update context time
  1513. */
  1514. if (ctx->is_active)
  1515. update_context_time(ctx);
  1516. update_event_times(event);
  1517. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1518. }
  1519. return perf_event_count(event);
  1520. }
  1521. /*
  1522. * Callchain support
  1523. */
  1524. struct callchain_cpus_entries {
  1525. struct rcu_head rcu_head;
  1526. struct perf_callchain_entry *cpu_entries[0];
  1527. };
  1528. static DEFINE_PER_CPU(int, callchain_recursion[PERF_NR_CONTEXTS]);
  1529. static atomic_t nr_callchain_events;
  1530. static DEFINE_MUTEX(callchain_mutex);
  1531. struct callchain_cpus_entries *callchain_cpus_entries;
  1532. __weak void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1533. struct pt_regs *regs)
  1534. {
  1535. }
  1536. __weak void perf_callchain_user(struct perf_callchain_entry *entry,
  1537. struct pt_regs *regs)
  1538. {
  1539. }
  1540. static void release_callchain_buffers_rcu(struct rcu_head *head)
  1541. {
  1542. struct callchain_cpus_entries *entries;
  1543. int cpu;
  1544. entries = container_of(head, struct callchain_cpus_entries, rcu_head);
  1545. for_each_possible_cpu(cpu)
  1546. kfree(entries->cpu_entries[cpu]);
  1547. kfree(entries);
  1548. }
  1549. static void release_callchain_buffers(void)
  1550. {
  1551. struct callchain_cpus_entries *entries;
  1552. entries = callchain_cpus_entries;
  1553. rcu_assign_pointer(callchain_cpus_entries, NULL);
  1554. call_rcu(&entries->rcu_head, release_callchain_buffers_rcu);
  1555. }
  1556. static int alloc_callchain_buffers(void)
  1557. {
  1558. int cpu;
  1559. int size;
  1560. struct callchain_cpus_entries *entries;
  1561. /*
  1562. * We can't use the percpu allocation API for data that can be
  1563. * accessed from NMI. Use a temporary manual per cpu allocation
  1564. * until that gets sorted out.
  1565. */
  1566. size = sizeof(*entries) + sizeof(struct perf_callchain_entry *) *
  1567. num_possible_cpus();
  1568. entries = kzalloc(size, GFP_KERNEL);
  1569. if (!entries)
  1570. return -ENOMEM;
  1571. size = sizeof(struct perf_callchain_entry) * PERF_NR_CONTEXTS;
  1572. for_each_possible_cpu(cpu) {
  1573. entries->cpu_entries[cpu] = kmalloc_node(size, GFP_KERNEL,
  1574. cpu_to_node(cpu));
  1575. if (!entries->cpu_entries[cpu])
  1576. goto fail;
  1577. }
  1578. rcu_assign_pointer(callchain_cpus_entries, entries);
  1579. return 0;
  1580. fail:
  1581. for_each_possible_cpu(cpu)
  1582. kfree(entries->cpu_entries[cpu]);
  1583. kfree(entries);
  1584. return -ENOMEM;
  1585. }
  1586. static int get_callchain_buffers(void)
  1587. {
  1588. int err = 0;
  1589. int count;
  1590. mutex_lock(&callchain_mutex);
  1591. count = atomic_inc_return(&nr_callchain_events);
  1592. if (WARN_ON_ONCE(count < 1)) {
  1593. err = -EINVAL;
  1594. goto exit;
  1595. }
  1596. if (count > 1) {
  1597. /* If the allocation failed, give up */
  1598. if (!callchain_cpus_entries)
  1599. err = -ENOMEM;
  1600. goto exit;
  1601. }
  1602. err = alloc_callchain_buffers();
  1603. if (err)
  1604. release_callchain_buffers();
  1605. exit:
  1606. mutex_unlock(&callchain_mutex);
  1607. return err;
  1608. }
  1609. static void put_callchain_buffers(void)
  1610. {
  1611. if (atomic_dec_and_mutex_lock(&nr_callchain_events, &callchain_mutex)) {
  1612. release_callchain_buffers();
  1613. mutex_unlock(&callchain_mutex);
  1614. }
  1615. }
  1616. static int get_recursion_context(int *recursion)
  1617. {
  1618. int rctx;
  1619. if (in_nmi())
  1620. rctx = 3;
  1621. else if (in_irq())
  1622. rctx = 2;
  1623. else if (in_softirq())
  1624. rctx = 1;
  1625. else
  1626. rctx = 0;
  1627. if (recursion[rctx])
  1628. return -1;
  1629. recursion[rctx]++;
  1630. barrier();
  1631. return rctx;
  1632. }
  1633. static inline void put_recursion_context(int *recursion, int rctx)
  1634. {
  1635. barrier();
  1636. recursion[rctx]--;
  1637. }
  1638. static struct perf_callchain_entry *get_callchain_entry(int *rctx)
  1639. {
  1640. int cpu;
  1641. struct callchain_cpus_entries *entries;
  1642. *rctx = get_recursion_context(__get_cpu_var(callchain_recursion));
  1643. if (*rctx == -1)
  1644. return NULL;
  1645. entries = rcu_dereference(callchain_cpus_entries);
  1646. if (!entries)
  1647. return NULL;
  1648. cpu = smp_processor_id();
  1649. return &entries->cpu_entries[cpu][*rctx];
  1650. }
  1651. static void
  1652. put_callchain_entry(int rctx)
  1653. {
  1654. put_recursion_context(__get_cpu_var(callchain_recursion), rctx);
  1655. }
  1656. static struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  1657. {
  1658. int rctx;
  1659. struct perf_callchain_entry *entry;
  1660. entry = get_callchain_entry(&rctx);
  1661. if (rctx == -1)
  1662. return NULL;
  1663. if (!entry)
  1664. goto exit_put;
  1665. entry->nr = 0;
  1666. if (!user_mode(regs)) {
  1667. perf_callchain_store(entry, PERF_CONTEXT_KERNEL);
  1668. perf_callchain_kernel(entry, regs);
  1669. if (current->mm)
  1670. regs = task_pt_regs(current);
  1671. else
  1672. regs = NULL;
  1673. }
  1674. if (regs) {
  1675. perf_callchain_store(entry, PERF_CONTEXT_USER);
  1676. perf_callchain_user(entry, regs);
  1677. }
  1678. exit_put:
  1679. put_callchain_entry(rctx);
  1680. return entry;
  1681. }
  1682. /*
  1683. * Initialize the perf_event context in a task_struct:
  1684. */
  1685. static void __perf_event_init_context(struct perf_event_context *ctx)
  1686. {
  1687. raw_spin_lock_init(&ctx->lock);
  1688. mutex_init(&ctx->mutex);
  1689. INIT_LIST_HEAD(&ctx->pinned_groups);
  1690. INIT_LIST_HEAD(&ctx->flexible_groups);
  1691. INIT_LIST_HEAD(&ctx->event_list);
  1692. atomic_set(&ctx->refcount, 1);
  1693. }
  1694. static struct perf_event_context *
  1695. alloc_perf_context(struct pmu *pmu, struct task_struct *task)
  1696. {
  1697. struct perf_event_context *ctx;
  1698. ctx = kzalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  1699. if (!ctx)
  1700. return NULL;
  1701. __perf_event_init_context(ctx);
  1702. if (task) {
  1703. ctx->task = task;
  1704. get_task_struct(task);
  1705. }
  1706. ctx->pmu = pmu;
  1707. return ctx;
  1708. }
  1709. static struct task_struct *
  1710. find_lively_task_by_vpid(pid_t vpid)
  1711. {
  1712. struct task_struct *task;
  1713. int err;
  1714. rcu_read_lock();
  1715. if (!vpid)
  1716. task = current;
  1717. else
  1718. task = find_task_by_vpid(vpid);
  1719. if (task)
  1720. get_task_struct(task);
  1721. rcu_read_unlock();
  1722. if (!task)
  1723. return ERR_PTR(-ESRCH);
  1724. /*
  1725. * Can't attach events to a dying task.
  1726. */
  1727. err = -ESRCH;
  1728. if (task->flags & PF_EXITING)
  1729. goto errout;
  1730. /* Reuse ptrace permission checks for now. */
  1731. err = -EACCES;
  1732. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1733. goto errout;
  1734. return task;
  1735. errout:
  1736. put_task_struct(task);
  1737. return ERR_PTR(err);
  1738. }
  1739. static struct perf_event_context *
  1740. find_get_context(struct pmu *pmu, struct task_struct *task, int cpu)
  1741. {
  1742. struct perf_event_context *ctx;
  1743. struct perf_cpu_context *cpuctx;
  1744. unsigned long flags;
  1745. int ctxn, err;
  1746. if (!task && cpu != -1) {
  1747. /* Must be root to operate on a CPU event: */
  1748. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1749. return ERR_PTR(-EACCES);
  1750. if (cpu < 0 || cpu >= nr_cpumask_bits)
  1751. return ERR_PTR(-EINVAL);
  1752. /*
  1753. * We could be clever and allow to attach a event to an
  1754. * offline CPU and activate it when the CPU comes up, but
  1755. * that's for later.
  1756. */
  1757. if (!cpu_online(cpu))
  1758. return ERR_PTR(-ENODEV);
  1759. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  1760. ctx = &cpuctx->ctx;
  1761. get_ctx(ctx);
  1762. return ctx;
  1763. }
  1764. err = -EINVAL;
  1765. ctxn = pmu->task_ctx_nr;
  1766. if (ctxn < 0)
  1767. goto errout;
  1768. retry:
  1769. ctx = perf_lock_task_context(task, ctxn, &flags);
  1770. if (ctx) {
  1771. unclone_ctx(ctx);
  1772. raw_spin_unlock_irqrestore(&ctx->lock, flags);
  1773. }
  1774. if (!ctx) {
  1775. ctx = alloc_perf_context(pmu, task);
  1776. err = -ENOMEM;
  1777. if (!ctx)
  1778. goto errout;
  1779. get_ctx(ctx);
  1780. if (cmpxchg(&task->perf_event_ctxp[ctxn], NULL, ctx)) {
  1781. /*
  1782. * We raced with some other task; use
  1783. * the context they set.
  1784. */
  1785. put_task_struct(task);
  1786. kfree(ctx);
  1787. goto retry;
  1788. }
  1789. }
  1790. return ctx;
  1791. errout:
  1792. return ERR_PTR(err);
  1793. }
  1794. static void perf_event_free_filter(struct perf_event *event);
  1795. static void free_event_rcu(struct rcu_head *head)
  1796. {
  1797. struct perf_event *event;
  1798. event = container_of(head, struct perf_event, rcu_head);
  1799. if (event->ns)
  1800. put_pid_ns(event->ns);
  1801. perf_event_free_filter(event);
  1802. kfree(event);
  1803. }
  1804. static void perf_buffer_put(struct perf_buffer *buffer);
  1805. static void free_event(struct perf_event *event)
  1806. {
  1807. irq_work_sync(&event->pending);
  1808. if (!event->parent) {
  1809. if (event->attach_state & PERF_ATTACH_TASK)
  1810. jump_label_dec(&perf_task_events);
  1811. if (event->attr.mmap || event->attr.mmap_data)
  1812. atomic_dec(&nr_mmap_events);
  1813. if (event->attr.comm)
  1814. atomic_dec(&nr_comm_events);
  1815. if (event->attr.task)
  1816. atomic_dec(&nr_task_events);
  1817. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)
  1818. put_callchain_buffers();
  1819. }
  1820. if (event->buffer) {
  1821. perf_buffer_put(event->buffer);
  1822. event->buffer = NULL;
  1823. }
  1824. if (event->destroy)
  1825. event->destroy(event);
  1826. if (event->ctx)
  1827. put_ctx(event->ctx);
  1828. call_rcu(&event->rcu_head, free_event_rcu);
  1829. }
  1830. int perf_event_release_kernel(struct perf_event *event)
  1831. {
  1832. struct perf_event_context *ctx = event->ctx;
  1833. /*
  1834. * Remove from the PMU, can't get re-enabled since we got
  1835. * here because the last ref went.
  1836. */
  1837. perf_event_disable(event);
  1838. WARN_ON_ONCE(ctx->parent_ctx);
  1839. /*
  1840. * There are two ways this annotation is useful:
  1841. *
  1842. * 1) there is a lock recursion from perf_event_exit_task
  1843. * see the comment there.
  1844. *
  1845. * 2) there is a lock-inversion with mmap_sem through
  1846. * perf_event_read_group(), which takes faults while
  1847. * holding ctx->mutex, however this is called after
  1848. * the last filedesc died, so there is no possibility
  1849. * to trigger the AB-BA case.
  1850. */
  1851. mutex_lock_nested(&ctx->mutex, SINGLE_DEPTH_NESTING);
  1852. raw_spin_lock_irq(&ctx->lock);
  1853. perf_group_detach(event);
  1854. list_del_event(event, ctx);
  1855. raw_spin_unlock_irq(&ctx->lock);
  1856. mutex_unlock(&ctx->mutex);
  1857. mutex_lock(&event->owner->perf_event_mutex);
  1858. list_del_init(&event->owner_entry);
  1859. mutex_unlock(&event->owner->perf_event_mutex);
  1860. put_task_struct(event->owner);
  1861. free_event(event);
  1862. return 0;
  1863. }
  1864. EXPORT_SYMBOL_GPL(perf_event_release_kernel);
  1865. /*
  1866. * Called when the last reference to the file is gone.
  1867. */
  1868. static int perf_release(struct inode *inode, struct file *file)
  1869. {
  1870. struct perf_event *event = file->private_data;
  1871. file->private_data = NULL;
  1872. return perf_event_release_kernel(event);
  1873. }
  1874. static int perf_event_read_size(struct perf_event *event)
  1875. {
  1876. int entry = sizeof(u64); /* value */
  1877. int size = 0;
  1878. int nr = 1;
  1879. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1880. size += sizeof(u64);
  1881. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1882. size += sizeof(u64);
  1883. if (event->attr.read_format & PERF_FORMAT_ID)
  1884. entry += sizeof(u64);
  1885. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  1886. nr += event->group_leader->nr_siblings;
  1887. size += sizeof(u64);
  1888. }
  1889. size += entry * nr;
  1890. return size;
  1891. }
  1892. u64 perf_event_read_value(struct perf_event *event, u64 *enabled, u64 *running)
  1893. {
  1894. struct perf_event *child;
  1895. u64 total = 0;
  1896. *enabled = 0;
  1897. *running = 0;
  1898. mutex_lock(&event->child_mutex);
  1899. total += perf_event_read(event);
  1900. *enabled += event->total_time_enabled +
  1901. atomic64_read(&event->child_total_time_enabled);
  1902. *running += event->total_time_running +
  1903. atomic64_read(&event->child_total_time_running);
  1904. list_for_each_entry(child, &event->child_list, child_list) {
  1905. total += perf_event_read(child);
  1906. *enabled += child->total_time_enabled;
  1907. *running += child->total_time_running;
  1908. }
  1909. mutex_unlock(&event->child_mutex);
  1910. return total;
  1911. }
  1912. EXPORT_SYMBOL_GPL(perf_event_read_value);
  1913. static int perf_event_read_group(struct perf_event *event,
  1914. u64 read_format, char __user *buf)
  1915. {
  1916. struct perf_event *leader = event->group_leader, *sub;
  1917. int n = 0, size = 0, ret = -EFAULT;
  1918. struct perf_event_context *ctx = leader->ctx;
  1919. u64 values[5];
  1920. u64 count, enabled, running;
  1921. mutex_lock(&ctx->mutex);
  1922. count = perf_event_read_value(leader, &enabled, &running);
  1923. values[n++] = 1 + leader->nr_siblings;
  1924. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1925. values[n++] = enabled;
  1926. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1927. values[n++] = running;
  1928. values[n++] = count;
  1929. if (read_format & PERF_FORMAT_ID)
  1930. values[n++] = primary_event_id(leader);
  1931. size = n * sizeof(u64);
  1932. if (copy_to_user(buf, values, size))
  1933. goto unlock;
  1934. ret = size;
  1935. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  1936. n = 0;
  1937. values[n++] = perf_event_read_value(sub, &enabled, &running);
  1938. if (read_format & PERF_FORMAT_ID)
  1939. values[n++] = primary_event_id(sub);
  1940. size = n * sizeof(u64);
  1941. if (copy_to_user(buf + ret, values, size)) {
  1942. ret = -EFAULT;
  1943. goto unlock;
  1944. }
  1945. ret += size;
  1946. }
  1947. unlock:
  1948. mutex_unlock(&ctx->mutex);
  1949. return ret;
  1950. }
  1951. static int perf_event_read_one(struct perf_event *event,
  1952. u64 read_format, char __user *buf)
  1953. {
  1954. u64 enabled, running;
  1955. u64 values[4];
  1956. int n = 0;
  1957. values[n++] = perf_event_read_value(event, &enabled, &running);
  1958. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1959. values[n++] = enabled;
  1960. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1961. values[n++] = running;
  1962. if (read_format & PERF_FORMAT_ID)
  1963. values[n++] = primary_event_id(event);
  1964. if (copy_to_user(buf, values, n * sizeof(u64)))
  1965. return -EFAULT;
  1966. return n * sizeof(u64);
  1967. }
  1968. /*
  1969. * Read the performance event - simple non blocking version for now
  1970. */
  1971. static ssize_t
  1972. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  1973. {
  1974. u64 read_format = event->attr.read_format;
  1975. int ret;
  1976. /*
  1977. * Return end-of-file for a read on a event that is in
  1978. * error state (i.e. because it was pinned but it couldn't be
  1979. * scheduled on to the CPU at some point).
  1980. */
  1981. if (event->state == PERF_EVENT_STATE_ERROR)
  1982. return 0;
  1983. if (count < perf_event_read_size(event))
  1984. return -ENOSPC;
  1985. WARN_ON_ONCE(event->ctx->parent_ctx);
  1986. if (read_format & PERF_FORMAT_GROUP)
  1987. ret = perf_event_read_group(event, read_format, buf);
  1988. else
  1989. ret = perf_event_read_one(event, read_format, buf);
  1990. return ret;
  1991. }
  1992. static ssize_t
  1993. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1994. {
  1995. struct perf_event *event = file->private_data;
  1996. return perf_read_hw(event, buf, count);
  1997. }
  1998. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1999. {
  2000. struct perf_event *event = file->private_data;
  2001. struct perf_buffer *buffer;
  2002. unsigned int events = POLL_HUP;
  2003. rcu_read_lock();
  2004. buffer = rcu_dereference(event->buffer);
  2005. if (buffer)
  2006. events = atomic_xchg(&buffer->poll, 0);
  2007. rcu_read_unlock();
  2008. poll_wait(file, &event->waitq, wait);
  2009. return events;
  2010. }
  2011. static void perf_event_reset(struct perf_event *event)
  2012. {
  2013. (void)perf_event_read(event);
  2014. local64_set(&event->count, 0);
  2015. perf_event_update_userpage(event);
  2016. }
  2017. /*
  2018. * Holding the top-level event's child_mutex means that any
  2019. * descendant process that has inherited this event will block
  2020. * in sync_child_event if it goes to exit, thus satisfying the
  2021. * task existence requirements of perf_event_enable/disable.
  2022. */
  2023. static void perf_event_for_each_child(struct perf_event *event,
  2024. void (*func)(struct perf_event *))
  2025. {
  2026. struct perf_event *child;
  2027. WARN_ON_ONCE(event->ctx->parent_ctx);
  2028. mutex_lock(&event->child_mutex);
  2029. func(event);
  2030. list_for_each_entry(child, &event->child_list, child_list)
  2031. func(child);
  2032. mutex_unlock(&event->child_mutex);
  2033. }
  2034. static void perf_event_for_each(struct perf_event *event,
  2035. void (*func)(struct perf_event *))
  2036. {
  2037. struct perf_event_context *ctx = event->ctx;
  2038. struct perf_event *sibling;
  2039. WARN_ON_ONCE(ctx->parent_ctx);
  2040. mutex_lock(&ctx->mutex);
  2041. event = event->group_leader;
  2042. perf_event_for_each_child(event, func);
  2043. func(event);
  2044. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  2045. perf_event_for_each_child(event, func);
  2046. mutex_unlock(&ctx->mutex);
  2047. }
  2048. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  2049. {
  2050. struct perf_event_context *ctx = event->ctx;
  2051. int ret = 0;
  2052. u64 value;
  2053. if (!event->attr.sample_period)
  2054. return -EINVAL;
  2055. if (copy_from_user(&value, arg, sizeof(value)))
  2056. return -EFAULT;
  2057. if (!value)
  2058. return -EINVAL;
  2059. raw_spin_lock_irq(&ctx->lock);
  2060. if (event->attr.freq) {
  2061. if (value > sysctl_perf_event_sample_rate) {
  2062. ret = -EINVAL;
  2063. goto unlock;
  2064. }
  2065. event->attr.sample_freq = value;
  2066. } else {
  2067. event->attr.sample_period = value;
  2068. event->hw.sample_period = value;
  2069. }
  2070. unlock:
  2071. raw_spin_unlock_irq(&ctx->lock);
  2072. return ret;
  2073. }
  2074. static const struct file_operations perf_fops;
  2075. static struct perf_event *perf_fget_light(int fd, int *fput_needed)
  2076. {
  2077. struct file *file;
  2078. file = fget_light(fd, fput_needed);
  2079. if (!file)
  2080. return ERR_PTR(-EBADF);
  2081. if (file->f_op != &perf_fops) {
  2082. fput_light(file, *fput_needed);
  2083. *fput_needed = 0;
  2084. return ERR_PTR(-EBADF);
  2085. }
  2086. return file->private_data;
  2087. }
  2088. static int perf_event_set_output(struct perf_event *event,
  2089. struct perf_event *output_event);
  2090. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  2091. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  2092. {
  2093. struct perf_event *event = file->private_data;
  2094. void (*func)(struct perf_event *);
  2095. u32 flags = arg;
  2096. switch (cmd) {
  2097. case PERF_EVENT_IOC_ENABLE:
  2098. func = perf_event_enable;
  2099. break;
  2100. case PERF_EVENT_IOC_DISABLE:
  2101. func = perf_event_disable;
  2102. break;
  2103. case PERF_EVENT_IOC_RESET:
  2104. func = perf_event_reset;
  2105. break;
  2106. case PERF_EVENT_IOC_REFRESH:
  2107. return perf_event_refresh(event, arg);
  2108. case PERF_EVENT_IOC_PERIOD:
  2109. return perf_event_period(event, (u64 __user *)arg);
  2110. case PERF_EVENT_IOC_SET_OUTPUT:
  2111. {
  2112. struct perf_event *output_event = NULL;
  2113. int fput_needed = 0;
  2114. int ret;
  2115. if (arg != -1) {
  2116. output_event = perf_fget_light(arg, &fput_needed);
  2117. if (IS_ERR(output_event))
  2118. return PTR_ERR(output_event);
  2119. }
  2120. ret = perf_event_set_output(event, output_event);
  2121. if (output_event)
  2122. fput_light(output_event->filp, fput_needed);
  2123. return ret;
  2124. }
  2125. case PERF_EVENT_IOC_SET_FILTER:
  2126. return perf_event_set_filter(event, (void __user *)arg);
  2127. default:
  2128. return -ENOTTY;
  2129. }
  2130. if (flags & PERF_IOC_FLAG_GROUP)
  2131. perf_event_for_each(event, func);
  2132. else
  2133. perf_event_for_each_child(event, func);
  2134. return 0;
  2135. }
  2136. int perf_event_task_enable(void)
  2137. {
  2138. struct perf_event *event;
  2139. mutex_lock(&current->perf_event_mutex);
  2140. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2141. perf_event_for_each_child(event, perf_event_enable);
  2142. mutex_unlock(&current->perf_event_mutex);
  2143. return 0;
  2144. }
  2145. int perf_event_task_disable(void)
  2146. {
  2147. struct perf_event *event;
  2148. mutex_lock(&current->perf_event_mutex);
  2149. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  2150. perf_event_for_each_child(event, perf_event_disable);
  2151. mutex_unlock(&current->perf_event_mutex);
  2152. return 0;
  2153. }
  2154. #ifndef PERF_EVENT_INDEX_OFFSET
  2155. # define PERF_EVENT_INDEX_OFFSET 0
  2156. #endif
  2157. static int perf_event_index(struct perf_event *event)
  2158. {
  2159. if (event->hw.state & PERF_HES_STOPPED)
  2160. return 0;
  2161. if (event->state != PERF_EVENT_STATE_ACTIVE)
  2162. return 0;
  2163. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  2164. }
  2165. /*
  2166. * Callers need to ensure there can be no nesting of this function, otherwise
  2167. * the seqlock logic goes bad. We can not serialize this because the arch
  2168. * code calls this from NMI context.
  2169. */
  2170. void perf_event_update_userpage(struct perf_event *event)
  2171. {
  2172. struct perf_event_mmap_page *userpg;
  2173. struct perf_buffer *buffer;
  2174. rcu_read_lock();
  2175. buffer = rcu_dereference(event->buffer);
  2176. if (!buffer)
  2177. goto unlock;
  2178. userpg = buffer->user_page;
  2179. /*
  2180. * Disable preemption so as to not let the corresponding user-space
  2181. * spin too long if we get preempted.
  2182. */
  2183. preempt_disable();
  2184. ++userpg->lock;
  2185. barrier();
  2186. userpg->index = perf_event_index(event);
  2187. userpg->offset = perf_event_count(event);
  2188. if (event->state == PERF_EVENT_STATE_ACTIVE)
  2189. userpg->offset -= local64_read(&event->hw.prev_count);
  2190. userpg->time_enabled = event->total_time_enabled +
  2191. atomic64_read(&event->child_total_time_enabled);
  2192. userpg->time_running = event->total_time_running +
  2193. atomic64_read(&event->child_total_time_running);
  2194. barrier();
  2195. ++userpg->lock;
  2196. preempt_enable();
  2197. unlock:
  2198. rcu_read_unlock();
  2199. }
  2200. static unsigned long perf_data_size(struct perf_buffer *buffer);
  2201. static void
  2202. perf_buffer_init(struct perf_buffer *buffer, long watermark, int flags)
  2203. {
  2204. long max_size = perf_data_size(buffer);
  2205. if (watermark)
  2206. buffer->watermark = min(max_size, watermark);
  2207. if (!buffer->watermark)
  2208. buffer->watermark = max_size / 2;
  2209. if (flags & PERF_BUFFER_WRITABLE)
  2210. buffer->writable = 1;
  2211. atomic_set(&buffer->refcount, 1);
  2212. }
  2213. #ifndef CONFIG_PERF_USE_VMALLOC
  2214. /*
  2215. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  2216. */
  2217. static struct page *
  2218. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2219. {
  2220. if (pgoff > buffer->nr_pages)
  2221. return NULL;
  2222. if (pgoff == 0)
  2223. return virt_to_page(buffer->user_page);
  2224. return virt_to_page(buffer->data_pages[pgoff - 1]);
  2225. }
  2226. static void *perf_mmap_alloc_page(int cpu)
  2227. {
  2228. struct page *page;
  2229. int node;
  2230. node = (cpu == -1) ? cpu : cpu_to_node(cpu);
  2231. page = alloc_pages_node(node, GFP_KERNEL | __GFP_ZERO, 0);
  2232. if (!page)
  2233. return NULL;
  2234. return page_address(page);
  2235. }
  2236. static struct perf_buffer *
  2237. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2238. {
  2239. struct perf_buffer *buffer;
  2240. unsigned long size;
  2241. int i;
  2242. size = sizeof(struct perf_buffer);
  2243. size += nr_pages * sizeof(void *);
  2244. buffer = kzalloc(size, GFP_KERNEL);
  2245. if (!buffer)
  2246. goto fail;
  2247. buffer->user_page = perf_mmap_alloc_page(cpu);
  2248. if (!buffer->user_page)
  2249. goto fail_user_page;
  2250. for (i = 0; i < nr_pages; i++) {
  2251. buffer->data_pages[i] = perf_mmap_alloc_page(cpu);
  2252. if (!buffer->data_pages[i])
  2253. goto fail_data_pages;
  2254. }
  2255. buffer->nr_pages = nr_pages;
  2256. perf_buffer_init(buffer, watermark, flags);
  2257. return buffer;
  2258. fail_data_pages:
  2259. for (i--; i >= 0; i--)
  2260. free_page((unsigned long)buffer->data_pages[i]);
  2261. free_page((unsigned long)buffer->user_page);
  2262. fail_user_page:
  2263. kfree(buffer);
  2264. fail:
  2265. return NULL;
  2266. }
  2267. static void perf_mmap_free_page(unsigned long addr)
  2268. {
  2269. struct page *page = virt_to_page((void *)addr);
  2270. page->mapping = NULL;
  2271. __free_page(page);
  2272. }
  2273. static void perf_buffer_free(struct perf_buffer *buffer)
  2274. {
  2275. int i;
  2276. perf_mmap_free_page((unsigned long)buffer->user_page);
  2277. for (i = 0; i < buffer->nr_pages; i++)
  2278. perf_mmap_free_page((unsigned long)buffer->data_pages[i]);
  2279. kfree(buffer);
  2280. }
  2281. static inline int page_order(struct perf_buffer *buffer)
  2282. {
  2283. return 0;
  2284. }
  2285. #else
  2286. /*
  2287. * Back perf_mmap() with vmalloc memory.
  2288. *
  2289. * Required for architectures that have d-cache aliasing issues.
  2290. */
  2291. static inline int page_order(struct perf_buffer *buffer)
  2292. {
  2293. return buffer->page_order;
  2294. }
  2295. static struct page *
  2296. perf_mmap_to_page(struct perf_buffer *buffer, unsigned long pgoff)
  2297. {
  2298. if (pgoff > (1UL << page_order(buffer)))
  2299. return NULL;
  2300. return vmalloc_to_page((void *)buffer->user_page + pgoff * PAGE_SIZE);
  2301. }
  2302. static void perf_mmap_unmark_page(void *addr)
  2303. {
  2304. struct page *page = vmalloc_to_page(addr);
  2305. page->mapping = NULL;
  2306. }
  2307. static void perf_buffer_free_work(struct work_struct *work)
  2308. {
  2309. struct perf_buffer *buffer;
  2310. void *base;
  2311. int i, nr;
  2312. buffer = container_of(work, struct perf_buffer, work);
  2313. nr = 1 << page_order(buffer);
  2314. base = buffer->user_page;
  2315. for (i = 0; i < nr + 1; i++)
  2316. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  2317. vfree(base);
  2318. kfree(buffer);
  2319. }
  2320. static void perf_buffer_free(struct perf_buffer *buffer)
  2321. {
  2322. schedule_work(&buffer->work);
  2323. }
  2324. static struct perf_buffer *
  2325. perf_buffer_alloc(int nr_pages, long watermark, int cpu, int flags)
  2326. {
  2327. struct perf_buffer *buffer;
  2328. unsigned long size;
  2329. void *all_buf;
  2330. size = sizeof(struct perf_buffer);
  2331. size += sizeof(void *);
  2332. buffer = kzalloc(size, GFP_KERNEL);
  2333. if (!buffer)
  2334. goto fail;
  2335. INIT_WORK(&buffer->work, perf_buffer_free_work);
  2336. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  2337. if (!all_buf)
  2338. goto fail_all_buf;
  2339. buffer->user_page = all_buf;
  2340. buffer->data_pages[0] = all_buf + PAGE_SIZE;
  2341. buffer->page_order = ilog2(nr_pages);
  2342. buffer->nr_pages = 1;
  2343. perf_buffer_init(buffer, watermark, flags);
  2344. return buffer;
  2345. fail_all_buf:
  2346. kfree(buffer);
  2347. fail:
  2348. return NULL;
  2349. }
  2350. #endif
  2351. static unsigned long perf_data_size(struct perf_buffer *buffer)
  2352. {
  2353. return buffer->nr_pages << (PAGE_SHIFT + page_order(buffer));
  2354. }
  2355. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  2356. {
  2357. struct perf_event *event = vma->vm_file->private_data;
  2358. struct perf_buffer *buffer;
  2359. int ret = VM_FAULT_SIGBUS;
  2360. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  2361. if (vmf->pgoff == 0)
  2362. ret = 0;
  2363. return ret;
  2364. }
  2365. rcu_read_lock();
  2366. buffer = rcu_dereference(event->buffer);
  2367. if (!buffer)
  2368. goto unlock;
  2369. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  2370. goto unlock;
  2371. vmf->page = perf_mmap_to_page(buffer, vmf->pgoff);
  2372. if (!vmf->page)
  2373. goto unlock;
  2374. get_page(vmf->page);
  2375. vmf->page->mapping = vma->vm_file->f_mapping;
  2376. vmf->page->index = vmf->pgoff;
  2377. ret = 0;
  2378. unlock:
  2379. rcu_read_unlock();
  2380. return ret;
  2381. }
  2382. static void perf_buffer_free_rcu(struct rcu_head *rcu_head)
  2383. {
  2384. struct perf_buffer *buffer;
  2385. buffer = container_of(rcu_head, struct perf_buffer, rcu_head);
  2386. perf_buffer_free(buffer);
  2387. }
  2388. static struct perf_buffer *perf_buffer_get(struct perf_event *event)
  2389. {
  2390. struct perf_buffer *buffer;
  2391. rcu_read_lock();
  2392. buffer = rcu_dereference(event->buffer);
  2393. if (buffer) {
  2394. if (!atomic_inc_not_zero(&buffer->refcount))
  2395. buffer = NULL;
  2396. }
  2397. rcu_read_unlock();
  2398. return buffer;
  2399. }
  2400. static void perf_buffer_put(struct perf_buffer *buffer)
  2401. {
  2402. if (!atomic_dec_and_test(&buffer->refcount))
  2403. return;
  2404. call_rcu(&buffer->rcu_head, perf_buffer_free_rcu);
  2405. }
  2406. static void perf_mmap_open(struct vm_area_struct *vma)
  2407. {
  2408. struct perf_event *event = vma->vm_file->private_data;
  2409. atomic_inc(&event->mmap_count);
  2410. }
  2411. static void perf_mmap_close(struct vm_area_struct *vma)
  2412. {
  2413. struct perf_event *event = vma->vm_file->private_data;
  2414. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  2415. unsigned long size = perf_data_size(event->buffer);
  2416. struct user_struct *user = event->mmap_user;
  2417. struct perf_buffer *buffer = event->buffer;
  2418. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  2419. vma->vm_mm->locked_vm -= event->mmap_locked;
  2420. rcu_assign_pointer(event->buffer, NULL);
  2421. mutex_unlock(&event->mmap_mutex);
  2422. perf_buffer_put(buffer);
  2423. free_uid(user);
  2424. }
  2425. }
  2426. static const struct vm_operations_struct perf_mmap_vmops = {
  2427. .open = perf_mmap_open,
  2428. .close = perf_mmap_close,
  2429. .fault = perf_mmap_fault,
  2430. .page_mkwrite = perf_mmap_fault,
  2431. };
  2432. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  2433. {
  2434. struct perf_event *event = file->private_data;
  2435. unsigned long user_locked, user_lock_limit;
  2436. struct user_struct *user = current_user();
  2437. unsigned long locked, lock_limit;
  2438. struct perf_buffer *buffer;
  2439. unsigned long vma_size;
  2440. unsigned long nr_pages;
  2441. long user_extra, extra;
  2442. int ret = 0, flags = 0;
  2443. /*
  2444. * Don't allow mmap() of inherited per-task counters. This would
  2445. * create a performance issue due to all children writing to the
  2446. * same buffer.
  2447. */
  2448. if (event->cpu == -1 && event->attr.inherit)
  2449. return -EINVAL;
  2450. if (!(vma->vm_flags & VM_SHARED))
  2451. return -EINVAL;
  2452. vma_size = vma->vm_end - vma->vm_start;
  2453. nr_pages = (vma_size / PAGE_SIZE) - 1;
  2454. /*
  2455. * If we have buffer pages ensure they're a power-of-two number, so we
  2456. * can do bitmasks instead of modulo.
  2457. */
  2458. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  2459. return -EINVAL;
  2460. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  2461. return -EINVAL;
  2462. if (vma->vm_pgoff != 0)
  2463. return -EINVAL;
  2464. WARN_ON_ONCE(event->ctx->parent_ctx);
  2465. mutex_lock(&event->mmap_mutex);
  2466. if (event->buffer) {
  2467. if (event->buffer->nr_pages == nr_pages)
  2468. atomic_inc(&event->buffer->refcount);
  2469. else
  2470. ret = -EINVAL;
  2471. goto unlock;
  2472. }
  2473. user_extra = nr_pages + 1;
  2474. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  2475. /*
  2476. * Increase the limit linearly with more CPUs:
  2477. */
  2478. user_lock_limit *= num_online_cpus();
  2479. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  2480. extra = 0;
  2481. if (user_locked > user_lock_limit)
  2482. extra = user_locked - user_lock_limit;
  2483. lock_limit = rlimit(RLIMIT_MEMLOCK);
  2484. lock_limit >>= PAGE_SHIFT;
  2485. locked = vma->vm_mm->locked_vm + extra;
  2486. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2487. !capable(CAP_IPC_LOCK)) {
  2488. ret = -EPERM;
  2489. goto unlock;
  2490. }
  2491. WARN_ON(event->buffer);
  2492. if (vma->vm_flags & VM_WRITE)
  2493. flags |= PERF_BUFFER_WRITABLE;
  2494. buffer = perf_buffer_alloc(nr_pages, event->attr.wakeup_watermark,
  2495. event->cpu, flags);
  2496. if (!buffer) {
  2497. ret = -ENOMEM;
  2498. goto unlock;
  2499. }
  2500. rcu_assign_pointer(event->buffer, buffer);
  2501. atomic_long_add(user_extra, &user->locked_vm);
  2502. event->mmap_locked = extra;
  2503. event->mmap_user = get_current_user();
  2504. vma->vm_mm->locked_vm += event->mmap_locked;
  2505. unlock:
  2506. if (!ret)
  2507. atomic_inc(&event->mmap_count);
  2508. mutex_unlock(&event->mmap_mutex);
  2509. vma->vm_flags |= VM_RESERVED;
  2510. vma->vm_ops = &perf_mmap_vmops;
  2511. return ret;
  2512. }
  2513. static int perf_fasync(int fd, struct file *filp, int on)
  2514. {
  2515. struct inode *inode = filp->f_path.dentry->d_inode;
  2516. struct perf_event *event = filp->private_data;
  2517. int retval;
  2518. mutex_lock(&inode->i_mutex);
  2519. retval = fasync_helper(fd, filp, on, &event->fasync);
  2520. mutex_unlock(&inode->i_mutex);
  2521. if (retval < 0)
  2522. return retval;
  2523. return 0;
  2524. }
  2525. static const struct file_operations perf_fops = {
  2526. .llseek = no_llseek,
  2527. .release = perf_release,
  2528. .read = perf_read,
  2529. .poll = perf_poll,
  2530. .unlocked_ioctl = perf_ioctl,
  2531. .compat_ioctl = perf_ioctl,
  2532. .mmap = perf_mmap,
  2533. .fasync = perf_fasync,
  2534. };
  2535. /*
  2536. * Perf event wakeup
  2537. *
  2538. * If there's data, ensure we set the poll() state and publish everything
  2539. * to user-space before waking everybody up.
  2540. */
  2541. void perf_event_wakeup(struct perf_event *event)
  2542. {
  2543. wake_up_all(&event->waitq);
  2544. if (event->pending_kill) {
  2545. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  2546. event->pending_kill = 0;
  2547. }
  2548. }
  2549. static void perf_pending_event(struct irq_work *entry)
  2550. {
  2551. struct perf_event *event = container_of(entry,
  2552. struct perf_event, pending);
  2553. if (event->pending_disable) {
  2554. event->pending_disable = 0;
  2555. __perf_event_disable(event);
  2556. }
  2557. if (event->pending_wakeup) {
  2558. event->pending_wakeup = 0;
  2559. perf_event_wakeup(event);
  2560. }
  2561. }
  2562. /*
  2563. * We assume there is only KVM supporting the callbacks.
  2564. * Later on, we might change it to a list if there is
  2565. * another virtualization implementation supporting the callbacks.
  2566. */
  2567. struct perf_guest_info_callbacks *perf_guest_cbs;
  2568. int perf_register_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2569. {
  2570. perf_guest_cbs = cbs;
  2571. return 0;
  2572. }
  2573. EXPORT_SYMBOL_GPL(perf_register_guest_info_callbacks);
  2574. int perf_unregister_guest_info_callbacks(struct perf_guest_info_callbacks *cbs)
  2575. {
  2576. perf_guest_cbs = NULL;
  2577. return 0;
  2578. }
  2579. EXPORT_SYMBOL_GPL(perf_unregister_guest_info_callbacks);
  2580. /*
  2581. * Output
  2582. */
  2583. static bool perf_output_space(struct perf_buffer *buffer, unsigned long tail,
  2584. unsigned long offset, unsigned long head)
  2585. {
  2586. unsigned long mask;
  2587. if (!buffer->writable)
  2588. return true;
  2589. mask = perf_data_size(buffer) - 1;
  2590. offset = (offset - tail) & mask;
  2591. head = (head - tail) & mask;
  2592. if ((int)(head - offset) < 0)
  2593. return false;
  2594. return true;
  2595. }
  2596. static void perf_output_wakeup(struct perf_output_handle *handle)
  2597. {
  2598. atomic_set(&handle->buffer->poll, POLL_IN);
  2599. if (handle->nmi) {
  2600. handle->event->pending_wakeup = 1;
  2601. irq_work_queue(&handle->event->pending);
  2602. } else
  2603. perf_event_wakeup(handle->event);
  2604. }
  2605. /*
  2606. * We need to ensure a later event_id doesn't publish a head when a former
  2607. * event isn't done writing. However since we need to deal with NMIs we
  2608. * cannot fully serialize things.
  2609. *
  2610. * We only publish the head (and generate a wakeup) when the outer-most
  2611. * event completes.
  2612. */
  2613. static void perf_output_get_handle(struct perf_output_handle *handle)
  2614. {
  2615. struct perf_buffer *buffer = handle->buffer;
  2616. preempt_disable();
  2617. local_inc(&buffer->nest);
  2618. handle->wakeup = local_read(&buffer->wakeup);
  2619. }
  2620. static void perf_output_put_handle(struct perf_output_handle *handle)
  2621. {
  2622. struct perf_buffer *buffer = handle->buffer;
  2623. unsigned long head;
  2624. again:
  2625. head = local_read(&buffer->head);
  2626. /*
  2627. * IRQ/NMI can happen here, which means we can miss a head update.
  2628. */
  2629. if (!local_dec_and_test(&buffer->nest))
  2630. goto out;
  2631. /*
  2632. * Publish the known good head. Rely on the full barrier implied
  2633. * by atomic_dec_and_test() order the buffer->head read and this
  2634. * write.
  2635. */
  2636. buffer->user_page->data_head = head;
  2637. /*
  2638. * Now check if we missed an update, rely on the (compiler)
  2639. * barrier in atomic_dec_and_test() to re-read buffer->head.
  2640. */
  2641. if (unlikely(head != local_read(&buffer->head))) {
  2642. local_inc(&buffer->nest);
  2643. goto again;
  2644. }
  2645. if (handle->wakeup != local_read(&buffer->wakeup))
  2646. perf_output_wakeup(handle);
  2647. out:
  2648. preempt_enable();
  2649. }
  2650. __always_inline void perf_output_copy(struct perf_output_handle *handle,
  2651. const void *buf, unsigned int len)
  2652. {
  2653. do {
  2654. unsigned long size = min_t(unsigned long, handle->size, len);
  2655. memcpy(handle->addr, buf, size);
  2656. len -= size;
  2657. handle->addr += size;
  2658. buf += size;
  2659. handle->size -= size;
  2660. if (!handle->size) {
  2661. struct perf_buffer *buffer = handle->buffer;
  2662. handle->page++;
  2663. handle->page &= buffer->nr_pages - 1;
  2664. handle->addr = buffer->data_pages[handle->page];
  2665. handle->size = PAGE_SIZE << page_order(buffer);
  2666. }
  2667. } while (len);
  2668. }
  2669. int perf_output_begin(struct perf_output_handle *handle,
  2670. struct perf_event *event, unsigned int size,
  2671. int nmi, int sample)
  2672. {
  2673. struct perf_buffer *buffer;
  2674. unsigned long tail, offset, head;
  2675. int have_lost;
  2676. struct {
  2677. struct perf_event_header header;
  2678. u64 id;
  2679. u64 lost;
  2680. } lost_event;
  2681. rcu_read_lock();
  2682. /*
  2683. * For inherited events we send all the output towards the parent.
  2684. */
  2685. if (event->parent)
  2686. event = event->parent;
  2687. buffer = rcu_dereference(event->buffer);
  2688. if (!buffer)
  2689. goto out;
  2690. handle->buffer = buffer;
  2691. handle->event = event;
  2692. handle->nmi = nmi;
  2693. handle->sample = sample;
  2694. if (!buffer->nr_pages)
  2695. goto out;
  2696. have_lost = local_read(&buffer->lost);
  2697. if (have_lost)
  2698. size += sizeof(lost_event);
  2699. perf_output_get_handle(handle);
  2700. do {
  2701. /*
  2702. * Userspace could choose to issue a mb() before updating the
  2703. * tail pointer. So that all reads will be completed before the
  2704. * write is issued.
  2705. */
  2706. tail = ACCESS_ONCE(buffer->user_page->data_tail);
  2707. smp_rmb();
  2708. offset = head = local_read(&buffer->head);
  2709. head += size;
  2710. if (unlikely(!perf_output_space(buffer, tail, offset, head)))
  2711. goto fail;
  2712. } while (local_cmpxchg(&buffer->head, offset, head) != offset);
  2713. if (head - local_read(&buffer->wakeup) > buffer->watermark)
  2714. local_add(buffer->watermark, &buffer->wakeup);
  2715. handle->page = offset >> (PAGE_SHIFT + page_order(buffer));
  2716. handle->page &= buffer->nr_pages - 1;
  2717. handle->size = offset & ((PAGE_SIZE << page_order(buffer)) - 1);
  2718. handle->addr = buffer->data_pages[handle->page];
  2719. handle->addr += handle->size;
  2720. handle->size = (PAGE_SIZE << page_order(buffer)) - handle->size;
  2721. if (have_lost) {
  2722. lost_event.header.type = PERF_RECORD_LOST;
  2723. lost_event.header.misc = 0;
  2724. lost_event.header.size = sizeof(lost_event);
  2725. lost_event.id = event->id;
  2726. lost_event.lost = local_xchg(&buffer->lost, 0);
  2727. perf_output_put(handle, lost_event);
  2728. }
  2729. return 0;
  2730. fail:
  2731. local_inc(&buffer->lost);
  2732. perf_output_put_handle(handle);
  2733. out:
  2734. rcu_read_unlock();
  2735. return -ENOSPC;
  2736. }
  2737. void perf_output_end(struct perf_output_handle *handle)
  2738. {
  2739. struct perf_event *event = handle->event;
  2740. struct perf_buffer *buffer = handle->buffer;
  2741. int wakeup_events = event->attr.wakeup_events;
  2742. if (handle->sample && wakeup_events) {
  2743. int events = local_inc_return(&buffer->events);
  2744. if (events >= wakeup_events) {
  2745. local_sub(wakeup_events, &buffer->events);
  2746. local_inc(&buffer->wakeup);
  2747. }
  2748. }
  2749. perf_output_put_handle(handle);
  2750. rcu_read_unlock();
  2751. }
  2752. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  2753. {
  2754. /*
  2755. * only top level events have the pid namespace they were created in
  2756. */
  2757. if (event->parent)
  2758. event = event->parent;
  2759. return task_tgid_nr_ns(p, event->ns);
  2760. }
  2761. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  2762. {
  2763. /*
  2764. * only top level events have the pid namespace they were created in
  2765. */
  2766. if (event->parent)
  2767. event = event->parent;
  2768. return task_pid_nr_ns(p, event->ns);
  2769. }
  2770. static void perf_output_read_one(struct perf_output_handle *handle,
  2771. struct perf_event *event)
  2772. {
  2773. u64 read_format = event->attr.read_format;
  2774. u64 values[4];
  2775. int n = 0;
  2776. values[n++] = perf_event_count(event);
  2777. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  2778. values[n++] = event->total_time_enabled +
  2779. atomic64_read(&event->child_total_time_enabled);
  2780. }
  2781. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  2782. values[n++] = event->total_time_running +
  2783. atomic64_read(&event->child_total_time_running);
  2784. }
  2785. if (read_format & PERF_FORMAT_ID)
  2786. values[n++] = primary_event_id(event);
  2787. perf_output_copy(handle, values, n * sizeof(u64));
  2788. }
  2789. /*
  2790. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  2791. */
  2792. static void perf_output_read_group(struct perf_output_handle *handle,
  2793. struct perf_event *event)
  2794. {
  2795. struct perf_event *leader = event->group_leader, *sub;
  2796. u64 read_format = event->attr.read_format;
  2797. u64 values[5];
  2798. int n = 0;
  2799. values[n++] = 1 + leader->nr_siblings;
  2800. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2801. values[n++] = leader->total_time_enabled;
  2802. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2803. values[n++] = leader->total_time_running;
  2804. if (leader != event)
  2805. leader->pmu->read(leader);
  2806. values[n++] = perf_event_count(leader);
  2807. if (read_format & PERF_FORMAT_ID)
  2808. values[n++] = primary_event_id(leader);
  2809. perf_output_copy(handle, values, n * sizeof(u64));
  2810. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2811. n = 0;
  2812. if (sub != event)
  2813. sub->pmu->read(sub);
  2814. values[n++] = perf_event_count(sub);
  2815. if (read_format & PERF_FORMAT_ID)
  2816. values[n++] = primary_event_id(sub);
  2817. perf_output_copy(handle, values, n * sizeof(u64));
  2818. }
  2819. }
  2820. static void perf_output_read(struct perf_output_handle *handle,
  2821. struct perf_event *event)
  2822. {
  2823. if (event->attr.read_format & PERF_FORMAT_GROUP)
  2824. perf_output_read_group(handle, event);
  2825. else
  2826. perf_output_read_one(handle, event);
  2827. }
  2828. void perf_output_sample(struct perf_output_handle *handle,
  2829. struct perf_event_header *header,
  2830. struct perf_sample_data *data,
  2831. struct perf_event *event)
  2832. {
  2833. u64 sample_type = data->type;
  2834. perf_output_put(handle, *header);
  2835. if (sample_type & PERF_SAMPLE_IP)
  2836. perf_output_put(handle, data->ip);
  2837. if (sample_type & PERF_SAMPLE_TID)
  2838. perf_output_put(handle, data->tid_entry);
  2839. if (sample_type & PERF_SAMPLE_TIME)
  2840. perf_output_put(handle, data->time);
  2841. if (sample_type & PERF_SAMPLE_ADDR)
  2842. perf_output_put(handle, data->addr);
  2843. if (sample_type & PERF_SAMPLE_ID)
  2844. perf_output_put(handle, data->id);
  2845. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2846. perf_output_put(handle, data->stream_id);
  2847. if (sample_type & PERF_SAMPLE_CPU)
  2848. perf_output_put(handle, data->cpu_entry);
  2849. if (sample_type & PERF_SAMPLE_PERIOD)
  2850. perf_output_put(handle, data->period);
  2851. if (sample_type & PERF_SAMPLE_READ)
  2852. perf_output_read(handle, event);
  2853. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2854. if (data->callchain) {
  2855. int size = 1;
  2856. if (data->callchain)
  2857. size += data->callchain->nr;
  2858. size *= sizeof(u64);
  2859. perf_output_copy(handle, data->callchain, size);
  2860. } else {
  2861. u64 nr = 0;
  2862. perf_output_put(handle, nr);
  2863. }
  2864. }
  2865. if (sample_type & PERF_SAMPLE_RAW) {
  2866. if (data->raw) {
  2867. perf_output_put(handle, data->raw->size);
  2868. perf_output_copy(handle, data->raw->data,
  2869. data->raw->size);
  2870. } else {
  2871. struct {
  2872. u32 size;
  2873. u32 data;
  2874. } raw = {
  2875. .size = sizeof(u32),
  2876. .data = 0,
  2877. };
  2878. perf_output_put(handle, raw);
  2879. }
  2880. }
  2881. }
  2882. void perf_prepare_sample(struct perf_event_header *header,
  2883. struct perf_sample_data *data,
  2884. struct perf_event *event,
  2885. struct pt_regs *regs)
  2886. {
  2887. u64 sample_type = event->attr.sample_type;
  2888. data->type = sample_type;
  2889. header->type = PERF_RECORD_SAMPLE;
  2890. header->size = sizeof(*header);
  2891. header->misc = 0;
  2892. header->misc |= perf_misc_flags(regs);
  2893. if (sample_type & PERF_SAMPLE_IP) {
  2894. data->ip = perf_instruction_pointer(regs);
  2895. header->size += sizeof(data->ip);
  2896. }
  2897. if (sample_type & PERF_SAMPLE_TID) {
  2898. /* namespace issues */
  2899. data->tid_entry.pid = perf_event_pid(event, current);
  2900. data->tid_entry.tid = perf_event_tid(event, current);
  2901. header->size += sizeof(data->tid_entry);
  2902. }
  2903. if (sample_type & PERF_SAMPLE_TIME) {
  2904. data->time = perf_clock();
  2905. header->size += sizeof(data->time);
  2906. }
  2907. if (sample_type & PERF_SAMPLE_ADDR)
  2908. header->size += sizeof(data->addr);
  2909. if (sample_type & PERF_SAMPLE_ID) {
  2910. data->id = primary_event_id(event);
  2911. header->size += sizeof(data->id);
  2912. }
  2913. if (sample_type & PERF_SAMPLE_STREAM_ID) {
  2914. data->stream_id = event->id;
  2915. header->size += sizeof(data->stream_id);
  2916. }
  2917. if (sample_type & PERF_SAMPLE_CPU) {
  2918. data->cpu_entry.cpu = raw_smp_processor_id();
  2919. data->cpu_entry.reserved = 0;
  2920. header->size += sizeof(data->cpu_entry);
  2921. }
  2922. if (sample_type & PERF_SAMPLE_PERIOD)
  2923. header->size += sizeof(data->period);
  2924. if (sample_type & PERF_SAMPLE_READ)
  2925. header->size += perf_event_read_size(event);
  2926. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2927. int size = 1;
  2928. data->callchain = perf_callchain(regs);
  2929. if (data->callchain)
  2930. size += data->callchain->nr;
  2931. header->size += size * sizeof(u64);
  2932. }
  2933. if (sample_type & PERF_SAMPLE_RAW) {
  2934. int size = sizeof(u32);
  2935. if (data->raw)
  2936. size += data->raw->size;
  2937. else
  2938. size += sizeof(u32);
  2939. WARN_ON_ONCE(size & (sizeof(u64)-1));
  2940. header->size += size;
  2941. }
  2942. }
  2943. static void perf_event_output(struct perf_event *event, int nmi,
  2944. struct perf_sample_data *data,
  2945. struct pt_regs *regs)
  2946. {
  2947. struct perf_output_handle handle;
  2948. struct perf_event_header header;
  2949. /* protect the callchain buffers */
  2950. rcu_read_lock();
  2951. perf_prepare_sample(&header, data, event, regs);
  2952. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  2953. goto exit;
  2954. perf_output_sample(&handle, &header, data, event);
  2955. perf_output_end(&handle);
  2956. exit:
  2957. rcu_read_unlock();
  2958. }
  2959. /*
  2960. * read event_id
  2961. */
  2962. struct perf_read_event {
  2963. struct perf_event_header header;
  2964. u32 pid;
  2965. u32 tid;
  2966. };
  2967. static void
  2968. perf_event_read_event(struct perf_event *event,
  2969. struct task_struct *task)
  2970. {
  2971. struct perf_output_handle handle;
  2972. struct perf_read_event read_event = {
  2973. .header = {
  2974. .type = PERF_RECORD_READ,
  2975. .misc = 0,
  2976. .size = sizeof(read_event) + perf_event_read_size(event),
  2977. },
  2978. .pid = perf_event_pid(event, task),
  2979. .tid = perf_event_tid(event, task),
  2980. };
  2981. int ret;
  2982. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  2983. if (ret)
  2984. return;
  2985. perf_output_put(&handle, read_event);
  2986. perf_output_read(&handle, event);
  2987. perf_output_end(&handle);
  2988. }
  2989. /*
  2990. * task tracking -- fork/exit
  2991. *
  2992. * enabled by: attr.comm | attr.mmap | attr.mmap_data | attr.task
  2993. */
  2994. struct perf_task_event {
  2995. struct task_struct *task;
  2996. struct perf_event_context *task_ctx;
  2997. struct {
  2998. struct perf_event_header header;
  2999. u32 pid;
  3000. u32 ppid;
  3001. u32 tid;
  3002. u32 ptid;
  3003. u64 time;
  3004. } event_id;
  3005. };
  3006. static void perf_event_task_output(struct perf_event *event,
  3007. struct perf_task_event *task_event)
  3008. {
  3009. struct perf_output_handle handle;
  3010. struct task_struct *task = task_event->task;
  3011. int size, ret;
  3012. size = task_event->event_id.header.size;
  3013. ret = perf_output_begin(&handle, event, size, 0, 0);
  3014. if (ret)
  3015. return;
  3016. task_event->event_id.pid = perf_event_pid(event, task);
  3017. task_event->event_id.ppid = perf_event_pid(event, current);
  3018. task_event->event_id.tid = perf_event_tid(event, task);
  3019. task_event->event_id.ptid = perf_event_tid(event, current);
  3020. perf_output_put(&handle, task_event->event_id);
  3021. perf_output_end(&handle);
  3022. }
  3023. static int perf_event_task_match(struct perf_event *event)
  3024. {
  3025. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3026. return 0;
  3027. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3028. return 0;
  3029. if (event->attr.comm || event->attr.mmap ||
  3030. event->attr.mmap_data || event->attr.task)
  3031. return 1;
  3032. return 0;
  3033. }
  3034. static void perf_event_task_ctx(struct perf_event_context *ctx,
  3035. struct perf_task_event *task_event)
  3036. {
  3037. struct perf_event *event;
  3038. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3039. if (perf_event_task_match(event))
  3040. perf_event_task_output(event, task_event);
  3041. }
  3042. }
  3043. static void perf_event_task_event(struct perf_task_event *task_event)
  3044. {
  3045. struct perf_cpu_context *cpuctx;
  3046. struct perf_event_context *ctx;
  3047. struct pmu *pmu;
  3048. int ctxn;
  3049. rcu_read_lock();
  3050. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3051. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3052. perf_event_task_ctx(&cpuctx->ctx, task_event);
  3053. ctx = task_event->task_ctx;
  3054. if (!ctx) {
  3055. ctxn = pmu->task_ctx_nr;
  3056. if (ctxn < 0)
  3057. goto next;
  3058. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3059. }
  3060. if (ctx)
  3061. perf_event_task_ctx(ctx, task_event);
  3062. next:
  3063. put_cpu_ptr(pmu->pmu_cpu_context);
  3064. }
  3065. rcu_read_unlock();
  3066. }
  3067. static void perf_event_task(struct task_struct *task,
  3068. struct perf_event_context *task_ctx,
  3069. int new)
  3070. {
  3071. struct perf_task_event task_event;
  3072. if (!atomic_read(&nr_comm_events) &&
  3073. !atomic_read(&nr_mmap_events) &&
  3074. !atomic_read(&nr_task_events))
  3075. return;
  3076. task_event = (struct perf_task_event){
  3077. .task = task,
  3078. .task_ctx = task_ctx,
  3079. .event_id = {
  3080. .header = {
  3081. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  3082. .misc = 0,
  3083. .size = sizeof(task_event.event_id),
  3084. },
  3085. /* .pid */
  3086. /* .ppid */
  3087. /* .tid */
  3088. /* .ptid */
  3089. .time = perf_clock(),
  3090. },
  3091. };
  3092. perf_event_task_event(&task_event);
  3093. }
  3094. void perf_event_fork(struct task_struct *task)
  3095. {
  3096. perf_event_task(task, NULL, 1);
  3097. }
  3098. /*
  3099. * comm tracking
  3100. */
  3101. struct perf_comm_event {
  3102. struct task_struct *task;
  3103. char *comm;
  3104. int comm_size;
  3105. struct {
  3106. struct perf_event_header header;
  3107. u32 pid;
  3108. u32 tid;
  3109. } event_id;
  3110. };
  3111. static void perf_event_comm_output(struct perf_event *event,
  3112. struct perf_comm_event *comm_event)
  3113. {
  3114. struct perf_output_handle handle;
  3115. int size = comm_event->event_id.header.size;
  3116. int ret = perf_output_begin(&handle, event, size, 0, 0);
  3117. if (ret)
  3118. return;
  3119. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  3120. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  3121. perf_output_put(&handle, comm_event->event_id);
  3122. perf_output_copy(&handle, comm_event->comm,
  3123. comm_event->comm_size);
  3124. perf_output_end(&handle);
  3125. }
  3126. static int perf_event_comm_match(struct perf_event *event)
  3127. {
  3128. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3129. return 0;
  3130. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3131. return 0;
  3132. if (event->attr.comm)
  3133. return 1;
  3134. return 0;
  3135. }
  3136. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  3137. struct perf_comm_event *comm_event)
  3138. {
  3139. struct perf_event *event;
  3140. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3141. if (perf_event_comm_match(event))
  3142. perf_event_comm_output(event, comm_event);
  3143. }
  3144. }
  3145. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  3146. {
  3147. struct perf_cpu_context *cpuctx;
  3148. struct perf_event_context *ctx;
  3149. char comm[TASK_COMM_LEN];
  3150. unsigned int size;
  3151. struct pmu *pmu;
  3152. int ctxn;
  3153. memset(comm, 0, sizeof(comm));
  3154. strlcpy(comm, comm_event->task->comm, sizeof(comm));
  3155. size = ALIGN(strlen(comm)+1, sizeof(u64));
  3156. comm_event->comm = comm;
  3157. comm_event->comm_size = size;
  3158. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  3159. rcu_read_lock();
  3160. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3161. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3162. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  3163. ctxn = pmu->task_ctx_nr;
  3164. if (ctxn < 0)
  3165. goto next;
  3166. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3167. if (ctx)
  3168. perf_event_comm_ctx(ctx, comm_event);
  3169. next:
  3170. put_cpu_ptr(pmu->pmu_cpu_context);
  3171. }
  3172. rcu_read_unlock();
  3173. }
  3174. void perf_event_comm(struct task_struct *task)
  3175. {
  3176. struct perf_comm_event comm_event;
  3177. struct perf_event_context *ctx;
  3178. int ctxn;
  3179. for_each_task_context_nr(ctxn) {
  3180. ctx = task->perf_event_ctxp[ctxn];
  3181. if (!ctx)
  3182. continue;
  3183. perf_event_enable_on_exec(ctx);
  3184. }
  3185. if (!atomic_read(&nr_comm_events))
  3186. return;
  3187. comm_event = (struct perf_comm_event){
  3188. .task = task,
  3189. /* .comm */
  3190. /* .comm_size */
  3191. .event_id = {
  3192. .header = {
  3193. .type = PERF_RECORD_COMM,
  3194. .misc = 0,
  3195. /* .size */
  3196. },
  3197. /* .pid */
  3198. /* .tid */
  3199. },
  3200. };
  3201. perf_event_comm_event(&comm_event);
  3202. }
  3203. /*
  3204. * mmap tracking
  3205. */
  3206. struct perf_mmap_event {
  3207. struct vm_area_struct *vma;
  3208. const char *file_name;
  3209. int file_size;
  3210. struct {
  3211. struct perf_event_header header;
  3212. u32 pid;
  3213. u32 tid;
  3214. u64 start;
  3215. u64 len;
  3216. u64 pgoff;
  3217. } event_id;
  3218. };
  3219. static void perf_event_mmap_output(struct perf_event *event,
  3220. struct perf_mmap_event *mmap_event)
  3221. {
  3222. struct perf_output_handle handle;
  3223. int size = mmap_event->event_id.header.size;
  3224. int ret = perf_output_begin(&handle, event, size, 0, 0);
  3225. if (ret)
  3226. return;
  3227. mmap_event->event_id.pid = perf_event_pid(event, current);
  3228. mmap_event->event_id.tid = perf_event_tid(event, current);
  3229. perf_output_put(&handle, mmap_event->event_id);
  3230. perf_output_copy(&handle, mmap_event->file_name,
  3231. mmap_event->file_size);
  3232. perf_output_end(&handle);
  3233. }
  3234. static int perf_event_mmap_match(struct perf_event *event,
  3235. struct perf_mmap_event *mmap_event,
  3236. int executable)
  3237. {
  3238. if (event->state < PERF_EVENT_STATE_INACTIVE)
  3239. return 0;
  3240. if (event->cpu != -1 && event->cpu != smp_processor_id())
  3241. return 0;
  3242. if ((!executable && event->attr.mmap_data) ||
  3243. (executable && event->attr.mmap))
  3244. return 1;
  3245. return 0;
  3246. }
  3247. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  3248. struct perf_mmap_event *mmap_event,
  3249. int executable)
  3250. {
  3251. struct perf_event *event;
  3252. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3253. if (perf_event_mmap_match(event, mmap_event, executable))
  3254. perf_event_mmap_output(event, mmap_event);
  3255. }
  3256. }
  3257. static void perf_event_mmap_event(struct perf_mmap_event *mmap_event)
  3258. {
  3259. struct perf_cpu_context *cpuctx;
  3260. struct perf_event_context *ctx;
  3261. struct vm_area_struct *vma = mmap_event->vma;
  3262. struct file *file = vma->vm_file;
  3263. unsigned int size;
  3264. char tmp[16];
  3265. char *buf = NULL;
  3266. const char *name;
  3267. struct pmu *pmu;
  3268. int ctxn;
  3269. memset(tmp, 0, sizeof(tmp));
  3270. if (file) {
  3271. /*
  3272. * d_path works from the end of the buffer backwards, so we
  3273. * need to add enough zero bytes after the string to handle
  3274. * the 64bit alignment we do later.
  3275. */
  3276. buf = kzalloc(PATH_MAX + sizeof(u64), GFP_KERNEL);
  3277. if (!buf) {
  3278. name = strncpy(tmp, "//enomem", sizeof(tmp));
  3279. goto got_name;
  3280. }
  3281. name = d_path(&file->f_path, buf, PATH_MAX);
  3282. if (IS_ERR(name)) {
  3283. name = strncpy(tmp, "//toolong", sizeof(tmp));
  3284. goto got_name;
  3285. }
  3286. } else {
  3287. if (arch_vma_name(mmap_event->vma)) {
  3288. name = strncpy(tmp, arch_vma_name(mmap_event->vma),
  3289. sizeof(tmp));
  3290. goto got_name;
  3291. }
  3292. if (!vma->vm_mm) {
  3293. name = strncpy(tmp, "[vdso]", sizeof(tmp));
  3294. goto got_name;
  3295. } else if (vma->vm_start <= vma->vm_mm->start_brk &&
  3296. vma->vm_end >= vma->vm_mm->brk) {
  3297. name = strncpy(tmp, "[heap]", sizeof(tmp));
  3298. goto got_name;
  3299. } else if (vma->vm_start <= vma->vm_mm->start_stack &&
  3300. vma->vm_end >= vma->vm_mm->start_stack) {
  3301. name = strncpy(tmp, "[stack]", sizeof(tmp));
  3302. goto got_name;
  3303. }
  3304. name = strncpy(tmp, "//anon", sizeof(tmp));
  3305. goto got_name;
  3306. }
  3307. got_name:
  3308. size = ALIGN(strlen(name)+1, sizeof(u64));
  3309. mmap_event->file_name = name;
  3310. mmap_event->file_size = size;
  3311. mmap_event->event_id.header.size = sizeof(mmap_event->event_id) + size;
  3312. rcu_read_lock();
  3313. list_for_each_entry_rcu(pmu, &pmus, entry) {
  3314. cpuctx = get_cpu_ptr(pmu->pmu_cpu_context);
  3315. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event,
  3316. vma->vm_flags & VM_EXEC);
  3317. ctxn = pmu->task_ctx_nr;
  3318. if (ctxn < 0)
  3319. goto next;
  3320. ctx = rcu_dereference(current->perf_event_ctxp[ctxn]);
  3321. if (ctx) {
  3322. perf_event_mmap_ctx(ctx, mmap_event,
  3323. vma->vm_flags & VM_EXEC);
  3324. }
  3325. next:
  3326. put_cpu_ptr(pmu->pmu_cpu_context);
  3327. }
  3328. rcu_read_unlock();
  3329. kfree(buf);
  3330. }
  3331. void perf_event_mmap(struct vm_area_struct *vma)
  3332. {
  3333. struct perf_mmap_event mmap_event;
  3334. if (!atomic_read(&nr_mmap_events))
  3335. return;
  3336. mmap_event = (struct perf_mmap_event){
  3337. .vma = vma,
  3338. /* .file_name */
  3339. /* .file_size */
  3340. .event_id = {
  3341. .header = {
  3342. .type = PERF_RECORD_MMAP,
  3343. .misc = PERF_RECORD_MISC_USER,
  3344. /* .size */
  3345. },
  3346. /* .pid */
  3347. /* .tid */
  3348. .start = vma->vm_start,
  3349. .len = vma->vm_end - vma->vm_start,
  3350. .pgoff = (u64)vma->vm_pgoff << PAGE_SHIFT,
  3351. },
  3352. };
  3353. perf_event_mmap_event(&mmap_event);
  3354. }
  3355. /*
  3356. * IRQ throttle logging
  3357. */
  3358. static void perf_log_throttle(struct perf_event *event, int enable)
  3359. {
  3360. struct perf_output_handle handle;
  3361. int ret;
  3362. struct {
  3363. struct perf_event_header header;
  3364. u64 time;
  3365. u64 id;
  3366. u64 stream_id;
  3367. } throttle_event = {
  3368. .header = {
  3369. .type = PERF_RECORD_THROTTLE,
  3370. .misc = 0,
  3371. .size = sizeof(throttle_event),
  3372. },
  3373. .time = perf_clock(),
  3374. .id = primary_event_id(event),
  3375. .stream_id = event->id,
  3376. };
  3377. if (enable)
  3378. throttle_event.header.type = PERF_RECORD_UNTHROTTLE;
  3379. ret = perf_output_begin(&handle, event, sizeof(throttle_event), 1, 0);
  3380. if (ret)
  3381. return;
  3382. perf_output_put(&handle, throttle_event);
  3383. perf_output_end(&handle);
  3384. }
  3385. /*
  3386. * Generic event overflow handling, sampling.
  3387. */
  3388. static int __perf_event_overflow(struct perf_event *event, int nmi,
  3389. int throttle, struct perf_sample_data *data,
  3390. struct pt_regs *regs)
  3391. {
  3392. int events = atomic_read(&event->event_limit);
  3393. struct hw_perf_event *hwc = &event->hw;
  3394. int ret = 0;
  3395. if (!throttle) {
  3396. hwc->interrupts++;
  3397. } else {
  3398. if (hwc->interrupts != MAX_INTERRUPTS) {
  3399. hwc->interrupts++;
  3400. if (HZ * hwc->interrupts >
  3401. (u64)sysctl_perf_event_sample_rate) {
  3402. hwc->interrupts = MAX_INTERRUPTS;
  3403. perf_log_throttle(event, 0);
  3404. ret = 1;
  3405. }
  3406. } else {
  3407. /*
  3408. * Keep re-disabling events even though on the previous
  3409. * pass we disabled it - just in case we raced with a
  3410. * sched-in and the event got enabled again:
  3411. */
  3412. ret = 1;
  3413. }
  3414. }
  3415. if (event->attr.freq) {
  3416. u64 now = perf_clock();
  3417. s64 delta = now - hwc->freq_time_stamp;
  3418. hwc->freq_time_stamp = now;
  3419. if (delta > 0 && delta < 2*TICK_NSEC)
  3420. perf_adjust_period(event, delta, hwc->last_period);
  3421. }
  3422. /*
  3423. * XXX event_limit might not quite work as expected on inherited
  3424. * events
  3425. */
  3426. event->pending_kill = POLL_IN;
  3427. if (events && atomic_dec_and_test(&event->event_limit)) {
  3428. ret = 1;
  3429. event->pending_kill = POLL_HUP;
  3430. if (nmi) {
  3431. event->pending_disable = 1;
  3432. irq_work_queue(&event->pending);
  3433. } else
  3434. perf_event_disable(event);
  3435. }
  3436. if (event->overflow_handler)
  3437. event->overflow_handler(event, nmi, data, regs);
  3438. else
  3439. perf_event_output(event, nmi, data, regs);
  3440. return ret;
  3441. }
  3442. int perf_event_overflow(struct perf_event *event, int nmi,
  3443. struct perf_sample_data *data,
  3444. struct pt_regs *regs)
  3445. {
  3446. return __perf_event_overflow(event, nmi, 1, data, regs);
  3447. }
  3448. /*
  3449. * Generic software event infrastructure
  3450. */
  3451. struct swevent_htable {
  3452. struct swevent_hlist *swevent_hlist;
  3453. struct mutex hlist_mutex;
  3454. int hlist_refcount;
  3455. /* Recursion avoidance in each contexts */
  3456. int recursion[PERF_NR_CONTEXTS];
  3457. };
  3458. static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
  3459. /*
  3460. * We directly increment event->count and keep a second value in
  3461. * event->hw.period_left to count intervals. This period event
  3462. * is kept in the range [-sample_period, 0] so that we can use the
  3463. * sign as trigger.
  3464. */
  3465. static u64 perf_swevent_set_period(struct perf_event *event)
  3466. {
  3467. struct hw_perf_event *hwc = &event->hw;
  3468. u64 period = hwc->last_period;
  3469. u64 nr, offset;
  3470. s64 old, val;
  3471. hwc->last_period = hwc->sample_period;
  3472. again:
  3473. old = val = local64_read(&hwc->period_left);
  3474. if (val < 0)
  3475. return 0;
  3476. nr = div64_u64(period + val, period);
  3477. offset = nr * period;
  3478. val -= offset;
  3479. if (local64_cmpxchg(&hwc->period_left, old, val) != old)
  3480. goto again;
  3481. return nr;
  3482. }
  3483. static void perf_swevent_overflow(struct perf_event *event, u64 overflow,
  3484. int nmi, struct perf_sample_data *data,
  3485. struct pt_regs *regs)
  3486. {
  3487. struct hw_perf_event *hwc = &event->hw;
  3488. int throttle = 0;
  3489. data->period = event->hw.last_period;
  3490. if (!overflow)
  3491. overflow = perf_swevent_set_period(event);
  3492. if (hwc->interrupts == MAX_INTERRUPTS)
  3493. return;
  3494. for (; overflow; overflow--) {
  3495. if (__perf_event_overflow(event, nmi, throttle,
  3496. data, regs)) {
  3497. /*
  3498. * We inhibit the overflow from happening when
  3499. * hwc->interrupts == MAX_INTERRUPTS.
  3500. */
  3501. break;
  3502. }
  3503. throttle = 1;
  3504. }
  3505. }
  3506. static void perf_swevent_event(struct perf_event *event, u64 nr,
  3507. int nmi, struct perf_sample_data *data,
  3508. struct pt_regs *regs)
  3509. {
  3510. struct hw_perf_event *hwc = &event->hw;
  3511. local64_add(nr, &event->count);
  3512. if (!regs)
  3513. return;
  3514. if (!hwc->sample_period)
  3515. return;
  3516. if (nr == 1 && hwc->sample_period == 1 && !event->attr.freq)
  3517. return perf_swevent_overflow(event, 1, nmi, data, regs);
  3518. if (local64_add_negative(nr, &hwc->period_left))
  3519. return;
  3520. perf_swevent_overflow(event, 0, nmi, data, regs);
  3521. }
  3522. static int perf_exclude_event(struct perf_event *event,
  3523. struct pt_regs *regs)
  3524. {
  3525. if (event->hw.state & PERF_HES_STOPPED)
  3526. return 0;
  3527. if (regs) {
  3528. if (event->attr.exclude_user && user_mode(regs))
  3529. return 1;
  3530. if (event->attr.exclude_kernel && !user_mode(regs))
  3531. return 1;
  3532. }
  3533. return 0;
  3534. }
  3535. static int perf_swevent_match(struct perf_event *event,
  3536. enum perf_type_id type,
  3537. u32 event_id,
  3538. struct perf_sample_data *data,
  3539. struct pt_regs *regs)
  3540. {
  3541. if (event->attr.type != type)
  3542. return 0;
  3543. if (event->attr.config != event_id)
  3544. return 0;
  3545. if (perf_exclude_event(event, regs))
  3546. return 0;
  3547. return 1;
  3548. }
  3549. static inline u64 swevent_hash(u64 type, u32 event_id)
  3550. {
  3551. u64 val = event_id | (type << 32);
  3552. return hash_64(val, SWEVENT_HLIST_BITS);
  3553. }
  3554. static inline struct hlist_head *
  3555. __find_swevent_head(struct swevent_hlist *hlist, u64 type, u32 event_id)
  3556. {
  3557. u64 hash = swevent_hash(type, event_id);
  3558. return &hlist->heads[hash];
  3559. }
  3560. /* For the read side: events when they trigger */
  3561. static inline struct hlist_head *
  3562. find_swevent_head_rcu(struct swevent_htable *swhash, u64 type, u32 event_id)
  3563. {
  3564. struct swevent_hlist *hlist;
  3565. hlist = rcu_dereference(swhash->swevent_hlist);
  3566. if (!hlist)
  3567. return NULL;
  3568. return __find_swevent_head(hlist, type, event_id);
  3569. }
  3570. /* For the event head insertion and removal in the hlist */
  3571. static inline struct hlist_head *
  3572. find_swevent_head(struct swevent_htable *swhash, struct perf_event *event)
  3573. {
  3574. struct swevent_hlist *hlist;
  3575. u32 event_id = event->attr.config;
  3576. u64 type = event->attr.type;
  3577. /*
  3578. * Event scheduling is always serialized against hlist allocation
  3579. * and release. Which makes the protected version suitable here.
  3580. * The context lock guarantees that.
  3581. */
  3582. hlist = rcu_dereference_protected(swhash->swevent_hlist,
  3583. lockdep_is_held(&event->ctx->lock));
  3584. if (!hlist)
  3585. return NULL;
  3586. return __find_swevent_head(hlist, type, event_id);
  3587. }
  3588. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3589. u64 nr, int nmi,
  3590. struct perf_sample_data *data,
  3591. struct pt_regs *regs)
  3592. {
  3593. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3594. struct perf_event *event;
  3595. struct hlist_node *node;
  3596. struct hlist_head *head;
  3597. rcu_read_lock();
  3598. head = find_swevent_head_rcu(swhash, type, event_id);
  3599. if (!head)
  3600. goto end;
  3601. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3602. if (perf_swevent_match(event, type, event_id, data, regs))
  3603. perf_swevent_event(event, nr, nmi, data, regs);
  3604. }
  3605. end:
  3606. rcu_read_unlock();
  3607. }
  3608. int perf_swevent_get_recursion_context(void)
  3609. {
  3610. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3611. return get_recursion_context(swhash->recursion);
  3612. }
  3613. EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
  3614. void inline perf_swevent_put_recursion_context(int rctx)
  3615. {
  3616. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3617. put_recursion_context(swhash->recursion, rctx);
  3618. }
  3619. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3620. struct pt_regs *regs, u64 addr)
  3621. {
  3622. struct perf_sample_data data;
  3623. int rctx;
  3624. preempt_disable_notrace();
  3625. rctx = perf_swevent_get_recursion_context();
  3626. if (rctx < 0)
  3627. return;
  3628. perf_sample_data_init(&data, addr);
  3629. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi, &data, regs);
  3630. perf_swevent_put_recursion_context(rctx);
  3631. preempt_enable_notrace();
  3632. }
  3633. static void perf_swevent_read(struct perf_event *event)
  3634. {
  3635. }
  3636. static int perf_swevent_add(struct perf_event *event, int flags)
  3637. {
  3638. struct swevent_htable *swhash = &__get_cpu_var(swevent_htable);
  3639. struct hw_perf_event *hwc = &event->hw;
  3640. struct hlist_head *head;
  3641. if (hwc->sample_period) {
  3642. hwc->last_period = hwc->sample_period;
  3643. perf_swevent_set_period(event);
  3644. }
  3645. hwc->state = !(flags & PERF_EF_START);
  3646. head = find_swevent_head(swhash, event);
  3647. if (WARN_ON_ONCE(!head))
  3648. return -EINVAL;
  3649. hlist_add_head_rcu(&event->hlist_entry, head);
  3650. return 0;
  3651. }
  3652. static void perf_swevent_del(struct perf_event *event, int flags)
  3653. {
  3654. hlist_del_rcu(&event->hlist_entry);
  3655. }
  3656. static void perf_swevent_start(struct perf_event *event, int flags)
  3657. {
  3658. event->hw.state = 0;
  3659. }
  3660. static void perf_swevent_stop(struct perf_event *event, int flags)
  3661. {
  3662. event->hw.state = PERF_HES_STOPPED;
  3663. }
  3664. /* Deref the hlist from the update side */
  3665. static inline struct swevent_hlist *
  3666. swevent_hlist_deref(struct swevent_htable *swhash)
  3667. {
  3668. return rcu_dereference_protected(swhash->swevent_hlist,
  3669. lockdep_is_held(&swhash->hlist_mutex));
  3670. }
  3671. static void swevent_hlist_release_rcu(struct rcu_head *rcu_head)
  3672. {
  3673. struct swevent_hlist *hlist;
  3674. hlist = container_of(rcu_head, struct swevent_hlist, rcu_head);
  3675. kfree(hlist);
  3676. }
  3677. static void swevent_hlist_release(struct swevent_htable *swhash)
  3678. {
  3679. struct swevent_hlist *hlist = swevent_hlist_deref(swhash);
  3680. if (!hlist)
  3681. return;
  3682. rcu_assign_pointer(swhash->swevent_hlist, NULL);
  3683. call_rcu(&hlist->rcu_head, swevent_hlist_release_rcu);
  3684. }
  3685. static void swevent_hlist_put_cpu(struct perf_event *event, int cpu)
  3686. {
  3687. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  3688. mutex_lock(&swhash->hlist_mutex);
  3689. if (!--swhash->hlist_refcount)
  3690. swevent_hlist_release(swhash);
  3691. mutex_unlock(&swhash->hlist_mutex);
  3692. }
  3693. static void swevent_hlist_put(struct perf_event *event)
  3694. {
  3695. int cpu;
  3696. if (event->cpu != -1) {
  3697. swevent_hlist_put_cpu(event, event->cpu);
  3698. return;
  3699. }
  3700. for_each_possible_cpu(cpu)
  3701. swevent_hlist_put_cpu(event, cpu);
  3702. }
  3703. static int swevent_hlist_get_cpu(struct perf_event *event, int cpu)
  3704. {
  3705. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  3706. int err = 0;
  3707. mutex_lock(&swhash->hlist_mutex);
  3708. if (!swevent_hlist_deref(swhash) && cpu_online(cpu)) {
  3709. struct swevent_hlist *hlist;
  3710. hlist = kzalloc(sizeof(*hlist), GFP_KERNEL);
  3711. if (!hlist) {
  3712. err = -ENOMEM;
  3713. goto exit;
  3714. }
  3715. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  3716. }
  3717. swhash->hlist_refcount++;
  3718. exit:
  3719. mutex_unlock(&swhash->hlist_mutex);
  3720. return err;
  3721. }
  3722. static int swevent_hlist_get(struct perf_event *event)
  3723. {
  3724. int err;
  3725. int cpu, failed_cpu;
  3726. if (event->cpu != -1)
  3727. return swevent_hlist_get_cpu(event, event->cpu);
  3728. get_online_cpus();
  3729. for_each_possible_cpu(cpu) {
  3730. err = swevent_hlist_get_cpu(event, cpu);
  3731. if (err) {
  3732. failed_cpu = cpu;
  3733. goto fail;
  3734. }
  3735. }
  3736. put_online_cpus();
  3737. return 0;
  3738. fail:
  3739. for_each_possible_cpu(cpu) {
  3740. if (cpu == failed_cpu)
  3741. break;
  3742. swevent_hlist_put_cpu(event, cpu);
  3743. }
  3744. put_online_cpus();
  3745. return err;
  3746. }
  3747. atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  3748. static void sw_perf_event_destroy(struct perf_event *event)
  3749. {
  3750. u64 event_id = event->attr.config;
  3751. WARN_ON(event->parent);
  3752. jump_label_dec(&perf_swevent_enabled[event_id]);
  3753. swevent_hlist_put(event);
  3754. }
  3755. static int perf_swevent_init(struct perf_event *event)
  3756. {
  3757. int event_id = event->attr.config;
  3758. if (event->attr.type != PERF_TYPE_SOFTWARE)
  3759. return -ENOENT;
  3760. switch (event_id) {
  3761. case PERF_COUNT_SW_CPU_CLOCK:
  3762. case PERF_COUNT_SW_TASK_CLOCK:
  3763. return -ENOENT;
  3764. default:
  3765. break;
  3766. }
  3767. if (event_id > PERF_COUNT_SW_MAX)
  3768. return -ENOENT;
  3769. if (!event->parent) {
  3770. int err;
  3771. err = swevent_hlist_get(event);
  3772. if (err)
  3773. return err;
  3774. jump_label_inc(&perf_swevent_enabled[event_id]);
  3775. event->destroy = sw_perf_event_destroy;
  3776. }
  3777. return 0;
  3778. }
  3779. static struct pmu perf_swevent = {
  3780. .task_ctx_nr = perf_sw_context,
  3781. .event_init = perf_swevent_init,
  3782. .add = perf_swevent_add,
  3783. .del = perf_swevent_del,
  3784. .start = perf_swevent_start,
  3785. .stop = perf_swevent_stop,
  3786. .read = perf_swevent_read,
  3787. };
  3788. #ifdef CONFIG_EVENT_TRACING
  3789. static int perf_tp_filter_match(struct perf_event *event,
  3790. struct perf_sample_data *data)
  3791. {
  3792. void *record = data->raw->data;
  3793. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  3794. return 1;
  3795. return 0;
  3796. }
  3797. static int perf_tp_event_match(struct perf_event *event,
  3798. struct perf_sample_data *data,
  3799. struct pt_regs *regs)
  3800. {
  3801. /*
  3802. * All tracepoints are from kernel-space.
  3803. */
  3804. if (event->attr.exclude_kernel)
  3805. return 0;
  3806. if (!perf_tp_filter_match(event, data))
  3807. return 0;
  3808. return 1;
  3809. }
  3810. void perf_tp_event(u64 addr, u64 count, void *record, int entry_size,
  3811. struct pt_regs *regs, struct hlist_head *head, int rctx)
  3812. {
  3813. struct perf_sample_data data;
  3814. struct perf_event *event;
  3815. struct hlist_node *node;
  3816. struct perf_raw_record raw = {
  3817. .size = entry_size,
  3818. .data = record,
  3819. };
  3820. perf_sample_data_init(&data, addr);
  3821. data.raw = &raw;
  3822. hlist_for_each_entry_rcu(event, node, head, hlist_entry) {
  3823. if (perf_tp_event_match(event, &data, regs))
  3824. perf_swevent_event(event, count, 1, &data, regs);
  3825. }
  3826. perf_swevent_put_recursion_context(rctx);
  3827. }
  3828. EXPORT_SYMBOL_GPL(perf_tp_event);
  3829. static void tp_perf_event_destroy(struct perf_event *event)
  3830. {
  3831. perf_trace_destroy(event);
  3832. }
  3833. static int perf_tp_event_init(struct perf_event *event)
  3834. {
  3835. int err;
  3836. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  3837. return -ENOENT;
  3838. /*
  3839. * Raw tracepoint data is a severe data leak, only allow root to
  3840. * have these.
  3841. */
  3842. if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
  3843. perf_paranoid_tracepoint_raw() &&
  3844. !capable(CAP_SYS_ADMIN))
  3845. return -EPERM;
  3846. err = perf_trace_init(event);
  3847. if (err)
  3848. return err;
  3849. event->destroy = tp_perf_event_destroy;
  3850. return 0;
  3851. }
  3852. static struct pmu perf_tracepoint = {
  3853. .task_ctx_nr = perf_sw_context,
  3854. .event_init = perf_tp_event_init,
  3855. .add = perf_trace_add,
  3856. .del = perf_trace_del,
  3857. .start = perf_swevent_start,
  3858. .stop = perf_swevent_stop,
  3859. .read = perf_swevent_read,
  3860. };
  3861. static inline void perf_tp_register(void)
  3862. {
  3863. perf_pmu_register(&perf_tracepoint);
  3864. }
  3865. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3866. {
  3867. char *filter_str;
  3868. int ret;
  3869. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  3870. return -EINVAL;
  3871. filter_str = strndup_user(arg, PAGE_SIZE);
  3872. if (IS_ERR(filter_str))
  3873. return PTR_ERR(filter_str);
  3874. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  3875. kfree(filter_str);
  3876. return ret;
  3877. }
  3878. static void perf_event_free_filter(struct perf_event *event)
  3879. {
  3880. ftrace_profile_free_filter(event);
  3881. }
  3882. #else
  3883. static inline void perf_tp_register(void)
  3884. {
  3885. }
  3886. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3887. {
  3888. return -ENOENT;
  3889. }
  3890. static void perf_event_free_filter(struct perf_event *event)
  3891. {
  3892. }
  3893. #endif /* CONFIG_EVENT_TRACING */
  3894. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  3895. void perf_bp_event(struct perf_event *bp, void *data)
  3896. {
  3897. struct perf_sample_data sample;
  3898. struct pt_regs *regs = data;
  3899. perf_sample_data_init(&sample, bp->attr.bp_addr);
  3900. if (!bp->hw.state && !perf_exclude_event(bp, regs))
  3901. perf_swevent_event(bp, 1, 1, &sample, regs);
  3902. }
  3903. #endif
  3904. /*
  3905. * hrtimer based swevent callback
  3906. */
  3907. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  3908. {
  3909. enum hrtimer_restart ret = HRTIMER_RESTART;
  3910. struct perf_sample_data data;
  3911. struct pt_regs *regs;
  3912. struct perf_event *event;
  3913. u64 period;
  3914. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  3915. event->pmu->read(event);
  3916. perf_sample_data_init(&data, 0);
  3917. data.period = event->hw.last_period;
  3918. regs = get_irq_regs();
  3919. if (regs && !perf_exclude_event(event, regs)) {
  3920. if (!(event->attr.exclude_idle && current->pid == 0))
  3921. if (perf_event_overflow(event, 0, &data, regs))
  3922. ret = HRTIMER_NORESTART;
  3923. }
  3924. period = max_t(u64, 10000, event->hw.sample_period);
  3925. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  3926. return ret;
  3927. }
  3928. static void perf_swevent_start_hrtimer(struct perf_event *event)
  3929. {
  3930. struct hw_perf_event *hwc = &event->hw;
  3931. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3932. hwc->hrtimer.function = perf_swevent_hrtimer;
  3933. if (hwc->sample_period) {
  3934. s64 period = local64_read(&hwc->period_left);
  3935. if (period) {
  3936. if (period < 0)
  3937. period = 10000;
  3938. local64_set(&hwc->period_left, 0);
  3939. } else {
  3940. period = max_t(u64, 10000, hwc->sample_period);
  3941. }
  3942. __hrtimer_start_range_ns(&hwc->hrtimer,
  3943. ns_to_ktime(period), 0,
  3944. HRTIMER_MODE_REL_PINNED, 0);
  3945. }
  3946. }
  3947. static void perf_swevent_cancel_hrtimer(struct perf_event *event)
  3948. {
  3949. struct hw_perf_event *hwc = &event->hw;
  3950. if (hwc->sample_period) {
  3951. ktime_t remaining = hrtimer_get_remaining(&hwc->hrtimer);
  3952. local64_set(&hwc->period_left, ktime_to_ns(remaining));
  3953. hrtimer_cancel(&hwc->hrtimer);
  3954. }
  3955. }
  3956. /*
  3957. * Software event: cpu wall time clock
  3958. */
  3959. static void cpu_clock_event_update(struct perf_event *event)
  3960. {
  3961. s64 prev;
  3962. u64 now;
  3963. now = local_clock();
  3964. prev = local64_xchg(&event->hw.prev_count, now);
  3965. local64_add(now - prev, &event->count);
  3966. }
  3967. static void cpu_clock_event_start(struct perf_event *event, int flags)
  3968. {
  3969. local64_set(&event->hw.prev_count, local_clock());
  3970. perf_swevent_start_hrtimer(event);
  3971. }
  3972. static void cpu_clock_event_stop(struct perf_event *event, int flags)
  3973. {
  3974. perf_swevent_cancel_hrtimer(event);
  3975. cpu_clock_event_update(event);
  3976. }
  3977. static int cpu_clock_event_add(struct perf_event *event, int flags)
  3978. {
  3979. if (flags & PERF_EF_START)
  3980. cpu_clock_event_start(event, flags);
  3981. return 0;
  3982. }
  3983. static void cpu_clock_event_del(struct perf_event *event, int flags)
  3984. {
  3985. cpu_clock_event_stop(event, flags);
  3986. }
  3987. static void cpu_clock_event_read(struct perf_event *event)
  3988. {
  3989. cpu_clock_event_update(event);
  3990. }
  3991. static int cpu_clock_event_init(struct perf_event *event)
  3992. {
  3993. if (event->attr.type != PERF_TYPE_SOFTWARE)
  3994. return -ENOENT;
  3995. if (event->attr.config != PERF_COUNT_SW_CPU_CLOCK)
  3996. return -ENOENT;
  3997. return 0;
  3998. }
  3999. static struct pmu perf_cpu_clock = {
  4000. .task_ctx_nr = perf_sw_context,
  4001. .event_init = cpu_clock_event_init,
  4002. .add = cpu_clock_event_add,
  4003. .del = cpu_clock_event_del,
  4004. .start = cpu_clock_event_start,
  4005. .stop = cpu_clock_event_stop,
  4006. .read = cpu_clock_event_read,
  4007. };
  4008. /*
  4009. * Software event: task time clock
  4010. */
  4011. static void task_clock_event_update(struct perf_event *event, u64 now)
  4012. {
  4013. u64 prev;
  4014. s64 delta;
  4015. prev = local64_xchg(&event->hw.prev_count, now);
  4016. delta = now - prev;
  4017. local64_add(delta, &event->count);
  4018. }
  4019. static void task_clock_event_start(struct perf_event *event, int flags)
  4020. {
  4021. local64_set(&event->hw.prev_count, event->ctx->time);
  4022. perf_swevent_start_hrtimer(event);
  4023. }
  4024. static void task_clock_event_stop(struct perf_event *event, int flags)
  4025. {
  4026. perf_swevent_cancel_hrtimer(event);
  4027. task_clock_event_update(event, event->ctx->time);
  4028. }
  4029. static int task_clock_event_add(struct perf_event *event, int flags)
  4030. {
  4031. if (flags & PERF_EF_START)
  4032. task_clock_event_start(event, flags);
  4033. return 0;
  4034. }
  4035. static void task_clock_event_del(struct perf_event *event, int flags)
  4036. {
  4037. task_clock_event_stop(event, PERF_EF_UPDATE);
  4038. }
  4039. static void task_clock_event_read(struct perf_event *event)
  4040. {
  4041. u64 time;
  4042. if (!in_nmi()) {
  4043. update_context_time(event->ctx);
  4044. time = event->ctx->time;
  4045. } else {
  4046. u64 now = perf_clock();
  4047. u64 delta = now - event->ctx->timestamp;
  4048. time = event->ctx->time + delta;
  4049. }
  4050. task_clock_event_update(event, time);
  4051. }
  4052. static int task_clock_event_init(struct perf_event *event)
  4053. {
  4054. if (event->attr.type != PERF_TYPE_SOFTWARE)
  4055. return -ENOENT;
  4056. if (event->attr.config != PERF_COUNT_SW_TASK_CLOCK)
  4057. return -ENOENT;
  4058. return 0;
  4059. }
  4060. static struct pmu perf_task_clock = {
  4061. .task_ctx_nr = perf_sw_context,
  4062. .event_init = task_clock_event_init,
  4063. .add = task_clock_event_add,
  4064. .del = task_clock_event_del,
  4065. .start = task_clock_event_start,
  4066. .stop = task_clock_event_stop,
  4067. .read = task_clock_event_read,
  4068. };
  4069. static void perf_pmu_nop_void(struct pmu *pmu)
  4070. {
  4071. }
  4072. static int perf_pmu_nop_int(struct pmu *pmu)
  4073. {
  4074. return 0;
  4075. }
  4076. static void perf_pmu_start_txn(struct pmu *pmu)
  4077. {
  4078. perf_pmu_disable(pmu);
  4079. }
  4080. static int perf_pmu_commit_txn(struct pmu *pmu)
  4081. {
  4082. perf_pmu_enable(pmu);
  4083. return 0;
  4084. }
  4085. static void perf_pmu_cancel_txn(struct pmu *pmu)
  4086. {
  4087. perf_pmu_enable(pmu);
  4088. }
  4089. /*
  4090. * Ensures all contexts with the same task_ctx_nr have the same
  4091. * pmu_cpu_context too.
  4092. */
  4093. static void *find_pmu_context(int ctxn)
  4094. {
  4095. struct pmu *pmu;
  4096. if (ctxn < 0)
  4097. return NULL;
  4098. list_for_each_entry(pmu, &pmus, entry) {
  4099. if (pmu->task_ctx_nr == ctxn)
  4100. return pmu->pmu_cpu_context;
  4101. }
  4102. return NULL;
  4103. }
  4104. static void free_pmu_context(void * __percpu cpu_context)
  4105. {
  4106. struct pmu *pmu;
  4107. mutex_lock(&pmus_lock);
  4108. /*
  4109. * Like a real lame refcount.
  4110. */
  4111. list_for_each_entry(pmu, &pmus, entry) {
  4112. if (pmu->pmu_cpu_context == cpu_context)
  4113. goto out;
  4114. }
  4115. free_percpu(cpu_context);
  4116. out:
  4117. mutex_unlock(&pmus_lock);
  4118. }
  4119. int perf_pmu_register(struct pmu *pmu)
  4120. {
  4121. int cpu, ret;
  4122. mutex_lock(&pmus_lock);
  4123. ret = -ENOMEM;
  4124. pmu->pmu_disable_count = alloc_percpu(int);
  4125. if (!pmu->pmu_disable_count)
  4126. goto unlock;
  4127. pmu->pmu_cpu_context = find_pmu_context(pmu->task_ctx_nr);
  4128. if (pmu->pmu_cpu_context)
  4129. goto got_cpu_context;
  4130. pmu->pmu_cpu_context = alloc_percpu(struct perf_cpu_context);
  4131. if (!pmu->pmu_cpu_context)
  4132. goto free_pdc;
  4133. for_each_possible_cpu(cpu) {
  4134. struct perf_cpu_context *cpuctx;
  4135. cpuctx = per_cpu_ptr(pmu->pmu_cpu_context, cpu);
  4136. __perf_event_init_context(&cpuctx->ctx);
  4137. cpuctx->ctx.type = cpu_context;
  4138. cpuctx->ctx.pmu = pmu;
  4139. cpuctx->jiffies_interval = 1;
  4140. INIT_LIST_HEAD(&cpuctx->rotation_list);
  4141. }
  4142. got_cpu_context:
  4143. if (!pmu->start_txn) {
  4144. if (pmu->pmu_enable) {
  4145. /*
  4146. * If we have pmu_enable/pmu_disable calls, install
  4147. * transaction stubs that use that to try and batch
  4148. * hardware accesses.
  4149. */
  4150. pmu->start_txn = perf_pmu_start_txn;
  4151. pmu->commit_txn = perf_pmu_commit_txn;
  4152. pmu->cancel_txn = perf_pmu_cancel_txn;
  4153. } else {
  4154. pmu->start_txn = perf_pmu_nop_void;
  4155. pmu->commit_txn = perf_pmu_nop_int;
  4156. pmu->cancel_txn = perf_pmu_nop_void;
  4157. }
  4158. }
  4159. if (!pmu->pmu_enable) {
  4160. pmu->pmu_enable = perf_pmu_nop_void;
  4161. pmu->pmu_disable = perf_pmu_nop_void;
  4162. }
  4163. list_add_rcu(&pmu->entry, &pmus);
  4164. ret = 0;
  4165. unlock:
  4166. mutex_unlock(&pmus_lock);
  4167. return ret;
  4168. free_pdc:
  4169. free_percpu(pmu->pmu_disable_count);
  4170. goto unlock;
  4171. }
  4172. void perf_pmu_unregister(struct pmu *pmu)
  4173. {
  4174. mutex_lock(&pmus_lock);
  4175. list_del_rcu(&pmu->entry);
  4176. mutex_unlock(&pmus_lock);
  4177. /*
  4178. * We dereference the pmu list under both SRCU and regular RCU, so
  4179. * synchronize against both of those.
  4180. */
  4181. synchronize_srcu(&pmus_srcu);
  4182. synchronize_rcu();
  4183. free_percpu(pmu->pmu_disable_count);
  4184. free_pmu_context(pmu->pmu_cpu_context);
  4185. }
  4186. struct pmu *perf_init_event(struct perf_event *event)
  4187. {
  4188. struct pmu *pmu = NULL;
  4189. int idx;
  4190. idx = srcu_read_lock(&pmus_srcu);
  4191. list_for_each_entry_rcu(pmu, &pmus, entry) {
  4192. int ret = pmu->event_init(event);
  4193. if (!ret)
  4194. goto unlock;
  4195. if (ret != -ENOENT) {
  4196. pmu = ERR_PTR(ret);
  4197. goto unlock;
  4198. }
  4199. }
  4200. pmu = ERR_PTR(-ENOENT);
  4201. unlock:
  4202. srcu_read_unlock(&pmus_srcu, idx);
  4203. return pmu;
  4204. }
  4205. /*
  4206. * Allocate and initialize a event structure
  4207. */
  4208. static struct perf_event *
  4209. perf_event_alloc(struct perf_event_attr *attr, int cpu,
  4210. struct task_struct *task,
  4211. struct perf_event *group_leader,
  4212. struct perf_event *parent_event,
  4213. perf_overflow_handler_t overflow_handler)
  4214. {
  4215. struct pmu *pmu;
  4216. struct perf_event *event;
  4217. struct hw_perf_event *hwc;
  4218. long err;
  4219. event = kzalloc(sizeof(*event), GFP_KERNEL);
  4220. if (!event)
  4221. return ERR_PTR(-ENOMEM);
  4222. /*
  4223. * Single events are their own group leaders, with an
  4224. * empty sibling list:
  4225. */
  4226. if (!group_leader)
  4227. group_leader = event;
  4228. mutex_init(&event->child_mutex);
  4229. INIT_LIST_HEAD(&event->child_list);
  4230. INIT_LIST_HEAD(&event->group_entry);
  4231. INIT_LIST_HEAD(&event->event_entry);
  4232. INIT_LIST_HEAD(&event->sibling_list);
  4233. init_waitqueue_head(&event->waitq);
  4234. init_irq_work(&event->pending, perf_pending_event);
  4235. mutex_init(&event->mmap_mutex);
  4236. event->cpu = cpu;
  4237. event->attr = *attr;
  4238. event->group_leader = group_leader;
  4239. event->pmu = NULL;
  4240. event->oncpu = -1;
  4241. event->parent = parent_event;
  4242. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  4243. event->id = atomic64_inc_return(&perf_event_id);
  4244. event->state = PERF_EVENT_STATE_INACTIVE;
  4245. if (task) {
  4246. event->attach_state = PERF_ATTACH_TASK;
  4247. #ifdef CONFIG_HAVE_HW_BREAKPOINT
  4248. /*
  4249. * hw_breakpoint is a bit difficult here..
  4250. */
  4251. if (attr->type == PERF_TYPE_BREAKPOINT)
  4252. event->hw.bp_target = task;
  4253. #endif
  4254. }
  4255. if (!overflow_handler && parent_event)
  4256. overflow_handler = parent_event->overflow_handler;
  4257. event->overflow_handler = overflow_handler;
  4258. if (attr->disabled)
  4259. event->state = PERF_EVENT_STATE_OFF;
  4260. pmu = NULL;
  4261. hwc = &event->hw;
  4262. hwc->sample_period = attr->sample_period;
  4263. if (attr->freq && attr->sample_freq)
  4264. hwc->sample_period = 1;
  4265. hwc->last_period = hwc->sample_period;
  4266. local64_set(&hwc->period_left, hwc->sample_period);
  4267. /*
  4268. * we currently do not support PERF_FORMAT_GROUP on inherited events
  4269. */
  4270. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  4271. goto done;
  4272. pmu = perf_init_event(event);
  4273. done:
  4274. err = 0;
  4275. if (!pmu)
  4276. err = -EINVAL;
  4277. else if (IS_ERR(pmu))
  4278. err = PTR_ERR(pmu);
  4279. if (err) {
  4280. if (event->ns)
  4281. put_pid_ns(event->ns);
  4282. kfree(event);
  4283. return ERR_PTR(err);
  4284. }
  4285. event->pmu = pmu;
  4286. if (!event->parent) {
  4287. if (event->attach_state & PERF_ATTACH_TASK)
  4288. jump_label_inc(&perf_task_events);
  4289. if (event->attr.mmap || event->attr.mmap_data)
  4290. atomic_inc(&nr_mmap_events);
  4291. if (event->attr.comm)
  4292. atomic_inc(&nr_comm_events);
  4293. if (event->attr.task)
  4294. atomic_inc(&nr_task_events);
  4295. if (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN) {
  4296. err = get_callchain_buffers();
  4297. if (err) {
  4298. free_event(event);
  4299. return ERR_PTR(err);
  4300. }
  4301. }
  4302. }
  4303. return event;
  4304. }
  4305. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  4306. struct perf_event_attr *attr)
  4307. {
  4308. u32 size;
  4309. int ret;
  4310. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  4311. return -EFAULT;
  4312. /*
  4313. * zero the full structure, so that a short copy will be nice.
  4314. */
  4315. memset(attr, 0, sizeof(*attr));
  4316. ret = get_user(size, &uattr->size);
  4317. if (ret)
  4318. return ret;
  4319. if (size > PAGE_SIZE) /* silly large */
  4320. goto err_size;
  4321. if (!size) /* abi compat */
  4322. size = PERF_ATTR_SIZE_VER0;
  4323. if (size < PERF_ATTR_SIZE_VER0)
  4324. goto err_size;
  4325. /*
  4326. * If we're handed a bigger struct than we know of,
  4327. * ensure all the unknown bits are 0 - i.e. new
  4328. * user-space does not rely on any kernel feature
  4329. * extensions we dont know about yet.
  4330. */
  4331. if (size > sizeof(*attr)) {
  4332. unsigned char __user *addr;
  4333. unsigned char __user *end;
  4334. unsigned char val;
  4335. addr = (void __user *)uattr + sizeof(*attr);
  4336. end = (void __user *)uattr + size;
  4337. for (; addr < end; addr++) {
  4338. ret = get_user(val, addr);
  4339. if (ret)
  4340. return ret;
  4341. if (val)
  4342. goto err_size;
  4343. }
  4344. size = sizeof(*attr);
  4345. }
  4346. ret = copy_from_user(attr, uattr, size);
  4347. if (ret)
  4348. return -EFAULT;
  4349. /*
  4350. * If the type exists, the corresponding creation will verify
  4351. * the attr->config.
  4352. */
  4353. if (attr->type >= PERF_TYPE_MAX)
  4354. return -EINVAL;
  4355. if (attr->__reserved_1)
  4356. return -EINVAL;
  4357. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  4358. return -EINVAL;
  4359. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  4360. return -EINVAL;
  4361. out:
  4362. return ret;
  4363. err_size:
  4364. put_user(sizeof(*attr), &uattr->size);
  4365. ret = -E2BIG;
  4366. goto out;
  4367. }
  4368. static int
  4369. perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
  4370. {
  4371. struct perf_buffer *buffer = NULL, *old_buffer = NULL;
  4372. int ret = -EINVAL;
  4373. if (!output_event)
  4374. goto set;
  4375. /* don't allow circular references */
  4376. if (event == output_event)
  4377. goto out;
  4378. /*
  4379. * Don't allow cross-cpu buffers
  4380. */
  4381. if (output_event->cpu != event->cpu)
  4382. goto out;
  4383. /*
  4384. * If its not a per-cpu buffer, it must be the same task.
  4385. */
  4386. if (output_event->cpu == -1 && output_event->ctx != event->ctx)
  4387. goto out;
  4388. set:
  4389. mutex_lock(&event->mmap_mutex);
  4390. /* Can't redirect output if we've got an active mmap() */
  4391. if (atomic_read(&event->mmap_count))
  4392. goto unlock;
  4393. if (output_event) {
  4394. /* get the buffer we want to redirect to */
  4395. buffer = perf_buffer_get(output_event);
  4396. if (!buffer)
  4397. goto unlock;
  4398. }
  4399. old_buffer = event->buffer;
  4400. rcu_assign_pointer(event->buffer, buffer);
  4401. ret = 0;
  4402. unlock:
  4403. mutex_unlock(&event->mmap_mutex);
  4404. if (old_buffer)
  4405. perf_buffer_put(old_buffer);
  4406. out:
  4407. return ret;
  4408. }
  4409. /**
  4410. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  4411. *
  4412. * @attr_uptr: event_id type attributes for monitoring/sampling
  4413. * @pid: target pid
  4414. * @cpu: target cpu
  4415. * @group_fd: group leader event fd
  4416. */
  4417. SYSCALL_DEFINE5(perf_event_open,
  4418. struct perf_event_attr __user *, attr_uptr,
  4419. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  4420. {
  4421. struct perf_event *group_leader = NULL, *output_event = NULL;
  4422. struct perf_event *event, *sibling;
  4423. struct perf_event_attr attr;
  4424. struct perf_event_context *ctx;
  4425. struct file *event_file = NULL;
  4426. struct file *group_file = NULL;
  4427. struct task_struct *task = NULL;
  4428. struct pmu *pmu;
  4429. int event_fd;
  4430. int move_group = 0;
  4431. int fput_needed = 0;
  4432. int err;
  4433. /* for future expandability... */
  4434. if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
  4435. return -EINVAL;
  4436. err = perf_copy_attr(attr_uptr, &attr);
  4437. if (err)
  4438. return err;
  4439. if (!attr.exclude_kernel) {
  4440. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  4441. return -EACCES;
  4442. }
  4443. if (attr.freq) {
  4444. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  4445. return -EINVAL;
  4446. }
  4447. event_fd = get_unused_fd_flags(O_RDWR);
  4448. if (event_fd < 0)
  4449. return event_fd;
  4450. if (group_fd != -1) {
  4451. group_leader = perf_fget_light(group_fd, &fput_needed);
  4452. if (IS_ERR(group_leader)) {
  4453. err = PTR_ERR(group_leader);
  4454. goto err_fd;
  4455. }
  4456. group_file = group_leader->filp;
  4457. if (flags & PERF_FLAG_FD_OUTPUT)
  4458. output_event = group_leader;
  4459. if (flags & PERF_FLAG_FD_NO_GROUP)
  4460. group_leader = NULL;
  4461. }
  4462. if (pid != -1) {
  4463. task = find_lively_task_by_vpid(pid);
  4464. if (IS_ERR(task)) {
  4465. err = PTR_ERR(task);
  4466. goto err_group_fd;
  4467. }
  4468. }
  4469. event = perf_event_alloc(&attr, cpu, task, group_leader, NULL, NULL);
  4470. if (IS_ERR(event)) {
  4471. err = PTR_ERR(event);
  4472. goto err_task;
  4473. }
  4474. /*
  4475. * Special case software events and allow them to be part of
  4476. * any hardware group.
  4477. */
  4478. pmu = event->pmu;
  4479. if (group_leader &&
  4480. (is_software_event(event) != is_software_event(group_leader))) {
  4481. if (is_software_event(event)) {
  4482. /*
  4483. * If event and group_leader are not both a software
  4484. * event, and event is, then group leader is not.
  4485. *
  4486. * Allow the addition of software events to !software
  4487. * groups, this is safe because software events never
  4488. * fail to schedule.
  4489. */
  4490. pmu = group_leader->pmu;
  4491. } else if (is_software_event(group_leader) &&
  4492. (group_leader->group_flags & PERF_GROUP_SOFTWARE)) {
  4493. /*
  4494. * In case the group is a pure software group, and we
  4495. * try to add a hardware event, move the whole group to
  4496. * the hardware context.
  4497. */
  4498. move_group = 1;
  4499. }
  4500. }
  4501. /*
  4502. * Get the target context (task or percpu):
  4503. */
  4504. ctx = find_get_context(pmu, task, cpu);
  4505. if (IS_ERR(ctx)) {
  4506. err = PTR_ERR(ctx);
  4507. goto err_alloc;
  4508. }
  4509. /*
  4510. * Look up the group leader (we will attach this event to it):
  4511. */
  4512. if (group_leader) {
  4513. err = -EINVAL;
  4514. /*
  4515. * Do not allow a recursive hierarchy (this new sibling
  4516. * becoming part of another group-sibling):
  4517. */
  4518. if (group_leader->group_leader != group_leader)
  4519. goto err_context;
  4520. /*
  4521. * Do not allow to attach to a group in a different
  4522. * task or CPU context:
  4523. */
  4524. if (move_group) {
  4525. if (group_leader->ctx->type != ctx->type)
  4526. goto err_context;
  4527. } else {
  4528. if (group_leader->ctx != ctx)
  4529. goto err_context;
  4530. }
  4531. /*
  4532. * Only a group leader can be exclusive or pinned
  4533. */
  4534. if (attr.exclusive || attr.pinned)
  4535. goto err_context;
  4536. }
  4537. if (output_event) {
  4538. err = perf_event_set_output(event, output_event);
  4539. if (err)
  4540. goto err_context;
  4541. }
  4542. event_file = anon_inode_getfile("[perf_event]", &perf_fops, event, O_RDWR);
  4543. if (IS_ERR(event_file)) {
  4544. err = PTR_ERR(event_file);
  4545. goto err_context;
  4546. }
  4547. if (move_group) {
  4548. struct perf_event_context *gctx = group_leader->ctx;
  4549. mutex_lock(&gctx->mutex);
  4550. perf_event_remove_from_context(group_leader);
  4551. list_for_each_entry(sibling, &group_leader->sibling_list,
  4552. group_entry) {
  4553. perf_event_remove_from_context(sibling);
  4554. put_ctx(gctx);
  4555. }
  4556. mutex_unlock(&gctx->mutex);
  4557. put_ctx(gctx);
  4558. }
  4559. event->filp = event_file;
  4560. WARN_ON_ONCE(ctx->parent_ctx);
  4561. mutex_lock(&ctx->mutex);
  4562. if (move_group) {
  4563. perf_install_in_context(ctx, group_leader, cpu);
  4564. get_ctx(ctx);
  4565. list_for_each_entry(sibling, &group_leader->sibling_list,
  4566. group_entry) {
  4567. perf_install_in_context(ctx, sibling, cpu);
  4568. get_ctx(ctx);
  4569. }
  4570. }
  4571. perf_install_in_context(ctx, event, cpu);
  4572. ++ctx->generation;
  4573. mutex_unlock(&ctx->mutex);
  4574. event->owner = current;
  4575. get_task_struct(current);
  4576. mutex_lock(&current->perf_event_mutex);
  4577. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4578. mutex_unlock(&current->perf_event_mutex);
  4579. /*
  4580. * Drop the reference on the group_event after placing the
  4581. * new event on the sibling_list. This ensures destruction
  4582. * of the group leader will find the pointer to itself in
  4583. * perf_group_detach().
  4584. */
  4585. fput_light(group_file, fput_needed);
  4586. fd_install(event_fd, event_file);
  4587. return event_fd;
  4588. err_context:
  4589. put_ctx(ctx);
  4590. err_alloc:
  4591. free_event(event);
  4592. err_task:
  4593. if (task)
  4594. put_task_struct(task);
  4595. err_group_fd:
  4596. fput_light(group_file, fput_needed);
  4597. err_fd:
  4598. put_unused_fd(event_fd);
  4599. return err;
  4600. }
  4601. /**
  4602. * perf_event_create_kernel_counter
  4603. *
  4604. * @attr: attributes of the counter to create
  4605. * @cpu: cpu in which the counter is bound
  4606. * @task: task to profile (NULL for percpu)
  4607. */
  4608. struct perf_event *
  4609. perf_event_create_kernel_counter(struct perf_event_attr *attr, int cpu,
  4610. struct task_struct *task,
  4611. perf_overflow_handler_t overflow_handler)
  4612. {
  4613. struct perf_event_context *ctx;
  4614. struct perf_event *event;
  4615. int err;
  4616. /*
  4617. * Get the target context (task or percpu):
  4618. */
  4619. event = perf_event_alloc(attr, cpu, task, NULL, NULL, overflow_handler);
  4620. if (IS_ERR(event)) {
  4621. err = PTR_ERR(event);
  4622. goto err;
  4623. }
  4624. ctx = find_get_context(event->pmu, task, cpu);
  4625. if (IS_ERR(ctx)) {
  4626. err = PTR_ERR(ctx);
  4627. goto err_free;
  4628. }
  4629. event->filp = NULL;
  4630. WARN_ON_ONCE(ctx->parent_ctx);
  4631. mutex_lock(&ctx->mutex);
  4632. perf_install_in_context(ctx, event, cpu);
  4633. ++ctx->generation;
  4634. mutex_unlock(&ctx->mutex);
  4635. event->owner = current;
  4636. get_task_struct(current);
  4637. mutex_lock(&current->perf_event_mutex);
  4638. list_add_tail(&event->owner_entry, &current->perf_event_list);
  4639. mutex_unlock(&current->perf_event_mutex);
  4640. return event;
  4641. err_free:
  4642. free_event(event);
  4643. err:
  4644. return ERR_PTR(err);
  4645. }
  4646. EXPORT_SYMBOL_GPL(perf_event_create_kernel_counter);
  4647. static void sync_child_event(struct perf_event *child_event,
  4648. struct task_struct *child)
  4649. {
  4650. struct perf_event *parent_event = child_event->parent;
  4651. u64 child_val;
  4652. if (child_event->attr.inherit_stat)
  4653. perf_event_read_event(child_event, child);
  4654. child_val = perf_event_count(child_event);
  4655. /*
  4656. * Add back the child's count to the parent's count:
  4657. */
  4658. atomic64_add(child_val, &parent_event->child_count);
  4659. atomic64_add(child_event->total_time_enabled,
  4660. &parent_event->child_total_time_enabled);
  4661. atomic64_add(child_event->total_time_running,
  4662. &parent_event->child_total_time_running);
  4663. /*
  4664. * Remove this event from the parent's list
  4665. */
  4666. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4667. mutex_lock(&parent_event->child_mutex);
  4668. list_del_init(&child_event->child_list);
  4669. mutex_unlock(&parent_event->child_mutex);
  4670. /*
  4671. * Release the parent event, if this was the last
  4672. * reference to it.
  4673. */
  4674. fput(parent_event->filp);
  4675. }
  4676. static void
  4677. __perf_event_exit_task(struct perf_event *child_event,
  4678. struct perf_event_context *child_ctx,
  4679. struct task_struct *child)
  4680. {
  4681. struct perf_event *parent_event;
  4682. perf_event_remove_from_context(child_event);
  4683. parent_event = child_event->parent;
  4684. /*
  4685. * It can happen that parent exits first, and has events
  4686. * that are still around due to the child reference. These
  4687. * events need to be zapped - but otherwise linger.
  4688. */
  4689. if (parent_event) {
  4690. sync_child_event(child_event, child);
  4691. free_event(child_event);
  4692. }
  4693. }
  4694. static void perf_event_exit_task_context(struct task_struct *child, int ctxn)
  4695. {
  4696. struct perf_event *child_event, *tmp;
  4697. struct perf_event_context *child_ctx;
  4698. unsigned long flags;
  4699. if (likely(!child->perf_event_ctxp[ctxn])) {
  4700. perf_event_task(child, NULL, 0);
  4701. return;
  4702. }
  4703. local_irq_save(flags);
  4704. /*
  4705. * We can't reschedule here because interrupts are disabled,
  4706. * and either child is current or it is a task that can't be
  4707. * scheduled, so we are now safe from rescheduling changing
  4708. * our context.
  4709. */
  4710. child_ctx = child->perf_event_ctxp[ctxn];
  4711. task_ctx_sched_out(child_ctx, EVENT_ALL);
  4712. /*
  4713. * Take the context lock here so that if find_get_context is
  4714. * reading child->perf_event_ctxp, we wait until it has
  4715. * incremented the context's refcount before we do put_ctx below.
  4716. */
  4717. raw_spin_lock(&child_ctx->lock);
  4718. child->perf_event_ctxp[ctxn] = NULL;
  4719. /*
  4720. * If this context is a clone; unclone it so it can't get
  4721. * swapped to another process while we're removing all
  4722. * the events from it.
  4723. */
  4724. unclone_ctx(child_ctx);
  4725. update_context_time(child_ctx);
  4726. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  4727. /*
  4728. * Report the task dead after unscheduling the events so that we
  4729. * won't get any samples after PERF_RECORD_EXIT. We can however still
  4730. * get a few PERF_RECORD_READ events.
  4731. */
  4732. perf_event_task(child, child_ctx, 0);
  4733. /*
  4734. * We can recurse on the same lock type through:
  4735. *
  4736. * __perf_event_exit_task()
  4737. * sync_child_event()
  4738. * fput(parent_event->filp)
  4739. * perf_release()
  4740. * mutex_lock(&ctx->mutex)
  4741. *
  4742. * But since its the parent context it won't be the same instance.
  4743. */
  4744. mutex_lock(&child_ctx->mutex);
  4745. again:
  4746. list_for_each_entry_safe(child_event, tmp, &child_ctx->pinned_groups,
  4747. group_entry)
  4748. __perf_event_exit_task(child_event, child_ctx, child);
  4749. list_for_each_entry_safe(child_event, tmp, &child_ctx->flexible_groups,
  4750. group_entry)
  4751. __perf_event_exit_task(child_event, child_ctx, child);
  4752. /*
  4753. * If the last event was a group event, it will have appended all
  4754. * its siblings to the list, but we obtained 'tmp' before that which
  4755. * will still point to the list head terminating the iteration.
  4756. */
  4757. if (!list_empty(&child_ctx->pinned_groups) ||
  4758. !list_empty(&child_ctx->flexible_groups))
  4759. goto again;
  4760. mutex_unlock(&child_ctx->mutex);
  4761. put_ctx(child_ctx);
  4762. }
  4763. /*
  4764. * When a child task exits, feed back event values to parent events.
  4765. */
  4766. void perf_event_exit_task(struct task_struct *child)
  4767. {
  4768. int ctxn;
  4769. for_each_task_context_nr(ctxn)
  4770. perf_event_exit_task_context(child, ctxn);
  4771. }
  4772. static void perf_free_event(struct perf_event *event,
  4773. struct perf_event_context *ctx)
  4774. {
  4775. struct perf_event *parent = event->parent;
  4776. if (WARN_ON_ONCE(!parent))
  4777. return;
  4778. mutex_lock(&parent->child_mutex);
  4779. list_del_init(&event->child_list);
  4780. mutex_unlock(&parent->child_mutex);
  4781. fput(parent->filp);
  4782. perf_group_detach(event);
  4783. list_del_event(event, ctx);
  4784. free_event(event);
  4785. }
  4786. /*
  4787. * free an unexposed, unused context as created by inheritance by
  4788. * perf_event_init_task below, used by fork() in case of fail.
  4789. */
  4790. void perf_event_free_task(struct task_struct *task)
  4791. {
  4792. struct perf_event_context *ctx;
  4793. struct perf_event *event, *tmp;
  4794. int ctxn;
  4795. for_each_task_context_nr(ctxn) {
  4796. ctx = task->perf_event_ctxp[ctxn];
  4797. if (!ctx)
  4798. continue;
  4799. mutex_lock(&ctx->mutex);
  4800. again:
  4801. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups,
  4802. group_entry)
  4803. perf_free_event(event, ctx);
  4804. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups,
  4805. group_entry)
  4806. perf_free_event(event, ctx);
  4807. if (!list_empty(&ctx->pinned_groups) ||
  4808. !list_empty(&ctx->flexible_groups))
  4809. goto again;
  4810. mutex_unlock(&ctx->mutex);
  4811. put_ctx(ctx);
  4812. }
  4813. }
  4814. void perf_event_delayed_put(struct task_struct *task)
  4815. {
  4816. int ctxn;
  4817. for_each_task_context_nr(ctxn)
  4818. WARN_ON_ONCE(task->perf_event_ctxp[ctxn]);
  4819. }
  4820. /*
  4821. * inherit a event from parent task to child task:
  4822. */
  4823. static struct perf_event *
  4824. inherit_event(struct perf_event *parent_event,
  4825. struct task_struct *parent,
  4826. struct perf_event_context *parent_ctx,
  4827. struct task_struct *child,
  4828. struct perf_event *group_leader,
  4829. struct perf_event_context *child_ctx)
  4830. {
  4831. struct perf_event *child_event;
  4832. unsigned long flags;
  4833. /*
  4834. * Instead of creating recursive hierarchies of events,
  4835. * we link inherited events back to the original parent,
  4836. * which has a filp for sure, which we use as the reference
  4837. * count:
  4838. */
  4839. if (parent_event->parent)
  4840. parent_event = parent_event->parent;
  4841. child_event = perf_event_alloc(&parent_event->attr,
  4842. parent_event->cpu,
  4843. child,
  4844. group_leader, parent_event,
  4845. NULL);
  4846. if (IS_ERR(child_event))
  4847. return child_event;
  4848. get_ctx(child_ctx);
  4849. /*
  4850. * Make the child state follow the state of the parent event,
  4851. * not its attr.disabled bit. We hold the parent's mutex,
  4852. * so we won't race with perf_event_{en, dis}able_family.
  4853. */
  4854. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  4855. child_event->state = PERF_EVENT_STATE_INACTIVE;
  4856. else
  4857. child_event->state = PERF_EVENT_STATE_OFF;
  4858. if (parent_event->attr.freq) {
  4859. u64 sample_period = parent_event->hw.sample_period;
  4860. struct hw_perf_event *hwc = &child_event->hw;
  4861. hwc->sample_period = sample_period;
  4862. hwc->last_period = sample_period;
  4863. local64_set(&hwc->period_left, sample_period);
  4864. }
  4865. child_event->ctx = child_ctx;
  4866. child_event->overflow_handler = parent_event->overflow_handler;
  4867. /*
  4868. * Link it up in the child's context:
  4869. */
  4870. raw_spin_lock_irqsave(&child_ctx->lock, flags);
  4871. add_event_to_ctx(child_event, child_ctx);
  4872. raw_spin_unlock_irqrestore(&child_ctx->lock, flags);
  4873. /*
  4874. * Get a reference to the parent filp - we will fput it
  4875. * when the child event exits. This is safe to do because
  4876. * we are in the parent and we know that the filp still
  4877. * exists and has a nonzero count:
  4878. */
  4879. atomic_long_inc(&parent_event->filp->f_count);
  4880. /*
  4881. * Link this into the parent event's child list
  4882. */
  4883. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  4884. mutex_lock(&parent_event->child_mutex);
  4885. list_add_tail(&child_event->child_list, &parent_event->child_list);
  4886. mutex_unlock(&parent_event->child_mutex);
  4887. return child_event;
  4888. }
  4889. static int inherit_group(struct perf_event *parent_event,
  4890. struct task_struct *parent,
  4891. struct perf_event_context *parent_ctx,
  4892. struct task_struct *child,
  4893. struct perf_event_context *child_ctx)
  4894. {
  4895. struct perf_event *leader;
  4896. struct perf_event *sub;
  4897. struct perf_event *child_ctr;
  4898. leader = inherit_event(parent_event, parent, parent_ctx,
  4899. child, NULL, child_ctx);
  4900. if (IS_ERR(leader))
  4901. return PTR_ERR(leader);
  4902. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  4903. child_ctr = inherit_event(sub, parent, parent_ctx,
  4904. child, leader, child_ctx);
  4905. if (IS_ERR(child_ctr))
  4906. return PTR_ERR(child_ctr);
  4907. }
  4908. return 0;
  4909. }
  4910. static int
  4911. inherit_task_group(struct perf_event *event, struct task_struct *parent,
  4912. struct perf_event_context *parent_ctx,
  4913. struct task_struct *child, int ctxn,
  4914. int *inherited_all)
  4915. {
  4916. int ret;
  4917. struct perf_event_context *child_ctx;
  4918. if (!event->attr.inherit) {
  4919. *inherited_all = 0;
  4920. return 0;
  4921. }
  4922. child_ctx = child->perf_event_ctxp[ctxn];
  4923. if (!child_ctx) {
  4924. /*
  4925. * This is executed from the parent task context, so
  4926. * inherit events that have been marked for cloning.
  4927. * First allocate and initialize a context for the
  4928. * child.
  4929. */
  4930. child_ctx = alloc_perf_context(event->pmu, child);
  4931. if (!child_ctx)
  4932. return -ENOMEM;
  4933. child->perf_event_ctxp[ctxn] = child_ctx;
  4934. }
  4935. ret = inherit_group(event, parent, parent_ctx,
  4936. child, child_ctx);
  4937. if (ret)
  4938. *inherited_all = 0;
  4939. return ret;
  4940. }
  4941. /*
  4942. * Initialize the perf_event context in task_struct
  4943. */
  4944. int perf_event_init_context(struct task_struct *child, int ctxn)
  4945. {
  4946. struct perf_event_context *child_ctx, *parent_ctx;
  4947. struct perf_event_context *cloned_ctx;
  4948. struct perf_event *event;
  4949. struct task_struct *parent = current;
  4950. int inherited_all = 1;
  4951. int ret = 0;
  4952. child->perf_event_ctxp[ctxn] = NULL;
  4953. mutex_init(&child->perf_event_mutex);
  4954. INIT_LIST_HEAD(&child->perf_event_list);
  4955. if (likely(!parent->perf_event_ctxp[ctxn]))
  4956. return 0;
  4957. /*
  4958. * If the parent's context is a clone, pin it so it won't get
  4959. * swapped under us.
  4960. */
  4961. parent_ctx = perf_pin_task_context(parent, ctxn);
  4962. /*
  4963. * No need to check if parent_ctx != NULL here; since we saw
  4964. * it non-NULL earlier, the only reason for it to become NULL
  4965. * is if we exit, and since we're currently in the middle of
  4966. * a fork we can't be exiting at the same time.
  4967. */
  4968. /*
  4969. * Lock the parent list. No need to lock the child - not PID
  4970. * hashed yet and not running, so nobody can access it.
  4971. */
  4972. mutex_lock(&parent_ctx->mutex);
  4973. /*
  4974. * We dont have to disable NMIs - we are only looking at
  4975. * the list, not manipulating it:
  4976. */
  4977. list_for_each_entry(event, &parent_ctx->pinned_groups, group_entry) {
  4978. ret = inherit_task_group(event, parent, parent_ctx,
  4979. child, ctxn, &inherited_all);
  4980. if (ret)
  4981. break;
  4982. }
  4983. list_for_each_entry(event, &parent_ctx->flexible_groups, group_entry) {
  4984. ret = inherit_task_group(event, parent, parent_ctx,
  4985. child, ctxn, &inherited_all);
  4986. if (ret)
  4987. break;
  4988. }
  4989. child_ctx = child->perf_event_ctxp[ctxn];
  4990. if (child_ctx && inherited_all) {
  4991. /*
  4992. * Mark the child context as a clone of the parent
  4993. * context, or of whatever the parent is a clone of.
  4994. * Note that if the parent is a clone, it could get
  4995. * uncloned at any point, but that doesn't matter
  4996. * because the list of events and the generation
  4997. * count can't have changed since we took the mutex.
  4998. */
  4999. cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
  5000. if (cloned_ctx) {
  5001. child_ctx->parent_ctx = cloned_ctx;
  5002. child_ctx->parent_gen = parent_ctx->parent_gen;
  5003. } else {
  5004. child_ctx->parent_ctx = parent_ctx;
  5005. child_ctx->parent_gen = parent_ctx->generation;
  5006. }
  5007. get_ctx(child_ctx->parent_ctx);
  5008. }
  5009. mutex_unlock(&parent_ctx->mutex);
  5010. perf_unpin_context(parent_ctx);
  5011. return ret;
  5012. }
  5013. /*
  5014. * Initialize the perf_event context in task_struct
  5015. */
  5016. int perf_event_init_task(struct task_struct *child)
  5017. {
  5018. int ctxn, ret;
  5019. for_each_task_context_nr(ctxn) {
  5020. ret = perf_event_init_context(child, ctxn);
  5021. if (ret)
  5022. return ret;
  5023. }
  5024. return 0;
  5025. }
  5026. static void __init perf_event_init_all_cpus(void)
  5027. {
  5028. struct swevent_htable *swhash;
  5029. int cpu;
  5030. for_each_possible_cpu(cpu) {
  5031. swhash = &per_cpu(swevent_htable, cpu);
  5032. mutex_init(&swhash->hlist_mutex);
  5033. INIT_LIST_HEAD(&per_cpu(rotation_list, cpu));
  5034. }
  5035. }
  5036. static void __cpuinit perf_event_init_cpu(int cpu)
  5037. {
  5038. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5039. mutex_lock(&swhash->hlist_mutex);
  5040. if (swhash->hlist_refcount > 0) {
  5041. struct swevent_hlist *hlist;
  5042. hlist = kzalloc_node(sizeof(*hlist), GFP_KERNEL, cpu_to_node(cpu));
  5043. WARN_ON(!hlist);
  5044. rcu_assign_pointer(swhash->swevent_hlist, hlist);
  5045. }
  5046. mutex_unlock(&swhash->hlist_mutex);
  5047. }
  5048. #ifdef CONFIG_HOTPLUG_CPU
  5049. static void perf_pmu_rotate_stop(struct pmu *pmu)
  5050. {
  5051. struct perf_cpu_context *cpuctx = this_cpu_ptr(pmu->pmu_cpu_context);
  5052. WARN_ON(!irqs_disabled());
  5053. list_del_init(&cpuctx->rotation_list);
  5054. }
  5055. static void __perf_event_exit_context(void *__info)
  5056. {
  5057. struct perf_event_context *ctx = __info;
  5058. struct perf_event *event, *tmp;
  5059. perf_pmu_rotate_stop(ctx->pmu);
  5060. list_for_each_entry_safe(event, tmp, &ctx->pinned_groups, group_entry)
  5061. __perf_event_remove_from_context(event);
  5062. list_for_each_entry_safe(event, tmp, &ctx->flexible_groups, group_entry)
  5063. __perf_event_remove_from_context(event);
  5064. }
  5065. static void perf_event_exit_cpu_context(int cpu)
  5066. {
  5067. struct perf_event_context *ctx;
  5068. struct pmu *pmu;
  5069. int idx;
  5070. idx = srcu_read_lock(&pmus_srcu);
  5071. list_for_each_entry_rcu(pmu, &pmus, entry) {
  5072. ctx = &per_cpu_ptr(pmu->pmu_cpu_context, cpu)->ctx;
  5073. mutex_lock(&ctx->mutex);
  5074. smp_call_function_single(cpu, __perf_event_exit_context, ctx, 1);
  5075. mutex_unlock(&ctx->mutex);
  5076. }
  5077. srcu_read_unlock(&pmus_srcu, idx);
  5078. }
  5079. static void perf_event_exit_cpu(int cpu)
  5080. {
  5081. struct swevent_htable *swhash = &per_cpu(swevent_htable, cpu);
  5082. mutex_lock(&swhash->hlist_mutex);
  5083. swevent_hlist_release(swhash);
  5084. mutex_unlock(&swhash->hlist_mutex);
  5085. perf_event_exit_cpu_context(cpu);
  5086. }
  5087. #else
  5088. static inline void perf_event_exit_cpu(int cpu) { }
  5089. #endif
  5090. static int __cpuinit
  5091. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  5092. {
  5093. unsigned int cpu = (long)hcpu;
  5094. switch (action & ~CPU_TASKS_FROZEN) {
  5095. case CPU_UP_PREPARE:
  5096. case CPU_DOWN_FAILED:
  5097. perf_event_init_cpu(cpu);
  5098. break;
  5099. case CPU_UP_CANCELED:
  5100. case CPU_DOWN_PREPARE:
  5101. perf_event_exit_cpu(cpu);
  5102. break;
  5103. default:
  5104. break;
  5105. }
  5106. return NOTIFY_OK;
  5107. }
  5108. void __init perf_event_init(void)
  5109. {
  5110. int ret;
  5111. perf_event_init_all_cpus();
  5112. init_srcu_struct(&pmus_srcu);
  5113. perf_pmu_register(&perf_swevent);
  5114. perf_pmu_register(&perf_cpu_clock);
  5115. perf_pmu_register(&perf_task_clock);
  5116. perf_tp_register();
  5117. perf_cpu_notifier(perf_cpu_notify);
  5118. ret = init_hw_breakpoint();
  5119. WARN(ret, "hw_breakpoint initialization failed with: %d", ret);
  5120. }