perf_event.c 121 KB

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