perf_event.c 121 KB

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