perf_event.c 121 KB

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