perf_event.c 138 KB

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