perf_event.c 117 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174
  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 <asm/irq_regs.h>
  32. /*
  33. * Each CPU has a list of per CPU events:
  34. */
  35. DEFINE_PER_CPU(struct perf_cpu_context, perf_cpu_context);
  36. int perf_max_events __read_mostly = 1;
  37. static int perf_reserved_percpu __read_mostly;
  38. static int perf_overcommit __read_mostly = 1;
  39. static atomic_t nr_events __read_mostly;
  40. static atomic_t nr_mmap_events __read_mostly;
  41. static atomic_t nr_comm_events __read_mostly;
  42. static atomic_t nr_task_events __read_mostly;
  43. /*
  44. * perf event paranoia level:
  45. * -1 - not paranoid at all
  46. * 0 - disallow raw tracepoint access for unpriv
  47. * 1 - disallow cpu events for unpriv
  48. * 2 - disallow kernel profiling for unpriv
  49. */
  50. int sysctl_perf_event_paranoid __read_mostly = 1;
  51. static inline bool perf_paranoid_tracepoint_raw(void)
  52. {
  53. return sysctl_perf_event_paranoid > -1;
  54. }
  55. static inline bool perf_paranoid_cpu(void)
  56. {
  57. return sysctl_perf_event_paranoid > 0;
  58. }
  59. static inline bool perf_paranoid_kernel(void)
  60. {
  61. return sysctl_perf_event_paranoid > 1;
  62. }
  63. int sysctl_perf_event_mlock __read_mostly = 512; /* 'free' kb per user */
  64. /*
  65. * max perf event sample rate
  66. */
  67. int sysctl_perf_event_sample_rate __read_mostly = 100000;
  68. static atomic64_t perf_event_id;
  69. /*
  70. * Lock for (sysadmin-configurable) event reservations:
  71. */
  72. static DEFINE_SPINLOCK(perf_resource_lock);
  73. /*
  74. * Architecture provided APIs - weak aliases:
  75. */
  76. extern __weak const struct pmu *hw_perf_event_init(struct perf_event *event)
  77. {
  78. return NULL;
  79. }
  80. void __weak hw_perf_disable(void) { barrier(); }
  81. void __weak hw_perf_enable(void) { barrier(); }
  82. void __weak hw_perf_event_setup(int cpu) { barrier(); }
  83. void __weak hw_perf_event_setup_online(int cpu) { barrier(); }
  84. int __weak
  85. hw_perf_group_sched_in(struct perf_event *group_leader,
  86. struct perf_cpu_context *cpuctx,
  87. struct perf_event_context *ctx, int cpu)
  88. {
  89. return 0;
  90. }
  91. void __weak perf_event_print_debug(void) { }
  92. static DEFINE_PER_CPU(int, perf_disable_count);
  93. void __perf_disable(void)
  94. {
  95. __get_cpu_var(perf_disable_count)++;
  96. }
  97. bool __perf_enable(void)
  98. {
  99. return !--__get_cpu_var(perf_disable_count);
  100. }
  101. void perf_disable(void)
  102. {
  103. __perf_disable();
  104. hw_perf_disable();
  105. }
  106. void perf_enable(void)
  107. {
  108. if (__perf_enable())
  109. hw_perf_enable();
  110. }
  111. static void get_ctx(struct perf_event_context *ctx)
  112. {
  113. WARN_ON(!atomic_inc_not_zero(&ctx->refcount));
  114. }
  115. static void free_ctx(struct rcu_head *head)
  116. {
  117. struct perf_event_context *ctx;
  118. ctx = container_of(head, struct perf_event_context, rcu_head);
  119. kfree(ctx);
  120. }
  121. static void put_ctx(struct perf_event_context *ctx)
  122. {
  123. if (atomic_dec_and_test(&ctx->refcount)) {
  124. if (ctx->parent_ctx)
  125. put_ctx(ctx->parent_ctx);
  126. if (ctx->task)
  127. put_task_struct(ctx->task);
  128. call_rcu(&ctx->rcu_head, free_ctx);
  129. }
  130. }
  131. static void unclone_ctx(struct perf_event_context *ctx)
  132. {
  133. if (ctx->parent_ctx) {
  134. put_ctx(ctx->parent_ctx);
  135. ctx->parent_ctx = NULL;
  136. }
  137. }
  138. /*
  139. * If we inherit events we want to return the parent event id
  140. * to userspace.
  141. */
  142. static u64 primary_event_id(struct perf_event *event)
  143. {
  144. u64 id = event->id;
  145. if (event->parent)
  146. id = event->parent->id;
  147. return id;
  148. }
  149. /*
  150. * Get the perf_event_context for a task and lock it.
  151. * This has to cope with with the fact that until it is locked,
  152. * the context could get moved to another task.
  153. */
  154. static struct perf_event_context *
  155. perf_lock_task_context(struct task_struct *task, unsigned long *flags)
  156. {
  157. struct perf_event_context *ctx;
  158. rcu_read_lock();
  159. retry:
  160. ctx = rcu_dereference(task->perf_event_ctxp);
  161. if (ctx) {
  162. /*
  163. * If this context is a clone of another, it might
  164. * get swapped for another underneath us by
  165. * perf_event_task_sched_out, though the
  166. * rcu_read_lock() protects us from any context
  167. * getting freed. Lock the context and check if it
  168. * got swapped before we could get the lock, and retry
  169. * if so. If we locked the right context, then it
  170. * can't get swapped on us any more.
  171. */
  172. spin_lock_irqsave(&ctx->lock, *flags);
  173. if (ctx != rcu_dereference(task->perf_event_ctxp)) {
  174. spin_unlock_irqrestore(&ctx->lock, *flags);
  175. goto retry;
  176. }
  177. if (!atomic_inc_not_zero(&ctx->refcount)) {
  178. spin_unlock_irqrestore(&ctx->lock, *flags);
  179. ctx = NULL;
  180. }
  181. }
  182. rcu_read_unlock();
  183. return ctx;
  184. }
  185. /*
  186. * Get the context for a task and increment its pin_count so it
  187. * can't get swapped to another task. This also increments its
  188. * reference count so that the context can't get freed.
  189. */
  190. static struct perf_event_context *perf_pin_task_context(struct task_struct *task)
  191. {
  192. struct perf_event_context *ctx;
  193. unsigned long flags;
  194. ctx = perf_lock_task_context(task, &flags);
  195. if (ctx) {
  196. ++ctx->pin_count;
  197. spin_unlock_irqrestore(&ctx->lock, flags);
  198. }
  199. return ctx;
  200. }
  201. static void perf_unpin_context(struct perf_event_context *ctx)
  202. {
  203. unsigned long flags;
  204. spin_lock_irqsave(&ctx->lock, flags);
  205. --ctx->pin_count;
  206. spin_unlock_irqrestore(&ctx->lock, flags);
  207. put_ctx(ctx);
  208. }
  209. /*
  210. * Add a event from the lists for its context.
  211. * Must be called with ctx->mutex and ctx->lock held.
  212. */
  213. static void
  214. list_add_event(struct perf_event *event, struct perf_event_context *ctx)
  215. {
  216. struct perf_event *group_leader = event->group_leader;
  217. /*
  218. * Depending on whether it is a standalone or sibling event,
  219. * add it straight to the context's event list, or to the group
  220. * leader's sibling list:
  221. */
  222. if (group_leader == event)
  223. list_add_tail(&event->group_entry, &ctx->group_list);
  224. else {
  225. list_add_tail(&event->group_entry, &group_leader->sibling_list);
  226. group_leader->nr_siblings++;
  227. }
  228. list_add_rcu(&event->event_entry, &ctx->event_list);
  229. ctx->nr_events++;
  230. if (event->attr.inherit_stat)
  231. ctx->nr_stat++;
  232. }
  233. /*
  234. * Remove a event from the lists for its context.
  235. * Must be called with ctx->mutex and ctx->lock held.
  236. */
  237. static void
  238. list_del_event(struct perf_event *event, struct perf_event_context *ctx)
  239. {
  240. struct perf_event *sibling, *tmp;
  241. if (list_empty(&event->group_entry))
  242. return;
  243. ctx->nr_events--;
  244. if (event->attr.inherit_stat)
  245. ctx->nr_stat--;
  246. list_del_init(&event->group_entry);
  247. list_del_rcu(&event->event_entry);
  248. if (event->group_leader != event)
  249. event->group_leader->nr_siblings--;
  250. /*
  251. * If this was a group event with sibling events then
  252. * upgrade the siblings to singleton events by adding them
  253. * to the context list directly:
  254. */
  255. list_for_each_entry_safe(sibling, tmp, &event->sibling_list, group_entry) {
  256. list_move_tail(&sibling->group_entry, &ctx->group_list);
  257. sibling->group_leader = sibling;
  258. }
  259. }
  260. static void
  261. event_sched_out(struct perf_event *event,
  262. struct perf_cpu_context *cpuctx,
  263. struct perf_event_context *ctx)
  264. {
  265. if (event->state != PERF_EVENT_STATE_ACTIVE)
  266. return;
  267. event->state = PERF_EVENT_STATE_INACTIVE;
  268. if (event->pending_disable) {
  269. event->pending_disable = 0;
  270. event->state = PERF_EVENT_STATE_OFF;
  271. }
  272. event->tstamp_stopped = ctx->time;
  273. event->pmu->disable(event);
  274. event->oncpu = -1;
  275. if (!is_software_event(event))
  276. cpuctx->active_oncpu--;
  277. ctx->nr_active--;
  278. if (event->attr.exclusive || !cpuctx->active_oncpu)
  279. cpuctx->exclusive = 0;
  280. }
  281. static void
  282. group_sched_out(struct perf_event *group_event,
  283. struct perf_cpu_context *cpuctx,
  284. struct perf_event_context *ctx)
  285. {
  286. struct perf_event *event;
  287. if (group_event->state != PERF_EVENT_STATE_ACTIVE)
  288. return;
  289. event_sched_out(group_event, cpuctx, ctx);
  290. /*
  291. * Schedule out siblings (if any):
  292. */
  293. list_for_each_entry(event, &group_event->sibling_list, group_entry)
  294. event_sched_out(event, cpuctx, ctx);
  295. if (group_event->attr.exclusive)
  296. cpuctx->exclusive = 0;
  297. }
  298. /*
  299. * Cross CPU call to remove a performance event
  300. *
  301. * We disable the event on the hardware level first. After that we
  302. * remove it from the context list.
  303. */
  304. static void __perf_event_remove_from_context(void *info)
  305. {
  306. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  307. struct perf_event *event = info;
  308. struct perf_event_context *ctx = event->ctx;
  309. /*
  310. * If this is a task context, we need to check whether it is
  311. * the current task context of this cpu. If not it has been
  312. * scheduled out before the smp call arrived.
  313. */
  314. if (ctx->task && cpuctx->task_ctx != ctx)
  315. return;
  316. spin_lock(&ctx->lock);
  317. /*
  318. * Protect the list operation against NMI by disabling the
  319. * events on a global level.
  320. */
  321. perf_disable();
  322. event_sched_out(event, cpuctx, ctx);
  323. list_del_event(event, ctx);
  324. if (!ctx->task) {
  325. /*
  326. * Allow more per task events with respect to the
  327. * reservation:
  328. */
  329. cpuctx->max_pertask =
  330. min(perf_max_events - ctx->nr_events,
  331. perf_max_events - perf_reserved_percpu);
  332. }
  333. perf_enable();
  334. spin_unlock(&ctx->lock);
  335. }
  336. /*
  337. * Remove the event from a task's (or a CPU's) list of events.
  338. *
  339. * Must be called with ctx->mutex held.
  340. *
  341. * CPU events are removed with a smp call. For task events we only
  342. * call when the task is on a CPU.
  343. *
  344. * If event->ctx is a cloned context, callers must make sure that
  345. * every task struct that event->ctx->task could possibly point to
  346. * remains valid. This is OK when called from perf_release since
  347. * that only calls us on the top-level context, which can't be a clone.
  348. * When called from perf_event_exit_task, it's OK because the
  349. * context has been detached from its task.
  350. */
  351. static void perf_event_remove_from_context(struct perf_event *event)
  352. {
  353. struct perf_event_context *ctx = event->ctx;
  354. struct task_struct *task = ctx->task;
  355. if (!task) {
  356. /*
  357. * Per cpu events are removed via an smp call and
  358. * the removal is always sucessful.
  359. */
  360. smp_call_function_single(event->cpu,
  361. __perf_event_remove_from_context,
  362. event, 1);
  363. return;
  364. }
  365. retry:
  366. task_oncpu_function_call(task, __perf_event_remove_from_context,
  367. event);
  368. spin_lock_irq(&ctx->lock);
  369. /*
  370. * If the context is active we need to retry the smp call.
  371. */
  372. if (ctx->nr_active && !list_empty(&event->group_entry)) {
  373. spin_unlock_irq(&ctx->lock);
  374. goto retry;
  375. }
  376. /*
  377. * The lock prevents that this context is scheduled in so we
  378. * can remove the event safely, if the call above did not
  379. * succeed.
  380. */
  381. if (!list_empty(&event->group_entry)) {
  382. list_del_event(event, ctx);
  383. }
  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. perf_enable();
  888. out:
  889. spin_unlock(&ctx->lock);
  890. }
  891. /*
  892. * Test whether two contexts are equivalent, i.e. whether they
  893. * have both been cloned from the same version of the same context
  894. * and they both have the same number of enabled events.
  895. * If the number of enabled events is the same, then the set
  896. * of enabled events should be the same, because these are both
  897. * inherited contexts, therefore we can't access individual events
  898. * in them directly with an fd; we can only enable/disable all
  899. * events via prctl, or enable/disable all events in a family
  900. * via ioctl, which will have the same effect on both contexts.
  901. */
  902. static int context_equiv(struct perf_event_context *ctx1,
  903. struct perf_event_context *ctx2)
  904. {
  905. return ctx1->parent_ctx && ctx1->parent_ctx == ctx2->parent_ctx
  906. && ctx1->parent_gen == ctx2->parent_gen
  907. && !ctx1->pin_count && !ctx2->pin_count;
  908. }
  909. static void __perf_event_read(void *event);
  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. __perf_event_read(event);
  926. break;
  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. event = list_first_entry(&ctx->event_list,
  957. struct perf_event, event_entry);
  958. next_event = list_first_entry(&next_ctx->event_list,
  959. struct perf_event, event_entry);
  960. while (&event->event_entry != &ctx->event_list &&
  961. &next_event->event_entry != &next_ctx->event_list) {
  962. __perf_event_sync_stat(event, next_event);
  963. event = list_next_entry(event, event_entry);
  964. next_event = list_next_entry(next_event, event_entry);
  965. }
  966. }
  967. /*
  968. * Called from scheduler to remove the events of the current task,
  969. * with interrupts disabled.
  970. *
  971. * We stop each event and update the event value in event->count.
  972. *
  973. * This does not protect us against NMI, but disable()
  974. * sets the disabled bit in the control field of event _before_
  975. * accessing the event control register. If a NMI hits, then it will
  976. * not restart the event.
  977. */
  978. void perf_event_task_sched_out(struct task_struct *task,
  979. struct task_struct *next, int cpu)
  980. {
  981. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  982. struct perf_event_context *ctx = task->perf_event_ctxp;
  983. struct perf_event_context *next_ctx;
  984. struct perf_event_context *parent;
  985. struct pt_regs *regs;
  986. int do_switch = 1;
  987. regs = task_pt_regs(task);
  988. perf_sw_event(PERF_COUNT_SW_CONTEXT_SWITCHES, 1, 1, regs, 0);
  989. if (likely(!ctx || !cpuctx->task_ctx))
  990. return;
  991. update_context_time(ctx);
  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. unsigned long flags;
  1284. /*
  1285. * If this is a task context, we need to check whether it is
  1286. * the current task context of this cpu. If not it has been
  1287. * scheduled out before the smp call arrived. In that case
  1288. * event->count would have been updated to a recent sample
  1289. * when the event was scheduled out.
  1290. */
  1291. if (ctx->task && cpuctx->task_ctx != ctx)
  1292. return;
  1293. local_irq_save(flags);
  1294. if (ctx->is_active)
  1295. update_context_time(ctx);
  1296. event->pmu->read(event);
  1297. update_event_times(event);
  1298. local_irq_restore(flags);
  1299. }
  1300. static u64 perf_event_read(struct perf_event *event)
  1301. {
  1302. /*
  1303. * If event is enabled and currently active on a CPU, update the
  1304. * value in the event structure:
  1305. */
  1306. if (event->state == PERF_EVENT_STATE_ACTIVE) {
  1307. smp_call_function_single(event->oncpu,
  1308. __perf_event_read, event, 1);
  1309. } else if (event->state == PERF_EVENT_STATE_INACTIVE) {
  1310. update_event_times(event);
  1311. }
  1312. return atomic64_read(&event->count);
  1313. }
  1314. /*
  1315. * Initialize the perf_event context in a task_struct:
  1316. */
  1317. static void
  1318. __perf_event_init_context(struct perf_event_context *ctx,
  1319. struct task_struct *task)
  1320. {
  1321. memset(ctx, 0, sizeof(*ctx));
  1322. spin_lock_init(&ctx->lock);
  1323. mutex_init(&ctx->mutex);
  1324. INIT_LIST_HEAD(&ctx->group_list);
  1325. INIT_LIST_HEAD(&ctx->event_list);
  1326. atomic_set(&ctx->refcount, 1);
  1327. ctx->task = task;
  1328. }
  1329. static struct perf_event_context *find_get_context(pid_t pid, int cpu)
  1330. {
  1331. struct perf_event_context *ctx;
  1332. struct perf_cpu_context *cpuctx;
  1333. struct task_struct *task;
  1334. unsigned long flags;
  1335. int err;
  1336. /*
  1337. * If cpu is not a wildcard then this is a percpu event:
  1338. */
  1339. if (cpu != -1) {
  1340. /* Must be root to operate on a CPU event: */
  1341. if (perf_paranoid_cpu() && !capable(CAP_SYS_ADMIN))
  1342. return ERR_PTR(-EACCES);
  1343. if (cpu < 0 || cpu > num_possible_cpus())
  1344. return ERR_PTR(-EINVAL);
  1345. /*
  1346. * We could be clever and allow to attach a event to an
  1347. * offline CPU and activate it when the CPU comes up, but
  1348. * that's for later.
  1349. */
  1350. if (!cpu_isset(cpu, cpu_online_map))
  1351. return ERR_PTR(-ENODEV);
  1352. cpuctx = &per_cpu(perf_cpu_context, cpu);
  1353. ctx = &cpuctx->ctx;
  1354. get_ctx(ctx);
  1355. return ctx;
  1356. }
  1357. rcu_read_lock();
  1358. if (!pid)
  1359. task = current;
  1360. else
  1361. task = find_task_by_vpid(pid);
  1362. if (task)
  1363. get_task_struct(task);
  1364. rcu_read_unlock();
  1365. if (!task)
  1366. return ERR_PTR(-ESRCH);
  1367. /*
  1368. * Can't attach events to a dying task.
  1369. */
  1370. err = -ESRCH;
  1371. if (task->flags & PF_EXITING)
  1372. goto errout;
  1373. /* Reuse ptrace permission checks for now. */
  1374. err = -EACCES;
  1375. if (!ptrace_may_access(task, PTRACE_MODE_READ))
  1376. goto errout;
  1377. retry:
  1378. ctx = perf_lock_task_context(task, &flags);
  1379. if (ctx) {
  1380. unclone_ctx(ctx);
  1381. spin_unlock_irqrestore(&ctx->lock, flags);
  1382. }
  1383. if (!ctx) {
  1384. ctx = kmalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  1385. err = -ENOMEM;
  1386. if (!ctx)
  1387. goto errout;
  1388. __perf_event_init_context(ctx, task);
  1389. get_ctx(ctx);
  1390. if (cmpxchg(&task->perf_event_ctxp, NULL, ctx)) {
  1391. /*
  1392. * We raced with some other task; use
  1393. * the context they set.
  1394. */
  1395. kfree(ctx);
  1396. goto retry;
  1397. }
  1398. get_task_struct(task);
  1399. }
  1400. put_task_struct(task);
  1401. return ctx;
  1402. errout:
  1403. put_task_struct(task);
  1404. return ERR_PTR(err);
  1405. }
  1406. static void perf_event_free_filter(struct perf_event *event);
  1407. static void free_event_rcu(struct rcu_head *head)
  1408. {
  1409. struct perf_event *event;
  1410. event = container_of(head, struct perf_event, rcu_head);
  1411. if (event->ns)
  1412. put_pid_ns(event->ns);
  1413. perf_event_free_filter(event);
  1414. kfree(event);
  1415. }
  1416. static void perf_pending_sync(struct perf_event *event);
  1417. static void free_event(struct perf_event *event)
  1418. {
  1419. perf_pending_sync(event);
  1420. if (!event->parent) {
  1421. atomic_dec(&nr_events);
  1422. if (event->attr.mmap)
  1423. atomic_dec(&nr_mmap_events);
  1424. if (event->attr.comm)
  1425. atomic_dec(&nr_comm_events);
  1426. if (event->attr.task)
  1427. atomic_dec(&nr_task_events);
  1428. }
  1429. if (event->output) {
  1430. fput(event->output->filp);
  1431. event->output = NULL;
  1432. }
  1433. if (event->destroy)
  1434. event->destroy(event);
  1435. put_ctx(event->ctx);
  1436. call_rcu(&event->rcu_head, free_event_rcu);
  1437. }
  1438. /*
  1439. * Called when the last reference to the file is gone.
  1440. */
  1441. static int perf_release(struct inode *inode, struct file *file)
  1442. {
  1443. struct perf_event *event = file->private_data;
  1444. struct perf_event_context *ctx = event->ctx;
  1445. file->private_data = NULL;
  1446. WARN_ON_ONCE(ctx->parent_ctx);
  1447. mutex_lock(&ctx->mutex);
  1448. perf_event_remove_from_context(event);
  1449. mutex_unlock(&ctx->mutex);
  1450. mutex_lock(&event->owner->perf_event_mutex);
  1451. list_del_init(&event->owner_entry);
  1452. mutex_unlock(&event->owner->perf_event_mutex);
  1453. put_task_struct(event->owner);
  1454. free_event(event);
  1455. return 0;
  1456. }
  1457. static int perf_event_read_size(struct perf_event *event)
  1458. {
  1459. int entry = sizeof(u64); /* value */
  1460. int size = 0;
  1461. int nr = 1;
  1462. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  1463. size += sizeof(u64);
  1464. if (event->attr.read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  1465. size += sizeof(u64);
  1466. if (event->attr.read_format & PERF_FORMAT_ID)
  1467. entry += sizeof(u64);
  1468. if (event->attr.read_format & PERF_FORMAT_GROUP) {
  1469. nr += event->group_leader->nr_siblings;
  1470. size += sizeof(u64);
  1471. }
  1472. size += entry * nr;
  1473. return size;
  1474. }
  1475. static u64 perf_event_read_value(struct perf_event *event)
  1476. {
  1477. struct perf_event *child;
  1478. u64 total = 0;
  1479. total += perf_event_read(event);
  1480. list_for_each_entry(child, &event->child_list, child_list)
  1481. total += perf_event_read(child);
  1482. return total;
  1483. }
  1484. static int perf_event_read_entry(struct perf_event *event,
  1485. u64 read_format, char __user *buf)
  1486. {
  1487. int n = 0, count = 0;
  1488. u64 values[2];
  1489. values[n++] = perf_event_read_value(event);
  1490. if (read_format & PERF_FORMAT_ID)
  1491. values[n++] = primary_event_id(event);
  1492. count = n * sizeof(u64);
  1493. if (copy_to_user(buf, values, count))
  1494. return -EFAULT;
  1495. return count;
  1496. }
  1497. static int perf_event_read_group(struct perf_event *event,
  1498. u64 read_format, char __user *buf)
  1499. {
  1500. struct perf_event *leader = event->group_leader, *sub;
  1501. int n = 0, size = 0, err = -EFAULT;
  1502. u64 values[3];
  1503. values[n++] = 1 + leader->nr_siblings;
  1504. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1505. values[n++] = leader->total_time_enabled +
  1506. atomic64_read(&leader->child_total_time_enabled);
  1507. }
  1508. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1509. values[n++] = leader->total_time_running +
  1510. atomic64_read(&leader->child_total_time_running);
  1511. }
  1512. size = n * sizeof(u64);
  1513. if (copy_to_user(buf, values, size))
  1514. return -EFAULT;
  1515. err = perf_event_read_entry(leader, read_format, buf + size);
  1516. if (err < 0)
  1517. return err;
  1518. size += err;
  1519. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  1520. err = perf_event_read_entry(sub, read_format,
  1521. buf + size);
  1522. if (err < 0)
  1523. return err;
  1524. size += err;
  1525. }
  1526. return size;
  1527. }
  1528. static int perf_event_read_one(struct perf_event *event,
  1529. u64 read_format, char __user *buf)
  1530. {
  1531. u64 values[4];
  1532. int n = 0;
  1533. values[n++] = perf_event_read_value(event);
  1534. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  1535. values[n++] = event->total_time_enabled +
  1536. atomic64_read(&event->child_total_time_enabled);
  1537. }
  1538. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  1539. values[n++] = event->total_time_running +
  1540. atomic64_read(&event->child_total_time_running);
  1541. }
  1542. if (read_format & PERF_FORMAT_ID)
  1543. values[n++] = primary_event_id(event);
  1544. if (copy_to_user(buf, values, n * sizeof(u64)))
  1545. return -EFAULT;
  1546. return n * sizeof(u64);
  1547. }
  1548. /*
  1549. * Read the performance event - simple non blocking version for now
  1550. */
  1551. static ssize_t
  1552. perf_read_hw(struct perf_event *event, char __user *buf, size_t count)
  1553. {
  1554. u64 read_format = event->attr.read_format;
  1555. int ret;
  1556. /*
  1557. * Return end-of-file for a read on a event that is in
  1558. * error state (i.e. because it was pinned but it couldn't be
  1559. * scheduled on to the CPU at some point).
  1560. */
  1561. if (event->state == PERF_EVENT_STATE_ERROR)
  1562. return 0;
  1563. if (count < perf_event_read_size(event))
  1564. return -ENOSPC;
  1565. WARN_ON_ONCE(event->ctx->parent_ctx);
  1566. mutex_lock(&event->child_mutex);
  1567. if (read_format & PERF_FORMAT_GROUP)
  1568. ret = perf_event_read_group(event, read_format, buf);
  1569. else
  1570. ret = perf_event_read_one(event, read_format, buf);
  1571. mutex_unlock(&event->child_mutex);
  1572. return ret;
  1573. }
  1574. static ssize_t
  1575. perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
  1576. {
  1577. struct perf_event *event = file->private_data;
  1578. return perf_read_hw(event, buf, count);
  1579. }
  1580. static unsigned int perf_poll(struct file *file, poll_table *wait)
  1581. {
  1582. struct perf_event *event = file->private_data;
  1583. struct perf_mmap_data *data;
  1584. unsigned int events = POLL_HUP;
  1585. rcu_read_lock();
  1586. data = rcu_dereference(event->data);
  1587. if (data)
  1588. events = atomic_xchg(&data->poll, 0);
  1589. rcu_read_unlock();
  1590. poll_wait(file, &event->waitq, wait);
  1591. return events;
  1592. }
  1593. static void perf_event_reset(struct perf_event *event)
  1594. {
  1595. (void)perf_event_read(event);
  1596. atomic64_set(&event->count, 0);
  1597. perf_event_update_userpage(event);
  1598. }
  1599. /*
  1600. * Holding the top-level event's child_mutex means that any
  1601. * descendant process that has inherited this event will block
  1602. * in sync_child_event if it goes to exit, thus satisfying the
  1603. * task existence requirements of perf_event_enable/disable.
  1604. */
  1605. static void perf_event_for_each_child(struct perf_event *event,
  1606. void (*func)(struct perf_event *))
  1607. {
  1608. struct perf_event *child;
  1609. WARN_ON_ONCE(event->ctx->parent_ctx);
  1610. mutex_lock(&event->child_mutex);
  1611. func(event);
  1612. list_for_each_entry(child, &event->child_list, child_list)
  1613. func(child);
  1614. mutex_unlock(&event->child_mutex);
  1615. }
  1616. static void perf_event_for_each(struct perf_event *event,
  1617. void (*func)(struct perf_event *))
  1618. {
  1619. struct perf_event_context *ctx = event->ctx;
  1620. struct perf_event *sibling;
  1621. WARN_ON_ONCE(ctx->parent_ctx);
  1622. mutex_lock(&ctx->mutex);
  1623. event = event->group_leader;
  1624. perf_event_for_each_child(event, func);
  1625. func(event);
  1626. list_for_each_entry(sibling, &event->sibling_list, group_entry)
  1627. perf_event_for_each_child(event, func);
  1628. mutex_unlock(&ctx->mutex);
  1629. }
  1630. static int perf_event_period(struct perf_event *event, u64 __user *arg)
  1631. {
  1632. struct perf_event_context *ctx = event->ctx;
  1633. unsigned long size;
  1634. int ret = 0;
  1635. u64 value;
  1636. if (!event->attr.sample_period)
  1637. return -EINVAL;
  1638. size = copy_from_user(&value, arg, sizeof(value));
  1639. if (size != sizeof(value))
  1640. return -EFAULT;
  1641. if (!value)
  1642. return -EINVAL;
  1643. spin_lock_irq(&ctx->lock);
  1644. if (event->attr.freq) {
  1645. if (value > sysctl_perf_event_sample_rate) {
  1646. ret = -EINVAL;
  1647. goto unlock;
  1648. }
  1649. event->attr.sample_freq = value;
  1650. } else {
  1651. event->attr.sample_period = value;
  1652. event->hw.sample_period = value;
  1653. }
  1654. unlock:
  1655. spin_unlock_irq(&ctx->lock);
  1656. return ret;
  1657. }
  1658. static int perf_event_set_output(struct perf_event *event, int output_fd);
  1659. static int perf_event_set_filter(struct perf_event *event, void __user *arg);
  1660. static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  1661. {
  1662. struct perf_event *event = file->private_data;
  1663. void (*func)(struct perf_event *);
  1664. u32 flags = arg;
  1665. switch (cmd) {
  1666. case PERF_EVENT_IOC_ENABLE:
  1667. func = perf_event_enable;
  1668. break;
  1669. case PERF_EVENT_IOC_DISABLE:
  1670. func = perf_event_disable;
  1671. break;
  1672. case PERF_EVENT_IOC_RESET:
  1673. func = perf_event_reset;
  1674. break;
  1675. case PERF_EVENT_IOC_REFRESH:
  1676. return perf_event_refresh(event, arg);
  1677. case PERF_EVENT_IOC_PERIOD:
  1678. return perf_event_period(event, (u64 __user *)arg);
  1679. case PERF_EVENT_IOC_SET_OUTPUT:
  1680. return perf_event_set_output(event, arg);
  1681. case PERF_EVENT_IOC_SET_FILTER:
  1682. return perf_event_set_filter(event, (void __user *)arg);
  1683. default:
  1684. return -ENOTTY;
  1685. }
  1686. if (flags & PERF_IOC_FLAG_GROUP)
  1687. perf_event_for_each(event, func);
  1688. else
  1689. perf_event_for_each_child(event, func);
  1690. return 0;
  1691. }
  1692. int perf_event_task_enable(void)
  1693. {
  1694. struct perf_event *event;
  1695. mutex_lock(&current->perf_event_mutex);
  1696. list_for_each_entry(event, &current->perf_event_list, owner_entry)
  1697. perf_event_for_each_child(event, perf_event_enable);
  1698. mutex_unlock(&current->perf_event_mutex);
  1699. return 0;
  1700. }
  1701. int perf_event_task_disable(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_disable);
  1707. mutex_unlock(&current->perf_event_mutex);
  1708. return 0;
  1709. }
  1710. #ifndef PERF_EVENT_INDEX_OFFSET
  1711. # define PERF_EVENT_INDEX_OFFSET 0
  1712. #endif
  1713. static int perf_event_index(struct perf_event *event)
  1714. {
  1715. if (event->state != PERF_EVENT_STATE_ACTIVE)
  1716. return 0;
  1717. return event->hw.idx + 1 - PERF_EVENT_INDEX_OFFSET;
  1718. }
  1719. /*
  1720. * Callers need to ensure there can be no nesting of this function, otherwise
  1721. * the seqlock logic goes bad. We can not serialize this because the arch
  1722. * code calls this from NMI context.
  1723. */
  1724. void perf_event_update_userpage(struct perf_event *event)
  1725. {
  1726. struct perf_event_mmap_page *userpg;
  1727. struct perf_mmap_data *data;
  1728. rcu_read_lock();
  1729. data = rcu_dereference(event->data);
  1730. if (!data)
  1731. goto unlock;
  1732. userpg = data->user_page;
  1733. /*
  1734. * Disable preemption so as to not let the corresponding user-space
  1735. * spin too long if we get preempted.
  1736. */
  1737. preempt_disable();
  1738. ++userpg->lock;
  1739. barrier();
  1740. userpg->index = perf_event_index(event);
  1741. userpg->offset = atomic64_read(&event->count);
  1742. if (event->state == PERF_EVENT_STATE_ACTIVE)
  1743. userpg->offset -= atomic64_read(&event->hw.prev_count);
  1744. userpg->time_enabled = event->total_time_enabled +
  1745. atomic64_read(&event->child_total_time_enabled);
  1746. userpg->time_running = event->total_time_running +
  1747. atomic64_read(&event->child_total_time_running);
  1748. barrier();
  1749. ++userpg->lock;
  1750. preempt_enable();
  1751. unlock:
  1752. rcu_read_unlock();
  1753. }
  1754. static unsigned long perf_data_size(struct perf_mmap_data *data)
  1755. {
  1756. return data->nr_pages << (PAGE_SHIFT + data->data_order);
  1757. }
  1758. #ifndef CONFIG_PERF_USE_VMALLOC
  1759. /*
  1760. * Back perf_mmap() with regular GFP_KERNEL-0 pages.
  1761. */
  1762. static struct page *
  1763. perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
  1764. {
  1765. if (pgoff > data->nr_pages)
  1766. return NULL;
  1767. if (pgoff == 0)
  1768. return virt_to_page(data->user_page);
  1769. return virt_to_page(data->data_pages[pgoff - 1]);
  1770. }
  1771. static struct perf_mmap_data *
  1772. perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
  1773. {
  1774. struct perf_mmap_data *data;
  1775. unsigned long size;
  1776. int i;
  1777. WARN_ON(atomic_read(&event->mmap_count));
  1778. size = sizeof(struct perf_mmap_data);
  1779. size += nr_pages * sizeof(void *);
  1780. data = kzalloc(size, GFP_KERNEL);
  1781. if (!data)
  1782. goto fail;
  1783. data->user_page = (void *)get_zeroed_page(GFP_KERNEL);
  1784. if (!data->user_page)
  1785. goto fail_user_page;
  1786. for (i = 0; i < nr_pages; i++) {
  1787. data->data_pages[i] = (void *)get_zeroed_page(GFP_KERNEL);
  1788. if (!data->data_pages[i])
  1789. goto fail_data_pages;
  1790. }
  1791. data->data_order = 0;
  1792. data->nr_pages = nr_pages;
  1793. return data;
  1794. fail_data_pages:
  1795. for (i--; i >= 0; i--)
  1796. free_page((unsigned long)data->data_pages[i]);
  1797. free_page((unsigned long)data->user_page);
  1798. fail_user_page:
  1799. kfree(data);
  1800. fail:
  1801. return NULL;
  1802. }
  1803. static void perf_mmap_free_page(unsigned long addr)
  1804. {
  1805. struct page *page = virt_to_page((void *)addr);
  1806. page->mapping = NULL;
  1807. __free_page(page);
  1808. }
  1809. static void perf_mmap_data_free(struct perf_mmap_data *data)
  1810. {
  1811. int i;
  1812. perf_mmap_free_page((unsigned long)data->user_page);
  1813. for (i = 0; i < data->nr_pages; i++)
  1814. perf_mmap_free_page((unsigned long)data->data_pages[i]);
  1815. }
  1816. #else
  1817. /*
  1818. * Back perf_mmap() with vmalloc memory.
  1819. *
  1820. * Required for architectures that have d-cache aliasing issues.
  1821. */
  1822. static struct page *
  1823. perf_mmap_to_page(struct perf_mmap_data *data, unsigned long pgoff)
  1824. {
  1825. if (pgoff > (1UL << data->data_order))
  1826. return NULL;
  1827. return vmalloc_to_page((void *)data->user_page + pgoff * PAGE_SIZE);
  1828. }
  1829. static void perf_mmap_unmark_page(void *addr)
  1830. {
  1831. struct page *page = vmalloc_to_page(addr);
  1832. page->mapping = NULL;
  1833. }
  1834. static void perf_mmap_data_free_work(struct work_struct *work)
  1835. {
  1836. struct perf_mmap_data *data;
  1837. void *base;
  1838. int i, nr;
  1839. data = container_of(work, struct perf_mmap_data, work);
  1840. nr = 1 << data->data_order;
  1841. base = data->user_page;
  1842. for (i = 0; i < nr + 1; i++)
  1843. perf_mmap_unmark_page(base + (i * PAGE_SIZE));
  1844. vfree(base);
  1845. }
  1846. static void perf_mmap_data_free(struct perf_mmap_data *data)
  1847. {
  1848. schedule_work(&data->work);
  1849. }
  1850. static struct perf_mmap_data *
  1851. perf_mmap_data_alloc(struct perf_event *event, int nr_pages)
  1852. {
  1853. struct perf_mmap_data *data;
  1854. unsigned long size;
  1855. void *all_buf;
  1856. WARN_ON(atomic_read(&event->mmap_count));
  1857. size = sizeof(struct perf_mmap_data);
  1858. size += sizeof(void *);
  1859. data = kzalloc(size, GFP_KERNEL);
  1860. if (!data)
  1861. goto fail;
  1862. INIT_WORK(&data->work, perf_mmap_data_free_work);
  1863. all_buf = vmalloc_user((nr_pages + 1) * PAGE_SIZE);
  1864. if (!all_buf)
  1865. goto fail_all_buf;
  1866. data->user_page = all_buf;
  1867. data->data_pages[0] = all_buf + PAGE_SIZE;
  1868. data->data_order = ilog2(nr_pages);
  1869. data->nr_pages = 1;
  1870. return data;
  1871. fail_all_buf:
  1872. kfree(data);
  1873. fail:
  1874. return NULL;
  1875. }
  1876. #endif
  1877. static int perf_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
  1878. {
  1879. struct perf_event *event = vma->vm_file->private_data;
  1880. struct perf_mmap_data *data;
  1881. int ret = VM_FAULT_SIGBUS;
  1882. if (vmf->flags & FAULT_FLAG_MKWRITE) {
  1883. if (vmf->pgoff == 0)
  1884. ret = 0;
  1885. return ret;
  1886. }
  1887. rcu_read_lock();
  1888. data = rcu_dereference(event->data);
  1889. if (!data)
  1890. goto unlock;
  1891. if (vmf->pgoff && (vmf->flags & FAULT_FLAG_WRITE))
  1892. goto unlock;
  1893. vmf->page = perf_mmap_to_page(data, vmf->pgoff);
  1894. if (!vmf->page)
  1895. goto unlock;
  1896. get_page(vmf->page);
  1897. vmf->page->mapping = vma->vm_file->f_mapping;
  1898. vmf->page->index = vmf->pgoff;
  1899. ret = 0;
  1900. unlock:
  1901. rcu_read_unlock();
  1902. return ret;
  1903. }
  1904. static void
  1905. perf_mmap_data_init(struct perf_event *event, struct perf_mmap_data *data)
  1906. {
  1907. long max_size = perf_data_size(data);
  1908. atomic_set(&data->lock, -1);
  1909. if (event->attr.watermark) {
  1910. data->watermark = min_t(long, max_size,
  1911. event->attr.wakeup_watermark);
  1912. }
  1913. if (!data->watermark)
  1914. data->watermark = max_t(long, PAGE_SIZE, max_size / 2);
  1915. rcu_assign_pointer(event->data, data);
  1916. }
  1917. static void perf_mmap_data_free_rcu(struct rcu_head *rcu_head)
  1918. {
  1919. struct perf_mmap_data *data;
  1920. data = container_of(rcu_head, struct perf_mmap_data, rcu_head);
  1921. perf_mmap_data_free(data);
  1922. kfree(data);
  1923. }
  1924. static void perf_mmap_data_release(struct perf_event *event)
  1925. {
  1926. struct perf_mmap_data *data = event->data;
  1927. WARN_ON(atomic_read(&event->mmap_count));
  1928. rcu_assign_pointer(event->data, NULL);
  1929. call_rcu(&data->rcu_head, perf_mmap_data_free_rcu);
  1930. }
  1931. static void perf_mmap_open(struct vm_area_struct *vma)
  1932. {
  1933. struct perf_event *event = vma->vm_file->private_data;
  1934. atomic_inc(&event->mmap_count);
  1935. }
  1936. static void perf_mmap_close(struct vm_area_struct *vma)
  1937. {
  1938. struct perf_event *event = vma->vm_file->private_data;
  1939. WARN_ON_ONCE(event->ctx->parent_ctx);
  1940. if (atomic_dec_and_mutex_lock(&event->mmap_count, &event->mmap_mutex)) {
  1941. unsigned long size = perf_data_size(event->data);
  1942. struct user_struct *user = current_user();
  1943. atomic_long_sub((size >> PAGE_SHIFT) + 1, &user->locked_vm);
  1944. vma->vm_mm->locked_vm -= event->data->nr_locked;
  1945. perf_mmap_data_release(event);
  1946. mutex_unlock(&event->mmap_mutex);
  1947. }
  1948. }
  1949. static const struct vm_operations_struct perf_mmap_vmops = {
  1950. .open = perf_mmap_open,
  1951. .close = perf_mmap_close,
  1952. .fault = perf_mmap_fault,
  1953. .page_mkwrite = perf_mmap_fault,
  1954. };
  1955. static int perf_mmap(struct file *file, struct vm_area_struct *vma)
  1956. {
  1957. struct perf_event *event = file->private_data;
  1958. unsigned long user_locked, user_lock_limit;
  1959. struct user_struct *user = current_user();
  1960. unsigned long locked, lock_limit;
  1961. struct perf_mmap_data *data;
  1962. unsigned long vma_size;
  1963. unsigned long nr_pages;
  1964. long user_extra, extra;
  1965. int ret = 0;
  1966. if (!(vma->vm_flags & VM_SHARED))
  1967. return -EINVAL;
  1968. vma_size = vma->vm_end - vma->vm_start;
  1969. nr_pages = (vma_size / PAGE_SIZE) - 1;
  1970. /*
  1971. * If we have data pages ensure they're a power-of-two number, so we
  1972. * can do bitmasks instead of modulo.
  1973. */
  1974. if (nr_pages != 0 && !is_power_of_2(nr_pages))
  1975. return -EINVAL;
  1976. if (vma_size != PAGE_SIZE * (1 + nr_pages))
  1977. return -EINVAL;
  1978. if (vma->vm_pgoff != 0)
  1979. return -EINVAL;
  1980. WARN_ON_ONCE(event->ctx->parent_ctx);
  1981. mutex_lock(&event->mmap_mutex);
  1982. if (event->output) {
  1983. ret = -EINVAL;
  1984. goto unlock;
  1985. }
  1986. if (atomic_inc_not_zero(&event->mmap_count)) {
  1987. if (nr_pages != event->data->nr_pages)
  1988. ret = -EINVAL;
  1989. goto unlock;
  1990. }
  1991. user_extra = nr_pages + 1;
  1992. user_lock_limit = sysctl_perf_event_mlock >> (PAGE_SHIFT - 10);
  1993. /*
  1994. * Increase the limit linearly with more CPUs:
  1995. */
  1996. user_lock_limit *= num_online_cpus();
  1997. user_locked = atomic_long_read(&user->locked_vm) + user_extra;
  1998. extra = 0;
  1999. if (user_locked > user_lock_limit)
  2000. extra = user_locked - user_lock_limit;
  2001. lock_limit = current->signal->rlim[RLIMIT_MEMLOCK].rlim_cur;
  2002. lock_limit >>= PAGE_SHIFT;
  2003. locked = vma->vm_mm->locked_vm + extra;
  2004. if ((locked > lock_limit) && perf_paranoid_tracepoint_raw() &&
  2005. !capable(CAP_IPC_LOCK)) {
  2006. ret = -EPERM;
  2007. goto unlock;
  2008. }
  2009. WARN_ON(event->data);
  2010. data = perf_mmap_data_alloc(event, nr_pages);
  2011. ret = -ENOMEM;
  2012. if (!data)
  2013. goto unlock;
  2014. ret = 0;
  2015. perf_mmap_data_init(event, data);
  2016. atomic_set(&event->mmap_count, 1);
  2017. atomic_long_add(user_extra, &user->locked_vm);
  2018. vma->vm_mm->locked_vm += extra;
  2019. event->data->nr_locked = extra;
  2020. if (vma->vm_flags & VM_WRITE)
  2021. event->data->writable = 1;
  2022. unlock:
  2023. mutex_unlock(&event->mmap_mutex);
  2024. vma->vm_flags |= VM_RESERVED;
  2025. vma->vm_ops = &perf_mmap_vmops;
  2026. return ret;
  2027. }
  2028. static int perf_fasync(int fd, struct file *filp, int on)
  2029. {
  2030. struct inode *inode = filp->f_path.dentry->d_inode;
  2031. struct perf_event *event = filp->private_data;
  2032. int retval;
  2033. mutex_lock(&inode->i_mutex);
  2034. retval = fasync_helper(fd, filp, on, &event->fasync);
  2035. mutex_unlock(&inode->i_mutex);
  2036. if (retval < 0)
  2037. return retval;
  2038. return 0;
  2039. }
  2040. static const struct file_operations perf_fops = {
  2041. .release = perf_release,
  2042. .read = perf_read,
  2043. .poll = perf_poll,
  2044. .unlocked_ioctl = perf_ioctl,
  2045. .compat_ioctl = perf_ioctl,
  2046. .mmap = perf_mmap,
  2047. .fasync = perf_fasync,
  2048. };
  2049. /*
  2050. * Perf event wakeup
  2051. *
  2052. * If there's data, ensure we set the poll() state and publish everything
  2053. * to user-space before waking everybody up.
  2054. */
  2055. void perf_event_wakeup(struct perf_event *event)
  2056. {
  2057. wake_up_all(&event->waitq);
  2058. if (event->pending_kill) {
  2059. kill_fasync(&event->fasync, SIGIO, event->pending_kill);
  2060. event->pending_kill = 0;
  2061. }
  2062. }
  2063. /*
  2064. * Pending wakeups
  2065. *
  2066. * Handle the case where we need to wakeup up from NMI (or rq->lock) context.
  2067. *
  2068. * The NMI bit means we cannot possibly take locks. Therefore, maintain a
  2069. * single linked list and use cmpxchg() to add entries lockless.
  2070. */
  2071. static void perf_pending_event(struct perf_pending_entry *entry)
  2072. {
  2073. struct perf_event *event = container_of(entry,
  2074. struct perf_event, pending);
  2075. if (event->pending_disable) {
  2076. event->pending_disable = 0;
  2077. __perf_event_disable(event);
  2078. }
  2079. if (event->pending_wakeup) {
  2080. event->pending_wakeup = 0;
  2081. perf_event_wakeup(event);
  2082. }
  2083. }
  2084. #define PENDING_TAIL ((struct perf_pending_entry *)-1UL)
  2085. static DEFINE_PER_CPU(struct perf_pending_entry *, perf_pending_head) = {
  2086. PENDING_TAIL,
  2087. };
  2088. static void perf_pending_queue(struct perf_pending_entry *entry,
  2089. void (*func)(struct perf_pending_entry *))
  2090. {
  2091. struct perf_pending_entry **head;
  2092. if (cmpxchg(&entry->next, NULL, PENDING_TAIL) != NULL)
  2093. return;
  2094. entry->func = func;
  2095. head = &get_cpu_var(perf_pending_head);
  2096. do {
  2097. entry->next = *head;
  2098. } while (cmpxchg(head, entry->next, entry) != entry->next);
  2099. set_perf_event_pending();
  2100. put_cpu_var(perf_pending_head);
  2101. }
  2102. static int __perf_pending_run(void)
  2103. {
  2104. struct perf_pending_entry *list;
  2105. int nr = 0;
  2106. list = xchg(&__get_cpu_var(perf_pending_head), PENDING_TAIL);
  2107. while (list != PENDING_TAIL) {
  2108. void (*func)(struct perf_pending_entry *);
  2109. struct perf_pending_entry *entry = list;
  2110. list = list->next;
  2111. func = entry->func;
  2112. entry->next = NULL;
  2113. /*
  2114. * Ensure we observe the unqueue before we issue the wakeup,
  2115. * so that we won't be waiting forever.
  2116. * -- see perf_not_pending().
  2117. */
  2118. smp_wmb();
  2119. func(entry);
  2120. nr++;
  2121. }
  2122. return nr;
  2123. }
  2124. static inline int perf_not_pending(struct perf_event *event)
  2125. {
  2126. /*
  2127. * If we flush on whatever cpu we run, there is a chance we don't
  2128. * need to wait.
  2129. */
  2130. get_cpu();
  2131. __perf_pending_run();
  2132. put_cpu();
  2133. /*
  2134. * Ensure we see the proper queue state before going to sleep
  2135. * so that we do not miss the wakeup. -- see perf_pending_handle()
  2136. */
  2137. smp_rmb();
  2138. return event->pending.next == NULL;
  2139. }
  2140. static void perf_pending_sync(struct perf_event *event)
  2141. {
  2142. wait_event(event->waitq, perf_not_pending(event));
  2143. }
  2144. void perf_event_do_pending(void)
  2145. {
  2146. __perf_pending_run();
  2147. }
  2148. /*
  2149. * Callchain support -- arch specific
  2150. */
  2151. __weak struct perf_callchain_entry *perf_callchain(struct pt_regs *regs)
  2152. {
  2153. return NULL;
  2154. }
  2155. /*
  2156. * Output
  2157. */
  2158. static bool perf_output_space(struct perf_mmap_data *data, unsigned long tail,
  2159. unsigned long offset, unsigned long head)
  2160. {
  2161. unsigned long mask;
  2162. if (!data->writable)
  2163. return true;
  2164. mask = perf_data_size(data) - 1;
  2165. offset = (offset - tail) & mask;
  2166. head = (head - tail) & mask;
  2167. if ((int)(head - offset) < 0)
  2168. return false;
  2169. return true;
  2170. }
  2171. static void perf_output_wakeup(struct perf_output_handle *handle)
  2172. {
  2173. atomic_set(&handle->data->poll, POLL_IN);
  2174. if (handle->nmi) {
  2175. handle->event->pending_wakeup = 1;
  2176. perf_pending_queue(&handle->event->pending,
  2177. perf_pending_event);
  2178. } else
  2179. perf_event_wakeup(handle->event);
  2180. }
  2181. /*
  2182. * Curious locking construct.
  2183. *
  2184. * We need to ensure a later event_id doesn't publish a head when a former
  2185. * event_id isn't done writing. However since we need to deal with NMIs we
  2186. * cannot fully serialize things.
  2187. *
  2188. * What we do is serialize between CPUs so we only have to deal with NMI
  2189. * nesting on a single CPU.
  2190. *
  2191. * We only publish the head (and generate a wakeup) when the outer-most
  2192. * event_id completes.
  2193. */
  2194. static void perf_output_lock(struct perf_output_handle *handle)
  2195. {
  2196. struct perf_mmap_data *data = handle->data;
  2197. int cpu;
  2198. handle->locked = 0;
  2199. local_irq_save(handle->flags);
  2200. cpu = smp_processor_id();
  2201. if (in_nmi() && atomic_read(&data->lock) == cpu)
  2202. return;
  2203. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  2204. cpu_relax();
  2205. handle->locked = 1;
  2206. }
  2207. static void perf_output_unlock(struct perf_output_handle *handle)
  2208. {
  2209. struct perf_mmap_data *data = handle->data;
  2210. unsigned long head;
  2211. int cpu;
  2212. data->done_head = data->head;
  2213. if (!handle->locked)
  2214. goto out;
  2215. again:
  2216. /*
  2217. * The xchg implies a full barrier that ensures all writes are done
  2218. * before we publish the new head, matched by a rmb() in userspace when
  2219. * reading this position.
  2220. */
  2221. while ((head = atomic_long_xchg(&data->done_head, 0)))
  2222. data->user_page->data_head = head;
  2223. /*
  2224. * NMI can happen here, which means we can miss a done_head update.
  2225. */
  2226. cpu = atomic_xchg(&data->lock, -1);
  2227. WARN_ON_ONCE(cpu != smp_processor_id());
  2228. /*
  2229. * Therefore we have to validate we did not indeed do so.
  2230. */
  2231. if (unlikely(atomic_long_read(&data->done_head))) {
  2232. /*
  2233. * Since we had it locked, we can lock it again.
  2234. */
  2235. while (atomic_cmpxchg(&data->lock, -1, cpu) != -1)
  2236. cpu_relax();
  2237. goto again;
  2238. }
  2239. if (atomic_xchg(&data->wakeup, 0))
  2240. perf_output_wakeup(handle);
  2241. out:
  2242. local_irq_restore(handle->flags);
  2243. }
  2244. void perf_output_copy(struct perf_output_handle *handle,
  2245. const void *buf, unsigned int len)
  2246. {
  2247. unsigned int pages_mask;
  2248. unsigned long offset;
  2249. unsigned int size;
  2250. void **pages;
  2251. offset = handle->offset;
  2252. pages_mask = handle->data->nr_pages - 1;
  2253. pages = handle->data->data_pages;
  2254. do {
  2255. unsigned long page_offset;
  2256. unsigned long page_size;
  2257. int nr;
  2258. nr = (offset >> PAGE_SHIFT) & pages_mask;
  2259. page_size = 1UL << (handle->data->data_order + PAGE_SHIFT);
  2260. page_offset = offset & (page_size - 1);
  2261. size = min_t(unsigned int, page_size - page_offset, len);
  2262. memcpy(pages[nr] + page_offset, buf, size);
  2263. len -= size;
  2264. buf += size;
  2265. offset += size;
  2266. } while (len);
  2267. handle->offset = offset;
  2268. /*
  2269. * Check we didn't copy past our reservation window, taking the
  2270. * possible unsigned int wrap into account.
  2271. */
  2272. WARN_ON_ONCE(((long)(handle->head - handle->offset)) < 0);
  2273. }
  2274. int perf_output_begin(struct perf_output_handle *handle,
  2275. struct perf_event *event, unsigned int size,
  2276. int nmi, int sample)
  2277. {
  2278. struct perf_event *output_event;
  2279. struct perf_mmap_data *data;
  2280. unsigned long tail, offset, head;
  2281. int have_lost;
  2282. struct {
  2283. struct perf_event_header header;
  2284. u64 id;
  2285. u64 lost;
  2286. } lost_event;
  2287. rcu_read_lock();
  2288. /*
  2289. * For inherited events we send all the output towards the parent.
  2290. */
  2291. if (event->parent)
  2292. event = event->parent;
  2293. output_event = rcu_dereference(event->output);
  2294. if (output_event)
  2295. event = output_event;
  2296. data = rcu_dereference(event->data);
  2297. if (!data)
  2298. goto out;
  2299. handle->data = data;
  2300. handle->event = event;
  2301. handle->nmi = nmi;
  2302. handle->sample = sample;
  2303. if (!data->nr_pages)
  2304. goto fail;
  2305. have_lost = atomic_read(&data->lost);
  2306. if (have_lost)
  2307. size += sizeof(lost_event);
  2308. perf_output_lock(handle);
  2309. do {
  2310. /*
  2311. * Userspace could choose to issue a mb() before updating the
  2312. * tail pointer. So that all reads will be completed before the
  2313. * write is issued.
  2314. */
  2315. tail = ACCESS_ONCE(data->user_page->data_tail);
  2316. smp_rmb();
  2317. offset = head = atomic_long_read(&data->head);
  2318. head += size;
  2319. if (unlikely(!perf_output_space(data, tail, offset, head)))
  2320. goto fail;
  2321. } while (atomic_long_cmpxchg(&data->head, offset, head) != offset);
  2322. handle->offset = offset;
  2323. handle->head = head;
  2324. if (head - tail > data->watermark)
  2325. atomic_set(&data->wakeup, 1);
  2326. if (have_lost) {
  2327. lost_event.header.type = PERF_RECORD_LOST;
  2328. lost_event.header.misc = 0;
  2329. lost_event.header.size = sizeof(lost_event);
  2330. lost_event.id = event->id;
  2331. lost_event.lost = atomic_xchg(&data->lost, 0);
  2332. perf_output_put(handle, lost_event);
  2333. }
  2334. return 0;
  2335. fail:
  2336. atomic_inc(&data->lost);
  2337. perf_output_unlock(handle);
  2338. out:
  2339. rcu_read_unlock();
  2340. return -ENOSPC;
  2341. }
  2342. void perf_output_end(struct perf_output_handle *handle)
  2343. {
  2344. struct perf_event *event = handle->event;
  2345. struct perf_mmap_data *data = handle->data;
  2346. int wakeup_events = event->attr.wakeup_events;
  2347. if (handle->sample && wakeup_events) {
  2348. int events = atomic_inc_return(&data->events);
  2349. if (events >= wakeup_events) {
  2350. atomic_sub(wakeup_events, &data->events);
  2351. atomic_set(&data->wakeup, 1);
  2352. }
  2353. }
  2354. perf_output_unlock(handle);
  2355. rcu_read_unlock();
  2356. }
  2357. static u32 perf_event_pid(struct perf_event *event, struct task_struct *p)
  2358. {
  2359. /*
  2360. * only top level events have the pid namespace they were created in
  2361. */
  2362. if (event->parent)
  2363. event = event->parent;
  2364. return task_tgid_nr_ns(p, event->ns);
  2365. }
  2366. static u32 perf_event_tid(struct perf_event *event, struct task_struct *p)
  2367. {
  2368. /*
  2369. * only top level events have the pid namespace they were created in
  2370. */
  2371. if (event->parent)
  2372. event = event->parent;
  2373. return task_pid_nr_ns(p, event->ns);
  2374. }
  2375. static void perf_output_read_one(struct perf_output_handle *handle,
  2376. struct perf_event *event)
  2377. {
  2378. u64 read_format = event->attr.read_format;
  2379. u64 values[4];
  2380. int n = 0;
  2381. values[n++] = atomic64_read(&event->count);
  2382. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED) {
  2383. values[n++] = event->total_time_enabled +
  2384. atomic64_read(&event->child_total_time_enabled);
  2385. }
  2386. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING) {
  2387. values[n++] = event->total_time_running +
  2388. atomic64_read(&event->child_total_time_running);
  2389. }
  2390. if (read_format & PERF_FORMAT_ID)
  2391. values[n++] = primary_event_id(event);
  2392. perf_output_copy(handle, values, n * sizeof(u64));
  2393. }
  2394. /*
  2395. * XXX PERF_FORMAT_GROUP vs inherited events seems difficult.
  2396. */
  2397. static void perf_output_read_group(struct perf_output_handle *handle,
  2398. struct perf_event *event)
  2399. {
  2400. struct perf_event *leader = event->group_leader, *sub;
  2401. u64 read_format = event->attr.read_format;
  2402. u64 values[5];
  2403. int n = 0;
  2404. values[n++] = 1 + leader->nr_siblings;
  2405. if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
  2406. values[n++] = leader->total_time_enabled;
  2407. if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
  2408. values[n++] = leader->total_time_running;
  2409. if (leader != event)
  2410. leader->pmu->read(leader);
  2411. values[n++] = atomic64_read(&leader->count);
  2412. if (read_format & PERF_FORMAT_ID)
  2413. values[n++] = primary_event_id(leader);
  2414. perf_output_copy(handle, values, n * sizeof(u64));
  2415. list_for_each_entry(sub, &leader->sibling_list, group_entry) {
  2416. n = 0;
  2417. if (sub != event)
  2418. sub->pmu->read(sub);
  2419. values[n++] = atomic64_read(&sub->count);
  2420. if (read_format & PERF_FORMAT_ID)
  2421. values[n++] = primary_event_id(sub);
  2422. perf_output_copy(handle, values, n * sizeof(u64));
  2423. }
  2424. }
  2425. static void perf_output_read(struct perf_output_handle *handle,
  2426. struct perf_event *event)
  2427. {
  2428. if (event->attr.read_format & PERF_FORMAT_GROUP)
  2429. perf_output_read_group(handle, event);
  2430. else
  2431. perf_output_read_one(handle, event);
  2432. }
  2433. void perf_output_sample(struct perf_output_handle *handle,
  2434. struct perf_event_header *header,
  2435. struct perf_sample_data *data,
  2436. struct perf_event *event)
  2437. {
  2438. u64 sample_type = data->type;
  2439. perf_output_put(handle, *header);
  2440. if (sample_type & PERF_SAMPLE_IP)
  2441. perf_output_put(handle, data->ip);
  2442. if (sample_type & PERF_SAMPLE_TID)
  2443. perf_output_put(handle, data->tid_entry);
  2444. if (sample_type & PERF_SAMPLE_TIME)
  2445. perf_output_put(handle, data->time);
  2446. if (sample_type & PERF_SAMPLE_ADDR)
  2447. perf_output_put(handle, data->addr);
  2448. if (sample_type & PERF_SAMPLE_ID)
  2449. perf_output_put(handle, data->id);
  2450. if (sample_type & PERF_SAMPLE_STREAM_ID)
  2451. perf_output_put(handle, data->stream_id);
  2452. if (sample_type & PERF_SAMPLE_CPU)
  2453. perf_output_put(handle, data->cpu_entry);
  2454. if (sample_type & PERF_SAMPLE_PERIOD)
  2455. perf_output_put(handle, data->period);
  2456. if (sample_type & PERF_SAMPLE_READ)
  2457. perf_output_read(handle, event);
  2458. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2459. if (data->callchain) {
  2460. int size = 1;
  2461. if (data->callchain)
  2462. size += data->callchain->nr;
  2463. size *= sizeof(u64);
  2464. perf_output_copy(handle, data->callchain, size);
  2465. } else {
  2466. u64 nr = 0;
  2467. perf_output_put(handle, nr);
  2468. }
  2469. }
  2470. if (sample_type & PERF_SAMPLE_RAW) {
  2471. if (data->raw) {
  2472. perf_output_put(handle, data->raw->size);
  2473. perf_output_copy(handle, data->raw->data,
  2474. data->raw->size);
  2475. } else {
  2476. struct {
  2477. u32 size;
  2478. u32 data;
  2479. } raw = {
  2480. .size = sizeof(u32),
  2481. .data = 0,
  2482. };
  2483. perf_output_put(handle, raw);
  2484. }
  2485. }
  2486. }
  2487. void perf_prepare_sample(struct perf_event_header *header,
  2488. struct perf_sample_data *data,
  2489. struct perf_event *event,
  2490. struct pt_regs *regs)
  2491. {
  2492. u64 sample_type = event->attr.sample_type;
  2493. data->type = sample_type;
  2494. header->type = PERF_RECORD_SAMPLE;
  2495. header->size = sizeof(*header);
  2496. header->misc = 0;
  2497. header->misc |= perf_misc_flags(regs);
  2498. if (sample_type & PERF_SAMPLE_IP) {
  2499. data->ip = perf_instruction_pointer(regs);
  2500. header->size += sizeof(data->ip);
  2501. }
  2502. if (sample_type & PERF_SAMPLE_TID) {
  2503. /* namespace issues */
  2504. data->tid_entry.pid = perf_event_pid(event, current);
  2505. data->tid_entry.tid = perf_event_tid(event, current);
  2506. header->size += sizeof(data->tid_entry);
  2507. }
  2508. if (sample_type & PERF_SAMPLE_TIME) {
  2509. data->time = perf_clock();
  2510. header->size += sizeof(data->time);
  2511. }
  2512. if (sample_type & PERF_SAMPLE_ADDR)
  2513. header->size += sizeof(data->addr);
  2514. if (sample_type & PERF_SAMPLE_ID) {
  2515. data->id = primary_event_id(event);
  2516. header->size += sizeof(data->id);
  2517. }
  2518. if (sample_type & PERF_SAMPLE_STREAM_ID) {
  2519. data->stream_id = event->id;
  2520. header->size += sizeof(data->stream_id);
  2521. }
  2522. if (sample_type & PERF_SAMPLE_CPU) {
  2523. data->cpu_entry.cpu = raw_smp_processor_id();
  2524. data->cpu_entry.reserved = 0;
  2525. header->size += sizeof(data->cpu_entry);
  2526. }
  2527. if (sample_type & PERF_SAMPLE_PERIOD)
  2528. header->size += sizeof(data->period);
  2529. if (sample_type & PERF_SAMPLE_READ)
  2530. header->size += perf_event_read_size(event);
  2531. if (sample_type & PERF_SAMPLE_CALLCHAIN) {
  2532. int size = 1;
  2533. data->callchain = perf_callchain(regs);
  2534. if (data->callchain)
  2535. size += data->callchain->nr;
  2536. header->size += size * sizeof(u64);
  2537. }
  2538. if (sample_type & PERF_SAMPLE_RAW) {
  2539. int size = sizeof(u32);
  2540. if (data->raw)
  2541. size += data->raw->size;
  2542. else
  2543. size += sizeof(u32);
  2544. WARN_ON_ONCE(size & (sizeof(u64)-1));
  2545. header->size += size;
  2546. }
  2547. }
  2548. static void perf_event_output(struct perf_event *event, int nmi,
  2549. struct perf_sample_data *data,
  2550. struct pt_regs *regs)
  2551. {
  2552. struct perf_output_handle handle;
  2553. struct perf_event_header header;
  2554. perf_prepare_sample(&header, data, event, regs);
  2555. if (perf_output_begin(&handle, event, header.size, nmi, 1))
  2556. return;
  2557. perf_output_sample(&handle, &header, data, event);
  2558. perf_output_end(&handle);
  2559. }
  2560. /*
  2561. * read event_id
  2562. */
  2563. struct perf_read_event {
  2564. struct perf_event_header header;
  2565. u32 pid;
  2566. u32 tid;
  2567. };
  2568. static void
  2569. perf_event_read_event(struct perf_event *event,
  2570. struct task_struct *task)
  2571. {
  2572. struct perf_output_handle handle;
  2573. struct perf_read_event read_event = {
  2574. .header = {
  2575. .type = PERF_RECORD_READ,
  2576. .misc = 0,
  2577. .size = sizeof(read_event) + perf_event_read_size(event),
  2578. },
  2579. .pid = perf_event_pid(event, task),
  2580. .tid = perf_event_tid(event, task),
  2581. };
  2582. int ret;
  2583. ret = perf_output_begin(&handle, event, read_event.header.size, 0, 0);
  2584. if (ret)
  2585. return;
  2586. perf_output_put(&handle, read_event);
  2587. perf_output_read(&handle, event);
  2588. perf_output_end(&handle);
  2589. }
  2590. /*
  2591. * task tracking -- fork/exit
  2592. *
  2593. * enabled by: attr.comm | attr.mmap | attr.task
  2594. */
  2595. struct perf_task_event {
  2596. struct task_struct *task;
  2597. struct perf_event_context *task_ctx;
  2598. struct {
  2599. struct perf_event_header header;
  2600. u32 pid;
  2601. u32 ppid;
  2602. u32 tid;
  2603. u32 ptid;
  2604. u64 time;
  2605. } event_id;
  2606. };
  2607. static void perf_event_task_output(struct perf_event *event,
  2608. struct perf_task_event *task_event)
  2609. {
  2610. struct perf_output_handle handle;
  2611. int size;
  2612. struct task_struct *task = task_event->task;
  2613. int ret;
  2614. size = task_event->event_id.header.size;
  2615. ret = perf_output_begin(&handle, event, size, 0, 0);
  2616. if (ret)
  2617. return;
  2618. task_event->event_id.pid = perf_event_pid(event, task);
  2619. task_event->event_id.ppid = perf_event_pid(event, current);
  2620. task_event->event_id.tid = perf_event_tid(event, task);
  2621. task_event->event_id.ptid = perf_event_tid(event, current);
  2622. task_event->event_id.time = perf_clock();
  2623. perf_output_put(&handle, task_event->event_id);
  2624. perf_output_end(&handle);
  2625. }
  2626. static int perf_event_task_match(struct perf_event *event)
  2627. {
  2628. if (event->attr.comm || event->attr.mmap || event->attr.task)
  2629. return 1;
  2630. return 0;
  2631. }
  2632. static void perf_event_task_ctx(struct perf_event_context *ctx,
  2633. struct perf_task_event *task_event)
  2634. {
  2635. struct perf_event *event;
  2636. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2637. return;
  2638. rcu_read_lock();
  2639. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2640. if (perf_event_task_match(event))
  2641. perf_event_task_output(event, task_event);
  2642. }
  2643. rcu_read_unlock();
  2644. }
  2645. static void perf_event_task_event(struct perf_task_event *task_event)
  2646. {
  2647. struct perf_cpu_context *cpuctx;
  2648. struct perf_event_context *ctx = task_event->task_ctx;
  2649. cpuctx = &get_cpu_var(perf_cpu_context);
  2650. perf_event_task_ctx(&cpuctx->ctx, task_event);
  2651. put_cpu_var(perf_cpu_context);
  2652. rcu_read_lock();
  2653. if (!ctx)
  2654. ctx = rcu_dereference(task_event->task->perf_event_ctxp);
  2655. if (ctx)
  2656. perf_event_task_ctx(ctx, task_event);
  2657. rcu_read_unlock();
  2658. }
  2659. static void perf_event_task(struct task_struct *task,
  2660. struct perf_event_context *task_ctx,
  2661. int new)
  2662. {
  2663. struct perf_task_event task_event;
  2664. if (!atomic_read(&nr_comm_events) &&
  2665. !atomic_read(&nr_mmap_events) &&
  2666. !atomic_read(&nr_task_events))
  2667. return;
  2668. task_event = (struct perf_task_event){
  2669. .task = task,
  2670. .task_ctx = task_ctx,
  2671. .event_id = {
  2672. .header = {
  2673. .type = new ? PERF_RECORD_FORK : PERF_RECORD_EXIT,
  2674. .misc = 0,
  2675. .size = sizeof(task_event.event_id),
  2676. },
  2677. /* .pid */
  2678. /* .ppid */
  2679. /* .tid */
  2680. /* .ptid */
  2681. },
  2682. };
  2683. perf_event_task_event(&task_event);
  2684. }
  2685. void perf_event_fork(struct task_struct *task)
  2686. {
  2687. perf_event_task(task, NULL, 1);
  2688. }
  2689. /*
  2690. * comm tracking
  2691. */
  2692. struct perf_comm_event {
  2693. struct task_struct *task;
  2694. char *comm;
  2695. int comm_size;
  2696. struct {
  2697. struct perf_event_header header;
  2698. u32 pid;
  2699. u32 tid;
  2700. } event_id;
  2701. };
  2702. static void perf_event_comm_output(struct perf_event *event,
  2703. struct perf_comm_event *comm_event)
  2704. {
  2705. struct perf_output_handle handle;
  2706. int size = comm_event->event_id.header.size;
  2707. int ret = perf_output_begin(&handle, event, size, 0, 0);
  2708. if (ret)
  2709. return;
  2710. comm_event->event_id.pid = perf_event_pid(event, comm_event->task);
  2711. comm_event->event_id.tid = perf_event_tid(event, comm_event->task);
  2712. perf_output_put(&handle, comm_event->event_id);
  2713. perf_output_copy(&handle, comm_event->comm,
  2714. comm_event->comm_size);
  2715. perf_output_end(&handle);
  2716. }
  2717. static int perf_event_comm_match(struct perf_event *event)
  2718. {
  2719. if (event->attr.comm)
  2720. return 1;
  2721. return 0;
  2722. }
  2723. static void perf_event_comm_ctx(struct perf_event_context *ctx,
  2724. struct perf_comm_event *comm_event)
  2725. {
  2726. struct perf_event *event;
  2727. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2728. return;
  2729. rcu_read_lock();
  2730. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2731. if (perf_event_comm_match(event))
  2732. perf_event_comm_output(event, comm_event);
  2733. }
  2734. rcu_read_unlock();
  2735. }
  2736. static void perf_event_comm_event(struct perf_comm_event *comm_event)
  2737. {
  2738. struct perf_cpu_context *cpuctx;
  2739. struct perf_event_context *ctx;
  2740. unsigned int size;
  2741. char comm[TASK_COMM_LEN];
  2742. memset(comm, 0, sizeof(comm));
  2743. strncpy(comm, comm_event->task->comm, sizeof(comm));
  2744. size = ALIGN(strlen(comm)+1, sizeof(u64));
  2745. comm_event->comm = comm;
  2746. comm_event->comm_size = size;
  2747. comm_event->event_id.header.size = sizeof(comm_event->event_id) + size;
  2748. cpuctx = &get_cpu_var(perf_cpu_context);
  2749. perf_event_comm_ctx(&cpuctx->ctx, comm_event);
  2750. put_cpu_var(perf_cpu_context);
  2751. rcu_read_lock();
  2752. /*
  2753. * doesn't really matter which of the child contexts the
  2754. * events ends up in.
  2755. */
  2756. ctx = rcu_dereference(current->perf_event_ctxp);
  2757. if (ctx)
  2758. perf_event_comm_ctx(ctx, comm_event);
  2759. rcu_read_unlock();
  2760. }
  2761. void perf_event_comm(struct task_struct *task)
  2762. {
  2763. struct perf_comm_event comm_event;
  2764. if (task->perf_event_ctxp)
  2765. perf_event_enable_on_exec(task);
  2766. if (!atomic_read(&nr_comm_events))
  2767. return;
  2768. comm_event = (struct perf_comm_event){
  2769. .task = task,
  2770. /* .comm */
  2771. /* .comm_size */
  2772. .event_id = {
  2773. .header = {
  2774. .type = PERF_RECORD_COMM,
  2775. .misc = 0,
  2776. /* .size */
  2777. },
  2778. /* .pid */
  2779. /* .tid */
  2780. },
  2781. };
  2782. perf_event_comm_event(&comm_event);
  2783. }
  2784. /*
  2785. * mmap tracking
  2786. */
  2787. struct perf_mmap_event {
  2788. struct vm_area_struct *vma;
  2789. const char *file_name;
  2790. int file_size;
  2791. struct {
  2792. struct perf_event_header header;
  2793. u32 pid;
  2794. u32 tid;
  2795. u64 start;
  2796. u64 len;
  2797. u64 pgoff;
  2798. } event_id;
  2799. };
  2800. static void perf_event_mmap_output(struct perf_event *event,
  2801. struct perf_mmap_event *mmap_event)
  2802. {
  2803. struct perf_output_handle handle;
  2804. int size = mmap_event->event_id.header.size;
  2805. int ret = perf_output_begin(&handle, event, size, 0, 0);
  2806. if (ret)
  2807. return;
  2808. mmap_event->event_id.pid = perf_event_pid(event, current);
  2809. mmap_event->event_id.tid = perf_event_tid(event, current);
  2810. perf_output_put(&handle, mmap_event->event_id);
  2811. perf_output_copy(&handle, mmap_event->file_name,
  2812. mmap_event->file_size);
  2813. perf_output_end(&handle);
  2814. }
  2815. static int perf_event_mmap_match(struct perf_event *event,
  2816. struct perf_mmap_event *mmap_event)
  2817. {
  2818. if (event->attr.mmap)
  2819. return 1;
  2820. return 0;
  2821. }
  2822. static void perf_event_mmap_ctx(struct perf_event_context *ctx,
  2823. struct perf_mmap_event *mmap_event)
  2824. {
  2825. struct perf_event *event;
  2826. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  2827. return;
  2828. rcu_read_lock();
  2829. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  2830. if (perf_event_mmap_match(event, mmap_event))
  2831. perf_event_mmap_output(event, mmap_event);
  2832. }
  2833. rcu_read_unlock();
  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. cpuctx = &get_cpu_var(perf_cpu_context);
  2881. perf_event_mmap_ctx(&cpuctx->ctx, mmap_event);
  2882. put_cpu_var(perf_cpu_context);
  2883. rcu_read_lock();
  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. perf_event_output(event, nmi, data, regs);
  3002. return ret;
  3003. }
  3004. int perf_event_overflow(struct perf_event *event, int nmi,
  3005. struct perf_sample_data *data,
  3006. struct pt_regs *regs)
  3007. {
  3008. return __perf_event_overflow(event, nmi, 1, data, regs);
  3009. }
  3010. /*
  3011. * Generic software event infrastructure
  3012. */
  3013. /*
  3014. * We directly increment event->count and keep a second value in
  3015. * event->hw.period_left to count intervals. This period event
  3016. * is kept in the range [-sample_period, 0] so that we can use the
  3017. * sign as trigger.
  3018. */
  3019. static u64 perf_swevent_set_period(struct perf_event *event)
  3020. {
  3021. struct hw_perf_event *hwc = &event->hw;
  3022. u64 period = hwc->last_period;
  3023. u64 nr, offset;
  3024. s64 old, val;
  3025. hwc->last_period = hwc->sample_period;
  3026. again:
  3027. old = val = atomic64_read(&hwc->period_left);
  3028. if (val < 0)
  3029. return 0;
  3030. nr = div64_u64(period + val, period);
  3031. offset = nr * period;
  3032. val -= offset;
  3033. if (atomic64_cmpxchg(&hwc->period_left, old, val) != old)
  3034. goto again;
  3035. return nr;
  3036. }
  3037. static void perf_swevent_overflow(struct perf_event *event,
  3038. int nmi, struct perf_sample_data *data,
  3039. struct pt_regs *regs)
  3040. {
  3041. struct hw_perf_event *hwc = &event->hw;
  3042. int throttle = 0;
  3043. u64 overflow;
  3044. data->period = event->hw.last_period;
  3045. overflow = perf_swevent_set_period(event);
  3046. if (hwc->interrupts == MAX_INTERRUPTS)
  3047. return;
  3048. for (; overflow; overflow--) {
  3049. if (__perf_event_overflow(event, nmi, throttle,
  3050. data, regs)) {
  3051. /*
  3052. * We inhibit the overflow from happening when
  3053. * hwc->interrupts == MAX_INTERRUPTS.
  3054. */
  3055. break;
  3056. }
  3057. throttle = 1;
  3058. }
  3059. }
  3060. static void perf_swevent_unthrottle(struct perf_event *event)
  3061. {
  3062. /*
  3063. * Nothing to do, we already reset hwc->interrupts.
  3064. */
  3065. }
  3066. static void perf_swevent_add(struct perf_event *event, u64 nr,
  3067. int nmi, struct perf_sample_data *data,
  3068. struct pt_regs *regs)
  3069. {
  3070. struct hw_perf_event *hwc = &event->hw;
  3071. atomic64_add(nr, &event->count);
  3072. if (!hwc->sample_period)
  3073. return;
  3074. if (!regs)
  3075. return;
  3076. if (!atomic64_add_negative(nr, &hwc->period_left))
  3077. perf_swevent_overflow(event, nmi, data, regs);
  3078. }
  3079. static int perf_swevent_is_counting(struct perf_event *event)
  3080. {
  3081. /*
  3082. * The event is active, we're good!
  3083. */
  3084. if (event->state == PERF_EVENT_STATE_ACTIVE)
  3085. return 1;
  3086. /*
  3087. * The event is off/error, not counting.
  3088. */
  3089. if (event->state != PERF_EVENT_STATE_INACTIVE)
  3090. return 0;
  3091. /*
  3092. * The event is inactive, if the context is active
  3093. * we're part of a group that didn't make it on the 'pmu',
  3094. * not counting.
  3095. */
  3096. if (event->ctx->is_active)
  3097. return 0;
  3098. /*
  3099. * We're inactive and the context is too, this means the
  3100. * task is scheduled out, we're counting events that happen
  3101. * to us, like migration events.
  3102. */
  3103. return 1;
  3104. }
  3105. static int perf_tp_event_match(struct perf_event *event,
  3106. struct perf_sample_data *data);
  3107. static int perf_swevent_match(struct perf_event *event,
  3108. enum perf_type_id type,
  3109. u32 event_id,
  3110. struct perf_sample_data *data,
  3111. struct pt_regs *regs)
  3112. {
  3113. if (!perf_swevent_is_counting(event))
  3114. return 0;
  3115. if (event->attr.type != type)
  3116. return 0;
  3117. if (event->attr.config != event_id)
  3118. return 0;
  3119. if (regs) {
  3120. if (event->attr.exclude_user && user_mode(regs))
  3121. return 0;
  3122. if (event->attr.exclude_kernel && !user_mode(regs))
  3123. return 0;
  3124. }
  3125. if (event->attr.type == PERF_TYPE_TRACEPOINT &&
  3126. !perf_tp_event_match(event, data))
  3127. return 0;
  3128. return 1;
  3129. }
  3130. static void perf_swevent_ctx_event(struct perf_event_context *ctx,
  3131. enum perf_type_id type,
  3132. u32 event_id, u64 nr, int nmi,
  3133. struct perf_sample_data *data,
  3134. struct pt_regs *regs)
  3135. {
  3136. struct perf_event *event;
  3137. if (system_state != SYSTEM_RUNNING || list_empty(&ctx->event_list))
  3138. return;
  3139. rcu_read_lock();
  3140. list_for_each_entry_rcu(event, &ctx->event_list, event_entry) {
  3141. if (perf_swevent_match(event, type, event_id, data, regs))
  3142. perf_swevent_add(event, nr, nmi, data, regs);
  3143. }
  3144. rcu_read_unlock();
  3145. }
  3146. static int *perf_swevent_recursion_context(struct perf_cpu_context *cpuctx)
  3147. {
  3148. if (in_nmi())
  3149. return &cpuctx->recursion[3];
  3150. if (in_irq())
  3151. return &cpuctx->recursion[2];
  3152. if (in_softirq())
  3153. return &cpuctx->recursion[1];
  3154. return &cpuctx->recursion[0];
  3155. }
  3156. static void do_perf_sw_event(enum perf_type_id type, u32 event_id,
  3157. u64 nr, int nmi,
  3158. struct perf_sample_data *data,
  3159. struct pt_regs *regs)
  3160. {
  3161. struct perf_cpu_context *cpuctx = &get_cpu_var(perf_cpu_context);
  3162. int *recursion = perf_swevent_recursion_context(cpuctx);
  3163. struct perf_event_context *ctx;
  3164. if (*recursion)
  3165. goto out;
  3166. (*recursion)++;
  3167. barrier();
  3168. perf_swevent_ctx_event(&cpuctx->ctx, type, event_id,
  3169. nr, nmi, data, regs);
  3170. rcu_read_lock();
  3171. /*
  3172. * doesn't really matter which of the child contexts the
  3173. * events ends up in.
  3174. */
  3175. ctx = rcu_dereference(current->perf_event_ctxp);
  3176. if (ctx)
  3177. perf_swevent_ctx_event(ctx, type, event_id, nr, nmi, data, regs);
  3178. rcu_read_unlock();
  3179. barrier();
  3180. (*recursion)--;
  3181. out:
  3182. put_cpu_var(perf_cpu_context);
  3183. }
  3184. void __perf_sw_event(u32 event_id, u64 nr, int nmi,
  3185. struct pt_regs *regs, u64 addr)
  3186. {
  3187. struct perf_sample_data data = {
  3188. .addr = addr,
  3189. };
  3190. do_perf_sw_event(PERF_TYPE_SOFTWARE, event_id, nr, nmi,
  3191. &data, regs);
  3192. }
  3193. static void perf_swevent_read(struct perf_event *event)
  3194. {
  3195. }
  3196. static int perf_swevent_enable(struct perf_event *event)
  3197. {
  3198. struct hw_perf_event *hwc = &event->hw;
  3199. if (hwc->sample_period) {
  3200. hwc->last_period = hwc->sample_period;
  3201. perf_swevent_set_period(event);
  3202. }
  3203. return 0;
  3204. }
  3205. static void perf_swevent_disable(struct perf_event *event)
  3206. {
  3207. }
  3208. static const struct pmu perf_ops_generic = {
  3209. .enable = perf_swevent_enable,
  3210. .disable = perf_swevent_disable,
  3211. .read = perf_swevent_read,
  3212. .unthrottle = perf_swevent_unthrottle,
  3213. };
  3214. /*
  3215. * hrtimer based swevent callback
  3216. */
  3217. static enum hrtimer_restart perf_swevent_hrtimer(struct hrtimer *hrtimer)
  3218. {
  3219. enum hrtimer_restart ret = HRTIMER_RESTART;
  3220. struct perf_sample_data data;
  3221. struct pt_regs *regs;
  3222. struct perf_event *event;
  3223. u64 period;
  3224. event = container_of(hrtimer, struct perf_event, hw.hrtimer);
  3225. event->pmu->read(event);
  3226. data.addr = 0;
  3227. regs = get_irq_regs();
  3228. /*
  3229. * In case we exclude kernel IPs or are somehow not in interrupt
  3230. * context, provide the next best thing, the user IP.
  3231. */
  3232. if ((event->attr.exclude_kernel || !regs) &&
  3233. !event->attr.exclude_user)
  3234. regs = task_pt_regs(current);
  3235. if (regs) {
  3236. if (perf_event_overflow(event, 0, &data, regs))
  3237. ret = HRTIMER_NORESTART;
  3238. }
  3239. period = max_t(u64, 10000, event->hw.sample_period);
  3240. hrtimer_forward_now(hrtimer, ns_to_ktime(period));
  3241. return ret;
  3242. }
  3243. /*
  3244. * Software event: cpu wall time clock
  3245. */
  3246. static void cpu_clock_perf_event_update(struct perf_event *event)
  3247. {
  3248. int cpu = raw_smp_processor_id();
  3249. s64 prev;
  3250. u64 now;
  3251. now = cpu_clock(cpu);
  3252. prev = atomic64_read(&event->hw.prev_count);
  3253. atomic64_set(&event->hw.prev_count, now);
  3254. atomic64_add(now - prev, &event->count);
  3255. }
  3256. static int cpu_clock_perf_event_enable(struct perf_event *event)
  3257. {
  3258. struct hw_perf_event *hwc = &event->hw;
  3259. int cpu = raw_smp_processor_id();
  3260. atomic64_set(&hwc->prev_count, cpu_clock(cpu));
  3261. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3262. hwc->hrtimer.function = perf_swevent_hrtimer;
  3263. if (hwc->sample_period) {
  3264. u64 period = max_t(u64, 10000, hwc->sample_period);
  3265. __hrtimer_start_range_ns(&hwc->hrtimer,
  3266. ns_to_ktime(period), 0,
  3267. HRTIMER_MODE_REL, 0);
  3268. }
  3269. return 0;
  3270. }
  3271. static void cpu_clock_perf_event_disable(struct perf_event *event)
  3272. {
  3273. if (event->hw.sample_period)
  3274. hrtimer_cancel(&event->hw.hrtimer);
  3275. cpu_clock_perf_event_update(event);
  3276. }
  3277. static void cpu_clock_perf_event_read(struct perf_event *event)
  3278. {
  3279. cpu_clock_perf_event_update(event);
  3280. }
  3281. static const struct pmu perf_ops_cpu_clock = {
  3282. .enable = cpu_clock_perf_event_enable,
  3283. .disable = cpu_clock_perf_event_disable,
  3284. .read = cpu_clock_perf_event_read,
  3285. };
  3286. /*
  3287. * Software event: task time clock
  3288. */
  3289. static void task_clock_perf_event_update(struct perf_event *event, u64 now)
  3290. {
  3291. u64 prev;
  3292. s64 delta;
  3293. prev = atomic64_xchg(&event->hw.prev_count, now);
  3294. delta = now - prev;
  3295. atomic64_add(delta, &event->count);
  3296. }
  3297. static int task_clock_perf_event_enable(struct perf_event *event)
  3298. {
  3299. struct hw_perf_event *hwc = &event->hw;
  3300. u64 now;
  3301. now = event->ctx->time;
  3302. atomic64_set(&hwc->prev_count, now);
  3303. hrtimer_init(&hwc->hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  3304. hwc->hrtimer.function = perf_swevent_hrtimer;
  3305. if (hwc->sample_period) {
  3306. u64 period = max_t(u64, 10000, hwc->sample_period);
  3307. __hrtimer_start_range_ns(&hwc->hrtimer,
  3308. ns_to_ktime(period), 0,
  3309. HRTIMER_MODE_REL, 0);
  3310. }
  3311. return 0;
  3312. }
  3313. static void task_clock_perf_event_disable(struct perf_event *event)
  3314. {
  3315. if (event->hw.sample_period)
  3316. hrtimer_cancel(&event->hw.hrtimer);
  3317. task_clock_perf_event_update(event, event->ctx->time);
  3318. }
  3319. static void task_clock_perf_event_read(struct perf_event *event)
  3320. {
  3321. u64 time;
  3322. if (!in_nmi()) {
  3323. update_context_time(event->ctx);
  3324. time = event->ctx->time;
  3325. } else {
  3326. u64 now = perf_clock();
  3327. u64 delta = now - event->ctx->timestamp;
  3328. time = event->ctx->time + delta;
  3329. }
  3330. task_clock_perf_event_update(event, time);
  3331. }
  3332. static const struct pmu perf_ops_task_clock = {
  3333. .enable = task_clock_perf_event_enable,
  3334. .disable = task_clock_perf_event_disable,
  3335. .read = task_clock_perf_event_read,
  3336. };
  3337. #ifdef CONFIG_EVENT_PROFILE
  3338. void perf_tp_event(int event_id, u64 addr, u64 count, void *record,
  3339. int entry_size)
  3340. {
  3341. struct perf_raw_record raw = {
  3342. .size = entry_size,
  3343. .data = record,
  3344. };
  3345. struct perf_sample_data data = {
  3346. .addr = addr,
  3347. .raw = &raw,
  3348. };
  3349. struct pt_regs *regs = get_irq_regs();
  3350. if (!regs)
  3351. regs = task_pt_regs(current);
  3352. do_perf_sw_event(PERF_TYPE_TRACEPOINT, event_id, count, 1,
  3353. &data, regs);
  3354. }
  3355. EXPORT_SYMBOL_GPL(perf_tp_event);
  3356. static int perf_tp_event_match(struct perf_event *event,
  3357. struct perf_sample_data *data)
  3358. {
  3359. void *record = data->raw->data;
  3360. if (likely(!event->filter) || filter_match_preds(event->filter, record))
  3361. return 1;
  3362. return 0;
  3363. }
  3364. static void tp_perf_event_destroy(struct perf_event *event)
  3365. {
  3366. ftrace_profile_disable(event->attr.config);
  3367. }
  3368. static const struct pmu *tp_perf_event_init(struct perf_event *event)
  3369. {
  3370. /*
  3371. * Raw tracepoint data is a severe data leak, only allow root to
  3372. * have these.
  3373. */
  3374. if ((event->attr.sample_type & PERF_SAMPLE_RAW) &&
  3375. perf_paranoid_tracepoint_raw() &&
  3376. !capable(CAP_SYS_ADMIN))
  3377. return ERR_PTR(-EPERM);
  3378. if (ftrace_profile_enable(event->attr.config))
  3379. return NULL;
  3380. event->destroy = tp_perf_event_destroy;
  3381. return &perf_ops_generic;
  3382. }
  3383. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3384. {
  3385. char *filter_str;
  3386. int ret;
  3387. if (event->attr.type != PERF_TYPE_TRACEPOINT)
  3388. return -EINVAL;
  3389. filter_str = strndup_user(arg, PAGE_SIZE);
  3390. if (IS_ERR(filter_str))
  3391. return PTR_ERR(filter_str);
  3392. ret = ftrace_profile_set_filter(event, event->attr.config, filter_str);
  3393. kfree(filter_str);
  3394. return ret;
  3395. }
  3396. static void perf_event_free_filter(struct perf_event *event)
  3397. {
  3398. ftrace_profile_free_filter(event);
  3399. }
  3400. #else
  3401. static int perf_tp_event_match(struct perf_event *event,
  3402. struct perf_sample_data *data)
  3403. {
  3404. return 1;
  3405. }
  3406. static const struct pmu *tp_perf_event_init(struct perf_event *event)
  3407. {
  3408. return NULL;
  3409. }
  3410. static int perf_event_set_filter(struct perf_event *event, void __user *arg)
  3411. {
  3412. return -ENOENT;
  3413. }
  3414. static void perf_event_free_filter(struct perf_event *event)
  3415. {
  3416. }
  3417. #endif /* CONFIG_EVENT_PROFILE */
  3418. atomic_t perf_swevent_enabled[PERF_COUNT_SW_MAX];
  3419. static void sw_perf_event_destroy(struct perf_event *event)
  3420. {
  3421. u64 event_id = event->attr.config;
  3422. WARN_ON(event->parent);
  3423. atomic_dec(&perf_swevent_enabled[event_id]);
  3424. }
  3425. static const struct pmu *sw_perf_event_init(struct perf_event *event)
  3426. {
  3427. const struct pmu *pmu = NULL;
  3428. u64 event_id = event->attr.config;
  3429. /*
  3430. * Software events (currently) can't in general distinguish
  3431. * between user, kernel and hypervisor events.
  3432. * However, context switches and cpu migrations are considered
  3433. * to be kernel events, and page faults are never hypervisor
  3434. * events.
  3435. */
  3436. switch (event_id) {
  3437. case PERF_COUNT_SW_CPU_CLOCK:
  3438. pmu = &perf_ops_cpu_clock;
  3439. break;
  3440. case PERF_COUNT_SW_TASK_CLOCK:
  3441. /*
  3442. * If the user instantiates this as a per-cpu event,
  3443. * use the cpu_clock event instead.
  3444. */
  3445. if (event->ctx->task)
  3446. pmu = &perf_ops_task_clock;
  3447. else
  3448. pmu = &perf_ops_cpu_clock;
  3449. break;
  3450. case PERF_COUNT_SW_PAGE_FAULTS:
  3451. case PERF_COUNT_SW_PAGE_FAULTS_MIN:
  3452. case PERF_COUNT_SW_PAGE_FAULTS_MAJ:
  3453. case PERF_COUNT_SW_CONTEXT_SWITCHES:
  3454. case PERF_COUNT_SW_CPU_MIGRATIONS:
  3455. if (!event->parent) {
  3456. atomic_inc(&perf_swevent_enabled[event_id]);
  3457. event->destroy = sw_perf_event_destroy;
  3458. }
  3459. pmu = &perf_ops_generic;
  3460. break;
  3461. }
  3462. return pmu;
  3463. }
  3464. /*
  3465. * Allocate and initialize a event structure
  3466. */
  3467. static struct perf_event *
  3468. perf_event_alloc(struct perf_event_attr *attr,
  3469. int cpu,
  3470. struct perf_event_context *ctx,
  3471. struct perf_event *group_leader,
  3472. struct perf_event *parent_event,
  3473. gfp_t gfpflags)
  3474. {
  3475. const struct pmu *pmu;
  3476. struct perf_event *event;
  3477. struct hw_perf_event *hwc;
  3478. long err;
  3479. event = kzalloc(sizeof(*event), gfpflags);
  3480. if (!event)
  3481. return ERR_PTR(-ENOMEM);
  3482. /*
  3483. * Single events are their own group leaders, with an
  3484. * empty sibling list:
  3485. */
  3486. if (!group_leader)
  3487. group_leader = event;
  3488. mutex_init(&event->child_mutex);
  3489. INIT_LIST_HEAD(&event->child_list);
  3490. INIT_LIST_HEAD(&event->group_entry);
  3491. INIT_LIST_HEAD(&event->event_entry);
  3492. INIT_LIST_HEAD(&event->sibling_list);
  3493. init_waitqueue_head(&event->waitq);
  3494. mutex_init(&event->mmap_mutex);
  3495. event->cpu = cpu;
  3496. event->attr = *attr;
  3497. event->group_leader = group_leader;
  3498. event->pmu = NULL;
  3499. event->ctx = ctx;
  3500. event->oncpu = -1;
  3501. event->parent = parent_event;
  3502. event->ns = get_pid_ns(current->nsproxy->pid_ns);
  3503. event->id = atomic64_inc_return(&perf_event_id);
  3504. event->state = PERF_EVENT_STATE_INACTIVE;
  3505. if (attr->disabled)
  3506. event->state = PERF_EVENT_STATE_OFF;
  3507. pmu = NULL;
  3508. hwc = &event->hw;
  3509. hwc->sample_period = attr->sample_period;
  3510. if (attr->freq && attr->sample_freq)
  3511. hwc->sample_period = 1;
  3512. hwc->last_period = hwc->sample_period;
  3513. atomic64_set(&hwc->period_left, hwc->sample_period);
  3514. /*
  3515. * we currently do not support PERF_FORMAT_GROUP on inherited events
  3516. */
  3517. if (attr->inherit && (attr->read_format & PERF_FORMAT_GROUP))
  3518. goto done;
  3519. switch (attr->type) {
  3520. case PERF_TYPE_RAW:
  3521. case PERF_TYPE_HARDWARE:
  3522. case PERF_TYPE_HW_CACHE:
  3523. pmu = hw_perf_event_init(event);
  3524. break;
  3525. case PERF_TYPE_SOFTWARE:
  3526. pmu = sw_perf_event_init(event);
  3527. break;
  3528. case PERF_TYPE_TRACEPOINT:
  3529. pmu = tp_perf_event_init(event);
  3530. break;
  3531. default:
  3532. break;
  3533. }
  3534. done:
  3535. err = 0;
  3536. if (!pmu)
  3537. err = -EINVAL;
  3538. else if (IS_ERR(pmu))
  3539. err = PTR_ERR(pmu);
  3540. if (err) {
  3541. if (event->ns)
  3542. put_pid_ns(event->ns);
  3543. kfree(event);
  3544. return ERR_PTR(err);
  3545. }
  3546. event->pmu = pmu;
  3547. if (!event->parent) {
  3548. atomic_inc(&nr_events);
  3549. if (event->attr.mmap)
  3550. atomic_inc(&nr_mmap_events);
  3551. if (event->attr.comm)
  3552. atomic_inc(&nr_comm_events);
  3553. if (event->attr.task)
  3554. atomic_inc(&nr_task_events);
  3555. }
  3556. return event;
  3557. }
  3558. static int perf_copy_attr(struct perf_event_attr __user *uattr,
  3559. struct perf_event_attr *attr)
  3560. {
  3561. u32 size;
  3562. int ret;
  3563. if (!access_ok(VERIFY_WRITE, uattr, PERF_ATTR_SIZE_VER0))
  3564. return -EFAULT;
  3565. /*
  3566. * zero the full structure, so that a short copy will be nice.
  3567. */
  3568. memset(attr, 0, sizeof(*attr));
  3569. ret = get_user(size, &uattr->size);
  3570. if (ret)
  3571. return ret;
  3572. if (size > PAGE_SIZE) /* silly large */
  3573. goto err_size;
  3574. if (!size) /* abi compat */
  3575. size = PERF_ATTR_SIZE_VER0;
  3576. if (size < PERF_ATTR_SIZE_VER0)
  3577. goto err_size;
  3578. /*
  3579. * If we're handed a bigger struct than we know of,
  3580. * ensure all the unknown bits are 0 - i.e. new
  3581. * user-space does not rely on any kernel feature
  3582. * extensions we dont know about yet.
  3583. */
  3584. if (size > sizeof(*attr)) {
  3585. unsigned char __user *addr;
  3586. unsigned char __user *end;
  3587. unsigned char val;
  3588. addr = (void __user *)uattr + sizeof(*attr);
  3589. end = (void __user *)uattr + size;
  3590. for (; addr < end; addr++) {
  3591. ret = get_user(val, addr);
  3592. if (ret)
  3593. return ret;
  3594. if (val)
  3595. goto err_size;
  3596. }
  3597. size = sizeof(*attr);
  3598. }
  3599. ret = copy_from_user(attr, uattr, size);
  3600. if (ret)
  3601. return -EFAULT;
  3602. /*
  3603. * If the type exists, the corresponding creation will verify
  3604. * the attr->config.
  3605. */
  3606. if (attr->type >= PERF_TYPE_MAX)
  3607. return -EINVAL;
  3608. if (attr->__reserved_1 || attr->__reserved_2 || attr->__reserved_3)
  3609. return -EINVAL;
  3610. if (attr->sample_type & ~(PERF_SAMPLE_MAX-1))
  3611. return -EINVAL;
  3612. if (attr->read_format & ~(PERF_FORMAT_MAX-1))
  3613. return -EINVAL;
  3614. out:
  3615. return ret;
  3616. err_size:
  3617. put_user(sizeof(*attr), &uattr->size);
  3618. ret = -E2BIG;
  3619. goto out;
  3620. }
  3621. static int perf_event_set_output(struct perf_event *event, int output_fd)
  3622. {
  3623. struct perf_event *output_event = NULL;
  3624. struct file *output_file = NULL;
  3625. struct perf_event *old_output;
  3626. int fput_needed = 0;
  3627. int ret = -EINVAL;
  3628. if (!output_fd)
  3629. goto set;
  3630. output_file = fget_light(output_fd, &fput_needed);
  3631. if (!output_file)
  3632. return -EBADF;
  3633. if (output_file->f_op != &perf_fops)
  3634. goto out;
  3635. output_event = output_file->private_data;
  3636. /* Don't chain output fds */
  3637. if (output_event->output)
  3638. goto out;
  3639. /* Don't set an output fd when we already have an output channel */
  3640. if (event->data)
  3641. goto out;
  3642. atomic_long_inc(&output_file->f_count);
  3643. set:
  3644. mutex_lock(&event->mmap_mutex);
  3645. old_output = event->output;
  3646. rcu_assign_pointer(event->output, output_event);
  3647. mutex_unlock(&event->mmap_mutex);
  3648. if (old_output) {
  3649. /*
  3650. * we need to make sure no existing perf_output_*()
  3651. * is still referencing this event.
  3652. */
  3653. synchronize_rcu();
  3654. fput(old_output->filp);
  3655. }
  3656. ret = 0;
  3657. out:
  3658. fput_light(output_file, fput_needed);
  3659. return ret;
  3660. }
  3661. /**
  3662. * sys_perf_event_open - open a performance event, associate it to a task/cpu
  3663. *
  3664. * @attr_uptr: event_id type attributes for monitoring/sampling
  3665. * @pid: target pid
  3666. * @cpu: target cpu
  3667. * @group_fd: group leader event fd
  3668. */
  3669. SYSCALL_DEFINE5(perf_event_open,
  3670. struct perf_event_attr __user *, attr_uptr,
  3671. pid_t, pid, int, cpu, int, group_fd, unsigned long, flags)
  3672. {
  3673. struct perf_event *event, *group_leader;
  3674. struct perf_event_attr attr;
  3675. struct perf_event_context *ctx;
  3676. struct file *event_file = NULL;
  3677. struct file *group_file = NULL;
  3678. int fput_needed = 0;
  3679. int fput_needed2 = 0;
  3680. int err;
  3681. /* for future expandability... */
  3682. if (flags & ~(PERF_FLAG_FD_NO_GROUP | PERF_FLAG_FD_OUTPUT))
  3683. return -EINVAL;
  3684. err = perf_copy_attr(attr_uptr, &attr);
  3685. if (err)
  3686. return err;
  3687. if (!attr.exclude_kernel) {
  3688. if (perf_paranoid_kernel() && !capable(CAP_SYS_ADMIN))
  3689. return -EACCES;
  3690. }
  3691. if (attr.freq) {
  3692. if (attr.sample_freq > sysctl_perf_event_sample_rate)
  3693. return -EINVAL;
  3694. }
  3695. /*
  3696. * Get the target context (task or percpu):
  3697. */
  3698. ctx = find_get_context(pid, cpu);
  3699. if (IS_ERR(ctx))
  3700. return PTR_ERR(ctx);
  3701. /*
  3702. * Look up the group leader (we will attach this event to it):
  3703. */
  3704. group_leader = NULL;
  3705. if (group_fd != -1 && !(flags & PERF_FLAG_FD_NO_GROUP)) {
  3706. err = -EINVAL;
  3707. group_file = fget_light(group_fd, &fput_needed);
  3708. if (!group_file)
  3709. goto err_put_context;
  3710. if (group_file->f_op != &perf_fops)
  3711. goto err_put_context;
  3712. group_leader = group_file->private_data;
  3713. /*
  3714. * Do not allow a recursive hierarchy (this new sibling
  3715. * becoming part of another group-sibling):
  3716. */
  3717. if (group_leader->group_leader != group_leader)
  3718. goto err_put_context;
  3719. /*
  3720. * Do not allow to attach to a group in a different
  3721. * task or CPU context:
  3722. */
  3723. if (group_leader->ctx != ctx)
  3724. goto err_put_context;
  3725. /*
  3726. * Only a group leader can be exclusive or pinned
  3727. */
  3728. if (attr.exclusive || attr.pinned)
  3729. goto err_put_context;
  3730. }
  3731. event = perf_event_alloc(&attr, cpu, ctx, group_leader,
  3732. NULL, GFP_KERNEL);
  3733. err = PTR_ERR(event);
  3734. if (IS_ERR(event))
  3735. goto err_put_context;
  3736. err = anon_inode_getfd("[perf_event]", &perf_fops, event, 0);
  3737. if (err < 0)
  3738. goto err_free_put_context;
  3739. event_file = fget_light(err, &fput_needed2);
  3740. if (!event_file)
  3741. goto err_free_put_context;
  3742. if (flags & PERF_FLAG_FD_OUTPUT) {
  3743. err = perf_event_set_output(event, group_fd);
  3744. if (err)
  3745. goto err_fput_free_put_context;
  3746. }
  3747. event->filp = event_file;
  3748. WARN_ON_ONCE(ctx->parent_ctx);
  3749. mutex_lock(&ctx->mutex);
  3750. perf_install_in_context(ctx, event, cpu);
  3751. ++ctx->generation;
  3752. mutex_unlock(&ctx->mutex);
  3753. event->owner = current;
  3754. get_task_struct(current);
  3755. mutex_lock(&current->perf_event_mutex);
  3756. list_add_tail(&event->owner_entry, &current->perf_event_list);
  3757. mutex_unlock(&current->perf_event_mutex);
  3758. err_fput_free_put_context:
  3759. fput_light(event_file, fput_needed2);
  3760. err_free_put_context:
  3761. if (err < 0)
  3762. kfree(event);
  3763. err_put_context:
  3764. if (err < 0)
  3765. put_ctx(ctx);
  3766. fput_light(group_file, fput_needed);
  3767. return err;
  3768. }
  3769. /*
  3770. * inherit a event from parent task to child task:
  3771. */
  3772. static struct perf_event *
  3773. inherit_event(struct perf_event *parent_event,
  3774. struct task_struct *parent,
  3775. struct perf_event_context *parent_ctx,
  3776. struct task_struct *child,
  3777. struct perf_event *group_leader,
  3778. struct perf_event_context *child_ctx)
  3779. {
  3780. struct perf_event *child_event;
  3781. /*
  3782. * Instead of creating recursive hierarchies of events,
  3783. * we link inherited events back to the original parent,
  3784. * which has a filp for sure, which we use as the reference
  3785. * count:
  3786. */
  3787. if (parent_event->parent)
  3788. parent_event = parent_event->parent;
  3789. child_event = perf_event_alloc(&parent_event->attr,
  3790. parent_event->cpu, child_ctx,
  3791. group_leader, parent_event,
  3792. GFP_KERNEL);
  3793. if (IS_ERR(child_event))
  3794. return child_event;
  3795. get_ctx(child_ctx);
  3796. /*
  3797. * Make the child state follow the state of the parent event,
  3798. * not its attr.disabled bit. We hold the parent's mutex,
  3799. * so we won't race with perf_event_{en, dis}able_family.
  3800. */
  3801. if (parent_event->state >= PERF_EVENT_STATE_INACTIVE)
  3802. child_event->state = PERF_EVENT_STATE_INACTIVE;
  3803. else
  3804. child_event->state = PERF_EVENT_STATE_OFF;
  3805. if (parent_event->attr.freq)
  3806. child_event->hw.sample_period = parent_event->hw.sample_period;
  3807. /*
  3808. * Link it up in the child's context:
  3809. */
  3810. add_event_to_ctx(child_event, child_ctx);
  3811. /*
  3812. * Get a reference to the parent filp - we will fput it
  3813. * when the child event exits. This is safe to do because
  3814. * we are in the parent and we know that the filp still
  3815. * exists and has a nonzero count:
  3816. */
  3817. atomic_long_inc(&parent_event->filp->f_count);
  3818. /*
  3819. * Link this into the parent event's child list
  3820. */
  3821. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  3822. mutex_lock(&parent_event->child_mutex);
  3823. list_add_tail(&child_event->child_list, &parent_event->child_list);
  3824. mutex_unlock(&parent_event->child_mutex);
  3825. return child_event;
  3826. }
  3827. static int inherit_group(struct perf_event *parent_event,
  3828. struct task_struct *parent,
  3829. struct perf_event_context *parent_ctx,
  3830. struct task_struct *child,
  3831. struct perf_event_context *child_ctx)
  3832. {
  3833. struct perf_event *leader;
  3834. struct perf_event *sub;
  3835. struct perf_event *child_ctr;
  3836. leader = inherit_event(parent_event, parent, parent_ctx,
  3837. child, NULL, child_ctx);
  3838. if (IS_ERR(leader))
  3839. return PTR_ERR(leader);
  3840. list_for_each_entry(sub, &parent_event->sibling_list, group_entry) {
  3841. child_ctr = inherit_event(sub, parent, parent_ctx,
  3842. child, leader, child_ctx);
  3843. if (IS_ERR(child_ctr))
  3844. return PTR_ERR(child_ctr);
  3845. }
  3846. return 0;
  3847. }
  3848. static void sync_child_event(struct perf_event *child_event,
  3849. struct task_struct *child)
  3850. {
  3851. struct perf_event *parent_event = child_event->parent;
  3852. u64 child_val;
  3853. if (child_event->attr.inherit_stat)
  3854. perf_event_read_event(child_event, child);
  3855. child_val = atomic64_read(&child_event->count);
  3856. /*
  3857. * Add back the child's count to the parent's count:
  3858. */
  3859. atomic64_add(child_val, &parent_event->count);
  3860. atomic64_add(child_event->total_time_enabled,
  3861. &parent_event->child_total_time_enabled);
  3862. atomic64_add(child_event->total_time_running,
  3863. &parent_event->child_total_time_running);
  3864. /*
  3865. * Remove this event from the parent's list
  3866. */
  3867. WARN_ON_ONCE(parent_event->ctx->parent_ctx);
  3868. mutex_lock(&parent_event->child_mutex);
  3869. list_del_init(&child_event->child_list);
  3870. mutex_unlock(&parent_event->child_mutex);
  3871. /*
  3872. * Release the parent event, if this was the last
  3873. * reference to it.
  3874. */
  3875. fput(parent_event->filp);
  3876. }
  3877. static void
  3878. __perf_event_exit_task(struct perf_event *child_event,
  3879. struct perf_event_context *child_ctx,
  3880. struct task_struct *child)
  3881. {
  3882. struct perf_event *parent_event;
  3883. update_event_times(child_event);
  3884. perf_event_remove_from_context(child_event);
  3885. parent_event = child_event->parent;
  3886. /*
  3887. * It can happen that parent exits first, and has events
  3888. * that are still around due to the child reference. These
  3889. * events need to be zapped - but otherwise linger.
  3890. */
  3891. if (parent_event) {
  3892. sync_child_event(child_event, child);
  3893. free_event(child_event);
  3894. }
  3895. }
  3896. /*
  3897. * When a child task exits, feed back event values to parent events.
  3898. */
  3899. void perf_event_exit_task(struct task_struct *child)
  3900. {
  3901. struct perf_event *child_event, *tmp;
  3902. struct perf_event_context *child_ctx;
  3903. unsigned long flags;
  3904. if (likely(!child->perf_event_ctxp)) {
  3905. perf_event_task(child, NULL, 0);
  3906. return;
  3907. }
  3908. local_irq_save(flags);
  3909. /*
  3910. * We can't reschedule here because interrupts are disabled,
  3911. * and either child is current or it is a task that can't be
  3912. * scheduled, so we are now safe from rescheduling changing
  3913. * our context.
  3914. */
  3915. child_ctx = child->perf_event_ctxp;
  3916. __perf_event_task_sched_out(child_ctx);
  3917. /*
  3918. * Take the context lock here so that if find_get_context is
  3919. * reading child->perf_event_ctxp, we wait until it has
  3920. * incremented the context's refcount before we do put_ctx below.
  3921. */
  3922. spin_lock(&child_ctx->lock);
  3923. child->perf_event_ctxp = NULL;
  3924. /*
  3925. * If this context is a clone; unclone it so it can't get
  3926. * swapped to another process while we're removing all
  3927. * the events from it.
  3928. */
  3929. unclone_ctx(child_ctx);
  3930. spin_unlock_irqrestore(&child_ctx->lock, flags);
  3931. /*
  3932. * Report the task dead after unscheduling the events so that we
  3933. * won't get any samples after PERF_RECORD_EXIT. We can however still
  3934. * get a few PERF_RECORD_READ events.
  3935. */
  3936. perf_event_task(child, child_ctx, 0);
  3937. /*
  3938. * We can recurse on the same lock type through:
  3939. *
  3940. * __perf_event_exit_task()
  3941. * sync_child_event()
  3942. * fput(parent_event->filp)
  3943. * perf_release()
  3944. * mutex_lock(&ctx->mutex)
  3945. *
  3946. * But since its the parent context it won't be the same instance.
  3947. */
  3948. mutex_lock_nested(&child_ctx->mutex, SINGLE_DEPTH_NESTING);
  3949. again:
  3950. list_for_each_entry_safe(child_event, tmp, &child_ctx->group_list,
  3951. group_entry)
  3952. __perf_event_exit_task(child_event, child_ctx, child);
  3953. /*
  3954. * If the last event was a group event, it will have appended all
  3955. * its siblings to the list, but we obtained 'tmp' before that which
  3956. * will still point to the list head terminating the iteration.
  3957. */
  3958. if (!list_empty(&child_ctx->group_list))
  3959. goto again;
  3960. mutex_unlock(&child_ctx->mutex);
  3961. put_ctx(child_ctx);
  3962. }
  3963. /*
  3964. * free an unexposed, unused context as created by inheritance by
  3965. * init_task below, used by fork() in case of fail.
  3966. */
  3967. void perf_event_free_task(struct task_struct *task)
  3968. {
  3969. struct perf_event_context *ctx = task->perf_event_ctxp;
  3970. struct perf_event *event, *tmp;
  3971. if (!ctx)
  3972. return;
  3973. mutex_lock(&ctx->mutex);
  3974. again:
  3975. list_for_each_entry_safe(event, tmp, &ctx->group_list, group_entry) {
  3976. struct perf_event *parent = event->parent;
  3977. if (WARN_ON_ONCE(!parent))
  3978. continue;
  3979. mutex_lock(&parent->child_mutex);
  3980. list_del_init(&event->child_list);
  3981. mutex_unlock(&parent->child_mutex);
  3982. fput(parent->filp);
  3983. list_del_event(event, ctx);
  3984. free_event(event);
  3985. }
  3986. if (!list_empty(&ctx->group_list))
  3987. goto again;
  3988. mutex_unlock(&ctx->mutex);
  3989. put_ctx(ctx);
  3990. }
  3991. /*
  3992. * Initialize the perf_event context in task_struct
  3993. */
  3994. int perf_event_init_task(struct task_struct *child)
  3995. {
  3996. struct perf_event_context *child_ctx, *parent_ctx;
  3997. struct perf_event_context *cloned_ctx;
  3998. struct perf_event *event;
  3999. struct task_struct *parent = current;
  4000. int inherited_all = 1;
  4001. int ret = 0;
  4002. child->perf_event_ctxp = NULL;
  4003. mutex_init(&child->perf_event_mutex);
  4004. INIT_LIST_HEAD(&child->perf_event_list);
  4005. if (likely(!parent->perf_event_ctxp))
  4006. return 0;
  4007. /*
  4008. * This is executed from the parent task context, so inherit
  4009. * events that have been marked for cloning.
  4010. * First allocate and initialize a context for the child.
  4011. */
  4012. child_ctx = kmalloc(sizeof(struct perf_event_context), GFP_KERNEL);
  4013. if (!child_ctx)
  4014. return -ENOMEM;
  4015. __perf_event_init_context(child_ctx, child);
  4016. child->perf_event_ctxp = child_ctx;
  4017. get_task_struct(child);
  4018. /*
  4019. * If the parent's context is a clone, pin it so it won't get
  4020. * swapped under us.
  4021. */
  4022. parent_ctx = perf_pin_task_context(parent);
  4023. /*
  4024. * No need to check if parent_ctx != NULL here; since we saw
  4025. * it non-NULL earlier, the only reason for it to become NULL
  4026. * is if we exit, and since we're currently in the middle of
  4027. * a fork we can't be exiting at the same time.
  4028. */
  4029. /*
  4030. * Lock the parent list. No need to lock the child - not PID
  4031. * hashed yet and not running, so nobody can access it.
  4032. */
  4033. mutex_lock(&parent_ctx->mutex);
  4034. /*
  4035. * We dont have to disable NMIs - we are only looking at
  4036. * the list, not manipulating it:
  4037. */
  4038. list_for_each_entry(event, &parent_ctx->group_list, group_entry) {
  4039. if (!event->attr.inherit) {
  4040. inherited_all = 0;
  4041. continue;
  4042. }
  4043. ret = inherit_group(event, parent, parent_ctx,
  4044. child, child_ctx);
  4045. if (ret) {
  4046. inherited_all = 0;
  4047. break;
  4048. }
  4049. }
  4050. if (inherited_all) {
  4051. /*
  4052. * Mark the child context as a clone of the parent
  4053. * context, or of whatever the parent is a clone of.
  4054. * Note that if the parent is a clone, it could get
  4055. * uncloned at any point, but that doesn't matter
  4056. * because the list of events and the generation
  4057. * count can't have changed since we took the mutex.
  4058. */
  4059. cloned_ctx = rcu_dereference(parent_ctx->parent_ctx);
  4060. if (cloned_ctx) {
  4061. child_ctx->parent_ctx = cloned_ctx;
  4062. child_ctx->parent_gen = parent_ctx->parent_gen;
  4063. } else {
  4064. child_ctx->parent_ctx = parent_ctx;
  4065. child_ctx->parent_gen = parent_ctx->generation;
  4066. }
  4067. get_ctx(child_ctx->parent_ctx);
  4068. }
  4069. mutex_unlock(&parent_ctx->mutex);
  4070. perf_unpin_context(parent_ctx);
  4071. return ret;
  4072. }
  4073. static void __cpuinit perf_event_init_cpu(int cpu)
  4074. {
  4075. struct perf_cpu_context *cpuctx;
  4076. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4077. __perf_event_init_context(&cpuctx->ctx, NULL);
  4078. spin_lock(&perf_resource_lock);
  4079. cpuctx->max_pertask = perf_max_events - perf_reserved_percpu;
  4080. spin_unlock(&perf_resource_lock);
  4081. hw_perf_event_setup(cpu);
  4082. }
  4083. #ifdef CONFIG_HOTPLUG_CPU
  4084. static void __perf_event_exit_cpu(void *info)
  4085. {
  4086. struct perf_cpu_context *cpuctx = &__get_cpu_var(perf_cpu_context);
  4087. struct perf_event_context *ctx = &cpuctx->ctx;
  4088. struct perf_event *event, *tmp;
  4089. list_for_each_entry_safe(event, tmp, &ctx->group_list, group_entry)
  4090. __perf_event_remove_from_context(event);
  4091. }
  4092. static void perf_event_exit_cpu(int cpu)
  4093. {
  4094. struct perf_cpu_context *cpuctx = &per_cpu(perf_cpu_context, cpu);
  4095. struct perf_event_context *ctx = &cpuctx->ctx;
  4096. mutex_lock(&ctx->mutex);
  4097. smp_call_function_single(cpu, __perf_event_exit_cpu, NULL, 1);
  4098. mutex_unlock(&ctx->mutex);
  4099. }
  4100. #else
  4101. static inline void perf_event_exit_cpu(int cpu) { }
  4102. #endif
  4103. static int __cpuinit
  4104. perf_cpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
  4105. {
  4106. unsigned int cpu = (long)hcpu;
  4107. switch (action) {
  4108. case CPU_UP_PREPARE:
  4109. case CPU_UP_PREPARE_FROZEN:
  4110. perf_event_init_cpu(cpu);
  4111. break;
  4112. case CPU_ONLINE:
  4113. case CPU_ONLINE_FROZEN:
  4114. hw_perf_event_setup_online(cpu);
  4115. break;
  4116. case CPU_DOWN_PREPARE:
  4117. case CPU_DOWN_PREPARE_FROZEN:
  4118. perf_event_exit_cpu(cpu);
  4119. break;
  4120. default:
  4121. break;
  4122. }
  4123. return NOTIFY_OK;
  4124. }
  4125. /*
  4126. * This has to have a higher priority than migration_notifier in sched.c.
  4127. */
  4128. static struct notifier_block __cpuinitdata perf_cpu_nb = {
  4129. .notifier_call = perf_cpu_notify,
  4130. .priority = 20,
  4131. };
  4132. void __init perf_event_init(void)
  4133. {
  4134. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_UP_PREPARE,
  4135. (void *)(long)smp_processor_id());
  4136. perf_cpu_notify(&perf_cpu_nb, (unsigned long)CPU_ONLINE,
  4137. (void *)(long)smp_processor_id());
  4138. register_cpu_notifier(&perf_cpu_nb);
  4139. }
  4140. static ssize_t perf_show_reserve_percpu(struct sysdev_class *class, char *buf)
  4141. {
  4142. return sprintf(buf, "%d\n", perf_reserved_percpu);
  4143. }
  4144. static ssize_t
  4145. perf_set_reserve_percpu(struct sysdev_class *class,
  4146. const char *buf,
  4147. size_t count)
  4148. {
  4149. struct perf_cpu_context *cpuctx;
  4150. unsigned long val;
  4151. int err, cpu, mpt;
  4152. err = strict_strtoul(buf, 10, &val);
  4153. if (err)
  4154. return err;
  4155. if (val > perf_max_events)
  4156. return -EINVAL;
  4157. spin_lock(&perf_resource_lock);
  4158. perf_reserved_percpu = val;
  4159. for_each_online_cpu(cpu) {
  4160. cpuctx = &per_cpu(perf_cpu_context, cpu);
  4161. spin_lock_irq(&cpuctx->ctx.lock);
  4162. mpt = min(perf_max_events - cpuctx->ctx.nr_events,
  4163. perf_max_events - perf_reserved_percpu);
  4164. cpuctx->max_pertask = mpt;
  4165. spin_unlock_irq(&cpuctx->ctx.lock);
  4166. }
  4167. spin_unlock(&perf_resource_lock);
  4168. return count;
  4169. }
  4170. static ssize_t perf_show_overcommit(struct sysdev_class *class, char *buf)
  4171. {
  4172. return sprintf(buf, "%d\n", perf_overcommit);
  4173. }
  4174. static ssize_t
  4175. perf_set_overcommit(struct sysdev_class *class, const char *buf, size_t count)
  4176. {
  4177. unsigned long val;
  4178. int err;
  4179. err = strict_strtoul(buf, 10, &val);
  4180. if (err)
  4181. return err;
  4182. if (val > 1)
  4183. return -EINVAL;
  4184. spin_lock(&perf_resource_lock);
  4185. perf_overcommit = val;
  4186. spin_unlock(&perf_resource_lock);
  4187. return count;
  4188. }
  4189. static SYSDEV_CLASS_ATTR(
  4190. reserve_percpu,
  4191. 0644,
  4192. perf_show_reserve_percpu,
  4193. perf_set_reserve_percpu
  4194. );
  4195. static SYSDEV_CLASS_ATTR(
  4196. overcommit,
  4197. 0644,
  4198. perf_show_overcommit,
  4199. perf_set_overcommit
  4200. );
  4201. static struct attribute *perfclass_attrs[] = {
  4202. &attr_reserve_percpu.attr,
  4203. &attr_overcommit.attr,
  4204. NULL
  4205. };
  4206. static struct attribute_group perfclass_attr_group = {
  4207. .attrs = perfclass_attrs,
  4208. .name = "perf_events",
  4209. };
  4210. static int __init perf_event_sysfs_init(void)
  4211. {
  4212. return sysfs_create_group(&cpu_sysdev_class.kset.kobj,
  4213. &perfclass_attr_group);
  4214. }
  4215. device_initcall(perf_event_sysfs_init);