perf_event.c 121 KB

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